Skip to content

Commit aeca1d1

Browse files
committed
Removed ref requirement on BasicBlock since it's Copy
Fixes rust-lang#155
1 parent 8b3c0d3 commit aeca1d1

20 files changed

+152
-152
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl<'ctx> CodeGen<'ctx> {
7777
let function = self.module.add_function("sum", fn_type, None);
7878
let basic_block = self.context.append_basic_block(function, "entry");
7979

80-
self.builder.position_at_end(&basic_block);
80+
self.builder.position_at_end(basic_block);
8181

8282
let x = function.get_nth_param(0)?.into_int_value();
8383
let y = function.get_nth_param(1)?.into_int_value();

examples/jit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl<'ctx> CodeGen<'ctx> {
2828
let function = self.module.add_function("sum", fn_type, None);
2929
let basic_block = self.context.append_basic_block(function, "entry");
3030

31-
self.builder.position_at_end(&basic_block);
31+
self.builder.position_at_end(basic_block);
3232

3333
let x = function.get_nth_param(0)?.into_int_value();
3434
let y = function.get_nth_param(1)?.into_int_value();

examples/kaleidoscope/main.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ impl<'a, 'ctx> Compiler<'a, 'ctx> {
868868

869869
match entry.get_first_instruction() {
870870
Some(first_instr) => builder.position_before(&first_instr),
871-
None => builder.position_at_end(&entry)
871+
None => builder.position_at_end(entry)
872872
}
873873

874874
builder.build_alloca(self.context.f64_type(), name)
@@ -1006,24 +1006,24 @@ impl<'a, 'ctx> Compiler<'a, 'ctx> {
10061006
let else_bb = self.context.append_basic_block(parent, "else");
10071007
let cont_bb = self.context.append_basic_block(parent, "ifcont");
10081008

1009-
self.builder.build_conditional_branch(cond, &then_bb, &else_bb);
1009+
self.builder.build_conditional_branch(cond, then_bb, else_bb);
10101010

10111011
// build then block
1012-
self.builder.position_at_end(&then_bb);
1012+
self.builder.position_at_end(then_bb);
10131013
let then_val = self.compile_expr(consequence)?;
1014-
self.builder.build_unconditional_branch(&cont_bb);
1014+
self.builder.build_unconditional_branch(cont_bb);
10151015

10161016
let then_bb = self.builder.get_insert_block().unwrap();
10171017

10181018
// build else block
1019-
self.builder.position_at_end(&else_bb);
1019+
self.builder.position_at_end(else_bb);
10201020
let else_val = self.compile_expr(alternative)?;
1021-
self.builder.build_unconditional_branch(&cont_bb);
1021+
self.builder.build_unconditional_branch(cont_bb);
10221022

10231023
let else_bb = self.builder.get_insert_block().unwrap();
10241024

10251025
// emit merge block
1026-
self.builder.position_at_end(&cont_bb);
1026+
self.builder.position_at_end(cont_bb);
10271027

10281028
let phi = self.builder.build_phi(self.context.f64_type(), "iftmp");
10291029

@@ -1046,8 +1046,8 @@ impl<'a, 'ctx> Compiler<'a, 'ctx> {
10461046
// go from current block to loop block
10471047
let loop_bb = self.context.append_basic_block(parent, "loop");
10481048

1049-
self.builder.build_unconditional_branch(&loop_bb);
1050-
self.builder.position_at_end(&loop_bb);
1049+
self.builder.build_unconditional_branch(loop_bb);
1050+
self.builder.position_at_end(loop_bb);
10511051

10521052
let old_val = self.variables.remove(var_name.as_str());
10531053

@@ -1073,8 +1073,8 @@ impl<'a, 'ctx> Compiler<'a, 'ctx> {
10731073
let end_cond = self.builder.build_float_compare(FloatPredicate::ONE, end_cond, self.context.f64_type().const_float(0.0), "loopcond");
10741074
let after_bb = self.context.append_basic_block(parent, "afterloop");
10751075

1076-
self.builder.build_conditional_branch(end_cond, &loop_bb, &after_bb);
1077-
self.builder.position_at_end(&after_bb);
1076+
self.builder.build_conditional_branch(end_cond, loop_bb, after_bb);
1077+
self.builder.position_at_end(after_bb);
10781078

10791079
self.variables.remove(var_name);
10801080

@@ -1120,7 +1120,7 @@ impl<'a, 'ctx> Compiler<'a, 'ctx> {
11201120

11211121
let entry = self.context.append_basic_block(function, "entry");
11221122

1123-
self.builder.position_at_end(&entry);
1123+
self.builder.position_at_end(entry);
11241124

11251125
// update fn field
11261126
self.fn_value_opt = Some(function);

src/basic_block.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,13 @@ impl BasicBlock {
162162
/// let basic_block1 = context.append_basic_block(function, "entry");
163163
/// let basic_block2 = context.append_basic_block(function, "next");
164164
///
165-
/// basic_block2.move_before(&basic_block1);
165+
/// basic_block2.move_before(basic_block1);
166166
///
167167
/// assert!(basic_block1.get_next_basic_block().is_none());
168168
/// assert_eq!(basic_block2.get_next_basic_block().unwrap(), basic_block1);
169169
/// ```
170170
// REVIEW: What happens if blocks are from different scopes?
171-
pub fn move_before(&self, basic_block: &BasicBlock) -> Result<(), ()> {
171+
pub fn move_before(&self, basic_block: BasicBlock) -> Result<(), ()> {
172172
// This method is UB if the parent no longer exists, so we must check for parent (or encode into type system)
173173
if self.get_parent().is_none() || basic_block.get_parent().is_none() {
174174
return Err(());
@@ -199,13 +199,13 @@ impl BasicBlock {
199199
/// let basic_block1 = context.append_basic_block(function, "entry");
200200
/// let basic_block2 = context.append_basic_block(function, "next");
201201
///
202-
/// basic_block1.move_after(&basic_block2);
202+
/// basic_block1.move_after(basic_block2);
203203
///
204204
/// assert!(basic_block1.get_next_basic_block().is_none());
205205
/// assert_eq!(basic_block2.get_next_basic_block().unwrap(), basic_block1);
206206
/// ```
207207
// REVIEW: What happens if blocks are from different scopes?
208-
pub fn move_after(&self, basic_block: &BasicBlock) -> Result<(), ()> {
208+
pub fn move_after(&self, basic_block: BasicBlock) -> Result<(), ()> {
209209
// This method is UB if the parent no longer exists, so we must check for parent (or encode into type system)
210210
if self.get_parent().is_none() || basic_block.get_parent().is_none() {
211211
return Err(());
@@ -235,7 +235,7 @@ impl BasicBlock {
235235
/// let function = module.add_function("do_nothing", fn_type, None);
236236
/// let basic_block = context.append_basic_block(function, "entry");
237237
///
238-
/// builder.position_at_end(&basic_block);
238+
/// builder.position_at_end(basic_block);
239239
/// builder.build_return(None);
240240
///
241241
/// assert_eq!(basic_block.get_first_instruction().unwrap().get_opcode(), InstructionOpcode::Return);
@@ -269,7 +269,7 @@ impl BasicBlock {
269269
/// let function = module.add_function("do_nothing", fn_type, None);
270270
/// let basic_block = context.append_basic_block(function, "entry");
271271
///
272-
/// builder.position_at_end(&basic_block);
272+
/// builder.position_at_end(basic_block);
273273
/// builder.build_return(None);
274274
///
275275
/// assert_eq!(basic_block.get_last_instruction().unwrap().get_opcode(), InstructionOpcode::Return);
@@ -303,7 +303,7 @@ impl BasicBlock {
303303
/// let function = module.add_function("do_nothing", fn_type, None);
304304
/// let basic_block = context.append_basic_block(function, "entry");
305305
///
306-
/// builder.position_at_end(&basic_block);
306+
/// builder.position_at_end(basic_block);
307307
/// builder.build_return(None);
308308
///
309309
/// assert_eq!(basic_block.get_terminator().unwrap().get_opcode(), InstructionOpcode::Return);

src/builder.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl<'ctx> Builder<'ctx> {
5454
/// let entry = context.append_basic_block(fn_value, "entry");
5555
/// let i32_arg = fn_value.get_first_param().unwrap();
5656
///
57-
/// builder.position_at_end(&entry);
57+
/// builder.position_at_end(entry);
5858
/// builder.build_return(Some(&i32_arg));
5959
/// ```
6060
pub fn build_return(&self, value: Option<&dyn BasicValue<'ctx>>) -> InstructionValue<'ctx> {
@@ -85,7 +85,7 @@ impl<'ctx> Builder<'ctx> {
8585
/// let fn_value = module.add_function("ret", fn_type, None);
8686
/// let entry = context.append_basic_block(fn_value, "entry");
8787
///
88-
/// builder.position_at_end(&entry);
88+
/// builder.position_at_end(entry);
8989
/// builder.build_aggregate_return(&[i32_three.into(), i32_seven.into()]);
9090
/// ```
9191
pub fn build_aggregate_return(&self, values: &[BasicValueEnum<'ctx>]) -> InstructionValue<'ctx> {
@@ -118,7 +118,7 @@ impl<'ctx> Builder<'ctx> {
118118
/// let entry = context.append_basic_block(fn_value, "entry");
119119
/// let i32_arg = fn_value.get_first_param().unwrap();
120120
///
121-
/// builder.position_at_end(&entry);
121+
/// builder.position_at_end(entry);
122122
///
123123
/// let ret_val = builder.build_call(fn_value, &[i32_arg], "call")
124124
/// .try_as_basic_value()
@@ -228,7 +228,7 @@ impl<'ctx> Builder<'ctx> {
228228
/// let i32_ptr_param1 = fn_value.get_first_param().unwrap().into_pointer_value();
229229
/// let i32_ptr_param2 = fn_value.get_nth_param(1).unwrap().into_pointer_value();
230230
///
231-
/// builder.position_at_end(&entry);
231+
/// builder.position_at_end(entry);
232232
/// builder.build_ptr_diff(i32_ptr_param1, i32_ptr_param2, "diff");
233233
/// builder.build_return(None);
234234
/// ```
@@ -278,7 +278,7 @@ impl<'ctx> Builder<'ctx> {
278278
/// let entry = context.append_basic_block(fn_value, "entry");
279279
/// let i32_ptr_param = fn_value.get_first_param().unwrap().into_pointer_value();
280280
///
281-
/// builder.position_at_end(&entry);
281+
/// builder.position_at_end(entry);
282282
/// builder.build_store(i32_ptr_param, i32_seven);
283283
/// builder.build_return(None);
284284
/// ```
@@ -309,7 +309,7 @@ impl<'ctx> Builder<'ctx> {
309309
/// let entry = context.append_basic_block(fn_value, "entry");
310310
/// let i32_ptr_param = fn_value.get_first_param().unwrap().into_pointer_value();
311311
///
312-
/// builder.position_at_end(&entry);
312+
/// builder.position_at_end(entry);
313313
///
314314
/// let pointee = builder.build_load(i32_ptr_param, "load");
315315
///
@@ -524,7 +524,7 @@ impl<'ctx> Builder<'ctx> {
524524
/// let entry = context.append_basic_block(fn_value, "entry");
525525
/// let i32_arg = fn_value.get_first_param().unwrap();
526526
///
527-
/// builder.position_at_end(&entry);
527+
/// builder.position_at_end(entry);
528528
///
529529
/// builder.build_bitcast(i32_arg, f32_type, "i32tof32");
530530
/// builder.build_return(None);
@@ -834,7 +834,7 @@ impl<'ctx> Builder<'ctx> {
834834
/// let n = function.get_nth_param(1).unwrap().into_int_value();
835835
/// let entry_block = context.append_basic_block(function, "entry");
836836
///
837-
/// builder.position_at_end(&entry_block);
837+
/// builder.position_at_end(entry_block);
838838
///
839839
/// let shift = builder.build_left_shift(value, n, "left_shift"); // value << n
840840
///
@@ -906,7 +906,7 @@ impl<'ctx> Builder<'ctx> {
906906
/// let n = function.get_nth_param(1).unwrap().into_int_value();
907907
/// let entry_block = context.append_basic_block(function, "entry");
908908
///
909-
/// builder.position_at_end(&entry_block);
909+
/// builder.position_at_end(entry_block);
910910
///
911911
/// // Whether or not your right shift is sign extended (true) or logical (false) depends
912912
/// // on the boolean input parameter:
@@ -1078,7 +1078,7 @@ impl<'ctx> Builder<'ctx> {
10781078
<<T::BaseType as FloatMathType>::MathConvType as IntMathType>::ValueType::new(value)
10791079
}
10801080

1081-
pub fn build_unconditional_branch(&self, destination_block: &BasicBlock) -> InstructionValue<'ctx> {
1081+
pub fn build_unconditional_branch(&self, destination_block: BasicBlock) -> InstructionValue<'ctx> {
10821082
let value = unsafe {
10831083
LLVMBuildBr(self.builder, destination_block.basic_block)
10841084
};
@@ -1089,8 +1089,8 @@ impl<'ctx> Builder<'ctx> {
10891089
pub fn build_conditional_branch(
10901090
&self,
10911091
comparison: IntValue<'ctx>,
1092-
then_block: &BasicBlock,
1093-
else_block: &BasicBlock,
1092+
then_block: BasicBlock,
1093+
else_block: BasicBlock,
10941094
) -> InstructionValue<'ctx> {
10951095
let value = unsafe {
10961096
LLVMBuildCondBr(self.builder, comparison.as_value_ref(), then_block.basic_block, else_block.basic_block)
@@ -1102,7 +1102,7 @@ impl<'ctx> Builder<'ctx> {
11021102
pub fn build_indirect_branch<BV: BasicValue<'ctx>>(
11031103
&self,
11041104
address: BV,
1105-
destinations: &[&BasicBlock],
1105+
destinations: &[BasicBlock],
11061106
) -> InstructionValue<'ctx> {
11071107
let value = unsafe {
11081108
LLVMBuildIndirectBr(self.builder, address.as_value_ref(), destinations.len() as u32)
@@ -1175,7 +1175,7 @@ impl<'ctx> Builder<'ctx> {
11751175

11761176
// REVIEW: What if instruction and basic_block are completely unrelated?
11771177
// It'd be great if we could get the BB from the instruction behind the scenes
1178-
pub fn position_at(&self, basic_block: &BasicBlock, instruction: &InstructionValue<'ctx>) {
1178+
pub fn position_at(&self, basic_block: BasicBlock, instruction: &InstructionValue<'ctx>) {
11791179
unsafe {
11801180
LLVMPositionBuilder(self.builder, basic_block.basic_block, instruction.as_value_ref())
11811181
}
@@ -1187,7 +1187,7 @@ impl<'ctx> Builder<'ctx> {
11871187
}
11881188
}
11891189

1190-
pub fn position_at_end(&self, basic_block: &BasicBlock) {
1190+
pub fn position_at_end(&self, basic_block: BasicBlock) {
11911191
unsafe {
11921192
LLVMPositionBuilderAtEnd(self.builder, basic_block.basic_block);
11931193
}
@@ -1213,7 +1213,7 @@ impl<'ctx> Builder<'ctx> {
12131213
/// let builder = context.create_builder();
12141214
/// let entry = context.append_basic_block(fn_value, "entry");
12151215
///
1216-
/// builder.position_at_end(&entry);
1216+
/// builder.position_at_end(entry);
12171217
///
12181218
/// let array_alloca = builder.build_alloca(array_type, "array_alloca");
12191219
/// let array = builder.build_load(array_alloca, "array_load").into_array_value();
@@ -1275,7 +1275,7 @@ impl<'ctx> Builder<'ctx> {
12751275
/// let builder = context.create_builder();
12761276
/// let entry = context.append_basic_block(fn_value, "entry");
12771277
///
1278-
/// builder.position_at_end(&entry);
1278+
/// builder.position_at_end(entry);
12791279
///
12801280
/// let array_alloca = builder.build_alloca(array_type, "array_alloca");
12811281
/// let array = builder.build_load(array_alloca, "array_load").into_array_value();
@@ -1329,7 +1329,7 @@ impl<'ctx> Builder<'ctx> {
13291329
/// let entry = context.append_basic_block(fn_value, "entry");
13301330
/// let vector_param = fn_value.get_first_param().unwrap().into_vector_value();
13311331
///
1332-
/// builder.position_at_end(&entry);
1332+
/// builder.position_at_end(entry);
13331333
///
13341334
/// let extracted = builder.build_extract_element(vector_param, i32_zero, "insert");
13351335
///
@@ -1366,7 +1366,7 @@ impl<'ctx> Builder<'ctx> {
13661366
/// let entry = context.append_basic_block(fn_value, "entry");
13671367
/// let vector_param = fn_value.get_first_param().unwrap().into_vector_value();
13681368
///
1369-
/// builder.position_at_end(&entry);
1369+
/// builder.position_at_end(entry);
13701370
/// builder.build_insert_element(vector_param, i32_seven, i32_zero, "insert");
13711371
/// builder.build_return(None);
13721372
/// ```
@@ -1464,7 +1464,7 @@ impl<'ctx> Builder<'ctx> {
14641464
// REVIEW: Returning InstructionValue is the safe move here; but if the value means something
14651465
// (IE the result of the switch) it should probably return BasicValueEnum?
14661466
// SubTypes: I think value and case values must be the same subtype (maybe). Case value might need to be constants
1467-
pub fn build_switch(&self, value: IntValue<'ctx>, else_block: &BasicBlock, cases: &[(IntValue<'ctx>, &BasicBlock)]) -> InstructionValue<'ctx> {
1467+
pub fn build_switch(&self, value: IntValue<'ctx>, else_block: BasicBlock, cases: &[(IntValue<'ctx>, BasicBlock)]) -> InstructionValue<'ctx> {
14681468
let switch_value = unsafe {
14691469
LLVMBuildSwitch(self.builder, value.as_value_ref(), else_block.basic_block, cases.len() as u32)
14701470
};
@@ -1550,7 +1550,7 @@ impl<'ctx> Builder<'ctx> {
15501550
/// let entry = context.append_basic_block(fn_value, "entry");
15511551
/// let i32_ptr_param = fn_value.get_first_param().unwrap().into_pointer_value();
15521552
/// let builder = context.create_builder();
1553-
/// builder.position_at_end(&entry);
1553+
/// builder.position_at_end(entry);
15541554
/// builder.build_atomicrmw(AtomicRMWBinOp::Add, i32_ptr_param, i32_seven, AtomicOrdering::Unordered);
15551555
/// builder.build_return(None);
15561556
/// ```
@@ -1599,7 +1599,7 @@ impl<'ctx> Builder<'ctx> {
15991599
/// let i32_eight = i32_type.const_int(8, false);
16001600
/// let entry = context.append_basic_block(fn_value, "entry");
16011601
/// let builder = context.create_builder();
1602-
/// builder.position_at_end(&entry);
1602+
/// builder.position_at_end(entry);
16031603
/// builder.build_cmpxchg(i32_ptr_param, i32_seven, i32_eight, AtomicOrdering::AcquireRelease, AtomicOrdering::Monotonic);
16041604
/// builder.build_return(None);
16051605
/// ```

src/context.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use std::thread_local;
3131
// 1) Only one thread has access to the global context at a time.
3232
// 2) The thread has shared access across different points in the thread.
3333
// This is still technically unsafe because another program in the same process
34-
// could also be accessing the global context via the C API. `get_context` has been
34+
// could also be accessing the global context via the C API. `get_global` has been
3535
// marked unsafe for this reason. Iff this isn't the case then this should be fully safe.
3636
static GLOBAL_CTX: Lazy<Mutex<Context>> = Lazy::new(|| {
3737
let ctx = unsafe {
@@ -168,7 +168,7 @@ impl Context {
168168
/// let fn_val = module.add_function("my_fn", fn_type, None);
169169
/// let basic_block = context.append_basic_block(fn_val, "entry");
170170
///
171-
/// builder.position_at_end(&basic_block);
171+
/// builder.position_at_end(basic_block);
172172
/// builder.build_return(None);
173173
///
174174
/// let memory_buffer = module.write_bitcode_to_memory();
@@ -691,7 +691,7 @@ impl Context {
691691
/// let fn_value = module.add_function("my_func", fn_type, None);
692692
/// let entry_block = context.append_basic_block(fn_value, "entry");
693693
///
694-
/// builder.position_at_end(&entry_block);
694+
/// builder.position_at_end(entry_block);
695695
///
696696
/// let ret_instr = builder.build_return(None);
697697
///
@@ -731,7 +731,7 @@ impl Context {
731731
/// let fn_value = module.add_function("my_func", fn_type, None);
732732
/// let entry_block = context.append_basic_block(fn_value, "entry");
733733
///
734-
/// builder.position_at_end(&entry_block);
734+
/// builder.position_at_end(entry_block);
735735
///
736736
/// let ret_instr = builder.build_return(None);
737737
///

src/execution_engine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl<'ctx> ExecutionEngine<'ctx> {
162162
/// let f = module.add_function("test_fn", fnt, None);
163163
/// let b = context.append_basic_block(f, "entry");
164164
///
165-
/// builder.position_at_end(&b);
165+
/// builder.position_at_end(b);
166166
///
167167
/// let extf = module.add_function("sumf", ft.fn_type(&[ft.into(), ft.into()], false), None);
168168
///
@@ -274,7 +274,7 @@ impl<'ctx> ExecutionEngine<'ctx> {
274274
/// // Add the function to our module
275275
/// let f = module.add_function("test_fn", sig, None);
276276
/// let b = context.append_basic_block(f, "entry");
277-
/// builder.position_at_end(&b);
277+
/// builder.position_at_end(b);
278278
///
279279
/// // Insert a return statement
280280
/// let ret = double.const_float(64.0);

0 commit comments

Comments
 (0)