Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Oaklisp
Oaklisp
Procesadores de
Lenguaje I
Lenguaje Intermedio
Generacin de Cdigo
Fuente
Scanner
Parser
Generador
de
Cdigo
LI
Objeto
TDS
back-ends
front-ends
Pascal
Ada
C++
Java
Generador
de
Cdigo Intemedio
Optimizacin
independiente
de mquina
Intel X
Dec-Alpha
Motorola
N+M vs N*M
Fase de Anlisis
position := initial + rate * 60
lexical analyzer
id1 := id2 + id3 * 60
syntax analyzer
:=
+
id1
id2l
60
id3
semantic analyzer
:=
Symbol
Table
rate.
id2l
inttoreal
id3
position ....
initial .
E
r
r
o
r
s
id1
60
intermediate code generator
Fase de Sntesis
Symbol Table
position ....
initial .
rate.
E
r
r
o
r
s
temp1 := inttoreal(60)
temp2 := id3 * temp1
temp3 := id2 + temp2
id1 := temp3
3 address code
code optimizer
temp1 := id3 * 60.0
id1 := id2 + temp1
final code generator
MOVF id3, R2
MULF #60.0, R2
MOVF id2, R1
ADDF R1, R2
MOVF R1, id1
Tipos de representaciones
intermedias
Tipos de representaciones
intermedias
Propiedades:
Tipos
Representaciones intermedias:
RPN
Notacin Polaca Inversa (RPN)
SABC*+=
Ventajas
Facilidad para generar cdigo a partir de ella
Es la notacin ms sencilla para el lenguaje intermedio
Inconvenientes
El cdigo es difcil de entender
Poca flexibilidad
No es til para optimizacin de cdigo
Representaciones intermedias: P
Cdigo P
Representaciones intermedias: P
Ejemplos Cdigo P
Ejemplo 1:
x:=y+1
Ejemplo 2:
if cond then x=3
Codigo P
lda x // carga direccion x
lod y // carga valor y
ldc 1 // carga constante 1
adi
// suma 2 pos pila
sto
// almacena tope en inf.
Codigo P
lod cond // carga cond
fjp L1
// si falso salta a L1
lda x
// carga direccion x
ldc 3
// carga constante 3
sto
// almacena tope en inf
lab L1 // etiqueta L1
y
&x
cond
&x
E
T
F
rbol
ASA
T
F
B
S:=A+B*C
:=
B
A[I]:=B
:=
IF
+
<
*
:=
B
[]
B
B
I
Codigo 3 direcciones
t1=-c
t2=b*t1
t3=4
t4=t3+t2
a=t4
Ejemplo 2:
if cond then then_statements
else
else_statements;
else_statements;
end if;
if;
Codigo 3 direcciones
t1 = cond
iff t1 else_label
codigo para then_statements
then_statements
goto endif_label
else_label:
codigo para else_statements
else_statements
endif_label:
endif_label:
Instrucciones en Cdigo 3
direcciones
Asignaciones indexadas
x:=y[i]
x[i]=y
Cdigos de 3 direcciones
Cada lnea de cdigo tiene un operador y hasta tres direcciones
Cuartetos
Otra representacin
S:=A+B*C
*
+
:=
B C
A T1
T2 S
<
A
IFF
E1
:=
B
LABEL
T1
T2
(*, B, C, T1)
(+, A, T1, T2)
(:=,T2, , S )
E1
E2
X
E2
(<,
A, B, E1)
(IFF,
E1, , E2 )
(:=,
B, , X )
(LABEL, , , E2)
Implementacin de cdigo 3
direcciones
Implementacin con cuartetos
Ejemplo: a = b+(c*d)
[cuartetos]
1. (mul,c,d,t1)
2. (add,b,t1,t2)
3. (:=,a,t2,_)
[tercetos]
1: (mul,c,d)
2: (add,b,(1))
3: (:=,a,(2))
Tercetos
Los cuartetos son la herramienta ms general
Inconvenientes
Tercetos
S:=A+B*C
1
2
3
1
2
3
4
(*, B, C)
(+, A, (1))
(:=, 2, S)
(<, A, B)
(IFF,(1),(4))
(:=, B, X)
(,,)
Tercetos Ind.
1 (*, B, C)
2 (+, A, (1))
3 (:=, (2), S)
4 (:=, (2), X)
VE=(1, 2, 3, 1, 2, 4)
1 (<, X, B)
2 (gotoc, (1), (4))
3 (goto, , (7))
4 (+, X, 1)
5 (:=, (4), X)
6 (goto, , (1))
7 (, , )
VE=(1, (2, 3, 4, 5, 6)+, 7)
la tabla
Declaraciones (1 procedimiento)
Producciones
P
D
DD; D
Did: T
Tinteger
Treal
Tarray[num]
of T
TT
Reglas semnticas
{offset=0}
10
Asignaciones y Aritmtica
Producciones Regla Semntica
S id := E
E E+E
{p=lookup(id.lex);
if (p!=null) S.cdigo := E.cdigo || gen(p := E.lugar)}
{E0.lugar := tempnueva();
E0.cdigo := E1.cdigo || E2.cdigo ||
gen(E0.lugar := E1.lugar + E2.lugar)}
E E*E
{E0.lugar := tempnueva();
E0.cdigo := E1.cdigo || E2.cdigo ||
gen(E.lugar := E1.lugar * E2.lugar)}
E -E
{E0.lugar := tempnueva();
E0.cdigo := E1.cdigo || gen(E0.lugar:=menosu E1.lugar)}
E (E)
{E0.lugar := E1.lugar;
E0.cdigo := E1.cdigo}
{p=lookup(id.lex), if (p!=null) E.lugar := p;}
E id
11
Conversiones de Tipos
Producciones Regla Semntica
S id := E
{p=lookup(id.lex);
S.cdigo := E.cdigo || gen(p := E.lugar)}
{E0.lugar := tempnuevo();
if (E1.tipo=integer and E2.tipo=integer) then
c=gen(E0.lugar := E1.lugar + E2.lugar)
E0.tipo=int
if (E1.tipo=real and E2.tipo=real) then
c=gen(E0.lugar := E1.lugar + E2.lugar)
E0.tipo=real
if (E1.tipo=integerl and E2.tipo=real) then
c=gen(E0.lugar :=inttoreal E1.lugar + E2.lugar)
E0.tipo=real
if (E1.tipo=real and E2.tipo=integer) then
c=gen(E0.lugar :=l E1.lugar + inttoreal E2.lugar)
E0.tipo=real}
E0.cdigo := E1.cdigo || E2.cdigo ||c
E E+E
t1:=multi(i,j)
t3:=inttoreal t1
t2:=addr(y, t3)
Ej.: x:=y+i*j
x:=t2
Evaluacin de Declaraciones y
Expresiones
TS
id : T
id : T
(c) (real)
(a) (int)
entero
real
100
104
Ss
D
S
id = E
(a)
Cdigo
Intermedio:
Ss
S
(0)
Ss
id =E
(c)
E + E
(1)
t1:=0
a:=t1
t2:=inttoreal a
t3:= t2 *4.0
t4:= t3+1
c:=t4
E * E
(a)
(4)
12
Evaluacin de Declaraciones y
Expresiones
Ss
t3:= t2 *4
t4:= t3+1
Ss
c:=t4
t1:=0
id = E
(a)
a:=t1
(0)
id
(c)
Ss
E
+
E
E * E
(a)
(4)
t2:=inttoreal a
(1)
t2:=inttoreal a
t3:= t2 *4
t4:= t3+1
c:=t4
t2:=inttoreal a
t3:= t2 *4
t4:= t3+1
t3:= t2 *4
Evaluacin de Declaraciones y
Expresiones
Sin atributo cdigo: escribir cdigo globalmente
a: int; c: real;
a=0;c=a*4.0+1;
Ss
t1:=0
a:=t1
Ss
t2:=inttoreal a
t1:=0
id = E
(a)
t3:= t2 *4
a:=t1
(0)
id
(c)
E
+
t2:=inttoreal a
E * E
(a)
Ss
c:=t4
t4:= t3+1
c:=t4
t4:= t3+1
Salida global
(1)
t3:= t2 *4
(4)
13
Regla Semntica
S id := E {p=lookup(id.lex);
if (p!=null) emitir(p := E.lugar)}
E E+E
{E0.lugar := tempnuevo();
emitir(E0.lugar := E1.lugar + E2.lugar)}
E E*E
{E0.lugar := tempnuevo;
emitir(E0.lugar := E1.lugar * E2.lugar)}
E -E
{E0.lugar := tempnuevo();
emitir(E0.lugar:=menosu E1.lugar)}
E (E)
{E0.lugar := E1.lugar;}
E id
Literales numricos
Dos posibilidades:
tmp1=3+b
tmp2=tmp1+b
tmp2=tmp1+7
tmp3=7
tmp3=tmp2+c
tmp4=tmp2+tmp3
a=tmp3
tmp5=tmp4+c
a=tmp5
14
a[i+1]:: dir_base(a)+(i+1)*sizeof(a)
Ejemplo:
a[i+1]=a[j+2]+3;
E0id [E1]
{p=lookup(id.lex)
E0.lugar=tempnuevo()
t=tempnuevo()
gen(t = E1.lugar * size(p))
gen(E0.lugar =p[ t ] )
}
Sid [E1]=E2 {p=lookup(id.lex)
t=tempnuevo()
gen(t = E1.lugar * size(p))
gen( p[ t ]= E2.lugar)
}
t1=j+2
t2=t1*size(a)
t3=a[t2];
t4=t3+3
t5=i+1
t6=t5*size(a)
a[t6]=t4
t1=&p;
t2=t1+i;
a=*t2
t3:=t2*size(a)
id
(a)
E
id
(i)
E
+
]
E
t1:=i+1
num
(1)
t4:=a[t3]
id
(a)
E
id
(j)
E
+
]
t2:=j+2
E
num
(2)
Ejemplo:
a[i+1]=a[j+2]+3;
t1:=i+1
t2:=j+2
t3:=t2*size(a)
t4:=a[t3]
t5:= t1*size(a)
a[t5]:=t4
Salida global
15
base de cplx
typedef struct cplx{
cplx.real
desplaz.
campo imag
double real;
cplx.imag
double imag;
};
cdigo 3 dir independiente: funcin offset (tipo,campo) en la TS
Eid . id
E.lugar=tempnuevo();
t1=tempnuevo();
t2=tempnuevo();
gen(t1 =& id1.lex)
gen(t2 = t1 +offset( id1.lex ,id2.lex ));
gen(E.lugar =* t2);
i=*x;
*x=i;
c1 ->real=c2->imag;
cplx.real
cplx.imag
base de cplx
desplaz. campo imag
Eid -> id {
E.lugar=tempnuevo();
t=tempnuevo();
gen(t = id1 +offset( id1.lex ,id2.lex ));
gen(E.lugar =* t);
}
16
c1 ->real=c2->imag;
base de cplx
cplx.real
cplx.imag
Ejemplo:
num.real=num.imag;
Ejemplo:
c1->real= c2-> num.imag;
t2=&num
t3=t1+offset(num,imag)
t1=*t1
t4=&num
t5=t4+offset(num,real)
*t5=t1
t2=c1+offset(c1,imag)
t3=*t2
t4=c2+offset(c2,real)
*t4=t3
a[i]
a[1]
a[0]
base
i2
posicion: base+(i1*n2+i2)*w
i1
A[0][0]
A[0][i2]
A[i1][0]
A[i1][i2]
Ecuacin recurrente:
n2
e1=i1
em=em-1*nm+im
17
Lista0::=Lista1 , E
ListaLista][E
Listaid [E
Regla Semntica
{if (L.offset == null) gen(L.lugar= E.lugar
else gen(L.lugar [ L.offset ]=E.lugar}
{if (L.offset == null) gen(E.lugar= L.lugar
else E.lugar=tempnuevo(); gen(E.lugar =L.lugar [ L.offset ])}
{E.lugar=tempnuevo(); gen(E.lugar = num.val)}
{p=lookup(id.lex), L.lugar := p, L.offset=null}
{L.lugar= tempnuevo();
L.offset= tempnuevo();
gen(L.lugar := base(Lista.array) )
gen(L.offset = Lista.lugar * ancho (Lista.array))}
{t= tempnuevo(), m=Lista1.ndim+1
gen(t = Lista1.lugar * limite(Lista1.array,m))
gen(t = t + E.lugar)
Lista0.array=Lista1.array
Lista0.lugar=t, Lista0.ndim=m}
{p=lookup(id.lex), Lista.array= p
Lista.lugar=E.lugar
Lista.ndim=1}
18
base
limite(i)
ancho
celda
:= E1
id
L2
(x)
Cdigo
Intermedio:
Lista1 ]
t1:=3
t2:=1
E3
Lista2 ] [
id
(M)
E2
num (1)
num (3)
t3:=t1*5
t3:=t3+ t2
t4:=base(M)
t5:=t3*4
t6:=t4[t5]
x:=t6
19
t1=a<b
iff t1 L1
x:=1
L1
E, a<b, tiene un
atributo: t1
L1
L2
if a < b goto L1
goto L2
x:=1
while (E) S
Codigo 3 direcciones
Cdigo de evaluaci
evaluacin de E
(hacia qu
qu etiqueta salto)
salto)
E.true:
E.true:
Cdigo para S1
goto Seguir
Seguir
E.false:
E.false:
Cdigo para S2
Seguir:
siguientes instrucciones
Codigo 3 direcciones
Comienzo:
Comienzo:
Cdigo de evaluaci
evaluacin de E
(hacia qu
qu etiqueta salto)
salto)
E.true:
E.true:
Cdigo para S
goto Comienzo
Comienzo
E.false:
E.false:
siguientes instrucciones
20
AND: E0 -> E1 || E2
true
E1 false
true
false
label E1.false:
true
false
E2
E0
true
E1 false
true
false
label E1.true:
true
E2 false
E0
21
E0:=E1and E2
E0:=(E1)
Regla Semntica
{E.codigo = gen (goto E.true)}
{E.codigo = gen (goto E.false)}
{E.code=gen(if id1.lugar relop id2.lugar goto E.true)||
gen(goto E.false)}
{E1.false= newlabel()
E1.true= E0.true
E2.true= E0.true
E2.false= E0.false
E0.codigo= E1.codigo || gen(E1.false :) || E2.codigo}
{E1.true= newlabel()
E1.false= E0.false
E2.true= E0.true
E2.false= E0.false
E0.codigo= E1.codigo || gen(E1.true :) || E2.codigo}
{E1.false= E0.false
E1.true= E0.true
E1.code= E0.code}
Ejemplo:
if (a<(b+32) || (b>0 && a>0))
x=1
t1=b+32
if a < t1 goto Lt0
if b > 0 goto L1
goto Lf0
Label L1:
if a > 0 goto Lt0
goto Lf0
Label Lt0
x=1
Labl Lf0
22
true
B false
label B.true:
S1
goto S.next:
label B.false:
S2
label S.next:
E0
label inicio:
true
B false
label B.true:
S
goto inicio
label S.next:
E0
23
Produccin
S:=if E then S1
S0:= while E do S1
Regla Semntica
{E.true= newlabel()
E.false= S0.next
S1.next= S0.next
S0.codigo=E.codigo||gen(E.true :)||S1.codigo}
{E.true= newlabel()
E.false= newlabel()
S1.next= S0.next
S2.next= S0.next
S0.codigo=E.codigo||gen(E.true :)||S1.codigo||
gen(goto S0.next||gen(E.false :)||S2.codigo}
{begin = newlabel()
E.true= newlabel()
E.false= S0.next
S1.next= begin
S0.codigo=gen(begin :)||E.codigo|| gen(E.true :)||
S1.codigo||gen(goto begin)}
Regla Semntica
{E.true= newlabel(); E.false= S0.next}
{emitir(label E.true :); S1.next= S0.next;}
{E.true= newlabel(); E.false= newlabel()}
{emitir(label E.true :); S1.next= S0.next}
{emitir(label E.false :); S2.next= S0.next}
{begin = newlabel(); E.true= newlabel();
E.false= S0.next; emitir(label begin :);}
{S1.next= begin; emitir(label E.true :); }
24
S1
while E1
id
(s)
do
S2
> id if E2 S3 else S4
(K)
id
(a)
> id
(b)
Cdigo 3
direcciones
L1: if s<K goto L2
goto L0
L2: if a>b goto L3
goto L4
L3: t1=s+a
s=t1
goto L0
L4: t2=s-a
s=t2
goto L1
L0:
25
Cdigo
target:
goto target
...
goto target
...
mov foobar,r1
Tabla
goto target
.
goto target
target
backpatch list
(lista enlazada)
Definicin
Instruccin de entrada
<cdigo del cuerpo de la funcin>
Instruccin de salida
Llamada
Instruccin de comienzo de clculo de parmetros
<cdigo del clculo de parmetros>
Instruccin de llamada
26
entry f
return x
Cdigo P
mst
cup
ent f
ret
Ej.:
int f(int x, int y)
{return x+y+1;}
f(2+3,4);
entry f
t1=x+y
t2=t1+1
return t2
t1=2+3
param t1
param 4
call f 2
Cdigo 3-dir
ent
lod
lod
adi
ldc
adi
ret
mst
ldc
ldc
adi
ldc
cup
f
x
y
1
2
3
4
f
Cdigo P
{p=lookup(id.lex);
if (p!=null) emitir(p := E.lugar)}
{E0.lugar := tempnuevo();
emitir(E0.lugar := E1.lugar + E2.lugar)}
E id
E id(lista)
27
(c)
c:=t4
E
E
(1)
id
(f)
t1:=0
t4=1+t3
param t2
param 1
t2:=4*a
t3=call f 2
param t2
( lista )
param 1
t2 1
E , lista
(1)
E
E
(4)
a:=t1
t3=call f 2
t2
t2:=4*a
t4:= 1+t3
c:=t4
(a)
28