Skip to content

Remove enum layout assumptions from type visitor #5778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions src/libcore/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Runtime type reflection
*/

use intrinsic::{TyDesc, TyVisitor};
#[cfg(not(stage0))] use intrinsic::Opaque;
use libc::c_void;
use sys;
use vec;
Expand Down Expand Up @@ -393,6 +394,7 @@ impl<V:TyVisitor + MovePtr> TyVisitor for MovePtrAdaptor<V> {
true
}

#[cfg(stage0)]
fn visit_enter_enum(&self, n_variants: uint, sz: uint, align: uint)
-> bool {
self.align(align);
Expand All @@ -402,25 +404,47 @@ impl<V:TyVisitor + MovePtr> TyVisitor for MovePtrAdaptor<V> {
true
}

#[cfg(not(stage0))]
fn visit_enter_enum(&self, n_variants: uint,
get_disr: extern unsafe fn(ptr: *Opaque) -> int,
sz: uint, align: uint)
-> bool {
self.align(align);
if ! self.inner.visit_enter_enum(n_variants, get_disr, sz, align) {
return false;
}
true
}

fn visit_enter_enum_variant(&self, variant: uint,
disr_val: int,
n_fields: uint,
name: &str) -> bool {
self.inner.push_ptr();
self.inner.push_ptr(); // NOTE remove after next snapshot
if ! self.inner.visit_enter_enum_variant(variant, disr_val,
n_fields, name) {
return false;
}
true
}

#[cfg(stage0)]
fn visit_enum_variant_field(&self, i: uint, inner: *TyDesc) -> bool {
unsafe { self.align((*inner).align); }
if ! self.inner.visit_enum_variant_field(i, inner) { return false; }
unsafe { self.bump((*inner).size); }
true
}

#[cfg(not(stage0))]
fn visit_enum_variant_field(&self, i: uint, offset: uint, inner: *TyDesc) -> bool {
self.inner.push_ptr();
self.bump(offset);
if ! self.inner.visit_enum_variant_field(i, offset, inner) { return false; }
self.inner.pop_ptr();
true
}

fn visit_leave_enum_variant(&self, variant: uint,
disr_val: int,
n_fields: uint,
Expand All @@ -429,10 +453,11 @@ impl<V:TyVisitor + MovePtr> TyVisitor for MovePtrAdaptor<V> {
n_fields, name) {
return false;
}
self.inner.pop_ptr();
self.inner.pop_ptr(); // NOTE remove after next snapshot
true
}

#[cfg(stage0)]
fn visit_leave_enum(&self, n_variants: uint, sz: uint, align: uint)
-> bool {
if ! self.inner.visit_leave_enum(n_variants, sz, align) {
Expand All @@ -442,6 +467,17 @@ impl<V:TyVisitor + MovePtr> TyVisitor for MovePtrAdaptor<V> {
true
}

#[cfg(not(stage0))]
fn visit_leave_enum(&self, n_variants: uint,
get_disr: extern unsafe fn(ptr: *Opaque) -> int,
sz: uint, align: uint) -> bool {
if ! self.inner.visit_leave_enum(n_variants, get_disr, sz, align) {
return false;
}
self.bump(sz);
true
}

fn visit_trait(&self) -> bool {
self.align_to::<@TyVisitor>();
if ! self.inner.visit_trait() { return false; }
Expand Down
98 changes: 96 additions & 2 deletions src/libcore/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use cast::transmute;
use char;
use intrinsic;
use intrinsic::{TyDesc, TyVisitor, visit_tydesc};
#[cfg(not(stage0))] use intrinsic::Opaque;
use io::{Writer, WriterUtil};
use libc::c_void;
use managed;
Expand Down Expand Up @@ -137,12 +138,20 @@ impl Repr for char {

// New implementation using reflect::MovePtr

#[cfg(stage0)]
enum VariantState {
Degenerate,
TagMatch,
TagMismatch,
}

#[cfg(not(stage0))]
enum VariantState {
SearchingFor(int),
Matched,
AlreadyFound
}

pub struct ReprVisitor {
mut ptr: *c_void,
mut ptr_stk: ~[*c_void],
Expand Down Expand Up @@ -181,14 +190,14 @@ pub impl ReprVisitor {
true
}

#[inline(always)]
#[cfg(stage0)] #[inline(always)]
fn bump(&self, sz: uint) {
do self.move_ptr() |p| {
((p as uint) + sz) as *c_void
};
}

#[inline(always)]
#[cfg(stage0)] #[inline(always)]
fn bump_past<T>(&self) {
self.bump(sys::size_of::<T>());
}
Expand Down Expand Up @@ -458,6 +467,7 @@ impl TyVisitor for ReprVisitor {
true
}

#[cfg(stage0)]
fn visit_enter_enum(&self, n_variants: uint,
_sz: uint, _align: uint) -> bool {
if n_variants == 1 {
Expand All @@ -468,6 +478,16 @@ impl TyVisitor for ReprVisitor {
true
}

#[cfg(not(stage0))]
fn visit_enter_enum(&self, n_variants: uint,
get_disr: extern unsafe fn(ptr: *Opaque) -> int,
_sz: uint, _align: uint) -> bool {
let disr = unsafe { get_disr(transmute(self.ptr)) };
self.var_stk.push(SearchingFor(disr));
true
}

#[cfg(stage0)]
fn visit_enter_enum_variant(&self, _variant: uint,
disr_val: int,
n_fields: uint,
Expand Down Expand Up @@ -500,6 +520,36 @@ impl TyVisitor for ReprVisitor {
true
}

#[cfg(not(stage0))]
fn visit_enter_enum_variant(&self, _variant: uint,
disr_val: int,
n_fields: uint,
name: &str) -> bool {
let mut write = false;
match self.var_stk.pop() {
SearchingFor(sought) => {
if disr_val == sought {
self.var_stk.push(Matched);
write = true;
} else {
self.var_stk.push(SearchingFor(sought));
}
}
Matched | AlreadyFound => {
self.var_stk.push(AlreadyFound);
}
}

if write {
self.writer.write_str(name);
if n_fields > 0 {
self.writer.write_char('(');
}
}
true
}

#[cfg(stage0)]
fn visit_enum_variant_field(&self, i: uint, inner: *TyDesc) -> bool {
match self.var_stk[vec::uniq_len(&const self.var_stk) - 1] {
Degenerate | TagMatch => {
Expand All @@ -515,6 +565,23 @@ impl TyVisitor for ReprVisitor {
true
}

#[cfg(not(stage0))]
fn visit_enum_variant_field(&self, i: uint, _offset: uint, inner: *TyDesc) -> bool {
match self.var_stk[vec::uniq_len(&const self.var_stk) - 1] {
Matched => {
if i != 0 {
self.writer.write_str(", ");
}
if ! self.visit_inner(inner) {
return false;
}
}
_ => ()
}
true
}

#[cfg(stage0)]
fn visit_leave_enum_variant(&self, _variant: uint,
_disr_val: int,
n_fields: uint,
Expand All @@ -530,12 +597,39 @@ impl TyVisitor for ReprVisitor {
true
}

#[cfg(not(stage0))]
fn visit_leave_enum_variant(&self, _variant: uint,
_disr_val: int,
n_fields: uint,
_name: &str) -> bool {
match self.var_stk[vec::uniq_len(&const self.var_stk) - 1] {
Matched => {
if n_fields > 0 {
self.writer.write_char(')');
}
}
_ => ()
}
true
}

#[cfg(stage0)]
fn visit_leave_enum(&self, _n_variants: uint,
_sz: uint, _align: uint) -> bool {
self.var_stk.pop();
true
}

#[cfg(not(stage0))]
fn visit_leave_enum(&self, _n_variants: uint,
_get_disr: extern unsafe fn(ptr: *Opaque) -> int,
_sz: uint, _align: uint) -> bool {
match self.var_stk.pop() {
SearchingFor(*) => fail!(~"enum value matched no variant"),
_ => true
}
}

fn visit_enter_fn(&self, _purity: uint, _proto: uint,
_n_inputs: uint, _retstyle: uint) -> bool { true }
fn visit_fn_input(&self, _i: uint, _mode: uint, _inner: *TyDesc) -> bool {
Expand Down
6 changes: 5 additions & 1 deletion src/librustc/front/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub mod intrinsic {
// Remaining fields not listed
}

pub enum Opaque { }

pub trait TyVisitor {
fn visit_bot(&self) -> bool;
fn visit_nil(&self) -> bool;
Expand Down Expand Up @@ -91,17 +93,19 @@ pub mod intrinsic {
sz: uint, align: uint) -> bool;

fn visit_enter_enum(&self, n_variants: uint,
get_disr: extern unsafe fn(ptr: *Opaque) -> int,
sz: uint, align: uint) -> bool;
fn visit_enter_enum_variant(&self, variant: uint,
disr_val: int,
n_fields: uint,
name: &str) -> bool;
fn visit_enum_variant_field(&self, i: uint, inner: *TyDesc) -> bool;
fn visit_enum_variant_field(&self, i: uint, offset: uint, inner: *TyDesc) -> bool;
fn visit_leave_enum_variant(&self, variant: uint,
disr_val: int,
n_fields: uint,
name: &str) -> bool;
fn visit_leave_enum(&self, n_variants: uint,
get_disr: extern unsafe fn(ptr: *Opaque) -> int,
sz: uint, align: uint) -> bool;

fn visit_enter_fn(&self, purity: uint, proto: uint,
Expand Down
63 changes: 48 additions & 15 deletions src/librustc/middle/trans/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.


use lib::llvm::{TypeRef, ValueRef};
use back::link::mangle_internal_name_by_path_and_seq;
use lib::llvm::{TypeRef, ValueRef, llvm};
use middle::trans::adt;
use middle::trans::base::*;
use middle::trans::build::*;
use middle::trans::callee::{ArgVals, DontAutorefArg};
Expand All @@ -24,10 +25,13 @@ use middle::trans::type_of::*;
use middle::ty;
use util::ppaux::ty_to_str;

use core::libc::c_uint;
use core::option::None;
use core::vec;
use syntax::ast::def_id;
use syntax::ast;
use syntax::ast_map::path_name;
use syntax::parse::token::special_idents;

pub struct Reflector {
visitor_val: ValueRef,
Expand Down Expand Up @@ -266,23 +270,52 @@ pub impl Reflector {
// variant?
ty::ty_enum(did, ref substs) => {
let bcx = self.bcx;
let tcx = bcx.ccx().tcx;
let variants = ty::substd_enum_variants(tcx, did, substs);
let ccx = bcx.ccx();
let repr = adt::represent_type(bcx.ccx(), t);
let variants = ty::substd_enum_variants(ccx.tcx, did, substs);
let llptrty = T_ptr(type_of(ccx, t));
let (_, opaquety) = *(ccx.tcx.intrinsic_defs.find(&ccx.sess.ident_of(~"Opaque"))
.expect("Failed to resolve intrinsic::Opaque"));
let opaqueptrty = ty::mk_ptr(ccx.tcx, ty::mt { ty: opaquety, mutbl: ast::m_imm });

let make_get_disr = || {
let sub_path = bcx.fcx.path + ~[path_name(special_idents::anon)];
let sym = mangle_internal_name_by_path_and_seq(ccx, sub_path, ~"get_disr");
let args = [ty::arg { mode: ast::expl(ast::by_copy),
ty: opaqueptrty }];
let llfty = type_of_fn(ccx, args, ty::mk_int(ccx.tcx));
let llfdecl = decl_internal_cdecl_fn(ccx.llmod, sym, llfty);
let arg = unsafe {
llvm::LLVMGetParam(llfdecl, first_real_arg as c_uint)
};
let fcx = new_fn_ctxt(ccx, ~[], llfdecl, None);
let bcx = top_scope_block(fcx, None);
let arg = BitCast(bcx, arg, llptrty);
let ret = adt::trans_get_discr(bcx, repr, arg);
Store(bcx, ret, fcx.llretptr);
cleanup_and_Br(bcx, bcx, fcx.llreturn);
finish_fn(fcx, bcx.llbb);
llfdecl
};

let extra = ~[self.c_uint(vec::len(variants))]
let enum_args = ~[self.c_uint(vec::len(variants)), make_get_disr()]
+ self.c_size_and_align(t);
do self.bracketed(~"enum", extra) |this| {
do self.bracketed(~"enum", enum_args) |this| {
for variants.eachi |i, v| {
let extra1 = ~[this.c_uint(i),
this.c_int(v.disr_val),
this.c_uint(vec::len(v.args)),
this.c_slice(
bcx.ccx().sess.str_of(v.name))];
do this.bracketed(~"enum_variant", extra1) |this| {
let variant_args = ~[this.c_uint(i),
this.c_int(v.disr_val),
this.c_uint(vec::len(v.args)),
this.c_slice(ccx.sess.str_of(v.name))];
do this.bracketed(~"enum_variant", variant_args) |this| {
for v.args.eachi |j, a| {
let extra = ~[this.c_uint(j),
this.c_tydesc(*a)];
this.visit(~"enum_variant_field", extra);
let bcx = this.bcx;
let null = C_null(llptrty);
let offset = p2i(ccx, adt::trans_field_ptr(bcx, repr, null,
v.disr_val, j));
let field_args = ~[this.c_uint(j),
offset,
this.c_tydesc(*a)];
this.visit(~"enum_variant_field", field_args);
}
}
}
Expand Down
Loading