Skip to content

Commit 5ae5252

Browse files
committed
Use new reflection API
1 parent 0443c3a commit 5ae5252

File tree

11 files changed

+40
-82
lines changed

11 files changed

+40
-82
lines changed

Cargo.toml

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

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

2019
target-lexicon = "0.10.0"
2120

gcc-test-backend/Cargo.lock

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

gcc-test-backend/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition = "2018"
77
[dependencies]
88
#depth1 = { path = "depth1" }
99
#mini_core = { path = "mini_core" }
10-
deflate = "0.7.19"
10+
#deflate = "0.7.19"
1111

1212
[profile.dev]
1313
panic = "abort"

gcc-test-backend/src/main.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,14 @@ fn main() {
128128
129129
println!("{}", leaves.capacity());*/
130130

131+
println!("Hello, world!");
131132

132-
let mut leaves: Vec<Node> = Vec::with_capacity(286);
133+
/*let mut leaves: Vec<Node> = Vec::with_capacity(286);
133134
let frequencies/*: &[i32]*/ = &[526, 124, 128, 125, 107, 92, 84, 75, 88, 53, 63, 43, 54, 68, 56, 60, 38, 53, 37, 23, 36, 32, 21, 25, 20, 21, 27, 26, 16, 15, 20, 24, 22, 14, 12, 19, 19, 10, 15, 11, 13, 4, 15, 7, 6, 10, 10, 2, 1, 3, 3, 2, 2, 3, 2, 1, 1, 0, 0, 0, 2, 1, 0, 0, 0, 1, 3, 0, 1, 2, 3, 0, 2, 1, 4, 0, 1, 1, 1, 2, 2, 2, 3, 1, 1, 0, 0, 3, 0, 1, 0, 2, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 2, 1, 0, 2, 0, 0, 1, 0, 2, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 2, 0, 0, 1, 1, 0, 2, 0, 1, 1, 0, 0, 1, 2, 0, 2, 1, 1, 2, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 2, 0, 0, 0, 2, 1, 2, 0, 2, 0, 0, 1, 0, 0, 0, 2, 1, 0, 0, 1, 0, 0, 2, 0, 2, 0, 4, 6, 3, 4, 8, 7, 2, 7, 6, 8, 5, 8, 8, 17, 11, 16, 10, 8, 7, 13, 9, 14, 18, 12, 10, 19, 12, 11, 17, 15, 13, 17, 14, 17, 23, 31, 44, 27, 25, 30, 18, 17, 20, 25, 72, 52, 55, 69, 87, 75, 1, 7754, 5054, 3016, 2120, 1790, 1263, 606, 602, 958, 783, 481, 333, 510, 392, 211, 177, 169, 89, 58, 70, 110, 56, 37, 52, 63, 38, 48, 13, 1373];
134135
let len = 10;
135136
unsafe {
136137
println!("&{:?}[10] = {:?}", frequencies.as_ptr(), frequencies.as_ptr().add(len));
137-
}
138+
}*/
138139
/*leaves.extend(frequencies.iter().enumerate().filter_map(
139140
|(n, f)| if *f > 0 {
140141
Some(Node {

gcc_path

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
/home/bouanto/Ordinateur/Programmation/Projets/gcc-build/build/gcc
1+
/tmp/gcc-build/build/gcc

src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub fn compile_codegen_unit<'tcx>(tcx: TyCtxt<'tcx>, cgu_name: Symbol) -> (Modul
7373
//let llvm_module = ModuleLlvm::new(tcx, &cgu_name.as_str());
7474
let context = Context::default();
7575
//context.set_dump_code_on_compile(true);
76-
//context.set_dump_initial_gimple(true);
76+
context.set_dump_initial_gimple(true);
7777
context.set_debug_info(true);
7878
//context.set_dump_everything(true);
7979
//context.set_keep_intermediates(true);

src/builder.rs

+25-33
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl<'gcc, 'tcx> Builder<'_, 'gcc, 'tcx> {
131131
Cow::Owned(casted_args)
132132
}
133133

134-
fn check_ptr_call<'b>(&mut self, typ: &str, func: RValue<'gcc>, args: &'b [RValue<'gcc>]) -> Cow<'b, [RValue<'gcc>]> {
134+
fn check_ptr_call<'b>(&mut self, typ: &str, func_ptr: RValue<'gcc>, args: &'b [RValue<'gcc>]) -> Cow<'b, [RValue<'gcc>]> {
135135
//let mut fn_ty = self.cx.val_ty(func);
136136
// Strip off pointers
137137
/*while self.cx.type_kind(fn_ty) == TypeKind::Pointer {
@@ -154,9 +154,9 @@ impl<'gcc, 'tcx> Builder<'_, 'gcc, 'tcx> {
154154

155155
let mut all_args_match = true;
156156
let mut param_types = vec![];
157-
let gcc_func: Function<'gcc> = self.cx.rvalue_as_function(func);
157+
let gcc_func = func_ptr.get_type().is_function_ptr_type().expect("function ptr");
158158
for (index, arg) in args.iter().enumerate().take(gcc_func.get_param_count()) {
159-
let param = gcc_func.get_param(index as i32).to_rvalue().get_type();
159+
let param = gcc_func.get_param_type(index);
160160
if param != arg.get_type() {
161161
all_args_match = false;
162162
}
@@ -253,16 +253,16 @@ impl<'gcc, 'tcx> Builder<'_, 'gcc, 'tcx> {
253253
}
254254
}
255255

256-
fn function_ptr_call(&mut self, mut func: RValue<'gcc>, args: &[RValue<'gcc>], funclet: Option<&Funclet>) -> RValue<'gcc> {
256+
fn function_ptr_call(&mut self, mut func_ptr: RValue<'gcc>, args: &[RValue<'gcc>], funclet: Option<&Funclet>) -> RValue<'gcc> {
257257
//debug!("func ptr call {:?} with args ({:?})", func, args);
258258

259-
let args = self.check_ptr_call("call", func, args);
259+
let args = self.check_ptr_call("call", func_ptr, args);
260260
//let bundle = funclet.map(|funclet| funclet.bundle());
261261
//let bundle = bundle.as_ref().map(|b| &*b.raw);
262262

263263
// gccjit requires to use the result of functions, even when it's not used.
264264
// That's why we assign the result to a local or call add_eval().
265-
let gcc_func: Function<'gcc> = self.cx.rvalue_as_function(func);
265+
let gcc_func = func_ptr.get_type().is_function_ptr_type().expect("function ptr");
266266
let return_type = gcc_func.get_return_type();
267267
let current_block = self.current_block.borrow().expect("block");
268268
let void_type = self.context.new_type::<()>();
@@ -271,11 +271,11 @@ impl<'gcc, 'tcx> Builder<'_, 'gcc, 'tcx> {
271271
if return_type != void_type {
272272
unsafe { RETURN_VALUE_COUNT += 1 };
273273
let result = current_func.new_local(None, return_type, &format!("returnValue{}", unsafe { RETURN_VALUE_COUNT }));
274-
current_block.add_assignment(None, result, self.cx.context.new_call_through_ptr(None, func, &args));
274+
current_block.add_assignment(None, result, self.cx.context.new_call_through_ptr(None, func_ptr, &args));
275275
result.to_rvalue()
276276
}
277277
else {
278-
current_block.add_eval(None, self.cx.context.new_call_through_ptr(None, func, &args));
278+
current_block.add_eval(None, self.cx.context.new_call_through_ptr(None, func_ptr, &args));
279279
// Return dummy value when not having return value.
280280
self.context.new_rvalue_from_long(self.isize_type, 0)
281281
}
@@ -751,16 +751,6 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
751751

752752
fn alloca(&mut self, ty: Type<'gcc>, align: Align) -> RValue<'gcc> {
753753
let aligned_type = ty.get_aligned(align.bytes());
754-
let type_fields = self.fields.borrow().get(&ty).cloned();
755-
// TODO: remove these conditions when libgccjit has a reflection API.
756-
if self.vector_types.borrow().contains_key(&ty) {
757-
let value = self.vector_types.borrow().get(&ty).expect("vector type").clone();
758-
self.vector_types.borrow_mut().insert(aligned_type, value);
759-
}
760-
else if let Some(fields) = type_fields {
761-
self.fields.borrow_mut().insert(aligned_type, fields);
762-
}
763-
764754
// TODO: It might be better to return a LValue, but fixing the rustc API is non-trivial.
765755
self.current_func().new_local(None, aligned_type, "stack_var").get_address(None)
766756
}
@@ -1027,7 +1017,9 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
10271017
let element = self.context.new_array_access(None, value, index);
10281018
element.get_address(None)
10291019
}
1030-
else if let Some(&(count, element_type)) = self.vector_types.borrow().get(&value_type) {
1020+
else if let Some(vector_type) = value_type.is_vector() {
1021+
let count = vector_type.get_num_units();
1022+
let element_type = vector_type.get_element_type();
10311023
let indexes = vec![self.context.new_rvalue_from_long(element_type, i64::try_from(idx).expect("i64::try_from")); count as usize];
10321024
let indexes = self.context.new_rvalue_from_vector(None, value_type, &indexes);
10331025
let variable = self.current_func.borrow().expect("func")
@@ -1036,11 +1028,11 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
10361028
.add_assignment(None, variable, value + indexes);
10371029
variable.get_address(None)
10381030
}
1031+
else if let Some(struct_type) = value_type.is_struct() {
1032+
ptr.dereference_field(None, struct_type.get_field(idx as i32)).get_address(None)
1033+
}
10391034
else {
1040-
let fields = self.fields.borrow();
1041-
let fields = &fields.get(&value_type)
1042-
.unwrap_or_else(|| panic!("Structure {:?} ({:?}) not in fields", value, value.get_type()));
1043-
ptr.dereference_field(None, fields[idx as usize]).get_address(None)
1035+
panic!("Unexpected type {:?}", value_type);
10441036
}
10451037
}
10461038

@@ -1241,14 +1233,14 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
12411233
let element = self.context.new_array_access(None, aggregate_value, index);
12421234
element.get_address(None)
12431235
}
1244-
else if let Some(&(count, element_type)) = self.vector_types.borrow().get(&value_type) {
1236+
else if value_type.is_vector().is_some() {
12451237
panic!();
12461238
}
1239+
else if let Some(struct_type) = value_type.is_struct() {
1240+
aggregate_value.access_field(None, struct_type.get_field(idx as i32)).to_rvalue()
1241+
}
12471242
else {
1248-
let fields = self.fields.borrow();
1249-
let fields = &fields.get(&value_type)
1250-
.unwrap_or_else(|| panic!("Structure {:?} not in fields", aggregate_value.get_type()));
1251-
aggregate_value.access_field(None, fields[idx as usize]).to_rvalue()
1243+
panic!("Unexpected type {:?}", value_type);
12521244
}
12531245
/*assert_eq!(idx as c_uint as u64, idx);
12541246
unsafe { llvm::LLVMBuildExtractValue(self.llbuilder, agg_val, idx as c_uint, UNNAMED) }*/
@@ -1264,14 +1256,14 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
12641256
let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
12651257
self.context.new_array_access(None, aggregate_value, index)
12661258
}
1267-
else if let Some(&(count, element_type)) = self.vector_types.borrow().get(&value_type) {
1259+
else if value_type.is_vector().is_some() {
12681260
panic!();
12691261
}
1262+
else if let Some(struct_type) = value_type.is_struct() {
1263+
aggregate_value.access_field(None, struct_type.get_field(idx as i32))
1264+
}
12701265
else {
1271-
let fields = self.fields.borrow();
1272-
let fields = &fields.get(&value_type)
1273-
.unwrap_or_else(|| panic!("Structure {:?} not in fields", aggregate_value.get_type()));
1274-
aggregate_value.access_field(None, fields[idx as usize])
1266+
panic!("Unexpected type {:?}", value_type);
12751267
};
12761268
self.llbb().add_assignment(None, lvalue, value);
12771269

src/common.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,10 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
187187
let name = fields.iter().map(|typ| format!("{:?}", typ)).collect::<Vec<_>>().join("_");
188188
let typ = self.type_struct(&fields, packed);
189189
let structure = self.global_init_func.new_local(None, typ, &name);
190-
let fields = &self.fields.borrow()[&typ];
191-
for (value, field) in values.iter().zip(fields.iter()) {
192-
let field_lvalue = structure.access_field(None, *field);
190+
let struct_type = typ.is_struct().expect("struct type");
191+
for (index, value) in values.iter().enumerate() {
192+
let field = struct_type.get_field(index as i32);
193+
let field_lvalue = structure.access_field(None, field);
193194
self.global_init_block.add_assignment(None, field_lvalue, *value);
194195
}
195196
self.lvalue_to_rvalue(structure)

src/context.rs

-4
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ pub struct CodegenCx<'gcc, 'tcx> {
8383
pub types: RefCell<FxHashMap<(Ty<'tcx>, Option<VariantIdx>), Type<'gcc>>>,
8484
pub tcx: TyCtxt<'tcx>,
8585

86-
pub fields: RefCell<FxHashMap<Type<'gcc>, Vec<Field<'gcc>>>>,
87-
pub vector_types: RefCell<FxHashMap<Type<'gcc>, (u64, Type<'gcc>)>>,
8886
pub struct_types: RefCell<FxHashMap<Vec<Type<'gcc>>, Type<'gcc>>>,
8987

9088
pub types_with_fields_to_set: RefCell<FxHashMap<Type<'gcc>, (Struct<'gcc>, TyAndLayout<'tcx>)>>,
@@ -212,8 +210,6 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
212210
scalar_types: Default::default(),
213211
types: Default::default(),
214212
tcx,
215-
fields: Default::default(),
216-
vector_types: Default::default(),
217213
struct_types: Default::default(),
218214
types_with_fields_to_set: Default::default(),
219215
local_gen_sym_counter: Cell::new(0),

src/type_.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,15 @@ impl<'gcc, 'tcx> BaseTypeMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
120120
let name = types.iter().map(|typ| format!("{:?}", typ)).collect::<Vec<_>>().join("_");
121121
//let typ = self.context.new_struct_type(None, format!("struct{}", name), &fields).as_type();
122122
let typ = self.context.new_struct_type(None, "struct", &fields).as_type();
123-
self.fields.borrow_mut().insert(typ, fields);
124123
self.struct_types.borrow_mut().insert(types, typ);
125124
typ
126125
}
127126

128127
fn type_kind(&self, typ: Type<'gcc>) -> TypeKind {
129-
if typ.is_int() {
128+
if typ.is_integral() {
130129
TypeKind::Integer
131130
}
132-
else if typ.is_vector() {
131+
else if typ.is_vector().is_some() {
133132
TypeKind::Vector
134133
}
135134
else {
@@ -219,17 +218,14 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
219218
.map(|field| self.context.new_field(None, *field, "field"))
220219
.collect();
221220
typ.set_fields(None, &fields);
222-
self.fields.borrow_mut().insert(typ.as_type(), fields);
223221
}
224222

225223
fn type_struct(&self, fields: &[Type<'gcc>], packed: bool) -> Type<'gcc> {
226224
// TODO: use packed.
227225
let fields: Vec<_> = fields.iter()
228226
.map(|field| self.context.new_field(None, *field, "field"))
229227
.collect();
230-
let typ = self.context.new_struct_type(None, "unnamedStruct", &fields).as_type();
231-
self.fields.borrow_mut().insert(typ, fields);
232-
typ
228+
return self.context.new_struct_type(None, "unnamedStruct", &fields).as_type();
233229
}
234230

235231
pub fn type_named_struct(&self, name: &str) -> Struct<'gcc> {

src/type_of.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ pub fn uncached_gcc_type<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, layout: TyAndLa
4444
Abi::Scalar(_) => bug!("handled elsewhere"),
4545
Abi::Vector { ref element, count } => {
4646
let element = layout.scalar_gcc_type_at(cx, element, Size::ZERO);
47-
let vector_type = cx.context.new_vector_type(element, count);
48-
cx.vector_types.borrow_mut().insert(vector_type, (count, element));
49-
return vector_type
47+
return cx.context.new_vector_type(element, count);
5048
},
5149
Abi::ScalarPair(..) => {
5250
return cx.type_struct(

0 commit comments

Comments
 (0)