-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#pragma once | ||
#include "visitor.h" | ||
void compile(CodeGenVisitor v); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters