-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreg.c
727 lines (684 loc) · 22 KB
/
reg.c
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
/*******************************************************************************
*
* Chronos: A Timing Analyzer for Embedded Software
* =============================================================================
* http://www.comp.nus.edu.sg/~rpembed/chronos/
*
* Symbolic Execution & Infeasible path detection on Chronos
* Vivy Suhendra - Huynh Bach Khoa
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* 03/2007 reg.c
*
* v4.1:
* _ completely rewrite the symbolic execution framework
* + intraprocedure execution, instead of intra basic block
* + memory modeling -> can trace value when saved to memory
* + richer value types: induction, expression, parameter
*
* v4.0: as in Chronos-4.0 distribution
*
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "symexec.h"
#include "address.h"
#include "reg.h"
static FILE *dbgF;
void setDebugFile(char *fName) {
dbgF = fopen(fName,"w");
}
/*** REGISTER OPERATIONS ***/
void initReg(reg_t *reg) {
//NOTE: cause memory leaks if pointer used
//use clrReg reinitialize without causing mem.leak
reg->t = VALUE_UNDEF;
reg->val = 0;
reg->expr = NULL;
reg->biv = NULL;
reg->para[0]='\0';
reg->name[0]='\0';
reg->flag = 0;
}
static
void panicRegType(reg_t reg) {
int dbg = 1;
if (dbg) {
fprintf(dbgF,"\nPanic: unknown reg value type: %d, reinit",reg.t);
printf("\nPanic: unknown reg value type: %d, reinit",reg.t);
fflush(dbgF);
fflush(stdout);
}
exit(1);
}
void printReg(FILE *fp, reg_t reg) {
//fprintf(fp,"{%d,",reg.t);
fprintf(fp,"{");
switch (reg.t) {
case VALUE_UNDEF:
fprintf(fp,"_");
break;
case VALUE_CONST:
fprintf(fp,"%lx",reg.val);
break;
case VALUE_EXPR:
printExpr(fp,reg.expr);
break;
case VALUE_PARA:
fprintf(fp,"%s",reg.para);
break;
case VALUE_BIV:
printBIV(fp,reg.biv);
break;
default:
panicRegType(reg);
}
fprintf(fp,"}");
}
UNUSED
void printRegList(FILE *fp, reg_t *regList) {
int i;
for (i=0; i<NO_REG; i++) {
fprintf(fp,"\n%s -> ",regList[i].name);
printReg(fp,regList[i]);
}
fflush(fp);
}
void setInt(reg_t *reg, long k) {
int dbg = 0;
if (reg->t !=VALUE_CONST) {clrReg(reg);reg->t = VALUE_CONST;}
reg->val = k;
if (dbg) {fprintf(dbgF,"\n Set int: ");printReg(dbgF,*reg);}
}
void clrReg(reg_t *reg) {
//NOTE: avoid continually alloc/realloc
switch (reg->t) {
case VALUE_UNDEF: //undef
case VALUE_CONST: //const
case VALUE_PARA: //para
break;
case VALUE_BIV: //induction
if (reg->biv) {
clrReg( &((reg->biv)->initVal));
free(reg->biv);
}
reg->biv = NULL;
break;
case VALUE_EXPR: //expr
if (reg->expr) {
clrExpr(reg->expr);
free(reg->expr);
}
reg->expr = NULL;
break;
default: //unknown
panicRegType(*reg);
initReg(reg);
break;
}
reg->t = VALUE_UNDEF;
}
void regUnknown(reg_t *reg) {
clrReg(reg);
reg->t = VALUE_UNPRED;
strcpy(reg->para,"T");
}
int regEq( reg_t reg1, reg_t reg2 ) {
if (reg1.t != reg2.t) return 0;
switch (reg1.t) {
case VALUE_UNDEF:
return 1;
case VALUE_CONST:
return (reg1.val==reg2.val);
case VALUE_PARA:
return (strcmp(reg1.para,reg2.para)==0);
case VALUE_EXPR:
return exprEq(reg1.expr,reg2.expr);
case VALUE_BIV:
return bivEq(reg1.biv, reg2.biv);
default:
panicRegType(reg1);
return 0;
}
}
int cpyReg(reg_t *dst, reg_t src) {
if (regEq(*dst,src)) return 0;//no change
if (dst->t!= src.t) {
if (dst->t!=VALUE_UNDEF) clrReg(dst);
dst->t = src.t;
}
switch (src.t) {
case VALUE_UNDEF:
break;
case VALUE_CONST:
dst->val = src.val;
break;
case VALUE_EXPR:
if (dst->expr==NULL) dst->expr = calloc(1,sizeof(expr_s));
cpyExpr( dst->expr,src.expr);
break;
case VALUE_PARA:
strcpy(dst->para,src.para);
break;
case VALUE_BIV:
if (dst->biv==NULL) dst->biv = calloc(1,sizeof(biv_s));
cpyBIV(dst->biv,src.biv);
break;
default:
panicRegType(src);
panicRegType(*dst);regUnknown(dst);
}
return 1;
}
int initRegSet(reg_t *regList) {
int i;
//NOTE: cause memory leak if regList is being used
for (i=0; i<NO_REG; i++) initReg(regList+i);
strcpy( regList[0].name , "$0" ); strcpy( regList[1].name , "$1" );
strcpy( regList[2].name , "$2" ); strcpy( regList[3].name , "$3" );
strcpy( regList[4].name , "$4" ); strcpy( regList[5].name , "$5" );
strcpy( regList[6].name , "$6" ); strcpy( regList[7].name , "$7" );
strcpy( regList[8].name , "$8" ); strcpy( regList[9].name , "$9" );
strcpy( regList[10].name, "$10" ); strcpy( regList[11].name, "$11" );
strcpy( regList[12].name, "$12" ); strcpy( regList[13].name, "$13" );
strcpy( regList[14].name, "$14" ); strcpy( regList[15].name, "$15" );
strcpy( regList[16].name, "$16" );
#if 0
strcpy( regList[17].name, "$17" );
strcpy( regList[18].name, "$18" ); strcpy( regList[19].name, "$19" );
strcpy( regList[20].name, "$20" ); strcpy( regList[21].name, "$21" );
strcpy( regList[22].name, "$22" ); strcpy( regList[23].name, "$23" );
strcpy( regList[24].name, "$24" ); strcpy( regList[25].name, "$25" );
strcpy( regList[26].name, "$26" ); strcpy( regList[27].name, "$27" );
strcpy( regList[28].name, "$28" ); strcpy( regList[29].name, "$29" );
strcpy( regList[30].name, "$30" ); strcpy( regList[31].name, "$31" );
/*HBK: ignore floating point register*/
strcpy( regList[34].name, "$f0" ); strcpy( regList[35].name, "$f1" );
strcpy( regList[36].name, "$f2" ); strcpy( regList[37].name, "$f3" );
strcpy( regList[38].name, "$f4" ); strcpy( regList[39].name, "$f5" );
strcpy( regList[40].name, "$f6" ); strcpy( regList[41].name, "$f7" );
strcpy( regList[42].name, "$f8" ); strcpy( regList[43].name, "$f9" );
strcpy( regList[44].name, "$f10" ); strcpy( regList[45].name, "$f11" );
strcpy( regList[46].name, "$f12" ); strcpy( regList[47].name, "$f13" );
strcpy( regList[48].name, "$f14" ); strcpy( regList[49].name, "$f15" );
strcpy( regList[50].name, "$f16" ); strcpy( regList[51].name, "$f17" );
strcpy( regList[52].name, "$f18" ); strcpy( regList[53].name, "$f19" );
strcpy( regList[54].name, "$f20" ); strcpy( regList[55].name, "$f21" );
strcpy( regList[56].name, "$f22" ); strcpy( regList[57].name, "$f23" );
strcpy( regList[58].name, "$f24" ); strcpy( regList[59].name, "$f25" );
strcpy( regList[60].name, "$f26" ); strcpy( regList[61].name, "$f27" );
strcpy( regList[62].name, "$f28" ); strcpy( regList[63].name, "$f29" );
strcpy( regList[64].name, "$f30" ); strcpy( regList[65].name, "$f31" );
strcpy( regList[66].name, "$fcc" );
#endif
setInt(regList+0,0);
//ERR: should only initialize bb0 with this
//setInt(regList+28,GLOBAL_START);
//setInt(regList+29,STACK_START);
//regUnknown(regList+30);
return 0;
}
int clearRegList(reg_t *regList) {
int i;
for( i = 1; i < NO_REG; i++ ) {
//if( i == 28 || i == 29 ) continue;// || i == 30
clrReg(regList+i);
}
return 0;
}
int findReg(reg_t *regList, char regName[] ) {
int i;
for( i = 0; i < NO_REG; i++ ) {
if( strcmp( regName, regList[i].name )==0) return i;
}
return -1;
}
/*** PARAMETER VALUE OPERATION ***/
void setNewPara(char *para) {
int dbg = 0;
//ERR: not implemented
//sprintf(name,"V%d",paraCount);
sprintf(para,"T");
if (dbg) fprintf(dbgF,"\nNew para: %s",para);
}
/*** INDUCTION VALUE OPERATION ***/
int updateInitVal(biv_p biVar, reg_t initVal) {
return mergeReg( &(biVar->initVal),initVal,0);
}
void freeBIV(biv_p *biv) {
clrReg( &( (*biv)->initVal) );
free(*biv);
*biv=NULL;
}
int bivEq(biv_p inVar1, biv_p inVar2) {
if (inVar1->regName != inVar2->regName) return 0;
if (regEq(inVar1->initVal, inVar2->initVal )==0) return 0;
if (inVar1->stride != inVar2->stride) return 0;
//if (strcmp(inVar1->regName, inVar2->regName)!=0) return 1;
return 1;
}
void cpyBIV(biv_p varDst, biv_p varSrc) {
varDst->insn = varSrc->insn;
cpyReg( &(varDst->initVal), varSrc->initVal );
varDst->opr = varSrc->opr;
varDst->regName = varSrc->regName;
varDst->stride = varSrc->stride;
}
void printBIV(FILE *fp, biv_p biVar) {
fprintf(fp," [r%d,",biVar->regName);
printReg(fp, biVar->initVal);
fprintf(fp,",%d",biVar->stride);//, biVar->opr;
fprintf(fp,"] ");fflush(fp);
}
/*** EXPRESSION VALUE OPERATION ***/
void printExpr(FILE* fp, expr_p expr) {
int i;
fprintf(fp,"\"");
for (i=0; i<expr->varNum; i++) {
fprintf(fp,"%d:",expr->coef[i]);
printReg(fp, expr->value[i]);
fprintf(fp,"+");
}
fprintf(fp," %lx ",expr->k);
fprintf(fp,"\"");fflush(fp);
}
int is_complex_op(operation_t op) {
return op == OPR_NOT || op == OPR_AND || op == OPR_ORR || op == OPR_XOR;
}
int computeExpr(operation_t op, expr_p exprD, expr_p expr1, expr_p expr2) {
int dbg = 1;
int i,j,k;
expr_p opr1, opr2;
int dis; //distribution opr, e.g. * >>
int inv; //inversion: (expr1 - expr2) --> (expr2 - expr1)
int sll; //shift left
//initReg(&rD);initReg(&r1);initReg(&r2);
if (dbg) {
fprintf(dbgF,"\nCompute "); printExpr(dbgF,expr1);
fprintf(dbgF," %d ",op); printExpr(dbgF,expr2);fflush(dbgF);
}
//check if operation requires distribution
if (op == OPR_MUL || op == OPR_RSF || op == OPR_LSF) {
if ( (expr1->varNum != 0 && expr2->varNum !=0) ) {
// not support opr like (5:a + 7:1) * (2:b + 15:1) -> a*b
printf("\nPanic, not support expr1 * expr2 for complex expr");
if (dbg) {
fprintf(stdout,"\nCompute "); printExpr(stdout,expr1);
fprintf(stdout," %d ",op);printExpr(stdout,expr2);fflush(stdout);
}
return 0; //cannot compute
}
dis = 1;
}
else dis = 0;// + -
for (i=0; i<expr1->varNum; i++) expr1->added[i]=0;
for (i=0; i<expr2->varNum; i++) expr2->added[i]=0;
inv = 0;
if (dis) {
if (expr1->varNum==0) { opr1 = expr2; opr2 = expr1; inv = 1; }
else if (expr2->varNum==0) {opr1 = expr1; opr2 = expr2; }
else {
printf("\nPanic, not implement for 1:a * 2:b, must 1:a * k:1 ");
return 0; //cannot compute
}
}
else {
opr1 = expr1; opr2 = expr2;
}
cpyExpr(exprD, opr1);
if (0) {
fprintf(dbgF,"\neD = e1 = "); printExpr(dbgF,exprD);fflush(dbgF);
}
if (dis) {//opr likes * , >> , << requires distribution
if (op==OPR_LSF && !inv ) sll = 1;
else if (op==OPR_LSF && inv ) sll = -1;
else if (op==OPR_RSF && !inv ) sll = -1;
else if (op==OPR_RSF && inv ) sll = 1;
else sll = 0;//not >> <<
for (i=0; i<exprD->varNum; i++) {
if (op==OPR_MUL) exprD->coef[i] *= opr2->k;
else if (op==OPR_RSF || op==OPR_LSF) {
if (sll==1)
for (k=0; k< opr2->k; k++) exprD->coef[i] *= 2;
else if (sll==-1)
for (k=0; k< opr2->k; k++) exprD->coef[i] /= 2;
else {printf("\nPanic: ????");exit(1);}
}
}
if (op == OPR_MUL) exprD->k = exprD->k * opr2->k;
else if (op == OPR_RSF || op == OPR_LSF) {
if (sll==1)
for (k=0; k< opr2->k; k++) exprD->k *= 2;
else if (sll==-1)
for (k=0; k< opr2->k; k++) exprD->k /= 2;
else {printf("\nPanic: ????");exit(1);}
}
if (dbg){fprintf(dbgF,"\neD %d %ld = ",op,opr2->k);printExpr(dbgF,exprD);}
goto COMPUTE_EXPR_FIN;
}
//else opr likes + -
else {
for (i=0; i<exprD->varNum; i++) {
for (j=0; j<opr2->varNum; j++) {
if (opr2->added[j] == 0 && regEq( exprD->value[i], opr2->value[j] )) {
opr2->added[j] = 1;
switch (op) {
case OPR_ADD:
exprD->coef[i] = exprD->coef[i] + opr2->coef[j];
break;
case OPR_SUB:
exprD->coef[i] = exprD->coef[i] - opr2->coef[j];
break;
default:
printf("Panic: cannot process opr %d\n", op);
}
break;
}
}
}
}
if (0) {
fprintf(dbgF,"\n eD after added elements in both E1 & E2: ");
printExpr(dbgF,exprD);fflush(dbgF);
}
for (j=0; j<opr2->varNum; j++) {
if (opr2->added[j]==0) {
int varNum = exprD->varNum;
if (varNum==MAX_EXPR_LEN) return 0;//too long, cannot compute
exprD->coef[varNum] = opr2->coef[j];
cpyReg(exprD->value+varNum, opr2->value[j]);
exprD->varNum++;
}
}
if (op == OPR_ADD)
exprD->k += opr2->k;
else
if (op == OPR_SUB)
exprD->k -= opr2->k;
COMPUTE_EXPR_FIN:
if (dbg) {
fprintf(dbgF,"\n ==> ");
printExpr(dbgF,exprD);
fflush(dbgF);
}
return 1;
}
int cpyExpr(expr_p dst, expr_p src) {
int dbg = 0;
int i;
if (exprEq(dst,src)) return 0;//same expr -> no need to copy
dst->varNum = src->varNum;
dst->k = src->k;
for (i=0; i<src->varNum; i++) {
dst->coef[i] = src->coef[i];
cpyReg(dst->value+i, src->value[i]);
}
if (dbg) {
fprintf(dbgF,"\nCopy expr: Dst ");printExpr(dbgF,dst);
fprintf(dbgF," <- Src ");printExpr(dbgF,src);
}
return 1;
}
int exprEq(expr_p expr1, expr_p expr2) {
int i;
if (expr1->varNum!=expr2->varNum) return 0;
if (expr1->k != expr2->k) return 0;
for (i=0; i<expr1->varNum; i++) {
if (expr1->coef[i] != expr2->coef[i]) return 0;
if (!regEq(expr1->value[i], expr2->value[i])) return 0;
}
return 1;
}
void clrExpr(expr_p expr) {
int i;
for (i=0; i<expr->varNum; i++) clrReg(expr->value+i);
expr->varNum=0;
expr->k = 0;
}
//convert reg to expr type
void initExpr(expr_p expr) {
int i=0;
for (i=0; i<MAX_EXPR_LEN; i++) initReg(expr->value+i);
expr->varNum = 0;
}
static
expr_p createExpr(void) {
expr_p expr;
expr = calloc(1,sizeof(expr_s));
initExpr(expr);
return expr;
}
void reg2expr(reg_t *r) {
expr_p expr;
if (r->t == VALUE_EXPR) return;
if (r->t==VALUE_CONST) {
if (r->expr==NULL) r->expr = createExpr();
expr = r->expr;
expr->k = r->val;
expr->varNum=0;
}
else {
if (r->expr==NULL) r->expr = createExpr();
expr = r->expr;
cpyReg( &(expr->value[0]),*r);
expr->k = 0;
expr->varNum = 1;
expr->coef[0] = 1;
}
r->t = VALUE_EXPR;
}
void setExpr(expr_p expr, reg_t r) {
clrExpr(expr);
if (r.t==VALUE_CONST) {
expr->k = r.val;
expr->varNum = 0;
}
else {
expr->k = 0;
cpyReg(expr->value+0,r);
expr->varNum = 1;
expr->coef[0] = 1;
}
}
/*** ABSTRACT INTERPRETATION OPERATIONS ON REGISTER VALUE ***/
//compute representative value for reg.values from different paths
int mergeReg(reg_t *dstReg, reg_t srcReg, int isBackEdge) {
int dbg = 0;
int flag, changed;
if ( regEq(srcReg,*dstReg) ) return 0; //Same value
if ( dstReg->t==VALUE_UNPRED ) return 0; //Already top
if ( srcReg.t==VALUE_UNDEF ) return 0; //Src is undefined
if (dbg) {
fprintf(dbgF,"\nMerge: ");
printReg( dbgF, *dstReg ); fprintf(dbgF," <- ");
printReg( dbgF, srcReg ); fprintf(dbgF," ");fflush(dbgF);
}
flag = 0;
changed = 0;
//scenarios where srcReg's value decides dstReg's value
if ( srcReg.t==VALUE_UNPRED ) {
regUnknown(dstReg);
if (0) fprintf(dbgF," src==T -> dst=T");
changed = 1;
goto FIN;
}
else if (dstReg->t!=VALUE_BIV && srcReg.t == VALUE_BIV) { //&& isBackEdge
cpyReg(dstReg,srcReg);
changed = 1;
goto FIN;
}
switch (dstReg->t) {
case VALUE_UNDEF:
cpyReg(dstReg,srcReg);
changed = 1;goto FIN;
break;
case VALUE_BIV:
if (srcReg.t == VALUE_BIV) {
//both dstReg & srcReg are both induction values
if (regEq(srcReg.biv->initVal, *dstReg)) {
cpyReg(dstReg,srcReg);
}
else goto NO_SPECIAL;
}
else {
/*when enter a loop, srcReg = initial constant*/
flag = updateInitVal(dstReg->biv, srcReg);
if (dbg) fprintf(dbgF," dstBIV->initVal = src");
if (flag) changed = 1;
goto FIN;
}
break;
default:
NO_SPECIAL://no special relationship, not equal -> unpredictable
if (!regEq(*dstReg,srcReg)) {
regUnknown(dstReg);
if (dbg) fprintf(dbgF," dst != src -> dst=T");
changed = 1;goto FIN;
}
//else dstReg==srcReg -> do nothing
return 0;
}
FIN:
if (dbg) {fprintf(dbgF," ==> ");printReg(dbgF,*dstReg);}
return changed;
}
int compute_cond_flags(long res) {
int flag = 0;
// N
if (res < 0)
flag |= 0x8;
// Z
if (res == 0)
flag |= 0x4;
// C
if (res > 0xffffffff)
flag |= 0x2;
// V
if (res > 0x7fffffff)
flag |= 0x1;
return flag;
}
// Return NZCV flags
int regOpr(operation_t op,reg_t *rD, reg_t r1, reg_t r2) {
int dbg = 0;
int flag;
FILE *dbgF = stdout;
if (r1.t == VALUE_UNDEF || r2.t == VALUE_UNDEF) {
clrReg(rD);
return UNKNOWN_COND_FLAGS;
}
if( dbg ) {
fprintf(dbgF,"\nOpr "); printReg(dbgF,r1);fprintf(dbgF," %d",op);
fprintf(dbgF," "); printReg(dbgF,r2);fflush(dbgF);
}
if (r1.t == VALUE_CONST && r1.t == r2.t) {
long res;
switch (op) {
case OPR_ADD:
res = (long)r1.val + (long)r2.val;
setInt(rD, res);
break;
case OPR_SUB:
res = (long)r1.val - (long)r2.val;
setInt(rD, res);
break;
case OPR_MUL:
res = (long)r1.val * (long)r2.val;
setInt(rD, res);
break;
case OPR_NOT:
res = ~ r1.val;
setInt(rD, res);
case OPR_ORR:
res = (long)r1.val | (long)r2.val;
setInt(rD, res);
break;
case OPR_XOR:
res = (long)r1.val ^ (long)r2.val;
setInt(rD, res);
break;
case OPR_AND:
res = (long)r1.val & (long)r2.val;
setInt(rD, res);
break;
case OPR_LSF:
res = (long)r1.val << (long)r2.val;
setInt(rD, res);
break;
case OPR_RSF:
res = (long)r1.val >> (long)r2.val;
setInt(rD, res);
break;
default:
printf("\nNot implemented opr %d\n",op);
regUnknown(rD);
}
if (rD->t == VALUE_CONST)
return compute_cond_flags(res);
else
return UNKNOWN_COND_FLAGS;
}
if (r1.t == VALUE_CONST && r1.val == 0 &&
(op == OPR_MUL || op == OPR_AND || op == OPR_LSF || op == OPR_RSF))
{
setInt(rD, 0);
return compute_cond_flags(0);
}
if (r2.t == VALUE_CONST && r2.val == 0 &&
(op == OPR_MUL || op == OPR_AND))
{
setInt(rD, 0);
return compute_cond_flags(0);
}
/* if (r1.t == VALUE_UNPRED || r2.t == VALUE_UNPRED) {
regUnknown(rD);
return UNKNOWN_COND_FLAGS;
}*/
if (is_complex_op(op)) {
regUnknown(rD);
return UNKNOWN_COND_FLAGS;
}
{
expr_p opr1, opr2;
expr_s expr1, expr2;
if (r1.t!=VALUE_EXPR) {
initExpr(&expr1);
setExpr(&expr1,r1);
opr1=&expr1;
}
else {
opr1 = r1.expr;
}
if (r2.t!=VALUE_EXPR) {
initExpr(&expr2);setExpr(&expr2,r2);
opr2=&expr2;
}
else {
opr2 = r2.expr;
}
if (rD->expr==NULL) rD->expr = createExpr();
if (dbg) {fprintf(dbgF,"\nE1: "); printExpr(dbgF,opr1);fflush(dbgF);}
if (dbg) {fprintf(dbgF,"\nE2: "); printExpr(dbgF,opr2);fflush(dbgF);}
if (OPR_MIN < op && op < OPR_MAX) {
rD->t = VALUE_EXPR;
flag = computeExpr(op, rD->expr, opr1, opr2);
if (flag == 0) regUnknown(rD);
}
else {
if (1) printf("\nNot implemented opr %d\n",op);
regUnknown(rD);
}
if (r1.t!=VALUE_EXPR) clrExpr(&expr1);
if (r2.t!=VALUE_EXPR) clrExpr(&expr2);
}
return UNKNOWN_COND_FLAGS;
}