Repository navigation
Expand file tree
/
Copy pathcompiler.cpp
More file actions
66 lines (54 loc) · 1.89 KB
/
Copy pathcompiler.cpp
File metadata and controls
66 lines (54 loc) · 1.89 KB
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
#include <iostream>
#include <fstream>
#include <string>
#include <functional>
#include <tree_sitter/api.h>
#include <tree_sitter/parser.h>
#include <cassert>
#include <memory>
#include <cstring>
#include "TreeSitterParser.hpp"
#include "AST.hpp"
#include "Functions.hpp"
using namespace std;
int main(int argc, const char **argv)
{
// assert that there is a valid argument
bool lessVerbose = true;
if (argc == 3 && std::strcmp("debug",argv[2])==0) {
lessVerbose = false;
}
else if (argc != 2)
{
throw invalid_argument("invalid argument ");
}
const char *filename = argv[1];
CompilationContext ctx;
if (!lessVerbose) printf("Starting...\n");
// initialize ffi functions
initializeAllFFIFunc(ctx);
// start parsing actual file
Parser pars(filename);
if (!lessVerbose)
pars.printTree(true);
auto main_fn = pars.ParseAST();
// ================== DELETE THIS LATER ==================
printf("OpenCL str: \n%s\n", main_fn->returnOpenCLStr().c_str());
exit(0); // ================== DELETE THIS LATER ==================
if (!lessVerbose) printf("============================= CodeGen =======================\n");
auto fn = main_fn->codegen(ctx);
if (!fn) throw invalid_argument("Invalid main function");
error_code EC;
raw_fd_ostream ofile("main.ll", EC);
ctx.mod->print(ofile, nullptr);
if (!lessVerbose) ctx.mod->print(errs(), nullptr);
if (!lessVerbose) printf("================== Compiling to object File =================\n");
bool res = verifyFunction(**fn, &errs());
if (!lessVerbose) printf("Verify function (1 if successful else 0): %d\n", !res);
if (!lessVerbose)
printf("Compiling...\n");
system("clang -Wno-override-module -fPIE -fpie main.ll");
if (!lessVerbose)
printf("Running Executable...\n");
system("./a.out");
}