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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
mod builtins;
mod jit;
mod jit_builtins;
pub mod types;
use rustyline::error::ReadlineError;
use thiserror::Error;
use types::*;
// resolve functions so we don't need to do a costly hashmap lookup
fn resolve_funcmap(funcs: &mut FuncMap) {
for function in &mut funcs.functions {
let Function::User(_, f) = function;
for token in f {
if let Instr::FunctionCall(FuncRef::Unresolved(name)) = token
&& let Some(resolved) = funcs.map.get(name)
{
*token = Instr::FunctionCall(FuncRef::Resolved(*resolved));
}
}
}
}
fn parse(token: &str) -> Token {
use Token::*;
match token {
"print" => Print,
"quit" => Quit,
"drop" => Drop,
"swap" => Swap,
"rot" => Rot,
"pick" => Pick,
"if" => If,
"skip" => Skip,
":" => Colon,
";" => Semicolon,
// "syscall" => Syscall,
id => match id.parse() {
Ok(num) => Literal(num),
Err(_) => Identifier(id.to_string()),
},
}
}
impl ClacState {
fn execute<'cs>(
functions: &'cs FuncMap,
stack: &mut Stack,
jit: &JITState,
token: &Instr,
) -> Result<ExecRes<'cs>, ExecError> {
let mut xpop = || stack.pop().ok_or(ExecError::MissingArguments);
match token {
Instr::Literal(n) => {
stack.push(*n);
Ok(ExecRes::Executed)
}
Instr::Quit => Err(ExecError::Quit),
Instr::FunctionCall(state) => {
let f = match state {
FuncRef::Resolved(x) => &functions.functions[*x],
FuncRef::Unresolved(name) => match functions.map.get(name) {
Some(_) => unreachable!("Should have already been resolved"), // NOTE: we SHOULD be executing top level, because otherwise this token should have already been resolved.
None => return Err(ExecError::UnknownFunction(name.to_string())),
},
};
match f {
Function::User(fid, code) => match fid {
Some(compiled) => {
let asm = jit.get_function(*compiled);
let new_rsp = unsafe { asm(stack.rsp) };
stack.rsp = new_rsp;
Ok(ExecRes::Executed)
}
None => Ok(ExecRes::RecursiveCall(code)),
},
}
}
Instr::Print => {
println!("{}", xpop()?);
Ok(ExecRes::Executed)
}
Instr::Drop => {
xpop()?;
Ok(ExecRes::Executed)
}
Instr::Swap => {
let b = xpop()?;
let a = xpop()?;
stack.push(b);
stack.push(a);
Ok(ExecRes::Executed)
}
Instr::Rot => {
let z = xpop()?;
let y = xpop()?;
let x = xpop()?;
stack.push(y);
stack.push(z);
stack.push(x);
Ok(ExecRes::Executed)
}
Instr::If => match xpop()? {
0 => Ok(ExecRes::Skip(3)),
_ => Ok(ExecRes::Executed),
},
Instr::Skip => Ok(ExecRes::Skip(
xpop()?.try_into().map_err(|_| ExecError::InvalidSkip)?,
)),
Instr::Arith(it) => {
let b = xpop()?;
let a = xpop()?;
stack.push(match it {
ArithOp::Add => a + b,
ArithOp::Sub => a - b,
ArithOp::Mul => a * b,
ArithOp::Div => a / b,
ArithOp::Rem => a % b,
ArithOp::Lt => {
if a < b {
1
} else {
0
}
}
ArithOp::Pow => builtins::pow(a, b).ok_or(ExecError::InvalidExponent)?,
});
Ok(ExecRes::Executed)
}
Instr::Mem(memop) => {
match memop {
MemOp::Read8 => {
let addr = xpop()?;
let val = (unsafe { *(addr as *const u8) }) as Value;
stack.push(val);
}
MemOp::Write8 => {
let value: u8 = xpop()?
.try_into()
.expect("trying to write8 on a value that doesn't fit in a byte");
let addr = xpop()?;
let ptr = addr as *mut u8;
unsafe {
*ptr = value;
}
}
MemOp::ReadNative => {
let addr = xpop()?;
let val = (unsafe { *(addr as *const Value) }) as Value;
stack.push(val);
}
MemOp::WriteNative => {
let value: Value = xpop()?;
let addr = xpop()?;
let ptr = addr as *mut Value;
unsafe {
*ptr = value;
}
}
MemOp::WidthNative => {
stack.push(Value::BITS.into());
}
};
Ok(ExecRes::Executed)
}
Instr::Syscall => {
let v6 = xpop()?;
let v5 = xpop()?;
let v4 = xpop()?;
let v3 = xpop()?;
let v2 = xpop()?;
let v1 = xpop()?;
let rax = xpop()?;
stack.push(unsafe { builtins::syscall(rax, v1, v2, v3, v4, v5, v6) });
Ok(ExecRes::Executed)
}
Instr::Pick => {
let conv: usize = xpop()?.try_into().map_err(|_| ExecError::InvalidPick)?;
let val = stack.rsp.wrapping_sub(conv);
// TODO: undefined behavior for invalid picks?
stack.push(unsafe { *val });
Ok(ExecRes::Executed)
}
Instr::DropRange => {
let amount: usize = xpop()?
.try_into()
.map_err(|_| ExecError::InvalidDropRange)?;
let start: usize = xpop()?
.try_into()
.map_err(|_| ExecError::InvalidDropRange)?;
let true = amount <= start else {
return Err(ExecError::InvalidDropRange);
};
let drop_start = stack.rsp.wrapping_sub(start);
let drop_end = drop_start.wrapping_add(amount);
debug_assert!(stack.rsp >= drop_end);
let keep_amount = start - amount;
debug_assert_eq!(
unsafe { stack.rsp.offset_from_unsigned(drop_end) },
keep_amount
);
unsafe { std::ptr::copy(drop_end, drop_start, keep_amount) };
stack.rsp = stack.rsp.wrapping_sub(amount);
Ok(ExecRes::Executed)
}
}
}
// we have to split execute_line and this version, due to lifetime problems. When you call clac functions, it will be executing in this context, where the FunctionMap CANNOT be modified, since you cannot define functions within a function.
fn exec_function<'cs>(
funcs: &'cs FuncMap,
stack: &mut Stack,
jit: &JITState,
mut callstack: CallStack<'cs>,
) -> Result<(), ExecError> {
while let Some(line) = callstack.pop() {
// println!("cs = {callstack:?}");
let Some((token, xs)) = line.split_first() else {
continue;
};
let mut optimize_push = |vals: &[Instr]| match vals {
[] => {}
[Instr::Literal(n), Instr::Skip, rest @ ..]
if (*n >= 0 && ((*n as usize) == rest.len())) => {}
_ => {
callstack.push(xs);
}
};
match Self::execute(funcs, stack, jit, token)? {
ExecRes::Executed => {
if !xs.is_empty() {
callstack.push(xs);
}
}
ExecRes::Skip(n) => match xs.split_at_checked(n) {
Some((_, remain)) => {
if !remain.is_empty() {
callstack.push(remain);
}
}
None => return Err(ExecError::InvalidSkip),
},
ExecRes::RecursiveCall(newfunc) => {
// TODO: tailcall optimization
optimize_push(xs);
callstack.push(newfunc);
}
}
}
Ok(())
}
fn flush_queue_and_recompile(&mut self) {
for (name, f) in self.undefined_functions.drain(..) {
match self.funcmap.map.get(&name) {
Some(idx) => {
// replace already defined function
self.funcmap.functions[*idx] = Function::User(None, f);
}
None => {
// create new function
let len = self.funcmap.functions.len();
self.funcmap.functions.push(Function::User(None, f));
self.funcmap.map.insert(name.to_string(), len);
}
};
}
resolve_funcmap(&mut self.funcmap);
assert!(self.undefined_functions.is_empty());
// Reset the JIT
let old = std::mem::replace(&mut self.jit, JITState::new().unwrap());
unsafe { old.module.free_memory() };
self.declare_and_compile_all_functions().unwrap();
}
/// Execute a slice of [`Token`]s representing a line of Clac++ code.
pub fn execute_tokens(&mut self, mut line: &[Token]) -> Result<(), ExecError> {
let mut cur_func: Option<(&String, Code)> = None;
let mut funcs = &mut self.funcmap;
let mut stack = &mut self.stack;
loop {
(line, cur_func) = match (line, cur_func) {
([Token::Colon, Token::Identifier(name), rem @ ..], None) => {
(rem, Some((name, Vec::new())))
}
([Token::Semicolon, rem @ ..], Some((name, f))) => {
self.undefined_functions.push((name.to_string(), f));
// first, resolve function names to indices in FuncMap
(rem, None)
}
([Token::Colon | Token::Semicolon, ..], _) => {
return Err(ExecError::BadFunctionDefinition);
}
([tok, rem @ ..], Some((nm, mut f))) => {
f.push(tok.clone().token_to_instruction(funcs));
(rem, Some((nm, f)))
}
([tok, rem @ ..], None) => {
if let Token::Identifier(_) = tok
&& !self.undefined_functions.is_empty()
{
self.flush_queue_and_recompile();
funcs = &mut self.funcmap;
stack = &mut self.stack;
}
match Self::execute(
funcs,
stack,
&self.jit,
&tok.clone().token_to_instruction(funcs),
)? {
ExecRes::Executed => (rem, None),
ExecRes::Skip(n) => match rem.split_at_checked(n) {
Some((_, rem2)) => (rem2, None),
None => return Err(ExecError::InvalidSkip),
},
ExecRes::RecursiveCall(f) => {
Self::exec_function(funcs, stack, &self.jit, vec![f])?;
(rem, None)
}
}
}
([], Some(_)) => return Err(ExecError::BadFunctionDefinition),
([], None) => return Ok(()),
};
}
}
/// Execute a line of Clac++ code in a string.
pub fn execute_str(&mut self, line: &str) -> Result<(), ExecError> {
let parsed: Vec<Token> = line.split_whitespace().map(parse).collect();
self.execute_tokens(&parsed)
}
}