OpenASIP  2.0
Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
InstructionDictionary Class Reference
Inheritance diagram for InstructionDictionary:
Inheritance graph
Collaboration diagram for InstructionDictionary:
Collaboration graph

Public Member Functions

 InstructionDictionary ()
 
virtual InstructionBitVectorcompress (const std::string &programName)
 
virtual void generateDecompressor (std::ostream &stream, TCEString entityStr)
 
virtual void printDescription (std::ostream &stream)
 
- Public Member Functions inherited from CodeCompressorPlugin
virtual ~CodeCompressorPlugin ()
 
void setParameters (ParameterTable parameters)
 
void setPrograms (std::map< std::string, TPEF::Binary * > &programs)
 
void setMachine (const TTAMachine::Machine &machine)
 
void setBEM (const BinaryEncoding &bem)
 
const BinaryEncodingbinaryEncoding () const
 
const TTAMachine::Machinemachine () const
 
unsigned int memoryAddress (const TTAProgram::Instruction &instruction) const
 
TTAProgram::ProgramcurrentProgram () const
 
int imemMauWidth () const
 
InstructionBitVectorbemInstructionBits (const TTAProgram::Instruction &)
 

Private Types

typedef std::map< BitVector, unsigned int > Dictionary
 Map type for dictionary. More...
 

Private Member Functions

void createDictionary ()
 
void addToDictionary (const BitVector &instructionBits)
 
void updateDictionary (const Program &program)
 
void addInstructions ()
 

Private Attributes

Dictionary dictionary_
 The dictionary. More...
 
bool compatibilityProgDone_
 Indicates whether the compatibility program is in dictionary. More...
 
bool dictionaryCreated_
 Indicates whether the whole dictionary has been created. More...
 

Additional Inherited Members

- Public Types inherited from CodeCompressorPlugin
typedef std::vector< ParameterParameterTable
 Table for passing plugin parameters. More...
 
- Protected Types inherited from CodeCompressorPlugin
typedef std::map< std::string, TPEF::Binary * > TPEFMap
 Map type for handling programs. More...
 
- Protected Member Functions inherited from CodeCompressorPlugin
 CodeCompressorPlugin ()
 
InstructionBitVectorbemBits (const TTAProgram::Program &program)
 
int moveSlotCount () const
 
int moveSlotWidth (int index) const
 
int firstMoveSlotIndex () const
 
void startNewProgram (const std::string &programName)
 
void addInstruction (const TTAProgram::Instruction &instruction, InstructionBitVector *bits)
 
void setInstructionToStartAtBeginningOfMAU (const TTAProgram::Instruction &instruction)
 
void setAllInstructionsToStartAtBeginningOfMAU ()
 
InstructionBitVectorprogramBits () const
 
bool hasParameter (const std::string &paramName) const
 
std::string parameterValue (const std::string &paramName) const
 
void setImemWidth (int mau, int widthInMaus=1)
 
int numberOfPrograms () const
 
TPEFMap::const_iterator programElement (int index) const
 
std::string indentation (int level)
 

Detailed Description

Implements a simple instruction based dictionary compression scheme.

Definition at line 78 of file InstructionDictionary.cc.

Member Typedef Documentation

◆ Dictionary

typedef std::map<BitVector, unsigned int> InstructionDictionary::Dictionary
private

Map type for dictionary.

Definition at line 251 of file InstructionDictionary.cc.

Constructor & Destructor Documentation

◆ InstructionDictionary()

InstructionDictionary::InstructionDictionary ( )
inline

The constructor.

Definition at line 84 of file InstructionDictionary.cc.

84  :
86  dictionaryCreated_(false) {
87  }

Member Function Documentation

◆ addInstructions()

void InstructionDictionary::addInstructions ( )
inlineprivate

Adds the compressed instructions to the program.

Definition at line 315 of file InstructionDictionary.cc.

