Skip to content

Commit 2989a25

Browse files
authored
Feature/global rvalue initialization petter tomner (#111)
* Use new initialization functions * Fix for new reflection patch * Fix for new TLS patch
1 parent ddb015a commit 2989a25

File tree

6 files changed

+41
-54
lines changed

6 files changed

+41
-54
lines changed

Cargo.lock

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/builder.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
200200
fn check_ptr_call<'b>(&mut self, _typ: &str, func_ptr: RValue<'gcc>, args: &'b [RValue<'gcc>]) -> Cow<'b, [RValue<'gcc>]> {
201201
let mut all_args_match = true;
202202
let mut param_types = vec![];
203-
let gcc_func = func_ptr.get_type().is_function_ptr_type().expect("function ptr");
203+
let gcc_func = func_ptr.get_type().dyncast_function_ptr_type().expect("function ptr");
204204
for (index, arg) in args.iter().enumerate().take(gcc_func.get_param_count()) {
205205
let param = gcc_func.get_param_type(index);
206206
if param != arg.get_type() {
@@ -277,7 +277,7 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
277277

278278
// gccjit requires to use the result of functions, even when it's not used.
279279
// That's why we assign the result to a local or call add_eval().
280-
let gcc_func = func_ptr.get_type().is_function_ptr_type().expect("function ptr");
280+
let gcc_func = func_ptr.get_type().dyncast_function_ptr_type().expect("function ptr");
281281
let mut return_type = gcc_func.get_return_type();
282282
let current_block = self.current_block.borrow().expect("block");
283283
let void_type = self.context.new_type::<()>();
@@ -810,7 +810,10 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
810810
let atomic_load = self.context.get_builtin_function(&format!("__atomic_load_{}", size.bytes()));
811811
let ordering = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
812812

813-
let volatile_const_void_ptr_type = self.context.new_type::<*mut ()>().make_const().make_volatile();
813+
let volatile_const_void_ptr_type = self.context.new_type::<()>()
814+
.make_const()
815+
.make_volatile()
816+
.make_pointer();
814817
let ptr = self.context.new_cast(None, ptr, volatile_const_void_ptr_type);
815818
self.context.new_call(None, atomic_load, &[ptr, ordering])
816819
}
@@ -935,7 +938,9 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
935938
// TODO(antoyo): handle alignment.
936939
let atomic_store = self.context.get_builtin_function(&format!("__atomic_store_{}", size.bytes()));
937940
let ordering = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
938-
let volatile_const_void_ptr_type = self.context.new_type::<*mut ()>().make_const().make_volatile();
941+
let volatile_const_void_ptr_type = self.context.new_type::<()>()
942+
.make_volatile()
943+
.make_pointer();
939944
let ptr = self.context.new_cast(None, ptr, volatile_const_void_ptr_type);
940945

941946
// FIXME(antoyo): fix libgccjit to allow comparing an integer type with an aligned integer type because
@@ -975,12 +980,12 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
975980
assert_eq!(idx as usize as u64, idx);
976981
let value = ptr.dereference(None).to_rvalue();
977982

978-
if value_type.is_array().is_some() {
983+
if value_type.dyncast_array().is_some() {
979984
let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
980985
let element = self.context.new_array_access(None, value, index);
981986
element.get_address(None)
982987
}
983-
else if let Some(vector_type) = value_type.is_vector() {
988+
else if let Some(vector_type) = value_type.dyncast_vector() {
984989
let array_type = vector_type.get_element_type().make_pointer();
985990
let array = self.bitcast(ptr, array_type);
986991
let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
@@ -1003,7 +1008,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
10031008

10041009
fn sext(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
10051010
// TODO(antoyo): check that it indeed sign extend the value.
1006-
if dest_ty.is_vector().is_some() {
1011+
if dest_ty.dyncast_vector().is_some() {
10071012
// TODO(antoyo): nothing to do as it is only for LLVM?
10081013
return value;
10091014
}
@@ -1075,7 +1080,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
10751080
let right_type = rhs.get_type();
10761081
if left_type != right_type {
10771082
// NOTE: because libgccjit cannot compare function pointers.
1078-
if left_type.is_function_ptr_type().is_some() && right_type.is_function_ptr_type().is_some() {
1083+
if left_type.dyncast_function_ptr_type().is_some() && right_type.dyncast_function_ptr_type().is_some() {
10791084
lhs = self.context.new_cast(None, lhs, self.usize_type.make_pointer());
10801085
rhs = self.context.new_cast(None, rhs, self.usize_type.make_pointer());
10811086
}
@@ -1183,12 +1188,12 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
11831188
assert_eq!(idx as usize as u64, idx);
11841189
let value_type = aggregate_value.get_type();
11851190

1186-
if value_type.is_array().is_some() {
1191+
if value_type.dyncast_array().is_some() {
11871192
let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
11881193
let element = self.context.new_array_access(None, aggregate_value, index);
11891194
element.get_address(None)
11901195
}
1191-
else if value_type.is_vector().is_some() {
1196+
else if value_type.dyncast_vector().is_some() {
11921197
panic!();
11931198
}
11941199
else if let Some(pointer_type) = value_type.get_pointee() {
@@ -1215,11 +1220,11 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
12151220
let value_type = aggregate_value.get_type();
12161221

12171222
let lvalue =
1218-
if value_type.is_array().is_some() {
1223+
if value_type.dyncast_array().is_some() {
12191224
let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
12201225
self.context.new_array_access(None, aggregate_value, index)
12211226
}
1222-
else if value_type.is_vector().is_some() {
1227+
else if value_type.dyncast_vector().is_some() {
12231228
panic!();
12241229
}
12251230
else if let Some(pointer_type) = value_type.get_pointee() {

src/common.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::convert::TryFrom;
2-
use std::convert::TryInto;
32

43
use gccjit::LValue;
54
use gccjit::{Block, CType, RValue, Type, ToRValue};
@@ -44,7 +43,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
4443
let string = self.context.new_string_literal(&*string);
4544
let sym = self.generate_local_symbol_name("str");
4645
let global = self.declare_private_global(&sym, self.val_ty(string));
47-
global.global_set_initializer_value(string);
46+
global.global_set_initializer_rvalue(string);
4847
global
4948
// TODO(antoyo): set linkage.
5049
}
@@ -79,7 +78,7 @@ pub fn bytes_in_context<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, bytes: &[u8]) ->
7978
bytes.iter()
8079
.map(|&byte| context.new_rvalue_from_int(byte_type, byte as i32))
8180
.collect();
82-
context.new_rvalue_from_array(None, typ, &elements)
81+
context.new_array_constructor(None, typ, &elements)
8382
}
8483

8584
pub fn type_is_pointer<'gcc>(typ: Type<'gcc>) -> bool {
@@ -120,13 +119,6 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
120119
}
121120

122121
fn const_uint_big(&self, typ: Type<'gcc>, num: u128) -> RValue<'gcc> {
123-
let num64: Result<i64, _> = num.try_into();
124-
if let Ok(num) = num64 {
125-
// FIXME(antoyo): workaround for a bug where libgccjit is expecting a constant.
126-
// The operations >> 64 and | low are making the normal case a non-constant.
127-
return self.context.new_rvalue_from_long(typ, num as i64);
128-
}
129-
130122
if num >> 64 != 0 {
131123
// FIXME(antoyo): use a new function new_rvalue_from_unsigned_long()?
132124
let low = self.context.new_rvalue_from_long(self.u64_type, num as u64 as i64);
@@ -193,7 +185,7 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
193185
// TODO(antoyo): cache the type? It's anonymous, so probably not.
194186
let typ = self.type_struct(&fields, packed);
195187
let struct_type = typ.is_struct().expect("struct type");
196-
self.context.new_rvalue_from_struct(None, struct_type, values)
188+
self.context.new_struct_constructor(None, struct_type.as_type(), None, values)
197189
}
198190

199191
fn const_to_opt_uint(&self, _v: RValue<'gcc>) -> Option<u64> {

src/consts.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
2020
pub fn const_bitcast(&self, value: RValue<'gcc>, typ: Type<'gcc>) -> RValue<'gcc> {
2121
if value.get_type() == self.bool_type.make_pointer() {
2222
if let Some(pointee) = typ.get_pointee() {
23-
if pointee.is_vector().is_some() {
23+
if pointee.dyncast_vector().is_some() {
2424
panic!()
2525
}
2626
}
@@ -81,7 +81,7 @@ impl<'gcc, 'tcx> StaticMethods for CodegenCx<'gcc, 'tcx> {
8181
else {
8282
value
8383
};
84-
global.global_set_initializer_value(value);
84+
global.global_set_initializer_rvalue(value);
8585

8686
// As an optimization, all shared statics which do not have interior
8787
// mutability are placed into read-only memory.
@@ -180,7 +180,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
180180
};
181181
// FIXME(antoyo): I think the name coming from generate_local_symbol_name() above cannot be used
182182
// globally.
183-
global.global_set_initializer_value(cv);
183+
global.global_set_initializer_rvalue(cv);
184184
// TODO(antoyo): set unnamed address.
185185
global.get_address(None)
186186
}
@@ -375,7 +375,7 @@ fn check_and_apply_linkage<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, attrs: &Codeg
375375
real_name.push_str(&sym);
376376
let global2 = cx.define_global(&real_name, llty, is_tls, attrs.link_section);
377377
// TODO(antoyo): set linkage.
378-
global2.global_set_initializer_value(global1.get_address(None));
378+
global2.global_set_initializer_rvalue(global1.get_address(None));
379379
// TODO(antoyo): use global_set_initializer() when it will work.
380380
global2
381381
}

src/context.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
use std::cell::{Cell, RefCell};
22

3-
use gccjit::{
4-
Block,
5-
Context,
6-
CType,
7-
Function,
8-
FunctionType,
9-
LValue,
10-
RValue,
11-
Struct,
12-
Type,
13-
};
3+
use gccjit::{Block, CType, Context, Function, FunctionType, LValue, RValue, Struct, Type};
144
use rustc_codegen_ssa::base::wants_msvc_seh;
155
use rustc_codegen_ssa::traits::{
166
BackendTypes,

src/type_.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl<'gcc, 'tcx> BaseTypeMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
122122
if typ.is_integral() {
123123
TypeKind::Integer
124124
}
125-
else if typ.is_vector().is_some() {
125+
else if typ.dyncast_vector().is_some() {
126126
TypeKind::Vector
127127
}
128128
else {
@@ -141,10 +141,10 @@ impl<'gcc, 'tcx> BaseTypeMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
141141
}
142142

143143
fn element_type(&self, ty: Type<'gcc>) -> Type<'gcc> {
144-
if let Some(typ) = ty.is_array() {
144+
if let Some(typ) = ty.dyncast_array() {
145145
typ
146146
}
147-
else if let Some(vector_type) = ty.is_vector() {
147+
else if let Some(vector_type) = ty.dyncast_vector() {
148148
vector_type.get_element_type()
149149
}
150150
else if let Some(typ) = ty.get_pointee() {

0 commit comments

Comments
 (0)