2013-02-18 16:16:21 -06:00
|
|
|
// Copyright 2013 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.
|
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/*!
|
|
|
|
* # Representation of Algebraic Data Types
|
|
|
|
*
|
2013-03-05 20:06:53 -06:00
|
|
|
* This module determines how to represent enums, structs, and tuples
|
|
|
|
* based on their monomorphized types; it is responsible both for
|
|
|
|
* choosing a representation and translating basic operations on
|
|
|
|
* values of those types.
|
2013-02-28 01:08:52 -06:00
|
|
|
*
|
|
|
|
* Note that the interface treats everything as a general case of an
|
|
|
|
* enum, so structs/tuples/etc. have one pseudo-variant with
|
|
|
|
* discriminant 0; i.e., as if they were a univariant enum.
|
|
|
|
*
|
|
|
|
* Having everything in one place will enable improvements to data
|
|
|
|
* structure representation; possibilities include:
|
|
|
|
*
|
|
|
|
* - User-specified alignment (e.g., cacheline-aligning parts of
|
|
|
|
* concurrently accessed data structures); LLVM can't represent this
|
|
|
|
* directly, so we'd have to insert padding fields in any structure
|
|
|
|
* that might contain one and adjust GEP indices accordingly. See
|
|
|
|
* issue #4578.
|
|
|
|
*
|
|
|
|
* - Using smaller integer types for discriminants.
|
|
|
|
*
|
|
|
|
* - Store nested enums' discriminants in the same word. Rather, if
|
|
|
|
* some variants start with enums, and those enums representations
|
|
|
|
* have unused alignment padding between discriminant and body, the
|
|
|
|
* outer enum's discriminant can be stored there and those variants
|
|
|
|
* can start at offset 0. Kind of fancy, and might need work to
|
|
|
|
* make copies of the inner enum type cooperate, but it could help
|
|
|
|
* with `Option` or `Result` wrapped around another enum.
|
|
|
|
*
|
|
|
|
* - Tagged pointers would be neat, but given that any type can be
|
|
|
|
* used unboxed and any field can have pointers (including mutable)
|
|
|
|
* taken to it, implementing them for Rust seems difficult.
|
|
|
|
*/
|
|
|
|
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::container::Map;
|
|
|
|
use std::libc::c_ulonglong;
|
|
|
|
use std::option::{Option, Some, None};
|
2013-02-28 01:08:52 -06:00
|
|
|
|
2013-06-15 09:29:52 -05:00
|
|
|
use lib::llvm::{ValueRef, True, IntEQ, IntNE};
|
2013-02-18 16:16:21 -06:00
|
|
|
use middle::trans::_match;
|
|
|
|
use middle::trans::build::*;
|
|
|
|
use middle::trans::common::*;
|
|
|
|
use middle::trans::machine;
|
|
|
|
use middle::trans::type_of;
|
|
|
|
use middle::ty;
|
|
|
|
use syntax::ast;
|
|
|
|
use util::ppaux::ty_to_str;
|
|
|
|
|
2013-06-16 05:52:44 -05:00
|
|
|
use middle::trans::type_::Type;
|
|
|
|
|
2013-02-18 16:16:21 -06:00
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/// Representations.
|
2013-02-18 16:16:21 -06:00
|
|
|
pub enum Repr {
|
2013-02-28 01:08:52 -06:00
|
|
|
/// C-like enums; basically an int.
|
2013-07-23 10:52:44 -05:00
|
|
|
CEnum(uint, uint), // discriminant range
|
2013-03-11 00:58:44 -05:00
|
|
|
/**
|
|
|
|
* Single-case variants, and structs/tuples/records.
|
|
|
|
*
|
|
|
|
* Structs with destructors need a dynamic destroyedness flag to
|
|
|
|
* avoid running the destructor too many times; this is included
|
|
|
|
* in the `Struct` if present.
|
|
|
|
*/
|
|
|
|
Univariant(Struct, bool),
|
2013-02-28 01:08:52 -06:00
|
|
|
/**
|
2013-03-11 03:04:08 -05:00
|
|
|
* General-case enums: for each case there is a struct, and they
|
|
|
|
* all start with a field for the discriminant.
|
2013-02-28 01:08:52 -06:00
|
|
|
*/
|
2013-03-31 17:55:30 -05:00
|
|
|
General(~[Struct]),
|
|
|
|
/**
|
|
|
|
* Two cases distinguished by a nullable pointer: the case with discriminant
|
|
|
|
* `nndiscr` is represented by the struct `nonnull`, where the `ptrfield`th
|
|
|
|
* field is known to be nonnull due to its type; if that field is null, then
|
|
|
|
* it represents the other case, which is inhabited by at most one value
|
|
|
|
* (and all other fields are undefined/unused).
|
|
|
|
*
|
2013-06-28 17:32:26 -05:00
|
|
|
* For example, `std::option::Option` instantiated at a safe pointer type
|
2013-03-31 17:55:30 -05:00
|
|
|
* is represented such that `None` is a null pointer and `Some` is the
|
|
|
|
* identity function.
|
|
|
|
*/
|
2013-07-23 10:52:44 -05:00
|
|
|
NullablePointer{ nonnull: Struct, nndiscr: uint, ptrfield: uint,
|
2013-03-31 17:55:30 -05:00
|
|
|
nullfields: ~[ty::t] }
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/// For structs, and struct-like parts of anything fancier.
|
2013-07-11 05:51:47 -05:00
|
|
|
pub struct Struct {
|
2013-02-18 16:16:21 -06:00
|
|
|
size: u64,
|
|
|
|
align: u64,
|
2013-04-10 07:47:22 -05:00
|
|
|
packed: bool,
|
2013-02-18 16:16:21 -06:00
|
|
|
fields: ~[ty::t]
|
|
|
|
}
|
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/**
|
|
|
|
* Convenience for `represent_type`. There should probably be more or
|
|
|
|
* these, for places in trans where the `ty::t` isn't directly
|
|
|
|
* available.
|
|
|
|
*/
|
2013-07-27 03:25:59 -05:00
|
|
|
pub fn represent_node(bcx: @mut Block, node: ast::NodeId) -> @Repr {
|
2013-02-18 16:16:21 -06:00
|
|
|
represent_type(bcx.ccx(), node_id_type(bcx, node))
|
|
|
|
}
|
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/// Decides how to represent a given type.
|
2013-06-13 02:19:50 -05:00
|
|
|
pub fn represent_type(cx: &mut CrateContext, t: ty::t) -> @Repr {
|
2013-02-18 16:16:21 -06:00
|
|
|
debug!("Representing: %s", ty_to_str(cx.tcx, t));
|
2013-02-25 03:49:21 -06:00
|
|
|
match cx.adt_reprs.find(&t) {
|
|
|
|
Some(repr) => return *repr,
|
|
|
|
None => { }
|
|
|
|
}
|
2013-03-31 17:55:30 -05:00
|
|
|
let repr = @represent_type_uncached(cx, t);
|
|
|
|
debug!("Represented as: %?", repr)
|
|
|
|
cx.adt_reprs.insert(t, repr);
|
|
|
|
return repr;
|
|
|
|
}
|
|
|
|
|
2013-06-13 02:19:50 -05:00
|
|
|
fn represent_type_uncached(cx: &mut CrateContext, t: ty::t) -> Repr {
|
2013-03-31 17:55:30 -05:00
|
|
|
match ty::get(t).sty {
|
2013-02-18 16:16:21 -06:00
|
|
|
ty::ty_tup(ref elems) => {
|
2013-03-31 17:55:30 -05:00
|
|
|
return Univariant(mk_struct(cx, *elems, false), false)
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
ty::ty_struct(def_id, ref substs) => {
|
|
|
|
let fields = ty::lookup_struct_fields(cx.tcx, def_id);
|
2013-07-02 21:13:00 -05:00
|
|
|
let mut ftys = do fields.map |field| {
|
2013-02-18 16:16:21 -06:00
|
|
|
ty::lookup_field_type(cx.tcx, def_id, field.id, substs)
|
2013-03-11 00:58:44 -05:00
|
|
|
};
|
2013-04-10 07:47:22 -05:00
|
|
|
let packed = ty::lookup_packed(cx.tcx, def_id);
|
2013-06-22 21:41:45 -05:00
|
|
|
let dtor = ty::ty_dtor(cx.tcx, def_id).has_drop_flag();
|
2013-07-02 21:13:00 -05:00
|
|
|
if dtor { ftys.push(ty::mk_bool()); }
|
|
|
|
|
2013-03-31 17:55:30 -05:00
|
|
|
return Univariant(mk_struct(cx, ftys, packed), dtor)
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
ty::ty_enum(def_id, ref substs) => {
|
2013-07-23 10:52:44 -05:00
|
|
|
struct Case { discr: uint, tys: ~[ty::t] };
|
2013-03-31 17:55:30 -05:00
|
|
|
impl Case {
|
2013-06-13 02:19:50 -05:00
|
|
|
fn is_zerolen(&self, cx: &mut CrateContext) -> bool {
|
2013-03-31 17:55:30 -05:00
|
|
|
mk_struct(cx, self.tys, false).size == 0
|
|
|
|
}
|
|
|
|
fn find_ptr(&self) -> Option<uint> {
|
2013-07-04 21:13:26 -05:00
|
|
|
self.tys.iter().position(|&ty| mono_data_classify(ty) == MonoNonNull)
|
2013-03-31 17:55:30 -05:00
|
|
|
}
|
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
|
|
|
|
let cases = do ty::enum_variants(cx.tcx, def_id).map |vi| {
|
|
|
|
let arg_tys = do vi.args.map |&raw_ty| {
|
|
|
|
ty::subst(cx.tcx, substs, raw_ty)
|
|
|
|
};
|
2013-02-23 02:59:24 -06:00
|
|
|
Case { discr: vi.disr_val, tys: arg_tys }
|
2013-02-18 16:16:21 -06:00
|
|
|
};
|
2013-03-31 17:55:30 -05:00
|
|
|
|
2013-02-18 16:16:21 -06:00
|
|
|
if cases.len() == 0 {
|
|
|
|
// Uninhabitable; represent as unit
|
2013-05-19 00:07:44 -05:00
|
|
|
return Univariant(mk_struct(cx, [], false), false);
|
2013-03-31 17:55:30 -05:00
|
|
|
}
|
|
|
|
|
2013-06-17 18:43:22 -05:00
|
|
|
if cases.iter().all(|c| c.tys.len() == 0) {
|
2013-02-28 01:08:52 -06:00
|
|
|
// All bodies empty -> intlike
|
2013-02-23 02:59:24 -06:00
|
|
|
let discrs = cases.map(|c| c.discr);
|
2013-06-15 02:16:10 -05:00
|
|
|
return CEnum(*discrs.iter().min().unwrap(), *discrs.iter().max().unwrap());
|
2013-03-31 17:55:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if cases.len() == 1 {
|
2013-03-11 01:16:08 -05:00
|
|
|
// Equivalent to a struct/tuple/newtype.
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(cases[0].discr, 0);
|
2013-03-31 17:55:30 -05:00
|
|
|
return Univariant(mk_struct(cx, cases[0].tys, false), false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since there's at least one
|
|
|
|
// non-empty body, explicit discriminants should have
|
|
|
|
// been rejected by a checker before this point.
|
2013-07-23 10:52:44 -05:00
|
|
|
if !cases.iter().enumerate().all(|(i,c)| c.discr == i) {
|
2013-03-31 17:55:30 -05:00
|
|
|
cx.sess.bug(fmt!("non-C-like enum %s with specified \
|
|
|
|
discriminants",
|
|
|
|
ty::item_path_str(cx.tcx, def_id)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if cases.len() == 2 {
|
|
|
|
let mut discr = 0;
|
|
|
|
while discr < 2 {
|
|
|
|
if cases[1 - discr].is_zerolen(cx) {
|
|
|
|
match cases[discr].find_ptr() {
|
|
|
|
Some(ptrfield) => {
|
|
|
|
return NullablePointer {
|
|
|
|
nndiscr: discr,
|
2013-07-02 14:47:32 -05:00
|
|
|
nonnull: mk_struct(cx,
|
|
|
|
cases[discr].tys,
|
|
|
|
false),
|
2013-03-31 17:55:30 -05:00
|
|
|
ptrfield: ptrfield,
|
2013-07-02 14:47:32 -05:00
|
|
|
nullfields: cases[1 - discr].tys.clone()
|
2013-03-31 17:55:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None => { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
discr += 1;
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
}
|
2013-03-31 17:55:30 -05:00
|
|
|
|
|
|
|
// The general case.
|
2013-07-23 10:52:44 -05:00
|
|
|
let discr = ~[ty::mk_uint()];
|
2013-03-31 17:55:30 -05:00
|
|
|
return General(cases.map(|c| mk_struct(cx, discr + c.tys, false)))
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
2013-05-19 00:07:44 -05:00
|
|
|
_ => cx.sess.bug("adt::represent_type called on non-ADT type")
|
2013-03-31 17:55:30 -05:00
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
|
2013-06-13 02:19:50 -05:00
|
|
|
fn mk_struct(cx: &mut CrateContext, tys: &[ty::t], packed: bool) -> Struct {
|
2013-02-18 16:16:21 -06:00
|
|
|
let lltys = tys.map(|&ty| type_of::sizing_type_of(cx, ty));
|
2013-06-15 09:29:52 -05:00
|
|
|
let llty_rec = Type::struct_(lltys, packed);
|
2013-02-18 16:16:21 -06:00
|
|
|
Struct {
|
|
|
|
size: machine::llsize_of_alloc(cx, llty_rec) /*bad*/as u64,
|
|
|
|
align: machine::llalign_of_min(cx, llty_rec) /*bad*/as u64,
|
2013-04-10 07:47:22 -05:00
|
|
|
packed: packed,
|
2013-07-12 02:53:03 -05:00
|
|
|
fields: tys.to_owned(),
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/**
|
|
|
|
* Returns the fields of a struct for the given representation.
|
|
|
|
* All nominal types are LLVM structs, in order to be able to use
|
|
|
|
* forward-declared opaque types to prevent circularity in `type_of`.
|
|
|
|
*/
|
2013-06-15 09:29:52 -05:00
|
|
|
pub fn fields_of(cx: &mut CrateContext, r: &Repr) -> ~[Type] {
|
2013-02-18 16:16:21 -06:00
|
|
|
generic_fields_of(cx, r, false)
|
|
|
|
}
|
2013-02-28 01:08:52 -06:00
|
|
|
/// Like `fields_of`, but for `type_of::sizing_type_of` (q.v.).
|
2013-06-15 09:29:52 -05:00
|
|
|
pub fn sizing_fields_of(cx: &mut CrateContext, r: &Repr) -> ~[Type] {
|
2013-02-28 01:08:52 -06:00
|
|
|
generic_fields_of(cx, r, true)
|
|
|
|
}
|
2013-06-15 09:29:52 -05:00
|
|
|
fn generic_fields_of(cx: &mut CrateContext, r: &Repr, sizing: bool) -> ~[Type] {
|
2013-02-18 16:16:21 -06:00
|
|
|
match *r {
|
2013-06-15 09:29:52 -05:00
|
|
|
CEnum(*) => ~[Type::enum_discrim(cx)],
|
2013-03-11 03:04:08 -05:00
|
|
|
Univariant(ref st, _dtor) => struct_llfields(cx, st, sizing),
|
2013-03-31 17:55:30 -05:00
|
|
|
NullablePointer{ nonnull: ref st, _ } => struct_llfields(cx, st, sizing),
|
2013-02-18 16:16:21 -06:00
|
|
|
General(ref sts) => {
|
2013-03-11 03:04:08 -05:00
|
|
|
// To get "the" type of a general enum, we pick the case
|
|
|
|
// with the largest alignment (so it will always align
|
|
|
|
// correctly in containing structures) and pad it out.
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(sts.len() >= 1);
|
2013-03-11 03:04:08 -05:00
|
|
|
let mut most_aligned = None;
|
|
|
|
let mut largest_align = 0;
|
|
|
|
let mut largest_size = 0;
|
2013-08-03 11:45:23 -05:00
|
|
|
for st in sts.iter() {
|
2013-03-11 03:04:08 -05:00
|
|
|
if largest_size < st.size {
|
|
|
|
largest_size = st.size;
|
|
|
|
}
|
|
|
|
if largest_align < st.align {
|
|
|
|
// Clang breaks ties by size; it is unclear if
|
|
|
|
// that accomplishes anything important.
|
|
|
|
largest_align = st.align;
|
|
|
|
most_aligned = Some(st);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let most_aligned = most_aligned.get();
|
|
|
|
let padding = largest_size - most_aligned.size;
|
|
|
|
|
|
|
|
struct_llfields(cx, most_aligned, sizing)
|
2013-07-02 21:13:00 -05:00
|
|
|
+ &[Type::array(&Type::i8(), padding)]
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 09:29:52 -05:00
|
|
|
fn struct_llfields(cx: &mut CrateContext, st: &Struct, sizing: bool) -> ~[Type] {
|
2013-03-11 03:04:08 -05:00
|
|
|
if sizing {
|
|
|
|
st.fields.map(|&ty| type_of::sizing_type_of(cx, ty))
|
|
|
|
} else {
|
|
|
|
st.fields.map(|&ty| type_of::type_of(cx, ty))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/**
|
2013-02-28 14:13:00 -06:00
|
|
|
* Obtain a representation of the discriminant sufficient to translate
|
|
|
|
* destructuring; this may or may not involve the actual discriminant.
|
|
|
|
*
|
2013-02-28 01:08:52 -06:00
|
|
|
* This should ideally be less tightly tied to `_match`.
|
|
|
|
*/
|
2013-07-17 05:12:08 -05:00
|
|
|
pub fn trans_switch(bcx: @mut Block, r: &Repr, scrutinee: ValueRef)
|
2013-02-28 13:43:20 -06:00
|
|
|
-> (_match::branch_kind, Option<ValueRef>) {
|
2013-02-18 16:16:21 -06:00
|
|
|
match *r {
|
2013-02-28 13:43:20 -06:00
|
|
|
CEnum(*) | General(*) => {
|
2013-02-28 14:13:00 -06:00
|
|
|
(_match::switch, Some(trans_get_discr(bcx, r, scrutinee)))
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
2013-03-31 17:55:30 -05:00
|
|
|
NullablePointer{ nonnull: ref nonnull, nndiscr, ptrfield, _ } => {
|
|
|
|
(_match::switch, Some(nullable_bitdiscr(bcx, nonnull, nndiscr, ptrfield, scrutinee)))
|
|
|
|
}
|
2013-03-11 01:16:08 -05:00
|
|
|
Univariant(*) => {
|
2013-02-18 16:16:21 -06:00
|
|
|
(_match::single, None)
|
|
|
|
}
|
2013-02-28 13:43:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-31 17:55:30 -05:00
|
|
|
|
|
|
|
|
2013-02-28 14:13:00 -06:00
|
|
|
/// Obtain the actual discriminant of a value.
|
2013-07-17 05:12:08 -05:00
|
|
|
pub fn trans_get_discr(bcx: @mut Block, r: &Repr, scrutinee: ValueRef)
|
2013-02-28 13:43:20 -06:00
|
|
|
-> ValueRef {
|
|
|
|
match *r {
|
|
|
|
CEnum(min, max) => load_discr(bcx, scrutinee, min, max),
|
2013-07-23 10:52:44 -05:00
|
|
|
Univariant(*) => C_uint(bcx.ccx(), 0),
|
|
|
|
General(ref cases) => load_discr(bcx, scrutinee, 0, cases.len() - 1),
|
2013-03-31 17:55:30 -05:00
|
|
|
NullablePointer{ nonnull: ref nonnull, nndiscr, ptrfield, _ } => {
|
|
|
|
ZExt(bcx, nullable_bitdiscr(bcx, nonnull, nndiscr, ptrfield, scrutinee),
|
2013-06-15 09:29:52 -05:00
|
|
|
Type::enum_discrim(bcx.ccx()))
|
2013-03-31 17:55:30 -05:00
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-23 10:52:44 -05:00
|
|
|
fn nullable_bitdiscr(bcx: @mut Block, nonnull: &Struct, nndiscr: uint, ptrfield: uint,
|
2013-03-31 17:55:30 -05:00
|
|
|
scrutinee: ValueRef) -> ValueRef {
|
|
|
|
let cmp = if nndiscr == 0 { IntEQ } else { IntNE };
|
|
|
|
let llptr = Load(bcx, GEPi(bcx, scrutinee, [0, ptrfield]));
|
|
|
|
let llptrty = type_of::type_of(bcx.ccx(), nonnull.fields[ptrfield]);
|
|
|
|
ICmp(bcx, cmp, llptr, C_null(llptrty))
|
|
|
|
}
|
|
|
|
|
2013-02-28 14:13:00 -06:00
|
|
|
/// Helper for cases where the discriminant is simply loaded.
|
2013-07-23 10:52:44 -05:00
|
|
|
fn load_discr(bcx: @mut Block, scrutinee: ValueRef, min: uint, max: uint)
|
2013-02-28 13:24:30 -06:00
|
|
|
-> ValueRef {
|
|
|
|
let ptr = GEPi(bcx, scrutinee, [0, 0]);
|
|
|
|
if max + 1 == min {
|
|
|
|
// i.e., if the range is everything. The lo==hi case would be
|
|
|
|
// rejected by the LLVM verifier (it would mean either an
|
|
|
|
// empty set, which is impossible, or the entire range of the
|
|
|
|
// type, which is pointless).
|
|
|
|
Load(bcx, ptr)
|
|
|
|
} else {
|
|
|
|
// llvm::ConstantRange can deal with ranges that wrap around,
|
|
|
|
// so an overflow on (max + 1) is fine.
|
|
|
|
LoadRangeAssert(bcx, ptr, min as c_ulonglong,
|
|
|
|
(max + 1) as c_ulonglong,
|
|
|
|
/* signed: */ True)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/**
|
|
|
|
* Yield information about how to dispatch a case of the
|
|
|
|
* discriminant-like value returned by `trans_switch`.
|
2013-02-28 14:13:00 -06:00
|
|
|
*
|
2013-02-28 01:08:52 -06:00
|
|
|
* This should ideally be less tightly tied to `_match`.
|
|
|
|
*/
|
2013-07-23 10:52:44 -05:00
|
|
|
pub fn trans_case(bcx: @mut Block, r: &Repr, discr: uint) -> _match::opt_result {
|
2013-02-18 16:16:21 -06:00
|
|
|
match *r {
|
2013-02-23 02:59:24 -06:00
|
|
|
CEnum(*) => {
|
2013-07-23 10:52:44 -05:00
|
|
|
_match::single_result(rslt(bcx, C_uint(bcx.ccx(), discr)))
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
2013-03-31 17:55:30 -05:00
|
|
|
Univariant(*) => {
|
2013-05-19 00:07:44 -05:00
|
|
|
bcx.ccx().sess.bug("no cases for univariants or structs")
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
General(*) => {
|
2013-07-23 10:52:44 -05:00
|
|
|
_match::single_result(rslt(bcx, C_uint(bcx.ccx(), discr)))
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
2013-03-31 17:55:30 -05:00
|
|
|
NullablePointer{ _ } => {
|
|
|
|
assert!(discr == 0 || discr == 1);
|
|
|
|
_match::single_result(rslt(bcx, C_i1(discr != 0)))
|
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/**
|
|
|
|
* Begin initializing a new value of the given case of the given
|
2013-03-02 18:08:49 -06:00
|
|
|
* representation. The fields, if any, should then be initialized via
|
|
|
|
* `trans_field_ptr`.
|
2013-02-28 01:08:52 -06:00
|
|
|
*/
|
2013-07-23 10:52:44 -05:00
|
|
|
pub fn trans_start_init(bcx: @mut Block, r: &Repr, val: ValueRef, discr: uint) {
|
2013-02-18 16:16:21 -06:00
|
|
|
match *r {
|
2013-02-23 02:59:24 -06:00
|
|
|
CEnum(min, max) => {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(min <= discr && discr <= max);
|
2013-07-23 10:52:44 -05:00
|
|
|
Store(bcx, C_uint(bcx.ccx(), discr), GEPi(bcx, val, [0, 0]))
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
2013-03-11 00:58:44 -05:00
|
|
|
Univariant(ref st, true) => {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(discr, 0);
|
2013-03-11 00:58:44 -05:00
|
|
|
Store(bcx, C_bool(true),
|
|
|
|
GEPi(bcx, val, [0, st.fields.len() - 1]))
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
Univariant(*) => {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(discr, 0);
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
General(*) => {
|
2013-07-23 10:52:44 -05:00
|
|
|
Store(bcx, C_uint(bcx.ccx(), discr), GEPi(bcx, val, [0, 0]))
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
2013-03-31 17:55:30 -05:00
|
|
|
NullablePointer{ nonnull: ref nonnull, nndiscr, ptrfield, _ } => {
|
|
|
|
if discr != nndiscr {
|
|
|
|
let llptrptr = GEPi(bcx, val, [0, ptrfield]);
|
|
|
|
let llptrty = type_of::type_of(bcx.ccx(), nonnull.fields[ptrfield]);
|
|
|
|
Store(bcx, C_null(llptrty), llptrptr)
|
|
|
|
}
|
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/**
|
|
|
|
* The number of fields in a given case; for use when obtaining this
|
|
|
|
* information from the type or definition is less convenient.
|
|
|
|
*/
|
2013-07-23 10:52:44 -05:00
|
|
|
pub fn num_args(r: &Repr, discr: uint) -> uint {
|
2013-02-18 16:16:21 -06:00
|
|
|
match *r {
|
2013-03-11 01:16:08 -05:00
|
|
|
CEnum(*) => 0,
|
2013-03-11 00:58:44 -05:00
|
|
|
Univariant(ref st, dtor) => {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(discr, 0);
|
2013-03-11 00:58:44 -05:00
|
|
|
st.fields.len() - (if dtor { 1 } else { 0 })
|
|
|
|
}
|
2013-07-23 10:52:44 -05:00
|
|
|
General(ref cases) => cases[discr].fields.len() - 1,
|
2013-04-30 13:36:22 -05:00
|
|
|
NullablePointer{ nonnull: ref nonnull, nndiscr, nullfields: ref nullfields, _ } => {
|
|
|
|
if discr == nndiscr { nonnull.fields.len() } else { nullfields.len() }
|
2013-03-31 17:55:30 -05:00
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/// Access a field, at a point when the value's case is known.
|
2013-07-23 10:52:44 -05:00
|
|
|
pub fn trans_field_ptr(bcx: @mut Block, r: &Repr, val: ValueRef, discr: uint,
|
2013-03-02 18:08:49 -06:00
|
|
|
ix: uint) -> ValueRef {
|
2013-02-18 16:16:21 -06:00
|
|
|
// Note: if this ever needs to generate conditionals (e.g., if we
|
|
|
|
// decide to do some kind of cdr-coding-like non-unique repr
|
2013-02-28 01:08:52 -06:00
|
|
|
// someday), it will need to return a possibly-new bcx as well.
|
2013-02-18 16:16:21 -06:00
|
|
|
match *r {
|
2013-03-11 01:16:08 -05:00
|
|
|
CEnum(*) => {
|
2013-05-19 00:07:44 -05:00
|
|
|
bcx.ccx().sess.bug("element access in C-like enum")
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
2013-03-11 00:58:44 -05:00
|
|
|
Univariant(ref st, _dtor) => {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(discr, 0);
|
2013-03-02 18:08:49 -06:00
|
|
|
struct_field_ptr(bcx, st, val, ix, false)
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
General(ref cases) => {
|
2013-07-23 10:52:44 -05:00
|
|
|
struct_field_ptr(bcx, &cases[discr], val, ix + 1, true)
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
2013-03-31 17:55:30 -05:00
|
|
|
NullablePointer{ nonnull: ref nonnull, nullfields: ref nullfields, nndiscr, _ } => {
|
|
|
|
if (discr == nndiscr) {
|
|
|
|
struct_field_ptr(bcx, nonnull, val, ix, false)
|
|
|
|
} else {
|
|
|
|
// The unit-like case might have a nonzero number of unit-like fields.
|
|
|
|
// (e.g., Result or Either with () as one side.)
|
2013-06-15 09:29:52 -05:00
|
|
|
let ty = type_of::type_of(bcx.ccx(), nullfields[ix]);
|
2013-06-16 05:52:44 -05:00
|
|
|
assert_eq!(machine::llsize_of_alloc(bcx.ccx(), ty), 0);
|
2013-03-31 17:55:30 -05:00
|
|
|
// The contents of memory at this pointer can't matter, but use
|
|
|
|
// the value that's "reasonable" in case of pointer comparison.
|
2013-06-15 09:29:52 -05:00
|
|
|
PointerCast(bcx, val, ty.ptr_to())
|
2013-03-31 17:55:30 -05:00
|
|
|
}
|
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-17 05:12:08 -05:00
|
|
|
fn struct_field_ptr(bcx: @mut Block, st: &Struct, val: ValueRef, ix: uint,
|
2013-02-23 00:03:59 -06:00
|
|
|
needs_cast: bool) -> ValueRef {
|
2013-02-18 16:16:21 -06:00
|
|
|
let ccx = bcx.ccx();
|
|
|
|
|
2013-02-23 00:03:59 -06:00
|
|
|
let val = if needs_cast {
|
2013-06-13 02:19:50 -05:00
|
|
|
let fields = do st.fields.map |&ty| {
|
|
|
|
type_of::type_of(ccx, ty)
|
|
|
|
};
|
2013-06-15 09:29:52 -05:00
|
|
|
let real_ty = Type::struct_(fields, st.packed);
|
2013-06-16 05:52:44 -05:00
|
|
|
PointerCast(bcx, val, real_ty.ptr_to())
|
2013-02-23 00:03:59 -06:00
|
|
|
} else {
|
|
|
|
val
|
|
|
|
};
|
2013-02-18 16:16:21 -06:00
|
|
|
|
2013-02-23 00:03:59 -06:00
|
|
|
GEPi(bcx, val, [0, ix])
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/// Access the struct drop flag, if present.
|
2013-07-17 05:12:08 -05:00
|
|
|
pub fn trans_drop_flag_ptr(bcx: @mut Block, r: &Repr, val: ValueRef) -> ValueRef {
|
2013-02-25 01:20:08 -06:00
|
|
|
match *r {
|
2013-03-11 00:58:44 -05:00
|
|
|
Univariant(ref st, true) => GEPi(bcx, val, [0, st.fields.len() - 1]),
|
2013-05-19 00:07:44 -05:00
|
|
|
_ => bcx.ccx().sess.bug("tried to get drop flag of non-droppable type")
|
2013-02-25 01:20:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/**
|
|
|
|
* Construct a constant value, suitable for initializing a
|
|
|
|
* GlobalVariable, given a case and constant values for its fields.
|
|
|
|
* Note that this may have a different LLVM type (and different
|
|
|
|
* alignment!) from the representation's `type_of`, so it needs a
|
|
|
|
* pointer cast before use.
|
|
|
|
*
|
2013-03-02 16:03:41 -06:00
|
|
|
* The LLVM type system does not directly support unions, and only
|
|
|
|
* pointers can be bitcast, so a constant (and, by extension, the
|
|
|
|
* GlobalVariable initialized by it) will have a type that can vary
|
|
|
|
* depending on which case of an enum it is.
|
|
|
|
*
|
|
|
|
* To understand the alignment situation, consider `enum E { V64(u64),
|
2013-03-11 03:04:08 -05:00
|
|
|
* V32(u32, u32) }` on win32. The type has 8-byte alignment to
|
|
|
|
* accommodate the u64, but `V32(x, y)` would have LLVM type `{i32,
|
|
|
|
* i32, i32}`, which is 4-byte aligned.
|
2013-03-02 16:03:41 -06:00
|
|
|
*
|
|
|
|
* Currently the returned value has the same size as the type, but
|
2013-03-11 03:04:08 -05:00
|
|
|
* this could be changed in the future to avoid allocating unnecessary
|
2013-03-02 16:03:41 -06:00
|
|
|
* space after values of shorter-than-maximum cases.
|
2013-02-28 01:08:52 -06:00
|
|
|
*/
|
2013-07-23 10:52:44 -05:00
|
|
|
pub fn trans_const(ccx: &mut CrateContext, r: &Repr, discr: uint,
|
2013-02-18 16:16:21 -06:00
|
|
|
vals: &[ValueRef]) -> ValueRef {
|
|
|
|
match *r {
|
2013-02-23 02:59:24 -06:00
|
|
|
CEnum(min, max) => {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(vals.len(), 0);
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(min <= discr && discr <= max);
|
2013-07-23 10:52:44 -05:00
|
|
|
C_uint(ccx, discr)
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
2013-03-11 00:58:44 -05:00
|
|
|
Univariant(ref st, _dro) => {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(discr, 0);
|
2013-03-11 00:58:44 -05:00
|
|
|
C_struct(build_const_struct(ccx, st, vals))
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
General(ref cases) => {
|
2013-07-23 10:52:44 -05:00
|
|
|
let case = &cases[discr];
|
2013-06-15 02:16:10 -05:00
|
|
|
let max_sz = cases.iter().transform(|x| x.size).max().unwrap();
|
2013-07-23 10:52:44 -05:00
|
|
|
let discr_ty = C_uint(ccx, discr);
|
2013-03-11 03:04:08 -05:00
|
|
|
let contents = build_const_struct(ccx, case,
|
2013-06-13 02:19:50 -05:00
|
|
|
~[discr_ty] + vals);
|
2013-07-02 21:13:00 -05:00
|
|
|
C_struct(contents + &[padding(max_sz - case.size)])
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
2013-03-31 17:55:30 -05:00
|
|
|
NullablePointer{ nonnull: ref nonnull, nndiscr, ptrfield, _ } => {
|
|
|
|
if discr == nndiscr {
|
|
|
|
C_struct(build_const_struct(ccx, nonnull, vals))
|
|
|
|
} else {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(vals.len(), 0);
|
2013-06-29 00:05:50 -05:00
|
|
|
let vals = do nonnull.fields.iter().enumerate().transform |(i, &ty)| {
|
2013-03-31 17:55:30 -05:00
|
|
|
let llty = type_of::sizing_type_of(ccx, ty);
|
|
|
|
if i == ptrfield { C_null(llty) } else { C_undef(llty) }
|
2013-06-29 00:05:50 -05:00
|
|
|
}.collect::<~[ValueRef]>();
|
2013-03-31 17:55:30 -05:00
|
|
|
C_struct(build_const_struct(ccx, nonnull, vals))
|
|
|
|
}
|
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 13:24:30 -06:00
|
|
|
/**
|
|
|
|
* Building structs is a little complicated, because we might need to
|
|
|
|
* insert padding if a field's value is less aligned than its type.
|
2013-03-02 16:03:41 -06:00
|
|
|
*
|
|
|
|
* Continuing the example from `trans_const`, a value of type `(u32,
|
|
|
|
* E)` should have the `E` at offset 8, but if that field's
|
|
|
|
* initializer is 4-byte aligned then simply translating the tuple as
|
|
|
|
* a two-element struct will locate it at offset 4, and accesses to it
|
|
|
|
* will read the wrong memory.
|
2013-02-28 13:24:30 -06:00
|
|
|
*/
|
2013-06-13 02:19:50 -05:00
|
|
|
fn build_const_struct(ccx: &mut CrateContext, st: &Struct, vals: &[ValueRef])
|
2013-02-18 16:16:21 -06:00
|
|
|
-> ~[ValueRef] {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(vals.len(), st.fields.len());
|
2013-02-18 16:16:21 -06:00
|
|
|
|
|
|
|
let mut offset = 0;
|
|
|
|
let mut cfields = ~[];
|
2013-08-03 11:45:23 -05:00
|
|
|
for (i, &ty) in st.fields.iter().enumerate() {
|
2013-02-18 16:16:21 -06:00
|
|
|
let llty = type_of::sizing_type_of(ccx, ty);
|
|
|
|
let type_align = machine::llalign_of_min(ccx, llty)
|
|
|
|
/*bad*/as u64;
|
|
|
|
let val_align = machine::llalign_of_min(ccx, val_ty(vals[i]))
|
|
|
|
/*bad*/as u64;
|
|
|
|
let target_offset = roundup(offset, type_align);
|
|
|
|
offset = roundup(offset, val_align);
|
|
|
|
if (offset != target_offset) {
|
|
|
|
cfields.push(padding(target_offset - offset));
|
|
|
|
offset = target_offset;
|
|
|
|
}
|
2013-03-31 17:55:30 -05:00
|
|
|
let val = if is_undef(vals[i]) {
|
|
|
|
let wrapped = C_struct([vals[i]]);
|
|
|
|
assert!(!is_undef(wrapped));
|
|
|
|
wrapped
|
|
|
|
} else {
|
|
|
|
vals[i]
|
|
|
|
};
|
|
|
|
cfields.push(val);
|
2013-05-30 19:08:49 -05:00
|
|
|
offset += machine::llsize_of_alloc(ccx, llty) as u64
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return cfields;
|
|
|
|
}
|
|
|
|
|
2013-02-28 13:24:30 -06:00
|
|
|
fn padding(size: u64) -> ValueRef {
|
2013-06-16 05:52:44 -05:00
|
|
|
C_undef(Type::array(&Type::i8(), size))
|
2013-02-28 13:24:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXX this utility routine should be somewhere more general
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-02-18 16:16:21 -06:00
|
|
|
fn roundup(x: u64, a: u64) -> u64 { ((x + (a - 1)) / a) * a }
|
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/// Get the discriminant of a constant value. (Not currently used.)
|
2013-06-13 02:19:50 -05:00
|
|
|
pub fn const_get_discrim(ccx: &mut CrateContext, r: &Repr, val: ValueRef)
|
2013-07-23 10:52:44 -05:00
|
|
|
-> uint {
|
2013-02-18 16:16:21 -06:00
|
|
|
match *r {
|
2013-07-23 10:52:44 -05:00
|
|
|
CEnum(*) => const_to_uint(val) as uint,
|
2013-02-18 16:16:21 -06:00
|
|
|
Univariant(*) => 0,
|
2013-07-23 10:52:44 -05:00
|
|
|
General(*) => const_to_uint(const_get_elt(ccx, val, [0])) as uint,
|
2013-03-31 17:55:30 -05:00
|
|
|
NullablePointer{ nndiscr, ptrfield, _ } => {
|
2013-07-23 10:52:44 -05:00
|
|
|
if is_null(const_struct_field(ccx, val, ptrfield)) {
|
|
|
|
/* subtraction as uint is ok because nndiscr is either 0 or 1 */
|
|
|
|
(1 - nndiscr) as uint
|
|
|
|
} else {
|
|
|
|
nndiscr
|
|
|
|
}
|
2013-03-31 17:55:30 -05:00
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-02 18:08:49 -06:00
|
|
|
/**
|
|
|
|
* Extract a field of a constant value, as appropriate for its
|
|
|
|
* representation.
|
|
|
|
*
|
|
|
|
* (Not to be confused with `common::const_get_elt`, which operates on
|
|
|
|
* raw LLVM-level structs and arrays.)
|
|
|
|
*/
|
2013-06-13 02:19:50 -05:00
|
|
|
pub fn const_get_field(ccx: &mut CrateContext, r: &Repr, val: ValueRef,
|
2013-07-23 10:52:44 -05:00
|
|
|
_discr: uint, ix: uint) -> ValueRef {
|
2013-02-18 16:16:21 -06:00
|
|
|
match *r {
|
2013-05-19 00:07:44 -05:00
|
|
|
CEnum(*) => ccx.sess.bug("element access in C-like enum const"),
|
2013-03-11 00:58:44 -05:00
|
|
|
Univariant(*) => const_struct_field(ccx, val, ix),
|
2013-03-31 17:55:30 -05:00
|
|
|
General(*) => const_struct_field(ccx, val, ix + 1),
|
|
|
|
NullablePointer{ _ } => const_struct_field(ccx, val, ix)
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 13:24:30 -06:00
|
|
|
/// Extract field of struct-like const, skipping our alignment padding.
|
2013-06-13 02:19:50 -05:00
|
|
|
fn const_struct_field(ccx: &mut CrateContext, val: ValueRef, ix: uint)
|
2013-02-18 16:16:21 -06:00
|
|
|
-> ValueRef {
|
|
|
|
// Get the ix-th non-undef element of the struct.
|
|
|
|
let mut real_ix = 0; // actual position in the struct
|
|
|
|
let mut ix = ix; // logical index relative to real_ix
|
|
|
|
let mut field;
|
|
|
|
loop {
|
|
|
|
loop {
|
|
|
|
field = const_get_elt(ccx, val, [real_ix]);
|
|
|
|
if !is_undef(field) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
real_ix = real_ix + 1;
|
|
|
|
}
|
|
|
|
if ix == 0 {
|
|
|
|
return field;
|
|
|
|
}
|
|
|
|
ix = ix - 1;
|
|
|
|
real_ix = real_ix + 1;
|
|
|
|
}
|
|
|
|
}
|
2013-02-24 16:01:43 -06:00
|
|
|
|
|
|
|
/// Is it safe to bitcast a value to the one field of its one variant?
|
|
|
|
pub fn is_newtypeish(r: &Repr) -> bool {
|
|
|
|
match *r {
|
2013-03-11 00:58:44 -05:00
|
|
|
Univariant(ref st, false) => st.fields.len() == 1,
|
2013-02-24 16:01:43 -06:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|