Skip to content

Commit 018747b

Browse files
committed
Add a method for emiting a switch.
1 parent d7a528c commit 018747b

File tree

5 files changed

+29
-14
lines changed

5 files changed

+29
-14
lines changed

src/librustc_codegen_llvm/builder.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ fn noname() -> *const c_char {
4646

4747
impl BackendTypes for Builder<'_, 'll, 'tcx> {
4848
type Value = <CodegenCx<'ll, 'tcx> as BackendTypes>::Value;
49+
type Switch = <CodegenCx<'ll, 'tcx> as BackendTypes>::Switch;
4950
type BasicBlock = <CodegenCx<'ll, 'tcx> as BackendTypes>::BasicBlock;
5051
type Type = <CodegenCx<'ll, 'tcx> as BackendTypes>::Type;
5152
type Funclet = <CodegenCx<'ll, 'tcx> as BackendTypes>::Funclet;
@@ -165,7 +166,7 @@ impl ControlFlowBuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
165166
}
166167
}
167168

168-
fn switch(
169+
fn switch_new(
169170
&mut self,
170171
v: &'ll Value,
171172
else_llbb: &'ll BasicBlock,
@@ -176,12 +177,14 @@ impl ControlFlowBuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
176177
}
177178
}
178179

179-
fn add_case(&mut self, s: &'ll Value, on_val: &'ll Value, dest: &'ll BasicBlock) {
180+
fn switch_add_case(&mut self, s: &mut &'ll Value, on_val: &'ll Value, dest: &'ll BasicBlock) {
180181
unsafe {
181-
llvm::LLVMAddCase(s, on_val, dest)
182+
llvm::LLVMAddCase(*s, on_val, dest)
182183
}
183184
}
184185

186+
fn switch_emit(&mut self, _: &'ll Value) {}
187+
185188
fn unreachable(&mut self) {
186189
self.count_insn("unreachable");
187190
unsafe {
@@ -506,15 +509,15 @@ impl MemoryBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
506509
}
507510

508511
fn write_operand_repeatedly(
509-
mut self,
512+
&mut self,
510513
cg_elem: OperandRef<'tcx, &'ll Value>,
511514
count: u64,
512515
dest: PlaceRef<'tcx, &'ll Value>,
513516
) -> Self {
514517
let zero = self.const_usize(0);
515518
let count = self.const_usize(count);
516-
let start = dest.project_index(&mut self, zero).llval;
517-
let end = dest.project_index(&mut self, count).llval;
519+
let start = dest.project_index(self, zero).llval;
520+
let end = dest.project_index(self, count).llval;
518521

519522
let mut header_bx = self.build_sibling_block("repeat_loop_header");
520523
let mut body_bx = self.build_sibling_block("repeat_loop_body");

src/librustc_codegen_llvm/common.rs

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ impl Funclet<'ll> {
8686

8787
impl BackendTypes for CodegenCx<'ll, 'tcx> {
8888
type Value = &'ll Value;
89+
type Switch = &'ll Value;
8990
type BasicBlock = &'ll BasicBlock;
9091
type Type = &'ll Type;
9192
type Funclet = Funclet<'ll>;

src/librustc_codegen_ssa/mir/block.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,20 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
214214
}
215215
} else {
216216
let (otherwise, targets) = targets.split_last().unwrap();
217-
let switch = bx.switch(discr.immediate(),
218-
helper.llblock(self, *otherwise),
219-
values.len());
217+
let mut switch = bx.switch_new(
218+
discr.immediate(),
219+
helper.llblock(self, *otherwise),
220+
values.len(),
221+
);
220222
let switch_llty = bx.immediate_backend_type(
221223
bx.layout_of(switch_ty)
222224
);
223225
for (&value, target) in values.iter().zip(targets) {
224226
let llval = bx.const_uint_big(switch_llty, value);
225227
let llbb = helper.llblock(self, *target);
226-
bx.add_case(switch, llval, llbb)
228+
bx.switch_add_case(&mut switch, llval, llbb)
227229
}
230+
bx.switch_emit(switch);
228231
}
229232
}
230233

src/librustc_codegen_ssa/traits/backend.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use syntax_pos::symbol::InternedString;
1414

1515
pub trait BackendTypes {
1616
type Value: CodegenObject;
17+
type Switch;
1718
type BasicBlock: Copy;
1819
type Type: CodegenObject;
1920
type Funclet;

src/librustc_codegen_ssa/traits/builder.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,20 @@ pub trait ControlFlowBuilderMethods<'a, 'tcx: 'a>: HasCodegen<'tcx> {
3636
then_llbb: Self::BasicBlock,
3737
else_llbb: Self::BasicBlock,
3838
);
39-
fn switch(
39+
fn switch_new(
4040
&mut self,
4141
v: Self::Value,
4242
else_llbb: Self::BasicBlock,
4343
num_cases: usize,
44-
) -> Self::Value;
45-
fn add_case(&mut self, s: Self::Value, on_val: Self::Value, dest: Self::BasicBlock);
44+
) -> Self::Switch;
45+
fn switch_add_case(
46+
&mut self,
47+
s: &mut Self::Switch,
48+
on_val: Self::Value,
49+
dest: Self::BasicBlock,
50+
);
51+
fn switch_emit(&mut self, s: Self::Switch);
52+
4653
fn unreachable(&mut self);
4754
}
4855

@@ -116,7 +123,7 @@ pub trait MemoryBuilderMethods<'tcx>: HasCodegen<'tcx> {
116123

117124
/// Called for Rvalue::Repeat when the elem is neither a ZST nor optimizable using memset.
118125
fn write_operand_repeatedly(
119-
self,
126+
&mut self,
120127
elem: OperandRef<'tcx, Self::Value>,
121128
count: u64,
122129
dest: PlaceRef<'tcx, Self::Value>,

0 commit comments

Comments
 (0)