use static stack instead of link stack

This commit is contained in:
lyon1998 2022-03-30 00:27:04 +08:00
parent 60514571b6
commit 26942923bf
11 changed files with 185 additions and 110 deletions

View File

@ -18,10 +18,11 @@
"program": "${workspaceFolder}/build/test/pikascript_test",
// "program": "${workspaceFolder}/../build/src/boot/demo06-pikamain/pikascript_demo06-pikamain",
"args": [
// "--gtest_filter=VM.WHILE",
// "--gtest_filter=VM*",
// "--gtest_filter=parser.while_true_false",
// "--gtest_filter=VM.for_break_byte",
// "--gtest_filter=pikaMain.range",
// "--gtest_filter=parser.class_def_void_line",
// "--gtest_filter=parser.while_true_block",
// "--gtest_filter=VM.Run_add_1_2_3",
// "--gtest_filter=stack*",
// "--gtest_filter=pikaMain.task_run_once",

View File

@ -210,7 +210,8 @@ static char* parse(const char* line,
TEST(parser, while_true_block) {
Args* bf = New_strBuff();
Stack* bs = New_Stack();
Stack bs;
stack_init(&bs);
char* s = strsCopy(bf, (char*)"");
s = parse("while true:", bf, s, bs);
s = parse(" rgb.flow()", bf, s, bs);
@ -232,7 +233,8 @@ TEST(parser, while_true_block) {
TEST(parser, while_true_false) {
Args* bf = New_strBuff();
Stack* bs = New_Stack();
Stack bs;
stack_init(&bs);
char* s = strsCopy(bf, (char*)"");
s = parse("while true:", bf, s, bs);
s = parse(" rgb.flow()", bf, s, bs);
@ -277,7 +279,8 @@ TEST(parser, while_true_false) {
TEST(parser, while_true_false_both_exit) {
Args* bf = New_strBuff();
Stack* bs = New_Stack();
Stack bs;
stack_init(&bs);
char* s = strsCopy(bf, (char*)"");
s = parse("while true:", bf, s, bs);
s = parse(" rgb.flow()", bf, s, bs);
@ -315,7 +318,8 @@ TEST(parser, while_true_false_both_exit) {
TEST(parser, if_) {
Args* bf = New_strBuff();
Stack* bs = New_Stack();
Stack bs;
stack_init(&bs);
char* s = strsCopy(bf, (char*)"");
s = parse("if true:", bf, s, bs);
s = parse(" rgb.flow()", bf, s, bs);
@ -337,7 +341,8 @@ extern PikaMemInfo pikaMemInfo;
TEST(parser, while_true_if_false_both_exit) {
pikaMemInfo.heapUsedMax = 0;
Args* bf = New_strBuff();
Stack* bs = New_Stack();
Stack bs;
stack_init(&bs);
char* s = strsCopy(bf, (char*)"");
s = parse("while true:", bf, s, bs);
s = parse(" rgb.flow()", bf, s, bs);

View File

@ -240,11 +240,12 @@ static char* parse(const char* line,
TEST(parser, while_true_block) {
Args* bf = New_strBuff();
Stack* bs = New_Stack();
Stack bs;
stack_init(&bs);
char* s = strsCopy(bf, (char*)"");
s = parse("while true:", bf, s, bs);
s = parse(" rgb.flow()", bf, s, bs);
s = parse("", bf, s, bs);
s = parse("while true:", bf, s, &bs);
s = parse(" rgb.flow()", bf, s, &bs);
s = parse("", bf, s, &bs);
printf("%s", s);
EXPECT_STREQ(s,
"B0\n"
@ -255,24 +256,25 @@ TEST(parser, while_true_block) {
"B0\n"
"0 JMP -1\n"
"B0\n");
stack_deinit(bs);
stack_deinit(&bs);
args_deinit(bf);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, while_true_false) {
Args* bf = New_strBuff();
Stack* bs = New_Stack();
Stack bs;
stack_init(&bs);
char* s = strsCopy(bf, (char*)"");
s = parse("while true:", bf, s, bs);
s = parse(" rgb.flow()", bf, s, bs);
s = parse(" while false:", bf, s, bs);
s = parse(" a=3", bf, s, bs);
s = parse(" test.on(add(2,3))", bf, s, bs);
s = parse(" print('flowing')", bf, s, bs);
s = parse("", bf, s, bs);
s = parse("while true:", bf, s, &bs);
s = parse(" rgb.flow()", bf, s, &bs);
s = parse(" while false:", bf, s, &bs);
s = parse(" a=3", bf, s, &bs);
s = parse(" test.on(add(2,3))", bf, s, &bs);
s = parse(" print('flowing')", bf, s, &bs);
s = parse("", bf, s, &bs);
Arg* buffArg = arg_setStr(NULL, (char*)"", s);
stack_deinit(bs);
stack_deinit(&bs);
args_deinit(bf);
s = arg_getStr(buffArg);
printf("%s", s);
@ -307,14 +309,15 @@ TEST(parser, while_true_false) {
TEST(parser, while_true_false_both_exit) {
Args* bf = New_strBuff();
Stack* bs = New_Stack();
Stack bs;
stack_init(&bs);
char* s = strsCopy(bf, (char*)"");
s = parse("while true:", bf, s, bs);
s = parse(" rgb.flow()", bf, s, bs);
s = parse(" while false:", bf, s, bs);
s = parse(" a=3", bf, s, bs);
s = parse(" test.on(add(2,3))", bf, s, bs);
s = parse("", bf, s, bs);
s = parse("while true:", bf, s, &bs);
s = parse(" rgb.flow()", bf, s, &bs);
s = parse(" while false:", bf, s, &bs);
s = parse(" a=3", bf, s, &bs);
s = parse(" test.on(add(2,3))", bf, s, &bs);
s = parse("", bf, s, &bs);
printf("%s", s);
EXPECT_STREQ(s,
"B0\n"
@ -338,18 +341,19 @@ TEST(parser, while_true_false_both_exit) {
"B0\n"
"0 JMP -1\n"
"B0\n");
stack_deinit(bs);
stack_deinit(&bs);
args_deinit(bf);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, if_) {
Args* bf = New_strBuff();
Stack* bs = New_Stack();
Stack bs;
stack_init(&bs);
char* s = strsCopy(bf, (char*)"");
s = parse("if true:", bf, s, bs);
s = parse(" rgb.flow()", bf, s, bs);
s = parse("", bf, s, bs);
s = parse("if true:", bf, s, &bs);
s = parse(" rgb.flow()", bf, s, &bs);
s = parse("", bf, s, &bs);
printf("%s", s);
EXPECT_STREQ(s,
"B0\n"
@ -358,7 +362,7 @@ TEST(parser, if_) {
"B1\n"
"0 RUN rgb.flow\n"
"B0\n");
stack_deinit(bs);
stack_deinit(&bs);
args_deinit(bf);
EXPECT_EQ(pikaMemNow(), 0);
}
@ -367,14 +371,15 @@ extern PikaMemInfo pikaMemInfo;
TEST(parser, while_true_if_false_both_exit) {
pikaMemInfo.heapUsedMax = 0;
Args* bf = New_strBuff();
Stack* bs = New_Stack();
Stack bs;
stack_init(&bs);
char* s = strsCopy(bf, (char*)"");
s = parse("while true:", bf, s, bs);
s = parse(" rgb.flow()", bf, s, bs);
s = parse(" if false:", bf, s, bs);
s = parse(" a=3", bf, s, bs);
s = parse(" test.on(add(2,3))", bf, s, bs);
s = parse("", bf, s, bs);
s = parse("while true:", bf, s, &bs);
s = parse(" rgb.flow()", bf, s, &bs);
s = parse(" if false:", bf, s, &bs);
s = parse(" a=3", bf, s, &bs);
s = parse(" test.on(add(2,3))", bf, s, &bs);
s = parse("", bf, s, &bs);
printf("%s", s);
EXPECT_STREQ(s,
"B0\n"
@ -396,7 +401,7 @@ TEST(parser, while_true_if_false_both_exit) {
"B0\n"
"0 JMP -1\n"
"B0\n");
stack_deinit(bs);
stack_deinit(&bs);
args_deinit(bf);
EXPECT_EQ(pikaMemNow(), 0);
}
@ -1480,11 +1485,12 @@ TEST(parser, for_range_rtt) {
TEST(parser, for_list) {
Args* bf = New_strBuff();
Stack* bs = New_Stack();
Stack bs;
stack_init(&bs);
char* s = strsCopy(bf, (char*)"");
s = parse("for arg in xrange(0,10):", bf, s, bs);
s = parse(" print(arg)", bf, s, bs);
s = parse("", bf, s, bs);
s = parse("for arg in xrange(0,10):", bf, s, &bs);
s = parse(" print(arg)", bf, s, &bs);
s = parse("", bf, s, &bs);
printf("%s", s);
EXPECT_STREQ(s,
"B0\n"
@ -1506,7 +1512,7 @@ TEST(parser, for_list) {
"B0\n"
"0 DEL _l0\n"
"B0\n");
stack_deinit(bs);
stack_deinit(&bs);
args_deinit(bf);
EXPECT_EQ(pikaMemNow(), 0);
}
@ -2553,4 +2559,3 @@ TEST(parser, multiLine_import) {
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}

View File

@ -6,20 +6,61 @@ extern "C" {
}
TEST(stack, NEW) {
Stack* s = New_Stack();
stack_deinit(s);
Stack s;
stack_init(&s);
stack_deinit(&s);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(stack, size) {
Stack s;
stack_init(&s);
stack_pushSize(&s, 10);
stack_pushSize(&s, 20);
stack_pushSize(&s, 30);
EXPECT_EQ(stack_popSize(&s), 30);
EXPECT_EQ(stack_popSize(&s), 20);
EXPECT_EQ(stack_popSize(&s), 10);
stack_deinit(&s);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(stack, arg) {
Stack s;
stack_init(&s);
Arg* arg1 = arg_setInt(NULL, (char*)"", 10);
Arg* arg2 = arg_setInt(NULL, (char*)"", 20);
Arg* arg3 = arg_setInt(NULL, (char*)"", 30);
stack_pushArg(&s, arg1);
stack_pushArg(&s, arg2);
stack_pushArg(&s, arg3);
Arg* arg4 = stack_popArg(&s);
Arg* arg5 = stack_popArg(&s);
Arg* arg6 = stack_popArg(&s);
EXPECT_EQ(arg_getInt(arg4), 30);
EXPECT_EQ(arg_getInt(arg5), 20);
EXPECT_EQ(arg_getInt(arg6), 10);
stack_deinit(&s);
arg_deinit(arg4);
arg_deinit(arg5);
arg_deinit(arg6);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(stack, str) {
Stack* s = New_Stack();
stack_pushStr(s, (char*)"abc");
stack_pushStr(s, (char*)"123");
stack_pushStr(s, (char*)"xyz");
Stack s;
stack_init(&s);
stack_pushStr(&s, (char*)"abc");
stack_pushStr(&s, (char*)"123");
stack_pushStr(&s, (char*)"xyz");
char buff[32] = {0};
EXPECT_STREQ(stack_popStr(s, buff), (char*)"xyz");
EXPECT_STREQ(stack_popStr(s, buff), (char*)"123");
EXPECT_STREQ(stack_popStr(s, buff), (char*)"abc");
stack_deinit(s);
EXPECT_STREQ(stack_popStr(&s, buff), (char*)"xyz");
EXPECT_STREQ(stack_popStr(&s, buff), (char*)"123");
EXPECT_STREQ(stack_popStr(&s, buff), (char*)"abc");
stack_deinit(&s);
EXPECT_EQ(pikaMemNow(), 0);
}

View File

@ -1182,7 +1182,8 @@ static int Parser_isVoidLine(char* line) {
static char* Parser_parsePyLines(Args* outBuffs,
ByteCodeFrame* bytecode_frame,
char* py_lines) {
Stack* block_stack = New_Stack();
Stack block_stack;
stack_init(&block_stack);
Arg* asm_buff = arg_setStr(NULL, "", "");
uint32_t lines_offset = 0;
uint32_t lines_size = strGetSize(py_lines);
@ -1203,7 +1204,7 @@ static char* Parser_parsePyLines(Args* outBuffs,
}
}
/* parse single Line to Asm */
char* single_ASM = Parser_LineToAsm(&buffs, line, block_stack);
char* single_ASM = Parser_LineToAsm(&buffs, line, &block_stack);
if (NULL == single_ASM) {
out_ASM = NULL;
strsDeinit(&buffs);
@ -1236,7 +1237,7 @@ exit:
if (NULL != asm_buff) {
arg_deinit(asm_buff);
}
stack_deinit(block_stack);
stack_deinit(&block_stack);
return out_ASM;
};

View File

@ -194,7 +194,7 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self, VMState* vs, char* data) {
ByteCodeFrame* method_bytecodeFrame;
/* return arg directly */
if (strEqu(data, "")) {
return_arg = stack_popArg(vs->stack);
return_arg = stack_popArg(&(vs->stack));
goto RUN_exit;
}
@ -249,7 +249,7 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self, VMState* vs, char* data) {
char* argDef = strPopLastToken(type_list, ',');
strPopLastToken(argDef, ':');
char* argName = argDef;
call_arg = stack_popArg(vs->stack);
call_arg = stack_popArg(&(vs->stack));
call_arg = arg_setName(call_arg, argName);
args_setArg(sub_locals->list, call_arg);
call_arg_index++;
@ -319,7 +319,7 @@ static Arg* __VM_OUT(PikaObj* self,
VMState* vs,
char* data,
is_init_obj_t is_init_obj) {
Arg* outArg = stack_popArg(vs->stack);
Arg* outArg = stack_popArg(&(vs->stack));
ArgType outArg_type = arg_getType(outArg);
PikaObj* hostObj = vs->locals;
/* match global_list */
@ -416,7 +416,7 @@ static Arg* VM_instruction_handler_JMP(PikaObj* self, VMState* vs, char* data) {
static Arg* VM_instruction_handler_JEZ(PikaObj* self, VMState* vs, char* data) {
int thisBlockDeepth;
thisBlockDeepth = VMState_getBlockDeepthNow(vs);
Arg* assertArg = stack_popArg(vs->stack);
Arg* assertArg = stack_popArg(&(vs->stack));
int assert = arg_getInt(assertArg);
arg_deinit(assertArg);
char __else[] = "__else0";
@ -431,8 +431,8 @@ static Arg* VM_instruction_handler_JEZ(PikaObj* self, VMState* vs, char* data) {
static Arg* VM_instruction_handler_OPT(PikaObj* self, VMState* vs, char* data) {
Arg* outArg = NULL;
Arg* arg2 = stack_popArg(vs->stack);
Arg* arg1 = stack_popArg(vs->stack);
Arg* arg2 = stack_popArg(&(vs->stack));
Arg* arg1 = stack_popArg(&(vs->stack));
ArgType type_arg1 = arg_getType(arg1);
ArgType type_arg2 = arg_getType(arg2);
int num1_i = 0;
@ -647,7 +647,7 @@ static Arg* VM_instruction_handler_DEF(PikaObj* self, VMState* vs, char* data) {
static Arg* VM_instruction_handler_RET(PikaObj* self, VMState* vs, char* data) {
/* exit jmp signal */
vs->jmp = -999;
Arg* returnArg = stack_popArg(vs->stack);
Arg* returnArg = stack_popArg(&(vs->stack));
method_returnArg(vs->locals->list, returnArg);
return NULL;
}
@ -734,7 +734,7 @@ static int pikaVM_runInstructUnit(PikaObj* self,
/* run instruct */
resArg = VM_instruct_handler_table[instruct](self, vs, data);
if (NULL != resArg) {
stack_pushArg(vs->stack, resArg);
stack_pushArg(&(vs->stack), resArg);
}
goto nextLine;
nextLine:
@ -1129,27 +1129,17 @@ VMParameters* pikaVM_runByteCodeWithState(PikaObj* self,
.locals = locals,
.globals = globals,
.jmp = 0,
.stack = New_Stack(),
.pc = pc,
.error_code = 0,
};
stack_init(&(vs.stack));
while (vs.pc < size) {
if (vs.pc == -99999) {
break;
}
InstructUnit* this_ins_unit = VMState_getInstructNow(&vs);
if (instructUnit_getIsNewLine(this_ins_unit)) {
for (int8_t i = 0; i < stack_getTop(vs.stack); i++) {
// Args print_args = {0};
// Arg* print_arg = stack_popArg(vs.stack);
// arg_setName(print_arg, "val");
// args_setArg(&print_args, print_arg);
// if (ARG_TYPE_NONE != arg_getType(print_arg)) {
// baseobj_print(self, &print_args);
// }
// args_deinit_stack(&print_args);
arg_deinit(stack_popArg(vs.stack));
}
stack_reset(&(vs.stack));
}
vs.pc = pikaVM_runInstructUnit(self, &vs, this_ins_unit);
if (0 != vs.error_code) {
@ -1178,7 +1168,7 @@ VMParameters* pikaVM_runByteCodeWithState(PikaObj* self,
__platform_error_handle();
}
}
stack_deinit(vs.stack);
stack_deinit(&(vs.stack));
return locals;
}

View File

@ -41,7 +41,7 @@ enum Instruct {
typedef struct VMState_t {
VMParameters* locals;
VMParameters* globals;
Stack *stack;
Stack stack;
int32_t jmp;
int32_t pc;
ByteCodeFrame* bytecode_frame;

View File

@ -235,7 +235,6 @@ int64_t arg_getInt(Arg* self) {
if (NULL == arg_getContent(self)) {
return -999999;
}
return *(int64_t*)(((__arg*)self)->content);
}

View File

@ -28,27 +28,54 @@
#include "dataStack.h"
#include "dataQueue.h"
Stack* New_Stack(void) {
Args* args = New_args(NULL);
args_setInt(args, "_", 0);
Stack* stack = args;
return stack;
void stack_reset(Stack* stack) {
stack->sp = (uint8_t*)arg_getContent(stack->stack_pyload);
stack->sp_size = (int16_t*)arg_getContent(stack->stack_size_array);
stack->top = 0;
}
int32_t stack_init(Stack* stack) {
stack->stack_pyload =
arg_setContent(NULL, NULL, PIKA_CONFIG_STACK_BUFF_SIZE);
stack->stack_size_array =
arg_setContent(NULL, NULL, PIKA_CONFIG_STACK_BUFF_SIZE / 4);
stack_reset(stack);
return 0;
};
void stack_pushSize(Stack* stack, int16_t size) {
*(stack->sp_size) = size;
stack->sp_size++;
}
int16_t stack_popSize(Stack* stack) {
stack->sp_size--;
return *(stack->sp_size);
}
int32_t stack_deinit(Stack* stack) {
Args* args = stack;
args_deinit(args);
arg_deinit(stack->stack_pyload);
arg_deinit(stack->stack_size_array);
return 0;
}
void stack_pushPyload(Stack* stack, uint8_t* content, size_t size) {
__platform_memcpy(stack->sp, content, size);
stack->sp += size;
}
uint8_t* stack_popPyload(Stack* stack, size_t size) {
stack->sp -= size;
return stack->sp;
}
int32_t stack_pushArg(Stack* stack, Arg* arg) {
Args* args = stack;
uint64_t top = args_getInt(args, "_");
/* add top */
args_setInt(args, "_", top + 1);
char buff[11];
arg = arg_setName(arg, fast_itoa(buff, top));
return args_setArg(args, arg);
stack->top++;
int8_t size = arg_getTotleSize(arg);
stack_pushSize(stack, size);
stack_pushPyload(stack, arg, size);
arg_deinit(arg);
return 0;
}
int32_t stack_pushStr(Stack* stack, char* str) {
@ -57,17 +84,12 @@ int32_t stack_pushStr(Stack* stack, char* str) {
}
Arg* stack_popArg(Stack* stack) {
Args* args = stack;
int64_t top = args_getInt(args, "_") - 1;
if (top < 0) {
if (stack->top == 0) {
return NULL;
}
char buff[11];
Arg* topArg = args_getArg(args, fast_itoa(buff, top));
Arg* res = arg_copy(topArg);
/* dec top */
args_setInt(args, "_", top);
args_removeArg(args, topArg);
stack->top--;
int16_t size = stack_popSize(stack);
Arg* res = arg_copy((Arg*)stack_popPyload(stack, size));
return res;
}
@ -79,5 +101,5 @@ char* stack_popStr(Stack* stack, char* outBuff) {
}
int8_t stack_getTop(Stack* stack) {
return args_getInt(stack, "_");
return stack->top;
}

View File

@ -29,8 +29,14 @@
#define __DATA_STACK__H
#include "dataArgs.h"
typedef Args Stack;
Stack* New_Stack(void);
typedef struct Stack_t {
Arg* stack_pyload;
Arg* stack_size_array;
uint8_t* sp;
int16_t* sp_size;
int16_t top;
} Stack;
int32_t stack_deinit(Stack* stack);
int32_t stack_pushStr(Stack* stack, char* str);
@ -38,4 +44,8 @@ char* stack_popStr(Stack* stack, char* outBuff);
Arg* stack_popArg(Stack* stack);
int32_t stack_pushArg(Stack* stack, Arg* arg);
int8_t stack_getTop(Stack* stack);
int32_t stack_init(Stack* stack);
int16_t stack_popSize(Stack* stack);
void stack_pushSize(Stack* stack, int16_t size);
void stack_reset(Stack* stack);
#endif

View File

@ -30,6 +30,7 @@
/* default configuration */
#define PIKA_CONFIG_LINE_BUFF_SIZE 128
#define PIKA_CONFIG_SPRINTF_BUFF_SIZE 128
#define PIKA_CONFIG_STACK_BUFF_SIZE 256
#define PIKA_CONFIG_NAME_BUFF_SIZE 32
#define PIKA_CONFIG_PATH_BUFF_SIZE 64
#undef PIKA_CONFIG_METHOD_CACHE_ENABLE