authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-11-24 03:06:10-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-11-24 03:06:10-07:00
loge112818e25b710199f5c757c17717499356d516e
tree40958c73fa1e08a594d7d9bad4b6ab54057cccb6
parent4bbc074dd7d78f83c57103b22f95967f0936e904

codegen: fix param type of const strings


2 files changed, 15 insertions(+), 4 deletions(-)

README.md-1
......@@ -41,7 +41,6 @@ readable, safe, optimal, and concise code to solve any computing problem.
4141## Roadmap
4242
4343 * Hello, world.
44 - Code Gen
4544 - Produce .o file.
4645 * Produce executable file instead of .o file.
4746 * Add debugging symbols.
src/codegen.cpp+15-3
......@@ -4,6 +4,7 @@
44#include <stdio.h>
55
66#include <llvm-c/Core.h>
7#include <llvm-c/Analysis.h>
78
89struct FnTableEntry {
910 LLVMValueRef fn_value;
......@@ -235,9 +236,10 @@ static LLVMValueRef find_or_create_string(CodeGen *g, Buf *str) {
235236 }
236237 LLVMValueRef text = LLVMConstString(buf_ptr(str), buf_len(str), false);
237238 LLVMValueRef global_value = LLVMAddGlobal(g->mod, LLVMTypeOf(text), "");
238 LLVMSetLinkage(global_value, LLVMInternalLinkage);
239 LLVMSetLinkage(global_value, LLVMPrivateLinkage);
239240 LLVMSetInitializer(global_value, text);
240241 LLVMSetGlobalConstant(global_value, true);
242 LLVMSetUnnamedAddr(global_value, true);
241243 g->str_table.put(str, global_value);
242244
243245 return global_value;
......@@ -257,8 +259,15 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *expr_node) {
257259 case AstNodeExpressionTypeString:
258260 {
259261 Buf *str = &expr_node->data.expression.data.string;
260 fprintf(stderr, "str = '%s'\n", buf_ptr(str));
261 return find_or_create_string(g, str);
262 LLVMValueRef str_val = find_or_create_string(g, str);
263 LLVMValueRef indices[] = {
264 LLVMConstInt(LLVMInt32Type(), 0, false),
265 LLVMConstInt(LLVMInt32Type(), 0, false)
266 };
267 LLVMValueRef ptr_val = LLVMBuildInBoundsGEP(g->builder, str_val,
268 indices, 2, "");
269
270 return ptr_val;
262271 }
263272 case AstNodeExpressionTypeFnCall:
264273 return gen_fn_call(g, expr_node->data.expression.data.fn_call);
......@@ -322,6 +331,9 @@ void code_gen(CodeGen *g) {
322331 }
323332
324333 LLVMDumpModule(g->mod);
334
335 char *error = nullptr;
336 LLVMVerifyModule(g->mod, LLVMAbortProcessAction, &error);
325337}
326338
327339ZigList<ErrorMsg> *codegen_error_messages(CodeGen *g) {