1//===- toyc.cpp - The Toy Compiler ----------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the entry point for the Toy compiler.
10//
11//===----------------------------------------------------------------------===//
12
13#include "toy/AST.h"
14#include "toy/Dialect.h"
15#include "toy/Lexer.h"
16#include "toy/MLIRGen.h"
17#include "toy/Parser.h"
18#include <memory>
19#include <string>
20#include <system_error>
21#include <utility>
22
23#include "mlir/IR/AsmState.h"
24#include "mlir/IR/BuiltinOps.h"
25#include "mlir/IR/MLIRContext.h"
26#include "mlir/Parser/Parser.h"
27
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"
34
35using namespace toy;
36namespace cl = llvm::cl;
37
38static cl::opt<std::string> inputFilename(cl::Positional,
39 cl::desc("<input toy file>"),
40 cl::init(Val: "-"),
41 cl::value_desc("filename"));
42
43namespace {
44enum InputType { Toy, MLIR };
45} // namespace
46static cl::opt<enum InputType> inputType(
47 "x", cl::init(Val: 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")));
51
52namespace {
53enum Action { None, DumpAST, DumpMLIR };
54} // namespace
55static 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")));
59
60/// Returns a Toy AST resulting from parsing the file or a nullptr on error.
61std::unique_ptr<toy::ModuleAST> parseInputFile(llvm::StringRef filename) {
62 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileOrErr =
63 llvm::MemoryBuffer::getFileOrSTDIN(Filename: filename);
64 if (std::error_code ec = fileOrErr.getError()) {
65 llvm::errs() << "Could not open input file: " << ec.message() << "\n";
66 return nullptr;
67 }
68 auto buffer = fileOrErr.get()->getBuffer();
69 LexerBuffer lexer(buffer.begin(), buffer.end(), std::string(filename));
70 Parser parser(lexer);
71 return parser.parseModule();
72}
73
74int dumpMLIR() {
75 mlir::MLIRContext context;
76 // Load our Dialect in this MLIR Context.
77 context.getOrLoadDialect<mlir::toy::ToyDialect>();
78
79 // Handle '.toy' input to the compiler.
80 if (inputType != InputType::MLIR &&
81 !llvm::StringRef(inputFilename).ends_with(Suffix: ".mlir")) {
82 auto moduleAST = parseInputFile(filename: inputFilename);
83 if (!moduleAST)
84 return 6;
85 mlir::OwningOpRef<mlir::ModuleOp> module = mlirGen(context, *moduleAST);
86 if (!module)
87 return 1;
88
89 module->dump();
90 return 0;
91 }
92
93 // Otherwise, the input is '.mlir'.
94 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileOrErr =
95 llvm::MemoryBuffer::getFileOrSTDIN(Filename: inputFilename);
96 if (std::error_code ec = fileOrErr.getError()) {
97 llvm::errs() << "Could not open input file: " << ec.message() << "\n";
98 return -1;
99 }
100
101 // Parse the input mlir.
102 llvm::SourceMgr sourceMgr;
103 sourceMgr.AddNewSourceBuffer(F: std::move(*fileOrErr), IncludeLoc: llvm::SMLoc());
104 mlir::OwningOpRef<mlir::ModuleOp> module =
105 mlir::parseSourceFile<mlir::ModuleOp>(sourceMgr, config: &context);
106 if (!module) {
107 llvm::errs() << "Error can't load file " << inputFilename << "\n";
108 return 3;
109 }
110
111 module->dump();
112 return 0;
113}
114
115int dumpAST() {
116 if (inputType == InputType::MLIR) {
117 llvm::errs() << "Can't dump a Toy AST when the input is MLIR\n";
118 return 5;
119 }
120
121 auto moduleAST = parseInputFile(filename: inputFilename);
122 if (!moduleAST)
123 return 1;
124
125 dump(*moduleAST);
126 return 0;
127}
128
129int main(int argc, char **argv) {
130 // Register any command line options.
131 mlir::registerAsmPrinterCLOptions();
132 mlir::registerMLIRContextCLOptions();
133 cl::ParseCommandLineOptions(argc, argv, Overview: "toy compiler\n");
134
135 switch (emitAction) {
136 case Action::DumpAST:
137 return dumpAST();
138 case Action::DumpMLIR:
139 return dumpMLIR();
140 default:
141 llvm::errs() << "No action specified (parsing only?), use -emit=<action>\n";
142 }
143
144 return 0;
145}
146

source code of mlir/examples/toy/Ch2/toyc.cpp