2 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
3 * See included license file for license details.
6 /* write header with token defines */
9 /* make it reentrant */
12 /* put more info in error messages */
15 /* enable location processing */
19 #include "ElftosbLexer.h"
20 #include "ElftosbAST.h"
23 #include "format_string.h"
25 #include "ConversionController.h"
27 using namespace elftosb;
29 //! Our special location type.
30 #define YYLTYPE token_loc_t
32 // this indicates that we're using our own type. it should be unset automatically
33 // but that's not working for some reason with the .hpp file.
34 #if defined(YYLTYPE_IS_TRIVIAL)
35 #undef YYLTYPE_IS_TRIVIAL
36 #define YYLTYPE_IS_TRIVIAL 0
39 //! Default location action
40 #define YYLLOC_DEFAULT(Current, Rhs, N) \
44 (Current).m_firstLine = YYRHSLOC(Rhs, 1).m_firstLine; \
45 (Current).m_lastLine = YYRHSLOC(Rhs, N).m_lastLine; \
49 (Current).m_firstLine = (Current).m_lastLine = YYRHSLOC(Rhs, 0).m_lastLine; \
53 //! Forward declaration of yylex().
54 static int yylex(YYSTYPE * lvalp, YYLTYPE * yylloc, ElftosbLexer * lexer);
56 // Forward declaration of error handling function.
57 static void yyerror(YYLTYPE * yylloc, ElftosbLexer * lexer, CommandFileASTNode ** resultAST, const char * error);
64 elftosb::SizedIntegerValue * m_int;
67 elftosb::ASTNode * m_ast; // must use full name here because this is put into *.tab.hpp
70 /* extra parameters for the parser and lexer */
71 %parse-param {ElftosbLexer * lexer}
72 %parse-param {CommandFileASTNode ** resultAST}
73 %lex-param {ElftosbLexer * lexer}
75 /* token definitions */
76 %token <m_str> TOK_IDENT "identifier"
77 %token <m_str> TOK_STRING_LITERAL "string"
78 %token <m_int> TOK_INT_LITERAL "integer"
79 %token <m_str> TOK_SECTION_NAME "section name"
80 %token <m_str> TOK_SOURCE_NAME "source name"
81 %token <m_blob> TOK_BLOB "binary object"
94 %token TOK_DOT_DOT ".."
107 %token TOK_POWER "**"
108 %token TOK_LSHIFT "<<"
109 %token TOK_RSHIFT ">>"
110 %token <m_int> TOK_INT_SIZE "integer size"
111 %token TOK_OPTIONS "options"
112 %token TOK_CONSTANTS "constants"
113 %token TOK_SOURCES "sources"
114 %token TOK_FILTERS "filters"
115 %token TOK_SECTION "section"
116 %token TOK_EXTERN "extern"
117 %token TOK_FROM "from"
119 %token TOK_LOAD "load"
120 %token TOK_JUMP "jump"
121 %token TOK_CALL "call"
122 %token TOK_MODE "mode"
124 %token TOK_ELSE "else"
125 %token TOK_DEFINED "defined"
126 %token TOK_INFO "info"
127 %token TOK_WARNING "warning"
128 %token TOK_ERROR "error"
129 %token TOK_SIZEOF "sizeof"
134 /* operator precedence */
136 %left '>' '<' ">=" "<=" "==" "!="
147 /* nonterminal types - most nonterminal symbols are subclasses of ASTNode */
148 %type <m_ast> command_file blocks_list pre_section_block options_block const_def_list const_def_list_elem
149 %type <m_ast> const_def const_expr expr int_const_expr unary_expr int_value constants_block
150 %type <m_ast> sources_block source_def_list source_def_list_elem source_def
151 %type <m_ast> section_defs section_def section_contents full_stmt_list full_stmt_list_elem
152 %type <m_ast> basic_stmt load_stmt call_stmt from_stmt load_data load_target call_target
153 %type <m_ast> address_or_range load_target_opt call_arg_opt basic_stmt_list basic_stmt_list_elem
154 %type <m_ast> source_attr_list source_attr_list_elem source_attrs_opt
155 %type <m_ast> section_list section_list_elem symbol_ref mode_stmt
156 %type <m_ast> section_options_opt source_attr_list_opt
157 %type <m_ast> if_stmt else_opt message_stmt
158 %type <m_ast> bool_expr ivt_def assignment_list_opt
160 %type <m_num> call_or_jump dcd_opt
162 %destructor { delete $$; } TOK_IDENT TOK_STRING_LITERAL TOK_SECTION_NAME TOK_SOURCE_NAME TOK_BLOB TOK_INT_SIZE TOK_INT_LITERAL
166 command_file : blocks_list section_defs
168 CommandFileASTNode * commandFile = new CommandFileASTNode();
169 commandFile->setBlocks(dynamic_cast<ListASTNode*>($1));
170 commandFile->setSections(dynamic_cast<ListASTNode*>($2));
171 commandFile->setLocation(@1, @2);
172 *resultAST = commandFile;
176 blocks_list : pre_section_block
178 ListASTNode * list = new ListASTNode();
179 list->appendNode($1);
182 | blocks_list pre_section_block
184 dynamic_cast<ListASTNode*>($1)->appendNode($2);
190 : options_block { $$ = $1; }
191 | constants_block { $$ = $1; }
192 | sources_block { $$ = $1; }
195 options_block : "options" '{' const_def_list '}'
197 $$ = new OptionsBlockASTNode(dynamic_cast<ListASTNode *>($3));
201 constants_block : "constants" '{' const_def_list '}'
203 $$ = new ConstantsBlockASTNode(dynamic_cast<ListASTNode *>($3));
207 const_def_list : const_def_list_elem
209 ListASTNode * list = new ListASTNode();
210 list->appendNode($1);
213 | const_def_list const_def_list_elem
215 dynamic_cast<ListASTNode*>($1)->appendNode($2);
220 const_def_list_elem : const_def ';' { $$ = $1; }
221 | /* empty */ { $$ = NULL; }
224 const_def : TOK_IDENT '=' const_expr
226 $$ = new AssignmentASTNode($1, $3);
227 $$->setLocation(@1, @3);
231 sources_block : "sources" '{' source_def_list '}'
233 $$ = new SourcesBlockASTNode(dynamic_cast<ListASTNode *>($3));
237 source_def_list : source_def_list_elem
239 ListASTNode * list = new ListASTNode();
240 list->appendNode($1);
243 | source_def_list source_def_list_elem
245 dynamic_cast<ListASTNode*>($1)->appendNode($2);
251 : source_def source_attrs_opt ';'
253 // tell the lexer that this is the name of a source file
254 SourceDefASTNode * node = dynamic_cast<SourceDefASTNode*>($1);
257 node->setAttributes(dynamic_cast<ListASTNode*>($2));
259 node->setLocation(node->getLocation(), @3);
260 lexer->addSourceName(node->getName());
263 | /* empty */ { $$ = NULL; }
266 source_def : TOK_IDENT '=' TOK_STRING_LITERAL
268 $$ = new PathSourceDefASTNode($1, $3);
269 $$->setLocation(@1, @3);
271 | TOK_IDENT '=' "extern" '(' int_const_expr ')'
273 $$ = new ExternSourceDefASTNode($1, dynamic_cast<ExprASTNode*>($5));
274 $$->setLocation(@1, @6);
279 : '(' source_attr_list ')' { $$ = $2; }
280 | /* empty */ { $$ = NULL; }
284 : source_attr_list_elem
286 ListASTNode * list = new ListASTNode();
287 list->appendNode($1);
290 | source_attr_list ',' source_attr_list_elem
292 dynamic_cast<ListASTNode*>($1)->appendNode($3);
297 source_attr_list_elem
298 : TOK_IDENT '=' const_expr
300 $$ = new AssignmentASTNode($1, $3);
301 $$->setLocation(@1, @3);
305 section_defs : section_def
307 ListASTNode * list = new ListASTNode();
308 list->appendNode($1);
311 | section_defs section_def
313 dynamic_cast<ListASTNode*>($1)->appendNode($2);
318 section_def : "section" '(' int_const_expr section_options_opt ')' section_contents
320 SectionContentsASTNode * sectionNode = dynamic_cast<SectionContentsASTNode*>($6);
323 ExprASTNode * exprNode = dynamic_cast<ExprASTNode*>($3);
324 sectionNode->setSectionNumberExpr(exprNode);
325 sectionNode->setOptions(dynamic_cast<ListASTNode*>($4));
326 sectionNode->setLocation(@1, sectionNode->getLocation());
333 : ';' source_attr_list_opt
357 DataSectionContentsASTNode * dataSection = new DataSectionContentsASTNode($2);
358 dataSection->setLocation(@1, @3);
361 | '{' full_stmt_list '}'
363 ListASTNode * listNode = dynamic_cast<ListASTNode*>($2);
364 $$ = new BootableSectionContentsASTNode(listNode);
365 $$->setLocation(@1, @3);
369 full_stmt_list : full_stmt_list_elem
371 ListASTNode * list = new ListASTNode();
372 list->appendNode($1);
375 | full_stmt_list full_stmt_list_elem
377 dynamic_cast<ListASTNode*>($1)->appendNode($2);
383 : basic_stmt ';' { $$ = $1; }
384 | from_stmt { $$ = $1; }
385 | if_stmt { $$ = $1; }
386 | /* empty */ { $$ = NULL; }
389 basic_stmt_list : basic_stmt_list_elem
391 ListASTNode * list = new ListASTNode();
392 list->appendNode($1);
395 | basic_stmt_list basic_stmt_list_elem
397 dynamic_cast<ListASTNode*>($1)->appendNode($2);
403 : basic_stmt ';' { $$ = $1; }
404 | if_stmt { $$ = $1; }
405 | /* empty */ { $$ = NULL; }
408 basic_stmt : load_stmt { $$ = $1; }
409 | call_stmt { $$ = $1; }
410 | mode_stmt { $$ = $1; }
411 | message_stmt { $$ = $1; }
414 load_stmt : "load" dcd_opt load_data load_target_opt
416 LoadStatementASTNode * stmt = new LoadStatementASTNode();
419 // set dcd load flag if the "dcd" keyword was present.
422 stmt->setDCDLoad(true);
424 // set char locations for the statement
427 stmt->setLocation(@1, @4);
431 stmt->setLocation(@1, @3);
439 if (!elftosb::g_enableHABSupport)
441 yyerror(&yylloc, lexer, resultAST, "HAB features not supported with the selected family");
447 | /* empty */ { $$ = 0; }
449 load_data : int_const_expr
455 $$ = new StringConstASTNode($1);
460 $$ = new SourceASTNode($1);
465 $$ = new SectionMatchListASTNode(dynamic_cast<ListASTNode*>($1));
468 | section_list "from" TOK_SOURCE_NAME
470 $$ = new SectionMatchListASTNode(dynamic_cast<ListASTNode*>($1), $3);
471 $$->setLocation(@1, @3);
473 | TOK_SOURCE_NAME '[' section_list ']'
475 $$ = new SectionMatchListASTNode(dynamic_cast<ListASTNode*>($3), $1);
476 $$->setLocation(@1, @4);
480 $$ = new BlobConstASTNode($1);
488 section_list : section_list_elem
490 ListASTNode * list = new ListASTNode();
491 list->appendNode($1);
494 | section_list ',' section_list_elem
496 dynamic_cast<ListASTNode*>($1)->appendNode($3);
504 $$ = new SectionASTNode($1, SectionASTNode::kInclude);
507 | '~' TOK_SECTION_NAME
509 $$ = new SectionASTNode($2, SectionASTNode::kExclude);
510 $$->setLocation(@1, @2);
514 load_target_opt : '>' load_target
520 $$ = new NaturalLocationASTNode();
521 // $$->setLocation();
527 $$ = new NaturalLocationASTNode();
536 ivt_def : "ivt" '(' assignment_list_opt ')'
538 IVTConstASTNode * ivt = new IVTConstASTNode();
541 ivt->setFieldAssignments(dynamic_cast<ListASTNode*>($3));
543 ivt->setLocation(@1, @4);
548 assignment_list_opt : source_attr_list { $$ = $1; }
549 | /* empty */ { $$ = NULL; }
552 call_stmt : call_or_jump call_target call_arg_opt
554 CallStatementASTNode * stmt = new CallStatementASTNode();
558 stmt->setCallType(CallStatementASTNode::kCallType);
561 stmt->setCallType(CallStatementASTNode::kJumpType);
564 yyerror(&yylloc, lexer, resultAST, "invalid call_or_jump value");
569 stmt->setArgument($3);
570 stmt->setIsHAB(false);
573 stmt->setLocation(@1, @3);
577 stmt->setLocation(@1, @2);
581 | "hab" call_or_jump address_or_range call_arg_opt
583 if (!elftosb::g_enableHABSupport)
585 yyerror(&yylloc, lexer, resultAST, "HAB features not supported with the selected family");
589 CallStatementASTNode * stmt = new CallStatementASTNode();
593 stmt->setCallType(CallStatementASTNode::kCallType);
596 stmt->setCallType(CallStatementASTNode::kJumpType);
599 yyerror(&yylloc, lexer, resultAST, "invalid call_or_jump value");
604 stmt->setArgument($4);
605 stmt->setIsHAB(true);
608 stmt->setLocation(@1, @4);
612 stmt->setLocation(@1, @3);
618 call_or_jump : "call" { $$ = 1; }
622 call_target : TOK_SOURCE_NAME
624 $$ = new SymbolASTNode(NULL, $1);
629 $$ = new AddressRangeASTNode($1, NULL);
634 call_arg_opt : '(' int_const_expr ')' { $$ = $2; }
635 | '(' ')' { $$ = NULL; }
636 | /* empty */ { $$ = NULL; }
639 from_stmt : "from" TOK_SOURCE_NAME '{' basic_stmt_list '}'
641 $$ = new FromStatementASTNode($2, dynamic_cast<ListASTNode*>($4));
642 $$->setLocation(@1, @5);
646 mode_stmt : "mode" int_const_expr
648 $$ = new ModeStatementASTNode(dynamic_cast<ExprASTNode*>($2));
649 $$->setLocation(@1, @2);
653 message_stmt : "info" TOK_STRING_LITERAL
655 $$ = new MessageStatementASTNode(MessageStatementASTNode::kInfo, $2);
656 $$->setLocation(@1, @2);
658 | "warning" TOK_STRING_LITERAL
660 $$ = new MessageStatementASTNode(MessageStatementASTNode::kWarning, $2);
661 $$->setLocation(@1, @2);
663 | "error" TOK_STRING_LITERAL
665 $$ = new MessageStatementASTNode(MessageStatementASTNode::kError, $2);
666 $$->setLocation(@1, @2);
670 if_stmt : "if" bool_expr '{' full_stmt_list '}' else_opt
672 IfStatementASTNode * ifStmt = new IfStatementASTNode();
673 ifStmt->setConditionExpr(dynamic_cast<ExprASTNode*>($2));
674 ifStmt->setIfStatements(dynamic_cast<ListASTNode*>($4));
675 ifStmt->setElseStatements(dynamic_cast<ListASTNode*>($6));
676 ifStmt->setLocation(@1, @6);
681 else_opt : "else" '{' full_stmt_list '}'
687 ListASTNode * list = new ListASTNode();
688 list->appendNode($2);
690 $$->setLocation(@1, @2);
692 | /* empty */ { $$ = NULL; }
695 address_or_range : int_const_expr
697 $$ = new AddressRangeASTNode($1, NULL);
700 | int_const_expr ".." int_const_expr
702 $$ = new AddressRangeASTNode($1, $3);
703 $$->setLocation(@1, @3);
707 const_expr : bool_expr
713 $$ = new StringConstASTNode($1);
718 bool_expr : int_const_expr
722 | bool_expr '<' bool_expr
724 ExprASTNode * left = dynamic_cast<ExprASTNode*>($1);
725 ExprASTNode * right = dynamic_cast<ExprASTNode*>($3);
726 $$ = new BinaryOpExprASTNode(left, BinaryOpExprASTNode::kLessThan, right);
727 $$->setLocation(@1, @3);
729 | bool_expr '>' bool_expr
731 ExprASTNode * left = dynamic_cast<ExprASTNode*>($1);
732 ExprASTNode * right = dynamic_cast<ExprASTNode*>($3);
733 $$ = new BinaryOpExprASTNode(left, BinaryOpExprASTNode::kGreaterThan, right);
734 $$->setLocation(@1, @3);
736 | bool_expr ">=" bool_expr
738 ExprASTNode * left = dynamic_cast<ExprASTNode*>($1);
739 ExprASTNode * right = dynamic_cast<ExprASTNode*>($3);
740 $$ = new BinaryOpExprASTNode(left, BinaryOpExprASTNode::kGreaterThanEqual, right);
741 $$->setLocation(@1, @3);
743 | bool_expr "<=" bool_expr
745 ExprASTNode * left = dynamic_cast<ExprASTNode*>($1);
746 ExprASTNode * right = dynamic_cast<ExprASTNode*>($3);
747 $$ = new BinaryOpExprASTNode(left, BinaryOpExprASTNode::kLessThanEqual, right);
748 $$->setLocation(@1, @3);
750 | bool_expr "==" bool_expr
752 ExprASTNode * left = dynamic_cast<ExprASTNode*>($1);
753 ExprASTNode * right = dynamic_cast<ExprASTNode*>($3);
754 $$ = new BinaryOpExprASTNode(left, BinaryOpExprASTNode::kEqual, right);
755 $$->setLocation(@1, @3);
757 | bool_expr "!=" bool_expr
759 ExprASTNode * left = dynamic_cast<ExprASTNode*>($1);
760 ExprASTNode * right = dynamic_cast<ExprASTNode*>($3);
761 $$ = new BinaryOpExprASTNode(left, BinaryOpExprASTNode::kNotEqual, right);
762 $$->setLocation(@1, @3);
764 | bool_expr "&&" bool_expr
766 ExprASTNode * left = dynamic_cast<ExprASTNode*>($1);
767 ExprASTNode * right = dynamic_cast<ExprASTNode*>($3);
768 $$ = new BinaryOpExprASTNode(left, BinaryOpExprASTNode::kBooleanAnd, right);
769 $$->setLocation(@1, @3);
771 | bool_expr "||" bool_expr
773 ExprASTNode * left = dynamic_cast<ExprASTNode*>($1);
774 ExprASTNode * right = dynamic_cast<ExprASTNode*>($3);
775 $$ = new BinaryOpExprASTNode(left, BinaryOpExprASTNode::kBooleanOr, right);
776 $$->setLocation(@1, @3);
778 | '!' bool_expr %prec UNARY_OP
780 $$ = new BooleanNotExprASTNode(dynamic_cast<ExprASTNode*>($2));
781 $$->setLocation(@1, @2);
783 | TOK_IDENT '(' TOK_SOURCE_NAME ')'
785 $$ = new SourceFileFunctionASTNode($1, $3);
786 $$->setLocation(@1, @4);
791 $$->setLocation(@1, @3);
793 | "defined" '(' TOK_IDENT ')'
795 $$ = new DefinedOperatorASTNode($3);
796 $$->setLocation(@1, @4);
800 int_const_expr : expr { $$ = $1; }
803 symbol_ref : TOK_SOURCE_NAME ':' TOK_IDENT
805 $$ = new SymbolASTNode($3, $1);
806 $$->setLocation(@1, @3);
810 $$ = new SymbolASTNode($2);
811 $$->setLocation(@1, @2);
822 $$ = new VariableExprASTNode($1);
827 $$ = new SymbolRefExprASTNode(dynamic_cast<SymbolASTNode*>($1));
832 ExprASTNode * left = dynamic_cast<ExprASTNode*>($1);
833 ExprASTNode * right = dynamic_cast<ExprASTNode*>($3);
834 $$ = new RangeExprASTNode(left, right);
838 ExprASTNode * left = dynamic_cast<ExprASTNode*>($1);
839 ExprASTNode * right = dynamic_cast<ExprASTNode*>($3);
840 $$ = new BinaryOpExprASTNode(left, BinaryOpExprASTNode::kAdd, right);
841 $$->setLocation(@1, @3);
845 ExprASTNode * left = dynamic_cast<ExprASTNode*>($1);
846 ExprASTNode * right = dynamic_cast<ExprASTNode*>($3);
847 $$ = new BinaryOpExprASTNode(left, BinaryOpExprASTNode::kSubtract, right);
848 $$->setLocation(@1, @3);
852 ExprASTNode * left = dynamic_cast<ExprASTNode*>($1);
853 ExprASTNode * right = dynamic_cast<ExprASTNode*>($3);
854 $$ = new BinaryOpExprASTNode(left, BinaryOpExprASTNode::kMultiply, right);
855 $$->setLocation(@1, @3);
859 ExprASTNode * left = dynamic_cast<ExprASTNode*>($1);
860 ExprASTNode * right = dynamic_cast<ExprASTNode*>($3);
861 $$ = new BinaryOpExprASTNode(left, BinaryOpExprASTNode::kDivide, right);
862 $$->setLocation(@1, @3);
866 ExprASTNode * left = dynamic_cast<ExprASTNode*>($1);
867 ExprASTNode * right = dynamic_cast<ExprASTNode*>($3);
868 $$ = new BinaryOpExprASTNode(left, BinaryOpExprASTNode::kModulus, right);
869 $$->setLocation(@1, @3);
873 ExprASTNode * left = dynamic_cast<ExprASTNode*>($1);
874 ExprASTNode * right = dynamic_cast<ExprASTNode*>($3);
875 $$ = new BinaryOpExprASTNode(left, BinaryOpExprASTNode::kPower, right);
876 $$->setLocation(@1, @3);
880 ExprASTNode * left = dynamic_cast<ExprASTNode*>($1);
881 ExprASTNode * right = dynamic_cast<ExprASTNode*>($3);
882 $$ = new BinaryOpExprASTNode(left, BinaryOpExprASTNode::kBitwiseAnd, right);
883 $$->setLocation(@1, @3);
887 ExprASTNode * left = dynamic_cast<ExprASTNode*>($1);
888 ExprASTNode * right = dynamic_cast<ExprASTNode*>($3);
889 $$ = new BinaryOpExprASTNode(left, BinaryOpExprASTNode::kBitwiseOr, right);
890 $$->setLocation(@1, @3);
894 ExprASTNode * left = dynamic_cast<ExprASTNode*>($1);
895 ExprASTNode * right = dynamic_cast<ExprASTNode*>($3);
896 $$ = new BinaryOpExprASTNode(left, BinaryOpExprASTNode::kBitwiseXor, right);
897 $$->setLocation(@1, @3);
901 ExprASTNode * left = dynamic_cast<ExprASTNode*>($1);
902 ExprASTNode * right = dynamic_cast<ExprASTNode*>($3);
903 $$ = new BinaryOpExprASTNode(left, BinaryOpExprASTNode::kShiftLeft, right);
904 $$->setLocation(@1, @3);
908 ExprASTNode * left = dynamic_cast<ExprASTNode*>($1);
909 ExprASTNode * right = dynamic_cast<ExprASTNode*>($3);
910 $$ = new BinaryOpExprASTNode(left, BinaryOpExprASTNode::kShiftRight, right);
911 $$->setLocation(@1, @3);
917 | expr '.' TOK_INT_SIZE
919 $$ = new IntSizeExprASTNode(dynamic_cast<ExprASTNode*>($1), $3->getWordSize());
920 $$->setLocation(@1, @3);
925 $$->setLocation(@1, @3);
927 | "sizeof" '(' symbol_ref ')'
929 $$ = new SizeofOperatorASTNode(dynamic_cast<SymbolASTNode*>($3));
930 $$->setLocation(@1, @4);
932 | "sizeof" '(' TOK_IDENT ')'
934 $$ = new SizeofOperatorASTNode($3);
935 $$->setLocation(@1, @4);
937 | "sizeof" '(' TOK_SOURCE_NAME ')'
939 $$ = new SizeofOperatorASTNode($3);
940 $$->setLocation(@1, @4);
944 unary_expr : '+' expr %prec UNARY_OP
948 | '-' expr %prec UNARY_OP
950 $$ = new NegativeExprASTNode(dynamic_cast<ExprASTNode*>($2));
951 $$->setLocation(@1, @2);
955 int_value : TOK_INT_LITERAL
957 $$ = new IntConstExprASTNode($1->getValue(), $1->getWordSize());
966 static int yylex(YYSTYPE * lvalp, YYLTYPE * yylloc, ElftosbLexer * lexer)
968 int token = lexer->yylex();
969 *yylloc = lexer->getLocation();
970 lexer->getSymbolValue(lvalp);
974 static void yyerror(YYLTYPE * yylloc, ElftosbLexer * lexer, CommandFileASTNode ** resultAST, const char * error)
976 throw syntax_error(format_string("line %d: %s\n", yylloc->m_firstLine, error));