Optimization Topics/Exercise 03: Difference between revisions
From Wiki**3
Created page with "Considere a seguinte função em C: # Que optimizações independentes da máquina são possíveis? # Traduza-a para Postfix optimizado. <c> int fun(int a, int b) { int i = 10..." |
No edit summary |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
{{TOCright}} | |||
== Problema == | |||
Considere a seguinte função em C: | Considere a seguinte função em C: | ||
# Que optimizações independentes da máquina são possíveis? | # Que optimizações independentes da máquina são possíveis? | ||
# Traduza-a para Postfix optimizado. | # Traduza-a para Postfix optimizado. | ||
<c> | <source lang="c"> | ||
int fun(int a, int b) { | int fun(int a, int b) { | ||
int i = 10, c; | int i = 10, c; | ||
| Line 12: | Line 15: | ||
return c; | return c; | ||
} | } | ||
</c> | </source> | ||
== Código Postfix (não optimizado) == | |||
O código Postfix correspondente ao código C é o seguinte (agradece-se a comunicação de questões relativas a este código). | |||
{{CollapsedCode|Código Postfix| | |||
<source lang="asm"> | |||
;;---- BB1 START ---- | |||
TEXT | |||
ALIGN | |||
GLOBAL fun, FUNC | |||
LABEL fun | |||
ENTER 8 ; i@-4 c@-8 a@+8 b@+12 | |||
INT 10 | |||
LOCA -4 ; i=10 | |||
INT 20 | |||
INT 1024 | |||
MUL | |||
LOCAL -4 | |||
LDINT | |||
ADD | |||
DUP32 | |||
LOCAL -8 | |||
STINT ; c = ... | |||
TRASH 4 | |||
;;---- BB1 END ---- | |||
;;---- BB2 START ---- | |||
ALIGN | |||
LABEL while_test | |||
LOCAL -4 | |||
LDINT | |||
INT 1 | |||
ADD | |||
DUP32 | |||
LOCAL -4 | |||
STINT ; ++i | |||
LOCAL +8 | |||
LDINT | |||
LOCAL +12 | |||
LDINT | |||
ADD ; a + b | |||
LT | |||
JZ while_end | |||
;;---- BB2 END ---- | |||
;;---- BB3 START ---- | |||
LOCAL +8 | |||
LDINT | |||
LOCAL +12 | |||
LDINT | |||
ADD ; a + b | |||
INT 3 | |||
LOCAL -4 | |||
LDINT | |||
MUL | |||
INT 5 | |||
ADD | |||
LT | |||
JZ ifend | |||
;;---- BB3 END ---- | |||
;;---- BB4 START ---- | |||
LOCAL -8 | |||
LDINT ; c | |||
INT 2 | |||
LOCAL -4 | |||
LDINT | |||
MUL ; 2 * i | |||
ADD | |||
DUP32 | |||
LOCAL -8 ; &c | |||
STINT | |||
TRASH 4 | |||
;;---- BB4 END ---- | |||
;;---- BB5 START ---- | |||
ALIGN | |||
LABEL ifend | |||
JMP while_test | |||
;;---- BB5 END ---- | |||
;;---- BB6 START ---- | |||
ALIGN | |||
LABEL while_end | |||
LOCAL -8 | |||
LDINT ; c | |||
STFVAL32 | |||
LEAVE | |||
RET | |||
;;---- BB6 END ---- | |||
</source> | |||
}} | |||
== Compiling and Running == | |||
Para compilar o código Postfix directamente, pode ser utilizada a ferramenta [[Compiladores/Projecto de Compiladores/Compiladores Exemplo|pf2asm]] (assumindo uma arquitectura Intel de 32 bits -- ix86): | |||
pf2asm code.pf | |||
yasm -felf32 code.asm | |||
[[category:Compiladores]] | |||
[[category:Ensino]] | |||
Latest revision as of 10:10, 12 June 2023
Problema
Considere a seguinte função em C:
- Que optimizações independentes da máquina são possíveis?
- Traduza-a para Postfix optimizado.
int fun(int a, int b) {
int i = 10, c;
c = 20 * 1024 + i;
while (++i < a + b) {
if (a + b < 3 * i + 5) c += 2 * i;
}
return c;
}
Código Postfix (não optimizado)
O código Postfix correspondente ao código C é o seguinte (agradece-se a comunicação de questões relativas a este código).
| Código Postfix |
|---|
;;---- BB1 START ----
TEXT
ALIGN
GLOBAL fun, FUNC
LABEL fun
ENTER 8 ; i@-4 c@-8 a@+8 b@+12
INT 10
LOCA -4 ; i=10
INT 20
INT 1024
MUL
LOCAL -4
LDINT
ADD
DUP32
LOCAL -8
STINT ; c = ...
TRASH 4
;;---- BB1 END ----
;;---- BB2 START ----
ALIGN
LABEL while_test
LOCAL -4
LDINT
INT 1
ADD
DUP32
LOCAL -4
STINT ; ++i
LOCAL +8
LDINT
LOCAL +12
LDINT
ADD ; a + b
LT
JZ while_end
;;---- BB2 END ----
;;---- BB3 START ----
LOCAL +8
LDINT
LOCAL +12
LDINT
ADD ; a + b
INT 3
LOCAL -4
LDINT
MUL
INT 5
ADD
LT
JZ ifend
;;---- BB3 END ----
;;---- BB4 START ----
LOCAL -8
LDINT ; c
INT 2
LOCAL -4
LDINT
MUL ; 2 * i
ADD
DUP32
LOCAL -8 ; &c
STINT
TRASH 4
;;---- BB4 END ----
;;---- BB5 START ----
ALIGN
LABEL ifend
JMP while_test
;;---- BB5 END ----
;;---- BB6 START ----
ALIGN
LABEL while_end
LOCAL -8
LDINT ; c
STFVAL32
LEAVE
RET
;;---- BB6 END ----
|
Compiling and Running
Para compilar o código Postfix directamente, pode ser utilizada a ferramenta pf2asm (assumindo uma arquitectura Intel de 32 bits -- ix86):
pf2asm code.pf yasm -felf32 code.asm