-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypecheckVis.cpp
335 lines (315 loc) · 10.6 KB
/
typecheckVis.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright (C) 2014, Daniel S. Fava
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
#include "node.h"
#include "scope.h"
#include "typecheckVis.h"
#include "parser.hpp"
#include <llvm/IR/Value.h>
#include <llvm/IR/Type.h>
#include <llvm/IR/DerivedTypes.h>
#include <llvm/IR/LLVMContext.h>
#include <fstream>
#include <string>
using namespace llvm;
TypeCheckerVisitor::TypeCheckerVisitor()
{
scope = new Scope();
scope->InitializeScope("global");
}
TypeCheckerVisitor::~TypeCheckerVisitor() {
assert(scope->depth() == 1);
scope->FinalizeScope();
delete scope;
}
void TypeCheckerVisitor::setFileName(char* filename)
{
this->filename = filename;
std::ifstream input(filename);
int lineno = 0;
for(std::string line; getline(input, line);) {
fmap[++lineno] = line;
if (verbose) std::cout << line << std::endl;
}
}
void TypeCheckerVisitor::printErrorMessage(std::string message, int lineno)
{
std::cerr << "ERR: " << message << std::endl;
if (filename != NULL) {
std::cerr << filename;
if (lineno > 0) {
std::cerr << " line " << lineno << ": " << std::endl << fmap[lineno] << std::endl;
} else {
std::cerr << std::endl;
}
}
}
void TypeCheckerVisitor::visit(NSkip* element, uint64_t flag)
{
if (verbose) std::cout << "TypeCheckerVisitor " << typeid(element).name() << std::endl;
types.push_front(new SType(Type::getVoidTy(getGlobalContext()), ""));
}
void TypeCheckerVisitor::visit(NInteger* element, uint64_t flag)
{
if (verbose) std::cout << "TypeCheckerVisitor " << typeid(element).name() << std::endl;
types.push_front(new SType(Type::getInt64Ty(getGlobalContext()), ""));
}
void TypeCheckerVisitor::visit(NDouble* element, uint64_t flag)
{
if (verbose) std::cout << "TypeCheckerVisitor " << typeid(element).name() << std::endl;
types.push_front(new SType(Type::getDoubleTy(getGlobalContext()), ""));
}
void TypeCheckerVisitor::visit(NBool* element, uint64_t flag)
{
if (verbose) std::cout << "TypeCheckerVisitor " << typeid(element).name() << std::endl;
types.push_front(new SType(Type::getInt1Ty(getGlobalContext()), ""));
}
void TypeCheckerVisitor::visit(NSecurity* element, uint64_t flag)
{
if (verbose) std::cout << "TypeCheckerVisitor " << typeid(element).name() << " " << element->name << std::endl;
if (element->name == "") element->name = "low";
types.push_front(new SType(NULL, element->name));
}
void TypeCheckerVisitor::visit(NType* element, uint64_t flag)
{
if (verbose) std::cout << "TypeCheckerVisitor " << typeid(element).name() << std::endl;
if (element->name.compare("int") == 0) {
types.push_front(new SType(Type::getInt64Ty(getGlobalContext()), ""));
return;
} else if (element->name.compare("double") == 0) {
types.push_front(new SType(Type::getDoubleTy(getGlobalContext()), ""));
return;
} else if (element->name.compare("bool") == 0) {
types.push_front(new SType(Type::getInt1Ty(getGlobalContext()), ""));
return;
}
types.push_front(new SType(Type::getVoidTy(getGlobalContext()), ""));
return;
}
void TypeCheckerVisitor::visit(NIdentifier* element, uint64_t flag)
{
if (verbose) std::cout << "TypeCheckerVisitor " << typeid(element).name() << " " << element->name << std::endl;
Symbol* sym = scope->LookUp(element->name);
if (sym == NULL) {
printErrorMessage("Undeclared variable " + element->name, element->lineno);
passed = false;
return;
}
types.push_front(sym->stype);
}
void TypeCheckerVisitor::visit(NAssignment* element, uint64_t flag)
{
if (verbose) std::cout << "TypeCheckerVisitor " << typeid(element).name() << std::endl;
Symbol* sym = scope->LookUp(element->lhs.name);
if (sym == NULL) {
printErrorMessage("Undeclared variable " + element->lhs.name, element->lineno);
passed = false;
return;
}
SType* dtype = sym->stype;
if (dtype == NULL) {
assert(!passed);
return;
}
SType* atype = types.front();
if (atype == NULL) {
assert(!passed);
return;
}
types.pop_front();
// Check if the scope allow us to write to a low variable
if (dtype->sec == "low" && scope->getSecurityContext() == "high") {
printErrorMessage("Failed when trying to assign to a low var from a high context (implicit flow)", element->lineno);
passed = false;
return;
}
if (dtype->type != atype->type) {
// TODO: Print legible types:
std::cout << dtype->type << " " << atype->type << std::endl;
printErrorMessage("Failed on types", element->lineno);
passed = false;
return;
}
// If the right hand side expression doesn't have a type,
// its because it doesn't operate on variables.
// In this case, its safe to allow this to proceed.
if (dtype->sec == "low" && atype->sec == "high") {
printErrorMessage("Failed on security (explicit flow)", element->lineno);
passed = false;
return;
}
}
void TypeCheckerVisitor::visit(NVariableDeclaration* element, uint64_t flag)
{
if (verbose) std::cout << "TypeCheckerVisitor " << typeid(element).name() << " " << element->type.name << " " << element->id.name << std::endl;
Symbol* sym = scope->LookUp(element->id.name);
if (sym != NULL) {
printErrorMessage("Variable redeclaration " + element->id.name, element->lineno);
passed = false;
return;
}
SType* tmp;
// Get info about NSecurity
tmp = types.front();
types.pop_front();
if (tmp == NULL) {
assert(!passed);
return;
}
std::string sec = tmp->sec;
delete tmp;
// Get info about NType
tmp = types.front();
types.pop_front();
if (tmp == NULL) {
assert(!passed);
return;
}
Type* dtype = tmp->type;
delete tmp;
scope->Insert(element->id.name, new Symbol(NULL, new SType(dtype, sec)));
}
void TypeCheckerVisitor::visit(NBinaryOperator* element, uint64_t flag)
{
if (verbose) std::cout << "TypeCheckerVisitor " << typeid(element).name() << std::endl;
SType* trhs = types.front();
types.pop_front();
SType* tlhs = types.front();
types.pop_front();
if (tlhs == NULL || trhs == NULL) {
assert(!passed);
return;
}
std::string sec = "";
if (tlhs->sec == "high" || trhs->sec == "high") {
sec = "high";
}
switch (element->op) {
case TPLUS:
case TMINUS:
case TMUL:
case TDIV:
if (tlhs->type == trhs->type && tlhs->type == Type::getInt64Ty(getGlobalContext())) {
types.push_front(new SType(Type::getInt64Ty(getGlobalContext()), sec));
return;
} else if (tlhs->type == trhs->type && tlhs->type == Type::getDoubleTy(getGlobalContext())) {
types.push_front(new SType(Type::getDoubleTy(getGlobalContext()), sec));
return;
}
case TCEQ:
case TCNE:
case TCLT:
case TCLE:
case TCGT:
case TCGE :
if (tlhs->type == trhs->type && tlhs->type == Type::getInt64Ty(getGlobalContext())) {
types.push_front(new SType(Type::getInt1Ty(getGlobalContext()), sec));
return;
} else if (tlhs->type == trhs->type && tlhs->type == Type::getDoubleTy(getGlobalContext())) {
types.push_front(new SType(Type::getInt1Ty(getGlobalContext()), sec));
return;
}
default:
printErrorMessage( "Type mismatch on binary operator", element->lineno );
passed = false;
return;
}
}
void TypeCheckerVisitor::visit(NIfExpression* element, uint64_t flag)
{
switch (flag)
{
case V_FLAG_GUARD | V_FLAG_EXIT:
{
if (verbose) std::cout << "TypeCheckerVisitor if-guard-enter " << typeid(element).name() << std::endl;
SType* gtype = types.front();
types.pop_front();
assert(gtype != NULL);
if (gtype->type != Type::getInt1Ty(getGlobalContext())) {
printErrorMessage("Failed on the guard", element->lineno);
passed = false;
return;
}
guard_sec = gtype->sec;
}
return;
case V_FLAG_EXIT:
guard_sec = "";
return;
default:
return;
}
}
void TypeCheckerVisitor::visit(NWhileExpression* element, uint64_t flag)
{
switch (flag)
{
case V_FLAG_GUARD | V_FLAG_EXIT:
{
if (verbose) std::cout << "TypeCheckerVisitor while-guard-enter " << typeid(element).name() << std::endl;
SType* gtype = types.front();
types.pop_front();
assert(gtype != NULL);
if (gtype->type != Type::getInt1Ty(getGlobalContext())) {
printErrorMessage("Failed on the guard", element->lineno);
passed = false;
return;
}
guard_sec = gtype->sec;
}
return;
case V_FLAG_EXIT:
guard_sec = "";
return;
default:
return;
}
}
void TypeCheckerVisitor::visit(NExpressionStatement* element, uint64_t flag)
{
if (verbose) std::cout << "TypeCheckerVisitor " << typeid(element).name() << std::endl;
}
void TypeCheckerVisitor::visit(NBlock* element, uint64_t flag)
{
static int size_on_entering = 0;
static int size_on_leaving = 0;
switch (flag)
{
case V_FLAG_ENTER:
{
if (verbose) std::cout << "TypeCheckerVisitor entering " << typeid(element).name() << std::endl;
size_on_entering = types.size();
//std::cout << "Size on entering: " << size_on_entering << std::endl;;
std::string next_sec = scope->getSecurityContext() == "high" ? "high" : guard_sec;
next_sec = (next_sec == "" ? "low" : next_sec);
if (verbose) std::cout << "TypeCheckerVisitor initializing scope to: " << next_sec << std::endl;;
scope->InitializeScope("", next_sec);
}
break;
case V_FLAG_EXIT:
if (verbose) std::cout << "TypeCheckerVisitor leaving " << typeid(element).name() << std::endl;
size_on_leaving = types.size();
//std::cout << "Size on leaving: " << size_on_leaving << std::endl;;
scope->FinalizeScope();
break;
default:
assert(0);
}
}