-
Notifications
You must be signed in to change notification settings - Fork 0
/
Translate.cpp
653 lines (632 loc) · 19.1 KB
/
Translate.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
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/*
* Copyright (c) 2016, Johns Hopkins University Applied Physics Laboratory
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <cstdio>
#include <memory>
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iterator>
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Parse/ParseAST.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/raw_ostream.h"
using namespace clang;
class MyASTVisitor : public RecursiveASTVisitor<MyASTVisitor> {
public:
MyASTVisitor(SourceManager &SM) : TheSourceManager(SM) {}
bool VisitRecordDecl(RecordDecl * crd) {
std::string recordName = crd->getNameAsString();
std::stringstream fields;
if (existsRecordFile) {
recordFile.open("records.v", std::ofstream::out | std::ofstream::app);
} else {
recordFile.open("records.v");
existsRecordFile = true;
}
if (!recordFile.is_open()) {
std::cout << "Cannot create records file\n";
existsRecordFile = false;
return false;
}
RecordDecl::field_iterator itr;
for (itr = crd->field_begin(); itr != crd->field_end(); itr++) {
if (itr == crd->field_begin()) {
fields << ExploreDecl(*itr);
} else {
fields << ";" << ExploreDecl(*itr);
}
}
recordFile << "Record "
<< recordName
<< " : Type := "
<< "mk_" << recordName
<< "{"
<< fields.str()
<< "}.\n";
recordFile.close();
return false;
}
bool VisitFunctionDecl(FunctionDecl *f) {
std::string locals_list = "nil";
std::string var_decl = "";
std::set<std::string>::iterator itr;
if (f->hasBody()) {
std::ofstream coqfile;
std::string function_name = f->getNameInfo().getName().getAsString();
std::string filename = function_name + ".v";
coqfile.open(filename);
if (!coqfile.is_open()) {
std::cout << "Cannot create file " << filename << "\n";
return false;
}
//Clear out any variables from a previous function
//and insert return' and errno' which are always there
//even if they aren't used. As a bonus, there shouldn't be any
//naming conflicts with these as ' is not allowed in a variable name
//in C, but is allowed in COQ
vars.clear();
vars.insert("return'");
vars.insert("errno'");
//Populate the vars set with the parameters of the function
for(ParmVarDecl ** itr = f->param_begin(); itr != f->param_end(); itr++) {
ExploreDecl(*itr);
}
//The parameters are now assumed to be in the locals set
for(itr = vars.begin(); itr != vars.end(); itr++) {
locals_list = *itr + "::" + locals_list;
}
//Convert the function body to sl-mech "code", and as a side effect
//pick up all declared variables
Stmt * FuncBody = cast<Stmt>(f->getBody());
std::string program = ExploreStmt(FuncBody);
//List out the declared variables (x y z : var) for the precondition
if (!vars.empty()) {
var_decl = ": var)";
for (itr = vars.begin(); itr != vars.end(); itr++) {
var_decl = *itr + " " + var_decl;
}
var_decl = "(" + var_decl;
}
//Create the main body of our theorem
coqfile << "Theorem "
<< function_name
<<" :\n" //The theorem has the same name as our function
<< "forall" //Some preconditions that are always true
<< " (st2 : state) "
<< "(s : store_f)(h : heap_f)(l : locals)(d : domain)"
<< var_decl // the declared variables from earlier
<< ","
<< "(lift_prop_sprop (store_bits "
<< locals_list //special + paraemeter vars are local
<< " l) )"
<< "(*ADDITIONAL_PRECONDITIONS*)" //User defined preconditions?
<< "{|srf := s ; lc := l ; hpf := h ; dm := d|}"//Start state
<< "\n->\n(completes {|srf := s ; lc := l ; hpf := h ; dm := d|} st2\n"
<< program //The main body of the function
<< ")->(*POSTCONDITIONS*) st2\n\n"; //User defined postconditions
coqfile.close();
}
//We don't want the walker to go through the rest of this as we take care of it ourselves
return false;
}
private:
std::set<std::string> vars; //The set of all variables in a function
std::ofstream recordFile;
bool existsRecordFile = false;
SourceManager &TheSourceManager;
//Attempts to translate a unary operator into the proper sl-mech code
std::string ExploreUnOp(UnaryOperator * uo) {
bool hasAssignment = false; //is the unary operator assigning a value to a variable
std::string sl_op; //the corresponding binary sl-mech operator
std::stringstream lhs; //The left hand side of the sl-mech operator
std::stringstream rhs; //The right hand side of the sl-mech operator
Expr * subExpr = uo->getSubExpr(); //The subexpression of the unary operator
std::string sub_str = ExploreExpr(subExpr); //Recursively walks down this branch
std::stringstream ss; //For expressions that do not fit the "usual" mold
switch (uo->getOpcode()) {
case UO_PostInc :
hasAssignment = true;
sl_op = "eplus";
rhs << " ( "
<< typeModifier(subExpr)
<< " 1 ) ";
lhs << sub_str;
break;
case UO_PostDec :
hasAssignment = true;
sl_op = "eminus";
rhs << " ( "
<< typeModifier(subExpr)
<< " 1 ) ";
lhs << sub_str;
break;
case UO_PreInc : //These don't capture the order of the assignments properly
hasAssignment = true;
sl_op = "eplus";
rhs << " ( "
<< typeModifier(subExpr)
<< " 1 ) ";
lhs << sub_str;
break;
case UO_PreDec :
hasAssignment = true;
sl_op = "eplus";
rhs << " ( "
<< typeModifier(subExpr)
<< " 1 ) ";
lhs << sub_str;
break;
case UO_AddrOf :
ss << "["
<< sub_str
<< "]";
return ss.str();
case UO_Deref :
ss << "["
<< sub_str
<< "]";
return ss.str();
case UO_Plus :
hasAssignment = false;
sl_op = "eplus";
lhs << " ( "
<< typeModifier(subExpr)
<< " 0 ) ";
rhs << sub_str;
break;
case UO_Minus :
hasAssignment = false;
sl_op = "eminus";
lhs << " ( "
<< typeModifier(subExpr)
<< " 0 ) ";
rhs << sub_str;
break;
case UO_Not :
return "BITWISE_NOT_PLACEHOLDER";
case UO_LNot :
ss << " ( bnot ( "
<< sub_str
<< " ) ) ";
return ss.str();
case UO_Real :
return "REAL_PLACEHOLDER";
case UO_Imag :
return "IMAG_PLACEHOLDER";
case UO_Extension :
return "EXTENSION_PLACEHOLDER";
default :
return "UNKNOWN_UNARY_OP";
}
return createBinOp(lhs.str(), rhs.str(), sl_op, hasAssignment);
}
//Produces sl-mech code for binary operators
std::string ExploreBinOp(BinaryOperator * bo) {
std::string lhs_str = ExploreExpr(bo->getLHS());//Recursively explore the left
std::string rhs_str = ExploreExpr(bo->getRHS());//and right hand sides of the binop
std::string sl_op;//This is the sl-mech operator
bool hasAssignment;//This tells us if the binary operator does an assignment
std::stringstream ss;//For unusual binary operators
switch (bo->getOpcode()) {
case BO_PtrMemD :
return "PTR_MEMD_PLACEHOLDER";
case BO_PtrMemI :
return "PTR_MEMI_PLACEHODLER";
case BO_Mul :
sl_op = "emul";
hasAssignment = false;
break;
case BO_Div :
sl_op = "ediv";
hasAssignment = false;
break;
case BO_Rem :
sl_op = "emod";
hasAssignment = false;
break;
case BO_Add :
sl_op = "eplus";
hasAssignment = false;
break;
case BO_Sub :
sl_op = "eminus";
hasAssignment = false;
break;
case BO_Shl :
return "SHL_PLACEHOLDER";
case BO_Shr :
return "SHR_PLACEHOLDER";
case BO_LT :
sl_op = "blt";
hasAssignment = false;
break;
case BO_GT :
sl_op = "bgt";
hasAssignment = false;
break;
case BO_LE :
sl_op = "ble";
hasAssignment = false;
break;
case BO_GE :
sl_op = "bge";
hasAssignment = false;
break;
case BO_EQ :
sl_op = "beq";
hasAssignment = false;
break;
case BO_NE :
ss << "bnot("
<< createBinOp(lhs_str, rhs_str, "beq", false)
<< ")";
return ss.str();
case BO_And :
return "BITWISE_AND_PLACEHOLDER";
case BO_Xor :
return "BITWISE_XOR_PLACEHOLDER";
case BO_Or :
return "BITWISE_OR_PLACEHOLDER";
case BO_LAnd :
sl_op = "band";
hasAssignment = false;
break;
case BO_LOr :
sl_op = "bor";
hasAssignment = false;
break;
case BO_Assign :
ss << lhs_str
<< "≔"
<< rhs_str;
return ss.str();
case BO_MulAssign :
sl_op = "emul";
hasAssignment = true;
break;
case BO_DivAssign :
sl_op = "ediv";
hasAssignment = true;
break;
case BO_RemAssign :
sl_op = "emod";
hasAssignment = true;
break;
case BO_AddAssign :
sl_op = "eplus";
hasAssignment = true;
break;
case BO_SubAssign :
sl_op = "eminus";
hasAssignment = true;
break;
case BO_ShlAssign :
return "SHL_ASSIGN_PLACEHOLDER";
case BO_ShrAssign :
return "SHR_ASSIGN_PLACEHOLDER";
case BO_AndAssign :
return "BITWISE_AND_ASSIGN_PLACEHOLDER";
case BO_XorAssign :
return "BITWISE_XOR_ASSIGN_PLACEHOLDER";
case BO_OrAssign :
return "BITWISE_OR_ASSIGN_PLACEHOLDER";
case BO_Comma :
return "COMMA_PLACEHOLDER";
default :
return "UNKNOWN BINOP\n";
}
return createBinOp(lhs_str, rhs_str, sl_op, hasAssignment);
}
//Given an expression this determines the type of expression and builds the proper
//sl-mech code from there
std::string ExploreExpr(Expr * e) {
if (e == NULL) {
return "EMPTY_EXPR";
}
std::stringstream ss;
//Reference to an already declared variable
//so far we only need the name
if (isa<DeclRefExpr>(e)) {
DeclRefExpr * dre = cast<DeclRefExpr>(e);
ss << dre->getNameInfo().getAsString();
return ss.str();
}
//An integer of any type
if (isa<IntegerLiteral>(e)) {
IntegerLiteral * il = cast<IntegerLiteral>(e);
//Extract the variable type and value
ss << " ( "
<< typeModifier(e)
<< " "
<< il->getValue().getLimitedValue()
<< " ) ";
return ss.str();
}
//A binary operator
if (isa<BinaryOperator>(e)) {
BinaryOperator * bo = cast<BinaryOperator>(e);
return ExploreBinOp(bo);
}
//We don't really care about the implicit cast here,
//just the expression contained within
if (isa<ImplicitCastExpr>(e)) {
ImplicitCastExpr * ice = cast<ImplicitCastExpr>(e);
return ExploreExpr(ice->getSubExpr());
}
//Unary operator
if (isa<UnaryOperator>(e)) {
UnaryOperator * uo = cast<UnaryOperator>(e);
return ExploreUnOp(uo);
}
e->dumpColor();
ss << "UNKNOWN EXPR\n";
return ss.str();
}
//Given a declaration this determines the type of declaration and builds the proper sl-mech code
std::string ExploreDecl(Decl * d) {
std::stringstream ss;
//An empty declaration
if (isa<EmptyDecl>(d)) {
return "skip";
}
//A variable declaration
if (isa<VarDecl>(d)) {
VarDecl * nd = cast<VarDecl>(d);
std::string varName = nd->getNameAsString();
vars.insert(varName); //Add the name to the set of all vars
ss << "local "
<< varName //locals x skip
<< " skip";
if (nd->hasInit() && nd->getInitStyle() == VarDecl::CInit) {
//This is a decl that also initializes the var
ss << ";; ( assign "
<< varName
<< " "
<< ExploreExpr(nd->getInit())
<< " ) "; //local x skip;;assign x y
}
return ss.str();
}
if (isa<FieldDecl>(d)) {
FieldDecl * fd = cast<FieldDecl>(d);
ss << fd->getNameAsString() << " : var ";
return ss.str();
}
if (isa<RecordDecl>(d)) {
RecordDecl * crd = cast<RecordDecl>(d);
std::string recordName = crd->getNameAsString();
std::stringstream fields;
RecordDecl::field_iterator itr;
for (itr = crd->field_begin(); itr != crd->field_end(); itr++) {
fields << ExploreDecl(*itr);
}
ss << "Record "
<< recordName
<< " : Set := "
<< "mk" << recordName
<< "{"
<< fields.str()
<< "}";
}
d->dumpColor();
return "UNKNOWN DECL\n";
}
//Given a statement, determine the type of statement and build the proper sl-mech code
std::string ExploreStmt(Stmt * s) {
if (s == NULL)
return " skip";
std::stringstream ss;
//Compound statement as in {stmt;stmt}
if (isa<CompoundStmt>(s)) {
CompoundStmt * cs = cast<CompoundStmt>(s);
//Iterate through the compound statement, translating sequentially
ss << "(";
for(Stmt ** itr = cs->body_begin(); itr != cs->body_end(); itr++) {
ss << ExploreStmt(*itr) << ";;\n";
}
ss << "skip)";
return ss.str();
}
//This is a statement about (possibly many) declarations
if (isa<DeclStmt>(s)) {
DeclStmt * ds = cast<DeclStmt>(s);
//Iterate through each declaration, traslating sequentially
ss << "(";
for (Decl ** itr = ds->decl_begin(); itr != ds->decl_end(); itr++) {
ss << ExploreDecl(*itr) << ";;";
}
ss << "skip)";
return ss.str();
}
//A do-while loop
if (isa<DoStmt>(s)) {
DoStmt * ds = cast<DoStmt>(s);
std::string body = ExploreStmt(ds->getBody());
//Only while loops exist in sl-mech, so this translates to
//do{body}while(cond) -> {body while(cond) {body}}
ss << body
<< " ( while "
<< ExploreExpr(ds->getCond())
<< body
<< " ) ";
return ss.str();
}
//A for loop
if (isa<ForStmt>(s)) {
ForStmt * fs = cast<ForStmt>(s);
//Only while loops exist in sl-mech so we translate to
//for(init;cond;incr){body} -> {init; while(cond){body;incr;}}
ss << ExploreStmt(fs->getInit())
<< ";; ( while "
<< ExploreExpr(fs->getCond())
<< ExploreStmt(fs->getBody())
<< ExploreExpr(fs->getInc())
<< " ) ";
return ss.str();
}
//An if statement
if (isa<IfStmt>(s)) {
IfStmt * is = cast<IfStmt>(s);
ss << " (ifelse "
<< ExploreExpr(is->getCond())
<< ExploreStmt(is->getThen())
<< ExploreStmt(is->getElse())
<< " ) ";
return ss.str();
}
//The empty statemnt ";"
if (isa<NullStmt>(s)) {
ss << "skip";
return ss.str();
}
//The return statement in a function
if (isa<ReturnStmt>(s)) {
ReturnStmt * rs = cast<ReturnStmt>(s);
ss << " ( assign __return__ " //Not quite right for void function returns
<< ExploreExpr(rs->getRetValue())
<< " ) ;; ret ";
return ss.str();
}
//A while statement
if (isa<WhileStmt>(s)){
WhileStmt * ws = cast<WhileStmt>(s);
ss << " ( while "
<< ExploreExpr(ws->getCond())
<< ExploreStmt(ws->getBody())
<< " ) ";
return ss.str();
}
//An expression
if (isa<Expr>(s)) {
Expr * e = cast<Expr>(s);
return ExploreExpr(e);
}
s->dumpColor();
ss << "UNKNOWN STMT TYPE\n";
return ss.str();
}
//Constructs a binary operation from the left hand side, right hand side, and
//the name of the operator in sl-mech
std::string createBinOp(std::string lhs, std::string rhs,
std::string binOp, bool isAssignment) {
std::stringstream ss;
if (!isAssignment) {
ss << binOp
<< " ("
<< lhs
<< " "
<< rhs
<< ") ";
} else {
//This is for operators such as += and *=
ss << " ( assign "
<< lhs
<< " "
<< createBinOp(lhs, rhs, binOp, false)
<< " ) ";
}
return ss.str();
}
//Given an integer and a C type, this casts the integer into a val in sl-mech
std::string typeModifier(Expr * e) {
std::string type_str;
const Type * ty = e->getType().getTypePtr();
if (ty->isBuiltinType()) {
const BuiltinType * bt = dyn_cast<BuiltinType>(ty);
switch (bt->getKind()) {
case BuiltinType::Int :
type_str = "Z_to_sint32 "; break;
case BuiltinType::UInt :
type_str = "Z_to_uint32 "; break;
case BuiltinType::Long :
type_str = "Z_to_sint64 "; break;
case BuiltinType::ULong :
type_str = "Z_to_uint64 "; break;
default :
type_str = "TYPE_NOT_HANDLED "; break;
}
} else {
type_str = "NON_CANONICAL_TYPE ";
}
return type_str;
}
};
class MyASTConsumer : public ASTConsumer {
public:
MyASTConsumer(SourceManager &SM) : Visitor(SM) {}
virtual bool HandleTopLevelDecl(DeclGroupRef DR) {
for (DeclGroupRef::iterator b = DR.begin(), e = DR.end(); b != e; ++b)
Visitor.TraverseDecl(*b);
return true;
}
private:
MyASTVisitor Visitor;
};
int main(int argc, char *argv[]) {
if (argc < 2) {
llvm::errs() << "Usage: " << argv[0] << " <filename>\n";
return 1;
}
CompilerInstance TheCompInst;
TheCompInst.createDiagnostics();
LangOptions &lo = TheCompInst.getLangOpts();
lo.CPlusPlus = 1;
auto TO = std::make_shared<TargetOptions>();
TO->Triple = llvm::sys::getDefaultTargetTriple();
TargetInfo *TI =
TargetInfo::CreateTargetInfo(TheCompInst.getDiagnostics(), TO);
TheCompInst.setTarget(TI);
TheCompInst.createFileManager();
FileManager &FileMgr = TheCompInst.getFileManager();
TheCompInst.createSourceManager(FileMgr);
SourceManager &SourceMgr = TheCompInst.getSourceManager();
TheCompInst.createPreprocessor(TU_Module);
TheCompInst.createASTContext();
const FileEntry *FileIn = FileMgr.getFile(argv[1]);
SourceMgr.setMainFileID(
SourceMgr.createFileID(FileIn, SourceLocation(), SrcMgr::C_User));
TheCompInst.getDiagnosticClient().BeginSourceFile(
TheCompInst.getLangOpts(), &TheCompInst.getPreprocessor());
MyASTConsumer TheConsumer(SourceMgr);
ParseAST(TheCompInst.getPreprocessor(), &TheConsumer,
TheCompInst.getASTContext());
return 0;
}