ready to intruce mark-sweep GC

This commit is contained in:
lyon 2023-03-01 21:23:33 +08:00
parent ae2ec0a7cd
commit cc6fd6605b
10 changed files with 80 additions and 63 deletions

View File

@ -288,7 +288,7 @@ int PikaStdLib_SysObj_len(PikaObj* self, Arg* arg) {
return arg_getBytesSize(arg); return arg_getBytesSize(arg);
} }
if (argType_isObject(arg_getType(arg))) { if (arg_isObject(arg)) {
PikaObj* arg_obj = arg_getPtr(arg); PikaObj* arg_obj = arg_getPtr(arg);
Arg* method_arg = obj_getMethodArg(arg_obj, "__len__"); Arg* method_arg = obj_getMethodArg(arg_obj, "__len__");
if (NULL != method_arg) { if (NULL != method_arg) {
@ -503,7 +503,7 @@ char* PikaStdLib_SysObj_cformat(PikaObj* self, char* fmt, PikaTuple* var) {
int PikaStdLib_SysObj_id(PikaObj* self, Arg* obj) { int PikaStdLib_SysObj_id(PikaObj* self, Arg* obj) {
uintptr_t ptr = 0; uintptr_t ptr = 0;
if (argType_isObject(arg_getType(obj))) { if (arg_isObject(obj)) {
ptr = (uintptr_t)arg_getPtr(obj); ptr = (uintptr_t)arg_getPtr(obj);
} else { } else {
ptr = (uintptr_t)obj; ptr = (uintptr_t)obj;
@ -543,7 +543,7 @@ int32_t __dir_each(Arg* argEach, Args* context) {
} }
PikaObj* PikaStdLib_SysObj_dir(PikaObj* self, Arg* arg) { PikaObj* PikaStdLib_SysObj_dir(PikaObj* self, Arg* arg) {
if (!argType_isObject(arg_getType(arg))) { if (!arg_isObject(arg)) {
obj_setErrorCode(self, 1); obj_setErrorCode(self, 1);
__platform_printf("[Error] dir: not support type.\r\n"); __platform_printf("[Error] dir: not support type.\r\n");
return NULL; return NULL;

View File

@ -1,9 +1,10 @@
#include "weakref.h" #include "weakref.h"
Arg* weakref_ref(PikaObj* self, Arg* obj) { Arg* weakref_ref(PikaObj* self, Arg* aObj) {
if (argType_isObject(arg_getType(obj))) { if (arg_isObject(aObj)) {
Arg* ret = arg_copy(obj); PikaObj* obj = arg_getPtr(aObj);
obj_refcntDec((PikaObj*)arg_getPtr(ret)); Arg* ret = arg_copy(aObj);
obj_refcntDec(obj);
arg_setIsWeakRef(ret, PIKA_TRUE); arg_setIsWeakRef(ret, PIKA_TRUE);
return ret; return ret;
} }

View File

@ -11,7 +11,7 @@
"program": "${workspaceFolder}/build/test/pikascript_test", "program": "${workspaceFolder}/build/test/pikascript_test",
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain", // "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
"args": [ "args": [
"--gtest_filter=vm.getattr_native" // "--gtest_filter=vm.getattr_native"
], ],
"stopAtEntry": false, "stopAtEntry": false,
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",

View File

@ -109,7 +109,10 @@
"_network_wlan.h": "c", "_network_wlan.h": "c",
"pikastddata_tuple.h": "c", "pikastddata_tuple.h": "c",
"setjmp.h": "c", "setjmp.h": "c",
"pthread.h": "c" "pthread.h": "c",
"deque": "c",
"list": "c",
"vector": "c"
}, },
"python.formatting.provider": "autopep8", "python.formatting.provider": "autopep8",
} }

View File

@ -288,7 +288,7 @@ int PikaStdLib_SysObj_len(PikaObj* self, Arg* arg) {
return arg_getBytesSize(arg); return arg_getBytesSize(arg);
} }
if (argType_isObject(arg_getType(arg))) { if (arg_isObject(arg)) {
PikaObj* arg_obj = arg_getPtr(arg); PikaObj* arg_obj = arg_getPtr(arg);
Arg* method_arg = obj_getMethodArg(arg_obj, "__len__"); Arg* method_arg = obj_getMethodArg(arg_obj, "__len__");
if (NULL != method_arg) { if (NULL != method_arg) {
@ -503,7 +503,7 @@ char* PikaStdLib_SysObj_cformat(PikaObj* self, char* fmt, PikaTuple* var) {
int PikaStdLib_SysObj_id(PikaObj* self, Arg* obj) { int PikaStdLib_SysObj_id(PikaObj* self, Arg* obj) {
uintptr_t ptr = 0; uintptr_t ptr = 0;
if (argType_isObject(arg_getType(obj))) { if (arg_isObject(obj)) {
ptr = (uintptr_t)arg_getPtr(obj); ptr = (uintptr_t)arg_getPtr(obj);
} else { } else {
ptr = (uintptr_t)obj; ptr = (uintptr_t)obj;
@ -543,7 +543,7 @@ int32_t __dir_each(Arg* argEach, Args* context) {
} }
PikaObj* PikaStdLib_SysObj_dir(PikaObj* self, Arg* arg) { PikaObj* PikaStdLib_SysObj_dir(PikaObj* self, Arg* arg) {
if (!argType_isObject(arg_getType(arg))) { if (!arg_isObject(arg)) {
obj_setErrorCode(self, 1); obj_setErrorCode(self, 1);
__platform_printf("[Error] dir: not support type.\r\n"); __platform_printf("[Error] dir: not support type.\r\n");
return NULL; return NULL;

View File

@ -1,7 +1,7 @@
#include "weakref.h" #include "weakref.h"
Arg* weakref_ref(PikaObj* self, Arg* aObj) { Arg* weakref_ref(PikaObj* self, Arg* aObj) {
if (argType_isObject(arg_getType(aObj))) { if (arg_isObject(aObj)) {
PikaObj* obj = arg_getPtr(aObj); PikaObj* obj = arg_getPtr(aObj);
Arg* ret = arg_copy(aObj); Arg* ret = arg_copy(aObj);
obj_refcntDec(obj); obj_refcntDec(obj);

View File

@ -301,7 +301,7 @@ int LibObj_staticLinkFile(LibObj* self, char* input_file_name) {
} }
static int32_t __foreach_handler_listModules(Arg* argEach, Args* context) { static int32_t __foreach_handler_listModules(Arg* argEach, Args* context) {
if (argType_isObject(arg_getType(argEach))) { if (arg_isObject(argEach)) {
PikaObj* module_obj = arg_getPtr(argEach); PikaObj* module_obj = arg_getPtr(argEach);
pika_platform_printf("%s\r\n", obj_getStr(module_obj, "name")); pika_platform_printf("%s\r\n", obj_getStr(module_obj, "name"));
} }
@ -314,7 +314,7 @@ void LibObj_listModules(LibObj* self) {
static int32_t __foreach_handler_libWriteBytecode(Arg* argEach, Args* context) { static int32_t __foreach_handler_libWriteBytecode(Arg* argEach, Args* context) {
FILE* out_file = args_getPtr(context, "out_file"); FILE* out_file = args_getPtr(context, "out_file");
if (argType_isObject(arg_getType(argEach))) { if (arg_isObject(argEach)) {
PikaObj* module_obj = arg_getPtr(argEach); PikaObj* module_obj = arg_getPtr(argEach);
char* bytecode = obj_getPtr(module_obj, "bytecode"); char* bytecode = obj_getPtr(module_obj, "bytecode");
size_t bytecode_size = obj_getBytesSize(module_obj, "buff"); size_t bytecode_size = obj_getBytesSize(module_obj, "buff");
@ -330,7 +330,7 @@ static int32_t __foreach_handler_libWriteBytecode(Arg* argEach, Args* context) {
static int32_t __foreach_handler_libWriteIndex(Arg* argEach, Args* context) { static int32_t __foreach_handler_libWriteIndex(Arg* argEach, Args* context) {
FILE* out_file = args_getPtr(context, "out_file"); FILE* out_file = args_getPtr(context, "out_file");
Args buffs = {0}; Args buffs = {0};
if (argType_isObject(arg_getType(argEach))) { if (arg_isObject(argEach)) {
PikaObj* module_obj = arg_getPtr(argEach); PikaObj* module_obj = arg_getPtr(argEach);
uint32_t bytecode_size = obj_getBytesSize(module_obj, "buff"); uint32_t bytecode_size = obj_getBytesSize(module_obj, "buff");
char buff[LIB_INFO_BLOCK_SIZE - sizeof(uint32_t)] = {0}; char buff[LIB_INFO_BLOCK_SIZE - sizeof(uint32_t)] = {0};
@ -349,7 +349,7 @@ static int32_t __foreach_handler_libWriteIndex(Arg* argEach, Args* context) {
} }
static int32_t __foreach_handler_libSumSize(Arg* argEach, Args* context) { static int32_t __foreach_handler_libSumSize(Arg* argEach, Args* context) {
if (argType_isObject(arg_getType(argEach))) { if (arg_isObject(argEach)) {
PikaObj* module_obj = arg_getPtr(argEach); PikaObj* module_obj = arg_getPtr(argEach);
uint32_t bytecode_size = obj_getBytesSize(module_obj, "buff"); uint32_t bytecode_size = obj_getBytesSize(module_obj, "buff");
bytecode_size = aline_by(bytecode_size, sizeof(uint32_t)); bytecode_size = aline_by(bytecode_size, sizeof(uint32_t));
@ -360,7 +360,7 @@ static int32_t __foreach_handler_libSumSize(Arg* argEach, Args* context) {
} }
static int32_t __foreach_handler_getModuleNum(Arg* argEach, Args* context) { static int32_t __foreach_handler_getModuleNum(Arg* argEach, Args* context) {
if (argType_isObject(arg_getType(argEach))) { if (arg_isObject(argEach)) {
args_setInt(context, "module_num", args_setInt(context, "module_num",
args_getInt(context, "module_num") + 1); args_getInt(context, "module_num") + 1);
} }
@ -511,7 +511,7 @@ int LibObj_loadLibrary(LibObj* self, uint8_t* library_bytes) {
} }
int32_t __foreach_handler_printModule(Arg* argEach, Args* context) { int32_t __foreach_handler_printModule(Arg* argEach, Args* context) {
if (argType_isObject(arg_getType(argEach))) { if (arg_isObject(argEach)) {
PikaObj* module_obj = arg_getPtr(argEach); PikaObj* module_obj = arg_getPtr(argEach);
char* module_name = obj_getStr(module_obj, "name"); char* module_name = obj_getStr(module_obj, "name");
if (NULL != module_name) { if (NULL != module_name) {
@ -784,7 +784,7 @@ exit:
} }
int32_t __foreach_handler_printStates(Arg* argEach, Args* context) { int32_t __foreach_handler_printStates(Arg* argEach, Args* context) {
if (argType_isObject(arg_getType(argEach))) { if (arg_isObject(argEach)) {
PikaObj* module_obj = arg_getPtr(argEach); PikaObj* module_obj = arg_getPtr(argEach);
pika_platform_printf("%s: %s\r\n", obj_getStr(module_obj, "name"), pika_platform_printf("%s: %s\r\n", obj_getStr(module_obj, "name"),
obj_getStr(module_obj, "state")); obj_getStr(module_obj, "state"));
@ -797,7 +797,7 @@ void pikaMaker_printStates(PikaMaker* self) {
} }
int32_t __foreach_handler_getFirstNocompiled(Arg* argEach, Args* context) { int32_t __foreach_handler_getFirstNocompiled(Arg* argEach, Args* context) {
if (argType_isObject(arg_getType(argEach))) { if (arg_isObject(argEach)) {
PikaObj* module_obj = arg_getPtr(argEach); PikaObj* module_obj = arg_getPtr(argEach);
char* state = obj_getStr(module_obj, "state"); char* state = obj_getStr(module_obj, "state");
if (args_isArgExist(context, "res")) { if (args_isArgExist(context, "res")) {
@ -860,7 +860,7 @@ PIKA_RES pikaMaker_compileModuleWithDepends(PikaMaker* self,
int32_t __foreach_handler_linkCompiledModules(Arg* argEach, Args* context) { int32_t __foreach_handler_linkCompiledModules(Arg* argEach, Args* context) {
Args buffs = {0}; Args buffs = {0};
if (argType_isObject(arg_getType(argEach))) { if (arg_isObject(argEach)) {
LibObj* lib = args_getPtr(context, "@lib"); LibObj* lib = args_getPtr(context, "@lib");
PikaMaker* maker = args_getPtr(context, "__maker"); PikaMaker* maker = args_getPtr(context, "__maker");
PikaObj* module_obj = arg_getPtr(argEach); PikaObj* module_obj = arg_getPtr(argEach);

View File

@ -581,7 +581,7 @@ PikaObj* _arg_to_obj(Arg* self, PIKA_BOOL* pIsTemp) {
if (NULL == self) { if (NULL == self) {
return NULL; return NULL;
} }
if (argType_isObject(arg_getType(self))) { if (arg_isObject(self)) {
return arg_getPtr(self); return arg_getPtr(self);
} }
#if !PIKA_NANO_ENABLE #if !PIKA_NANO_ENABLE
@ -792,28 +792,28 @@ void _update_proxy(PikaObj* self, char* name) {
} }
} }
static void obj_saveMethodInfo(PikaObj* self, MethodInfo* method_info) { static void obj_saveMethodInfo(PikaObj* self, MethodInfo* tInfo) {
Arg* arg = New_arg(NULL); Arg* aMethod = New_arg(NULL);
MethodProp method_store = { MethodProp tProp = {
.ptr = method_info->ptr, .ptr = tInfo->ptr,
.type_list = method_info->typelist, .type_list = tInfo->typelist,
.name = method_info->name, .name = tInfo->name,
.bytecode_frame = method_info->bytecode_frame, .bytecode_frame = tInfo->bytecode_frame,
.def_context = method_info->def_context, .def_context = tInfo->def_context,
.declareation = method_info->dec, // const .declareation = tInfo->dec, // const
.host_obj = NULL, .host_obj = NULL,
}; };
char* name = method_info->name; char* name = tInfo->name;
if (NULL == method_info->name) { if (NULL == tInfo->name) {
char name_buff[PIKA_LINE_BUFF_SIZE / 2] = {0}; char name_buff[PIKA_LINE_BUFF_SIZE / 2] = {0};
name = strGetFirstToken(name_buff, method_info->dec, '('); name = strGetFirstToken(name_buff, tInfo->dec, '(');
} }
/* the first arg_value */ /* the first arg_value */
arg = arg_setStruct(arg, name, &method_store, sizeof(method_store)); aMethod = arg_setStruct(aMethod, name, &tProp, sizeof(tProp));
pika_assert(NULL != arg); pika_assert(NULL != aMethod);
arg_setType(arg, method_info->type); arg_setType(aMethod, tInfo->type);
_update_proxy(self, name); _update_proxy(self, name);
args_setArg(self->list, arg); args_setArg(self->list, aMethod);
} }
static int32_t __class_defineMethodWithType(PikaObj* self, static int32_t __class_defineMethodWithType(PikaObj* self,
@ -1727,7 +1727,7 @@ PikaObj* obj_importModuleWithByteCode(PikaObj* self,
PikaObj* oModule = arg_getPtr(aModule); PikaObj* oModule = arg_getPtr(aModule);
obj_setArg(self, name, aModule); obj_setArg(self, name, aModule);
arg_setIsWeakRef(obj_getArg(self, name), PIKA_TRUE); arg_setIsWeakRef(obj_getArg(self, name), PIKA_TRUE);
pika_assert(argType_isObject(arg_getType(aModule))); pika_assert(arg_isObject(aModule));
/* decrase refcnt to avoid circle reference */ /* decrase refcnt to avoid circle reference */
obj_refcntDec(oModule); obj_refcntDec(oModule);
} }

View File

@ -472,7 +472,7 @@ static int _obj_getLen(PikaObj* self) {
} }
static int arg_getLen(Arg* self) { static int arg_getLen(Arg* self) {
if (argType_isObject(arg_getType(self))) { if (arg_isObject(self)) {
return _obj_getLen(arg_getPtr(self)); return _obj_getLen(arg_getPtr(self));
} }
if (arg_getType(self) == ARG_TYPE_STRING) { if (arg_getType(self) == ARG_TYPE_STRING) {
@ -683,7 +683,7 @@ Arg* _vm_slice(VMState* vm,
return sliced_arg; return sliced_arg;
} }
if (argType_isObject(arg_getType(obj))) { if (arg_isObject(obj)) {
PikaObj* arg_obj = arg_getPtr(obj); PikaObj* arg_obj = arg_getPtr(obj);
PikaObj* New_PikaStdData_List(Args * args); PikaObj* New_PikaStdData_List(Args * args);
PikaObj* New_PikaStdData_Tuple(Args * args); PikaObj* New_PikaStdData_Tuple(Args * args);
@ -871,7 +871,7 @@ static Arg* VM_instruction_handler_REF(PikaObj* self,
if (arg_path[0] == '.') { if (arg_path[0] == '.') {
/* find host from stack */ /* find host from stack */
Arg* host_arg = stack_popArg_alloc(&(vm->stack)); Arg* host_arg = stack_popArg_alloc(&(vm->stack));
if (argType_isObject(arg_getType(host_arg))) { if (arg_isObject(host_arg)) {
oHost = arg_getPtr(host_arg); oHost = arg_getPtr(host_arg);
aRes = arg_copy_noalloc(obj_getArg(oHost, arg_path + 1), aRetReg); aRes = arg_copy_noalloc(obj_getArg(oHost, arg_path + 1), aRetReg);
} }
@ -955,53 +955,56 @@ Arg* _get_return_arg(PikaObj* locals) {
Arg* _obj_runMethodArgWithState(PikaObj* self, Arg* _obj_runMethodArgWithState(PikaObj* self,
PikaObj* locals, PikaObj* locals,
Arg* method_arg, Arg* aMethod,
RunState* run_state, RunState* run_state,
Arg* ret_arg_reg) { Arg* aReturnCache) {
pika_assert(NULL != run_state); pika_assert(NULL != run_state);
Arg* return_arg = NULL; Arg* aReturn = NULL;
/* get method Ptr */ /* get method Ptr */
Method method_ptr = methodArg_getPtr(method_arg); Method fMethod = methodArg_getPtr(aMethod);
/* get method type list */ /* get method type list */
ArgType method_type = arg_getType(method_arg); ArgType methodType = arg_getType(aMethod);
/* error */ /* error */
if (ARG_TYPE_NONE == method_type) { if (ARG_TYPE_NONE == methodType) {
return NULL; return NULL;
} }
/* redirect to def context */ /* redirect to def context */
if (!argType_isNative(method_type)) { if (!argType_isNative(methodType)) {
self = methodArg_getDefContext(method_arg); self = methodArg_getDefContext(aMethod);
} }
obj_setErrorCode(self, PIKA_RES_OK); obj_setErrorCode(self, PIKA_RES_OK);
/* run method */ /* run method */
if (method_type == ARG_TYPE_METHOD_NATIVE) { if (methodType == ARG_TYPE_METHOD_NATIVE) {
/* native method */ /* native method */
method_ptr(self, locals->list); fMethod(self, locals->list);
/* get method return */ /* get method return */
return_arg = _get_return_arg(locals); aReturn = _get_return_arg(locals);
} else if (method_type == ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR) { } else if (methodType == ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR) {
/* native method */ /* native method */
method_ptr(self, locals->list); fMethod(self, locals->list);
/* get method return */ /* get method return */
return_arg = _get_return_arg(locals); aReturn = _get_return_arg(locals);
} else { } else {
/* static method and object method */ /* static method and object method */
/* byteCode */ /* byteCode */
ByteCodeFrame* method_bytecodeFrame = ByteCodeFrame* method_bytecodeFrame =
methodArg_getBytecodeFrame(method_arg); methodArg_getBytecodeFrame(aMethod);
uintptr_t insturctArray_start = (uintptr_t)instructArray_getByOffset( uintptr_t insturctArray_start = (uintptr_t)instructArray_getByOffset(
&(method_bytecodeFrame->instruct_array), 0); &(method_bytecodeFrame->instruct_array), 0);
uint16_t pc = (uintptr_t)method_ptr - insturctArray_start; uint16_t pc = (uintptr_t)fMethod - insturctArray_start;
locals = __pikaVM_runByteCodeFrameWithState( locals = __pikaVM_runByteCodeFrameWithState(
self, locals, self, method_bytecodeFrame, pc, run_state); self, locals, self, method_bytecodeFrame, pc, run_state);
/* get method return */ /* get method return */
return_arg = _get_return_arg(locals); aReturn = _get_return_arg(locals);
} }
return return_arg; if (argType_isConstructor(methodType)) {
// aReturn = arg_copy_noalloc(aReturn, aReturnCache);
}
return aReturn;
} }
Arg* obj_runMethodArgWithState(PikaObj* self, Arg* obj_runMethodArgWithState(PikaObj* self,
@ -1242,7 +1245,7 @@ static int _get_n_input_with_unpack(VMState* vm, int n_used) {
break; break;
} }
if (arg_getIsStarred(call_arg)) { if (arg_getIsStarred(call_arg)) {
pika_assert(argType_isObject(arg_getType(call_arg))); pika_assert(arg_isObject(call_arg));
PikaObj* obj = arg_getPtr(call_arg); PikaObj* obj = arg_getPtr(call_arg);
int len = _obj_getLen(obj); int len = _obj_getLen(obj);
for (int i_star_arg = len - 1; i_star_arg >= 0; i_star_arg--) { for (int i_star_arg = len - 1; i_star_arg >= 0; i_star_arg--) {
@ -1270,7 +1273,7 @@ static int _get_n_input_with_unpack(VMState* vm, int n_used) {
goto __continue; goto __continue;
} }
if (arg_getIsDoubleStarred(call_arg)) { if (arg_getIsDoubleStarred(call_arg)) {
pika_assert(argType_isObject(arg_getType(call_arg))); pika_assert(arg_isObject(call_arg));
PikaObj* New_PikaStdData_Dict(Args * args); PikaObj* New_PikaStdData_Dict(Args * args);
PikaObj* obj = arg_getPtr(call_arg); PikaObj* obj = arg_getPtr(call_arg);
pika_assert(obj->constructor == New_PikaStdData_Dict); pika_assert(obj->constructor == New_PikaStdData_Dict);

View File

@ -272,11 +272,21 @@ static inline uint8_t argType_isCallable(ArgType type) {
(type) == ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR); (type) == ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR);
} }
static inline uint8_t argType_isConstructor(ArgType type) {
return ((type) == ARG_TYPE_METHOD_CONSTRUCTOR ||
(type) == ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR);
}
static inline uint8_t argType_isNative(ArgType type) { static inline uint8_t argType_isNative(ArgType type) {
return ((type) == ARG_TYPE_METHOD_NATIVE || return ((type) == ARG_TYPE_METHOD_NATIVE ||
(type) == ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR); (type) == ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR);
} }
#define arg_isObject(__self) argType_isObject(arg_getType(__self))
#define arg_isCallable(__self) argType_isCallable(arg_getType(__self))
#define arg_isConstructor(__self) argType_isConstructor(arg_getType(__self))
#define arg_isNative(__self) argType_isNative(arg_getType(__self))
#define arg_newReg(__name, __size) \ #define arg_newReg(__name, __size) \
Arg __name = {0}; \ Arg __name = {0}; \
uint8_t __##__name##_buff[__size] = {0}; \ uint8_t __##__name##_buff[__size] = {0}; \