I am doing a project in which I have to create a translator that would generate a MIPS assembly code for a C code. The programming language that am using is C++ and I have done till generation of three address code and am really confused about how to proceed further.
Asked
Active
Viewed 4,838 times
3
-
23 address code? Can you clarify? (And I've removed the compiler-optimization tag on your question. Considering it's not yet fully working, I'm assuming you're not optimizing.) – Corbin Apr 28 '12 at 09:28
-
1Start with finding a translation for one 3-address instruction, say `add`. Can you find an equivalent in the list of MIPS instructions? Perhaps an approximate equivalent? – n. m. could be an AI Apr 28 '12 at 09:48
-
@user - You have to look at each 3-address instruction and figure out how you would do that in assembly. Then produce one or more assembly instructions. – Bo Persson Apr 28 '12 at 09:57
-
Yes,even i am thinking about taking one instruction at a time, but i am not getting a clear picture about it and about compiler optimization, to generate a assembly code from intermediate code(three address code) you need to optimize it first. – user1362653 Apr 28 '12 at 18:00
1 Answers
5
As already stated, it's a direct translation. There's really nothing to clarify. As an example, take the following three-address code:
i := 0 ; assignment
L1: if i >= 10 goto L2 ; conditional jump
t0 := i*i
t1 := &b ; address-of operation
t2 := t1 + i ; t2 holds the address of b[i]
*t2 := t0 ; store through pointer
i := i + 1
goto L1
L2:
The MIPS translation is:
li $t0, 0 #allocator assigned i to t0
L1: bge $t0, 10, L2
mult $t1, $t0, $t0
la $t2, b
add $t3, $t2, $t0
sw $t1, ($t3)
addi $t0, $t0, 1
j L1
L2:
If your lucky enough to have three-address like that, you barely have to do anything. Find the corresponding opcode to go with the instruction. The register allocation has already been done. If the three-address code is literally a bunch a strings, I'd consider writing a small parser (using a generator) instead of trying to extract information from the strings.

blackcompe
- 3,180
- 16
- 27