Skip to content

Commit

Permalink
refactor main into a compiler class (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackparsonss committed Jun 27, 2024
1 parent 4c83ea2 commit 8595de6
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 61 deletions.
5 changes: 2 additions & 3 deletions include/backend/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ class Backend {
mlir::Value visit(shared_ptr<ast::Node>);

public:
shared_ptr<ast::Block> ast;
explicit Backend(shared_ptr<ast::Block>);
explicit Backend();

void codegen(std::ostream& outstream);
void to_object(std::string filename);

shared_ptr<ast::Block> traverse();
shared_ptr<ast::Block> traverse(shared_ptr<ast::Block> ast);
mlir::Value visit_block(shared_ptr<ast::Block>);
mlir::Value visit_integer_literal(shared_ptr<ast::IntegerLiteral>);
mlir::Value visit_character_literal(shared_ptr<ast::CharacterLiteral>);
Expand Down
40 changes: 40 additions & 0 deletions include/compiler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include <memory>

#include "ANTLRFileStream.h"
#include "FusionLexer.h"
#include "ParseTree.h"
#include "ast/builder.h"
#include "ast/symbol/symbol_table.h"
#include "backend/backend.h"

using std::shared_ptr, std::unique_ptr;

class Compiler {
private:
shared_ptr<SymbolTable> symbol_table;
unique_ptr<Backend> backend;
unique_ptr<Builder> builder;

antlr4::ANTLRFileStream* file;
fusion::FusionLexer* lexer;
antlr4::tree::ParseTree* tree;
antlr4::CommonTokenStream* tokens;
fusion::FusionParser* parser;

public:
Compiler(std::string filename,
shared_ptr<SymbolTable> symbol_table,
unique_ptr<Backend> backend,
unique_ptr<Builder> builder);
~Compiler();

void build_ast();
void run_passes();
void xml();

void build_backend();
void to_object(std::string filename);
void codegen(std::ofstream& outfile);
};
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
set(
fusion_src_files
"${CMAKE_CURRENT_SOURCE_DIR}/main.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/compiler.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/backend/backend.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/backend/visitor.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/backend/io.cpp"
Expand Down
6 changes: 3 additions & 3 deletions src/ast/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,11 @@ void ast::Function::xml(int level) {
<< type->get_name() << "\" name=\"" << name << "\" ref_name=\""
<< ref_name << "\">\n";

std::cout << std::string(level * 4, ' ') << "<parameters>\n";
std::cout << std::string((level + 1) * 4, ' ') << "<parameters>\n";
for (const auto& param : params) {
param->xml(level + 1);
param->xml(level + 2);
}
std::cout << std::string(level * 4, ' ') << "</parameters>\n";
std::cout << std::string((level + 1) * 4, ' ') << "</parameters>\n";
body->xml(level + 1);

std::cout << std::string(level * 4, ' ') << "</function>\n";
Expand Down
5 changes: 2 additions & 3 deletions src/backend/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
#include "mlir/Target/LLVMIR/Export.h"

Backend::Backend(shared_ptr<ast::Block> ast) {
this->ast = ast;
Backend::Backend() {
ctx::builder->setInsertionPointToStart(ctx::module->getBody());
builtin::define_all();
}

shared_ptr<ast::Block> Backend::traverse() {
shared_ptr<ast::Block> Backend::traverse(shared_ptr<ast::Block> ast) {
visit(ast);

if (mlir::failed(mlir::verify(*ctx::module))) {
Expand Down
76 changes: 76 additions & 0 deletions src/compiler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <cassert>

#include "ANTLRFileStream.h"
#include "CommonTokenStream.h"
#include "FusionLexer.h"
#include "FusionParser.h"
#include "ast/passes/pass.h"
#include "compiler.h"
#include "errors.h"

Compiler::Compiler(std::string filename,
shared_ptr<SymbolTable> symbol_table,
unique_ptr<Backend> backend,
unique_ptr<Builder> builder) {
this->symbol_table = symbol_table;
this->backend = std::move(backend);
this->builder = std::move(builder);

file = new antlr4::ANTLRFileStream();
file->loadFromFile(filename);

lexer = new fusion::FusionLexer(file);
tokens = new antlr4::CommonTokenStream(lexer);
parser = new fusion::FusionParser(tokens);

tree = parser->file();
}

Compiler::~Compiler() {
delete file;
delete lexer;
delete tokens;
delete parser;
}

void Compiler::build_ast() {
try {
builder->visit(tree);
assert(builder->has_ast());
} catch (CompileTimeException const& e) {
std::cerr << e.what() << std::endl;
exit(1);
}
}

void Compiler::run_passes() {
try {
Pass::run_passes(builder->get_ast(), symbol_table);
assert(builder->has_ast());
} catch (CompileTimeException const& e) {
std::cerr << e.what() << std::endl;
exit(1);
}
}

void Compiler::xml() {
builder->get_ast()->xml(0);
}

void Compiler::build_backend() {
backend->traverse(this->builder->get_ast());
}

void Compiler::to_object(std::string filename) {
backend->to_object(filename + ".o");

std::string command = "clang " + filename + ".o -o " + filename;
system(command.c_str());

command = "rm " + filename + ".o";
system(command.c_str());
}

void Compiler::codegen(std::ofstream& outfile) {
backend->codegen(outfile);
}
78 changes: 26 additions & 52 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,87 +1,61 @@
#include "FusionLexer.h"
#include "FusionParser.h"

#include "ANTLRFileStream.h"
#include "CommonTokenStream.h"
#include "tree/ParseTree.h"

#include "ast/builder.h"
#include "ast/passes/pass.h"
#include "ast/symbol/symbol_table.h"
#include "backend/backend.h"
#include "errors.h"
#include "compiler.h"
#include "shared/context.h"

#include <iostream>
#include <memory>

int main(int argc, char** argv) {
antlr4::ANTLRFileStream afs;
afs.loadFromFile(argv[1]);
fusion::FusionLexer lexer(&afs);
antlr4::CommonTokenStream tokens(&lexer);
fusion::FusionParser parser(&tokens);

antlr4::tree::ParseTree* tree = parser.file();
using std::shared_ptr, std::unique_ptr, std::make_shared, std::make_unique;

int main(int argc, char** argv) {
ctx::initialize_context();
std::shared_ptr<SymbolTable> symtab = std::make_shared<SymbolTable>();

Builder builder = Builder(symtab);
shared_ptr<SymbolTable> symbol_table = make_shared<SymbolTable>();
unique_ptr<Builder> builder = make_unique<Builder>(symbol_table);
unique_ptr<Backend> backend = make_unique<Backend>();
Compiler compiler =
Compiler(argv[1], symbol_table, std::move(backend), std::move(builder));

try {
builder.visit(tree);
assert(builder.has_ast());
for (size_t i = 0; i < argc; i++) {
std::string arg = std::string(argv[i]);
if (arg == "--xml") {
builder.get_ast()->xml(0);
}
compiler.build_ast();
for (size_t i = 0; i < argc; i++) {
std::string arg = std::string(argv[i]);
if (arg == "--xml") {
compiler.xml();
}
}

Pass::run_passes(builder.get_ast(), symtab);
assert(builder.has_ast());
for (size_t i = 0; i < argc; i++) {
std::string arg = std::string(argv[i]);
if (arg == "--pass-xml") {
builder.get_ast()->xml(0);
}
compiler.run_passes();
for (size_t i = 0; i < argc; i++) {
std::string arg = std::string(argv[i]);
if (arg == "--pass-xml") {
compiler.xml();
}
} catch (CompileTimeException const& e) {
std::cerr << e.what() << std::endl;
return 1;
}

Backend backend = Backend(builder.get_ast());
backend.traverse();

// backend args
compiler.build_backend();
for (size_t i = 0; i < argc; i++) {
std::string arg = std::string(argv[i]);
if (arg == "--emit-llvm" && i == argc - 1) {
std::cerr << "You must provide a file to target llvm ir"
<< std::endl;
std::cerr << "You must provide a file emit llvm ir to" << std::endl;
return 1;
}

if (arg == "-o" && i == argc - 1 && argv[i + 1][0] != '-') {
std::cerr << "You must provide a file to target" << std::endl;
if (arg == "-o" && i == argc - 1) {
std::cerr << "You must provide a file to compile" << std::endl;
return 1;
}

if (arg == "-o") {
std::string filename = std::string(argv[i + 1]);
backend.to_object(filename + ".o");

std::string command = "clang " + filename + ".o -o " + filename;
system(command.c_str());

command = "rm " + filename + ".o";
system(command.c_str());
compiler.to_object(filename);
}

if (arg == "--emit-llvm") {
std::ofstream outfile(argv[i + 1]);
backend.codegen(outfile);
compiler.codegen(outfile);
}
}

Expand Down

0 comments on commit 8595de6

Please sign in to comment.