Skip to content

Commit 1b93712

Browse files
committed
WIP: Fix const_undef
1 parent 15d23bd commit 1b93712

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

src/builder.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -539,9 +539,10 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
539539

540540
fn ret(&mut self, mut value: RValue<'gcc>) {
541541
if self.structs_as_pointer.borrow().contains(&value) {
542+
panic!();
542543
// NOTE: hack to workaround a limitation of the rustc API: see comment on
543544
// CodegenCx.structs_as_pointer
544-
value = value.dereference(self.location).to_rvalue();
545+
//value = value.dereference(self.location).to_rvalue();
545546
}
546547
let expected_return_type = self.current_func().get_return_type();
547548
if !expected_return_type.is_compatible_with(value.get_type()) {
@@ -1119,11 +1120,12 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
11191120
// TODO(antoyo)
11201121
}
11211122

1122-
fn store(&mut self, mut val: RValue<'gcc>, ptr: RValue<'gcc>, align: Align) -> RValue<'gcc> {
1123+
fn store(&mut self, val: RValue<'gcc>, ptr: RValue<'gcc>, align: Align) -> RValue<'gcc> {
11231124
if self.structs_as_pointer.borrow().contains(&val) {
1125+
panic!();
11241126
// NOTE: hack to workaround a limitation of the rustc API: see comment on
11251127
// CodegenCx.structs_as_pointer
1126-
val = val.dereference(self.location).to_rvalue();
1128+
//val = val.dereference(self.location).to_rvalue();
11271129
}
11281130

11291131
self.store_with_flags(val, ptr, align, MemFlags::empty())
@@ -1537,26 +1539,44 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
15371539
assert_eq!(idx as usize as u64, idx);
15381540
let value_type = aggregate_value.get_type();
15391541

1540-
let lvalue = if value_type.dyncast_array().is_some() {
1542+
if value_type.dyncast_array().is_some() {
15411543
let index = self
15421544
.context
15431545
.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
15441546
self.context.new_array_access(self.location, aggregate_value, index)
15451547
} else if value_type.dyncast_vector().is_some() {
15461548
panic!();
1547-
} else if let Some(pointer_type) = value_type.get_pointee() {
1548-
if let Some(struct_type) = pointer_type.is_struct() {
1549+
} else if let Some(_pointer_type) = value_type.get_pointee() {
1550+
unreachable!();
1551+
/*if let Some(struct_type) = pointer_type.is_struct() {
15491552
// NOTE: hack to workaround a limitation of the rustc API: see comment on
15501553
// CodegenCx.structs_as_pointer
15511554
aggregate_value.dereference_field(self.location, struct_type.get_field(idx as i32))
15521555
} else {
15531556
panic!("Unexpected type {:?}", value_type);
1554-
}
1557+
}*/
1558+
} else if let Some(struct_type) = value_type.is_struct() {
1559+
let new_val = self.current_func().new_local(None, value_type, "aggregate_value");
1560+
self.block.add_assignment(None, new_val, aggregate_value);
1561+
let lvalue = new_val.access_field(None, struct_type.get_field(idx as i32));
1562+
let lvalue_type = lvalue.to_rvalue().get_type();
1563+
let value =
1564+
// NOTE: sometimes, rustc will create a value with the wrong type.
1565+
if lvalue_type != value.get_type() {
1566+
self.context.new_cast(self.location, value, lvalue_type)
1567+
}
1568+
else {
1569+
value
1570+
};
1571+
self.block.add_assignment(None, lvalue, value);
1572+
return new_val.to_rvalue();
15551573
} else {
15561574
panic!("Unexpected type {:?}", value_type);
15571575
};
15581576

1559-
let lvalue_type = lvalue.to_rvalue().get_type();
1577+
panic!();
1578+
1579+
/*let lvalue_type = lvalue.to_rvalue().get_type();
15601580
let value =
15611581
// NOTE: sometimes, rustc will create a value with the wrong type.
15621582
if lvalue_type != value.get_type() {
@@ -1568,7 +1588,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
15681588
15691589
self.llbb().add_assignment(self.location, lvalue, value);
15701590
1571-
aggregate_value
1591+
aggregate_value*/
15721592
}
15731593

15741594
fn set_personality_fn(&mut self, _personality: Function<'gcc>) {

src/common.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,15 @@ impl<'gcc, 'tcx> ConstCodegenMethods for CodegenCx<'gcc, 'tcx> {
117117

118118
fn const_undef(&self, typ: Type<'gcc>) -> RValue<'gcc> {
119119
let local = self.current_func.borrow().expect("func").new_local(None, typ, "undefined");
120-
if typ.is_struct().is_some() {
120+
/*if typ.is_struct().is_some() {
121121
// NOTE: hack to workaround a limitation of the rustc API: see comment on
122122
// CodegenCx.structs_as_pointer
123123
let pointer = local.get_address(None);
124124
self.structs_as_pointer.borrow_mut().insert(pointer);
125125
pointer
126-
} else {
127-
local.to_rvalue()
128-
}
126+
} else {*/
127+
local.to_rvalue()
128+
//}
129129
}
130130

131131
fn const_poison(&self, typ: Type<'gcc>) -> RValue<'gcc> {

0 commit comments

Comments
 (0)