2 use rustc::mir::interpret::{GlobalId, AllocId, read_target_uint};
3 use rustc_mir::interpret::{CompileTimeEvaluator, Memory, MemoryKind};
4 use cranelift_module::*;
6 pub fn trans_constant<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, const_: &Constant<'tcx>) -> CValue<'tcx> {
7 let value = match const_.literal {
8 Literal::Value { value } => value,
9 Literal::Promoted { index } => fx
11 .const_eval(ParamEnv::reveal_all().and(GlobalId {
12 instance: fx.instance,
13 promoted: Some(index),
18 let ty = fx.monomorphize(&const_.ty);
19 let layout = fx.layout_of(ty);
21 TypeVariants::TyBool => {
22 let bits = value.to_scalar().unwrap().to_bits(layout.size).unwrap();
23 CValue::const_val(fx, ty, bits as u64 as i64)
25 TypeVariants::TyUint(_) => {
26 let bits = value.to_scalar().unwrap().to_bits(layout.size).unwrap();
27 CValue::const_val(fx, ty, bits as u64 as i64)
29 TypeVariants::TyInt(_) => {
30 let bits = value.to_scalar().unwrap().to_bits(layout.size).unwrap();
31 CValue::const_val(fx, ty, bits as i128 as i64)
33 TypeVariants::TyFnDef(def_id, substs) => {
34 let func_ref = fx.get_function_ref(Instance::new(def_id, substs));
35 CValue::Func(func_ref, layout)
38 let mut memory = Memory::<CompileTimeEvaluator>::new(fx.tcx.at(DUMMY_SP), ());
39 let alloc = fx.tcx.const_value_to_allocation(value);
40 //println!("const value: {:?} allocation: {:?}", value, alloc);
41 let alloc_id = memory.allocate_value(alloc.clone(), MemoryKind::Stack).unwrap();
42 let data_id = get_global_for_alloc_id(fx, &memory, alloc_id);
43 let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
44 // TODO: does global_value return a ptr of a val?
45 let global_ptr = fx.bcx.ins().global_value(types::I64, local_data_id);
46 CValue::ByRef(global_ptr, layout)
51 // If ret.1 is true, then the global didn't exist before
52 fn define_global_for_alloc_id(fx: &mut FunctionCx, alloc_id: AllocId, todo: &mut HashMap<AllocId, DataId>) -> (DataId, bool) {
53 use std::collections::hash_map::Entry;
54 match fx.constants.entry(alloc_id) {
55 Entry::Occupied(mut occ) => {
56 (*occ.get_mut(), false)
58 Entry::Vacant(vac) => {
59 let data_id = fx.module.declare_data(&alloc_id.0.to_string(), Linkage::Local, false).unwrap();
60 todo.insert(alloc_id, data_id);
67 fn get_global_for_alloc_id(fx: &mut FunctionCx, memory: &Memory<CompileTimeEvaluator>, alloc_id: AllocId) -> DataId {
68 if let Some(data_id) = fx.constants.get(&alloc_id) {
72 let mut todo = HashMap::new();
73 let mut done = HashSet::new();
74 define_global_for_alloc_id(fx, alloc_id, &mut todo);
76 while let Some((alloc_id, data_id)) = { let next = todo.drain().next(); next } {
77 println!("cur: {:?}:{:?} todo: {:?} done: {:?}", alloc_id, data_id, todo, done);
79 let alloc = memory.get(alloc_id).unwrap();
80 let mut data_ctx = DataContext::new();
82 data_ctx.define(alloc.bytes.to_vec().into_boxed_slice(), Writability::Readonly);
84 for &(offset, reloc) in alloc.relocations.iter() {
85 let data_id = define_global_for_alloc_id(fx, reloc, &mut todo).0;
88 let endianness = memory.endianness();
89 let offset = offset.bytes() as usize;
90 let ptr_size = fx.tcx.data_layout.pointer_size;
91 let bytes = &alloc.bytes[offset..offset + ptr_size.bytes() as usize];
92 read_target_uint(endianness, bytes).unwrap()
95 // TODO: is this a correct usage of the api
96 let global_value = fx.module.declare_data_in_data(data_id, &mut data_ctx);
97 data_ctx.write_data_addr(reloc_offset as u32, global_value, 0);
100 fx.module.define_data(data_id, &data_ctx).unwrap();
101 done.insert(data_id);
103 for data_id in done.drain() {
104 fx.module.finalize_data(data_id);
106 *fx.constants.get(&alloc_id).unwrap()