(→Compiling and Running) |
(→Postfix Code) |
||
Line 27: | Line 27: | ||
== Postfix Code == | == Postfix Code == | ||
− | The Postfix code for the above code is as follows: | + | The Postfix code for the above code is as follows: (code has not been thoroughly checked: bug reports are welcome) |
+ | === Variables === | ||
+ | <asm> | ||
+ | ;; variable "a" | ||
+ | DATA | ||
+ | ALIGN | ||
+ | LABEL a | ||
+ | CONST 10 | ||
+ | ;; variable "v" | ||
+ | DATA ; "static" variables are always initialized | ||
+ | ALIGN | ||
+ | LABEL v | ||
+ | CONST 0 ; this is a null pointer | ||
+ | </asm> | ||
+ | |||
+ | === Declaration of external function === | ||
+ | <asm> | ||
+ | EXTERN malloc | ||
+ | </asm> | ||
+ | |||
+ | === Function "mkvec" === | ||
+ | <asm> | ||
+ | ;; start of BB1 | ||
+ | TEXT | ||
+ | ALIGN | ||
+ | LABEL mkvec | ||
+ | ENTER 8 ; s@-4 v@-8 | ||
+ | LOCV 8 | ||
+ | INT 1 | ||
+ | LT | ||
+ | JZ ifend1 | ||
+ | ;; end of BB1 | ||
+ | |||
+ | ;; start of BB2 | ||
+ | INT 0 | ||
+ | POP | ||
+ | LEAVE | ||
+ | RET | ||
+ | ;; end of BB2 | ||
+ | |||
+ | ;; start of BB3 | ||
+ | ALIGN | ||
+ | LABEL ifend1 | ||
+ | INT 8 ; sizeof(double) | ||
+ | LOCA -4 | ||
+ | LOCV +8 | ||
+ | LOCV -4 | ||
+ | MUL | ||
+ | CALL malloc | ||
+ | ;; end of BB3 | ||
+ | |||
+ | ;; start of BB4 | ||
+ | TRASH 4 | ||
+ | PUSH | ||
+ | LOCA -8 | ||
+ | LOCV -8 | ||
+ | POP | ||
+ | LEAVE | ||
+ | RET | ||
+ | ;; end of BB4 | ||
+ | </asm> | ||
+ | |||
+ | === Function "compute" === | ||
<asm> | <asm> | ||
Consider the following C code:
<c> 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;
} </c>
Assume that the size of pointers, int, and unsigned long is 32 bits and that the size of double is 64 bits.
The Postfix code for the above code is as follows: (code has not been thoroughly checked: bug reports are welcome)
<asm>
DATA ALIGN LABEL a CONST 10
DATA ; "static" variables are always initialized ALIGN LABEL v CONST 0 ; this is a null pointer </asm>
<asm> EXTERN malloc </asm>
<asm>
TEXT ALIGN LABEL mkvec ENTER 8 ; s@-4 v@-8 LOCV 8 INT 1 LT JZ ifend1
INT 0 POP LEAVE RET
ALIGN LABEL ifend1 INT 8 ; sizeof(double) LOCA -4 LOCV +8 LOCV -4 MUL CALL malloc
TRASH 4 PUSH LOCA -8 LOCV -8 POP LEAVE RET
</asm>
<asm>
</asm>
To compile the Postfix code directly, pf2asm can be used (assuming a 32-bit architecture):