changing compiler code to preserve some debugging information

This commit is contained in:
hyung-hwan 2021-01-27 08:47:08 +00:00
parent 79d0385600
commit d36c12e21a
7 changed files with 55 additions and 34 deletions

View File

@ -210,13 +210,13 @@ static int emit_byte_instruction (hcl_t* hcl, hcl_oob_t bc, const hcl_ioloc_t* s
{ {
hcl_oow_t newcapa; hcl_oow_t newcapa;
hcl_oob_t* tmp; hcl_oob_t* tmp;
hcl_oow_t* tmp2; hcl_dbgl_t* tmp2;
newcapa = HCL_ALIGN(hcl->code.bc.capa + 1, HCL_BC_BUFFER_ALIGN); newcapa = HCL_ALIGN(hcl->code.bc.capa + 1, HCL_BC_BUFFER_ALIGN);
tmp = (hcl_oob_t*)hcl_reallocmem(hcl, hcl->code.bc.ptr, HCL_SIZEOF(*tmp) * newcapa); tmp = (hcl_oob_t*)hcl_reallocmem(hcl, hcl->code.bc.ptr, HCL_SIZEOF(*tmp) * newcapa);
if (HCL_UNLIKELY(!tmp)) return -1; if (HCL_UNLIKELY(!tmp)) return -1;
tmp2 = (hcl_oow_t*)hcl_reallocmem(hcl, hcl->code.locptr, HCL_SIZEOF(*tmp2) * newcapa); tmp2 = (hcl_dbgl_t*)hcl_reallocmem(hcl, hcl->code.locptr, HCL_SIZEOF(*tmp2) * newcapa);
if (HCL_UNLIKELY(!tmp2)) if (HCL_UNLIKELY(!tmp2))
{ {
hcl_freemem (hcl, tmp); hcl_freemem (hcl, tmp);
@ -231,10 +231,13 @@ static int emit_byte_instruction (hcl_t* hcl, hcl_oob_t bc, const hcl_ioloc_t* s
hcl->code.bc.ptr[hcl->code.bc.len] = bc; hcl->code.bc.ptr[hcl->code.bc.len] = bc;
/*
if (srcloc) if (srcloc)
{ {
hcl->code.locptr[hcl->code.bc.len] = srcloc->line; hcl->code.locptr[hcl->code.bc.len].fname = srcloc->file;
hcl->code.locptr[hcl->code.bc.len].sline = srcloc->line;
} }
*/
hcl->code.bc.len++; hcl->code.bc.len++;
return 0; return 0;

View File

@ -277,13 +277,13 @@ static int emit_byte_instruction (hcl_t* hcl, hcl_oob_t bc, const hcl_ioloc_t* s
{ {
hcl_oow_t newcapa; hcl_oow_t newcapa;
hcl_oob_t* tmp; hcl_oob_t* tmp;
hcl_oow_t* tmp2; hcl_dbgl_t* tmp2;
newcapa = HCL_ALIGN(hcl->code.bc.capa + 1, HCL_BC_BUFFER_ALIGN); newcapa = HCL_ALIGN(hcl->code.bc.capa + 1, HCL_BC_BUFFER_ALIGN);
tmp = (hcl_oob_t*)hcl_reallocmem(hcl, hcl->code.bc.ptr, HCL_SIZEOF(*tmp) * newcapa); tmp = (hcl_oob_t*)hcl_reallocmem(hcl, hcl->code.bc.ptr, HCL_SIZEOF(*tmp) * newcapa);
if (HCL_UNLIKELY(!tmp)) return -1; if (HCL_UNLIKELY(!tmp)) return -1;
tmp2 = (hcl_oow_t*)hcl_reallocmem(hcl, hcl->code.locptr, HCL_SIZEOF(*tmp2) * newcapa); tmp2 = (hcl_dbgl_t*)hcl_reallocmem(hcl, hcl->code.locptr, HCL_SIZEOF(*tmp2) * newcapa);
if (HCL_UNLIKELY(!tmp2)) if (HCL_UNLIKELY(!tmp2))
{ {
hcl_freemem (hcl, tmp); hcl_freemem (hcl, tmp);
@ -300,8 +300,8 @@ static int emit_byte_instruction (hcl_t* hcl, hcl_oob_t bc, const hcl_ioloc_t* s
if (srcloc) if (srcloc)
{ {
hcl->code.locptr[hcl->code.bc.len].fname = srcloc->file;
hcl->code.locptr[hcl->code.bc.len] = srcloc->line; hcl->code.locptr[hcl->code.bc.len].sline = srcloc->line;
} }
hcl->code.bc.len++; hcl->code.bc.len++;
@ -983,7 +983,7 @@ static int compile_if (hcl_t* hcl, hcl_cnode_t* src)
SWITCH_TOP_CFRAME (hcl, COP_COMPILE_OBJECT, cond); /* 1 */ SWITCH_TOP_CFRAME (hcl, COP_COMPILE_OBJECT, cond); /* 1 */
PUSH_SUBCFRAME (hcl, COP_POST_IF_COND, obj); /* 2 */ PUSH_SUBCFRAME (hcl, COP_POST_IF_COND, obj); /* 2 */
cf = GET_SUBCFRAME (hcl); cf = GET_SUBCFRAME(hcl);
cf->u.post_if.body_pos = -1; /* unknown yet */ cf->u.post_if.body_pos = -1; /* unknown yet */
cf->u.post_if.start_loc = *HCL_CNODE_GET_LOC(src); cf->u.post_if.start_loc = *HCL_CNODE_GET_LOC(src);
/* TODO: OPTIMIZATION: /* TODO: OPTIMIZATION:
@ -1242,10 +1242,10 @@ static int compile_lambda (hcl_t* hcl, hcl_cnode_t* src, int defun)
} }
} }
PUSH_SUBCFRAME (hcl, COP_EMIT_LAMBDA, HCL_SMOOI_TO_OOP(jump_inst_pos)); PUSH_SUBCFRAME (hcl, COP_EMIT_LAMBDA, src);
cf = GET_SUBCFRAME (hcl); cf = GET_SUBCFRAME (hcl);
cf->u.lambda.start_loc = *HCL_CNODE_GET_LOC(src); cf->u.lambda.jump_inst_pos = jump_inst_pos;
if (hcl->option.trait & HCL_TRAIT_INTERACTIVE) if (hcl->option.trait & HCL_TRAIT_INTERACTIVE)
{ {
@ -1256,9 +1256,10 @@ static int compile_lambda (hcl_t* hcl, hcl_cnode_t* src, int defun)
return 0; return 0;
} }
static int compile_return (hcl_t* hcl, hcl_cnode_t* src, int mode) static int compile_return (hcl_t* hcl, hcl_cnode_t* src, int ret_from_home)
{ {
hcl_cnode_t* obj, * val; hcl_cnode_t* obj, * val;
hcl_cframe2_t* cf;
HCL_ASSERT (hcl, HCL_CNODE_IS_CONS(src)); HCL_ASSERT (hcl, HCL_CNODE_IS_CONS(src));
HCL_ASSERT (hcl, HCL_CNODE_IS_SYMBOL_SYNCODED(HCL_CNODE_CONS_CAR(src), HCL_SYNCODE_RETURN) || HCL_ASSERT (hcl, HCL_CNODE_IS_SYMBOL_SYNCODED(HCL_CNODE_CONS_CAR(src), HCL_SYNCODE_RETURN) ||
@ -1293,7 +1294,9 @@ static int compile_return (hcl_t* hcl, hcl_cnode_t* src, int mode)
SWITCH_TOP_CFRAME (hcl, COP_COMPILE_OBJECT, val); SWITCH_TOP_CFRAME (hcl, COP_COMPILE_OBJECT, val);
PUSH_SUBCFRAME (hcl, COP_EMIT_RETURN, HCL_SMOOI_TO_OOP(mode)); PUSH_SUBCFRAME (hcl, COP_EMIT_RETURN, src);
cf = GET_SUBCFRAME(hcl);
cf->u._return.from_home = ret_from_home;
return 0; return 0;
} }
@ -1447,7 +1450,7 @@ static int compile_while (hcl_t* hcl, hcl_cnode_t* src, int next_cop)
SWITCH_TOP_CFRAME (hcl, COP_COMPILE_OBJECT, cond); /* 1 */ SWITCH_TOP_CFRAME (hcl, COP_COMPILE_OBJECT, cond); /* 1 */
PUSH_SUBCFRAME (hcl, next_cop, obj); /* 2 */ PUSH_SUBCFRAME (hcl, next_cop, obj); /* 2 */
cf = GET_SUBCFRAME (hcl); cf = GET_SUBCFRAME(hcl);
cf->u.post_while.cond_pos = cond_pos; cf->u.post_while.cond_pos = cond_pos;
cf->u.post_while.body_pos = -1; /* unknown yet*/ cf->u.post_while.body_pos = -1; /* unknown yet*/
cf->u.post_while.start_loc = *HCL_CNODE_GET_LOC(src); cf->u.post_while.start_loc = *HCL_CNODE_GET_LOC(src);
@ -2361,13 +2364,12 @@ static int compile_qlist (hcl_t* hcl)
else else
{ {
hcl_cnode_t* car, * cdr; hcl_cnode_t* car, * cdr;
hcl_ooi_t oldidx;
if (!HCL_CNODE_IS_CONS(oprnd)) if (!HCL_CNODE_IS_CONS(oprnd))
{ {
/* the last element after . */ /* the last element after . */
SWITCH_TOP_CFRAME (hcl, COP_COMPILE_OBJECT, oprnd); SWITCH_TOP_CFRAME (hcl, COP_COMPILE_OBJECT, oprnd);
PUSH_SUBCFRAME (hcl, COP_EMIT_POP_INTO_CONS_CDR, HCL_NULL); PUSH_SUBCFRAME (hcl, COP_EMIT_POP_INTO_CONS_CDR, oprnd); /* TODO: can i pass the location of the closing )? */
} }
else else
{ {
@ -2378,12 +2380,12 @@ static int compile_qlist (hcl_t* hcl)
if (cdr) if (cdr)
{ {
PUSH_SUBCFRAME (hcl, COP_COMPILE_QLIST, cdr); /* 3 */ PUSH_SUBCFRAME (hcl, COP_COMPILE_QLIST, cdr); /* 3 */
PUSH_SUBCFRAME (hcl, COP_EMIT_POP_INTO_CONS, HCL_NULL); /* 2 */ PUSH_SUBCFRAME (hcl, COP_EMIT_POP_INTO_CONS, oprnd); /* 2 */
} }
else else
{ {
/* the last element */ /* the last element */
PUSH_SUBCFRAME (hcl, COP_EMIT_POP_INTO_CONS_END, HCL_NULL); /* 2 */ PUSH_SUBCFRAME (hcl, COP_EMIT_POP_INTO_CONS_END, oprnd); /* 2 */
} }
} }
} }
@ -2479,7 +2481,7 @@ static HCL_INLINE int subcompile_elif (hcl_t* hcl)
SWITCH_TOP_CFRAME (hcl, COP_COMPILE_OBJECT, cond); /* 1 */ SWITCH_TOP_CFRAME (hcl, COP_COMPILE_OBJECT, cond); /* 1 */
PUSH_SUBCFRAME (hcl, COP_POST_IF_COND, obj); /* 2 */ PUSH_SUBCFRAME (hcl, COP_POST_IF_COND, obj); /* 2 */
cf = GET_SUBCFRAME (hcl); cf = GET_SUBCFRAME(hcl);
cf->u.post_if.body_pos = -1; /* unknown yet */ cf->u.post_if.body_pos = -1; /* unknown yet */
cf->u.post_if.start_loc = *HCL_CNODE_GET_LOC(src); cf->u.post_if.start_loc = *HCL_CNODE_GET_LOC(src);
@ -2953,9 +2955,9 @@ static HCL_INLINE int emit_pop_into_cons (hcl_t* hcl, int cmd)
HCL_ASSERT (hcl, cf->opcode == COP_EMIT_POP_INTO_CONS || HCL_ASSERT (hcl, cf->opcode == COP_EMIT_POP_INTO_CONS ||
cf->opcode == COP_EMIT_POP_INTO_CONS_END || cf->opcode == COP_EMIT_POP_INTO_CONS_END ||
cf->opcode == COP_EMIT_POP_INTO_CONS_CDR); cf->opcode == COP_EMIT_POP_INTO_CONS_CDR);
HCL_ASSERT (hcl, cf->operand == HCL_NULL); HCL_ASSERT (hcl, cf->operand != HCL_NULL);
n = emit_byte_instruction (hcl, cmd, HCL_NULL); n = emit_byte_instruction (hcl, cmd, HCL_CNODE_GET_LOC(cf->operand));
POP_CFRAME (hcl); POP_CFRAME (hcl);
return n; return n;
@ -2969,9 +2971,9 @@ static HCL_INLINE int emit_lambda (hcl_t* hcl)
cf = GET_TOP_CFRAME(hcl); cf = GET_TOP_CFRAME(hcl);
HCL_ASSERT (hcl, cf->opcode == COP_EMIT_LAMBDA); HCL_ASSERT (hcl, cf->opcode == COP_EMIT_LAMBDA);
HCL_ASSERT (hcl, HCL_OOP_IS_SMOOI(cf->operand)); HCL_ASSERT (hcl, cf->operand != HCL_NULL);
jip = HCL_OOP_TO_SMOOI(cf->operand); jip = cf->u.lambda.jump_inst_pos;
if (hcl->option.trait & HCL_TRAIT_INTERACTIVE) if (hcl->option.trait & HCL_TRAIT_INTERACTIVE)
lfsize = hcl->code.lit.len - hcl->c->blk.info[hcl->c->blk.depth].lfbase; lfsize = hcl->code.lit.len - hcl->c->blk.info[hcl->c->blk.depth].lfbase;
@ -2987,16 +2989,16 @@ static HCL_INLINE int emit_lambda (hcl_t* hcl)
{ {
/* no body in lambda - (lambda (a b c)) */ /* no body in lambda - (lambda (a b c)) */
/* TODO: is this correct??? */ /* TODO: is this correct??? */
if (emit_byte_instruction(hcl, HCL_CODE_PUSH_NIL, HCL_NULL) <= -1) return -1; if (emit_byte_instruction(hcl, HCL_CODE_PUSH_NIL, HCL_CNODE_GET_LOC(cf->operand)) <= -1) return -1;
block_code_size++; block_code_size++;
} }
if (emit_byte_instruction(hcl, HCL_CODE_RETURN_FROM_BLOCK, HCL_NULL) <= -1) return -1; if (emit_byte_instruction(hcl, HCL_CODE_RETURN_FROM_BLOCK, HCL_CNODE_GET_LOC(cf->operand)) <= -1) return -1;
block_code_size++; block_code_size++;
if (block_code_size > MAX_CODE_JUMP * 2) if (block_code_size > MAX_CODE_JUMP * 2)
{ {
hcl_setsynerrbfmt (hcl, HCL_SYNERR_BLKFLOOD, &cf->u.lambda.start_loc, HCL_NULL, "code too big - size %zu", block_code_size); hcl_setsynerrbfmt (hcl, HCL_SYNERR_BLKFLOOD, HCL_CNODE_GET_LOC(cf->operand), HCL_NULL, "code too big - size %zu", block_code_size);
return -1; return -1;
} }
patch_long_jump (hcl, jip, block_code_size); patch_long_jump (hcl, jip, block_code_size);
@ -3030,9 +3032,9 @@ static HCL_INLINE int emit_return (hcl_t* hcl)
cf = GET_TOP_CFRAME(hcl); cf = GET_TOP_CFRAME(hcl);
HCL_ASSERT (hcl, cf->opcode == COP_EMIT_RETURN); HCL_ASSERT (hcl, cf->opcode == COP_EMIT_RETURN);
HCL_ASSERT (hcl, HCL_OOP_IS_SMOOI(cf->operand)); HCL_ASSERT (hcl, cf->operand != HCL_NULL);
n = emit_byte_instruction(hcl, (HCL_OOP_TO_SMOOI(cf->operand) == 0? HCL_CODE_RETURN_FROM_BLOCK: HCL_CODE_RETURN_STACKTOP), HCL_NULL); n = emit_byte_instruction(hcl, (cf->u._return.from_home? HCL_CODE_RETURN_STACKTOP: HCL_CODE_RETURN_FROM_BLOCK), HCL_CNODE_GET_LOC(cf->operand));
POP_CFRAME (hcl); POP_CFRAME (hcl);
return n; return n;

View File

@ -3649,6 +3649,8 @@ hcl_oop_t hcl_execute (hcl_t* hcl)
func = make_function(hcl, hcl->code.lit.len, hcl->code.bc.ptr, hcl->code.bc.len); func = make_function(hcl, hcl->code.lit.len, hcl->code.bc.ptr, hcl->code.bc.len);
if (HCL_UNLIKELY(!func)) return HCL_NULL; if (HCL_UNLIKELY(!func)) return HCL_NULL;
/* TODO: copy the debug information as well into the dbgi field of the function object. */
/* pass nil for the home context of the initial function */ /* pass nil for the home context of the initial function */
fill_function_data (hcl, func, 0, 0, (hcl_oop_context_t)hcl->_nil, hcl->code.lit.arr->slot, hcl->code.lit.len); fill_function_data (hcl, func, 0, 0, (hcl_oop_context_t)hcl->_nil, hcl->code.lit.arr->slot, hcl->code.lit.len);

View File

@ -47,7 +47,7 @@
#define HCL_ENABLE_FLTFMT #define HCL_ENABLE_FLTFMT
#if defined(HCL_BUILD_DEBUG) #if defined(HCL_BUILD_DEBUG)
#define HCL_DEBUG_LEXER 1 /*#define HCL_DEBUG_LEXER 1*/
#define HCL_DEBUG_VM_PROCESSOR 1 #define HCL_DEBUG_VM_PROCESSOR 1
#define HCL_DEBUG_VM_EXEC 1 #define HCL_DEBUG_VM_EXEC 1
/*#define HCL_PROFILE_VM 1*/ /*#define HCL_PROFILE_VM 1*/
@ -349,10 +349,15 @@ struct hcl_cframe2_t
struct struct
{ {
hcl_ioloc_t start_loc; hcl_oow_t jump_inst_pos;
hcl_ooi_t lfbase_pos; hcl_ooi_t lfbase_pos;
hcl_ooi_t lfsize_pos; hcl_ooi_t lfsize_pos;
} lambda; } lambda;
struct
{
int from_home;
} _return;
} u; } u;
}; };
typedef struct hcl_cframe2_t hcl_cframe2_t; typedef struct hcl_cframe2_t hcl_cframe2_t;

View File

@ -1373,6 +1373,14 @@ struct hcl_synerr_t
} tgt; } tgt;
}; };
typedef struct hcl_dbgl_t hcl_dbgl_t;
struct hcl_dbgl_t
{
const hcl_ooch_t* fname; /* file name */
hcl_oow_t sline; /* source line in the file */
};
#if defined(HCL_INCLUDE_COMPILER) #if defined(HCL_INCLUDE_COMPILER)
typedef struct hcl_compiler_t hcl_compiler_t; typedef struct hcl_compiler_t hcl_compiler_t;
typedef struct hcl_cnode_t hcl_cnode_t; typedef struct hcl_cnode_t hcl_cnode_t;
@ -1567,7 +1575,8 @@ struct hcl_t
} lit; } lit;
/* array that hold the location of the byte code emitted */ /* array that hold the location of the byte code emitted */
hcl_oow_t* locptr;
hcl_dbgl_t* locptr;
} code; } code;
/* == PRINTER == */ /* == PRINTER == */

