summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Harley2022-05-11 11:59:16 +0100
committerTom Harley2022-05-11 11:59:16 +0100
commit8432156ed42e4f2bb636c23434008ac7be291d38 (patch)
tree6cb9b0fae042aeb8418746d71612e8b34ea5b70e
parentAdd String support (diff)
downloadsinter-8432156ed42e4f2bb636c23434008ac7be291d38.tar.gz
sinter-8432156ed42e4f2bb636c23434008ac7be291d38.zip

Fix issue with nested case statements

Previously, nested case statements would cause phis to be given incorrect incoming blocks, causing issues when compiling the LLVMIR with clang.

To fix this, all case branches are given a second “exit” block that they jump to before jumping to the phi block. This way, we always know the block that was just left to get to the phi.

-rw-r--r--llvm_codegen.c20
1 files changed, 18 insertions, 2 deletions
diff --git a/llvm_codegen.c b/llvm_codegen.c
index de136dc..9715e44 100644
--- a/llvm_codegen.c
+++ b/llvm_codegen.c
@@ -1510,6 +1510,7 @@ LLVMValueRef codegen_case(sexpr *se, int tail_position) {
// FIXME magic number
LLVMValueRef matches[100];
LLVMBasicBlockRef case_blocks[100];
+ LLVMBasicBlockRef case_exit_blocks[100];
LLVMValueRef results[100];
unsigned case_count = 0;
@@ -1529,13 +1530,21 @@ LLVMValueRef codegen_case(sexpr *se, int tail_position) {
put_builder_at_end(builder, before_b);
LLVMValueRef match = constant_int(match_node);
- LLVMBasicBlockRef case_b = LLVMAppendBasicBlock(function, "case");
+ LLVMBasicBlockRef case_b =
+ LLVMAppendBasicBlock(function, "case_branch");
+ LLVMBasicBlockRef case_exit_b =
+ LLVMAppendBasicBlock(function, "case_branch_exit");
+
put_builder_at_end(builder, case_b);
LLVMValueRef then_result = _codegen(then_node, tail_position);
+ LLVMBuildBr(builder, case_exit_b);
+
+ put_builder_at_end(builder, case_exit_b);
LLVMBuildBr(builder, exit_b);
matches[case_count] = match;
case_blocks[case_count] = case_b;
+ case_exit_blocks[case_count] = case_exit_b;
results[case_count] = then_result;
case_count++;
@@ -1543,8 +1552,14 @@ LLVMValueRef codegen_case(sexpr *se, int tail_position) {
LLVMBasicBlockRef default_b =
LLVMAppendBasicBlock(function, "case_default");
+ LLVMBasicBlockRef default_exit_b =
+ LLVMAppendBasicBlock(function, "case_default_exit");
+
put_builder_at_end(builder, default_b);
LLVMValueRef def_result = _codegen(default_case_node, tail_position);
+ LLVMBuildBr(builder, default_exit_b);
+
+ put_builder_at_end(builder, default_exit_b);
LLVMBuildBr(builder, exit_b);
/* FIXME
@@ -1568,8 +1583,9 @@ LLVMValueRef codegen_case(sexpr *se, int tail_position) {
// FIXME shouldn't be using boxed_t here
LLVMValueRef res = LLVMBuildPhi(builder, boxed_t, "case_result");
case_blocks[case_count] = default_b;
+ case_exit_blocks[case_count] = default_exit_b;
results[case_count] = def_result;
- LLVMAddIncoming(res, results, case_blocks, case_count + 1);
+ LLVMAddIncoming(res, results, case_exit_blocks, case_count + 1);
return res;
}