Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 73 additions & 14 deletions src/llvm_backend_stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1950,19 +1950,29 @@ gb_internal void lb_build_type_switch_stmt(lbProcedure *p, AstTypeSwitchStmt *ss
GB_PANIC("Unknown switch kind");
}

if (tag.value != nullptr) {
String tag_name = str_lit("typeswitch.tag");
LLVMSetValueName2(tag.value, cast(char const *)tag_name.text, tag_name.len);
}

ast_node(body, BlockStmt, ss->body);

lbBlock *done = lb_create_block(p, "typeswitch.done");
lbBlock *else_block = done;
lbBlock *default_block = nullptr;
isize num_cases = 0;

for (Ast *clause : body->stmts) {
auto body_blocks = slice_make<lbBlock *>(permanent_allocator(), body->stmts.count);
for_array(i, body->stmts) {
Ast *clause = body->stmts[i];
ast_node(cc, CaseClause, clause);
num_cases += cc->list.count;

body_blocks[i] = lb_create_block(p, cc->list.count == 0 ? "typeswitch.default.body" : "typeswitch.case.body");

if (cc->list.count == 0) {
GB_ASSERT(default_block == nullptr);
default_block = lb_create_block(p, "typeswitch.default.body");
default_block = body_blocks[i];
else_block = default_block;
}
}
Expand All @@ -1974,7 +1984,11 @@ gb_internal void lb_build_type_switch_stmt(lbProcedure *p, AstTypeSwitchStmt *ss
switch_instr = LLVMBuildSwitch(p->builder, lb_const_bool(p->module, t_llvm_bool, false).value, else_block->block, cast(unsigned)num_cases);
} else {
GB_ASSERT(tag.value != nullptr);
switch_instr = LLVMBuildSwitch(p->builder, tag.value, else_block->block, cast(unsigned)num_cases);
if (switch_kind == TypeSwitch_Any && is_arch_wasm()) {
// Only do the if-else chain on wasm targets
} else {
switch_instr = LLVMBuildSwitch(p->builder, tag.value, else_block->block, cast(unsigned)num_cases);
}
}

bool all_by_reference = false;
Expand Down Expand Up @@ -2025,30 +2039,42 @@ gb_internal void lb_build_type_switch_stmt(lbProcedure *p, AstTypeSwitchStmt *ss
}
lbValue backing_ptr = backing_data.addr;

for (Ast *clause : body->stmts) {

Ast *default_clause = nullptr;

for_array(i, body->stmts) {
Ast *clause = body->stmts[i];
ast_node(cc, CaseClause, clause);

Entity *case_entity = implicit_entity_of_node(clause);
lb_open_scope(p, cc->scope);

if (cc->list.count == 0) {
lb_start_block(p, default_block);
if (case_entity->flags & EntityFlag_Value) {
lb_store_type_case_implicit(p, clause, parent_value, true);
} else {
lb_store_type_case_implicit(p, clause, parent_ptr, true);
default_clause = clause;
if (switch_instr != nullptr) {
lb_open_scope(p, cc->scope);

lb_start_block(p, default_block);
if (case_entity->flags & EntityFlag_Value) {
lb_store_type_case_implicit(p, clause, parent_value, true);
} else {
lb_store_type_case_implicit(p, clause, parent_ptr, true);
}
lb_type_case_body(p, ss->label, clause, p->curr_block, done);
}
lb_type_case_body(p, ss->label, clause, p->curr_block, done);
continue;
}

lbBlock *body = lb_create_block(p, "typeswitch.body");
lb_open_scope(p, cc->scope);

lbBlock *body = body_blocks[i];
if (p->debug_info != nullptr) {
LLVMSetCurrentDebugLocation2(p->builder, lb_debug_location_from_ast(p, clause));
}

lbBlock *next_cond = nullptr;
bool saw_nil = false;
for (Ast *type_expr : cc->list) {
for_array(i, cc->list) {
Ast *type_expr = cc->list[i];
Type *case_type = type_of_expr(type_expr);
lbValue on_val = {};
if (switch_kind == TypeSwitch_Union) {
Expand All @@ -2064,7 +2090,17 @@ gb_internal void lb_build_type_switch_stmt(lbProcedure *p, AstTypeSwitchStmt *ss
}
}
GB_ASSERT(on_val.value != nullptr);
LLVMAddCase(switch_instr, on_val.value, body->block);

if (switch_instr != nullptr) {
LLVMAddCase(switch_instr, on_val.value, body->block);
continue;
}

lbValue cond = lb_emit_comp(p, Token_CmpEq, on_val, tag);

next_cond = lb_create_block(p, "typeswitch.case.next");
lb_emit_if(p, cond, body, next_cond);
lb_start_block(p, next_cond);
}


Expand Down Expand Up @@ -2111,6 +2147,29 @@ gb_internal void lb_build_type_switch_stmt(lbProcedure *p, AstTypeSwitchStmt *ss
}

lb_type_case_body(p, ss->label, clause, body, done);

if (next_cond != nullptr) {
GB_ASSERT(switch_instr == nullptr);
lb_start_block(p, next_cond);
}
}

if (default_block != nullptr && switch_instr == nullptr) {
GB_ASSERT(default_clause != nullptr);
lb_emit_jump(p, default_block);

Entity *case_entity = implicit_entity_of_node(default_clause);
ast_node(cc, CaseClause, default_clause);

lb_open_scope(p, cc->scope);

lb_start_block(p, else_block);
if (case_entity->flags & EntityFlag_Value) {
lb_store_type_case_implicit(p, default_clause, parent_value, true);
} else {
lb_store_type_case_implicit(p, default_clause, parent_ptr, true);
}
lb_type_case_body(p, ss->label, default_clause, p->curr_block, done);
}

lb_emit_jump(p, done);
Expand Down