From 84e53e3459cf97b1f5efaaae70b5bdc2fb9b5420 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sat, 29 Jan 2022 17:43:11 +0000 Subject: [PATCH] adding the class_set instruction --- lib/comp.c | 36 +++++++++++++++++++++++++++++++----- lib/decode.c | 9 +++++++-- lib/exec.c | 10 ++++++++-- lib/hcl-prv.h | 5 +++-- 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/lib/comp.c b/lib/comp.c index a2d7720..cbffb84 100644 --- a/lib/comp.c +++ b/lib/comp.c @@ -246,7 +246,7 @@ static int find_variable_backward (hcl_t* hcl, const hcl_cnode_t* token, hcl_var HCL_ASSERT (hcl, hcl->c->fnblk.depth >= 0); HCL_ASSERT (hcl, hcl->c->fnblk.info[hcl->c->fnblk.depth].tmprlen == hcl->c->tv.s.len); - + name = HCL_CNODE_GET_TOK(token); /* depth begins at -1. so it is the actual index. let the looping begin at depth + 1 @@ -552,6 +552,7 @@ static int emit_single_param_instruction (hcl_t* hcl, int cmd, hcl_oow_t param_1 case HCL_CODE_STORE_INTO_CLSVAR_M_X: case HCL_CODE_POP_INTO_CLSVAR_M_X: + case HCL_CODE_CLASS_SET: case HCL_CODE_TRY_ENTER: case HCL_CODE_TRY_ENTER2: case HCL_CODE_PUSH_INTLIT: @@ -1278,6 +1279,7 @@ enum COP_EMIT_POP_STACKTOP, COP_EMIT_RETURN, COP_EMIT_SET, + COP_EMIT_CLASS_SET, COP_EMIT_THROW, COP_POST_IF_COND, @@ -2269,7 +2271,7 @@ static HCL_INLINE int compile_class_p2 (hcl_t* hcl) pop_clsblk (hcl); /* end of the class block */ - if (emit_byte_instruction(hcl, HCL_CODE_CLASS_PUSH_EXIT, HCL_CNODE_GET_LOC(cf->operand)) <= -1) return -1; + if (emit_byte_instruction(hcl, HCL_CODE_CLASS_PEXIT, HCL_CNODE_GET_LOC(cf->operand)) <= -1) return -1; // if (cf->operand) { @@ -4793,8 +4795,9 @@ static HCL_INLINE int post_lambda (hcl_t* hcl) if (x <= -1) return -1; if (x == 0) { - /* save to the method slot */ -printf ("this is a method defintion...^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.\n"); + /* arrange to save to the method slot */ + SWITCH_TOP_CFRAME (hcl, COP_EMIT_CLASS_SET, defun_name); + cf = GET_TOP_CFRAME(hcl); } else { @@ -4802,7 +4805,6 @@ printf ("this is a method defintion...^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.\n"); hcl_setsynerrbfmt (hcl, HCL_SYNERR_VARNAMEDUP, HCL_CNODE_GET_LOC(defun_name), HCL_CNODE_GET_TOK(defun_name), "duplicate name"); return -1; } - cf->u.set.mode = VAR_ACCESS_STORE; } else { @@ -4902,6 +4904,26 @@ static HCL_INLINE int emit_set (hcl_t* hcl) return 0; } +static HCL_INLINE int emit_class_set (hcl_t* hcl) +{ + hcl_cframe_t* cf; + hcl_oop_t lit; + hcl_oow_t index; + + cf = GET_TOP_CFRAME(hcl); + HCL_ASSERT (hcl, cf->opcode == COP_EMIT_CLASS_SET); + + lit = hcl_makesymbol(hcl, HCL_CNODE_GET_TOKPTR(cf->operand), HCL_CNODE_GET_TOKLEN(cf->operand)); + if (HCL_UNLIKELY(!lit)) return -1; + + if (add_literal(hcl, lit, &index) <= -1) return -1; + if (emit_single_param_instruction(hcl, HCL_CODE_CLASS_SET, index, HCL_CNODE_GET_LOC(cf->operand)) <= -1) return -1; + + + POP_CFRAME (hcl); + return 0; +} + static HCL_INLINE int emit_throw (hcl_t* hcl) { hcl_cframe_t* cf; @@ -5149,6 +5171,10 @@ int hcl_compile (hcl_t* hcl, hcl_cnode_t* obj, int flags) if (emit_set(hcl) <= -1) goto oops; break; + case COP_EMIT_CLASS_SET: + if (emit_class_set(hcl) <= -1) goto oops; + break; + case COP_EMIT_THROW: if (emit_throw(hcl) <= -1) goto oops; break; diff --git a/lib/decode.c b/lib/decode.c index 76f650d..6875257 100644 --- a/lib/decode.c +++ b/lib/decode.c @@ -382,8 +382,13 @@ int hcl_decode (hcl_t* hcl, hcl_oow_t start, hcl_oow_t end) LOG_INST_0 (hcl, "class_exit"); break; - case HCL_CODE_CLASS_PUSH_EXIT: - LOG_INST_0 (hcl, "class_push_exit"); + case HCL_CODE_CLASS_PEXIT: + LOG_INST_0 (hcl, "class_pexit"); + break; + + case HCL_CODE_CLASS_SET: + FETCH_PARAM_CODE_TO (hcl, b1); + LOG_INST_1 (hcl, "class_set %zu", b1); break; /* -------------------------------------------------------- */ diff --git a/lib/exec.c b/lib/exec.c index c656a30..e8062e4 100644 --- a/lib/exec.c +++ b/lib/exec.c @@ -3534,11 +3534,11 @@ if (do_throw(hcl, hcl->_nil, fetched_instruction_pointer) <= -1) break; } - case HCL_CODE_CLASS_PUSH_EXIT: + case HCL_CODE_CLASS_PEXIT: { hcl_oop_t c; - LOG_INST_0 (hcl, "class_push_exit"); + LOG_INST_0 (hcl, "class_pexit"); if (HCL_CLSTACK_IS_EMPTY(hcl)) { @@ -3551,6 +3551,12 @@ if (do_throw(hcl, hcl->_nil, fetched_instruction_pointer) <= -1) break; } + + case HCL_CODE_CLASS_SET: + { + FETCH_PARAM_CODE_TO (hcl, b1); + LOG_INST_1 (hcl, "class_set %zu", b1); + } /* -------------------------------------------------------- */ case HCL_CODE_PUSH_CTXTEMPVAR_X: diff --git a/lib/hcl-prv.h b/lib/hcl-prv.h index 15aa7e1..fd9d54e 100644 --- a/lib/hcl-prv.h +++ b/lib/hcl-prv.h @@ -925,10 +925,11 @@ enum hcl_bcode_t HCL_CODE_PUSH_CTXTEMPVAR_X = 0xE0, /* 224 ## */ HCL_CODE_CLASS_ENTER = 0xE1, /* 225 ## */ HCL_CODE_CLASS_EXIT = 0xE2, /* 226 */ - HCL_CODE_CLASS_PUSH_EXIT = 0xE3, /* 227 */ + HCL_CODE_CLASS_PEXIT = 0xE3, /* 227 */ HCL_CODE_PUSH_OBJVAR_X = 0xE4, /* 228 ## */ - /* UNUSED - 0xE5 - 0xE7 */ + HCL_CODE_CLASS_SET = 0xE5, /* 229 */ + /* UNUSED - 0xE6 - 0xE7 */ HCL_CODE_STORE_INTO_OBJVAR_X = 0xE8, /* 232 ## */ HCL_CODE_MAKE_ARRAY = 0xE9, /* 233 ## */