bottled_ghost
bottled_ghost

Reputation: 113

Logical expressions and intermediate code generation

I managed to get the lexer, syntax checker and semantics and now I want to move on intermediate code generation. The problem is that i don't know how to handle logical expressions. I read something about E.true and E.false. This example is everywhere but I didn't understand it.

For example if I have the following code

if x>y and x<y or x == 1 then
    //super duper code here
    x = x+1    
else
    //super duper wow code here
    y = y+1
endif

The result must be something like this

1: > x y 3
2: jmp _ _ 9
3: < x y 7
4: jmp _ _ 5
5: == x 1 _
6: jmp _ _ 9
7: + 1 x $1
8: = $1 _ x
9: + 1 y $2
10: = $2 _ y

but the labels for the jumps are not known until you actually finish parsing the if statement.

So I have to generate quads and then backpatch them. How can I do it with the grammar of this post?

Can someone explain how it will go because I am really confused.

Upvotes: 0

Views: 1093

Answers (1)

Stephen C
Stephen C

Reputation: 718946

Yes you should be generating branch targets that are symbols / labels. If your intermediate language (IL) supports this, then you probably should not use numeric instruction locations at all. (Numeric intermediate instruction location don't help the final code generation ... because they don't map simply to addresses / offsets for the final instructions.)

The other hint is that many constructs involve sequential execution, and that includes most types of simple expression. So it would simplify things if the IL supported this; i.e. no target label means continue to next instruction. This will simplify code generation of the IL sequences.

Upvotes: 1

Related Questions