315  {
316  Instruction* instruction = &currentProgram().firstInstruction();
317  while (instruction != &NullInstruction::instance()) {
319  unsigned int code = MapTools::valueForKey<unsigned int>(
320  dictionary_, *bemBits);
321  InstructionBitVector* compressedInstruction =
322  new InstructionBitVector();
323  static_cast<BitVector*>(compressedInstruction)->pushBack(
324  code, static_cast<int>(
325  MathTools::requiredBits(dictionary_.size() - 1)));
326  addInstruction(*instruction, compressedInstruction);
327  instruction = &currentProgram().nextInstruction(*instruction);
328 
329  }
330  }

References MathTools::requiredBits().

Here is the call graph for this function:

◆ addToDictionary()

void InstructionDictionary::addToDictionary ( const BitVector instructionBits)
inlineprivate

Adds the given instruction bits to the dictionary.

Parameters
instructionBitsThe instruction bits to add.

Definition at line 287 of file InstructionDictionary.cc.

287  {
288  if (!MapTools::containsKey(dictionary_, instructionBits)) {
289  unsigned int code = dictionary_.size();
290  dictionary_.insert(
291  std::pair<BitVector, unsigned int>(instructionBits, code));
292  }
293  }

References MapTools::containsKey().

Here is the call graph for this function:

◆ compress()

virtual InstructionBitVector* InstructionDictionary::compress ( const std::string &  programName)
inlinevirtual

Creates the compressed code and returns the bit vector of it.

Implements CodeCompressorPlugin.

Definition at line 94 of file InstructionDictionary.cc.

94  {
95  try {
99  ProgrammabilityValidator validator(machine());
101  Binary* compatibilityBin = validator.profile(results);
102  TPEFProgramFactory factory(*compatibilityBin, machine());
103  Program* compatibilityProg = factory.build();
104  updateDictionary(*compatibilityProg);
105  delete compatibilityBin;
106  delete compatibilityProg;
107  compatibilityProgDone_ = true;
108  }
109  } catch (const Exception& e) {
110  string errorMsg = "Unable to ensure programmability: " +
111  e.errorMessage();
112  throw InvalidData(
113  __FILE__, __LINE__, __func__, errorMsg);
114  }
115  if (!dictionaryCreated_) {
117 
118  unsigned int compressedImemWidth =
120  assert(compressedImemWidth <= sizeof(long long unsigned int)*8
121  && "Compressed instruction width is too big");
122 
123  // fix imem width (mau == instruction width)
124  setImemWidth(compressedImemWidth);
125  }
126  startNewProgram(programName);
128  addInstructions();
129  return programBits();
130  }

References __func__, assert, TTAProgram::TPEFProgramFactory::build(), ENSURE_PROGRAMMABILITY, Exception::errorMessage(), machine, ProgrammabilityValidator::profile(), MathTools::requiredBits(), and YES.

Here is the call graph for this function:

◆ createDictionary()

void InstructionDictionary::createDictionary ( )
inlineprivate

Creates the whole dictionary

Definition at line 258 of file InstructionDictionary.cc.

258  {
259  for (int i = 0; i < numberOfPrograms(); i++) {
260  TPEFMap::const_iterator iter = programElement(i);
261  string name = iter->first;
262  startNewProgram(name);
265  }
266  dictionaryCreated_ = true;
267  if (Application::verboseLevel() > 0 && dictionary_.size() > 0) {
268  std::size_t keyWidth =
270  std::size_t entrySize = binaryEncoding().width();
271  std::size_t entries = dictionary_.size();
273  << (boost::format(
274  "dictionary width: %d bits, entries: %d, "
275  "total size: %d bits (%d bytes)\n" )
276  % keyWidth % entries % (entries * entrySize)
277  % std::size_t(std::ceil(entries * entrySize / 8.0))).str();
278  }
279  }

References Application::logStream(), MathTools::requiredBits(), and Application::verboseLevel().

Here is the call graph for this function:

◆ generateDecompressor()

