From d8113efc281400d95676825d58dbb7ac6ccc6c30 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 5 Sep 2025 22:41:45 +0900 Subject: [PATCH] expanded MAKE_FUNCTION and MAKE_LAMBDA to encode literal frame index to name. expanded hak_context_t to store the name field taken from the compiled block --- lib/bigint.c | 2 +- lib/comp.c | 331 +++++++++++++++++++++++++++++-------------------- lib/decode.c | 48 ++++--- lib/dic.c | 10 +- lib/exec.c | 169 ++++++++++++++----------- lib/fmt.c | 6 +- lib/gc.c | 10 +- lib/hak-prv.h | 5 +- lib/hak.c | 14 +-- lib/hak.h | 10 +- lib/number.c | 6 +- lib/prim.c | 4 +- lib/read.c | 12 +- lib/std.c | 16 +-- lib/sym.c | 2 +- lib/x-client.c | 2 +- lib/x-server.c | 16 +-- lib/xchg.c | 4 +- lib/xma.c | 2 +- src/kernel.hak | 17 ++- 20 files changed, 405 insertions(+), 281 deletions(-) diff --git a/lib/bigint.c b/lib/bigint.c index b6f7e88..725c89f 100644 --- a/lib/bigint.c +++ b/lib/bigint.c @@ -1470,7 +1470,7 @@ static HAK_INLINE void multiply_unsigned_array (const hak_liw_t* x, hak_oow_t xs #if defined(HAK_BUILD_DEBUG) #define CANNOT_KARATSUBA(hak,xs,ys) \ - ((xs) < (hak)->option.karatsuba_cutoff || (ys) < (hak)->option.karatsuba_cutoff || \ + ((xs) <(hak)->option.karatsuba_cutoff || (ys) <(hak)->option.karatsuba_cutoff || \ ((xs) > (ys) && (ys) <= (((xs) + 1) / 2)) || \ ((xs) < (ys) && (xs) <= (((ys) + 1) / 2))) #else diff --git a/lib/comp.c b/lib/comp.c index ca07553..8572d60 100644 --- a/lib/comp.c +++ b/lib/comp.c @@ -602,8 +602,20 @@ ok: static HAK_INLINE int add_literal (hak_t* hak, hak_oop_t obj, hak_oow_t* index) { hak_oow_t lfbase; + int n; + lfbase = (hak->option.trait & HAK_TRAIT_INTERACTIVE)? hak->c->funblk.info[hak->c->funblk.depth].lfbase: 0; - return hak_addliteraltocode(hak, &hak->code, obj, lfbase, index); + n = hak_addliteraltocode(hak, &hak->code, obj, lfbase, index); + if (n <= -1) return -1; + if (*index >= MAX_CODE_PARAM2) + { + /* most literal-related instructions can't handle index higher than MAX_CODE_PARAM2. + * MAKE_FUNCTION uses MAX_CODE_PARAM2 as an indicator that no name literal is specified. + * To be safe, limit the maximum number of index to MAX_CODE_PARAM2 - 1 */ + hak_seterrbfmt(hak, HAK_EFLOOD, "literal frame full - %O\n", obj); + return -1; + } + return 0; } /* ========================================================================= */ @@ -676,7 +688,7 @@ int hak_emitbyteinstruction (hak_t* hak, hak_oob_t bc) return emit_byte_instruction(hak, bc, HAK_NULL); }*/ -static int emit_single_param_instruction (hak_t* hak, int cmd, hak_oow_t param_1, const hak_loc_t* srcloc) +static int emit_one_param_instruction (hak_t* hak, int cmd, hak_oow_t param_1, const hak_loc_t* srcloc) { hak_oob_t bc; @@ -794,7 +806,7 @@ write_long: /* long parameter */ #if (HAK_CODE_LONG_PARAM_SIZE == 2) if (emit_byte_instruction(hak, bc, srcloc) <= -1 || emit_byte_instruction(hak, (param_1 >> 8) & 0xFF, HAK_NULL) <= -1 || - emit_byte_instruction(hak, param_1 & 0xFF, HAK_NULL) <= -1) return -1; + emit_byte_instruction(hak, (param_1 >> 0) & 0xFF, HAK_NULL) <= -1) return -1; #else if (emit_byte_instruction(hak, bc, srcloc) <= -1 || emit_byte_instruction(hak, param_1, HAK_NULL) <= -1) return -1; @@ -821,7 +833,7 @@ write_long2: /* double-long parameter */ return 0; } -static int emit_double_param_instruction (hak_t* hak, int cmd, hak_oow_t param_1, hak_oow_t param_2, const hak_loc_t* srcloc) +static int emit_two_param_instruction (hak_t* hak, int cmd, hak_oow_t param_1, hak_oow_t param_2, const hak_loc_t* srcloc) { hak_oob_t bc; @@ -851,8 +863,8 @@ static int emit_double_param_instruction (hak_t* hak, int cmd, hak_oow_t param_1 * however the instruction format is the same up to the second * parameters between MAKE_FUNCTION and MAKE_BLOCK. */ - case HAK_CODE_MAKE_BLOCK: case HAK_CODE_MAKE_FUNCTION: + case HAK_CODE_MAKE_BLOCK: case HAK_CODE_CALL_R: case HAK_CODE_SEND_R: bc = cmd; @@ -876,9 +888,9 @@ write_long: #if (HAK_CODE_LONG_PARAM_SIZE == 2) if (emit_byte_instruction(hak, bc, srcloc) <= -1 || emit_byte_instruction(hak, (param_1 >> 8) & 0xFF, HAK_NULL) <= -1 || - emit_byte_instruction(hak, param_1 & 0xFF, HAK_NULL) <= -1 || + emit_byte_instruction(hak, (param_1 >> 0) & 0xFF, HAK_NULL) <= -1 || emit_byte_instruction(hak, (param_2 >> 8) & 0xFF, HAK_NULL) <= -1 || - emit_byte_instruction(hak, param_2 & 0xFF, HAK_NULL) <= -1) return -1; + emit_byte_instruction(hak, (param_2 >> 0) & 0xFF, HAK_NULL) <= -1) return -1; #else if (emit_byte_instruction(hak, bc, srcloc) <= -1 || emit_byte_instruction(hak, param_1, HAK_NULL) <= -1 || @@ -889,6 +901,7 @@ write_long: static HAK_INLINE int emit_long_param (hak_t* hak, hak_oow_t param) { + /* generate a long parameter */ if (param > MAX_CODE_PARAM) { hak_seterrnum(hak, HAK_ERANGE); @@ -896,13 +909,37 @@ static HAK_INLINE int emit_long_param (hak_t* hak, hak_oow_t param) } #if (HAK_CODE_LONG_PARAM_SIZE == 2) - return (emit_byte_instruction(hak, param >> 8, HAK_NULL) <= -1 || - emit_byte_instruction(hak, param & 0xFF, HAK_NULL) <= -1)? -1: 0; + /* take up 2 bytes */ + return (emit_byte_instruction(hak, (param >> 8) & 0xFF, HAK_NULL) <= -1 || + emit_byte_instruction(hak, (param >> 0) & 0xFF, HAK_NULL) <= -1)? -1: 0; #else + /* take up 1 byte */ return emit_byte_instruction(hak, param_1, HAK_NULL); #endif } +static HAK_INLINE int emit_extended_long_param (hak_t* hak, hak_oow_t param) +{ + /* generate an extended long parameter */ + if (param > MAX_CODE_PARAM2) + { + hak_seterrnum(hak, HAK_ERANGE); + return -1; + } + +#if (HAK_CODE_LONG_PARAM_SIZE == 2) + /* take up 4 bytes */ + return (emit_byte_instruction(hak, (param >> 24) & 0xFF, HAK_NULL) <= -1 || + emit_byte_instruction(hak, (param >> 16) & 0xFF, HAK_NULL) <= -1 || + emit_byte_instruction(hak, (param >> 8) & 0xFF, HAK_NULL) <= -1 || + emit_byte_instruction(hak, (param >> 0) & 0xFF, HAK_NULL) <= -1)? -1: 0; +#else + /* take up 2 bytes */ + return (emit_byte_instruction(hak, (param >> 8) & 0xFF, HAK_NULL) <= -1 || + emit_byte_instruction(hak, (param >> 0) & 0xFF, HAK_NULL) <= -1)? -1: 0; +#endif +} + static int emit_push_literal (hak_t* hak, hak_oop_t obj, const hak_loc_t* srcloc) { hak_oow_t index; @@ -929,11 +966,11 @@ static int emit_push_literal (hak_t* hak, hak_oop_t obj, const hak_loc_t* srcloc if (i >= 0 && i <= MAX_CODE_PARAM) { - return emit_single_param_instruction(hak, HAK_CODE_PUSH_INTLIT, i, srcloc); + return emit_one_param_instruction(hak, HAK_CODE_PUSH_INTLIT, i, srcloc); } else if (i < 0 && i >= -(hak_ooi_t)MAX_CODE_PARAM) { - return emit_single_param_instruction(hak, HAK_CODE_PUSH_NEGINTLIT, -i, srcloc); + return emit_one_param_instruction(hak, HAK_CODE_PUSH_NEGINTLIT, -i, srcloc); } } else if (HAK_OOP_IS_CHAR(obj)) @@ -943,11 +980,11 @@ static int emit_push_literal (hak_t* hak, hak_oop_t obj, const hak_loc_t* srcloc i = HAK_OOP_TO_CHAR(obj); if (i >= 0 && i <= MAX_CODE_PARAM) - return emit_single_param_instruction(hak, HAK_CODE_PUSH_CHARLIT, i, srcloc); + return emit_one_param_instruction(hak, HAK_CODE_PUSH_CHARLIT, i, srcloc); } if (add_literal(hak, obj, &index) <= -1 || - emit_single_param_instruction(hak, HAK_CODE_PUSH_LITERAL_0, index, srcloc) <= -1) return -1; + emit_one_param_instruction(hak, HAK_CODE_PUSH_LITERAL_0, index, srcloc) <= -1) return -1; return 0; } @@ -984,6 +1021,8 @@ static HAK_INLINE void patch_long_jump (hak_t* hak, hak_ooi_t jip, hak_ooi_t jum static HAK_INLINE void patch_long_param (hak_t* hak, hak_ooi_t ip, hak_oow_t param) { + HAK_ASSERT(hak, param <= MAX_CODE_PARAM); + #if (HAK_CODE_LONG_PARAM_SIZE == 2) patch_instruction(hak, ip, param >> 8); patch_instruction(hak, ip + 1, param & 0xFF); @@ -992,7 +1031,8 @@ static HAK_INLINE void patch_long_param (hak_t* hak, hak_ooi_t ip, hak_oow_t par #endif } -static HAK_INLINE void patch_double_long_params (hak_t* hak, hak_ooi_t ip, hak_oow_t param_1, hak_oow_t param_2) +/* +static HAK_INLINE void patch_two_long_params (hak_t* hak, hak_ooi_t ip, hak_oow_t param_1, hak_oow_t param_2) { #if (HAK_CODE_LONG_PARAM_SIZE == 2) patch_instruction(hak, ip, param_1 >> 8); @@ -1004,9 +1044,12 @@ static HAK_INLINE void patch_double_long_params (hak_t* hak, hak_ooi_t ip, hak_o patch_instruction(hak, ip + 1, param_2); #endif } +*/ -static HAK_INLINE void patch_double_long_params_with_oow (hak_t* hak, hak_ooi_t ip, hak_oow_t param) +static HAK_INLINE void patch_extended_long_param (hak_t* hak, hak_ooi_t ip, hak_oow_t param) { + HAK_ASSERT(hak, param <= MAX_CODE_PARAM2); + #if (HAK_CODE_LONG_PARAM_SIZE == 2) patch_instruction(hak, ip, (param >> 24) & 0xFF); patch_instruction(hak, ip + 1, (param >> 16) & 0xFF); @@ -1031,20 +1074,20 @@ static int emit_variable_access (hak_t* hak, int mode, const hak_var_info_t* vi, switch (vi->type) { case VAR_INDEXED: - return emit_double_param_instruction(hak, inst_map[0][mode], vi->ctx_offset, vi->index_in_ctx, srcloc); + return emit_two_param_instruction(hak, inst_map[0][mode], vi->ctx_offset, vi->index_in_ctx, srcloc); case VAR_INST: HAK_ASSERT(hak, vi->ctx_offset == 0); - return emit_single_param_instruction(hak, inst_map[1][mode], vi->index_in_ctx, srcloc); + return emit_one_param_instruction(hak, inst_map[1][mode], vi->index_in_ctx, srcloc); case VAR_CLASS_I: /* class variable in initialization scope */ HAK_ASSERT(hak, vi->ctx_offset == 0); - return emit_single_param_instruction(hak, inst_map[2][mode], vi->index_in_ctx, srcloc); + return emit_one_param_instruction(hak, inst_map[2][mode], vi->index_in_ctx, srcloc); case VAR_CLASS_CM: /* class variable in class method scope */ case VAR_CLASS_IM: /* class variable in instance method scope */ HAK_ASSERT(hak, vi->ctx_offset == 0); - return emit_single_param_instruction(hak, inst_map[3][mode], vi->index_in_ctx, srcloc); + return emit_one_param_instruction(hak, inst_map[3][mode], vi->index_in_ctx, srcloc); } return -1; @@ -1250,8 +1293,8 @@ static void clear_funblk_inners (hak_t* hak) { hak_funblk_info_t* fbi; fbi = &hak->c->funblk.info[hak->c->funblk.depth]; - while (hak->c->ctlblk.depth > fbi->ctlblk_base) pop_ctlblk (hak); - while (!(fbi->clsblk_base <= -1 && fbi->clsblk_top <= -1)) pop_clsblk (hak); + while (hak->c->ctlblk.depth > fbi->ctlblk_base) pop_ctlblk(hak); + while (!(fbi->clsblk_base <= -1 && fbi->clsblk_top <= -1)) pop_clsblk(hak); } static void pop_funblk (hak_t* hak) @@ -1260,7 +1303,7 @@ static void pop_funblk (hak_t* hak) HAK_ASSERT(hak, hak->c->funblk.depth >= 0); - clear_funblk_inners (hak); + clear_funblk_inners(hak); fbi = &hak->c->funblk.info[hak->c->funblk.depth]; /* if pop_ctlblk() has been called properly, the following assertion must be true * and the assignment on the next line isn't necessary */ @@ -1301,7 +1344,7 @@ static void pop_funblk (hak_t* hak) * and it's split to two intruction parameters when used with MAKE_BLOCK and MAKE_FUNCTION. * the INSTA bit is on if fbi->fun_type == FUN_CIM */ attr_mask = ENCODE_BLK_MASK(((fbi->fun_type & 0xFF) == FUN_CIM), fbi->tmpr_va, fbi->tmpr_nargs, fbi->tmpr_nrvars, fbi->tmpr_nlvars); - patch_double_long_params_with_oow(hak, fbi->make_inst_pos + 1, attr_mask); + patch_extended_long_param(hak, fbi->make_inst_pos + 1, attr_mask); } } @@ -1375,10 +1418,10 @@ static HAK_INLINE void pop_cframe (hak_t* hak) } #define PUSH_CFRAME(hak,opcode,operand) \ - do { if (push_cframe(hak,opcode,operand) <= -1) return -1; } while(0) + do { if (push_cframe(hak,opcode,operand) <= -1) return -1; } while (0) #define INSERT_CFRAME(hak,index,opcode,operand) \ - do { if (insert_cframe(hak,index,opcode,operand) <= -1) return -1; } while(0) + do { if (insert_cframe(hak,index,opcode,operand) <= -1) return -1; } while (0) #define POP_CFRAME(hak) pop_cframe(hak) @@ -1395,14 +1438,14 @@ static HAK_INLINE void pop_cframe (hak_t* hak) hak_cframe_t* _cf = GET_TOP_CFRAME(hak); \ _cf->opcode = _opcode; \ _cf->operand = _operand; \ - } while(0) + } while (0) #define SWITCH_CFRAME(hak,_index,_opcode,_operand) \ do { \ hak_cframe_t* _cf = GET_CFRAME(hak,_index); \ _cf->opcode = _opcode; \ _cf->operand = _operand; \ - } while(0) + } while (0) static int push_subcframe (hak_t* hak, int opcode, hak_cnode_t* operand) { @@ -1435,7 +1478,7 @@ static HAK_INLINE hak_cframe_t* find_cframe_from_top (hak_t* hak, int opcode) } #define PUSH_SUBCFRAME(hak,opcode,operand) \ - do { if (push_subcframe(hak,opcode,operand) <= -1) return -1; } while(0) + do { if (push_subcframe(hak,opcode,operand) <= -1) return -1; } while (0) #define GET_SUBCFRAME(hak) (&hak->c->cfs.ptr[hak->c->cfs.top - 1]) @@ -1805,7 +1848,7 @@ static HAK_INLINE int compile_and_p1 (hak_t* hak) jump_inst_pos = hak->code.bc.len; /* this conditional jump make evaluation short-circuited. the actual jump point is to be patched in compile_and_p2() */ - if (emit_single_param_instruction(hak, HAK_CODE_JUMP_FORWARD_IF_FALSE, MAX_CODE_JUMP, HAK_CNODE_GET_LOC(obj)) <= -1) return -1; + if (emit_one_param_instruction(hak, HAK_CODE_JUMP_FORWARD_IF_FALSE, MAX_CODE_JUMP, HAK_CNODE_GET_LOC(obj)) <= -1) return -1; if (emit_byte_instruction(hak, HAK_CODE_POP_STACKTOP, HAK_CNODE_GET_LOC(obj)) <= -1) return -1; expr = HAK_CNODE_CONS_CAR(obj); @@ -1900,7 +1943,7 @@ static HAK_INLINE int compile_or_p1 (hak_t* hak) jump_inst_pos = hak->code.bc.len; /* this conditional jump makes evaluation short-circuited. the actual jump point is to be patched in compile_or_p2() */ - if (emit_single_param_instruction(hak, HAK_CODE_JUMP_FORWARD_IF_TRUE, MAX_CODE_JUMP, HAK_CNODE_GET_LOC(obj)) <= -1) return -1; + if (emit_one_param_instruction(hak, HAK_CODE_JUMP_FORWARD_IF_TRUE, MAX_CODE_JUMP, HAK_CNODE_GET_LOC(obj)) <= -1) return -1; if (emit_byte_instruction(hak, HAK_CODE_POP_STACKTOP, HAK_CNODE_GET_LOC(obj)) <= -1) return -1; expr = HAK_CNODE_CONS_CAR(obj); @@ -2001,7 +2044,7 @@ static HAK_INLINE int emit_plus (hak_t* hak) if (emit_byte_instruction(hak, HAK_CODE_PLUS, HAK_CNODE_GET_LOC(cf->operand)) <= -1) return -1; - POP_CFRAME (hak); + POP_CFRAME(hak); return 0; } @@ -2078,12 +2121,12 @@ inside_loop: HAK_ASSERT(hak, hak->code.bc.len < HAK_SMOOI_MAX); jump_inst_pos = hak->code.bc.len; - if (emit_single_param_instruction(hak, HAK_CODE_JUMP_FORWARD_0, MAX_CODE_JUMP, HAK_CNODE_GET_LOC(cmd)) <= -1) return -1; + if (emit_one_param_instruction(hak, HAK_CODE_JUMP_FORWARD_0, MAX_CODE_JUMP, HAK_CNODE_GET_LOC(cmd)) <= -1) return -1; INSERT_CFRAME(hak, i, COP_COMPILE_BREAK_P1, cmd); cf = GET_CFRAME(hak, i); cf->u._break.jump_inst_pos = jump_inst_pos; - POP_CFRAME (hak); + POP_CFRAME(hak); return 0; } } @@ -2116,7 +2159,7 @@ static int compile_break_p1 (hak_t* hak) HAK_ASSERT(hak, jump_offset <= MAX_CODE_JUMP * 2); patch_long_jump(hak, jip, jump_offset); - POP_CFRAME (hak); + POP_CFRAME(hak); return 0; } @@ -2182,9 +2225,9 @@ inside_loop: HAK_ASSERT(hak, hak->code.bc.len < HAK_SMOOI_MAX); jump_offset = hak->code.bc.len - tcf->u.post_while.cond_pos + 1; if (jump_offset > 3) jump_offset += HAK_CODE_LONG_PARAM_SIZE; - if (emit_single_param_instruction(hak, HAK_CODE_JUMP_BACKWARD_0, jump_offset, HAK_CNODE_GET_LOC(cmd)) <= -1) return -1; + if (emit_one_param_instruction(hak, HAK_CODE_JUMP_BACKWARD_0, jump_offset, HAK_CNODE_GET_LOC(cmd)) <= -1) return -1; - POP_CFRAME (hak); + POP_CFRAME(hak); return 0; } } @@ -2356,7 +2399,7 @@ static int compile_do_p1 (hak_t* hak) */ kill_temporary_variables(hak, cf->u.post_do.lvar_start, cf->u.post_do.lvar_end); - POP_CFRAME (hak); + POP_CFRAME(hak); return 0; } @@ -2439,7 +2482,7 @@ static HAK_INLINE int patch_nearest_post_if_body (hak_t* hak, hak_cnode_t* cmd) /* emit jump_forward before the beginning of the else block. * this is to make the earlier if or elif block to skip * the else part. it is to be patched in post_else_body(). */ - if (emit_single_param_instruction(hak, HAK_CODE_JUMP_FORWARD_0, MAX_CODE_JUMP, HAK_CNODE_GET_LOC(cf->operand)) <= -1) return -1; + if (emit_one_param_instruction(hak, HAK_CODE_JUMP_FORWARD_0, MAX_CODE_JUMP, HAK_CNODE_GET_LOC(cf->operand)) <= -1) return -1; /* HAK_CODE_LONG_PARAM_SIZE + 1 => size of the long JUMP_FORWARD instruction */ jump_offset = hak->code.bc.len - jip - (HAK_CODE_LONG_PARAM_SIZE + 1); @@ -2869,7 +2912,7 @@ static int compile_class (hak_t* hak, hak_cnode_t* src) * this provides performance advantage at the execution time because * the dictionary doesn't need to be searched for the object. */ if (add_literal(hak, cons, &index) <= -1 || - emit_single_param_instruction(hak, HAK_CODE_PUSH_LITERAL_0, index, HAK_CNODE_GET_LOC(class_name)) <= -1) return -1; + emit_one_param_instruction(hak, HAK_CODE_PUSH_LITERAL_0, index, HAK_CNODE_GET_LOC(class_name)) <= -1) return -1; #endif } else @@ -2877,7 +2920,7 @@ static int compile_class (hak_t* hak, hak_cnode_t* src) /* push nil for class name of an anonymous class */ if (emit_byte_instruction(hak, HAK_CODE_PUSH_NIL, HAK_CNODE_GET_LOC(cmd)) <= -1) return -1; } - POP_CFRAME (hak); + POP_CFRAME(hak); PUSH_CFRAME(hak, COP_COMPILE_CLASS_P2, class_name); /* 3 - use class name for assignment */ cf = GET_TOP_CFRAME(hak); @@ -2931,8 +2974,8 @@ static HAK_INLINE int compile_class_p1 (hak_t* hak) } /* emit placeholder instructions to be patched in compile_class_p2() */ - if (emit_single_param_instruction(hak, HAK_CODE_PUSH_LITERAL_0, MAX_CODE_PARAM2, &cf->u._class.start_loc) <= -1) return -1; - if (emit_single_param_instruction(hak, HAK_CODE_PUSH_LITERAL_0, MAX_CODE_PARAM2, &cf->u._class.start_loc) <= -1) return -1; + if (emit_one_param_instruction(hak, HAK_CODE_PUSH_LITERAL_0, MAX_CODE_PARAM2, &cf->u._class.start_loc) <= -1) return -1; + if (emit_one_param_instruction(hak, HAK_CODE_PUSH_LITERAL_0, MAX_CODE_PARAM2, &cf->u._class.start_loc) <= -1) return -1; if (check_block_expression_as_body(hak, obj, cf->u._class.cmd_cnode, FOR_CLASS) <= -1) return -1; @@ -3021,7 +3064,7 @@ static HAK_INLINE int compile_class_p2 (hak_t* hak) obj = hak_makestring(hak, cbi->ivars.ptr, cbi->ivars.len); if (HAK_UNLIKELY(!obj)) return -1; if (add_literal(hak, obj, &index) <= -1) return -1; - patch_double_long_params_with_oow(hak, patch_pos, index); + patch_extended_long_param(hak, patch_pos, index); } else { @@ -3043,7 +3086,7 @@ static HAK_INLINE int compile_class_p2 (hak_t* hak) obj = hak_makestring(hak, cbi->cvars.ptr, cbi->cvars.len); if (HAK_UNLIKELY(!obj)) return -1; if (add_literal(hak, obj, &index) <= -1) return -1; - patch_double_long_params_with_oow(hak, patch_pos, index); + patch_extended_long_param(hak, patch_pos, index); } else { @@ -3054,8 +3097,8 @@ static HAK_INLINE int compile_class_p2 (hak_t* hak) patch_instruction(hak, patch_pos, HAK_CODE_NOOP); } - pop_ctlblk (hak); - pop_clsblk (hak); /* end of the class block */ + pop_ctlblk(hak); + pop_clsblk(hak); /* end of the class block */ if (emit_byte_instruction(hak, HAK_CODE_CLASS_PEXIT, &class_loc) <= -1) return -1; /* pop + exit */ @@ -3069,7 +3112,7 @@ static HAK_INLINE int compile_class_p2 (hak_t* hak) } else { - POP_CFRAME (hak); + POP_CFRAME(hak); } return 0; @@ -3178,9 +3221,10 @@ static int compile_fun (hak_t* hak, hak_cnode_t* src) { hak_cnode_t* cmd, * next; hak_oow_t va, nargs, nrvars, nlvars; - hak_ooi_t jump_inst_pos, lfbase_pos, lfsize_pos; + hak_ooi_t jump_inst_pos, lfsize_pos; hak_oow_t saved_tv_wcount, tv_dup_start; hak_cnode_t* fun_name; + hak_ooi_t fun_name_lfindex; hak_cnode_t* class_name; hak_cnode_t* attr_list; hak_cnode_t* arg_list; @@ -3596,43 +3640,60 @@ static int compile_fun (hak_t* hak, hak_cnode_t* src) hak, HAK_CNODE_GET_LOC(src), va, nargs, nrvars, nlvars, hak->c->tv.wcount, hak->c->tv.s.len, hak->code.bc.len, hak->code.lit.len, fun_type) <= -1) return -1; + fun_name_lfindex = MAX_CODE_PARAM2; /* this maximum value indicates no function name */ + if (fun_name) + { + hak_oop_t sym; + sym = hak_makesymbol(hak, HAK_CNODE_GET_TOKPTR(fun_name), HAK_CNODE_GET_TOKLEN(fun_name)); + if (HAK_UNLIKELY(!sym)) return -1; + if (add_literal(hak, sym, &fun_name_lfindex) <= -1) return -1; + } + if (hak->option.trait & HAK_TRAIT_INTERACTIVE) { - /* MAKE_FUNCTION attr_mask_1 attr_mask_2 lfbase lfsize */ - if (emit_double_param_instruction(hak, HAK_CODE_MAKE_FUNCTION, 0, 0, HAK_CNODE_GET_LOC(cmd)) <= -1) return -1; - lfbase_pos = hak->code.bc.len; - if (emit_long_param(hak, hak->code.lit.len - hak->c->funblk.info[hak->c->funblk.depth - 1].lfbase) <= -1) return -1; /* lfbase(literal frame base) */ + /* MAKE_FUNCTION attr_mask_1 attr_mask_2 name_lfindex lfbase lfsize */ + hak_oow_t lfbase; + + lfbase = hak->code.lit.len - hak->c->funblk.info[hak->c->funblk.depth - 1].lfbase; + if (lfbase > MAX_CODE_PARAM2) + { + hak_seterrbfmt(hak, HAK_ERANGE, "literal frame base too large"); + return -1; + } + + if (emit_two_param_instruction(hak, HAK_CODE_MAKE_FUNCTION, 0, 0, HAK_CNODE_GET_LOC(cmd)) <= -1) return -1; + if (emit_extended_long_param(hak, fun_name_lfindex) <= -1) return -1; + if (emit_long_param(hak, lfbase) <= -1) return -1; /* lfbase(literal frame base) */ lfsize_pos = hak->code.bc.len; /* literal frame size */ if (emit_long_param(hak, 0) <= -1) return -1; /* place holder for lfsize */ } else { /* MAKE_BLOCK attr_mask_1 attr_mask_2 - will patch attr_mask in pop_funblk() */ - if (emit_double_param_instruction(hak, HAK_CODE_MAKE_BLOCK, 0, 0, HAK_CNODE_GET_LOC(cmd)) <= -1) return -1; + if (emit_two_param_instruction(hak, HAK_CODE_MAKE_BLOCK, 0, 0, HAK_CNODE_GET_LOC(cmd)) <= -1) return -1; + if (emit_extended_long_param(hak, fun_name_lfindex) <= -1) return -1; } HAK_ASSERT(hak, hak->code.bc.len < HAK_SMOOI_MAX); /* guaranteed in emit_byte_instruction() */ jump_inst_pos = hak->code.bc.len; - /* specifying MAX_CODE_JUMP causes emit_single_param_instruction() to + /* specifying MAX_CODE_JUMP causes emit_one_param_instruction() to * produce the long jump instruction (HAK_CODE_JUMP_FORWARD_X) */ - if (emit_single_param_instruction(hak, HAK_CODE_JUMP_FORWARD_0, MAX_CODE_JUMP, HAK_CNODE_GET_LOC(cmd)) <= -1) return -1; + if (emit_one_param_instruction(hak, HAK_CODE_JUMP_FORWARD_0, MAX_CODE_JUMP, HAK_CNODE_GET_LOC(cmd)) <= -1) return -1; SWITCH_TOP_CFRAME(hak, COP_COMPILE_OBJECT_LIST, fun_body); /* 1 */ PUSH_SUBCFRAME(hak, COP_POST_FUN, fun_name); /* 3*/ cf = GET_SUBCFRAME(hak); cf->u.fun.fun_type = fun_type; cf->u.fun.class_name = class_name; + cf->u.fun.jump_inst_pos = jump_inst_pos; + if (hak->option.trait & HAK_TRAIT_INTERACTIVE) cf->u.fun.lfsize_pos = lfsize_pos; PUSH_SUBCFRAME(hak, COP_EMIT_FUN, src); /* 2 */ cf = GET_SUBCFRAME(hak); cf->u.fun.fun_type = fun_type; + cf->u.fun.class_name = class_name; cf->u.fun.jump_inst_pos = jump_inst_pos; - - if (hak->option.trait & HAK_TRAIT_INTERACTIVE) - { - cf->u.fun.lfbase_pos = lfbase_pos; - cf->u.fun.lfsize_pos = lfsize_pos; - } + if (hak->option.trait & HAK_TRAIT_INTERACTIVE) cf->u.fun.lfsize_pos = lfsize_pos; return 0; } @@ -3843,7 +3904,7 @@ static int compile_var (hak_t* hak, hak_cnode_t* src) * remove generating this instruction after having fixed the problem in that function */ if (emit_byte_instruction(hak, HAK_CODE_PUSH_NIL, HAK_CNODE_GET_LOC(cmd)) <= -1) return -1; - POP_CFRAME (hak); + POP_CFRAME(hak); return 0; @@ -3905,7 +3966,7 @@ static int compile_return (hak_t* hak, hak_cnode_t* src, int ret_from_home) /* TODO: pop stack if this is not the first statement... */ if (emit_byte_instruction(hak, HAK_CODE_PUSH_RETURN_R, HAK_CNODE_GET_LOC(tmp)) <= -1) return -1; - POP_CFRAME (hak); + POP_CFRAME(hak); } else { @@ -4193,7 +4254,7 @@ static int compile_try (hak_t* hak, hak_cnode_t* src) /* TODO: HAK_TRAIT_INTERACTIVE??? */ jump_inst_pos = hak->code.bc.len; - if (emit_single_param_instruction(hak, HAK_CODE_TRY_ENTER, MAX_CODE_JUMP, HAK_CNODE_GET_LOC(cmd)) <= -1) return -1; + if (emit_one_param_instruction(hak, HAK_CODE_TRY_ENTER, MAX_CODE_JUMP, HAK_CNODE_GET_LOC(cmd)) <= -1) return -1; if (check_block_expression_as_body(hak, obj, cmd, FOR_TRY) <= -1) return -1; @@ -4230,7 +4291,7 @@ static HAK_INLINE int patch_nearest_post_try (hak_t* hak, hak_ooi_t* catch_skip_ if (emit_byte_instruction(hak, HAK_CODE_TRY_EXIT, HAK_CNODE_GET_LOC(cf->operand)) <= -1) return -1; *catch_skip_jip = hak->code.bc.len; - if (emit_single_param_instruction(hak, HAK_CODE_JUMP_FORWARD_0, MAX_CODE_JUMP, HAK_CNODE_GET_LOC(cf->operand)) <= -1) return -1; + if (emit_one_param_instruction(hak, HAK_CODE_JUMP_FORWARD_0, MAX_CODE_JUMP, HAK_CNODE_GET_LOC(cf->operand)) <= -1) return -1; /* HAK_CODE_LONG_PARAM_SIZE + 1 => size of the long JUMP_FORWARD instruction */ block_code_size = hak->code.bc.len - jip - (HAK_CODE_LONG_PARAM_SIZE + 1); @@ -4355,8 +4416,8 @@ static HAK_INLINE int compile_catch (hak_t* hak) static HAK_INLINE int post_try (hak_t* hak) { /* TODO: anything else? */ - pop_ctlblk (hak); - POP_CFRAME (hak); + pop_ctlblk(hak); + POP_CFRAME(hak); return 0; } @@ -4395,7 +4456,7 @@ static HAK_INLINE int post_catch (hak_t* hak) * the variable entity is still be accounted into the local variable list. */ kill_temporary_variable_at_offset(hak, cf->u.post_catch.exarg_offset); - POP_CFRAME (hak); + POP_CFRAME(hak); return 0; } @@ -5068,7 +5129,7 @@ static HAK_INLINE int compile_symbol (hak_t* hak, hak_cnode_t* obj) /* add the entire cons pair to the literal frame */ if (add_literal(hak, cons, &index) <= -1 || - emit_single_param_instruction(hak, HAK_CODE_PUSH_OBJECT_0, index, HAK_CNODE_GET_LOC(obj)) <= -1) return -1; + emit_one_param_instruction(hak, HAK_CODE_PUSH_OBJECT_0, index, HAK_CNODE_GET_LOC(obj)) <= -1) return -1; return 0; } @@ -5194,17 +5255,17 @@ static HAK_INLINE int compile_dsymbol (hak_t* hak, hak_cnode_t* obj) break; default: - hak_popvolat (hak); + hak_popvolat(hak); hak_seterrbfmt(hak, HAK_EINVAL, "invalid pfbase type - %d\n", pfbase->type); return -1; } if (!val || !(cons = (hak_oop_t)hak_putatsysdic(hak, sym, val))) { - hak_popvolat (hak); + hak_popvolat(hak); return -1; } - hak_popvolat (hak); + hak_popvolat(hak); /* make this dotted symbol special that it can't get changed * to a different value */ @@ -5212,7 +5273,7 @@ static HAK_INLINE int compile_dsymbol (hak_t* hak, hak_cnode_t* obj) } if (add_literal(hak, cons, &index) <= -1 || - emit_single_param_instruction(hak, HAK_CODE_PUSH_OBJECT_0, index, HAK_CNODE_GET_LOC(obj)) <= -1) return -1; + emit_one_param_instruction(hak, HAK_CODE_PUSH_OBJECT_0, index, HAK_CNODE_GET_LOC(obj)) <= -1) return -1; return 0; } @@ -5436,7 +5497,7 @@ static int compile_symbol_literal (hak_t* hak) if (HAK_UNLIKELY(!lit)) return -1; if (emit_push_literal(hak, lit, HAK_CNODE_GET_LOC(oprnd)) <= -1) return -1; - POP_CFRAME (hak); + POP_CFRAME(hak); return 0; } @@ -5611,19 +5672,19 @@ redo: break; case HAK_CONCODE_ARRAY: - if (emit_single_param_instruction(hak, HAK_CODE_MAKE_ARRAY, 0, HAK_CNODE_GET_LOC(oprnd)) <= -1) return -1; + if (emit_one_param_instruction(hak, HAK_CODE_MAKE_ARRAY, 0, HAK_CNODE_GET_LOC(oprnd)) <= -1) return -1; goto done; case HAK_CONCODE_BYTEARRAY: - if (emit_single_param_instruction(hak, HAK_CODE_MAKE_BYTEARRAY, 0, HAK_CNODE_GET_LOC(oprnd)) <= -1) return -1; + if (emit_one_param_instruction(hak, HAK_CODE_MAKE_BYTEARRAY, 0, HAK_CNODE_GET_LOC(oprnd)) <= -1) return -1; goto done; case HAK_CONCODE_CHARARRAY: - if (emit_single_param_instruction(hak, HAK_CODE_MAKE_CHARARRAY, 0, HAK_CNODE_GET_LOC(oprnd)) <= -1) return -1; + if (emit_one_param_instruction(hak, HAK_CODE_MAKE_CHARARRAY, 0, HAK_CNODE_GET_LOC(oprnd)) <= -1) return -1; goto done; case HAK_CONCODE_DIC: - if (emit_single_param_instruction(hak, HAK_CODE_MAKE_DIC, 16, HAK_CNODE_GET_LOC(oprnd)) <= -1) return -1; + if (emit_one_param_instruction(hak, HAK_CODE_MAKE_DIC, 16, HAK_CNODE_GET_LOC(oprnd)) <= -1) return -1; goto done; case HAK_CONCODE_QLIST: @@ -5675,7 +5736,7 @@ literal: if (emit_push_literal(hak, lit, HAK_CNODE_GET_LOC(oprnd)) <= -1) return -1; done: - POP_CFRAME (hak); + POP_CFRAME(hak); return 0; } @@ -5739,7 +5800,7 @@ static int compile_object_list (hak_t* hak) if (!oprnd) { - POP_CFRAME (hak); + POP_CFRAME(hak); } else { @@ -5853,7 +5914,7 @@ static int compile_array_list (hak_t* hak) if (!oprnd) { - POP_CFRAME (hak); + POP_CFRAME(hak); } else { @@ -5899,7 +5960,7 @@ static int compile_pure_array_list (hak_t* hak) if (!oprnd) { - POP_CFRAME (hak); + POP_CFRAME(hak); } else { @@ -5953,7 +6014,7 @@ static int compile_dic_list (hak_t* hak) if (!oprnd) { - POP_CFRAME (hak); + POP_CFRAME(hak); } else { @@ -6002,7 +6063,7 @@ static int compile_qlist (hak_t* hak) if (!oprnd) { - POP_CFRAME (hak); + POP_CFRAME(hak); } else { @@ -6056,7 +6117,7 @@ static HAK_INLINE int post_if_cond (hak_t* hak) HAK_ASSERT(hak, hak->code.bc.len < HAK_SMOOI_MAX); jump_inst_pos = hak->code.bc.len; - if (emit_single_param_instruction(hak, HAK_CODE_JUMP_FORWARD_IF_FALSE, MAX_CODE_JUMP, &cf->u.post_if.start_loc) <= -1) return -1; + if (emit_one_param_instruction(hak, HAK_CODE_JUMP_FORWARD_IF_FALSE, MAX_CODE_JUMP, &cf->u.post_if.start_loc) <= -1) return -1; /* to drop the result of the conditional when it is true */ if (emit_byte_instruction(hak, HAK_CODE_POP_STACKTOP, &cf->u.post_if.start_loc) <= -1) return -1; @@ -6110,7 +6171,7 @@ static HAK_INLINE int post_if_body (hak_t* hak) } patch_long_jump(hak, jip, jump_offset); - POP_CFRAME (hak); + POP_CFRAME(hak); return 0; } @@ -6150,7 +6211,7 @@ static HAK_INLINE int post_while_cond (hak_t* hak) next_cop = COP_POST_WHILE_BODY; } - if (emit_single_param_instruction(hak, jump_inst, MAX_CODE_JUMP, HAK_CNODE_GET_LOC(cond)) <= -1) return -1; + if (emit_one_param_instruction(hak, jump_inst, MAX_CODE_JUMP, HAK_CNODE_GET_LOC(cond)) <= -1) return -1; if (emit_byte_instruction(hak, HAK_CODE_POP_STACKTOP, HAK_CNODE_GET_LOC(cond)) <= -1) return -1; HAK_ASSERT(hak, hak->code.bc.len < HAK_SMOOI_MAX); @@ -6204,7 +6265,7 @@ static HAK_INLINE int post_while_body (hak_t* hak) HAK_ASSERT(hak, hak->code.bc.len < HAK_SMOOI_MAX); jump_offset = hak->code.bc.len - cf->u.post_while.cond_pos + 1; if (jump_offset > 3) jump_offset += HAK_CODE_LONG_PARAM_SIZE; - if (emit_single_param_instruction(hak, HAK_CODE_JUMP_BACKWARD_0, jump_offset, HAK_CNODE_GET_LOC(cf->operand)) <= -1) return -1; + if (emit_one_param_instruction(hak, HAK_CODE_JUMP_BACKWARD_0, jump_offset, HAK_CNODE_GET_LOC(cf->operand)) <= -1) return -1; jip = cf->u.post_while.jump_inst_pos; /* HAK_CODE_LONG_PARAM_SIZE + 1 => size of the long JUMP_FORWARD_IF_FALSE/JUMP_FORWARD_IF_TRUE instruction */ @@ -6216,10 +6277,10 @@ static HAK_INLINE int post_while_body (hak_t* hak) } patch_long_jump(hak, jip, jump_offset); - POP_CFRAME (hak); + POP_CFRAME(hak); HAK_ASSERT(hak, hak->c->ctlblk.info[hak->c->ctlblk.depth]._type == HAK_CTLBLK_TYPE_LOOP); - pop_ctlblk (hak); + pop_ctlblk(hak); return 0; } @@ -6238,14 +6299,14 @@ static HAK_INLINE int emit_call (hak_t* hak) if (cf->u.call.nrets > 0) { - n = emit_double_param_instruction(hak, HAK_CODE_CALL_R, cf->u.call.index, cf->u.call.nrets, HAK_CNODE_GET_LOC(cf->operand)); + n = emit_two_param_instruction(hak, HAK_CODE_CALL_R, cf->u.call.index, cf->u.call.nrets, HAK_CNODE_GET_LOC(cf->operand)); } else { - n = emit_single_param_instruction(hak, HAK_CODE_CALL_0, cf->u.call.index, HAK_CNODE_GET_LOC(cf->operand)); + n = emit_one_param_instruction(hak, HAK_CODE_CALL_0, cf->u.call.index, HAK_CNODE_GET_LOC(cf->operand)); } - POP_CFRAME (hak); + POP_CFRAME(hak); return n; } @@ -6259,7 +6320,7 @@ static HAK_INLINE int emit_push_nil (hak_t* hak) HAK_ASSERT(hak, cf->operand != HAK_NULL); n = emit_byte_instruction(hak, HAK_CODE_PUSH_NIL, HAK_CNODE_GET_LOC(cf->operand)); - POP_CFRAME (hak); + POP_CFRAME(hak); return n; } @@ -6276,7 +6337,7 @@ static HAK_INLINE int emit_push_symbol (hak_t* hak) if (HAK_UNLIKELY(!lit)) return -1; if (emit_push_literal(hak, lit, HAK_CNODE_GET_LOC(cf->operand)) <= -1) return -1; - POP_CFRAME (hak); + POP_CFRAME(hak); return 0; } @@ -6291,14 +6352,14 @@ static HAK_INLINE int emit_send (hak_t* hak) if (cf->u.sendmsg.nrets > 0) { - n = emit_double_param_instruction(hak, (cf->u.sendmsg.to_super? HAK_CODE_SEND_TO_SUPER_R: HAK_CODE_SEND_R), cf->u.sendmsg.nargs, cf->u.sendmsg.nrets, HAK_CNODE_GET_LOC(cf->operand)); + n = emit_two_param_instruction(hak, (cf->u.sendmsg.to_super? HAK_CODE_SEND_TO_SUPER_R: HAK_CODE_SEND_R), cf->u.sendmsg.nargs, cf->u.sendmsg.nrets, HAK_CNODE_GET_LOC(cf->operand)); } else { - n = emit_single_param_instruction(hak, (cf->u.sendmsg.to_super? HAK_CODE_SEND_TO_SUPER_0: HAK_CODE_SEND_0), cf->u.sendmsg.nargs, HAK_CNODE_GET_LOC(cf->operand)); + n = emit_one_param_instruction(hak, (cf->u.sendmsg.to_super? HAK_CODE_SEND_TO_SUPER_0: HAK_CODE_SEND_0), cf->u.sendmsg.nargs, HAK_CNODE_GET_LOC(cf->operand)); } - POP_CFRAME (hak); + POP_CFRAME(hak); return n; } @@ -6313,9 +6374,9 @@ static HAK_INLINE int emit_make_array (hak_t* hak) HAK_ASSERT(hak, cf->opcode == COP_EMIT_MAKE_ARRAY); HAK_ASSERT(hak, cf->operand != HAK_NULL); - n = emit_single_param_instruction(hak, HAK_CODE_MAKE_ARRAY, cf->u.array_list.index, HAK_CNODE_GET_LOC(cf->operand)); + n = emit_one_param_instruction(hak, HAK_CODE_MAKE_ARRAY, cf->u.array_list.index, HAK_CNODE_GET_LOC(cf->operand)); - POP_CFRAME (hak); + POP_CFRAME(hak); return n; } @@ -6329,9 +6390,9 @@ static HAK_INLINE int emit_make_pure_array (hak_t* hak) HAK_ASSERT(hak, cf->operand != HAK_NULL); inst = (cf->u.pure_array_list.elem_type == HAK_CONCODE_BYTEARRAY)? HAK_CODE_MAKE_BYTEARRAY: HAK_CODE_MAKE_CHARARRAY; - n = emit_single_param_instruction(hak, inst, cf->u.pure_array_list.index, HAK_CNODE_GET_LOC(cf->operand)); + n = emit_one_param_instruction(hak, inst, cf->u.pure_array_list.index, HAK_CNODE_GET_LOC(cf->operand)); - POP_CFRAME (hak); + POP_CFRAME(hak); return n; } @@ -6344,9 +6405,9 @@ static HAK_INLINE int emit_make_dic (hak_t* hak) HAK_ASSERT(hak, cf->opcode == COP_EMIT_MAKE_DIC); HAK_ASSERT(hak, cf->operand != HAK_NULL); - n = emit_single_param_instruction(hak, HAK_CODE_MAKE_DIC, cf->u.dic_list.index, HAK_CNODE_GET_LOC(cf->operand)); + n = emit_one_param_instruction(hak, HAK_CODE_MAKE_DIC, cf->u.dic_list.index, HAK_CNODE_GET_LOC(cf->operand)); - POP_CFRAME (hak); + POP_CFRAME(hak); return n; } @@ -6361,7 +6422,7 @@ static HAK_INLINE int emit_make_cons (hak_t* hak) n = emit_byte_instruction(hak, HAK_CODE_MAKE_CONS, HAK_CNODE_GET_LOC(cf->operand)); - POP_CFRAME (hak); + POP_CFRAME(hak); return n; } @@ -6374,9 +6435,9 @@ static HAK_INLINE int emit_pop_into_array (hak_t* hak) HAK_ASSERT(hak, cf->opcode == COP_EMIT_POP_INTO_ARRAY); HAK_ASSERT(hak, cf->operand != HAK_NULL); - n = emit_single_param_instruction(hak, HAK_CODE_POP_INTO_ARRAY, cf->u.array_list.index, HAK_CNODE_GET_LOC(cf->operand)); + n = emit_one_param_instruction(hak, HAK_CODE_POP_INTO_ARRAY, cf->u.array_list.index, HAK_CNODE_GET_LOC(cf->operand)); - POP_CFRAME (hak); + POP_CFRAME(hak); return n; } @@ -6390,9 +6451,9 @@ static HAK_INLINE int emit_pop_into_pure_array (hak_t* hak) HAK_ASSERT(hak, cf->operand != HAK_NULL); inst = (cf->u.pure_array_list.elem_type == HAK_CONCODE_BYTEARRAY)? HAK_CODE_POP_INTO_BYTEARRAY: HAK_CODE_POP_INTO_CHARARRAY; - n = emit_single_param_instruction(hak, inst, cf->u.pure_array_list.index, HAK_CNODE_GET_LOC(cf->operand)); + n = emit_one_param_instruction(hak, inst, cf->u.pure_array_list.index, HAK_CNODE_GET_LOC(cf->operand)); - POP_CFRAME (hak); + POP_CFRAME(hak); return n; } @@ -6407,7 +6468,7 @@ static HAK_INLINE int emit_pop_into_dic (hak_t* hak) n = emit_byte_instruction(hak, HAK_CODE_POP_INTO_DIC, HAK_CNODE_GET_LOC(cf->operand)); - POP_CFRAME (hak); + POP_CFRAME(hak); return n; } @@ -6424,7 +6485,7 @@ static HAK_INLINE int emit_pop_into_cons (hak_t* hak, int cmd) n = emit_byte_instruction(hak, cmd, HAK_CNODE_GET_LOC(cf->operand)); - POP_CFRAME (hak); + POP_CFRAME(hak); return n; } @@ -6519,7 +6580,7 @@ static HAK_INLINE int emit_fun (hak_t* hak) if (hak->option.trait & HAK_TRAIT_INTERACTIVE) patch_long_param(hak, cf->u.fun.lfsize_pos, lfsize); - POP_CFRAME (hak); + POP_CFRAME(hak); return 0; } @@ -6533,7 +6594,7 @@ static HAK_INLINE int post_fun (hak_t* hak) /*hak->c->funblk.depth--; hak->c->tv.s.len = hak->c->funblk.info[hak->c->funblk.depth].tmprlen; hak->c->tv.wcount = hak->c->funblk.info[hak->c->funblk.depth].tmprcnt;*/ - pop_funblk (hak); + pop_funblk(hak); if (cf->operand) { @@ -6659,9 +6720,9 @@ static HAK_INLINE int post_fun (hak_t* hak) return -1; } - if (emit_single_param_instruction(hak, inst, index, HAK_CNODE_GET_LOC(fun_name)) <= -1) return -1; + if (emit_one_param_instruction(hak, inst, index, HAK_CNODE_GET_LOC(fun_name)) <= -1) return -1; if (emit_byte_instruction(hak, HAK_CODE_CLASS_EXIT, HAK_CNODE_GET_LOC(class_name)) <= -1) return -1; - POP_CFRAME (hak); + POP_CFRAME(hak); } else { @@ -6676,7 +6737,7 @@ static HAK_INLINE int post_fun (hak_t* hak) } else { - POP_CFRAME (hak); + POP_CFRAME(hak); } return 0; @@ -6695,7 +6756,7 @@ static HAK_INLINE int emit_pop_stacktop (hak_t* hak) n = emit_byte_instruction(hak, HAK_CODE_POP_STACKTOP, HAK_CNODE_GET_LOC(cf->operand)); - POP_CFRAME (hak); + POP_CFRAME(hak); return n; } @@ -6710,7 +6771,7 @@ static HAK_INLINE int emit_return (hak_t* hak) n = emit_byte_instruction(hak, (cf->u._return.from_home? HAK_CODE_RETURN_STACKTOP: HAK_CODE_RETURN_FROM_BLOCK), HAK_CNODE_GET_LOC(cf->operand)); - POP_CFRAME (hak); + POP_CFRAME(hak); return n; } @@ -6740,7 +6801,7 @@ static HAK_INLINE int emit_set (hak_t* hak) } if (add_literal(hak, cons, &index) <= -1) return -1; - if (emit_single_param_instruction(hak, (cf->u.set.mode == VAR_ACCESS_POP? HAK_CODE_POP_INTO_OBJECT_0: HAK_CODE_STORE_INTO_OBJECT_0), index, HAK_CNODE_GET_LOC(cf->operand)) <= -1) return -1; + if (emit_one_param_instruction(hak, (cf->u.set.mode == VAR_ACCESS_POP? HAK_CODE_POP_INTO_OBJECT_0: HAK_CODE_STORE_INTO_OBJECT_0), index, HAK_CNODE_GET_LOC(cf->operand)) <= -1) return -1; } else { @@ -6748,7 +6809,7 @@ static HAK_INLINE int emit_set (hak_t* hak) if (emit_variable_access(hak, cf->u.set.mode, &cf->u.set.vi, HAK_CNODE_GET_LOC(cf->operand)) <= -1) return -1; } - POP_CFRAME (hak); + POP_CFRAME(hak); return 0; } @@ -6765,9 +6826,9 @@ static HAK_INLINE int emit_class_cmstore (hak_t* hak) if (HAK_UNLIKELY(!lit)) return -1; if (add_literal(hak, lit, &index) <= -1) return -1; - if (emit_single_param_instruction(hak, HAK_CODE_CLASS_CMSTORE, index, HAK_CNODE_GET_LOC(cf->operand)) <= -1) return -1; + if (emit_one_param_instruction(hak, HAK_CODE_CLASS_CMSTORE, index, HAK_CNODE_GET_LOC(cf->operand)) <= -1) return -1; - POP_CFRAME (hak); + POP_CFRAME(hak); return 0; } @@ -6784,9 +6845,9 @@ static HAK_INLINE int emit_class_cimstore (hak_t* hak) if (HAK_UNLIKELY(!lit)) return -1; if (add_literal(hak, lit, &index) <= -1) return -1; - if (emit_single_param_instruction(hak, HAK_CODE_CLASS_CIMSTORE, index, HAK_CNODE_GET_LOC(cf->operand)) <= -1) return -1; + if (emit_one_param_instruction(hak, HAK_CODE_CLASS_CIMSTORE, index, HAK_CNODE_GET_LOC(cf->operand)) <= -1) return -1; - POP_CFRAME (hak); + POP_CFRAME(hak); return 0; } @@ -6803,9 +6864,9 @@ static HAK_INLINE int emit_class_imstore (hak_t* hak) if (HAK_UNLIKELY(!lit)) return -1; if (add_literal(hak, lit, &index) <= -1) return -1; - if (emit_single_param_instruction(hak, HAK_CODE_CLASS_IMSTORE, index, HAK_CNODE_GET_LOC(cf->operand)) <= -1) return -1; + if (emit_one_param_instruction(hak, HAK_CODE_CLASS_IMSTORE, index, HAK_CNODE_GET_LOC(cf->operand)) <= -1) return -1; - POP_CFRAME (hak); + POP_CFRAME(hak); return 0; } @@ -6820,7 +6881,7 @@ static HAK_INLINE int emit_throw (hak_t* hak) n = emit_byte_instruction(hak, HAK_CODE_THROW, HAK_CNODE_GET_LOC(cf->operand)); - POP_CFRAME (hak); + POP_CFRAME(hak); return n; } @@ -6866,11 +6927,11 @@ int hak_compile (hak_t* hak, hak_cnode_t* obj, int flags) * in the interactive mode, the information doesn't have * to get carried over. */ - while (hak->c->funblk.depth >= 0) pop_funblk (hak); + while (hak->c->funblk.depth >= 0) pop_funblk(hak); HAK_ASSERT(hak, hak->c->funblk.depth == -1); /* it will be recreated below */ } - if (flags & HAK_COMPILE_CLEAR_CODE) hak_clearcode (hak); + if (flags & HAK_COMPILE_CLEAR_CODE) hak_clearcode(hak); saved_bc_len = hak->code.bc.len; saved_lit_len = hak->code.lit.len; @@ -6991,7 +7052,7 @@ int hak_compile (hak_t* hak, hak_cnode_t* obj, int flags) break; case COP_COMPILE_ELIF: - if (compile_elif(hak) <= -1) goto oops; + if (compile_elif (hak) <= -1) goto oops; break; case COP_COMPILE_ELSE: @@ -7174,7 +7235,7 @@ int hak_compile (hak_t* hak, hak_cnode_t* obj, int flags) hak->code.ngtmprs = hak->c->funblk.info[0].tmprcnt; /* populate the number of global temporary variables */ #if defined(CLEAR_FUNBLK_ALWAYS) - pop_funblk (hak); + pop_funblk(hak); HAK_ASSERT(hak, hak->c->tv.s.len == 0); HAK_ASSERT(hak, hak->c->tv.wcount == 0); #endif @@ -7183,7 +7244,7 @@ int hak_compile (hak_t* hak, hak_cnode_t* obj, int flags) return 0; oops: - POP_ALL_CFRAMES (hak); + POP_ALL_CFRAMES(hak); hak->log.default_type_mask = log_default_type_mask; @@ -7191,12 +7252,12 @@ oops: hak->code.bc.len = saved_bc_len; hak->code.lit.len = saved_lit_len; - while (hak->c->funblk.depth > 0) pop_funblk (hak); + while (hak->c->funblk.depth > 0) pop_funblk(hak); HAK_ASSERT(hak, hak->c->funblk.depth == 0); if (top_funblk_pushed_here) { - pop_funblk (hak); + pop_funblk(hak); HAK_ASSERT(hak, hak->c->funblk.depth == -1); HAK_ASSERT(hak, hak->c->tv.s.len == 0); HAK_ASSERT(hak, hak->c->tv.wcount == 0); @@ -7213,7 +7274,7 @@ oops: */ /* restore the top level function block as it's first captured in this function */ - clear_funblk_inners (hak); + clear_funblk_inners(hak); HAK_ASSERT(hak, hak->c->funblk.depth == 0); hak->c->funblk.info[0] = top_funblk_saved; hak->c->tv.s.len = top_funblk_saved.tmprlen; diff --git a/lib/decode.c b/lib/decode.c index 1499979..225ddee 100644 --- a/lib/decode.c +++ b/lib/decode.c @@ -34,6 +34,7 @@ #define LOG_INST_5(hak,fmt,a1,a2,a3,a4,a5) HAK_LOG6(hak, DECODE_LOG_MASK, " %06zd " fmt "\n", fetched_instruction_pointer, a1, a2, a3, a4, a5) #define LOG_INST_6(hak,fmt,a1,a2,a3,a4,a5,a6) HAK_LOG7(hak, DECODE_LOG_MASK, " %06zd " fmt "\n", fetched_instruction_pointer, a1, a2, a3, a4, a5, a6) #define LOG_INST_7(hak,fmt,a1,a2,a3,a4,a5,a6,a7) HAK_LOG8(hak, DECODE_LOG_MASK, " %06zd " fmt "\n", fetched_instruction_pointer, a1, a2, a3, a4, a5, a6, a7) +#define LOG_INST_8(hak,fmt,a1,a2,a3,a4,a5,a6,a7,a8) HAK_LOG9(hak, DECODE_LOG_MASK, " %06zd " fmt "\n", fetched_instruction_pointer, a1, a2, a3, a4, a5, a6, a7, a8) #define FETCH_BYTE_CODE(hak) (cdptr[ip++]) @@ -703,45 +704,62 @@ int hak_decode (hak_t* hak, const hak_code_t* code, hak_oow_t start, hak_oow_t e case HAK_CODE_MAKE_FUNCTION: { - hak_oow_t b3, b4; - /* b1 - block mask - * b2 - block mask - * b3 - base literal frame start - * b4 - base literal frame end */ + hak_oow_t b3, b4, x; + /* - block mask(extended long) + * - literal frame index to the name symbol(extended long) + * - base literal frame start + * - base literal frame end */ FETCH_PARAM_CODE_TO(hak, b1); + FETCH_PARAM_CODE_TO(hak, x); + b1 = (b1 << (8 * HAK_CODE_LONG_PARAM_SIZE)) | x; + + /* lfindex to name is an extended long parameter */ FETCH_PARAM_CODE_TO(hak, b2); + FETCH_PARAM_CODE_TO(hak, x); + b2 = (b2 << (8 * HAK_CODE_LONG_PARAM_SIZE)) | x; + FETCH_PARAM_CODE_TO(hak, b3); FETCH_PARAM_CODE_TO(hak, b4); - b1 = (b1 << (8 * HAK_CODE_LONG_PARAM_SIZE)) | b2; - LOG_INST_7 (hak, "make_function %zu %zu %zu %zu %zu %zu %zu", + LOG_INST_8 (hak, "make_function %zu %zu %zu %zu %zu %zu %zu %zu", GET_BLK_MASK_INSTA(b1), GET_BLK_MASK_VA(b1), GET_BLK_MASK_NARGS(b1), GET_BLK_MASK_NRVARS(b1), GET_BLK_MASK_NLVARS(b1), - b3, b4); + b2, b3, b4); HAK_ASSERT(hak, b1 >= 0); break; } case HAK_CODE_MAKE_BLOCK: - /* b1 - block mask - * b2 - block mask */ - FETCH_PARAM_CODE_TO(hak, b1); - FETCH_PARAM_CODE_TO(hak, b2); - b1 = (b1 << (8 * HAK_CODE_LONG_PARAM_SIZE)) | b2; + { + hak_oow_t x; - LOG_INST_5 (hak, "make_block %zu %zu %zu %zu %zu", + /* block mask (extended long) + * literal frame index to the name symbol(extended long) + */ + FETCH_PARAM_CODE_TO(hak, b1); + FETCH_PARAM_CODE_TO(hak, x); + b1 = (b1 << (8 * HAK_CODE_LONG_PARAM_SIZE)) | x; + + /* lfindex to name is an extended long parameter */ + FETCH_PARAM_CODE_TO(hak, b2); + FETCH_PARAM_CODE_TO(hak, x); + b2 = (b2 << (8 * HAK_CODE_LONG_PARAM_SIZE)) | x; + + LOG_INST_6 (hak, "make_block %zu %zu %zu %zu %zu %zu", GET_BLK_MASK_INSTA(b1), GET_BLK_MASK_VA(b1), GET_BLK_MASK_NARGS(b1), GET_BLK_MASK_NRVARS(b1), - GET_BLK_MASK_NLVARS(b1)); + GET_BLK_MASK_NLVARS(b1), + b2); HAK_ASSERT(hak, b1 >= 0); break; + } case HAK_CODE_NOOP: /* do nothing */ diff --git a/lib/dic.c b/lib/dic.c index 7c1bd13..e50216b 100644 --- a/lib/dic.c +++ b/lib/dic.c @@ -66,7 +66,7 @@ static hak_oop_oop_t expand_bucket (hak_t* hak, hak_oop_oop_t oldbuc) hak_pushvolat(hak, (hak_oop_t*)&oldbuc); newbuc = (hak_oop_oop_t)hak_makearray(hak, newsz); - hak_popvolat (hak); + hak_popvolat(hak); if (!newbuc) return HAK_NULL; while (oldsz > 0) @@ -224,7 +224,7 @@ static hak_oop_cons_t find_or_upsert (hak_t* hak, hak_oop_dic_t dic, hak_oop_t k HAK_ASSERT(hak, HAK_IS_COMPILED_BLOCK(hak, value)); hak_pushvolat(hak, &key); pair = hak_makecons(hak, (is_method & 1? value: hak->_nil), (is_method & 2? value: hak->_nil)); - hak_popvolat (hak); + hak_popvolat(hak); if (HAK_UNLIKELY(!pair)) goto oops; value = pair; } @@ -468,7 +468,7 @@ hak_oop_t hak_makedic (hak_t* hak, hak_oow_t inisize) hak_pushvolat(hak, (hak_oop_t*)&obj); bucket = (hak_oop_oop_t)hak_makearray(hak, inisize); - hak_popvolat (hak); + hak_popvolat(hak); if (!bucket) obj = HAK_NULL; else obj->bucket = bucket; @@ -493,7 +493,7 @@ hak_oop_t hak_makedic (hak_t* hak, hak_oow_t inisize) hak_pushvolat(hak, (hak_oop_t*)&v); bucket = (hak_oop_oop_t)hak_makearray(hak, inisize); - hak_popvolat (hak); + hak_popvolat(hak); if (HAK_UNLIKELY(!bucket)) { @@ -521,7 +521,7 @@ int hak_walkdic (hak_t* hak, hak_oop_dic_t dic, hak_dic_walker_t walker, void* c if (HAK_IS_CONS(hak, tmp) && walker(hak, dic, (hak_oop_cons_t)tmp, ctx) <= -1) return -1; } - hak_popvolat (hak); + hak_popvolat(hak); return 0; } diff --git a/lib/exec.c b/lib/exec.c index 68a5329..50990b6 100644 --- a/lib/exec.c +++ b/lib/exec.c @@ -74,21 +74,21 @@ static hak_ooch_t oocstr_dash[] = { '-', '\0' }; #define LOAD_SP(hak, v_ctx) ((hak)->sp = HAK_OOP_TO_SMOOI((v_ctx)->sp)) #define STORE_SP(hak, v_ctx) ((v_ctx)->sp = HAK_SMOOI_TO_OOP((hak)->sp)) -#define LOAD_ACTIVE_IP(hak) LOAD_IP(hak, (hak)->active_context) -#define STORE_ACTIVE_IP(hak) STORE_IP(hak, (hak)->active_context) +#define LOAD_ACTIVE_IP(hak) LOAD_IP(hak,(hak)->active_context) +#define STORE_ACTIVE_IP(hak) STORE_IP(hak,(hak)->active_context) -#define LOAD_ACTIVE_SP(hak) LOAD_SP(hak, (hak)->processor->active) -#define STORE_ACTIVE_SP(hak) STORE_SP(hak, (hak)->processor->active) +#define LOAD_ACTIVE_SP(hak) LOAD_SP(hak,(hak)->processor->active) +#define STORE_ACTIVE_SP(hak) STORE_SP(hak,(hak)->processor->active) #define SWITCH_ACTIVE_CONTEXT(hak,v_ctx) \ do \ { \ - STORE_ACTIVE_IP (hak); \ + STORE_ACTIVE_IP(hak); \ (hak)->active_context = (v_ctx); \ - (hak)->active_function = (hak)->active_context->base; \ + (hak)->active_function =(hak)->active_context->base; \ (hak)->active_code = HAK_FUNCTION_GET_CODE_BYTE((hak)->active_function); \ - LOAD_ACTIVE_IP (hak); \ - (hak)->processor->active->current_context = (hak)->active_context; \ + LOAD_ACTIVE_IP(hak); \ + (hak)->processor->active->current_context =(hak)->active_context; \ } while (0) /*#define FETCH_BYTE_CODE(hak) ((hak)->code.bc.arr->slot[(hak)->ip++])*/ @@ -116,6 +116,7 @@ static hak_ooch_t oocstr_dash[] = { '-', '\0' }; # define LOG_INST_5(hak,fmt,a1,a2,a3,a4,a5) HAK_LOG6(hak, LOG_MASK_INST, "%010zd " fmt "\n", fetched_instruction_pointer, a1, a2, a3, a4, a5) # define LOG_INST_6(hak,fmt,a1,a2,a3,a4,a5,a6) HAK_LOG7(hak, LOG_MASK_INST, "%010zd " fmt "\n", fetched_instruction_pointer, a1, a2, a3, a4, a5, a6) # define LOG_INST_7(hak,fmt,a1,a2,a3,a4,a5,a6,a7) HAK_LOG8(hak, LOG_MASK_INST, "%010zd " fmt "\n", fetched_instruction_pointer, a1, a2, a3, a4, a5, a6, a7) +# define LOG_INST_8(hak,fmt,a1,a2,a3,a4,a5,a6,a7,a8) HAK_LOG9(hak, LOG_MASK_INST, "%010zd " fmt "\n", fetched_instruction_pointer, a1, a2, a3, a4, a5, a6, a7, a8) #else # define LOG_INST_0(hak,fmt) # define LOG_INST_1(hak,fmt,a1) @@ -125,6 +126,7 @@ static hak_ooch_t oocstr_dash[] = { '-', '\0' }; # define LOG_INST_5(hak,fmt,a1,a2,a3,a4,a5) # define LOG_INST_6(hak,fmt,a1,a2,a3,a4,a5,a6) # define LOG_INST_7(hak,fmt,a1,a2,a3,a4,a5,a6,a7) +# define LOG_INST_8(hak,fmt,a1,a2,a3,a4,a5,a6,a7,a8) #endif static int delete_sem_from_sem_io_tuple (hak_t* hak, hak_oop_semaphore_t sem, int force); @@ -135,7 +137,7 @@ static void terminate_all_processes (hak_t* hak); #define HAK_EXSTACK_PUSH(hak, ctx_, ip_, clsp_, sp_) \ do { \ - hak_oop_process_t ap = (hak)->processor->active; \ + hak_oop_process_t ap =(hak)->processor->active; \ hak_ooi_t exsp = HAK_OOP_TO_SMOOI(ap->exsp); \ if (exsp >= HAK_OOP_TO_SMOOI(ap->exst) - 1) \ { \ @@ -151,7 +153,7 @@ static void terminate_all_processes (hak_t* hak); #define HAK_EXSTACK_POP(hak) \ do { \ - hak_oop_process_t ap = (hak)->processor->active; \ + hak_oop_process_t ap =(hak)->processor->active; \ hak_ooi_t exsp = HAK_OOP_TO_SMOOI(ap->exsp); \ exsp -= 4; \ ap->exsp = HAK_SMOOI_TO_OOP(exsp); \ @@ -159,7 +161,7 @@ static void terminate_all_processes (hak_t* hak); #define HAK_EXSTACK_POP_TO(hak, ctx_, ip_, clsp_, sp_) \ do { \ - hak_oop_process_t ap = (hak)->processor->active; \ + hak_oop_process_t ap =(hak)->processor->active; \ hak_ooi_t exsp = HAK_OOP_TO_SMOOI(ap->exsp); \ sp_ = HAK_OOP_TO_SMOOI(ap->slot[exsp]); exsp--; \ clsp_ = HAK_OOP_TO_SMOOI(ap->slot[exsp]); exsp--; \ @@ -178,7 +180,7 @@ static void terminate_all_processes (hak_t* hak); #define HAK_CLSTACK_PUSH(hak, v) \ do { \ - hak_oop_process_t ap = (hak)->processor->active; \ + hak_oop_process_t ap =(hak)->processor->active; \ hak_ooi_t clsp_ = HAK_OOP_TO_SMOOI(ap->clsp); \ if (clsp_ >= HAK_OOP_TO_SMOOI(ap->clst)) \ { \ @@ -191,7 +193,7 @@ static void terminate_all_processes (hak_t* hak); #define HAK_CLSTACK_POP(hak) \ do { \ - hak_oop_process_t ap = (hak)->processor->active; \ + hak_oop_process_t ap =(hak)->processor->active; \ hak_ooi_t clsp_ = HAK_OOP_TO_SMOOI(ap->clsp); \ clsp_--; \ ap->clsp = HAK_SMOOI_TO_OOP(clsp_); \ @@ -199,7 +201,7 @@ static void terminate_all_processes (hak_t* hak); #define HAK_CLSTACK_POPS(hak, count) \ do { \ - hak_oop_process_t ap = (hak)->processor->active; \ + hak_oop_process_t ap =(hak)->processor->active; \ hak_ooi_t clsp_ = HAK_OOP_TO_SMOOI(ap->clsp); \ clsp_ -= count; \ ap->clsp = HAK_SMOOI_TO_OOP(clsp_); \ @@ -207,7 +209,7 @@ static void terminate_all_processes (hak_t* hak); #define HAK_CLSTACK_POP_TO(hak, v) \ do { \ - hak_oop_process_t ap = (hak)->processor->active; \ + hak_oop_process_t ap =(hak)->processor->active; \ hak_ooi_t clsp_ = HAK_OOP_TO_SMOOI(ap->clsp); \ v = ap->slot[clsp_]; clsp_--; \ ap->clsp = HAK_SMOOI_TO_OOP(clsp_); \ @@ -215,7 +217,7 @@ static void terminate_all_processes (hak_t* hak); #define HAK_CLSTACK_FETCH_TOP_TO(hak, v) \ do { \ - hak_oop_process_t ap = (hak)->processor->active; \ + hak_oop_process_t ap =(hak)->processor->active; \ hak_ooi_t clsp_ = HAK_OOP_TO_SMOOI(ap->clsp); \ v = ap->slot[clsp_]; \ } while (0) @@ -243,7 +245,7 @@ static HAK_INLINE int vm_startup (hak_t* hak) { for (cb = cb->prev; cb; cb = cb->prev) { - if (cb->vm_cleanup) cb->vm_cleanup (hak); + if (cb->vm_cleanup) cb->vm_cleanup(hak); } return -1; } @@ -278,7 +280,7 @@ static void vm_cleanup (hak_t* hak) (hak_ooi_t)HAK_OOP_TO_SMOOI(hak->processor->suspended.count)); HAK_LOG0 (hak, HAK_LOG_WARN, "Warning - terminating all residue processes\n"); - terminate_all_processes (hak); + terminate_all_processes(hak); } HAK_ASSERT(hak, hak->processor->active == hak->nil_process); @@ -328,7 +330,7 @@ static void vm_cleanup (hak_t* hak) /* deregister all pending finalizable objects pending just in case these * have not been removed for various reasons. (e.g. sudden VM abortion) */ - hak_deregallfinalizables (hak); + hak_deregallfinalizables(hak); #endif HAK_DEBUG0 (hak, "VM cleaned up\n"); @@ -398,7 +400,7 @@ static HAK_INLINE hak_oop_function_t make_function (hak_t* hak, hak_oow_t lfsize hak_oop_t tmp; hak_pushvolat(hak, (hak_oop_t*)&func); tmp = hak_makebytearray(hak, (hak_oob_t*)dbgi, HAK_SIZEOF(*dbgi) * blen); - hak_popvolat (hak); + hak_popvolat(hak); if (HAK_LIKELY(tmp)) func->dbgi = tmp; } @@ -406,7 +408,7 @@ static HAK_INLINE hak_oop_function_t make_function (hak_t* hak, hak_oow_t lfsize return func; } -static HAK_INLINE void fill_function_data (hak_t* hak, hak_oop_function_t func, hak_ooi_t attr_mask, hak_oop_context_t homectx, const hak_oop_t* lfptr, hak_oow_t lfsize) +static HAK_INLINE void fill_function_data (hak_t* hak, hak_oop_function_t func, hak_ooi_t attr_mask, hak_oow_t name_lfindex, hak_oop_context_t homectx, const hak_oop_t* lfptr, hak_oow_t lfsize) { /* Although this function could be integrated into make_function(), * this function has been separated from make_function() to make GC handling simpler */ @@ -425,8 +427,15 @@ static HAK_INLINE void fill_function_data (hak_t* hak, hak_oop_function_t func, } /* initialize other fields */ + if (name_lfindex < lfsize) + { + /* the compiler must ensure that lfsize is less than the maximum extended long parameter value */ + func->name = hak->active_function->literal_frame[name_lfindex]; + } + func->home = homectx; func->attr_mask = HAK_SMOOI_TO_OOP(attr_mask); + func->literal_frame_size = HAK_SMOOI_TO_OOP(lfsize); } static HAK_INLINE hak_oop_block_t make_compiled_block (hak_t* hak) @@ -436,11 +445,14 @@ static HAK_INLINE hak_oop_block_t make_compiled_block (hak_t* hak) return (hak_oop_block_t)hak_instantiate(hak, hak->c_compiled_block, HAK_NULL, 0); } -static HAK_INLINE void fill_block_data (hak_t* hak, hak_oop_block_t blk, hak_ooi_t attr_mask, hak_ooi_t ip, hak_oop_context_t homectx) +static HAK_INLINE void fill_block_data (hak_t* hak, hak_oop_block_t blk, hak_ooi_t attr_mask, hak_oow_t name_lfindex, hak_ooi_t ip, hak_oop_context_t homectx) { HAK_ASSERT(hak, attr_mask >= 0 && attr_mask <= HAK_SMOOI_MAX); HAK_ASSERT(hak, ip >= 0 && ip <= HAK_SMOOI_MAX); + if (name_lfindex < HAK_OOP_TO_SMOOI(hak->active_function->literal_frame_size)) + blk->name = hak->active_function->literal_frame[name_lfindex]; + blk->home = homectx; blk->ip = HAK_SMOOI_TO_OOP(ip); blk->attr_mask = HAK_SMOOI_TO_OOP(attr_mask); @@ -559,7 +571,7 @@ static hak_oop_process_t make_process (hak_t* hak, hak_oop_context_t c) hak_pushvolat(hak, (hak_oop_t*)&c); proc = (hak_oop_process_t)hak_instantiate(hak, hak->c_process, HAK_NULL, stksize + exstksize + clstksize); - hak_popvolat (hak); + hak_popvolat(hak); if (HAK_UNLIKELY(!proc)) { const hak_ooch_t* oldmsg = hak_backuperrmsg(hak); @@ -609,7 +621,7 @@ static hak_oop_process_t make_process (hak_t* hak, hak_oop_context_t c) static HAK_INLINE void sleep_active_process (hak_t* hak, int state) { - STORE_ACTIVE_SP (hak); + STORE_ACTIVE_SP(hak); /* store the current active context to the current process. * it is the suspended context of the process to be suspended */ @@ -944,7 +956,7 @@ static void terminate_process (hak_t* hak, hak_oop_process_t proc) nrp = find_next_runnable_process(hak); - STORE_ACTIVE_SP (hak); /* commit the stack pointer before termination */ + STORE_ACTIVE_SP(hak); /* commit the stack pointer before termination */ unchain_from_processor(hak, proc, HAK_PROCESS_STATE_TERMINATED); reset_process_stack_pointers(hak, proc); /* invalidate the process stack */ @@ -1647,7 +1659,7 @@ static int add_sem_to_sem_io_tuple (hak_t* hak, hak_oop_semaphore_t sem, hak_ooi hak_pushvolat(hak, (hak_oop_t*)&sem); n = hak->vmprim.vm_muxadd(hak, io_handle, new_mask); - hak_popvolat (hak); + hak_popvolat(hak); } else { @@ -1662,7 +1674,7 @@ static int add_sem_to_sem_io_tuple (hak_t* hak, hak_oop_semaphore_t sem, hak_ooi hak_pushvolat(hak, (hak_oop_t*)&sem); n = hak->vmprim.vm_muxmod(hak, io_handle, new_mask); - hak_popvolat (hak); + hak_popvolat(hak); } if (n <= -1) @@ -1731,7 +1743,7 @@ static int delete_sem_from_sem_io_tuple (hak_t* hak, hak_oop_semaphore_t sem, in hak_pushvolat(hak, (hak_oop_t*)&sem); x = new_mask? hak->vmprim.vm_muxmod(hak, io_handle, new_mask): hak->vmprim.vm_muxdel(hak, io_handle); - hak_popvolat (hak); + hak_popvolat(hak); if (x <= -1) { HAK_LOG3(hak, HAK_LOG_WARN, "Failed to delete IO semaphore at index %zd handle %zd for %hs\n", index, io_handle, io_type_str[io_type]); @@ -1942,7 +1954,7 @@ static int prepare_new_context (hak_t* hak, hak_oop_block_t op_blk, hak_ooi_t na /* create a new block context to clone op_blk */ hak_pushvolat(hak, (hak_oop_t*)&op_blk); blkctx = make_context(hak, fixed_nargs + fblk_nrvars + fblk_nlvars + excess_nargs); - hak_popvolat (hak); + hak_popvolat(hak); if (HAK_UNLIKELY(!blkctx)) return -1; #if 0 @@ -1956,6 +1968,7 @@ static int prepare_new_context (hak_t* hak, hak_oop_block_t op_blk, hak_ooi_t na blkctx->req_nrets = HAK_SMOOI_TO_OOP(req_nrvars); blkctx->attr_mask = op_blk->attr_mask; blkctx->base = op_blk->home->base; + blkctx->name = op_blk->name; if (is_msgsend) { @@ -2080,7 +2093,7 @@ static int __activate_function (hak_t* hak, hak_oop_function_t op_func, hak_ooi_ /* create a new block context to clone op_func */ hak_pushvolat(hak, (hak_oop_t*)&op_func); functx = make_context(hak, fixed_nargs + nrvars + nlvars + excess_nargs); - hak_popvolat (hak); + hak_popvolat(hak); if (HAK_UNLIKELY(!functx)) return -1; functx->ip = HAK_SMOOI_TO_OOP(0); @@ -2399,17 +2412,15 @@ static HAK_INLINE int do_throw (hak_t* hak, hak_oop_t val, hak_ooi_t ip) } /* output backtrace */ - HAK_LOG0(hak, HAK_LOG_IC | HAK_LOG_INFO, "Backtrace\n"); + HAK_LOG0(hak, HAK_LOG_IC | HAK_LOG_INFO, "[BACKTRACE]\n"); c = hak->active_context; - while (c != hak->_nil) { -/* - hak_oop_class_t ow; - ow = c->owner; - if (ow != hak->_nil) -*/ + while ((hak_oop_t)c != hak->_nil) { f = c->base; if (f->dbgi != hak->_nil) { + static hak_ooch_t none[1] = { '\0' }; + static hak_ooch_t colon[2] = { ':', '\0' }; + hak_dbgi_t* dbgi; hak_loc_t loc; hak_ooi_t cip; @@ -2419,7 +2430,14 @@ static HAK_INLINE int do_throw (hak_t* hak, hak_oop_t val, hak_ooi_t ip) HAK_MEMSET(&loc, 0, HAK_SIZEOF(loc)); loc.file = dbgi[cip].fname; loc.line = dbgi[cip].sline; - HAK_LOG3(hak, HAK_LOG_IC | HAK_LOG_INFO, " %O (%js:%zu)\n", c->owner, (dbgi[cip].fname? dbgi[ip].fname: oocstr_dash), dbgi[cip].sline); + + HAK_LOG7(hak, HAK_LOG_IC | HAK_LOG_INFO, " %.*js%js%.*js(%js:%zu)\n", + (c->owner == hak->_nil? 0: HAK_OBJ_GET_SIZE(((hak_oop_class_t)c->owner)->name)), + (c->owner == hak->_nil? none: ((hak_oop_char_t)((hak_oop_class_t)c->owner)->name)->slot), + (c->owner == hak->_nil? none: colon), + (c->owner == hak->_nil? 0: HAK_OBJ_GET_SIZE(((hak_oop_char_t)c->name))), + (c->name == hak->_nil? none: ((hak_oop_char_t)c->name)->slot), + (dbgi[cip].fname? dbgi[ip].fname: oocstr_dash), dbgi[cip].sline); } c = c->sender; } @@ -2734,7 +2752,7 @@ static int start_initial_process_and_context (hak_t* hak, hak_ooi_t initial_ip, hak_pushvolat(hak, (hak_oop_t*)&ctx); proc = start_initial_process(hak, ctx); - hak_popvolat (hak); + hak_popvolat(hak); if (HAK_UNLIKELY(!proc)) return -1; /* the stack must contain nothing as it should emulate the expresssion - (the-initial-function). @@ -3010,7 +3028,7 @@ switch_to_next: #endif if (!hak->proc_switched) { - switch_to_next_runnable_process (hak); + switch_to_next_runnable_process(hak); hak->proc_switched = 0; } #if defined(HAK_EXTERNAL_PROCESS_SWITCH) @@ -3905,7 +3923,7 @@ static int execute (hak_t* hak) /* similar to HAK_CODE_RETURN_FROM_BLOCK */ hak->last_retv = ctx->slot[fixed_nargs]; /* remember the first pushed one as the last return value. currently no good way to hak_execute() recognize multiple return values. */ - do_return_from_block (hak); + do_return_from_block(hak); break; } @@ -4228,7 +4246,7 @@ hak_logbfmt(hak, HAK_LOG_STDERR, ">>>%O c->sc=%O sc=%O b2=%d b3=%d nivars=%d ncv { hak_pushvolat(hak, (hak_oop_t*)&_class); mdic = hak_makedic(hak, 16); /* TODO: configurable initial size? */ - hak_popvolat (hak); + hak_popvolat(hak); if (HAK_UNLIKELY(!mdic)) goto oops_with_errmsg_supplement; ((hak_oop_class_t)_class)->mdic = mdic; } @@ -4788,7 +4806,7 @@ hak_logbfmt(hak, HAK_LOG_STDERR, ">>>%O c->sc=%O sc=%O b2=%d b3=%d nivars=%d ncv hak_pushvolat(hak, &t3); t = hak_makecons(hak, t1, hak->_nil); - hak_popvolat (hak); + hak_popvolat(hak); if (HAK_UNLIKELY(!t)) goto oops; ((hak_oop_oop_t)t3)->slot[1] = t; @@ -4826,7 +4844,7 @@ hak_logbfmt(hak, HAK_LOG_STDERR, ">>>%O c->sc=%O sc=%O b2=%d b3=%d nivars=%d ncv hak_pushvolat(hak, &t3); t = hak_makecons(hak, t1, hak->_nil); - hak_popvolat (hak); + hak_popvolat(hak); if (HAK_UNLIKELY(!t)) goto oops; ((hak_oop_oop_t)t3)->slot[1] = t; @@ -4911,54 +4929,58 @@ hak_logbfmt(hak, HAK_LOG_STDERR, ">>>%O c->sc=%O sc=%O b2=%d b3=%d nivars=%d ncv HAK_ASSERT(hak, HAK_IS_CONTEXT(hak, hak->active_context)); hak->last_retv = HAK_STACK_GETTOP(hak); /* get the stack top */ - do_return_from_block (hak); + do_return_from_block(hak); break; case HAK_CODE_MAKE_FUNCTION: { hak_oop_function_t funcobj; - hak_oow_t b3, b4; + hak_oow_t b3, b4, x; hak_oow_t joff; /* b1 - block temporaries mask * b2 - block temporaries mask - * b3 - literal frame base - * b4 - literal frame size */ + * b3 - literal frame index to name + * b4 - literal frame base + * b5 - literal frame size */ FETCH_PARAM_CODE_TO(hak, b1); + FETCH_PARAM_CODE_TO(hak, x); + b1 = (b1 << (8 * HAK_CODE_LONG_PARAM_SIZE)) | x; + FETCH_PARAM_CODE_TO(hak, b2); + FETCH_PARAM_CODE_TO(hak, x); + b2 = (b2 << (8 * HAK_CODE_LONG_PARAM_SIZE)) | x; + FETCH_PARAM_CODE_TO(hak, b3); FETCH_PARAM_CODE_TO(hak, b4); - b1 = (b1 << (8 * HAK_CODE_LONG_PARAM_SIZE)) | b2; - LOG_INST_7(hak, "make_function %zu %zu %zu %zu %zu %zu %zu", + LOG_INST_8(hak, "make_function %zu %zu %zu %zu %zu %zu %zu %zu", GET_BLK_MASK_INSTA(b1), GET_BLK_MASK_VA(b1), GET_BLK_MASK_NARGS(b1), GET_BLK_MASK_NRVARS(b1), GET_BLK_MASK_NLVARS(b1), - b3, b4); + b2, b3, b4); HAK_ASSERT(hak, b1 >= 0); /* the MAKE_FUNCTION instruction is followed by the long JUMP_FORWARD_X instruction. - * i can decode the instruction and get the size of instructions - * of the block context */ + * i can decode the instruction and get the size of instructions of the block context */ HAK_ASSERT(hak, hak->active_code[hak->ip] == HAK_CODE_JUMP_FORWARD_X); + #if (HAK_CODE_LONG_PARAM_SIZE == 2) + /* don't use the FETCH_BYTE_CODE() macros or the like to not increment hak->ip */ joff = hak->active_code[hak->ip + 1]; - #if (HAK_CODE_LONG_PARAM_SIZE == 2) joff = (joff << 8) | hak->active_code[hak->ip + 2]; - #endif - /* copy the byte codes from the active context to the new context */ - #if (HAK_CODE_LONG_PARAM_SIZE == 2) funcobj = make_function(hak, b4, &hak->active_code[hak->ip + 3], joff, HAK_NULL); #else + joff = hak->active_code[hak->ip + 1]; funcobj = make_function(hak, b4, &hak->active_code[hak->ip + 2], joff, HAK_NULL); #endif if (HAK_UNLIKELY(!funcobj)) goto oops; - fill_function_data(hak, funcobj, b1, hak->active_context, &hak->active_function->literal_frame[b3], b4); + fill_function_data(hak, funcobj, b1, b2, hak->active_context, &hak->active_function->literal_frame[b3], b4); /* push the new function to the stack of the active context */ HAK_STACK_PUSH(hak, (hak_oop_t)funcobj); @@ -4967,19 +4989,26 @@ hak_logbfmt(hak, HAK_LOG_STDERR, ">>>%O c->sc=%O sc=%O b2=%d b3=%d nivars=%d ncv case HAK_CODE_MAKE_BLOCK: { + hak_oow_t x; hak_oop_block_t blkobj; - /* b1 - block temporaries mask - * b2 - block temporaries mask */ + /* block temporaries mask (extended long) + * literal frame index to name (extended long) */ FETCH_PARAM_CODE_TO(hak, b1); + FETCH_PARAM_CODE_TO(hak, x); + b1 = (b1 << (8 * HAK_CODE_LONG_PARAM_SIZE)) | x; + FETCH_PARAM_CODE_TO(hak, b2); - b1 = (b1 << (8 * HAK_CODE_LONG_PARAM_SIZE)) | b2; - LOG_INST_5(hak, "make_block %zu %zu %zu %zu %zu", + FETCH_PARAM_CODE_TO(hak, x); + b2 = (b2 << (8 * HAK_CODE_LONG_PARAM_SIZE)) | x; + + LOG_INST_6(hak, "make_block %zu %zu %zu %zu %zu %zu", GET_BLK_MASK_INSTA(b1), GET_BLK_MASK_VA(b1), GET_BLK_MASK_NARGS(b1), GET_BLK_MASK_NRVARS(b1), - GET_BLK_MASK_NLVARS(b1)); + GET_BLK_MASK_NLVARS(b1), + b2); HAK_ASSERT(hak, b1 >= 0); @@ -4990,7 +5019,7 @@ hak_logbfmt(hak, HAK_LOG_STDERR, ">>>%O c->sc=%O sc=%O b2=%d b3=%d nivars=%d ncv * 11000100 KKKKKKKK or 11000100 KKKKKKKK KKKKKKKK * depending on HAK_CODE_LONG_PARAM_SIZE. change 'ip' to point to * the instruction after the jump. */ - fill_block_data(hak, blkobj, b1, hak->ip + HAK_CODE_LONG_PARAM_SIZE + 1, hak->active_context); + fill_block_data(hak, blkobj, b1, b2, hak->ip + HAK_CODE_LONG_PARAM_SIZE + 1, hak->active_context); /* push the new block context to the stack of the active context */ HAK_STACK_PUSH(hak, (hak_oop_t)blkobj); @@ -5012,7 +5041,7 @@ hak_logbfmt(hak, HAK_LOG_STDERR, ">>>%O c->sc=%O sc=%O b2=%d b3=%d nivars=%d ncv done: hak->gci.lazy_sweep = 1; - vm_cleanup (hak); + vm_cleanup(hak); #if defined(HAK_PROFILE_VM) HAK_LOG1(hak, HAK_LOG_IC | HAK_LOG_INFO, "EXEC OK - TOTAL INST COUTNER = %zu\n", inst_counter); #endif @@ -5024,7 +5053,7 @@ oops_with_errmsg_supplement: oops: hak->gci.lazy_sweep = 1; - vm_cleanup (hak); + vm_cleanup(hak); #if defined(HAK_PROFILE_VM) HAK_LOG1(hak, HAK_LOG_IC | HAK_LOG_INFO, "EXEC ERROR - TOTAL INST COUTNER = %zu\n", inst_counter); #endif @@ -5069,7 +5098,7 @@ hak_oop_t hak_execute (hak_t* hak) if (HAK_UNLIKELY(!funcobj)) return HAK_NULL; /* pass nil for the home context of the initial function */ - fill_function_data(hak, funcobj, ENCODE_BLK_MASK(0,0,0,0,hak->code.ngtmprs), (hak_oop_context_t)hak->_nil, hak->code.lit.arr->slot, hak->code.lit.len); + fill_function_data(hak, funcobj, ENCODE_BLK_MASK(0,0,0,0,hak->code.ngtmprs), HAK_TYPE_MAX(hak_oow_t), (hak_oop_context_t)hak->_nil, hak->code.lit.arr->slot, hak->code.lit.len); hak->initial_function = funcobj; /* the initial function is ready */ @@ -5114,7 +5143,7 @@ hak_oop_t hak_execute (hak_t* hak) HAK_ASSERT(hak, HAK_OOP_TO_SMOOI(hak->processor->runnable.count) == 0); HAK_ASSERT(hak, HAK_OOP_TO_SMOOI(hak->processor->suspended.count) == 0); - LOAD_ACTIVE_SP (hak); /* sync hak->nil_process->sp with hak->sp */ + LOAD_ACTIVE_SP(hak); /* sync hak->nil_process->sp with hak->sp */ HAK_ASSERT(hak, hak->sp == -1); #if defined(HAK_PROFILE_VM) @@ -5179,7 +5208,7 @@ hak_pfrc_t hak_pf_process_fork (hak_t* hak, hak_mod_t* mod, hak_ooi_t nargs) hak_pushvolat(hak, (hak_oop_t*)&newctx); newprc = make_process(hak, newctx); - hak_popvolat (hak); + hak_popvolat(hak); if (HAK_UNLIKELY(!newprc)) return HAK_PF_FAILURE; chain_into_processor(hak, newprc, HAK_PROCESS_STATE_RUNNABLE); @@ -5249,7 +5278,7 @@ hak_pfrc_t hak_pf_process_terminate (hak_t* hak, hak_mod_t* mod, hak_ooi_t nargs hak_pfrc_t hak_pf_process_terminate_all (hak_t* hak, hak_mod_t* mod, hak_ooi_t nargs) { - terminate_all_processes (hak); + terminate_all_processes(hak); return HAK_PF_SUCCESS; } diff --git a/lib/fmt.c b/lib/fmt.c index 142ee1c..64183d7 100644 --- a/lib/fmt.c +++ b/lib/fmt.c @@ -2037,12 +2037,12 @@ static int sprint_ucs (hak_t* hak, hak_fmtout_t* fmtout, const hak_uch_t* ptr, h if ((arg_state)->idx >= nargs) { (arg_state)->stop = 1; goto invalid_format; } \ arg = HAK_STACK_GETARG(hak, nargs, (arg_state)->idx); \ (arg_state)->idx++; \ -} while(0) +} while (0) #define GET_NEXT_CHAR_TO(hak,fmt,fmtend,ch) do { \ if (fmt >= fmtend) ch = HAK_OOCI_EOF; \ else { ch = *(fmt); (fmt)++; }\ -} while(0) +} while (0) static HAK_INLINE int format_stack_args (hak_t* hak, hak_fmtout_t* fmtout, hak_ooi_t nargs, int rcv_is_fmtstr) { @@ -2648,7 +2648,7 @@ static HAK_INLINE int format_stack_args (hak_t* hak, hak_fmtout_t* fmtout, hak_o * i will drop all digits after the fixed point */ hak_pushvolat(hak, &arg); nv = hak_truncfpdecval(hak, fa->value, HAK_OOP_TO_SMOOI(fa->scale), 0); - hak_popvolat (hak); + hak_popvolat(hak); if (!nv) { HAK_LOG1 (hak, HAK_LOG_WARN | HAK_LOG_UNTYPED, "unable to truncate a fixed-point number %O to an integer for output\n", arg); diff --git a/lib/gc.c b/lib/gc.c index 3558fb9..2103318 100644 --- a/lib/gc.c +++ b/lib/gc.c @@ -892,7 +892,7 @@ static HAK_INLINE void gc_ms_scan_stack (hak_t* hak) static HAK_INLINE void gc_ms_mark (hak_t* hak, hak_oop_t oop) { gc_ms_mark_object(hak, oop); - gc_ms_scan_stack (hak); + gc_ms_scan_stack(hak); } #endif @@ -987,7 +987,7 @@ static HAK_INLINE void gc_ms_mark_roots (hak_t* hak) for (cb = hak->cblist; cb; cb = cb->next) { - if (cb->on_gc) cb->on_gc (hak); + if (cb->on_gc) cb->on_gc(hak); } #if defined(ENABLE_GCFIN) @@ -1136,7 +1136,7 @@ void hak_gc (hak_t* hak, int full) hak->gci.stack.len = 0; /*hak->gci.stack.max = 0;*/ - gc_ms_mark_roots (hak); + gc_ms_mark_roots(hak); if (!full && hak->gci.lazy_sweep) { @@ -1148,7 +1148,7 @@ void hak_gc (hak_t* hak, int full) } else { - gc_ms_sweep (hak); + gc_ms_sweep(hak); } HAK_LOG2 (hak, HAK_LOG_GC | HAK_LOG_INFO, "Finished GC (mark-sweep) - gci.bsz = %zu, gci.stack.max %zu\n", hak->gci.bsz, hak->gci.stack.max); @@ -1267,7 +1267,7 @@ void hak_gc (hak_t* hak) for (cb = hak->cblist; cb; cb = cb->next) { - if (cb->gc) cb->gc (hak); + if (cb->gc) cb->gc(hak); } /* scan the new heap to move referenced objects */ diff --git a/lib/hak-prv.h b/lib/hak-prv.h index bc53fc5..31045ba 100644 --- a/lib/hak-prv.h +++ b/lib/hak-prv.h @@ -663,8 +663,7 @@ struct hak_cframe_t struct { unsigned int fun_type; - hak_oow_t jump_inst_pos; - hak_ooi_t lfbase_pos; + hak_ooi_t jump_inst_pos; hak_ooi_t lfsize_pos; hak_cnode_t* class_name; /* class name for out-of-class method definition */ } fun; @@ -1355,7 +1354,7 @@ enum hak_bcode_t HAK_CODE_PUSH_CHARLIT = 0xB4, /* 180 */ HAK_CODE_PLUS = 0xB5, /* 181 TOOD: move it to a lower code number later after killing OBJVAR instructions */ - /* UNUSED - 0xB6 - 0xB7 */ + /* UNUSED - 0xB6-0xB7 */ HAK_CODE_STORE_INTO_OBJECT_X = 0xB8, /* 184 ## */ HAK_CODE_POP_INTO_OBJECT_X = 0xBC, /* 188 ## */ diff --git a/lib/hak.c b/lib/hak.c index 22d329f..9fadb2f 100644 --- a/lib/hak.c +++ b/lib/hak.c @@ -49,7 +49,7 @@ hak_t* hak_open (hak_mmgr_t* mmgr, hak_oow_t xtnsize, const hak_vmprim_t* vmprim void hak_close (hak_t* hak) { - hak_fini (hak); + hak_fini(hak); HAK_MMGR_FREE (HAK_MMGR(hak), hak); } @@ -164,7 +164,7 @@ int hak_init (hak_t* hak, hak_mmgr_t* mmgr, const hak_vmprim_t* vmprim) modtab_inited = 1; hak_rbt_setstyle(&hak->modtab, hak_get_rbt_style(HAK_RBT_STYLE_INLINE_COPIERS)); - fill_bigint_tables (hak); + fill_bigint_tables(hak); hak->tagged_brands[HAK_OOP_TAG_SMOOI] = HAK_BRAND_SMOOI; hak->tagged_brands[HAK_OOP_TAG_SMPTR] = HAK_BRAND_SMPTR; @@ -184,7 +184,7 @@ int hak_init (hak_t* hak, hak_mmgr_t* mmgr, const hak_vmprim_t* vmprim) hak->sp = -1; hak->ip = 0; - if (hak->vmprim.dl_startup) hak->vmprim.dl_startup (hak); + if (hak->vmprim.dl_startup) hak->vmprim.dl_startup(hak); return 0; oops: @@ -230,7 +230,7 @@ void hak_fini (hak_t* hak) for (cb = hak->cblist; cb; cb = cb->next) { - if (cb->on_fini) cb->on_fini (hak); + if (cb->on_fini) cb->on_fini(hak); } if (hak->log.len > 0) @@ -247,7 +247,7 @@ void hak_fini (hak_t* hak) while (hak->cblist) hak_deregcb(hak, hak->cblist); /* detach the user data io handlers just in case */ - hak_detachudio (hak); + hak_detachudio(hak); if (hak->sem_list) { @@ -367,7 +367,7 @@ void hak_fini (hak_t* hak) hak->sprintf.xbuf.len = 0; } - if (hak->vmprim.dl_cleanup) hak->vmprim.dl_cleanup (hak); + if (hak->vmprim.dl_cleanup) hak->vmprim.dl_cleanup(hak); } void hak_resetcode (hak_t* hak) @@ -883,7 +883,7 @@ hak_mod_data_t* hak_openmod (hak_t* hak, const hak_ooch_t* name, hak_oow_t namel mdp = (hak_mod_data_t*)HAK_RBT_VPTR(pair); if (load(hak, &mdp->mod) <= -1) { - const hak_ooch_t* oldmsg = hak_backuperrmsg (hak); + const hak_ooch_t* oldmsg = hak_backuperrmsg(hak); hak_seterrbfmt(hak, HAK_ERRNUM(hak), "module initializer [%js] returned failure in [%.*js] - %js", buf, namelen, name, oldmsg); HAK_DEBUG3(hak, "Module function [%js] returned failure in [%.*js]\n", buf, namelen, name); hak_rbt_delete(&hak->modtab, name, namelen); diff --git a/lib/hak.h b/lib/hak.h index 03c6f54..4fa79ac 100644 --- a/lib/hak.h +++ b/lib/hak.h @@ -578,15 +578,15 @@ struct hak_fpdec_t #define HAK_FUNCTION_GET_CODE_BYTE(m) HAK_OBJ_GET_TRAILER_BYTE(m) #define HAK_FUNCTION_GET_CODE_SIZE(m) HAK_OBJ_GET_TRAILER_SIZE(m) -#define HAK_FUNCTION_NAMED_INSTVARS 3 /* this excludes literal frames and byte codes */ +#define HAK_FUNCTION_NAMED_INSTVARS 5 /* this excludes literal frames and byte codes */ typedef struct hak_function_t hak_function_t; typedef struct hak_function_t* hak_oop_function_t; -#define HAK_BLOCK_NAMED_INSTVARS 3 +#define HAK_BLOCK_NAMED_INSTVARS 4 typedef struct hak_block_t hak_block_t; typedef struct hak_block_t* hak_oop_block_t; -#define HAK_CONTEXT_NAMED_INSTVARS 10 +#define HAK_CONTEXT_NAMED_INSTVARS 11 typedef struct hak_context_t hak_context_t; typedef struct hak_context_t* hak_oop_context_t; @@ -595,9 +595,11 @@ struct hak_function_t HAK_OBJ_HEADER; hak_oop_t attr_mask; /* smooi */ + hak_oop_t name; /* symbol or nil */ hak_oop_context_t home; /* home context. nil for the initial function */ hak_oop_t dbgi; /* byte array containing debug information. nil if not available */ + hak_oop_t literal_frame_size; /* == variable indexed part == */ hak_oop_t literal_frame[1]; /* it stores literals. it may not exist */ @@ -614,6 +616,7 @@ struct hak_block_t HAK_OBJ_HEADER; hak_oop_t attr_mask; /* smooi */ + hak_oop_t name; /* symbol or nil */ hak_oop_context_t home; /* home context */ hak_oop_t ip; /* smooi. instruction pointer where the byte code begins in home->base */ }; @@ -627,6 +630,7 @@ struct hak_context_t /* SmallInteger. */ hak_oop_t attr_mask; + hak_oop_t name; /* symbol or nil. copied from the compiled block. TODO: is it better to maintain the backward pointer to the compiled block itself? */ /* SmallInteger, instruction pointer */ hak_oop_t ip; diff --git a/lib/number.c b/lib/number.c index 11f825d..a42cf55 100644 --- a/lib/number.c +++ b/lib/number.c @@ -273,13 +273,13 @@ hak_oop_t hak_divnums (hak_t* hak, hak_oop_t x, hak_oop_t y) nv = hak_mulints(hak, nv, HAK_SMOOI_TO_OOP(10)); if (!nv) { - hak_popvolat (hak); + hak_popvolat(hak); return HAK_NULL; } } nv = hak_divints(hak, nv, yv, 0, HAK_NULL); - hak_popvolat (hak); + hak_popvolat(hak); if (!nv) return HAK_NULL; return hak_makefpdec(hak, nv, xs); @@ -360,7 +360,7 @@ hak_oop_t hak_sqrtnum (hak_t* hak, hak_oop_t x) v = hak_mulints(hak, v, HAK_SMOOI_TO_OOP(10)); if (!v) { - hak_popvolat (hak); + hak_popvolat(hak); return HAK_NULL; } } diff --git a/lib/prim.c b/lib/prim.c index 795cf63..119e4f5 100644 --- a/lib/prim.c +++ b/lib/prim.c @@ -1392,7 +1392,7 @@ int hak_addbuiltinprims (hak_t* hak) hak_pushvolat(hak, &prim); name = hak_makesymbol(hak, builtin_prims[i].name, builtin_prims[i].namelen); - hak_popvolat (hak); + hak_popvolat(hak); if (HAK_UNLIKELY(!name)) { const hak_ooch_t* orgmsg = hak_backuperrmsg(hak); @@ -1403,7 +1403,7 @@ int hak_addbuiltinprims (hak_t* hak) hak_pushvolat(hak, &name); cons = hak_putatsysdic(hak, name, prim); - hak_popvolat (hak); + hak_popvolat(hak); if (HAK_UNLIKELY(!cons)) { const hak_ooch_t* orgmsg = hak_backuperrmsg(hak); diff --git a/lib/read.c b/lib/read.c index 946e792..560d0d6 100644 --- a/lib/read.c +++ b/lib/read.c @@ -314,8 +314,8 @@ static HAK_INLINE int is_ident_char (hak_ooci_t c) #define GET_CHAR_TO(hak,c) \ do { \ if (get_char(hak) <= -1) return -1; \ - c = (hak)->c->lxc.c; \ - } while(0) + c =(hak)->c->lxc.c; \ + } while (0) #define ADD_TOKEN_STR(hak,s,l) \ @@ -2206,11 +2206,11 @@ static int feed_continue_with_char (hak_t* hak, hak_ooci_t c, hak_flx_state_t st return 0; } -#define FEED_WRAP_UP(hak, type) do { if (feed_wrap_up(hak, type) <= -1) return -1; } while(0) -#define FEED_WRAP_UP_WITH_CHAR(hak, c, type) do { if (feed_wrap_up_with_char(hak, c, type) <= -1) return -1; } while(0) -#define FEED_WRAP_UP_WITH_CHARS(hak, str, len, type) do { if (feed_wrap_up_with_str(hak, str, len, type) <= -1) return -1; } while(0) +#define FEED_WRAP_UP(hak, type) do { if (feed_wrap_up(hak, type) <= -1) return -1; } while (0) +#define FEED_WRAP_UP_WITH_CHAR(hak, c, type) do { if (feed_wrap_up_with_char(hak, c, type) <= -1) return -1; } while (0) +#define FEED_WRAP_UP_WITH_CHARS(hak, str, len, type) do { if (feed_wrap_up_with_str(hak, str, len, type) <= -1) return -1; } while (0) #define FEED_CONTINUE(hak, state) (feed_continue(hak, state)) -#define FEED_CONTINUE_WITH_CHAR(hak, c, state) do { if (feed_continue_with_char(hak, c, state) <= -1) return -1; } while(0) +#define FEED_CONTINUE_WITH_CHAR(hak, c, state) do { if (feed_continue_with_char(hak, c, state) <= -1) return -1; } while (0) /* ------------------------------------------------------------------------ */ diff --git a/lib/std.c b/lib/std.c index 76910f7..cd36cc0 100644 --- a/lib/std.c +++ b/lib/std.c @@ -1299,7 +1299,7 @@ static void backtrace_stack_frames (hak_t* hak) static void _assertfail (hak_t* hak, const hak_bch_t* expr, const hak_bch_t* file, hak_oow_t line) { hak_logbfmt(hak, HAK_LOG_STDERR | HAK_LOG_UNTYPED | HAK_LOG_FATAL, "ASSERTION FAILURE: %hs at %hs:%zu\n", expr, file, line); - backtrace_stack_frames (hak); + backtrace_stack_frames(hak); #if defined(_WIN32) ExitProcess (249); @@ -2726,7 +2726,7 @@ static HAK_INLINE void abort_all_haks (int signo) { xtn_t* xtn = GET_XTN(hak); hak_uint8_t u8; - /*hak_abortstd (hak);*/ + /*hak_abortstd(hak);*/ u8 = signo & 0xFF; write (xtn->sigfd.p[1], &u8, HAK_SIZEOF(u8)); hak = xtn->next; @@ -2752,7 +2752,7 @@ static HAK_INLINE void swproc_all_haks (int unused) do { xtn_t* xtn = GET_XTN(hak); - if (xtn->rcv_tick) hak_switchprocess (hak); + if (xtn->rcv_tick) hak_switchprocess(hak); hak = xtn->next; } while (hak); @@ -3610,7 +3610,7 @@ static void cb_on_fini (hak_t* hak) xtn_t* xtn = GET_XTN(hak); if ((xtn->log.fd_flags & LOGFD_OPENED_HERE) && xtn->log.fd >= 0) close (xtn->log.fd); reset_log_to_default (xtn); - unchain (hak); + unchain(hak); } static void cb_halting (hak_t* hak) @@ -3963,7 +3963,7 @@ static void cb_vm_cleanup (hak_t* hak) close (xtn->ep); xtn->ep = -1; } - /*destroy_poll_data_space (hak);*/ + /*destroy_poll_data_space(hak);*/ #elif defined(USE_KQUEUE) if (xtn->ep >= 0) { @@ -3989,7 +3989,7 @@ static void cb_vm_cleanup (hak_t* hak) hak_freemem(hak, xtn->ev.buf); xtn->ev.buf = HAK_NULL; } - /*destroy_poll_data_space (hak);*/ + /*destroy_poll_data_space(hak);*/ MUTEX_DESTROY (&xtn->ev.reg.pmtx); #elif defined(USE_SELECT) FD_ZERO (&xtn->ev.reg.rfds); @@ -4122,7 +4122,7 @@ hak_t* hak_openstdwithmmgr (hak_mmgr_t* mmgr, hak_oow_t xtnsize, hak_errnum_t* e /* adjust the object size by the sizeof xtn_t so that hak_getxtn() returns the right pointer. */ hak->_instsize += HAK_SIZEOF(xtn_t); - chain (hak); /* call chian() before hak_regcb() as fini_hak() calls unchain() */ + chain(hak); /* call chian() before hak_regcb() as fini_hak() calls unchain() */ reset_log_to_default (GET_XTN(hak)); HAK_MEMSET(&cb, 0, HAK_SIZEOF(cb)); @@ -4134,7 +4134,7 @@ hak_t* hak_openstdwithmmgr (hak_mmgr_t* mmgr, hak_oow_t xtnsize, hak_errnum_t* e if (hak_regcb(hak, &cb) == HAK_NULL) { if (errnum) *errnum = HAK_ERRNUM(hak); - hak_close (hak); + hak_close(hak); return HAK_NULL; } diff --git a/lib/sym.c b/lib/sym.c index d1a5ddb..a8e0bb6 100644 --- a/lib/sym.c +++ b/lib/sym.c @@ -60,7 +60,7 @@ static hak_oop_oop_t expand_bucket (hak_t* hak, hak_oop_oop_t oldbuc) hak_pushvolat(hak, (hak_oop_t*)&oldbuc); newbuc = (hak_oop_oop_t)hak_makearray(hak, newsz); - hak_popvolat (hak); + hak_popvolat(hak); if (!newbuc) return HAK_NULL; while (oldsz > 0) diff --git a/lib/x-client.c b/lib/x-client.c index f3e7732..04132ee 100644 --- a/lib/x-client.c +++ b/lib/x-client.c @@ -214,7 +214,7 @@ hak_client_t* hak_client_open (hak_mmgr_t* mmgr, hak_oow_t xtnsize, hak_client_p oops: /* NOTE: pipe should be closed if jump to here is made after pipe() above */ - if (hak) hak_close (hak); + if (hak) hak_close(hak); if (client) HAK_MMGR_FREE (mmgr, client); return HAK_NULL; } diff --git a/lib/x-server.c b/lib/x-server.c index 4d74e38..14fd05c 100644 --- a/lib/x-server.c +++ b/lib/x-server.c @@ -555,7 +555,7 @@ printf ("IO CLOSE SOMETHING...........\n"); /* writing failure on the socket is a critical failure. * execution must get aborted */ - hak_abort (hak); + hak_abort(hak); return -1; } @@ -575,7 +575,7 @@ printf ("IO CLOSE SOMETHING...........\n"); /* writing failure on the socket is a critical failure. * execution must get aborted */ - hak_abort (hak); + hak_abort(hak); return -1; } outarg->xlen = outarg->len; @@ -968,8 +968,8 @@ printf ("EXECUTING hak_executing......\n"); /* save error message before other calls override erro info */ errlen = hak_copyerrbmsg(hak, errmsg, HAK_COUNTOF(errmsg)); - hak_flushudio (hak); - hak_clearcode (hak); + hak_flushudio(hak); + hak_clearcode(hak); if (send_bytes(proto, HAK_XPKT_ERROR, errmsg, errlen) <= -1) { @@ -983,8 +983,8 @@ printf ("EXECUTING hak_executing......\n"); hak_bch_t rvbuf[512]; /* TODO make this dynamic in side? */ hak_oow_t rvlen; - hak_flushudio (hak); - hak_clearcode (hak); + hak_flushudio(hak); + hak_clearcode(hak); /* TODO or make hak_fmtXXXX that accepts the output function */ rvlen = hak_fmttobcstr(hak, rvbuf, HAK_COUNTOF(rvbuf), "[%O]", retv); @@ -1187,7 +1187,7 @@ hak_server_t* hak_server_open (hak_mmgr_t* mmgr, hak_oow_t xtnsize, hak_server_p oops: /* NOTE: pipe should be closed if jump to here is made after pipe() above */ if (tmr) hak_tmr_close (tmr); - if (hak) hak_close (hak); + if (hak) hak_close(hak); if (server) HAK_MMGR_FREE (mmgr, server); return HAK_NULL; } @@ -1545,7 +1545,7 @@ static int init_worker_hak (hak_server_worker_t* worker) return 0; oops: - if (hak) hak_close (hak); + if (hak) hak_close(hak); return -1; } diff --git a/lib/xchg.c b/lib/xchg.c index d1f30ad..752b6eb 100644 --- a/lib/xchg.c +++ b/lib/xchg.c @@ -545,7 +545,7 @@ int hak_unmarshalcode (hak_t* hak, hak_code_t* code, hak_xchg_reader_t rdr, void } hak_pushvolat(hak, &v); ns = hak_makefpdec(hak, v, scale); - hak_popvolat (hak); + hak_popvolat(hak); if (HAK_UNLIKELY(!ns)) goto oops; } if (hak_addliteraltocode(hak, code, ns, 0, HAK_NULL) <= -1) goto oops; @@ -758,6 +758,6 @@ int hak_addliteraltocode (hak_t* hak, hak_code_t* code, hak_oop_t obj, hak_oow_t /* make read-only an object in the literal table. * some immutable objects(e.g. literal symbol) don't need this part * but we just execute it regardless */ - if (HAK_OOP_IS_POINTER(obj)) HAK_OBJ_SET_FLAGS_RDONLY (obj, 1); + if (HAK_OOP_IS_POINTER(obj)) HAK_OBJ_SET_FLAGS_RDONLY(obj, 1); return 0; } diff --git a/lib/xma.c b/lib/xma.c index 979ac29..16286b2 100644 --- a/lib/xma.c +++ b/lib/xma.c @@ -159,7 +159,7 @@ static HAK_INLINE hak_oow_t szlog2 (hak_oow_t n) * x = log2(n); * ------------------------------------------- * unsigned int x = 0; - * while((n >> x) > 1) ++x; + * while ((n >> x) > 1) ++x; * return x; */ diff --git a/src/kernel.hak b/src/kernel.hak index 4c0adc3..7c1a30b 100644 --- a/src/kernel.hak +++ b/src/kernel.hak @@ -152,7 +152,8 @@ fun Z:abc() { printf "%d %d %d\n" a b c ## this is not recognized as .... } -fun k () { +class Q { +fun(#class) k () { k := (Z:basicNew 10) ## #varying is really required? what is the big deal even if you allow it regardless? ##k := (Z:new) ## no way to add extra fields. k:basicAtPut 2 "hello" @@ -163,4 +164,16 @@ printf "%O\n" (k:basicAt 20) ##k := (Z:new) ##k:aaa } -(k) +} + +try { +(Q:k) +} catch (e) { + printf "EXCEPTION: %O\n" e + + try { + throw 10000 + } catch (e) { + printf "EXCEPTION-X: %O\n" e + } +}