Reduce unnecessary push/pop in expression compilation.

This commit is contained in:
Lawrence Kesteloot 2018-08-02 14:52:23 -07:00
parent e95f2cf67f
commit 9175c4c234

17
main.c
View File

@ -371,18 +371,24 @@ static uint16_t parse_uint16(uint8_t **s_ptr) {
*/ */
static uint8_t *compile_expression(uint8_t *s) { static uint8_t *compile_expression(uint8_t *s) {
int plus_count = 0; int plus_count = 0;
char have_value_in_ax = 0;
while (1) { while (1) {
if (*s >= '0' && *s <= '9') { if (*s >= '0' && *s <= '9') {
// Parse number. // Parse number.
uint16_t value = parse_uint16(&s); uint16_t value;
if (have_value_in_ax) {
// Push on the number stack.
add_call(pushax);
}
value = parse_uint16(&s);
g_compiled[g_compiled_length++] = I_LDX; g_compiled[g_compiled_length++] = I_LDX;
g_compiled[g_compiled_length++] = value >> 8; g_compiled[g_compiled_length++] = value >> 8;
g_compiled[g_compiled_length++] = I_LDA; g_compiled[g_compiled_length++] = I_LDA;
g_compiled[g_compiled_length++] = value & 0xFF; g_compiled[g_compiled_length++] = value & 0xFF;
have_value_in_ax = 1;
// Push on the number stack.
add_call(pushax);
} else if (*s == '+') { } else if (*s == '+') {
plus_count += 1; plus_count += 1;
s += 1; s += 1;
@ -391,9 +397,6 @@ static uint8_t *compile_expression(uint8_t *s) {
} }
} }
// Pop the last value from the number stack.
add_call(popax);
while (plus_count > 0) { while (plus_count > 0) {
add_call(tosaddax); add_call(tosaddax);
plus_count -= 1; plus_count -= 1;