-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
143 lines (108 loc) · 3.43 KB
/
main.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
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <llvm-14/llvm/IR/Module.h>
#include <llvm/Support/raw_ostream.h>
#include <sstream>
#include <system_error>
#include <utility>
#include "src/core/lexer/Lexer.h"
#include "src/core/parser/Parser.h"
#include "src/core/sema/Sema.h"
#include "src/core/codegen/Codegen.h"
#include "src/utils/Driver.h"
#include "src/utils/Utils.h"
int main(int argc, const char **argv) {
hlx::CompilerOptions options=hlx::parseArguments(argc, argv);
if(options.displayHelp){
hlx::displayHelp();
return 0;
}
if(options.source.empty())
hlx::error("no source file empty");
if(options.source.extension()!=".hlx")
hlx::error("unexpected source file extension");
std::ifstream file(options.source);
if(!file)
hlx::error("failed to open '"+options.source.string()+'\'');
std::stringstream buffer;
buffer<<file.rdbuf();
hlx::SourceFile sourceFile={options.source.c_str(),buffer.str()};
hlx::Lexer lexer(sourceFile);
hlx::Parser parser(lexer);
auto [ast,success]=parser.parseSourceFile();
if(options.astDump){
for(auto &&fn:ast){
fn->dump();
}
return 0;
}
if(!success)
return 1;
hlx::Sema sema(std::move(ast));
auto resolvedTree=sema.resolveAST();
if(options.resDump){
for(auto &&fn:resolvedTree)
fn->dump();
return 0;
}
if (resolvedTree.empty()) {
return 1;
}
hlx::Codegen codegen(std::move(resolvedTree),options.source.c_str());
llvm::Module *llvmIR=codegen.generateIR();
if(options.llvmDump){
llvmIR->dump();
return 0;
}
std::stringstream path;
path<<"tmp-"<<std::filesystem::hash_value(options.source)<<".ll";
const std::string &&llvmIRPath=path.str();
std::error_code errorCode;
llvm::raw_fd_ostream f(llvmIRPath,errorCode);
llvmIR->print(f, nullptr);
std::stringstream command;
command<<"clang "<<llvmIRPath;
if(!options.output.empty())
command<<"-o"<<options.output;
int ret=std::system(command.str().c_str());
std::filesystem::remove(llvmIRPath);
return ret;
/*
std::ifstream file(argv[1]);
if(!file){
std::cerr<<"Couldn't find file: "<<argv[1]<<'\n';
}
std::stringstream buffer;
buffer << file.rdbuf();
hlx::SourceFile sourceFile = {argv[1], buffer.str()};
hlx::Lexer lexer(sourceFile);
hlx::Parser parser(lexer);
auto [ast, success] = parser.parseSourceFile();
for (auto &&fn : ast)
{
fn->dump();
}
hlx::Sema sema(std::move(ast));
auto res=sema.resolveAST();
std::cerr<<"Resolved: \n";
for (auto &&fn : res) {
fn->dump();
}
hlx::Codegen codegen(std::move(res),argv[1]);
std::error_code EC;
// Open a file for writing
llvm::raw_fd_ostream fileStream("intermediate.ll", EC, llvm::sys::fs::OpenFlags{});
if (EC) {
llvm::errs() << "Could not open file: " << EC.message() << "\n";
return -1;
}
// Write the module to the file
codegen.generateIR()->print(fileStream, nullptr);
// Close the file
fileStream.flush();
//codegen.generateIR()->print(llvm::errs(), nullptr);
return !success;
*/
}