1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- /*
- * 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 targets.mips;
- import API.CodeProcessorInterface;
- import common.Block;
- import common.Code;
- import common.Instruction;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.LinkedHashMap;
- import java.util.Map;
- /**
- *
- * @author EUGENIO CARVALHO
- */
- public class MipsUpdateAddressProcessor implements CodeProcessorInterface {
- protected HashMap<String, Boolean> updateOffsetShift;/*
-
- public MipsUpdateAddressProcessor() {
- }
- (parseInt("2c",10) * 4).toString(16)
- */
- public MipsUpdateAddressProcessor() {
- this.updateOffsetShift = new HashMap<String, Boolean>() {
- {
- put("beq", true);
- put("bne", true);
- put("blez", true);
- put("bltz", true);
- put("bgez", true);
- put("bgtz", true);
- }
- };
- }
- @Override
- public void Exec(Code c, LinkedHashMap<String, CodeProcessorInterface> cp) throws Exception {
- Integer offset;
- for (Map.Entry<String, Block> x : c.getBlocks().entrySet()) {
- for (Instruction inst : x.getValue().Instructions()) {
- // atualiza o indice da instrução de pc + 4
- if (inst.isNumber("global.position")) {
- inst.Set("block.position", inst.getInt("block.position") * 4);
- inst.Set("global.position", inst.getInt("global.position") * 4);
- } else if (inst.eq("type", "label")) {
- // atualiza o endereco dos labels
- inst.Set("reference.position", inst.getInt("reference.position") * 4);
- inst.Set("global.reference.position", inst.getInt("global.reference.position") * 4);
- }
- // Atualziza o offset das instruições de branch
- if (updateOffsetShift.containsKey(inst.Get("inst"))) {
- // System.out.println("UPDATE ADDRESS: " + inst.Get("label") + " c.labels:" + c.labels);
- offset = Integer.parseInt(c.labels.get(inst.Get("label")).get(0)) * 4;
- // System.out.println("Offset:" + offset + "--" + y.getInt("global.position"));
- offset = ((offset - inst.getInt("global.position")) / 4) - 1;
- // System.out.println("OffsetCalculado:" + offset);
- inst.Set("offset", offset);
- }
- }
- }
- // System.out.println("Labels:" + c.labels);
- String[] parts;
- int index;
- // Update address of labels
- for (Map.Entry<String, ArrayList<String>> entry : c.labels.entrySet()) {
- ArrayList<String> labeldata = entry.getValue();
- labeldata.set(0, (Integer.parseInt(labeldata.get(0)) * 4) + "");
- try {
- parts = labeldata.get(1).split("(\\+)");
- if (parts.length > 1) {
- index = Integer.parseInt(parts[1]);
- labeldata.set(1, parts[0] + "+" + (index * 4));
- }
- } catch (Exception e) {
- }
- }
- }
- }
|