OpenASIP  2.0
Public Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
HDLTemplateInstantiator Class Reference

#include <HDLTemplateInstantiator.hh>

Collaboration diagram for HDLTemplateInstantiator:
Collaboration graph

Public Member Functions

 HDLTemplateInstantiator ()
 
 HDLTemplateInstantiator (const TCEString &entityStr)
 
void setEntityString (const TCEString &entityStr)
 
void replacePlaceholder (const std::string &key, const std::string &replacer, bool append=false)
 
void replacePlaceholderFromFile (const std::string &key, const Path &filePath, bool append=false)
 
void instantiateTemplateFile (const std::string &templateFile, const std::string &dstFile)
 

Private Types

typedef TCEString PlaceholderKey
 
typedef TCEString Replacer
 
typedef std::map< PlaceholderKey, ReplacerReplacerMap
 

Private Member Functions

void fillPlaceholders (TCEString &str)
 
TCEString findPlaceholder (const TCEString &str)
 
TCEString getPlaceholderKey (const TCEString &str)
 
TCEString getPlaceholderDefault (const TCEString &str)
 

Private Attributes

TCEString entityStr_
 
ReplacerMap replacers_
 

Static Private Attributes

static const std::string PLACEHOLDERSEPARATOR = ","
 
static const std::string PLACEHOLDERBEGIN
 
static const std::string PLACEHOLDEREND = ">>"
 

Friends

class HDLTemplateInstantiatorTest
 

Detailed Description

Helper class for instantiating HDL template files.

Definition at line 45 of file HDLTemplateInstantiator.hh.

Member Typedef Documentation

◆ PlaceholderKey

Definition at line 65 of file HDLTemplateInstantiator.hh.

◆ Replacer

Definition at line 66 of file HDLTemplateInstantiator.hh.

◆ ReplacerMap

Definition at line 67 of file HDLTemplateInstantiator.hh.

Constructor & Destructor Documentation

◆ HDLTemplateInstantiator() [1/2]

HDLTemplateInstantiator::HDLTemplateInstantiator ( )

Definition at line 42 of file HDLTemplateInstantiator.cc.

◆ HDLTemplateInstantiator() [2/2]

HDLTemplateInstantiator::HDLTemplateInstantiator ( const TCEString entityStr)

Definition at line 45 of file HDLTemplateInstantiator.cc.

46  : entityStr_(entityStr), replacers_() {}

Member Function Documentation

◆ fillPlaceholders()

void HDLTemplateInstantiator::fillPlaceholders ( TCEString str)
private

Fills all placeholder templates with added replace strings by key.

The placeholder markers are in form "<<placeholder,key>>" or "<<placeholder,key,default-value>>". If no replacer is not found for the key the placeholder template is replaced with empty string or default-value if defined.

Parameters
strThe string to search and fill placeholders.

Definition at line 153 of file HDLTemplateInstantiator.cc.

153  {
154  TCEString target(str);
155  TCEString placeholder;
156  while (!(placeholder = findPlaceholder(target)).empty()) {
157  TCEString replacer("");
158  TCEString key = getPlaceholderKey(placeholder);
159  if (replacers_.count(key)) {
160  replacer = replacers_[key];
161  } else {
162  replacer = getPlaceholderDefault(placeholder);
163  }
164  target.replaceString(placeholder, replacer);
165  }
166 
167  // Correct indentation
168  if (!target.empty()) {
169  TCEString identation("");
170  unsigned int i = 0;
171  while (i < target.size() && target.at(i) == ' ') {
172  identation.append(" ");
173  i++;
174  }
175  // Note: only LF newline convention is considered.
176  target.replaceString("\n", "\n" + identation);
177  }
178 
179  str = target;
180 }

References findPlaceholder(), getPlaceholderDefault(), getPlaceholderKey(), replacers_, and TCEString::replaceString().

Referenced by instantiateTemplateFile().

Here is the call graph for this function:

◆ findPlaceholder()

TCEString HDLTemplateInstantiator::findPlaceholder ( const TCEString str)
private

Searches placeholder template over given string.

Parameters
strThe string to search over for placeholder template.
Returns
The placeholder string or empty string if not found.

Definition at line 189 of file HDLTemplateInstantiator.cc.

