Skip to content

Commit b8c4933

Browse files
celinvaltedinski
authored andcommitted
Revert some changes to rust compiler code (rust-lang#42) (rust-lang#667)
* Use rustc SwitchTarget as is (rust-lang#42) There is no need to modify rustc code in order to access the SwitchTarget targets field. Change direct access to calls to all_targets(). * Remove old change made to rustc_driver (rust-lang#42) This is no longer needed. * Use rustc TyS as is (rust-lang#42) There is no need to access the kind and flags field of TyS directly. We should be using kind() and flags() methods instead. Thus, I'm removing the modification we made to the original rustc code to make these variables public and fixing the code that relied on the direct access. * Revert changes from ScalarInt Use conversion methods instead.
1 parent 60e56a9 commit b8c4933

File tree

7 files changed

+96
-85
lines changed

7 files changed

+96
-85
lines changed

compiler/rustc_codegen_rmc/src/codegen/assumptions.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ use cbmc::NO_PRETTY_NAME;
88
use rustc_middle::mir::interpret::{ConstValue, Scalar};
99
use rustc_middle::ty;
1010
use rustc_middle::ty::layout::LayoutOf;
11-
use rustc_middle::ty::ScalarInt;
1211
use rustc_middle::ty::Ty;
1312
use rustc_middle::ty::{IntTy, UintTy};
14-
use rustc_target::abi::{FieldsShape, Primitive, TagEncoding, Variants};
13+
use rustc_target::abi::{FieldsShape, Primitive, Size, TagEncoding, Variants};
1514

1615
fn fold_invariants_gen<F: Fn(Expr, Expr) -> Expr>(mut iv: Vec<Expr>, dfl: Expr, comb: F) -> Expr {
1716
let mut res: Option<Expr> = None;
@@ -446,8 +445,9 @@ impl<'tcx> GotocCtx<'tcx> {
446445
};
447446
for (_, discr) in def.discriminants(tcx.tcx) {
448447
let val = (discr.val << (128 - 8 * sz)) >> (128 - 8 * sz);
448+
let scalar = Scalar::from_uint(val, Size::from_bytes(sz));
449449
cases.push(case.clone().eq(tcx.codegen_const_value(
450-
ConstValue::Scalar(Scalar::Int(ScalarInt { data: val, size: sz })),
450+
ConstValue::Scalar(scalar),
451451
discr_t,
452452
None,
453453
)));

compiler/rustc_codegen_rmc/src/codegen/operand.rs

Lines changed: 84 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ use rustc_middle::mir::interpret::{
1010
};
1111
use rustc_middle::mir::{Constant, ConstantKind, Operand};
1212
use rustc_middle::ty::layout::LayoutOf;
13-
use rustc_middle::ty::{
14-
self, Const, ConstKind, FloatTy, Instance, IntTy, ScalarInt, Ty, Uint, UintTy,
15-
};
13+
use rustc_middle::ty::{self, Const, ConstKind, FloatTy, Instance, IntTy, Ty, Uint, UintTy};
1614
use rustc_span::def_id::DefId;
1715
use rustc_span::Span;
1816
use rustc_target::abi::{FieldsShape, Size, TagEncoding, Variants};
@@ -78,46 +76,67 @@ impl<'tcx> GotocCtx<'tcx> {
7876
}
7977
}
8078

81-
pub fn codegen_const_value(
79+
fn codegen_slice_value(
8280
&mut self,
8381
v: ConstValue<'tcx>,
8482
lit_ty: Ty<'tcx>,
8583
span: Option<&Span>,
84+
data: &Allocation,
85+
start: usize,
86+
end: usize,
8687
) -> Expr {
87-
match v {
88-
ConstValue::Scalar(s) => self.codegen_scalar(s, lit_ty, span),
89-
ConstValue::Slice { data, start, end } => match lit_ty.kind() {
90-
ty::Ref(_, ty::TyS { kind: ty::Str, .. }, _) => {
88+
if let ty::Ref(_, ref_ty, _) = lit_ty.kind() {
89+
match ref_ty.kind() {
90+
ty::Str => {
9191
let slice = data.inspect_with_uninit_and_ptr_outside_interpreter(start..end);
9292
let s = ::std::str::from_utf8(slice).expect("non utf8 str from miri");
93-
Expr::struct_expr_from_values(
93+
return Expr::struct_expr_from_values(
9494
self.codegen_ty(lit_ty),
9595
vec![Expr::string_constant(s), Expr::int_constant(s.len(), Type::size_t())],
9696
&self.symbol_table,
97-
)
97+
);
9898
}
99-
ty::Ref(
100-
_,
101-
ty::TyS { kind: ty::Slice(ty::TyS { kind: Uint(UintTy::U8), .. }), .. },
102-
_,
103-
) => {
104-
// The case where we have a slice of u8 is easy enough: make an array of u8
105-
// TODO: Handle cases with larger int types by making an array of bytes,
106-
// then using byte-extract on it.
107-
let slice = data.inspect_with_uninit_and_ptr_outside_interpreter(start..end);
108-
let vec_of_bytes: Vec<Expr> = slice
109-
.iter()
110-
.map(|b| Expr::int_constant(*b, Type::unsigned_int(8)))
111-
.collect();
112-
let len = vec_of_bytes.len();
113-
let array_expr =
114-
Expr::array_expr(Type::unsigned_int(8).array_of(len), vec_of_bytes);
115-
let data_expr = array_expr.array_to_ptr();
116-
let len_expr = Expr::int_constant(len, Type::size_t());
117-
slice_fat_ptr(self.codegen_ty(lit_ty), data_expr, len_expr, &self.symbol_table)
99+
ty::Slice(slice_ty) => {
100+
if let Uint(UintTy::U8) = slice_ty.kind() {
101+
// The case where we have a slice of u8 is easy enough: make an array of u8
102+
// TODO: Handle cases with larger int types by making an array of bytes,
103+
// then using byte-extract on it.
104+
let slice =
105+
data.inspect_with_uninit_and_ptr_outside_interpreter(start..end);
106+
let vec_of_bytes: Vec<Expr> = slice
107+
.iter()
108+
.map(|b| Expr::int_constant(*b, Type::unsigned_int(8)))
109+
.collect();
110+
let len = vec_of_bytes.len();
111+
let array_expr =
112+
Expr::array_expr(Type::unsigned_int(8).array_of(len), vec_of_bytes);
113+
let data_expr = array_expr.array_to_ptr();
114+
let len_expr = Expr::int_constant(len, Type::size_t());
115+
return slice_fat_ptr(
116+
self.codegen_ty(lit_ty),
117+
data_expr,
118+
len_expr,
119+
&self.symbol_table,
120+
);
121+
}
118122
}
119-
_ => unimplemented!("\nv {:?}\nlit_ty {:?}\nspan {:?}", v, lit_ty, span),
120-
},
123+
_ => {}
124+
}
125+
}
126+
unimplemented!("\nv {:?}\nlit_ty {:?}\nspan {:?}", v, lit_ty, span);
127+
}
128+
129+
pub fn codegen_const_value(
130+
&mut self,
131+
v: ConstValue<'tcx>,
132+
lit_ty: Ty<'tcx>,
133+
span: Option<&Span>,
134+
) -> Expr {
135+
match v {
136+
ConstValue::Scalar(s) => self.codegen_scalar(s, lit_ty, span),
137+
ConstValue::Slice { data, start, end } => {
138+
self.codegen_slice_value(v, lit_ty, span, &data, start, end)
139+
}
121140
ConstValue::ByRef { alloc, offset } => {
122141
debug!("ConstValue by ref {:?} {:?}", alloc, offset);
123142
let mem_var =
@@ -132,31 +151,35 @@ impl<'tcx> GotocCtx<'tcx> {
132151
}
133152

134153
fn codegen_scalar(&mut self, s: Scalar, ty: Ty<'tcx>, span: Option<&Span>) -> Expr {
135-
debug! {"codegen_scalar\n{:?}\n{:?}\n{:?}\n{:?}",s, ty, span, &ty.kind};
154+
debug! {"codegen_scalar\n{:?}\n{:?}\n{:?}\n{:?}",s, ty, span, &ty.kind()};
136155
match (s, &ty.kind()) {
137-
(Scalar::Int(ScalarInt { data, .. }), ty::Int(it)) => match it {
138-
IntTy::I8 => Expr::int_constant(data, Type::signed_int(8)),
139-
IntTy::I16 => Expr::int_constant(data, Type::signed_int(16)),
140-
IntTy::I32 => Expr::int_constant(data, Type::signed_int(32)),
141-
IntTy::I64 => Expr::int_constant(data, Type::signed_int(64)),
142-
IntTy::I128 => Expr::int_constant(data, Type::signed_int(128)),
143-
IntTy::Isize => Expr::int_constant(data, Type::ssize_t()),
156+
(Scalar::Int(_), ty::Int(it)) => match it {
157+
// We treat the data as bit vector. Thus, we extract the value as unsigned and set
158+
// the type to signed int.
159+
IntTy::I8 => Expr::int_constant(s.to_u8().unwrap(), Type::signed_int(8)),
160+
IntTy::I16 => Expr::int_constant(s.to_u16().unwrap(), Type::signed_int(16)),
161+
IntTy::I32 => Expr::int_constant(s.to_u32().unwrap(), Type::signed_int(32)),
162+
IntTy::I64 => Expr::int_constant(s.to_u64().unwrap(), Type::signed_int(64)),
163+
IntTy::I128 => Expr::int_constant(s.to_u128().unwrap(), Type::signed_int(128)),
164+
IntTy::Isize => {
165+
Expr::int_constant(s.to_machine_usize(self).unwrap(), Type::ssize_t())
166+
}
144167
},
145-
(Scalar::Int(ScalarInt { data, .. }), ty::Uint(it)) => match it {
146-
UintTy::U8 => Expr::int_constant(data, Type::unsigned_int(8)),
147-
UintTy::U16 => Expr::int_constant(data, Type::unsigned_int(16)),
148-
UintTy::U32 => Expr::int_constant(data, Type::unsigned_int(32)),
149-
UintTy::U64 => Expr::int_constant(data, Type::unsigned_int(64)),
150-
UintTy::U128 => Expr::int_constant(data, Type::unsigned_int(128)),
151-
UintTy::Usize => Expr::int_constant(data, Type::size_t()),
168+
(Scalar::Int(_), ty::Uint(it)) => match it {
169+
UintTy::U8 => Expr::int_constant(s.to_u8().unwrap(), Type::unsigned_int(8)),
170+
UintTy::U16 => Expr::int_constant(s.to_u16().unwrap(), Type::unsigned_int(16)),
171+
UintTy::U32 => Expr::int_constant(s.to_u32().unwrap(), Type::unsigned_int(32)),
172+
UintTy::U64 => Expr::int_constant(s.to_u64().unwrap(), Type::unsigned_int(64)),
173+
UintTy::U128 => Expr::int_constant(s.to_u128().unwrap(), Type::unsigned_int(128)),
174+
UintTy::Usize => {
175+
Expr::int_constant(s.to_machine_usize(self).unwrap(), Type::size_t())
176+
}
152177
},
153-
(Scalar::Int(ScalarInt { .. }), ty::Bool) => {
154-
Expr::c_bool_constant(s.to_bool().unwrap())
155-
}
156-
(Scalar::Int(ScalarInt { .. }), ty::Char) => {
178+
(Scalar::Int(_), ty::Bool) => Expr::c_bool_constant(s.to_bool().unwrap()),
179+
(Scalar::Int(_), ty::Char) => {
157180
Expr::int_constant(s.to_i32().unwrap(), Type::signed_int(32))
158181
}
159-
(Scalar::Int(ScalarInt { .. }), ty::Float(k)) =>
182+
(Scalar::Int(_), ty::Float(k)) =>
160183
// rustc uses a sophisticated format for floating points that is hard to get f32/f64 from.
161184
// Instead, we use integers with the right width to represent the bit pattern.
162185
{
@@ -165,18 +188,23 @@ impl<'tcx> GotocCtx<'tcx> {
165188
FloatTy::F64 => Expr::double_constant_from_bitpattern(s.to_u64().unwrap()),
166189
}
167190
}
168-
(Scalar::Int(ScalarInt { size: 0, .. }), ty::FnDef(d, substs)) => {
191+
(Scalar::Int(int), ty::FnDef(d, substs)) => {
192+
assert_eq!(int.size(), Size::ZERO);
169193
self.codegen_fndef(*d, substs, span)
170194
}
171-
(Scalar::Int(ScalarInt { .. }), ty::RawPtr(tm)) => {
195+
(Scalar::Int(_), ty::RawPtr(tm)) => {
172196
Expr::pointer_constant(s.to_u64().unwrap(), self.codegen_ty(tm.ty).to_pointer())
173197
}
174198
// TODO: Removing this doesn't cause any regressions to fail.
175199
// We need a regression for this case.
176-
(Scalar::Int(ScalarInt { data: 0, .. }), ty::Ref(_, ty, _)) => {
177-
self.codegen_ty(ty).to_pointer().null()
200+
(Scalar::Int(int), ty::Ref(_, ty, _)) => {
201+
if int.is_null() {
202+
self.codegen_ty(ty).to_pointer().null()
203+
} else {
204+
unreachable!()
205+
}
178206
}
179-
(Scalar::Int(ScalarInt { .. }), ty::Adt(adt, subst)) => {
207+
(Scalar::Int(_), ty::Adt(adt, subst)) => {
180208
if adt.is_struct() || adt.is_union() {
181209
// in this case, we must have a one variant ADT. there are two cases
182210
let variant = &adt.variants.raw[0];

compiler/rustc_codegen_rmc/src/codegen/statement.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ impl<'tcx> GotocCtx<'tcx> {
4141
Stmt::goto(self.current_fn().find_label(target), loc)
4242
}
4343
TerminatorKind::SwitchInt { discr, switch_ty, targets } => match targets {
44-
SwitchTargets { values, targets } => {
45-
self.codegen_switch_int(discr, switch_ty, values, targets)
44+
SwitchTargets { values, .. } => {
45+
let all_targets = targets.all_targets();
46+
self.codegen_switch_int(discr, switch_ty, values, all_targets)
4647
}
4748
},
4849
TerminatorKind::Resume => Stmt::assert_false("resume instruction", loc),
@@ -197,7 +198,7 @@ impl<'tcx> GotocCtx<'tcx> {
197198
discr: &Operand<'tcx>,
198199
switch_ty: Ty<'tcx>,
199200
values: &SmallVec<[u128; 1]>,
200-
targets: &SmallVec<[BasicBlock; 2]>,
201+
targets: &[BasicBlock],
201202
) -> Stmt {
202203
assert_eq!(targets.len(), values.len() + 1);
203204

compiler/rustc_driver/src/lib.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -595,24 +595,6 @@ fn show_content_with_pager(content: &str) {
595595
}
596596

597597
impl RustcDefaultCalls {
598-
fn process_rlink(sess: &Session, compiler: &interface::Compiler) -> Result<(), ErrorReported> {
599-
if let Input::File(file) = compiler.input() {
600-
// FIXME: #![crate_type] and #![crate_name] support not implemented yet
601-
let attrs = vec![];
602-
sess.init_crate_types(collect_crate_types(sess, &attrs));
603-
let outputs = compiler.build_output_filenames(&sess, &attrs);
604-
let rlink_data = fs::read_to_string(file).unwrap_or_else(|err| {
605-
sess.fatal(&format!("failed to read rlink file: {}", err));
606-
});
607-
let codegen_results: CodegenResults = json::decode(&rlink_data).unwrap_or_else(|err| {
608-
sess.fatal(&format!("failed to decode rlink: {}", err));
609-
});
610-
compiler.codegen_backend().link(&sess, Box::new(codegen_results), &outputs)
611-
} else {
612-
sess.fatal("rlink must be a file")
613-
}
614-
}
615-
616598
pub fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Compilation {
617599
if sess.opts.debugging_opts.link_only {
618600
if let Input::File(file) = compiler.input() {

compiler/rustc_middle/src/mir/terminator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub struct SwitchTargets {
3535
//
3636
// However we’ve decided to keep this as-is until we figure a case
3737
// where some other approach seems to be strictly better than other.
38-
pub targets: SmallVec<[BasicBlock; 2]>,
38+
targets: SmallVec<[BasicBlock; 2]>,
3939
}
4040

4141
impl SwitchTargets {

compiler/rustc_middle/src/ty/consts/int.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ impl std::fmt::Debug for ConstInt {
122122
pub struct ScalarInt {
123123
/// The first `size` bytes of `data` are the value.
124124
/// Do not try to read less or more bytes than that. The remaining bytes must be 0.
125-
pub data: u128,
126-
pub size: u8,
125+
data: u128,
126+
size: u8,
127127
}
128128

129129
// Cannot derive these, as the derives take references to the fields, and we

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,10 @@ pub struct CReaderCacheKey {
365365
pub struct TyS<'tcx> {
366366
/// This field shouldn't be used directly and may be removed in the future.
367367
/// Use `TyS::kind()` instead.
368-
pub kind: TyKind<'tcx>,
368+
kind: TyKind<'tcx>,
369369
/// This field shouldn't be used directly and may be removed in the future.
370370
/// Use `TyS::flags()` instead.
371-
pub flags: TypeFlags,
371+
flags: TypeFlags,
372372

373373
/// This is a kind of confusing thing: it stores the smallest
374374
/// binder such that

0 commit comments

Comments
 (0)