1 //===- toyc.cpp - The Toy Compiler ----------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the entry point for the Toy compiler.
11 //===----------------------------------------------------------------------===//
14 #include "toy/Dialect.h"
15 #include "toy/Lexer.h"
16 #include "toy/MLIRGen.h"
17 #include "toy/Parser.h"
20 #include <system_error>
23 #include "mlir/IR/AsmState.h"
24 #include "mlir/IR/BuiltinOps.h"
25 #include "mlir/IR/MLIRContext.h"
26 #include "mlir/Parser/Parser.h"
28 #include "llvm/ADT/StringRef.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/ErrorOr.h"
31 #include "llvm/Support/MemoryBuffer.h"
32 #include "llvm/Support/SourceMgr.h"
33 #include "llvm/Support/raw_ostream.h"
36 namespace cl
= llvm::cl
;
38 static cl::opt
<std::string
> inputFilename(cl::Positional
,
39 cl::desc("<input toy file>"),
41 cl::value_desc("filename"));
44 enum InputType
{ Toy
, MLIR
};
46 static cl::opt
<enum InputType
> inputType(
47 "x", cl::init(Toy
), cl::desc("Decided the kind of output desired"),
48 cl::values(clEnumValN(Toy
, "toy", "load the input file as a Toy source.")),
49 cl::values(clEnumValN(MLIR
, "mlir",
50 "load the input file as an MLIR file")));
53 enum Action
{ None
, DumpAST
, DumpMLIR
};
55 static cl::opt
<enum Action
> emitAction(
56 "emit", cl::desc("Select the kind of output desired"),
57 cl::values(clEnumValN(DumpAST
, "ast", "output the AST dump")),
58 cl::values(clEnumValN(DumpMLIR
, "mlir", "output the MLIR dump")));
60 /// Returns a Toy AST resulting from parsing the file or a nullptr on error.
61 std::unique_ptr
<toy::ModuleAST
> parseInputFile(llvm::StringRef filename
) {
62 llvm::ErrorOr
<std::unique_ptr
<llvm::MemoryBuffer
>> fileOrErr
=
63 llvm::MemoryBuffer::getFileOrSTDIN(filename
);
64 if (std::error_code ec
= fileOrErr
.getError()) {
65 llvm::errs() << "Could not open input file: " << ec
.message() << "\n";
68 auto buffer
= fileOrErr
.get()->getBuffer();
69 LexerBuffer
lexer(buffer
.begin(), buffer
.end(), std::string(filename
));
71 return parser
.parseModule();
75 mlir::MLIRContext context
;
76 // Load our Dialect in this MLIR Context.
77 context
.getOrLoadDialect
<mlir::toy::ToyDialect
>();
79 // Handle '.toy' input to the compiler.
80 if (inputType
!= InputType::MLIR
&&
81 !llvm::StringRef(inputFilename
).ends_with(".mlir")) {
82 auto moduleAST
= parseInputFile(inputFilename
);
85 mlir::OwningOpRef
<mlir::ModuleOp
> module
= mlirGen(context
, *moduleAST
);
93 // Otherwise, the input is '.mlir'.
94 llvm::ErrorOr
<std::unique_ptr
<llvm::MemoryBuffer
>> fileOrErr
=
95 llvm::MemoryBuffer::getFileOrSTDIN(inputFilename
);
96 if (std::error_code ec
= fileOrErr
.getError()) {
97 llvm::errs() << "Could not open input file: " << ec
.message() << "\n";
101 // Parse the input mlir.
102 llvm::SourceMgr sourceMgr
;
103 sourceMgr
.AddNewSourceBuffer(std::move(*fileOrErr
), llvm::SMLoc());
104 mlir::OwningOpRef
<mlir::ModuleOp
> module
=
105 mlir::parseSourceFile
<mlir::ModuleOp
>(sourceMgr
, &context
);
107 llvm::errs() << "Error can't load file " << inputFilename
<< "\n";
116 if (inputType
== InputType::MLIR
) {
117 llvm::errs() << "Can't dump a Toy AST when the input is MLIR\n";
121 auto moduleAST
= parseInputFile(inputFilename
);
129 int main(int argc
, char **argv
) {
130 // Register any command line options.
131 mlir::registerAsmPrinterCLOptions();
132 mlir::registerMLIRContextCLOptions();
133 cl::ParseCommandLineOptions(argc
, argv
, "toy compiler\n");
135 switch (emitAction
) {
136 case Action::DumpAST
:
138 case Action::DumpMLIR
:
141 llvm::errs() << "No action specified (parsing only?), use -emit=<action>\n";