39 #include <boost/algorithm/string.hpp>
40 #include <boost/algorithm/string/trim.hpp>
68 const char *orig = sourceCode.c_str();
72 parse_info<const char*> result =
86 for (
unsigned int i = 0; i < skip.
strippedParts.size(); i++) {
90 skip.
strippedParts[i].first -=
reinterpret_cast<long int>(orig);
100 unsigned int charsToPos = 0;
101 for (
unsigned int i = 0; i < result.length ; i++) {
103 for (
unsigned int j = 0; j < skip.
strippedParts.size(); j++) {
121 for (
unsigned int i = 0; i < charsToPos; i++) {
122 if (sourceCode.at(i) ==
'\n') {
128 int lineStart = sourceCode.rfind(
';', charsToPos);
129 int lineEnd = sourceCode.find(
';', charsToPos);
131 std::string errorLine =
132 boost::algorithm::trim_copy(
133 sourceCode.substr(lineStart + 1, lineEnd - lineStart));
140 std::string message =
"Parsing failed at the position " +
168 #define DEBUG_CODE(x)
175 std::map<VariableKey, std::string>& varBindings,
176 std::set<const OperationDAGNode*>& alreadyHandled,
178 std::set<const OperationDAGNode*>& currentlyHandling,
179 std::map<std::string, std::string>* opReplace,
180 std::vector<std::string>* varReplacements) {
182 static int tmpCounter = 0;
184 DEBUG_CODE(
static std::string recursion_level =
""; recursion_level +=
"----";);
188 if (currentlyHandling.find(&node) != currentlyHandling.end()) {
189 DEBUG_CODE(recursion_level = recursion_level.erase(0,4););
193 if (alreadyHandled.find(&node) == alreadyHandled.end()) {
195 currentlyHandling.insert(&node);
202 if (constNode != NULL) {
203 currentlyHandling.erase(constNode);
204 alreadyHandled.insert(constNode);
206 varBindings[termKey] = constNode->
toString();
208 }
else if (termNode != NULL) {
214 std::string ioName =
"";
215 if (!varReplacements ||
216 (termNode->
operandIndex() - 1) > (
int)varReplacements->size()) {
220 ioName = varReplacements->at(termNode->
operandIndex() - 1);
226 <<
"**** Started handling term node " <<
long(&node)
227 <<
":" << currentStepsToRoot <<
":" + ioName +
"\n";);
229 const bool inputTerminalNode = dag.
inDegree(*termNode) == 0;
230 if (inputTerminalNode) {
232 TCEString tmpName =
"inTmp_"; tmpName << tmpCounter++;
233 retVal +=
"SimValue " + tmpName +
";\n";
234 retVal += tmpName +
" = " +
239 varBindings[termKey] = tmpName;
241 <<
"Added input terminal: " + ioName +
"\n";);
251 <<
"** Read input of terminal " << ioName <<
"\n";);
254 retVal, dag, tail, varBindings, alreadyHandled,
255 tempVarCount, currentlyHandling, opReplace, varReplacements);
257 <<
"** Ready reading input of terminal " << ioName <<
"\n";);
261 retVal += ioName +
" = " + varBindings[srcKey] +
";\n";
265 <<
"added terminal " <<
long (&node)
266 <<
"to already handled nodes\n";);
268 currentlyHandling.erase(termNode);
269 alreadyHandled.insert(termNode);
279 if (opNode == NULL) {
282 "Must be either OperationNode or Terminal Node. "
283 "Got: %s.") % node.
toString()).str());
289 <<
"Started handling opnode " <<
long(&node) <<
":"
290 << currentStepsToRoot <<
": " + refOp.
name() +
"\n";);
295 std::vector<std::string> operandVec(
303 std::vector<bool> isConstOperand(
307 for (
int i = 0; i < dag.
inDegree(node); i++) {
332 <<
"** Read input op: " << refOp.
name() <<
":" << i <<
"\n";);
334 if (!
writeNode(retVal, dag, tail, varBindings,
335 alreadyHandled, tempVarCount, currentlyHandling, opReplace, varReplacements)) {
338 <<
"Input can't be read yet: " << refOp.
name() <<
":" << i <<
"\n";);
340 currentlyHandling.erase(&node);
342 DEBUG_CODE(recursion_level = recursion_level.erase(0,4););
347 <<
"** Ready reading input of op: " << refOp.
name() <<
":" << i <<
"\n";);
350 if (varBindings[sourceKey].empty()) {
354 "DAG cannot be written to OSAL code, because illegal output edge. "
358 operandVec[edge.
dstOperand()-1] = varBindings[sourceKey];
361 <<
"Added operand: " << varBindings[sourceKey]
362 <<
" key: (" << sourceKey.first <<
":" << sourceKey.second <<
")"
363 <<
" to " << refOp.
name() <<
" paramer vector index: "
372 std::string tempVarName =
"tmp" +
374 retVal =
"SimValue " + tempVarName +
";\n" + retVal;
375 varBindings[operandKey] = tempVarName;
378 <<
"Created variable: " << tempVarName
379 <<
" key: (" << operandKey.first <<
":" << operandKey.second <<
")"
380 <<
" for " << refOp.
name() <<
":" << opNumber << std::endl;);
383 operandVec[opNumber-1] = varBindings[operandKey];
385 <<
"Added operand: " << varBindings[operandKey]
386 <<
" to " << refOp.
name() <<
" paramer vector index: "
387 << opNumber -1 << std::endl;);
391 DEBUG_CODE(std::cerr << recursion_level <<
"added node "
392 <<
long (&node) <<
"to already handled nodes\n" ;);
394 currentlyHandling.erase(&node);
395 alreadyHandled.insert(&node);
398 if (opReplace != NULL &&
399 opReplace->find(refOp.
name()) != opReplace->end()) {
400 std::string simOp = (*opReplace)[refOp.
name()];
402 std::string rightSide =
"";
403 if (isConstOperand[0]) {
405 rightSide = operandVec[0];
412 rightSide = simOp + rightSide;
415 if (isConstOperand[i]) {
417 " " + simOp +
" " + operandVec[i];
429 "Cant write simulation replacement code only"
430 "for singleoutput operations.");
434 " = " + rightSide +
";\n";
437 retVal +=
"{ EXEC_OPERATION(" + std::string(refOp.
name());
438 for (
unsigned int i = 0; i < operandVec.size(); i++) {
439 retVal +=
", " + operandVec[i];
444 <<
"Added EXEC: " + refOp.
name() << std::endl;);
448 for (
int i = 0; i < dag.
outDegree(node); i++) {
454 if (dag.
stepsToRoot(headNode) == currentStepsToRoot + 1) {
456 <<
"** Goto output op: " << refOp.
name()
457 <<
":" << i <<
"\n";);
459 writeNode(retVal, dag, headNode, varBindings,
460 alreadyHandled, tempVarCount, currentlyHandling, opReplace, varReplacements);
464 <<
"** Ready going output op: " << refOp.
name()
465 <<
":" << i <<
"\n";);
471 DEBUG_CODE(recursion_level = recursion_level.erase(0,4););
484 return var +
".sIntWordValue()";
486 return var +
".uIntWordValue()";
488 return var +
".floatWordValue()";
490 return var +
".halfFloatWordValue()";
492 return var +
".doubleWordValue()";
494 return var +
".uLongWordValue()";
496 return var +
".sLongWordValue()";
511 std::map<VariableKey, std::string> varBindings;
512 std::set<const OperationDAGNode*> alreadyHandled;
513 std::set<const OperationDAGNode*> currentlyHandling;
514 int tempVarCount = 0;
522 alreadyHandled, tempVarCount, currentlyHandling);
536 std::vector<std::string>* varReplacements) {
538 std::map<VariableKey, std::string> varBindings;
539 std::set<const OperationDAGNode*> alreadyHandled;
540 std::set<const OperationDAGNode*> currentlyHandling;
541 int tempVarCount = 0;
542 std::map<std::string, std::string> opReplacements;
544 opReplacements[
"ADD"] =
"+";
545 opReplacements[
"SUB"] =
"-";
546 opReplacements[
"MUL"] =
"*";
547 opReplacements[
"DIV"] =
"/";
548 opReplacements[
"DIVU"] =
"/";
549 opReplacements[
"EQ"] =
"==";
550 opReplacements[
"GT"] =
">";
551 opReplacements[
"GTU"] =
">";
552 opReplacements[
"SHL"] =
"<<";
553 opReplacements[
"SHR"] =
">>";
554 opReplacements[
"SHRU"] =
">>";
555 opReplacements[
"AND"] =
"&";
556 opReplacements[
"IOR"] =
"|";
557 opReplacements[
"XOR"] =
"^";
558 opReplacements[
"NEG"] =
"-";
559 opReplacements[
"NEGF"] =
"-";
560 opReplacements[
"ADDF"] =
"+";
561 opReplacements[
"SUBF"] =
"-";
562 opReplacements[
"MULF"] =
"*";
563 opReplacements[
"DIVF"] =
"/";
564 opReplacements[
"EQF"] =
"==";
565 opReplacements[
"GTF"] =
">";
566 opReplacements[
"CFI"] =
"(UIntWord)";
567 opReplacements[
"CIF"] =
"(FloatWord)";
568 opReplacements[
"CFD"] =
"(DoubleWord)";
569 opReplacements[
"CDF"] =
"(FloatWord)";
570 opReplacements[
"MOD"] =
"%";
571 opReplacements[
"MODU"] =
"%";
572 opReplacements[
"MULH"] =
"*";
574 opReplacements[
"ADD64"] =
"+";
575 opReplacements[
"SUB64"] =
"-";
576 opReplacements[
"MUL64"] =
"*";
577 opReplacements[
"DIV64"] =
"/";
578 opReplacements[
"DIVU64"] =
"/";
579 opReplacements[
"EQ64"] =
"==";
580 opReplacements[
"GT64"] =
">";
581 opReplacements[
"GTU64"] =
">";
582 opReplacements[
"SHL64"] =
"<<";
583 opReplacements[
"SHR64"] =
">>";
584 opReplacements[
"SHRU64"] =
">>";
585 opReplacements[
"AND64"] =
"&";
586 opReplacements[
"IOR64"] =
"|";
587 opReplacements[
"XOR64"] =
"^";
588 opReplacements[
"NEG64"] =
"-";
589 opReplacements[
"NEGF64"] =
"-";
590 opReplacements[
"ADDF64"] =
"+";
591 opReplacements[
"SUBF64"] =
"-";
592 opReplacements[
"MULF64"] =
"*";
593 opReplacements[
"DIVF64"] =
"/";
594 opReplacements[
"EQF64"] =
"==";
595 opReplacements[
"GTF64"] =
">";
596 opReplacements[
"CFI64"] =
"(UIntWord)";
597 opReplacements[
"CIF64"] =
"(FloatWord)";
598 opReplacements[
"CFD64"] =
"(DoubleWord)";
599 opReplacements[
"CDF64"] =
"(FloatWord)";
600 opReplacements[
"MOD64"] =
"%";
601 opReplacements[
"MODU64"] =
"%";
607 for (
int i = 0; i < dag.
nodeCount(); i++) {
609 if (alreadyHandled.find(node) == alreadyHandled.end()) {
610 writeNode(retVal, dag, *node, varBindings,
611 alreadyHandled, tempVarCount, currentlyHandling,
612 &opReplacements, varReplacements);