OpenASIP  2.0
Public Member Functions | Protected Types | Protected Attributes | List of all members
TTAProgram::CodeSnippet Class Reference

#include <CodeSnippet.hh>

Inheritance diagram for TTAProgram::CodeSnippet:
Inheritance graph
Collaboration diagram for TTAProgram::CodeSnippet:
Collaboration graph

Public Member Functions

 CodeSnippet ()
 
 CodeSnippet (const TTAProgram::Address &start)
 
virtual ~CodeSnippet ()
 
virtual void clear ()
 
virtual void removeLastInstruction ()
 
virtual int instructionCount () const
 
virtual Programparent () const
 
virtual void setParent (Program &prog)
 
virtual bool isInProgram () const
 
virtual Address address (const Instruction &ins) const
 
virtual Address startAddress () const
 
virtual void setStartAddress (Address start)
 
virtual Address endAddress () const
 
virtual void setEndAddress (Address end)
 
virtual InstructionfirstInstruction () const
 
virtual InstructioninstructionAt (UIntWord address) const
 
virtual InstructioninstructionAtIndex (int index) const
 
virtual Instructionoperator[] (size_t index) const
 
virtual bool hasNextInstruction (const Instruction &ins) const
 
virtual InstructionnextInstruction (const Instruction &ins) const
 
virtual InstructionpreviousInstruction (const Instruction &ins) const
 
virtual InstructionlastInstruction () const
 
virtual void addFront (Instruction *ins)
 
virtual void add (Instruction *ins)
 
virtual void insertAfter (const Instruction &pos, Instruction *ins)
 
virtual void insertBefore (const Instruction &pos, Instruction *ins)
 
virtual void remove (Instruction &ins)
 
virtual void deleteInstructionAt (InstructionAddress address)
 
virtual CodeSnippetcopy () const
 
virtual void prepend (const CodeSnippet &cs)
 
virtual void prepend (CodeSnippet *cs)
 
virtual void append (const CodeSnippet &cs)
 
virtual void append (CodeSnippet *cs)
 
virtual void insertBefore (const Instruction &pos, const CodeSnippet &cs)
 
virtual void insertBefore (const Instruction &pos, CodeSnippet *cs)
 
virtual void insertAfter (const Instruction &pos, const CodeSnippet &cs)
 
virtual void insertAfter (const Instruction &pos, CodeSnippet *cs)
 
bool hasReturn () const
 
virtual std::string disassembly () const
 
virtual std::string toString () const
 

Protected Types

typedef std::vector< Instruction * > InsList
 List of instructions. More...
 
typedef InsList::iterator InsIter
 Iterator for the instruction list. More...
 

Protected Attributes

InsList instructions_
 The instructions in this procedure. More...
 
Programparent_
 The parent program of the procedure. More...
 
Address startAddr_
 The start (lowest) address of the procedure. More...
 
Address endAddr_
 The highest address of the procedure. More...
 

Detailed Description

A code snippet is an ordered sequence of adjacent instructions.

It is a helper class for representing pieces of code that are not necessarily full procedures, for example basic blocks. Code snippet doesn't care, whether the sequence of instructions in it makes sense or not. That's the responsibility of the client that created the snippet.

Definition at line 59 of file CodeSnippet.hh.

Member Typedef Documentation

◆ InsIter

typedef InsList::iterator TTAProgram::CodeSnippet::InsIter
protected

Iterator for the instruction list.

Definition at line 123 of file CodeSnippet.hh.

◆ InsList

typedef std::vector<Instruction*> TTAProgram::CodeSnippet::InsList
protected

List of instructions.

Definition at line 121 of file CodeSnippet.hh.

Constructor & Destructor Documentation

◆ CodeSnippet() [1/2]

TTAProgram::CodeSnippet::CodeSnippet ( )

Constructor.

Definition at line 55 of file CodeSnippet.cc.

Referenced by copy().

◆ CodeSnippet() [2/2]

TTAProgram::CodeSnippet::CodeSnippet ( const TTAProgram::Address start)

Constructor.

Definition at line 65 of file CodeSnippet.cc.

65  :
66  parent_(NULL), startAddr_(start), endAddr_(start) {
67 }

◆ ~CodeSnippet()

TTAProgram::CodeSnippet::~CodeSnippet ( )
virtual

The destructor.

Definition at line 73 of file CodeSnippet.cc.

73  {
74  for (unsigned int i = 0; i < instructions_.size(); i++) {
75  if (instructions_.at(i) != &NullInstruction::instance()) {
76  delete instructions_.at(i);
77  }
78  }
79  instructions_.clear();
80 }

References TTAProgram::NullInstruction::instance(), and instructions_.

Here is the call graph for this function:

Member Function Documentation

◆ add()

void TTAProgram::CodeSnippet::add ( Instruction ins)
virtual

Adds an instruction as the last instruction in the code snippet.

The ownership of the instruction will be passed to the code snippet.

Parameters
insThe instruction to add.
Exceptions
IllegalRegistrationif the instruction is already registered in another code snippet.

Reimplemented in TTAProgram::Procedure.

Definition at line 432 of file CodeSnippet.cc.

432  {
433  if (!ins->isInProcedure()) {
434 
435  if (instructions_.size() == instructions_.capacity()) {
436  instructions_.reserve(instructions_.size() * 2);
437  }
438 
439  ins->setParent(*this);
440  instructions_.push_back(ins);
441 
442  } else {
443  throw IllegalRegistration(__FILE__, __LINE__, __func__,
444  "Instruction: " +
446  + " allready has parent:\n " +
447  ins->parent().disassembly());
448  }
449 }

References __func__, POMDisassembler::disassemble(), disassembly(), instructions_, TTAProgram::Instruction::isInProcedure(), TTAProgram::Instruction::parent(), and TTAProgram::Instruction::setParent().

