]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Ecc/C.g
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / BaseTools / Source / Python / Ecc / C.g
1 /* @file
2 This file is used to be the grammar file of ECC tool
3
4 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6 */
7
8 grammar C;
9 options {
10 language=Python;
11 backtrack=true;
12 memoize=true;
13 k=2;
14 }
15
16 @lexer::header{
17 ## @file
18 # The file defines the Lexer for C source files.
19 #
20 # THIS FILE IS AUTO-GENERATED. PLEASE DO NOT MODIFY THIS FILE.
21 # This file is generated by running:
22 # java org.antlr.Tool C.g
23 #
24 # Copyright (c) 2009 - 2010, Intel Corporation All rights reserved.
25 #
26 # This program and the accompanying materials are licensed and made available
27 # under the terms and conditions of the BSD License which accompanies this
28 # distribution. The full text of the license may be found at:
29 # http://opensource.org/licenses/bsd-license.php
30 #
31 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
32 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
33 #
34 ##
35 }
36
37 @header {
38 ## @file
39 # The file defines the parser for C source files.
40 #
41 # THIS FILE IS AUTO-GENERATED. PLEASE DO NOT MODIFY THIS FILE.
42 # This file is generated by running:
43 # java org.antlr.Tool C.g
44 #
45 # Copyright (c) 2009 - 2010, Intel Corporation All rights reserved.
46 #
47 # This program and the accompanying materials are licensed and made available
48 # under the terms and conditions of the BSD License which accompanies this
49 # distribution. The full text of the license may be found at:
50 # http://opensource.org/licenses/bsd-license.php
51 #
52 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
53 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
54 #
55 ##
56
57 import CodeFragment
58 import FileProfile
59 }
60
61 @members {
62
63 def printTokenInfo(self, line, offset, tokenText):
64 print str(line)+ ',' + str(offset) + ':' + str(tokenText)
65
66 def StorePredicateExpression(self, StartLine, StartOffset, EndLine, EndOffset, Text):
67 PredExp = CodeFragment.PredicateExpression(Text, (StartLine, StartOffset), (EndLine, EndOffset))
68 FileProfile.PredicateExpressionList.append(PredExp)
69
70 def StoreEnumerationDefinition(self, StartLine, StartOffset, EndLine, EndOffset, Text):
71 EnumDef = CodeFragment.EnumerationDefinition(Text, (StartLine, StartOffset), (EndLine, EndOffset))
72 FileProfile.EnumerationDefinitionList.append(EnumDef)
73
74 def StoreStructUnionDefinition(self, StartLine, StartOffset, EndLine, EndOffset, Text):
75 SUDef = CodeFragment.StructUnionDefinition(Text, (StartLine, StartOffset), (EndLine, EndOffset))
76 FileProfile.StructUnionDefinitionList.append(SUDef)
77
78 def StoreTypedefDefinition(self, StartLine, StartOffset, EndLine, EndOffset, FromText, ToText):
79 Tdef = CodeFragment.TypedefDefinition(FromText, ToText, (StartLine, StartOffset), (EndLine, EndOffset))
80 FileProfile.TypedefDefinitionList.append(Tdef)
81
82 def StoreFunctionDefinition(self, StartLine, StartOffset, EndLine, EndOffset, ModifierText, DeclText, LeftBraceLine, LeftBraceOffset, DeclLine, DeclOffset):
83 FuncDef = CodeFragment.FunctionDefinition(ModifierText, DeclText, (StartLine, StartOffset), (EndLine, EndOffset), (LeftBraceLine, LeftBraceOffset), (DeclLine, DeclOffset))
84 FileProfile.FunctionDefinitionList.append(FuncDef)
85
86 def StoreVariableDeclaration(self, StartLine, StartOffset, EndLine, EndOffset, ModifierText, DeclText):
87 VarDecl = CodeFragment.VariableDeclaration(ModifierText, DeclText, (StartLine, StartOffset), (EndLine, EndOffset))
88 FileProfile.VariableDeclarationList.append(VarDecl)
89
90 def StoreFunctionCalling(self, StartLine, StartOffset, EndLine, EndOffset, FuncName, ParamList):
91 FuncCall = CodeFragment.FunctionCalling(FuncName, ParamList, (StartLine, StartOffset), (EndLine, EndOffset))
92 FileProfile.FunctionCallingList.append(FuncCall)
93
94 }
95
96 translation_unit
97 : external_declaration*
98 ;
99
100
101 /*function_declaration
102 @after{
103 print $function_declaration.text
104 }
105 : declaration_specifiers IDENTIFIER '(' parameter_list ')' ';'
106 ;
107 */
108 external_declaration
109 options {k=1;}
110 /*@after{
111 print $external_declaration.text
112 }*/
113 : ( declaration_specifiers? declarator declaration* '{' )=> function_definition
114 | declaration
115 | macro_statement (';')?
116 ;
117
118
119
120 function_definition
121 scope {
122 ModifierText;
123 DeclText;
124 LBLine;
125 LBOffset;
126 DeclLine;
127 DeclOffset;
128 }
129 @init {
130 $function_definition::ModifierText = '';
131 $function_definition::DeclText = '';
132 $function_definition::LBLine = 0;
133 $function_definition::LBOffset = 0;
134 $function_definition::DeclLine = 0;
135 $function_definition::DeclOffset = 0;
136 }
137 @after{
138 self.StoreFunctionDefinition($function_definition.start.line, $function_definition.start.charPositionInLine, $function_definition.stop.line, $function_definition.stop.charPositionInLine, $function_definition::ModifierText, $function_definition::DeclText, $function_definition::LBLine, $function_definition::LBOffset, $function_definition::DeclLine, $function_definition::DeclOffset)
139 }
140 : d=declaration_specifiers? declarator
141 ( declaration+ a=compound_statement // K&R style
142 | b=compound_statement // ANSI style
143 ) {
144 if d != None:
145 $function_definition::ModifierText = $declaration_specifiers.text
146 else:
147 $function_definition::ModifierText = ''
148 $function_definition::DeclText = $declarator.text
149 $function_definition::DeclLine = $declarator.start.line
150 $function_definition::DeclOffset = $declarator.start.charPositionInLine
151 if a != None:
152 $function_definition::LBLine = $a.start.line
153 $function_definition::LBOffset = $a.start.charPositionInLine
154 else:
155 $function_definition::LBLine = $b.start.line
156 $function_definition::LBOffset = $b.start.charPositionInLine
157 }
158 ;
159
160 declaration
161 : a='typedef' b=declaration_specifiers?
162 c=init_declarator_list d=';'
163 {
164 if b != None:
165 self.StoreTypedefDefinition($a.line, $a.charPositionInLine, $d.line, $d.charPositionInLine, $b.text, $c.text)
166 else:
167 self.StoreTypedefDefinition($a.line, $a.charPositionInLine, $d.line, $d.charPositionInLine, '', $c.text)
168 }
169 | s=declaration_specifiers t=init_declarator_list? e=';'
170 {
171 if t != None:
172 self.StoreVariableDeclaration($s.start.line, $s.start.charPositionInLine, $t.start.line, $t.start.charPositionInLine, $s.text, $t.text)
173 }
174 ;
175
176 declaration_specifiers
177 : ( storage_class_specifier
178 | type_specifier
179 | type_qualifier
180 )+
181 ;
182
183 init_declarator_list
184 : init_declarator (',' init_declarator)*
185 ;
186
187 init_declarator
188 : declarator ('=' initializer)?
189 ;
190
191 storage_class_specifier
192 : 'extern'
193 | 'static'
194 | 'auto'
195 | 'register'
196 | 'STATIC'
197 ;
198
199 type_specifier
200 : 'void'
201 | 'char'
202 | 'short'
203 | 'int'
204 | 'long'
205 | 'float'
206 | 'double'
207 | 'signed'
208 | 'unsigned'
209 | s=struct_or_union_specifier
210 {
211 if s.stop != None:
212 self.StoreStructUnionDefinition($s.start.line, $s.start.charPositionInLine, $s.stop.line, $s.stop.charPositionInLine, $s.text)
213 }
214 | e=enum_specifier
215 {
216 if e.stop != None:
217 self.StoreEnumerationDefinition($e.start.line, $e.start.charPositionInLine, $e.stop.line, $e.stop.charPositionInLine, $e.text)
218 }
219 | (IDENTIFIER type_qualifier* declarator)=> type_id
220 ;
221
222 type_id
223 : IDENTIFIER
224 //{self.printTokenInfo($a.line, $a.pos, $a.text)}
225 ;
226
227 struct_or_union_specifier
228 options {k=3;}
229 : struct_or_union IDENTIFIER? '{' struct_declaration_list '}'
230 | struct_or_union IDENTIFIER
231 ;
232
233 struct_or_union
234 : 'struct'
235 | 'union'
236 ;
237
238 struct_declaration_list
239 : struct_declaration+
240 ;
241
242 struct_declaration
243 : specifier_qualifier_list struct_declarator_list ';'
244 ;
245
246 specifier_qualifier_list
247 : ( type_qualifier | type_specifier )+
248 ;
249
250 struct_declarator_list
251 : struct_declarator (',' struct_declarator)*
252 ;
253
254 struct_declarator
255 : declarator (':' constant_expression)?
256 | ':' constant_expression
257 ;
258
259 enum_specifier
260 options {k=3;}
261 : 'enum' '{' enumerator_list ','? '}'
262 | 'enum' IDENTIFIER '{' enumerator_list ','? '}'
263 | 'enum' IDENTIFIER
264 ;
265
266 enumerator_list
267 : enumerator (',' enumerator)*
268 ;
269
270 enumerator
271 : IDENTIFIER ('=' constant_expression)?
272 ;
273
274 type_qualifier
275 : 'const'
276 | 'volatile'
277 | 'IN'
278 | 'OUT'
279 | 'OPTIONAL'
280 | 'CONST'
281 | 'UNALIGNED'
282 | 'VOLATILE'
283 | 'GLOBAL_REMOVE_IF_UNREFERENCED'
284 | 'EFIAPI'
285 | 'EFI_BOOTSERVICE'
286 | 'EFI_RUNTIMESERVICE'
287 | 'PACKED'
288 ;
289
290 declarator
291 : pointer? ('EFIAPI')? ('EFI_BOOTSERVICE')? ('EFI_RUNTIMESERVICE')? direct_declarator
292 // | ('EFIAPI')? ('EFI_BOOTSERVICE')? ('EFI_RUNTIMESERVICE')? pointer? direct_declarator
293 | pointer
294 ;
295
296 direct_declarator
297 : IDENTIFIER declarator_suffix*
298 | '(' ('EFIAPI')? declarator ')' declarator_suffix+
299 ;
300
301 declarator_suffix
302 : '[' constant_expression ']'
303 | '[' ']'
304 | '(' parameter_type_list ')'
305 | '(' identifier_list ')'
306 | '(' ')'
307 ;
308
309 pointer
310 : '*' type_qualifier+ pointer?
311 | '*' pointer
312 | '*'
313 ;
314
315 parameter_type_list
316 : parameter_list (',' ('OPTIONAL')? '...')?
317 ;
318
319 parameter_list
320 : parameter_declaration (',' ('OPTIONAL')? parameter_declaration)*
321 ;
322
323 parameter_declaration
324 : declaration_specifiers (declarator|abstract_declarator)* ('OPTIONAL')?
325 //accomerdate user-defined type only, no declarator follow.
326 | pointer* IDENTIFIER
327 ;
328
329 identifier_list
330 : IDENTIFIER
331 (',' IDENTIFIER)*
332 ;
333
334 type_name
335 : specifier_qualifier_list abstract_declarator?
336 | type_id
337 ;
338
339 abstract_declarator
340 : pointer direct_abstract_declarator?
341 | direct_abstract_declarator
342 ;
343
344 direct_abstract_declarator
345 : ( '(' abstract_declarator ')' | abstract_declarator_suffix ) abstract_declarator_suffix*
346 ;
347
348 abstract_declarator_suffix
349 : '[' ']'
350 | '[' constant_expression ']'
351 | '(' ')'
352 | '(' parameter_type_list ')'
353 ;
354
355 initializer
356
357 : assignment_expression
358 | '{' initializer_list ','? '}'
359 ;
360
361 initializer_list
362 : initializer (',' initializer )*
363 ;
364
365 // E x p r e s s i o n s
366
367 argument_expression_list
368 : assignment_expression ('OPTIONAL')? (',' assignment_expression ('OPTIONAL')?)*
369 ;
370
371 additive_expression
372 : (multiplicative_expression) ('+' multiplicative_expression | '-' multiplicative_expression)*
373 ;
374
375 multiplicative_expression
376 : (cast_expression) ('*' cast_expression | '/' cast_expression | '%' cast_expression)*
377 ;
378
379 cast_expression
380 : '(' type_name ')' cast_expression
381 | unary_expression
382 ;
383
384 unary_expression
385 : postfix_expression
386 | '++' unary_expression
387 | '--' unary_expression
388 | unary_operator cast_expression
389 | 'sizeof' unary_expression
390 | 'sizeof' '(' type_name ')'
391 ;
392
393 postfix_expression
394 scope {
395 FuncCallText;
396 }
397 @init {
398 $postfix_expression::FuncCallText = '';
399 }
400 : p=primary_expression {$postfix_expression::FuncCallText += $p.text}
401 ( '[' expression ']'
402 | '(' a=')'{self.StoreFunctionCalling($p.start.line, $p.start.charPositionInLine, $a.line, $a.charPositionInLine, $postfix_expression::FuncCallText, '')}
403 | '(' c=argument_expression_list b=')' {self.StoreFunctionCalling($p.start.line, $p.start.charPositionInLine, $b.line, $b.charPositionInLine, $postfix_expression::FuncCallText, $c.text)}
404 | '(' macro_parameter_list ')'
405 | '.' x=IDENTIFIER {$postfix_expression::FuncCallText += '.' + $x.text}
406 | '*' y=IDENTIFIER {$postfix_expression::FuncCallText = $y.text}
407 | '->' z=IDENTIFIER {$postfix_expression::FuncCallText += '->' + $z.text}
408 | '++'
409 | '--'
410 )*
411 ;
412
413 macro_parameter_list
414 : parameter_declaration (',' parameter_declaration)*
415 ;
416
417 unary_operator
418 : '&'
419 | '*'
420 | '+'
421 | '-'
422 | '~'
423 | '!'
424 ;
425
426 primary_expression
427 : IDENTIFIER
428 | constant
429 | '(' expression ')'
430 ;
431
432 constant
433 : HEX_LITERAL
434 | OCTAL_LITERAL
435 | DECIMAL_LITERAL
436 | CHARACTER_LITERAL
437 | (IDENTIFIER* STRING_LITERAL+)+ IDENTIFIER*
438 | FLOATING_POINT_LITERAL
439 ;
440
441 /////
442
443 expression
444 : assignment_expression (',' assignment_expression)*
445 ;
446
447 constant_expression
448 : conditional_expression
449 ;
450
451 assignment_expression
452 : lvalue assignment_operator assignment_expression
453 | conditional_expression
454 ;
455
456 lvalue
457 : unary_expression
458 ;
459
460 assignment_operator
461 : '='
462 | '*='
463 | '/='
464 | '%='
465 | '+='
466 | '-='
467 | '<<='
468 | '>>='
469 | '&='
470 | '^='
471 | '|='
472 ;
473
474 conditional_expression
475 : e=logical_or_expression ('?' expression ':' conditional_expression {self.StorePredicateExpression($e.start.line, $e.start.charPositionInLine, $e.stop.line, $e.stop.charPositionInLine, $e.text)})?
476 ;
477
478 logical_or_expression
479 : logical_and_expression ('||' logical_and_expression)*
480 ;
481
482 logical_and_expression
483 : inclusive_or_expression ('&&' inclusive_or_expression)*
484 ;
485
486 inclusive_or_expression
487 : exclusive_or_expression ('|' exclusive_or_expression)*
488 ;
489
490 exclusive_or_expression
491 : and_expression ('^' and_expression)*
492 ;
493
494 and_expression
495 : equality_expression ('&' equality_expression)*
496 ;
497 equality_expression
498 : relational_expression (('=='|'!=') relational_expression )*
499 ;
500
501 relational_expression
502 : shift_expression (('<'|'>'|'<='|'>=') shift_expression)*
503 ;
504
505 shift_expression
506 : additive_expression (('<<'|'>>') additive_expression)*
507 ;
508
509 // S t a t e m e n t s
510
511 statement
512 : labeled_statement
513 | compound_statement
514 | expression_statement
515 | selection_statement
516 | iteration_statement
517 | jump_statement
518 | macro_statement
519 | asm2_statement
520 | asm1_statement
521 | asm_statement
522 | declaration
523 ;
524
525 asm2_statement
526 : '__asm__'? IDENTIFIER '(' (~(';'))* ')' ';'
527 ;
528
529 asm1_statement
530 : '_asm' '{' (~('}'))* '}'
531 ;
532
533 asm_statement
534 : '__asm' '{' (~('}'))* '}'
535 ;
536
537 macro_statement
538 : IDENTIFIER '(' declaration* statement_list? expression? ')'
539 ;
540
541 labeled_statement
542 : IDENTIFIER ':' statement
543 | 'case' constant_expression ':' statement
544 | 'default' ':' statement
545 ;
546
547 compound_statement
548 : '{' declaration* statement_list? '}'
549 ;
550
551 statement_list
552 : statement+
553 ;
554
555 expression_statement
556 : ';'
557 | expression ';'
558 ;
559
560 selection_statement
561 : 'if' '(' e=expression ')' {self.StorePredicateExpression($e.start.line, $e.start.charPositionInLine, $e.stop.line, $e.stop.charPositionInLine, $e.text)} statement (options {k=1; backtrack=false;}:'else' statement)?
562 | 'switch' '(' expression ')' statement
563 ;
564
565 iteration_statement
566 : 'while' '(' e=expression ')' statement {self.StorePredicateExpression($e.start.line, $e.start.charPositionInLine, $e.stop.line, $e.stop.charPositionInLine, $e.text)}
567 | 'do' statement 'while' '(' e=expression ')' ';' {self.StorePredicateExpression($e.start.line, $e.start.charPositionInLine, $e.stop.line, $e.stop.charPositionInLine, $e.text)}
568 | 'for' '(' expression_statement e=expression_statement expression? ')' statement {self.StorePredicateExpression($e.start.line, $e.start.charPositionInLine, $e.stop.line, $e.stop.charPositionInLine, $e.text)}
569 ;
570
571 jump_statement
572 : 'goto' IDENTIFIER ';'
573 | 'continue' ';'
574 | 'break' ';'
575 | 'return' ';'
576 | 'return' expression ';'
577 ;
578
579 IDENTIFIER
580 : LETTER (LETTER|'0'..'9')*
581 ;
582
583 fragment
584 LETTER
585 : '$'
586 | 'A'..'Z'
587 | 'a'..'z'
588 | '_'
589 ;
590
591 CHARACTER_LITERAL
592 : ('L')? '\'' ( EscapeSequence | ~('\''|'\\') ) '\''
593 ;
594
595 STRING_LITERAL
596 : ('L')? '"' ( EscapeSequence | ~('\\'|'"') )* '"'
597 ;
598
599 HEX_LITERAL : '0' ('x'|'X') HexDigit+ IntegerTypeSuffix? ;
600
601 DECIMAL_LITERAL : ('0' | '1'..'9' '0'..'9'*) IntegerTypeSuffix? ;
602
603 OCTAL_LITERAL : '0' ('0'..'7')+ IntegerTypeSuffix? ;
604
605 fragment
606 HexDigit : ('0'..'9'|'a'..'f'|'A'..'F') ;
607
608 fragment
609 IntegerTypeSuffix
610 : ('u'|'U')
611 | ('l'|'L')
612 | ('u'|'U') ('l'|'L')
613 | ('u'|'U') ('l'|'L') ('l'|'L')
614 ;
615
616 FLOATING_POINT_LITERAL
617 : ('0'..'9')+ '.' ('0'..'9')* Exponent? FloatTypeSuffix?
618 | '.' ('0'..'9')+ Exponent? FloatTypeSuffix?
619 | ('0'..'9')+ Exponent FloatTypeSuffix?
620 | ('0'..'9')+ Exponent? FloatTypeSuffix
621 ;
622
623 fragment
624 Exponent : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;
625
626 fragment
627 FloatTypeSuffix : ('f'|'F'|'d'|'D') ;
628
629 fragment
630 EscapeSequence
631 : '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
632 | OctalEscape
633 ;
634
635 fragment
636 OctalEscape
637 : '\\' ('0'..'3') ('0'..'7') ('0'..'7')
638 | '\\' ('0'..'7') ('0'..'7')
639 | '\\' ('0'..'7')
640 ;
641
642 fragment
643 UnicodeEscape
644 : '\\' 'u' HexDigit HexDigit HexDigit HexDigit
645 ;
646
647 WS : (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;}
648 ;
649
650 // ignore '\' of line concatenation
651 BS : ('\\') {$channel=HIDDEN;}
652 ;
653
654 // ignore function modifiers
655 //FUNC_MODIFIERS : 'EFIAPI' {$channel=HIDDEN;}
656 // ;
657
658 UnicodeVocabulary
659 : '\u0003'..'\uFFFE'
660 ;
661 COMMENT
662 : '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;}
663 ;
664
665
666 LINE_COMMENT
667 : '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
668 ;
669
670 // ignore #line info for now
671 LINE_COMMAND
672 : '#' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
673 ;