In: Computer Science
Can someone please convert this java code to C code?
import java.util.LinkedList; | |
import java.util.List; | |
public class Phase1 { | |
/* Translates the MAL instruction to 1-3 TAL instructions | |
* and returns the TAL instructions in a list | |
* | |
* mals: input program as a list of Instruction objects | |
* | |
* returns a list of TAL instructions (should be same size or longer than input list) | |
*/ | |
public static List<Instruction> temp = new LinkedList<>(); | |
public static List<Instruction> mal_to_tal(List<Instruction> mals) { | |
for (int i = 0; i < mals.size(); i++) { | |
Instruction current = mals.get(i); | |
int rs = current.rs; | |
int rd = current.rd; | |
int rt = current.rt; | |
int imm = current.immediate; | |
int jumpAddress = current.jump_address; | |
int shiftAmount = current.shift_amount; | |
String label = current.label; | |
String branchLabel = current.branch_label; | |
int upperImm = imm >>> 16; | |
int lowerImm = imm & 0xFFFF; | |
int t1 = 0; | |
int t2 = 0; | |
int t3 = 0; | |
int at = 0; | |
if ((current.instruction_id == Instruction.ID.addiu | |
|| current.instruction_id == Instruction.ID.ori) && (imm > 65535)) { | |
at = 1; | |
temp.add(InstructionFactory.CreateLui(at, upperImm, label)); | |
temp.add(InstructionFactory.CreateOri(at, at, lowerImm)); | |
if (current.instruction_id == Instruction.ID.addiu) temp.add(InstructionFactory.CreateAddu(rt, rs, at)); | |
else temp.add(InstructionFactory.CreateOr(rt, rs, at)); | |
} | |
else if (current.instruction_id == Instruction.ID.blt) { | |
//t1 = rt, t2 = rs | |
if (rt > rs) { | |
at = 1; | |
} | |
temp.add(InstructionFactory.CreateSlt(at, rt, rs)); | |
temp.add(InstructionFactory.CreateBne(at, 0, branchLabel)); | |
} | |
else if (current.instruction_id == Instruction.ID.bge) { | |
at = 1; | |
temp.add(InstructionFactory.CreateSlt(at, rt, rs)); | |
temp.add(InstructionFactory.CreateBeq(at,0, branchLabel)); | |
} | |
else temp.add(current); | |
} | |
return temp; | |
} | |
} to C code: void mal_to_tal(structArrayList *mals, structArrayList *tals) { } |
Working code implemented in C and appropriate comments provided for better understanding.
Note: I am not able to paste all the code here. That's why I am sharing whole project through a link.
Link: https://gofile.io/d/NvyckR
Mirror Link: https://anonymousfiles.io/oUDFfGmV/
Sample Codes:
Source Code for Phase1.c:
#include "Phase1.h"
void mal_to_tal(struct ArrayList *mals, struct ArrayList *tals)
{
for (int i = 0; i < mals->size; i++) {
enum ID id =
get(mals,i).instruction_id; // get the id number at the start of
the loop
// instruction is addiu
if (id == 7 && (get(mals,
i).immediate >= 65536 || get(mals, i).immediate <= -32768))
{
struct
Instruction inst;
int shifted =
get(mals, i).immediate >> 16; // upper 16 bits of the
immediate
if (shifted >
0xFFFF0000) { shifted -= 0xFFFF0000; }
inst =
newInstruction(lui, 0, 0, 1, shifted, 0, 0, get(mals, i).label,
"");
addLast(tals,
inst);
shifted =
get(mals, i).immediate << 16;
// lower 16 bits of the immediate
shifted =
shifted >> 16;
if (shifted >
0xFFFF0000) { shifted -= 0xFFFF0000; }
inst =
newInstruction(ori, 0, 1, 1, shifted, 0, 0, "", "");
addLast(tals,
inst);
inst =
newInstruction(addu, get(mals, i).rt, get(mals, i).rs, 1, 0, 0, 0,
"", "");
addLast(tals,
inst);
}
// instruction is ori
else if (id == 8 &&
(get(mals, i).immediate >= 65536 || get(mals, i).immediate <=
0)) {
struct
Instruction inst;
int shifted =
get(mals, i).immediate >> 16; // upper 16 bits of the
immediate
if (shifted >
0xFFFF0000) { shifted -= 0xFFFF0000; }
inst =
newInstruction(lui, 0, 0, 1, shifted, 0, 0, get(mals, i).label,
"");
addLast(tals,
inst);
shifted =
get(mals, i).immediate << 16;
// lower 16 bits of the immediate
shifted =
shifted >> 16;
if (shifted >
0xFFFF0000) { shifted -= 0xFFFF0000; }
inst =
newInstruction(ori, 0, 1, 1, shifted, 0, 0, "", "");
addLast(tals,
inst);
inst =
newInstruction(or, get(mals, i).rt, get(mals, i).rs, 1, 0, 0, 0,
"", "");
addLast(tals,
inst);
}
// instruction is blt
else if (id == 10) {
struct
Instruction inst = newInstruction(slt, 1, get(mals, i).rt,
get(mals, i).rs, 0, 0, 0, get(mals, i).label, "");
addLast(tals,
inst);
inst =
newInstruction(bne, 0, 1, 0, 0, 0, 0, "", get(mals,
i).branch_label);
addLast(tals,
inst);
}
// instruction is bge
else if (id == 11) {
struct
Instruction inst = newInstruction(slt, 1, get(mals, i).rt,
get(mals, i).rs, 0, 0, 0, get(mals, i).label, "");
addLast(tals,
inst);
inst =
newInstruction(beq, 0, 1, 0, 0, 0, 0, "", get(mals,
i).branch_label);
addLast(tals,
inst);
}
// instruction is mov
else if (id == 12) {
struct
Instruction inst = newInstruction(addu, get(mals, i).rd, get(mals,
i).rs, get(mals, i).rt, 0 , 0, 0, get(mals, i).label, "");
addLast(tals,
inst);
}
// if instruction is already
TAL, add it to tals
else { addLast(tals, get(mals,i));
}
}
}
Source Code for Phase1.h:
#pragma once
#include "ArrayList.h"
/* Translates the MAL instruction to 1-3 TAL instructions
* and returns the TAL instructions in a list
*
* mals: input program as a list of Instruction objects
* tals: after the function returns, will contain TAL instructions
(should be same size or longer than input list)
*/
void mal_to_tal(struct ArrayList *mals, struct ArrayList
*tals);