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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use base::fnv::FnvMap;
use base::kind::{ArcKind, Kind, KindEnv};
use base::symbol::{Symbol, SymbolRef};
use base::types::{Alias, ArcType, TypeEnv, Type};
pub use self::Instruction::*;
pub type VmIndex = u32;
pub type VmTag = u32;
pub type VmInt = isize;
#[derive(Copy, Clone, Debug)]
pub enum Instruction {
PushInt(isize),
PushByte(u8),
PushFloat(f64),
PushString(VmIndex),
PushUpVar(VmIndex),
Push(VmIndex),
PushGlobal(VmIndex),
Call(VmIndex),
TailCall(VmIndex),
Construct {
tag: VmIndex,
args: VmIndex,
},
ConstructRecord {
record: VmIndex,
args: VmIndex,
},
ConstructArray(VmIndex),
GetOffset(VmIndex),
GetField(VmIndex),
Split,
TestTag(VmTag),
Jump(VmIndex),
CJump(VmIndex),
Pop(VmIndex),
Slide(VmIndex),
MakeClosure {
function_index: VmIndex,
upvars: VmIndex,
},
NewClosure {
function_index: VmIndex,
upvars: VmIndex,
},
CloseClosure(VmIndex),
AddInt,
SubtractInt,
MultiplyInt,
DivideInt,
IntLT,
IntEQ,
AddByte,
SubtractByte,
MultiplyByte,
DivideByte,
ByteLT,
ByteEQ,
AddFloat,
SubtractFloat,
MultiplyFloat,
DivideFloat,
FloatLT,
FloatEQ,
}
impl Instruction {
pub fn adjust(&self) -> i32 {
match *self {
PushInt(_) | PushByte(_) | PushFloat(_) | PushString(_) | Push(_) | PushGlobal(_) => 1,
Call(n) => -(n as i32),
TailCall(n) => -(n as i32),
Construct { args, .. } |
ConstructRecord { args, .. } |
ConstructArray(args) => 1 - args as i32,
GetField(_) | GetOffset(_) => 0,
Split => -1,
TestTag(_) => 1,
Jump(_) => 0,
CJump(_) => -1,
Pop(n) => -(n as i32),
Slide(n) => -(n as i32),
MakeClosure { .. } => 1,
NewClosure { .. } => 1,
CloseClosure(_) => -1,
PushUpVar(_) => 1,
AddInt | SubtractInt | MultiplyInt | DivideInt | IntLT | IntEQ | AddFloat |
AddByte | SubtractByte | MultiplyByte | DivideByte | ByteLT | ByteEQ |
SubtractFloat | MultiplyFloat | DivideFloat | FloatLT | FloatEQ => -1,
}
}
}
#[derive(Debug)]
pub struct TypeInfos {
pub id_to_type: FnvMap<String, Alias<Symbol, ArcType>>,
}
impl KindEnv for TypeInfos {
fn find_kind(&self, type_name: &SymbolRef) -> Option<ArcKind> {
let type_name = AsRef::<str>::as_ref(type_name);
self.id_to_type
.get(type_name)
.map(|alias| {
alias.args.iter().rev().fold(Kind::typ(),
|acc, arg| Kind::function(arg.kind.clone(), acc))
})
}
}
impl TypeEnv for TypeInfos {
fn find_type(&self, id: &SymbolRef) -> Option<&ArcType> {
let id = AsRef::<str>::as_ref(id);
self.id_to_type
.iter()
.filter_map(|(_, ref alias)| match **alias.unresolved_type() {
Type::Variant(ref row) => row.row_iter().find(|field| field.name.as_ref() == id),
_ => None,
})
.next()
.map(|field| &field.typ)
}
fn find_type_info(&self, id: &SymbolRef) -> Option<&Alias<Symbol, ArcType>> {
let id = AsRef::<str>::as_ref(id);
self.id_to_type
.get(id)
}
fn find_record(&self, _fields: &[Symbol]) -> Option<(ArcType, ArcType)> {
None
}
}
impl TypeInfos {
pub fn new() -> TypeInfos {
TypeInfos { id_to_type: FnvMap::default() }
}
pub fn extend(&mut self, other: TypeInfos) {
let TypeInfos { id_to_type } = other;
self.id_to_type.extend(id_to_type);
}
}