Consider the following C function:
<c> int *traverse(int vec[], int lim) {
int *ptr = vec + lim - 1, ix = lim - 2; while (ix >= 0) { ptr = (vec[ix] > *ptr) ? vec + ix : ptr; ix--; } return ptr;
} </c>
The Postfix code for the above function is as follows:
<asm> TEXT ALIGN GLOBL traverse, FUNC LABEL traverse ENTER 8 ; ptr@-4 ix@-8 vec@+8 lim@+12
LOCV +8 ; vec LOCV +12 ; lim INT 4 ; sizeof int MUL ADD ; vec + lim INT 1 INT 4 ; sizeof int MUL SUB ; vec + lim - 1 LOCA -4 ; ptr = vec + lim - 1
LOCV +12 ; lim INT 2 SUB LOCA -8 ; ix = lim -2
ALIGN LABEL while_test LOCV -8 INT 0 GE ; ix >= 0 JZ while_end
LOCV +8 ; vec LOCV -8 ; ix INT 4 ; sizeof int MUL ADD ; vec + ix LOAD ; *(vec+ix) --> vec[ix]
LOCV -4 ; ptr LOAD ; *ptr
GT JZ three_false_part
LOCV +8 ; vec LOCV -8 ; ix INT 4 ; sizeof int MUL ADD ; vec + ix
JMP three_end
LOCV -4 ; ptr
ALIGN LABEL three_end
DUP ; for assignment LOCA -4 ; ptr = ?: ...
TRASH 4
LOCV -8 ; ix DUP INT 1 SUB LOCA -8 ; ix = ix - 1 TRASH 4 ; (ix-- used as instruction)
JMP while_test
ALIGN LABEL while_end
LOCV -4 POP LEAVE RET </asm>
To compile the Postfix code directly, pf2asm can be used: