Compiler code generation for expressions with multiple operands

I am in the middle of compiler coding for a C-like language.

I got past syntax and semantic validation, and I'm starting the code generation phase.

The final code that I have to generate should be in a 3 address loading / storage architecture. I can accept an unlimited set of registers and 32 M memory for the stack and system memory.

Right now, when I start generating code, I start with the assumption that a really large array of int int R[2000000]denotes a set of registers. And when and when I come across a variable declaration (through an analyzer / semantic analyzer), I allocate case for this particular variable.

Now that the program encounters this variable again, I am returning it from this register. I save the register number of each variable in a character table.

Now my question is this: suppose we have such an expression -

a := b + c + e / f *h;

And I saved a, b, c, e, f, h in R1, R2, R3, R4, R5, R6 respectively, the final generated code will be (assuming the following available regs start with R7 ...)

R9 = R5 * R6;
R8 = R4 / R9;
R7 = R3 + R8;
R1 = R2 + R7;

What is the approach to "remembering" what was in the previous register and operation?

If this is not the right way to do this, can someone please give me some guidance on how to implement it?

Any suggestion would be great. Thanks

+4
source share
4 answers

, "". , , , .

" ". , ( ) , . , ( ), . ( , "".)

, "" , . .

, .

, , -, .

+3

, . , , , . , .

+1

. node , node , .

0

.

, , :

a b c + e f / h * + :=

, , , , , . , . :

a b c +

, . x86 . , b c:

MOV R1, [&b]
ADD R1, [&c]

, , R1. :

a R1

.

a R1 e f /

x86 , 64- EDX: EAX, EAX. , 32- , e EAX EDX . :

MOV EAX, [&e]
CDQ
IDIV [&f]

, EDX EAX , . , , , .

EAX. , :

a R1 EAX

:

a R1 EAX h *

- , , :

IMUL EAX, [&h]

EAX, :

a R1 EAX

:

a R1 EAX +

, :

ADD R1, EAX

R1. :

a R1

:

MOV [&a], R1

, :

MOV R1, [&b]
ADD R1, [&c]
MOV EAX, [&e]
CDQ
IDIV [&f]
IMUL EAX, [&h]
ADD R1, EAX
MOV [&a], R1

R1. ? , . , (, MOV) .

MOV R1, [&b]  ; R1 lifetime starts
ADD R1, [&c]
MOV EAX, [&e] ; EAX lifetime starts.
CDQ           ; EDX lifetime starts
IDIV [&f]     ; EDX lifetime ends.
IMUL EAX, [&h]
ADD R1, EAX   ; EAX lifetime ends.
MOV [&a], R1  ; R1 lifetime ends

R1 EAX EDX. R1, EBX , :

MOV EBX, [&b]
ADD EBX, [&c]
MOV EAX, [&e] 
CDQ           
IDIV [&f]     
IMUL EAX, [&h] 
ADD EBX, EAX  
MOV [&a], EBX

.

. , . , . (, , ), , .

, , , , 4 * a, a, .

0

Source: https://habr.com/ru/post/1794698/


All Articles