virtual void InstructionDictionary::generateDecompressor ( std::ostream &  stream,
TCEString  entityStr 
)
inlinevirtual

Generates the decompressor in VHDL.

Note! The programs must be compressed by compress method before calling this method.

Parameters
streamThe stream to write.

Implements CodeCompressorPlugin.

Definition at line 141 of file InstructionDictionary.cc.

141  {
142 
143  stream << "library ieee;" << endl;
144  stream << "use ieee.std_logic_1164.all;" << endl;
145  stream << "use ieee.std_logic_arith.all;" << endl << endl;
146 
147  stream << "package " << entityStr << "_dict_init is" << endl << endl;
148  stream << indentation(1)
149  << "type std_logic_dict_matrix is array (natural range <>) "
150  << "of std_logic_vector(" << binaryEncoding().width() - 1
151  << " downto 0);" << endl << endl;
152 
153  // write the dictionary contents
154  stream << indentation(1)
155  << "constant dict_init : std_logic_dict_matrix := (" << endl;
156  for (unsigned int i = 0; i < dictionary_.size(); i++) {
157  BitVector instr = MapTools::keyForValue<BitVector>(
158  dictionary_, i);
159  AsciiImageWriter writer(instr, instr.size());
160  stream << indentation(2) << "\"";
161  writer.writeImage(stream);
162  stream << "\"";
163  if (i+1 < dictionary_.size()) {
164  stream << "," << endl;
165  } else {
166  stream << ");" << endl;
167  }
168  }
169  stream << "end " << entityStr << "_dict_init;" << endl << endl;
170 
171  stream << "library ieee;" << endl;
172  stream << "use ieee.std_logic_1164.all;" << endl;
173  stream << "use ieee.std_logic_arith.all;" << endl;
174  stream << "use work." << entityStr << "_globals.all;" << endl;
175  stream << "use work." << entityStr << "_dict_init.all;" << endl;
176  stream << "use work." << entityStr << "_imem_mau.all;" << endl << endl;
177 
178  // write the decompressor entity
179  stream << "entity " << entityStr << "_decompressor is" << endl;
180  stream << indentation(1) << "port (" << endl;
181  stream << indentation(2) << "fetch_en : out std_logic;" << endl;
182  stream << indentation(2) << "lock : in std_logic;" << endl;
183  stream << indentation(2)
184  << "fetchblock : in std_logic_vector("
185  << "IMEMWIDTHINMAUS*IMEMMAUWIDTH-1 downto 0);" << endl;
186  stream << indentation(2)
187  << "instructionword : out std_logic_vector("
188  << "INSTRUCTIONWIDTH-1 downto 0);" << endl;
189  stream << indentation(2) << "glock : out std_logic;" << endl;
190  stream << indentation(2) << "lock_r : in std_logic;" << endl;
191  stream << indentation(2) << "clk : in std_logic;" << endl;
192  stream << indentation(2) << "rstx : in std_logic);" << endl << endl;
193  stream << "end " << entityStr << "_decompressor;" << endl << endl;
194 
195  stream << "architecture simple_dict of " << entityStr
196  << "_decompressor is" << endl << endl;
197 
198  stream << indentation(1)
199  << "subtype dict_index is integer range 0 to "
200  << "dict_init'length-1;" << endl;
201  stream << indentation(1) << "signal dict_line : dict_index;" << endl;
202  stream << indentation(1)
203  << "constant dict : std_logic_dict_matrix("
204  << "0 to dict_init'length-1) := dict_init;" << endl << endl;
205 
206  stream << "begin" << endl << endl;
207  stream << indentation(1) << "glock <= lock;" << endl;
208  stream << indentation(1) << "fetch_en <= not lock_r;" << endl;
209  stream << indentation(1)
210  << "dict_line <= conv_integer(unsigned(fetchblock("
211  << "fetchblock'length-1 downto fetchblock'length-"
213  << ")));" << endl << endl;
214 
215  stream << indentation(1) << "process (dict_line)" << endl;
216  stream << indentation(1) << "begin" << endl;
217  stream << indentation(2) << "instructionword <= dict(dict_line);"
218  << endl;
219  stream << indentation(1) << "end process;" << endl << endl;
220  stream << "end simple_dict;" << endl;
221  }

