0

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.

1
  • 1
    Should the homework tag be added to this question also? Commented Dec 21, 2011 at 23:46

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you give an example with the code that I have in my post?
How nice of you. You 've been very helpful. I didn't ask you to write the code for me. I figured it out myself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.