189  {
190  size_t beginpos = str.find(PLACEHOLDERBEGIN);
191  if (beginpos == std::string::npos) {
192  return "";
193  }
194  size_t endpos =
195  str.find(PLACEHOLDEREND, beginpos + PLACEHOLDERBEGIN.size());
196  if (endpos == std::string::npos) {
197  return "";
198  }
199  TCEString placeholder;
200  placeholder =
201  str.substr(beginpos, endpos - beginpos + PLACEHOLDEREND.size());
202  return placeholder;
203 }

References PLACEHOLDERBEGIN, and PLACEHOLDEREND.

Referenced by fillPlaceholders(), getPlaceholderDefault(), and getPlaceholderKey().

◆ getPlaceholderDefault()

TCEString HDLTemplateInstantiator::getPlaceholderDefault ( const TCEString str)
private

Searches placeholder template over given string and returns its default part.

The placeholder template is in form of <placeholder,keystring,default> and this function return default part.

Definition at line 247 of file HDLTemplateInstantiator.cc.

247  {
248  TCEString placeholder = findPlaceholder(str);
249  if (placeholder.empty()) {
250  return "";
251  }
252  int count = 0;
253  for (size_t offset = placeholder.find(PLACEHOLDERSEPARATOR);
254  offset != std::string::npos;
255  offset = placeholder.find(
256  PLACEHOLDERSEPARATOR, offset + PLACEHOLDERSEPARATOR.size())) {
257  count++;
258  }
259  if (count == 2) {
260  size_t beginpos = placeholder.rfind(PLACEHOLDERSEPARATOR);
261  size_t endpos = placeholder.rfind(PLACEHOLDEREND);
262  if (beginpos == std::string::npos || endpos == std::string::npos) {
263  return "";
264  }
265  assert(endpos >= beginpos);
266  TCEString defaultStr;
267  defaultStr = placeholder.substr(
268  beginpos + PLACEHOLDERSEPARATOR.size(),
269  endpos - beginpos - PLACEHOLDERSEPARATOR.size());
270  return StringTools::trim(defaultStr);
271  } else {
272  return "";
273  }
274 }

References assert, findPlaceholder(), PLACEHOLDEREND, PLACEHOLDERSEPARATOR, and StringTools::trim().

Referenced by fillPlaceholders().

Here is the call graph for this function:

◆ getPlaceholderKey()

TCEString HDLTemplateInstantiator::getPlaceholderKey ( const TCEString str)
private

Searches placeholder template over given string and returns its key.

The placeholder template is in form of <placeholder, keystring, default> and this function return keystring part.

Parameters
strThe string to search over for placeholder template
Returns
Key for the placeholder or empty string if not found.

Definition at line 215 of file HDLTemplateInstantiator.cc.

215  {
216  TCEString placeholder = findPlaceholder(str);
217  if (placeholder.empty()) {
218  return "";
219  }
220  // todo Better field separation
221  size_t beginpos = placeholder.find(PLACEHOLDERBEGIN);
222  size_t midpos = placeholder.rfind(PLACEHOLDERSEPARATOR);
223  size_t endpos = placeholder.find(PLACEHOLDEREND);
224  if (beginpos == std::string::npos || endpos == std::string::npos) {
225  return "";
226  }
227  // Check for optional default value field
228  if ((beginpos + PLACEHOLDERBEGIN.size()) < midpos && midpos < endpos) {
229  endpos = midpos;
230  }
231  assert(endpos >= beginpos);
232  TCEString key = placeholder.substr(
233  beginpos + PLACEHOLDERBEGIN.size(),
234  endpos - beginpos - PLACEHOLDERBEGIN.size());
235  return StringTools::trim(key);
236 }

References assert, findPlaceholder(), PLACEHOLDERBEGIN, PLACEHOLDEREND, PLACEHOLDERSEPARATOR, and StringTools::trim().

Referenced by fillPlaceholders().

Here is the call graph for this function:

◆ instantiateTemplateFile()

void HDLTemplateInstantiator::instantiateTemplateFile ( const std::string &  templateFile,
const std::string &  dstFile 
)

Creates a target HDL file from a HDL template, replacing certain magic strings with certain variable contents.

Currently supported magic strings: ENTITY_STR The entity name string used to make entities unique. Same as the toplevel entity name.

