2004-08-13 20:25:54 +00:00
|
|
|
//===- CompilerDriver.cpp - The LLVM Compiler Driver ------------*- C++ -*-===//
|
2004-08-13 20:22:43 +00:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Reid Spencer and is distributed under the
|
|
|
|
// University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2004-08-13 20:25:54 +00:00
|
|
|
// This file implements the bulk of the LLVM Compiler Driver (llvmc).
|
2004-08-13 20:22:43 +00:00
|
|
|
//
|
|
|
|
//===------------------------------------------------------------------------===
|
|
|
|
|
|
|
|
#include "CompilerDriver.h"
|
2004-08-15 08:19:46 +00:00
|
|
|
#include "ConfigLexer.h"
|
2004-08-24 13:55:17 +00:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "Support/FileUtilities.h"
|
2004-08-19 04:49:47 +00:00
|
|
|
#include "Support/SystemUtils.h"
|
2004-08-24 13:55:17 +00:00
|
|
|
#include "Support/StringExtras.h"
|
2004-08-13 20:22:43 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
inline std::string RemoveSuffix(const std::string& fullName) {
|
|
|
|
size_t dotpos = fullName.rfind('.',fullName.size());
|
|
|
|
if ( dotpos == std::string::npos ) return fullName;
|
|
|
|
return fullName.substr(0, dotpos);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char OutputSuffix[] = ".o";
|
|
|
|
|
2004-08-19 04:49:47 +00:00
|
|
|
void WriteAction(CompilerDriver::Action* action ) {
|
|
|
|
std::cerr << action->program;
|
|
|
|
std::vector<std::string>::iterator I = action->args.begin();
|
|
|
|
while (I != action->args.end()) {
|
2004-08-14 09:37:15 +00:00
|
|
|
std::cerr << " " + *I;
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
std::cerr << "\n";
|
|
|
|
}
|
|
|
|
|
2004-08-19 04:49:47 +00:00
|
|
|
void DumpAction(CompilerDriver::Action* action) {
|
|
|
|
std::cerr << "command = " << action->program;
|
|
|
|
std::vector<std::string>::iterator I = action->args.begin();
|
|
|
|
while (I != action->args.end()) {
|
2004-08-15 08:19:46 +00:00
|
|
|
std::cerr << " " + *I;
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
std::cerr << "\n";
|
2004-08-19 04:49:47 +00:00
|
|
|
std::cerr << "flags = " << action->flags << "\n";
|
2004-08-15 08:19:46 +00:00
|
|
|
}
|
|
|
|
|
2004-08-14 09:37:15 +00:00
|
|
|
void DumpConfigData(CompilerDriver::ConfigData* cd, const std::string& type ){
|
|
|
|
std::cerr << "Configuration Data For '" << cd->langName << "' (" << type
|
|
|
|
<< ")\n";
|
|
|
|
std::cerr << "PreProcessor: ";
|
2004-08-15 08:19:46 +00:00
|
|
|
DumpAction(&cd->PreProcessor);
|
2004-08-14 09:37:15 +00:00
|
|
|
std::cerr << "Translator: ";
|
2004-08-15 08:19:46 +00:00
|
|
|
DumpAction(&cd->Translator);
|
2004-08-14 09:37:15 +00:00
|
|
|
std::cerr << "Optimizer: ";
|
2004-08-15 08:19:46 +00:00
|
|
|
DumpAction(&cd->Optimizer);
|
2004-08-14 09:37:15 +00:00
|
|
|
std::cerr << "Assembler: ";
|
2004-08-15 08:19:46 +00:00
|
|
|
DumpAction(&cd->Assembler);
|
2004-08-14 09:37:15 +00:00
|
|
|
std::cerr << "Linker: ";
|
2004-08-15 08:19:46 +00:00
|
|
|
DumpAction(&cd->Linker);
|
2004-08-14 09:37:15 +00:00
|
|
|
}
|
2004-08-13 20:22:43 +00:00
|
|
|
|
2004-08-16 07:06:38 +00:00
|
|
|
/// This specifies the passes to run for OPT_FAST_COMPILE (-O1)
|
|
|
|
/// which should reduce the volume of code and make compilation
|
|
|
|
/// faster. This is also safe on any llvm module.
|
2004-08-19 04:49:47 +00:00
|
|
|
static const char* DefaultFastCompileOptimizations[] = {
|
|
|
|
"-simplifycfg", "-mem2reg", "-instcombine"
|
2004-08-16 07:06:38 +00:00
|
|
|
};
|
|
|
|
}
|
2004-08-13 20:22:43 +00:00
|
|
|
|
2004-08-19 04:49:47 +00:00
|
|
|
// Stuff in this namespace properly belongs in lib/System and needs
|
|
|
|
// to be portable but we're avoiding that for now.
|
|
|
|
namespace sys {
|
2004-08-20 09:14:05 +00:00
|
|
|
|
2004-08-24 13:55:17 +00:00
|
|
|
bool FileIsReadable(const std::string& fname) {
|
2004-08-20 09:14:05 +00:00
|
|
|
return 0 == access(fname.c_str(), F_OK | R_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CleanupTempFile(const std::string& fname) {
|
2004-08-24 13:55:17 +00:00
|
|
|
if (FileIsReadable(fname))
|
2004-08-20 09:14:05 +00:00
|
|
|
unlink(fname.c_str());
|
|
|
|
}
|
|
|
|
|
2004-08-19 04:49:47 +00:00
|
|
|
std::string MakeTemporaryDirectory() {
|
|
|
|
char temp_name[64];
|
|
|
|
strcpy(temp_name,"/tmp/llvm_XXXXXX");
|
|
|
|
if (0 == mkdtemp(temp_name))
|
|
|
|
throw std::string("Can't create temporary directory");
|
|
|
|
return temp_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string FindExecutableInPath(const std::string& program) {
|
|
|
|
// First, just see if the program is already executable
|
|
|
|
if (isExecutableFile(program)) return program;
|
|
|
|
|
|
|
|
// Get the path. If its empty, we can't do anything
|
|
|
|
const char *PathStr = getenv("PATH");
|
|
|
|
if (PathStr == 0) return "";
|
|
|
|
|
|
|
|
// Now we have a colon separated list of directories to search; try them.
|
|
|
|
unsigned PathLen = strlen(PathStr);
|
|
|
|
while (PathLen) {
|
|
|
|
// Find the first colon...
|
|
|
|
const char *Colon = std::find(PathStr, PathStr+PathLen, ':');
|
|
|
|
|
|
|
|
// Check to see if this first directory contains the executable...
|
|
|
|
std::string FilePath = std::string(PathStr, Colon) + '/' + program;
|
|
|
|
if (isExecutableFile(FilePath))
|
|
|
|
return FilePath; // Found the executable!
|
|
|
|
|
|
|
|
// Nope it wasn't in this directory, check the next range!
|
|
|
|
PathLen -= Colon-PathStr;
|
|
|
|
PathStr = Colon;
|
|
|
|
|
|
|
|
// Advance past duplicate coons
|
|
|
|
while (*PathStr == ':') {
|
|
|
|
PathStr++;
|
|
|
|
PathLen--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we fell out, we ran out of directories in PATH to search, return failure
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-13 20:22:43 +00:00
|
|
|
CompilerDriver::CompilerDriver(ConfigDataProvider& confDatProv )
|
|
|
|
: cdp(&confDatProv)
|
|
|
|
, finalPhase(LINKING)
|
|
|
|
, optLevel(OPT_FAST_COMPILE)
|
|
|
|
, isDryRun(false)
|
|
|
|
, isVerbose(false)
|
|
|
|
, isDebug(false)
|
|
|
|
, timeActions(false)
|
|
|
|
, emitRawCode(false)
|
|
|
|
, emitNativeCode(false)
|
2004-08-19 04:49:47 +00:00
|
|
|
, keepTemps(false)
|
2004-08-13 20:22:43 +00:00
|
|
|
, machine()
|
2004-08-15 08:19:46 +00:00
|
|
|
, LibraryPaths()
|
2004-08-19 04:49:47 +00:00
|
|
|
, AdditionalArgs()
|
|
|
|
, TempDir()
|
2004-08-13 20:22:43 +00:00
|
|
|
{
|
|
|
|
// FIXME: These libraries are platform specific
|
2004-08-15 08:19:46 +00:00
|
|
|
LibraryPaths.push_back("/lib");
|
|
|
|
LibraryPaths.push_back("/usr/lib");
|
2004-08-19 04:49:47 +00:00
|
|
|
AdditionalArgs.reserve(NUM_PHASES);
|
|
|
|
StringVector emptyVec;
|
|
|
|
for (unsigned i = 0; i < NUM_PHASES; ++i)
|
|
|
|
AdditionalArgs.push_back(emptyVec);
|
2004-08-13 20:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CompilerDriver::~CompilerDriver() {
|
|
|
|
cdp = 0;
|
2004-08-15 08:19:46 +00:00
|
|
|
LibraryPaths.clear();
|
2004-08-19 04:49:47 +00:00
|
|
|
AdditionalArgs.clear();
|
2004-08-13 20:22:43 +00:00
|
|
|
}
|
|
|
|
|
2004-08-19 04:49:47 +00:00
|
|
|
CompilerDriver::ConfigData::ConfigData()
|
|
|
|
: langName()
|
|
|
|
, PreProcessor()
|
|
|
|
, Translator()
|
|
|
|
, Optimizer()
|
|
|
|
, Assembler()
|
|
|
|
, Linker()
|
|
|
|
{
|
|
|
|
StringVector emptyVec;
|
|
|
|
for (unsigned i = 0; i < NUM_PHASES; ++i)
|
|
|
|
opts.push_back(emptyVec);
|
2004-08-13 20:22:43 +00:00
|
|
|
}
|
|
|
|
|
2004-08-19 04:49:47 +00:00
|
|
|
void CompilerDriver::error( const std::string& errmsg ) {
|
|
|
|
std::cerr << "llvmc: Error: " << errmsg << ".\n";
|
|
|
|
exit(1);
|
2004-08-15 08:19:46 +00:00
|
|
|
}
|
|
|
|
|
2004-08-13 20:22:43 +00:00
|
|
|
CompilerDriver::Action* CompilerDriver::GetAction(ConfigData* cd,
|
|
|
|
const std::string& input,
|
|
|
|
const std::string& output,
|
|
|
|
Phases phase)
|
|
|
|
{
|
2004-08-19 04:49:47 +00:00
|
|
|
Action* pat = 0; ///< The pattern/template for the action
|
|
|
|
Action* action = new Action; ///< The actual action to execute
|
|
|
|
|
2004-08-15 08:19:46 +00:00
|
|
|
// Get the action pattern
|
2004-08-13 20:22:43 +00:00
|
|
|
switch (phase) {
|
|
|
|
case PREPROCESSING: pat = &cd->PreProcessor; break;
|
|
|
|
case TRANSLATION: pat = &cd->Translator; break;
|
|
|
|
case OPTIMIZATION: pat = &cd->Optimizer; break;
|
|
|
|
case ASSEMBLY: pat = &cd->Assembler; break;
|
|
|
|
case LINKING: pat = &cd->Linker; break;
|
|
|
|
default:
|
|
|
|
assert(!"Invalid driver phase!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
assert(pat != 0 && "Invalid command pattern");
|
2004-08-15 08:19:46 +00:00
|
|
|
|
2004-08-19 04:49:47 +00:00
|
|
|
// Copy over some pattern things that don't need to change
|
|
|
|
action->program = pat->program;
|
|
|
|
action->flags = pat->flags;
|
|
|
|
|
|
|
|
// Do the substitutions from the pattern to the actual
|
|
|
|
StringVector::iterator PI = pat->args.begin();
|
|
|
|
StringVector::iterator PE = pat->args.end();
|
|
|
|
while (PI != PE) {
|
2004-08-20 22:53:11 +00:00
|
|
|
if ((*PI)[0] == '%') {
|
|
|
|
if (*PI == "%in%") {
|
2004-08-19 04:49:47 +00:00
|
|
|
action->args.push_back(input);
|
2004-08-20 22:53:11 +00:00
|
|
|
} else if (*PI == "%out%") {
|
2004-08-19 04:49:47 +00:00
|
|
|
action->args.push_back(output);
|
2004-08-20 22:53:11 +00:00
|
|
|
} else if (*PI == "%time%") {
|
2004-08-19 04:49:47 +00:00
|
|
|
if (timePasses)
|
|
|
|
action->args.push_back("-time-passes");
|
2004-08-20 22:53:11 +00:00
|
|
|
} else if (*PI == "%stats%") {
|
2004-08-19 04:49:47 +00:00
|
|
|
if (showStats)
|
|
|
|
action->args.push_back("-stats");
|
2004-08-20 22:53:11 +00:00
|
|
|
} else if (*PI == "%target%") {
|
2004-08-19 04:49:47 +00:00
|
|
|
// FIXME: Ignore for now
|
2004-08-20 22:53:11 +00:00
|
|
|
} else if (*PI == "%opt%") {
|
2004-08-19 04:49:47 +00:00
|
|
|
if (!emitRawCode) {
|
2004-08-24 13:55:17 +00:00
|
|
|
if (cd->opts.size() > static_cast<unsigned>(optLevel) &&
|
|
|
|
!cd->opts[optLevel].empty())
|
|
|
|
action->args.insert(action->args.end(), cd->opts[optLevel].begin(),
|
2004-08-19 04:49:47 +00:00
|
|
|
cd->opts[optLevel].end());
|
2004-08-24 13:55:17 +00:00
|
|
|
else
|
|
|
|
error("Optimization options for level " + utostr(unsigned(optLevel)) +
|
|
|
|
" were not specified");
|
2004-08-19 04:49:47 +00:00
|
|
|
}
|
2004-08-24 13:55:17 +00:00
|
|
|
} else if (*PI == "%args%") {
|
|
|
|
if (AdditionalArgs.size() > unsigned(phase))
|
|
|
|
if (!AdditionalArgs[phase].empty()) {
|
|
|
|
// Get specific options for each kind of action type
|
|
|
|
StringVector& addargs = AdditionalArgs[phase];
|
|
|
|
// Add specific options for each kind of action type
|
|
|
|
action->args.insert(action->args.end(), addargs.begin(), addargs.end());
|
|
|
|
}
|
2004-08-19 04:49:47 +00:00
|
|
|
} else {
|
2004-08-24 13:55:17 +00:00
|
|
|
error("Invalid substitution name" + *PI);
|
2004-08-19 04:49:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Its not a substitution, just put it in the action
|
|
|
|
action->args.push_back(*PI);
|
|
|
|
}
|
|
|
|
PI++;
|
|
|
|
}
|
2004-08-15 08:19:46 +00:00
|
|
|
|
2004-08-19 04:49:47 +00:00
|
|
|
|
|
|
|
// Finally, we're done
|
|
|
|
return action;
|
2004-08-13 20:22:43 +00:00
|
|
|
}
|
|
|
|
|
2004-08-19 04:49:47 +00:00
|
|
|
bool CompilerDriver::DoAction(Action*action) {
|
|
|
|
assert(action != 0 && "Invalid Action!");
|
2004-08-13 20:22:43 +00:00
|
|
|
if (isVerbose)
|
2004-08-19 04:49:47 +00:00
|
|
|
WriteAction(action);
|
2004-08-13 20:22:43 +00:00
|
|
|
if (!isDryRun) {
|
2004-08-19 04:49:47 +00:00
|
|
|
std::string prog(sys::FindExecutableInPath(action->program));
|
|
|
|
if (prog.empty())
|
|
|
|
error("Can't find program '" + action->program + "'");
|
|
|
|
|
|
|
|
// Get the program's arguments
|
|
|
|
const char* argv[action->args.size() + 1];
|
|
|
|
argv[0] = prog.c_str();
|
|
|
|
unsigned i = 1;
|
|
|
|
for (; i <= action->args.size(); ++i)
|
|
|
|
argv[i] = action->args[i-1].c_str();
|
|
|
|
argv[i] = 0;
|
|
|
|
|
|
|
|
// Invoke the program
|
|
|
|
return !ExecWait(argv, environ);
|
2004-08-13 20:22:43 +00:00
|
|
|
}
|
2004-08-19 04:49:47 +00:00
|
|
|
return true;
|
2004-08-13 20:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int CompilerDriver::execute(const InputList& InpList,
|
2004-08-16 07:06:38 +00:00
|
|
|
const std::string& Output ) {
|
2004-08-13 20:22:43 +00:00
|
|
|
// Echo the configuration of options if we're running verbose
|
|
|
|
if (isDebug)
|
|
|
|
{
|
|
|
|
std::cerr << "Compiler Driver Options:\n";
|
|
|
|
std::cerr << "DryRun = " << isDryRun << "\n";
|
|
|
|
std::cerr << "Verbose = " << isVerbose << " \n";
|
|
|
|
std::cerr << "TimeActions = " << timeActions << "\n";
|
|
|
|
std::cerr << "EmitRawCode = " << emitRawCode << "\n";
|
|
|
|
std::cerr << "OutputMachine = " << machine << "\n";
|
|
|
|
std::cerr << "EmitNativeCode = " << emitNativeCode << "\n";
|
|
|
|
InputList::const_iterator I = InpList.begin();
|
|
|
|
while ( I != InpList.end() ) {
|
|
|
|
std::cerr << "Input: " << I->first << "(" << I->second << ")\n";
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
std::cerr << "Output: " << Output << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's no input, we're done.
|
|
|
|
if (InpList.empty())
|
|
|
|
error("Nothing to compile.");
|
|
|
|
|
|
|
|
// If they are asking for linking and didn't provide an output
|
|
|
|
// file then its an error (no way for us to "make up" a meaningful
|
|
|
|
// file name based on the various linker input files).
|
|
|
|
if (finalPhase == LINKING && Output.empty())
|
|
|
|
error("An output file name must be specified for linker output");
|
|
|
|
|
2004-08-15 08:19:46 +00:00
|
|
|
// This vector holds all the resulting actions of the following loop.
|
2004-08-13 20:22:43 +00:00
|
|
|
std::vector<Action*> actions;
|
|
|
|
|
2004-08-15 08:19:46 +00:00
|
|
|
// Create a temporary directory for our temporary files
|
2004-08-19 04:49:47 +00:00
|
|
|
std::string TempDir(sys::MakeTemporaryDirectory());
|
|
|
|
std::string TempPreprocessorOut(TempDir + "/preproc.o");
|
|
|
|
std::string TempTranslatorOut(TempDir + "/trans.o");
|
|
|
|
std::string TempOptimizerOut(TempDir + "/opt.o");
|
|
|
|
std::string TempAssemblerOut(TempDir + "/asm.o");
|
2004-08-15 08:19:46 +00:00
|
|
|
|
2004-08-13 20:22:43 +00:00
|
|
|
/// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases
|
|
|
|
// for each input item
|
|
|
|
std::vector<std::string> LinkageItems;
|
2004-08-24 13:55:17 +00:00
|
|
|
std::string OutFile(Output);
|
2004-08-13 20:22:43 +00:00
|
|
|
InputList::const_iterator I = InpList.begin();
|
|
|
|
while ( I != InpList.end() ) {
|
|
|
|
// Get the suffix of the file name
|
2004-08-24 13:55:17 +00:00
|
|
|
const std::string& ftype = I->second;
|
2004-08-13 20:22:43 +00:00
|
|
|
|
|
|
|
// If its a library, bytecode file, or object file, save
|
|
|
|
// it for linking below and short circuit the
|
|
|
|
// pre-processing/translation/assembly phases
|
2004-08-24 13:55:17 +00:00
|
|
|
if (ftype.empty() || ftype == "o" || ftype == "bc") {
|
2004-08-13 20:22:43 +00:00
|
|
|
// We shouldn't get any of these types of files unless we're
|
|
|
|
// later going to link. Enforce this limit now.
|
|
|
|
if (finalPhase != LINKING) {
|
|
|
|
error("Pre-compiled objects found but linking not requested");
|
|
|
|
}
|
|
|
|
LinkageItems.push_back(I->first);
|
2004-08-24 13:55:17 +00:00
|
|
|
++I; continue; // short circuit remainder of loop
|
2004-08-13 20:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, we know its something we need to translate
|
|
|
|
// and/or optimize. See if we can get the configuration data
|
|
|
|
// for this kind of file.
|
|
|
|
ConfigData* cd = cdp->ProvideConfigData(I->second);
|
|
|
|
if (cd == 0)
|
|
|
|
error(std::string("Files of type '") + I->second +
|
|
|
|
"' are not recognized." );
|
2004-08-14 09:37:15 +00:00
|
|
|
if (isDebug)
|
|
|
|
DumpConfigData(cd,I->second);
|
2004-08-13 20:22:43 +00:00
|
|
|
|
2004-08-19 04:49:47 +00:00
|
|
|
// Initialize the input file
|
|
|
|
std::string InFile(I->first);
|
|
|
|
|
2004-08-15 08:19:46 +00:00
|
|
|
// PRE-PROCESSING PHASE
|
2004-08-19 04:49:47 +00:00
|
|
|
Action& action = cd->PreProcessor;
|
2004-08-15 08:19:46 +00:00
|
|
|
|
|
|
|
// Get the preprocessing action, if needed, or error if appropriate
|
2004-08-19 04:49:47 +00:00
|
|
|
if (!action.program.empty()) {
|
|
|
|
if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) {
|
|
|
|
if (finalPhase == PREPROCESSING)
|
|
|
|
actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING));
|
|
|
|
else {
|
|
|
|
actions.push_back(GetAction(cd,InFile,TempPreprocessorOut,
|
|
|
|
PREPROCESSING));
|
|
|
|
InFile = TempPreprocessorOut;
|
|
|
|
}
|
2004-08-13 20:22:43 +00:00
|
|
|
}
|
2004-08-15 08:19:46 +00:00
|
|
|
} else if (finalPhase == PREPROCESSING) {
|
|
|
|
error(cd->langName + " does not support pre-processing");
|
2004-08-19 04:49:47 +00:00
|
|
|
} else if (action.isSet(REQUIRED_FLAG)) {
|
2004-08-15 08:19:46 +00:00
|
|
|
error(std::string("Don't know how to pre-process ") +
|
|
|
|
cd->langName + " files");
|
2004-08-13 20:22:43 +00:00
|
|
|
}
|
2004-08-19 04:49:47 +00:00
|
|
|
|
2004-08-13 20:22:43 +00:00
|
|
|
// Short-circuit remaining actions if all they want is pre-processing
|
|
|
|
if (finalPhase == PREPROCESSING) { ++I; continue; };
|
|
|
|
|
|
|
|
/// TRANSLATION PHASE
|
2004-08-19 04:49:47 +00:00
|
|
|
action = cd->Translator;
|
2004-08-15 08:19:46 +00:00
|
|
|
|
|
|
|
// Get the translation action, if needed, or error if appropriate
|
2004-08-19 04:49:47 +00:00
|
|
|
if (!action.program.empty()) {
|
|
|
|
if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) {
|
|
|
|
if (finalPhase == TRANSLATION)
|
|
|
|
actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION));
|
|
|
|
else {
|
|
|
|
actions.push_back(GetAction(cd,InFile,TempTranslatorOut,TRANSLATION));
|
|
|
|
InFile = TempTranslatorOut;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ll -> bc Helper
|
|
|
|
if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
|
|
|
|
/// The output of the translator is an LLVM Assembly program
|
|
|
|
/// We need to translate it to bytecode
|
|
|
|
Action* action = new Action();
|
|
|
|
action->program = "llvm-as";
|
|
|
|
action->args.push_back(InFile);
|
|
|
|
action->args.push_back("-o");
|
|
|
|
InFile += ".bc";
|
|
|
|
action->args.push_back(InFile);
|
|
|
|
actions.push_back(action);
|
|
|
|
}
|
2004-08-15 08:19:46 +00:00
|
|
|
}
|
|
|
|
} else if (finalPhase == TRANSLATION) {
|
|
|
|
error(cd->langName + " does not support translation");
|
2004-08-19 04:49:47 +00:00
|
|
|
} else if (action.isSet(REQUIRED_FLAG)) {
|
2004-08-15 08:19:46 +00:00
|
|
|
error(std::string("Don't know how to translate ") +
|
|
|
|
cd->langName + " files");
|
|
|
|
}
|
2004-08-19 04:49:47 +00:00
|
|
|
|
2004-08-13 20:22:43 +00:00
|
|
|
// Short-circuit remaining actions if all they want is translation
|
|
|
|
if (finalPhase == TRANSLATION) { ++I; continue; }
|
|
|
|
|
|
|
|
/// OPTIMIZATION PHASE
|
2004-08-19 04:49:47 +00:00
|
|
|
action = cd->Optimizer;
|
2004-08-15 08:19:46 +00:00
|
|
|
|
|
|
|
// Get the optimization action, if needed, or error if appropriate
|
2004-08-24 13:55:17 +00:00
|
|
|
if (!emitRawCode) {
|
|
|
|
if (!action.program.empty()) {
|
|
|
|
if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) {
|
|
|
|
if (finalPhase == OPTIMIZATION)
|
|
|
|
actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION));
|
|
|
|
else {
|
|
|
|
actions.push_back(GetAction(cd,InFile,TempOptimizerOut,OPTIMIZATION));
|
|
|
|
InFile = TempOptimizerOut;
|
|
|
|
}
|
|
|
|
// ll -> bc Helper
|
|
|
|
if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
|
|
|
|
/// The output of the translator is an LLVM Assembly program
|
|
|
|
/// We need to translate it to bytecode
|
|
|
|
Action* action = new Action();
|
|
|
|
action->program = "llvm-as";
|
|
|
|
action->args.push_back(InFile);
|
|
|
|
action->args.push_back("-f");
|
|
|
|
action->args.push_back("-o");
|
|
|
|
InFile += ".bc";
|
|
|
|
action->args.push_back(InFile);
|
|
|
|
actions.push_back(action);
|
|
|
|
}
|
2004-08-19 04:49:47 +00:00
|
|
|
}
|
2004-08-24 13:55:17 +00:00
|
|
|
} else if (finalPhase == OPTIMIZATION) {
|
|
|
|
error(cd->langName + " does not support optimization");
|
|
|
|
} else if (action.isSet(REQUIRED_FLAG)) {
|
|
|
|
error(std::string("Don't know how to optimize ") +
|
2004-08-15 08:19:46 +00:00
|
|
|
cd->langName + " files");
|
2004-08-24 13:55:17 +00:00
|
|
|
}
|
2004-08-15 08:19:46 +00:00
|
|
|
}
|
2004-08-19 04:49:47 +00:00
|
|
|
|
2004-08-13 20:22:43 +00:00
|
|
|
// Short-circuit remaining actions if all they want is optimization
|
|
|
|
if (finalPhase == OPTIMIZATION) { ++I; continue; }
|
|
|
|
|
2004-08-19 04:49:47 +00:00
|
|
|
/// ASSEMBLY PHASE
|
2004-08-20 09:14:05 +00:00
|
|
|
action = cd->Assembler;
|
|
|
|
|
2004-08-24 13:55:17 +00:00
|
|
|
if (finalPhase == ASSEMBLY || emitNativeCode) {
|
2004-08-20 09:14:05 +00:00
|
|
|
if (emitNativeCode) {
|
|
|
|
if (action.program.empty()) {
|
|
|
|
error(std::string("Native Assembler not specified for ") +
|
|
|
|
cd->langName + " files");
|
2004-08-24 13:55:17 +00:00
|
|
|
} else if (finalPhase == ASSEMBLY) {
|
2004-08-20 09:14:05 +00:00
|
|
|
actions.push_back(GetAction(cd,InFile,OutFile,ASSEMBLY));
|
2004-08-24 13:55:17 +00:00
|
|
|
} else {
|
|
|
|
actions.push_back(GetAction(cd,InFile,TempAssemblerOut,ASSEMBLY));
|
|
|
|
InFile = TempAssemblerOut;
|
2004-08-20 09:14:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Just convert back to llvm assembly with llvm-dis
|
|
|
|
Action* action = new Action();
|
|
|
|
action->program = "llvm-dis";
|
|
|
|
action->args.push_back(InFile);
|
|
|
|
action->args.push_back("-f");
|
|
|
|
action->args.push_back("-o");
|
|
|
|
action->args.push_back(OutFile);
|
|
|
|
actions.push_back(action);
|
|
|
|
}
|
2004-08-19 04:49:47 +00:00
|
|
|
}
|
2004-08-24 13:55:17 +00:00
|
|
|
|
|
|
|
// Short-circuit remaining actions if all they want is assembly output
|
|
|
|
if (finalPhase == ASSEMBLY) { ++I; continue; }
|
2004-08-19 04:49:47 +00:00
|
|
|
|
2004-08-24 13:55:17 +00:00
|
|
|
// Register the OutFile as a link candidate
|
|
|
|
LinkageItems.push_back(InFile);
|
|
|
|
|
2004-08-19 04:49:47 +00:00
|
|
|
// Go to next file to be processed
|
2004-08-13 20:22:43 +00:00
|
|
|
++I;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// LINKING PHASE
|
2004-08-20 09:14:05 +00:00
|
|
|
if (finalPhase == LINKING) {
|
|
|
|
if (emitNativeCode) {
|
2004-08-24 13:55:17 +00:00
|
|
|
error("llvmc doesn't know how to link native code yet");
|
2004-08-20 09:14:05 +00:00
|
|
|
} else {
|
2004-08-24 13:55:17 +00:00
|
|
|
// First, we need to examine the files to ensure that they all contain
|
|
|
|
// bytecode files. Since the final output is bytecode, we can only
|
|
|
|
// link bytecode.
|
|
|
|
StringVector::const_iterator I = LinkageItems.begin();
|
|
|
|
StringVector::const_iterator E = LinkageItems.end();
|
|
|
|
StringVector lib_list;
|
|
|
|
while (I != E ) {
|
|
|
|
if (sys::FileIsReadable(*I)) {
|
|
|
|
if (CheckMagic(*I, "llvm")) {
|
|
|
|
// Examine the bytecode file for additional bytecode libraries to
|
|
|
|
// link in.
|
|
|
|
ModuleProvider* mp = getBytecodeModuleProvider(*I);
|
|
|
|
assert(mp!=0 && "Couldn't get module provider from bytecode file");
|
|
|
|
Module* M = mp->releaseModule();
|
|
|
|
assert(M!=0 && "Couldn't get module from module provider");
|
|
|
|
Module::lib_iterator LI = M->lib_begin();
|
|
|
|
Module::lib_iterator LE = M->lib_end();
|
|
|
|
while ( LI != LE ) {
|
|
|
|
if (sys::FileIsReadable(*LI)) {
|
|
|
|
if (CheckMagic(*I,"llvm")) {
|
|
|
|
lib_list.push_back(*LI);
|
|
|
|
} else {
|
|
|
|
error(std::string("Library '") + *LI + "' required by file '"
|
|
|
|
+ *I + "' is not an LLVM bytecode file");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
error(std::string("Library '") + *LI + "' required by file '"
|
|
|
|
+ *I + "' is not readable");
|
|
|
|
}
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
error(std::string("File '") + *I + " is not an LLVM byte code file");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
error(std::string("File '") + *I + " is not readable");
|
|
|
|
}
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're emitting bytecode so let's build an llvm-link Action
|
|
|
|
Action* link = new Action();
|
|
|
|
link->program = "llvm-link";
|
|
|
|
link->args = LinkageItems;
|
|
|
|
link->args.insert(link->args.end(), lib_list.begin(), lib_list.end());
|
|
|
|
link->args.push_back("-f");
|
|
|
|
link->args.push_back("-o");
|
|
|
|
link->args.push_back(OutFile);
|
|
|
|
if (timePasses)
|
|
|
|
link->args.push_back("-time-passes");
|
|
|
|
if (showStats)
|
|
|
|
link->args.push_back("-stats");
|
|
|
|
actions.push_back(link);
|
2004-08-20 09:14:05 +00:00
|
|
|
}
|
2004-08-19 04:49:47 +00:00
|
|
|
}
|
2004-08-13 20:22:43 +00:00
|
|
|
|
|
|
|
/// RUN THE ACTIONS
|
|
|
|
std::vector<Action*>::iterator aIter = actions.begin();
|
|
|
|
while (aIter != actions.end()) {
|
2004-08-19 04:49:47 +00:00
|
|
|
if (!DoAction(*aIter))
|
|
|
|
error("Action failed");
|
2004-08-13 20:22:43 +00:00
|
|
|
aIter++;
|
|
|
|
}
|
|
|
|
|
2004-08-19 04:49:47 +00:00
|
|
|
if (!keepTemps) {
|
|
|
|
// Cleanup files
|
2004-08-20 09:14:05 +00:00
|
|
|
::sys::CleanupTempFile(TempPreprocessorOut);
|
|
|
|
::sys::CleanupTempFile(TempTranslatorOut);
|
|
|
|
::sys::CleanupTempFile(TempOptimizerOut);
|
2004-08-16 07:06:38 +00:00
|
|
|
|
2004-08-19 04:49:47 +00:00
|
|
|
// Cleanup temporary directory we created
|
2004-08-24 13:55:17 +00:00
|
|
|
if (::sys::FileIsReadable(TempDir))
|
2004-08-19 04:49:47 +00:00
|
|
|
rmdir(TempDir.c_str());
|
|
|
|
}
|
2004-08-15 08:19:46 +00:00
|
|
|
|
2004-08-13 20:22:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab
|