From cc6fd6605baedf553795c4a6f01ca3dc5e2d0f5f Mon Sep 17 00:00:00 2001 From: lyon Date: Wed, 1 Mar 2023 21:23:33 +0800 Subject: [PATCH] ready to intruce mark-sweep GC --- package/PikaStdLib/PikaStdLib_SysObj.c | 6 +-- package/weakref/weakref.c | 9 ++-- port/linux/.vscode/launch.json | 2 +- port/linux/.vscode/settings.json | 5 +- .../PikaStdLib/PikaStdLib_SysObj.c | 6 +-- .../pikascript-lib/weakref/weakref.c | 2 +- src/PikaCompiler.c | 18 +++---- src/PikaObj.c | 36 +++++++------- src/PikaVM.c | 49 ++++++++++--------- src/dataArg.h | 10 ++++ 10 files changed, 80 insertions(+), 63 deletions(-) diff --git a/package/PikaStdLib/PikaStdLib_SysObj.c b/package/PikaStdLib/PikaStdLib_SysObj.c index 02d61433f..44bb68490 100644 --- a/package/PikaStdLib/PikaStdLib_SysObj.c +++ b/package/PikaStdLib/PikaStdLib_SysObj.c @@ -288,7 +288,7 @@ int PikaStdLib_SysObj_len(PikaObj* self, Arg* arg) { return arg_getBytesSize(arg); } - if (argType_isObject(arg_getType(arg))) { + if (arg_isObject(arg)) { PikaObj* arg_obj = arg_getPtr(arg); Arg* method_arg = obj_getMethodArg(arg_obj, "__len__"); 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) { uintptr_t ptr = 0; - if (argType_isObject(arg_getType(obj))) { + if (arg_isObject(obj)) { ptr = (uintptr_t)arg_getPtr(obj); } else { ptr = (uintptr_t)obj; @@ -543,7 +543,7 @@ int32_t __dir_each(Arg* argEach, Args* context) { } PikaObj* PikaStdLib_SysObj_dir(PikaObj* self, Arg* arg) { - if (!argType_isObject(arg_getType(arg))) { + if (!arg_isObject(arg)) { obj_setErrorCode(self, 1); __platform_printf("[Error] dir: not support type.\r\n"); return NULL; diff --git a/package/weakref/weakref.c b/package/weakref/weakref.c index e03194333..8da20dde5 100644 --- a/package/weakref/weakref.c +++ b/package/weakref/weakref.c @@ -1,9 +1,10 @@ #include "weakref.h" -Arg* weakref_ref(PikaObj* self, Arg* obj) { - if (argType_isObject(arg_getType(obj))) { - Arg* ret = arg_copy(obj); - obj_refcntDec((PikaObj*)arg_getPtr(ret)); +Arg* weakref_ref(PikaObj* self, Arg* aObj) { + if (arg_isObject(aObj)) { + PikaObj* obj = arg_getPtr(aObj); + Arg* ret = arg_copy(aObj); + obj_refcntDec(obj); arg_setIsWeakRef(ret, PIKA_TRUE); return ret; } diff --git a/port/linux/.vscode/launch.json b/port/linux/.vscode/launch.json index a559e4acb..3547a7830 100644 --- a/port/linux/.vscode/launch.json +++ b/port/linux/.vscode/launch.json @@ -11,7 +11,7 @@ "program": "${workspaceFolder}/build/test/pikascript_test", // "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain", "args": [ - "--gtest_filter=vm.getattr_native" + // "--gtest_filter=vm.getattr_native" ], "stopAtEntry": false, "cwd": "${workspaceFolder}", diff --git a/port/linux/.vscode/settings.json b/port/linux/.vscode/settings.json index d00671930..5058dc926 100644 --- a/port/linux/.vscode/settings.json +++ b/port/linux/.vscode/settings.json @@ -109,7 +109,10 @@ "_network_wlan.h": "c", "pikastddata_tuple.h": "c", "setjmp.h": "c", - "pthread.h": "c" + "pthread.h": "c", + "deque": "c", + "list": "c", + "vector": "c" }, "python.formatting.provider": "autopep8", } \ No newline at end of file diff --git a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c index 02d61433f..44bb68490 100644 --- a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c +++ b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c @@ -288,7 +288,7 @@ int PikaStdLib_SysObj_len(PikaObj* self, Arg* arg) { return arg_getBytesSize(arg); } - if (argType_isObject(arg_getType(arg))) { + if (arg_isObject(arg)) { PikaObj* arg_obj = arg_getPtr(arg); Arg* method_arg = obj_getMethodArg(arg_obj, "__len__"); 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) { uintptr_t ptr = 0; - if (argType_isObject(arg_getType(obj))) { + if (arg_isObject(obj)) { ptr = (uintptr_t)arg_getPtr(obj); } else { ptr = (uintptr_t)obj; @@ -543,7 +543,7 @@ int32_t __dir_each(Arg* argEach, Args* context) { } PikaObj* PikaStdLib_SysObj_dir(PikaObj* self, Arg* arg) { - if (!argType_isObject(arg_getType(arg))) { + if (!arg_isObject(arg)) { obj_setErrorCode(self, 1); __platform_printf("[Error] dir: not support type.\r\n"); return NULL; diff --git a/port/linux/package/pikascript/pikascript-lib/weakref/weakref.c b/port/linux/package/pikascript/pikascript-lib/weakref/weakref.c index 9579e8848..8da20dde5 100644 --- a/port/linux/package/pikascript/pikascript-lib/weakref/weakref.c +++ b/port/linux/package/pikascript/pikascript-lib/weakref/weakref.c @@ -1,7 +1,7 @@ #include "weakref.h" Arg* weakref_ref(PikaObj* self, Arg* aObj) { - if (argType_isObject(arg_getType(aObj))) { + if (arg_isObject(aObj)) { PikaObj* obj = arg_getPtr(aObj); Arg* ret = arg_copy(aObj); obj_refcntDec(obj); diff --git a/src/PikaCompiler.c b/src/PikaCompiler.c index e18b53003..720e72fa3 100644 --- a/src/PikaCompiler.c +++ b/src/PikaCompiler.c @@ -301,7 +301,7 @@ int LibObj_staticLinkFile(LibObj* self, char* input_file_name) { } 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); 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) { FILE* out_file = args_getPtr(context, "out_file"); - if (argType_isObject(arg_getType(argEach))) { + if (arg_isObject(argEach)) { PikaObj* module_obj = arg_getPtr(argEach); char* bytecode = obj_getPtr(module_obj, "bytecode"); 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) { FILE* out_file = args_getPtr(context, "out_file"); Args buffs = {0}; - if (argType_isObject(arg_getType(argEach))) { + if (arg_isObject(argEach)) { PikaObj* module_obj = arg_getPtr(argEach); uint32_t bytecode_size = obj_getBytesSize(module_obj, "buff"); 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) { - if (argType_isObject(arg_getType(argEach))) { + if (arg_isObject(argEach)) { PikaObj* module_obj = arg_getPtr(argEach); uint32_t bytecode_size = obj_getBytesSize(module_obj, "buff"); 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) { - if (argType_isObject(arg_getType(argEach))) { + if (arg_isObject(argEach)) { args_setInt(context, "module_num", 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) { - if (argType_isObject(arg_getType(argEach))) { + if (arg_isObject(argEach)) { PikaObj* module_obj = arg_getPtr(argEach); char* module_name = obj_getStr(module_obj, "name"); if (NULL != module_name) { @@ -784,7 +784,7 @@ exit: } 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); pika_platform_printf("%s: %s\r\n", obj_getStr(module_obj, "name"), obj_getStr(module_obj, "state")); @@ -797,7 +797,7 @@ void pikaMaker_printStates(PikaMaker* self) { } 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); char* state = obj_getStr(module_obj, "state"); if (args_isArgExist(context, "res")) { @@ -860,7 +860,7 @@ PIKA_RES pikaMaker_compileModuleWithDepends(PikaMaker* self, int32_t __foreach_handler_linkCompiledModules(Arg* argEach, Args* context) { Args buffs = {0}; - if (argType_isObject(arg_getType(argEach))) { + if (arg_isObject(argEach)) { LibObj* lib = args_getPtr(context, "@lib"); PikaMaker* maker = args_getPtr(context, "__maker"); PikaObj* module_obj = arg_getPtr(argEach); diff --git a/src/PikaObj.c b/src/PikaObj.c index 6caaf8bec..4bfa00b96 100644 --- a/src/PikaObj.c +++ b/src/PikaObj.c @@ -581,7 +581,7 @@ PikaObj* _arg_to_obj(Arg* self, PIKA_BOOL* pIsTemp) { if (NULL == self) { return NULL; } - if (argType_isObject(arg_getType(self))) { + if (arg_isObject(self)) { return arg_getPtr(self); } #if !PIKA_NANO_ENABLE @@ -792,28 +792,28 @@ void _update_proxy(PikaObj* self, char* name) { } } -static void obj_saveMethodInfo(PikaObj* self, MethodInfo* method_info) { - Arg* arg = New_arg(NULL); - MethodProp method_store = { - .ptr = method_info->ptr, - .type_list = method_info->typelist, - .name = method_info->name, - .bytecode_frame = method_info->bytecode_frame, - .def_context = method_info->def_context, - .declareation = method_info->dec, // const +static void obj_saveMethodInfo(PikaObj* self, MethodInfo* tInfo) { + Arg* aMethod = New_arg(NULL); + MethodProp tProp = { + .ptr = tInfo->ptr, + .type_list = tInfo->typelist, + .name = tInfo->name, + .bytecode_frame = tInfo->bytecode_frame, + .def_context = tInfo->def_context, + .declareation = tInfo->dec, // const .host_obj = NULL, }; - char* name = method_info->name; - if (NULL == method_info->name) { + char* name = tInfo->name; + if (NULL == tInfo->name) { 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 */ - arg = arg_setStruct(arg, name, &method_store, sizeof(method_store)); - pika_assert(NULL != arg); - arg_setType(arg, method_info->type); + aMethod = arg_setStruct(aMethod, name, &tProp, sizeof(tProp)); + pika_assert(NULL != aMethod); + arg_setType(aMethod, tInfo->type); _update_proxy(self, name); - args_setArg(self->list, arg); + args_setArg(self->list, aMethod); } static int32_t __class_defineMethodWithType(PikaObj* self, @@ -1727,7 +1727,7 @@ PikaObj* obj_importModuleWithByteCode(PikaObj* self, PikaObj* oModule = arg_getPtr(aModule); obj_setArg(self, name, aModule); 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 */ obj_refcntDec(oModule); } diff --git a/src/PikaVM.c b/src/PikaVM.c index e0f951595..634aa0820 100644 --- a/src/PikaVM.c +++ b/src/PikaVM.c @@ -472,7 +472,7 @@ static int _obj_getLen(PikaObj* self) { } static int arg_getLen(Arg* self) { - if (argType_isObject(arg_getType(self))) { + if (arg_isObject(self)) { return _obj_getLen(arg_getPtr(self)); } if (arg_getType(self) == ARG_TYPE_STRING) { @@ -683,7 +683,7 @@ Arg* _vm_slice(VMState* vm, return sliced_arg; } - if (argType_isObject(arg_getType(obj))) { + if (arg_isObject(obj)) { PikaObj* arg_obj = arg_getPtr(obj); PikaObj* New_PikaStdData_List(Args * args); PikaObj* New_PikaStdData_Tuple(Args * args); @@ -871,7 +871,7 @@ static Arg* VM_instruction_handler_REF(PikaObj* self, if (arg_path[0] == '.') { /* find host from 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); 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, PikaObj* locals, - Arg* method_arg, + Arg* aMethod, RunState* run_state, - Arg* ret_arg_reg) { + Arg* aReturnCache) { pika_assert(NULL != run_state); - Arg* return_arg = NULL; + Arg* aReturn = NULL; /* get method Ptr */ - Method method_ptr = methodArg_getPtr(method_arg); + Method fMethod = methodArg_getPtr(aMethod); /* get method type list */ - ArgType method_type = arg_getType(method_arg); + ArgType methodType = arg_getType(aMethod); /* error */ - if (ARG_TYPE_NONE == method_type) { + if (ARG_TYPE_NONE == methodType) { return NULL; } /* redirect to def context */ - if (!argType_isNative(method_type)) { - self = methodArg_getDefContext(method_arg); + if (!argType_isNative(methodType)) { + self = methodArg_getDefContext(aMethod); } obj_setErrorCode(self, PIKA_RES_OK); /* run method */ - if (method_type == ARG_TYPE_METHOD_NATIVE) { + if (methodType == ARG_TYPE_METHOD_NATIVE) { /* native method */ - method_ptr(self, locals->list); + fMethod(self, locals->list); /* get method return */ - return_arg = _get_return_arg(locals); - } else if (method_type == ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR) { + aReturn = _get_return_arg(locals); + } else if (methodType == ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR) { /* native method */ - method_ptr(self, locals->list); + fMethod(self, locals->list); /* get method return */ - return_arg = _get_return_arg(locals); + aReturn = _get_return_arg(locals); } else { /* static method and object method */ /* byteCode */ ByteCodeFrame* method_bytecodeFrame = - methodArg_getBytecodeFrame(method_arg); + methodArg_getBytecodeFrame(aMethod); uintptr_t insturctArray_start = (uintptr_t)instructArray_getByOffset( &(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( self, locals, self, method_bytecodeFrame, pc, run_state); /* 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, @@ -1242,7 +1245,7 @@ static int _get_n_input_with_unpack(VMState* vm, int n_used) { break; } 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); int len = _obj_getLen(obj); 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; } 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* obj = arg_getPtr(call_arg); pika_assert(obj->constructor == New_PikaStdData_Dict); diff --git a/src/dataArg.h b/src/dataArg.h index 3c4550975..c9c615eb5 100644 --- a/src/dataArg.h +++ b/src/dataArg.h @@ -272,11 +272,21 @@ static inline uint8_t argType_isCallable(ArgType type) { (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) { return ((type) == ARG_TYPE_METHOD_NATIVE || (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) \ Arg __name = {0}; \ uint8_t __##__name##_buff[__size] = {0}; \