authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-10-20 01:13:39-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-10-20 01:13:39-04:00
loga9a6f77a1f0a6e173d85fee42aec0a8d5e22a64d
treeb296c79800a216da4a9caa1c335be78d5f2ebd70
parent682511d1b22d50a77ff967d1d6415a68560cd49a

add variable declaration initialization IR


3 files changed, 58 insertions(+), 7 deletions(-)

src/analyze.cpp-3
...@@ -3615,9 +3615,6 @@ static VariableTableEntry *add_local_var_shadowable(CodeGen *g, AstNode *source_...@@ -3615,9 +3615,6 @@ static VariableTableEntry *add_local_var_shadowable(CodeGen *g, AstNode *source_
3615 // TODO replace _anon with @anon and make sure all tests still pass3615 // TODO replace _anon with @anon and make sure all tests still pass
3616 buf_init_from_str(&variable_entry->name, "_anon");3616 buf_init_from_str(&variable_entry->name, "_anon");
3617 }3617 }
3618 if (context->fn_entry) {
3619 context->fn_entry->variable_list.append(variable_entry);
3620 }
36213618
3622 variable_entry->is_const = is_const;3619 variable_entry->is_const = is_const;
3623 variable_entry->decl_node = source_node;3620 variable_entry->decl_node = source_node;
src/codegen.cpp+54-1
...@@ -2811,6 +2811,59 @@ static LLVMValueRef ir_render_un_op(CodeGen *g, IrExecutable *executable, IrInst...@@ -2811,6 +2811,59 @@ static LLVMValueRef ir_render_un_op(CodeGen *g, IrExecutable *executable, IrInst
2811 zig_unreachable();2811 zig_unreachable();
2812}2812}
28132813
2814static LLVMValueRef ir_render_decl_var(CodeGen *g, IrExecutable *executable,
2815 IrInstructionDeclVar *decl_var_instruction)
2816{
2817 VariableTableEntry *var = decl_var_instruction->var;
2818
2819 if (!type_has_bits(var->type))
2820 return nullptr;
2821
2822 IrInstruction *init_value = decl_var_instruction->init_value;
2823
2824 bool have_init_expr = false;
2825 bool want_zeroes = false;
2826
2827 ConstExprValue *const_val = &init_value->static_value;
2828 if (!const_val->ok || const_val->special == ConstValSpecialOther)
2829 have_init_expr = true;
2830 if (const_val->ok && const_val->special == ConstValSpecialZeroes)
2831 want_zeroes = true;
2832
2833 if (have_init_expr) {
2834 gen_assign_raw(g, init_value->source_node, BinOpTypeAssign, var->value_ref,
2835 ir_llvm_value(g, init_value), var->type, init_value->type_entry);
2836 } else {
2837 bool ignore_uninit = false;
2838 // handle runtime stack allocation
2839 bool want_safe = ir_want_debug_safety(g, &decl_var_instruction->base);
2840 if (!ignore_uninit && (want_safe || want_zeroes)) {
2841 TypeTableEntry *usize = g->builtin_types.entry_usize;
2842 uint64_t size_bytes = LLVMStoreSizeOfType(g->target_data_ref, var->type->type_ref);
2843 uint64_t align_bytes = get_memcpy_align(g, var->type);
2844
2845 // memset uninitialized memory to 0xa
2846 LLVMTypeRef ptr_u8 = LLVMPointerType(LLVMInt8Type(), 0);
2847 LLVMValueRef fill_char = LLVMConstInt(LLVMInt8Type(), want_zeroes ? 0x00 : 0xaa, false);
2848 LLVMValueRef dest_ptr = LLVMBuildBitCast(g->builder, var->value_ref, ptr_u8, "");
2849 LLVMValueRef byte_count = LLVMConstInt(usize->type_ref, size_bytes, false);
2850 LLVMValueRef align_in_bytes = LLVMConstInt(LLVMInt32Type(), align_bytes, false);
2851 LLVMValueRef params[] = {
2852 dest_ptr,
2853 fill_char,
2854 byte_count,
2855 align_in_bytes,
2856 LLVMConstNull(LLVMInt1Type()), // is volatile
2857 };
2858
2859 LLVMBuildCall(g->builder, g->memset_fn_val, params, 5, "");
2860 }
2861 }
2862
2863 gen_var_debug_decl(g, var);
2864 return nullptr;
2865}
2866
2814static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, IrInstruction *instruction) {2867static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, IrInstruction *instruction) {
2815 set_debug_source_node(g, instruction->source_node);2868 set_debug_source_node(g, instruction->source_node);
28162869
...@@ -2821,7 +2874,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,...@@ -2821,7 +2874,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
2821 case IrInstructionIdReturn:2874 case IrInstructionIdReturn:
2822 return ir_render_return(g, executable, (IrInstructionReturn *)instruction);2875 return ir_render_return(g, executable, (IrInstructionReturn *)instruction);
2823 case IrInstructionIdDeclVar:2876 case IrInstructionIdDeclVar:
2824 return nullptr;2877 return ir_render_decl_var(g, executable, (IrInstructionDeclVar *)instruction);
2825 case IrInstructionIdLoadVar:2878 case IrInstructionIdLoadVar:
2826 return ir_render_load_var(g, executable, (IrInstructionLoadVar *)instruction);2879 return ir_render_load_var(g, executable, (IrInstructionLoadVar *)instruction);
2827 case IrInstructionIdBinOp:2880 case IrInstructionIdBinOp:
src/ir.cpp+4-3
...@@ -586,9 +586,6 @@ static VariableTableEntry *ir_add_local_var(IrBuilder *irb, AstNode *node, Buf *...@@ -586,9 +586,6 @@ static VariableTableEntry *ir_add_local_var(IrBuilder *irb, AstNode *node, Buf *
586 // TODO replace _anon with @anon and make sure all tests still pass586 // TODO replace _anon with @anon and make sure all tests still pass
587 buf_init_from_str(&variable_entry->name, "_anon");587 buf_init_from_str(&variable_entry->name, "_anon");
588 }588 }
589 if (node->block_context->fn_entry) {
590 node->block_context->fn_entry->variable_list.append(variable_entry);
591 }
592589
593 variable_entry->is_const = is_const;590 variable_entry->is_const = is_const;
594 variable_entry->decl_node = node;591 variable_entry->decl_node = node;
...@@ -2177,6 +2174,10 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc...@@ -2177,6 +2174,10 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
2177 }2174 }
2178 ir_build_var_decl_from(&ira->new_irb, &decl_var_instruction->base, var, var_type, casted_init_value);2175 ir_build_var_decl_from(&ira->new_irb, &decl_var_instruction->base, var, var_type, casted_init_value);
21792176
2177 BlockContext *scope = decl_var_instruction->base.source_node->block_context;
2178 if (scope->fn_entry)
2179 scope->fn_entry->variable_list.append(var);
2180
2180 return ira->codegen->builtin_types.entry_void;2181 return ira->codegen->builtin_types.entry_void;
2181}2182}
21822183