-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProofUtils.cpp
311 lines (261 loc) · 11 KB
/
ProofUtils.cpp
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
#include "ProofUtils.h"
#include "wnt_FunctionExpression.h"
#include "wnt_FunctionDefinition.h"
#include "wnt_IfExpression.h"
#include "VMState.h"
namespace Winter
{
static const IntervalSetInt64 updateIndexBounds(const ComparisonExpression& comp_expr, const ASTNodeRef& index, const IntervalSetInt64& bounds)
{
// We know comp_expr is of type 'i T x' where T is some comparison token
// Check i is the same as the original array or vector index:
if(expressionsHaveSameValue(comp_expr.a, index))
{
if(comp_expr.b->isConstant() && comp_expr.b->type()->getType() == Type::IntType) // if the x value is constant
{
// Evaluate the index expression
VMState vmstate;
vmstate.func_args_start.push_back(0);
ValueRef retval = comp_expr.b->exec(vmstate);
assert(retval->valueType() == Value::ValueType_Int);
const int64 x_val = static_cast<IntValue*>(retval.getPointer())->value;
switch(comp_expr.token->getType())
{
case LEFT_ANGLE_BRACKET_TOKEN: // i < x
return intervalSetIntersection(bounds, IntervalSetInt64(std::numeric_limits<int64>::min(), x_val - 1));
case RIGHT_ANGLE_BRACKET_TOKEN: // i > x
return intervalSetIntersection(bounds, IntervalSetInt64(x_val + 1, std::numeric_limits<int64>::max()));
case DOUBLE_EQUALS_TOKEN: // i == x
return IntervalSetInt64(x_val, x_val);
case NOT_EQUALS_TOKEN: // i != x
return intervalSetIntersection(bounds, intervalWithHole(x_val));
case LESS_EQUAL_TOKEN: // i <= x
return intervalSetIntersection(bounds, IntervalSetInt64(std::numeric_limits<int64>::min(), x_val));
case GREATER_EQUAL_TOKEN: // i >= x
return intervalSetIntersection(bounds, IntervalSetInt64(x_val, std::numeric_limits<int64>::max()));
default:
return bounds;
}
}
}
return bounds;
}
// NOTE: Are these correct?
static float myPriorFloat(float x)
{
if(x == -std::numeric_limits<float>::infinity())
return std::numeric_limits<float>::infinity();
else
{
uint32 i;
std::memcpy(&i, &x, 4);
assert(i != 0);
i--;
std::memcpy(&x, &i, 4);
return x;
}
}
static float myNextFloat(float x)
{
if(x == std::numeric_limits<float>::infinity())
return std::numeric_limits<float>::infinity();
else
{
uint32 i;
std::memcpy(&i, &x, 4);
assert(i != 0);
i++;
std::memcpy(&x, &i, 4);
return x;
}
}
static const IntervalSetFloat updateBounds(const ComparisonExpression& comp_expr, const ASTNodeRef& index, const IntervalSetFloat& bounds)
{
// We know comp_expr is of type 'i T x' where T is some comparison token
// Check i is the same as the original array or vector index:
if(expressionsHaveSameValue(comp_expr.a, index))
{
if(comp_expr.b->isConstant() && comp_expr.b->type()->getType() == Type::FloatType) // if the x value is constant
{
// Evaluate the index expression
VMState vmstate;
vmstate.func_args_start.push_back(0);
ValueRef retval = comp_expr.b->exec(vmstate);
assert(retval->valueType() == Value::ValueType_Float);
const float x_val = static_cast<FloatValue*>(retval.getPointer())->value;
switch(comp_expr.token->getType())
{
case LEFT_ANGLE_BRACKET_TOKEN: // i < x
return intervalSetIntersection(bounds, IntervalSetFloat(-std::numeric_limits<float>::infinity(), myPriorFloat(x_val)));
case RIGHT_ANGLE_BRACKET_TOKEN: // i > x
return intervalSetIntersection(bounds, IntervalSetFloat(myNextFloat(x_val), std::numeric_limits<float>::infinity()));
case DOUBLE_EQUALS_TOKEN: // i == x
return IntervalSetFloat(x_val, x_val);
case NOT_EQUALS_TOKEN: // i != x
return intervalSetIntersection<float>(bounds, intervalWithHole<float>(x_val));
case LESS_EQUAL_TOKEN: // i <= x
return intervalSetIntersection(bounds, IntervalSetFloat(-std::numeric_limits<float>::infinity(), x_val));
case GREATER_EQUAL_TOKEN: // i >= x
return intervalSetIntersection(bounds, IntervalSetFloat(x_val, std::numeric_limits<float>::infinity()));
default:
return bounds;
}
}
}
return bounds;
}
IntervalSetInt64 ProofUtils::getInt64Range(std::vector<ASTNode*>& stack, const ASTNodeRef& integer_value)
{
// Lower and upper inclusive bounds
IntervalSetInt64 bounds;
const TypeRef type = integer_value->type();
// Get initial bounds based on type
assert(type->getType() == Type::IntType);
if(type.downcastToPtr<Int>()->numBits() == 32)
bounds = IntervalSetInt64(std::numeric_limits<int32>::min(), std::numeric_limits<int32>::max());
else if(type.downcastToPtr<Int>()->numBits() == 64)
bounds = IntervalSetInt64(std::numeric_limits<int64>::min(), std::numeric_limits<int64>::max());
else
{
assert(0);
throw BaseException("invalid num bits");
}
for(int z=(int)stack.size()-1; z >= 0; --z)
{
ASTNode* stack_node = stack[z];
// Get next node up the call stack
if(stack_node->nodeType() == ASTNode::IfExpressionType)
{
// AST node above this one is an "if" expression
IfExpression* if_node = static_cast<IfExpression*>(stack_node);
// Is this node the 1st arg of the if expression?
// e.g. if condition then this_node else other_node
// Or is this node a child of the 1st arg?
if(/*if_node->argument_expressions[1].getPointer() == this || */((z+1) < (int)stack.size() && if_node->then_expr.getPointer() == stack[z+1]))
{
// Ok, now we need to check the condition of the if expression.
// A valid proof condition will be of form
// inBounds(array, index)
// Where array and index are the same as the ones for this elem() call.
if(if_node->condition->nodeType() == ASTNode::FunctionExpressionType)
{
FunctionExpression* condition_func_express = static_cast<FunctionExpression*>(if_node->condition.getPointer());
if(condition_func_express->static_target_function && condition_func_express->static_target_function->sig.name == "inBounds")
{
// Is the index the same?
if(expressionsHaveSameValue(condition_func_express->argument_expressions[1], integer_value))
{
const TypeRef container_type = condition_func_express->static_target_function->sig.param_types[0];
// Update bounds to reflect that we are in bounds of the container -
// We know that the lower bound is >= 0 and the upper bound is < container size.
if(container_type->getType() == Type::ArrayTypeType)
{
const ArrayType* array_type = static_cast<const ArrayType*>(container_type.getPointer());
//bounds = Vec2<int>(myMax(bounds.x, 0), myMin(bounds.y, (int)array_type->num_elems - 1));
bounds = intervalSetIntersection(bounds, IntervalSetInt64(0, (int64)array_type->num_elems - 1));
}
if(container_type->getType() == Type::VectorTypeType)
{
const VectorType* vector_type = static_cast<const VectorType*>(container_type.getPointer());
//bounds = Vec2<int>(myMax(bounds.x, 0), myMin(bounds.y, (int)vector_type->num- 1));
bounds = intervalSetIntersection(bounds, IntervalSetInt64(0, (int64)vector_type->num - 1));
}
}
// Is the array the same?
/*if(expressionsHaveSameValue(condition_func_express->argument_expressions[0], this->argument_expressions[0]))
{
// Is the index the same?
if(expressionsHaveSameValue(condition_func_express->argument_expressions[1], this->argument_expressions[1]))
{
// Success, inBounds uses the same variables, proving that the array access is in-bounds
return;
}
}*/
}
}
else if(if_node->condition->nodeType() == ASTNode::BinaryBooleanType)
{
BinaryBooleanExpr* bin = static_cast<BinaryBooleanExpr*>(if_node->condition.getPointer());
if(bin->t == BinaryBooleanExpr::AND)
{
// We know condition expression is of type A AND B
// Process A
if(bin->a->nodeType() == ASTNode::ComparisonExpressionType)
{
ComparisonExpression* a = static_cast<ComparisonExpression*>(bin->a.getPointer());
bounds = updateIndexBounds(*a, integer_value, bounds);
}
// Process B
if(bin->b->nodeType() == ASTNode::ComparisonExpressionType)
{
ComparisonExpression* b = static_cast<ComparisonExpression*>(bin->b.getPointer());
bounds = updateIndexBounds(*b, integer_value, bounds);
}
}
}
else if(if_node->condition->nodeType() == ASTNode::ComparisonExpressionType)
{
ComparisonExpression* comp = static_cast<ComparisonExpression*>(if_node->condition.getPointer());
bounds = updateIndexBounds(*comp, integer_value, bounds);
}
}
}
}
return bounds;
}
IntervalSetFloat ProofUtils::getFloatRange(std::vector<ASTNode*>& stack, const ASTNodeRef& float_value)
{
// Lower and upper inclusive bounds
IntervalSetFloat bounds(-std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());
for(int z=(int)stack.size()-1; z >= 0; --z)
{
ASTNode* stack_node = stack[z];
// Get next node up the call stack
if(stack_node->nodeType() == ASTNode::IfExpressionType)
{
// AST node above this one is an "if" expression
IfExpression* if_node = static_cast<IfExpression*>(stack_node);
// Is this node the 1st arg of the if expression?
// e.g. if condition then this_node else other_node
// Or is this node a child of the 1st arg?
if(/*if_node->argument_expressions[1].getPointer() == this || */((z+1) < (int)stack.size() && if_node->then_expr.getPointer() == stack[z+1]))
{
// Ok, now we need to check the condition of the if expression.
// A valid proof condition will be of form
// inBounds(array, index)
// Where array and index are the same as the ones for this elem() call.
if(if_node->condition->nodeType() == ASTNode::FunctionExpressionType)
{
// FunctionExpression* condition_func_express = static_cast<FunctionExpression*>(if_node->condition.getPointer());
}
else if(if_node->condition->nodeType() == ASTNode::BinaryBooleanType)
{
BinaryBooleanExpr* bin = static_cast<BinaryBooleanExpr*>(if_node->condition.getPointer());
if(bin->t == BinaryBooleanExpr::AND)
{
// We know condition expression is of type A AND B
// Process A
if(bin->a->nodeType() == ASTNode::ComparisonExpressionType)
{
ComparisonExpression* a = static_cast<ComparisonExpression*>(bin->a.getPointer());
bounds = updateBounds(*a, float_value, bounds);
}
// Process B
if(bin->b->nodeType() == ASTNode::ComparisonExpressionType)
{
ComparisonExpression* b = static_cast<ComparisonExpression*>(bin->b.getPointer());
bounds = updateBounds(*b, float_value, bounds);
}
}
}
else if(if_node->condition->nodeType() == ASTNode::ComparisonExpressionType)
{
ComparisonExpression* comp = static_cast<ComparisonExpression*>(if_node->condition.getPointer());
bounds = updateBounds(*comp, float_value, bounds);
}
}
}
}
return bounds;
}
} // end namespace Winter