forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvm.rs
More file actions
561 lines (527 loc) · 22 KB
/
Copy pathvm.rs
File metadata and controls
561 lines (527 loc) · 22 KB
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! Register-based VM with token-threaded dispatch.
use crate::bytecode::*;
use crate::types::Value;
use std::collections::HashMap;
// ---------------------------------------------------------------------------
// VM state
// ---------------------------------------------------------------------------
const STACK_SIZE: usize = 64 * 1024;
const REG_COUNT: usize = 256;
pub struct VM {
// Register file (sliced by call frame)
registers: [Value; REG_COUNT],
// Call stack
frames: Vec<CallFrame>,
// Program counter (index into current module's instructions)
pc: usize,
// Loaded modules
modules: Vec<Module>,
// Function name -> (module_idx, behavior_idx)
function_table: HashMap<String, (usize, usize)>,
// Heap (simple bump allocator for MVP)
heap: Vec<u8>,
heap_ptr: usize,
// String constants
strings: Vec<String>,
// Output buffer (for print operations)
output: Vec<String>,
}
#[derive(Debug, Clone)]
struct CallFrame {
module_idx: usize,
behavior_idx: usize,
pc: usize,
base_reg: usize,
return_reg: u8,
}
#[derive(Debug)]
pub enum VMError {
UnknownFunction(String),
InvalidOpcode(u8),
TypeMismatch { expected: String, got: String },
DivisionByZero,
StackOverflow,
OutOfBounds,
ModuleNotLoaded,
InvalidConstant(u16),
MissingHandler(String),
}
impl std::fmt::Display for VMError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
VMError::UnknownFunction(name) => write!(f, "Unknown function: {}", name),
VMError::InvalidOpcode(op) => write!(f, "Invalid opcode: {}", op),
VMError::TypeMismatch { expected, got } => write!(f, "Type mismatch: expected {}, got {}", expected, got),
VMError::DivisionByZero => write!(f, "Division by zero"),
VMError::StackOverflow => write!(f, "Stack overflow"),
VMError::OutOfBounds => write!(f, "Out of bounds"),
VMError::ModuleNotLoaded => write!(f, "Module not loaded"),
VMError::InvalidConstant(idx) => write!(f, "Invalid constant index: {}", idx),
VMError::MissingHandler(name) => write!(f, "Missing effect handler: {}", name),
}
}
}
impl std::error::Error for VMError {}
// ---------------------------------------------------------------------------
// Dispatch table
// ---------------------------------------------------------------------------
macro_rules! dispatch {
($vm:ident, $inst:ident, $body:block) => {
{
let opcode = OpCode::from_u8($inst.opcode).ok_or(VMError::InvalidOpcode($inst.opcode))?;
match opcode {
OpCode::LoadConst => {
let idx = (($inst.b as u16) << 8) | ($inst.c as u16);
$vm.registers[$inst.a as usize] = $vm.load_constant(idx)?;
}
OpCode::LoadNull => {
$vm.registers[$inst.a as usize] = Value::null();
}
OpCode::Move => {
$vm.registers[$inst.a as usize] = $vm.registers[$inst.b as usize];
}
OpCode::Add => {
let lhs = $vm.registers[$inst.b as usize];
let rhs = $vm.registers[$inst.c as usize];
$vm.registers[$inst.a as usize] = if lhs.is_int() && rhs.is_int() {
Value::int(lhs.as_int().unwrap() + rhs.as_int().unwrap())
} else if let (Some(l), Some(r)) = (lhs.as_float(), rhs.as_float()) {
Value::float(l + r)
} else {
return Err(VMError::TypeMismatch { expected: "number".to_string(), got: "other".to_string() });
};
}
OpCode::Sub => {
let lhs = $vm.registers[$inst.b as usize];
let rhs = $vm.registers[$inst.c as usize];
$vm.registers[$inst.a as usize] = if lhs.is_int() && rhs.is_int() {
Value::int(lhs.as_int().unwrap() - rhs.as_int().unwrap())
} else if let (Some(l), Some(r)) = (lhs.as_float(), rhs.as_float()) {
Value::float(l - r)
} else {
return Err(VMError::TypeMismatch { expected: "number".to_string(), got: "other".to_string() });
};
}
OpCode::Mul => {
let lhs = $vm.registers[$inst.b as usize];
let rhs = $vm.registers[$inst.c as usize];
$vm.registers[$inst.a as usize] = if lhs.is_int() && rhs.is_int() {
Value::int(lhs.as_int().unwrap() * rhs.as_int().unwrap())
} else if let (Some(l), Some(r)) = (lhs.as_float(), rhs.as_float()) {
Value::float(l * r)
} else {
return Err(VMError::TypeMismatch { expected: "number".to_string(), got: "other".to_string() });
};
}
OpCode::Div => {
let lhs = $vm.registers[$inst.b as usize];
let rhs = $vm.registers[$inst.c as usize];
if rhs.is_int() && rhs.as_int().unwrap() == 0 {
return Err(VMError::DivisionByZero);
}
$vm.registers[$inst.a as usize] = if lhs.is_int() && rhs.is_int() {
Value::int(lhs.as_int().unwrap() / rhs.as_int().unwrap())
} else if let (Some(l), Some(r)) = (lhs.as_float(), rhs.as_float()) {
if r == 0.0 {
return Err(VMError::DivisionByZero);
}
Value::float(l / r)
} else {
return Err(VMError::TypeMismatch { expected: "number".to_string(), got: "other".to_string() });
};
}
OpCode::Mod => {
let lhs = $vm.registers[$inst.b as usize];
let rhs = $vm.registers[$inst.c as usize];
$vm.registers[$inst.a as usize] = if lhs.is_int() && rhs.is_int() {
Value::int(lhs.as_int().unwrap() % rhs.as_int().unwrap())
} else {
return Err(VMError::TypeMismatch { expected: "int".to_string(), got: "other".to_string() });
};
}
OpCode::Neg => {
let val = $vm.registers[$inst.b as usize];
$vm.registers[$inst.a as usize] = if val.is_int() {
Value::int(-val.as_int().unwrap())
} else if let Some(f) = val.as_float() {
Value::float(-f)
} else {
return Err(VMError::TypeMismatch { expected: "number".to_string(), got: "other".to_string() });
};
}
OpCode::Eq => {
let lhs = $vm.registers[$inst.b as usize];
let rhs = $vm.registers[$inst.c as usize];
$vm.registers[$inst.a as usize] = Value::bool(lhs == rhs);
}
OpCode::Ne => {
let lhs = $vm.registers[$inst.b as usize];
let rhs = $vm.registers[$inst.c as usize];
$vm.registers[$inst.a as usize] = Value::bool(lhs != rhs);
}
OpCode::Lt => {
let lhs = $vm.registers[$inst.b as usize];
let rhs = $vm.registers[$inst.c as usize];
let result = if let (Some(l), Some(r)) = (lhs.as_int(), rhs.as_int()) {
l < r
} else if let (Some(l), Some(r)) = (lhs.as_float(), rhs.as_float()) {
l < r
} else {
false
};
$vm.registers[$inst.a as usize] = Value::bool(result);
}
OpCode::Le => {
let lhs = $vm.registers[$inst.b as usize];
let rhs = $vm.registers[$inst.c as usize];
let result = if let (Some(l), Some(r)) = (lhs.as_int(), rhs.as_int()) {
l <= r
} else if let (Some(l), Some(r)) = (lhs.as_float(), rhs.as_float()) {
l <= r
} else {
false
};
$vm.registers[$inst.a as usize] = Value::bool(result);
}
OpCode::Gt => {
let lhs = $vm.registers[$inst.b as usize];
let rhs = $vm.registers[$inst.c as usize];
let result = if let (Some(l), Some(r)) = (lhs.as_int(), rhs.as_int()) {
l > r
} else if let (Some(l), Some(r)) = (lhs.as_float(), rhs.as_float()) {
l > r
} else {
false
};
$vm.registers[$inst.a as usize] = Value::bool(result);
}
OpCode::Ge => {
let lhs = $vm.registers[$inst.b as usize];
let rhs = $vm.registers[$inst.c as usize];
let result = if let (Some(l), Some(r)) = (lhs.as_int(), rhs.as_int()) {
l >= r
} else if let (Some(l), Some(r)) = (lhs.as_float(), rhs.as_float()) {
l >= r
} else {
false
};
$vm.registers[$inst.a as usize] = Value::bool(result);
}
OpCode::And => {
let lhs = $vm.registers[$inst.b as usize];
let rhs = $vm.registers[$inst.c as usize];
$vm.registers[$inst.a as usize] = Value::bool(
lhs.as_bool().unwrap_or(false) && rhs.as_bool().unwrap_or(false)
);
}
OpCode::Or => {
let lhs = $vm.registers[$inst.b as usize];
let rhs = $vm.registers[$inst.c as usize];
$vm.registers[$inst.a as usize] = Value::bool(
lhs.as_bool().unwrap_or(false) || rhs.as_bool().unwrap_or(false)
);
}
OpCode::Not => {
let val = $vm.registers[$inst.b as usize];
$vm.registers[$inst.a as usize] = Value::bool(!val.as_bool().unwrap_or(false));
}
OpCode::Jump => {
let offset = (($inst.b as i16) << 8) | ($inst.c as i16);
if offset >= 0 {
$vm.pc += offset as usize;
} else {
$vm.pc = ($vm.pc as i64 + offset as i64) as usize;
}
// The increment at the end of the loop will add 1, so subtract 1 here
$vm.pc = $vm.pc.saturating_sub(1);
}
OpCode::JumpIf => {
let val = $vm.registers[$inst.a as usize];
if val.as_bool().unwrap_or(false) {
let offset = (($inst.b as i16) << 8) | ($inst.c as i16);
if offset >= 0 {
$vm.pc += offset as usize;
} else {
$vm.pc = ($vm.pc as i64 + offset as i64) as usize;
}
$vm.pc = $vm.pc.saturating_sub(1);
}
}
OpCode::JumpIfNot => {
let val = $vm.registers[$inst.a as usize];
if !val.as_bool().unwrap_or(true) {
let offset = (($inst.b as i16) << 8) | ($inst.c as i16);
if offset >= 0 {
$vm.pc += offset as usize;
} else {
$vm.pc = ($vm.pc as i64 + offset as i64) as usize;
}
$vm.pc = $vm.pc.saturating_sub(1);
}
}
OpCode::Call => {
let func_val = $vm.registers[$inst.b as usize];
// Look up function in function table
let func_name = $vm.find_function_name(func_val)?;
let (mod_idx, beh_idx) = $vm.function_table.get(&func_name)
.copied()
.ok_or(VMError::UnknownFunction(func_name.clone()))?;
let entry = $vm.modules[mod_idx].behavior_table[*beh_idx].entry_point as usize;
$vm.frames.push(CallFrame {
module_idx: *mod_idx,
behavior_idx: *beh_idx,
pc: $vm.pc,
base_reg: $inst.a as usize,
return_reg: $inst.a,
});
$vm.pc = entry;
// Continue without incrementing pc
continue;
}
OpCode::Ret => {
let val = $vm.registers[$inst.a as usize];
if let Some(frame) = $vm.frames.pop() {
$vm.pc = frame.pc;
$vm.registers[frame.return_reg as usize] = val;
} else {
// Top-level return
$vm.registers[0] = val;
break;
}
}
OpCode::Halt => {
break;
}
OpCode::NewTuple => {
// For now, just store the first element
if $inst.c > $inst.b {
$vm.registers[$inst.a as usize] = $vm.registers[$inst.b as usize];
} else {
$vm.registers[$inst.a as usize] = Value::null();
}
}
OpCode::FieldGet => {
// Placeholder: just return the object
$vm.registers[$inst.a as usize] = $vm.registers[$inst.b as usize];
}
OpCode::FieldSet => {
// Placeholder
}
OpCode::NewArray => {
// Placeholder
$vm.registers[$inst.a as usize] = Value::null();
}
OpCode::ArrayGet => {
$vm.registers[$inst.a as usize] = Value::null();
}
OpCode::ArraySet => {
// Placeholder
}
OpCode::Cons => {
// Placeholder
$vm.registers[$inst.a as usize] = $vm.registers[$inst.b as usize];
}
OpCode::TestTag => {
// Placeholder
$vm.registers[$inst.a as usize] = Value::bool(true);
}
OpCode::TestTupleLen => {
// Placeholder
$vm.registers[$inst.a as usize] = Value::bool(true);
}
OpCode::Destructure => {
// Placeholder
}
OpCode::Spawn => {
// Placeholder: return a dummy actor ref
$vm.registers[$inst.a as usize] = Value::actor_ref(0, 0, 1);
}
OpCode::Send => {
// Placeholder
}
OpCode::Ask => {
// Placeholder: return null
$vm.registers[$inst.a as usize] = Value::null();
}
OpCode::Receive => {
// Placeholder
$vm.registers[$inst.a as usize] = Value::null();
}
OpCode::SelfAddr => {
$vm.registers[$inst.a as usize] = Value::actor_ref(0, 0, 1);
}
OpCode::Perform => {
// Placeholder
$vm.registers[$inst.a as usize] = Value::null();
}
OpCode::Handle => {
// Placeholder
}
OpCode::PopHandler => {
// Placeholder
}
OpCode::Migrate => {
// Placeholder
$vm.registers[$inst.a as usize] = $vm.registers[$inst.b as usize];
}
OpCode::NewRecord => {
// Placeholder
$vm.registers[$inst.a as usize] = Value::null();
}
OpCode::TailCall => {
// Placeholder
break;
}
}
$vm.pc += 1;
}
};
}
// ---------------------------------------------------------------------------
// VM methods
// ---------------------------------------------------------------------------
impl VM {
pub fn new() -> Self {
VM {
registers: [Value::null(); REG_COUNT],
frames: Vec::with_capacity(1024),
pc: 0,
modules: Vec::new(),
function_table: HashMap::new(),
heap: vec![0; 1024 * 1024], // 1MB heap
heap_ptr: 0,
strings: Vec::new(),
output: Vec::new(),
}
}
pub fn load_module(&mut self, module: &Module) -> Result<(), VMError> {
let mod_idx = self.modules.len();
// Register all functions in the module
for (beh_idx, beh) in module.behavior_table.iter().enumerate() {
self.function_table.insert(
beh.name.clone(),
(mod_idx, beh_idx),
);
}
// Copy constants
for c in &module.constants {
if let Constant::String(s) = c {
self.strings.push(s.clone());
}
}
self.modules.push(module.clone());
Ok(())
}
pub fn call_function(&mut self, name: &str, args: &[Value]) -> Result<Value, VMError> {
let (mod_idx, beh_idx) = self.function_table.get(name)
.copied()
.ok_or_else(|| VMError::UnknownFunction(name.to_string()))?;
let entry = self.modules[mod_idx].behavior_table[beh_idx].entry_point as usize;
// Set up arguments in registers
for (i, &arg) in args.iter().enumerate() {
self.registers[i + 1] = arg;
}
self.pc = entry;
self.run()?;
Ok(self.registers[0])
}
pub fn run(&mut self) -> Result<(), VMError> {
let max_instructions = 10_000_000;
let mut executed = 0;
while self.pc < self.modules.last().map(|m| m.instructions.len()).unwrap_or(0)
&& executed < max_instructions {
let inst = self.modules[self.frames.last().map(|f| f.module_idx).unwrap_or(self.modules.len() - 1)]
.instructions[self.pc];
dispatch!(self, inst, {});
executed += 1;
}
if executed >= max_instructions {
return Err(VMError::StackOverflow);
}
Ok(())
}
fn load_constant(&self, idx: u16) -> Result<Value, VMError> {
let mod_idx = self.frames.last().map(|f| f.module_idx).unwrap_or(0);
let module = &self.modules[mod_idx];
module.constants.get(idx as usize)
.map(|c| match c {
Constant::Int(n) => Value::int(*n),
Constant::Float(f) => Value::float(*f),
Constant::String(s) => Value::heap_ptr(s.as_ptr() as usize),
Constant::Bool(b) => Value::bool(*b),
Constant::Unit => Value::null(),
_ => Value::null(),
})
.ok_or(VMError::InvalidConstant(idx))
}
fn find_function_name(&self, _val: Value) -> Result<String, VMError> {
// In a real implementation, this would extract the function name from a closure value
// For now, search the function table
self.function_table.keys().next()
.cloned()
.ok_or_else(|| VMError::UnknownFunction("unknown".to_string()))
}
pub fn output(&self) -> &[String] {
&self.output
}
}
impl Default for VM {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ast::*;
use crate::compiler::compile;
use crate::parser::parse;
use crate::types::Span;
fn s() -> Span { Span { start: 0, end: 0, line: 1, col: 1 } }
#[test]
fn test_vm_arithmetic() {
let ast = parse("fun main() = 1 + 2 * 3\n").unwrap();
let module = compile(&ast);
let mut vm = VM::new();
vm.load_module(&module).unwrap();
let result = vm.call_function("main", &[]).unwrap();
assert!(result.is_int());
assert_eq!(result.as_int(), Some(7));
}
#[test]
fn test_vm_comparison() {
let ast = parse("fun main() = if 1 < 2 then 42 else 0\n").unwrap();
let module = compile(&ast);
let mut vm = VM::new();
vm.load_module(&module).unwrap();
let result = vm.call_function("main", &[]).unwrap();
assert!(result.is_int());
assert_eq!(result.as_int(), Some(42));
}
#[test]
fn test_vm_function_call() {
let ast = parse("fun add(x, y) = x + y\nfun main() = add(3, 4)\n").unwrap();
let module = compile(&ast);
let mut vm = VM::new();
vm.load_module(&module).unwrap();
let result = vm.call_function("main", &[]).unwrap();
assert!(result.is_int());
assert_eq!(result.as_int(), Some(7));
}
#[test]
fn test_vm_nested_function() {
let input = r#"
fun outer(x) =
let y = x + 1 in
y * 2
fun main() = outer(5)
"#;
let ast = parse(input).unwrap();
let module = compile(&ast);
let mut vm = VM::new();
vm.load_module(&module).unwrap();
let result = vm.call_function("main", &[]).unwrap();
assert!(result.is_int());
assert_eq!(result.as_int(), Some(12));
}
}