How to convert a three address code to MIPS Assemb

2019-04-15 02:08发布

问题:

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.