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
|
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All Rights Reserved.
//
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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
{
internal class StatementTraverser : BaseCodeTraverser {
public readonly MethodTraverser MethodTraverser;
public readonly Bpl.Variable HeapVariable;
public readonly Bpl.StmtListBuilder StmtBuilder = new Bpl.StmtListBuilder();
#region Constructors
public StatementTraverser(MethodTraverser mt) {
HeapVariable = mt.HeapVariable;
MethodTraverser = mt;
}
public StatementTraverser(MethodTraverser mt, Bpl.Variable heapvar) {
HeapVariable = heapvar;
MethodTraverser = mt;
}
#endregion
public override void Visit(IBlockStatement block) {
BasicBlock b = (BasicBlock)block;
this.Visit(b);
}
#region Basic Statements
/// <summary>
/// Visit all statements in a single block
/// </summary>
/// <param name="b"></param>
private void Visit(BasicBlock b) {
Bpl.StmtListBuilder slb = new Bpl.StmtListBuilder();
foreach (IStatement st in b.Statements) {
this.Visit(st);
}
}
/// <summary>
///
/// </summary>
/// <remarks>(mschaef) Works, but still a stub</remarks>
/// <param name="conditionalStatement"></param>
public override void Visit(IConditionalStatement conditionalStatement) {
StatementTraverser thenTraverser = new StatementTraverser(this.MethodTraverser);
StatementTraverser elseTraverser = new StatementTraverser(this.MethodTraverser);
ExpressionTraverser condTraverser = new ExpressionTraverser(this);
condTraverser.Visit(conditionalStatement.Condition);
thenTraverser.Visit(conditionalStatement.TrueBranch);
elseTraverser.Visit(conditionalStatement.FalseBranch);
Bpl.IfCmd ifcmd = new Bpl.IfCmd(TranslationHelper.CciLocationToBoogieToken(conditionalStatement.Locations),
condTraverser.TranslatedExpressions.Pop(),
thenTraverser.StmtBuilder.Collect(TranslationHelper.CciLocationToBoogieToken(conditionalStatement.TrueBranch.Locations)),
null,
elseTraverser.StmtBuilder.Collect(TranslationHelper.CciLocationToBoogieToken(conditionalStatement.FalseBranch.Locations))
);
StmtBuilder.Add(ifcmd);
}
/// <summary>
///
/// </summary>
/// <param name="expressionStatement"></param>
/// <remarks> TODO: might be wrong for the general case</remarks>
public override void Visit(IExpressionStatement expressionStatement) {
ExpressionTraverser etrav = new ExpressionTraverser(this);
etrav.Visit(expressionStatement.Expression);
}
/// <summary>
///
/// </summary>
/// <remarks>(mschaef) Not Implemented</remarks>
/// <param name="breakStatement"></param>
public override void Visit(IBreakStatement breakStatement) {
StmtBuilder.Add(new Bpl.BreakCmd(TranslationHelper.CciLocationToBoogieToken(breakStatement.Locations), "I dont know"));
}
/// <summary>
///
/// </summary>
/// <remarks>(mschaef) not implemented</remarks>
/// <param name="returnStatement"></param>
public override void Visit(IReturnStatement returnStatement) {
Bpl.IToken tok = TranslationHelper.CciLocationToBoogieToken(returnStatement.Locations);
#region Copy values of all out parameters to outvariables
foreach (MethodTraverser.MethodParameter mp in MethodTraverser.OutVars) {
StmtBuilder.Add(Bpl.Cmd.SimpleAssign(tok,
new Bpl.IdentifierExpr(tok, mp.outParameterCopy), new Bpl.IdentifierExpr(tok, mp.localParameter)));
}
#endregion
if (returnStatement.Expression != null) {
ExpressionTraverser etrav = new ExpressionTraverser(this);
etrav.Visit(returnStatement.Expression);
if (this.MethodTraverser.RetVariable == null || etrav.TranslatedExpressions.Count < 1) {
throw new TranslationException(String.Format("{0} returns a value that is not supported by the function", returnStatement.ToString()));
}
StmtBuilder.Add(Bpl.Cmd.SimpleAssign(tok,
new Bpl.IdentifierExpr(tok, this.MethodTraverser.RetVariable), etrav.TranslatedExpressions.Pop()));
}
StmtBuilder.Add(new Bpl.ReturnCmd(TranslationHelper.CciLocationToBoogieToken(returnStatement.Locations)));
}
#endregion
#region Goto and Labels
/// <summary>
///
/// </summary>
/// <remarks> STUB </remarks>
/// <param name="gotoStatement"></param>
public override void Visit(IGotoStatement gotoStatement) {
String[] target = new String[1];
target[0] = gotoStatement.TargetStatement.Label.Value;
Bpl.GotoCmd gotocmd = new Microsoft.Boogie.GotoCmd(TranslationHelper.CciLocationToBoogieToken(gotoStatement.Locations),
new Bpl.StringSeq(target));
StmtBuilder.Add(gotocmd);
}
/// <summary>
///
/// </summary>
/// <remarks> (mschaef) not sure if there is more work to do</remarks>
/// <param name="labeledStatement"></param>
public override void Visit(ILabeledStatement labeledStatement) {
StmtBuilder.AddLabelCmd(labeledStatement.Label.Value);
base.Visit(labeledStatement.Statement);
}
#endregion
#region Looping Statements
public override void Visit(IWhileDoStatement whileDoStatement) {
throw new NotImplementedException("While Statements are not implemented");
}
#endregion
}
}
|