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
1001
1002
1003
1004
1005
1006
|
%{
/********************************************************************
* AUTHORS: Vijay Ganesh, David L. Dill
*
* BEGIN DATE: November, 2005
*
* LICENSE: Please view LICENSE file in the home dir of this Program
********************************************************************/
// -*- c++ -*-
#include "../AST/AST.h"
using namespace std;
// Suppress the bogus warning suppression in bison (it generates
// compile error)
#undef __GNUC_MINOR__
extern int yylex(void);
extern char* yytext;
extern int yylineno;
int yyerror(const char *s) {
cout << "syntax error: line " << yylineno << "\n" << s << endl;
BEEV::FatalError("");
return 1; /* Dill: don't know what it should return */
};
#define YYLTYPE_IS_TRIVIAL 1
#define YYMAXDEPTH 10485760
#define YYERROR_VERBOSE 1
#define YY_EXIT_FAILURE -1
%}
%union {
unsigned int uintval; /* for numerals in types. */
struct {
//stores the indexwidth and valuewidth
//indexwidth is 0 iff type is bitvector. positive iff type is
//array, and stores the width of the indexing bitvector
unsigned int indexwidth;
//width of the bitvector type
unsigned int valuewidth;
} indexvaluewidth;
//ASTNode,ASTVec
BEEV::ASTNode *node;
BEEV::ASTVec *vec;
//Hash_Map to hold Array Updates during parse A map from array index
//to array values. To support the WITH construct
BEEV::ASTNodeMap * Index_To_UpdateValue;
};
%start cmd
%token AND_TOK "AND"
%token OR_TOK "OR"
%token NOT_TOK "NOT"
%token XOR_TOK "XOR"
%token NAND_TOK "NAND"
%token NOR_TOK "NOR"
%token IMPLIES_TOK "=>"
%token IFF_TOK "<=>"
%token IF_TOK "IF"
%token THEN_TOK "THEN"
%token ELSE_TOK "ELSE"
%token ELSIF_TOK "ELSIF"
%token END_TOK "END"
%token ENDIF_TOK "ENDIF"
%token NEQ_TOK "/="
%token ASSIGN_TOK ":="
%token BV_TOK "BV"
%token BVLEFTSHIFT_TOK "<<"
%token BVRIGHTSHIFT_TOK ">>"
%token BVPLUS_TOK "BVPLUS"
%token BVSUB_TOK "BVSUB"
%token BVUMINUS_TOK "BVUMINUS"
%token BVMULT_TOK "BVMULT"
%token BVDIV_TOK "BVDIV"
%token BVMOD_TOK "BVMOD"
%token SBVDIV_TOK "SBVDIV"
%token SBVMOD_TOK "SBVMOD"
%token BVNEG_TOK "~"
%token BVAND_TOK "&"
%token BVOR_TOK "|"
%token BVXOR_TOK "BVXOR"
%token BVNAND_TOK "BVNAND"
%token BVNOR_TOK "BVNOR"
%token BVXNOR_TOK "BVXNOR"
%token BVCONCAT_TOK "@"
%token BVLT_TOK "BVLT"
%token BVGT_TOK "BVGT"
%token BVLE_TOK "BVLE"
%token BVGE_TOK "BVGE"
%token BVSLT_TOK "BVSLT"
%token BVSGT_TOK "BVSGT"
%token BVSLE_TOK "BVSLE"
%token BVSGE_TOK "BVSGE"
%token BOOL_TO_BV_TOK "BOOLBV"
%token BVSX_TOK "BVSX"
%token BOOLEXTRACT_TOK "BOOLEXTRACT"
%token ASSERT_TOK "ASSERT"
%token QUERY_TOK "QUERY"
%token BOOLEAN_TOK "BOOLEAN"
%token ARRAY_TOK "ARRAY"
%token OF_TOK "OF"
%token WITH_TOK "WITH"
%token TRUELIT_TOK "TRUE"
%token FALSELIT_TOK "FALSE"
%token IN_TOK "IN"
%token LET_TOK "LET"
//%token COUNTEREXAMPLE_TOK "COUNTEREXAMPLE"
%token PUSH_TOK "PUSH"
%token POP_TOK "POP"
%left IN_TOK
%left XOR_TOK
%left IFF_TOK
%right IMPLIES_TOK
%left OR_TOK
%left AND_TOK
%left NAND_TOK
%left NOR_TOK
%left NOT_TOK
%left BVCONCAT_TOK
%left BVOR_TOK
%left BVAND_TOK
%left BVXOR_TOK
%left BVNAND_TOK
%left BVNOR_TOK
%left BVXNOR_TOK
%left BVNEG_TOK
%left BVLEFTSHIFT_TOK BVRIGHTSHIFT_TOK
%left WITH_TOK
%nonassoc '=' NEQ_TOK ASSIGN_TOK
%nonassoc BVLT_TOK BVLE_TOK BVGT_TOK BVGE_TOK
%nonassoc BVUMINUS_TOK BVPLUS_TOK BVSUB_TOK BVSX_TOK
%nonassoc '['
%nonassoc '{' '.' '('
%nonassoc BV_TOK
%type <vec> Exprs FORM_IDs reverseFORM_IDs
%type <vec> Asserts
%type <node> Expr Formula IfExpr ElseRestExpr IfForm ElseRestForm Assert Query ArrayUpdateExpr
%type <Index_To_UpdateValue> Updates
%type <indexvaluewidth> BvType BoolType ArrayType Type
%token <node> BVCONST_TOK
%token <node> TERMID_TOK FORMID_TOK COUNTEREXAMPLE_TOK
%token <uintval> NUMERAL_TOK
%%
cmd : other_cmd
| other_cmd counterexample
;
counterexample : COUNTEREXAMPLE_TOK ';'
{
BEEV::print_counterexample = true;
BEEV::globalBeevMgr_for_parser->PrintCounterExample(true);
}
;
other_cmd : other_cmd1
| Query
{
BEEV::globalBeevMgr_for_parser->TopLevelSAT(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::TRUE),*$1);
delete $1;
}
| VarDecls Query
{
BEEV::globalBeevMgr_for_parser->TopLevelSAT(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::TRUE),*$2);
delete $2;
}
| other_cmd1 Query
{
BEEV::ASTVec aaa = BEEV::globalBeevMgr_for_parser->GetAsserts();
if(aaa.size() == 0)
yyerror("Fatal Error: parsing: GetAsserts() call: no assertions: ");
if(aaa.size() == 1)
BEEV::globalBeevMgr_for_parser->TopLevelSAT(aaa[0],*$2);
else
BEEV::globalBeevMgr_for_parser->TopLevelSAT(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::AND,aaa),*$2);
delete $2;
}
;
other_cmd1 : VarDecls Asserts
{
delete $2;
}
| Asserts
{
delete $1;
}
| other_cmd1 VarDecls Asserts
{
delete $3;
}
;
/* push : PUSH_TOK */
/* { */
/* BEEV::globalBeevMgr_for_parser->Push(); */
/* } */
/* | */
/* ; */
/* pop : POP_TOK */
/* { */
/* BEEV::globalBeevMgr_for_parser->Pop(); */
/* } */
/* | */
/* ; */
Asserts : Assert
{
$$ = new BEEV::ASTVec;
$$->push_back(*$1);
BEEV::globalBeevMgr_for_parser->AddAssert(*$1);
delete $1;
}
| Asserts Assert
{
$1->push_back(*$2);
BEEV::globalBeevMgr_for_parser->AddAssert(*$2);
$$ = $1;
delete $2;
}
;
Assert : ASSERT_TOK Formula ';' { $$ = $2; }
;
Query : QUERY_TOK Formula ';' { BEEV::globalBeevMgr_for_parser->AddQuery(*$2); $$ = $2;}
;
/* Grammar for Variable Declaration */
VarDecls : VarDecl ';'
{
}
| VarDecls VarDecl ';'
{
}
;
VarDecl : FORM_IDs ':' Type
{
for(BEEV::ASTVec::iterator i=$1->begin(),iend=$1->end();i!=iend;i++) {
BEEV::_parser_symbol_table.insert(*i);
i->SetIndexWidth($3.indexwidth);
i->SetValueWidth($3.valuewidth);
//FIXME: HACK_ATTACK. this vector was hacked into the code to
//support a special request by Dawson' group. They want the
//counterexample to be printed in the order of variables declared.
BEEV::globalBeevMgr_for_parser->_special_print_set.push_back(*i);
}
delete $1;
}
| FORM_IDs ':' Type '=' Expr
{
//do type checking. if doesn't pass then abort
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*$5);
if($3.indexwidth != $5->GetIndexWidth())
yyerror("Fatal Error: parsing: LET Expr: Type check fail: ");
if($3.valuewidth != $5->GetValueWidth())
yyerror("Fatal Error: parsing: LET Expr: Type check fail: ");
for(BEEV::ASTVec::iterator i=$1->begin(),iend=$1->end();i!=iend;i++) {
//set the valuewidth of the identifier
i->SetValueWidth($5->GetValueWidth());
i->SetIndexWidth($5->GetIndexWidth());
BEEV::globalBeevMgr_for_parser->LetExprMgr(*i,*$5);
delete $5;
}
}
| FORM_IDs ':' Type '=' Formula
{
//do type checking. if doesn't pass then abort
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*$5);
if($3.indexwidth != $5->GetIndexWidth())
yyerror("Fatal Error: parsing: LET Expr: Type check fail: ");
if($3.valuewidth != $5->GetValueWidth())
yyerror("Fatal Error: parsing: LET Expr: Type check fail: ");
for(BEEV::ASTVec::iterator i=$1->begin(),iend=$1->end();i!=iend;i++) {
//set the valuewidth of the identifier
i->SetValueWidth($5->GetValueWidth());
i->SetIndexWidth($5->GetIndexWidth());
BEEV::globalBeevMgr_for_parser->LetExprMgr(*i,*$5);
delete $5;
}
}
;
reverseFORM_IDs : FORMID_TOK
{
$$ = new BEEV::ASTVec;
$$->push_back(*$1);
delete $1;
}
| FORMID_TOK ',' reverseFORM_IDs
{
$3->push_back(*$1);
$$ = $3;
delete $1;
}
;
FORM_IDs : reverseFORM_IDs
{
$$ = new BEEV::ASTVec($1->rbegin(),$1->rend());
delete $1;
}
;
/* Grammar for Types */
Type : BvType { $$ = $1; }
| BoolType { $$ = $1; }
| ArrayType { $$ = $1; }
;
BvType : BV_TOK '(' NUMERAL_TOK ')'
{
/*((indexwidth is 0) && (valuewidth>0)) iff type is BV*/
$$.indexwidth = 0;
unsigned int length = $3;
if(length > 0) {
$$.valuewidth = length;
}
else
BEEV::FatalError("Fatal Error: parsing: BITVECTORS must be of positive length: \n");
}
;
BoolType : BOOLEAN_TOK
{
$$.indexwidth = 0;
$$.valuewidth = 0;
}
;
ArrayType : ARRAY_TOK BvType OF_TOK BvType
{
$$.indexwidth = $2.valuewidth;
$$.valuewidth = $4.valuewidth;
}
;
/*Grammar for ITEs which are a type of Term*/
IfExpr : IF_TOK Formula THEN_TOK Expr ElseRestExpr
{
unsigned int width = $4->GetValueWidth();
if (width != $5->GetValueWidth())
yyerror("Width mismatch in IF-THEN-ELSE");
if($4->GetIndexWidth() != $5->GetIndexWidth())
yyerror("Width mismatch in IF-THEN-ELSE");
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*$2);
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*$4);
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*$5);
$$ = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::ITE, width, *$2, *$4, *$5));
$$->SetIndexWidth($5->GetIndexWidth());
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*$$);
delete $2;
delete $4;
delete $5;
}
;
ElseRestExpr : ELSE_TOK Expr ENDIF_TOK { $$ = $2; }
| ELSIF_TOK Expr THEN_TOK Expr ElseRestExpr
{
unsigned int width = $2->GetValueWidth();
if (width != $4->GetValueWidth() || width != $5->GetValueWidth())
yyerror("Width mismatch in IF-THEN-ELSE");
if ($2->GetIndexWidth() != $4->GetValueWidth() || $2->GetIndexWidth() != $5->GetValueWidth())
yyerror("Width mismatch in IF-THEN-ELSE");
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*$2);
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*$4);
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*$5);
$$ = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::ITE, width, *$2, *$4, *$5));
$$->SetIndexWidth($5->GetIndexWidth());
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*$$);
delete $2;
delete $4;
delete $5;
}
;
/* Grammar for formulas */
Formula : '(' Formula ')' { $$ = $2; }
| FORMID_TOK { $$ = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->ResolveID(*$1)); delete $1;}
| BOOLEXTRACT_TOK '(' Expr ',' NUMERAL_TOK ')'
{
unsigned int width = $3->GetValueWidth();
if(0 > (unsigned)$5 || width <= (unsigned)$5)
yyerror("Fatal Error: BOOLEXTRACT: trying to boolextract a bit which beyond range");
BEEV::ASTNode hi = BEEV::globalBeevMgr_for_parser->CreateBVConst(32, $5);
BEEV::ASTNode low = BEEV::globalBeevMgr_for_parser->CreateBVConst(32, $5);
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::BVEXTRACT,1,*$3,hi,low));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
BEEV::ASTNode zero = BEEV::globalBeevMgr_for_parser->CreateBVConst(1,0);
BEEV::ASTNode * out = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::EQ,*n,zero));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*out);
$$ = out;
delete $3;
}
| Expr '=' Expr
{
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::EQ, *$1, *$3));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $1;
delete $3;
}
| Expr NEQ_TOK Expr
{
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::NEQ, *$1, *$3));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $1;
delete $3;
}
| NOT_TOK Formula
{
$$ = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::NOT, *$2));
delete $2;
}
| Formula OR_TOK Formula %prec OR_TOK
{
$$ = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::OR, *$1, *$3));
delete $1;
delete $3;
}
| Formula NOR_TOK Formula
{
$$ = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::NOR, *$1, *$3));
delete $1;
delete $3;
}
| Formula AND_TOK Formula %prec AND_TOK
{
$$ = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::AND, *$1, *$3));
delete $1;
delete $3;
}
| Formula NAND_TOK Formula
{
$$ = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::NAND, *$1, *$3));
delete $1;
delete $3;
}
| Formula IMPLIES_TOK Formula
{
$$ = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::IMPLIES, *$1, *$3));
delete $1;
delete $3;
}
| Formula IFF_TOK Formula
{
$$ = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::IFF, *$1, *$3));
delete $1;
delete $3;
}
| Formula XOR_TOK Formula
{
$$ = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::XOR, *$1, *$3));
delete $1;
delete $3;
}
| BVLT_TOK '(' Expr ',' Expr ')'
{
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::BVLT, *$3, *$5));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $3;
delete $5;
}
| BVGT_TOK '(' Expr ',' Expr ')'
{
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::BVGT, *$3, *$5));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $3;
delete $5;
}
| BVLE_TOK '(' Expr ',' Expr ')'
{
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::BVLE, *$3, *$5));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $3;
delete $5;
}
| BVGE_TOK '(' Expr ',' Expr ')'
{
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::BVGE, *$3, *$5));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $3;
delete $5;
}
| BVSLT_TOK '(' Expr ',' Expr ')'
{
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::BVSLT, *$3, *$5));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $3;
delete $5;
}
| BVSGT_TOK '(' Expr ',' Expr ')'
{
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::BVSGT, *$3, *$5));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $3;
delete $5;
}
| BVSLE_TOK '(' Expr ',' Expr ')'
{
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::BVSLE, *$3, *$5));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $3;
delete $5;
}
| BVSGE_TOK '(' Expr ',' Expr ')'
{
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::BVSGE, *$3, *$5));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $3;
delete $5;
}
| IfForm
| TRUELIT_TOK
{
$$ = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::TRUE));
$$->SetIndexWidth(0);
$$->SetValueWidth(0);
}
| FALSELIT_TOK
{
$$ = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::FALSE));
$$->SetIndexWidth(0);
$$->SetValueWidth(0);
}
| LET_TOK LetDecls IN_TOK Formula
{
$$ = $4;
//Cleanup the LetIDToExprMap
BEEV::globalBeevMgr_for_parser->CleanupLetIDMap();
}
;
/*Grammar for ITEs which are Formulas */
IfForm : IF_TOK Formula THEN_TOK Formula ElseRestForm
{
$$ = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::ITE, *$2, *$4, *$5));
delete $2;
delete $4;
delete $5;
}
;
ElseRestForm : ELSE_TOK Formula ENDIF_TOK { $$ = $2; }
| ELSIF_TOK Formula THEN_TOK Formula ElseRestForm
{
$$ = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateNode(BEEV::ITE, *$2, *$4, *$5));
delete $2;
delete $4;
delete $5;
}
;
/*Grammar for a list of expressions*/
Exprs : Expr
{
$$ = new BEEV::ASTVec;
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*$1);
$$->push_back(*$1);
delete $1;
}
| Exprs ',' Expr
{
$1->push_back(*$3);
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*$3);
$$ = $1;
delete $3;
}
;
/* Grammar for Expr */
Expr : TERMID_TOK { $$ = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->ResolveID(*$1)); delete $1;}
| '(' Expr ')' { $$ = $2; }
| BVCONST_TOK { $$ = $1; }
| BOOL_TO_BV_TOK '(' Formula ')'
{
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*$3);
BEEV::ASTNode one = BEEV::globalBeevMgr_for_parser->CreateBVConst(1,1);
BEEV::ASTNode zero = BEEV::globalBeevMgr_for_parser->CreateBVConst(1,0);
//return ITE(*$3, length(1), 0bin1, 0bin0)
$$ = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::ITE,1,*$3,one,zero));
delete $3;
}
| Expr '[' Expr ']'
{
// valuewidth is same as array, indexwidth is 0.
unsigned int width = $1->GetValueWidth();
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::READ, width, *$1, *$3));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $1;
delete $3;
}
| Expr '(' Expr ')' //array read but in the form of a uninterpreted function application
{
// valuewidth is same as array, indexwidth is 0.
unsigned int width = $1->GetValueWidth();
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::READ, width, *$1, *$3));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $1;
delete $3;
}
| Expr '[' NUMERAL_TOK ':' NUMERAL_TOK ']'
{
int width = $3 - $5 + 1;
if (width < 0)
yyerror("Negative width in extract");
if((unsigned)$3 >= $1->GetValueWidth() || (unsigned)$5 < 0)
yyerror("Parsing: Wrong width in BVEXTRACT\n");
BEEV::ASTNode hi = BEEV::globalBeevMgr_for_parser->CreateBVConst(32, $3);
BEEV::ASTNode low = BEEV::globalBeevMgr_for_parser->CreateBVConst(32, $5);
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::BVEXTRACT, width, *$1,hi,low));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $1;
}
| BVNEG_TOK Expr
{
unsigned int width = $2->GetValueWidth();
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::BVNEG, width, *$2));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $2;
}
| Expr BVAND_TOK Expr
{
unsigned int width = $1->GetValueWidth();
if (width != $3->GetValueWidth()) {
yyerror("Width mismatch in AND");
}
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::BVAND, width, *$1, *$3));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $1;
delete $3;
}
| Expr BVOR_TOK Expr
{
unsigned int width = $1->GetValueWidth();
if (width != $3->GetValueWidth()) {
yyerror("Width mismatch in OR");
}
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::BVOR, width, *$1, *$3));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $1;
delete $3;
}
| BVXOR_TOK '(' Expr ',' Expr ')'
{
unsigned int width = $3->GetValueWidth();
if (width != $5->GetValueWidth()) {
yyerror("Width mismatch in XOR");
}
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::BVXOR, width, *$3, *$5));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $3;
delete $5;
}
| BVNAND_TOK '(' Expr ',' Expr ')'
{
unsigned int width = $3->GetValueWidth();
if (width != $5->GetValueWidth()) {
yyerror("Width mismatch in NAND");
}
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::BVNAND, width, *$3, *$5));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $3;
delete $5;
}
| BVNOR_TOK '(' Expr ',' Expr ')'
{
unsigned int width = $3->GetValueWidth();
if (width != $5->GetValueWidth()) {
yyerror("Width mismatch in NOR");
}
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::BVNOR, width, *$3, *$5));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $3;
delete $5;
}
| BVXNOR_TOK '(' Expr ',' Expr ')'
{
unsigned int width = $3->GetValueWidth();
if (width != $5->GetValueWidth()) {
yyerror("Width mismatch in NOR");
}
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::BVXNOR, width, *$3, *$5));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $3;
delete $5;
}
| BVSX_TOK '(' Expr ',' NUMERAL_TOK ')'
{
//width of the expr which is being sign
//extended. $5 is the resulting length of the
//signextended expr
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*$3);
if($3->GetValueWidth() == $5) {
$$ = $3;
}
else {
BEEV::ASTNode width = BEEV::globalBeevMgr_for_parser->CreateBVConst(32,$5);
BEEV::ASTNode *n =
new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::BVSX, $5,*$3,width));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $3;
}
}
| Expr BVCONCAT_TOK Expr
{
unsigned int width = $1->GetValueWidth() + $3->GetValueWidth();
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::BVCONCAT, width, *$1, *$3));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $1;
delete $3;
}
| Expr BVLEFTSHIFT_TOK NUMERAL_TOK
{
BEEV::ASTNode zero_bits = BEEV::globalBeevMgr_for_parser->CreateZeroConst($3);
BEEV::ASTNode * n =
new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::BVCONCAT,
$1->GetValueWidth() + $3, *$1, zero_bits));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $1;
}
| Expr BVRIGHTSHIFT_TOK NUMERAL_TOK
{
BEEV::ASTNode len = BEEV::globalBeevMgr_for_parser->CreateZeroConst($3);
unsigned int w = $1->GetValueWidth();
//the amount by which you are rightshifting
//is less-than/equal-to the length of input
//bitvector
if((unsigned)$3 < w) {
BEEV::ASTNode hi = BEEV::globalBeevMgr_for_parser->CreateBVConst(32,w-1);
BEEV::ASTNode low = BEEV::globalBeevMgr_for_parser->CreateBVConst(32,$3);
BEEV::ASTNode extract = BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::BVEXTRACT,w-$3,*$1,hi,low);
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::BVCONCAT, w,len, extract));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
}
else
$$ = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateZeroConst(w));
delete $1;
}
| BVPLUS_TOK '(' NUMERAL_TOK ',' Exprs ')'
{
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::BVPLUS, $3, *$5));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $5;
}
| BVSUB_TOK '(' NUMERAL_TOK ',' Expr ',' Expr ')'
{
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::BVSUB, $3, *$5, *$7));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $5;
delete $7;
}
| BVUMINUS_TOK '(' Expr ')'
{
unsigned width = $3->GetValueWidth();
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::BVUMINUS,width,*$3));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $3;
}
| BVMULT_TOK '(' NUMERAL_TOK ',' Expr ',' Expr ')'
{
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::BVMULT, $3, *$5, *$7));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $5;
delete $7;
}
| BVDIV_TOK '(' NUMERAL_TOK ',' Expr ',' Expr ')'
{
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::BVDIV, $3, *$5, *$7));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $5;
delete $7;
}
| BVMOD_TOK '(' NUMERAL_TOK ',' Expr ',' Expr ')'
{
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::BVMOD, $3, *$5, *$7));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $5;
delete $7;
}
| SBVDIV_TOK '(' NUMERAL_TOK ',' Expr ',' Expr ')'
{
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::SBVDIV, $3, *$5, *$7));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $5;
delete $7;
}
| SBVMOD_TOK '(' NUMERAL_TOK ',' Expr ',' Expr ')'
{
BEEV::ASTNode * n = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::SBVMOD, $3, *$5, *$7));
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*n);
$$ = n;
delete $5;
delete $7;
}
| IfExpr { $$ = $1; }
| ArrayUpdateExpr
| LET_TOK LetDecls IN_TOK Expr
{
$$ = $4;
//Cleanup the LetIDToExprMap
//BEEV::globalBeevMgr_for_parser->CleanupLetIDMap();
}
;
/*Grammar for Array Update Expr*/
ArrayUpdateExpr : Expr WITH_TOK Updates
{
BEEV::ASTNode * result;
unsigned int width = $1->GetValueWidth();
BEEV::ASTNodeMap::iterator it = $3->begin();
BEEV::ASTNodeMap::iterator itend = $3->end();
result = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::WRITE,
width,
*$1,
(*it).first,
(*it).second));
result->SetIndexWidth($1->GetIndexWidth());
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*result);
for(it++;it!=itend;it++) {
result = new BEEV::ASTNode(BEEV::globalBeevMgr_for_parser->CreateTerm(BEEV::WRITE,
width,
*result,
(*it).first,
(*it).second));
result->SetIndexWidth($1->GetIndexWidth());
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*result);
}
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*result);
$$ = result;
delete $3;
}
;
Updates : '[' Expr ']' ASSIGN_TOK Expr
{
$$ = new BEEV::ASTNodeMap();
(*$$)[*$2] = *$5;
}
| Updates WITH_TOK '[' Expr ']' ASSIGN_TOK Expr
{
(*$1)[*$4] = *$7;
}
;
/*Grammar for LET Expr*/
LetDecls : LetDecl
| LetDecls ',' LetDecl
;
LetDecl : FORMID_TOK '=' Expr
{
//Expr must typecheck
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*$3);
//set the valuewidth of the identifier
$1->SetValueWidth($3->GetValueWidth());
$1->SetIndexWidth($3->GetIndexWidth());
//populate the hashtable from LET-var -->
//LET-exprs and then process them:
//
//1. ensure that LET variables do not clash
//1. with declared variables.
//
//2. Ensure that LET variables are not
//2. defined more than once
BEEV::globalBeevMgr_for_parser->LetExprMgr(*$1,*$3);
delete $1;
delete $3;
}
| FORMID_TOK ':' Type '=' Expr
{
//do type checking. if doesn't pass then abort
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*$5);
if($3.indexwidth != $5->GetIndexWidth())
yyerror("Fatal Error: parsing: LET Expr: Type check fail: ");
if($3.valuewidth != $5->GetValueWidth())
yyerror("Fatal Error: parsing: LET Expr: Type check fail: ");
//set the valuewidth of the identifier
$1->SetValueWidth($5->GetValueWidth());
$1->SetIndexWidth($5->GetIndexWidth());
BEEV::globalBeevMgr_for_parser->LetExprMgr(*$1,*$5);
delete $1;
delete $5;
}
| FORMID_TOK '=' Formula
{
//Expr must typecheck
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*$3);
//set the valuewidth of the identifier
$1->SetValueWidth($3->GetValueWidth());
$1->SetIndexWidth($3->GetIndexWidth());
//Do LET-expr management
BEEV::globalBeevMgr_for_parser->LetExprMgr(*$1,*$3);
delete $1;
delete $3;
}
| FORMID_TOK ':' Type '=' Formula
{
//do type checking. if doesn't pass then abort
BEEV::globalBeevMgr_for_parser->BVTypeCheck(*$5);
if($3.indexwidth != $5->GetIndexWidth())
yyerror("Fatal Error: parsing: LET Expr: Type check fail: ");
if($3.valuewidth != $5->GetValueWidth())
yyerror("Fatal Error: parsing: LET Expr: Type check fail: ");
//set the valuewidth of the identifier
$1->SetValueWidth($5->GetValueWidth());
$1->SetIndexWidth($5->GetIndexWidth());
//Do LET-expr management
BEEV::globalBeevMgr_for_parser->LetExprMgr(*$1,*$5);
delete $1;
delete $5;
}
;
%%
|