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
|
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
|
-
+
-
+
-
+
-
+
+
-
+
-
-
-
+
+
+
-
-
-
+
+
+
+
-
+
+
+
-
+
+
-
+
+
-
+
-
+
+
+
+
+
+
+
+
+
-
+
-
-
+
+
-
+
-
-
+
+
-
-
+
-
-
+
-
-
-
-
+
+
+
-
-
-
+
+
-
+
+
-
+
-
+
-
-
-
+
+
+
-
-
-
+
+
+
+
-
+
+
+
-
+
+
-
+
+
-
+
-
+
-
-
-
+
+
+
-
-
-
+
+
+
-
+
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
-
+
+
+
+
+
+
-
+
+
+
+
-
+
+
-
-
+
+
-
-
+
+
-
+
-
+
-
+
-
+
-
+
-
+
+
+
+
-
-
+
+
+
+
+
-
+
-
+
+
+
+
+
-
-
+
+
-
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
-
-
-
-
+
+
+
+
+
+
-
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
-
-
-
-
+
+
-
-
-
-
+
+
+
+
-
-
-
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
-
+
-
+
-
-
-
+
+
-
-
-
+
+
+
+
+
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
+
+
-
-
+
+
-
+
-
-
+
+
|
else if( to.expr.is_bv() )
return GetAsBitVec( expr, to.type );
else
return expr;
}
template< typename I, typename F >
optional< Z3Val > BuildZ3UnaryExpr( Builder& b, const I& instr, F&& func )
bool BuildZ3UnaryExpr( Builder& b, const I& instr, F&& func )
{
auto operand = BuildZ3ExprFromValue( b, instr.operand() );
auto operand = b.pop();
if( !operand )
return nullopt;
return false;
return Z3Val{ func( operand->expr ), operand->type };
b.push( Z3Val{ func( operand->expr ), operand->type } );
return true;
}
template< typename I, typename F >
optional< Z3Val > BuildZ3BinExpr( Builder& b, const I& instr, F&& func )
bool BuildZ3BinExpr( Builder& b, const I& instr, F&& func )
{
auto lhs = BuildZ3ExprFromValue( b, instr.lhs() );
if( !lhs )
return nullopt;
auto rhs = b.pop();
if( !rhs )
return false;
auto rhs = BuildZ3ExprFromValue( b, instr.rhs() );
if( !rhs )
return nullopt;
auto lhs = b.pop();
if( !lhs )
return false;
if( lhs->expr.get_sort().sort_kind() == rhs->expr.get_sort().sort_kind() )
{
return Z3Val{ func( lhs->expr, rhs->expr, lhs->type ), lhs->type };
b.push( Z3Val{ func( lhs->expr, rhs->expr, lhs->type ), lhs->type } );
return true;
}
// If we are trying to do an operation on a mix of bitvec and int,
// convert the int to a bitvec first.
if( lhs->expr.is_bv() )
{
assert( rhs->expr.is_int() );
return Z3Val{ func( lhs->expr, GetAsBitVec( *rhs ), lhs->type ), lhs->type };
b.push( Z3Val{ func( lhs->expr, GetAsBitVec( *rhs ), lhs->type ), lhs->type } );
return true;
}
else
{
assert( lhs->expr.is_int() );
return Z3Val{ func( GetAsBitVec( *lhs ), rhs->expr, lhs->type ), lhs->type };
b.push( Z3Val{ func( GetAsBitVec( *lhs ), rhs->expr, lhs->type ), lhs->type } );
return true;
}
return nullopt;
return false;
}
template< typename I, typename F >
optional< Z3Val > BuildZ3BinBitwiseExpr( Builder& b, const I& instr, F&& func )
bool BuildZ3BinBitwiseExpr( Builder& b, const I& instr, F&& func )
{
auto rhs = b.pop();
if( !rhs )
return false;
auto lhs = b.pop();
if( !lhs )
return false;
if( instr.lhs().type() == GetValueType< BigInt >() )
if( ValueToEIR( lhs->type ) == GetValueType< BigInt >() )
{
DiagnosticsManager::GetInstance().emitErrorMessage( instr.lhs().locationId(), "verifier error: bitwise operands can't be ct_int." );
return nullopt;
DiagnosticsManager::GetInstance().emitErrorMessage( 0, "verifier error: bitwise operands can't be ct_int." );
return false;
}
if( instr.rhs().type() == GetValueType< BigInt >() )
if( ValueToEIR( rhs->type ) == GetValueType< BigInt >() )
{
DiagnosticsManager::GetInstance().emitErrorMessage( instr.rhs().locationId(), "verifier error: bitwise operands can't be ct_int." );
return nullopt;
DiagnosticsManager::GetInstance().emitErrorMessage( 0, "verifier error: bitwise operands can't be ct_int." );
return false;
}
auto lhs = BuildZ3ExprFromValue( b, instr.lhs() );
if( !lhs )
if( lhs->expr.get_sort().sort_kind() == rhs->expr.get_sort().sort_kind() )
return nullopt;
{
auto rhs = BuildZ3ExprFromValue( b, instr.rhs() );
if( !rhs )
return nullopt;
b.push( Z3Val{ func( lhs->expr, rhs->expr, lhs->type ), lhs->type } );
return true;
}
if( lhs->expr.get_sort().sort_kind() == rhs->expr.get_sort().sort_kind() )
return Z3Val{ func( lhs->expr, rhs->expr, lhs->type ), lhs->type };
// If we are trying to do an operation on a mix of bitvec and int,
// convert the int to a bitvec first.
if( lhs->expr.is_bv() )
{
assert( rhs->expr.is_int() );
return Z3Val{ func( lhs->expr, GetAsBitVec( *rhs ), lhs->type ), lhs->type };
b.push( Z3Val{ func( lhs->expr, GetAsBitVec( *rhs ), lhs->type ), lhs->type } );
return true;
}
else
{
assert( lhs->expr.is_int() );
return Z3Val{ func( GetAsBitVec( *lhs ), rhs->expr, lhs->type ), lhs->type };
b.push( Z3Val{ func( GetAsBitVec( *lhs ), rhs->expr, lhs->type ), lhs->type } );
return true;
}
return nullopt;
return false;
}
template< typename I, typename F >
optional< Z3Val > BuildZ3BinBoolExpr( Builder& b, const I& instr, F&& func )
bool BuildZ3BinBoolExpr( Builder& b, const I& instr, F&& func )
{
auto lhs = BuildZ3ExprFromValue( b, instr.lhs() );
if( !lhs )
return nullopt;
auto rhs = b.pop();
if( !rhs )
return false;
auto rhs = BuildZ3ExprFromValue( b, instr.rhs() );
if( !rhs )
return nullopt;
auto lhs = b.pop();
if( !lhs )
return false;
if( lhs->expr.get_sort().sort_kind() == rhs->expr.get_sort().sort_kind() )
{
return Z3Val{ func( lhs->expr, rhs->expr, lhs->type ), *EIRToValue( GetValueType< bool >() ) };
b.push( Z3Val{ func( lhs->expr, rhs->expr, lhs->type ), *EIRToValue( GetValueType< bool >() ) } );
return true;
}
// If we are trying to do an operation on a mix of bitvec and int,
// convert the int to a bitvec first.
if( lhs->expr.is_bv() )
{
assert( rhs->expr.is_int() );
return Z3Val{ func( lhs->expr, GetAsBitVec( *rhs ), lhs->type ), *EIRToValue( GetValueType< bool >() ) };
b.push( Z3Val{ func( lhs->expr, GetAsBitVec( *rhs ), lhs->type ), *EIRToValue( GetValueType< bool >() ) } );
return true;
}
else
{
assert( lhs->expr.is_int() );
return Z3Val{ func( GetAsBitVec( *lhs ), rhs->expr, lhs->type ), *EIRToValue( GetValueType< bool >() ) };
b.push( Z3Val{ func( GetAsBitVec( *lhs ), rhs->expr, lhs->type ), *EIRToValue( GetValueType< bool >() ) } );
return true;
}
return nullopt;
return false;
}
template< typename I, typename F >
optional< Z3Val > BuildZ3BinLogicExpr( Builder& b, const I& instr, F&& func )
bool BuildZ3BinLogicExpr( Builder& b, const I& instr, F&& func )
{
auto lhs = BuildZ3ExprFromValue( b, instr.lhs() );
if( !lhs )
return nullopt;
auto rhs = b.pop();
if( !rhs )
return false;
auto rhs = BuildZ3ExprFromValue( b, instr.rhs() );
if( !rhs )
return nullopt;
auto lhs = b.pop();
if( !lhs )
return false;
if( !lhs->expr.is_bool() )
return nullopt;
return false;
if( !rhs->expr.is_bool() )
return nullopt;
return false;
return Z3Val{ func( lhs->expr, rhs->expr, lhs->type ), *EIRToValue( GetValueType< bool >() ) };
b.push( Z3Val{ func( lhs->expr, rhs->expr, lhs->type ), *EIRToValue( GetValueType< bool >() ) } );
return true;
}
bool BuildZ3Op( Builder& b, const cir::InstrSeq& is )
{
for( const auto& instr : is )
{
if( !BuildZ3Op( b, instr ) )
return false;
}
return true;
}
template< typename T >
optional< Z3Val > BuildZ3Op( Builder& b, const T& instr )
bool BuildZ3Op( Builder& b, const T& instr )
{
return nullopt;
return false;
}
optional< Z3Val > BuildZ3Op( Builder& b, const Load& instr )
bool BuildZ3Op( Builder& b, const Load& instr )
{
optional< Z3Val > zv;
if( auto cstAddr = b.pop< Address >() )
zv = LoadFromAddress( b, *cstAddr, instr.type() );
else if( auto gfa = b.pop< GhostFuncApplication >() )
auto zv = LoadFromAddress( b, *instr.addr() );
zv = LoadFromAddress( b, *gfa );
else if( auto symAddr = b.pop< Z3Val >() )
zv = BuildZ3ConstantFromType( b, instr.type(), format( "val{}", b.newUniqueId() ) );
if( !zv )
return nullopt;
return false;
if( b.mustLoadAssume() )
ForEachPredicate( b, instr.type(), zv->expr, [&]( auto&& z3expr, auto locId )
{
ForEachPredicate( b, instr.type(), zv->expr, [&]( auto&& z3expr, auto locId )
{
if( b.mustLoadAssume() )
b.assume( z3expr );
} );
} );
b.push( move( *zv ) );
return zv;
return true;
}
optional< Z3Val > BuildZ3Op( Builder& b, const Store& instr )
bool BuildZ3Op( Builder& b, const Store& instr )
{
auto zv = BuildZ3ExprFromValue( b, instr.val() );
auto zv = b.pop();
if( !zv )
return nullopt;
return false;
ForEachPredicate( b, instr.type(), zv->expr, [&]( auto&& z3expr, auto locId )
{
if( !instr.destLocId().invalid() && !instr.val().locationId().invalid() )
if( !instr.srcLocId().invalid() && !instr.destLocId().invalid() )
{
DiagnosticsContext dc( instr.destLocId(), "...to this." );
DiagnosticsContext dc2( instr.val().locationId(), "When assigning this..." );
DiagnosticsContext dc2( instr.srcLocId(), "When assigning this..." );
b.checkAssertion( z3expr, locId );
}
else
b.checkAssertion( z3expr, locId );
} );
if( auto cstAddr = b.pop< Address >() )
StoreToAddress( b, *cstAddr, move( *zv ) );
else if( auto gfa = b.pop< GhostFuncApplication >() )
StoreToAddress( b, *instr.addr(), move( *zv ) );
return nullopt;
StoreToAddress( b, *gfa, move( *zv ) );
else
b.pop();
return true;
}
// Implemented in call.cpp
extern optional< Z3Val > BuildZ3Op( Builder& b, const Call& instr );
extern bool BuildZ3Op( Builder& b, const Call& instr );
optional< Z3Val > BuildZ3Op( Builder& b, const CreateTemporary& instr )
bool BuildZ3Op( Builder& b, const CreateTemporary& instr )
{
auto zv = b.pop();
if( !zv )
return false;
b.setVar( instr.index(), instr.value() );
return nullopt;
b.setVar( instr.index(), move( *zv ) );
return true;
}
optional< Z3Val > BuildZ3Op( Builder& b, const GetTemporary& instr )
bool BuildZ3Op( Builder& b, const GetTemporary& instr )
{
auto zv = b.retrieveVar( instr.index() );
if( zv )
{
b.push( move( *zv ) );
return zv;
return BuildZ3ConstantFromType( b, instr.type(), format( "v{}", instr.index() ) );
}
optional< Z3Val > BuildZ3Op( Builder& b, const AllocVar& instr )
return true;
}
auto result = BuildZ3ConstantFromType( b, instr.type(), format( "v{}", instr.index() ) );
if( !result )
return false;
b.push( move( *result ) );
return true;
}
bool BuildZ3Op( Builder& b, const AllocVar& instr )
{
auto tinfo = TypeCache::GetInstance()->getTypeInfo( b.context(), ValueToEIR( instr.type() ) );
if( !tinfo )
return nullopt;
return false;
b.setVar( instr.index(), Z3Val{ tinfo->undefined( b ), instr.type() } );
return nullopt;
return true;
}
// Implemented in phi.cpp
optional< Z3Val > BuildZ3Op( Builder& b, const Phi& instr );
bool BuildZ3Op( Builder& b, const Phi& instr );
// TODO: LoadConstStr. Build a z3 str value.
optional< Z3Val > BuildZ3Op( Builder& b, const Not& instr )
bool BuildZ3Op( Builder& b, const Not& instr )
{
return BuildZ3UnaryExpr( b, instr, []( auto&& operand )
{
if( operand.is_bool() )
return !operand;
assert( operand.is_bv() );
return ~operand;
} );
}
optional< Z3Val > BuildZ3Op( Builder& b, const And& instr )
bool BuildZ3Op( Builder& b, const And& instr )
{
return BuildZ3BinBitwiseExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type )
{
if( lhs.is_bool() && rhs.is_bool() )
return lhs && rhs;
auto lhsBV = GetAsBitVec( lhs, type );
auto rhsBV = GetAsBitVec( rhs, type );
return lhsBV & rhsBV;
} );
}
optional< Z3Val > BuildZ3Op( Builder& b, const Or& instr )
bool BuildZ3Op( Builder& b, const Or& instr )
{
return BuildZ3BinBitwiseExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type )
{
if( lhs.is_bool() && rhs.is_bool() )
return lhs || rhs;
auto lhsBV = GetAsBitVec( lhs, type );
auto rhsBV = GetAsBitVec( rhs, type );
return lhsBV | rhsBV;
} );
}
optional< Z3Val > BuildZ3Op( Builder& b, const Xor& instr )
bool BuildZ3Op( Builder& b, const Xor& instr )
{
return BuildZ3BinBitwiseExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type )
{
if( lhs.is_bool() && rhs.is_bool() )
return lhs ^ rhs;
auto lhsBV = GetAsBitVec( lhs, type );
auto rhsBV = GetAsBitVec( rhs, type );
return lhsBV ^ rhsBV;
} );
}
optional< Z3Val > BuildZ3Op( Builder& b, const Implies& instr )
bool BuildZ3Op( Builder& b, const Implies& instr )
{
return BuildZ3BinLogicExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type )
{
return z3::implies( lhs, rhs );
} );
}
optional< Z3Val > BuildZ3Op( Builder& b, const Shl& instr )
bool BuildZ3Op( Builder& b, const Shl& instr )
{
return BuildZ3BinBitwiseExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type )
{
auto lhsBV = GetAsBitVec( lhs, type );
auto rhsBV = GetAsBitVec( rhs, type );
return z3::shl( lhsBV, rhsBV );
} );
}
optional< Z3Val > BuildZ3Op( Builder& b, const LShr& instr )
bool BuildZ3Op( Builder& b, const LShr& instr )
{
return BuildZ3BinBitwiseExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type )
{
auto lhsBV = GetAsBitVec( lhs, type );
auto rhsBV = GetAsBitVec( rhs, type );
return z3::lshr( lhsBV, rhsBV );
} );
}
optional< Z3Val > BuildZ3Op( Builder& b, const AShr& instr )
bool BuildZ3Op( Builder& b, const AShr& instr )
{
return BuildZ3BinBitwiseExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type )
{
auto lhsBV = GetAsBitVec( lhs, type );
auto rhsBV = GetAsBitVec( rhs, type );
return z3::ashr( lhsBV, rhsBV );
} );
}
optional< Z3Val > BuildZ3Op( Builder& b, const Add& instr )
bool BuildZ3Op( Builder& b, const Add& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return lhs + rhs; } );
}
optional< Z3Val > BuildZ3Op( Builder& b, const Sub& instr )
bool BuildZ3Op( Builder& b, const Sub& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return lhs - rhs; } );
}
optional< Z3Val > BuildZ3Op( Builder& b, const Mul& instr )
bool BuildZ3Op( Builder& b, const Mul& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return lhs * rhs; } );
}
optional< Z3Val > BuildZ3Op( Builder& b, const UDiv& instr )
bool BuildZ3Op( Builder& b, const UDiv& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return z3::udiv( lhs, rhs ); } );
}
optional< Z3Val > BuildZ3Op( Builder& b, const SDiv& instr )
bool BuildZ3Op( Builder& b, const SDiv& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return lhs / rhs; } );
}
optional< Z3Val > BuildZ3Op( Builder& b, const URem& instr )
bool BuildZ3Op( Builder& b, const URem& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return z3::urem( lhs, rhs ); } );
}
optional< Z3Val > BuildZ3Op( Builder& b, const SRem& instr )
bool BuildZ3Op( Builder& b, const SRem& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type )
{
if( lhs.is_bv() )
return z3::srem( lhs, rhs );
return z3::rem( lhs, rhs );
} );
}
optional< Z3Val > BuildZ3Op( Builder& b, const Eq& instr )
bool BuildZ3Op( Builder& b, const Eq& instr )
{
return BuildZ3BinBoolExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return lhs == rhs; } );
}
optional< Z3Val > BuildZ3Op( Builder& b, const Neq& instr )
bool BuildZ3Op( Builder& b, const Neq& instr )
{
return BuildZ3BinBoolExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return lhs != rhs; } );
}
optional< Z3Val > BuildZ3Op( Builder& b, const UGT& instr )
bool BuildZ3Op( Builder& b, const UGT& instr )
{
return BuildZ3BinBoolExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return z3::ugt( lhs, rhs ); } );
}
optional< Z3Val > BuildZ3Op( Builder& b, const UGE& instr )
bool BuildZ3Op( Builder& b, const UGE& instr )
{
return BuildZ3BinBoolExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return z3::uge( lhs, rhs ); } );
}
optional< Z3Val > BuildZ3Op( Builder& b, const ULT& instr )
bool BuildZ3Op( Builder& b, const ULT& instr )
{
return BuildZ3BinBoolExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return z3::ult( lhs, rhs ); } );
}
optional< Z3Val > BuildZ3Op( Builder& b, const ULE& instr )
bool BuildZ3Op( Builder& b, const ULE& instr )
{
return BuildZ3BinBoolExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return z3::ule( lhs, rhs ); } );
}
optional< Z3Val > BuildZ3Op( Builder& b, const SGT& instr )
bool BuildZ3Op( Builder& b, const SGT& instr )
{
return BuildZ3BinBoolExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return lhs > rhs; } );
}
optional< Z3Val > BuildZ3Op( Builder& b, const SGE& instr )
bool BuildZ3Op( Builder& b, const SGE& instr )
{
return BuildZ3BinBoolExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return lhs >= rhs; } );
}
optional< Z3Val > BuildZ3Op( Builder& b, const SLT& instr )
bool BuildZ3Op( Builder& b, const SLT& instr )
{
return BuildZ3BinBoolExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return lhs < rhs; } );
}
optional< Z3Val > BuildZ3Op( Builder& b, const SLE& instr )
bool BuildZ3Op( Builder& b, const SLE& instr )
{
return BuildZ3BinBoolExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return lhs <= rhs; } );
}
optional< Z3Val > BuildZ3Op( Builder& b, const Assert& instr )
bool BuildZ3Op( Builder& b, const Assert& instr )
{
// TODO: refactor the return value here to be able to indicate failure versus "nothing to do"
auto cond = BuildZ3ExprFromValue( b, instr.cond() );
if( cond )
b.checkAssertion( cond->expr, instr.cond().locationId() );
return nullopt;
auto cond = b.pop();
if( !cond )
return false;
b.checkAssertion( cond->expr, instr.locationId() );
return true;
}
optional< Z3Val > BuildZ3Op( Builder& b, const Placeholder& instr )
bool BuildZ3Op( Builder& b, const Placeholder& instr )
{
const auto* expr = b.retrievePlaceholder( instr.name() );
if( expr )
{
return Z3Val{ *expr, *EIRToValue( instr.type() ) };
return BuildZ3ConstantFromType( b, instr.type(), format( "p{}", instr.name().str() ) );
}
optional< Z3Val > BuildZ3Op( Builder& b, const PHOverride& instr )
b.push( Z3Val{ *expr, *EIRToValue( instr.type() ) } );
return true;
}
auto result = BuildZ3ConstantFromType( b, instr.type(), format( "p{}", instr.name().str() ) );
if( !result )
return false;
b.push( move( *result ) );
return true;
}
bool BuildZ3Op( Builder& b, const PHOverrideSet& instr )
{
const auto* savedExpr = b.retrievePlaceholder( instr.name() );
auto phExpr = b.pop();
if( !phExpr )
return false;
auto newExpr = BuildZ3ExprFromValue( b, instr.phVal() );
if( newExpr )
b.setPlaceholder( instr.name(), newExpr->expr );
else
b.setPlaceholder( instr.name(), phExpr->expr );
return true;
b.unsetPlaceholder( instr.name() );
auto result = BuildZ3ExprFromValue( b, instr.val() );
}
bool BuildZ3Op( Builder& b, const PHOverrideClear& instr )
{
if( savedExpr )
b.setPlaceholder( instr.name(), *savedExpr );
else
b.unsetPlaceholder( instr.name() );
b.unsetPlaceholder( instr.name() );
return result;
return true;
}
bool BuildZ3Op( Builder& b, const Constant& instr )
{
auto v = BuildZ3ExprFromValue( b, instr.value() );
if( v )
b.push( move( *v ) );
else
b.push( instr.value() );
return true;
}
bool BuildZ3Op( Builder& b, const VarAddr& instr )
{
b.push( Address( Address::Origin::Stack, instr.varIndex() ) );
return true;
}
bool BuildZ3Op( Builder& b, const TempAddr& instr )
{
auto initVal = b.pop();
if( !initVal )
return false;
b.push( Address( Address::Origin::Stack, instr.tempIndex() ) );
return !!b.setVar( instr.tempIndex(), move( *initVal ) );
optional< Z3Val > BuildZ3Op( Builder& b, const VarAddr& instr )
}
bool BuildZ3Op( Builder& b, const Select& instr )
{
return Z3Val
if( auto cstBaseLoc = b.pop< Address >() )
{
GetZ3Context().int_val( instr.varIndex() ).unit(),
b.push( Address::Select( move( *cstBaseLoc ), instr.memberIndex() ) );
*EIRToValue( GetValueType< builtins::MemLoc >() )
};
}
return true;
}
optional< Z3Val > BuildZ3Op( Builder& b, const TempAddr& instr )
{
return Z3Val
auto symBaseLoc = b.pop();
if( !symBaseLoc )
return false;
b.push( Z3Val
{
GetZ3Context().int_val( instr.tempIndex() ).unit(),
z3::concat( symBaseLoc->expr, GetZ3Context().int_val( instr.memberIndex() ).unit() ),
*EIRToValue( GetValueType< builtins::MemLoc >() )
};
}
} );
return true;
}
bool BuildZ3Op( Builder& b, const GhostCall& instr )
{
auto func = b.pop< Value >();
if( !func )
return false;
auto argCount = instr.numArgs();
llvm::SmallVector< Z3Val, 16 > args;
args.reserve( argCount );
optional< Z3Val > BuildZ3Op( Builder& b, const Select& instr )
{
auto baseLoc = BuildZ3Op( b, *instr.baseAddr() );
if( !baseLoc )
return nullopt;
for( uint32_t argIndex = 0; argIndex < argCount; ++argIndex )
{
auto zv = b.pop();
if( !zv )
return false;
args.emplace_back( move( *zv ) );
}
return Z3Val
{
z3::concat( baseLoc->expr, GetZ3Context().int_val( instr.memberIndex() ).unit() ),
GhostFuncApplication gfa( move( *func ) );
gfa.args().reserve( args.size() );
for( auto it = args.rbegin(); it != args.rend(); ++it )
gfa.args().emplace_back( move( *it ) );
*EIRToValue( GetValueType< builtins::MemLoc >() )
};
b.push( move( gfa ) );
return true;
}
optional< Z3Val > BuildZ3Op( Builder& b, const cir::Instruction& instr )
bool BuildZ3Op( Builder& b, const cir::Instruction& instr )
{
return visit( [&]( auto&& e )
{
return BuildZ3Op( b, e );
}, instr.content() );
}
optional< Z3Val > BuildZ3ExprFromValue( Builder& b, const Value& val )
{
if( val.isPoison() )
return nullopt;
if( val.isConstant() )
return BuildZ3ValFromConstant( b, val );
if( auto expr = BuildZ3Op( b, *val.cir() ) )
return expr;
if( BuildZ3Op( b, *val.cir() ) )
return b.pop();
return BuildZ3ConstantFromType( b, val.type(), format( "val{}", b.newUniqueId() ) );
}
}
|