| ... | @@ -4,6 +4,7 @@ | ... | @@ -4,6 +4,7 @@ |
| 4 | #include <stdio.h> | 4 | #include <stdio.h> |
| 5 | | 5 | |
| 6 | #include <llvm-c/Core.h> | 6 | #include <llvm-c/Core.h> |
| | 7 | #include <llvm-c/Analysis.h> |
| 7 | | 8 | |
| 8 | struct FnTableEntry { | 9 | struct FnTableEntry { |
| 9 | LLVMValueRef fn_value; | 10 | LLVMValueRef fn_value; |
| ... | @@ -235,9 +236,10 @@ static LLVMValueRef find_or_create_string(CodeGen *g, Buf *str) { | ... | @@ -235,9 +236,10 @@ static LLVMValueRef find_or_create_string(CodeGen *g, Buf *str) { |
| 235 | } | 236 | } |
| 236 | LLVMValueRef text = LLVMConstString(buf_ptr(str), buf_len(str), false); | 237 | LLVMValueRef text = LLVMConstString(buf_ptr(str), buf_len(str), false); |
| 237 | LLVMValueRef global_value = LLVMAddGlobal(g->mod, LLVMTypeOf(text), ""); | 238 | LLVMValueRef global_value = LLVMAddGlobal(g->mod, LLVMTypeOf(text), ""); |
| 238 | LLVMSetLinkage(global_value, LLVMInternalLinkage); | 239 | LLVMSetLinkage(global_value, LLVMPrivateLinkage); |
| 239 | LLVMSetInitializer(global_value, text); | 240 | LLVMSetInitializer(global_value, text); |
| 240 | LLVMSetGlobalConstant(global_value, true); | 241 | LLVMSetGlobalConstant(global_value, true); |
| | 242 | LLVMSetUnnamedAddr(global_value, true); |
| 241 | g->str_table.put(str, global_value); | 243 | g->str_table.put(str, global_value); |
| 242 | | 244 | |
| 243 | return global_value; | 245 | return global_value; |
| ... | @@ -257,8 +259,15 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *expr_node) { | ... | @@ -257,8 +259,15 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *expr_node) { |
| 257 | case AstNodeExpressionTypeString: | 259 | case AstNodeExpressionTypeString: |
| 258 | { | 260 | { |
| 259 | Buf *str = &expr_node->data.expression.data.string; | 261 | Buf *str = &expr_node->data.expression.data.string; |
| 260 | fprintf(stderr, "str = '%s'\n", buf_ptr(str)); | 262 | LLVMValueRef str_val = find_or_create_string(g, str); |
| 261 | return 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; |
| 262 | } | 271 | } |
| 263 | case AstNodeExpressionTypeFnCall: | 272 | case AstNodeExpressionTypeFnCall: |
| 264 | return gen_fn_call(g, expr_node->data.expression.data.fn_call); | 273 | return gen_fn_call(g, expr_node->data.expression.data.fn_call); |
| ... | @@ -322,6 +331,9 @@ void code_gen(CodeGen *g) { | ... | @@ -322,6 +331,9 @@ void code_gen(CodeGen *g) { |
| 322 | } | 331 | } |
| 323 | | 332 | |
| 324 | LLVMDumpModule(g->mod); | 333 | LLVMDumpModule(g->mod); |
| | 334 | |
| | 335 | char *error = nullptr; |
| | 336 | LLVMVerifyModule(g->mod, LLVMAbortProcessAction, &error); |
| 325 | } | 337 | } |
| 326 | | 338 | |
| 327 | ZigList<ErrorMsg> *codegen_error_messages(CodeGen *g) { | 339 | ZigList<ErrorMsg> *codegen_error_messages(CodeGen *g) { |