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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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.