View File

@ -1299,7 +1299,7 @@ static const hcl_ooch_t* add_io_name (hcl_t* hcl, const hcl_oocs_t* name)
hcl_iolink_t* link; hcl_iolink_t* link;
hcl_ooch_t* ptr; hcl_ooch_t* ptr;
link = (hcl_iolink_t*)hcl_callocmem (hcl, HCL_SIZEOF(*link) + HCL_SIZEOF(hcl_ooch_t) * (name->len + 1)); link = (hcl_iolink_t*)hcl_callocmem(hcl, HCL_SIZEOF(*link) + HCL_SIZEOF(hcl_ooch_t) * (name->len + 1));
if (HCL_UNLIKELY(!link)) return HCL_NULL; if (HCL_UNLIKELY(!link)) return HCL_NULL;
ptr = (hcl_ooch_t*)(link + 1); ptr = (hcl_ooch_t*)(link + 1);

View File

@ -1319,10 +1319,10 @@ static int begin_include (hcl_t* hcl)
hcl_ioinarg_t* arg; hcl_ioinarg_t* arg;
const hcl_ooch_t* io_name; const hcl_ooch_t* io_name;
io_name = add_io_name (hcl, TOKEN_NAME(hcl)); io_name = add_io_name(hcl, TOKEN_NAME(hcl));
if (!io_name) return -1; if (HCL_UNLIKELY(!io_name)) return -1;
arg = (hcl_ioinarg_t*)hcl_callocmem (hcl, HCL_SIZEOF(*arg)); arg = (hcl_ioinarg_t*)hcl_callocmem(hcl, HCL_SIZEOF(*arg));
if (HCL_UNLIKELY(!arg)) goto oops; if (HCL_UNLIKELY(!arg)) goto oops;
arg->name = io_name; arg->name = io_name;