OpenASIP 2.2
Loading...
Searching...
No Matches
AbsoluteToRelativeJumps.cc
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2019 Tampere University.
3
4 This file is part of TTA-Based Codesign Environment (TCE).
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24/**
25 * @file AbsoloteToRelativeJumps.cc
26 *
27 * Definition of AbsoloteToRelativeJumps class.
28 *
29 * @author Heikki Kultala 2019 (heikki.kultala-no.spam-tuni.fi)
30 * @note rating: red
31 */
32
34#include "Procedure.hh"
35#include "Instruction.hh"
36#include "Move.hh"
37#include "Terminal.hh"
38#include "Machine.hh"
39#include "ControlUnit.hh"
40#include "Operation.hh"
41#include "TerminalImmediate.hh"
42#include "TerminalFUPort.hh"
43#include "Immediate.hh"
45#include "Program.hh"
46
47namespace TTAMachine {
48 class HWOperation;
49}
50
51
53 TTAProgram::Procedure& procedure,
54 const TTAMachine::Machine& targetMachine) {
55
56 std::map<TTAProgram::Move*, int> jumpOffsetMoves;
57 std::map<std::shared_ptr<TTAProgram::Immediate>, int> jumpOffsetLimms;
58
59 auto cu = targetMachine.controlUnit();
60 if (!cu->hasOperation("reljump")) {
61 return;
62 }
63
64 auto& irm = procedure.parent().instructionReferenceManager();
65 auto hwop = cu->operation("reljump");
66
67 for (int i = 0; i < procedure.instructionCount(); i++) {
68 auto& ins = procedure[i];
69 for (int j = 0; j < ins.moveCount(); j++) {
70 auto& m = ins.move(j);
71 auto& src = m.source();
72 auto& dst = m.destination();
73 if (!dst.isFUPort()) {
74 continue;
75 }
76 auto& dfu = dst.functionUnit();
77 if (&dfu != targetMachine.controlUnit()) {
78 continue;
79 }
80 if (!dst.isOpcodeSetting()) {
81 continue;
82 }
83 if (dst.operation().name() != "JUMP") {
84 continue;
85 }
86
87 int jumpAddr = ins.address().location();
88
89 if (src.isInstructionAddress()) {
90 int dstAddr = src.value().intValue();
91 int diff = dstAddr - jumpAddr;
92
93 auto& bus = m.bus();
94 if (!bus.signExtends() && diff < 0 && bus.immediateWidth() < 32) {
95 continue;
96 }
97
98 jumpOffsetMoves[&m] = diff;
99 m.setDestination(new TTAProgram::TerminalFUPort(*hwop, 1));
100 continue;
101 }
102
103 if (src.isImmediateRegister()) {
104 auto& immu = src.immediateUnit();
105 int index = static_cast<int>(src.index());
106 bool found = false;
107 for (int k = i - immu.latency(); k >= 0 && !found; k--) {
108 auto &immIns = procedure.instructionAtIndex(k);
109
110 for (int l = 0; l < immIns.immediateCount(); l++) {
111 auto imm = immIns.immediatePtr(l);
112 if (imm->destination().index() == index &&
113 &imm->destination().immediateUnit() == &immu) {
114 int dstAddr = imm->value().value().intValue();
115 int diff = dstAddr - jumpAddr;
116
117 if (!immu.signExtends() && diff < 0 && immu.width() < 32) {
118 found = true;
119 break;
120 }
121
122 jumpOffsetLimms[imm] = diff;
123 m.setDestination(new TTAProgram::TerminalFUPort(*hwop, 1));
124 found = true;
125 break;
126 }
127 }
128 if (irm.hasReference(immIns)) {
129 break;
130 }
131 }
132 }
133 }
134 }
135
136 // Only update the indeces at the end to keep instr references valid.
137 for (auto i : jumpOffsetMoves) {
138 i.first->setSource(new TTAProgram::TerminalImmediate(SimValue(i.second, 32)));
139 }
140
141 for (auto i : jumpOffsetLimms) {
142 i.first->setValue(new TTAProgram::TerminalImmediate(SimValue(i.second, 32)));
143 }
144}
virtual void handleProcedure(TTAProgram::Procedure &procedure, const TTAMachine::Machine &targetMachine) override
virtual ControlUnit * controlUnit() const
Definition Machine.cc:345
InstructionAddress location() const
virtual int instructionCount() const
virtual Program & parent() const
virtual Instruction & instructionAtIndex(int index) const
std::shared_ptr< Immediate > immediatePtr(int i) const
Address address(const Instruction &ins) const
Definition Procedure.cc:101
InstructionReferenceManager & instructionReferenceManager() const
Definition Program.cc:688