Difference between revisions of "Code Generation/Example 3"

From Wiki**3

< Code Generation
(Postfix Code)
(Compiling and Running)
 
(5 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">
 
int *click(int *x, int dim) {
 
int *click(int *x, int dim) {
 
   int *res, i;
 
   int *res, i;
Line 10: Line 10:
 
   return res;
 
   return res;
 
}
 
}
</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">
 
TEXT
 
TEXT
 
ALIGN
 
ALIGN
Line 26: Line 26:
  
 
;; i = dim-2
 
;; i = dim-2
LOCV 12
+
LOCAL 12
 +
LDINT
 
INT 2
 
INT 2
 
SUB
 
SUB
DUP
+
DUP32
LOCA -8
+
LOCAL -8
 +
STINT
 
TRASH 4
 
TRASH 4
  
 
;; res = x + dim - 1
 
;; res = x + dim - 1
LOCV 8
+
LOCAL 8
LOCV 12
+
LDINT
 +
LOCAL 12
 +
LDINT
 
INT 4
 
INT 4
 
MUL
 
MUL
Line 43: Line 47:
 
MUL
 
MUL
 
SUB
 
SUB
DUP
+
DUP32
LOCA -4
+
LOCAL -4
 +
STINT
 
TRASH 4
 
TRASH 4
  
Line 50: Line 55:
 
ALIGN
 
ALIGN
 
LABEL for
 
LABEL for
LOCV -8
+
LOCAL -8
 +
LDINT
 
INT 0
 
INT 0
 
GE
 
GE
Line 58: Line 64:
  
 
;; if test
 
;; if test
LOCV 8 ; x
+
LOCAL 8
LOCV -8 ; i
+
LDINT ; x
 +
LOCAL -8
 +
LDINT; i
 
INT 4
 
INT 4
 
MUL
 
MUL
 
ADD ; x+i
 
ADD ; x+i
LOAD ; x[i] = *(x+i)
+
LDINT ; x[i] = *(x+i)
  
LOCV -4 ; res
+
LOCAL -4
LOAD ; *res
+
LDINT ; res
 +
LDINT ; *res
  
 
GT
 
GT
Line 72: Line 81:
  
 
;; if block
 
;; if block
LOCV 8 ; x
+
LOCAL 8
LOVC -8 ; i
+
LDINT ; x
 +
LOCAL -8
 +
LDINT ; i
 
INT 4
 
INT 4
 
MUL
 
MUL
 
ADD ; x+i = &x[i]
 
ADD ; x+i = &x[i]
DUP
+
DUP32
LOCA -4 ; res = &x[i]
+
LOCAL -4
 +
STINT ; res = &x[i]
 
TRASH 4
 
TRASH 4
  
Line 87: Line 99:
 
ALIGN
 
ALIGN
 
LABEL forincr
 
LABEL forincr
LOCV -8 ; i
+
LOCAL -8
DUP
+
LDINT ; i
 +
DUP32
 
INT 1
 
INT 1
 
SUB
 
SUB
LOCA -8
+
LOCAL -8
 +
STINT
 
TRASH 4
 
TRASH 4
  
Line 101: Line 115:
  
 
;; return
 
;; return
LOCV -4 ; res
+
LOCAL -4
 +
LDINT ; res
  
POP
+
STFVAL32
 
LEAVE
 
LEAVE
 
RET
 
RET
</asm>
+
</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 click.pf
+
pf2asm click.pf
* yasm -felf click.asm
+
yasm -felf click.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:

int *click(int *x, int dim) {
  int *res, i;
  for (i = dim-2, res = x+dim-1; i >= 0; i--)
    if (x[i] > *res) res = &x[i];
  return res;
}

Postfix Code

The Postfix code for the above function is as follows:

Postfix code
TEXT
ALIGN
GLOBL click, FUNC
LABEL click
ENTER 8  ;-- x@+8 dim@+12 res@-4 i@-8

;; for init

;; i = dim-2
LOCAL 12
LDINT
INT 2
SUB
DUP32
LOCAL -8
STINT
TRASH 4

;; res = x + dim - 1
LOCAL 8
LDINT
LOCAL 12
LDINT
INT 4
MUL
ADD
INT 1
INT 4
MUL
SUB
DUP32
LOCAL -4
STINT
TRASH 4

;; for test
ALIGN
LABEL for
LOCAL -8
LDINT
INT 0
GE
JZ forend

;; for block

;; if test
LOCAL 8
LDINT ; x
LOCAL -8
LDINT; i
INT 4
MUL
ADD ; x+i
LDINT ; x[i] = *(x+i)

LOCAL -4
LDINT ; res
LDINT ; *res

GT
JZ ifend

;; if block
LOCAL 8
LDINT ; x
LOCAL -8
LDINT ; i
INT 4
MUL
ADD ; x+i = &x[i]
DUP32
LOCAL -4
STINT ; res = &x[i]
TRASH 4

ALIGN
LABEL ifend

;; for increment
ALIGN
LABEL forincr
LOCAL -8
LDINT ; i
DUP32
INT 1
SUB
LOCAL -8
STINT
TRASH 4

;; for jump to cycle
JMP for

ALIGN
LABEL forend

;; return
LOCAL -4
LDINT ; res

STFVAL32
LEAVE
RET

Compiling and Running

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

pf2asm click.pf
yasm -felf click.asm