mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-29 17:22:56 +08:00
use more macro inner VM
This commit is contained in:
parent
04d69acc38
commit
b986ea9296
46
src/PikaVM.c
46
src/PikaVM.c
@ -65,21 +65,10 @@ static VMParameters* __pikaVM_runByteCodeFrameWithState(
|
||||
|
||||
/* head declare end */
|
||||
|
||||
static InstructUnit* VMState_getInstructNow(VMState* vm) {
|
||||
return instructArray_getByOffset(&(vm->bytecode_frame->instruct_array),
|
||||
vm->pc);
|
||||
}
|
||||
|
||||
static void VMState_setErrorCode(VMState* vm, uint8_t error_code) {
|
||||
vm->error_code = error_code;
|
||||
}
|
||||
|
||||
static InstructUnit* VMState_getInstructUnitWithOffset(VMState* vm,
|
||||
int32_t offset) {
|
||||
return instructArray_getByOffset(&(vm->bytecode_frame->instruct_array),
|
||||
vm->pc + offset);
|
||||
}
|
||||
|
||||
static enum Instruct VMstate_getInstructWithOffset(VMState* vm,
|
||||
int32_t offset) {
|
||||
return instructUnit_getInstruct(
|
||||
@ -92,12 +81,6 @@ static int VMState_getBlockDeepthNow(VMState* vm) {
|
||||
return instructUnit_getBlockDeepth(ins_unit);
|
||||
}
|
||||
|
||||
static char* VMState_getConstWithInstructUnit(VMState* vm,
|
||||
InstructUnit* ins_unit) {
|
||||
return constPool_getByOffset(&(vm->bytecode_frame->const_pool),
|
||||
instructUnit_getConstPoolIndex(ins_unit));
|
||||
}
|
||||
|
||||
#if !PIKA_NANO_ENABLE
|
||||
static char* VMState_getConstWithOffset(VMState* vm, int32_t offset) {
|
||||
return VMState_getConstWithInstructUnit(
|
||||
@ -123,7 +106,7 @@ static int32_t VMState_getAddrOffsetOfJmpBack(VMState* vm) {
|
||||
uint16_t invoke_deepth = instructUnit_getInvokeDeepth(ins_unit_now);
|
||||
enum Instruct ins = instructUnit_getInstruct(ins_unit_now);
|
||||
char* data = VMState_getConstWithInstructUnit(vm, ins_unit_now);
|
||||
if ((0 == invoke_deepth) && (JEZ == ins) && strEqu(data, "2")) {
|
||||
if ((0 == invoke_deepth) && (JEZ == ins) && data[0] == '2') {
|
||||
loop_deepth = instructUnit_getBlockDeepth(ins_unit_now);
|
||||
break;
|
||||
}
|
||||
@ -138,29 +121,25 @@ static int32_t VMState_getAddrOffsetOfJmpBack(VMState* vm) {
|
||||
char* data = VMState_getConstWithInstructUnit(vm, ins_unit_now);
|
||||
int block_deepth_now = instructUnit_getBlockDeepth(ins_unit_now);
|
||||
if ((block_deepth_now == loop_deepth) && (JMP == ins) &&
|
||||
strEqu(data, "-1")) {
|
||||
data[0] == '-' && data[1] == '1') {
|
||||
return offset;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static size_t VMState_getInstructArraySize(VMState* vm) {
|
||||
return instructArray_getSize(&(vm->bytecode_frame->instruct_array));
|
||||
}
|
||||
|
||||
static int32_t VMState_getAddrOffsetFromJmp(VMState* vm) {
|
||||
int offset = 0;
|
||||
/* run byte Code */
|
||||
InstructUnit* this_ins_unit = VMState_getInstructNow(vm);
|
||||
int thisBlockDeepth = instructUnit_getBlockDeepth(this_ins_unit);
|
||||
int8_t blockNum = 0;
|
||||
|
||||
int pc_max = (int)VMState_getInstructArraySize(vm);
|
||||
if (vm->jmp > 0) {
|
||||
offset = 0;
|
||||
while (1) {
|
||||
offset += instructUnit_getSize();
|
||||
/* reach the end */
|
||||
if (vm->pc + offset >= (int)VMState_getInstructArraySize(vm)) {
|
||||
if (vm->pc + offset >= pc_max) {
|
||||
break;
|
||||
}
|
||||
this_ins_unit = VMState_getInstructUnitWithOffset(vm, offset);
|
||||
@ -793,7 +772,7 @@ static int VMState_loadArgsFromMethodArg(VMState* vm,
|
||||
method_type = arg_getType(method_arg);
|
||||
|
||||
/* get arg_num_dec */
|
||||
if (strEqu("", type_list)) {
|
||||
if (type_list[0] == 0) {
|
||||
arg_num_dec = 0;
|
||||
} else {
|
||||
arg_num_dec = strCountSign(type_list, ',') + 1;
|
||||
@ -1608,13 +1587,12 @@ static Arg* VM_instruction_handler_JEZ(PikaObj* self,
|
||||
VMState* vm,
|
||||
char* data,
|
||||
Arg* arg_ret_reg) {
|
||||
arg_newReg(pika_assertArg_reg, PIKA_ARG_BUFF_SIZE);
|
||||
Arg* pika_assertArg = stack_popArg(&(vm->stack), &pika_assertArg_reg);
|
||||
Arg* pika_assertArg = stack_popArg(&(vm->stack), arg_ret_reg);
|
||||
int pika_assert = 0;
|
||||
if (NULL != pika_assertArg) {
|
||||
pika_assert = arg_getInt(pika_assertArg);
|
||||
arg_deinit(pika_assertArg);
|
||||
}
|
||||
arg_deinit(pika_assertArg);
|
||||
return _VM_JEZ(self, vm, data, arg_ret_reg, pika_assert);
|
||||
}
|
||||
|
||||
@ -1622,13 +1600,12 @@ static Arg* VM_instruction_handler_JNZ(PikaObj* self,
|
||||
VMState* vm,
|
||||
char* data,
|
||||
Arg* arg_ret_reg) {
|
||||
arg_newReg(pika_assertArg_reg, PIKA_ARG_BUFF_SIZE);
|
||||
Arg* pika_assertArg = stack_popArg(&(vm->stack), &pika_assertArg_reg);
|
||||
Arg* pika_assertArg = stack_popArg(&(vm->stack), arg_ret_reg);
|
||||
int pika_assert = 0;
|
||||
if (NULL != pika_assertArg) {
|
||||
pika_assert = arg_getInt(pika_assertArg);
|
||||
arg_deinit(pika_assertArg);
|
||||
}
|
||||
arg_deinit(pika_assertArg);
|
||||
return _VM_JEZ(self, vm, data, arg_ret_reg, !pika_assert);
|
||||
}
|
||||
|
||||
@ -2948,11 +2925,6 @@ VMParameters* pikaVM_runByteCodeFrame(PikaObj* self,
|
||||
0, &run_state);
|
||||
}
|
||||
|
||||
InstructUnit* instructArray_getByOffset(InstructArray* self, int32_t offset) {
|
||||
return (InstructUnit*)((uintptr_t)instructArray_getStart(self) +
|
||||
(uintptr_t)offset);
|
||||
}
|
||||
|
||||
void constPool_printAsArray(ConstPool* self) {
|
||||
uint8_t* const_size_str = (uint8_t*)&(self->size);
|
||||
__platform_printf("0x%02x, ", *(const_size_str));
|
||||
|
30
src/PikaVM.h
30
src/PikaVM.h
@ -161,6 +161,10 @@ void constPool_append(ConstPool* self, char* content);
|
||||
#define constPool_getByOffset(self, offset) \
|
||||
(char*)((uintptr_t)constPool_getStart((self)) + (uintptr_t)(offset))
|
||||
|
||||
#define VMState_getConstWithInstructUnit(__vm, __ins_unit) \
|
||||
(constPool_getByOffset(&((__vm)->bytecode_frame->const_pool), \
|
||||
instructUnit_getConstPoolIndex(__ins_unit)))
|
||||
|
||||
char* constPool_getNow(ConstPool* self);
|
||||
char* constPool_getNext(ConstPool* self);
|
||||
char* constPool_getByIndex(ConstPool* self, uint16_t index);
|
||||
@ -176,13 +180,31 @@ void instructArray_append(InstructArray* ins_array, InstructUnit* ins_unit);
|
||||
void instructUnit_init(InstructUnit* ins_unit);
|
||||
void instructUnit_print(InstructUnit* self);
|
||||
void instructArray_print(InstructArray* self);
|
||||
void byteCodeFrame_print(ByteCodeFrame* self);
|
||||
InstructUnit* instructArray_getByOffset(InstructArray* self, int32_t offset);
|
||||
|
||||
#define instructUnit_getSize(InstructUnit_p_self) ((size_t)sizeof(InstructUnit))
|
||||
#define instructArray_getStart(InsturctArry_p_self) \
|
||||
((InsturctArry_p_self)->content_start)
|
||||
|
||||
#define instructArray_getSize(InsturctArry_p_self) \
|
||||
((size_t)(InsturctArry_p_self)->size)
|
||||
#define instructArray_getStart(InsturctArry_p_self) ((self)->content_start)
|
||||
|
||||
#define VMState_getInstructArraySize(vm) \
|
||||
(instructArray_getSize(&((vm)->bytecode_frame->instruct_array)))
|
||||
|
||||
#define instructArray_getByOffset(__self, __offset) \
|
||||
((InstructUnit*)((uintptr_t)instructArray_getStart((__self)) + \
|
||||
(uintptr_t)(__offset)))
|
||||
|
||||
#define VMState_getInstructUnitWithOffset(vm, offset) \
|
||||
(instructArray_getByOffset(&((vm)->bytecode_frame->instruct_array), \
|
||||
(vm)->pc + (offset)))
|
||||
|
||||
#define VMState_getInstructNow(vm) \
|
||||
(instructArray_getByOffset(&((vm)->bytecode_frame->instruct_array), \
|
||||
(vm)->pc))
|
||||
|
||||
void byteCodeFrame_print(ByteCodeFrame* self);
|
||||
|
||||
#define instructUnit_getSize(InstructUnit_p_self) ((size_t)sizeof(InstructUnit))
|
||||
|
||||
uint16_t constPool_getOffsetByData(ConstPool* self, char* data);
|
||||
void instructArray_printWithConst(InstructArray* self, ConstPool* const_pool);
|
||||
|
Loading…
x
Reference in New Issue
Block a user