123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- /*
- * 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<Instruction> instructions = new LinkedList<>();
- protected String name;
- protected Code code;
- protected HashMap<String, Instruction> labels = new HashMap<>();
- protected HashMap<String, Boolean> 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<Instruction> 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<String> GetDependences() {
- return new ArrayList<String>() {
- {
- for (Map.Entry<String, Boolean> entry : dependences.entrySet()) {
- add(entry.getKey());
- }
- }
- };
- }
- }
|