2005-05-01 11:48:36 +00:00
|
|
|
#include "Machine.h"
|
|
|
|
#include "Cons.h"
|
|
|
|
#include "Lambda.h"
|
2005-05-06 23:25:06 +00:00
|
|
|
#include "Boolean.h"
|
2005-05-01 11:48:36 +00:00
|
|
|
#include "Nil.h"
|
|
|
|
#include "defs.h"
|
2005-05-08 06:38:01 +00:00
|
|
|
#include "string.h"
|
|
|
|
#include "Error.h"
|
|
|
|
//#include "debug.h"
|
2005-05-01 11:48:36 +00:00
|
|
|
|
|
|
|
string GlobalGetKey (void []ele, void []data)
|
|
|
|
{
|
|
|
|
return [[((Cons) ele) car] printForm];
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalFree (void []ele, void []data)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
@implementation Machine
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
state.program = NIL;
|
|
|
|
state.pc = 0;
|
|
|
|
value = NIL;
|
2005-05-06 23:25:06 +00:00
|
|
|
state.cont = NIL;
|
|
|
|
state.env = NIL;
|
2005-05-01 11:48:36 +00:00
|
|
|
state.literals = NIL;
|
2005-05-08 06:38:01 +00:00
|
|
|
state.proc = NIL;
|
2005-05-01 11:48:36 +00:00
|
|
|
state.stack = [Nil nil];
|
2005-05-08 06:38:01 +00:00
|
|
|
state.lineinfo = NIL;
|
2005-05-01 11:48:36 +00:00
|
|
|
globals = Hash_NewTable(1024, GlobalGetKey, GlobalFree, NIL);
|
2005-05-06 23:25:06 +00:00
|
|
|
all_globals = [Nil nil];
|
2005-05-01 11:48:36 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) addGlobal: (Symbol) sym value: (SchemeObject) val
|
|
|
|
{
|
2005-05-02 02:33:44 +00:00
|
|
|
local Cons c = cons(sym, val);
|
|
|
|
Hash_Add(globals, c);
|
2005-05-06 23:25:06 +00:00
|
|
|
all_globals = cons(c, all_globals);
|
2005-05-01 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) loadCode: (CompiledCode) code
|
|
|
|
{
|
|
|
|
state.program = [code code];
|
|
|
|
state.literals = [code literals];
|
2005-05-08 06:38:01 +00:00
|
|
|
state.lineinfo = [code lineinfo];
|
2005-05-01 11:48:36 +00:00
|
|
|
state.pc = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) environment: (Frame) e
|
|
|
|
{
|
2005-05-06 23:25:06 +00:00
|
|
|
state.env = e;
|
2005-05-01 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) continuation: (Continuation) c
|
|
|
|
{
|
2005-05-06 23:25:06 +00:00
|
|
|
state.cont = c;
|
2005-05-01 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) value: (SchemeObject) v
|
|
|
|
{
|
|
|
|
value = v;
|
|
|
|
}
|
|
|
|
|
2005-05-06 23:25:06 +00:00
|
|
|
- (SchemeObject) value
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2005-05-01 11:48:36 +00:00
|
|
|
- (Continuation) continuation
|
|
|
|
{
|
2005-05-06 23:25:06 +00:00
|
|
|
return state.cont;
|
2005-05-01 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (SchemeObject) stack
|
|
|
|
{
|
|
|
|
return state.stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) stack: (SchemeObject) o
|
|
|
|
{
|
|
|
|
state.stack = o;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (state_t []) state
|
|
|
|
{
|
|
|
|
return &state;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) state: (state_t []) st
|
|
|
|
{
|
|
|
|
state.program = st[0].program;
|
|
|
|
state.pc = st[0].pc;
|
|
|
|
state.literals = st[0].literals;
|
|
|
|
state.stack = st[0].stack;
|
2005-05-06 23:25:06 +00:00
|
|
|
state.cont = st[0].cont;
|
|
|
|
state.env = st[0].env;
|
|
|
|
state.proc = st[0].proc;
|
2005-05-08 06:38:01 +00:00
|
|
|
state.lineinfo = st[0].lineinfo;
|
2005-05-06 23:25:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) procedure: (Procedure) pr
|
|
|
|
{
|
|
|
|
state.proc = pr;
|
2005-05-01 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (SchemeObject) run
|
|
|
|
{
|
|
|
|
local integer opcode;
|
|
|
|
local integer operand;
|
2005-05-08 06:38:01 +00:00
|
|
|
local SchemeObject res;
|
2005-05-01 11:48:36 +00:00
|
|
|
while (1) {
|
2005-05-08 06:38:01 +00:00
|
|
|
if (value && [value isError]) {
|
|
|
|
dprintf("Error: %s[%s]\n", [value description], [value printForm]);
|
|
|
|
return value;
|
|
|
|
}
|
2005-05-01 11:48:36 +00:00
|
|
|
opcode = state.program[state.pc].opcode;
|
|
|
|
operand = state.program[state.pc].operand;
|
|
|
|
state.pc = state.pc + 1;
|
|
|
|
|
|
|
|
|
|
|
|
switch (opcode) {
|
|
|
|
case PUSH:
|
|
|
|
if (value) {
|
|
|
|
dprintf("Push: %s\n", [value printForm]);
|
|
|
|
} else {
|
|
|
|
dprintf("Push: NULL!!!!\n");
|
|
|
|
}
|
|
|
|
state.stack = cons(value, state.stack);
|
|
|
|
break;
|
|
|
|
case POP:
|
|
|
|
value = [state.stack car];
|
|
|
|
if (value) {
|
|
|
|
dprintf("Pop: %s\n", [value printForm]);
|
|
|
|
} else {
|
|
|
|
dprintf("Pop: NULL!!!!\n");
|
|
|
|
}
|
|
|
|
state.stack = [state.stack cdr];
|
|
|
|
break;
|
|
|
|
case MAKECLOSURE:
|
|
|
|
dprintf("Makeclosure\n");
|
2005-05-02 02:33:44 +00:00
|
|
|
value = [Lambda newWithCode: (CompiledCode) value
|
2005-05-06 23:25:06 +00:00
|
|
|
environment: state.env];
|
2005-05-01 11:48:36 +00:00
|
|
|
break;
|
|
|
|
case MAKECONT:
|
|
|
|
dprintf("Makecont\n");
|
2005-05-06 23:25:06 +00:00
|
|
|
state.cont = [Continuation newWithState: &state
|
|
|
|
pc: operand];
|
|
|
|
state.stack = [Nil nil];
|
2005-05-01 11:48:36 +00:00
|
|
|
break;
|
|
|
|
case LOADENV:
|
|
|
|
dprintf("Loadenv\n");
|
2005-05-06 23:25:06 +00:00
|
|
|
value = state.env;
|
2005-05-01 11:48:36 +00:00
|
|
|
break;
|
|
|
|
case LOADLITS:
|
|
|
|
dprintf("Loadlits\n");
|
|
|
|
value = state.literals;
|
|
|
|
break;
|
|
|
|
case MAKEENV:
|
|
|
|
dprintf("Makeenv\n");
|
2005-05-06 23:25:06 +00:00
|
|
|
state.env = [Frame newWithSize: operand link: state.env];
|
2005-05-01 11:48:36 +00:00
|
|
|
break;
|
2005-05-08 03:44:18 +00:00
|
|
|
case POPENV:
|
|
|
|
dprintf("Popenv\n");
|
|
|
|
state.env = [state.env getLink];
|
2005-05-01 11:48:36 +00:00
|
|
|
case GET:
|
|
|
|
value = [value get: operand];
|
|
|
|
dprintf("Get: %i --> %s\n", operand, [value printForm]);
|
|
|
|
break;
|
|
|
|
case SET:
|
|
|
|
[value set: operand to: [state.stack car]];
|
2005-05-06 23:25:06 +00:00
|
|
|
dprintf("Set: %i --> %s\n", operand, [[state.stack car] printForm]);
|
2005-05-01 11:48:36 +00:00
|
|
|
state.stack = [state.stack cdr];
|
|
|
|
break;
|
2005-05-06 23:25:06 +00:00
|
|
|
case SETREST:
|
|
|
|
[value set: operand to: state.stack];
|
|
|
|
dprintf("Setrest: %i --> %s\n", operand, [state.stack printForm]);
|
|
|
|
state.stack = [Nil nil];
|
|
|
|
break;
|
|
|
|
case SETSTACK:
|
|
|
|
dprintf("Setstack: %s\n", [value printForm]);
|
|
|
|
state.stack = value;
|
|
|
|
break;
|
2005-05-01 11:48:36 +00:00
|
|
|
case GETLINK:
|
2005-05-06 23:25:06 +00:00
|
|
|
dprintf("Getlink\n");
|
2005-05-01 11:48:36 +00:00
|
|
|
value = [value getLink];
|
|
|
|
break;
|
|
|
|
case GETGLOBAL:
|
|
|
|
dprintf("Getglobal: %s\n", [value printForm]);
|
2005-05-08 06:38:01 +00:00
|
|
|
res = [((Cons) Hash_Find(globals, [value printForm])) cdr];
|
|
|
|
if (!res) {
|
|
|
|
return [Error type: "binding"
|
|
|
|
message: sprintf("Undefined binding: %s",
|
|
|
|
[value printForm])
|
|
|
|
by: self];
|
|
|
|
}
|
|
|
|
value = res;
|
2005-05-06 23:25:06 +00:00
|
|
|
dprintf(" --> %s\n", [value printForm]);
|
|
|
|
break;
|
|
|
|
case SETGLOBAL:
|
|
|
|
dprintf("Setglobal: %s\n", [value printForm]);
|
|
|
|
[self addGlobal: (Symbol) value value: [state.stack car]];
|
|
|
|
state.stack = [state.stack cdr];
|
2005-05-01 11:48:36 +00:00
|
|
|
break;
|
|
|
|
case CALL:
|
|
|
|
dprintf("Call\n");
|
2005-05-06 23:25:06 +00:00
|
|
|
[SchemeObject collectCheckPoint];
|
2005-05-08 06:38:01 +00:00
|
|
|
if (![value isKindOfClass: [Procedure class]]) {
|
|
|
|
return [Error type: "call"
|
|
|
|
message:
|
|
|
|
sprintf("Attempted to apply non-procedure: %s. Arguments were: %s",
|
|
|
|
[value printForm], [state.stack printForm])
|
|
|
|
by: self];
|
|
|
|
}
|
2005-05-01 11:48:36 +00:00
|
|
|
[value invokeOnMachine: self];
|
|
|
|
break;
|
|
|
|
case RETURN:
|
|
|
|
dprintf("Return: %s\n", [value printForm]);
|
2005-05-06 23:25:06 +00:00
|
|
|
if (!state.cont) {
|
2005-05-01 11:48:36 +00:00
|
|
|
return value;
|
|
|
|
} else {
|
2005-05-08 03:44:18 +00:00
|
|
|
[state.cont restoreOnMachine: self];
|
2005-05-01 11:48:36 +00:00
|
|
|
}
|
|
|
|
break;
|
2005-05-06 23:25:06 +00:00
|
|
|
case IFFALSE:
|
|
|
|
dprintf("Iffalse: %s\n", [value printForm]);
|
|
|
|
if (value == [Boolean falseConstant]) {
|
|
|
|
state.pc = operand;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GOTO:
|
|
|
|
dprintf("Goto: %i\n", operand);
|
|
|
|
state.pc = operand;
|
|
|
|
break;
|
2005-05-01 11:48:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-05-02 02:33:44 +00:00
|
|
|
|
2005-05-08 06:38:01 +00:00
|
|
|
- (string) source
|
|
|
|
{
|
|
|
|
if (state.lineinfo) {
|
|
|
|
return [state.lineinfo[state.pc-1].sourcefile stringValue];
|
|
|
|
} else {
|
|
|
|
return [super source];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (integer) line
|
|
|
|
{
|
|
|
|
if (state.lineinfo) {
|
|
|
|
return state.lineinfo[state.pc-1].linenumber;
|
|
|
|
} else {
|
|
|
|
return [super line];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-02 02:33:44 +00:00
|
|
|
- (void) markReachable
|
|
|
|
{
|
|
|
|
[state.literals mark];
|
|
|
|
[state.stack mark];
|
2005-05-06 23:25:06 +00:00
|
|
|
[state.cont mark];
|
|
|
|
[state.env mark];
|
|
|
|
[state.proc mark];
|
2005-05-02 02:33:44 +00:00
|
|
|
[value mark];
|
2005-05-06 23:25:06 +00:00
|
|
|
[all_globals mark];
|
2005-05-02 02:33:44 +00:00
|
|
|
}
|
2005-05-08 06:38:01 +00:00
|
|
|
|
|
|
|
- (void) reset
|
|
|
|
{
|
|
|
|
state.stack = [Nil nil];
|
|
|
|
}
|
2005-05-01 11:48:36 +00:00
|
|
|
@end
|
2005-05-06 23:25:06 +00:00
|
|
|
|