Referenced by TTAProgram::CodeGenerator::addAnnotatedMoveToProcedure(), LoopPrologAndEpilogBuilder::addEpilogFromRM(), SimpleIfConverter::addJump(), TTAProgram::CodeGenerator::addMoveToProcedure(), append(), Peel2BBLoops::appendBB(), SimpleIfConverter::appendBB(), LoopPrologAndEpilogBuilder::build(), TTAProgram::BasicBlock::copy(), copy(), InlineAsmParser::copyInstructions(), BasicBlockPass::copyRMToBB(), SequentialScheduler::createBasicBlocks(), ControlFlowGraph::createBlock(), ProgramDependenceGraph::createJump(), llvm::LLVMTCEBuilder::emitComparisonForBranch(), llvm::LLVMTCEBuilder::emitGlobalXXtructorCalls(), llvm::LLVMTCEBuilder::emitInstruction(), llvm::LLVMTCEPOMBuilder::emitMove(), llvm::LLVMTCEBuilder::emitMove(), llvm::LLVMTCEBuilder::emitOperationMacro(), llvm::LLVMTCEBuilder::emitRemaingingBrach(), llvm::LLVMTCEBuilder::emitReturn(), llvm::LLVMTCEBuilder::emitSelect(), llvm::LLVMTCEBuilder::emitSetjmp(), llvm::LLVMTCEBuilder::emitSPInitialization(), insertAfter(), ControlFlowGraph::mergeNodes(), ProgramDependenceGraph::processLoopClose(), ProgramDependenceGraph::processPredicate(), ProgramDependenceGraph::processRegion(), ControlFlowGraph::splitBasicBlockAtIndex(), and ControlFlowGraph::splitBB().

Here is the call graph for this function:

◆ addFront()

void TTAProgram::CodeSnippet::addFront ( Instruction ins)
virtual

Adds an instruction as the first instruction in the code snippet.

The ownership of the instruction will be passed to the code snippet.

Parameters
insThe instruction to add.
Exceptions
IllegalRegistrationif the instruction is already registered in another code snippet.

Reimplemented in TTAProgram::Procedure.

Definition at line 406 of file CodeSnippet.cc.

406  {
407  if (!ins->isInProcedure()) {
408 
409  if (instructions_.size() == instructions_.capacity()) {
410  instructions_.reserve(instructions_.size() * 2);
411  }
412 
413  ins->setParent(*this);
414  instructions_.insert(instructions_.begin(), ins);
415 
416  } else {
417  throw IllegalRegistration(__FILE__, __LINE__);
418  }
419 }

References instructions_, TTAProgram::Instruction::isInProcedure(), and TTAProgram::Instruction::setParent().

Referenced by prepend().

Here is the call graph for this function:

◆ address()

Address TTAProgram::CodeSnippet::address ( const Instruction ins) const
virtual

Returns the address of the given instruction.

Returns
The address of the given instruction.
Exceptions
IllegalRegistrationif the instruction does not belong to this code snippet.

Reimplemented in TTAProgram::Procedure, and TTAProgram::NullProcedure.

Definition at line 163 of file CodeSnippet.cc.

