123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999 |
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package targets.mips;
- import API.Utils;
- import common.Code;
- import common.DataLayout;
- import common.Instruction;
- import common.IvannosysTargetArch;
- import common.Offset;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.LinkedList;
- /**
- * MipS tranSlation
- *
- * @author EUGENIO CARVALHO
- */
- public class Translate extends API.TargetGen {
- protected String[] registersKeys = "reg.p1,reg.p2".split(",");
- public static LinkedList<String> returnRegisters;
- protected HashMap<String, String> AddressCalculated = new HashMap<>();
- protected HashMap<String, String> comments = new HashMap<String, String>() {
- {
- put("beq", "branch if equals");
- put("bne", "branch if not equals");
- put("blez", "branch if register <= 0");
- put("bltz", "branch if register < 0");
- put("bgez", "branch if register >= 0");
- put("bgtz", "branch if register > 0");
- }
- };
- protected HashMap<String, Boolean> ctrlDataUpdate = new HashMap<>();
- public Translate() {
- super("mips");
- }
- @Override
- public void TranslateLabel(Instruction inst) throws Exception {
- Add(new Instruction()
- .Set("type", "label")
- .Set("format", "norender")
- .Set("label", inst.Get("label")));
- getTarget().RegisterLabelAddress(inst.Get("label"));
- }
- @Override
- public void TranslateCopy(Instruction inst) throws Exception {
- Instruction c = Copy(inst, inst.Get("reg.dst"));
- if (c != null) {
- c.Set("comment", F("copy %s ← %s", inst.Get("dst"), inst.Get("p1")));
- }
- }
- @Override
- public void TranslateAssign(Instruction inst) throws Exception {
- String instruction = Descriprion.InstructionByOperator(inst);
- String rd = inst.Get("reg.dst"),
- dst = inst.Get("dst"),
- op = inst.Get("op"),
- rs,
- offset;
- Instruction ninst = new Instruction(instruction);
- switch (Descriprion.Codops.get(instruction).Get("type")) {
- //addi, addiu, slti, sltiu
- case "I":
- ninst.Set("rt", rd);
- // System.out.println("Assign:" + inst);
- // LoadParam(inst, "p1,p2", true);
- if (inst.eq("p2value", "true")) {
- // LoadParam(inst, "p1");
- // ninst.Set("offset", (op.equals("-") ? "-" : "") + inst.Get("p2"))
- // .Set("rs", inst.Get("reg.p1"));
- offset = inst.Get("p2");
- rs = inst.Get("reg.p1");
- } else {
- offset = inst.Get("p1");
- rs = inst.Get("reg.p2");
- // LoadParam(inst, "p2");
- // ninst.Set("offset", inst.Get("p1"))
- // .Set("rs", inst.Get("reg.p2"));
- }
- ninst.Set("offset", (op.equals("-") ? "-" : "") + offset)
- .Set("rs", rs);
- break;
- // mult, divu, sgtu ,sltu
- // sra, srl, sll
- case "R":
- LoadParam(inst, "p1,p2", true);
- //
- if (ninst.in("inst", "sll,srl,sra".split(","))) {
- rd = inst.Get("reg.dst");
- ninst.Set("sa", inst.Get("p2"))
- .Set("rt", inst.Get("reg.p1"));
- } else {
- ninst.Set("rs", inst.Get("reg.p1"))
- .Set("rt", inst.Get("reg.p2"));
- }
- ninst.Set("rd", rd);
- // System.out.println("inst["+rd+"]" + inst + ":" + ninst);
- break;
- default:
- throw new Exception("Invalid instruction type");
- }
- Add(ninst).Set("comment", F(
- "%s = %s %s %s",
- dst,
- inst.Get("p1"),
- inst.Get("op"),
- inst.Get("p2")
- ));
- // Verifica se a expr possui overflow
- String overflow = Descriprion.Instruction(instruction).Get("overflow");
- if (!overflow.equals("")) {
- Add(new Instruction("R", overflow)
- .Set("rd", rd)
- );
- }
- // StoreResult(inst, rd, dst);
- }
- @Override
- public void TranslateJump(Instruction inst) throws Exception {
- Add(new Instruction("J", "j")
- .copy("label", inst)
- .Set("comment", F("jump to %s", inst.Get("label")))
- );
- Nop(false);
- }
- @Override
- public void TranslateCall(Instruction inst) throws Exception {
- ArrayList<Instruction> pushs = new ArrayList<>();
- ResetReturnArgs();
- // Before load params
- // Copy(inst, returnRegisters.remove(2))
- // .Set("IR.position", inst.Get("block.position"))
- // .Set("comment", "push param");
- // System.out.println("Call" + inst);
- Instruction retur;
- LinkedList<Instruction> intructions = getIR().Block().Instructions();
- int base = intructions.indexOf(inst);
- // System.out.println("TranslateCall" + inst.GetInt("numParams"));
- for (int i = inst.GetInt("numParams"), j = 1; i > 0; j--) {
- retur = intructions.get(base + j);
- if (retur.eq("type", "push_param")) {
- pushs.add(0, retur);
- i--;
- }
- }
- for (Instruction push : pushs) {
- Copy(push, returnRegisters.remove(2))
- .Set("comment", "push param");
- }
- Add(
- new Instruction("J", "jal")
- .Set("label", inst.Get("funcname"))
- .Set("comment", F("jump to <%s>", inst.Get("funcname")))
- );
- // After store results
- // Restaura o array de registradores de retorno para fazer o pop dos returns
- ResetReturnArgs();
- Nop(
- false);
- }
- @Override
- public void TranslateReturn(Instruction inst) throws Exception {
- // Restaura o array de registradores de retorno
- ResetReturnArgs();
- Instruction retur;
- LinkedList<Instruction> intructions = getIR().Block().Instructions();
- int base = intructions.indexOf(inst);
- for (int i = inst.GetInt("p1"); i > 0; i--) {
- retur = intructions.get(base - i);
- // System.out.println("Return:" + retur);
- Copy(retur, returnRegisters.remove(0))
- .Set("comment", "push return ");
- }
- ResetReturnArgs();
- }
- @Override
- public void TranslatePopReturn(Instruction inst) throws Exception {
- String p1 = inst.Get("p1"), reg = returnRegisters.remove(0);
- // Se for uma variavel temporaria
- if (p1.contains("_T")) {
- CopyReg(reg, inst.Get("reg.p1"));
- } else {
- StoreWord(
- target.DEFAULT_WORD_TYPE,
- reg,
- FrameRegister(p1),
- Offset(p1)
- );
- }
- // System.out.println("Translate pop return" + inst);
- }
- @Override
- public void TranslatePopParam(Instruction inst) throws Exception {
- // todo - resetar args
- // System.out.println("PopParam:" + inst);
- String dst = inst.Get("p1");
- // O indice é 2 por conta dos dois primeiros registradores {v0, v1}
- // System.out.println("PopParams:" + inst);
- StoreWord(
- target.DEFAULT_WORD_TYPE,
- returnRegisters.remove(2),
- FrameRegister(dst),
- Offset(dst)
- ).Set("comment", "pop param");
- // System.out.println("POPpARAM:" + inst);
- // LoadWord(inst.Get("reg.p1"), "fp", offset);
- }
- @Override
- public void TranslateBranch(Instruction inst) throws Exception {
- String instruction = Descriprion.InstructionByOperator(inst);
- LoadParam(inst, "p1,p2", true);
- String rs = inst.Get("reg.p1"),
- rt = inst.Get("reg.p2");
- Instruction branch = Descriprion.Instruction(instruction);
- switch (instruction) {
- case "beq": // == {[inst]}' ' {[rt]},{[rs]},{immediate}
- case "bne": // !=
- branch.Set("rs", rs).Set("rt", rt);
- break;
- case "blez": // <= 0{[inst]}' ' {[rs]},{immediate}
- case "bltz": // < 0
- case "bgez": // >= 0
- case "bgtz": // > 0
- Add(new Instruction("R", "subu")// Rd <-- [Rs] - [Rt];
- .Set("rs", rs)
- .Set("rt", rt)
- .Set("rd", "v0")
- .Set("comment", "")
- );
- branch.Set("rs", "v0");
- break;
- }
- Add(branch)
- .Set("label", inst.Get("label"))
- .Set("comment", comments.get(instruction));
- Nop(false);
- }
- protected void Nop(Boolean checkDependence) throws Exception {
- if (checkDependence) {
- Instruction n = Next();
- Integer dep = 0;
- // Se existe uma proxima intrucao
- if (n != null) {
- String dst = currentIrInstruction.Get(
- currentIrInstruction.Has("reg.dst")
- ? "reg.dst" : "reg.p1"
- );
- for (String reg : registersKeys) {
- if (n.eq(reg, dst)) {
- dep++;
- break;
- }
- }
- if (dep == 0) {
- return;
- }
- }
- }
- Add(Descriprion.Instruction("nop"));
- }
- @Override
- public void Prolog(String id) throws Exception {
- DataLayout data = Context();
- // Adiciona o label
- Add(new Instruction()
- .Set("type", "label")
- .Set("format", "label")
- .Set("label", id)
- );
- // Aloca o espaco da pilha
- Instruction alloc = Copy("sp", "sp", "-" + data.Size())
- .Set("comment", "prolog| push stack frame");
- // System.out.println("Stack size for " + id + " -" + data.Size());
- if (!getTarget().Block().getName().equals("main")) {
- // Restaura o fp
- // Restaura o ra
- // boolean call = getIR().Block().HasCall();
- if (getIR().Block().HasCall()) {
- // int newvars = 0;
- for (String reg : new String[]{"fp", "ra"}) {
- data.Add(reg, "int32", 1);
- StoreWord(
- target.DEFAULT_WORD_TYPE,
- reg,
- "sp",
- data.Offset(reg)
- ).Set("comment", F("prolog| backup %s", reg));
- }
- alloc.Set("offset", "-" + data.Size());
- // System.out.println("Update Stack size for " + id + " -" + data.Size());
- }
- }
- if (alloc.getInt("offset", 0) != 0) {
- // Copia o sp para o fp
- CopyReg("sp", "fp").Prepend("comment", "prolog|");
- ResetReturnArgs();
- } else {
- getTarget().Block().Remove(alloc);
- }
- }
- /**
- * Encerra a chamada de uma funcao
- *
- * @param id
- * @throws Exception
- */
- @Override
- public void Epilog(String id) throws Exception {
- DataLayout data = Context();
- if (!getTarget().Block().getName().equals("main")) {
- // Restaura o fp
- // Restaura o ra
- boolean call = getIR().Block().HasCall();
- for (String reg : new String[]{"fp", "ra"}) {
- if (call) {
- LoadWord(
- target.DEFAULT_WORD_TYPE,
- reg,
- "sp",
- data.Offset(reg)
- ).Set("comment", F("epilog| restore ", reg));
- }
- }
- // Desaloca a pilha
- Copy("sp", "sp", Long.valueOf(data.Size()).toString())
- .Set("comment", "epilog| pop stack frame");
- CopyReg("sp", "fp")
- .Set("comment", "epilog| pop stack frame");
- Add(new Instruction("jr")
- .Set("rs", "ra")
- .Set("comment", "epilog| return"));
- } else {
- Add(Descriprion.Instruction("stop").copy());
- }
- }
- @Override
- public void TranslateIndexedAssignment(Instruction inst) throws Exception {
- // //Fase de leitura
- // IndexedRead(inst);
- // //Fase de atribuicao
- // IndexedDest(inst);
- }
- @Override
- public void TranslateUnary(Instruction inst) throws Exception {
- // Carrega o valor se nao esta em registrador
- // System.out.println("Translate Unary:" + inst);
- switch (inst.Get("op")) {
- case "-": // Retorna o valor negado 10 -> -10
- Add(new Instruction("subu")// Rd <-- [Rs] - [Rt];
- .Set("rd", inst.Get("reg.dst"))
- .Set("rs", "zero")
- .Set("rt", inst.Get("reg.p1"))
- .Set("comment", "negation arith")
- );
- break;
- case "!": // XOR ( 0 1 -> 1) XOR( 1 1 -> 0)
- Add(new Instruction("xori") // Rt <-- [Rs] AND (016 || [I15..0]); ;
- .Set("rt", inst.Get("reg.dst"))
- .Set("rs", inst.Get("reg.p1"))
- .Set("offset", "1")
- .Set("comment", "negation bool")
- );
- break;
- // case "*": // Copia do conteudo do ponteiro
- //
- // case "&": // Copia do endereco da variavel
- // break;// case "*": // Copia do conteudo do ponteiro
- //
- // case "&": // Copia do endereco da variavel
- // break;// case "*": // Copia do conteudo do ponteiro
- //
- // case "&": // Copia do endereco da variavel
- // break;// case "*": // Copia do conteudo do ponteiro
- //
- // case "&": // Copia do endereco da variavel
- // break;// case "*": // Copia do conteudo do ponteiro
- //
- // case "&": // Copia do endereco da variavel
- // break;// case "*": // Copia do conteudo do ponteiro
- //
- // case "&": // Copia do endereco da variavel
- // break;// case "*": // Copia do conteudo do ponteiro
- //
- // case "&": // Copia do endereco da variavel
- // break;// case "*": // Copia do conteudo do ponteiro
- //
- // case "&": // Copia do endereco da variavel
- // break;
- }
- }
- @Override
- public void TranslateLoad(Instruction inst) throws Exception {
- String addr = inst.Get("p1"),
- rd = inst.Get("reg.dst"),
- rs;
- if (inst.Has("reg.p1.indice")) {
- rs = inst.Get("reg.p1.indice");
- // Se a variavel não for global adiciona o fp ao deslocamento
- if (!addr.contains("_G")) {
- Add(Descriprion.Instruction("addu"))
- .Set("rt", "fp")
- .Set("rs", rs)
- .Set("rd", rs);
- }
- } else {
- rs = FrameRegister(addr);
- }
- Instruction lw = LoadWord(
- target.DEFAULT_WORD_TYPE,
- rd,
- rs,
- Offset(addr)
- ).Set("comment", F("load content from %s in %s", addr, rd));
- }
- @Override
- public void TranslateStore(Instruction inst) throws Exception {
- // Carrega o parametro p1 se constante
- // Caso a constante seja 0 a instrução não carrega o valor e atualiza o registrador fonte para zero
- LoadParam(inst, "p1", true);
- String addr = inst.Get("dst"),
- rt = inst.Get("reg.p1"),
- rs;
- // System.out.println("Translate Store" + inst);
- if (inst.Has("reg.dst.indice")) {
- rs = inst.Get("reg.dst.indice");
- // Se a variavel não for global adiciona o fp ao deslocamento
- if (!addr.contains("_G")) {
- Add(Descriprion.Instruction("addu"))
- .Set("rt", "fp")
- .Set("rs", rs)
- .Set("rd", rs);
- }
- } else {
- rs = FrameRegister(addr);
- }
- StoreWord(
- target.DEFAULT_WORD_TYPE,
- rt,
- rs,
- Offset(addr)
- ).Set("comment", F("store content of %s in %s", rt, addr));
- }
- /**
- * Traducao de intrucoeS do tipo
- *
- * - x = &a - copia do endereco de uma varaivel <br>
- * - x = *b - x recebe o conteudo da variavel referenciada por b<br>
- * - *x = y - conpia o conteudo de y para o endereco armazenado em x
- *
- * @param inst
- * @throws Exception
- */
- @Override
- public void TranslatePointerAssignment(Instruction inst) throws Exception {
- String p1 = inst.Get("p1"),
- dst = inst.Get("dst"),
- regdst = inst.Get("reg.dst"),
- regp1 = inst.Get("reg.p1");
- switch (inst.Get("op")) {
- case "&": // Lendo o endereco de um ponteiro
- Copy(regp1,
- FrameRegister(p1),
- Offset(p1)
- ).Set("comment", F("copy address of ", p1));
- System.out.println("PointerAssi&:" + inst);
- // Se nao fez o store
- // if (StoreResult(inst, regp1, dst) == null) {
- // CopyReg(regp1, regdst);
- // .Set("comment", "store content of " + regdst + " in " + dst);
- // }
- break;
- case "*": // Lendo o conteudo de um ponteiro
- /**
- * Carrega o valor contido no ponteiro<br>
- * Utiliza o valor como endereço em um segundo load que carrega
- * o dado<br>
- * lw ${p1},12($fp)<br>
- * lw ${dst},0(${p1}) $s<br>
- */
- LoadWord(
- target.DEFAULT_WORD_TYPE,
- regp1,
- FrameRegister(p1),
- Offset(p1)
- ).Set("comment", F("load address stored in %s", p1));
- LoadWord(
- target.DEFAULT_WORD_TYPE,
- regdst,
- regp1,
- "0"
- ).Set("comment", F("load content of address stored in %s", regp1));
- /*Se for a ultima operacao de escrita nessa variavel salva em memoria*/
- // Instruction store = StoreResult(inst, regdst, dst);
- // if (store != null) {
- // store.Set("comment", "store content of " + regdst + " in " + dst);
- // }
- break;
- default: // Atribuicao a um ponteiro
- LoadParam(inst, "p1,p2", true);
- /**
- * Carrega o valor dentro do endereco do ponteiro <br>
- * lw ${p1},12(${fp|gp})<br>
- * lw ${dst},0(${p1}) $s<br>
- * conteudo da variavel referenciada
- */
- LoadWord(
- target.DEFAULT_WORD_TYPE,
- regdst,
- FrameRegister(dst),
- Offset(dst)
- ).Set("comment", F("load address stored in %s", dst));
- // Grava o valor do registrador na memoria
- StoreWord(
- target.DEFAULT_WORD_TYPE,
- regp1,
- regdst,
- "0"
- ).Set("comment", F("store content of %s in *%s", regp1, dst));
- }
- }
- @Override
- public void TranslatePushReturn(Instruction inst) throws Exception {
- // Tratado no metodo 'TranslateReturn'
- }
- @Override
- public void TranslatePushParam(Instruction inst) throws Exception {
- // Tratado no metodo 'TranslateCall'
- // System.out.println("translate push param 2" + (returnRegisters == null) + "/");
- // String reg = returnRegisters.remove(2);
- // System.out.println("translate push param 1" + reg + "");
- // Copy(inst, reg)
- // .Set("comment", "push param");
- }
- @Override
- public IvannosysTargetArch Init(Code tac) throws Exception {
- this.IR = tac;
- // ResetReturnArgs();
- // Middleware.On("mips", "before.translate.block", (target, ir) -> {
- // this.ResetReturnArgs();
- // System.out.println("before.translate.block .... " + this.returnRegisters);
- // });
- getTarget()
- .setAddressIncrement(4)
- .Template(new MipsTemplateDescription());
- return this;
- }
- /**
- * Criar uma inStrucao que copia o valor ou conteudo do regiStrador para o
- * regiStrador 'r'
- *
- * @param inst
- * @param rt
- * @return
- */
- protected Instruction Copy(Instruction inst, String rt) throws Exception {
- Instruction c = null;
- LoadParam(inst, "p1", false);
- if (inst.eq("load.large.constante", "true")) {
- System.out.println("COPY LARGE:" + currentTargetInstruction);
- } else {
- if (inst.IsValue("p1")) {
- // andi Rt,Rs,immediate | Rt <-- [Rs] + ([I15]16 || [I15..0]);
- c = Copy(rt, "zero", inst.Get("p1"));
- } else {
- // addu Rd,Rs,Rt | Rd <-- [Rs] + [Rt];
- c = CopyReg(inst.Get("reg.p1"), rt);
- }
- }
- return c;
- }
- /**
- * @param inst
- * @return
- */
- protected Instruction LoadParam(Instruction inst, String params, boolean copy) throws Exception {
- return LoadParam(inst, params.split(","), copy);
- }
- protected Instruction LoadParam(Instruction inst, String[] params, boolean copy) throws Exception {
- String offset, bin, reg, r;
- // boolean loaded = false;
- Long value;
- Integer lower, upper;
- Instruction loaded = null;
- for (String param : params) {
- if (inst.IsValue(param) && inst.Has("reg." + param)) {
- // loaded = true;
- offset = inst.Get(param);
- value = Long.parseLong(offset);
- bin = Utils.Pad(32, Long.toBinaryString(value));
- // System.out.println("bin:"
- // + "\n" + bin
- // + "\n" + offset
- // + "\n" + bin.length()
- // + "\n" + Long.toBinaryString(Integer.parseInt(bin.substring(0, 16), 2))
- // )
- // );
- //
- // System.out.println(Long.toBinaryString(Integer.parseInt(bin.substring(16), 2));
- if (value > 0 && bin.substring(0, 16).contains("1")) {
- // System.out.println("LOAD PARAM:" + bin);
- upper = Integer.parseInt(bin.substring(0, 16), 2);
- lower = Integer.parseInt(bin.substring(16), 2);
- inst.Set("load.large.constante", "true");
- // System.out.println("Load param:" + inst);
- // r = inst.Get("reg.dst");
- r = inst.Get("reg." + param);
- // System.out.println("upper:" + upper);
- //clui $s0, upper(big)
- Add(Descriprion.Instruction("lui"))
- .Set("rt", r)
- .Set("offset", upper)
- .Set("comment", "load param upper(" + param + ")");
- //ori $s0, $s0, lower(big)
- loaded = Add(Descriprion.Instruction("ori"))
- .Set("rs", r)
- .Set("rt", r)
- .Set("offset", lower)
- .Set("comment", "load param lower(" + param + ")");
- } else if (copy) {
- reg = "reg." + param;
- r = inst.Get(reg);
- if (offset.equals("0")) {
- inst.Set(reg, "zero");
- continue;
- }
- loaded = Copy(r, "zero", offset).Set("comment", "load param (" + param + ")");
- }
- }
- }
- // for (String param : params) {
- // if (inst.IsValue(param) && inst.Has("reg." + param)) {
- //
- // offset = inst.Get(param);
- //
- // if (offset.equals("0")) {
- // inst.Set("reg." + param, "zero");
- // continue;
- // }
- //
- // Copy(inst.Get("reg." + param), "zero", offset);
- // }
- // }
- // return loaded;
- return loaded;
- }
- /**
- * Copia o valor do regiStrador Src para o dSt
- *
- * @param src
- *
- * @param dst
- * @return
- */
- protected Instruction CopyReg(String src, String dst) throws Exception {
- return Add(new Instruction("addu")
- .Set("rs", "zero")
- .Set("rt", src)
- .Set("rd", dst)
- .Set("comment", F("copy %s ← %s", dst, src)));
- }
- protected Instruction Copy(String rt, String rs, Offset offset) throws Exception {
- return Copy(rt, rs, "" + offset);
- }
- protected Instruction Copy(String rt, String rs, String offset) throws Exception {
- return Add(new Instruction("addiu") // lw $rt, imm($rs)
- .Set("rt", rt)
- .Set("rs", rs)
- .Set("offset", offset)
- .Set("comment", F("copy %s ← %s", rs, rt))
- );
- }
- protected Instruction StoreWord(String type, String rt, String rs, Offset offset) throws Exception {
- Instruction n = Next();
- String inst;
- switch (type) {
- case "byte":
- case "int8":
- case "uint8":
- case "bool":
- inst = "sb"; // Load byte
- break;
- case "int16":
- case "uint16":
- inst = "sh"; //Load half
- break;
- default:
- inst = "sw"; // Load word
- }
- Instruction sw = Add(new Instruction(inst)
- .Set("rt", rt)
- .Set("rs", rs)
- .Set("offset", offset.getAddress() + offset.getShift()));
- return sw;
- }
- protected Instruction StoreWord(String type, String rt, String rs, String offset) throws Exception {
- return StoreWord(type, rt, rs, new Offset(Long.parseLong(offset), 0L));
- }
- protected Instruction LoadWord(String type, String rt, String rs, Offset offset) throws Exception {
- Instruction n = Next();
- String inst;
- switch (type) {
- case "byte":
- case "int8":
- case "uint8":
- case "bool":
- // LB -- Load byte 8 bits
- // Operation:$t = MEM[$s + offset]; advance_pc (4);
- // Syntax: lb $t, offset($s)
- // Offset é o endereço base da variavel
- // Shift é o byte a ser lido
- inst = "lb";
- break;
- case "int16":
- case "uint16":
- // LW -- Load half 16 bits
- // Operation:$t = MEM[$s + offset]; advance_pc (4);
- // Syntax: lw $t, offset($s)
- inst = "lh";
- break;
- default:
- // LW -- Load word 32 bits
- // Operation:$t = MEM[$s + offset]; advance_pc (4);
- // Syntax: lw $t, offset($s)
- inst = "lw"; // Load word
- }
- Instruction lw = Add(new Instruction(inst) // lw $rt, imm($rs)
- .Set("rt", rt)
- .Set("rs", rs)
- .Set("offset", offset.getAddress() + offset.getShift())
- );
- Nop(true);
- return lw;
- }
- protected Instruction LoadWord(String type, String rt, String rs, String offset) throws Exception {
- return LoadWord(type, rt, rs, new Offset(Long.parseLong(offset), 0L));
- }
- @Override
- protected Instruction Add(Instruction inst) throws Exception {
- if (!inst.eq("type", "label")) {
- getTarget().PositionInc();
- Instruction definiction = Descriprion.Instruction(inst.Get("inst"));
- inst.Merge("sa,type,inst,codop,func,format", definiction);
- }
- if (currentIrInstruction != null) {
- inst.Set("inloop", currentIrInstruction.Get("inloop"));
- }
- // System.out.println("Add:" + inst);
- return super.Add(inst); //To change body of generated methods, choose Tools | Templates.
- }
- protected String FrameRegister(String var) {
- return var.contains("_G") ? "gp" : "fp";
- }
- public static void ResetReturnArgs() {
- returnRegisters = new LinkedList<String>() {
- {
- add("v0");
- add("v1");
- add("a0");
- add("a1");
- add("a2");
- add("a3");
- }
- };
- // System.out.println("Resetei args" + returnRegisters);
- }
- }
- // protected void IndexedRead(Instruction inst) throws Exception {
- // // Se a fonte é indexada
- // if (inst.eq("src_indexed", "true")) {
- // try {
- // String p1 = inst.Get("p1"),
- // rt = inst.Get("reg.p1"),
- // rs = FrameRegister(p1);
- //
- //// System.out.println("Indexed read:" + inst);
- // if (!inst.isNumber("p1.indice")) {
- //
- // String rd = inst.Get("reg.p1.indice");
- // if (!AddressCalculated.containsKey(p1)) {
- // Add(new Instruction("addu")
- // .Set("rd", rd)
- // .Set("rs", rs)
- // .Set("rt", rd));
- // AddressCalculated.put(p1, rs);
- // }
- // rs = rd;
- // }
- // // Carrega o conteudo enderecado em rt para rt
- // LoadWord(
- // target.DEFAULT_WORD_TYPE,
- // rt,
- // rs,
- // Offset(p1)
- // );
- //
- // } catch (Exception ex) {
- // Logger.getLogger(Translate.class.getName()).log(Level.SEVERE, null, ex);
- // }
- //
- // } else {
- // // Se não é indexada carrega o parametro normalmente
- // LoadParam(inst);
- // }
- // }
- //
- // /**
- // * FaSe de atribuicao
- // *
- // * @param inst
- // */
- // protected void IndexedDest(Instruction inst) throws Exception {
- //// System.out.println("Indexeds:" + inst);
- //
- // String regp1 = inst.Get("reg.p1"),
- // regdst = inst.Get("reg.dst");
- //
- // // Valor atribuido é indexado
- // // 1 - Verificar se deve ser armazenado ou copiado
- //// boolean dstIndexed = inst.eq("dst.indexed", "true");
- // if (inst.eq("dst.indexed", "true")) {
- //// if (dstIndexed || inst.eq("reg.dst.store", "true")) {
- // // deve ser armazenado
- // String dst = inst.Get("dst"),
- // rs = FrameRegister(dst);
- //// if (dstIndexed) {
- // if (!inst.isNumber("dst.indice")) {
- // // Offset agora deve ser 0 pois o registrador conterá o endereco completo
- //// offset = 0;
- // String rd = inst.Get("reg.dst.indice");
- // if (!AddressCalculated.containsKey(dst)) {
- // Add(new Instruction("addu")
- // .Set("rd", rd)
- // .Set("rs", rs)
- // .Set("rt", rd)
- // );
- // AddressCalculated.put(dst, rs);
- // }
- // rs = rd;
- // }
- //// comment = "store in loaded address";
- //// } else {
- //// comment = "store in " + dst;
- //// }// System.out.println("Inst:" + inst);
- // StoreWord(
- // target.DEFAULT_WORD_TYPE,
- // regp1,
- // rs,
- // Offset(dst)
- // ).Set("comment", F("store content of %s in %s", rs, dst));
- //
- // } else { // deve ser copiado
- // CopyReg(regp1, regdst);
- // }
- // }
- // public void CopyData(LinkedHashMap<String, Node> dst, LinkedHashMap<String, Node> src) throws Exception {
- // public void CopyData(DataLayout dst, DataLayout src) throws Exception {
- // Node value;
- // LinkedHashMap<String, Node> svalues = src.values(),
- // dvalues = dst.values();
- //
- // for (Map.Entry<String, Node> x : svalues.entrySet()) {
- // value = x.getValue().copy();
- // value.S("size", value.GetInt("size") * WORD_INC);
- //// System.out.println("Copy:[" + x.getKey() + "][" + value.Get("id") + "]" + value.Get("size") + "\n" + value);
- //// dvalues.put(x.getKey(), value);
- //
- // dst.Set(x.getKey(), value);
- // }
- // }
|