Skip to content

Commit

Permalink
External mode compiler: Compile-time evaluate binary ops of constants
Browse files Browse the repository at this point in the history
  • Loading branch information
solardiz committed May 19, 2024
1 parent 55fe305 commit d10b350
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@ Major changes from 1.9.0-jumbo-1 (May 2019) in this bleeding-edge version:

- Add Combinator external mode (combines words into pairs). [Solar; 2024]

- External mode compiler: Compile-time evaluate most constant subexpressions.
[Solar; 2024]


Major changes from 1.8.0-jumbo-1 (December 2014) to 1.9.0-jumbo-1 (May 2019):

Expand Down
22 changes: 21 additions & 1 deletion src/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,26 @@ static void (*c_push

static void c_subexpr(void (**last)(void), const struct c_op *op)
{
/* Compile-time evaluate binary op after push_imm_imm */
if (op->class == C_CLASS_BINARY && *last == c_op_push_imm_imm) {
/* Patch push_imm_imm to push_imm of operation result */
*last = c_op_push_imm;
if (c_pass) {
c_stack[1].mem = &(c_code_ptr - 2)->imm;
(c_code_ptr++)->op = op->op;
(c_code_ptr++)->op = c_op_assign_pop;
(c_code_ptr++)->op = c_op_return;
c_execute_fast(c_code_ptr -= 6);
c_code_ptr->op = c_op_push_imm;
c_code_ptr += 2;
} else
c_code_ptr--;
return;
}

/* Compile-time evaluate binary op after push_*_imm, push_imm */
/* Unimplemented, need to start tracking penultimate op first */

/* Compile-time evaluate unary op after any push ending in imm */
if (op->class == C_CLASS_LEFT && (*last == c_op_push_imm ||
*last == c_op_push_imm_imm || *last == c_op_push_mem_imm ||
Expand All @@ -457,10 +477,10 @@ static void c_subexpr(void (**last)(void), const struct c_op *op)
return;
}

*last = op->op;
if (c_pass)
c_code_ptr->op = op->op;
c_code_ptr++;
*last = op->op;
}

static int c_block(char term, struct c_ident *vars);
Expand Down

0 comments on commit d10b350

Please sign in to comment.