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
|
//-----------------------------------------------------------------------------
//
// 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;
using System.Diagnostics.Contracts;
namespace BytecodeTranslator
{
public class StatementTraverser : BaseCodeTraverser {
public readonly TraverserFactory factory;
readonly Sink sink;
private readonly Bpl.Variable HeapVariable;
public readonly Bpl.StmtListBuilder StmtBuilder = new Bpl.StmtListBuilder();
#region Constructors
public StatementTraverser(Sink sink) {
this.sink = sink;
this.factory = sink.Factory;
HeapVariable = sink.HeapVariable;
}
#endregion
#region Helper Methods
Bpl.IToken TokenFor(IStatement statement) {
return TranslationHelper.CciLocationToBoogieToken(statement.Locations);
}
Bpl.Expr ExpressionFor(IExpression expression) {
ExpressionTraverser etrav = this.factory.MakeExpressionTraverser(this.sink, this);
etrav.Visit(expression);
Contract.Assert(etrav.TranslatedExpressions.Count == 1);
return etrav.TranslatedExpressions.Pop();
}
#endregion
public override void Visit(IBlockStatement block) {
Bpl.StmtListBuilder slb = new Bpl.StmtListBuilder();
foreach (IStatement st in block.Statements) {
this.Visit(st);
}
}
#region Basic Statements
public override void Visit(IAssertStatement assertStatement) {
StmtBuilder.Add(
new Bpl.AssertCmd(TokenFor(assertStatement), ExpressionFor(assertStatement.Condition))
);
}
public override void Visit(IAssumeStatement assumeStatement) {
StmtBuilder.Add(
new Bpl.AssertCmd(TokenFor(assumeStatement), ExpressionFor(assumeStatement.Condition))
);
}
/// <summary>
///
/// </summary>
/// <remarks>(mschaef) Works, but still a stub</remarks>
/// <param name="conditionalStatement"></param>
public override void Visit(IConditionalStatement conditionalStatement) {
StatementTraverser thenTraverser = this.factory.MakeStatementTraverser(this.sink);
StatementTraverser elseTraverser = this.factory.MakeStatementTraverser(this.sink);
ExpressionTraverser condTraverser = this.factory.MakeExpressionTraverser(this.sink, null);
condTraverser.Visit(conditionalStatement.Condition);
thenTraverser.Visit(conditionalStatement.TrueBranch);
elseTraverser.Visit(conditionalStatement.FalseBranch);
Bpl.IfCmd ifcmd = new Bpl.IfCmd(TokenFor(conditionalStatement),
condTraverser.TranslatedExpressions.Pop(),
thenTraverser.StmtBuilder.Collect(TokenFor(conditionalStatement.TrueBranch)),
null,
elseTraverser.StmtBuilder.Collect(TokenFor(conditionalStatement.FalseBranch))
);
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 = this.factory.MakeExpressionTraverser(this.sink, 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(TokenFor(breakStatement), "I dont know"));
}
/// <summary>
/// If the local declaration has an initial value, then generate the
/// statement "loc := e" from it. Otherwise ignore it.
/// </summary>
public override void Visit(ILocalDeclarationStatement localDeclarationStatement) {
if (localDeclarationStatement.InitialValue == null) return;
var loc = this.sink.FindOrCreateLocalVariable(localDeclarationStatement.LocalVariable);
var tok = TokenFor(localDeclarationStatement);
var e = ExpressionFor(localDeclarationStatement.InitialValue);
StmtBuilder.Add(Bpl.Cmd.SimpleAssign(tok, new Bpl.IdentifierExpr(tok, loc), e));
return;
}
/// <summary>
///
/// </summary>
/// <remarks>(mschaef) not implemented</remarks>
/// <param name="returnStatement"></param>
public override void Visit(IReturnStatement returnStatement) {
Bpl.IToken tok = TokenFor(returnStatement);
if (returnStatement.Expression != null) {
ExpressionTraverser etrav = this.factory.MakeExpressionTraverser(this.sink, this);
etrav.Visit(returnStatement.Expression);
if (this.sink.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.sink.RetVariable), etrav.TranslatedExpressions.Pop()));
}
StmtBuilder.Add(new Bpl.ReturnCmd(TokenFor(returnStatement)));
}
#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] { gotoStatement.TargetStatement.Label.Value };
Bpl.GotoCmd gotocmd = new Microsoft.Boogie.GotoCmd(TokenFor(gotoStatement), 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
}
}
|