163  {
164  if (ins.hasFinalAddress()) return ins.address();
165 
166  unsigned int i = 0;
167 
168  /* this loop is executed very often, so it
169  uses a pre-computed size and [] for performance reasons.
170  TO CLEANUP: why not use std::find()? */
171  const unsigned int size = instructions_.size();
172  while (i < size && instructions_[i] != &ins) {
173  i++;
174  }
175 
176  if (i != instructions_.size()) {
177  Address insAddress(
179  return insAddress;
180  } else {
181  throw IllegalRegistration(
182  __FILE__, __LINE__, __func__, "Instruction not in CodeSnippet");
183  }
184 }

References __func__, TTAProgram::Instruction::address(), TTAProgram::Instruction::hasFinalAddress(), instructions_, TTAProgram::Address::location(), TTAProgram::Address::space(), and startAddr_.

Referenced by TTAProgram::Instruction::address(), deleteInstructionAt(), and instructionAt().

Here is the call graph for this function:

◆ append() [1/2]

void TTAProgram::CodeSnippet::append ( CodeSnippet cs)
virtual

Inserts a whole code snippet at the end of this code snippet.

This function moves the ownership of instructions from the old code snippet into the new one, and deletes the given code snippet.

@TODO: This routine is O(n^2). Optimize!

Parameters
csCodeSnippet containing the instructions being inserted.

Definition at line 730 of file CodeSnippet.cc.

730  {
731  for (int i = 0, count = cs->instructionCount(); i < count ; i++) {
732  Instruction &ins = cs->instructionAtIndex(0);
733  cs->remove(ins);
734  add(&ins);
735  }
736  delete cs;
737 }

References add(), instructionAtIndex(), instructionCount(), and remove().

Here is the call graph for this function:

◆ append() [2/2]

void TTAProgram::CodeSnippet::append ( const CodeSnippet cs)
virtual

Inserts a whole code snippet at end of this code snippet.

This function does not remove the instruction references from the code snippet.

Parameters
csCodeSnippet containing the instructions being inserted.

Definition at line 711 of file CodeSnippet.cc.

712  {
713  for( int i = 0; i < cs.instructionCount(); i++ ) {
714  add(cs.instructionAtIndex(i).copy());
715  }
716  }

References add(), TTAProgram::Instruction::copy(), instructionAtIndex(), and instructionCount().

Referenced by llvm::LLVMTCEBuilder::emitSPInitialization().

Here is the call graph for this function:

◆ clear()

void TTAProgram::CodeSnippet::clear ( )
virtual

Clears the code snippet.

Removes all instruction references inside the snippet and deletes the instructions.

Reimplemented in TTAProgram::BasicBlock, and TTAProgram::Procedure.

Definition at line 89 of file CodeSnippet.cc.

89  {
90  for (unsigned int i = 0; i < instructions_.size(); i++) {
91  if (instructions_.at(i) != &NullInstruction::instance()) {
92  delete instructions_.at(i);
93  }
94  }
95  instructions_.clear();
96 }

References TTAProgram::NullInstruction::instance(), and instructions_.

Referenced by TTAProgram::Procedure::clear(), and TTAProgram::BasicBlock::clear().

Here is the call graph for this function:

◆ copy()

CodeSnippet * TTAProgram::CodeSnippet::copy ( ) const
virtual

Make a complete copy of the code snippet.

The copy is identical, except that it is not registered to the program of the original code snippet.

Returns
A complete copy of the code snippet.

Reimplemented in TTAProgram::BasicBlock, and TTAProgram::Procedure.

Definition at line 608 of file CodeSnippet.cc.

608  {
609 
610  CodeSnippet* newProc = new CodeSnippet(startAddr_);
611  if (instructionCount() > 0) {
612  Instruction* ins = &firstInstruction();
613  while (ins != &NullInstruction::instance()) {
614  newProc->add(ins->copy());
615  ins = &nextInstruction(*ins);
616  }
617  }
618  return newProc;
619 }

References add(), CodeSnippet(), TTAProgram::Instruction::copy(), firstInstruction(), TTAProgram::NullInstruction::instance(), instructionCount(), nextInstruction(), and startAddr_.

Here is the call graph for this function:

◆ deleteInstructionAt()

void TTAProgram::CodeSnippet::deleteInstructionAt ( InstructionAddress  address)
virtual

Remove instruction from the code snippet at the given address and delete it.

All instructions following the removed code snippet are relocated appropriately. All code labels attached to the removed instruction are deleted from the Program.

Note
Possible references to the instruction elsewhere in the program are not automatically fixed! It's a responsibility of the caller to fix the Program back to a consistent state.
Parameters
addressIndex of the instruction to remove.
Exceptions
IllegalRegistrationIf instruction does not belong to the code snippet.

Definition at line 593 of file CodeSnippet.cc.

593  {
594  Instruction& instr = instructionAt(address);
595  remove(instr);
596  delete &instr;
597 }

References address(), instructionAt(), and remove().

Here is the call graph for this function:

◆ disassembly()

std::string TTAProgram::CodeSnippet::disassembly ( ) const
virtual

Returns the disassembly of the basic block as string.

Returns
The disassembly of the basic block.

Definition at line 820 of file CodeSnippet.cc.

820  {
821 
822  std::string content = "";
823  const int iCount = instructionCount();
824  for (int i = 0; i < iCount; ++i) {
826  content += POMDisassembler::disassemble(instr);
827  content += "\n";
828  }
829  return content;
830 }

References POMDisassembler::disassemble(), instructionAtIndex(), and instructionCount().

Referenced by add(), LoopPrologAndEpilogBuilder::addEpilogFromRM(), LoopPrologAndEpilogBuilder::addPrologFromRM(), LoopPrologAndEpilogBuilder::build(), SimpleIfConverter::canConvert(), and toString().

Here is the call graph for this function:

◆ endAddress()

Address TTAProgram::CodeSnippet::endAddress ( ) const
virtual

Returns the first address that is outside of the code snippet.

Reimplemented in TTAProgram::NullProcedure.

Definition at line 788 of file CodeSnippet.cc.

788  {
789 
790  if (isInProgram() && parent().isFinalized()) {
791  return endAddr_;
792  } else {
793  InstructionAddress endLocation =
794  startAddr_.location() + instructions_.size();
795  return Address(endLocation, startAddr_.space());
796  }
797 }

References endAddr_, instructions_, isInProgram(), TTAProgram::Address::location(), parent(), TTAProgram::Address::space(), and startAddr_.

Referenced by ControlFlowGraph::buildFrom(), ProgramDependenceGraph::disassemble(), DisassembleCommand::execute(), TTASimulationController::findProgramExitPoints(), SimulatorFrontend::finishSimulation(), SimControlLanguageCommand::parseInstructionAddressExpression(), SimulatorFrontend::programLocationDescription(), and TTAProgram::Program::removeProcedure().

Here is the call graph for this function:

◆ firstInstruction()

Instruction & TTAProgram::CodeSnippet::firstInstruction ( ) const
virtual

Returns the first instruction in the code snippet.

Returns
The first instruction in the code snippet.
Exceptions
InstanceNotFoundif there are no instructions in the code snippet.

Reimplemented in TTAProgram::NullProcedure.

Definition at line 216 of file CodeSnippet.cc.

216  {
217  if (!instructions_.empty()) {
218  return *instructions_.at(0);
219  } else {
220  throw InstanceNotFound(
221  __FILE__, __LINE__, __func__, "No instructions.");
222  }
223 }

References __func__, and instructions_.

Referenced by SimpleIfConverter::addJump(), ControlFlowGraph::computeLeadersFromRefManager(), ControlFlowGraph::convertBBRefsToInstRefs(), TTAProgram::Program::convertSymbolRef(), copy(), SequentialScheduler::copyBasicBlocksToProcedure(), BasicBlockPass::copyRMToBB(), ControlFlowGraph::copyToProcedure(), ControlFlowGraph::createBBEdges(), ProgramDependenceGraph::createJump(), llvm::LLVMTCEBuilder::emitSPInitialization(), TTAProgram::TerminalBasicBlockReference::equals(), TTAProgram::TerminalInstructionReference::equals(), TTAProgram::Program::finalize(), TTASimulationController::findProgramExitPoints(), TTAProgram::Program::firstInstruction(), CallsToJumps::handleControlFlowGraph(), TTAProgram::CodeLabel::instructionReference(), ControlFlowGraph::jumpToBBN(), ControlFlowGraph::optimizeBBOrdering(), TTAProgram::Procedure::remove(), BasicBlockNode::updateReferencesFromProcToCfg(), TTAProgram::InstructionReferenceManager::validate(), and llvm::LLVMTCEIRBuilder::writeMachineFunction().

◆ hasNextInstruction()

bool TTAProgram::CodeSnippet::hasNextInstruction ( const Instruction ins) const
virtual

Tells whether the given instruction is the last instruction of the code snippet or not.

Parameters
insThe instruction to compare.
Returns
True if the given instruction is not the last instruction in the code snippet, false otherwise.

Reimplemented in TTAProgram::NullProcedure.

Definition at line 303 of file CodeSnippet.cc.

303  {
304  return (&nextInstruction(ins) != &NullInstruction::instance());
305 }

References TTAProgram::NullInstruction::instance(), and nextInstruction().

Referenced by insertAfter().

Here is the call graph for this function:

◆ hasReturn()

bool TTAProgram::CodeSnippet::hasReturn ( ) const

Returns true in case there is at least one procedure return in the code snippet.

Definition at line 804 of file CodeSnippet.cc.

804  {
805  const int iCount = instructionCount();
806  for (int i = 0; i < iCount; ++i) {
807  if (instructionAtIndex(i).hasReturn()) {
808  return true;
809  }
810  }
811  return false;
812 }

References instructionAtIndex(), and instructionCount().

Here is the call graph for this function:

◆ insertAfter() [1/3]

void TTAProgram::CodeSnippet::insertAfter ( const Instruction pos,
CodeSnippet cs 
)
virtual

Inserts a whole code snippet into this code snippet just after specified instruction.

This function moves the ownership of the moves and deletes the another code snippet.

Parameters
insThe succeeding the place where to insert he instructions.
csCodeSnippet containing the instructions being inserted.

Definition at line 690 of file CodeSnippet.cc.

691  {
692 
693  for (int i = cs->instructionCount()-1; i >= 0 ; i-- ) {
694  Instruction& ins = cs->instructionAtIndex(i);
695  cs->remove(ins);
696  insertAfter(pos, &ins);
697  }
698  delete cs;
699 }

References insertAfter(), instructionAtIndex(), instructionCount(), and remove().

Here is the call graph for this function:

◆ insertAfter() [2/3]

void TTAProgram::CodeSnippet::insertAfter ( const Instruction pos,
const CodeSnippet cs 
)
virtual

Inserts a whole code snippet into this code snippet just after specified instruction.

This function does not remove the instruction references from the code snippet.

Parameters
insThe succeeding the place where to insert he instructions.
csCodeSnippet containing the instructions being inserted.

Definition at line 671 of file CodeSnippet.cc.

672  {
673 
674  for (int i = cs.instructionCount()-1; i >= 0 ; i-- ) {
675  insertAfter(pos, cs.instructionAtIndex(i).copy());
676  }
677 }

References TTAProgram::Instruction::copy(), insertAfter(), instructionAtIndex(), and instructionCount().

Here is the call graph for this function:

◆ insertAfter() [3/3]

void TTAProgram::CodeSnippet::insertAfter ( const Instruction pos,
Instruction ins 
)
virtual

Insert the given instruction after the given position.

Instructions from pos are relocated to make room for the new instruction.

Parameters
posThe position.
insThe instruction to insert.
Exceptions
IllegalRegistrationif pos does not belong to the code snippet or ins already belongs to a code snippet.

Reimplemented in TTAProgram::Procedure.

Definition at line 462 of file CodeSnippet.cc.

462  {
463  if (!ins->isInProcedure()) {
464 
465  if (hasNextInstruction(pos)) {
466 
467  InsList::iterator iter = instructions_.begin();
468  Instruction& next = nextInstruction(pos);
469 
470  while (iter != instructions_.end()) {
471 
472  if ((*iter) == &next) {
473 
474  ins->setParent(*this);
475 
476  iter = instructions_.insert(iter, ins);
477 
478  iter++;
479 
480  return;
481  }
482 
483  iter++;
484  }
485 
486  // should not go here in any case
487  assert(false);
488 
489  } else {
490  add(ins);
491  }
492 
493  } else {
494  throw IllegalRegistration(
495  __FILE__, __LINE__, "CodeSnippet::insertInstructionBefore",
496  "Instruction already belongs to a procedure.");
497  }
498 }

References add(), assert, hasNextInstruction(), instructions_, TTAProgram::Instruction::isInProcedure(), nextInstruction(), and TTAProgram::Instruction::setParent().

Referenced by insertAfter(), and PreOptimizer::tryToRemoveGuardInversingOp().

Here is the call graph for this function:

◆ insertBefore() [1/3]

void TTAProgram::CodeSnippet::insertBefore ( const Instruction pos,
CodeSnippet cs 
)
virtual

Inserts a whole code snippet into this code snippet just before specified instruction. This function moves the ownership of the moves and deletes the another code snippet.

Parameters
insThe succeeding the place where to insert he instructions.
csCodeSnippet containing the instructions being inserted.

Definition at line 650 of file CodeSnippet.cc.

651  {
652  for( int i = 0, count = cs->instructionCount(); i < count; i++ ) {
653  Instruction& ins = cs->instructionAtIndex(0);
654  cs->remove(ins);
655  insertBefore(pos, &ins);
656  }
657  delete cs;
658  }

References insertBefore(), instructionAtIndex(), instructionCount(), and remove().

Here is the call graph for this function:

◆ insertBefore() [2/3]

void TTAProgram::CodeSnippet::insertBefore ( const Instruction pos,
const CodeSnippet cs 
)
virtual

Inserts a whole code snippet into this code snippet just before specified instruction.

This function does not remove the instruction references from the code snippet.

Parameters
insThe succeeding the place where to insert he instructions.
csCodeSnippet containing the instructions being inserted.

Definition at line 632 of file CodeSnippet.cc.

633  {
634  for( int i = 0; i < cs.instructionCount(); i++ ) {
635  insertBefore(pos, cs.instructionAtIndex(i).copy());
636  }
637  }

References TTAProgram::Instruction::copy(), insertBefore(), instructionAtIndex(), and instructionCount().

Here is the call graph for this function:

◆ insertBefore() [3/3]

void TTAProgram::CodeSnippet::insertBefore ( const Instruction pos,
Instruction ins 
)
virtual

Insert the given instruction before the given position.

Instructions from and including pos are relocated to make room for the new instruction.

Todo:
Refactor and share implementation with insertInstructionAfter()
Parameters
posThe position.
insThe instruction to insert.
Exceptions
IllegalRegistrationif pos does not belong to the code snippet or ins already belongs to a code snippet.

Reimplemented in TTAProgram::Procedure.

Definition at line 514 of file CodeSnippet.cc.

514  {
515  if (!ins->isInProcedure()) {
516 
517  InsList::iterator iter = instructions_.begin();
518  const Instruction& next = pos;
519 
520  while (iter != instructions_.end()) {
521 
522  if ((*iter) == &next) {
523 
524  ins->setParent(*this);
525  iter = instructions_.insert(iter, ins);
526  iter++;
527  return;
528  }
529  iter++;
530  }
531  // should not go here in any case
532  assert(false);
533  } else {
534  throw IllegalRegistration(
535  __FILE__, __LINE__, "CodeSnippet::insertInstructionBefore",
536  "Instruction already belongs to a procedure.");
537  }
538 }

References assert, instructions_, TTAProgram::Instruction::isInProcedure(), and TTAProgram::Instruction::setParent().

Referenced by llvm::LLVMTCEBuilder::emitSPInitialization(), CallsToJumps::handleControlFlowGraph(), and insertBefore().

Here is the call graph for this function:

◆ instructionAt()

Instruction & TTAProgram::CodeSnippet::instructionAt ( UIntWord  address) const
virtual

Returns the explicit instruction at a given address.

The address space of the address is implied, since there is only one address space for instructions. In case there are multiple instructions at the given address, only one of them is considered explicit, others implicit. The implicit instructions are detected by having a size of 0.

Parameters
addressThe instruction address.
Note
This is a slow method to traverse instructions, it's O(N).
Exceptions
KeyNotFoundif given address is illegal.
Todo:
Rename to instruction() to match Program::procedure() and Instruction::move().

Reimplemented in TTAProgram::NullProcedure.

Definition at line 241 of file CodeSnippet.cc.

241  {
242  if (isInProgram() && !parent().isInstructionPerAddress()) {
243  // In the finalized program, there might not be one instruction
244  // per index, thus we have to find the instruction by an
245  // exhaustive search. @todo optimize this in case it's a
246  // bottleneck somewhere.
247 
248  for (size_t i = 0; i < instructions_.size(); ++i) {
249  Instruction* ins = instructions_.at(i);
250  if (ins->address().location() == address &&
251  ins->size() > 0)
252  return *ins;
253  }
254  } else {
255  int index = (address - startAddr_.location());
256  if (index >= 0 && static_cast<unsigned int>(index) <
257  instructions_.size()) {
258  Instruction* ins = instructions_.at(index);
259  if (ins == &NullInstruction::instance()) {
260  throw KeyNotFound(__FILE__, __LINE__);
261  } else {
262  return *ins;
263  }
264  }
265  }
266  std::string msg = "Address " + Conversion::toString(address) +
267  " not in this codesnippet( " +
270  instructions_.size()-1)
271  + " )";
272  throw KeyNotFound(__FILE__, __LINE__, __func__, msg );
273 }

