@@ -140,12 +140,12 @@ EmittedInstr convert_instruction(Parser* p, Node* fn_or_bb, BodyBuilder* b, LLVM
140
140
})
141
141
};
142
142
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 ++ )
146
146
targets [i ] = LLVMGetSuccessor (instr , i );
147
147
if (LLVMIsConditional (instr )) {
148
- assert (n_successors == 2 );
148
+ assert (n_targets == 2 );
149
149
const Node * condition = convert_value (p , LLVMGetCondition (instr ));
150
150
return (EmittedInstr ) {
151
151
.terminator = branch (a , (Branch ) {
@@ -155,14 +155,31 @@ EmittedInstr convert_instruction(Parser* p, Node* fn_or_bb, BodyBuilder* b, LLVM
155
155
})
156
156
};
157
157
} else {
158
- assert (n_successors == 1 );
158
+ assert (n_targets == 1 );
159
159
return (EmittedInstr ) {
160
160
.terminator = convert_jump (p , fn , fn_or_bb , targets [0 ])
161
161
};
162
162
}
163
163
}
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
+ }
166
183
case LLVMIndirectBr :
167
184
goto unimplemented ;
168
185
case LLVMInvoke :
0 commit comments