Skip to content

Commit

Permalink
compile!
Browse files Browse the repository at this point in the history
  • Loading branch information
rapiz1 committed Nov 3, 2021
1 parent c21a12d commit 05d4aa0
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmdargs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <iostream>
#include <string>

const std::string usage = "Usage: clox [script]\n";
const std::string usage = "Usage: clox [source]\n";

CmdArgs::CmdArgs(int argc, char** argv) {
lexOutput = false;
Expand Down
3 changes: 3 additions & 0 deletions main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string>

#include "cmdargs.h"
#include "object.h"
#include "parser.h"
#include "scanner.h"

Expand All @@ -25,6 +26,8 @@ int run(const string& s) {
cout << endl;
}
if (options->printIR()) l.mod->print(llvm::outs(), nullptr);
l.mod->print(llvm::errs(), nullptr);
compile(v);
return 0;
}

Expand Down
62 changes: 62 additions & 0 deletions object.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "object.h"

#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"

using namespace llvm;

void compile(CodeGenVisitor v) {
auto TargetTriple = sys::getDefaultTargetTriple();
InitializeAllTargetInfos();
InitializeAllTargets();
InitializeAllTargetMCs();
InitializeAllAsmParsers();
InitializeAllAsmPrinters();
std::string Error;

auto Target = TargetRegistry::lookupTarget(TargetTriple, Error);

// Print an error and exit if we couldn't find the requested target.
// This generally occurs if we've forgotten to initialise the
// TargetRegistry or we have a bogus target triple.
if (!Target) {
errs() << Error;
return;
}

auto CPU = "generic";
auto Features = "";

TargetOptions opt;
auto RM = Optional<Reloc::Model>();
auto TargetMachine =
Target->createTargetMachine(TargetTriple, CPU, Features, opt, RM);
v.getMod()->setDataLayout(TargetMachine->createDataLayout());
v.getMod()->setTargetTriple(TargetTriple);

auto Filename = "output.o";
std::error_code EC;
raw_fd_ostream dest(Filename, EC, sys::fs::OF_None);

if (EC) {
errs() << "Could not open file: " << EC.message();
return;
}

legacy::PassManager pass;
auto FileType = CGFT_ObjectFile;

if (TargetMachine->addPassesToEmitFile(pass, dest, nullptr, FileType)) {
errs() << "TargetMachine can't emit a file of this type";
return;
}

pass.run(*v.getMod());
dest.flush();
}
3 changes: 3 additions & 0 deletions object.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once
#include "visitor.h"
void compile(CodeGenVisitor v);
2 changes: 2 additions & 0 deletions visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ class CodeGenVisitor : public DeclVisitor {
: scope(scope), l(l), terminate(false){};
CodeGenVisitor wrap();
CodeGenVisitor wrapWithTrace(Trace* r);
auto getMod() { return l.mod; }
auto getCtx() { return l.ctx; }
virtual void visit(Declaration* d) override;

virtual void visit(ExprStmt* st) override;
Expand Down

0 comments on commit 05d4aa0

Please sign in to comment.