authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-07 20:57:45-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-07 20:57:45-07:00
log1279fe0caaa83dd9f573dfe3c359098143224abc
tree017bb5ff269f63d80a19fd72830f0970aea699f2
parentb66fb607bf00c8ac120f22080bcf48eb05cbc71f

all variables have memory addresses


4 files changed, 47 insertions(+), 20 deletions(-)

src/analyze.cpp+1
...@@ -525,6 +525,7 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,...@@ -525,6 +525,7 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
525 buf_init_from_buf(&variable_entry->name, &variable_declaration->symbol);525 buf_init_from_buf(&variable_entry->name, &variable_declaration->symbol);
526 variable_entry->type = type;526 variable_entry->type = type;
527 variable_entry->is_const = variable_declaration->is_const;527 variable_entry->is_const = variable_declaration->is_const;
528 variable_entry->is_ptr = true;
528 variable_entry->decl_node = node;529 variable_entry->decl_node = node;
529 context->variable_table.put(&variable_entry->name, variable_entry);530 context->variable_table.put(&variable_entry->name, variable_entry);
530 }531 }
src/codegen.cpp+34-20
...@@ -211,8 +211,6 @@ static LLVMValueRef gen_arithmetic_bin_op_expr(CodeGen *g, AstNode *node) {...@@ -211,8 +211,6 @@ static LLVMValueRef gen_arithmetic_bin_op_expr(CodeGen *g, AstNode *node) {
211 LLVMValueRef val2 = gen_expr(g, node->data.bin_op_expr.op2);211 LLVMValueRef val2 = gen_expr(g, node->data.bin_op_expr.op2);
212212
213 switch (node->data.bin_op_expr.bin_op) {213 switch (node->data.bin_op_expr.bin_op) {
214 case BinOpTypeAssign:
215 zig_panic("TODO assignment");
216 case BinOpTypeBinOr:214 case BinOpTypeBinOr:
217 add_debug_source_node(g, node);215 add_debug_source_node(g, node);
218 return LLVMBuildOr(g->builder, val1, val2, "");216 return LLVMBuildOr(g->builder, val1, val2, "");
...@@ -258,6 +256,7 @@ static LLVMValueRef gen_arithmetic_bin_op_expr(CodeGen *g, AstNode *node) {...@@ -258,6 +256,7 @@ static LLVMValueRef gen_arithmetic_bin_op_expr(CodeGen *g, AstNode *node) {
258 case BinOpTypeCmpLessOrEq:256 case BinOpTypeCmpLessOrEq:
259 case BinOpTypeCmpGreaterOrEq:257 case BinOpTypeCmpGreaterOrEq:
260 case BinOpTypeInvalid:258 case BinOpTypeInvalid:
259 case BinOpTypeAssign:
261 zig_unreachable();260 zig_unreachable();
262 }261 }
263 zig_unreachable();262 zig_unreachable();
...@@ -520,18 +519,22 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {...@@ -520,18 +519,22 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
520 return gen_return_expr(g, node);519 return gen_return_expr(g, node);
521 case NodeTypeVariableDeclaration:520 case NodeTypeVariableDeclaration:
522 {521 {
523 LocalVariableTableEntry *variable = find_local_variable(node->codegen_node->expr_node.block_context, &node->data.variable_declaration.symbol);522 LocalVariableTableEntry *variable = find_local_variable(
524 if (variable->is_const) {523 node->codegen_node->expr_node.block_context,
525 assert(node->data.variable_declaration.expr);524 &node->data.variable_declaration.symbol);
526 variable->value_ref = gen_expr(g, node->data.variable_declaration.expr);525
526 assert(variable);
527 assert(variable->is_ptr);
528
529 LLVMValueRef value;
530 if (node->data.variable_declaration.expr) {
531 value = gen_expr(g, node->data.variable_declaration.expr);
532 } else {
533 value = LLVMConstNull(variable->type->type_ref);
534 }
535 if (variable->type == g->builtin_types.entry_void) {
527 return nullptr;536 return nullptr;
528 } else {537 } else {
529 LLVMValueRef value;
530 if (node->data.variable_declaration.expr) {
531 value = gen_expr(g, node->data.variable_declaration.expr);
532 } else {
533 value = LLVMConstNull(variable->type->type_ref);
534 }
535 add_debug_source_node(g, node);538 add_debug_source_node(g, node);
536 return LLVMBuildStore(g->builder, value, variable->value_ref);539 return LLVMBuildStore(g->builder, value, variable->value_ref);
537 }540 }
...@@ -575,11 +578,16 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {...@@ -575,11 +578,16 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
575 }578 }
576 case NodeTypeSymbol:579 case NodeTypeSymbol:
577 {580 {
578 LocalVariableTableEntry *variable = find_local_variable(node->codegen_node->expr_node.block_context, &node->data.symbol);581 LocalVariableTableEntry *variable = find_local_variable(
579 if (variable->is_const) {582 node->codegen_node->expr_node.block_context,
580 return variable->value_ref;583 &node->data.symbol);
581 } else {584 assert(variable);
585 if (variable->type == g->builtin_types.entry_void) {
586 return nullptr;
587 } else if (variable->is_ptr) {
582 return LLVMBuildLoad(g->builder, variable->value_ref, "");588 return LLVMBuildLoad(g->builder, variable->value_ref, "");
589 } else {
590 return variable->value_ref;
583 }591 }
584 }592 }
585 case NodeTypeBlock:593 case NodeTypeBlock:
...@@ -742,6 +750,11 @@ static void do_code_gen(CodeGen *g) {...@@ -742,6 +750,11 @@ static void do_code_gen(CodeGen *g) {
742 for (int i = 0; i < codegen_fn_def->all_block_contexts.length; i += 1) {750 for (int i = 0; i < codegen_fn_def->all_block_contexts.length; i += 1) {
743 BlockContext *block_context = codegen_fn_def->all_block_contexts.at(i);751 BlockContext *block_context = codegen_fn_def->all_block_contexts.at(i);
744752
753 // skip the block context for function parameters
754 if (block_context->node->type == NodeTypeFnDef) {
755 continue;
756 }
757
745 auto it = block_context->variable_table.entry_iterator();758 auto it = block_context->variable_table.entry_iterator();
746 for (;;) {759 for (;;) {
747 auto *entry = it.next();760 auto *entry = it.next();
...@@ -749,10 +762,11 @@ static void do_code_gen(CodeGen *g) {...@@ -749,10 +762,11 @@ static void do_code_gen(CodeGen *g) {
749 break;762 break;
750763
751 LocalVariableTableEntry *var = entry->value;764 LocalVariableTableEntry *var = entry->value;
752 if (!var->is_const) {765 if (var->type == g->builtin_types.entry_void)
753 add_debug_source_node(g, var->decl_node);766 continue;
754 var->value_ref = LLVMBuildAlloca(g->builder, var->type->type_ref, buf_ptr(&var->name));767
755 }768 add_debug_source_node(g, var->decl_node);
769 var->value_ref = LLVMBuildAlloca(g->builder, var->type->type_ref, buf_ptr(&var->name));
756 }770 }
757 }771 }
758772
src/semantic_info.hpp+1
...@@ -123,6 +123,7 @@ struct LocalVariableTableEntry {...@@ -123,6 +123,7 @@ struct LocalVariableTableEntry {
123 TypeTableEntry *type;123 TypeTableEntry *type;
124 LLVMValueRef value_ref;124 LLVMValueRef value_ref;
125 bool is_const;125 bool is_const;
126 bool is_ptr; // if true, value_ref is a pointer
126 AstNode *decl_node;127 AstNode *decl_node;
127};128};
128129
src/zig_llvm.cpp+11
...@@ -211,6 +211,17 @@ LLVMZigDILexicalBlock *LLVMZigCreateLexicalBlock(LLVMZigDIBuilder *dbuilder, LLV...@@ -211,6 +211,17 @@ LLVMZigDILexicalBlock *LLVMZigCreateLexicalBlock(LLVMZigDIBuilder *dbuilder, LLV
211 return reinterpret_cast<LLVMZigDILexicalBlock*>(result);211 return reinterpret_cast<LLVMZigDILexicalBlock*>(result);
212}212}
213213
214/*
215LLVMZigDILocalVariable *
216
217 DILocalVariable *createLocalVariable(unsigned Tag, DIScope *Scope,
218 StringRef Name, DIFile *File,
219 unsigned LineNo, DIType *Ty,
220 bool AlwaysPreserve = false,
221 unsigned Flags = 0,
222 unsigned ArgNo = 0);
223 */
224
214LLVMZigDIScope *LLVMZigLexicalBlockToScope(LLVMZigDILexicalBlock *lexical_block) {225LLVMZigDIScope *LLVMZigLexicalBlockToScope(LLVMZigDILexicalBlock *lexical_block) {
215 DIScope *scope = reinterpret_cast<DILexicalBlock*>(lexical_block);226 DIScope *scope = reinterpret_cast<DILexicalBlock*>(lexical_block);
216 return reinterpret_cast<LLVMZigDIScope*>(scope);227 return reinterpret_cast<LLVMZigDIScope*>(scope);