stage1 重构到 USIZE, 使得 vm 支持 usize, isize
This commit is contained in:
@@ -124,14 +124,16 @@ static void parse_var_decl(spl_comp_t *ctx, int is_const) {
|
||||
* Store len at slot+1, then ptr at slot. */
|
||||
spl_emit(ctx, SPL_LADDR, SPL_PTR, slot + 1);
|
||||
spl_emit(ctx, SPL_SWAP, SPL_VOID, 0);
|
||||
spl_emit(ctx, SPL_STORE, SPL_I32, 0);
|
||||
spl_emit(ctx, SPL_STORE, SPL_USIZE, 0);
|
||||
spl_emit(ctx, SPL_LADDR, SPL_PTR, slot);
|
||||
spl_emit(ctx, SPL_SWAP, SPL_VOID, 0);
|
||||
spl_emit(ctx, SPL_STORE, SPL_PTR, 0);
|
||||
} else {
|
||||
/* Single value store */
|
||||
spl_emit(ctx, SPL_LADDR, SPL_PTR, slot);
|
||||
spl_type_t bt = var_type->kind == TYPE_BASIC ? var_type->basic_type : SPL_I32;
|
||||
spl_type_t bt = (var_type->kind == TYPE_BASIC) ? var_type->basic_type
|
||||
: (var_type->kind == TYPE_PTR) ? SPL_PTR
|
||||
: SPL_I32;
|
||||
spl_emit(ctx, SPL_SWAP, SPL_VOID, 0);
|
||||
spl_emit(ctx, SPL_STORE, bt, 0);
|
||||
}
|
||||
@@ -293,23 +295,23 @@ static void parse_for_stmt(spl_comp_t *ctx) {
|
||||
iname[inl] = '\0';
|
||||
|
||||
spl_push_scope(ctx);
|
||||
int islot = spl_declare_var(ctx, iname, spl_type_basic(SPL_I32), 0);
|
||||
int islot = spl_declare_var(ctx, iname, spl_type_basic(SPL_USIZE), 0);
|
||||
|
||||
/* Stack: [begin, end]; swap so TOS = begin */
|
||||
spl_emit(ctx, SPL_SWAP, SPL_VOID, 0);
|
||||
/* Store begin to i */
|
||||
spl_emit(ctx, SPL_LADDR, SPL_PTR, islot);
|
||||
spl_emit(ctx, SPL_SWAP, SPL_VOID, 0);
|
||||
spl_emit(ctx, SPL_STORE, SPL_I32, 0);
|
||||
spl_emit(ctx, SPL_STORE, SPL_USIZE, 0);
|
||||
/* Stack: [end] */
|
||||
|
||||
spl_val_t loop_start = vec_size(ctx->prog.insns);
|
||||
|
||||
/* Condition: i < end */
|
||||
spl_emit(ctx, SPL_LADDR, SPL_PTR, islot);
|
||||
spl_emit(ctx, SPL_LOAD, SPL_I32, 0);
|
||||
spl_emit(ctx, SPL_LOAD, SPL_USIZE, 0);
|
||||
spl_emit(ctx, SPL_PICK, SPL_VOID, 1);
|
||||
spl_emit(ctx, SPL_SLT, SPL_I32, 0);
|
||||
spl_emit(ctx, SPL_ULT, SPL_USIZE, 0);
|
||||
spl_val_t bz_addr = spl_emit_bz(ctx);
|
||||
|
||||
int saved_loop = ctx->in_loop;
|
||||
@@ -324,10 +326,10 @@ static void parse_for_stmt(spl_comp_t *ctx) {
|
||||
/* Increment: i = i + 1 */
|
||||
spl_emit(ctx, SPL_LADDR, SPL_PTR, islot);
|
||||
spl_emit(ctx, SPL_LADDR, SPL_PTR, islot);
|
||||
spl_emit(ctx, SPL_LOAD, SPL_I32, 0);
|
||||
spl_emit(ctx, SPL_PUSH, SPL_I32, 1);
|
||||
spl_emit(ctx, SPL_ADD, SPL_I32, 0);
|
||||
spl_emit(ctx, SPL_STORE, SPL_I32, 0);
|
||||
spl_emit(ctx, SPL_LOAD, SPL_USIZE, 0);
|
||||
spl_emit(ctx, SPL_PUSH, SPL_USIZE, 1);
|
||||
spl_emit(ctx, SPL_ADD, SPL_USIZE, 0);
|
||||
spl_emit(ctx, SPL_STORE, SPL_USIZE, 0);
|
||||
|
||||
/* JMP back to condition */
|
||||
spl_val_t jmp_here = vec_size(ctx->prog.insns);
|
||||
@@ -395,7 +397,7 @@ static void parse_for_stmt(spl_comp_t *ctx) {
|
||||
|
||||
int idx_slot = -1;
|
||||
if (iname[0])
|
||||
idx_slot = spl_declare_var(ctx, iname, spl_type_basic(SPL_I32), 0);
|
||||
idx_slot = spl_declare_var(ctx, iname, spl_type_basic(SPL_USIZE), 0);
|
||||
|
||||
spl_type_info_t *elem_type = NULL;
|
||||
if (start_expr.type) {
|
||||
@@ -414,8 +416,8 @@ static void parse_for_stmt(spl_comp_t *ctx) {
|
||||
spl_emit(ctx, SPL_SWAP, SPL_VOID, 0);
|
||||
spl_emit(ctx, SPL_PUSH, SPL_U64, sizeof(spl_val_t));
|
||||
spl_emit(ctx, SPL_ADD, SPL_U64, 0);
|
||||
spl_emit(ctx, SPL_LOAD, SPL_I32, 0);
|
||||
spl_emit(ctx, SPL_PUSH, SPL_I32, 0);
|
||||
spl_emit(ctx, SPL_LOAD, SPL_USIZE, 0);
|
||||
spl_emit(ctx, SPL_PUSH, SPL_USIZE, 0);
|
||||
/* Stack: [ptr, len, idx=0] */
|
||||
|
||||
spl_val_t loop_start = vec_size(ctx->prog.insns);
|
||||
@@ -424,7 +426,7 @@ static void parse_for_stmt(spl_comp_t *ctx) {
|
||||
spl_emit(ctx, SPL_PICK, SPL_VOID, 1); /* copy len */
|
||||
spl_emit(ctx, SPL_PICK, SPL_VOID, 1); /* copy idx */
|
||||
spl_emit(ctx, SPL_SWAP, SPL_VOID, 0); /* [idx, len] → [len, idx] → SWAP → [idx, len] */
|
||||
spl_emit(ctx, SPL_SLT, SPL_I32, 0);
|
||||
spl_emit(ctx, SPL_ULT, SPL_USIZE, 0);
|
||||
spl_val_t bz_addr = spl_emit_bz(ctx);
|
||||
|
||||
/* Store current idx to idx variable */
|
||||
@@ -432,7 +434,7 @@ static void parse_for_stmt(spl_comp_t *ctx) {
|
||||
spl_emit(ctx, SPL_PICK, SPL_VOID, 0); /* copy idx (TOS) */
|
||||
spl_emit(ctx, SPL_LADDR, SPL_PTR, idx_slot);
|
||||
spl_emit(ctx, SPL_SWAP, SPL_VOID, 0);
|
||||
spl_emit(ctx, SPL_STORE, SPL_I32, 0);
|
||||
spl_emit(ctx, SPL_STORE, SPL_USIZE, 0);
|
||||
}
|
||||
|
||||
/* Load slice[idx] and store to val */
|
||||
@@ -459,8 +461,8 @@ static void parse_for_stmt(spl_comp_t *ctx) {
|
||||
|
||||
/* Increment idx */
|
||||
spl_emit(ctx, SPL_PICK, SPL_VOID, 0); /* copy idx */
|
||||
spl_emit(ctx, SPL_PUSH, SPL_I32, 1);
|
||||
spl_emit(ctx, SPL_ADD, SPL_I32, 0);
|
||||
spl_emit(ctx, SPL_PUSH, SPL_USIZE, 1);
|
||||
spl_emit(ctx, SPL_ADD, SPL_USIZE, 0);
|
||||
spl_emit(ctx, SPL_SWAP, SPL_VOID, 0);
|
||||
spl_emit(ctx, SPL_DROP, SPL_VOID, 0); /* replace old idx with new */
|
||||
|
||||
@@ -629,14 +631,23 @@ static void parse_expr_stmt(spl_comp_t *ctx) {
|
||||
|
||||
int prev = ctx->tok_idx;
|
||||
|
||||
/* Look ahead past newlines for assignment operator */
|
||||
/* Look ahead for assignment operator (scan past expression tokens) */
|
||||
int is_assign = 0;
|
||||
if (peek(ctx)->type == TOK_IDENT) {
|
||||
usize look = ctx->tok_idx + 1;
|
||||
while (look < vec_size(ctx->toks) && vec_at(ctx->toks, look).type == TOK_ENDLINE)
|
||||
{
|
||||
usize look = ctx->tok_idx;
|
||||
while (look < vec_size(ctx->toks)) {
|
||||
spl_tok_type_t t = vec_at(ctx->toks, look).type;
|
||||
if (t == TOK_SEMICOLON || t == TOK_ENDLINE || t == TOK_L_BRACE || t == TOK_R_BRACE ||
|
||||
t == TOK_EOF)
|
||||
break;
|
||||
if (t == TOK_L_PAREN)
|
||||
break; /* function call, not assignment */
|
||||
if (is_assign_op(t)) {
|
||||
is_assign = 1;
|
||||
break;
|
||||
}
|
||||
look++;
|
||||
if (look < vec_size(ctx->toks))
|
||||
is_assign = is_assign_op(vec_at(ctx->toks, look).type);
|
||||
}
|
||||
}
|
||||
|
||||
if (is_assign)
|
||||
|
||||
Reference in New Issue
Block a user