Skip to content

Commit 39f39ba

Browse files
committed
Move string literal generation to ConstantGen.
1 parent 38d56b2 commit 39f39ba

File tree

3 files changed

+57
-39
lines changed

3 files changed

+57
-39
lines changed

src/d/llvm/codegen.d

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ final class CodeGen {
5151
import d.llvm.runtime;
5252
RuntimeData runtimeData;
5353

54-
private LLVMValueRef[string] stringLiterals;
54+
import d.llvm.constant;
55+
ConstantData constantData;
5556

5657
import d.llvm.statement;
5758
StatementGenData statementGenData;
@@ -136,40 +137,6 @@ final class CodeGen {
136137
return m;
137138
}
138139

139-
private auto buildStringConstant(string str)
140-
in(str.length <= uint.max, "string length must be < uint.max") {
141-
return stringLiterals.get(str, stringLiterals[str] = {
142-
auto charArray =
143-
LLVMConstStringInContext(llvmCtx, str.ptr,
144-
cast(uint) str.length, true);
145-
146-
auto type = LLVMTypeOf(charArray);
147-
auto globalVar = LLVMAddGlobal(dmodule, type, ".str");
148-
LLVMSetInitializer(globalVar, charArray);
149-
LLVMSetLinkage(globalVar, LLVMLinkage.Private);
150-
LLVMSetGlobalConstant(globalVar, true);
151-
LLVMSetUnnamedAddr(globalVar, true);
152-
153-
auto zero = LLVMConstInt(i32, 0, true);
154-
LLVMValueRef[2] indices = [zero, zero];
155-
return LLVMConstInBoundsGEP2(type, globalVar, indices.ptr,
156-
indices.length);
157-
}());
158-
}
159-
160-
auto buildCString(string str) {
161-
import std.string;
162-
auto cstr = str.toStringz()[0 .. str.length + 1];
163-
return buildStringConstant(cstr);
164-
}
165-
166-
auto buildDString(string str) {
167-
LLVMValueRef[2] slice =
168-
[LLVMConstInt(i64, str.length, false), buildStringConstant(str)];
169-
return
170-
LLVMConstStructInContext(llvmCtx, slice.ptr, slice.length, false);
171-
}
172-
173140
auto checkModule() {
174141
char* errorPtr;
175142
if (!LLVMVerifyModule(dmodule, LLVMVerifierFailureAction.ReturnStatus,

src/d/llvm/constant.d

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import util.visitor;
99

1010
import llvm.c.core;
1111

12+
struct ConstantData {
13+
private:
14+
LLVMValueRef[string] stringLiterals;
15+
}
16+
1217
struct 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) {

src/d/llvm/runtime.d

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,11 @@ struct RuntimeGen {
129129
auto genHalt(Location location, LLVMValueRef message) {
130130
auto floc = location.getFullLocation(context);
131131

132+
import d.llvm.constant;
133+
auto str = ConstantGen(pass.pass)
134+
.buildDString(floc.getSource().getFileName().toString());
132135
LLVMValueRef[3] args =
133-
[message, buildDString(floc.getSource().getFileName().toString()),
134-
LLVMConstInt(i32, floc.getStartLineNumber(), false), ];
136+
[message, str, LLVMConstInt(i32, floc.getStartLineNumber(), false)];
135137

136138
return message
137139
? ExpressionGen(pass).callGlobal(getAssertFailMsgFunction(), args)
@@ -155,9 +157,11 @@ struct RuntimeGen {
155157
auto genArrayOutOfBounds(Location location) {
156158
auto floc = location.getFullLocation(context);
157159

160+
import d.llvm.constant;
161+
auto str = ConstantGen(pass.pass)
162+
.buildDString(floc.getSource().getFileName().toString());
158163
LLVMValueRef[2] args =
159-
[buildDString(floc.getSource().getFileName().toString()),
160-
LLVMConstInt(i32, floc.getStartLineNumber(), false), ];
164+
[str, LLVMConstInt(i32, floc.getStartLineNumber(), false)];
161165

162166
return
163167
ExpressionGen(pass).callGlobal(getArrayOutOfBoundsFunction(), args);

0 commit comments

Comments
 (0)