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,
525525 buf_init_from_buf(&variable_entry->name, &variable_declaration->symbol);
526526 variable_entry->type = type;
527527 variable_entry->is_const = variable_declaration->is_const;
528 variable_entry->is_ptr = true;
528529 variable_entry->decl_node = node;
529530 context->variable_table.put(&variable_entry->name, variable_entry);
530531 }
src/codegen.cpp+34-20
......@@ -211,8 +211,6 @@ static LLVMValueRef gen_arithmetic_bin_op_expr(CodeGen *g, AstNode *node) {
211211 LLVMValueRef val2 = gen_expr(g, node->data.bin_op_expr.op2);
212212
213213 switch (node->data.bin_op_expr.bin_op) {
214 case BinOpTypeAssign:
215 zig_panic("TODO assignment");
216214 case BinOpTypeBinOr:
217215 add_debug_source_node(g, node);
218216 return LLVMBuildOr(g->builder, val1, val2, "");
......@@ -258,6 +256,7 @@ static LLVMValueRef gen_arithmetic_bin_op_expr(CodeGen *g, AstNode *node) {
258256 case BinOpTypeCmpLessOrEq:
259257 case BinOpTypeCmpGreaterOrEq:
260258 case BinOpTypeInvalid:
259 case BinOpTypeAssign:
261260 zig_unreachable();
262261 }
263262 zig_unreachable();
......@@ -520,18 +519,22 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
520519 return gen_return_expr(g, node);
521520 case NodeTypeVariableDeclaration:
522521 {
523 LocalVariableTableEntry *variable = find_local_variable(node->codegen_node->expr_node.block_context, &node->data.variable_declaration.symbol);
524 if (variable->is_const) {
525 assert(node->data.variable_declaration.expr);
526 variable->value_ref = gen_expr(g, node->data.variable_declaration.expr);
522 LocalVariableTableEntry *variable = find_local_variable(
523 node->codegen_node->expr_node.block_context,
524 &node->data.variable_declaration.symbol);
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) {
527536 return nullptr;
528537 } 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 }
535538 add_debug_source_node(g, node);
536539 return LLVMBuildStore(g->builder, value, variable->value_ref);
537540 }
......@@ -575,11 +578,16 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
575578 }
576579 case NodeTypeSymbol:
577580 {
578 LocalVariableTableEntry *variable = find_local_variable(node->codegen_node->expr_node.block_context, &node->data.symbol);
579 if (variable->is_const) {
580 return variable->value_ref;
581 } else {
581 LocalVariableTableEntry *variable = find_local_variable(
582 node->codegen_node->expr_node.block_context,
583 &node->data.symbol);
584 assert(variable);
585 if (variable->type == g->builtin_types.entry_void) {
586 return nullptr;
587 } else if (variable->is_ptr) {
582588 return LLVMBuildLoad(g->builder, variable->value_ref, "");
589 } else {
590 return variable->value_ref;
583591 }
584592 }
585593 case NodeTypeBlock:
......@@ -742,6 +750,11 @@ static void do_code_gen(CodeGen *g) {
742750 for (int i = 0; i < codegen_fn_def->all_block_contexts.length; i += 1) {
743751 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
745758 auto it = block_context->variable_table.entry_iterator();
746759 for (;;) {
747760 auto *entry = it.next();
......@@ -749,10 +762,11 @@ static void do_code_gen(CodeGen *g) {
749762 break;
750763
751764 LocalVariableTableEntry *var = entry->value;
752 if (!var->is_const) {
753 add_debug_source_node(g, var->decl_node);
754 var->value_ref = LLVMBuildAlloca(g->builder, var->type->type_ref, buf_ptr(&var->name));
755 }
765 if (var->type == g->builtin_types.entry_void)
766 continue;
767
768 add_debug_source_node(g, var->decl_node);
769 var->value_ref = LLVMBuildAlloca(g->builder, var->type->type_ref, buf_ptr(&var->name));
756770 }
757771 }
758772
src/semantic_info.hpp+1
......@@ -123,6 +123,7 @@ struct LocalVariableTableEntry {
123123 TypeTableEntry *type;
124124 LLVMValueRef value_ref;
125125 bool is_const;
126 bool is_ptr; // if true, value_ref is a pointer
126127 AstNode *decl_node;
127128};
128129
src/zig_llvm.cpp+11
......@@ -211,6 +211,17 @@ LLVMZigDILexicalBlock *LLVMZigCreateLexicalBlock(LLVMZigDIBuilder *dbuilder, LLV
211211 return reinterpret_cast<LLVMZigDILexicalBlock*>(result);
212212}
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
214225LLVMZigDIScope *LLVMZigLexicalBlockToScope(LLVMZigDILexicalBlock *lexical_block) {
215226 DIScope *scope = reinterpret_cast<DILexicalBlock*>(lexical_block);
216227 return reinterpret_cast<LLVMZigDIScope*>(scope);