123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494 |
- /*
- * 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 common.Instruction;
- import java.util.LinkedHashMap;
- /**
- *
- * @author EUGENIO CARVALHO
- */
- public class Descriprion {
- public static LinkedHashMap<String, Instruction> instOperators = new LinkedHashMap<String, Instruction>() {
- {
- put("+", new Instruction()
- // .Set("std", "add");
- // .Set("ime", "addi");
- .Set("std", "addu")
- .Set("ime", "addiu")
- );
- put("-", new Instruction()
- // .Set("std", "sub")
- // .Set("uns", "subu")
- .Set("ime", "addiu")
- .Set("std", "subu")
- );
- put("*", new Instruction()
- // .Set("std", "mult")
- // .Set("uns", "multu")
- .Set("std", "mult"));
- put("/", new Instruction()
- // .Set("std", "div")
- // .Set("uns", "divu")
- .Set("std", "divu")
- );
- put("%", new Instruction()
- // .Set("std", "div")
- // .Set("uns", "divu")
- .Set("std", "divu")
- );
- put("&&", new Instruction()
- .Set("std", "and")
- .Set("ime", "andi")
- .Set("assign", "and")
- .Set("atribuicao_ime", "andi")
- );
- put("||", new Instruction()
- .Set("std", "or")
- .Set("ime", "ori")
- .Set("assign", "or")
- .Set("atribuicao_ime", "ori")
- );
- put("<", new Instruction()
- .Set("std", "bltz")
- .Set("assign", "sltu")
- .Set("atribuicao_ime", "sltiu")
- );
- put(">", new Instruction()
- .Set("std", "bgtz")
- .Set("assign", "sgtu")
- );
- put("<=", new Instruction()
- .Set("std", "blez")
- .Set("assign", "sleu"));
- put(">=", new Instruction()
- .Set("std", "bgez")
- .Set("assign", "sgeu")
- );
- put("==", new Instruction()
- .Set("std", "beq")
- .Set("assign", "seq")
- );
- put("!=", new Instruction()
- .Set("std", "bne")
- .Set("assign", "sne")
- );
- put("<<", new Instruction()
- .Set("std", "sllv")
- .Set("ime", "sll")
- );
- put(">>", new Instruction()
- .Set("std", "srlv")
- .Set("ime", "srl")
- );
- put("|", new Instruction()
- .Set("std", "or")
- .Set("ime", "ori")
- .Set("assign", "or")
- .Set("atribuicao_ime", "ori")
- );
- put("^", new Instruction()
- .Set("std", "xor")
- .Set("ime", "xori")
- .Set("assign", "xor")
- .Set("atribuicao_ime", "xori")
- );
- put("&", new Instruction()
- .Set("std", "and")
- .Set("ime", "andi")
- .Set("assign", "and")
- .Set("atribuicao_ime", "andi")
- );
- }
- };
- public static LinkedHashMap<String, String> registers = new LinkedHashMap<String, String>() {
- {
- put("zero", "0");
- put("at", "1");
- put("v0", "2");
- put("v1", "3");
- put("a0", "4");
- put("a1", "5");
- put("a2", "6");
- put("a3", "7");
- put("t0", "8");
- put("t1", "9");
- put("t2", "10");
- put("t3", "11");
- put("t4", "12");
- put("t5", "13");
- put("t6", "14");
- put("t7", "15");
- put("s0", "16");
- put("s1", "17");
- put("s2", "18");
- put("s3", "19");
- put("s4", "20");
- put("s5", "21");
- put("s6", "22");
- put("s7", "23");
- put("t8", "24");
- put("t9", "25");
- put("k0", "26");
- put("k1", "27");
- put("gp", "28");
- put("sp", "29");
- put("fp", "30");
- put("ra", "31");
- }
- };
- public static LinkedHashMap<String, Instruction> Codops = new LinkedHashMap<String, Instruction>() {
- {
- // Inicio da definicao base das instruções I, R e J
- Instruction I = new Instruction()
- .Set("type", "I")
- .Set("rs", "zero")
- .Set("rt", "zero")
- .Set("offset", "0")
- .Set("format", "I0");
- Instruction R = new Instruction()
- .Set("type", "R")
- .Set("sa", "0")
- .Set("rs", "zero")
- .Set("rt", "zero")
- .Set("rd", "zero")
- .Set("func", "0")
- .Set("format", "R0");
- Instruction J = new Instruction()
- .Set("type", "J")
- .Set("format", "J");
- // Fim da definicao base das instruções I, R e J
- put("nop", new Instruction("sll")
- .Set("rs", "zero")
- .Set("rt", "zero")
- .Set("rd", "zero")
- .Set("comment", "Nop"));
- put("stop", new Instruction()
- .Set("type", "S")
- .Set("inst", "stop")
- .Set("codop", "111111")
- .Set("txt", "11111111111111111111111111111111")
- .Set("format", "S")
- .Set("comment", "End of programa"));
- put("add", R.copy()
- .Set("inst", "add")
- .Set("codop", "000000")
- .Set("func", "100000"));
- put("addu", R.copy()
- .Set("inst", "addu")
- .Set("codop", "000000")
- .Set("func", "100001"));
- put("and", R.copy()
- .Set("inst", "and")
- .Set("codop", "000000")
- .Set("func", "100100"));
- put("div", R.copy()
- .Set("inst", "div")
- .Set("overflow", "mflo")
- .Set("codop", "000000")
- .Set("func", "011010")
- .Set("format", "R1"));
- put("divu", R.copy()
- .Set("inst", "divu")
- .Set("/", "mflo")
- .Set("%", "mfhi")
- .Set("codop", "000000")
- .Set("func", "011011")
- .Set("format", "R1"));
- put("jr", R.copy()
- .Set("inst", "jr")
- .Set("codop", "000000")
- .Set("func", "001000")
- .Set("format", "R2"));
- put("mfhi", R.copy()
- .Set("inst", "mfhi")
- .Set("codop", "000000")
- .Set("func", "010000")
- .Set("format", "R5"));
- put("mflo", R.copy()
- .Set("inst", "mflo")
- .Set("codop", "000000")
- .Set("func", "010010")
- .Set("format", "R5"));
- put("mthi", R.copy()
- .Set("inst", "mthi")
- .Set("codop", "000000")
- .Set("func", "010001")
- .Set("format", "R5"));
- put("mtlo", R.copy()
- .Set("inst", "mtlo")
- .Set("codop", "000000")
- .Set("func", "010011")
- .Set("format", "R5"));
- put("mult", R.copy()
- .Set("inst", "mult")
- .Set("overflow", "mflo")
- .Set("codop", "000000")
- .Set("func", "011000")
- .Set("format", "R1"));
- put("multu", R.copy()
- .Set("inst", "multu")
- .Set("overflow", "mflo")
- .Set("codop", "000000")
- .Set("func", "011001")
- .Set("format", "R1"));
- put("nor", R.copy()
- .Set("inst", "nor")
- .Set("codop", "000000")
- .Set("func", "100111"));
- put("or", R.copy()
- .Set("inst", "or")
- .Set("codop", "000000")
- .Set("func", "100101"));
- put("sll", R.copy()
- .Set("inst", "sll")
- .Set("codop", "000000")
- .Set("func", "000000")
- .Set("rs", "zero")
- .Set("rt", "zero")
- .Set("rd", "zero")
- .Set("format", "R4"));
- put("slt", R.copy()
- .Set("inst", "slt")
- .Set("codop", "000000")
- .Set("func", "101010"));
- put("sra", R.copy()
- .Set("inst", "sra")
- .Set("codop", "000000")
- .Set("func", "000011")
- .Set("rs", "zero")
- .Set("rt", "zero")
- .Set("rd", "zero")
- .Set("format", "R4"));
- put("srl", R.copy()
- .Set("inst", "srl")
- .Set("codop", "000000")
- .Set("func", "000010")
- .Set("format", "R4"));
- put("sub", R.copy()
- .Set("inst", "sub")
- .Set("codop", "000000")
- .Set("func", "100010"));
- put("subu", R.copy()
- .Set("inst", "subu")
- .Set("codop", "000000")
- .Set("func", "100011"));
- put("xor", R.copy()
- .Set("inst", "xor")
- .Set("codop", "000000")
- .Set("func", "100110"));
- //
- // I instructions
- //
- put("addi", I.copy()
- .Set("inst", "addi")
- .Set("codop", "001000")
- .Set("format", "I0"));
- put("addiu", I.copy()
- .Set("inst", "addiu")
- .Set("codop", "001001")
- .Set("format", "I0"));
- put("andi", I.copy() //r<0
- .Set("inst", "andi")
- .Set("codop", "001100")
- .Set("format", "I0"));
- put("beq", I.copy()//r = Set
- .Set("inst", "beq")
- .Set("codop", "000100")
- .Set("format", "I0"));
- put("bgez", I.copy()//r >= 0
- .Set("inst", "bgez")
- .Set("codop", "000001")
- .Set("rt", "00001")
- .Set("format", "I2"));
- // .Set("format", "I0"));
- put("bgtz", I.copy()//r>0
- .Set("inst", "bgtz")
- .Set("codop", "000111")
- .Set("rt", "zero")
- .Set("format", "I2"));
- // .Set("format", "I0"));
- put("blez", I.copy()//r<=0
- .Set("inst", "blez")
- .Set("codop", "000110")
- .Set("rt", "zero")
- .Set("format", "I2"));
- // .Set("format", "I0"));
- put("bltz", I.copy()//r<0
- .Set("inst", "bltz")
- .Set("codop", "000001")
- .Set("rt", "00000")
- // .Set("format", "I0"));
- .Set("format", "I2"));
- put("bne", I.copy()//r != Set
- .Set("inst", "bne")
- .Set("codop", "000101")
- .Set("format", "I0"));
- // put("bnez", I.copy()//r = Set
- // .Set("inst", "bnez")
- // .Set("codop", "000101")
- // .Set("format", "I0"));
- put("lb", I.copy()
- .Set("inst", "lb")
- .Set("codop", "100000c")
- .Set("format", "I2"));
- put("lbu", I.copy()
- .Set("inst", "lbu")
- .Set("codop", "100100")
- .Set("format", "I2"));
- put("lh", I.copy()
- .Set("inst", "lh")
- .Set("codop", "100001")
- .Set("format", "I2"));
- put("lhu", I.copy()
- .Set("inst", "lhu")
- .Set("codop", "100101")
- .Set("format", "I2"));
- put("lui", I.copy()
- .Set("inst", "lui")
- .Set("codop", "001111")
- .Set("format", "I0"));
- put("lw", I.copy()
- .Set("inst", "lw")
- .Set("codop", "100011")
- .Set("format", "I1"));
- put("ori", I.copy() //r<0
- .Set("inst", "ori")
- .Set("codop", "001101")
- .Set("format", "I0"));
- put("slti", I.copy()
- .Set("inst", "slti")
- .Set("codop", "001010")
- .Set("format", "I0"));
- put("sltiu", I.copy()
- .Set("inst", "sltiu")
- .Set("codop", "001011")
- .Set("format", "I2"));
- put("sltiu", I.copy()
- .Set("inst", "sltiu")
- .Set("codop", "001011")
- .Set("format", "I0"));
- put("sw", I.copy()
- .Set("inst", "sw")
- .Set("codop", "101011")
- .Set("format", "I1"));
- put("sb", I.copy()
- .Set("inst", "sb")
- .Set("codop", "101000")
- .Set("format", "I0"));
- put("xori", I.copy()
- .Set("inst", "xori")
- .Set("codop", "001110")
- .Set("format", "I0"));
- //
- // J instructions
- //
- put("j", J.copy()
- .Set("inst", "j")
- .Set("codop", "000010"));
- put("jal", J.copy()
- .Set("inst", "jal")
- .Set("codop", "000011"));
- }
- };
- public static Instruction Instruction(String inst) throws Exception {
- if (!Codops.containsKey(inst)) {
- throw new Exception(String.format("Instruction '%s' not defined", inst));
- }
- return Codops.get(inst).copy();
- }
- public static String InstructionByOperator(Instruction inst) throws Exception {
- Instruction r = null;
- String sentence = inst.Get("type"), type = "std";
- String op = inst.Get("op");
- if (!op.equals("")) { // Se é um expr
- if (!instOperators.containsKey(op)) {
- throw new Exception("Operador {" + op + "} não definido.");
- }
- r = instOperators.get(op);
- if (r.Has("ime")
- && (inst.eq("p1value", "true") || inst.eq("p2value", "true"))) {
- type = "ime";
- }
- }
- if (r == null) {
- throw new Exception(String.format("Sentenca '%s' não definida", sentence));
- }
- return r.Get(type);
- }
- public static String Register(String reg) {
- return registers.get(reg);
- }
- }
|