/*
 * 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 target.mips;

import API.Instruction;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author EUGENIO CARVALHO
 */
class Mips {

    protected static Map<String, Instruction> instOperators = new HashMap<String, Instruction>() {
        {
            put("+", new Instruction()
                    //        .S("std", "add");
                    //        .S("ime", "addi");
                    .S("std", "addu")
                    .S("ime", "addiu")
            );

            put("-", new Instruction()
                    //        .S("std", "sub")
                    //        .S("uns", "subu")
                    .S("std", "subu")
            );

            put("*", new Instruction()
                    //        .S("std", "mult")
                    //        .S("uns", "multu")
                    .S("std", "mult"));

            put("/", new Instruction()
                    //        .S("std", "div")
                    //        .S("uns", "divu")
                    .S("std", "divu")
            );
            put("%", new Instruction()
                    //        .S("std", "div")
                    //        .S("uns", "divu")
                    .S("std", "divu")
            );

            put("&&", new Instruction()
                    .S("std", "and")
                    .S("ime", "andi")
                    .S("assign", "and")
                    .S("atribuicao_ime", "andi")
            );

            put("||", new Instruction()
                    .S("std", "or")
                    .S("ime", "ori")
                    .S("assign", "or")
                    .S("atribuicao_ime", "ori")
            );

            put("<", new Instruction()
                    .S("std", "bltz")
                    .S("assign", "sltu")
                    .S("atribuicao_ime", "sltiu")
            );

            put(">", new Instruction()
                    .S("std", "bgtz")
                    .S("assign", "sgtu")
            );

            put("<=", new Instruction()
                    .S("std", "blez")
                    .S("assign", "sleu"));

            put(">=", new Instruction()
                    .S("std", "bgez")
                    .S("assign", "sgeu")
            );

            put("==", new Instruction()
                    .S("std", "beq")
                    .S("assign", "seq")
            );

            put("!=", new Instruction()
                    .S("std", "bne")
                    .S("assign", "sne")
            );

            put("<<", new Instruction()
                    .S("std", "sllv")
                    .S("ime", "sll")
            );
            put(">>", new Instruction()
                    .S("std", "srlv")
                    .S("ime", "srl")
            );
            put("|", new Instruction()
                    .S("std", "or")
                    .S("ime", "ori")
                    .S("assign", "or")
                    .S("atribuicao_ime", "ori")
            );
            put("^", new Instruction()
                    .S("std", "xor")
                    .S("ime", "xori")
                    .S("assign", "xor")
                    .S("atribuicao_ime", "xori")
            );
            put("&", new Instruction()
                    .S("std", "and")
                    .S("ime", "andi")
                    .S("assign", "and")
                    .S("atribuicao_ime", "andi")
            );
        }
    };
    public static HashMap<String, String> registers = new HashMap<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");
        }
    };

    protected static Map<String, Instruction> Codops = new HashMap<String, Instruction>() {
        {
            put("stop", new Instruction()
                    .S("type", "S")
                    .S("inst", "stop")
                    .S("txt", "11111111111111111111111111111111")
                    .S("format", "S"));

            put("add", new Instruction()
                    .S("type", "R")
                    .S("sa", "0")
                    .S("inst", "add")
                    .S("codop", "000000")
                    .S("func", "100000")
                    .S("format", "R0"));

            put("addu", new Instruction()
                    .S("type", "R")
                    .S("sa", "0")
                    .S("inst", "addu")
                    .S("codop", "000000")
                    .S("func", "100001")
                    .S("format", "R0"));

            put("sub", new Instruction()
                    .S("type", "R")
                    .S("sa", "0")
                    .S("inst", "sub")
                    .S("codop", "000000")
                    .S("func", "100010")
                    .S("format", "R0"));

            put("subu", new Instruction()
                    .S("type", "R")
                    .S("sa", "0")
                    .S("inst", "subu")
                    .S("codop", "000000")
                    .S("func", "100011")
                    .S("format", "R0"));

            put("and", new Instruction()
                    .S("type", "R")
                    .S("sa", "0")
                    .S("inst", "and")
                    .S("codop", "000000")
                    .S("func", "100100")
                    .S("format", "R0"));

            put("or", new Instruction()
                    .S("type", "R")
                    .S("sa", "0")
                    .S("inst", "or")
                    .S("codop", "000000")
                    .S("func", "100101")
                    .S("format", "R0"));

            put("xor", new Instruction()
                    .S("type", "R")
                    .S("sa", "0")
                    .S("inst", "xor")
                    .S("codop", "000000")
                    .S("func", "100110")
                    .S("format", "R0"));
            put("xori", new Instruction()
                    .S("type", "I")
                    .S("inst", "xori")
                    .S("codop", "001110")
                    .S("format", "I0"));

            put("nor", new Instruction()
                    .S("type", "R")
                    .S("sa", "0")
                    .S("inst", "nor")
                    .S("codop", "000000")
                    .S("func", "100111")
                    .S("format", "R0"));

            put("addi", new Instruction()
                    .S("type", "I")
                    .S("inst", "addi")
                    .S("codop", "001000")
                    .S("format", "I0"));

            put("addiu", new Instruction()
                    .S("type", "I")
                    .S("inst", "addiu")
                    .S("codop", "000000")
                    .S("func", "001001")
                    .S("format", "I0"));

            put("mult", new Instruction()
                    .S("type", "R")
                    .S("sa", "0")
                    .S("inst", "mult")
                    .S("overflow", "mflo")
                    .S("codop", "000000")
                    .S("func", "011000")
                    .S("format", "R1"));

            put("div", new Instruction()
                    .S("type", "R")
                    .S("sa", "0")
                    .S("inst", "div")
                    .S("overflow", "mflo")
                    .S("codop", "000000")
                    .S("func", "011010")
                    .S("format", "R1"));

            put("divu", new Instruction()
                    .S("type", "R")
                    .S("sa", "0")
                    .S("inst", "div")
                    .S("/", "mflo")
                    .S("%", "mfhi")
                    .S("codop", "000000")
                    .S("func", "011011")
                    .S("format", "R1"));

            put("mfhi", new Instruction()
                    .S("type", "R")
                    .S("sa", "0")
                    .S("inst", "div")
                    .S("codop", "000000")
                    .S("func", "010000")
                    .S("format", "R5"));

            put("mflo", new Instruction()
                    .S("type", "R")
                    .S("sa", "0")
                    .S("inst", "div")
                    .S("codop", "000000")
                    .S("func", "010010")
                    .S("format", "R5"));

            put("slt", new Instruction()
                    .S("type", "R")
                    .S("sa", "0")
                    .S("inst", "div")
                    .S("codop", "000000")
                    .S("func", "101010")
                    .S("format", "R0"));

            put("sra", new Instruction()
                    .S("type", "R")
                    .S("sa", "0")
                    .S("inst", "sll")
                    .S("codop", "000000")
                    .S("func", "000011")
                    .S("rs", "zero")
                    .S("rt", "zero")
                    .S("rd", "zero")
                    .S("offset", "0")
                    .S("format", "R4"));

            put("sll", new Instruction()
                    .S("type", "R")
                    .S("sa", "0")
                    .S("inst", "sll")
                    .S("codop", "000000")
                    .S("func", "000000")
                    .S("rs", "zero")
                    .S("rt", "zero")
                    .S("rd", "zero")
                    .S("offset", "0")
                    .S("format", "R4"));

            put("srl", new Instruction()
                    .S("type", "R")
                    .S("sa", "0")
                    .S("inst", "srl")
                    .S("codop", "000000")
                    .S("func", "000010")
                    .S("format", "R4"));

            put("halt", new Instruction()
                    .S("inst", "halt")
                    .S("codop", "000000")
                    .S("func", "000100"));

            put("jr", new Instruction()
                    .S("type", "R")
                    .S("sa", "0")
                    .S("inst", "jr")
                    .S("codop", "000000")
                    .S("func", "001000")
                    .S("format", "R2"));

            put("j", new Instruction()
                    .S("type", "J")
                    .S("inst", "j")
                    .S("codop", "000010")
                    .S("func", "000011")
                    .S("format", "J"));

            put("jal", new Instruction()
                    .S("type", "J")
                    .S("inst", "jal")
                    .S("codop", "000011")
                    .S("func", "000011")
                    .S("format", "J"));

            put("slti", new Instruction()
                    .S("type", "I")
                    .S("inst", "slti")
                    .S("codop", "001010")
                    .S("format", "I0"));

            put("sltiu", new Instruction()
                    .S("type", "I")
                    .S("inst", "sltiu")
                    .S("codop", "001011")
                    .S("format", "I0"));

            put("beq", new Instruction()//r = S
                    .S("type", "I")
                    .S("inst", "beq")
                    .S("codop", "000100")
                    .S("format", "I0"));

            put("bne", new Instruction()//r != S
                    .S("type", "I")
                    .S("inst", "bne")
                    .S("codop", "000101")
                    .S("format", "I0"));

            put("blez", new Instruction()//r<=0
                    .S("type", "I")
                    .S("inst", "blez")
                    .S("codop", "000110")
                    .S("format", "I0"));

            put("bgtz", new Instruction()//r>0
                    .S("type", "I")
                    .S("inst", "bgtz")
                    .S("codop", "000111")
                    .S("format", "I0"));

            put("bgez", new Instruction()//r >= 0
                    .S("type", "I")
                    .S("inst", "bgez")
                    .S("codop", "000001")
                    .S("format", "I0"));

            put("bltz", new Instruction()//r<0
                    .S("type", "I")
                    .S("inst", "bltz")
                    .S("codop", "000001")
                    .S("format", "I0"));

            put("and", new Instruction()
                    .S("type", "R").S("sa", "0")
                    .S("inst", "and")
                    .S("codop", "000000")
                    .S("func", "100100")
                    .S("format", "R0"));

            put("andi", new Instruction() //r<0
                    .S("type", "I")
                    .S("inst", "andi")
                    .S("codop", "001100")
                    .S("format", "I0"));

            put("ori", new Instruction() //r<0
                    .S("type", "I")
                    .S("inst", "bltz")
                    .S("codop", "001100")
                    .S("format", "I1"));

            put("lw", new Instruction()
                    .S("type", "I")
                    .S("inst", "bltz")
                    .S("codop", "001100")
                    .S("format", "I0"));

            put("sw", new Instruction()
                    .S("type", "I")
                    .S("inst", "sw")
                    .S("codop", "101011")
                    .S("format", "I0"));

            put("lui", new Instruction()
                    .S("type", "I")
                    .S("inst", "lui")
                    .S("codop", "001111")
                    .S("format", "I0"));

            put("lb", new Instruction()
                    .S("type", "I")
                    .S("inst", "lb")
                    .S("codop", "010011")
                    .S("format", "I2"));

            put("lbu", new Instruction()
                    .S("type", "I")
                    .S("inst", "lbu")
                    .S("codop", "100100")
                    .S("format", "I2"));

            put("sltiu", new Instruction()
                    .S("type", "I")
                    .S("inst", "sltiu")
                    .S("codop", "001101")
                    .S("format", "I2"));

            put("sleu", new Instruction()
                    .S("type", "R").S("sa", "0")
                    .S("inst", "sleu")
                    .S("codop", "000000")
                    .S("func", "101011")
                    .S("format", "R0"));

            put("sgtu", new Instruction()
                    .S("type", "R").S("sa", "0")
                    .S("inst", "sgtu")
                    .S("codop", "000000")
                    .S("func", "101100")
                    .S("format", "R0"));

            put("sgeu", new Instruction()
                    .S("type", "R").S("sa", "0")
                    .S("inst", "sgeu")
                    .S("codop", "000000")
                    .S("func", "101101")
                    .S("format", "R0"));

            put("seq", new Instruction()
                    .S("type", "R").S("sa", "0")
                    .S("inst", "seq")
                    .S("codop", "000000")
                    .S("func", "101110")
                    .S("format", "R0"));
            put("sne", new Instruction()
                    .S("type", "R").S("sa", "0")
                    .S("inst", "sne")
                    .S("codop", "000000")
                    .S("func", "101111")
                    .S("format", "R0"));
            put("sltu", new Instruction()
                    .S("type", "R").S("sa", "0")
                    .S("inst", "sltu")
                    .S("codop", "000000")
                    .S("func", "001101")
                    .S("format", "R0"));
        }
    };

    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);
    }

    public static String InstructionByOperator(Instruction inst) throws Exception {
        Instruction r = null;
        String sentence = inst.G("type"), type = "std";
        String op = inst.G("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.G(type);
        return r.G(type);
    }

    public static String Register(String reg) {
        return registers.get(reg);
    }
}
