diff --git a/src/abi.rs b/src/abi.rs index f76aaba1d95..0b2b1807099 100644 --- a/src/abi.rs +++ b/src/abi.rs @@ -13,11 +13,11 @@ enum PassMode { } impl PassMode { - fn get_param_ty(self, _fx: &FunctionCx) -> Type { + fn get_param_ty(self, fx: &FunctionCx) -> 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(), } } } @@ -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) { @@ -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(), @@ -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) } } @@ -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 { @@ -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, @@ -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, }; @@ -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]; diff --git a/src/base.rs b/src/base.rs index a17c49868ba..9880d39afc7 100644 --- a/src/base.rs +++ b/src/base.rs @@ -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); } @@ -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); } diff --git a/src/common.rs b/src/common.rs index 8227f93f43b..6c537d72f9b 100644 --- a/src/common.rs +++ b/src/common.rs @@ -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>, @@ -22,7 +31,7 @@ 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, @@ -30,17 +39,17 @@ pub fn cton_type_from_ty<'a, 'tcx: 'a>( 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; } @@ -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) @@ -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) } } } @@ -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( @@ -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> { @@ -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; } @@ -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) } diff --git a/src/constant.rs b/src/constant.rs index 3bbf74b0a31..5d7971bc235 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -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), @@ -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) } diff --git a/src/lib.rs b/src/lib.rs index a6648e76780..d40c820a4f7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -228,6 +228,7 @@ impl CodegenBackend for CraneliftCodegenBackend { if std::env::var("SHOULD_RUN").is_ok() { let mut jit_module: Module = Module::new(SimpleJITBuilder::new()); + assert_eq!(pointer_ty(tcx), jit_module.pointer_type()); codegen_mono_items(tcx, &mut jit_module, &mono_items); @@ -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, }; @@ -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); @@ -443,10 +445,10 @@ fn maybe_create_entry_wrapper(cx: &mut CodegenCx) { 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, }; @@ -471,8 +473,8 @@ fn maybe_create_entry_wrapper(cx: &mut CodegenCx) { 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); @@ -490,7 +492,7 @@ fn maybe_create_entry_wrapper(cx: &mut CodegenCx) { .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])