References __func__, address(), TTAProgram::Instruction::address(), TTAProgram::NullInstruction::instance(), instructions_, isInProgram(), TTAProgram::Address::location(), parent(), TTAProgram::Instruction::size(), startAddr_, and Conversion::toString().

Referenced by ControlFlowGraph::computeLeadersFromRelocations(), SequentialScheduler::copyBasicBlocksToProcedure(), ProcedurePass::copyCfgToProcedure(), ControlFlowGraph::createAllBlocks(), deleteInstructionAt(), and ControlFlowGraph::splitBasicBlocksWithCallsAndRefs().

Here is the call graph for this function:

◆ instructionAtIndex()

Instruction & TTAProgram::CodeSnippet::instructionAtIndex ( int  index) const
virtual

Returns the instruction at a given index in the container of instructions.

This method is for quickly traversing through all the instructions within time critical loops.

Parameters
indexThe instruction index in the list of instructions.
Returns
The Instruction at the given index.

Definition at line 285 of file CodeSnippet.cc.

285  {
286  return *instructions_.at(index);
287 }

References instructions_.

Referenced by SimpleICOptimizer::addConnections(), InlineAsmParser::addDebugInfoToInlineAsmBB(), append(), Peel2BBLoops::appendBB(), SimpleIfConverter::appendBB(), CodeCompressorPlugin::bemBits(), LoopPrologAndEpilogBuilder::build(), MoveNodeGroupBuilder::build(), SimProgramBuilder::build(), ControlFlowGraph::buildMBBFromBB(), CopyingDelaySlotFiller::checkImmediatesAfter(), CopyingDelaySlotFiller::collectMoves(), ControlFlowGraph::computeLeadersFromJumpSuccessors(), DataDependenceGraphBuilder::constructIndividualBB(), ControlFlowGraph::convertBBRefsToInstRefs(), TTAProgram::Program::convertSymbolRefsToInsRefs(), TTAProgram::Procedure::copy(), TTAProgram::BasicBlock::copy(), SequentialScheduler::copyBasicBlocksToProcedure(), ProcedurePass::copyCfgToProcedure(), TTAProgram::Program::copyFrom(), BasicBlockPass::copyRMToBB(), ControlFlowGraph::copyToProcedure(), SequentialScheduler::createBasicBlocks(), ControlFlowGraph::createBBEdges(), ControlFlowGraph::createBlock(), TTAProgram::ProgramWriter::createCodeSection(), ControlFlowGraph::createJumps(), ControlFlowGraph::directJump(), ProgramDependenceGraph::disassemble(), POMDisassembler::disassembleProcedure(), disassembly(), llvm::LLVMTCEBuilder::emitInlineAsm(), llvm::LLVMTCEBuilder::emitLongjmp(), llvm::LLVMTCEBuilder::emitSetjmp(), CopyingDelaySlotFiller::fillDelaySlots(), CopyingDelaySlotFiller::findJump(), CopyingDelaySlotFiller::findJumpImmediate(), BasicBlockNode::findJumps(), ControlFlowGraph::findNextIndex(), TTASimulationController::findProgramExitPoints(), DataDependenceGraphBuilder::findStaticRegisters(), TTAProgram::Program::fixInstructionReferences(), DataDependenceGraph::fixInterBBAntiEdges(), CompiledSimCodeGenerator::generateProcedureCode(), PostpassOperandSharer::handleBasicBlock(), CallsToJumps::handleControlFlowGraph(), AbsoluteToRelativeJumps::handleProcedure(), SimpleIfConverter::hasConditionals(), hasReturn(), ControlFlowGraph::indirectJump(), insertAfter(), insertBefore(), ControlDependenceNode::instruction(), TTAProgram::Program::instructionVector(), main(), ControlFlowGraph::mergeNodes(), LoopPrologAndEpilogBuilder::moveJumpDestination(), ControlFlowGraph::optimizeBBOrdering(), LoopPrologAndEpilogBuilder::optimizeEpilog(), LoopPrologAndEpilogBuilder::optimizeProlog(), prepend(), ControlFlowGraph::removeJumpToTarget(), ControlFlowGraph::removeUnreachableNodes(), TTAProgram::Program::replaceUniversalAddressSpaces(), ControlFlowGraph::splitBasicBlockAtIndex(), TTAProgram::BasicBlock::statistics(), BasicBlockNode::toString(), PostpassOperandSharer::tryRemoveOperandWrite(), CopyingDelaySlotFiller::updateFTBBAndCfg(), BasicBlockNode::updateHWloopLength(), CopyingDelaySlotFiller::updateJumpsAndCfg(), ControlFlowGraph::updateReferencesFromProcToCfg(), and SimpleIfConverter::writesRegister().

