Skip to content

Commit e82df2a

Browse files
committed
l2s: support switchinst
1 parent 7c4a114 commit e82df2a

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/frontends/llvm/l2s_instr.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,12 @@ EmittedInstr convert_instruction(Parser* p, Node* fn_or_bb, BodyBuilder* b, LLVM
140140
})
141141
};
142142
case LLVMBr: {
143-
unsigned n_successors = LLVMGetNumSuccessors(instr);
144-
LARRAY(LLVMBasicBlockRef , targets, n_successors);
145-
for (size_t i = 0; i < n_successors; i++)
143+
unsigned n_targets = LLVMGetNumSuccessors(instr);
144+
LARRAY(LLVMBasicBlockRef, targets, n_targets);
145+
for (size_t i = 0; i < n_targets; i++)
146146
targets[i] = LLVMGetSuccessor(instr, i);
147147
if (LLVMIsConditional(instr)) {
148-
assert(n_successors == 2);
148+
assert(n_targets == 2);
149149
const Node* condition = convert_value(p, LLVMGetCondition(instr));
150150
return (EmittedInstr) {
151151
.terminator = branch(a, (Branch) {
@@ -155,14 +155,31 @@ EmittedInstr convert_instruction(Parser* p, Node* fn_or_bb, BodyBuilder* b, LLVM
155155
})
156156
};
157157
} else {
158-
assert(n_successors == 1);
158+
assert(n_targets == 1);
159159
return (EmittedInstr) {
160160
.terminator = convert_jump(p, fn, fn_or_bb, targets[0])
161161
};
162162
}
163163
}
164-
case LLVMSwitch:
165-
goto unimplemented;
164+
case LLVMSwitch: {
165+
const Node* inspectee = convert_value(p, LLVMGetOperand(instr, 0));
166+
const Node* default_jump = convert_jump(p, fn, fn_or_bb, LLVMGetOperand(instr, 1));
167+
int n_targets = LLVMGetNumOperands(instr) / 2 - 1;
168+
LARRAY(const Node*, targets, n_targets);
169+
LARRAY(const Node*, literals, n_targets);
170+
for (size_t i = 0; i < n_targets; i++) {
171+
literals[i] = convert_value(p, LLVMGetOperand(instr, i * 2 + 2));
172+
targets[i] = convert_jump(p, fn, fn_or_bb, LLVMGetOperand(instr, i * 2 + 3));
173+
}
174+
return (EmittedInstr) {
175+
.terminator = br_switch(a, (Switch) {
176+
.switch_value = inspectee,
177+
.default_jump = default_jump,
178+
.case_values = nodes(a, n_targets, literals),
179+
.case_jumps = nodes(a, n_targets, targets)
180+
})
181+
};
182+
}
166183
case LLVMIndirectBr:
167184
goto unimplemented;
168185
case LLVMInvoke:

0 commit comments

Comments
 (0)