Skip to content

Commit

Permalink
Implement int extension
Browse files Browse the repository at this point in the history
  • Loading branch information
sapir committed Aug 5, 2024
1 parent fb95096 commit cc2bbb8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,8 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
// TODO(antoyo): nothing to do as it is only for LLVM?
return value;
}
self.context.new_cast(self.location, value, dest_ty)

self.extend_int(value, dest_ty, true)
}

fn fptoui(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
Expand Down Expand Up @@ -1700,9 +1701,8 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
call
}

fn zext(&mut self, value: RValue<'gcc>, dest_typ: Type<'gcc>) -> RValue<'gcc> {
// FIXME(antoyo): this does not zero-extend.
self.gcc_int_cast(value, dest_typ)
fn zext(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
self.extend_int(value, dest_ty, false)
}

fn cx(&self) -> &CodegenCx<'gcc, 'tcx> {
Expand Down
27 changes: 26 additions & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ use rustc_data_structures::base_n::ToBaseN;
use rustc_data_structures::base_n::ALPHANUMERIC_ONLY;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_middle::mir::mono::CodegenUnit;
use rustc_middle::span_bug;
use rustc_middle::ty::layout::{
FnAbiError, FnAbiOf, FnAbiOfHelpers, FnAbiRequest, HasParamEnv, HasTyCtxt, LayoutError,
LayoutOfHelpers, TyAndLayout,
};
use rustc_middle::ty::{self, Instance, ParamEnv, PolyExistentialTraitRef, Ty, TyCtxt};
use rustc_middle::{bug, span_bug};
use rustc_session::Session;
use rustc_span::{source_map::respan, Span};
use rustc_target::abi::{
Expand Down Expand Up @@ -360,6 +360,31 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
function
}

pub fn get_matching_int_type(&self, typ: Type<'gcc>, signed: bool) -> Type<'gcc> {
let size = if typ.is_compatible_with(self.bool_type) {
// For booleans, return an 8-bit integer instead.
1
} else {
typ.get_size()
};

match (size, signed) {
(1, false) => self.u8_type,
(2, false) => self.u16_type,
(4, false) => self.u32_type,
(8, false) => self.u64_type,
(16, false) => self.u128_type,
(1, true) => self.i8_type,
(2, true) => self.i16_type,
(4, true) => self.i32_type,
(8, true) => self.i64_type,
(16, true) => self.i128_type,
_ => {
bug!("attempt to get bad int type {}{}", if signed { 'i' } else { 'u' }, size * 8);
}
}
}

pub fn is_native_int_type(&self, typ: Type<'gcc>) -> bool {
let types = [
self.u8_type,
Expand Down
16 changes: 16 additions & 0 deletions src/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use gccjit::{BinaryOp, ComparisonOp, FunctionType, Location, RValue, ToRValue, Type, UnaryOp};
use rustc_codegen_ssa::common::{IntPredicate, TypeKind};
use rustc_codegen_ssa::traits::{BackendTypes, BaseTypeMethods, BuilderMethods, OverflowOp};
use rustc_middle::bug;
use rustc_middle::ty::{ParamEnv, Ty};
use rustc_target::abi::{
call::{ArgAbi, ArgAttributes, Conv, FnAbi, PassMode},
Expand Down Expand Up @@ -866,6 +867,21 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
self.bitwise_operation(BinaryOp::BitwiseOr, a, b, loc)
}

pub fn extend_int(
&self,
value: RValue<'gcc>,
dest_ty: Type<'gcc>,
signed: bool,
) -> RValue<'gcc> {
let src_ty = value.get_type();
if !self.is_native_int_type_or_bool(src_ty) || !self.is_native_int_type_or_bool(dest_ty) {
bug!("got extend_int for non-int type {:?} -> {:?}", src_ty, dest_ty);
}

let intermediate_ty = self.get_matching_int_type(src_ty, signed);
self.gcc_int_cast(self.gcc_int_cast(value, intermediate_ty), dest_ty)
}

// TODO(antoyo): can we use https://github.com/rust-lang/compiler-builtins/blob/master/src/int/mod.rs#L379 instead?
pub fn gcc_int_cast(&self, value: RValue<'gcc>, dest_typ: Type<'gcc>) -> RValue<'gcc> {
let value_type = value.get_type();
Expand Down

0 comments on commit cc2bbb8

Please sign in to comment.