Skip to content

Commit 954d1e7

Browse files
committed
Don't hardcode usize being 64 bit (fixes #8)
1 parent 8a1c864 commit 954d1e7

File tree

5 files changed

+43
-32
lines changed

5 files changed

+43
-32
lines changed

src/abi.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ enum PassMode {
1313
}
1414

1515
impl PassMode {
16-
fn get_param_ty(self, _fx: &FunctionCx<impl Backend>) -> Type {
16+
fn get_param_ty(self, fx: &FunctionCx<impl Backend>) -> Type {
1717
match self {
1818
PassMode::NoPass => unimplemented!("pass mode nopass"),
1919
PassMode::ByVal(cton_type) => cton_type,
20-
PassMode::ByRef => types::I64,
20+
PassMode::ByRef => fx.module.pointer_type(),
2121
}
2222
}
2323
}
@@ -74,7 +74,7 @@ pub fn cton_sig_from_fn_ty<'a, 'tcx: 'a>(
7474
.filter_map(|ty| match get_pass_mode(tcx, sig.abi, ty, false) {
7575
PassMode::ByVal(cton_ty) => Some(cton_ty),
7676
PassMode::NoPass => unimplemented!("pass mode nopass"),
77-
PassMode::ByRef => Some(types::I64),
77+
PassMode::ByRef => Some(pointer_ty(tcx)),
7878
});
7979

8080
let (params, returns) = match get_pass_mode(tcx, sig.abi, output, true) {
@@ -85,7 +85,7 @@ pub fn cton_sig_from_fn_ty<'a, 'tcx: 'a>(
8585
),
8686
PassMode::ByRef => {
8787
(
88-
Some(types::I64).into_iter() // First param is place to put return val
88+
Some(pointer_ty(tcx)).into_iter() // First param is place to put return val
8989
.chain(inputs)
9090
.map(AbiParam::new)
9191
.collect(),
@@ -224,7 +224,7 @@ impl<'a, 'tcx: 'a, B: Backend + 'a> FunctionCx<'a, 'tcx, B> {
224224
if let Some(val) = self.lib_call(name, input_tys, return_ty, &args) {
225225
CValue::ByVal(val, return_layout)
226226
} else {
227-
CValue::ByRef(self.bcx.ins().iconst(types::I64, 0), return_layout)
227+
CValue::ByRef(self.bcx.ins().iconst(self.module.pointer_type(), 0), return_layout)
228228
}
229229
}
230230

@@ -253,7 +253,7 @@ pub fn codegen_fn_prelude<'a, 'tcx: 'a>(
253253
let ret_param = match output_pass_mode {
254254
PassMode::NoPass => None,
255255
PassMode::ByVal(_) => None,
256-
PassMode::ByRef => Some(fx.bcx.append_ebb_param(start_ebb, types::I64)),
256+
PassMode::ByRef => Some(fx.bcx.append_ebb_param(start_ebb, fx.module.pointer_type())),
257257
};
258258

259259
enum ArgKind {
@@ -305,7 +305,7 @@ pub fn codegen_fn_prelude<'a, 'tcx: 'a>(
305305

306306
match output_pass_mode {
307307
PassMode::NoPass => {
308-
let null = fx.bcx.ins().iconst(types::I64, 0);
308+
let null = fx.bcx.ins().iconst(fx.module.pointer_type(), 0);
309309
//unimplemented!("pass mode nopass");
310310
fx.local_map.insert(
311311
RETURN_PLACE,
@@ -457,7 +457,7 @@ pub fn codegen_call<'a, 'tcx: 'a>(
457457
PassMode::NoPass => None,
458458
PassMode::ByRef => match destination {
459459
Some((place, _)) => Some(place.expect_addr()),
460-
None => Some(fx.bcx.ins().iconst(types::I64, 0)),
460+
None => Some(fx.bcx.ins().iconst(fx.module.pointer_type(), 0)),
461461
},
462462
PassMode::ByVal(_) => None,
463463
};
@@ -570,7 +570,7 @@ fn codegen_intrinsic_call<'a, 'tcx: 'a>(
570570
"copy" | "copy_nonoverlapping" => {
571571
let elem_ty = substs.type_at(0);
572572
let elem_size: u64 = fx.layout_of(elem_ty).size.bytes();
573-
let elem_size = fx.bcx.ins().iconst(types::I64, elem_size as i64);
573+
let elem_size = fx.bcx.ins().iconst(fx.module.pointer_type(), elem_size as i64);
574574
assert_eq!(args.len(), 3);
575575
let src = args[0];
576576
let dst = args[1];

src/base.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ fn trans_stmt<'a, 'tcx: 'a>(
477477
Rvalue::Repeat(operand, times) => {
478478
let operand = trans_operand(fx, operand);
479479
for i in 0..*times {
480-
let index = fx.bcx.ins().iconst(types::I64, i as i64);
480+
let index = fx.bcx.ins().iconst(fx.module.pointer_type(), i as i64);
481481
let to = lval.place_index(fx, index);
482482
to.write_cvalue(fx, operand);
483483
}
@@ -498,7 +498,7 @@ fn trans_stmt<'a, 'tcx: 'a>(
498498
AggregateKind::Array(_ty) => {
499499
for (i, operand) in operands.into_iter().enumerate() {
500500
let operand = trans_operand(fx, operand);
501-
let index = fx.bcx.ins().iconst(types::I64, i as i64);
501+
let index = fx.bcx.ins().iconst(fx.module.pointer_type(), i as i64);
502502
let to = lval.place_index(fx, index);
503503
to.write_cvalue(fx, operand);
504504
}

src/common.rs

+19-10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ pub fn mir_var(loc: Local) -> Variable {
1010
Variable::with_u32(loc.index() as u32)
1111
}
1212

13+
pub fn pointer_ty(tcx: TyCtxt) -> types::Type {
14+
match tcx.data_layout.pointer_size.bits() {
15+
16 => types::I16,
16+
32 => types::I32,
17+
64 => types::I64,
18+
bits => bug!("ptr_sized_integer: unknown pointer bit size {}", bits),
19+
}
20+
}
21+
1322
pub fn cton_type_from_ty<'a, 'tcx: 'a>(
1423
tcx: TyCtxt<'a, 'tcx, 'tcx>,
1524
ty: Ty<'tcx>,
@@ -22,25 +31,25 @@ pub fn cton_type_from_ty<'a, 'tcx: 'a>(
2231
UintTy::U32 => types::I32,
2332
UintTy::U64 => types::I64,
2433
UintTy::U128 => unimpl!("u128"),
25-
UintTy::Usize => types::I64,
34+
UintTy::Usize => pointer_ty(tcx),
2635
},
2736
TypeVariants::TyInt(size) => match size {
2837
IntTy::I8 => types::I8,
2938
IntTy::I16 => types::I16,
3039
IntTy::I32 => types::I32,
3140
IntTy::I64 => types::I64,
3241
IntTy::I128 => unimpl!("i128"),
33-
IntTy::Isize => types::I64,
42+
IntTy::Isize => pointer_ty(tcx)
3443
},
3544
TypeVariants::TyChar => types::I32,
3645
TypeVariants::TyFloat(size) => match size {
3746
FloatTy::F32 => types::F32,
3847
FloatTy::F64 => types::F64,
3948
},
40-
TypeVariants::TyFnPtr(_) => types::I64,
49+
TypeVariants::TyFnPtr(_) => pointer_ty(tcx),
4150
TypeVariants::TyRawPtr(TypeAndMut { ty, mutbl: _ }) | TypeVariants::TyRef(_, ty, _) => {
4251
if ty.is_sized(tcx.at(DUMMY_SP), ParamEnv::reveal_all()) {
43-
types::I64
52+
pointer_ty(tcx)
4453
} else {
4554
return None;
4655
}
@@ -59,7 +68,7 @@ fn codegen_field<'a, 'tcx: 'a>(
5968
let field_offset = layout.fields.offset(field.index());
6069
let field_ty = layout.field(&*fx, field.index());
6170
if field_offset.bytes() > 0 {
62-
let field_offset = fx.bcx.ins().iconst(types::I64, field_offset.bytes() as i64);
71+
let field_offset = fx.bcx.ins().iconst(fx.module.pointer_type(), field_offset.bytes() as i64);
6372
(fx.bcx.ins().iadd(base, field_offset), field_ty)
6473
} else {
6574
(base, field_ty)
@@ -93,7 +102,7 @@ impl<'tcx> CValue<'tcx> {
93102
offset: None,
94103
});
95104
fx.bcx.ins().stack_store(value, stack_slot, 0);
96-
fx.bcx.ins().stack_addr(types::I64, stack_slot, 0)
105+
fx.bcx.ins().stack_addr(fx.module.pointer_type(), stack_slot, 0)
97106
}
98107
}
99108
}
@@ -179,7 +188,7 @@ impl<'a, 'tcx: 'a> CPlace<'tcx> {
179188
size: layout.size.bytes() as u32,
180189
offset: None,
181190
});
182-
CPlace::Addr(fx.bcx.ins().stack_addr(types::I64, stack_slot, 0), layout)
191+
CPlace::Addr(fx.bcx.ins().stack_addr(fx.module.pointer_type(), stack_slot, 0), layout)
183192
}
184193

185194
pub fn from_stack_slot(
@@ -188,7 +197,7 @@ impl<'a, 'tcx: 'a> CPlace<'tcx> {
188197
ty: Ty<'tcx>,
189198
) -> CPlace<'tcx> {
190199
let layout = fx.layout_of(ty);
191-
CPlace::Addr(fx.bcx.ins().stack_addr(types::I64, stack_slot, 0), layout)
200+
CPlace::Addr(fx.bcx.ins().stack_addr(fx.module.pointer_type(), stack_slot, 0), layout)
192201
}
193202

194203
pub fn to_cvalue(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> CValue<'tcx> {
@@ -249,7 +258,7 @@ impl<'a, 'tcx: 'a> CPlace<'tcx> {
249258
let byte = fx
250259
.bcx
251260
.ins()
252-
.load(types::I64, MemFlags::new(), from.0, offset);
261+
.load(fx.module.pointer_type(), MemFlags::new(), from.0, offset);
253262
fx.bcx.ins().store(MemFlags::new(), byte, addr, offset);
254263
offset += 8;
255264
}
@@ -299,7 +308,7 @@ impl<'a, 'tcx: 'a> CPlace<'tcx> {
299308
let size = fx
300309
.bcx
301310
.ins()
302-
.iconst(types::I64, elem_layout.size.bytes() as i64);
311+
.iconst(fx.module.pointer_type(), elem_layout.size.bytes() as i64);
303312
let offset = fx.bcx.ins().imul(size, index);
304313
CPlace::Addr(fx.bcx.ins().iadd(addr, offset), elem_layout)
305314
}

src/constant.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ fn trans_const_value<'a, 'tcx: 'a>(
109109
let func_ref = fx.get_function_ref(
110110
Instance::resolve(fx.tcx, ParamEnv::reveal_all(), def_id, substs).unwrap(),
111111
);
112-
let func_addr = fx.bcx.ins().func_addr(types::I64, func_ref);
112+
let func_addr = fx.bcx.ins().func_addr(fx.module.pointer_type(), func_ref);
113113
CValue::ByVal(func_addr, layout)
114114
}
115115
_ => trans_const_place(fx, const_).to_cvalue(fx),
@@ -152,7 +152,7 @@ fn cplace_for_dataid<'a, 'tcx: 'a>(
152152
data_id: DataId,
153153
) -> CPlace<'tcx> {
154154
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
155-
let global_ptr = fx.bcx.ins().global_value(types::I64, local_data_id);
155+
let global_ptr = fx.bcx.ins().global_value(fx.module.pointer_type(), local_data_id);
156156
let layout = fx.layout_of(fx.monomorphize(&ty));
157157
CPlace::Addr(global_ptr, layout)
158158
}

src/lib.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
228228

229229
if std::env::var("SHOULD_RUN").is_ok() {
230230
let mut jit_module: Module<SimpleJITBackend> = Module::new(SimpleJITBuilder::new());
231+
assert_eq!(pointer_ty(tcx), jit_module.pointer_type());
231232

232233
codegen_mono_items(tcx, &mut jit_module, &mono_items);
233234

@@ -238,10 +239,10 @@ impl CodegenBackend for CraneliftCodegenBackend {
238239

239240
let sig = Signature {
240241
params: vec![
241-
AbiParam::new(types::I64 /*usize*/),
242-
AbiParam::new(types::I64 /* *const _*/),
242+
AbiParam::new(jit_module.pointer_type()),
243+
AbiParam::new(jit_module.pointer_type()),
243244
],
244-
returns: vec![AbiParam::new(types::I64 /*isize*/)],
245+
returns: vec![AbiParam::new(jit_module.pointer_type() /*isize*/)],
245246
call_conv: CallConv::SystemV,
246247
argument_bytes: None,
247248
};
@@ -269,6 +270,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
269270
FaerieBuilder::default_libcall_names(),
270271
).unwrap(),
271272
);
273+
assert_eq!(pointer_ty(tcx), faerie_module.pointer_type());
272274

273275
codegen_mono_items(tcx, &mut faerie_module, &mono_items);
274276

@@ -443,10 +445,10 @@ fn maybe_create_entry_wrapper(cx: &mut CodegenCx<impl Backend + 'static>) {
443445

444446
let cmain_sig = Signature {
445447
params: vec![
446-
AbiParam::new(types::I64 /*usize*/),
447-
AbiParam::new(types::I64 /* *const _*/),
448+
AbiParam::new(m.pointer_type()),
449+
AbiParam::new(m.pointer_type()),
448450
],
449-
returns: vec![AbiParam::new(types::I64 /*isize*/)],
451+
returns: vec![AbiParam::new(m.pointer_type() /*isize*/)],
450452
call_conv: CallConv::SystemV,
451453
argument_bytes: None,
452454
};
@@ -471,8 +473,8 @@ fn maybe_create_entry_wrapper(cx: &mut CodegenCx<impl Backend + 'static>) {
471473

472474
let ebb = bcx.create_ebb();
473475
bcx.switch_to_block(ebb);
474-
let arg_argc = bcx.append_ebb_param(ebb, types::I64 /*usize*/);
475-
let arg_argv = bcx.append_ebb_param(ebb, types::I64 /* *const _*/);
476+
let arg_argc = bcx.append_ebb_param(ebb, m.pointer_type());
477+
let arg_argv = bcx.append_ebb_param(ebb, m.pointer_type());
476478

477479
let main_func_ref = m.declare_func_in_func(main_func_id, &mut bcx.func);
478480

@@ -490,7 +492,7 @@ fn maybe_create_entry_wrapper(cx: &mut CodegenCx<impl Backend + 'static>) {
490492
.declare_function(&start_name, Linkage::Import, &start_sig)
491493
.unwrap();
492494

493-
let main_val = bcx.ins().func_addr(types::I64, main_func_ref);
495+
let main_val = bcx.ins().func_addr(m.pointer_type(), main_func_ref);
494496

495497
let func_ref = m.declare_func_in_func(start_func_id, &mut bcx.func);
496498
bcx.ins().call(func_ref, &[main_val, arg_argc, arg_argv])

0 commit comments

Comments
 (0)