◆ instructionCount()

int TTAProgram::CodeSnippet::instructionCount ( ) const
virtual

Returns the number of instructions in this code snippet.

Returns
the number of instructions in this code snippet.

Reimplemented in TTAProgram::NullProcedure.

Definition at line 205 of file CodeSnippet.cc.

205  {
206  return instructions_.size();
207 }

References instructions_.

Referenced by SimpleICOptimizer::addConnections(), InlineAsmParser::addDebugInfoToInlineAsmBB(), LoopAnalyzer::analyze(), append(), Peel2BBLoops::appendBB(), SimpleIfConverter::appendBB(), CodeCompressorPlugin::bemBits(), MoveNodeGroupBuilder::build(), SimProgramBuilder::build(), ControlFlowGraph::buildMBBFromBB(), llvm::LLVMTCEIRBuilder::buildTCECFG(), SimpleIfConverter::canConvert(), CopyingDelaySlotFiller::checkImmediatesAfter(), TTAProgram::Procedure::clear(), CopyingDelaySlotFiller::collectMoves(), SimpleIfConverter::combineBlocks(), ControlFlowGraph::computeLeadersFromJumpSuccessors(), DataDependenceGraphBuilder::constructIndividualBB(), ControlFlowGraph::convertBBRefsToInstRefs(), TTAProgram::Program::convertSymbolRef(), TTAProgram::Program::convertSymbolRefsToInsRefs(), TTAProgram::Procedure::copy(), TTAProgram::BasicBlock::copy(), copy(), SequentialScheduler::copyBasicBlocksToProcedure(), ProcedurePass::copyCfgToProcedure(), TTAProgram::Program::copyFrom(), InlineAsmParser::copyInstructions(), BasicBlockPass::copyRMToBB(), ControlFlowGraph::copyToProcedure(), SequentialScheduler::createBasicBlocks(), ControlFlowGraph::createBBEdges(), TTAProgram::ProgramWriter::createCodeSection(), SimpleIfConverter::detectDiamond(), SimpleIfConverter::detectTriangleViaFt(), SimpleIfConverter::detectTriangleViaJump(), ControlFlowGraph::directJump(), ProgramDependenceGraph::disassemble(), POMDisassembler::disassembleProcedure(), disassembly(), llvm::LLVMTCEBuilder::emitInlineAsm(), llvm::LLVMTCEBuilder::emitLongjmp(), llvm::LLVMTCEBuilder::emitSetjmp(), llvm::LLVMTCEBuilder::emitSPInitialization(), BasicBlockPass::executeDDGPass(), CopyingDelaySlotFiller::fillDelaySlots(), CopyingDelaySlotFiller::findJump(), BasicBlockNode::findJumps(), ControlFlowGraph::findLimmWrite(), ControlFlowGraph::findNextIndex(), TTASimulationController::findProgramExitPoints(), DataDependenceGraphBuilder::findStaticRegisters(), TTAProgram::Program::fixInstructionReferences(), DataDependenceGraph::fixInterBBAntiEdges(), CompiledSimCodeGenerator::generateProcedureCode(), PostpassOperandSharer::handleBasicBlock(), BasicBlockPass::handleBasicBlock(), SequentialScheduler::handleBasicBlock(), BBSchedulerController::handleBasicBlock(), CallsToJumps::handleControlFlowGraph(), AbsoluteToRelativeJumps::handleProcedure(), PreOptimizer::handleProcedure(), BBSchedulerController::handleProcedure(), SimpleIfConverter::hasConditionals(), hasReturn(), ControlFlowGraph::indirectJump(), insertAfter(), insertBefore(), POMDisassembler::instructionCount(), ControlDependenceNode::instructionCount(), TTAProgram::Program::instructionCount(), TTAProgram::CodeLabel::instructionReference(), TTAProgram::Program::instructionVector(), TTAProgram::BasicBlock::isEmpty(), main(), BasicBlockNode::maximumSize(), ScheduleEstimator::maximumSizeOfBB(), ControlFlowGraph::mergeNodes(), ControlFlowGraph::optimizeBBOrdering(), LoopPrologAndEpilogBuilder::optimizeEpilog(), LoopPrologAndEpilogBuilder::optimizeProlog(), prepend(), SimpleIfConverter::removeJump(), ControlFlowGraph::removeJumpToTarget(), ControlFlowGraph::removeUnreachableNodes(), TTAProgram::Program::replaceUniversalAddressSpaces(), ControlFlowGraph::splitBasicBlockAtIndex(), ControlFlowGraph::splitBasicBlocksWithCallsAndRefs(), ControlFlowGraph::splitBB(), TTAProgram::BasicBlock::statistics(), SimpleIfConverter::successors(), BasicBlockNode::toString(), CopyingDelaySlotFiller::tryToFillSlots(), CopyingDelaySlotFiller::updateFTBBAndCfg(), BasicBlockNode::updateHWloopLength(), BasicBlockNode::updateReferencesFromProcToCfg(), ControlFlowGraph::updateReferencesFromProcToCfg(), llvm::LLVMTCEIRBuilder::writeMachineFunction(), and SimpleIfConverter::writesRegister().

