/* * 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 common; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.Map; public class Block { public static int uniq = 0; public LinkedList instructions = new LinkedList<>(); protected String name; protected Code code; protected HashMap labels = new HashMap<>(); protected HashMap dependences = new HashMap<>(); protected DataLayout Data; public int BaseAddress = 0; public int CurrentAddress = 0; public boolean HasCall; public int nonLabelCount; Block(String name, Code c) { // System.out.println("Open block:" + name + ":" + c.PosicaoLabel); code = c; BaseAddress = c.Position(); // BaseAddress = c.PosicaoLabel; this.name = name; Data = new DataLayout(name, code.GlobalData()); // labels = new HashMap<>(); // Data = new LinkedHashMap<>(); // constants = new LinkedHashMap<>(); // reverseName = new HashMap<>(); // pointers = new HashMap<>(); // ocorrencesPerBlocks = new LinkedHashMap<>(); } public LinkedList Instructions() { return this.instructions; } public boolean HasCall() { return HasCall; } public Block Add(Instruction nr) { int pos = CurrentAddress; if (!nr.eq("type", "label")) { // nr.Set("reference.position", pos); // } else { nr.Set("block.position", pos); nr.Set("block.position.origin", pos); CurrentAddress++; } else { } // System.out.println("Add:" + nr); this.instructions.add(nr); return this; } public Block Remove(Instruction instruction) throws Exception { // System.out.println("Remove indtruction do block:" + instruction); this.instructions.remove(instruction); return this; } public String getName() { return this.name; } /** * Retorna o grupo de variaveiS de eScopo locaiS * * @return */ public DataLayout Data() { return this.Data; } /** * Retorna o grupo de variaveiS de eScopo global * * @return */ public DataLayout GData() { return this.code.GlobalData(); } public DataLayout Data(DataLayout d) { return this.Data.copy(d); } public void Close() throws Exception { Update(); } public void Update() throws Exception { CurrentAddress = 0; for (Instruction n : instructions) { // System.out.println("Update->:" + CurrentAddress + ":" + code.Position()); if (!n.eq("type", "label")) { n.Set("block.position", CurrentAddress) .Set("block.position.origin", CurrentAddress++) .Set("global.position", code.PositionInc()); } else { n.Set("reference.position", CurrentAddress) .Set("global.reference.position", code.Position()); code.RegisterLabelAddress(n.Get("label")); } } // System.out.println("}"); } public int LastAddress() { return Instructions().size() - 1; } public Instruction RemoveLast() { // System.out.println("Remove indtruction do block:"); return instructions.removeLast(); } public Block ReplaceVar(String find, String replace) { // System.out.println("Replace " + find + " -> " + replace); for (Instruction inst : instructions) { for (String p : new String[]{"dst", "p1", "p2"}) { if (inst.eq(p, find)) { inst.Set(p, replace); } } } return this; } public int CountInstructions() { return nonLabelCount; } public void AddDependence(String id) { dependences.put(id, true); } public ArrayList GetDependences() { return new ArrayList() { { for (Map.Entry entry : dependences.entrySet()) { add(entry.getKey()); } } }; } }