//===- LLVMCConfigurationEmitter.cpp - Generate LLVMCC config -------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open
// Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This tablegen backend is responsible for emitting LLVMCC configuration code.
//
//===----------------------------------------------------------------------===//
#include "LLVMCCConfigurationEmitter.h"
#include "Record.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/Streams.h"
#include <algorithm>
#include <cassert>
#include <functional>
#include <string>
using namespace llvm;
//namespace {
//===----------------------------------------------------------------------===//
/// Typedefs
typedef std::vector<Record*> RecordVector;
typedef std::vector<std::string> StrVector;
//===----------------------------------------------------------------------===//
/// Constants
// Indentation strings
const char * Indent1 = " ";
const char * Indent2 = " ";
const char * Indent3 = " ";
const char * Indent4 = " ";
// Default help string
const char * DefaultHelpString = "NO HELP MESSAGE PROVIDED";
// Name for the "sink" option
const char * SinkOptionName = "AutoGeneratedSinkOption";
//===----------------------------------------------------------------------===//
/// Helper functions
std::string InitPtrToString(Init* ptr) {
StringInit& val = dynamic_cast<StringInit&>(*ptr);
return val.getValue();
}
// Ensure that the number of args in d is <= min_arguments,
// throw exception otherwise
void checkNumberOfArguments (const DagInit* d, unsigned min_arguments) {
if (d->getNumArgs() < min_arguments)
throw "Property " + d->getOperator()->getAsString()
+ " has too few arguments!";
}
//===----------------------------------------------------------------------===//
/// Back-end specific code
// A command-line option can have one of the following types:
//
// Switch - a simple switch w/o arguments, e.g. -O2
//
// 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
// allowed
namespace OptionType {
enum OptionType { Switch, Parameter, ParameterList, Prefix, PrefixList};
}
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.
// Base class for option descriptions
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) {
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>";
}
}
std::string GenVariableName() const {
switch (Type) {
case OptionType::Switch:
return "AutoGeneratedSwitch" + Name;
case OptionType::Prefix:
return "AutoGeneratedPrefix" + Name;
case OptionType::PrefixList:
return "AutoGeneratedPrefixList" + Name;
case OptionType::Parameter: