From cf77f8eb6f7908e19a354104a88a5cbae641e297 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 26 Nov 2021 14:55:50 +0000 Subject: [PATCH] trying to implement object instantiation --- lib/comp.c | 19 ++++++++++++++++--- lib/prim.c | 55 ++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 55 insertions(+), 19 deletions(-) diff --git a/lib/comp.c b/lib/comp.c index dd58de6..462dbf6 100644 --- a/lib/comp.c +++ b/lib/comp.c @@ -242,7 +242,7 @@ static int find_variable_backward (hcl_t* hcl, const hcl_oocs_t* name, hcl_var_i { clsbi = &hcl->c->clsblk.info[--j]; - if (clsbi->ivars_str) + if (i < hcl->c->fnblk.depth && clsbi->ivars_str) { haystack.ptr = clsbi->ivars_str; haystack.len = hcl_count_oocstr(clsbi->ivars_str); @@ -273,6 +273,15 @@ HCL_INFO6 (hcl, "FOUND CLASS VAR [%.*js]...[%.*js]................ ===> ctx_offs } } + if (i == hcl->c->fnblk.depth) + { + /* this condition indicates that the current function level contains a class defintion + * and this variable is looked up inside the class defintion */ +HCL_INFO2 (hcl, "CLASS NAMED VAR [%.*js]\n", name->len, name->ptr); + vi->type = VAR_CLASS;//_NAMED; // TODO: create VAR_CLASS_NAMED??? + vi->ctx_offset = 0; + vi->index_in_ctx = 0; + } break; } @@ -751,7 +760,8 @@ static int emit_variable_access (hcl_t* hcl, int mode, const hcl_var_info_t* vi, static hcl_oob_t inst_map[2][3] = { { HCL_CODE_PUSH_CTXTEMPVAR_0, HCL_CODE_POP_INTO_CTXTEMPVAR_0, HCL_CODE_STORE_INTO_CTXTEMPVAR_0 }, - { HCL_CODE_PUSH_INSTVAR_0, HCL_CODE_POP_INTO_INSTVAR_0, HCL_CODE_STORE_INTO_INSTVAR_0 } + { HCL_CODE_PUSH_INSTVAR_0, HCL_CODE_POP_INTO_INSTVAR_0, HCL_CODE_STORE_INTO_INSTVAR_0 }, + { HCL_CODE_PUSH_OBJVAR_0, HCL_CODE_POP_INTO_OBJVAR_0, HCL_CODE_STORE_INTO_OBJVAR_0 }, }; switch (vi->type) @@ -760,9 +770,12 @@ static int emit_variable_access (hcl_t* hcl, int mode, const hcl_var_info_t* vi, return emit_double_param_instruction(hcl, inst_map[0][mode], vi->ctx_offset, vi->index_in_ctx, srcloc); case VAR_INST: - case VAR_CLASS: HCL_ASSERT (hcl, vi->ctx_offset == 0); return emit_single_param_instruction(hcl, inst_map[1][mode], vi->index_in_ctx, srcloc); + + case VAR_CLASS: + HCL_ASSERT (hcl, vi->ctx_offset == 0); + return emit_double_param_instruction(hcl, inst_map[2][mode], vi->index_in_ctx, <>, srcloc); } return -1; diff --git a/lib/prim.c b/lib/prim.c index 18ec8dd..d01425c 100644 --- a/lib/prim.c +++ b/lib/prim.c @@ -865,6 +865,27 @@ static hcl_pfrc_t pf_va_get (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs) } +static hcl_pfrc_t pf_object_new (hcl_t* hcl, hcl_mod_t* mod, hcl_ooi_t nargs) +{ +/* TODO: accept the object size if the class is variable-sized. */ + hcl_oop_t obj; + hcl_oop_t class_; + + class_ = HCL_STACK_GETARG(hcl, nargs, 0); + if (!HCL_IS_CLASS(hcl, class_)) + { + hcl_seterrbfmt (hcl, HCL_EINVAL, "not a class - %O", class_); + return HCL_PF_FAILURE; + } + + obj = hcl_instantiate(hcl, class_, HCL_NULL, 0); + if (HCL_UNLIKELY(!obj)) return HCL_PF_FAILURE; + + + HCL_STACK_SETRET (hcl, nargs, obj); + return HCL_PF_SUCCESS; +} + /* ------------------------------------------------------------------------- */ static pf_t builtin_prims[] = @@ -934,26 +955,28 @@ static pf_t builtin_prims[] = { 0, 1, pf_va_count, 8, { 'v','a','-','c','o','u','n','t' } }, { 1, 2, pf_va_get, 6, { 'v','a','-','g','e','t' } }, - { 0, 0, hcl_pf_process_current, 15, { 'c','u','r','r','e','n','t','-','p','r','o','c','e','s','s'} }, - { 1, HCL_TYPE_MAX(hcl_oow_t), hcl_pf_process_fork, 4, { 'f','o','r','k'} }, - { 1, 1, hcl_pf_process_resume, 6, { 'r','e','s','u','m','e' } }, - { 0, 1, hcl_pf_process_suspend, 7, { 's','u','s','p','e','n','d' } }, - { 0, 1, hcl_pf_process_terminate, 9, { 't','e','r','m','i','n','a','t','e' } }, - { 0, 0, hcl_pf_process_terminate_all, 13, { 't','e','r','m','i','n','a','t','e','-','a','l','l' } }, - { 0, 0, hcl_pf_process_yield, 5, { 'y','i','e','l','d'} }, + { 1, 1, pf_object_new, 10, { 'o','b','j','e','c','t','-','n','e','w' } }, + + { 0, 0, hcl_pf_process_current, 15, { 'c','u','r','r','e','n','t','-','p','r','o','c','e','s','s'} }, + { 1, HCL_TYPE_MAX(hcl_oow_t), hcl_pf_process_fork, 4, { 'f','o','r','k'} }, + { 1, 1, hcl_pf_process_resume, 6, { 'r','e','s','u','m','e' } }, + { 0, 1, hcl_pf_process_suspend, 7, { 's','u','s','p','e','n','d' } }, + { 0, 1, hcl_pf_process_terminate, 9, { 't','e','r','m','i','n','a','t','e' } }, + { 0, 0, hcl_pf_process_terminate_all, 13, { 't','e','r','m','i','n','a','t','e','-','a','l','l' } }, + { 0, 0, hcl_pf_process_yield, 5, { 'y','i','e','l','d'} }, - { 0, 0, hcl_pf_semaphore_new, 7, { 's','e','m','-','n','e','w'} }, - { 1, 1, hcl_pf_semaphore_wait, 8, { 's','e','m','-','w','a','i','t'} }, - { 1, 3, hcl_pf_semaphore_signal, 10, { 's','e','m','-','s','i','g','n','a','l'} }, - { 2, 2, hcl_pf_semaphore_signal_on_input, 19, { 's','e','m','-','s','i','g','n','a','l','-','o','n','-','i','n','p','u','t'} }, - { 2, 2, hcl_pf_semaphore_signal_on_output, 20, { 's','e','m','-','s','i','g','n','a','l','-','o','n','-','o','u','t','p','u','t'} }, - { 1, 1, hcl_pf_semaphore_unsignal, 12, { 's','e','m','-','u','n','s','i','g','n','a','l'} }, + { 0, 0, hcl_pf_semaphore_new, 7, { 's','e','m','-','n','e','w'} }, + { 1, 1, hcl_pf_semaphore_wait, 8, { 's','e','m','-','w','a','i','t'} }, + { 1, 3, hcl_pf_semaphore_signal, 10, { 's','e','m','-','s','i','g','n','a','l'} }, + { 2, 2, hcl_pf_semaphore_signal_on_input, 19, { 's','e','m','-','s','i','g','n','a','l','-','o','n','-','i','n','p','u','t'} }, + { 2, 2, hcl_pf_semaphore_signal_on_output, 20, { 's','e','m','-','s','i','g','n','a','l','-','o','n','-','o','u','t','p','u','t'} }, + { 1, 1, hcl_pf_semaphore_unsignal, 12, { 's','e','m','-','u','n','s','i','g','n','a','l'} }, - { 0, 0, hcl_pf_semaphore_group_new, 9, { 's','e','m','g','r','-','n','e','w'} }, - { 1, 2, hcl_pf_semaphore_group_add_semaphore, 9, { 's','e','m','g','r','-','a','d','d'} }, + { 0, 0, hcl_pf_semaphore_group_new, 9, { 's','e','m','g','r','-','n','e','w'} }, + { 1, 2, hcl_pf_semaphore_group_add_semaphore, 9, { 's','e','m','g','r','-','a','d','d'} }, { 1, 2, hcl_pf_semaphore_group_remove_semaphore, 12, { 's','e','m','g','r','-','r','e','m','o','v','e'} }, - { 1, 1, hcl_pf_semaphore_group_wait, 10, { 's','e','m','g','r','-','w','a','i','t'} } + { 1, 1, hcl_pf_semaphore_group_wait, 10, { 's','e','m','g','r','-','w','a','i','t'} } }; int hcl_addbuiltinprims (hcl_t* hcl)