◆ isInProgram()

bool TTAProgram::CodeSnippet::isInProgram ( ) const
virtual

◆ lastInstruction()

Instruction & TTAProgram::CodeSnippet::lastInstruction ( ) const
virtual

◆ nextInstruction()

Instruction & TTAProgram::CodeSnippet::nextInstruction ( const Instruction ins) const
virtual

Returns the next instruction in the instruction stream.

Returns
The next instruction in the instruction stream or a special null instruction if the given instruction is the last instruction of the code snippet.
Exceptions
IllegalRegistrationIf given instruction does not belong to the code snippet.

Reimplemented in TTAProgram::NullProcedure.

Definition at line 318 of file CodeSnippet.cc.

318  {
319  if (&ins.parent() != this)
320  throw IllegalRegistration(__FILE__, __LINE__);
321 
322  if (isInProgram() && !parent().isInstructionPerAddress()) {
323  InsList::const_iterator pos =
324  std::find(instructions_.begin(), instructions_.end(), &ins);
325  if (pos == instructions_.end() || pos+1 == instructions_.end())
326  return NullInstruction::instance();
327  else
328  return **(pos+1);
329  } else {
330  int insAddress = ins.address().location();
331 
332  unsigned int current = (insAddress - startAddr_.location());
333  unsigned int next = current + 1;
334 
335  if (next < instructions_.size())
336  return *instructions_.at(next);
337  else
338  return NullInstruction::instance();
339  }
340 }

References TTAProgram::Instruction::address(), TTAProgram::NullInstruction::instance(), instructions_, isInProgram(), TTAProgram::Address::location(), parent(), TTAProgram::Instruction::parent(), and startAddr_.

