2008-05-30 06:20:54 +00:00
|
|
|
//===- LLVMCConfigurationEmitter.cpp - Generate LLVMC config ----*- C++ -*-===//
|
2008-03-23 08:57:20 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open
|
|
|
|
// Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-05-06 18:08:59 +00:00
|
|
|
// This tablegen backend is responsible for emitting LLVMC configuration code.
|
2008-03-23 08:57:20 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-06 18:09:29 +00:00
|
|
|
#include "LLVMCConfigurationEmitter.h"
|
2008-03-23 08:57:20 +00:00
|
|
|
#include "Record.h"
|
|
|
|
|
|
|
|
#include "llvm/ADT/IntrusiveRefCntPtr.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
2008-05-30 06:18:16 +00:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2008-03-23 08:57:20 +00:00
|
|
|
#include "llvm/Support/Streams.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <functional>
|
2008-05-30 06:21:48 +00:00
|
|
|
#include <stdexcept>
|
2008-03-23 08:57:20 +00:00
|
|
|
#include <string>
|
2008-06-04 04:46:14 +00:00
|
|
|
#include <typeinfo>
|
2008-03-23 08:57:20 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2008-05-06 18:12:03 +00:00
|
|
|
namespace {
|
2008-03-23 08:57:20 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// Typedefs
|
|
|
|
|
|
|
|
typedef std::vector<Record*> RecordVector;
|
|
|
|
typedef std::vector<std::string> StrVector;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// Constants
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
// Indentation strings.
|
2008-03-23 08:57:20 +00:00
|
|
|
const char * Indent1 = " ";
|
|
|
|
const char * Indent2 = " ";
|
|
|
|
const char * Indent3 = " ";
|
|
|
|
const char * Indent4 = " ";
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
// Default help string.
|
2008-03-23 08:57:20 +00:00
|
|
|
const char * DefaultHelpString = "NO HELP MESSAGE PROVIDED";
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
// Name for the "sink" option.
|
2008-03-23 08:57:20 +00:00
|
|
|
const char * SinkOptionName = "AutoGeneratedSinkOption";
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// Helper functions
|
|
|
|
|
2008-05-30 06:18:16 +00:00
|
|
|
int InitPtrToInt(const Init* ptr) {
|
|
|
|
const IntInit& val = dynamic_cast<const IntInit&>(*ptr);
|
|
|
|
return val.getValue();
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:10:19 +00:00
|
|
|
const std::string& InitPtrToString(const Init* ptr) {
|
|
|
|
const StringInit& val = dynamic_cast<const StringInit&>(*ptr);
|
2008-03-23 08:57:20 +00:00
|
|
|
return val.getValue();
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:18:16 +00:00
|
|
|
const ListInit& InitPtrToList(const Init* ptr) {
|
|
|
|
const ListInit& val = dynamic_cast<const ListInit&>(*ptr);
|
|
|
|
return val;
|
2008-05-06 18:18:20 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 06:18:16 +00:00
|
|
|
const DagInit& InitPtrToDag(const Init* ptr) {
|
2008-05-30 06:10:19 +00:00
|
|
|
const DagInit& val = dynamic_cast<const DagInit&>(*ptr);
|
2008-05-06 18:18:20 +00:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
// checkNumberOfArguments - Ensure that the number of args in d is
|
2008-05-30 06:08:50 +00:00
|
|
|
// less than or equal to min_arguments, otherwise throw an exception.
|
2008-05-06 17:22:03 +00:00
|
|
|
void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
|
|
|
|
if (d->getNumArgs() < min_arguments)
|
|
|
|
throw "Property " + d->getOperator()->getAsString()
|
|
|
|
+ " has too few arguments!";
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:08:50 +00:00
|
|
|
// isDagEmpty - is this DAG marked with an empty marker?
|
|
|
|
bool isDagEmpty (const DagInit* d) {
|
|
|
|
return d->getOperator()->getAsString() == "empty";
|
|
|
|
}
|
2008-05-06 17:22:03 +00:00
|
|
|
|
2008-03-23 08:57:20 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// Back-end specific code
|
|
|
|
|
|
|
|
// A command-line option can have one of the following types:
|
|
|
|
//
|
2008-05-30 06:22:52 +00:00
|
|
|
// Alias - an alias for another option.
|
|
|
|
//
|
|
|
|
// Switch - a simple switch without arguments, e.g. -O2
|
2008-03-23 08:57:20 +00:00
|
|
|
//
|
|
|
|
// Parameter - an option that takes one(and only one) argument, e.g. -o file,
|
|
|
|
// --output=file
|
|
|
|
//
|
|
|
|
// ParameterList - same as Parameter, but more than one occurence
|
|
|
|
// of the option is allowed, e.g. -lm -lpthread
|
|
|
|
//
|
|
|
|
// Prefix - argument is everything after the prefix,
|
|
|
|
// e.g. -Wa,-foo,-bar, -DNAME=VALUE
|
|
|
|
//
|
|
|
|
// PrefixList - same as Prefix, but more than one option occurence is
|
2008-05-30 06:22:52 +00:00
|
|
|
// allowed.
|
2008-03-23 08:57:20 +00:00
|
|
|
|
|
|
|
namespace OptionType {
|
2008-05-30 06:22:52 +00:00
|
|
|
enum OptionType { Alias, Switch,
|
|
|
|
Parameter, ParameterList, Prefix, PrefixList};
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsListOptionType (OptionType::OptionType t) {
|
|
|
|
return (t == OptionType::ParameterList || t == OptionType::PrefixList);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Code duplication here is necessary because one option can affect
|
|
|
|
// several tools and those tools may have different actions associated
|
|
|
|
// with this option. GlobalOptionDescriptions are used to generate
|
|
|
|
// the option registration code, while ToolOptionDescriptions are used
|
|
|
|
// to generate tool-specific code.
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// OptionDescription - Base class for option descriptions.
|
2008-03-23 08:57:20 +00:00
|
|
|
struct OptionDescription {
|
|
|
|
OptionType::OptionType Type;
|
|
|
|
std::string Name;
|
|
|
|
|
|
|
|
OptionDescription(OptionType::OptionType t = OptionType::Switch,
|
|
|
|
const std::string& n = "")
|
|
|
|
: Type(t), Name(n)
|
|
|
|
{}
|
|
|
|
|
|
|
|
const char* GenTypeDeclaration() const {
|
|
|
|
switch (Type) {
|
2008-05-30 06:22:52 +00:00
|
|
|
case OptionType::Alias:
|
|
|
|
return "cl::alias";
|
2008-03-23 08:57:20 +00:00
|
|
|
case OptionType::PrefixList:
|
|
|
|
case OptionType::ParameterList:
|
|
|
|
return "cl::list<std::string>";
|
|
|
|
case OptionType::Switch:
|
|
|
|
return "cl::opt<bool>";
|
|
|
|
case OptionType::Parameter:
|
|
|
|
case OptionType::Prefix:
|
|
|
|
default:
|
|
|
|
return "cl::opt<std::string>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-12 16:33:06 +00:00
|
|
|
// Escape commas and other symbols not allowed in the C++ variable
|
|
|
|
// names. Makes it possible to use options with names like "Wa,"
|
|
|
|
// (useful for prefix options).
|
|
|
|
std::string EscapeVariableName(const std::string& Var) const {
|
|
|
|
std::string ret;
|
|
|
|
for (unsigned i = 0; i != Var.size(); ++i) {
|
2008-09-22 20:46:19 +00:00
|
|
|
char cur_char = Var[i];
|
|
|
|
if (cur_char == ',') {
|
|
|
|
ret += "_comma_";
|
|
|
|
}
|
|
|
|
else if (cur_char == '+') {
|
|
|
|
ret += "_plus_";
|
|
|
|
}
|
|
|
|
else if (cur_char == ',') {
|
2008-05-12 16:33:06 +00:00
|
|
|
ret += "_comma_";
|
|
|
|
}
|
|
|
|
else {
|
2008-09-22 20:46:19 +00:00
|
|
|
ret.push_back(cur_char);
|
2008-05-12 16:33:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-03-23 08:57:20 +00:00
|
|
|
std::string GenVariableName() const {
|
2008-05-12 16:33:06 +00:00
|
|
|
const std::string& EscapedName = EscapeVariableName(Name);
|
2008-03-23 08:57:20 +00:00
|
|
|
switch (Type) {
|
2008-05-30 06:22:52 +00:00
|
|
|
case OptionType::Alias:
|
|
|
|
return "AutoGeneratedAlias" + EscapedName;
|
2008-03-23 08:57:20 +00:00
|
|
|
case OptionType::Switch:
|
2008-05-30 06:22:52 +00:00
|
|
|
return "AutoGeneratedSwitch" + EscapedName;
|
|
|
|
case OptionType::Prefix:
|
|
|
|
return "AutoGeneratedPrefix" + EscapedName;
|
|
|
|
case OptionType::PrefixList:
|
|
|
|
return "AutoGeneratedPrefixList" + EscapedName;
|
|
|
|
case OptionType::Parameter:
|
|
|
|
return "AutoGeneratedParameter" + EscapedName;
|
|
|
|
case OptionType::ParameterList:
|
|
|
|
default:
|
|
|
|
return "AutoGeneratedParameterList" + EscapedName;
|
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
// Global option description.
|
2008-03-23 08:57:20 +00:00
|
|
|
|
|
|
|
namespace GlobalOptionDescriptionFlags {
|
|
|
|
enum GlobalOptionDescriptionFlags { Required = 0x1 };
|
|
|
|
}
|
|
|
|
|
|
|
|
struct GlobalOptionDescription : public OptionDescription {
|
|
|
|
std::string Help;
|
|
|
|
unsigned Flags;
|
|
|
|
|
2008-05-09 08:27:26 +00:00
|
|
|
// We need to provide a default constructor because
|
|
|
|
// StringMap can only store DefaultConstructible objects.
|
2008-03-23 08:57:20 +00:00
|
|
|
GlobalOptionDescription() : OptionDescription(), Flags(0)
|
|
|
|
{}
|
|
|
|
|
2008-05-30 06:22:52 +00:00
|
|
|
GlobalOptionDescription (OptionType::OptionType t, const std::string& n,
|
|
|
|
const std::string& h = DefaultHelpString)
|
|
|
|
: OptionDescription(t, n), Help(h), Flags(0)
|
2008-03-23 08:57:20 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
bool isRequired() const {
|
|
|
|
return Flags & GlobalOptionDescriptionFlags::Required;
|
|
|
|
}
|
|
|
|
void setRequired() {
|
|
|
|
Flags |= GlobalOptionDescriptionFlags::Required;
|
|
|
|
}
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// Merge - Merge two option descriptions.
|
2008-03-23 08:57:20 +00:00
|
|
|
void Merge (const GlobalOptionDescription& other)
|
|
|
|
{
|
|
|
|
if (other.Type != Type)
|
|
|
|
throw "Conflicting definitions for the option " + Name + "!";
|
|
|
|
|
2008-05-06 18:13:00 +00:00
|
|
|
if (Help == DefaultHelpString)
|
2008-03-23 08:57:20 +00:00
|
|
|
Help = other.Help;
|
2008-05-06 18:13:00 +00:00
|
|
|
else if (other.Help != DefaultHelpString) {
|
|
|
|
llvm::cerr << "Warning: more than one help string defined for option "
|
2008-03-23 08:57:20 +00:00
|
|
|
+ Name + "\n";
|
2008-05-06 18:13:00 +00:00
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
|
|
|
Flags |= other.Flags;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// GlobalOptionDescriptions - A GlobalOptionDescription array
|
|
|
|
/// together with some flags affecting generation of option
|
|
|
|
/// declarations.
|
2008-03-23 08:57:20 +00:00
|
|
|
struct GlobalOptionDescriptions {
|
|
|
|
typedef StringMap<GlobalOptionDescription> container_type;
|
|
|
|
typedef container_type::const_iterator const_iterator;
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// Descriptions - A list of GlobalOptionDescriptions.
|
2008-03-23 08:57:20 +00:00
|
|
|
container_type Descriptions;
|
2008-05-07 21:50:19 +00:00
|
|
|
/// HasSink - Should the emitter generate a "cl::sink" option?
|
2008-03-23 08:57:20 +00:00
|
|
|
bool HasSink;
|
|
|
|
|
2008-05-30 06:28:37 +00:00
|
|
|
/// FindOption - exception-throwing wrapper for find().
|
2008-05-06 17:22:03 +00:00
|
|
|
const GlobalOptionDescription& FindOption(const std::string& OptName) const {
|
|
|
|
const_iterator I = Descriptions.find(OptName);
|
|
|
|
if (I != Descriptions.end())
|
|
|
|
return I->second;
|
|
|
|
else
|
|
|
|
throw OptName + ": no such option!";
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:28:37 +00:00
|
|
|
/// insertDescription - Insert new GlobalOptionDescription into
|
|
|
|
/// GlobalOptionDescriptions list
|
2008-05-30 06:27:02 +00:00
|
|
|
void insertDescription (const GlobalOptionDescription& o)
|
|
|
|
{
|
|
|
|
container_type::iterator I = Descriptions.find(o.Name);
|
|
|
|
if (I != Descriptions.end()) {
|
|
|
|
GlobalOptionDescription& D = I->second;
|
|
|
|
D.Merge(o);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Descriptions[o.Name] = o;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-23 08:57:20 +00:00
|
|
|
// Support for STL-style iteration
|
|
|
|
const_iterator begin() const { return Descriptions.begin(); }
|
|
|
|
const_iterator end() const { return Descriptions.end(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-05-09 08:27:26 +00:00
|
|
|
// Tool-local option description.
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-09 08:27:26 +00:00
|
|
|
// Properties without arguments are implemented as flags.
|
2008-03-23 08:57:20 +00:00
|
|
|
namespace ToolOptionDescriptionFlags {
|
|
|
|
enum ToolOptionDescriptionFlags { StopCompilation = 0x1,
|
|
|
|
Forward = 0x2, UnpackValues = 0x4};
|
|
|
|
}
|
|
|
|
namespace OptionPropertyType {
|
2008-09-22 20:46:19 +00:00
|
|
|
enum OptionPropertyType { AppendCmd, ForwardAs, OutputSuffix };
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef std::pair<OptionPropertyType::OptionPropertyType, std::string>
|
|
|
|
OptionProperty;
|
|
|
|
typedef SmallVector<OptionProperty, 4> OptionPropertyList;
|
|
|
|
|
|
|
|
struct ToolOptionDescription : public OptionDescription {
|
|
|
|
unsigned Flags;
|
|
|
|
OptionPropertyList Props;
|
|
|
|
|
|
|
|
// StringMap can only store DefaultConstructible objects
|
2008-03-27 09:53:47 +00:00
|
|
|
ToolOptionDescription() : OptionDescription(), Flags(0) {}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
|
|
|
ToolOptionDescription (OptionType::OptionType t, const std::string& n)
|
|
|
|
: OptionDescription(t, n)
|
|
|
|
{}
|
|
|
|
|
|
|
|
// Various boolean properties
|
|
|
|
bool isStopCompilation() const {
|
|
|
|
return Flags & ToolOptionDescriptionFlags::StopCompilation;
|
|
|
|
}
|
|
|
|
void setStopCompilation() {
|
|
|
|
Flags |= ToolOptionDescriptionFlags::StopCompilation;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isForward() const {
|
|
|
|
return Flags & ToolOptionDescriptionFlags::Forward;
|
|
|
|
}
|
|
|
|
void setForward() {
|
|
|
|
Flags |= ToolOptionDescriptionFlags::Forward;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isUnpackValues() const {
|
|
|
|
return Flags & ToolOptionDescriptionFlags::UnpackValues;
|
|
|
|
}
|
|
|
|
void setUnpackValues() {
|
|
|
|
Flags |= ToolOptionDescriptionFlags::UnpackValues;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddProperty (OptionPropertyType::OptionPropertyType t,
|
|
|
|
const std::string& val)
|
|
|
|
{
|
|
|
|
Props.push_back(std::make_pair(t, val));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef StringMap<ToolOptionDescription> ToolOptionDescriptions;
|
|
|
|
|
|
|
|
// Tool information record
|
|
|
|
|
|
|
|
namespace ToolFlags {
|
|
|
|
enum ToolFlags { Join = 0x1, Sink = 0x2 };
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ToolProperties : public RefCountedBase<ToolProperties> {
|
|
|
|
std::string Name;
|
2008-05-30 06:10:19 +00:00
|
|
|
Init* CmdLine;
|
2008-05-30 06:18:16 +00:00
|
|
|
StrVector InLanguage;
|
2008-03-23 08:57:20 +00:00
|
|
|
std::string OutLanguage;
|
|
|
|
std::string OutputSuffix;
|
|
|
|
unsigned Flags;
|
|
|
|
ToolOptionDescriptions OptDescs;
|
|
|
|
|
|
|
|
// Various boolean properties
|
|
|
|
void setSink() { Flags |= ToolFlags::Sink; }
|
|
|
|
bool isSink() const { return Flags & ToolFlags::Sink; }
|
|
|
|
void setJoin() { Flags |= ToolFlags::Join; }
|
|
|
|
bool isJoin() const { return Flags & ToolFlags::Join; }
|
|
|
|
|
|
|
|
// Default ctor here is needed because StringMap can only store
|
|
|
|
// DefaultConstructible objects
|
2008-06-01 16:22:49 +00:00
|
|
|
ToolProperties() : CmdLine(0), Flags(0) {}
|
|
|
|
ToolProperties (const std::string& n) : Name(n), CmdLine(0), Flags(0) {}
|
2008-03-23 08:57:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// ToolPropertiesList - A list of Tool information records
|
|
|
|
/// IntrusiveRefCntPtrs are used here because StringMap has no copy
|
|
|
|
/// constructor (and we want to avoid copying ToolProperties anyway).
|
2008-03-23 08:57:20 +00:00
|
|
|
typedef std::vector<IntrusiveRefCntPtr<ToolProperties> > ToolPropertiesList;
|
|
|
|
|
|
|
|
|
2008-05-30 06:27:02 +00:00
|
|
|
/// CollectOptionProperties - Function object for iterating over a
|
|
|
|
/// list (usually, a DAG) of option property records.
|
|
|
|
class CollectOptionProperties {
|
|
|
|
private:
|
|
|
|
// Implementation details.
|
|
|
|
|
|
|
|
/// OptionPropertyHandler - a function that extracts information
|
|
|
|
/// about a given option property from its DAG representation.
|
|
|
|
typedef void (CollectOptionProperties::* OptionPropertyHandler)
|
|
|
|
(const DagInit*);
|
|
|
|
|
|
|
|
/// OptionPropertyHandlerMap - A map from option property names to
|
|
|
|
/// option property handlers
|
|
|
|
typedef StringMap<OptionPropertyHandler> OptionPropertyHandlerMap;
|
|
|
|
|
|
|
|
static OptionPropertyHandlerMap optionPropertyHandlers_;
|
|
|
|
static bool staticMembersInitialized_;
|
|
|
|
|
|
|
|
/// This is where the information is stored
|
|
|
|
|
|
|
|
/// toolProps_ - Properties of the current Tool.
|
|
|
|
ToolProperties* toolProps_;
|
|
|
|
/// optDescs_ - OptionDescriptions table (used to register options
|
|
|
|
/// globally).
|
|
|
|
GlobalOptionDescription& optDesc_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
explicit CollectOptionProperties(ToolProperties* TP,
|
|
|
|
GlobalOptionDescription& OD)
|
|
|
|
: toolProps_(TP), optDesc_(OD)
|
|
|
|
{
|
|
|
|
if (!staticMembersInitialized_) {
|
|
|
|
optionPropertyHandlers_["append_cmd"] =
|
|
|
|
&CollectOptionProperties::onAppendCmd;
|
|
|
|
optionPropertyHandlers_["forward"] =
|
|
|
|
&CollectOptionProperties::onForward;
|
2008-09-22 20:46:19 +00:00
|
|
|
optionPropertyHandlers_["forward_as"] =
|
|
|
|
&CollectOptionProperties::onForwardAs;
|
2008-05-30 06:27:02 +00:00
|
|
|
optionPropertyHandlers_["help"] =
|
|
|
|
&CollectOptionProperties::onHelp;
|
|
|
|
optionPropertyHandlers_["output_suffix"] =
|
|
|
|
&CollectOptionProperties::onOutputSuffix;
|
|
|
|
optionPropertyHandlers_["required"] =
|
|
|
|
&CollectOptionProperties::onRequired;
|
|
|
|
optionPropertyHandlers_["stop_compilation"] =
|
|
|
|
&CollectOptionProperties::onStopCompilation;
|
|
|
|
optionPropertyHandlers_["unpack_values"] =
|
|
|
|
&CollectOptionProperties::onUnpackValues;
|
|
|
|
|
|
|
|
staticMembersInitialized_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// operator() - Gets called for every option property; Just forwards
|
|
|
|
/// to the corresponding property handler.
|
|
|
|
void operator() (Init* i) {
|
|
|
|
const DagInit& option_property = InitPtrToDag(i);
|
|
|
|
const std::string& option_property_name
|
|
|
|
= option_property.getOperator()->getAsString();
|
|
|
|
OptionPropertyHandlerMap::iterator method
|
|
|
|
= optionPropertyHandlers_.find(option_property_name);
|
|
|
|
|
|
|
|
if (method != optionPropertyHandlers_.end()) {
|
|
|
|
OptionPropertyHandler h = method->second;
|
|
|
|
(this->*h)(&option_property);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw "Unknown option property: " + option_property_name + "!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/// Option property handlers --
|
|
|
|
/// Methods that handle properties that are common for all types of
|
|
|
|
/// options (like append_cmd, stop_compilation)
|
|
|
|
|
|
|
|
void onAppendCmd (const DagInit* d) {
|
|
|
|
checkNumberOfArguments(d, 1);
|
|
|
|
checkToolProps(d);
|
|
|
|
const std::string& cmd = InitPtrToString(d->getArg(0));
|
|
|
|
|
|
|
|
toolProps_->OptDescs[optDesc_.Name].
|
|
|
|
AddProperty(OptionPropertyType::AppendCmd, cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onOutputSuffix (const DagInit* d) {
|
|
|
|
checkNumberOfArguments(d, 1);
|
|
|
|
checkToolProps(d);
|
|
|
|
const std::string& suf = InitPtrToString(d->getArg(0));
|
|
|
|
|
|
|
|
if (toolProps_->OptDescs[optDesc_.Name].Type != OptionType::Switch)
|
|
|
|
throw "Option " + optDesc_.Name
|
|
|
|
+ " can't have 'output_suffix' property since it isn't a switch!";
|
|
|
|
|
|
|
|
toolProps_->OptDescs[optDesc_.Name].AddProperty
|
|
|
|
(OptionPropertyType::OutputSuffix, suf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onForward (const DagInit* d) {
|
|
|
|
checkNumberOfArguments(d, 0);
|
|
|
|
checkToolProps(d);
|
|
|
|
toolProps_->OptDescs[optDesc_.Name].setForward();
|
|
|
|
}
|
|
|
|
|
2008-09-22 20:46:19 +00:00
|
|
|
void onForwardAs (const DagInit* d) {
|
|
|
|
checkNumberOfArguments(d, 1);
|
|
|
|
checkToolProps(d);
|
|
|
|
const std::string& cmd = InitPtrToString(d->getArg(0));
|
|
|
|
|
|
|
|
toolProps_->OptDescs[optDesc_.Name].
|
|
|
|
AddProperty(OptionPropertyType::ForwardAs, cmd);
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:27:02 +00:00
|
|
|
void onHelp (const DagInit* d) {
|
|
|
|
checkNumberOfArguments(d, 1);
|
|
|
|
const std::string& help_message = InitPtrToString(d->getArg(0));
|
|
|
|
|
|
|
|
optDesc_.Help = help_message;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onRequired (const DagInit* d) {
|
|
|
|
checkNumberOfArguments(d, 0);
|
|
|
|
checkToolProps(d);
|
|
|
|
optDesc_.setRequired();
|
|
|
|
}
|
|
|
|
|
|
|
|
void onStopCompilation (const DagInit* d) {
|
|
|
|
checkNumberOfArguments(d, 0);
|
|
|
|
checkToolProps(d);
|
|
|
|
if (optDesc_.Type != OptionType::Switch)
|
|
|
|
throw std::string("Only options of type Switch can stop compilation!");
|
|
|
|
toolProps_->OptDescs[optDesc_.Name].setStopCompilation();
|
|
|
|
}
|
|
|
|
|
|
|
|
void onUnpackValues (const DagInit* d) {
|
|
|
|
checkNumberOfArguments(d, 0);
|
|
|
|
checkToolProps(d);
|
|
|
|
toolProps_->OptDescs[optDesc_.Name].setUnpackValues();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper functions
|
|
|
|
|
|
|
|
/// checkToolProps - Throw an error if toolProps_ == 0.
|
|
|
|
void checkToolProps(const DagInit* d) {
|
|
|
|
if (!d)
|
|
|
|
throw "Option property " + d->getOperator()->getAsString()
|
|
|
|
+ " can't be used in this context";
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
CollectOptionProperties::OptionPropertyHandlerMap
|
|
|
|
CollectOptionProperties::optionPropertyHandlers_;
|
|
|
|
|
|
|
|
bool CollectOptionProperties::staticMembersInitialized_ = false;
|
|
|
|
|
|
|
|
|
|
|
|
/// processOptionProperties - Go through the list of option
|
|
|
|
/// properties and call a corresponding handler for each.
|
|
|
|
void processOptionProperties (const DagInit* d, ToolProperties* t,
|
|
|
|
GlobalOptionDescription& o) {
|
|
|
|
checkNumberOfArguments(d, 2);
|
|
|
|
DagInit::const_arg_iterator B = d->arg_begin();
|
|
|
|
// Skip the first argument: it's always the option name.
|
|
|
|
++B;
|
|
|
|
std::for_each(B, d->arg_end(), CollectOptionProperties(t, o));
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:27:29 +00:00
|
|
|
/// AddOption - A function object wrapper for
|
|
|
|
/// processOptionProperties. Used by CollectProperties and
|
|
|
|
/// CollectPropertiesFromOptionList.
|
|
|
|
class AddOption {
|
|
|
|
private:
|
|
|
|
GlobalOptionDescriptions& OptDescs_;
|
|
|
|
ToolProperties* ToolProps_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit AddOption(GlobalOptionDescriptions& OD, ToolProperties* TP = 0)
|
|
|
|
: OptDescs_(OD), ToolProps_(TP)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void operator()(const Init* i) {
|
|
|
|
const DagInit& d = InitPtrToDag(i);
|
|
|
|
checkNumberOfArguments(&d, 2);
|
|
|
|
|
|
|
|
const OptionType::OptionType Type =
|
|
|
|
getOptionType(d.getOperator()->getAsString());
|
|
|
|
const std::string& Name = InitPtrToString(d.getArg(0));
|
|
|
|
|
|
|
|
GlobalOptionDescription OD(Type, Name);
|
|
|
|
if (Type != OptionType::Alias) {
|
|
|
|
processOptionProperties(&d, ToolProps_, OD);
|
|
|
|
if (ToolProps_) {
|
|
|
|
ToolProps_->OptDescs[Name].Type = Type;
|
|
|
|
ToolProps_->OptDescs[Name].Name = Name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
OD.Help = InitPtrToString(d.getArg(1));
|
|
|
|
}
|
|
|
|
OptDescs_.insertDescription(OD);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
OptionType::OptionType getOptionType(const std::string& T) const {
|
|
|
|
if (T == "alias_option")
|
|
|
|
return OptionType::Alias;
|
|
|
|
else if (T == "switch_option")
|
|
|
|
return OptionType::Switch;
|
|
|
|
else if (T == "parameter_option")
|
|
|
|
return OptionType::Parameter;
|
|
|
|
else if (T == "parameter_list_option")
|
|
|
|
return OptionType::ParameterList;
|
|
|
|
else if (T == "prefix_option")
|
|
|
|
return OptionType::Prefix;
|
|
|
|
else if (T == "prefix_list_option")
|
|
|
|
return OptionType::PrefixList;
|
|
|
|
else
|
|
|
|
throw "Unknown option type: " + T + '!';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// CollectProperties - Function object for iterating over a list of
|
2008-05-09 08:27:26 +00:00
|
|
|
/// tool property records.
|
2008-03-23 08:57:20 +00:00
|
|
|
class CollectProperties {
|
|
|
|
private:
|
|
|
|
|
2008-05-30 06:27:02 +00:00
|
|
|
// Implementation details
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// PropertyHandler - a function that extracts information
|
|
|
|
/// about a given tool property from its DAG representation
|
2008-05-06 18:18:20 +00:00
|
|
|
typedef void (CollectProperties::*PropertyHandler)(const DagInit*);
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// PropertyHandlerMap - A map from property names to property
|
|
|
|
/// handlers.
|
2008-03-23 08:57:20 +00:00
|
|
|
typedef StringMap<PropertyHandler> PropertyHandlerMap;
|
|
|
|
|
|
|
|
// Static maps from strings to CollectProperties methods("handlers")
|
|
|
|
static PropertyHandlerMap propertyHandlers_;
|
|
|
|
static bool staticMembersInitialized_;
|
|
|
|
|
|
|
|
|
|
|
|
/// This is where the information is stored
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// toolProps_ - Properties of the current Tool.
|
2008-03-23 08:57:20 +00:00
|
|
|
ToolProperties& toolProps_;
|
2008-05-07 21:50:19 +00:00
|
|
|
/// optDescs_ - OptionDescriptions table (used to register options
|
|
|
|
/// globally).
|
2008-03-23 08:57:20 +00:00
|
|
|
GlobalOptionDescriptions& optDescs_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
explicit CollectProperties (ToolProperties& p, GlobalOptionDescriptions& d)
|
|
|
|
: toolProps_(p), optDescs_(d)
|
|
|
|
{
|
|
|
|
if (!staticMembersInitialized_) {
|
|
|
|
propertyHandlers_["cmd_line"] = &CollectProperties::onCmdLine;
|
|
|
|
propertyHandlers_["in_language"] = &CollectProperties::onInLanguage;
|
|
|
|
propertyHandlers_["join"] = &CollectProperties::onJoin;
|
|
|
|
propertyHandlers_["out_language"] = &CollectProperties::onOutLanguage;
|
|
|
|
propertyHandlers_["output_suffix"] = &CollectProperties::onOutputSuffix;
|
|
|
|
propertyHandlers_["parameter_option"]
|
2008-05-30 06:27:29 +00:00
|
|
|
= &CollectProperties::addOption;
|
2008-03-23 08:57:20 +00:00
|
|
|
propertyHandlers_["parameter_list_option"] =
|
2008-05-30 06:27:29 +00:00
|
|
|
&CollectProperties::addOption;
|
|
|
|
propertyHandlers_["prefix_option"] = &CollectProperties::addOption;
|
2008-03-23 08:57:20 +00:00
|
|
|
propertyHandlers_["prefix_list_option"] =
|
2008-05-30 06:27:29 +00:00
|
|
|
&CollectProperties::addOption;
|
2008-03-23 08:57:20 +00:00
|
|
|
propertyHandlers_["sink"] = &CollectProperties::onSink;
|
2008-05-30 06:27:29 +00:00
|
|
|
propertyHandlers_["switch_option"] = &CollectProperties::addOption;
|
|
|
|
propertyHandlers_["alias_option"] = &CollectProperties::addOption;
|
2008-03-23 08:57:20 +00:00
|
|
|
|
|
|
|
staticMembersInitialized_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// operator() - Gets called for every tool property; Just forwards
|
|
|
|
/// to the corresponding property handler.
|
2008-03-23 08:57:20 +00:00
|
|
|
void operator() (Init* i) {
|
2008-05-30 06:18:16 +00:00
|
|
|
const DagInit& d = InitPtrToDag(i);
|
2008-05-06 17:22:03 +00:00
|
|
|
const std::string& property_name = d.getOperator()->getAsString();
|
2008-03-23 08:57:20 +00:00
|
|
|
PropertyHandlerMap::iterator method
|
|
|
|
= propertyHandlers_.find(property_name);
|
|
|
|
|
|
|
|
if (method != propertyHandlers_.end()) {
|
|
|
|
PropertyHandler h = method->second;
|
|
|
|
(this->*h)(&d);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw "Unknown tool property: " + property_name + "!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/// Property handlers --
|
|
|
|
/// Functions that extract information about tool properties from
|
|
|
|
/// DAG representation.
|
|
|
|
|
2008-05-06 18:18:20 +00:00
|
|
|
void onCmdLine (const DagInit* d) {
|
2008-03-23 08:57:20 +00:00
|
|
|
checkNumberOfArguments(d, 1);
|
2008-05-30 06:10:19 +00:00
|
|
|
toolProps_.CmdLine = d->getArg(0);
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2008-05-06 18:18:20 +00:00
|
|
|
void onInLanguage (const DagInit* d) {
|
2008-03-23 08:57:20 +00:00
|
|
|
checkNumberOfArguments(d, 1);
|
2008-05-30 06:18:16 +00:00
|
|
|
Init* arg = d->getArg(0);
|
|
|
|
|
|
|
|
// Find out the argument's type.
|
|
|
|
if (typeid(*arg) == typeid(StringInit)) {
|
|
|
|
// It's a string.
|
|
|
|
toolProps_.InLanguage.push_back(InitPtrToString(arg));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// It's a list.
|
|
|
|
const ListInit& lst = InitPtrToList(arg);
|
|
|
|
StrVector& out = toolProps_.InLanguage;
|
|
|
|
|
|
|
|
// Copy strings to the output vector.
|
|
|
|
for (ListInit::const_iterator B = lst.begin(), E = lst.end();
|
|
|
|
B != E; ++B) {
|
|
|
|
out.push_back(InitPtrToString(*B));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove duplicates.
|
|
|
|
std::sort(out.begin(), out.end());
|
|
|
|
StrVector::iterator newE = std::unique(out.begin(), out.end());
|
|
|
|
out.erase(newE, out.end());
|
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2008-05-06 18:18:20 +00:00
|
|
|
void onJoin (const DagInit* d) {
|
2008-03-23 08:57:20 +00:00
|
|
|
checkNumberOfArguments(d, 0);
|
|
|
|
toolProps_.setJoin();
|
|
|
|
}
|
|
|
|
|
2008-05-06 18:18:20 +00:00
|
|
|
void onOutLanguage (const DagInit* d) {
|
2008-03-23 08:57:20 +00:00
|
|
|
checkNumberOfArguments(d, 1);
|
|
|
|
toolProps_.OutLanguage = InitPtrToString(d->getArg(0));
|
|
|
|
}
|
|
|
|
|
2008-05-06 18:18:20 +00:00
|
|
|
void onOutputSuffix (const DagInit* d) {
|
2008-03-23 08:57:20 +00:00
|
|
|
checkNumberOfArguments(d, 1);
|
|
|
|
toolProps_.OutputSuffix = InitPtrToString(d->getArg(0));
|
|
|
|
}
|
|
|
|
|
2008-05-06 18:18:20 +00:00
|
|
|
void onSink (const DagInit* d) {
|
2008-03-23 08:57:20 +00:00
|
|
|
checkNumberOfArguments(d, 0);
|
|
|
|
optDescs_.HasSink = true;
|
|
|
|
toolProps_.setSink();
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:27:29 +00:00
|
|
|
// Just forwards to the AddOption function object. Somewhat
|
|
|
|
// non-optimal, but avoids code duplication.
|
|
|
|
void addOption (const DagInit* d) {
|
2008-03-23 08:57:20 +00:00
|
|
|
checkNumberOfArguments(d, 2);
|
2008-05-30 06:27:29 +00:00
|
|
|
AddOption(optDescs_, &toolProps_)(d);
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2008-05-30 06:27:02 +00:00
|
|
|
// Defintions of static members of CollectProperties.
|
|
|
|
CollectProperties::PropertyHandlerMap CollectProperties::propertyHandlers_;
|
2008-03-23 08:57:20 +00:00
|
|
|
bool CollectProperties::staticMembersInitialized_ = false;
|
|
|
|
|
|
|
|
|
2008-05-30 06:26:08 +00:00
|
|
|
/// CollectToolProperties - Gather information about tool properties
|
|
|
|
/// from the parsed TableGen data (basically a wrapper for the
|
|
|
|
/// CollectProperties function object).
|
2008-03-23 08:57:20 +00:00
|
|
|
void CollectToolProperties (RecordVector::const_iterator B,
|
|
|
|
RecordVector::const_iterator E,
|
|
|
|
ToolPropertiesList& TPList,
|
|
|
|
GlobalOptionDescriptions& OptDescs)
|
|
|
|
{
|
|
|
|
// Iterate over a properties list of every Tool definition
|
|
|
|
for (;B!=E;++B) {
|
2008-05-30 06:26:08 +00:00
|
|
|
Record* T = *B;
|
|
|
|
// Throws an exception if the value does not exist.
|
2008-03-23 08:57:20 +00:00
|
|
|
ListInit* PropList = T->getValueAsListInit("properties");
|
|
|
|
|
|
|
|
IntrusiveRefCntPtr<ToolProperties>
|
|
|
|
ToolProps(new ToolProperties(T->getName()));
|
|
|
|
|
|
|
|
std::for_each(PropList->begin(), PropList->end(),
|
|
|
|
CollectProperties(*ToolProps, OptDescs));
|
|
|
|
TPList.push_back(ToolProps);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:27:02 +00:00
|
|
|
|
|
|
|
/// CollectPropertiesFromOptionList - Gather information about
|
2008-05-30 06:26:08 +00:00
|
|
|
/// *global* option properties from the OptionList.
|
|
|
|
void CollectPropertiesFromOptionList (RecordVector::const_iterator B,
|
|
|
|
RecordVector::const_iterator E,
|
|
|
|
GlobalOptionDescriptions& OptDescs)
|
|
|
|
{
|
|
|
|
// Iterate over a properties list of every Tool definition
|
|
|
|
for (;B!=E;++B) {
|
|
|
|
RecordVector::value_type T = *B;
|
|
|
|
// Throws an exception if the value does not exist.
|
|
|
|
ListInit* PropList = T->getValueAsListInit("options");
|
|
|
|
|
2008-05-30 06:27:02 +00:00
|
|
|
std::for_each(PropList->begin(), PropList->end(), AddOption(OptDescs));
|
2008-05-30 06:26:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:28:37 +00:00
|
|
|
/// CheckForSuperfluousOptions - Check that there are no side
|
|
|
|
/// effect-free options (specified only in the OptionList). Otherwise,
|
|
|
|
/// output a warning.
|
|
|
|
void CheckForSuperfluousOptions (const ToolPropertiesList& TPList,
|
|
|
|
const GlobalOptionDescriptions& OptDescs) {
|
|
|
|
llvm::StringSet<> nonSuperfluousOptions;
|
|
|
|
|
|
|
|
// Add all options mentioned in the TPList to the set of
|
|
|
|
// non-superfluous options.
|
|
|
|
for (ToolPropertiesList::const_iterator B = TPList.begin(),
|
|
|
|
E = TPList.end(); B != E; ++B) {
|
|
|
|
const ToolProperties& TP = *(*B);
|
|
|
|
for (ToolOptionDescriptions::const_iterator B = TP.OptDescs.begin(),
|
|
|
|
E = TP.OptDescs.end(); B != E; ++B) {
|
|
|
|
nonSuperfluousOptions.insert(B->first());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that all options in OptDescs belong to the set of
|
|
|
|
// non-superfluous options.
|
|
|
|
for (GlobalOptionDescriptions::const_iterator B = OptDescs.begin(),
|
|
|
|
E = OptDescs.end(); B != E; ++B) {
|
|
|
|
const GlobalOptionDescription& Val = B->second;
|
|
|
|
if (!nonSuperfluousOptions.count(Val.Name)
|
|
|
|
&& Val.Type != OptionType::Alias)
|
|
|
|
cerr << "Warning: option '-" << Val.Name << "' has no effect! "
|
|
|
|
"Probable cause: this option is specified only in the OptionList.\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:10:19 +00:00
|
|
|
/// EmitCaseTest1Arg - Helper function used by
|
|
|
|
/// EmitCaseConstructHandler.
|
|
|
|
bool EmitCaseTest1Arg(const std::string& TestName,
|
|
|
|
const DagInit& d,
|
|
|
|
const GlobalOptionDescriptions& OptDescs,
|
|
|
|
std::ostream& O) {
|
|
|
|
checkNumberOfArguments(&d, 1);
|
|
|
|
const std::string& OptName = InitPtrToString(d.getArg(0));
|
|
|
|
if (TestName == "switch_on") {
|
|
|
|
const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName);
|
|
|
|
if (OptDesc.Type != OptionType::Switch)
|
|
|
|
throw OptName + ": incorrect option type!";
|
|
|
|
O << OptDesc.GenVariableName();
|
|
|
|
return true;
|
|
|
|
} else if (TestName == "input_languages_contain") {
|
|
|
|
O << "InLangs.count(\"" << OptName << "\") != 0";
|
|
|
|
return true;
|
2008-05-30 06:19:52 +00:00
|
|
|
} else if (TestName == "in_language") {
|
2008-09-22 20:48:22 +00:00
|
|
|
// This works only for single-argument Tool::GenerateAction. Join
|
|
|
|
// tools can process several files in different languages simultaneously.
|
|
|
|
|
|
|
|
// TODO: make this work with Edge::Weight (if possible).
|
2008-09-22 20:47:46 +00:00
|
|
|
O << "LangMap.GetLanguage(inFile) == \"" << OptName << '\"';
|
2008-05-30 06:19:52 +00:00
|
|
|
return true;
|
|
|
|
} else if (TestName == "not_empty") {
|
2008-05-30 06:24:07 +00:00
|
|
|
if (OptName == "o") {
|
|
|
|
O << "!OutputFilename.empty()";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName);
|
|
|
|
if (OptDesc.Type == OptionType::Switch)
|
|
|
|
throw OptName + ": incorrect option type!";
|
|
|
|
O << '!' << OptDesc.GenVariableName() << ".empty()";
|
|
|
|
return true;
|
|
|
|
}
|
2008-05-30 06:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitCaseTest2Args - Helper function used by
|
|
|
|
/// EmitCaseConstructHandler.
|
|
|
|
bool EmitCaseTest2Args(const std::string& TestName,
|
|
|
|
const DagInit& d,
|
|
|
|
const char* IndentLevel,
|
|
|
|
const GlobalOptionDescriptions& OptDescs,
|
|
|
|
std::ostream& O) {
|
|
|
|
checkNumberOfArguments(&d, 2);
|
|
|
|
const std::string& OptName = InitPtrToString(d.getArg(0));
|
|
|
|
const std::string& OptArg = InitPtrToString(d.getArg(1));
|
|
|
|
const GlobalOptionDescription& OptDesc = OptDescs.FindOption(OptName);
|
|
|
|
|
|
|
|
if (TestName == "parameter_equals") {
|
|
|
|
if (OptDesc.Type != OptionType::Parameter
|
|
|
|
&& OptDesc.Type != OptionType::Prefix)
|
|
|
|
throw OptName + ": incorrect option type!";
|
|
|
|
O << OptDesc.GenVariableName() << " == \"" << OptArg << "\"";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (TestName == "element_in_list") {
|
|
|
|
if (OptDesc.Type != OptionType::ParameterList
|
|
|
|
&& OptDesc.Type != OptionType::PrefixList)
|
|
|
|
throw OptName + ": incorrect option type!";
|
|
|
|
const std::string& VarName = OptDesc.GenVariableName();
|
|
|
|
O << "std::find(" << VarName << ".begin(),\n"
|
|
|
|
<< IndentLevel << Indent1 << VarName << ".end(), \""
|
|
|
|
<< OptArg << "\") != " << VarName << ".end()";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forward declaration.
|
|
|
|
// EmitLogicalOperationTest and EmitCaseTest are mutually recursive.
|
|
|
|
void EmitCaseTest(const DagInit& d, const char* IndentLevel,
|
|
|
|
const GlobalOptionDescriptions& OptDescs,
|
|
|
|
std::ostream& O);
|
|
|
|
|
|
|
|
/// EmitLogicalOperationTest - Helper function used by
|
|
|
|
/// EmitCaseConstructHandler.
|
|
|
|
void EmitLogicalOperationTest(const DagInit& d, const char* LogicOp,
|
|
|
|
const char* IndentLevel,
|
|
|
|
const GlobalOptionDescriptions& OptDescs,
|
|
|
|
std::ostream& O) {
|
|
|
|
O << '(';
|
|
|
|
for (unsigned j = 0, NumArgs = d.getNumArgs(); j < NumArgs; ++j) {
|
2008-05-30 06:18:16 +00:00
|
|
|
const DagInit& InnerTest = InitPtrToDag(d.getArg(j));
|
2008-05-30 06:10:19 +00:00
|
|
|
EmitCaseTest(InnerTest, IndentLevel, OptDescs, O);
|
|
|
|
if (j != NumArgs - 1)
|
|
|
|
O << ")\n" << IndentLevel << Indent1 << ' ' << LogicOp << " (";
|
|
|
|
else
|
|
|
|
O << ')';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitCaseTest - Helper function used by EmitCaseConstructHandler.
|
|
|
|
void EmitCaseTest(const DagInit& d, const char* IndentLevel,
|
|
|
|
const GlobalOptionDescriptions& OptDescs,
|
|
|
|
std::ostream& O) {
|
|
|
|
const std::string& TestName = d.getOperator()->getAsString();
|
|
|
|
|
|
|
|
if (TestName == "and")
|
|
|
|
EmitLogicalOperationTest(d, "&&", IndentLevel, OptDescs, O);
|
|
|
|
else if (TestName == "or")
|
|
|
|
EmitLogicalOperationTest(d, "||", IndentLevel, OptDescs, O);
|
|
|
|
else if (EmitCaseTest1Arg(TestName, d, OptDescs, O))
|
|
|
|
return;
|
|
|
|
else if (EmitCaseTest2Args(TestName, d, IndentLevel, OptDescs, O))
|
|
|
|
return;
|
|
|
|
else
|
|
|
|
throw TestName + ": unknown edge property!";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit code that handles the 'case' construct.
|
|
|
|
// Takes a function object that should emit code for every case clause.
|
|
|
|
// Callback's type is
|
|
|
|
// void F(Init* Statement, const char* IndentLevel, std::ostream& O).
|
|
|
|
template <typename F>
|
|
|
|
void EmitCaseConstructHandler(const DagInit* d, const char* IndentLevel,
|
2008-05-31 13:43:21 +00:00
|
|
|
F Callback, bool EmitElseIf,
|
2008-05-30 06:10:19 +00:00
|
|
|
const GlobalOptionDescriptions& OptDescs,
|
|
|
|
std::ostream& O) {
|
|
|
|
assert(d->getOperator()->getAsString() == "case");
|
|
|
|
|
2008-05-30 06:15:47 +00:00
|
|
|
unsigned numArgs = d->getNumArgs();
|
|
|
|
if (d->getNumArgs() < 2)
|
|
|
|
throw "There should be at least one clause in the 'case' expression:\n"
|
|
|
|
+ d->getAsString();
|
|
|
|
|
|
|
|
for (unsigned i = 0; i != numArgs; ++i) {
|
2008-05-30 06:18:16 +00:00
|
|
|
const DagInit& Test = InitPtrToDag(d->getArg(i));
|
2008-05-30 06:10:19 +00:00
|
|
|
|
2008-05-30 06:15:47 +00:00
|
|
|
// Emit the test.
|
2008-05-30 06:10:19 +00:00
|
|
|
if (Test.getOperator()->getAsString() == "default") {
|
|
|
|
if (i+2 != numArgs)
|
|
|
|
throw std::string("The 'default' clause should be the last in the"
|
|
|
|
"'case' construct!");
|
|
|
|
O << IndentLevel << "else {\n";
|
|
|
|
}
|
|
|
|
else {
|
2008-05-30 06:22:15 +00:00
|
|
|
O << IndentLevel << ((i != 0 && EmitElseIf) ? "else if (" : "if (");
|
2008-05-30 06:10:19 +00:00
|
|
|
EmitCaseTest(Test, IndentLevel, OptDescs, O);
|
|
|
|
O << ") {\n";
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:15:47 +00:00
|
|
|
// Emit the corresponding statement.
|
2008-05-30 06:10:19 +00:00
|
|
|
++i;
|
|
|
|
if (i == numArgs)
|
|
|
|
throw "Case construct handler: no corresponding action "
|
|
|
|
"found for the test " + Test.getAsString() + '!';
|
|
|
|
|
2008-05-30 06:22:15 +00:00
|
|
|
Init* arg = d->getArg(i);
|
|
|
|
if (dynamic_cast<DagInit*>(arg)
|
|
|
|
&& static_cast<DagInit*>(arg)->getOperator()->getAsString() == "case") {
|
|
|
|
EmitCaseConstructHandler(static_cast<DagInit*>(arg),
|
|
|
|
(std::string(IndentLevel) + Indent1).c_str(),
|
|
|
|
Callback, EmitElseIf, OptDescs, O);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Callback(arg, IndentLevel, O);
|
|
|
|
}
|
2008-05-30 06:10:19 +00:00
|
|
|
O << IndentLevel << "}\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-09 08:27:26 +00:00
|
|
|
/// EmitForwardOptionPropertyHandlingCode - Helper function used to
|
|
|
|
/// implement EmitOptionPropertyHandlingCode(). Emits code for
|
2008-09-22 20:46:19 +00:00
|
|
|
/// handling the (forward) and (forward_as) option properties.
|
2008-05-09 08:27:26 +00:00
|
|
|
void EmitForwardOptionPropertyHandlingCode (const ToolOptionDescription& D,
|
2008-09-22 20:46:19 +00:00
|
|
|
const std::string& NewName,
|
2008-05-09 08:27:26 +00:00
|
|
|
std::ostream& O) {
|
2008-09-22 20:46:19 +00:00
|
|
|
const std::string& Name = NewName.empty()
|
|
|
|
? ("-" + D.Name)
|
|
|
|
: NewName;
|
|
|
|
|
2008-05-09 08:27:26 +00:00
|
|
|
switch (D.Type) {
|
|
|
|
case OptionType::Switch:
|
2008-09-22 20:46:19 +00:00
|
|
|
O << Indent3 << "vec.push_back(\"" << Name << "\");\n";
|
2008-05-09 08:27:26 +00:00
|
|
|
break;
|
|
|
|
case OptionType::Parameter:
|
2008-09-22 20:46:19 +00:00
|
|
|
O << Indent3 << "vec.push_back(\"" << Name << "\");\n";
|
2008-05-09 08:27:26 +00:00
|
|
|
O << Indent3 << "vec.push_back(" << D.GenVariableName() << ");\n";
|
|
|
|
break;
|
|
|
|
case OptionType::Prefix:
|
2008-09-22 20:46:19 +00:00
|
|
|
O << Indent3 << "vec.push_back(\"" << Name << "\" + "
|
2008-05-09 08:27:26 +00:00
|
|
|
<< D.GenVariableName() << ");\n";
|
|
|
|
break;
|
|
|
|
case OptionType::PrefixList:
|
|
|
|
O << Indent3 << "for (" << D.GenTypeDeclaration()
|
|
|
|
<< "::iterator B = " << D.GenVariableName() << ".begin(),\n"
|
|
|
|
<< Indent3 << "E = " << D.GenVariableName() << ".end(); B != E; ++B)\n"
|
2008-09-22 20:46:19 +00:00
|
|
|
<< Indent4 << "vec.push_back(\"" << Name << "\" + "
|
2008-05-09 08:27:26 +00:00
|
|
|
<< "*B);\n";
|
|
|
|
break;
|
|
|
|
case OptionType::ParameterList:
|
|
|
|
O << Indent3 << "for (" << D.GenTypeDeclaration()
|
|
|
|
<< "::iterator B = " << D.GenVariableName() << ".begin(),\n"
|
|
|
|
<< Indent3 << "E = " << D.GenVariableName()
|
|
|
|
<< ".end() ; B != E; ++B) {\n"
|
2008-09-22 20:46:19 +00:00
|
|
|
<< Indent4 << "vec.push_back(\"" << Name << "\");\n"
|
2008-05-09 08:27:26 +00:00
|
|
|
<< Indent4 << "vec.push_back(*B);\n"
|
|
|
|
<< Indent3 << "}\n";
|
|
|
|
break;
|
2008-05-30 06:22:52 +00:00
|
|
|
case OptionType::Alias:
|
|
|
|
default:
|
|
|
|
throw std::string("Aliases are not allowed in tool option descriptions!");
|
2008-05-09 08:27:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:15:20 +00:00
|
|
|
// ToolOptionHasInterestingProperties - A helper function used by
|
|
|
|
// EmitOptionPropertyHandlingCode() that tells us whether we should
|
|
|
|
// emit any property handling code at all.
|
2008-05-30 06:10:47 +00:00
|
|
|
bool ToolOptionHasInterestingProperties(const ToolOptionDescription& D) {
|
2008-05-30 06:15:20 +00:00
|
|
|
bool ret = false;
|
|
|
|
for (OptionPropertyList::const_iterator B = D.Props.begin(),
|
|
|
|
E = D.Props.end(); B != E; ++B) {
|
|
|
|
const OptionProperty& OptProp = *B;
|
2008-09-22 20:46:19 +00:00
|
|
|
if (OptProp.first == OptionPropertyType::AppendCmd
|
|
|
|
|| OptProp.first == OptionPropertyType::ForwardAs)
|
2008-05-30 06:15:20 +00:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
if (D.isForward() || D.isUnpackValues())
|
|
|
|
ret = true;
|
|
|
|
return ret;
|
2008-05-30 06:10:47 +00:00
|
|
|
}
|
|
|
|
|
2008-05-09 08:27:26 +00:00
|
|
|
/// EmitOptionPropertyHandlingCode - Helper function used by
|
|
|
|
/// EmitGenerateActionMethod(). Emits code that handles option
|
|
|
|
/// properties.
|
2008-05-30 06:10:47 +00:00
|
|
|
void EmitOptionPropertyHandlingCode (const ToolOptionDescription& D,
|
2008-03-23 08:57:20 +00:00
|
|
|
std::ostream& O)
|
|
|
|
{
|
2008-05-30 06:10:47 +00:00
|
|
|
if (!ToolOptionHasInterestingProperties(D))
|
|
|
|
return;
|
2008-05-09 08:27:26 +00:00
|
|
|
// Start of the if-clause.
|
2008-03-23 08:57:20 +00:00
|
|
|
O << Indent2 << "if (";
|
|
|
|
if (D.Type == OptionType::Switch)
|
|
|
|
O << D.GenVariableName();
|
|
|
|
else
|
|
|
|
O << '!' << D.GenVariableName() << ".empty()";
|
|
|
|
|
|
|
|
O <<") {\n";
|
|
|
|
|
2008-05-09 08:27:26 +00:00
|
|
|
// Handle option properties that take an argument.
|
2008-03-23 08:57:20 +00:00
|
|
|
for (OptionPropertyList::const_iterator B = D.Props.begin(),
|
|
|
|
E = D.Props.end(); B!=E; ++B) {
|
|
|
|
const OptionProperty& val = *B;
|
|
|
|
|
|
|
|
switch (val.first) {
|
|
|
|
// (append_cmd cmd) property
|
|
|
|
case OptionPropertyType::AppendCmd:
|
|
|
|
O << Indent3 << "vec.push_back(\"" << val.second << "\");\n";
|
|
|
|
break;
|
2008-09-22 20:46:19 +00:00
|
|
|
// (forward_as) property
|
|
|
|
case OptionPropertyType::ForwardAs:
|
|
|
|
EmitForwardOptionPropertyHandlingCode(D, val.second, O);
|
|
|
|
break;
|
2008-03-23 08:57:20 +00:00
|
|
|
// Other properties with argument
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle flags
|
|
|
|
|
|
|
|
// (forward) property
|
2008-05-09 08:27:26 +00:00
|
|
|
if (D.isForward())
|
2008-09-22 20:46:19 +00:00
|
|
|
EmitForwardOptionPropertyHandlingCode(D, "", O);
|
2008-03-23 08:57:20 +00:00
|
|
|
|
|
|
|
// (unpack_values) property
|
|
|
|
if (D.isUnpackValues()) {
|
|
|
|
if (IsListOptionType(D.Type)) {
|
|
|
|
O << Indent3 << "for (" << D.GenTypeDeclaration()
|
|
|
|
<< "::iterator B = " << D.GenVariableName() << ".begin(),\n"
|
|
|
|
<< Indent3 << "E = " << D.GenVariableName()
|
|
|
|
<< ".end(); B != E; ++B)\n"
|
2008-05-06 18:13:45 +00:00
|
|
|
<< Indent4 << "llvm::SplitString(*B, vec, \",\");\n";
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
else if (D.Type == OptionType::Prefix || D.Type == OptionType::Parameter){
|
2008-05-06 18:13:45 +00:00
|
|
|
O << Indent3 << "llvm::SplitString("
|
|
|
|
<< D.GenVariableName() << ", vec, \",\");\n";
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw std::string("Switches can't have unpack_values property!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-09 08:27:26 +00:00
|
|
|
// End of the if-clause.
|
2008-03-23 08:57:20 +00:00
|
|
|
O << Indent2 << "}\n";
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:12:24 +00:00
|
|
|
/// SubstituteSpecialCommands - Perform string substitution for $CALL
|
|
|
|
/// and $ENV. Helper function used by EmitCmdLineVecFill().
|
|
|
|
std::string SubstituteSpecialCommands(const std::string& cmd) {
|
2008-05-30 06:13:29 +00:00
|
|
|
size_t cparen = cmd.find(")");
|
|
|
|
std::string ret;
|
|
|
|
|
|
|
|
if (cmd.find("$CALL(") == 0) {
|
|
|
|
if (cmd.size() == 6)
|
|
|
|
throw std::string("$CALL invocation: empty argument list!");
|
|
|
|
|
|
|
|
ret += "hooks::";
|
|
|
|
ret += std::string(cmd.begin() + 6, cmd.begin() + cparen);
|
|
|
|
ret += "()";
|
|
|
|
}
|
|
|
|
else if (cmd.find("$ENV(") == 0) {
|
|
|
|
if (cmd.size() == 5)
|
|
|
|
throw std::string("$ENV invocation: empty argument list!");
|
|
|
|
|
|
|
|
ret += "std::getenv(\"";
|
|
|
|
ret += std::string(cmd.begin() + 5, cmd.begin() + cparen);
|
|
|
|
ret += "\")";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw "Unknown special command: " + cmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd.begin() + cparen + 1 != cmd.end()) {
|
|
|
|
ret += " + std::string(\"";
|
|
|
|
ret += (cmd.c_str() + cparen + 1);
|
|
|
|
ret += "\")";
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2008-05-30 06:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitCmdLineVecFill - Emit code that fills in the command line
|
|
|
|
/// vector. Helper function used by EmitGenerateActionMethod().
|
2008-05-30 06:10:19 +00:00
|
|
|
void EmitCmdLineVecFill(const Init* CmdLine, const std::string& ToolName,
|
|
|
|
bool Version, const char* IndentLevel,
|
|
|
|
std::ostream& O) {
|
|
|
|
StrVector StrVec;
|
2008-05-30 06:13:29 +00:00
|
|
|
SplitString(InitPtrToString(CmdLine), StrVec);
|
2008-05-30 06:12:24 +00:00
|
|
|
if (StrVec.empty())
|
2008-05-30 06:10:19 +00:00
|
|
|
throw "Tool " + ToolName + " has empty command line!";
|
2008-05-06 16:35:25 +00:00
|
|
|
|
2008-05-30 06:10:19 +00:00
|
|
|
StrVector::const_iterator I = StrVec.begin();
|
2008-03-23 08:57:20 +00:00
|
|
|
++I;
|
2008-05-30 06:10:19 +00:00
|
|
|
for (StrVector::const_iterator E = StrVec.end(); I != E; ++I) {
|
2008-03-23 08:57:20 +00:00
|
|
|
const std::string& cmd = *I;
|
2008-05-30 06:10:19 +00:00
|
|
|
O << IndentLevel;
|
2008-05-30 06:12:24 +00:00
|
|
|
if (cmd.at(0) == '$') {
|
|
|
|
if (cmd == "$INFILE") {
|
|
|
|
if (Version)
|
|
|
|
O << "for (PathVector::const_iterator B = inFiles.begin()"
|
|
|
|
<< ", E = inFiles.end();\n"
|
|
|
|
<< IndentLevel << "B != E; ++B)\n"
|
|
|
|
<< IndentLevel << Indent1 << "vec.push_back(B->toString());\n";
|
|
|
|
else
|
|
|
|
O << "vec.push_back(inFile.toString());\n";
|
|
|
|
}
|
|
|
|
else if (cmd == "$OUTFILE") {
|
|
|
|
O << "vec.push_back(outFile.toString());\n";
|
|
|
|
}
|
|
|
|
else {
|
2008-05-30 06:13:29 +00:00
|
|
|
O << "vec.push_back(" << SubstituteSpecialCommands(cmd);
|
|
|
|
O << ");\n";
|
2008-05-30 06:12:24 +00:00
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
O << "vec.push_back(\"" << cmd << "\");\n";
|
|
|
|
}
|
|
|
|
}
|
2008-05-30 06:23:29 +00:00
|
|
|
O << IndentLevel << "cmd = "
|
2008-05-30 06:12:24 +00:00
|
|
|
<< ((StrVec[0][0] == '$') ? SubstituteSpecialCommands(StrVec[0])
|
|
|
|
: "\"" + StrVec[0] + "\"")
|
2008-05-30 06:23:29 +00:00
|
|
|
<< ";\n";
|
2008-05-30 06:10:19 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 06:12:24 +00:00
|
|
|
/// EmitCmdLineVecFillCallback - A function object wrapper around
|
|
|
|
/// EmitCmdLineVecFill(). Used by EmitGenerateActionMethod() as an
|
|
|
|
/// argument to EmitCaseConstructHandler().
|
2008-05-30 06:10:19 +00:00
|
|
|
class EmitCmdLineVecFillCallback {
|
|
|
|
bool Version;
|
|
|
|
const std::string& ToolName;
|
|
|
|
public:
|
|
|
|
EmitCmdLineVecFillCallback(bool Ver, const std::string& TN)
|
|
|
|
: Version(Ver), ToolName(TN) {}
|
|
|
|
|
|
|
|
void operator()(const Init* Statement, const char* IndentLevel,
|
|
|
|
std::ostream& O) const
|
|
|
|
{
|
2008-05-30 06:10:47 +00:00
|
|
|
EmitCmdLineVecFill(Statement, ToolName, Version,
|
|
|
|
(std::string(IndentLevel) + Indent1).c_str(), O);
|
2008-05-30 06:10:19 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// EmitGenerateActionMethod - Emit one of two versions of the
|
|
|
|
// Tool::GenerateAction() method.
|
|
|
|
void EmitGenerateActionMethod (const ToolProperties& P,
|
|
|
|
const GlobalOptionDescriptions& OptDescs,
|
|
|
|
bool Version, std::ostream& O) {
|
|
|
|
if (Version)
|
|
|
|
O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n";
|
|
|
|
else
|
|
|
|
O << Indent1 << "Action GenerateAction(const sys::Path& inFile,\n";
|
|
|
|
|
|
|
|
O << Indent2 << "const sys::Path& outFile,\n"
|
2008-09-22 20:47:46 +00:00
|
|
|
<< Indent2 << "const InputLanguagesSet& InLangs,\n"
|
|
|
|
<< Indent2 << "const LanguageMap& LangMap) const\n"
|
2008-05-30 06:10:19 +00:00
|
|
|
<< Indent1 << "{\n"
|
2008-05-30 06:24:49 +00:00
|
|
|
<< Indent2 << "const char* cmd;\n"
|
2008-05-30 06:10:19 +00:00
|
|
|
<< Indent2 << "std::vector<std::string> vec;\n";
|
|
|
|
|
|
|
|
// cmd_line is either a string or a 'case' construct.
|
|
|
|
if (typeid(*P.CmdLine) == typeid(StringInit))
|
|
|
|
EmitCmdLineVecFill(P.CmdLine, P.Name, Version, Indent2, O);
|
|
|
|
else
|
2008-05-30 06:18:16 +00:00
|
|
|
EmitCaseConstructHandler(&InitPtrToDag(P.CmdLine), Indent2,
|
2008-05-30 06:10:19 +00:00
|
|
|
EmitCmdLineVecFillCallback(Version, P.Name),
|
2008-05-30 06:22:15 +00:00
|
|
|
true, OptDescs, O);
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-09 08:27:26 +00:00
|
|
|
// For every understood option, emit handling code.
|
2008-03-23 08:57:20 +00:00
|
|
|
for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
|
|
|
|
E = P.OptDescs.end(); B != E; ++B) {
|
|
|
|
const ToolOptionDescription& val = B->second;
|
2008-05-30 06:10:47 +00:00
|
|
|
EmitOptionPropertyHandlingCode(val, O);
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2008-05-09 08:27:26 +00:00
|
|
|
// Handle the Sink property.
|
2008-03-23 08:57:20 +00:00
|
|
|
if (P.isSink()) {
|
|
|
|
O << Indent2 << "if (!" << SinkOptionName << ".empty()) {\n"
|
|
|
|
<< Indent3 << "vec.insert(vec.end(), "
|
|
|
|
<< SinkOptionName << ".begin(), " << SinkOptionName << ".end());\n"
|
|
|
|
<< Indent2 << "}\n";
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:23:29 +00:00
|
|
|
O << Indent2 << "return Action(cmd, vec);\n"
|
2008-03-23 08:57:20 +00:00
|
|
|
<< Indent1 << "}\n\n";
|
|
|
|
}
|
|
|
|
|
2008-05-09 08:27:26 +00:00
|
|
|
/// EmitGenerateActionMethods - Emit two GenerateAction() methods for
|
|
|
|
/// a given Tool class.
|
2008-05-30 06:10:19 +00:00
|
|
|
void EmitGenerateActionMethods (const ToolProperties& P,
|
|
|
|
const GlobalOptionDescriptions& OptDescs,
|
|
|
|
std::ostream& O) {
|
2008-03-23 08:57:20 +00:00
|
|
|
if (!P.isJoin())
|
|
|
|
O << Indent1 << "Action GenerateAction(const PathVector& inFiles,\n"
|
2008-05-30 06:10:19 +00:00
|
|
|
<< Indent2 << "const llvm::sys::Path& outFile,\n"
|
2008-09-22 20:47:46 +00:00
|
|
|
<< Indent2 << "const InputLanguagesSet& InLangs,\n"
|
|
|
|
<< Indent2 << "const LanguageMap& LangMap) const\n"
|
2008-03-23 08:57:20 +00:00
|
|
|
<< Indent1 << "{\n"
|
|
|
|
<< Indent2 << "throw std::runtime_error(\"" << P.Name
|
|
|
|
<< " is not a Join tool!\");\n"
|
|
|
|
<< Indent1 << "}\n\n";
|
|
|
|
else
|
2008-05-30 06:10:19 +00:00
|
|
|
EmitGenerateActionMethod(P, OptDescs, true, O);
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-30 06:10:19 +00:00
|
|
|
EmitGenerateActionMethod(P, OptDescs, false, O);
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2008-05-09 08:27:26 +00:00
|
|
|
/// EmitIsLastMethod - Emit the IsLast() method for a given Tool
|
|
|
|
/// class.
|
2008-03-23 08:57:20 +00:00
|
|
|
void EmitIsLastMethod (const ToolProperties& P, std::ostream& O) {
|
|
|
|
O << Indent1 << "bool IsLast() const {\n"
|
|
|
|
<< Indent2 << "bool last = false;\n";
|
|
|
|
|
|
|
|
for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
|
|
|
|
E = P.OptDescs.end(); B != E; ++B) {
|
|
|
|
const ToolOptionDescription& val = B->second;
|
|
|
|
|
|
|
|
if (val.isStopCompilation())
|
|
|
|
O << Indent2
|
|
|
|
<< "if (" << val.GenVariableName()
|
|
|
|
<< ")\n" << Indent3 << "last = true;\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
O << Indent2 << "return last;\n"
|
|
|
|
<< Indent1 << "}\n\n";
|
|
|
|
}
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// EmitInOutLanguageMethods - Emit the [Input,Output]Language()
|
|
|
|
/// methods for a given Tool class.
|
2008-03-23 08:57:20 +00:00
|
|
|
void EmitInOutLanguageMethods (const ToolProperties& P, std::ostream& O) {
|
2008-05-30 06:24:49 +00:00
|
|
|
O << Indent1 << "const char** InputLanguages() const {\n"
|
|
|
|
<< Indent2 << "return InputLanguages_;\n"
|
2008-03-23 08:57:20 +00:00
|
|
|
<< Indent1 << "}\n\n";
|
|
|
|
|
2008-05-06 17:24:26 +00:00
|
|
|
O << Indent1 << "const char* OutputLanguage() const {\n"
|
2008-03-23 08:57:20 +00:00
|
|
|
<< Indent2 << "return \"" << P.OutLanguage << "\";\n"
|
|
|
|
<< Indent1 << "}\n\n";
|
|
|
|
}
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// EmitOutputSuffixMethod - Emit the OutputSuffix() method for a
|
|
|
|
/// given Tool class.
|
2008-03-23 08:57:20 +00:00
|
|
|
void EmitOutputSuffixMethod (const ToolProperties& P, std::ostream& O) {
|
2008-05-06 17:24:26 +00:00
|
|
|
O << Indent1 << "const char* OutputSuffix() const {\n"
|
2008-05-30 06:13:02 +00:00
|
|
|
<< Indent2 << "const char* ret = \"" << P.OutputSuffix << "\";\n";
|
|
|
|
|
|
|
|
for (ToolOptionDescriptions::const_iterator B = P.OptDescs.begin(),
|
|
|
|
E = P.OptDescs.end(); B != E; ++B) {
|
|
|
|
const ToolOptionDescription& OptDesc = B->second;
|
|
|
|
for (OptionPropertyList::const_iterator B = OptDesc.Props.begin(),
|
|
|
|
E = OptDesc.Props.end(); B != E; ++B) {
|
|
|
|
const OptionProperty& OptProp = *B;
|
|
|
|
if (OptProp.first == OptionPropertyType::OutputSuffix) {
|
|
|
|
O << Indent2 << "if (" << OptDesc.GenVariableName() << ")\n"
|
|
|
|
<< Indent3 << "ret = \"" << OptProp.second << "\";\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
O << Indent2 << "return ret;\n"
|
2008-03-23 08:57:20 +00:00
|
|
|
<< Indent1 << "}\n\n";
|
|
|
|
}
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// EmitNameMethod - Emit the Name() method for a given Tool class.
|
2008-03-23 08:57:20 +00:00
|
|
|
void EmitNameMethod (const ToolProperties& P, std::ostream& O) {
|
2008-05-06 17:24:26 +00:00
|
|
|
O << Indent1 << "const char* Name() const {\n"
|
2008-03-23 08:57:20 +00:00
|
|
|
<< Indent2 << "return \"" << P.Name << "\";\n"
|
|
|
|
<< Indent1 << "}\n\n";
|
|
|
|
}
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// EmitIsJoinMethod - Emit the IsJoin() method for a given Tool
|
|
|
|
/// class.
|
2008-03-23 08:57:20 +00:00
|
|
|
void EmitIsJoinMethod (const ToolProperties& P, std::ostream& O) {
|
|
|
|
O << Indent1 << "bool IsJoin() const {\n";
|
|
|
|
if (P.isJoin())
|
|
|
|
O << Indent2 << "return true;\n";
|
|
|
|
else
|
|
|
|
O << Indent2 << "return false;\n";
|
|
|
|
O << Indent1 << "}\n\n";
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:24:49 +00:00
|
|
|
/// EmitStaticMemberDefinitions - Emit static member definitions for a
|
|
|
|
/// given Tool class.
|
|
|
|
void EmitStaticMemberDefinitions(const ToolProperties& P, std::ostream& O) {
|
|
|
|
O << "const char* " << P.Name << "::InputLanguages_[] = {";
|
|
|
|
for (StrVector::const_iterator B = P.InLanguage.begin(),
|
|
|
|
E = P.InLanguage.end(); B != E; ++B)
|
|
|
|
O << '\"' << *B << "\", ";
|
|
|
|
O << "0};\n\n";
|
|
|
|
}
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// EmitToolClassDefinition - Emit a Tool class definition.
|
2008-05-30 06:10:19 +00:00
|
|
|
void EmitToolClassDefinition (const ToolProperties& P,
|
|
|
|
const GlobalOptionDescriptions& OptDescs,
|
|
|
|
std::ostream& O) {
|
|
|
|
if (P.Name == "root")
|
2008-05-06 16:35:25 +00:00
|
|
|
return;
|
|
|
|
|
2008-03-23 08:57:20 +00:00
|
|
|
// Header
|
2008-05-06 17:26:53 +00:00
|
|
|
O << "class " << P.Name << " : public ";
|
|
|
|
if (P.isJoin())
|
|
|
|
O << "JoinTool";
|
|
|
|
else
|
|
|
|
O << "Tool";
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-30 06:24:49 +00:00
|
|
|
O << "{\nprivate:\n"
|
|
|
|
<< Indent1 << "static const char* InputLanguages_[];\n\n";
|
|
|
|
|
|
|
|
O << "public:\n";
|
2008-03-23 08:57:20 +00:00
|
|
|
EmitNameMethod(P, O);
|
|
|
|
EmitInOutLanguageMethods(P, O);
|
|
|
|
EmitOutputSuffixMethod(P, O);
|
|
|
|
EmitIsJoinMethod(P, O);
|
2008-05-30 06:10:19 +00:00
|
|
|
EmitGenerateActionMethods(P, OptDescs, O);
|
2008-03-23 08:57:20 +00:00
|
|
|
EmitIsLastMethod(P, O);
|
|
|
|
|
|
|
|
// Close class definition
|
2008-05-30 06:24:49 +00:00
|
|
|
O << "};\n";
|
|
|
|
|
|
|
|
EmitStaticMemberDefinitions(P, O);
|
|
|
|
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// EmitOptionDescriptions - Iterate over a list of option
|
|
|
|
/// descriptions and emit registration code.
|
2008-03-23 08:57:20 +00:00
|
|
|
void EmitOptionDescriptions (const GlobalOptionDescriptions& descs,
|
|
|
|
std::ostream& O)
|
|
|
|
{
|
2008-05-30 06:22:52 +00:00
|
|
|
std::vector<GlobalOptionDescription> Aliases;
|
|
|
|
|
2008-05-30 06:23:29 +00:00
|
|
|
// Emit static cl::Option variables.
|
2008-03-23 08:57:20 +00:00
|
|
|
for (GlobalOptionDescriptions::const_iterator B = descs.begin(),
|
|
|
|
E = descs.end(); B!=E; ++B) {
|
|
|
|
const GlobalOptionDescription& val = B->second;
|
|
|
|
|
2008-05-30 06:22:52 +00:00
|
|
|
if (val.Type == OptionType::Alias) {
|
|
|
|
Aliases.push_back(val);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-03-23 08:57:20 +00:00
|
|
|
O << val.GenTypeDeclaration() << ' '
|
|
|
|
<< val.GenVariableName()
|
|
|
|
<< "(\"" << val.Name << '\"';
|
|
|
|
|
|
|
|
if (val.Type == OptionType::Prefix || val.Type == OptionType::PrefixList)
|
|
|
|
O << ", cl::Prefix";
|
|
|
|
|
|
|
|
if (val.isRequired()) {
|
|
|
|
switch (val.Type) {
|
|
|
|
case OptionType::PrefixList:
|
|
|
|
case OptionType::ParameterList:
|
|
|
|
O << ", cl::OneOrMore";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
O << ", cl::Required";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:22:52 +00:00
|
|
|
if (!val.Help.empty())
|
|
|
|
O << ", cl::desc(\"" << val.Help << "\")";
|
|
|
|
|
|
|
|
O << ");\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the aliases (they should go after all the 'proper' options).
|
|
|
|
for (std::vector<GlobalOptionDescription>::const_iterator
|
|
|
|
B = Aliases.begin(), E = Aliases.end(); B != E; ++B) {
|
|
|
|
const GlobalOptionDescription& val = *B;
|
|
|
|
|
|
|
|
O << val.GenTypeDeclaration() << ' '
|
|
|
|
<< val.GenVariableName()
|
|
|
|
<< "(\"" << val.Name << '\"';
|
|
|
|
|
|
|
|
GlobalOptionDescriptions::container_type
|
|
|
|
::const_iterator F = descs.Descriptions.find(val.Help);
|
|
|
|
if (F != descs.Descriptions.end())
|
|
|
|
O << ", cl::aliasopt(" << F->second.GenVariableName() << ")";
|
|
|
|
else
|
|
|
|
throw val.Name + ": alias to an unknown option!";
|
|
|
|
|
|
|
|
O << ", cl::desc(\"" << "An alias for -" + val.Help << "\"));\n";
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 06:22:52 +00:00
|
|
|
// Emit the sink option.
|
2008-03-23 08:57:20 +00:00
|
|
|
if (descs.HasSink)
|
|
|
|
O << "cl::list<std::string> " << SinkOptionName << "(cl::Sink);\n";
|
|
|
|
|
|
|
|
O << '\n';
|
|
|
|
}
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// EmitPopulateLanguageMap - Emit the PopulateLanguageMap() function.
|
2008-03-23 08:57:20 +00:00
|
|
|
void EmitPopulateLanguageMap (const RecordKeeper& Records, std::ostream& O)
|
|
|
|
{
|
|
|
|
// Get the relevant field out of RecordKeeper
|
|
|
|
Record* LangMapRecord = Records.getDef("LanguageMap");
|
|
|
|
if (!LangMapRecord)
|
|
|
|
throw std::string("Language map definition not found!");
|
|
|
|
|
|
|
|
ListInit* LangsToSuffixesList = LangMapRecord->getValueAsListInit("map");
|
|
|
|
if (!LangsToSuffixesList)
|
|
|
|
throw std::string("Error in the language map definition!");
|
|
|
|
|
|
|
|
// Generate code
|
2008-09-22 20:49:34 +00:00
|
|
|
O << "namespace {\n\n";
|
|
|
|
O << "void PopulateLanguageMapLocal(LanguageMap& langMap) {\n";
|
2008-03-23 08:57:20 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < LangsToSuffixesList->size(); ++i) {
|
|
|
|
Record* LangToSuffixes = LangsToSuffixesList->getElementAsRecord(i);
|
|
|
|
|
|
|
|
const std::string& Lang = LangToSuffixes->getValueAsString("lang");
|
|
|
|
const ListInit* Suffixes = LangToSuffixes->getValueAsListInit("suffixes");
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < Suffixes->size(); ++i)
|
2008-09-22 20:47:46 +00:00
|
|
|
O << Indent1 << "langMap[\""
|
2008-03-23 08:57:20 +00:00
|
|
|
<< InitPtrToString(Suffixes->getElement(i))
|
|
|
|
<< "\"] = \"" << Lang << "\";\n";
|
|
|
|
}
|
|
|
|
|
2008-09-22 20:49:34 +00:00
|
|
|
O << "}\n\n}\n\n";
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// FillInToolToLang - Fills in two tables that map tool names to
|
|
|
|
/// (input, output) languages. Used by the typechecker.
|
2008-05-06 16:36:06 +00:00
|
|
|
void FillInToolToLang (const ToolPropertiesList& TPList,
|
2008-05-30 06:18:16 +00:00
|
|
|
StringMap<StringSet<> >& ToolToInLang,
|
2008-05-06 16:36:06 +00:00
|
|
|
StringMap<std::string>& ToolToOutLang) {
|
|
|
|
for (ToolPropertiesList::const_iterator B = TPList.begin(), E = TPList.end();
|
|
|
|
B != E; ++B) {
|
|
|
|
const ToolProperties& P = *(*B);
|
2008-05-30 06:18:16 +00:00
|
|
|
for (StrVector::const_iterator B = P.InLanguage.begin(),
|
|
|
|
E = P.InLanguage.end(); B != E; ++B)
|
|
|
|
ToolToInLang[P.Name].insert(*B);
|
2008-05-06 16:36:06 +00:00
|
|
|
ToolToOutLang[P.Name] = P.OutLanguage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// TypecheckGraph - Check that names for output and input languages
|
|
|
|
/// on all edges do match.
|
2008-05-30 06:18:16 +00:00
|
|
|
// TOFIX: It would be nice if this function also checked for cycles
|
|
|
|
// and multiple default edges in the graph (better error
|
|
|
|
// reporting). Unfortunately, it is awkward to do right now because
|
|
|
|
// our intermediate representation is not sufficiently
|
2008-09-22 20:45:17 +00:00
|
|
|
// sophisticated. Algorithms like these require a real graph instead of
|
|
|
|
// an AST.
|
2008-05-06 16:36:06 +00:00
|
|
|
void TypecheckGraph (Record* CompilationGraph,
|
|
|
|
const ToolPropertiesList& TPList) {
|
2008-05-30 06:18:16 +00:00
|
|
|
StringMap<StringSet<> > ToolToInLang;
|
2008-05-06 16:36:06 +00:00
|
|
|
StringMap<std::string> ToolToOutLang;
|
|
|
|
|
|
|
|
FillInToolToLang(TPList, ToolToInLang, ToolToOutLang);
|
|
|
|
ListInit* edges = CompilationGraph->getValueAsListInit("edges");
|
2008-05-30 06:18:16 +00:00
|
|
|
StringMap<std::string>::iterator IAE = ToolToOutLang.end();
|
|
|
|
StringMap<StringSet<> >::iterator IBE = ToolToInLang.end();
|
2008-05-06 16:36:06 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < edges->size(); ++i) {
|
|
|
|
Record* Edge = edges->getElementAsRecord(i);
|
|
|
|
Record* A = Edge->getValueAsDef("a");
|
|
|
|
Record* B = Edge->getValueAsDef("b");
|
|
|
|
StringMap<std::string>::iterator IA = ToolToOutLang.find(A->getName());
|
2008-05-30 06:18:16 +00:00
|
|
|
StringMap<StringSet<> >::iterator IB = ToolToInLang.find(B->getName());
|
2008-05-30 06:10:19 +00:00
|
|
|
if (IA == IAE)
|
2008-05-06 16:36:06 +00:00
|
|
|
throw A->getName() + ": no such tool!";
|
2008-05-30 06:10:19 +00:00
|
|
|
if (IB == IBE)
|
2008-05-06 16:36:06 +00:00
|
|
|
throw B->getName() + ": no such tool!";
|
2008-05-30 06:18:16 +00:00
|
|
|
if (A->getName() != "root" && IB->second.count(IA->second) == 0)
|
2008-05-06 16:36:06 +00:00
|
|
|
throw "Edge " + A->getName() + "->" + B->getName()
|
|
|
|
+ ": output->input language mismatch";
|
2008-05-30 06:10:19 +00:00
|
|
|
if (B->getName() == "root")
|
2008-05-06 16:36:50 +00:00
|
|
|
throw std::string("Edges back to the root are not allowed!");
|
2008-05-06 16:36:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:12:24 +00:00
|
|
|
/// IncDecWeight - Helper function passed to EmitCaseConstructHandler()
|
|
|
|
/// by EmitEdgeClass().
|
2008-05-30 06:10:19 +00:00
|
|
|
void IncDecWeight (const Init* i, const char* IndentLevel,
|
|
|
|
std::ostream& O) {
|
2008-05-30 06:18:16 +00:00
|
|
|
const DagInit& d = InitPtrToDag(i);
|
2008-05-30 06:08:50 +00:00
|
|
|
const std::string& OpName = d.getOperator()->getAsString();
|
|
|
|
|
|
|
|
if (OpName == "inc_weight")
|
|
|
|
O << IndentLevel << Indent1 << "ret += ";
|
|
|
|
else if (OpName == "dec_weight")
|
|
|
|
O << IndentLevel << Indent1 << "ret -= ";
|
|
|
|
else
|
|
|
|
throw "Unknown operator in edge properties list: " + OpName + '!';
|
|
|
|
|
|
|
|
if (d.getNumArgs() > 0)
|
|
|
|
O << InitPtrToInt(d.getArg(0)) << ";\n";
|
|
|
|
else
|
|
|
|
O << "2;\n";
|
|
|
|
|
2008-05-06 18:18:20 +00:00
|
|
|
}
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// EmitEdgeClass - Emit a single Edge# class.
|
2008-05-30 06:10:19 +00:00
|
|
|
void EmitEdgeClass (unsigned N, const std::string& Target,
|
|
|
|
DagInit* Case, const GlobalOptionDescriptions& OptDescs,
|
|
|
|
std::ostream& O) {
|
2008-05-06 17:23:14 +00:00
|
|
|
|
|
|
|
// Class constructor.
|
|
|
|
O << "class Edge" << N << ": public Edge {\n"
|
|
|
|
<< "public:\n"
|
|
|
|
<< Indent1 << "Edge" << N << "() : Edge(\"" << Target
|
|
|
|
<< "\") {}\n\n"
|
|
|
|
|
2008-05-06 18:14:24 +00:00
|
|
|
// Function Weight().
|
2008-05-06 18:15:12 +00:00
|
|
|
<< Indent1 << "unsigned Weight(const InputLanguagesSet& InLangs) const {\n"
|
2008-05-06 18:14:24 +00:00
|
|
|
<< Indent2 << "unsigned ret = 0;\n";
|
2008-05-06 17:23:14 +00:00
|
|
|
|
2008-05-30 06:08:50 +00:00
|
|
|
// Handle the 'case' construct.
|
2008-05-30 06:22:15 +00:00
|
|
|
EmitCaseConstructHandler(Case, Indent2, IncDecWeight, false, OptDescs, O);
|
2008-05-06 18:14:24 +00:00
|
|
|
|
|
|
|
O << Indent2 << "return ret;\n"
|
|
|
|
<< Indent1 << "};\n\n};\n\n";
|
2008-05-06 17:23:14 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 06:12:24 +00:00
|
|
|
/// EmitEdgeClasses - Emit Edge* classes that represent graph edges.
|
2008-05-06 16:36:06 +00:00
|
|
|
void EmitEdgeClasses (Record* CompilationGraph,
|
|
|
|
const GlobalOptionDescriptions& OptDescs,
|
|
|
|
std::ostream& O) {
|
|
|
|
ListInit* edges = CompilationGraph->getValueAsListInit("edges");
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < edges->size(); ++i) {
|
2008-05-06 16:36:50 +00:00
|
|
|
Record* Edge = edges->getElementAsRecord(i);
|
|
|
|
Record* B = Edge->getValueAsDef("b");
|
2008-05-30 06:08:50 +00:00
|
|
|
DagInit* Weight = Edge->getValueAsDag("weight");
|
2008-05-06 16:36:50 +00:00
|
|
|
|
2008-05-30 06:08:50 +00:00
|
|
|
if (isDagEmpty(Weight))
|
2008-05-06 16:36:50 +00:00
|
|
|
continue;
|
|
|
|
|
2008-05-30 06:08:50 +00:00
|
|
|
EmitEdgeClass(i, B->getName(), Weight, OptDescs, O);
|
2008-05-06 16:36:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// EmitPopulateCompilationGraph - Emit the PopulateCompilationGraph()
|
|
|
|
/// function.
|
2008-05-06 16:36:06 +00:00
|
|
|
void EmitPopulateCompilationGraph (Record* CompilationGraph,
|
2008-03-23 08:57:20 +00:00
|
|
|
std::ostream& O)
|
|
|
|
{
|
2008-05-06 16:35:25 +00:00
|
|
|
ListInit* edges = CompilationGraph->getValueAsListInit("edges");
|
2008-03-23 08:57:20 +00:00
|
|
|
|
|
|
|
// Generate code
|
2008-09-22 20:49:34 +00:00
|
|
|
O << "namespace {\n\n";
|
|
|
|
O << "void PopulateCompilationGraphLocal(CompilationGraph& G) {\n";
|
2008-05-06 16:35:25 +00:00
|
|
|
|
|
|
|
// Insert vertices
|
|
|
|
|
|
|
|
RecordVector Tools = Records.getAllDerivedDefinitions("Tool");
|
|
|
|
if (Tools.empty())
|
|
|
|
throw std::string("No tool definitions found!");
|
|
|
|
|
|
|
|
for (RecordVector::iterator B = Tools.begin(), E = Tools.end(); B != E; ++B) {
|
|
|
|
const std::string& Name = (*B)->getName();
|
2008-05-30 06:10:19 +00:00
|
|
|
if (Name != "root")
|
2008-05-06 16:36:06 +00:00
|
|
|
O << Indent1 << "G.insertNode(new "
|
|
|
|
<< Name << "());\n";
|
2008-05-06 16:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
O << '\n';
|
|
|
|
|
|
|
|
// Insert edges
|
|
|
|
for (unsigned i = 0; i < edges->size(); ++i) {
|
|
|
|
Record* Edge = edges->getElementAsRecord(i);
|
|
|
|
Record* A = Edge->getValueAsDef("a");
|
|
|
|
Record* B = Edge->getValueAsDef("b");
|
2008-05-30 06:08:50 +00:00
|
|
|
DagInit* Weight = Edge->getValueAsDag("weight");
|
2008-05-06 16:36:50 +00:00
|
|
|
|
|
|
|
O << Indent1 << "G.insertEdge(\"" << A->getName() << "\", ";
|
|
|
|
|
2008-05-30 06:08:50 +00:00
|
|
|
if (isDagEmpty(Weight))
|
2008-05-06 16:36:50 +00:00
|
|
|
O << "new SimpleEdge(\"" << B->getName() << "\")";
|
|
|
|
else
|
|
|
|
O << "new Edge" << i << "()";
|
|
|
|
|
|
|
|
O << ");\n";
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2008-09-22 20:49:34 +00:00
|
|
|
O << "}\n\n}\n\n";
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 06:12:24 +00:00
|
|
|
/// ExtractHookNames - Extract the hook names from all instances of
|
|
|
|
/// $CALL(HookName) in the provided command line string. Helper
|
|
|
|
/// function used by FillInHookNames().
|
|
|
|
void ExtractHookNames(const Init* CmdLine, StrVector& HookNames) {
|
|
|
|
StrVector cmds;
|
2008-05-30 06:13:29 +00:00
|
|
|
llvm::SplitString(InitPtrToString(CmdLine), cmds);
|
2008-05-30 06:12:24 +00:00
|
|
|
for (StrVector::const_iterator B = cmds.begin(), E = cmds.end();
|
|
|
|
B != E; ++B) {
|
|
|
|
const std::string& cmd = *B;
|
|
|
|
if (cmd.find("$CALL(") == 0) {
|
|
|
|
if (cmd.size() == 6)
|
|
|
|
throw std::string("$CALL invocation: empty argument list!");
|
2008-05-30 06:13:29 +00:00
|
|
|
HookNames.push_back(std::string(cmd.begin() + 6,
|
|
|
|
cmd.begin() + cmd.find(")")));
|
2008-05-30 06:12:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:22:15 +00:00
|
|
|
/// ExtractHookNamesFromCaseConstruct - Extract hook names from the
|
|
|
|
/// 'case' expression, handle nesting. Helper function used by
|
|
|
|
/// FillInHookNames().
|
|
|
|
void ExtractHookNamesFromCaseConstruct(Init* Case, StrVector& HookNames) {
|
|
|
|
const DagInit& d = InitPtrToDag(Case);
|
|
|
|
bool even = false;
|
|
|
|
for (DagInit::const_arg_iterator B = d.arg_begin(), E = d.arg_end();
|
|
|
|
B != E; ++B) {
|
|
|
|
Init* arg = *B;
|
|
|
|
if (even && dynamic_cast<DagInit*>(arg)
|
|
|
|
&& static_cast<DagInit*>(arg)->getOperator()->getAsString() == "case")
|
|
|
|
ExtractHookNamesFromCaseConstruct(arg, HookNames);
|
|
|
|
else if (even)
|
|
|
|
ExtractHookNames(arg, HookNames);
|
|
|
|
even = !even;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 06:12:24 +00:00
|
|
|
/// FillInHookNames - Actually extract the hook names from all command
|
|
|
|
/// line strings. Helper function used by EmitHookDeclarations().
|
|
|
|
void FillInHookNames(const ToolPropertiesList& TPList,
|
|
|
|
StrVector& HookNames) {
|
2008-05-30 06:22:15 +00:00
|
|
|
// For all command lines:
|
2008-05-30 06:12:24 +00:00
|
|
|
for (ToolPropertiesList::const_iterator B = TPList.begin(),
|
|
|
|
E = TPList.end(); B != E; ++B) {
|
|
|
|
const ToolProperties& P = *(*B);
|
|
|
|
if (!P.CmdLine)
|
|
|
|
continue;
|
2008-05-30 06:22:15 +00:00
|
|
|
if (dynamic_cast<StringInit*>(P.CmdLine))
|
2008-05-30 06:12:24 +00:00
|
|
|
// This is a string.
|
|
|
|
ExtractHookNames(P.CmdLine, HookNames);
|
2008-05-30 06:22:15 +00:00
|
|
|
else
|
2008-05-30 06:12:24 +00:00
|
|
|
// This is a 'case' construct.
|
2008-05-30 06:22:15 +00:00
|
|
|
ExtractHookNamesFromCaseConstruct(P.CmdLine, HookNames);
|
2008-05-30 06:12:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitHookDeclarations - Parse CmdLine fields of all the tool
|
|
|
|
/// property records and emit hook function declaration for each
|
|
|
|
/// instance of $CALL(HookName).
|
|
|
|
void EmitHookDeclarations(const ToolPropertiesList& ToolProps,
|
|
|
|
std::ostream& O) {
|
|
|
|
StrVector HookNames;
|
|
|
|
FillInHookNames(ToolProps, HookNames);
|
|
|
|
if (HookNames.empty())
|
|
|
|
return;
|
|
|
|
std::sort(HookNames.begin(), HookNames.end());
|
|
|
|
StrVector::const_iterator E = std::unique(HookNames.begin(), HookNames.end());
|
|
|
|
|
|
|
|
O << "namespace hooks {\n";
|
|
|
|
for (StrVector::const_iterator B = HookNames.begin(); B != E; ++B)
|
|
|
|
O << Indent1 << "std::string " << *B << "();\n";
|
|
|
|
|
|
|
|
O << "}\n\n";
|
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-09-22 20:49:34 +00:00
|
|
|
/// EmitRegisterPlugin - Emit code to register this plugin.
|
|
|
|
void EmitRegisterPlugin(std::ostream& O) {
|
|
|
|
O << "namespace {\n\n"
|
|
|
|
<< "struct Plugin : public llvmc::BasePlugin {\n"
|
|
|
|
<< Indent1 << "void PopulateLanguageMap(LanguageMap& langMap) const\n"
|
|
|
|
<< Indent1 << "{ PopulateLanguageMapLocal(langMap); }\n\n"
|
|
|
|
<< Indent1
|
|
|
|
<< "void PopulateCompilationGraph(CompilationGraph& graph) const\n"
|
|
|
|
<< Indent1 << "{ PopulateCompilationGraphLocal(graph); }\n"
|
|
|
|
<< "};\n\n"
|
|
|
|
|
|
|
|
<< "static llvmc::RegisterPlugin<Plugin> RP;\n\n}\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitInclude - Emit necessary #include directives.
|
|
|
|
void EmitIncludes(std::ostream& O) {
|
2008-09-22 20:50:40 +00:00
|
|
|
O << "#include \"llvm/CompilerDriver/CompilationGraph.h\"\n"
|
|
|
|
<< "#include \"llvm/CompilerDriver/Plugin.h\"\n"
|
|
|
|
<< "#include \"llvm/CompilerDriver/Tool.h\"\n\n"
|
2008-09-22 20:49:34 +00:00
|
|
|
|
|
|
|
<< "#include \"llvm/ADT/StringExtras.h\"\n"
|
|
|
|
<< "#include \"llvm/Support/CommandLine.h\"\n\n"
|
|
|
|
|
|
|
|
<< "#include <cstdlib>\n"
|
|
|
|
<< "#include <stdexcept>\n\n"
|
|
|
|
|
|
|
|
<< "using namespace llvm;\n"
|
|
|
|
<< "using namespace llvmc;\n\n"
|
|
|
|
|
|
|
|
<< "extern cl::opt<std::string> OutputFilename;\n\n";
|
|
|
|
}
|
|
|
|
|
2008-03-23 08:57:20 +00:00
|
|
|
// End of anonymous namespace
|
2008-05-06 18:12:03 +00:00
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
/// run - The back-end entry point.
|
2008-05-06 18:12:03 +00:00
|
|
|
void LLVMCConfigurationEmitter::run (std::ostream &O) {
|
2008-05-30 06:21:48 +00:00
|
|
|
try {
|
2008-05-07 21:50:19 +00:00
|
|
|
|
|
|
|
// Emit file header.
|
2008-05-06 18:08:59 +00:00
|
|
|
EmitSourceFileHeader("LLVMC Configuration Library", O);
|
2008-09-22 20:49:34 +00:00
|
|
|
EmitIncludes(O);
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
// Get a list of all defined Tools.
|
2008-03-23 08:57:20 +00:00
|
|
|
RecordVector Tools = Records.getAllDerivedDefinitions("Tool");
|
|
|
|
if (Tools.empty())
|
|
|
|
throw std::string("No tool definitions found!");
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
// Gather information from the Tool description dags.
|
2008-03-23 08:57:20 +00:00
|
|
|
ToolPropertiesList tool_props;
|
|
|
|
GlobalOptionDescriptions opt_descs;
|
|
|
|
CollectToolProperties(Tools.begin(), Tools.end(), tool_props, opt_descs);
|
|
|
|
|
2008-05-30 06:26:08 +00:00
|
|
|
RecordVector OptionLists = Records.getAllDerivedDefinitions("OptionList");
|
|
|
|
CollectPropertiesFromOptionList(OptionLists.begin(), OptionLists.end(),
|
|
|
|
opt_descs);
|
|
|
|
|
2008-05-30 06:28:37 +00:00
|
|
|
// Check that there are no options without side effects (specified
|
|
|
|
// only in the OptionList).
|
|
|
|
CheckForSuperfluousOptions(tool_props, opt_descs);
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
// Emit global option registration code.
|
2008-03-23 08:57:20 +00:00
|
|
|
EmitOptionDescriptions(opt_descs, O);
|
|
|
|
|
2008-05-30 06:12:24 +00:00
|
|
|
// Emit hook declarations.
|
|
|
|
EmitHookDeclarations(tool_props, O);
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
// Emit PopulateLanguageMap() function
|
|
|
|
// (a language map maps from file extensions to language names).
|
2008-03-23 08:57:20 +00:00
|
|
|
EmitPopulateLanguageMap(Records, O);
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
// Emit Tool classes.
|
2008-03-23 08:57:20 +00:00
|
|
|
for (ToolPropertiesList::const_iterator B = tool_props.begin(),
|
|
|
|
E = tool_props.end(); B!=E; ++B)
|
2008-05-30 06:10:19 +00:00
|
|
|
EmitToolClassDefinition(*(*B), opt_descs, O);
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-06 16:36:06 +00:00
|
|
|
Record* CompilationGraphRecord = Records.getDef("CompilationGraph");
|
|
|
|
if (!CompilationGraphRecord)
|
|
|
|
throw std::string("Compilation graph description not found!");
|
|
|
|
|
|
|
|
// Typecheck the compilation graph.
|
|
|
|
TypecheckGraph(CompilationGraphRecord, tool_props);
|
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
// Emit Edge# classes.
|
2008-05-06 16:36:06 +00:00
|
|
|
EmitEdgeClasses(CompilationGraphRecord, opt_descs, O);
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-07 21:50:19 +00:00
|
|
|
// Emit PopulateCompilationGraph() function.
|
2008-05-06 16:36:06 +00:00
|
|
|
EmitPopulateCompilationGraph(CompilationGraphRecord, O);
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-09-22 20:49:34 +00:00
|
|
|
// Emit code for plugin registration.
|
|
|
|
EmitRegisterPlugin(O);
|
|
|
|
|
2008-03-23 08:57:20 +00:00
|
|
|
// EOF
|
2008-05-30 06:21:48 +00:00
|
|
|
} catch (std::exception& Error) {
|
|
|
|
throw Error.what() + std::string(" - usually this means a syntax error.");
|
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|