authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-26 03:16:19-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-26 03:16:19-05:00
log73a751911e2c2be646287eb8c8d579dded25afe3
tree77d14408a8eb895878baa9678ee714dfa03497b7
parent6ed835ca67670098da79d4e329c6efcb12419599

IR: pass staticEvalListInit test


3 files changed, 41 insertions(+), 16 deletions(-)

src/codegen.cpp+26-2
......@@ -2181,6 +2181,30 @@ static LLVMValueRef ir_render_struct_init(CodeGen *g, IrExecutable *executable,
21812181 return instruction->tmp_ptr;
21822182}
21832183
2184static LLVMValueRef ir_render_container_init_list(CodeGen *g, IrExecutable *executable,
2185 IrInstructionContainerInitList *instruction)
2186{
2187 TypeTableEntry *array_type = instruction->base.value.type;
2188 assert(array_type->id == TypeTableEntryIdArray);
2189 LLVMValueRef tmp_array_ptr = instruction->tmp_ptr;
2190 assert(tmp_array_ptr);
2191
2192 size_t field_count = instruction->item_count;
2193
2194 TypeTableEntry *child_type = array_type->data.array.child_type;
2195 for (size_t i = 0; i < field_count; i += 1) {
2196 LLVMValueRef elem_val = ir_llvm_value(g, instruction->items[i]);
2197 LLVMValueRef indices[] = {
2198 LLVMConstNull(g->builtin_types.entry_usize->type_ref),
2199 LLVMConstInt(g->builtin_types.entry_usize->type_ref, i, false),
2200 };
2201 LLVMValueRef elem_ptr = LLVMBuildInBoundsGEP(g->builder, tmp_array_ptr, indices, 2, "");
2202 gen_assign_raw(g, elem_ptr, elem_val, child_type);
2203 }
2204
2205 return tmp_array_ptr;
2206}
2207
21842208static void set_debug_location(CodeGen *g, IrInstruction *instruction) {
21852209 AstNode *source_node = instruction->source_node;
21862210 Scope *scope = instruction->scope;
......@@ -2323,10 +2347,10 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
23232347 return ir_render_pointer_reinterpret(g, executable, (IrInstructionPointerReinterpret *)instruction);
23242348 case IrInstructionIdWidenOrShorten:
23252349 return ir_render_widen_or_shorten(g, executable, (IrInstructionWidenOrShorten *)instruction);
2350 case IrInstructionIdContainerInitList:
2351 return ir_render_container_init_list(g, executable, (IrInstructionContainerInitList *)instruction);
23262352 case IrInstructionIdSwitchVar:
23272353 zig_panic("TODO render switch var instruction to LLVM");
2328 case IrInstructionIdContainerInitList:
2329 zig_panic("TODO render container init list instruction to LLVM");
23302354 }
23312355 zig_unreachable();
23322356}
test/cases/eval.zig+15
......@@ -94,6 +94,21 @@ fn makePoint(x: i32, y: i32) -> Point {
9494}
9595
9696
97fn staticEvalListInit() {
98 @setFnTest(this);
99
100 assert(static_vec3.data[2] == 1.0);
101 assert(vec3(0.0, 0.0, 3.0).data[2] == 3.0);
102}
103const static_vec3 = vec3(0.0, 0.0, 1.0);
104pub const Vec3 = struct {
105 data: [3]f32,
106};
107pub fn vec3(x: f32, y: f32, z: f32) -> Vec3 {
108 Vec3 {
109 .data = []f32 { x, y, z, },
110 }
111}
97112
98113// TODO const assert = @import("std").debug.assert;
99114fn assert(ok: bool) {
test/self_hosted.zig-14
......@@ -4,20 +4,6 @@ const str = std.str;
44const cstr = std.cstr;
55
66
7fn staticEvalListInit() {
8 @setFnTest(this);
9
10 assert(static_vec3.data[2] == 1.0);
11}
12const static_vec3 = vec3(0.0, 0.0, 1.0);
13pub const Vec3 = struct {
14 data: [3]f32,
15};
16pub fn vec3(x: f32, y: f32, z: f32) -> Vec3 {
17 Vec3 {
18 .data = []f32 { x, y, z, },
19 }
20}
217
228fn genericFnWithImplicitCast() {
239 @setFnTest(this, true);