References MathTools::requiredBits(), and AsciiImageWriter::writeImage().

Here is the call graph for this function:

◆ printDescription()

virtual void InstructionDictionary::printDescription ( std::ostream &  stream)
inlinevirtual

Prints the description of the plugin to the given stream.

Parameters
streamThe stream.

Implements CodeCompressorPlugin.

Definition at line 230 of file InstructionDictionary.cc.

230  {
231  stream << "Generates the program image using instruction-based "
232  << "dictionary compression." << endl << endl
233  << "Warning! This compressor works correctly only when "
234  << "there is one instruction per MAU in the final program "
235  << "image. That is, the minimum addressable unit of the "
236  << "address space should be "
237  << "the same as the width of the compressed instructions or "
238  << "wider. Otherwise jump and call addresses are invalid in "
239  << "the code. This compressor creates the dictionary on the "
240  << "level of whole instruction." << endl << endl
241  << "Parameters accepted:" << endl
242  << "----------------------" << endl
243  << "ensure_programmability" << endl
244  << "If the value is 'yes', instructions that ensure "
245  << "programmability of the processor are added to the "
246  << "dictionary automatically." << endl;
247  }

◆ updateDictionary()

void InstructionDictionary::updateDictionary ( const Program program)
inlineprivate

Creates the dictionary.

Definition at line 300 of file InstructionDictionary.cc.

300  {
301  Instruction* instruction = &program.firstInstruction();
302  while (instruction != &NullInstruction::instance()) {
303  InstructionBitVector* instructionBits = bemInstructionBits(
304  *instruction);
305  addToDictionary(*instructionBits);
306  instruction = &program.nextInstruction(*instruction);
307  }
308  }

References program.

Member Data Documentation

◆ compatibilityProgDone_

bool InstructionDictionary::compatibilityProgDone_
private

Indicates whether the compatibility program is in dictionary.

Definition at line 337 of file InstructionDictionary.cc.

◆ dictionary_

Dictionary InstructionDictionary::dictionary_
private

The dictionary.

Definition at line 335 of file InstructionDictionary.cc.

◆ dictionaryCreated_

bool InstructionDictionary::dictionaryCreated_
private

Indicates whether the whole dictionary has been created.

Definition at line 339 of file InstructionDictionary.cc.


