Skip to content

Commit

Permalink
Added peephole for address calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
totalspectrum committed May 6, 2024
1 parent 97e92ff commit 52c2fad
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 34 deletions.
1 change: 1 addition & 0 deletions Changelog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version 6.9.6
- Added a peephole for some address calculations (as suggested by Evan)
- Fixed some built-in functions in spin2cpp

Version 6.9.5
Expand Down
3 changes: 1 addition & 2 deletions Test/Expect/stest064.pasm
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ dat
entry

_getptr
add objptr, #16
mov result1, objptr
sub objptr, #16
add result1, #16
_getptr_ret
ret

Expand Down
4 changes: 1 addition & 3 deletions Test/Expect/stest086.pasm
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ entry

_update
mov _var01, #0
add objptr, #4
mov _var02, objptr
sub objptr, #4
LR__0001
add _var02, #4
cmps _var01, #10 wc
if_b rdlong _var03, _var02
if_b add _var03, #1
if_b wrlong _var03, _var02
if_b add _var01, #1
if_b add _var02, #4
if_b jmp #LR__0001
_update_ret
ret
Expand Down
3 changes: 1 addition & 2 deletions Test/Expect/stest088.pasm
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ entry

_clr
mov _var01, #10
add objptr, #40
mov _var02, objptr
sub objptr, #40
add _var02, #40
LR__0001
sub _var02, #4
mov _var03, #0
Expand Down
3 changes: 1 addition & 2 deletions Test/Expect/stest156.pasm
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ _getbaz_ret
ret

_getbazaddr
add ptr__dat__, #12
mov result1, ptr__dat__
sub ptr__dat__, #12
add result1, #12
_getbazaddr_ret
ret

Expand Down
3 changes: 1 addition & 2 deletions Test/Expect/stest184.pasm
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,9 @@ __system___gc_doalloc
add __system___gc_doalloc_size, #23
andn __system___gc_doalloc_size, #15
shr __system___gc_doalloc_size, #4
add ptr___system__dat__, #44
mov __system___gc_doalloc__cse__0005, ptr___system__dat__
add __system___gc_doalloc__cse__0005, #44
mov arg01, __system___gc_doalloc__cse__0005
sub ptr___system__dat__, #44
cogid result1
add result1, #256
LR__0030
Expand Down
4 changes: 1 addition & 3 deletions Test/Expect/stest219.pasm
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ entry

_main
mov outa, #4
add ptr__dat__, #4
mov _var01, ptr__dat__
sub _var01, #2
sub ptr__dat__, #4
add _var01, #2
mov outb, _var01
_main_ret
ret
Expand Down
12 changes: 6 additions & 6 deletions Test/Expect/stest227.pasm
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ _f3
mov result1, arg01
shl result1, #1
add result1, arg01
add ptr__dat__, #4
add result1, ptr__dat__
mov arg01, ptr__dat__
add arg01, #4
add result1, arg01
add arg02, result1
rdbyte result1, arg02
sub ptr__dat__, #4
_f3_ret
ret

_f2
mov result1, arg01
shl result1, #1
add result1, arg01
add ptr__dat__, #10
add result1, ptr__dat__
mov arg01, ptr__dat__
add arg01, #10
add result1, arg01
add arg02, result1
rdbyte result1, arg02
sub ptr__dat__, #10
_f2_ret
ret

Expand Down
12 changes: 7 additions & 5 deletions Test/Expect/stest279.pasm
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ dat
entry

_fetch
add ptr__dat__, #1
rdbyte result1, ptr__dat__
mov _var01, #88
wrbyte _var01, ptr__dat__
sub ptr__dat__, #1
mov _var01, ptr__dat__
add _var01, #1
rdbyte result1, _var01
mov _var02, #88
wrbyte _var02, _var01
_fetch_ret
ret

Expand All @@ -26,4 +26,6 @@ _dat_
org COG_BSS_START
_var01
res 1
_var02
res 1
fit 496
12 changes: 6 additions & 6 deletions Test/Expect/stest283.pasm
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ _fetchy
shl result1, #1
add result1, arg01
shl result1, #3
add objptr, #96
add result1, objptr
mov arg01, objptr
add arg01, #96
add result1, arg01
shl arg02, #2
add arg02, result1
rdlong result1, arg02
sub objptr, #96
_fetchy_ret
ret

Expand All @@ -35,12 +35,12 @@ _fetchz
shl result1, #1
add result1, arg01
shl result1, #3
add objptr, #192
add result1, objptr
mov arg01, objptr
add arg01, #192
add result1, arg01
shl arg02, #2
add arg02, result1
rdlong result1, arg02
sub objptr, #192
_fetchz_ret
ret

Expand Down
3 changes: 1 addition & 2 deletions Test/Expect/stest293.pasm
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ _getval_ret

_getval2
mov arg01, ptr__dat__
add ptr__dat__, #4
mov arg02, ptr__dat__
sub ptr__dat__, #4
add arg02, #4
call #_getval
_getval2_ret
ret
Expand Down
26 changes: 26 additions & 0 deletions backends/asm/asm_peep.c
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,30 @@ static int FixupSignxAndImm(int arg, IRList *irl, IR *ir0)
return 0;
}

// add objptr, #N
// mov x, objptr
// sub objptr, #N
// This happens often enough that it's worth optimizing to
// mov x, objptr
// add x, #N
static PeepholePattern pat_lea_ptr[] = {
{ COND_TRUE, OPC_ADD, PEEP_OP_SET|0, PEEP_OP_SET|1, PEEP_FLAGS_NONE },
{ COND_TRUE, OPC_MOV, PEEP_OP_SET|2, PEEP_OP_MATCH|0, PEEP_FLAGS_NONE },
{ COND_TRUE, OPC_SUB, PEEP_OP_MATCH|0, PEEP_OP_MATCH|1, PEEP_FLAGS_NONE },
{ 0, 0, 0, 0, PEEP_FLAGS_DONE }
};

static int FixupLeaPtr(int arg, IRList *irl, IR *ir) {
IR *next_ir = NextIR(ir);
IR *last_ir = NextIR(next_ir);
Operand *dest = next_ir->dst;

ReplaceOpcode(last_ir, OPC_ADD);
last_ir->dst = dest;
DeleteIR(irl, ir);
return 1;
}

/*
* the actual list of peepholes
*/
Expand Down Expand Up @@ -1414,6 +1438,8 @@ struct Peepholes {
{ pat_jmp_jmp, 1, FixupDeleteInstr },

{ pat_mov255_and, 1, FixupMov255And },

{ pat_lea_ptr, 0, FixupLeaPtr },
};


Expand Down
2 changes: 1 addition & 1 deletion version.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#define VERSION_MAJOR 6
#define VERSION_MINOR 9
#define VERSION_REV 6
#define BETA "-beta"
//#define BETA "-beta"

#define VERSIONSTR version_string

Expand Down

0 comments on commit 52c2fad

Please sign in to comment.