Skip to content

Commit 50b1484

Browse files
committed
Skip non-tokens in peephole optimization passes.
This makes the passes faster, as we don't need to check data values for many different token types.
1 parent 0689289 commit 50b1484

1 file changed

Lines changed: 48 additions & 10 deletions

File tree

src/compiler/peephole.cc

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ class peephole
196196
{
197197
for(size_t i = 0; i < code.size(); i++)
198198
{
199+
// Skip non-tokens
200+
if(!code[i].is_tok())
201+
continue;
202+
199203
current = i;
200204
// Sequences:
201205
// TOK_BYTE / x
@@ -226,6 +230,10 @@ class peephole
226230
{
227231
for(size_t i = 0; i < code.size(); i++)
228232
{
233+
// Skip non-tokens
234+
if(!code[i].is_tok())
235+
continue;
236+
229237
current = i;
230238
// Sequences:
231239
// TOK_NUM / x == 0
@@ -259,6 +267,10 @@ class peephole
259267
{
260268
for(size_t i = 0; i < code.size(); i++)
261269
{
270+
// Skip non-tokens
271+
if(!code[i].is_tok())
272+
continue;
273+
262274
current = i;
263275
if(mtok(0, "TOK_PUSH_BYTE"))
264276
{
@@ -405,7 +417,13 @@ class peephole
405417
int ioch = 0;
406418
for(current = 0; current < code.size(); current++)
407419
{
408-
if(mlabel(0) || mtok(0, "TOK_CALL"))
420+
if(!code[current].is_tok())
421+
{
422+
if(mlabel(0))
423+
ioch = 0; // Assume 0 after any label or CALL
424+
continue;
425+
}
426+
if(mtok(0, "TOK_CALL"))
409427
ioch = 0; // Assume 0 after any label or CALL
410428
else if(mtok(0, "TOK_NUM") && mword(1) && mtok(2, "TOK_IOCHN"))
411429
{
@@ -468,6 +486,9 @@ class peephole
468486
{
469487
for(current = 0; current < code.size(); current++)
470488
{
489+
if(!code[current].is_tok())
490+
continue;
491+
471492
if((mtok(0, "TOK_CJUMP") || mtok(0, "TOK_CNJUMP") ||
472493
mtok(0, "TOK_JUMP")) &&
473494
mlblw(1) && tgt.find(wlbl(1)) != tgt.end())
@@ -511,11 +532,37 @@ class peephole
511532
replace_label_targets();
512533
trace_iochn();
513534
int print_color = 0;
535+
// Tracks last top-of-stack value, if known
536+
int last_TOS_value = -1;
514537

515538
for(size_t i = 0; i < code.size(); i++)
516539
{
540+
541+
// Only go through tokens
542+
if(!code[i].is_tok())
543+
{
544+
// Invalidate TOS after a label
545+
if(code[i].is_label())
546+
last_TOS_value = -1;
547+
continue;
548+
}
549+
517550
current = i;
518551

552+
// Track current print color
553+
if(mtok(0, "TOK_BYTE_POKE") && mcbyte(1, "PRINT_COLOR"))
554+
{
555+
print_color = last_TOS_value;
556+
last_TOS_value = -1;
557+
continue;
558+
}
559+
560+
// Track TOS value
561+
if(mtok(0, "TOK_NUM") && mword(1))
562+
last_TOS_value = val(1);
563+
else
564+
last_TOS_value = -1;
565+
519566
// Remove extra PRINT COLOR
520567
if(mtok(0, "TOK_NUM") && mword(1) && mtok(2, "TOK_BYTE_POKE") &&
521568
mcbyte(3, "PRINT_COLOR"))
@@ -532,15 +579,6 @@ class peephole
532579
}
533580
}
534581

535-
// Track current print color
536-
if(mtok(2, "TOK_BYTE_POKE") && mcbyte(3, "PRINT_COLOR"))
537-
{
538-
if(mtok(0, "TOK_NUM") && mword(1))
539-
print_color = val(1);
540-
else
541-
print_color = -1;
542-
}
543-
544582
// Sequences:
545583
// TOK_NUM / x / TOK_NEG -> TOK_NUM / -x
546584
if(mtok(0, "TOK_NUM") && mword(1) && mtok(2, "TOK_NEG"))

0 commit comments

Comments
 (0)