Referenced by copy(), TTAProgram::CodeGenerator::createSchedYieldProcedure(), llvm::LLVMTCEBuilder::emitReadSP(), llvm::LLVMTCEBuilder::emitReturnTo(), llvm::LLVMTCEBuilder::emitWriteSP(), llvm::LLVMTCEBuilder::handleMemoryCategoryInfo(), hasNextInstruction(), insertAfter(), and TTAProgram::Program::nextInstruction().

Here is the call graph for this function:

◆ operator[]()

Instruction & TTAProgram::CodeSnippet::operator[] ( size_t  index) const
virtual

Definition at line 290 of file CodeSnippet.cc.

290  {
291  return *instructions_[index];
292 }

References instructions_.

◆ parent()

Program & TTAProgram::CodeSnippet::parent ( ) const
virtual

◆ prepend() [1/2]

void TTAProgram::CodeSnippet::prepend ( CodeSnippet cs)
virtual

Inserts a whole code snippet at the beginning of this code snippet.

This function moves the ownership of instructions from the old code snippet into the new one, and deletes the given code snippet.

@TODO: This routine is O(n^2). Optimize!

Parameters
csCodeSnippet containing the instructions being inserted.

Definition at line 765 of file CodeSnippet.cc.

765  {
766  for (int i = cs->instructionCount()-1; i >= 0 ; i--) {
767  Instruction &ins = cs->instructionAtIndex(i);
768  cs->remove(ins);
769  addFront(&ins);
770  }
771  delete cs;
772 }

References addFront(), instructionAtIndex(), instructionCount(), and remove().

Here is the call graph for this function:

◆ prepend() [2/2]

void TTAProgram::CodeSnippet::prepend ( const CodeSnippet cs)
virtual

Inserts a whole code snippet at the beginning of this code snippet.

This function does not remove the instruction references from the code snippet.

Parameters
csCodeSnippet containing the instructions being inserted.

Definition at line 748 of file CodeSnippet.cc.

748  {
749  for (int i = cs.instructionCount()-1; i >= 0 ; i--) {
750  addFront(cs.instructionAtIndex(i).copy());
751  }
752 }

References addFront(), TTAProgram::Instruction::copy(), instructionAtIndex(), and instructionCount().

Here is the call graph for this function:

◆ previousInstruction()

Instruction & TTAProgram::CodeSnippet::previousInstruction ( const Instruction ins) const
virtual

Return the previous instruction in the instruction stream.

Returns
The previous instruction in the instruction stream or a special null instruction if the given instruction is the first instruction of the code snippet.
Exceptions
IllegalRegistrationIf given instruction does not belong to the code snippet.

Reimplemented in TTAProgram::NullProcedure.

Definition at line 352 of file CodeSnippet.cc.

352  {
353  if (&ins.parent() != this)
354  throw IllegalRegistration(__FILE__, __LINE__, __func__);
355 
356  if (isInProgram() && !parent().isInstructionPerAddress()) {
357  InsList::const_iterator pos =
358  std::find(instructions_.begin(), instructions_.end(), &ins);
359 
360  if (pos == instructions_.begin())
361  return NullInstruction::instance();
362 
363  return *const_cast<Instruction*>(*(--pos));
364  } else {
365  int insAddress =
366  (ins.address().location() - startAddr_.location()) - 1;
367  while (insAddress >= 0) {
368  Instruction& prevIns = *instructions_.at(insAddress);
369  if (&prevIns != &NullInstruction::instance()) {
370  return prevIns;
371  } else {
372  insAddress--;
373  }
374  }
375  return NullInstruction::instance();
376  }
377 }

References __func__, TTAProgram::Instruction::address(), TTAProgram::NullInstruction::instance(), instructions_, isInProgram(), TTAProgram::Address::location(), parent(), TTAProgram::Instruction::parent(), and startAddr_.

Referenced by SimpleIfConverter::canConvert(), ControlFlowGraph::createJumps(), and SimpleIfConverter::successors().

Here is the call graph for this function:

◆ remove()

void TTAProgram::CodeSnippet::remove ( Instruction ins)
virtual

Remove instruction from the code snippet.

The instruction becomes independent (it is not deleted). All instructions following the removed code snippet are relocated appropriately.

The instructions should not have code labels attached to them.

Note
Possible references to the instruction elsewhere in the program are not automatically fixed! It's a responsibility of the caller to fix the Program back to a consistent state.
Parameters
insInstruction to remove.
Exceptions
IllegalRegistrationIf instruction does not belong to the code snippet.

Reimplemented in TTAProgram::Procedure.

Definition at line 558 of file CodeSnippet.cc.

558  {
559  if (!ins.isInProcedure() || !(&ins.parent() == this)) {
560  string msg = "Instruction doesn't belong to the procedure.";
561  throw IllegalRegistration(__FILE__, __LINE__, __func__, msg);
562  }
563 
564  InsList::iterator iter = instructions_.begin();
565 
566  for (; iter != instructions_.end(); iter++) {
567  if ((*iter) == &ins) {
568 
569  iter = instructions_.erase(iter);
570  ins.setParent(NullProcedure::instance());
571 
572  return;
573  }
574  }
575 }

References __func__, TTAProgram::NullProcedure::instance(), instructions_, TTAProgram::Instruction::isInProcedure(), TTAProgram::Instruction::parent(), and TTAProgram::Instruction::setParent().

Referenced by append(), deleteInstructionAt(), CallsToJumps::handleControlFlowGraph(), insertAfter(), insertBefore(), ControlFlowGraph::mergeNodes(), LoopPrologAndEpilogBuilder::optimizeEpilog(), LoopPrologAndEpilogBuilder::optimizeProlog(), prepend(), ControlFlowGraph::splitBasicBlockAtIndex(), ControlFlowGraph::splitBB(), PreOptimizer::tryToPrecalcConstantAdd(), and PreOptimizer::tryToRemoveGuardInversingOp().

Here is the call graph for this function:

◆ removeLastInstruction()

void TTAProgram::CodeSnippet::removeLastInstruction ( )
virtual

Remove one instruction from the end of the code snippet.

Nothing happens if the code snippet is empty.

Definition at line 104 of file CodeSnippet.cc.

104  {
105  if (!instructions_.empty()) {
106  instructions_.pop_back();
107  }
108 }

