Skip to content

Commit

Permalink
Don't hardcode usize being 64 bit (fixes rust-lang#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Aug 22, 2018
1 parent 8a1c864 commit 954d1e7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 32 deletions.
18 changes: 9 additions & 9 deletions src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ enum PassMode {
}

impl PassMode {
fn get_param_ty(self, _fx: &FunctionCx<impl Backend>) -> Type {
fn get_param_ty(self, fx: &FunctionCx<impl Backend>) -> Type {
match self {
PassMode::NoPass => unimplemented!("pass mode nopass"),
PassMode::ByVal(cton_type) => cton_type,
PassMode::ByRef => types::I64,
PassMode::ByRef => fx.module.pointer_type(),
}
}
}
Expand Down Expand Up @@ -74,7 +74,7 @@ pub fn cton_sig_from_fn_ty<'a, 'tcx: 'a>(
.filter_map(|ty| match get_pass_mode(tcx, sig.abi, ty, false) {
PassMode::ByVal(cton_ty) => Some(cton_ty),
PassMode::NoPass => unimplemented!("pass mode nopass"),
PassMode::ByRef => Some(types::I64),
PassMode::ByRef => Some(pointer_ty(tcx)),
});

let (params, returns) = match get_pass_mode(tcx, sig.abi, output, true) {
Expand All @@ -85,7 +85,7 @@ pub fn cton_sig_from_fn_ty<'a, 'tcx: 'a>(
),
PassMode::ByRef => {
(
Some(types::I64).into_iter() // First param is place to put return val
Some(pointer_ty(tcx)).into_iter() // First param is place to put return val
.chain(inputs)
.map(AbiParam::new)
.collect(),
Expand Down Expand Up @@ -224,7 +224,7 @@ impl<'a, 'tcx: 'a, B: Backend + 'a> FunctionCx<'a, 'tcx, B> {
if let Some(val) = self.lib_call(name, input_tys, return_ty, &args) {
CValue::ByVal(val, return_layout)
} else {
CValue::ByRef(self.bcx.ins().iconst(types::I64, 0), return_layout)
CValue::ByRef(self.bcx.ins().iconst(self.module.pointer_type(), 0), return_layout)
}
}

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

enum ArgKind {
Expand Down Expand Up @@ -305,7 +305,7 @@ pub fn codegen_fn_prelude<'a, 'tcx: 'a>(

match output_pass_mode {
PassMode::NoPass => {
let null = fx.bcx.ins().iconst(types::I64, 0);
let null = fx.bcx.ins().iconst(fx.module.pointer_type(), 0);
//unimplemented!("pass mode nopass");
fx.local_map.insert(
RETURN_PLACE,
Expand Down Expand Up @@ -457,7 +457,7 @@ pub fn codegen_call<'a, 'tcx: 'a>(
PassMode::NoPass => None,
PassMode::ByRef => match destination {
Some((place, _)) => Some(place.expect_addr()),
None => Some(fx.bcx.ins().iconst(types::I64, 0)),
None => Some(fx.bcx.ins().iconst(fx.module.pointer_type(), 0)),
},
PassMode::ByVal(_) => None,
};
Expand Down Expand Up @@ -570,7 +570,7 @@ fn codegen_intrinsic_call<'a, 'tcx: 'a>(
"copy" | "copy_nonoverlapping" => {
let elem_ty = substs.type_at(0);
let elem_size: u64 = fx.layout_of(elem_ty).size.bytes();
let elem_size = fx.bcx.ins().iconst(types::I64, elem_size as i64);
let elem_size = fx.bcx.ins().iconst(fx.module.pointer_type(), elem_size as i64);
assert_eq!(args.len(), 3);
let src = args[0];
let dst = args[1];
Expand Down
4 changes: 2 additions & 2 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ fn trans_stmt<'a, 'tcx: 'a>(
Rvalue::Repeat(operand, times) => {
let operand = trans_operand(fx, operand);
for i in 0..*times {
let index = fx.bcx.ins().iconst(types::I64, i as i64);
let index = fx.bcx.ins().iconst(fx.module.pointer_type(), i as i64);
let to = lval.place_index(fx, index);
to.write_cvalue(fx, operand);
}
Expand All @@ -498,7 +498,7 @@ fn trans_stmt<'a, 'tcx: 'a>(
AggregateKind::Array(_ty) => {
for (i, operand) in operands.into_iter().enumerate() {
let operand = trans_operand(fx, operand);
let index = fx.bcx.ins().iconst(types::I64, i as i64);
let index = fx.bcx.ins().iconst(fx.module.pointer_type(), i as i64);
let to = lval.place_index(fx, index);
to.write_cvalue(fx, operand);
}
Expand Down
29 changes: 19 additions & 10 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ pub fn mir_var(loc: Local) -> Variable {
Variable::with_u32(loc.index() as u32)
}

pub fn pointer_ty(tcx: TyCtxt) -> types::Type {
match tcx.data_layout.pointer_size.bits() {
16 => types::I16,
32 => types::I32,
64 => types::I64,
bits => bug!("ptr_sized_integer: unknown pointer bit size {}", bits),
}
}

pub fn cton_type_from_ty<'a, 'tcx: 'a>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
ty: Ty<'tcx>,
Expand All @@ -22,25 +31,25 @@ pub fn cton_type_from_ty<'a, 'tcx: 'a>(
UintTy::U32 => types::I32,
UintTy::U64 => types::I64,
UintTy::U128 => unimpl!("u128"),
UintTy::Usize => types::I64,
UintTy::Usize => pointer_ty(tcx),
},
TypeVariants::TyInt(size) => match size {
IntTy::I8 => types::I8,
IntTy::I16 => types::I16,
IntTy::I32 => types::I32,
IntTy::I64 => types::I64,
IntTy::I128 => unimpl!("i128"),
IntTy::Isize => types::I64,
IntTy::Isize => pointer_ty(tcx)
},
TypeVariants::TyChar => types::I32,
TypeVariants::TyFloat(size) => match size {
FloatTy::F32 => types::F32,
FloatTy::F64 => types::F64,
},
TypeVariants::TyFnPtr(_) => types::I64,
TypeVariants::TyFnPtr(_) => pointer_ty(tcx),
TypeVariants::TyRawPtr(TypeAndMut { ty, mutbl: _ }) | TypeVariants::TyRef(_, ty, _) => {
if ty.is_sized(tcx.at(DUMMY_SP), ParamEnv::reveal_all()) {
types::I64
pointer_ty(tcx)
} else {
return None;
}
Expand All @@ -59,7 +68,7 @@ fn codegen_field<'a, 'tcx: 'a>(
let field_offset = layout.fields.offset(field.index());
let field_ty = layout.field(&*fx, field.index());
if field_offset.bytes() > 0 {
let field_offset = fx.bcx.ins().iconst(types::I64, field_offset.bytes() as i64);
let field_offset = fx.bcx.ins().iconst(fx.module.pointer_type(), field_offset.bytes() as i64);
(fx.bcx.ins().iadd(base, field_offset), field_ty)
} else {
(base, field_ty)
Expand Down Expand Up @@ -93,7 +102,7 @@ impl<'tcx> CValue<'tcx> {
offset: None,
});
fx.bcx.ins().stack_store(value, stack_slot, 0);
fx.bcx.ins().stack_addr(types::I64, stack_slot, 0)
fx.bcx.ins().stack_addr(fx.module.pointer_type(), stack_slot, 0)
}
}
}
Expand Down Expand Up @@ -179,7 +188,7 @@ impl<'a, 'tcx: 'a> CPlace<'tcx> {
size: layout.size.bytes() as u32,
offset: None,
});
CPlace::Addr(fx.bcx.ins().stack_addr(types::I64, stack_slot, 0), layout)
CPlace::Addr(fx.bcx.ins().stack_addr(fx.module.pointer_type(), stack_slot, 0), layout)
}

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

pub fn to_cvalue(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> CValue<'tcx> {
Expand Down Expand Up @@ -249,7 +258,7 @@ impl<'a, 'tcx: 'a> CPlace<'tcx> {
let byte = fx
.bcx
.ins()
.load(types::I64, MemFlags::new(), from.0, offset);
.load(fx.module.pointer_type(), MemFlags::new(), from.0, offset);
fx.bcx.ins().store(MemFlags::new(), byte, addr, offset);
offset += 8;
}
Expand Down Expand Up @@ -299,7 +308,7 @@ impl<'a, 'tcx: 'a> CPlace<'tcx> {
let size = fx
.bcx
.ins()
.iconst(types::I64, elem_layout.size.bytes() as i64);
.iconst(fx.module.pointer_type(), elem_layout.size.bytes() as i64);
let offset = fx.bcx.ins().imul(size, index);
CPlace::Addr(fx.bcx.ins().iadd(addr, offset), elem_layout)
}
Expand Down
4 changes: 2 additions & 2 deletions src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ fn trans_const_value<'a, 'tcx: 'a>(
let func_ref = fx.get_function_ref(
Instance::resolve(fx.tcx, ParamEnv::reveal_all(), def_id, substs).unwrap(),
);
let func_addr = fx.bcx.ins().func_addr(types::I64, func_ref);
let func_addr = fx.bcx.ins().func_addr(fx.module.pointer_type(), func_ref);
CValue::ByVal(func_addr, layout)
}
_ => trans_const_place(fx, const_).to_cvalue(fx),
Expand Down Expand Up @@ -152,7 +152,7 @@ fn cplace_for_dataid<'a, 'tcx: 'a>(
data_id: DataId,
) -> CPlace<'tcx> {
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
let global_ptr = fx.bcx.ins().global_value(types::I64, local_data_id);
let global_ptr = fx.bcx.ins().global_value(fx.module.pointer_type(), local_data_id);
let layout = fx.layout_of(fx.monomorphize(&ty));
CPlace::Addr(global_ptr, layout)
}
Expand Down
20 changes: 11 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ impl CodegenBackend for CraneliftCodegenBackend {

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

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

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

let sig = Signature {
params: vec![
AbiParam::new(types::I64 /*usize*/),
AbiParam::new(types::I64 /* *const _*/),
AbiParam::new(jit_module.pointer_type()),
AbiParam::new(jit_module.pointer_type()),
],
returns: vec![AbiParam::new(types::I64 /*isize*/)],
returns: vec![AbiParam::new(jit_module.pointer_type() /*isize*/)],
call_conv: CallConv::SystemV,
argument_bytes: None,
};
Expand Down Expand Up @@ -269,6 +270,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
FaerieBuilder::default_libcall_names(),
).unwrap(),
);
assert_eq!(pointer_ty(tcx), faerie_module.pointer_type());

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

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

let cmain_sig = Signature {
params: vec![
AbiParam::new(types::I64 /*usize*/),
AbiParam::new(types::I64 /* *const _*/),
AbiParam::new(m.pointer_type()),
AbiParam::new(m.pointer_type()),
],
returns: vec![AbiParam::new(types::I64 /*isize*/)],
returns: vec![AbiParam::new(m.pointer_type() /*isize*/)],
call_conv: CallConv::SystemV,
argument_bytes: None,
};
Expand All @@ -471,8 +473,8 @@ fn maybe_create_entry_wrapper(cx: &mut CodegenCx<impl Backend + 'static>) {

let ebb = bcx.create_ebb();
bcx.switch_to_block(ebb);
let arg_argc = bcx.append_ebb_param(ebb, types::I64 /*usize*/);
let arg_argv = bcx.append_ebb_param(ebb, types::I64 /* *const _*/);
let arg_argc = bcx.append_ebb_param(ebb, m.pointer_type());
let arg_argv = bcx.append_ebb_param(ebb, m.pointer_type());

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

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

let main_val = bcx.ins().func_addr(types::I64, main_func_ref);
let main_val = bcx.ins().func_addr(m.pointer_type(), main_func_ref);

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

0 comments on commit 954d1e7

Please sign in to comment.