Skip to content

Commit

Permalink
Pass the executable path into the compiler so that it can find runtim…
Browse files Browse the repository at this point in the history
…e.bc even when not run from the current directory.
  • Loading branch information
davidchisnall committed Aug 24, 2016
1 parent 3d62f73 commit ebe7dc9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
7 changes: 5 additions & 2 deletions ast.hh
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ namespace Compiler
int16_t height);
/**
* Compile the AST. The optimisation level indicates how aggressive
* optimisation should be. Zero indicates no optimisation.
* optimisation should be. Zero indicates no optimisation. The `path`
* argument tells the compiler where to look for the `runtime.bc` file.
*/
automaton compile(AST::StatementList *ast, int optimiseLevel);
automaton compile(AST::StatementList *ast,
int optimiseLevel,
const std::string &path);
}
namespace llvm
{
Expand Down
19 changes: 14 additions & 5 deletions compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,22 @@ struct State
* file and prepares the module, including setting up all of the LLVM state
* required.
*/
State() : B(C)
State(const std::string &path) : B(C)
{
std::string bcpath;
if (path.size() == 0)
{
bcpath = "runtime.bc";
}
else
{
bcpath = path + "/runtime.bc";
}
// Load the bitcode for the runtime helper code
auto buffer = MemoryBuffer::getFile("runtime.bc");
auto buffer = MemoryBuffer::getFile(bcpath);
if (std::error_code ec = buffer.getError())
{
std::cerr << "Failed to open runtime.bc: " << ec.message() << std::endl;
std::cerr << "Failed to open " << bcpath << ": " << ec.message() << std::endl;
exit(EXIT_FAILURE);
}
auto e = parseBitcodeFile(buffer.get()->getMemBufferRef(), C);
Expand Down Expand Up @@ -203,15 +212,15 @@ struct State

};

automaton compile(AST::StatementList *ast, int optimiseLevel)
automaton compile(AST::StatementList *ast, int optimiseLevel, const std::string &path)
{
// These functions do nothing, they just ensure that the correct modules are
// not removed by the linker.
InitializeNativeTarget();
InitializeNativeTargetAsmPrinter();
LLVMLinkInMCJIT();

State s;
State s(path);
ast->compile(s);
// And then return the compiled version.
return s.getAutomaton(optimiseLevel);
Expand Down
4 changes: 3 additions & 1 deletion main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <libgen.h>
#include <time.h>
#include <unistd.h>
#include "parser.hh"
Expand All @@ -43,6 +44,7 @@ static void logTimeSince(clock_t c1, const char *msg)

int main(int argc, char **argv)
{
std::string path = dirname(argv[0]);
int iterations = 1;
int useJIT = 0;
int optimiseLevel = 0;
Expand Down Expand Up @@ -141,7 +143,7 @@ int main(int argc, char **argv)
if (useJIT)
{
c1 = clock();
Compiler::automaton ca = Compiler::compile(ast.get(), optimiseLevel);
Compiler::automaton ca = Compiler::compile(ast.get(), optimiseLevel, path);
logTimeSince(c1, "Compiling");
c1 = clock();
for (int i=0 ; i<iterations ; i++)
Expand Down

0 comments on commit ebe7dc9

Please sign in to comment.