OpenASIP 2.2
Loading...
Searching...
No Matches
CallsToJumps.cc
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2014 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#include "CallsToJumps.hh"
26#include "ControlFlowGraph.hh"
27#include "Program.hh"
28#include "Procedure.hh"
29#include "BasicBlock.hh"
30#include "Instruction.hh"
31#include "Move.hh"
36#include "TerminalFUPort.hh"
37#include "Terminal.hh"
38#include "CodeGenerator.hh"
39#include "UniversalMachine.hh"
40#include "MoveGuard.hh"
41#include "ControlUnit.hh"
43
44using namespace TTAProgram;
45
46//#define DEBUG_CALLS_TO_JUMPS
47
48void
50 ControlFlowGraph& cfg, const TTAMachine::Machine& targetMachine) {
53 int nodeCount = cfg.nodeCount();
54 for (int bbIndex = 0; bbIndex < nodeCount; ++bbIndex) {
55 BasicBlockNode& bbn = dynamic_cast<BasicBlockNode&>(cfg.node(bbIndex));
56 if (!bbn.isNormalBB())
57 continue;
58
59 assert(!bbn.isScheduled());
60
61 BasicBlock& bb = bbn.basicBlock();
62
63 // scan through the instructions, looking for CALLs
64 // replace each call with
65 // 1) a move to RA with an instruction reference to the instruction
66 // following the call (the next BB), and
67 // 2) a JUMP to the original CALL destination
68 // 3) some kind of annotation that marks that this JUMP should
69 // be treated like a function call, e.g., in the DDG builder
70 for (int i = 0, count = bb.instructionCount(); i < count; ++i) {
71 Instruction& instr = bb.instructionAtIndex(i);
72 if (instr.hasCall()) {
73
74 // If unscheduled it should have exactly one move, the call.
75 assert(instr.moveCount() == 1);
76
77 // In the TCE's CFG, a call splits the basic block, so this
78 // should be the last instruction as there are no delay slots
79 // in the intermediate representation.
80 assert(i + 1 == count);
81
82
83 Move& originalCall = instr.move(0);
84
85 BasicBlockNode* nextBB = cfg.fallThruSuccessor(bbn);
86 assert(nextBB != NULL);
87
88#ifdef DEBUG_CALLS_TO_JUMPS
90 << "### found call: " << originalCall.toString()
91 << std::endl;
93 << "### BB: " << bb.toString() << std::endl;
95 << "### FallThroughBB: " << nextBB->basicBlock().toString()
96 << std::endl;
97#endif
98
99 // E.g. call to exit does not return and there is no sensible
100 // place to return. Just do not add the RA write in that case.
101 Instruction* returnLocation =
102 (nextBB->basicBlock().instructionCount() > 0) ?
103 &nextBB->basicBlock().firstInstruction() : NULL;
104
105 CodeGenerator codeGen(targetMachine);
106
108
109 std::shared_ptr<Move> returnAddressMove = NULL;
110 std::shared_ptr<Move> jumpToFunction;
111
112 if (originalCall.isUnconditional()) {
113
114 if (returnLocation != NULL) {
115 InstructionReference returnRef =
116 irm.createReference(*returnLocation);
117
118 returnAddressMove = std::make_shared<Move>(
119 new TerminalInstructionReference(returnRef),
121 *uMach.controlUnit()->returnAddressPort()),
122 uMach.universalBus());
123 }
124
125 jumpToFunction = std::make_shared<Move>(
126 originalCall.source().copy(),
127 codeGen.createTerminalFUPort("jump", 1),
128 uMach.universalBus());
129 } else {
130
131 if (returnLocation != NULL) {
132 InstructionReference returnRef =
133 irm.createReference(*returnLocation);
134
135 returnAddressMove = std::make_shared<Move>(
136 new TerminalInstructionReference(returnRef),
138 *uMach.controlUnit()->returnAddressPort()),
139 uMach.universalBus(),
140 originalCall.guard().copy());
141 }
142
143 jumpToFunction = std::make_shared<Move>(
144 originalCall.source().copy(),
145 codeGen.createTerminalFUPort("jump", 1),
146 uMach.universalBus(),
147 originalCall.guard().copy());
148 }
149
150 jumpToFunction->addAnnotation(
152
153 if (returnAddressMove != NULL) {
154 Instruction* raMoveInstr =
156 raMoveInstr->addMove(returnAddressMove);
157 bb.insertBefore(instr, raMoveInstr);
158
159 if (irm.hasReference(instr))
160 irm.replace(instr, *raMoveInstr);
161 }
162
163 Instruction* jumpInstr =
165 jumpInstr->addMove(jumpToFunction);
166
167 bb.insertBefore(instr, jumpInstr);
168
169 if (returnAddressMove == NULL && irm.hasReference(instr))
170 irm.replace(instr, *jumpInstr);
171
172 // Keep the instruction references up to date as
173 // the old call can be a jump target (so should be the
174 // new RA-move) if it starts a basic block.
175
176#ifdef DEBUG_CALLS_TO_JUMPS
178 << "### removing " << instr.toString() << std::endl;
179#endif
180 assert(!irm.hasReference(instr));
181 bb.remove(instr);
182 }
183 }
184 }
185}
#define assert(condition)
static std::ostream & logStream()
TTAProgram::BasicBlock & basicBlock()
bool isNormalBB() const
bool isScheduled() const
int nodeCount() const
Node & node(const int index) const
virtual void handleControlFlowGraph(ControlFlowGraph &cfg, const TTAMachine::Machine &targetMachine)
BasicBlockNode * fallThruSuccessor(const BasicBlockNode &bbn) const
TTAProgram::InstructionReferenceManager & instructionReferenceManager()
SpecialRegisterPort * returnAddressPort() const
virtual ControlUnit * controlUnit() const
Definition Machine.cc:345
static NullInstructionTemplate & instance()
TTAProgram::TerminalFUPort * createTerminalFUPort(const TCEString &opName, int operand)
virtual Instruction & firstInstruction() const
virtual std::string toString() const
virtual int instructionCount() const
virtual void remove(Instruction &ins)
virtual void insertBefore(const Instruction &pos, Instruction *ins)
virtual Instruction & instructionAtIndex(int index) const
void replace(Instruction &insA, Instruction &insB)
InstructionReference createReference(Instruction &ins)
std::string toString() const
Move & move(int i) const
void addMove(std::shared_ptr< Move > move)
MoveGuard * copy() const
Definition MoveGuard.cc:96
MoveGuard & guard() const
Definition Move.cc:345
bool isUnconditional() const
Definition Move.cc:154
std::string toString() const
Definition Move.cc:436
Terminal & source() const
Definition Move.cc:302
@ ANN_JUMP_FUNCTION_CALL
The JUMP in the annotated move is a function call and should be treated as such in the data dependenc...
virtual Terminal * copy() const =0
static UniversalMachine & instance()
TTAMachine::Bus & universalBus() const