Skip to content

Commit 729ecd9

Browse files
committed
Use new reflection API
1 parent 2538681 commit 729ecd9

File tree

7 files changed

+34
-118
lines changed

7 files changed

+34
-118
lines changed

Cargo.lock

-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ harness = false
1414

1515
[dependencies]
1616
#gccjit = "1.0.0"
17-
gccjit = { git = "https://github.com/antoyo/gccjit.rs", branch = "feature/rustc" }
17+
#gccjit = { git = "https://github.com/antoyo/gccjit.rs", branch = "feature/rustc" }
18+
gccjit = { path = "/home/bouanto/Ordinateur/Programmation/Rust/Projets/gccjit.rs" }
1819

1920
target-lexicon = "0.10.0"
2021

src/builder.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
599599

600600
fn not(&mut self, a: RValue<'gcc>) -> RValue<'gcc> {
601601
let operation =
602-
if a.get_type().is_bool(&self.cx) {
602+
if a.get_type().is_bool() {
603603
UnaryOp::LogicalNegate
604604
}
605605
else {
@@ -756,11 +756,8 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
756756
fn alloca(&mut self, ty: Type<'gcc>, align: Align) -> RValue<'gcc> {
757757
let aligned_type = ty.get_aligned(align.bytes());
758758
let type_fields = self.fields.borrow().get(&ty).cloned();
759-
// TODO: remove these conditiosn when libgccjit has a reflection API.
760-
if self.array_types.borrow().contains(&ty) {
761-
self.array_types.borrow_mut().insert(aligned_type);
762-
}
763-
else if self.vector_types.borrow().contains_key(&ty) {
759+
// TODO: remove these conditions when libgccjit has a reflection API.
760+
if self.vector_types.borrow().contains_key(&ty) {
764761
let value = self.vector_types.borrow().get(&ty).expect("vector type").clone();
765762
self.vector_types.borrow_mut().insert(aligned_type, value);
766763
}
@@ -1033,7 +1030,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
10331030
let value = ptr.dereference(None).to_rvalue();
10341031
let value_type = value.get_type();
10351032

1036-
if self.array_types.borrow().contains(&value_type) {
1033+
if value_type.is_array() {
10371034
let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
10381035
let element = self.context.new_array_access(None, value, index);
10391036
element.get_address(None)
@@ -1247,7 +1244,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
12471244
assert_eq!(idx as usize as u64, idx);
12481245
let value_type = aggregate_value.get_type();
12491246

1250-
if self.array_types.borrow().contains(&value_type) {
1247+
if value_type.is_array() {
12511248
let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
12521249
let element = self.context.new_array_access(None, aggregate_value, index);
12531250
element.get_address(None)
@@ -1271,7 +1268,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
12711268
let value_type = aggregate_value.get_type();
12721269

12731270
let lvalue =
1274-
if self.array_types.borrow().contains(&value_type) {
1271+
if value_type.is_array() {
12751272
let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
12761273
self.context.new_array_access(None, aggregate_value, index)
12771274
}
@@ -1469,7 +1466,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
14691466

14701467
fn zext(&mut self, value: RValue<'gcc>, dest_typ: Type<'gcc>) -> RValue<'gcc> {
14711468
// FIXME: this does not zero-extend.
1472-
if value.get_type().is_bool(&self.cx) && dest_typ.is_i8(&self.cx) {
1469+
if value.get_type().is_bool() && dest_typ.is_i8(&self.cx) {
14731470
// FIXME: hack because base::from_immediate converts i1 to i8.
14741471
// Fix the code in codegen_ssa::base::from_immediate.
14751472
return value;

src/common.rs

+18-50
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,7 @@ pub fn bytes_in_context<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, bytes: &[u8]) ->
9696
}
9797

9898
pub fn type_is_pointer<'gcc>(typ: Type<'gcc>) -> bool {
99-
// TODO: fix when having proper type reflection.
100-
// TODO: put all pointer types in a HashSet and check if it's there instead of relying on
101-
// string comparison.
102-
format!("{:?}", typ).ends_with('*')
99+
typ.get_pointee().is_some()
103100
}
104101

105102
impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
@@ -303,8 +300,6 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
303300
pub trait SignType<'gcc, 'tcx> {
304301
fn is_signed(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
305302
fn is_unsigned(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
306-
307-
fn to_unsigned(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
308303
}
309304

310305
impl<'gcc, 'tcx> SignType<'gcc, 'tcx> for Type<'gcc> {
@@ -315,31 +310,9 @@ impl<'gcc, 'tcx> SignType<'gcc, 'tcx> for Type<'gcc> {
315310
fn is_unsigned(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
316311
self.is_u8(cx) || self.is_u16(cx) || self.is_u32(cx) || self.is_u64(cx) || self.is_u128(cx)
317312
}
318-
319-
fn to_unsigned(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> {
320-
if self.is_i8(cx) {
321-
cx.u8_type
322-
}
323-
else if self.is_i16(cx) {
324-
cx.u16_type
325-
}
326-
else if self.is_i32(cx) {
327-
cx.u32_type
328-
}
329-
else if self.is_i64(cx) {
330-
cx.u64_type
331-
}
332-
else if self.is_i128(cx) {
333-
cx.u128_type
334-
}
335-
else {
336-
*self
337-
}
338-
}
339313
}
340314

341315
pub trait TypeReflection<'gcc, 'tcx> {
342-
fn is_bool(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
343316
fn is_uchar(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
344317
fn is_ushort(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
345318
fn is_uint(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
@@ -361,77 +334,72 @@ pub trait TypeReflection<'gcc, 'tcx> {
361334
fn is_f64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool;
362335
}
363336

364-
// TODO: remove all this when libgccjit has a reflection API.
365337
impl<'gcc, 'tcx> TypeReflection<'gcc, 'tcx> for Type<'gcc> {
366-
fn is_bool(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
367-
format!("{:?}", self).starts_with(&format!("{:?}", cx.bool_type))
368-
}
369-
370338
fn is_uchar(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
371-
format!("{:?}", self).starts_with(&format!("{:?}", cx.u8_type))
339+
self.get_original() == cx.u8_type
372340
}
373341

374342
fn is_ushort(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
375-
format!("{:?}", self).starts_with(&format!("{:?}", cx.u16_type))
343+
self.get_original() == cx.u16_type
376344
}
377345

378346
fn is_uint(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
379-
format!("{:?}", self).starts_with(&format!("{:?}", cx.uint_type))
347+
self.get_original() == cx.uint_type
380348
}
381349

382350
fn is_ulong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
383-
!self.is_ulonglong(cx) && format!("{:?}", self).starts_with(&format!("{:?}", cx.ulong_type))
351+
self.get_original() == cx.ulong_type
384352
}
385353

386354
fn is_ulonglong(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
387-
format!("{:?}", self).starts_with(&format!("{:?}", cx.ulonglong_type))
355+
self.get_original() == cx.ulonglong_type
388356
}
389357

390358
fn is_i8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
391-
format!("{:?}", self).starts_with(&format!("{:?}", cx.i8_type))
359+
self.get_original() == cx.i8_type
392360
}
393361

394362
fn is_u8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
395-
format!("{:?}", self).starts_with(&format!("{:?}", cx.u8_type))
363+
self.get_original() == cx.u8_type
396364
}
397365

398366
fn is_i16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
399-
format!("{:?}", self).starts_with(&format!("{:?}", cx.i16_type))
367+
self.get_original() == cx.i16_type
400368
}
401369

402370
fn is_u16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
403-
format!("{:?}", self).starts_with(&format!("{:?}", cx.u16_type))
371+
self.get_original() == cx.u16_type
404372
}
405373

406374
fn is_i32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
407-
format!("{:?}", self).starts_with(&format!("{:?}", cx.i32_type))
375+
self.get_original() == cx.i32_type
408376
}
409377

410378
fn is_u32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
411-
format!("{:?}", self).starts_with(&format!("{:?}", cx.u32_type))
379+
self.get_original() == cx.u32_type
412380
}
413381

414382
fn is_i64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
415-
format!("{:?}", self).starts_with(&format!("{:?}", cx.i64_type))
383+
self.get_original() == cx.i64_type
416384
}
417385

418386
fn is_u64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
419-
format!("{:?}", self).starts_with(&format!("{:?}", cx.u64_type))
387+
self.get_original() == cx.u64_type
420388
}
421389

422390
fn is_i128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
423-
format!("{:?}", self).starts_with(&format!("{:?}", cx.i128_type))
391+
self.get_original() == cx.i128_type
424392
}
425393

426394
fn is_u128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
427-
format!("{:?}", self).starts_with(&format!("{:?}", cx.u128_type))
395+
self.get_original() == cx.u128_type
428396
}
429397

430398
fn is_f32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
431-
format!("{:?}", self).starts_with(&format!("{:?}", cx.context.new_type::<f32>()))
399+
self.get_original() == cx.context.new_type::<f32>()
432400
}
433401

434402
fn is_f64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
435-
format!("{:?}", self).starts_with(&format!("{:?}", cx.context.new_type::<f64>()))
403+
self.get_original() == cx.context.new_type::<f64>()
436404
}
437405
}

src/context.rs

-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ pub struct CodegenCx<'gcc, 'tcx> {
8585
pub tcx: TyCtxt<'tcx>,
8686

8787
pub fields: RefCell<FxHashMap<Type<'gcc>, Vec<Field<'gcc>>>>,
88-
pub array_types: RefCell<FxHashSet<Type<'gcc>>>,
8988
pub vector_types: RefCell<FxHashMap<Type<'gcc>, (u64, Type<'gcc>)>>,
9089
pub struct_types: RefCell<FxHashMap<Vec<Type<'gcc>>, Type<'gcc>>>,
9190

@@ -216,7 +215,6 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
216215
types: Default::default(),
217216
tcx,
218217
fields: Default::default(),
219-
array_types: Default::default(),
220218
vector_types: Default::default(),
221219
struct_types: Default::default(),
222220
types_with_fields_to_set: Default::default(),

src/intrinsic.rs

+3-20
Original file line numberDiff line numberDiff line change
@@ -1246,24 +1246,7 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
12461246
}
12471247

12481248
fn int_width(&self, typ: Type<'gcc>) -> i64 {
1249-
if typ.is_i8(&self.cx) || typ.is_u8(&self.cx) {
1250-
8
1251-
}
1252-
else if typ.is_i16(&self.cx) || typ.is_u16(&self.cx) {
1253-
16
1254-
}
1255-
else if typ.is_i32(&self.cx) || typ.is_u32(&self.cx) {
1256-
32
1257-
}
1258-
else if typ.is_i64(&self.cx) || typ.is_u64(&self.cx) {
1259-
64
1260-
}
1261-
else if typ.is_i128(&self.cx) || typ.is_u128(&self.cx) {
1262-
128
1263-
}
1264-
else {
1265-
unimplemented!();
1266-
}
1249+
typ.get_size() as i64 * 8
12671250
}
12681251

12691252
fn overflow_intrinsic_call(&mut self, intrinsic: &str, lhs: RValue<'gcc>, rhs: RValue<'gcc>, result: &PlaceRef<'tcx, RValue<'gcc>>) {
@@ -1350,7 +1333,7 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
13501333

13511334
let then_block = func.new_block("then");
13521335

1353-
let unsigned_type = result_type.to_unsigned(&self.cx);
1336+
let unsigned_type = self.context.new_int_type(width as i32 / 8, false);
13541337
let shifted = self.context.new_cast(None, lhs, unsigned_type) >> self.context.new_rvalue_from_int(unsigned_type, width as i32 - 1);
13551338
let uint_max = self.context.new_unary_op(None, UnaryOp::BitwiseNegate, unsigned_type,
13561339
self.context.new_rvalue_from_int(unsigned_type, 0)
@@ -1412,7 +1395,7 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
14121395
let then_block = func.new_block("then");
14131396
let after_block = func.new_block("after");
14141397

1415-
let unsigned_type = result_type.to_unsigned(&self.cx);
1398+
let unsigned_type = self.context.new_int_type(width as i32 / 8, false);
14161399
let shifted = self.context.new_cast(None, lhs, unsigned_type) >> self.context.new_rvalue_from_int(unsigned_type, width as i32 - 1);
14171400
let uint_max = self.context.new_unary_op(None, UnaryOp::BitwiseNegate, unsigned_type,
14181401
self.context.new_rvalue_from_int(unsigned_type, 0)

src/type_.rs

+4-33
Original file line numberDiff line numberDiff line change
@@ -132,23 +132,13 @@ impl<'gcc, 'tcx> BaseTypeMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
132132
}
133133

134134
fn type_kind(&self, typ: Type<'gcc>) -> TypeKind {
135-
// TODO: find a better way to compare types without taking alignment into account.
136-
if typ.is_i8(self) || typ.is_u8(self) ||
137-
typ.is_i16(self) || typ.is_u16(self) ||
138-
typ.is_i32(self) || typ.is_u32(self) ||
139-
typ.is_i64(self) || typ.is_u64(self) ||
140-
typ.is_i128(self) || typ.is_u128(self)
141-
{
135+
if typ.is_int() {
142136
TypeKind::Integer
143137
}
144-
// TODO: find a better way to do that.
145-
else if format!("{:?}", typ).contains("__attribute__ ((vector_size") {
138+
else if typ.is_vector() {
146139
TypeKind::Vector
147140
}
148141
else {
149-
if format!("{:?}", typ).contains("vector_size") {
150-
panic!("missed vector type");
151-
}
152142
// TODO
153143
TypeKind::Void
154144
}
@@ -195,24 +185,7 @@ impl<'gcc, 'tcx> BaseTypeMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
195185
}
196186

197187
fn int_width(&self, typ: Type<'gcc>) -> u64 {
198-
if typ.is_i8(self) || typ.is_u8(self) {
199-
8
200-
}
201-
else if typ.is_i16(self) || typ.is_u16(self) {
202-
16
203-
}
204-
else if typ.is_i32(self) || typ.is_u32(self) {
205-
32
206-
}
207-
else if typ.is_i64(self) || typ.is_u64(self) {
208-
64
209-
}
210-
else if typ.is_i128(self) || typ.is_u128(self) {
211-
128
212-
}
213-
else {
214-
panic!("Cannot get width of int type {:?}", typ);
215-
}
188+
typ.get_size() as u64 * 8
216189
}
217190

218191
fn val_ty(&self, value: RValue<'gcc>) -> Type<'gcc> {
@@ -253,9 +226,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
253226
}
254227

255228
pub fn type_array(&self, ty: Type<'gcc>, len: u64) -> Type<'gcc> {
256-
let array_type = self.context.new_array_type(None, ty, len as i32);
257-
self.array_types.borrow_mut().insert(array_type);
258-
array_type
229+
self.context.new_array_type(None, ty, len as i32)
259230
}
260231
}
261232

0 commit comments

Comments
 (0)