-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.yy
2433 lines (2219 loc) · 76.1 KB
/
parse.yy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2010-2013, Intel Corporation
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
%locations
/* supress shift-reduces conflict message for dangling else */
/* one for 'if', one for 'cif' */
%expect 2
%error-verbose
%code requires {
#define yytnamerr lYYTNameErr
#define YYLTYPE SourcePos
# define YYLLOC_DEFAULT(Current, Rhs, N) \
do \
if (N) \
{ \
(Current).first_line = YYRHSLOC (Rhs, 1).first_line; \
(Current).first_column = YYRHSLOC (Rhs, 1).first_column; \
(Current).last_line = YYRHSLOC (Rhs, N).last_line; \
(Current).last_column = YYRHSLOC (Rhs, N).last_column; \
(Current).name = YYRHSLOC (Rhs, 1).name ; \
} \
else \
{ /* empty RHS */ \
(Current).first_line = (Current).last_line = \
YYRHSLOC (Rhs, 0).last_line; \
(Current).first_column = (Current).last_column = \
YYRHSLOC (Rhs, 0).last_column; \
(Current).name = NULL; /* new */ \
} \
while (0)
struct ForeachDimension;
}
%{
#include "ispc.h"
#include "type.h"
#include "module.h"
#include "decl.h"
#include "expr.h"
#include "sym.h"
#include "stmt.h"
#include "util.h"
#include <stdio.h>
#if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
#include <llvm/Constants.h>
#else
#include <llvm/IR/Constants.h>
#endif
#define UNIMPLEMENTED \
Error(yylloc, "Unimplemented parser functionality %s:%d", \
__FILE__, __LINE__);
union YYSTYPE;
extern int yylex();
extern char *yytext;
void yyerror(const char *s);
static int lYYTNameErr(char *yyres, const char *yystr);
static void lSuggestBuiltinAlternates();
static void lSuggestParamListAlternates();
static void lAddDeclaration(DeclSpecs *ds, Declarator *decl);
static void lAddFunctionParams(Declarator *decl);
static void lAddMaskToSymbolTable(SourcePos pos);
static void lAddThreadIndexCountToSymbolTable(SourcePos pos);
static std::string lGetAlternates(std::vector<std::string> &alternates);
static const char *lGetStorageClassString(StorageClass sc);
static bool lGetConstantInt(Expr *expr, int *value, SourcePos pos, const char *usage);
static EnumType *lCreateEnumType(const char *name, std::vector<Symbol *> *enums,
SourcePos pos);
static void lFinalizeEnumeratorSymbols(std::vector<Symbol *> &enums,
const EnumType *enumType);
static const char *lBuiltinTokens[] = {
"assert", "bool", "break", "case", "cdo",
"cfor", "cif", "cwhile", "const", "continue", "default",
"do", "delete", "double", "else", "enum", "export", "extern", "false",
"float", "for", "foreach", "foreach_active", "foreach_tiled",
"foreach_unique", "goto", "if", "in", "inline",
"int", "int8", "int16", "int32", "int64", "launch", "new", "NULL",
"print", "return", "signed", "sizeof", "static", "struct", "switch",
"sync", "task", "true", "typedef", "uniform", "unmasked", "unsigned",
"varying", "void", "while", NULL
};
static const char *lParamListTokens[] = {
"bool", "const", "double", "enum", "false", "float", "int",
"int8", "int16", "int32", "int64", "signed", "struct", "true",
"uniform", "unsigned", "varying", "void", NULL
};
struct ForeachDimension {
ForeachDimension(Symbol *s = NULL, Expr *b = NULL, Expr *e = NULL) {
sym = s;
beginExpr = b;
endExpr = e;
}
Symbol *sym;
Expr *beginExpr, *endExpr;
};
%}
%union {
uint64_t intVal;
float floatVal;
double doubleVal;
std::string *stringVal;
const char *constCharPtr;
Expr *expr;
ExprList *exprList;
const Type *type;
std::vector<std::pair<const Type *, SourcePos> > *typeList;
const AtomicType *atomicType;
int typeQualifier;
StorageClass storageClass;
Stmt *stmt;
DeclSpecs *declSpecs;
Declaration *declaration;
std::vector<Declarator *> *declarators;
std::vector<Declaration *> *declarationList;
Declarator *declarator;
std::vector<Declarator *> *structDeclaratorList;
StructDeclaration *structDeclaration;
std::vector<StructDeclaration *> *structDeclarationList;
const EnumType *enumType;
Symbol *symbol;
std::vector<Symbol *> *symbolList;
ForeachDimension *foreachDimension;
std::vector<ForeachDimension *> *foreachDimensionList;
std::pair<std::string, SourcePos> *declspecPair;
std::vector<std::pair<std::string, SourcePos> > *declspecList;
}
%token TOKEN_INT8_CONSTANT TOKEN_UINT8_CONSTANT
%token TOKEN_INT16_CONSTANT TOKEN_UINT16_CONSTANT
%token TOKEN_INT32_CONSTANT TOKEN_UINT32_CONSTANT
%token TOKEN_INT64_CONSTANT TOKEN_UINT64_CONSTANT
%token TOKEN_INT32DOTDOTDOT_CONSTANT TOKEN_UINT32DOTDOTDOT_CONSTANT
%token TOKEN_INT64DOTDOTDOT_CONSTANT TOKEN_UINT64DOTDOTDOT_CONSTANT
%token TOKEN_FLOAT_CONSTANT TOKEN_DOUBLE_CONSTANT TOKEN_STRING_C_LITERAL
%token TOKEN_IDENTIFIER TOKEN_STRING_LITERAL TOKEN_TYPE_NAME TOKEN_NULL
%token TOKEN_PTR_OP TOKEN_INC_OP TOKEN_DEC_OP TOKEN_LEFT_OP TOKEN_RIGHT_OP
%token TOKEN_LE_OP TOKEN_GE_OP TOKEN_EQ_OP TOKEN_NE_OP
%token TOKEN_AND_OP TOKEN_OR_OP TOKEN_MUL_ASSIGN TOKEN_DIV_ASSIGN TOKEN_MOD_ASSIGN
%token TOKEN_ADD_ASSIGN TOKEN_SUB_ASSIGN TOKEN_LEFT_ASSIGN TOKEN_RIGHT_ASSIGN
%token TOKEN_AND_ASSIGN TOKEN_OR_ASSIGN TOKEN_XOR_ASSIGN
%token TOKEN_SIZEOF TOKEN_NEW TOKEN_DELETE TOKEN_IN
%token TOKEN_EXTERN TOKEN_EXPORT TOKEN_STATIC TOKEN_INLINE TOKEN_TASK TOKEN_DECLSPEC
%token TOKEN_UNIFORM TOKEN_VARYING TOKEN_TYPEDEF TOKEN_SOA TOKEN_UNMASKED
%token TOKEN_CHAR TOKEN_INT TOKEN_SIGNED TOKEN_UNSIGNED TOKEN_FLOAT TOKEN_DOUBLE
%token TOKEN_INT8 TOKEN_INT16 TOKEN_INT64 TOKEN_CONST TOKEN_VOID TOKEN_BOOL
%token TOKEN_ENUM TOKEN_STRUCT TOKEN_TRUE TOKEN_FALSE
%token TOKEN_CASE TOKEN_DEFAULT TOKEN_IF TOKEN_ELSE TOKEN_SWITCH
%token TOKEN_WHILE TOKEN_DO TOKEN_LAUNCH TOKEN_FOREACH TOKEN_FOREACH_TILED
%token TOKEN_FOREACH_UNIQUE TOKEN_FOREACH_ACTIVE TOKEN_DOTDOTDOT
%token TOKEN_FOR TOKEN_GOTO TOKEN_CONTINUE TOKEN_BREAK TOKEN_RETURN
%token TOKEN_CIF TOKEN_CDO TOKEN_CFOR TOKEN_CWHILE
%token TOKEN_SYNC TOKEN_PRINT TOKEN_ASSERT
%type <expr> primary_expression postfix_expression integer_dotdotdot
%type <expr> unary_expression cast_expression funcall_expression launch_expression
%type <expr> multiplicative_expression additive_expression shift_expression
%type <expr> relational_expression equality_expression and_expression
%type <expr> exclusive_or_expression inclusive_or_expression
%type <expr> logical_and_expression logical_or_expression new_expression
%type <expr> conditional_expression assignment_expression expression
%type <expr> initializer constant_expression for_test
%type <exprList> argument_expression_list initializer_list
%type <stmt> statement labeled_statement compound_statement for_init_statement
%type <stmt> expression_statement selection_statement iteration_statement
%type <stmt> jump_statement statement_list declaration_statement print_statement
%type <stmt> assert_statement sync_statement delete_statement unmasked_statement
%type <declaration> declaration parameter_declaration
%type <declarators> init_declarator_list
%type <declarationList> parameter_list parameter_type_list
%type <declarator> declarator pointer reference
%type <declarator> init_declarator direct_declarator struct_declarator
%type <declarator> abstract_declarator direct_abstract_declarator
%type <structDeclaratorList> struct_declarator_list
%type <structDeclaration> struct_declaration
%type <structDeclarationList> struct_declaration_list
%type <symbolList> enumerator_list
%type <symbol> enumerator foreach_identifier foreach_active_identifier
%type <enumType> enum_specifier
%type <type> specifier_qualifier_list struct_or_union_specifier
%type <type> struct_or_union_and_name
%type <type> type_specifier type_name rate_qualified_type_specifier
%type <type> short_vec_specifier
%type <typeList> type_specifier_list
%type <atomicType> atomic_var_type_specifier
%type <typeQualifier> type_qualifier type_qualifier_list
%type <storageClass> storage_class_specifier
%type <declSpecs> declaration_specifiers
%type <stringVal> string_constant
%type <constCharPtr> struct_or_union_name enum_identifier goto_identifier
%type <constCharPtr> foreach_unique_identifier
%type <intVal> int_constant soa_width_specifier rate_qualified_new
%type <foreachDimension> foreach_dimension_specifier
%type <foreachDimensionList> foreach_dimension_list
%type <declspecPair> declspec_item
%type <declspecList> declspec_specifier declspec_list
%start translation_unit
%%
string_constant
: TOKEN_STRING_LITERAL { $$ = new std::string(*yylval.stringVal); }
| string_constant TOKEN_STRING_LITERAL
{
std::string s = *((std::string *)$1);
s += *yylval.stringVal;
$$ = new std::string(s);
}
;
primary_expression
: TOKEN_IDENTIFIER {
const char *name = yylval.stringVal->c_str();
Symbol *s = m->symbolTable->LookupVariable(name);
$$ = NULL;
if (s)
$$ = new SymbolExpr(s, @1);
else {
std::vector<Symbol *> funs;
m->symbolTable->LookupFunction(name, &funs);
if (funs.size() > 0)
$$ = new FunctionSymbolExpr(name, funs, @1);
}
if ($$ == NULL) {
std::vector<std::string> alternates =
m->symbolTable->ClosestVariableOrFunctionMatch(name);
std::string alts = lGetAlternates(alternates);
Error(@1, "Undeclared symbol \"%s\".%s", name, alts.c_str());
}
}
| TOKEN_INT8_CONSTANT {
$$ = new ConstExpr(AtomicType::UniformInt8->GetAsConstType(),
(int8_t)yylval.intVal, @1);
}
| TOKEN_UINT8_CONSTANT {
$$ = new ConstExpr(AtomicType::UniformUInt8->GetAsConstType(),
(uint8_t)yylval.intVal, @1);
}
| TOKEN_INT16_CONSTANT {
$$ = new ConstExpr(AtomicType::UniformInt16->GetAsConstType(),
(int16_t)yylval.intVal, @1);
}
| TOKEN_UINT16_CONSTANT {
$$ = new ConstExpr(AtomicType::UniformUInt16->GetAsConstType(),
(uint16_t)yylval.intVal, @1);
}
| TOKEN_INT32_CONSTANT {
$$ = new ConstExpr(AtomicType::UniformInt32->GetAsConstType(),
(int32_t)yylval.intVal, @1);
}
| TOKEN_UINT32_CONSTANT {
$$ = new ConstExpr(AtomicType::UniformUInt32->GetAsConstType(),
(uint32_t)yylval.intVal, @1);
}
| TOKEN_INT64_CONSTANT {
$$ = new ConstExpr(AtomicType::UniformInt64->GetAsConstType(),
(int64_t)yylval.intVal, @1);
}
| TOKEN_UINT64_CONSTANT {
$$ = new ConstExpr(AtomicType::UniformUInt64->GetAsConstType(),
(uint64_t)yylval.intVal, @1);
}
| TOKEN_FLOAT_CONSTANT {
$$ = new ConstExpr(AtomicType::UniformFloat->GetAsConstType(),
yylval.floatVal, @1);
}
| TOKEN_DOUBLE_CONSTANT {
$$ = new ConstExpr(AtomicType::UniformDouble->GetAsConstType(),
yylval.doubleVal, @1);
}
| TOKEN_TRUE {
$$ = new ConstExpr(AtomicType::UniformBool->GetAsConstType(), true, @1);
}
| TOKEN_FALSE {
$$ = new ConstExpr(AtomicType::UniformBool->GetAsConstType(), false, @1);
}
| TOKEN_NULL {
$$ = new NullPointerExpr(@1);
}
/* | TOKEN_STRING_LITERAL
{ UNIMPLEMENTED }*/
| '(' expression ')' { $$ = $2; }
| '(' error ')' { $$ = NULL; }
;
launch_expression
: TOKEN_LAUNCH postfix_expression '(' argument_expression_list ')'
{
ConstExpr *oneExpr = new ConstExpr(AtomicType::UniformInt32, (int32_t)1, @2);
Expr *launchCount[3] = {oneExpr, oneExpr, oneExpr};
$$ = new FunctionCallExpr($2, $4, Union(@2, @5), true, launchCount);
}
| TOKEN_LAUNCH postfix_expression '(' ')'
{
ConstExpr *oneExpr = new ConstExpr(AtomicType::UniformInt32, (int32_t)1, @2);
Expr *launchCount[3] = {oneExpr, oneExpr, oneExpr};
$$ = new FunctionCallExpr($2, new ExprList(Union(@3,@4)), Union(@2, @4), true, launchCount);
}
| TOKEN_LAUNCH '[' assignment_expression ']' postfix_expression '(' argument_expression_list ')'
{
ConstExpr *oneExpr = new ConstExpr(AtomicType::UniformInt32, (int32_t)1, @5);
Expr *launchCount[3] = {$3, oneExpr, oneExpr};
$$ = new FunctionCallExpr($5, $7, Union(@5,@8), true, launchCount);
}
| TOKEN_LAUNCH '[' assignment_expression ']' postfix_expression '(' ')'
{
ConstExpr *oneExpr = new ConstExpr(AtomicType::UniformInt32, (int32_t)1, @5);
Expr *launchCount[3] = {$3, oneExpr, oneExpr};
$$ = new FunctionCallExpr($5, new ExprList(Union(@5,@6)), Union(@5,@7), true, launchCount);
}
| TOKEN_LAUNCH '[' assignment_expression ',' assignment_expression ']' postfix_expression '(' argument_expression_list ')'
{
ConstExpr *oneExpr = new ConstExpr(AtomicType::UniformInt32, (int32_t)1, @7);
Expr *launchCount[3] = {$3, $5, oneExpr};
$$ = new FunctionCallExpr($7, $9, Union(@7,@10), true, launchCount);
}
| TOKEN_LAUNCH '[' assignment_expression ',' assignment_expression ']' postfix_expression '(' ')'
{
ConstExpr *oneExpr = new ConstExpr(AtomicType::UniformInt32, (int32_t)1, @7);
Expr *launchCount[3] = {$3, $5, oneExpr};
$$ = new FunctionCallExpr($7, new ExprList(Union(@7,@8)), Union(@7,@9), true, launchCount);
}
| TOKEN_LAUNCH '[' assignment_expression ']' '[' assignment_expression ']' postfix_expression '(' argument_expression_list ')'
{
ConstExpr *oneExpr = new ConstExpr(AtomicType::UniformInt32, (int32_t)1, @8);
Expr *launchCount[3] = {$6, $3, oneExpr};
$$ = new FunctionCallExpr($8, $10, Union(@8,@11), true, launchCount);
}
| TOKEN_LAUNCH '[' assignment_expression ']' '[' assignment_expression ']' postfix_expression '(' ')'
{
ConstExpr *oneExpr = new ConstExpr(AtomicType::UniformInt32, (int32_t)1, @8);
Expr *launchCount[3] = {$6, $3, oneExpr};
$$ = new FunctionCallExpr($8, new ExprList(Union(@8,@9)), Union(@8,@10), true, launchCount);
}
| TOKEN_LAUNCH '[' assignment_expression ',' assignment_expression ',' assignment_expression ']' postfix_expression '(' argument_expression_list ')'
{
Expr *launchCount[3] = {$3, $5, $7};
$$ = new FunctionCallExpr($9, $11, Union(@9,@12), true, launchCount);
}
| TOKEN_LAUNCH '[' assignment_expression ',' assignment_expression ',' assignment_expression ']' postfix_expression '(' ')'
{
Expr *launchCount[3] = {$3, $5, $7};
$$ = new FunctionCallExpr($9, new ExprList(Union(@9,@10)), Union(@9,@11), true, launchCount);
}
| TOKEN_LAUNCH '[' assignment_expression ']' '[' assignment_expression ']' '[' assignment_expression ']' postfix_expression '(' argument_expression_list ')'
{
Expr *launchCount[3] = {$9, $6, $3};
$$ = new FunctionCallExpr($11, $13, Union(@11,@14), true, launchCount);
}
| TOKEN_LAUNCH '[' assignment_expression ']' '[' assignment_expression ']' '[' assignment_expression ']' postfix_expression '(' ')'
{
Expr *launchCount[3] = {$9, $6, $3};
$$ = new FunctionCallExpr($11, new ExprList(Union(@11,@12)), Union(@11,@13), true, launchCount);
}
| TOKEN_LAUNCH '<' postfix_expression '(' argument_expression_list ')' '>'
{
Error(Union(@2, @7), "\"launch\" expressions no longer take '<' '>' "
"around function call expression.");
$$ = NULL;
}
| TOKEN_LAUNCH '<' postfix_expression '(' ')' '>'
{
Error(Union(@2, @6), "\"launch\" expressions no longer take '<' '>' "
"around function call expression.");
$$ = NULL;
}
| TOKEN_LAUNCH '[' assignment_expression ']' '<' postfix_expression '(' argument_expression_list ')' '>'
{
Error(Union(@5, @10), "\"launch\" expressions no longer take '<' '>' "
"around function call expression.");
$$ = NULL;
}
| TOKEN_LAUNCH '[' assignment_expression ']' '<' postfix_expression '(' ')' '>'
{
Error(Union(@5, @9), "\"launch\" expressions no longer take '<' '>' "
"around function call expression.");
$$ = NULL;
}
;
postfix_expression
: primary_expression
| postfix_expression '[' expression ']'
{ $$ = new IndexExpr($1, $3, Union(@1,@4)); }
| postfix_expression '[' error ']'
{ $$ = NULL; }
| launch_expression
| postfix_expression '.' TOKEN_IDENTIFIER
{ $$ = MemberExpr::create($1, yytext, Union(@1,@3), @3, false); }
| postfix_expression TOKEN_PTR_OP TOKEN_IDENTIFIER
{ $$ = MemberExpr::create($1, yytext, Union(@1,@3), @3, true); }
| postfix_expression TOKEN_INC_OP
{ $$ = new UnaryExpr(UnaryExpr::PostInc, $1, Union(@1,@2)); }
| postfix_expression TOKEN_DEC_OP
{ $$ = new UnaryExpr(UnaryExpr::PostDec, $1, Union(@1,@2)); }
;
funcall_expression
: postfix_expression
| postfix_expression '(' ')'
{ $$ = new FunctionCallExpr($1, new ExprList(Union(@1,@2)), Union(@1,@3)); }
| postfix_expression '(' argument_expression_list ')'
{ $$ = new FunctionCallExpr($1, $3, Union(@1,@4)); }
| postfix_expression '(' error ')'
{ $$ = NULL; }
;
argument_expression_list
: assignment_expression { $$ = new ExprList($1, @1); }
| argument_expression_list ',' assignment_expression
{
ExprList *argList = llvm::dyn_cast<ExprList>($1);
if (argList == NULL) {
AssertPos(@1, m->errorCount > 0);
argList = new ExprList(@3);
}
argList->exprs.push_back($3);
argList->pos = Union(argList->pos, @3);
$$ = argList;
}
;
unary_expression
: funcall_expression
| TOKEN_INC_OP unary_expression
{ $$ = new UnaryExpr(UnaryExpr::PreInc, $2, Union(@1, @2)); }
| TOKEN_DEC_OP unary_expression
{ $$ = new UnaryExpr(UnaryExpr::PreDec, $2, Union(@1, @2)); }
| '&' unary_expression
{ $$ = new AddressOfExpr($2, Union(@1, @2)); }
| '*' unary_expression
{ $$ = new PtrDerefExpr($2, Union(@1, @2)); }
| '+' cast_expression
{ $$ = $2; }
| '-' cast_expression
{ $$ = new UnaryExpr(UnaryExpr::Negate, $2, Union(@1, @2)); }
| '~' cast_expression
{ $$ = new UnaryExpr(UnaryExpr::BitNot, $2, Union(@1, @2)); }
| '!' cast_expression
{ $$ = new UnaryExpr(UnaryExpr::LogicalNot, $2, Union(@1, @2)); }
| TOKEN_SIZEOF unary_expression
{ $$ = new SizeOfExpr($2, Union(@1, @2)); }
| TOKEN_SIZEOF '(' type_name ')'
{ $$ = new SizeOfExpr($3, Union(@1, @4)); }
;
cast_expression
: unary_expression
| '(' type_name ')' cast_expression
{
$$ = new TypeCastExpr($2, $4, Union(@1,@4));
}
;
multiplicative_expression
: cast_expression
| multiplicative_expression '*' cast_expression
{ $$ = MakeBinaryExpr(BinaryExpr::Mul, $1, $3, Union(@1, @3)); }
| multiplicative_expression '/' cast_expression
{ $$ = MakeBinaryExpr(BinaryExpr::Div, $1, $3, Union(@1, @3)); }
| multiplicative_expression '%' cast_expression
{ $$ = MakeBinaryExpr(BinaryExpr::Mod, $1, $3, Union(@1, @3)); }
;
additive_expression
: multiplicative_expression
| additive_expression '+' multiplicative_expression
{ $$ = MakeBinaryExpr(BinaryExpr::Add, $1, $3, Union(@1, @3)); }
| additive_expression '-' multiplicative_expression
{ $$ = MakeBinaryExpr(BinaryExpr::Sub, $1, $3, Union(@1, @3)); }
;
shift_expression
: additive_expression
| shift_expression TOKEN_LEFT_OP additive_expression
{ $$ = MakeBinaryExpr(BinaryExpr::Shl, $1, $3, Union(@1, @3)); }
| shift_expression TOKEN_RIGHT_OP additive_expression
{ $$ = MakeBinaryExpr(BinaryExpr::Shr, $1, $3, Union(@1, @3)); }
;
relational_expression
: shift_expression
| relational_expression '<' shift_expression
{ $$ = new BinaryExpr(BinaryExpr::Lt, $1, $3, Union(@1, @3)); }
| relational_expression '>' shift_expression
{ $$ = new BinaryExpr(BinaryExpr::Gt, $1, $3, Union(@1, @3)); }
| relational_expression TOKEN_LE_OP shift_expression
{ $$ = new BinaryExpr(BinaryExpr::Le, $1, $3, Union(@1, @3)); }
| relational_expression TOKEN_GE_OP shift_expression
{ $$ = new BinaryExpr(BinaryExpr::Ge, $1, $3, Union(@1, @3)); }
;
equality_expression
: relational_expression
| equality_expression TOKEN_EQ_OP relational_expression
{ $$ = new BinaryExpr(BinaryExpr::Equal, $1, $3, Union(@1,@3)); }
| equality_expression TOKEN_NE_OP relational_expression
{ $$ = new BinaryExpr(BinaryExpr::NotEqual, $1, $3, Union(@1,@3)); }
;
and_expression
: equality_expression
| and_expression '&' equality_expression
{ $$ = new BinaryExpr(BinaryExpr::BitAnd, $1, $3, Union(@1, @3)); }
;
exclusive_or_expression
: and_expression
| exclusive_or_expression '^' and_expression
{ $$ = new BinaryExpr(BinaryExpr::BitXor, $1, $3, Union(@1, @3)); }
;
inclusive_or_expression
: exclusive_or_expression
| inclusive_or_expression '|' exclusive_or_expression
{ $$ = new BinaryExpr(BinaryExpr::BitOr, $1, $3, Union(@1, @3)); }
;
logical_and_expression
: inclusive_or_expression
| logical_and_expression TOKEN_AND_OP inclusive_or_expression
{ $$ = new BinaryExpr(BinaryExpr::LogicalAnd, $1, $3, Union(@1, @3)); }
;
logical_or_expression
: logical_and_expression
| logical_or_expression TOKEN_OR_OP logical_and_expression
{ $$ = new BinaryExpr(BinaryExpr::LogicalOr, $1, $3, Union(@1, @3)); }
;
conditional_expression
: logical_or_expression
| logical_or_expression '?' expression ':' conditional_expression
{ $$ = new SelectExpr($1, $3, $5, Union(@1,@5)); }
;
rate_qualified_new
: TOKEN_NEW { $$ = 0; }
| TOKEN_UNIFORM TOKEN_NEW { $$ = TYPEQUAL_UNIFORM; }
| TOKEN_VARYING TOKEN_NEW { $$ = TYPEQUAL_VARYING; }
;
rate_qualified_type_specifier
: type_specifier { $$ = $1; }
| TOKEN_UNIFORM type_specifier
{
if ($2 == NULL)
$$ = NULL;
else if ($2->IsVoidType()) {
Error(@1, "\"uniform\" qualifier is illegal with \"void\" type.");
$$ = NULL;
}
else
$$ = $2->GetAsUniformType();
}
| TOKEN_VARYING type_specifier
{
if ($2 == NULL)
$$ = NULL;
else if ($2->IsVoidType()) {
Error(@1, "\"varying\" qualifier is illegal with \"void\" type.");
$$ = NULL;
}
else
$$ = $2->GetAsVaryingType();
}
| soa_width_specifier type_specifier
{
if ($2 == NULL)
$$ = NULL;
else {
int soaWidth = (int)$1;
const StructType *st = CastType<StructType>($2);
if (st == NULL) {
Error(@1, "\"soa\" qualifier is illegal with non-struct type \"%s\".",
$2->GetString().c_str());
$$ = NULL;
}
else if (soaWidth <= 0 || (soaWidth & (soaWidth - 1)) != 0) {
Error(@1, "soa<%d> width illegal. Value must be positive power "
"of two.", soaWidth);
$$ = NULL;
}
else
$$ = st->GetAsSOAType(soaWidth);
}
}
;
new_expression
: conditional_expression
| rate_qualified_new rate_qualified_type_specifier
{
$$ = new NewExpr((int32_t)$1, $2, NULL, NULL, @1, Union(@1, @2));
}
| rate_qualified_new rate_qualified_type_specifier '(' initializer_list ')'
{
$$ = new NewExpr((int32_t)$1, $2, $4, NULL, @1, Union(@1, @2));
}
| rate_qualified_new rate_qualified_type_specifier '[' expression ']'
{
$$ = new NewExpr((int32_t)$1, $2, NULL, $4, @1, Union(@1, @4));
}
;
assignment_expression
: new_expression
| unary_expression '=' assignment_expression
{ $$ = new AssignExpr(AssignExpr::Assign, $1, $3, Union(@1, @3)); }
| unary_expression TOKEN_MUL_ASSIGN assignment_expression
{ $$ = new AssignExpr(AssignExpr::MulAssign, $1, $3, Union(@1, @3)); }
| unary_expression TOKEN_DIV_ASSIGN assignment_expression
{ $$ = new AssignExpr(AssignExpr::DivAssign, $1, $3, Union(@1, @3)); }
| unary_expression TOKEN_MOD_ASSIGN assignment_expression
{ $$ = new AssignExpr(AssignExpr::ModAssign, $1, $3, Union(@1, @3)); }
| unary_expression TOKEN_ADD_ASSIGN assignment_expression
{ $$ = new AssignExpr(AssignExpr::AddAssign, $1, $3, Union(@1, @3)); }
| unary_expression TOKEN_SUB_ASSIGN assignment_expression
{ $$ = new AssignExpr(AssignExpr::SubAssign, $1, $3, Union(@1, @3)); }
| unary_expression TOKEN_LEFT_ASSIGN assignment_expression
{ $$ = new AssignExpr(AssignExpr::ShlAssign, $1, $3, Union(@1, @3)); }
| unary_expression TOKEN_RIGHT_ASSIGN assignment_expression
{ $$ = new AssignExpr(AssignExpr::ShrAssign, $1, $3, Union(@1, @3)); }
| unary_expression TOKEN_AND_ASSIGN assignment_expression
{ $$ = new AssignExpr(AssignExpr::AndAssign, $1, $3, Union(@1, @3)); }
| unary_expression TOKEN_XOR_ASSIGN assignment_expression
{ $$ = new AssignExpr(AssignExpr::XorAssign, $1, $3, Union(@1, @3)); }
| unary_expression TOKEN_OR_ASSIGN assignment_expression
{ $$ = new AssignExpr(AssignExpr::OrAssign, $1, $3, Union(@1, @3)); }
;
expression
: assignment_expression
| expression ',' assignment_expression
{ $$ = new BinaryExpr(BinaryExpr::Comma, $1, $3, Union(@1, @3)); }
;
constant_expression
: conditional_expression
;
declaration_statement
: declaration
{
if ($1 == NULL) {
AssertPos(@1, m->errorCount > 0);
$$ = NULL;
}
else if ($1->declSpecs->storageClass == SC_TYPEDEF) {
for (unsigned int i = 0; i < $1->declarators.size(); ++i) {
if ($1->declarators[i] == NULL)
AssertPos(@1, m->errorCount > 0);
else
m->AddTypeDef($1->declarators[i]->name,
$1->declarators[i]->type,
$1->declarators[i]->pos);
}
$$ = NULL;
}
else {
$1->DeclareFunctions();
std::vector<VariableDeclaration> vars = $1->GetVariableDeclarations();
$$ = new DeclStmt(vars, @1);
}
}
;
declaration
: declaration_specifiers ';'
{
$$ = new Declaration($1);
}
| declaration_specifiers init_declarator_list ';'
{
$$ = new Declaration($1, $2);
}
;
soa_width_specifier
: TOKEN_SOA '<' int_constant '>'
{ $$ = $3; }
;
declspec_item
: TOKEN_IDENTIFIER
{
std::pair<std::string, SourcePos> *p = new std::pair<std::string, SourcePos>;
p->first = *(yylval.stringVal);
p->second = @1;
$$ = p;
}
;
declspec_list
: declspec_item
{
$$ = new std::vector<std::pair<std::string, SourcePos> >;
$$->push_back(*$1);
}
| declspec_list ',' declspec_item
{
if ($1 != NULL)
$1->push_back(*$3);
$$ = $1;
}
;
declspec_specifier
: TOKEN_DECLSPEC '(' declspec_list ')'
{
$$ = $3;
}
;
declaration_specifiers
: storage_class_specifier
{
$$ = new DeclSpecs(NULL, $1);
}
| storage_class_specifier declaration_specifiers
{
DeclSpecs *ds = (DeclSpecs *)$2;
if (ds != NULL) {
if (ds->storageClass != SC_NONE)
Error(@1, "Multiple storage class specifiers in a declaration are illegal. "
"(Have provided both \"%s\" and \"%s\".)",
lGetStorageClassString(ds->storageClass),
lGetStorageClassString($1));
else
ds->storageClass = $1;
}
$$ = ds;
}
| declspec_specifier
{
$$ = new DeclSpecs;
if ($1 != NULL)
$$->declSpecList = *$1;
}
| declspec_specifier declaration_specifiers
{
DeclSpecs *ds = (DeclSpecs *)$2;
std::vector<std::pair<std::string, SourcePos> > *declSpecList = $1;
if (ds != NULL && declSpecList != NULL) {
for (int i = 0; i < (int)declSpecList->size(); ++i)
ds->declSpecList.push_back((*declSpecList)[i]);
}
$$ = ds;
}
| soa_width_specifier
{
DeclSpecs *ds = new DeclSpecs;
ds->soaWidth = (int32_t)$1;
$$ = ds;
}
| soa_width_specifier declaration_specifiers
{
DeclSpecs *ds = (DeclSpecs *)$2;
if (ds != NULL) {
if (ds->soaWidth != 0)
Error(@1, "soa<> qualifier supplied multiple times in declaration.");
else
ds->soaWidth = (int32_t)$1;
}
$$ = ds;
}
| type_specifier
{
$$ = new DeclSpecs($1);
}
| type_specifier '<' int_constant '>'
{
DeclSpecs *ds = new DeclSpecs($1);
ds->vectorSize = (int32_t)$3;
$$ = ds;
}
| type_specifier declaration_specifiers
{
DeclSpecs *ds = (DeclSpecs *)$2;
if (ds != NULL) {
if (ds->baseType != NULL)
Error(@1, "Multiple types provided for declaration.");
ds->baseType = $1;
}
$$ = ds;
}
| type_qualifier
{
$$ = new DeclSpecs(NULL, SC_NONE, $1);
}
| type_qualifier declaration_specifiers
{
DeclSpecs *ds = (DeclSpecs *)$2;
if (ds != NULL)
ds->typeQualifiers |= $1;
$$ = ds;
}
;
init_declarator_list
: init_declarator
{
std::vector<Declarator *> *dl = new std::vector<Declarator *>;
if ($1 != NULL)
dl->push_back($1);
$$ = dl;
}
| init_declarator_list ',' init_declarator
{
std::vector<Declarator *> *dl = (std::vector<Declarator *> *)$1;
if (dl == NULL) {
AssertPos(@1, m->errorCount > 0);
dl = new std::vector<Declarator *>;
}
if ($3 != NULL)
dl->push_back($3);
$$ = dl;
}
;
init_declarator
: declarator
| declarator '=' initializer
{
if ($1 != NULL)
$1->initExpr = $3;
$$ = $1;
}
;
storage_class_specifier
: TOKEN_TYPEDEF { $$ = SC_TYPEDEF; }
| TOKEN_EXTERN { $$ = SC_EXTERN; }
| TOKEN_EXTERN TOKEN_STRING_C_LITERAL { $$ = SC_EXTERN_C; }
| TOKEN_STATIC { $$ = SC_STATIC; }
;
type_specifier
: atomic_var_type_specifier { $$ = $1; }
| TOKEN_TYPE_NAME
{
const Type *t = m->symbolTable->LookupType(yytext);
$$ = t;
}
| struct_or_union_specifier { $$ = $1; }
| enum_specifier { $$ = $1; }
;
type_specifier_list
: type_specifier
{
if ($1 == NULL)
$$ = NULL;
else {
std::vector<std::pair<const Type *, SourcePos> > *vec =
new std::vector<std::pair<const Type *, SourcePos> >;
vec->push_back(std::make_pair($1, @1));
$$ = vec;
}
}
| type_specifier_list ',' type_specifier
{
$$ = $1;
if ($1 == NULL)
Assert(m->errorCount > 0);
else
$$->push_back(std::make_pair($3, @3));
}
;
atomic_var_type_specifier
: TOKEN_VOID { $$ = AtomicType::Void; }
| TOKEN_BOOL { $$ = AtomicType::UniformBool->GetAsUnboundVariabilityType(); }
| TOKEN_INT8 { $$ = AtomicType::UniformInt8->GetAsUnboundVariabilityType(); }
| TOKEN_INT16 { $$ = AtomicType::UniformInt16->GetAsUnboundVariabilityType(); }
| TOKEN_INT { $$ = AtomicType::UniformInt32->GetAsUnboundVariabilityType(); }
| TOKEN_FLOAT { $$ = AtomicType::UniformFloat->GetAsUnboundVariabilityType(); }
| TOKEN_DOUBLE { $$ = AtomicType::UniformDouble->GetAsUnboundVariabilityType(); }
| TOKEN_INT64 { $$ = AtomicType::UniformInt64->GetAsUnboundVariabilityType(); }
;
short_vec_specifier
: atomic_var_type_specifier '<' int_constant '>'
{
$$ = $1 ? new VectorType($1, (int32_t)$3) : NULL;
}
;
struct_or_union_name
: TOKEN_IDENTIFIER { $$ = strdup(yytext); }
| TOKEN_TYPE_NAME { $$ = strdup(yytext); }
;
struct_or_union_and_name
: struct_or_union struct_or_union_name
{
const Type *st = m->symbolTable->LookupType($2);
if (st == NULL) {
st = new UndefinedStructType($2, Variability::Unbound, false, @2);
m->symbolTable->AddType($2, st, @2);
$$ = st;
}
else {
if (CastType<StructType>(st) == NULL &&
CastType<UndefinedStructType>(st) == NULL) {
Error(@2, "Type \"%s\" is not a struct type! (%s)", $2,
st->GetString().c_str());
$$ = NULL;
}
else
$$ = st;
}
}
;
struct_or_union_specifier
: struct_or_union_and_name
| struct_or_union_and_name '{' struct_declaration_list '}'
{
if ($3 != NULL) {
llvm::SmallVector<const Type *, 8> elementTypes;
llvm::SmallVector<std::string, 8> elementNames;
llvm::SmallVector<SourcePos, 8> elementPositions;
GetStructTypesNamesPositions(*$3, &elementTypes, &elementNames,
&elementPositions);
const std::string &name = CastType<StructType>($1) ?
CastType<StructType>($1)->GetStructName() :
CastType<UndefinedStructType>($1)->GetStructName();
StructType *st = new StructType(name, elementTypes, elementNames,