Merge pull request #320 from oli-obk/memory

some more things found in the rustc test suite
This commit is contained in:
Oliver Schneider
2017-08-29 14:38:07 +02:00
committed by GitHub
4 changed files with 43 additions and 4 deletions
+11 -4
View File
@@ -15,6 +15,7 @@
use rustc::hir::def_id::DefId;
use rustc::mir;
use syntax::ast::Mutability;
use syntax::codemap::Span;
use std::collections::{HashMap, BTreeMap};
@@ -98,15 +99,21 @@ fn run_main<'a, 'tcx: 'a>(
dest,
)?;
// Second argument (argc): 0
// Second argument (argc): 1
let dest = ecx.eval_lvalue(&mir::Lvalue::Local(args.next().unwrap()))?;
let ty = ecx.tcx.types.isize;
ecx.write_null(dest, ty)?;
ecx.write_primval(dest, PrimVal::Bytes(1), ty)?;
// Third argument (argv): 0
// FIXME: extract main source file path
// Third argument (argv): &[b"foo"]
let dest = ecx.eval_lvalue(&mir::Lvalue::Local(args.next().unwrap()))?;
let ty = ecx.tcx.mk_imm_ptr(ecx.tcx.mk_imm_ptr(ecx.tcx.types.u8));
ecx.write_null(dest, ty)?;
let foo = ecx.memory.allocate_cached(b"foo\0")?;
let ptr_size = ecx.memory.pointer_size();
let foo_ptr = ecx.memory.allocate(ptr_size * 1, ptr_size, MemoryKind::UninitializedStatic)?;
ecx.memory.write_primval(foo_ptr.into(), PrimVal::Ptr(foo.into()), ptr_size, false)?;
ecx.memory.mark_static_initalized(foo_ptr.alloc_id, Mutability::Immutable)?;
ecx.write_ptr(dest, foo_ptr.into(), ty)?;
} else {
ecx.push_stack_frame(
main_instance,
@@ -1138,6 +1138,11 @@ pub fn get_field_ty(
packed: nonnull.packed,
})
}
// mir optimizations treat single variant enums as structs
General { .. } if adt_def.variants.len() == 1 => Ok(TyAndPacked {
ty: adt_def.variants[0].fields[field_index].ty(self.tcx, substs),
packed: false,
}),
_ => {
err!(Unimplemented(format!(
"get_field_ty can't handle enum type: {:?}, {:?}",
@@ -1211,6 +1216,8 @@ fn get_field_offset(&self, ty: Ty<'tcx>, field_index: usize) -> EvalResult<'tcx,
}
StructWrappedNullablePointer { ref nonnull, .. } => Ok(nonnull.offsets[field_index]),
UntaggedUnion { .. } => Ok(Size::from_bytes(0)),
// mir optimizations treat single variant enums as structs
General { ref variants, .. } if variants.len() == 1 => Ok(variants[0].offsets[field_index]),
_ => {
let msg = format!(
"get_field_offset: can't handle type: {:?}, with layout: {:?}",
+5
View File
@@ -227,6 +227,11 @@ pub fn lvalue_field(
let (offset, packed) = match *base_layout {
Univariant { ref variant, .. } => (variant.offsets[field_index], variant.packed),
// mir optimizations treat single variant enums as structs
General { ref variants, .. } if variants.len() == 1 => {
(variants[0].offsets[field_index], variants[0].packed)
}
General { ref variants, .. } => {
let (_, base_extra) = base.to_ptr_extra_aligned();
if let LvalueExtra::DowncastVariant(variant_idx) = base_extra {
+20
View File
@@ -0,0 +1,20 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[repr(u8)]
enum Foo {
Foo(u8),
}
fn main() {
match Foo::Foo(1) {
_ => ()
}
}