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
|
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All Rights Reserved.
//
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Cci;
using Microsoft.Cci.MetadataReader;
using Microsoft.Cci.MutableCodeModel;
using Microsoft.Cci.Contracts;
using Microsoft.Cci.ILToCodeModel;
using Bpl = Microsoft.Boogie;
namespace BytecodeTranslator {
public class ExpressionTraverser : BaseCodeTraverser {
// warning! this has to be replaced by a HeapVariable from outside
public readonly Bpl.Variable HeapVariable;
public readonly Stack<Bpl.Expr> TranslatedExpressions;
protected readonly Sink sink;
protected readonly StatementTraverser StmtTraverser;
#region Constructors
/// <summary>
/// Use this constructor for translating expressions that do *not* occur
/// within the context of the statements in a method body.
/// </summary>
public ExpressionTraverser(Sink sink)
: this(sink, null)
{ }
/// <summary>
/// Use this constructor for translating expressions that do occur within
/// the context of the statements in a method body.
/// </summary>
public ExpressionTraverser(Sink sink, StatementTraverser/*?*/ statementTraverser) {
this.sink = sink;
HeapVariable = sink.HeapVariable;
this.StmtTraverser = statementTraverser;
TranslatedExpressions = new Stack<Bpl.Expr>();
}
#endregion
#region Translate Variable Access
/// <summary>
///
/// </summary>
/// <param name="addressableExpression"></param>
/// <remarks>still a stub</remarks>
public override void Visit(IAddressableExpression addressableExpression) {
ILocalDefinition/*?*/ local = addressableExpression.Definition as ILocalDefinition;
if (local != null) {
TranslatedExpressions.Push(Bpl.Expr.Ident(this.sink.FindOrCreateLocalVariable(local)));
return;
}
IParameterDefinition/*?*/ param = addressableExpression.Definition as IParameterDefinition;
if (param != null) {
TranslatedExpressions.Push(Bpl.Expr.Ident(this.sink.FindParameterVariable(param)));
return;
}
IFieldReference/*?*/ field = addressableExpression.Definition as IFieldReference;
if (field != null) {
//TranslatedExpressions.Push(Bpl.Expr.Ident(this.StmtTraverser.MethodTraverser.ClassTraverser.FindOrCreateFieldVariable(field.ResolvedField)));
throw new NotImplementedException();
}
IArrayIndexer/*?*/ arrayIndexer = addressableExpression.Definition as IArrayIndexer;
if (arrayIndexer != null) {
this.Visit(arrayIndexer);
return;
}
IAddressDereference/*?*/ addressDereference = addressableExpression.Definition as IAddressDereference;
if (addressDereference != null) {
this.Visit(addressDereference);
return;
}
IMethodReference/*?*/ method = addressableExpression.Definition as IMethodReference;
if (method != null) {
Console.WriteLine(MemberHelper.GetMethodSignature(method, NameFormattingOptions.Signature));
//TODO
throw new NotImplementedException();
}
Debug.Assert(addressableExpression.Definition is IThisReference);
}
public override void Visit(IAddressDereference addressDereference) {
IBoundExpression be = addressDereference.Address as IBoundExpression;
if (be != null) {
IParameterDefinition pd = be.Definition as IParameterDefinition;
if (pd != null) {
var pv = this.sink.FindParameterVariable(pd);
TranslatedExpressions.Push(Bpl.Expr.Ident(pv));
return;
}
}
this.Visit(addressDereference.Address);
throw new NotImplementedException();
}
public override void Visit(IArrayIndexer arrayIndexer) {
this.Visit(arrayIndexer.IndexedObject);
this.Visit(arrayIndexer.Indices);
//TODO
}
public override void Visit(ITargetExpression targetExpression) {
#region ArrayIndexer
IArrayIndexer/*?*/ indexer = targetExpression.Definition as IArrayIndexer;
if (indexer != null) {
this.Visit(indexer);
return;
}
#endregion
#region AddressDereference
IAddressDereference/*?*/ deref = targetExpression.Definition as IAddressDereference;
if (deref != null) {
IAddressOf/*?*/ addressOf = deref.Address as IAddressOf;
if (addressOf != null) {
this.Visit(addressOf.Expression);
return;
}
IConversion/*?*/ conversion = deref.Address as IConversion;
if (conversion != null) {
IBoundExpression be = conversion.ValueToConvert as IBoundExpression;
if (be != null) {
IParameterDefinition pd = be.Definition as IParameterDefinition;
if (pd != null) {
var pv = this.sink.FindParameterVariable(pd);
TranslatedExpressions.Push(Bpl.Expr.Ident(pv));
return;
}
}
}
if (targetExpression.Instance != null) {
// TODO
throw new NotImplementedException("targetExpr->AddrDeRef->InstanceNull");
} else if (deref.Address.Type is IPointerTypeReference)
throw new NotImplementedException("targetExpr->AddrDeRef->Pointertype");
this.Visit(deref.Address);
return;
}
#endregion
#region LocalDefinition
ILocalDefinition/*?*/ local = targetExpression.Definition as ILocalDefinition;
if (local != null) {
TranslatedExpressions.Push(Bpl.Expr.Ident(this.sink.FindOrCreateLocalVariable(local)));
return;
}
#endregion
#region ParameterDefenition
IParameterDefinition param = targetExpression.Definition as IParameterDefinition;
if (param != null) {
TranslatedExpressions.Push(Bpl.Expr.Ident(this.sink.FindParameterVariable(param)));
return;
}
#endregion
#region FieldReference
IFieldReference field = targetExpression.Definition as IFieldReference;
if (field != null) {
ProcessFieldVariable(field, targetExpression.Instance, false);
return;
}
#endregion
#region PropertyDefiniton
IPropertyDefinition prop = targetExpression.Definition as IPropertyDefinition;
if (prop != null) {
throw new NotImplementedException("targetExpr->Property");
}
#endregion
}
public override void Visit(IThisReference thisReference) {
TranslatedExpressions.Push(new Bpl.IdentifierExpr(thisReference.Token(),
this.sink.ThisVariable));
}
public override void Visit(IBoundExpression boundExpression) {
//if (boundExpression.Instance != null)
//{
// this.Visit(boundExpression.Instance);
// // TODO: (mschaef) look where it's bound and do something
//}
#region LocalDefinition
ILocalDefinition local = boundExpression.Definition as ILocalDefinition;
if (local != null) {
TranslatedExpressions.Push(Bpl.Expr.Ident(this.sink.FindOrCreateLocalVariable(local)));
return;
}
#endregion
#region ParameterDefiniton
IParameterDefinition param = boundExpression.Definition as IParameterDefinition;
if (param != null) {
TranslatedExpressions.Push(Bpl.Expr.Ident(this.sink.FindParameterVariable(param)));
return;
}
#endregion
#region FieldDefinition
IFieldReference field = boundExpression.Definition as IFieldReference;
if (field != null) {
if (boundExpression.Instance != null) {
ProcessFieldVariable(field, boundExpression.Instance,true);
return;
} else {
throw new NotImplementedException(String.Format("Field {0} with Instance NULL.", boundExpression.ToString()));
}
}
#endregion
#region PropertyDefinition
IPropertyDefinition prop = boundExpression.Definition as IPropertyDefinition;
if (prop != null) {
throw new NotImplementedException("Properties are not implemented");
}
#endregion
#region ArrayIndexer
IArrayIndexer/*?*/ indexer = boundExpression.Definition as IArrayIndexer;
if (indexer != null) {
this.Visit(indexer);
return;
}
#endregion
#region AddressDereference
IAddressDereference/*?*/ deref = boundExpression.Definition as IAddressDereference;
if (deref != null) {
IAddressOf/*?*/ addressOf = deref.Address as IAddressOf;
if (addressOf != null) {
this.Visit(addressOf.Expression);
return;
}
if (boundExpression.Instance != null) {
// TODO
this.Visit(boundExpression.Instance);
Console.Write("->");
} else if (deref.Address.Type is IPointerTypeReference)
Console.Write("*");
this.Visit(deref.Address);
return;
} else {
if (boundExpression.Instance != null) {
throw new NotImplementedException("Addr DeRef without instance.");
}
}
#endregion
}
/// <summary>
///
/// </summary>
/// <param name="addressOf"></param>
/// <remarks>Since we are doing Copy-In,Copy-Out for function calls we can ignore it.
/// But will this work for the general case?</remarks>
public override void Visit(IAddressOf addressOf) {
Visit(addressOf.Expression);
}
#endregion
#region Variable Access Helpers
private void ProcessFieldVariable(IFieldReference field, IExpression instance, bool buildSelectExpr) {
//TranslatedExpressions.Push(Bpl.Expr.Ident(this.StmtTraverser.MethodTraverser.ClassTraverser.FindOrCreateFieldVariable(field.ResolvedField))
TranslatedExpressions.Push( Bpl.Expr.Ident(
this.sink.FindOrCreateFieldVariable(field.ResolvedField) ) );
this.Visit(instance);
// if the field access is not a targetexpression we build a select expression
// otherwise the assignment visitor will build a mapassignment later on
if (buildSelectExpr) {
List<Bpl.Expr> elist = new List<Bpl.Expr>();
while (TranslatedExpressions.Count > 0) {
elist.Add(TranslatedExpressions.Pop());
}
TranslatedExpressions.Push(Bpl.Expr.Select(new Bpl.IdentifierExpr(field.Token(), HeapVariable), elist));
}
}
#endregion
#region Translate Constant Access
public override void Visit(ICompileTimeConstant constant) {
if (constant.Value == null) {
// TODO: (mschaef) hack
TranslatedExpressions.Push(Bpl.Expr.False);
} else if (constant.Value is bool) {
TranslatedExpressions.Push(((bool)constant.Value) ? Bpl.Expr.True : Bpl.Expr.False);
} else if (constant.Value is string) {
throw new NotImplementedException("Strings are not translated");
} else {
// TODO: (mschaef) hack
TranslatedExpressions.Push(Bpl.Expr.Literal((int)constant.Value));
}
}
#endregion
#region Translate Method Calls
/// <summary>
///
/// </summary>
/// <param name="methodCall"></param>
/// <remarks>Stub, This one really needs comments!</remarks>
public override void Visit(IMethodCall methodCall) {
#region Translate In Parameters
Bpl.ExprSeq inexpr = new Bpl.ExprSeq();
#region Create the 'this' argument for the function call
this.Visit(methodCall.ThisArgument);
inexpr.Add(this.TranslatedExpressions.Pop());
#endregion
Dictionary<IParameterDefinition, Bpl.Expr> p2eMap = new Dictionary<IParameterDefinition, Bpl.Expr>();
IEnumerator<IParameterDefinition> penum = methodCall.MethodToCall.ResolvedMethod.Parameters.GetEnumerator();
penum.MoveNext();
foreach (IExpression exp in methodCall.Arguments) {
if (penum.Current == null) {
throw new TranslationException("More Arguments than Parameters in functioncall");
}
this.Visit(exp);
Bpl.Expr e = this.TranslatedExpressions.Pop();
p2eMap.Add(penum.Current, e);
if (!penum.Current.IsOut) {
inexpr.Add(e);
}
penum.MoveNext();
}
#endregion
Bpl.IToken cloc = methodCall.Token();
// meeting a constructor is always something special
if (methodCall.MethodToCall.ResolvedMethod.IsConstructor) {
// Todo: do something with the constructor call
} else {
// Todo: if there is no stmttraverser we are visiting a contract and should use a boogie function instead of procedure!
#region Translate Out vars
Bpl.IdentifierExprSeq outvars = new Bpl.IdentifierExprSeq();
foreach (KeyValuePair<IParameterDefinition, Bpl.Expr> kvp in p2eMap) {
if (kvp.Key.IsOut || kvp.Key.IsByReference) {
Bpl.IdentifierExpr iexp = kvp.Value as Bpl.IdentifierExpr;
if (iexp == null) {
throw new TranslationException("Trying to pass complex expression as out in functioncall");
}
outvars.Add(iexp);
}
}
#endregion
if (methodCall.Type.ResolvedType.TypeCode != PrimitiveTypeCode.Void) {
Bpl.Variable v = this.sink.CreateFreshLocal(methodCall.Type.ResolvedType);
outvars.Add(new Bpl.IdentifierExpr(cloc, v));
TranslatedExpressions.Push(new Bpl.IdentifierExpr(cloc, v));
}
string methodname = TranslationHelper.CreateUniqueMethodName(methodCall.MethodToCall.ResolvedMethod);
this.StmtTraverser.StmtBuilder.Add(new Bpl.CallCmd(cloc, methodname, inexpr, outvars));
}
}
/// <summary>
///
/// </summary>
/// <remarks>(mschaef) Works, but still a stub </remarks>
/// <param name="assignment"></param>
public override void Visit(IAssignment assignment) {
#region Transform Right Hand Side ...
this.Visit(assignment.Source);
Bpl.Expr sourceexp = this.TranslatedExpressions.Pop();
#endregion
this.Visit(assignment.Target);
Bpl.Expr targetexp = this.TranslatedExpressions.Pop();
Bpl.IdentifierExpr idexp = targetexp as Bpl.IdentifierExpr;
if (idexp != null) {
StmtTraverser.StmtBuilder.Add(Bpl.Cmd.SimpleAssign(assignment.Token(),
idexp, sourceexp));
return;
} else {
throw new TranslationException("Trying to create a SimpleAssign with complex/illegal lefthand side");
}
}
#endregion
#region Translate Binary Operators
public override void Visit(IAddition addition) {
base.Visit(addition);
Bpl.Expr rexp = TranslatedExpressions.Pop();
Bpl.Expr lexp = TranslatedExpressions.Pop();
TranslatedExpressions.Push(Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Add, lexp, rexp));
}
public override void Visit(IDivision division) {
base.Visit(division);
Bpl.Expr rexp = TranslatedExpressions.Pop();
Bpl.Expr lexp = TranslatedExpressions.Pop();
TranslatedExpressions.Push(Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Div, lexp, rexp));
}
public override void Visit(ISubtraction subtraction) {
base.Visit(subtraction);
Bpl.Expr rexp = TranslatedExpressions.Pop();
Bpl.Expr lexp = TranslatedExpressions.Pop();
TranslatedExpressions.Push(Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Sub, lexp, rexp));
}
public override void Visit(IMultiplication multiplication) {
base.Visit(multiplication);
Bpl.Expr rexp = TranslatedExpressions.Pop();
Bpl.Expr lexp = TranslatedExpressions.Pop();
TranslatedExpressions.Push(Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Mul, lexp, rexp));
}
public override void Visit(IModulus modulus) {
base.Visit(modulus);
Bpl.Expr rexp = TranslatedExpressions.Pop();
Bpl.Expr lexp = TranslatedExpressions.Pop();
TranslatedExpressions.Push(Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Mod, lexp, rexp));
}
public override void Visit(IGreaterThan greaterThan) {
base.Visit(greaterThan);
Bpl.Expr rexp = TranslatedExpressions.Pop();
Bpl.Expr lexp = TranslatedExpressions.Pop();
TranslatedExpressions.Push(Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Gt, lexp, rexp));
}
public override void Visit(IGreaterThanOrEqual greaterEqual) {
base.Visit(greaterEqual);
Bpl.Expr rexp = TranslatedExpressions.Pop();
Bpl.Expr lexp = TranslatedExpressions.Pop();
TranslatedExpressions.Push(Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Ge, lexp, rexp));
}
public override void Visit(ILessThan lessThan) {
base.Visit(lessThan);
Bpl.Expr rexp = TranslatedExpressions.Pop();
Bpl.Expr lexp = TranslatedExpressions.Pop();
TranslatedExpressions.Push(Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Lt, lexp, rexp));
}
public override void Visit(ILessThanOrEqual lessEqual) {
base.Visit(lessEqual);
Bpl.Expr rexp = TranslatedExpressions.Pop();
Bpl.Expr lexp = TranslatedExpressions.Pop();
TranslatedExpressions.Push(Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Le, lexp, rexp));
}
public override void Visit(IEquality equal) {
base.Visit(equal);
Bpl.Expr rexp = TranslatedExpressions.Pop();
Bpl.Expr lexp = TranslatedExpressions.Pop();
TranslatedExpressions.Push(Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Eq, lexp, rexp));
}
public override void Visit(INotEquality nonEqual) {
base.Visit(nonEqual);
Bpl.Expr rexp = TranslatedExpressions.Pop();
Bpl.Expr lexp = TranslatedExpressions.Pop();
TranslatedExpressions.Push(Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Neq, lexp, rexp));
}
/// <summary>
/// There aren't any logical-and expressions or logical-or expressions in CCI.
/// Instead they are encoded as "x ? y : 0" for "x && y" and "x ? 1 : y"
/// for "x || y".
/// TODO:
/// If it isn't either of these short forms then emit the proper expression!
/// </summary>
public override void Visit(IConditional conditional) {
CompileTimeConstant ctc = conditional.ResultIfFalse as CompileTimeConstant;
if (ctc != null && ctc.Type == BCT.Host.PlatformType.SystemInt32) {
int v = (int)ctc.Value;
if (v == 0) {
Visit(conditional.Condition);
Bpl.Expr x = TranslatedExpressions.Pop();
Visit(conditional.ResultIfTrue);
Bpl.Expr y = TranslatedExpressions.Pop();
TranslatedExpressions.Push(Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.And, x, y));
return;
}
}
ctc = conditional.ResultIfTrue as CompileTimeConstant;
if (ctc != null && ctc.Type == BCT.Host.PlatformType.SystemInt32) {
int v = (int)ctc.Value;
if (v == 1) {
Visit(conditional.Condition);
Bpl.Expr x = TranslatedExpressions.Pop();
Visit(conditional.ResultIfFalse);
Bpl.Expr y = TranslatedExpressions.Pop();
TranslatedExpressions.Push(Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Or, x, y));
return;
}
}
base.Visit(conditional);
}
#endregion
#region Translate Unary Operators
public override void Visit(IUnaryNegation unaryNegation) {
base.Visit(unaryNegation);
Bpl.Expr exp = TranslatedExpressions.Pop();
Bpl.Expr zero = Bpl.Expr.Literal(0); // TODO: (mschaef) will this work in any case?
TranslatedExpressions.Push( Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Sub, zero, exp) );
}
public override void Visit(ILogicalNot logicalNot) {
base.Visit(logicalNot);
Bpl.Expr exp = TranslatedExpressions.Pop();
TranslatedExpressions.Push(Bpl.Expr.Unary(
logicalNot.Token(),
Bpl.UnaryOperator.Opcode.Not, exp));
}
#endregion
#region CodeContract Expressions
public override void Visit(IOldValue oldValue) {
base.Visit(oldValue);
TranslatedExpressions.Push(new Bpl.OldExpr(oldValue.Token(),
TranslatedExpressions.Pop()));
}
public override void Visit(IReturnValue returnValue) {
if (this.sink.RetVariable == null) {
throw new TranslationException(String.Format("Don't know what to do with return value {0}", returnValue.ToString()));
}
TranslatedExpressions.Push(new Bpl.IdentifierExpr(returnValue.Token(),
this.sink.RetVariable));
}
#endregion
}
}
|