🔥 expand stack depth to 65535

This commit is contained in:
ValKmjolnir 2024-05-15 22:59:36 +08:00
parent 290ed122ba
commit 1f43eae4fa
10 changed files with 24 additions and 24 deletions

View File

@ -38,7 +38,7 @@ const char* get_platform();
const char* get_arch(); const char* get_arch();
// virtual machine stack depth, both global depth and value stack depth // virtual machine stack depth, both global depth and value stack depth
const u32 STACK_DEPTH = 4096; const u32 VM_STACK_DEPTH = UINT16_MAX;
f64 hex_to_f64(const char*); f64 hex_to_f64(const char*);
f64 oct_to_f64(const char*); f64 oct_to_f64(const char*);

View File

@ -309,7 +309,7 @@ void codegen::func_gen(function* node) {
// the local scope should not cause stack overflow // the local scope should not cause stack overflow
// and should not greater than upvalue's max size(65536) // and should not greater than upvalue's max size(65536)
code[lsize].num = local.back().size(); code[lsize].num = local.back().size();
if (local.back().size()>=STACK_DEPTH || local.back().size()>=UINT16_MAX) { if (local.back().size()>=VM_STACK_DEPTH || local.back().size()>=UINT16_MAX) {
die("too many local variants: " + die("too many local variants: " +
std::to_string(local.back().size()), std::to_string(local.back().size()),
block->get_location() block->get_location()
@ -1361,7 +1361,7 @@ const error& codegen::compile(parse& parse,
} }
// check global variables size // check global variables size
if (global.size()>=STACK_DEPTH/2) { if (global.size()>=VM_STACK_DEPTH) {
err.err("code", err.err("code",
"too many global variables: " + "too many global variables: " +
std::to_string(global.size()) std::to_string(global.size())

View File

@ -87,7 +87,7 @@ private:
std::list<std::vector<u64>> break_ptr; std::list<std::vector<u64>> break_ptr;
// symbol table // symbol table
// global : max STACK_DEPTH-1 values // global : max VM_STACK_DEPTH-1 values
std::unordered_map<std::string, u64> global; std::unordered_map<std::string, u64> global;
// nasal namespace // nasal namespace
@ -95,7 +95,7 @@ private:
std::unordered_map<std::string, std::unordered_set<std::string>> nasal_namespace; std::unordered_map<std::string, std::unordered_set<std::string>> nasal_namespace;
// local : max 32768 upvalues 65536 values // local : max 32768 upvalues 65536 values
// but in fact local scope also has less than STACK_DEPTH value // but in fact local scope also has less than VM_STACK_DEPTH value
std::list<std::unordered_map<std::string, u64>> local; std::list<std::unordered_map<std::string, u64>> local;
void check_id_exist(identifier*); void check_id_exist(identifier*);

View File

@ -36,7 +36,7 @@ enum class tok:u32 {
rif, // condition expression keyword if rif, // condition expression keyword if
elsif, // condition expression keyword elsif elsif, // condition expression keyword elsif
relse, // condition expression keyword else relse, // condition expression keyword else
tknil, // nil literal nil, // nil literal
lcurve, // ( lcurve, // (
rcurve, // ) rcurve, // )
lbracket, // [ lbracket, // [
@ -114,7 +114,7 @@ private:
{"if" ,tok::rif }, {"if" ,tok::rif },
{"elsif" ,tok::elsif }, {"elsif" ,tok::elsif },
{"else" ,tok::relse }, {"else" ,tok::relse },
{"nil" ,tok::tknil }, {"nil" ,tok::nil },
{"(" ,tok::lcurve }, {"(" ,tok::lcurve },
{")" ,tok::rcurve }, {")" ,tok::rcurve },
{"[" ,tok::lbracket}, {"[" ,tok::lbracket},

View File

@ -256,7 +256,7 @@ vector_expr* parse::vec() {
// array end with tok::null=0 // array end with tok::null=0
const tok panic[] = { const tok panic[] = {
tok::id, tok::str, tok::num, tok::tktrue, tok::id, tok::str, tok::num, tok::tktrue,
tok::tkfalse, tok::opnot, tok::sub, tok::tknil, tok::tkfalse, tok::opnot, tok::sub, tok::nil,
tok::func, tok::var, tok::lcurve, tok::floater, tok::func, tok::var, tok::lcurve, tok::floater,
tok::lbrace, tok::lbracket, tok::null tok::lbrace, tok::lbracket, tok::null
}; };
@ -372,7 +372,7 @@ expr* parse::expression() {
} }
switch(type) { switch(type) {
case tok::use: return use_stmt_gen(); case tok::use: return use_stmt_gen();
case tok::tknil: case tok::nil:
case tok::num: case tok::num:
case tok::str: case tok::str:
case tok::id: case tok::id:
@ -637,9 +637,9 @@ unary_operator* parse::unary() {
expr* parse::scalar() { expr* parse::scalar() {
expr* node = nullptr; expr* node = nullptr;
if (lookahead(tok::tknil)) { if (lookahead(tok::nil)) {
node = nil(); node = nil();
match(tok::tknil); match(tok::nil);
} else if (lookahead(tok::num)) { } else if (lookahead(tok::num)) {
node = num(); node = num();
} else if (lookahead(tok::str)) { } else if (lookahead(tok::str)) {
@ -715,7 +715,7 @@ call_vector* parse::callv() {
// array end with tok::null=0 // array end with tok::null=0
const tok panic[] = { const tok panic[] = {
tok::id, tok::str, tok::num, tok::tktrue, tok::id, tok::str, tok::num, tok::tktrue,
tok::tkfalse, tok::opnot, tok::sub, tok::tknil, tok::tkfalse, tok::opnot, tok::sub, tok::nil,
tok::func, tok::var, tok::lcurve, tok::floater, tok::func, tok::var, tok::lcurve, tok::floater,
tok::lbrace, tok::lbracket, tok::colon, tok::null tok::lbrace, tok::lbracket, tok::colon, tok::null
}; };
@ -745,7 +745,7 @@ call_function* parse::callf() {
// array end with tok::null=0 // array end with tok::null=0
const tok panic[] = { const tok panic[] = {
tok::id, tok::str, tok::num, tok::tktrue, tok::id, tok::str, tok::num, tok::tktrue,
tok::tkfalse, tok::opnot, tok::sub, tok::tknil, tok::tkfalse, tok::opnot, tok::sub, tok::nil,
tok::func, tok::var, tok::lcurve, tok::floater, tok::func, tok::var, tok::lcurve, tok::floater,
tok::lbrace, tok::lbracket, tok::null tok::lbrace, tok::lbracket, tok::null
}; };
@ -844,7 +844,7 @@ tuple_expr* parse::multi_scalar() {
// we will check if value called here can reach a memory space // we will check if value called here can reach a memory space
const tok panic[] = { const tok panic[] = {
tok::id, tok::str, tok::num, tok::tktrue, tok::id, tok::str, tok::num, tok::tktrue,
tok::tkfalse, tok::opnot, tok::sub, tok::tknil, tok::tkfalse, tok::opnot, tok::sub, tok::nil,
tok::func, tok::var, tok::lcurve, tok::floater, tok::func, tok::var, tok::lcurve, tok::floater,
tok::lbrace, tok::lbracket, tok::null tok::lbrace, tok::lbracket, tok::null
}; };
@ -1070,7 +1070,7 @@ return_expr* parse::return_expression() {
auto node = new return_expr(toks[ptr].loc); auto node = new return_expr(toks[ptr].loc);
match(tok::ret); match(tok::ret);
tok type = toks[ptr].type; tok type = toks[ptr].type;
if (type==tok::tknil || type==tok::num || if (type==tok::nil || type==tok::num ||
type==tok::str || type==tok::id || type==tok::str || type==tok::id ||
type==tok::func || type==tok::sub || type==tok::func || type==tok::sub ||
type==tok::opnot || type==tok::lcurve || type==tok::opnot || type==tok::lcurve ||

View File

@ -37,7 +37,7 @@ private:
{tok::rif ,"if" }, {tok::rif ,"if" },
{tok::elsif ,"elsif" }, {tok::elsif ,"elsif" },
{tok::relse ,"else" }, {tok::relse ,"else" },
{tok::tknil ,"nil" }, {tok::nil ,"nil" },
{tok::lcurve ,"(" }, {tok::lcurve ,"(" },
{tok::rcurve ,")" }, {tok::rcurve ,")" },
{tok::lbracket,"[" }, {tok::lbracket,"[" },

View File

@ -171,14 +171,14 @@ void nas_co::clear() {
if (!ctx.stack) { if (!ctx.stack) {
return; return;
} }
for(u32 i = 0; i<STACK_DEPTH; ++i) { for(u32 i = 0; i<VM_STACK_DEPTH; ++i) {
ctx.stack[i] = var::nil(); ctx.stack[i] = var::nil();
} }
ctx.pc = 0; ctx.pc = 0;
ctx.localr = nullptr; ctx.localr = nullptr;
ctx.memr = nullptr; ctx.memr = nullptr;
ctx.canary = ctx.stack+STACK_DEPTH-1; ctx.canary = ctx.stack+VM_STACK_DEPTH-1;
ctx.top = ctx.stack; ctx.top = ctx.stack;
ctx.funcr = var::nil(); ctx.funcr = var::nil();
ctx.upvalr = var::nil(); ctx.upvalr = var::nil();

View File

@ -234,7 +234,7 @@ struct nas_co {
status status; status status;
nas_co() { nas_co() {
ctx.stack = new var[STACK_DEPTH]; ctx.stack = new var[VM_STACK_DEPTH];
clear(); clear();
} }
~nas_co() { ~nas_co() {

View File

@ -49,14 +49,14 @@ void vm::context_and_global_init() {
ctx.funcr = nil; ctx.funcr = nil;
ctx.upvalr = nil; ctx.upvalr = nil;
/* set canary = stack[STACK_DEPTH-1] */ /* set canary = stack[VM_STACK_DEPTH-1] */
ctx.canary = ctx.stack+STACK_DEPTH-1; ctx.canary = ctx.stack+VM_STACK_DEPTH-1;
/* nothing is on stack */ /* nothing is on stack */
ctx.top = ctx.stack - 1; ctx.top = ctx.stack - 1;
/* clear main stack and global */ /* clear main stack and global */
for(u32 i = 0; i<STACK_DEPTH; ++i) { for(u32 i = 0; i<VM_STACK_DEPTH; ++i) {
ctx.stack[i] = nil; ctx.stack[i] = nil;
global[i] = nil; global[i] = nil;
} }

View File

@ -176,8 +176,8 @@ public:
/* constructor of vm instance */ /* constructor of vm instance */
vm() { vm() {
ctx.stack = new var[STACK_DEPTH]; ctx.stack = new var[VM_STACK_DEPTH];
global = new var[STACK_DEPTH]; global = new var[VM_STACK_DEPTH];
} }
~vm() { ~vm() {
delete[] ctx.stack; delete[] ctx.stack;