-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
213 lines (174 loc) · 5.87 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
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
//===- main.cpp - The Mu Compiler -----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements the entry point for the Mu compiler.
//
//===----------------------------------------------------------------------===//
#include "Mu/MuDialect.h"
#include <algorithm>
#include <cstdlib>
#include <memory>
#include <fstream>
#include <iostream>
#include <optional>
#include <valarray>
#include <vector>
#include "Mu/MuMLIRGen.h"
#include "mlir/IR/AsmState.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/Parser/Parser.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"
#include "Mu/Parser/ast.h"
#include "Mu/Support/µt8.h"
using namespace mlir::mu;
namespace cl = llvm::cl;
std::optional<Ref<mu::ast::ASTNodeTracker>> mu::ast::ASTNodeTracker::instance;
const unsigned mu::ast::ASTNode::static_magic_number = 0xdeadbeef;
static cl::opt<std::string> inputFilename(cl::Positional,
cl::desc("<input mu file>"),
cl::init("-"),
cl::value_desc("filename"));
//===- Bison Parse Handling Code ------------------------------------------===//
int yyparse();
extern FILE *yyin;
#if YYDEBUG
extern int yydebug;
#endif
extern mu::ast::TranslationUnit *topnode;
//===----------------------------------------------------------------------===//
/// Returns a Mu AST resulting from parsing the file or a nullptr on error.
namespace {
fv bisonReset() {
// Bison is gross. Reset everything Bison related here.
#if YYDEBUG
yydebug = 0;
#endif
yyin = nullptr;
topnode = nullptr;
}
fn parseInputFile(llvm::StringRef filename)
-> std::unique_ptr<mu::ast::TranslationUnit> {
bisonReset();
// Sure wish this was C23
Defer<decltype(yyin)> D {
yyin = fopen(filename.data(), "r"),
[](auto f) {
fclose(f);
bisonReset();
}
};
if (nullptr == yyin) {
llvm::errs() << "File not found: " << filename << "\n";
exit(EXIT_FAILURE);
}
// Bison is gross, especially GNU Bison 2.3 on macOS where global yyin is the
// input to yyparse()
assert(yyin != nullptr && topnode == nullptr && "Test pre-parse pointers.");
yyparse();
assert(topnode != nullptr && "Expected non-null topnode");
return std::unique_ptr<mu::ast::TranslationUnit>(topnode);
}
enum InputType { Mu, MLIR };
enum Action { None, DumpAST, DumpMLIR };
cl::opt<enum InputType> inputType(
"x", cl::init(Mu), cl::desc("Decided the kind of output desired"),
cl::values(clEnumValN(Mu, "mu", "load the input file as a Mu source.")),
cl::values(clEnumValN(MLIR, "mlir",
"load the input file as an MLIR file")));
cl::opt<enum Action> emitAction(
"emit", cl::desc("Select the kind of output desired"),
cl::values(clEnumValN(DumpAST, "ast", "output the AST dump")),
cl::values(clEnumValN(DumpMLIR, "mlir", "output the MLIR dump")));
fn dumpMLIR() -> int {
mlir::MLIRContext context;
// Load our Dialect in this MLIR Context.
context.getOrLoadDialect<mlir::mu::MuDialect>();
// Handle '.mu' input to the compiler.
if (inputType != InputType::MLIR &&
!llvm::StringRef(inputFilename).endswith(".mlir")) {
if (!llvm::StringRef(inputFilename).endswith(".mu") &&
!llvm::StringRef(inputFilename).endswith(".mulang") &&
!llvm::StringRef(inputFilename).endswith(".\342\232\233")) {
llvm::errs() << "Invalid filetype: " << inputFilename << "\n";
llvm::errs() << "Files given to muc must end in .mu or .⚛️\n";
exit(EXIT_FAILURE);
}
auto moduleAST = parseInputFile(inputFilename);
if (!moduleAST)
return 6;
mlir::OwningOpRef<mlir::ModuleOp> module = mu::mlirGen(context, *moduleAST);
if (!module)
return 1;
module->dump();
return 0;
}
// Otherwise, the input is '.mlir'.
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileOrErr =
llvm::MemoryBuffer::getFileOrSTDIN(inputFilename);
if (std::error_code ec = fileOrErr.getError()) {
llvm::errs() << "Could not open input file: " << ec.message() << "\n";
return -1;
}
// Parse the input mlir.
llvm::SourceMgr sourceMgr;
sourceMgr.AddNewSourceBuffer(std::move(*fileOrErr), llvm::SMLoc());
mlir::OwningOpRef<mlir::ModuleOp> module =
mlir::parseSourceFile<mlir::ModuleOp>(sourceMgr, &context);
if (!module) {
llvm::errs() << "Error can't load file " << inputFilename << "\n";
return 3;
}
module->dump();
return 0;
}
fn dumpAST() -> int {
if (inputType == InputType::MLIR) {
llvm::errs() << "Can't dump a Mu AST when the input is MLIR\n";
return 5;
}
auto moduleAST = parseInputFile(inputFilename);
if (!moduleAST)
return 1;
// dump(*moduleAST);
moduleAST->dump();
#ifndef NDEBUG
llvm::errs() << "Tracked Node Count: "
<< mu::ast::ASTNodeTracker::get().size() << "\n";
#endif
return 0;
}
} // namespace
fn main(int argc, char **argv)->int {
// Register any command line options.
mlir::registerAsmPrinterCLOptions();
mlir::registerMLIRContextCLOptions();
cl::ParseCommandLineOptions(argc, argv, "mu compiler\n");
switch (emitAction) {
case Action::DumpAST: {
if (dumpAST())
return -1;
break;
}
case Action::DumpMLIR: {
if (dumpMLIR())
return -1;
break;
}
default:
llvm::errs() << "No action specified (parsing only?), use -emit=<action>\n";
return -1;
}
mu::ast::ASTNodeTracker::destroy();
}