35 #ifndef TCEASM_ASSEMBLER_PARSER_HH
36 #define TCEASM_ASSEMBLER_PARSER_HH
38 #include <boost/version.hpp>
42 #if BOOST_VERSION >= 103800
44 #include <boost/spirit/include/classic.hpp>
45 #include <boost/spirit/include/classic_push_back_actor.hpp>
46 #include <boost/spirit/include/classic_clear_actor.hpp>
47 #include <boost/spirit/include/classic_assign_actor.hpp>
48 #include <boost/spirit/include/classic_increment_actor.hpp>
49 #include <boost/spirit/include/classic_assign_key_actor.hpp>
50 #include <boost/spirit/include/classic_insert_at_actor.hpp>
51 #include <boost/spirit/include/classic_file_iterator.hpp>
53 using namespace boost::spirit::classic;
57 #include <boost/spirit.hpp>
58 #include <boost/spirit/actor/push_back_actor.hpp>
59 #include <boost/spirit/actor/clear_actor.hpp>
60 #include <boost/spirit/actor/assign_actor.hpp>
61 #include <boost/spirit/actor/increment_actor.hpp>
62 #include <boost/spirit/actor/assign_key_actor.hpp>
63 #include <boost/spirit/actor/insert_at_actor.hpp>
64 #include <boost/spirit/iterator/file_iterator.hpp>
66 using namespace boost::spirit;
91 ParserTemp() : lastNumber(0), lineNumber(1), codeLineCount(0) {}
199 void operator() (
const char*,
const char*)
const;
213 void operator() (
const char*,
const char*)
const;
228 void operator() (
const char*,
const char*)
const;
243 void operator() (
const char*,
const char*)
const;
258 void operator() (
const char*,
const char*)
const;
272 std::string& name,
UValue& value);
274 void operator() (
const char*,
const char*)
const;
290 std::string& name,
UValue& value);
292 void operator() (
const char*,
const char*)
const;
307 void operator() (
const char* start,
const char* end)
const;
347 bool codeLinesOnly =
false);
349 template <
typename ScannerT>
369 comment = str_p(
"#") >>
370 (*(anychar_p -
'\n') >> ch_p(
'\n'))
371 [increment_a(
self.parserTemp_.lineNumber)]
382 [increment_a(
self.parserTemp_.lineNumber)]
390 binNumber = str_p(
"0b") >> bin_p
391 [assign_a(
self.parserTemp_.lastNumber)]
392 [assign_a(
self.parserTemp_.isLastSigned,
MY_FALSE)];
394 hexNumber = str_p(
"0x") >> hex_p
395 [assign_a(
self.parserTemp_.lastNumber)]
396 [assign_a(
self.parserTemp_.isLastSigned,
MY_FALSE)];
399 [assign_a(
self.parserTemp_.lastNumber)]
400 [assign_a(
self.parserTemp_.isLastSigned,
MY_FALSE)];
403 [assign_a(
self.parserTemp_.lastNumber)]
404 [assign_a(
self.parserTemp_.isLastSigned,
MY_TRUE)];
409 uLiteral = binNumber|hexNumber|uNumber;
424 [assign_a(
self.parserTemp_.sLiteral,
"")] >>
429 (anychar_p - ch_p(
'\"')) ) >>
433 "String literals are not supported yet!\n").c_str())]
439 literal = uLiteral|sNumber|sLiteral;
446 name = (alpha_p | chset_p(
"_")) >> *(alnum_p | chset_p(
"_"));
451 index = uNumber[assign_a(
self.parserTemp_.index,
452 self.parserTemp_.lastNumber)];
459 [assign_a(
self.parserTemp_.busTerm.prev,
MY_TRUE)]|
461 [assign_a(
self.parserTemp_.busTerm.prev,
MY_FALSE)]) >>
'}';
468 [assign_a(
self.parserTemp_.fuTerm.part3Used,
MY_FALSE)] >>
471 [assign_a(
self.parserTemp_.fuTerm.part1)] >>
'.' >>
474 [assign_a(
self.parserTemp_.fuTerm.part2)] >>
476 !(
'.' >> name[assign_a(
self.parserTemp_.fuTerm.part3)])
477 [assign_a(
self.parserTemp_.fuTerm.part3Used,
MY_TRUE)];
485 [assign_a(
self.parserTemp_.indexTerm.part2Used,
MY_FALSE)] >>
488 [assign_a(
self.parserTemp_.indexTerm.part1)] >>
'.' >>
490 !(name[assign_a(
self.parserTemp_.indexTerm.part2)] >>
'.')
491 [assign_a(
self.parserTemp_.indexTerm.part2Used,
MY_TRUE)] >>
494 [assign_a(
self.parserTemp_.indexTerm.index,
495 self.parserTemp_.index)];
502 [assign_a(
self.parserTemp_.regTerm.indexTerm,
503 self.parserTemp_.indexTerm)]
504 [assign_a(
self.parserTemp_.regTerm.type,
INDEX_TERM)]|
507 [assign_a(
self.parserTemp_.regTerm.fuTerm,
508 self.parserTemp_.fuTerm)]
512 [assign_a(
self.parserTemp_.regTerm.busTerm,
513 self.parserTemp_.busTerm)]
514 [assign_a(
self.parserTemp_.regTerm.type,
BUS_TERM)];
521 sign_p[assign_a(
self.parserTemp_.expression.isMinus)] >>
526 [assign_a(
self.parserTemp_.expression.offset,
527 self.parserTemp_.lastNumber)];
534 [assign_a(
self.parserTemp_.expression.label)] >>
536 !(*my_space >> offset)
537 [assign_a(
self.parserTemp_.expression.hasOffset,
MY_TRUE)];
544 [assign_a(
self.parserTemp_.expression.hasValue,
MY_FALSE)]
545 [assign_a(
self.parserTemp_.expression.hasOffset,
MY_FALSE)] >>
549 !(*my_space >>
'=' >> *my_space >> literal)
550 [assign_a(
self.parserTemp_.expression.value,
551 self.parserTemp_.lastNumber)]
552 [assign_a(
self.parserTemp_.expression.hasValue,
MY_TRUE)];
557 literalOrExpression =
559 [assign_a(
self.parserTemp_.litOrExpr.value,
560 self.parserTemp_.lastNumber)]
561 [assign_a(
self.parserTemp_.litOrExpr.isExpression,
MY_FALSE)]
562 [assign_a(
self.parserTemp_.litOrExpr.isSigned,
563 self.parserTemp_.isLastSigned)]|
566 [assign_a(
self.parserTemp_.litOrExpr.expression,
567 self.parserTemp_.expression)]
568 [assign_a(
self.parserTemp_.litOrExpr.isExpression,
MY_TRUE)];
575 [assign_a(
self.parserTemp_.move.source.immTerm,
576 self.parserTemp_.litOrExpr)];
583 [assign_a(
self.parserTemp_.move.destination,
584 self.parserTemp_.regTerm)];
591 [assign_a(
self.parserTemp_.move.source.regTerm,
592 self.parserTemp_.regTerm)]
593 [assign_a(
self.parserTemp_.move.source.isRegister,
MY_TRUE)]|
596 [assign_a(
self.parserTemp_.move.source.isRegister,
MY_FALSE)];
602 ch_p(
'?')[assign_a(
self.parserTemp_.move.guard.isInverted,
605 ch_p(
'!')[assign_a(
self.parserTemp_.move.guard.isInverted,
612 invertFlag >> *my_space >> regTerm
613 [assign_a(
self.parserTemp_.move.guard.regTerm,
614 self.parserTemp_.regTerm)];
622 [assign_a(
self.parserTemp_.initDataField.width,
MY_ZERO)] >>
624 !(uNumber >> *my_space >>
':' >> *my_space)
625 [assign_a(
self.parserTemp_.initDataField.width,
626 self.parserTemp_.lastNumber)] >>
629 [assign_a(
self.parserTemp_.initDataField.litOrExpr,
630 self.parserTemp_.litOrExpr)];
640 [clear_a(
self.parserTemp_.annotation.payload)] >>
642 (ch_p(
'{') >> *my_space >>
643 hexNumber[assign_a(
self.parserTemp_.annotation.id,
644 self.parserTemp_.lastNumber)] >>
647 initDataField[push_back_a(
self.parserTemp_.annotation.payload,
648 self.parserTemp_.initDataField)]) >>
650 *my_space >> ch_p(
'}'));
657 [assign_a(
self.parserTemp_.move.guard.isGuarded,
MY_FALSE)]
658 [clear_a(
self.parserTemp_.move.annotationes)] >>
660 !(guard >> *my_space)
661 [assign_a(
self.parserTemp_.move.guard.isGuarded,
MY_TRUE)] >>
664 *my_space >> str_p(
"->") >> *my_space >>
668 *(*my_space >> annotation[push_back_a(
669 self.parserTemp_.move.annotationes,
670 self.parserTemp_.annotation)]);
678 [clear_a(
self.parserTemp_.move.annotationes)] >>
680 ch_p(
'[') >> *my_space >>
683 *my_space >>
'=' >> *my_space >>
687 [assign_a(
self.parserTemp_.move.source.immTerm,
688 self.parserTemp_.litOrExpr)]
689 [assign_a(
self.parserTemp_.move.source.isRegister,
692 *my_space >> ch_p(
']') >>
694 *(*my_space >> annotation[push_back_a(
695 self.parserTemp_.move.annotationes,
696 self.parserTemp_.annotation)]);
700 emptyMove = str_p(
"...");
707 [assign_a(
self.parserTemp_.move.type,
EMPTY_MOVE)] |
717 [assign_a(
self.parserTemp_.move.isBegin,
MY_TRUE)]
718 [assign_a(
self.parserTemp_.move.asmLineNumber,
719 self.parserTemp_.lineNumber)]
721 self.codeSectionCreator_,
self.parserTemp_.move)] >>
723 *(*my_space >>
',' >> *my_space >>
725 [assign_a(
self.parserTemp_.move.isBegin,
MY_FALSE)]
726 [assign_a(
self.parserTemp_.move.asmLineNumber,
727 self.parserTemp_.lineNumber)]
729 self.codeSectionCreator_,
self.parserTemp_.move)]);
750 (name[assign_a(
self.parserTemp_.label)] >> *my_space >>
':') ;
760 *
self.resourceManager_.codeAddressSpace(),
761 self.parserTemp_.label,
762 self.parserTemp_.codeLineCount)];
768 ch_p(
':') >> *my_space >>
770 ((str_p(
"procedure") >> +my_space >>
771 (name[assign_a(
self.parserTemp_.directive)])
775 self.parserTemp_.directive,
776 self.parserTemp_.codeLineCount)]) |
778 (str_p(
"global") >> +my_space >>
785 *(codeLabel >> *my_space) >>
788 [increment_a(
self.parserTemp_.codeLineCount)] >>
791 *(*my_space >> immediateSpec)
792 [assign_a(
self.parserTemp_.move.type,
LONG_IMM)]
794 self.codeSectionCreator_,
self.parserTemp_.move)];
800 *((codeLine|directive) >>
801 *my_space >>
';' >> *my_space);
808 [assign_a(
self.parserTemp_.lastNumber,
MY_ZERO)] >>
811 !(+my_space >> literal) >> *my_space >>
';';
819 self.codeSectionCreator_,
self.parserTemp_.lastNumber)] >>
821 +my_space >> codeLines;
829 [push_back_a(
self.parserTemp_.dataLine.initData,
830 self.parserTemp_.initDataField)]);
837 [push_back_a(
self.parserTemp_.dataLine.labels,
838 self.parserTemp_.label)];
844 *(dataLabel >> *my_space) >>
846 str_p(
"DA") >> +my_space >>
848 uNumber[assign_a(
self.parserTemp_.dataLine.width,
849 self.parserTemp_.lastNumber)] >>
858 [assign_a(
self.parserTemp_.dataLine.asmLineNumber,
859 self.parserTemp_.lineNumber)]
861 self.dataSectionCreator_,
self.parserTemp_.dataLine)]
862 [clear_a(
self.parserTemp_.dataLine.initData)]
863 [clear_a(
self.parserTemp_.dataLine.labels)] |
867 *my_space >>
';' >> *my_space);
876 name[assign_a(
self.parserTemp_.dataLine.dataSpace)] >>
878 !(+my_space >> literal)
880 self.parserTemp_.lastNumber)] >>
889 +my_space >> dataLines;
901 (*(*my_space >> area) >> *my_space);
906 comment, my_space, port, name,
program, area, dataArea,
908 uLiteral, literal, codeArea, codeHeader, codeLines, codeLine,
910 emptyInstruction,
moves, move, emptyMove, directive,
911 immediateSpec,
transport, annotation, initDataField, guard,
912 invertFlag, source, destination, dataLabel,
uNumber, sNumber,
913 hexNumber, binNumber,
index, fuTerm,
914 indexTerm,
sLiteral, backslashedChars, regTerm, offset,
920 rule<ScannerT>
const&
start()
const {
930 bool compile(
const std::string& asmCode)
const;
932 void finalize(
bool littleEndian)
const;
960 bool codeLinesOnly_ =
false;