authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-17 17:48:07-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-17 17:48:07-05:00
logc64f9991d561ff9a8e3c4575e8149404784497d3
treecd452b9cef98810f74414f5c36eab8c21dd0d2c5
parent12fcbecbf8a1c8496354b909e0de2a69600115a9

IR: fix switching on enum


4 files changed, 99 insertions(+), 39 deletions(-)

src/codegen.cpp+27-6
......@@ -1420,12 +1420,12 @@ static LLVMValueRef ir_render_struct_field_ptr(CodeGen *g, IrExecutable *executa
14201420static LLVMValueRef ir_render_enum_field_ptr(CodeGen *g, IrExecutable *executable,
14211421 IrInstructionEnumFieldPtr *instruction)
14221422{
1423 LLVMValueRef enum_ptr = ir_llvm_value(g, instruction->enum_ptr);
14241423 TypeEnumField *field = instruction->field;
14251424
14261425 if (!type_has_bits(field->type_entry))
14271426 return nullptr;
14281427
1428 LLVMValueRef enum_ptr = ir_llvm_value(g, instruction->enum_ptr);
14291429 LLVMTypeRef field_type_ref = LLVMPointerType(field->type_entry->type_ref, 0);
14301430 LLVMValueRef union_field_ptr = LLVMBuildStructGEP(g->builder, enum_ptr, enum_gen_union_index, "");
14311431 LLVMValueRef bitcasted_union_field_ptr = LLVMBuildBitCast(g->builder, union_field_ptr, field_type_ref, "");
......@@ -2140,6 +2140,20 @@ static LLVMValueRef ir_render_err_wrap_payload(CodeGen *g, IrExecutable *executa
21402140 return instruction->tmp_ptr;
21412141}
21422142
2143static LLVMValueRef ir_render_enum_tag(CodeGen *g, IrExecutable *executable, IrInstructionEnumTag *instruction) {
2144 TypeTableEntry *enum_type = instruction->value->type_entry;
2145 TypeTableEntry *tag_type = enum_type->data.enumeration.tag_type;
2146 if (!type_has_bits(tag_type))
2147 return nullptr;
2148
2149 LLVMValueRef enum_val = ir_llvm_value(g, instruction->value);
2150 if (enum_type->data.enumeration.gen_field_count == 0)
2151 return enum_val;
2152
2153 LLVMValueRef tag_field_ptr = LLVMBuildStructGEP(g->builder, enum_val, enum_gen_tag_index, "");
2154 return get_handle_value(g, tag_field_ptr, tag_type);
2155}
2156
21432157static void set_debug_location(CodeGen *g, IrInstruction *instruction) {
21442158 AstNode *source_node = instruction->source_node;
21452159 Scope *scope = instruction->scope;
......@@ -2271,10 +2285,11 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
22712285 return ir_render_err_wrap_code(g, executable, (IrInstructionErrWrapCode *)instruction);
22722286 case IrInstructionIdErrWrapPayload:
22732287 return ir_render_err_wrap_payload(g, executable, (IrInstructionErrWrapPayload *)instruction);
2288 case IrInstructionIdEnumTag:
2289 return ir_render_enum_tag(g, executable, (IrInstructionEnumTag *)instruction);
22742290 case IrInstructionIdSwitchVar:
22752291 case IrInstructionIdContainerInitList:
22762292 case IrInstructionIdStructInit:
2277 case IrInstructionIdEnumTag:
22782293 zig_panic("TODO render more IR instructions to LLVM");
22792294 }
22802295 zig_unreachable();
......@@ -3240,7 +3255,7 @@ static void define_builtin_types(CodeGen *g) {
32403255 {
32413256 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdEnum);
32423257 entry->zero_bits = true; // only allowed at compile time
3243 buf_init_from_str(&entry->name, "@OS");
3258 buf_init_from_str(&entry->name, "Os");
32443259 uint32_t field_count = target_os_count();
32453260 entry->data.enumeration.src_field_count = field_count;
32463261 entry->data.enumeration.fields = allocate<TypeEnumField>(field_count);
......@@ -3249,6 +3264,7 @@ static void define_builtin_types(CodeGen *g) {
32493264 ZigLLVM_OSType os_type = get_target_os(i);
32503265 type_enum_field->name = buf_create_from_str(get_target_os_name(os_type));
32513266 type_enum_field->value = i;
3267 type_enum_field->type_entry = g->builtin_types.entry_void;
32523268
32533269 if (os_type == g->zig_target.os) {
32543270 g->target_os_index = i;
......@@ -3260,12 +3276,13 @@ static void define_builtin_types(CodeGen *g) {
32603276 entry->data.enumeration.tag_type = tag_type_entry;
32613277
32623278 g->builtin_types.entry_os_enum = entry;
3279 g->primitive_type_table.put(&entry->name, entry);
32633280 }
32643281
32653282 {
32663283 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdEnum);
32673284 entry->zero_bits = true; // only allowed at compile time
3268 buf_init_from_str(&entry->name, "@Arch");
3285 buf_init_from_str(&entry->name, "Arch");
32693286 uint32_t field_count = target_arch_count();
32703287 entry->data.enumeration.src_field_count = field_count;
32713288 entry->data.enumeration.fields = allocate<TypeEnumField>(field_count);
......@@ -3278,6 +3295,7 @@ static void define_builtin_types(CodeGen *g) {
32783295 buf_resize(type_enum_field->name, strlen(buf_ptr(type_enum_field->name)));
32793296
32803297 type_enum_field->value = i;
3298 type_enum_field->type_entry = g->builtin_types.entry_void;
32813299
32823300 if (arch_type->arch == g->zig_target.arch.arch &&
32833301 arch_type->sub_arch == g->zig_target.arch.sub_arch)
......@@ -3291,12 +3309,13 @@ static void define_builtin_types(CodeGen *g) {
32913309 entry->data.enumeration.tag_type = tag_type_entry;
32923310
32933311 g->builtin_types.entry_arch_enum = entry;
3312 g->primitive_type_table.put(&entry->name, entry);
32943313 }
32953314
32963315 {
32973316 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdEnum);
32983317 entry->zero_bits = true; // only allowed at compile time
3299 buf_init_from_str(&entry->name, "@Environ");
3318 buf_init_from_str(&entry->name, "Environ");
33003319 uint32_t field_count = target_environ_count();
33013320 entry->data.enumeration.src_field_count = field_count;
33023321 entry->data.enumeration.fields = allocate<TypeEnumField>(field_count);
......@@ -3317,12 +3336,13 @@ static void define_builtin_types(CodeGen *g) {
33173336 entry->data.enumeration.tag_type = tag_type_entry;
33183337
33193338 g->builtin_types.entry_environ_enum = entry;
3339 g->primitive_type_table.put(&entry->name, entry);
33203340 }
33213341
33223342 {
33233343 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdEnum);
33243344 entry->zero_bits = true; // only allowed at compile time
3325 buf_init_from_str(&entry->name, "@ObjectFormat");
3345 buf_init_from_str(&entry->name, "ObjectFormat");
33263346 uint32_t field_count = target_oformat_count();
33273347 entry->data.enumeration.src_field_count = field_count;
33283348 entry->data.enumeration.fields = allocate<TypeEnumField>(field_count);
......@@ -3343,6 +3363,7 @@ static void define_builtin_types(CodeGen *g) {
33433363 entry->data.enumeration.tag_type = tag_type_entry;
33443364
33453365 g->builtin_types.entry_oformat_enum = entry;
3366 g->primitive_type_table.put(&entry->name, entry);
33463367 }
33473368
33483369 {
src/ir.cpp+46-7
......@@ -5690,8 +5690,10 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
56905690 var_type = decl_var_instruction->var_type->other;
56915691 TypeTableEntry *proposed_type = ir_resolve_type(ira, var_type);
56925692 explicit_type = validate_var_type(ira->codegen, var_type->source_node, proposed_type);
5693 if (explicit_type->id == TypeTableEntryIdInvalid)
5694 return explicit_type;
5693 if (explicit_type->id == TypeTableEntryIdInvalid) {
5694 var->type = ira->codegen->builtin_types.entry_invalid;
5695 return var->type;
5696 }
56955697 }
56965698
56975699 AstNode *source_node = decl_var_instruction->base.source_node;
......@@ -7596,6 +7598,33 @@ static TypeTableEntry *ir_analyze_instruction_clz(IrAnalyze *ira, IrInstructionC
75967598 }
75977599}
75987600
7601static IrInstruction *ir_analyze_enum_tag(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value) {
7602 if (value->type_entry->id == TypeTableEntryIdInvalid)
7603 return ira->codegen->invalid_instruction;
7604
7605 if (value->type_entry->id != TypeTableEntryIdEnum) {
7606 ir_add_error(ira, source_instr,
7607 buf_sprintf("expected enum type, found '%s'", buf_ptr(&value->type_entry->name)));
7608 return ira->codegen->invalid_instruction;
7609 }
7610
7611 if (instr_is_comptime(value)) {
7612 ConstExprValue *val = ir_resolve_const(ira, value);
7613 if (!val)
7614 return ira->codegen->invalid_instruction;
7615
7616 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(ira->new_irb.exec,
7617 source_instr->scope, source_instr->source_node);
7618 const_instruction->base.type_entry = value->type_entry->data.enumeration.tag_type;
7619 const_instruction->base.static_value.special = ConstValSpecialStatic;
7620 const_instruction->base.static_value.depends_on_compile_var = val->depends_on_compile_var;
7621 bignum_init_unsigned(&const_instruction->base.static_value.data.x_bignum, val->data.x_enum.tag);
7622 return &const_instruction->base;
7623 }
7624
7625 zig_panic("TODO runtime enum tag instruction");
7626}
7627
75997628static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira,
76007629 IrInstructionSwitchBr *switch_br_instruction)
76017630{
......@@ -7651,6 +7680,12 @@ static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira,
76517680 if (new_value->type_entry->id == TypeTableEntryIdInvalid)
76527681 continue;
76537682
7683 if (new_value->type_entry->id == TypeTableEntryIdEnum) {
7684 new_value = ir_analyze_enum_tag(ira, &switch_br_instruction->base, new_value);
7685 if (new_value->type_entry->id == TypeTableEntryIdInvalid)
7686 continue;
7687 }
7688
76547689 IrInstruction *casted_new_value = ir_implicit_cast(ira, new_value, target_value->type_entry);
76557690 if (casted_new_value->type_entry->id == TypeTableEntryIdInvalid)
76567691 continue;
......@@ -7719,7 +7754,10 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,
77197754 return tag_type;
77207755 }
77217756
7722 ir_build_enum_tag_from(&ira->new_irb, &switch_target_instruction->base, target_value_ptr);
7757 IrInstruction *enum_value = ir_build_load_ptr(&ira->new_irb, switch_target_instruction->base.scope,
7758 switch_target_instruction->base.source_node, target_value_ptr);
7759 enum_value->type_entry = target_type;
7760 ir_build_enum_tag_from(&ira->new_irb, &switch_target_instruction->base, enum_value);
77237761 return tag_type;
77247762 }
77257763 case TypeTableEntryIdErrorUnion:
......@@ -7748,10 +7786,11 @@ static TypeTableEntry *ir_analyze_instruction_switch_var(IrAnalyze *ira,
77487786 zig_panic("TODO switch var analyze");
77497787}
77507788
7751static TypeTableEntry *ir_analyze_instruction_enum_tag(IrAnalyze *ira,
7752 IrInstructionEnumTag *enum_tag_instruction)
7753{
7754 zig_panic("TODO ir_analyze_instruction_enum_tag");
7789static TypeTableEntry *ir_analyze_instruction_enum_tag(IrAnalyze *ira, IrInstructionEnumTag *enum_tag_instruction) {
7790 IrInstruction *value = enum_tag_instruction->value->other;
7791 IrInstruction *new_instruction = ir_analyze_enum_tag(ira, &enum_tag_instruction->base, value);
7792 ir_link_new_instruction(new_instruction, &enum_tag_instruction->base);
7793 return new_instruction->type_entry;
77557794}
77567795
77577796static TypeTableEntry *ir_analyze_instruction_static_eval(IrAnalyze *ira,
std/bootstrap.zig+4-4
......@@ -5,7 +5,7 @@ const linux = @import("linux.zig");
55const cstr = @import("cstr.zig");
66
77const want_start_symbol = switch(@compileVar("os")) {
8 linux => true,
8 Os.linux => true,
99 else => false,
1010};
1111const want_main_symbol = !want_start_symbol;
......@@ -17,11 +17,11 @@ export nakedcc fn _start() -> unreachable {
1717 @setFnVisible(this, want_start_symbol);
1818
1919 switch (@compileVar("arch")) {
20 x86_64 => {
20 Arch.x86_64 => {
2121 argc = asm("mov (%%rsp), %[argc]": [argc] "=r" (-> usize));
2222 argv = asm("lea 0x8(%%rsp), %[argv]": [argv] "=r" (-> &&u8));
2323 },
24 i386 => {
24 Arch.i386 => {
2525 argc = asm("mov (%%esp), %[argc]": [argc] "=r" (-> usize));
2626 argv = asm("lea 0x4(%%esp), %[argv]": [argv] "=r" (-> &&u8));
2727 },
......@@ -31,7 +31,7 @@ export nakedcc fn _start() -> unreachable {
3131}
3232
3333fn callMain() -> %void {
34 var args: [argc][]u8 = undefined;
34 const args = @alloca([]u8, argc);
3535 for (args) |arg, i| {
3636 const ptr = argv[i];
3737 args[i] = ptr[0...cstr.len(ptr)];
std/io.zig+22-22
......@@ -34,31 +34,31 @@ pub var stderr = OutStream {
3434
3535/// The function received invalid input at runtime. An Invalid error means a
3636/// bug in the program that called the function.
37pub error Invalid;
37error Invalid;
3838
3939/// When an Unexpected error occurs, code that emitted the error likely needs
4040/// a patch to recognize the unexpected case so that it can handle it and emit
4141/// a more specific error.
42pub error Unexpected;
43
44pub error DiskQuota;
45pub error FileTooBig;
46pub error Io;
47pub error NoSpaceLeft;
48pub error BadPerm;
49pub error PipeFail;
50pub error BadFd;
51pub error IsDir;
52pub error NotDir;
53pub error SymLinkLoop;
54pub error ProcessFdQuotaExceeded;
55pub error SystemFdQuotaExceeded;
56pub error NameTooLong;
57pub error NoDevice;
58pub error PathNotFound;
59pub error NoMem;
60pub error Unseekable;
61pub error Eof;
42error Unexpected;
43
44error DiskQuota;
45error FileTooBig;
46error Io;
47error NoSpaceLeft;
48error BadPerm;
49error PipeFail;
50error BadFd;
51error IsDir;
52error NotDir;
53error SymLinkLoop;
54error ProcessFdQuotaExceeded;
55error SystemFdQuotaExceeded;
56error NameTooLong;
57error NoDevice;
58error PathNotFound;
59error NoMem;
60error Unseekable;
61error Eof;
6262
6363const buffer_size = 4 * 1024;
6464const max_u64_base10_digits = 20;
......@@ -374,7 +374,7 @@ pub fn parseUnsigned(inline T: type, buf: []u8, radix: u8) -> %T {
374374 return x;
375375}
376376
377pub error InvalidChar;
377error InvalidChar;
378378fn charToDigit(c: u8, radix: u8) -> %u8 {
379379 const value = if ('0' <= c && c <= '9') {
380380 c - '0'