Skip to content

Commit

Permalink
Imported original SDCC's authors fixes
Browse files Browse the repository at this point in the history
* Fixes include tail jumping problem and left shifting bug
  • Loading branch information
lronaldo committed Aug 14, 2015
1 parent 26fb668 commit 312921a
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 69 deletions.
54 changes: 48 additions & 6 deletions cpctelera/tools/sdcc-3.5.0/src/src/z80/peeph-r2k.def
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Some of these peepholes could be potentially moved to peeph.def, but a
// GBZ80 expert should have a look at them before.
//
// (c) Philipp Klaus Krause (pkk@spth.de, philipp@colecovision.eu) 2006 - 2010
// (c) Philipp Klaus Krause (pkk@spth.de, philipp@colecovision.eu) 2006 - 2015
// *** modified by Leland Morrison for rabbit 2000 processor 2011
//
// This program is free software; you can redistribute it and/or modify it
Expand All @@ -23,7 +23,7 @@
replace restart {
ld %1, %1
} by {
; peephole 0 removed redundant load.
; peephole 0 removed redundant load from %1 into %1.
} if notVolatile(%1)

replace restart {
Expand Down Expand Up @@ -1413,6 +1413,28 @@ replace restart {
; peephole 111a incremented in hl instead of iy.
} if notUsed('hl')

replace restart {
push bc
pop iy
inc iy
} by {
inc bc
push bc
pop iy
; peephole 111b incremented in bc instead of iy.
} if notUsed('bc')

replace restart {
push de
pop iy
inc iy
} by {
inc de
push de
pop iy
; peephole 111c incremented in de instead of iy.
} if notUsed('de')

replace restart {
ld hl,%1
add hl,%2
Expand Down Expand Up @@ -1441,12 +1463,12 @@ replace restart {
} if notUsed('a')

replace restart {
call %1
call %1
ret
} by {
jp %1
jp %1
; peephole 114 replaced call at end of function by jump (tail call optimization).
}
} if symmParmStack(%1)

// Callee saves ix.
replace restart {
Expand All @@ -1457,7 +1479,7 @@ replace restart {
pop ix
jp %1
; peephole115 replaced call at end of function by jump moving call beyond pop ix (tail call optimization).
}
} if symmParmStack(%1)

replace restart {
ld %1,#%2
Expand Down Expand Up @@ -1776,6 +1798,15 @@ replace restart {
; peephole 141 used sign flag instead of testing bit 7.
}

replace restart {
dec %1
ld a, %1
or a, a
} by {
dec %1
; peephole 141a removed redundant transfer and flag setting in a
} if notUsed('a')

replace restart {
ld %1,a
or a,a
Expand Down Expand Up @@ -2105,6 +2136,9 @@ replace restart {
; peephole 152 replaced jump by return.
} if labelIsReturnOnly(%5), labelRefCountChange(%5 -1)

// Replace jp by ret before replacing jp by jr outside of jump tables, since using ret can reduce the number of references to jump labels.
barrier

replace {
jp %5
} by {
Expand Down Expand Up @@ -2140,3 +2174,11 @@ replace {
; peephole 157 changed absolute to relative conditional jump.
} if labelInRange(%5)

replace {
dec b
jr NZ, %5
} by {
djnz %5
; peephole 158 used djnz
}

32 changes: 26 additions & 6 deletions cpctelera/tools/sdcc-3.5.0/src/src/z80/peeph-tlcs90.def
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Some of these peepholes could be potentially moved to peeph.def, but a
// GBZ80 expert should have a look at them before.
//
// (c) Philipp Klaus Krause (pkk@spth.de, philipp@colecovision.eu) 2006 - 2012
// (c) Philipp Klaus Krause (pkk@spth.de, philipp@colecovision.eu) 2006 - 2015
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
Expand Down Expand Up @@ -1580,12 +1580,12 @@ replace restart {
} if notUsed('a')

replace restart {
call %1
call %1
ret
} by {
jp %1
jp %1
; peephole 114 replaced call at end of function by jump (tail call optimization).
}
} if symmParmStack(%1)

// Callee saves ix.
replace restart {
Expand All @@ -1596,7 +1596,7 @@ replace restart {
pop ix
jp %1
; peephole115 replaced call at end of function by jump moving call beyond pop ix (tail call optimization).
}
} if symmParmStack(%1)

replace restart {
ld %1,#%2
Expand Down Expand Up @@ -1642,7 +1642,7 @@ replace restart {
ld a, %2 (%3)
adc a, #%4
; peephole 119 loaded %6 from a directly instead of going through %1.
} if notUsed(%1)
} if notUsed(%1), notSame(%6 'a')

replace restart {
ld %1, a
Expand Down Expand Up @@ -1915,6 +1915,15 @@ replace restart {
; peephole 141 used sign flag instead of testing bit 7.
}

replace restart {
dec %1
ld a, %1
or a, a
} by {
dec %1
; peephole 141a removed redundant transfer and flag setting in a
} if notUsed('a')

replace restart {
ld %1,a
or a,a
Expand Down Expand Up @@ -2244,6 +2253,9 @@ replace restart {
; peephole 152 replaced jump by return.
} if labelIsReturnOnly(%5), labelRefCountChange(%5 -1)

// Replace jp by ret before replacing jp by jr outside of jump tables, since using ret can reduce the number of references to jump labels.
barrier

replace {
jp %5
} by {
Expand Down Expand Up @@ -2279,3 +2291,11 @@ replace {
; peephole 157 changed absolute to relative conditional jump.
} if labelInRange(%5)

replace {
dec b
jr NZ, %5
} by {
djnz %5
; peephole 158 used djnz
}

65 changes: 9 additions & 56 deletions cpctelera/tools/sdcc-3.5.0/src/src/z80/peeph-z80.def
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Some of these peepholes could be potentially moved to peeph.def, but a
// GBZ80 expert should have a look at them before.
//
// (c) Philipp Klaus Krause (pkk@spth.de, philipp@colecovision.eu) 2006 - 2012
// (c) Philipp Klaus Krause (pkk@spth.de, philipp@colecovision.eu) 2006 - 2015
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
Expand Down Expand Up @@ -1579,45 +1579,13 @@ replace restart {
ld sp,%1
} if notUsed('a')

// Modification to impede __z88dk_callee poisoning
// by jumping with no return address on top of parameters
// by Alcoholic Anonymous / CPCWiki
replace restart {
push %1
call %2
ret
} by {
push %1
call %2
nop
ret
; peephole 114a prevent tail code optimization for functions taking parameters
}

// 2nd Modification to impede __z88dk_callee poisoning
// by jumping with no return address on top of parameters
replace restart {
push %1
inc %2
call %3
ret
} by {
push %1
inc %2
call %3
nop
ret
; peephole 114c prevent tail code optimization for functions taking parameters
}

replace restart {
call %1
ret
call %1
ret
} by {
jp %1
; peephole 114b replaced call at end of function by jump (tail call optimization).
}

jp %1
; peephole 114 replaced call at end of function by jump (tail call optimization).
} if symmParmStack(%1)

// Callee saves ix.
replace restart {
Expand Down Expand Up @@ -1674,7 +1642,7 @@ replace restart {
ld a, %2 (%3)
adc a, #%4
; peephole 119 loaded %6 from a directly instead of going through %1.
} if notUsed(%1)
} if notUsed(%1), notSame(%6 'a')

replace restart {
ld %1, a
Expand Down Expand Up @@ -1998,22 +1966,6 @@ replace restart {
jp %3
}

///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
// POISON REMOVE
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////

barrier

// Removing nop used as poison to prevent
// tail jump optimization on functions taking parameters
replace {
nop
} by {
; poison removal
}

// These ex-generating rules should be among the last ones since ex counts as a read from both hl and de for notUsed().
barrier

Expand Down Expand Up @@ -2344,5 +2296,6 @@ replace {
jr NZ, %5
} by {
djnz %5
; peephole 156 used djnz
; peephole 158 used djnz
}

2 changes: 1 addition & 1 deletion cpctelera/tools/sdcc-3.5.0/src/src/z80/ralloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1720,7 +1720,7 @@ packRegsForAssign (iCode * ic, eBBlock * ebp)
for (dic = ic->prev; dic; dic = dic->prev)
{
/* PENDING: Don't pack across function calls. */
if (dic->op == CALL || dic->op == PCALL || dic->op == INLINEASM)
if (dic->op == CALL || dic->op == PCALL || dic->op == INLINEASM || dic->op == CRITICAL || dic->op == ENDCRITICAL)
{
dic = NULL;
break;
Expand Down

0 comments on commit 312921a

Please sign in to comment.