diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2016-12-26 03:16:19 -0500 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2016-12-26 03:16:19 -0500 |
| commit | 73a751911e2c2be646287eb8c8d579dded25afe3 (patch) | |
| tree | 77d14408a8eb895878baa9678ee714dfa03497b7 | |
| parent | 6ed835ca67670098da79d4e329c6efcb12419599 (diff) | |
| download | zig-73a751911e2c2be646287eb8c8d579dded25afe3.tar.gz zig-73a751911e2c2be646287eb8c8d579dded25afe3.zip | |
IR: pass staticEvalListInit test
| -rw-r--r-- | src/codegen.cpp | 28 | ||||
| -rw-r--r-- | test/cases/eval.zig | 15 | ||||
| -rw-r--r-- | test/self_hosted.zig | 14 |
3 files changed, 41 insertions, 16 deletions
diff --git a/src/codegen.cpp b/src/codegen.cpp index 39cd6c8fd9..6ab14cffe1 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -2181,6 +2181,30 @@ static LLVMValueRef ir_render_struct_init(CodeGen *g, IrExecutable *executable, return instruction->tmp_ptr; } +static LLVMValueRef ir_render_container_init_list(CodeGen *g, IrExecutable *executable, + IrInstructionContainerInitList *instruction) +{ + TypeTableEntry *array_type = instruction->base.value.type; + assert(array_type->id == TypeTableEntryIdArray); + LLVMValueRef tmp_array_ptr = instruction->tmp_ptr; + assert(tmp_array_ptr); + + size_t field_count = instruction->item_count; + + TypeTableEntry *child_type = array_type->data.array.child_type; + for (size_t i = 0; i < field_count; i += 1) { + LLVMValueRef elem_val = ir_llvm_value(g, instruction->items[i]); + LLVMValueRef indices[] = { + LLVMConstNull(g->builtin_types.entry_usize->type_ref), + LLVMConstInt(g->builtin_types.entry_usize->type_ref, i, false), + }; + LLVMValueRef elem_ptr = LLVMBuildInBoundsGEP(g->builder, tmp_array_ptr, indices, 2, ""); + gen_assign_raw(g, elem_ptr, elem_val, child_type); + } + + return tmp_array_ptr; +} + static void set_debug_location(CodeGen *g, IrInstruction *instruction) { AstNode *source_node = instruction->source_node; Scope *scope = instruction->scope; @@ -2323,10 +2347,10 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, return ir_render_pointer_reinterpret(g, executable, (IrInstructionPointerReinterpret *)instruction); case IrInstructionIdWidenOrShorten: return ir_render_widen_or_shorten(g, executable, (IrInstructionWidenOrShorten *)instruction); + case IrInstructionIdContainerInitList: + return ir_render_container_init_list(g, executable, (IrInstructionContainerInitList *)instruction); case IrInstructionIdSwitchVar: zig_panic("TODO render switch var instruction to LLVM"); - case IrInstructionIdContainerInitList: - zig_panic("TODO render container init list instruction to LLVM"); } zig_unreachable(); } diff --git a/test/cases/eval.zig b/test/cases/eval.zig index c34d552035..8f5b18f529 100644 --- a/test/cases/eval.zig +++ b/test/cases/eval.zig @@ -94,6 +94,21 @@ fn makePoint(x: i32, y: i32) -> Point { } +fn staticEvalListInit() { + @setFnTest(this); + + assert(static_vec3.data[2] == 1.0); + assert(vec3(0.0, 0.0, 3.0).data[2] == 3.0); +} +const static_vec3 = vec3(0.0, 0.0, 1.0); +pub const Vec3 = struct { + data: [3]f32, +}; +pub fn vec3(x: f32, y: f32, z: f32) -> Vec3 { + Vec3 { + .data = []f32 { x, y, z, }, + } +} // TODO const assert = @import("std").debug.assert; fn assert(ok: bool) { diff --git a/test/self_hosted.zig b/test/self_hosted.zig index 4c6c9641bc..d78eace9d1 100644 --- a/test/self_hosted.zig +++ b/test/self_hosted.zig @@ -4,20 +4,6 @@ const str = std.str; const cstr = std.cstr; -fn staticEvalListInit() { - @setFnTest(this); - - assert(static_vec3.data[2] == 1.0); -} -const static_vec3 = vec3(0.0, 0.0, 1.0); -pub const Vec3 = struct { - data: [3]f32, -}; -pub fn vec3(x: f32, y: f32, z: f32) -> Vec3 { - Vec3 { - .data = []f32 { x, y, z, }, - } -} fn genericFnWithImplicitCast() { @setFnTest(this, true); |
