Skip to content

Commit a4fb579

Browse files
committed
Fix compilation of overflow addition
1 parent d466953 commit a4fb579

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/int.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
55
// cSpell:words cmpti divti modti mulodi muloti udivti umodti
66

7-
use gccjit::{BinaryOp, ComparisonOp, FunctionType, Location, RValue, ToRValue, Type, UnaryOp};
7+
use gccjit::{
8+
BinaryOp, CType, ComparisonOp, FunctionType, Location, RValue, ToRValue, Type, UnaryOp,
9+
};
810
use rustc_abi::{CanonAbi, Endian, ExternAbi};
911
use rustc_codegen_ssa::common::{IntPredicate, TypeKind};
1012
use rustc_codegen_ssa::traits::{BackendTypes, BaseTypeCodegenMethods, BuilderMethods, OverflowOp};
1113
use rustc_middle::ty::{self, Ty};
1214
use rustc_target::callconv::{ArgAbi, ArgAttributes, FnAbi, PassMode};
15+
use rustc_type_ir::{Interner, TyKind};
1316

1417
use crate::builder::{Builder, ToGccComp};
1518
use crate::common::{SignType, TypeReflection};
@@ -351,6 +354,9 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
351354
// TODO(antoyo): is it correct to use rhs type instead of the parameter typ?
352355
.new_local(self.___location, rhs.get_type(), "binopResult")
353356
.get_address(self.___location);
357+
let new_type = type_kind_to_gcc_type(new_kind);
358+
let new_type = self.context.new_c_type(new_type);
359+
let lhs = self.context.new_cast(self.___location, lhs, new_type);
354360
let overflow = self.overflow_call(intrinsic, &[lhs, rhs, res], None);
355361
(res.dereference(self.___location).to_rvalue(), overflow)
356362
}
@@ -1042,3 +1048,25 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
10421048
self.context.new_array_constructor(None, typ, &values)
10431049
}
10441050
}
1051+
1052+
fn type_kind_to_gcc_type<I: Interner>(kind: TyKind<I>) -> CType {
1053+
use rustc_middle::ty::IntTy::*;
1054+
use rustc_middle::ty::UintTy::*;
1055+
use rustc_middle::ty::{Int, Uint};
1056+
1057+
match kind {
1058+
Int(I8) => CType::Int8t,
1059+
Int(I16) => CType::Int16t,
1060+
Int(I32) => CType::Int32t,
1061+
Int(I64) => CType::Int64t,
1062+
Int(I128) => CType::Int128t,
1063+
1064+
Uint(U8) => CType::UInt8t,
1065+
Uint(U16) => CType::UInt16t,
1066+
Uint(U32) => CType::UInt32t,
1067+
Uint(U64) => CType::UInt64t,
1068+
Uint(U128) => CType::UInt128t,
1069+
1070+
_ => unimplemented!("Kind: {:?}", kind),
1071+
}
1072+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ extern crate rustc_session;
5151
extern crate rustc_span;
5252
extern crate rustc_symbol_mangling;
5353
extern crate rustc_target;
54+
extern crate rustc_type_ir;
5455

5556
// This prevents duplicating functions and statics that are already part of the host rustc process.
5657
#[allow(unused_extern_crates)]

0 commit comments

Comments
 (0)