References instructions_.

◆ setEndAddress()

void TTAProgram::CodeSnippet::setEndAddress ( Address  end)
virtual

Definition at line 195 of file CodeSnippet.cc.

195  {
196  endAddr_ = end;
197 }

References endAddr_.

Referenced by TTAProgram::Program::finalize().

◆ setParent()

void TTAProgram::CodeSnippet::setParent ( Program prog)
virtual

Sets the parent program of the code snippet.

Note
This method does not check that the code snippet actually belongs to the given program. It should, therefore be called only be Program::addProcedure().
Parameters
progThe new parent program.

Reimplemented in TTAProgram::NullProcedure.

Definition at line 136 of file CodeSnippet.cc.

136  {
137  if (&prog == &NullProgram::instance()) {
138  parent_ = NULL;
139  } else {
140  parent_ = &prog;
141  }
142 }

References TTAProgram::NullProgram::instance(), and parent_.

Referenced by TTAProgram::Program::addProcedure(), and TTAProgram::Program::removeProcedure().

Here is the call graph for this function:

◆ setStartAddress()

void TTAProgram::CodeSnippet::setStartAddress ( Address  start)
virtual

Sets the start address of the code snippet.

Reimplemented in TTAProgram::NullProcedure.

Definition at line 190 of file CodeSnippet.cc.

190  {
191  startAddr_ = start;
192 }

References startAddr_.

Referenced by TTAProgram::Program::addProcedure(), TTAProgram::Program::finalize(), and TTAProgram::Program::moveProcedure().

◆ startAddress()

Address TTAProgram::CodeSnippet::startAddress ( ) const
virtual

◆ toString()

virtual std::string TTAProgram::CodeSnippet::toString ( ) const
inlinevirtual

Member Data Documentation

◆ endAddr_

Address TTAProgram::CodeSnippet::endAddr_
protected

The highest address of the procedure.

Definition at line 132 of file CodeSnippet.hh.

Referenced by endAddress(), and setEndAddress().

◆ instructions_

InsList TTAProgram::CodeSnippet::instructions_
protected

◆ parent_

Program* TTAProgram::CodeSnippet::parent_
protected

◆ startAddr_

Address TTAProgram::CodeSnippet::startAddr_
protected

The documentation for this class was generated from the following files:
TTAMachine::NullAddressSpace::instance
static NullAddressSpace & instance()
Definition: NullAddressSpace.cc:62
TTAProgram::CodeSnippet::endAddr_
Address endAddr_
The highest address of the procedure.
Definition: CodeSnippet.hh:132
InstructionAddress
UInt32 InstructionAddress
Definition: BaseType.hh:175
TTAProgram::CodeSnippet::firstInstruction
virtual Instruction & firstInstruction() const
Definition: CodeSnippet.cc:216
TTAProgram::CodeSnippet::insertBefore
virtual void insertBefore(const Instruction &pos, Instruction *ins)
Definition: CodeSnippet.cc:514
TTAProgram::Instruction
Definition: Instruction.hh:57
TTAProgram::NullProcedure::instance
static NullProcedure & instance()
Definition: NullProcedure.cc:68
TTAProgram::CodeSnippet::remove
virtual void remove(Instruction &ins)
Definition: CodeSnippet.cc:558
TTAProgram::Address::space
const TTAMachine::AddressSpace & space() const
TTAProgram::CodeSnippet::nextInstruction
virtual Instruction & nextInstruction(const Instruction &ins) const
Definition: CodeSnippet.cc:318
Conversion::toString
static std::string toString(const T &source)
TTAProgram::CodeSnippet::disassembly
virtual std::string disassembly() const
Definition: CodeSnippet.cc:820
TTAProgram::NullProgram::instance
static NullProgram & instance()
Definition: NullProgram.cc:72
assert
#define assert(condition)
Definition: Application.hh:86
TTAProgram::CodeSnippet::hasReturn
bool hasReturn() const
Definition: CodeSnippet.cc:804
TTAProgram::CodeSnippet::insertAfter
virtual void insertAfter(const Instruction &pos, Instruction *ins)
Definition: CodeSnippet.cc:462
TTAProgram::CodeSnippet::instructionCount
virtual int instructionCount() const
Definition: CodeSnippet.cc:205
TTAProgram::CodeSnippet::add
virtual void add(Instruction *ins)
Definition: CodeSnippet.cc:432
__func__
#define __func__
Definition: Application.hh:67
TTAProgram::CodeSnippet::hasNextInstruction
virtual bool hasNextInstruction(const Instruction &ins) const
Definition: CodeSnippet.cc:303
TTAProgram::Address::location
InstructionAddress location() const
TTAProgram::NullInstruction::instance
static NullInstruction & instance()
Definition: NullInstruction.cc:66
IllegalRegistration
Definition: Exception.hh:532
TTAProgram::CodeSnippet::addFront
virtual void addFront(Instruction *ins)
Definition: CodeSnippet.cc:406
TTAProgram::CodeSnippet::address
virtual Address address(const Instruction &ins) const
Definition: CodeSnippet.cc:163
POMDisassembler::disassemble
static std::string disassemble(const TTAProgram::Move &move)
Definition: POMDisassembler.cc:629
TTAProgram::CodeSnippet::parent
virtual Program & parent() const
Definition: CodeSnippet.cc:118
TTAProgram::CodeSnippet::parent_
Program * parent_
The parent program of the procedure.
Definition: CodeSnippet.hh:128
TTAProgram::CodeSnippet::instructionAtIndex
virtual Instruction & instructionAtIndex(int index) const
Definition: CodeSnippet.cc:285
KeyNotFound
Definition: Exception.hh:285
TTAProgram::CodeSnippet::instructionAt
virtual Instruction & instructionAt(UIntWord address) const
Definition: CodeSnippet.cc:241
TTAProgram::CodeSnippet::instructions_
InsList instructions_
The instructions in this procedure.
Definition: CodeSnippet.hh:126
TTAProgram::CodeSnippet::startAddr_
Address startAddr_
The start (lowest) address of the procedure.
Definition: CodeSnippet.hh:130
InstanceNotFound
Definition: Exception.hh:304
TTAProgram::CodeSnippet::CodeSnippet
CodeSnippet()
Definition: CodeSnippet.cc:55
TTAProgram::CodeSnippet::isInProgram
virtual bool isInProgram() const
Definition: CodeSnippet.cc:151