@@ -9,6 +9,11 @@ import util.visitor;
99
1010import llvm.c.core;
1111
12+ struct ConstantData {
13+ private :
14+ LLVMValueRef[string ] stringLiterals;
15+ }
16+
1217struct ConstantGen {
1318 private CodeGen pass;
1419 alias pass this ;
@@ -17,6 +22,14 @@ struct ConstantGen {
1722 this .pass = pass;
1823 }
1924
25+ // XXX: lack of multiple alias this, so we do it automanually.
26+ private {
27+ @property
28+ ref LLVMValueRef[string ] stringLiterals () {
29+ return pass.constantData.stringLiterals;
30+ }
31+ }
32+
2033 LLVMValueRef visit (Constant c) {
2134 return this .dispatch(c);
2235 }
@@ -59,6 +72,40 @@ struct ConstantGen {
5972 return buildCString (cs.value);
6073 }
6174
75+ private auto buildStringConstant (string str)
76+ in (str.length <= uint .max, " string length must be < uint.max" ) {
77+ return stringLiterals.get (str, stringLiterals[str] = {
78+ auto charArray =
79+ LLVMConstStringInContext(llvmCtx, str.ptr,
80+ cast (uint ) str.length, true );
81+
82+ auto type = LLVMTypeOf(charArray);
83+ auto globalVar = LLVMAddGlobal(dmodule, type, " .str" );
84+ LLVMSetInitializer(globalVar, charArray);
85+ LLVMSetLinkage(globalVar, LLVMLinkage.Private);
86+ LLVMSetGlobalConstant(globalVar, true );
87+ LLVMSetUnnamedAddr(globalVar, true );
88+
89+ auto zero = LLVMConstInt(i32, 0 , true );
90+ LLVMValueRef[2 ] indices = [zero, zero];
91+ return LLVMConstInBoundsGEP2 (type, globalVar, indices.ptr,
92+ indices.length);
93+ }());
94+ }
95+
96+ auto buildCString (string str) {
97+ import std.string ;
98+ auto cstr = str.toStringz()[0 .. str.length + 1 ];
99+ return buildStringConstant (cstr);
100+ }
101+
102+ auto buildDString (string str) {
103+ LLVMValueRef[2 ] slice =
104+ [LLVMConstInt(i64, str.length, false ), buildStringConstant(str)];
105+ return
106+ LLVMConstStructInContext(llvmCtx, slice.ptr, slice.length, false );
107+ }
108+
62109 // XXX: This should be removed at some point, but to ease transition.
63110 LLVMValueRef visit (Expression e) {
64111 if (auto ce = cast (CompileTimeExpression) e) {
0 commit comments