1
0

ダイレクトモードを実装

This commit is contained in:
otya128
2016-02-22 23:08:54 +09:00
parent 76aea0c4e6
commit 5f0078a8f9
4 changed files with 73 additions and 6 deletions

View File

@@ -27,6 +27,8 @@ class VMSlot
{ {
bool isUsed; bool isUsed;
DataTable globalDataTable; DataTable globalDataTable;
//2MBらしい
//Code code[2 * 1024 * 1024 / Code.sizeof];
Code[] code; Code[] code;
SourceLocation[] location; SourceLocation[] location;
Value[] global; Value[] global;
@@ -34,6 +36,7 @@ class VMSlot
Function[wstring] functions; Function[wstring] functions;
int[wstring] globalLabel; int[wstring] globalLabel;
DebugInfo debugInfo; DebugInfo debugInfo;
Value[wstring] directModeVariable;
} }
class VM class VM
{ {
@@ -57,11 +60,22 @@ class VM
{ {
currentSlot = slots[slot]; currentSlot = slots[slot];
} }
void directSlot(Code[] code, int len, VMVariable[wstring] globalTable, Function[wstring] functions, DataTable gdt/*GNU Debugging Tools*/, void directSlot(int start, Code[] code, int len, VMVariable[wstring] globalTable, Function[wstring] functions, DataTable gdt/*GNU Debugging Tools*/,
int[wstring] globalLabel, DebugInfo dinfo) int[wstring] globalLabel, DebugInfo dinfo)
{ {
this.pc = currentSlot.code.length; this.pc = start;
this.currentSlot.code ~= code; auto c = this.currentSlot;
c.globalTable = globalTable;
c.code = code;
sizediff_t oldlen = c.global.length;
sizediff_t addedvarcount = len - oldlen;
for(sizediff_t i = 0; i < addedvarcount; i++)
c.global ~= Value(ValueType.Void);
foreach(wstring k, VMVariable v ; globalTable)
{
if(v.index >= 0 && v.index >= oldlen)
c.global[v.index] = Value(v.type);
}
} }
void loadSlot(int slot, Code[] code, int len, VMVariable[wstring] globalTable, Function[wstring] functions, DataTable gdt/*GNU Debugging Tools*/, void loadSlot(int slot, Code[] code, int len, VMVariable[wstring] globalTable, Function[wstring] functions, DataTable gdt/*GNU Debugging Tools*/,
int[wstring] globalLabel, DebugInfo dinfo) int[wstring] globalLabel, DebugInfo dinfo)

View File

@@ -690,6 +690,10 @@ class Compiler
} }
void compileDefineFunction(DefineFunction node) void compileDefineFunction(DefineFunction node)
{ {
if(isDirectMode)
{
throw new CantUseFromDirectMode();
}
auto skip = genCodeGoto(); auto skip = genCodeGoto();
Function func = new Function(cast(int)this.code.length, node.name, node.returnExpr, cast(int)node.arguments.length); Function func = new Function(cast(int)this.code.length, node.name, node.returnExpr, cast(int)node.arguments.length);
Scope sc = new Scope(func); Scope sc = new Scope(func);
@@ -1054,8 +1058,12 @@ class Compiler
void compileDirectMode(VM vm) void compileDirectMode(VM vm)
{ {
isDirectMode = true; isDirectMode = true;
global = vm.currentSlot.globalTable;
auto start = vm.currentSlot.code.length;
this.code = vm.currentSlot.code;
globalIndex = vm.currentSlot.global.length;
compileProgram(); compileProgram();
vm.directSlot(code, globalIndex + 1, global, functions, globalScope.data, globalLabel, debugInfo); vm.directSlot(start, code, globalIndex + 1, global, functions, globalScope.data, globalLabel, debugInfo);
} }
VM compile() VM compile()
{ {

View File

@@ -104,4 +104,14 @@ class SubscriptOutOfRange : SmileBasicError
super("Subscript out of range"); super("Subscript out of range");
} }
} }
class CantUseFromDirectMode : SmileBasicError
{
this()
{
this.errnum = 43;
this.errprg = 0;//always 0
this.errline = 0;
super("Can't use from direct mode");
}
}

View File

@@ -1185,8 +1185,43 @@ class PetitComputer
} }
auto prg = input("", true); auto prg = input("", true);
parser = new Parser(prg); parser = new Parser(prg);
auto cc = parser.compiler; try
cc.compileDirectMode(vm); {
auto cc = parser.compiler;
cc.compileDirectMode(vm);
}
catch(SmileBasicError sbe)
{
try
{
printConsole(sbe.getErrorMessage, "\n");
printConsole(sbe.getErrorMessage2, "\n");
//printConsole(sbe.to!string);
writeln(sbe.to!string);
writeln(sbe.getErrorMessage2);
auto loc = vm.currentLocation;
printConsole(loc.line, ":", loc.pos, ":", parser.getLine(loc));
}
catch(Throwable t)
{
writeln(t);
}
continue;
}
catch(Throwable t)
{
try
{
printConsole(t.to!string);
writeln(t);
printConsole(parser.getLine(vm.currentLocation));
}
catch(Throwable t)
{
writeln(t);
}
continue;
}
vm.dump(); vm.dump();
running = true; running = true;
} }