1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use llvm_sys::core::*;
use llvm_sys::prelude::*;
use arret_runtime::abitype::{BoxedAbiType, EncodeBoxedAbiType, TOP_LIST_BOXED_ABI_TYPE};
use arret_runtime::boxed;
use arret_runtime::boxed::TypeTag;
use crate::codegen::record_struct;
use crate::codegen::target_gen::TargetCtx;
use crate::codegen::GenAbi;
#[derive(Clone, Hash, PartialEq, Eq)]
pub enum BoxLayout {
Any,
Bool,
Num,
List,
Union,
ConstTagged(boxed::TypeTag),
}
impl BoxLayout {
pub fn type_name(&self) -> &'static [u8] {
match self {
BoxLayout::Any => b"boxed_any\0",
BoxLayout::Bool => b"boxed_bool\0",
BoxLayout::Num => b"boxed_num\0",
BoxLayout::List => b"boxed_list\0",
BoxLayout::Union => b"boxed_union\0",
BoxLayout::ConstTagged(TypeTag::Nil) => b"boxed_nil\0",
BoxLayout::ConstTagged(TypeTag::True) => b"boxed_true\0",
BoxLayout::ConstTagged(TypeTag::False) => b"boxed_false\0",
BoxLayout::ConstTagged(TypeTag::Int) => b"boxed_int\0",
BoxLayout::ConstTagged(TypeTag::Float) => b"boxed_float\0",
BoxLayout::ConstTagged(TypeTag::Char) => b"boxed_char\0",
BoxLayout::ConstTagged(TypeTag::Set) => b"boxed_set\0",
BoxLayout::ConstTagged(TypeTag::Str) => b"boxed_str\0",
BoxLayout::ConstTagged(TypeTag::Sym) => b"boxed_sym\0",
BoxLayout::ConstTagged(TypeTag::FunThunk) => b"boxed_fun_thunk\0",
BoxLayout::ConstTagged(TypeTag::Pair) => b"boxed_pair\0",
BoxLayout::ConstTagged(TypeTag::Vector) => b"boxed_vector\0",
BoxLayout::ConstTagged(TypeTag::Record) => b"boxed_record\0",
BoxLayout::ConstTagged(TypeTag::Map) => b"boxed_map\0",
}
}
pub fn append_members(&self, tcx: &mut TargetCtx, members: &mut Vec<LLVMTypeRef>) {
unsafe {
match self {
BoxLayout::Any => {
use std::mem;
let llvm_byte = LLVMInt8TypeInContext(tcx.llx);
let padding_bytes =
mem::size_of::<boxed::Any>() - mem::size_of::<boxed::Header>();
members.push(LLVMArrayType(llvm_byte, padding_bytes as u32));
}
BoxLayout::ConstTagged(TypeTag::Int) => {
members.push(LLVMInt64TypeInContext(tcx.llx));
}
BoxLayout::ConstTagged(TypeTag::Float) => {
members.push(LLVMDoubleTypeInContext(tcx.llx));
}
BoxLayout::ConstTagged(TypeTag::Char) => {
members.push(LLVMInt32TypeInContext(tcx.llx));
}
BoxLayout::ConstTagged(TypeTag::Str) => {
members.push(LLVMInt8TypeInContext(tcx.llx));
}
BoxLayout::ConstTagged(TypeTag::Sym) => {
members.push(LLVMInt64TypeInContext(tcx.llx));
}
BoxLayout::ConstTagged(TypeTag::FunThunk) => {
members.extend_from_slice(&[
tcx.captures_llvm_type(),
LLVMPointerType(tcx.fun_abi_to_llvm_type(&GenAbi::thunk_abi()), 0),
]);
}
BoxLayout::ConstTagged(TypeTag::Pair) => {
let llvm_i64 = LLVMInt64TypeInContext(tcx.llx);
let llvm_any_ptr = tcx.boxed_abi_to_llvm_ptr_type(&BoxedAbiType::Any);
let llvm_any_list_ptr =
tcx.boxed_abi_to_llvm_ptr_type(&TOP_LIST_BOXED_ABI_TYPE);
members.extend_from_slice(&[llvm_i64, llvm_any_ptr, llvm_any_list_ptr]);
}
BoxLayout::ConstTagged(TypeTag::Record) => {
record_struct::append_common_internal_members(tcx, members);
}
BoxLayout::List => {
members.push(LLVMInt64TypeInContext(tcx.llx));
}
BoxLayout::ConstTagged(TypeTag::Set) => {
let llvm_i32 = LLVMInt32TypeInContext(tcx.llx);
let llvm_any_ptr = tcx.boxed_abi_to_llvm_ptr_type(&BoxedAbiType::Any);
members.extend_from_slice(&[llvm_i32, llvm_any_ptr, llvm_any_ptr, llvm_any_ptr])
}
BoxLayout::ConstTagged(TypeTag::Vector) => {
members.push(LLVMInt32TypeInContext(tcx.llx));
}
BoxLayout::ConstTagged(TypeTag::Nil)
| BoxLayout::ConstTagged(TypeTag::True)
| BoxLayout::ConstTagged(TypeTag::False)
| BoxLayout::ConstTagged(TypeTag::Map)
| BoxLayout::Bool
| BoxLayout::Num
| BoxLayout::Union => {}
};
}
}
}
impl From<&BoxedAbiType> for BoxLayout {
fn from(boxed_abi_type: &BoxedAbiType) -> BoxLayout {
match boxed_abi_type {
BoxedAbiType::Any => BoxLayout::Any,
BoxedAbiType::List(_) => BoxLayout::List,
&boxed::Num::BOXED_ABI_TYPE => BoxLayout::Num,
&boxed::Bool::BOXED_ABI_TYPE => BoxLayout::Bool,
BoxedAbiType::Union(_, _) => BoxLayout::Union,
BoxedAbiType::UniqueTagged(type_tag) => BoxLayout::ConstTagged(*type_tag),
BoxedAbiType::Pair(_) => BoxLayout::ConstTagged(TypeTag::Pair),
BoxedAbiType::Set(_) => BoxLayout::ConstTagged(TypeTag::Set),
BoxedAbiType::Vector(_) => BoxLayout::ConstTagged(TypeTag::Vector),
BoxedAbiType::Map(_, _) => BoxLayout::ConstTagged(TypeTag::Map),
}
}
}