Difference between revisions of "Code Generation/Example 4"

From Wiki**3

< Code Generation
(Postfix Code)
(Compiling and Running)
 
(7 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
Consider the following C function:
 
Consider the following C function:
  
<c>
+
<source lang="c">
 
extern int printf(const char *format, ...);
 
extern int printf(const char *format, ...);
 
int printlist(int lo, int hi) {
 
int printlist(int lo, int hi) {
Line 13: Line 13:
 
   return ix;
 
   return ix;
 
}
 
}
</c>
+
</source>
  
 
== Postfix Code ==
 
== Postfix Code ==
  
 
The Postfix code for the above function is as follows:  
 
The Postfix code for the above function is as follows:  
 
+
{{CollapsedCode|Postfix code|
<asm>
+
<source lang="asm">
 
EXTRN printf
 
EXTRN printf
  
Line 30: Line 30:
  
 
; int ix = lo is NOT an assignment
 
; int ix = lo is NOT an assignment
LOCV +8 ; read lo
+
LOCAL +8
LOCA -4 ; write to ix
+
LDINT ; read lo
 +
LOCAL -4
 +
STINT ; write to ix
  
 
ALIGN
 
ALIGN
 
LABEL whiletest
 
LABEL whiletest
LOCV -4  ; read ix
+
LOCAL -4
LOCV +12 ; read hi
+
LDINT ; read ix
 +
LOCAL +12
 +
LDINT ; read hi
 
LT
 
LT
 
JZ whileend
 
JZ whileend
 +
 +
; start while body
 +
 +
; prepare arguments for calling printf
 +
 +
LOCAL -4
 +
LDINT    ; second printf arg: read ix
  
 
; put string literal in read-only memory
 
; put string literal in read-only memory
Line 44: Line 55:
 
ALIGN
 
ALIGN
 
LABEL strlit
 
LABEL strlit
STR "%d\n"
+
SSTRING "%d\n"
 +
; now get the address of the string literal (strlit)
 +
TEXT
 +
ADDR strlit ; first printf arg: string literal address
  
TEXT
+
; args are in the stack: call the function
; start while body
 
LOCV -4    ; second printf arg: read ix
 
ADDR strlit ; first printf arg: string lit address
 
 
CALL printf
 
CALL printf
  
Line 56: Line 67:
  
 
; get printf return value
 
; get printf return value
PUSH
+
LDFVAL32
  
 
; get rid of return value (printf used as instruction)
 
; get rid of return value (printf used as instruction)
Line 62: Line 73:
  
 
; increment ix
 
; increment ix
LOCV -4 ; read ix
+
LOCAL -4
DUP
+
LDINT ; read ix
 +
DUP32
 
INT 1
 
INT 1
 
ADD
 
ADD
LOCA -4 ; ix = ix + 1
+
LOCAL -4
 +
STINT ; ix = ix + 1
 +
 
 +
; trash old value of ix
 +
TRASH 4
  
 
; end while body
 
; end while body
Line 78: Line 94:
  
 
; prepare return value
 
; prepare return value
LOCV -4 ; read ix
+
LOCAL -4
 +
LDINT ; read ix
  
 
; put it in the accumulator (register) to conform with Cdecl
 
; put it in the accumulator (register) to conform with Cdecl
POP
+
STFVAL32
 
 
; release stack frame
 
LEAVE
 
 
 
; return control to caller
 
RET
 
  
</asm>
+
LEAVE  ; release stack frame
 +
RET    ; return control to caller
 +
</source>
 +
}}
  
 
== Compiling and Running ==
 
== Compiling and Running ==
  
To compile the Postfix code directly, [[pf2asm]] can be used:
+
To compile the Postfix code directly, [[Compiladores/Projecto de Compiladores/Compiladores Exemplo|pf2asm]] can be used:
  
* pf2asm while.pf
+
pf2asm while.pf
* yasm -felf while.asm
+
yasm -felf while.asm
  
[[category:Compilers]]
+
[[category:Compiladores]]
[[category:Teaching]]
+
[[category:Ensino]]

Latest revision as of 18:52, 6 May 2019

The Original Code

Consider the following C function:

extern int printf(const char *format, ...);
int printlist(int lo, int hi) {
  int ix = lo;
  while (ix < hi) {
    printf("%d\n", ix);
    ix++;
  }
  return ix;
}

Postfix Code

The Postfix code for the above function is as follows:

Postfix code
EXTRN printf

TEXT
ALIGN
GLOBL printlist, FUNC
LABEL printlist

ENTER 4  ; ix@-4 lo@+8 hi@+12

; int ix = lo is NOT an assignment
LOCAL +8
LDINT ; read lo
LOCAL -4
STINT ; write to ix

ALIGN
LABEL whiletest
LOCAL -4
LDINT  ; read ix
LOCAL +12
LDINT ; read hi
LT
JZ whileend

; start while body

; prepare arguments for calling printf

LOCAL -4
LDINT     ; second printf arg: read ix

; put string literal in read-only memory
RODATA
ALIGN
LABEL strlit
SSTRING "%d\n"
; now get the address of the string literal (strlit)
TEXT
ADDR strlit ; first printf arg: string literal address

; args are in the stack: call the function
CALL printf

; clean args
TRASH 8

; get printf return value
LDFVAL32

; get rid of return value (printf used as instruction)
TRASH 4

; increment ix
LOCAL -4
LDINT ; read ix
DUP32
INT 1
ADD
LOCAL -4
STINT ; ix = ix + 1

; trash old value of ix
TRASH 4

; end while body

; restart while cycle
JMP whiletest

; this is the while exit point
ALIGN
LABEL whileend

; prepare return value
LOCAL -4
LDINT ; read ix

; put it in the accumulator (register) to conform with Cdecl
STFVAL32

LEAVE  ; release stack frame
RET     ; return control to caller

Compiling and Running

To compile the Postfix code directly, pf2asm can be used:

pf2asm while.pf
yasm -felf while.asm