diff options
| author | LemonBoy <thatlemon@gmail.com> | 2020-01-13 22:18:49 +0100 |
|---|---|---|
| committer | LemonBoy <thatlemon@gmail.com> | 2020-01-13 22:18:49 +0100 |
| commit | cae93c860bc2c599618482a4190daf619a0c69e2 (patch) | |
| tree | c61934554aeab9798a0bc8ae29ab95c80b54eefe /src/codegen.cpp | |
| parent | 84930fec279a8bf0e7ce79c79a7ccd98d1ef4d0d (diff) | |
| download | zig-cae93c860bc2c599618482a4190daf619a0c69e2.tar.gz zig-cae93c860bc2c599618482a4190daf619a0c69e2.zip | |
Allow switching on pointer types
Closes #4074
Diffstat (limited to 'src/codegen.cpp')
| -rw-r--r-- | src/codegen.cpp | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/src/codegen.cpp b/src/codegen.cpp index fd36e15f82..d64c191c93 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -4876,14 +4876,30 @@ static LLVMValueRef ir_render_pop_count(CodeGen *g, IrExecutable *executable, Ir } static LLVMValueRef ir_render_switch_br(CodeGen *g, IrExecutable *executable, IrInstructionSwitchBr *instruction) { - LLVMValueRef target_value = ir_llvm_value(g, instruction->target_value); + ZigType *target_type = instruction->target_value->value->type; LLVMBasicBlockRef else_block = instruction->else_block->llvm_block; + + LLVMValueRef target_value = ir_llvm_value(g, instruction->target_value); + if (target_type->id == ZigTypeIdPointer) { + const ZigType *usize = g->builtin_types.entry_usize; + target_value = LLVMBuildPtrToInt(g->builder, target_value, usize->llvm_type, ""); + } + LLVMValueRef switch_instr = LLVMBuildSwitch(g->builder, target_value, else_block, - (unsigned)instruction->case_count); + (unsigned)instruction->case_count); + for (size_t i = 0; i < instruction->case_count; i += 1) { IrInstructionSwitchBrCase *this_case = &instruction->cases[i]; - LLVMAddCase(switch_instr, ir_llvm_value(g, this_case->value), this_case->block->llvm_block); + + LLVMValueRef case_value = ir_llvm_value(g, this_case->value); + if (target_type->id == ZigTypeIdPointer) { + const ZigType *usize = g->builtin_types.entry_usize; + case_value = LLVMBuildPtrToInt(g->builder, case_value, usize->llvm_type, ""); + } + + LLVMAddCase(switch_instr, case_value, this_case->block->llvm_block); } + return nullptr; } |