Definition at line 113 of file HDLTemplateInstantiator.cc.

115  {
116  std::ifstream input(templateFile.c_str());
117 
118  if (!input.is_open())
119  throw UnreachableStream(
120  __FILE__, __LINE__, __func__,
121  TCEString("Could not open ") + templateFile + " for reading.");
122 
123  std::ofstream output(dstFile.c_str(), std::ios::trunc);
124 
125  if (!output.is_open())
126  throw UnreachableStream(
127  __FILE__, __LINE__, __func__,
128  TCEString("Could not open ") + dstFile + " for writing.");
129 
130  while (!input.eof()) {
131  char line_buf[1024];
132  input.getline(line_buf, 1024);
133  TCEString line(line_buf);
134  line.replaceString("ENTITY_STR", entityStr_);
135  fillPlaceholders(line);
136  output << line << std::endl;
137  }
138  input.close();
139  output.close();
140 }

References __func__, entityStr_, fillPlaceholders(), and TCEString::replaceString().

Referenced by AlmaIFIntegrator::addAlmaifFiles(), ProGe::BlockSourceCopier::copyProcessorSpecific(), ProGeTestBenchGenerator::copyTestBenchFiles(), ProGeTestBenchGenerator::createProcArchVhdl(), ProGe::RV32MicroCodeGenerator::generateWrapper(), ProGe::BlockSourceCopier::instantiateHDLTemplate(), MemoryGenerator::instantiateTemplate(), ProGe::TestBenchBlock::write(), ProGe::SinglePortByteMaskSSRAMBlock::write(), ProGe::SinglePortSSRAMBlock::write(), and ProGe::LoopBufferBlock::write().

Here is the call graph for this function:

◆ replacePlaceholder()

void HDLTemplateInstantiator::replacePlaceholder ( const std::string &  key,
const std::string &  replacer,
bool  append = false 
)

Adds replace string for the template placeholder by key.

During HDL template instantiation all placeholders by the key will be replaced with given replace string.

Parameters
keyThe key to identify placeholder.
replacerThe string to replace the placeholder.
appendAppends the given string to the existing placeholder filler at the key.
Exceptions
KeyAlreadyExistsIf a placeholder is already filled by the key and append flag is false.

Definition at line 62 of file HDLTemplateInstantiator.cc.

63  {
64  // todo: add rule for key (all uppercase)?
65  if (append) {
66  replacers_[key] += replacer;
67  } else {
68  if (replacers_.count(key)) {
71  "The placeholder \"" + key + "\" is already filled. ");
72  }
73  replacers_.insert(std::pair<PlaceholderKey, Replacer>(key, replacer));
74  }
75 }

References replacers_, and THROW_EXCEPTION.

Referenced by AlmaIFIntegrator::addAlmaifFiles(), DefaultICDecoderGenerator::generate(), ProGe::RV32MicroCodeGenerator::generateOperationLatencyLogic(), ProGe::ProcessorGenerator::generateProcessor(), ProGe::RV32MicroCodeGenerator::generateWrapper(), AlmaIFIntegrator::initAlmaifBlock(), replacePlaceholderFromFile(), ProGe::TestBenchBlock::write(), and ProGe::LoopBufferBlock::write().

◆ replacePlaceholderFromFile()

void HDLTemplateInstantiator::replacePlaceholderFromFile ( const std::string &  key,
const Path filePath,
bool  append = false 
)

Adds replace string from file for the template placeholder by key.

During HDL template instantiation all placeholders by the key will be replaced with given replace string.

Parameters
keyThe key to identify placeholder.
replacerThe string to replace the placeholder.
appendAppends the given string to the existing placeholder filler at the key.

Definition at line 89 of file HDLTemplateInstantiator.cc.

90  {
91  std::string snippetFileName = filePath;
92  std::ifstream snippetfile(snippetFileName.c_str());
93  if (!snippetfile.is_open()) {
95  UnreachableStream, "Could not open snippet file: \"" +
96  snippetFileName + "\" for reading.");
97  }
98  std::string str(
99  (std::istreambuf_iterator<char>(snippetfile)),
100  std::istreambuf_iterator<char>());
101  replacePlaceholder(key, str, append);
102 }

References replacePlaceholder(), and THROW_EXCEPTION.

