/* * 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; import API.Instruction; import IntermediaryCode.Block; import IntermediaryCode.Code; import IntermediaryCode.CodeProcessing; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; /** * * @author EUGENIO CARVALHO */ class UpdateAddressProcessor implements CodeProcessing { protected HashMap updateOffsetShift;/* public UpdateAddressProcessor() { } (parseInt("2c",10) * 4).toString(16) */ UpdateAddressProcessor() { this.updateOffsetShift = new HashMap() { { 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 cp) throws Exception { Integer offset; for (Map.Entry x : c.getBlocks().entrySet()) { for (Instruction y : x.getValue().Instructions()) { // atualiza o indice da instrução de pc + 4 if (y.isNumber("global.position")) { y.S("block.position", y.getInt("block.position") * 4); y.S("global.position", y.getInt("global.position") * 4); } else if (y.eq("type", "label")) { // atualiza o endereco dos labels y.S("reference.position", y.getInt("reference.position") * 4); y.S("global.reference.position", y.getInt("global.reference.position") * 4); } // Atualziza o offset das instruições de branch if (updateOffsetShift.containsKey(y.G("inst"))) { // System.out.println("UPDATE ADDRESS:" + y + c.labels); offset = Integer.parseInt(c.labels.get(y.G("label")).get(0)) * 4; // System.out.println("Offset:" + offset + "--" + y.getInt("global.position")); offset = ((offset - y.getInt("global.position")) / 4) - 1; // System.out.println("OffsetCalculado:" + offset); y.S("offset", offset); } } } // System.out.println("Labels:" + c.labels); String[] parts; int index; // Update address of labels for (Map.Entry> entry : c.labels.entrySet()) { ArrayList 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) { } } } }