1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- /*
- * 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 IntermediaryCode;
- import API.Utils;
- import common.Block;
- import common.Code;
- import common.Instruction;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.LinkedHashMap;
- import java.util.LinkedList;
- /**
- *
- * @author EUGENIO CARVALHO
- */
- class RemoveUnusedLabelsProcessor implements CodeProcessing {
- public RemoveUnusedLabelsProcessor() {
- }
- @Override
- public void Exec(Code c, LinkedHashMap<String, CodeProcessing> cp) throws Exception {
- String label;
- Instruction instruction = null;
- Block block = c.Block();
- HashMap<String, Integer> rc = c.labelsReferenceCount;
- LinkedList<Instruction> instructions = block.Instructions();
- ArrayList<Instruction> remover = new ArrayList();
- Iterator<Instruction> interator = instructions.iterator();
- String[] ignore = new String[]{"block", "user"};
- Instruction branch = null, last;
- int position = 0;
- System.out.println("block:" + block.getName() + "-------------------------------");
- // Remove todos os labels não referenciados
- while (interator.hasNext()) {
- position++;
- last = instruction;
- instruction = interator.next();
- // System.out.println("instruction:" + instruction);
- if (instruction.eq("type", "branch")) {
- branch = instruction;
- position = 0;
- }
- if (position == 3 && branch != null && last.eq("type", "jump")) {
- System.out.println(":" + branch + last);
- branch.S("op", Utils.ComplementOperation(branch.G("op")))
- .S("label", last.G("label"));
- branch = null;
- // instructions.remove(last);
- // interator.remove();
- // interator.remove(last);
- remover.add(last);
- remover.add(instruction);
-
- // System.out.println("'ERA UMA LABEL:" + instruction);
- continue;
- }
- // if (position == 1 // && instruction.eq("type", "label") // && (last != null && last.eq("type", "jump"))
- // ) {
- //
- ////
- //// instructions.remove(last);
- //// instructions.remove(instruction);
- ////
- //// continue;
- // }
- if (!instruction.eq("type", "label") || instruction.in("label_type", ignore)) {
- continue;
- }
- label = instruction.G("label");
- if (!rc.containsKey(label) || rc.get(label) == 0) {
- interator.remove();
- }
- }
- // System.out.println("Remover:" + remover);
- for (Instruction r : remover) {
- instructions.remove(r);
- }
- }
- }
|