Referenced by AlmaIFIntegrator::addAlmaifFiles(), AlmaIFIntegrator::connectCoreMemories(), DefaultICDecoderGenerator::generate(), AlmaIFIntegrator::initAlmaifBlock(), and ProGe::LoopBufferBlock::write().

Here is the call graph for this function:

◆ setEntityString()

void HDLTemplateInstantiator::setEntityString ( const TCEString entityStr)
inline

Friends And Related Function Documentation

◆ HDLTemplateInstantiatorTest

friend class HDLTemplateInstantiatorTest
friend

Definition at line 47 of file HDLTemplateInstantiator.hh.

Member Data Documentation

◆ entityStr_

TCEString HDLTemplateInstantiator::entityStr_
private

Definition at line 78 of file HDLTemplateInstantiator.hh.

Referenced by instantiateTemplateFile(), and setEntityString().

◆ PLACEHOLDERBEGIN

const std::string HDLTemplateInstantiator::PLACEHOLDERBEGIN
staticprivate
Initial value:
=
"<<placeholder" + PLACEHOLDERSEPARATOR

Definition at line 75 of file HDLTemplateInstantiator.hh.

Referenced by findPlaceholder(), and getPlaceholderKey().

◆ PLACEHOLDEREND

const std::string HDLTemplateInstantiator::PLACEHOLDEREND = ">>"
staticprivate

◆ PLACEHOLDERSEPARATOR

const std::string HDLTemplateInstantiator::PLACEHOLDERSEPARATOR = ","
staticprivate

Definition at line 74 of file HDLTemplateInstantiator.hh.

Referenced by getPlaceholderDefault(), and getPlaceholderKey().

◆ replacers_

ReplacerMap HDLTemplateInstantiator::replacers_
private

Definition at line 79 of file HDLTemplateInstantiator.hh.

Referenced by fillPlaceholders(), and replacePlaceholder().


The documentation for this class was generated from the following files:
KeyAlreadyExists
Definition: Exception.hh:268
UnreachableStream
Definition: Exception.hh:171
HDLTemplateInstantiator::findPlaceholder
TCEString findPlaceholder(const TCEString &str)
Definition: HDLTemplateInstantiator.cc:189
HDLTemplateInstantiator::HDLTemplateInstantiator
HDLTemplateInstantiator()
Definition: HDLTemplateInstantiator.cc:42
HDLTemplateInstantiator::getPlaceholderDefault
TCEString getPlaceholderDefault(const TCEString &str)
Definition: HDLTemplateInstantiator.cc:247
assert
#define assert(condition)
Definition: Application.hh:86
HDLTemplateInstantiator::replacers_
ReplacerMap replacers_
Definition: HDLTemplateInstantiator.hh:79
HDLTemplateInstantiator::fillPlaceholders
void fillPlaceholders(TCEString &str)
Definition: HDLTemplateInstantiator.cc:153
THROW_EXCEPTION
#define THROW_EXCEPTION(exceptionType, message)
Exception wrapper macro that automatically includes file name, line number and function name where th...
Definition: Exception.hh:39
__func__
#define __func__
Definition: Application.hh:67
HDLTemplateInstantiator::getPlaceholderKey
TCEString getPlaceholderKey(const TCEString &str)
Definition: HDLTemplateInstantiator.cc:215
StringTools::trim
static std::string trim(const std::string &source)
Definition: StringTools.cc:55
HDLTemplateInstantiator::replacePlaceholder
void replacePlaceholder(const std::string &key, const std::string &replacer, bool append=false)
Definition: HDLTemplateInstantiator.cc:62
HDLTemplateInstantiator::entityStr_
TCEString entityStr_
Definition: HDLTemplateInstantiator.hh:78
TCEString
Definition: TCEString.hh:53
HDLTemplateInstantiator::PLACEHOLDEREND
static const std::string PLACEHOLDEREND
Definition: HDLTemplateInstantiator.hh:76
HDLTemplateInstantiator::PLACEHOLDERSEPARATOR
static const std::string PLACEHOLDERSEPARATOR
Definition: HDLTemplateInstantiator.hh:74
HDLTemplateInstantiator::PLACEHOLDERBEGIN
static const std::string PLACEHOLDERBEGIN
Definition: HDLTemplateInstantiator.hh:75