added the code_start_loc field to moo_method_data_t and updated emit_byte_instruction()

to get distance of the instruction being emitted
This commit is contained in:
hyunghwan.chung 2019-07-06 09:11:25 +00:00
parent 70eaee601e
commit c334b0af28
2 changed files with 18 additions and 2 deletions

View File

@ -2513,7 +2513,20 @@ static MOO_INLINE int emit_byte_instruction (moo_t* moo, moo_oob_t code, const m
} }
cc->mth.code.ptr[cc->mth.code.len] = code; cc->mth.code.ptr[cc->mth.code.len] = code;
if (srcloc) cc->mth.code.locptr[cc->mth.code.len] = srcloc->line; if (srcloc)
{
/* store the distance from the method body start */
if (srcloc->file == cc->mth.code_start_loc.file && srcloc->line >= cc->mth.code_start_loc.line)
{
cc->mth.code.locptr[cc->mth.code.len] = srcloc->line - cc->mth.code_start_loc.line;
}
else
{
/* i can't record the distance as the method body is not in the same file */
/* TODO: warning, error handling? */
cc->mth.code.locptr[cc->mth.code.len] = 0;
}
}
cc->mth.code.len++; cc->mth.code.len++;
return 0; return 0;
@ -4148,7 +4161,7 @@ static int compile_method_name (moo_t* moo, moo_method_data_t* mth)
MOO_ASSERT (moo, mth->tmpr_count == 0); MOO_ASSERT (moo, mth->tmpr_count == 0);
mth->name_loc = moo->c->tok.loc; mth->name_loc = *TOKEN_LOC(moo);
switch (TOKEN_TYPE(moo)) switch (TOKEN_TYPE(moo))
{ {
case MOO_IOTOK_IDENT: case MOO_IOTOK_IDENT:
@ -7035,6 +7048,7 @@ static void reset_method_data (moo_t* moo, moo_method_data_t* mth)
mth->pfnum = 0; mth->pfnum = 0;
mth->blk_depth = 0; mth->blk_depth = 0;
mth->code.len = 0; mth->code.len = 0;
MOO_MEMSET (&mth->code_start_loc, 0, MOO_SIZEOF(mth->code_start_loc));
} }
static int process_method_modifiers (moo_t* moo, moo_method_data_t* mth) static int process_method_modifiers (moo_t* moo, moo_method_data_t* mth)
@ -7309,6 +7323,7 @@ static int __compile_method_definition (moo_t* moo)
return -1; return -1;
} }
cc->mth.code_start_loc = *TOKEN_LOC(moo); /* set it to the location of the opening brace */
GET_TOKEN (moo); GET_TOKEN (moo);
if (compile_method_temporaries(moo) <= -1 || if (compile_method_temporaries(moo) <= -1 ||

View File

@ -533,6 +533,7 @@ struct moo_method_data_t
/* byte code */ /* byte code */
moo_code_t code; moo_code_t code;
moo_ioloc_t code_start_loc; /* source location of the method body opening brace */
}; };
typedef struct moo_cunit_class_t moo_cunit_class_t; typedef struct moo_cunit_class_t moo_cunit_class_t;