123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- /*
- * 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.jun;
- import API.BuildParams;
- import API.Instruction;
- import API.Utils;
- import IntermediaryCode.Block;
- import IntermediaryCode.Code;
- import compiler.IvannosysTargetArch;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.LinkedList;
- import java.util.Map;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import org.json.simple.JSONArray;
- import org.json.simple.JSONObject;
- import org.json.simple.parser.JSONParser;
- import target.mips.Mips;
- import target.mips.Registers;
- import target.mips.TemplateMips;
- import target.mips.UpdateAddressProcessor;
- /**
- * MipS tranSlation
- *
- * @author EUGENIO CARVALHO
- */
- public class Gen extends target.mips.Gen {
- public Gen() {
- super();
- Init();
- }
- @Override
- public IvannosysTargetArch Export() {
- try {
- Code Target = getTarget(), newCode;
- System.out.println("Export do juninho");
- Long stackBaseAddress = 0L,
- updateBaseAddress = 0L,
- size;
- String id,
- initFunction,
- blockName,
- format,
- cacheBlockSize = BuildParams.Get("cacheBlockSize").get(0),
- // base = "\\src\\results\\mipsjun\\";
- base = BuildParams.Get("outputDirectory").get(0);
- String[] parts;
- Block func;
- JSONObject profile;
- UpdateAddressProcessor uap = new UpdateAddressProcessor();
- ArrayList<String> p = BuildParams.Get("profile");
- if (!p.isEmpty()) {
- JSONArray pobject = (JSONArray) new JSONParser().parse(p.get(0));
- for (Object pro : pobject) {
- profile = (JSONObject) pro;
- id = (String) profile.get("id");
- initFunction = (String) profile.get("initFunction");
- parts = initFunction.replace(")", "").split("\\(");
- blockName = parts[0];
- // Busca o bloco inicial referenciado pelo usuario
- func = Target.getBlocks().get(blockName);
- if (profile.containsKey("stackBaseAddress")) {
- stackBaseAddress = (Long) profile.get("stackBaseAddress");
- } else {
- // System.out.println("stackBaseAddress:(" + stackBaseAddress + ")(" + MaxStackSize(func) + ")");
- size = MaxStackSize(func);
- updateBaseAddress = stackBaseAddress
- - size
- - fixCacheBlockSize(size, Long.parseLong(cacheBlockSize));
- }
- // Cria um novo codigo para armazenas as instruções do core
- newCode = NewCode(id);
- // Copia o bloco da funcao e todos os blocos que gerem dependencia
- CopyBlock(newCode,
- func,
- parts,
- stackBaseAddress,
- true);
- // Atualiza os enderecos de salto do codigo
- uap.Exec(newCode, null);
- format = (String) profile.get("filename");
- // Utils.WriteFile("\\src\\results\\mipsjun\\" + id + "\\main.dec.txt", code);
- SaveMips(newCode, base + "\\" + String.format(format, "mips"));
- SaveDec(newCode, base + "\\" + String.format(format, "dec"));
- stackBaseAddress = updateBaseAddress;
- // System.out.println("value::" + code);
- }
- // System.out.println("params:" + getTac().ast.getCompileParams().GetLists());
- // System.out.println("params:" + p);
- }
- // } catch (ParseException ex) {
- // Logger.getLogger(Gen.class.getName()).log(Level.SEVERE, null, ex);
- } catch (Exception ex) {
- Logger.getLogger(Gen.class.getName()).log(Level.SEVERE, null, ex);
- }
- return this;
- // return super.Export(); //To change body of generated methods, choose Tools | Templates.
- }
- protected Long fixCacheBlockSize(Long size, Long sizeOfBlock) {
- Long t = sizeOfBlock;
- while (true) {
- if (sizeOfBlock > size) {
- break;
- }
- sizeOfBlock += t;
- }
- return sizeOfBlock - size;
- }
- protected void SaveMips(Code newCode, String filename) throws Exception {
- Utils.WriteFile(filename, newCode.getCodeStream());
- }
- protected void SaveDec(Code newCode, String filename) throws Exception {
- String code = "", tmp;
- for (Map.Entry<String, Block> x : newCode.stmts.entrySet()) {
- for (Instruction instr : x.getValue().Instructions()) {
- tmp = instr.G("inst.dec");
- if (tmp.equals("")) {
- continue;
- }
- code = code.concat(Long.parseLong(tmp) + "\n");
- }
- }
- Utils.WriteFile(filename, code);
- }
- protected Code CopyBlock(Code newCode, Block func, String[] funcCall, Long stackBaseAddress, boolean main) throws Exception {
- // Instruction Ninst;
- int index = 1;
- String blockName = funcCall[0];
- newCode.OpenBlock(blockName);
- Block block = newCode.Block();
- // Carrega os argumentos da chamada
- if (funcCall.length > 1) {
- // System.out.println("Tinha parametro:" + funcCall[1]);
- String[] args = funcCall[1].split(",");
- int i = 0;
- for (String arg : args) {
- // Ninst = new Instruction();
- // Variaveis.List();
- // System.out.println("Variaveis.Get(arg):" + Variaveis.get("main." + args));
- block.Add(Mips.Instruction("addiu")
- .S("rs", Registers.R_ZERO)
- .S("rt", "a" + (i++))
- .S("offset", arg));
- }
- index += args.length;
- }
- for (Instruction inst : func.Instructions()) {
- block.Add(inst.copy());
- }
- if (main) {
- LinkedList<Instruction> instructions = newCode.Block().Instructions();
- // Atualiza o offset da pilha
- Instruction first = instructions.get(index);
- // System.out.println("CopyBlock[" + stackBaseAddress + "||" + first.getLong("offset") + "]");
- first.set("offset", first.getLong("offset") + stackBaseAddress);
- // System.out.println("instructions.get(0);" + first);
- // Atualiza a ultima instrucao de um retorno para ra -> stop
- block.Remove(instructions.get(instructions.size() - 1));
- block.Add(Mips.Instruction("stop")
- .S("comment", "end of programa"));
- }
- newCode.CloseBlock();
- // Adiciona as funcoes que geram dependencias
- HashMap<String, Block> tb = getTarget().getBlocks();
- ArrayList<String> deps = tb.get(blockName).GetDependences();
- if (!deps.isEmpty()) {
- for (String dep : deps) {
- CopyBlock(newCode, tb.get(dep), new String[]{dep}, 0l, false);
- }
- }
- return newCode;
- }
- protected Code NewCode(String id) {
- return new Code(id)
- .Template(new TemplateMips())
- .Formats(formatMips);
- }
- protected Long MaxStackSize(Block blk) {
- HashMap<String, Block> tb = getTarget().getBlocks();
- int size = blk.Data().Size();
- // System.out.println("data:" + blk.Data());
- Long childSize = 0L, tmpChildSize;
- ArrayList<String> deps = blk.GetDependences();
- if (!deps.isEmpty()) {
- for (String dep : deps) {
- tmpChildSize = MaxStackSize(tb.get(dep));
- if (tmpChildSize > childSize) {
- childSize = tmpChildSize;
- }
- }
- }
- // System.out.println("MaxStackSize:" + size + "|" + childSize);
- return size + childSize;
- }
- }
|