]> git.lizzy.rs Git - rust.git/blob - src/constant.rs
Implement int casts
[rust.git] / src / constant.rs
1 use prelude::*;
2 use rustc::mir::interpret::{GlobalId, AllocId, read_target_uint};
3 use rustc_mir::interpret::{CompileTimeEvaluator, Memory, MemoryKind};
4 use cranelift_module::*;
5
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
10             .tcx
11             .const_eval(ParamEnv::reveal_all().and(GlobalId {
12                 instance: fx.instance,
13                 promoted: Some(index),
14             }))
15             .unwrap(),
16     };
17
18     let ty = fx.monomorphize(&const_.ty);
19     let layout = fx.layout_of(ty);
20     match ty.sty {
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)
24         }
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)
28         }
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)
32         }
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)
36         }
37         _ => {
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)
47         }
48     }
49 }
50
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)
57         }
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);
61             vac.insert(data_id);
62             (data_id, true)
63         }
64     }
65 }
66
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) {
69         return *data_id;
70     }
71
72     let mut todo = HashMap::new();
73     let mut done = HashSet::new();
74     define_global_for_alloc_id(fx, alloc_id, &mut todo);
75
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);
78
79         let alloc = memory.get(alloc_id).unwrap();
80         let mut data_ctx = DataContext::new();
81
82         data_ctx.define(alloc.bytes.to_vec().into_boxed_slice(), Writability::Readonly);
83
84         for &(offset, reloc) in alloc.relocations.iter() {
85             let data_id = define_global_for_alloc_id(fx, reloc, &mut todo).0;
86
87             let reloc_offset = {
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()
93             };
94
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);
98         }
99
100         fx.module.define_data(data_id, &data_ctx).unwrap();
101         done.insert(data_id);
102     }
103     for data_id in done.drain() {
104         fx.module.finalize_data(data_id);
105     }
106     *fx.constants.get(&alloc_id).unwrap()
107 }