The documentation for this class was generated from the following file:
TTAProgram::Program
Definition: Program.hh:63
TTAProgram::Program::firstInstruction
Instruction & firstInstruction() const
Definition: Program.cc:353
InstructionDictionary::dictionary_
Dictionary dictionary_
The dictionary.
Definition: InstructionDictionary.cc:335
CodeCompressorPlugin::parameterValue
std::string parameterValue(const std::string &paramName) const
Definition: CodeCompressorPlugin.cc:578
CodeCompressorPlugin::bemBits
InstructionBitVector * bemBits(const TTAProgram::Program &program)
Definition: CodeCompressorPlugin.cc:205
BitVector
Definition: BitVector.hh:44
ProgrammabilityValidatorResults
Definition: ProgrammabilityValidatorResults.hh:46
TTAProgram::Program::nextInstruction
Instruction & nextInstruction(const Instruction &) const
Definition: Program.cc:403
TPEF::Binary
Definition: Binary.hh:49
TTAProgram::Instruction
Definition: Instruction.hh:57
Application::verboseLevel
static int verboseLevel()
Definition: Application.hh:176
Application::logStream
static std::ostream & logStream()
Definition: Application.cc:155
CodeCompressorPlugin::addInstruction
void addInstruction(const TTAProgram::Instruction &instruction, InstructionBitVector *bits)
Definition: CodeCompressorPlugin.cc:384
CodeCompressorPlugin::numberOfPrograms
int numberOfPrograms() const
Definition: CodeCompressorPlugin.cc:623
CodeCompressorPlugin::machine
const TTAMachine::Machine & machine() const
Definition: CodeCompressorPlugin.cc:310
TTAProgram::TPEFProgramFactory
Definition: TPEFProgramFactory.hh:87
assert
#define assert(condition)
Definition: Application.hh:86
InvalidData
Definition: Exception.hh:149
InstructionDictionary::createDictionary
void createDictionary()
Definition: InstructionDictionary.cc:258
InstructionDictionary::dictionaryCreated_
bool dictionaryCreated_
Indicates whether the whole dictionary has been created.
Definition: InstructionDictionary.cc:339
CodeCompressorPlugin::bemInstructionBits
InstructionBitVector * bemInstructionBits(const TTAProgram::Instruction &)
Definition: CodeCompressorPlugin.cc:454
BinaryEncoding::width
virtual int width(const TCEString &templateName) const
Definition: BinaryEncoding.cc:768
__func__
#define __func__
Definition: Application.hh:67
InstructionDictionary::updateDictionary
void updateDictionary(const Program &program)
Definition: InstructionDictionary.cc:300
ENSURE_PROGRAMMABILITY
const string ENSURE_PROGRAMMABILITY
Definition: InstructionDictionary.cc:72
CodeCompressorPlugin::startNewProgram
void startNewProgram(const std::string &programName)
Definition: CodeCompressorPlugin.cc:328
ProgrammabilityValidator
Definition: ProgrammabilityValidator.hh:75
MathTools::requiredBits
static int requiredBits(unsigned long int number)
CodeCompressorPlugin::hasParameter
bool hasParameter(const std::string &paramName) const
Definition: CodeCompressorPlugin.cc:558
Exception
Definition: Exception.hh:54
CodeCompressorPlugin::currentProgram
TTAProgram::Program & currentProgram() const
Definition: CodeCompressorPlugin.cc:295
CodeCompressorPlugin::programElement
TPEFMap::const_iterator programElement(int index) const
Definition: CodeCompressorPlugin.cc:634
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
InstructionDictionary::compatibilityProgDone_
bool compatibilityProgDone_
Indicates whether the compatibility program is in dictionary.
Definition: InstructionDictionary.cc:337
MapTools::containsKey
static bool containsKey(const MapType &aMap, const KeyType &aKey)
InstructionDictionary::addInstructions
void addInstructions()
Definition: InstructionDictionary.cc:315
YES
const string YES
Definition: InstructionDictionary.cc:73
AsciiImageWriter
Definition: AsciiImageWriter.hh:45
CodeCompressorPlugin::setAllInstructionsToStartAtBeginningOfMAU
void setAllInstructionsToStartAtBeginningOfMAU()
Definition: CodeCompressorPlugin.cc:440
program
find Finds info of the inner loops in the program
Definition: InnerLoopFinder.cc:80
CodeCompressorPlugin::binaryEncoding
const BinaryEncoding & binaryEncoding() const
Definition: CodeCompressorPlugin.cc:280
InstructionDictionary::addToDictionary
void addToDictionary(const BitVector &instructionBits)
Definition: InstructionDictionary.cc:287
CodeCompressorPlugin::indentation
std::string indentation(int level)
Definition: CodeCompressorPlugin.cc:652
CodeCompressorPlugin::CodeCompressorPlugin
CodeCompressorPlugin()
Definition: CodeCompressorPlugin.cc:104
CodeCompressorPlugin::programBits
InstructionBitVector * programBits() const
Definition: CodeCompressorPlugin.cc:533
CodeCompressorPlugin::setImemWidth
void setImemWidth(int mau, int widthInMaus=1)
Definition: CodeCompressorPlugin.cc:612
InstructionBitVector
Definition: InstructionBitVector.hh:50