Code Generation/Example 1: Difference between revisions

From Wiki**3

< Code Generation
Line 38: Line 38:
LABEL "while"
LABEL "while"
LOCV -4          ; n
LOCV -4          ; n
DUP             ; n (because of n--)
DUP32             ; n (because of n--)
INT 1
INT 1
SUB              ; n-1
SUB              ; n-1
Line 48: Line 48:
LOCV -12        ; j
LOCV -12        ; j
ADD              ; i+j
ADD              ; i+j
DUP             ; same value (for assignment)
DUP32             ; same value (for assignment)
LOCV -16        ; seg
LOCV -16        ; seg
STORE           ; *seg = i+j
STINT           ; *seg = i+j
CALL "print"    ; we assume that "print" does not return any value
CALL "print"    ; we assume that "print" does not return any value
TRASH 4          ; trash argument (int)
TRASH 4          ; trash argument (int)
Line 68: Line 68:
ALIGN
ALIGN
LABEL "end3"
LABEL "end3"
DUP             ; trenary operator's value (duplicated because of assignment)
DUP32             ; trenary operator's value (duplicated because of assignment)
LOCA -16        ; seg = (seg == &i) ? &j : &i
LOCA -16        ; seg = (seg == &i) ? &j : &i
TRASH 4          ; trash assignment's value, since we used it as an instruction
TRASH 4          ; trash assignment's value, since we used it as an instruction
Line 79: Line 79:
;-- rest of function
;-- rest of function
INT 0
INT 0
POP             ; return value is 0
STFVAL32             ; return value is 0
LEAVE
LEAVE
RET
RET

Revision as of 22:22, 1 May 2018

The Original Code

Consider the following C function: <c> int main() {

 int n = 45, i = 0, j = 1, *seg = &i;
 while (n-- > 0) {
   print(*seg = i + j);
   seg = (seg == &i) ? &j : &i;
 }
 return 0;

} </c>

Postfix Code

The Postfix code for the above function is as follows:

Postfix code
{{{2}}}