Difference between revisions of "Code Generation/Exercise 9"

From Wiki**3

< Code Generation
(Compiling and Running)
(Compiling and Running)
 
(24 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
{{TOCright}}
 
== The Original Code ==
 
== The Original Code ==
  
Consider the following C code:
+
Consider the following C code and assume that the size of pointers, '''int''', and '''unsigned long''' is 32 bits and that the size of '''double''' is 64 bits.
  
<c>
+
<source lang="c">
 
static unsigned long a = 10;
 
static unsigned long a = 10;
 
static double *v;
 
static double *v;
Line 21: Line 22:
 
   return v;
 
   return v;
 
}
 
}
</c>
+
</source>
  
Assume that the size of pointers, '''int''', and '''unsigned long''' is 32 bits and that the size of '''double''' is 64 bits.
+
== Postfix Code ==
 +
 
 +
The Postfix code for the above code is as follows: (code has not been thoroughly checked: bug reports are welcome)
 +
 
 +
In the following code sections, '''BB#''' means "[[Optimization Topics|basic block]] number".
 +
 
 +
{{CollapsedCode|Static (global) variables|
 +
<source lang="asm">
 +
;; variable "a"
 +
DATA
 +
ALIGN
 +
LABEL a
 +
SINT 10
 +
 
 +
;; variable "v"
 +
DATA    ; "static" variables are always initialized
 +
ALIGN
 +
LABEL v
 +
SINT 0  ; this is a null pointer
 +
</source>
 +
}}
 +
 
 +
{{CollapsedCode|Declaration of external function|
 +
<source lang="asm">
 +
EXTERN malloc
 +
</source>
 +
}}
 +
 
 +
{{CollapsedCode|Function "mkvec"|
 +
<source lang="asm">
 +
;; start of BB1
 +
TEXT
 +
ALIGN
 +
LABEL mkvec
 +
ENTER 8  ; s@-4 v@-8
 +
LOCAL 8
 +
LDINT
 +
INT 1
 +
LT
 +
JZ ifend1
 +
;; end of BB1
  
== Postfix Code ==
+
;; start of BB2
 +
INT 0
 +
STFVAL32
 +
LEAVE
 +
RET
 +
;; end of BB2
 +
 
 +
;; start of BB3
 +
ALIGN
 +
LABEL ifend1
 +
INT 8  ; sizeof(double)
 +
LOCAL -4
 +
STINT
 +
LOCAL +8
 +
LDINT
 +
LOCAL -4
 +
LDINT
 +
MUL
 +
CALL malloc
 +
;; end of BB3
 +
 
 +
;; start of BB4
 +
TRASH 4
 +
LDFVAL32
 +
LOCAL -8
 +
STINT
 +
LOCAL -8
 +
LDINT
 +
STFVAL32
 +
LEAVE
 +
RET
 +
;; end of BB4
 +
</source>
 +
}}
 +
 
 +
{{CollapsedCode|Function "compute"|
 +
<source lang="asm">
 +
;; start of BB1
 +
TEXT
 +
ALIGN
 +
GLOBAL compute, FUNC
 +
LABEL compute
 +
ENTER 4  ; i@-4
 +
ADDR a
 +
LDINT
 +
INT 4
 +
MUL
 +
CALL mkvec
 +
;; end of BB1
 +
 
 +
;; start of BB2
 +
TRASH 4
 +
LDFVAL32
 +
DUP32
 +
ADDR v
 +
STINT
 +
TRASH 4
 +
INT 1
 +
LOCAL -4
 +
STINT
 +
;; end of BB2
 +
 
 +
;; start of BB3
 +
ALIGN
 +
LABEL fortest
 +
LOCAL -4
 +
LDINT
 +
ADDR a
 +
LDINT
 +
LT
 +
JZ forend
 +
;; end of BB3
 +
 
 +
;; start of BB4
 +
ADDR v
 +
LDINT
 +
LOCAL -4
 +
LDINT
 +
INT 8
 +
MUL
 +
ADD
 +
LDDOUBLE
 +
ADDR v
 +
LDINT
 +
INT 0
 +
INT 8
 +
MUL
 +
ADD
 +
LDDOUBLE
 +
DCMP
 +
INT 0
 +
GT
 +
JZ ifend2
 +
;; end of BB4
 +
 
 +
;; start of BB5
 +
INT 3
 +
LOCAL -4
 +
LDINT
 +
MUL
 +
INT 1
 +
SUB
 +
I2D
 +
DUP64
 +
ADDR v
 +
LDINT
 +
LOCAL -4
 +
LDINT
 +
INT 8
 +
MUL
 +
ADD
 +
STDOUBLE
 +
TRASH 8
 +
;; end of BB5
  
The Postfix code for the above code is as follows:
+
;; start of BB6
 +
ALIGN
 +
LABEL ifend2
 +
ALIGN
 +
LABEL forincr
 +
LOCAL -4
 +
LDINT
 +
DUP32
 +
INT 1
 +
ADD
 +
LOCAL -4
 +
STINT
 +
TRASH 4
 +
JMP fortest
 +
;; end of BB6
  
<asm>
+
;; start of BB7
 +
ALIGN
 +
LABEL forend
 +
ADDR v
 +
LDINT
 +
STFVAL32
 +
LEAVE
 +
RET
 +
;; end of BB7
  
</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 (assuming a 32-bit architecture):
  
* pf2asm compute.pf
+
pf2asm compute.pf
* yasm -felf compute.asm
+
yasm -felf compute.asm
  
[[category:Compilers]]
+
[[category:Compiladores]]
[[category:Teaching]]
+
[[category:Ensino]]

Latest revision as of 18:51, 6 May 2019

The Original Code

Consider the following C code and assume that the size of pointers, int, and unsigned long is 32 bits and that the size of double is 64 bits.

static unsigned long a = 10;
static double *v;
extern void *malloc(unsigned long);

static double *mkvec(unsigned long n) {
  if (n < 1) return (double *)0;
  unsigned long s = sizeof(double);
  double *v = (double *)malloc(n * s);
  return v;
}

double *compute() {
  v = mkvec(a * 4);
  for (unsigned long i = 1; i < a; i++)
    if (v[i] > v[0]) v[i] = 3 * i - 1;
  return v;
}

Postfix Code

The Postfix code for the above code is as follows: (code has not been thoroughly checked: bug reports are welcome)

In the following code sections, BB# means "basic block number".

Static (global) variables
;; variable "a"
DATA
ALIGN
LABEL a
SINT 10

;; variable "v"
DATA     ; "static" variables are always initialized
ALIGN
LABEL v
SINT 0  ; this is a null pointer
Declaration of external function
EXTERN malloc
Function "mkvec"
;; start of BB1
TEXT
ALIGN
LABEL mkvec
ENTER 8  ; s@-4 v@-8
LOCAL 8
LDINT
INT 1
LT
JZ ifend1
;; end of BB1

;; start of BB2
INT 0
STFVAL32
LEAVE
RET
;; end of BB2

;; start of BB3
ALIGN
LABEL ifend1
INT 8  ; sizeof(double)
LOCAL -4
STINT
LOCAL +8
LDINT
LOCAL -4
LDINT
MUL
CALL malloc
;; end of BB3

;; start of BB4
TRASH 4
LDFVAL32
LOCAL -8
STINT
LOCAL -8
LDINT
STFVAL32
LEAVE
RET
;; end of BB4
Function "compute"
;; start of BB1
TEXT
ALIGN
GLOBAL compute, FUNC
LABEL compute
ENTER 4  ; i@-4
ADDR a
LDINT
INT 4
MUL
CALL mkvec
;; end of BB1

;; start of BB2
TRASH 4
LDFVAL32
DUP32
ADDR v
STINT
TRASH 4
INT 1
LOCAL -4
STINT
;; end of BB2

;; start of BB3
ALIGN
LABEL fortest
LOCAL -4
LDINT
ADDR a
LDINT
LT
JZ forend
;; end of BB3

;; start of BB4
ADDR v
LDINT
LOCAL -4
LDINT
INT 8
MUL
ADD
LDDOUBLE
ADDR v
LDINT
INT 0
INT 8
MUL
ADD
LDDOUBLE
DCMP
INT 0
GT
JZ ifend2
;; end of BB4

;; start of BB5
INT 3
LOCAL -4
LDINT
MUL
INT 1
SUB
I2D
DUP64
ADDR v
LDINT
LOCAL -4
LDINT
INT 8
MUL
ADD
STDOUBLE
TRASH 8
;; end of BB5

;; start of BB6
ALIGN
LABEL ifend2
ALIGN
LABEL forincr
LOCAL -4
LDINT
DUP32
INT 1
ADD
LOCAL -4
STINT
TRASH 4
JMP fortest
;; end of BB6

;; start of BB7
ALIGN
LABEL forend
ADDR v
LDINT
STFVAL32
LEAVE
RET
;; end of BB7

Compiling and Running

To compile the Postfix code directly, pf2asm can be used (assuming a 32-bit architecture):

pf2asm compute.pf
yasm -felf compute.asm