1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===-- CPPBackend.cpp - Library for converting LLVM code to C++ code -----===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file implements the writing of the LLVM IR as a set of C++ calls to the
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// LLVM IR interface. The input module is assumed to be verified.
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "CPPTargetMachine.h"
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CallingConv.h"
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Constants.h"
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/DerivedTypes.h"
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/InlineAsm.h"
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Instruction.h"
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Instructions.h"
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Module.h"
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Pass.h"
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/PassManager.h"
2519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/MC/MCAsmInfo.h"
2619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/MC/MCInstrInfo.h"
2719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/MC/MCSubtargetInfo.h"
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/SmallPtrSet.h"
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/CommandLine.h"
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/ErrorHandling.h"
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/FormattedStream.h"
3219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/Support/TargetRegistry.h"
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/StringExtras.h"
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Config/config.h"
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <algorithm>
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <set>
3719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include <map>
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanusing namespace llvm;
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic cl::opt<std::string>
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanFuncName("cppfname", cl::desc("Specify the name of the generated function"),
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         cl::value_desc("function name"));
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanenum WhatToGenerate {
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  GenProgram,
46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  GenModule,
47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  GenContents,
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  GenFunction,
49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  GenFunctions,
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  GenInline,
51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  GenVariable,
52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  GenType
53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic cl::opt<WhatToGenerate> GenerationType("cppgen", cl::Optional,
56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  cl::desc("Choose what kind of output to generate"),
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  cl::init(GenProgram),
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  cl::values(
59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    clEnumValN(GenProgram,  "program",   "Generate a complete program"),
60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    clEnumValN(GenModule,   "module",    "Generate a module definition"),
61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    clEnumValN(GenContents, "contents",  "Generate contents of a module"),
62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    clEnumValN(GenFunction, "function",  "Generate a function definition"),
63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    clEnumValN(GenFunctions,"functions", "Generate all function definitions"),
64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    clEnumValN(GenInline,   "inline",    "Generate an inline function"),
65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    clEnumValN(GenVariable, "variable",  "Generate a variable definition"),
66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    clEnumValN(GenType,     "type",      "Generate a type definition"),
67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    clEnumValEnd
68894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  )
69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman);
70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic cl::opt<std::string> NameToGenerate("cppfor", cl::Optional,
72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  cl::desc("Specify the name of the thing to generate"),
73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  cl::init("!bad!"));
74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanextern "C" void LLVMInitializeCppBackendTarget() {
76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Register the target.
77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  RegisterTargetMachine<CPPTargetMachine> X(TheCppBackendTarget);
78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace {
8119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  typedef std::vector<Type*> TypeList;
8219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  typedef std::map<Type*,std::string> TypeMap;
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef std::map<const Value*,std::string> ValueMap;
84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef std::set<std::string> NameSet;
8519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  typedef std::set<Type*> TypeSet;
86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef std::set<const Value*> ValueSet;
87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef std::map<const Value*,std::string> ForwardRefMap;
88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// CppWriter - This class is the main chunk of code that converts an LLVM
90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// module to a C++ translation unit.
91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  class CppWriter : public ModulePass {
92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    formatted_raw_ostream &Out;
93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const Module *TheModule;
94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    uint64_t uniqueNum;
95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    TypeMap TypeNames;
96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ValueMap ValueNames;
97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    NameSet UsedNames;
98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    TypeSet DefinedTypes;
99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ValueSet DefinedValues;
100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ForwardRefMap ForwardRefs;
101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool is_inline;
102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned indent_level;
103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  public:
105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    static char ID;
106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    explicit CppWriter(formatted_raw_ostream &o) :
107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ModulePass(ID), Out(o), uniqueNum(0), is_inline(false), indent_level(0){}
108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    virtual const char *getPassName() const { return "C++ backend"; }
110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool runOnModule(Module &M);
112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printProgram(const std::string& fname, const std::string& modName );
114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printModule(const std::string& fname, const std::string& modName );
115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printContents(const std::string& fname, const std::string& modName );
116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printFunction(const std::string& fname, const std::string& funcName );
117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printFunctions();
118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printInline(const std::string& fname, const std::string& funcName );
119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printVariable(const std::string& fname, const std::string& varName );
120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printType(const std::string& fname, const std::string& typeName );
121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void error(const std::string& msg);
123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    formatted_raw_ostream& nl(formatted_raw_ostream &Out, int delta = 0);
126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    inline void in() { indent_level++; }
127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    inline void out() { if (indent_level >0) indent_level--; }
128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  private:
130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printLinkageType(GlobalValue::LinkageTypes LT);
131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printVisibilityType(GlobalValue::VisibilityTypes VisTypes);
132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printCallingConv(CallingConv::ID cc);
133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printEscapedString(const std::string& str);
134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printCFP(const ConstantFP* CFP);
135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    std::string getCppName(Type* val);
13719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    inline void printCppName(Type* val);
138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::string getCppName(const Value* val);
140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    inline void printCppName(const Value* val);
141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printAttributes(const AttrListPtr &PAL, const std::string &name);
14319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    void printType(Type* Ty);
144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printTypes(const Module* M);
145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printConstant(const Constant *CPV);
147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printConstants(const Module* M);
148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printVariableUses(const GlobalVariable *GV);
150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printVariableHead(const GlobalVariable *GV);
151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printVariableBody(const GlobalVariable *GV);
152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printFunctionUses(const Function *F);
154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printFunctionHead(const Function *F);
155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printFunctionBody(const Function *F);
156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printInstruction(const Instruction *I, const std::string& bbname);
15719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    std::string getOpName(const Value*);
158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void printModuleBody();
160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} // end anonymous namespace.
162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanformatted_raw_ostream &CppWriter::nl(formatted_raw_ostream &Out, int delta) {
164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << '\n';
165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (delta >= 0 || indent_level >= unsigned(-delta))
166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    indent_level += delta;
167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out.indent(indent_level);
168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Out;
169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic inline void sanitize(std::string &str) {
172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (size_t i = 0; i < str.length(); ++i)
173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!isalnum(str[i]) && str[i] != '_')
174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      str[i] = '_';
175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
17719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic std::string getTypePrefix(Type *Ty) {
178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Ty->getTypeID()) {
179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::VoidTyID:     return "void_";
180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::IntegerTyID:
181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return "int" + utostr(cast<IntegerType>(Ty)->getBitWidth()) + "_";
182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::FloatTyID:    return "float_";
183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::DoubleTyID:   return "double_";
184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::LabelTyID:    return "label_";
185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::FunctionTyID: return "func_";
186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::StructTyID:   return "struct_";
187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::ArrayTyID:    return "array_";
188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::PointerTyID:  return "ptr_";
189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::VectorTyID:   return "packed_";
190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:                 return "other_";
191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return "unknown_";
193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::error(const std::string& msg) {
196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  report_fatal_error(msg);
197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// printCFP - Print a floating point constant .. very carefully :)
200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This makes sure that conversion to/from floating yields the same binary
201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// result so that we don't lose precision.
202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printCFP(const ConstantFP *CFP) {
203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool ignored;
204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  APFloat APF = APFloat(CFP->getValueAPF());  // copy
205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (CFP->getType() == Type::getFloatTy(CFP->getContext()))
206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &ignored);
207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "ConstantFP::get(mod->getContext(), ";
208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "APFloat(";
209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if HAVE_PRINTF_A
210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  char Buffer[100];
211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  sprintf(Buffer, "%A", APF.convertToDouble());
212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if ((!strncmp(Buffer, "0x", 2) ||
213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       !strncmp(Buffer, "-0x", 3) ||
214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       !strncmp(Buffer, "+0x", 3)) &&
215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      APF.bitwiseIsEqual(APFloat(atof(Buffer)))) {
216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (CFP->getType() == Type::getDoubleTy(CFP->getContext()))
217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "BitsToDouble(" << Buffer << ")";
218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "BitsToFloat((float)" << Buffer << ")";
220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ")";
221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::string StrVal = ftostr(CFP->getValueAPF());
224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    while (StrVal[0] == ' ')
226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      StrVal.erase(StrVal.begin());
227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Check to make sure that the stringized number is not some string like
229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // "Inf" or NaN.  Check that the string matches the "[-+]?[0-9]" regex.
230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (((StrVal[0] >= '0' && StrVal[0] <= '9') ||
231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         ((StrVal[0] == '-' || StrVal[0] == '+') &&
232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          (StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        (CFP->isExactlyValue(atof(StrVal.c_str())))) {
234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (CFP->getType() == Type::getDoubleTy(CFP->getContext()))
235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out <<  StrVal;
236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else
237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << StrVal << "f";
238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else if (CFP->getType() == Type::getDoubleTy(CFP->getContext()))
239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "BitsToDouble(0x"
240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << utohexstr(CFP->getValueAPF().bitcastToAPInt().getZExtValue())
241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << "ULL) /* " << StrVal << " */";
242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "BitsToFloat(0x"
244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << utohexstr((uint32_t)CFP->getValueAPF().
245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      bitcastToAPInt().getZExtValue())
246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << "U) /* " << StrVal << " */";
247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ")";
248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if HAVE_PRINTF_A
249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << ")";
252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printCallingConv(CallingConv::ID cc){
255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Print the calling convention.
256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (cc) {
257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case CallingConv::C:     Out << "CallingConv::C"; break;
258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case CallingConv::Fast:  Out << "CallingConv::Fast"; break;
259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case CallingConv::Cold:  Out << "CallingConv::Cold"; break;
260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case CallingConv::FirstTargetCC: Out << "CallingConv::FirstTargetCC"; break;
261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:                 Out << cc; break;
262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) {
266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (LT) {
267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::InternalLinkage:
268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "GlobalValue::InternalLinkage"; break;
269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::PrivateLinkage:
270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "GlobalValue::PrivateLinkage"; break;
271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::LinkerPrivateLinkage:
272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "GlobalValue::LinkerPrivateLinkage"; break;
273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::LinkerPrivateWeakLinkage:
274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "GlobalValue::LinkerPrivateWeakLinkage"; break;
27519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
27619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Out << "GlobalValue::LinkerPrivateWeakDefAutoLinkage"; break;
277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::AvailableExternallyLinkage:
278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "GlobalValue::AvailableExternallyLinkage "; break;
279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::LinkOnceAnyLinkage:
280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "GlobalValue::LinkOnceAnyLinkage "; break;
281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::LinkOnceODRLinkage:
282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "GlobalValue::LinkOnceODRLinkage "; break;
283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::WeakAnyLinkage:
284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "GlobalValue::WeakAnyLinkage"; break;
285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::WeakODRLinkage:
286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "GlobalValue::WeakODRLinkage"; break;
287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::AppendingLinkage:
288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "GlobalValue::AppendingLinkage"; break;
289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::ExternalLinkage:
290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "GlobalValue::ExternalLinkage"; break;
291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::DLLImportLinkage:
292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "GlobalValue::DLLImportLinkage"; break;
293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::DLLExportLinkage:
294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "GlobalValue::DLLExportLinkage"; break;
295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::ExternalWeakLinkage:
296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "GlobalValue::ExternalWeakLinkage"; break;
297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::CommonLinkage:
298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "GlobalValue::CommonLinkage"; break;
299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printVisibilityType(GlobalValue::VisibilityTypes VisType) {
303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (VisType) {
304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: llvm_unreachable("Unknown GVar visibility");
305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::DefaultVisibility:
306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "GlobalValue::DefaultVisibility";
307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::HiddenVisibility:
309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "GlobalValue::HiddenVisibility";
310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::ProtectedVisibility:
312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "GlobalValue::ProtectedVisibility";
313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// printEscapedString - Print each character of the specified string, escaping
318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// it if it is not printable or if it is an escape char.
319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printEscapedString(const std::string &Str) {
320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = Str.size(); i != e; ++i) {
321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned char C = Str[i];
322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isprint(C) && C != '"' && C != '\\') {
323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << C;
324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "\\x"
326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << (char) ((C/16  < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
33219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstd::string CppWriter::getCppName(Type* Ty) {
333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // First, handle the primitive types .. easy
334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Ty->isPrimitiveType() || Ty->isIntegerTy()) {
335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (Ty->getTypeID()) {
336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Type::VoidTyID:   return "Type::getVoidTy(mod->getContext())";
337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Type::IntegerTyID: {
338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return "IntegerType::get(mod->getContext(), " + utostr(BitWidth) + ")";
340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Type::X86_FP80TyID: return "Type::getX86_FP80Ty(mod->getContext())";
342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Type::FloatTyID:    return "Type::getFloatTy(mod->getContext())";
343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Type::DoubleTyID:   return "Type::getDoubleTy(mod->getContext())";
344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Type::LabelTyID:    return "Type::getLabelTy(mod->getContext())";
34519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case Type::X86_MMXTyID:  return "Type::getX86_MMXTy(mod->getContext())";
346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    default:
347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      error("Invalid primitive type");
348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // shouldn't be returned, but make it sensible
351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return "Type::getVoidTy(mod->getContext())";
352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Now, see if we've seen the type before and return that
355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  TypeMap::iterator I = TypeNames.find(Ty);
356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (I != TypeNames.end())
357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return I->second;
358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Okay, let's build a new name for this type. Start with a prefix
360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const char* prefix = 0;
361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Ty->getTypeID()) {
362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::FunctionTyID:    prefix = "FuncTy_"; break;
363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::StructTyID:      prefix = "StructTy_"; break;
364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::ArrayTyID:       prefix = "ArrayTy_"; break;
365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::PointerTyID:     prefix = "PointerTy_"; break;
366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::VectorTyID:      prefix = "VectorTy_"; break;
367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:                    prefix = "OtherTy_"; break; // prevent breakage
368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // See if the type has a name in the symboltable and build accordingly
371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::string name;
37219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (StructType *STy = dyn_cast<StructType>(Ty))
37319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (STy->hasName())
37419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      name = STy->getName();
37519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
37619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (name.empty())
37719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    name = utostr(uniqueNum++);
37819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
37919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  name = std::string(prefix) + name;
380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  sanitize(name);
381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Save the name
383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return TypeNames[Ty] = name;
384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
38619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid CppWriter::printCppName(Type* Ty) {
387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printEscapedString(getCppName(Ty));
388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstd::string CppWriter::getCppName(const Value* val) {
391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::string name;
392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ValueMap::iterator I = ValueNames.find(val);
393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (I != ValueNames.end() && I->first == val)
394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return  I->second;
395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(val)) {
397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    name = std::string("gvar_") +
398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      getTypePrefix(GV->getType()->getElementType());
399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (isa<Function>(val)) {
400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    name = std::string("func_");
401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (const Constant* C = dyn_cast<Constant>(val)) {
402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    name = std::string("const_") + getTypePrefix(C->getType());
403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (const Argument* Arg = dyn_cast<Argument>(val)) {
404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (is_inline) {
405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned argNum = std::distance(Arg->getParent()->arg_begin(),
406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      Function::const_arg_iterator(Arg)) + 1;
407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      name = std::string("arg_") + utostr(argNum);
408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NameSet::iterator NI = UsedNames.find(name);
409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (NI != UsedNames.end())
410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        name += std::string("_") + utostr(uniqueNum++);
411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      UsedNames.insert(name);
412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return ValueNames[val] = name;
413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      name = getTypePrefix(val->getType());
415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    name = getTypePrefix(val->getType());
418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (val->hasName())
420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    name += val->getName();
421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    name += utostr(uniqueNum++);
423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  sanitize(name);
424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  NameSet::iterator NI = UsedNames.find(name);
425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (NI != UsedNames.end())
426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    name += std::string("_") + utostr(uniqueNum++);
427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  UsedNames.insert(name);
428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return ValueNames[val] = name;
429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printCppName(const Value* val) {
432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printEscapedString(getCppName(val));
433894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printAttributes(const AttrListPtr &PAL,
436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                const std::string &name) {
437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "AttrListPtr " << name << "_PAL;";
438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out);
439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!PAL.isEmpty()) {
440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << '{'; in(); nl(Out);
441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "SmallVector<AttributeWithIndex, 4> Attrs;"; nl(Out);
442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "AttributeWithIndex PAWI;"; nl(Out);
443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0; i < PAL.getNumSlots(); ++i) {
444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned index = PAL.getSlot(i).Index;
445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Attributes attrs = PAL.getSlot(i).Attrs;
446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "PAWI.Index = " << index << "U; PAWI.Attrs = 0 ";
447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define HANDLE_ATTR(X)                 \
448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (attrs & Attribute::X)      \
449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << " | Attribute::" #X;  \
450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      attrs &= ~Attribute::X;
451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HANDLE_ATTR(SExt);
453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HANDLE_ATTR(ZExt);
454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HANDLE_ATTR(NoReturn);
455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HANDLE_ATTR(InReg);
456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HANDLE_ATTR(StructRet);
457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HANDLE_ATTR(NoUnwind);
458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HANDLE_ATTR(NoAlias);
459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HANDLE_ATTR(ByVal);
460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HANDLE_ATTR(Nest);
461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HANDLE_ATTR(ReadNone);
462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HANDLE_ATTR(ReadOnly);
463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HANDLE_ATTR(NoInline);
464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HANDLE_ATTR(AlwaysInline);
465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HANDLE_ATTR(OptimizeForSize);
466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HANDLE_ATTR(StackProtect);
467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HANDLE_ATTR(StackProtectReq);
468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HANDLE_ATTR(NoCapture);
469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HANDLE_ATTR(NoRedZone);
470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HANDLE_ATTR(NoImplicitFloat);
471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HANDLE_ATTR(Naked);
472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HANDLE_ATTR(InlineHint);
47319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      HANDLE_ATTR(ReturnsTwice);
47419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      HANDLE_ATTR(UWTable);
47519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      HANDLE_ATTR(NonLazyBind);
476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#undef HANDLE_ATTR
477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (attrs & Attribute::StackAlignment)
478894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << " | Attribute::constructStackAlignmentFromInt("
479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            << Attribute::getStackAlignmentFromAttrs(attrs)
480894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            << ")";
481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      attrs &= ~Attribute::StackAlignment;
482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      assert(attrs == 0 && "Unhandled attribute!");
483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ";";
484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out);
485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "Attrs.push_back(PAWI);";
486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out);
487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << name << "_PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());";
489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    out(); nl(Out);
491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << '}'; nl(Out);
492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
49519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid CppWriter::printType(Type* Ty) {
496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // We don't print definitions for primitive types
497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Ty->isPrimitiveType() || Ty->isIntegerTy())
49819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return;
499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If we already defined this type, we don't need to define it again.
501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (DefinedTypes.find(Ty) != DefinedTypes.end())
50219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return;
503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Everything below needs the name for the type so get it now.
505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::string typeName(getCppName(Ty));
506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Print the type definition
508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Ty->getTypeID()) {
509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::FunctionTyID:  {
51019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    FunctionType* FT = cast<FunctionType>(Ty);
51119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Out << "std::vector<Type*>" << typeName << "_args;";
512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    FunctionType::param_iterator PI = FT->param_begin();
514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    FunctionType::param_iterator PE = FT->param_end();
515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (; PI != PE; ++PI) {
51619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Type* argTy = static_cast<Type*>(*PI);
51719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      printType(argTy);
518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      std::string argName(getCppName(argTy));
519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << typeName << "_args.push_back(" << argName;
520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ");";
521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out);
522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
52319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    printType(FT->getReturnType());
524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::string retTypeName(getCppName(FT->getReturnType()));
525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "FunctionType* " << typeName << " = FunctionType::get(";
526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    in(); nl(Out) << "/*Result=*/" << retTypeName;
527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ",";
528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out) << "/*Params=*/" << typeName << "_args,";
529894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true" : "false") << ");";
530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    out();
531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
532894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
533894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
534894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::StructTyID: {
53519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    StructType* ST = cast<StructType>(Ty);
53619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!ST->isLiteral()) {
53719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << "StructType *" << typeName << " = mod->getTypeByName(\"";
53819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      printEscapedString(ST->getName());
53919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << "\");";
54019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      nl(Out);
54119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << "if (!" << typeName << ") {";
54219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      nl(Out);
54319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << typeName << " = ";
54419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << "StructType::create(mod->getContext(), \"";
54519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      printEscapedString(ST->getName());
54619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << "\");";
54719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      nl(Out);
54819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << "}";
54919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      nl(Out);
55019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Indicate that this type is now defined.
55119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      DefinedTypes.insert(Ty);
55219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
55319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
55419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Out << "std::vector<Type*>" << typeName << "_fields;";
555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    StructType::element_iterator EI = ST->element_begin();
557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    StructType::element_iterator EE = ST->element_end();
558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (; EI != EE; ++EI) {
55919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Type* fieldTy = static_cast<Type*>(*EI);
56019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      printType(fieldTy);
561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      std::string fieldName(getCppName(fieldTy));
562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << typeName << "_fields.push_back(" << fieldName;
563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ");";
564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out);
565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
56619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
56719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (ST->isLiteral()) {
56819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << "StructType *" << typeName << " = ";
56919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << "StructType::get(" << "mod->getContext(), ";
57019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    } else {
57119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << "if (" << typeName << "->isOpaque()) {";
57219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      nl(Out);
57319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << typeName << "->setBody(";
57419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
57519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
57619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Out << typeName << "_fields, /*isPacked=*/"
577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << (ST->isPacked() ? "true" : "false") << ");";
578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
57919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!ST->isLiteral()) {
58019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << "}";
58119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      nl(Out);
58219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::ArrayTyID: {
58619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ArrayType* AT = cast<ArrayType>(Ty);
58719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Type* ET = AT->getElementType();
58819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    printType(ET);
58919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (DefinedTypes.find(Ty) == DefinedTypes.end()) {
59019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      std::string elemName(getCppName(ET));
59119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << "ArrayType* " << typeName << " = ArrayType::get("
59219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          << elemName
59319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          << ", " << utostr(AT->getNumElements()) << ");";
59419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      nl(Out);
59519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::PointerTyID: {
59919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    PointerType* PT = cast<PointerType>(Ty);
60019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Type* ET = PT->getElementType();
60119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    printType(ET);
60219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (DefinedTypes.find(Ty) == DefinedTypes.end()) {
60319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      std::string elemName(getCppName(ET));
60419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << "PointerType* " << typeName << " = PointerType::get("
60519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          << elemName
60619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          << ", " << utostr(PT->getAddressSpace()) << ");";
60719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      nl(Out);
60819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::VectorTyID: {
61219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    VectorType* PT = cast<VectorType>(Ty);
61319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Type* ET = PT->getElementType();
61419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    printType(ET);
61519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (DefinedTypes.find(Ty) == DefinedTypes.end()) {
61619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      std::string elemName(getCppName(ET));
61719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << "VectorType* " << typeName << " = VectorType::get("
61819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          << elemName
61919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          << ", " << utostr(PT->getNumElements()) << ");";
62019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      nl(Out);
62119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:
625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    error("Invalid TypeID");
626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Indicate that this type is now defined.
629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DefinedTypes.insert(Ty);
630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
631894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Finally, separate the type definition from other with a newline.
632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out);
633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printTypes(const Module* M) {
63619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Add all of the global variables to the value table.
637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (Module::const_global_iterator I = TheModule->global_begin(),
638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         E = TheModule->global_end(); I != E; ++I) {
639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (I->hasInitializer())
640894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      printType(I->getInitializer()->getType());
641894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printType(I->getType());
642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Add all the functions to the table
645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       FI != FE; ++FI) {
647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printType(FI->getReturnType());
648894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printType(FI->getFunctionType());
649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Add all the function arguments
650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (Function::const_arg_iterator AI = FI->arg_begin(),
651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           AE = FI->arg_end(); AI != AE; ++AI) {
652894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      printType(AI->getType());
653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Add all of the basic blocks and instructions
656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (Function::const_iterator BB = FI->begin(),
657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           E = FI->end(); BB != E; ++BB) {
658894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      printType(BB->getType());
659894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
660894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           ++I) {
661894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        printType(I->getType());
662894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        for (unsigned i = 0; i < I->getNumOperands(); ++i)
663894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          printType(I->getOperand(i)->getType());
664894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
668894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
669894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// printConstant - Print out a constant pool entry...
671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printConstant(const Constant *CV) {
672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // First, if the constant is actually a GlobalValue (variable or function)
673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // or its already in the constant list then we've printed it already and we
674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // can just return.
675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end())
676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::string constName(getCppName(CV));
679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::string typeName(getCppName(CV->getType()));
680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isa<GlobalValue>(CV)) {
682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Skip variables and functions, we emit them elsewhere
683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::string constValue = CI->getValue().toString(10, true);
688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "ConstantInt* " << constName
689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << " = ConstantInt::get(mod->getContext(), APInt("
690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << cast<IntegerType>(CI->getType())->getBitWidth()
691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << ", StringRef(\"" <<  constValue << "\"), 10));";
692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (isa<ConstantAggregateZero>(CV)) {
693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "ConstantAggregateZero* " << constName
694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << " = ConstantAggregateZero::get(" << typeName << ");";
695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (isa<ConstantPointerNull>(CV)) {
696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "ConstantPointerNull* " << constName
697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << " = ConstantPointerNull::get(" << typeName << ");";
698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "ConstantFP* " << constName << " = ";
700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printCFP(CFP);
701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ";";
702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (CA->isString() &&
704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        CA->getType()->getElementType() ==
705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            Type::getInt8Ty(CA->getContext())) {
706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "Constant* " << constName <<
707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             " = ConstantArray::get(mod->getContext(), \"";
708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      std::string tmp = CA->getAsString();
709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      bool nullTerminate = false;
710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (tmp[tmp.length()-1] == 0) {
711894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        tmp.erase(tmp.length()-1);
712894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        nullTerminate = true;
713894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      printEscapedString(tmp);
715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Determine if we want null termination or not.
716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (nullTerminate)
717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << "\", true"; // Indicate that the null terminator should be
718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           // added.
719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else
720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << "\", false";// No null terminator
721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ");";
722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "std::vector<Constant*> " << constName << "_elems;";
724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out);
725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned N = CA->getNumOperands();
726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (unsigned i = 0; i < N; ++i) {
727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        printConstant(CA->getOperand(i)); // recurse to print operands
728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << constName << "_elems.push_back("
729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            << getCppName(CA->getOperand(i)) << ");";
730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        nl(Out);
731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "Constant* " << constName << " = ConstantArray::get("
733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << typeName << ", " << constName << "_elems);";
734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "std::vector<Constant*> " << constName << "_fields;";
737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned N = CS->getNumOperands();
739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0; i < N; i++) {
740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      printConstant(CS->getOperand(i));
741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << constName << "_fields.push_back("
742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << getCppName(CS->getOperand(i)) << ");";
743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out);
744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "Constant* " << constName << " = ConstantStruct::get("
746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << typeName << ", " << constName << "_fields);";
747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "std::vector<Constant*> " << constName << "_elems;";
749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned N = CP->getNumOperands();
751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0; i < N; ++i) {
752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      printConstant(CP->getOperand(i));
753894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << constName << "_elems.push_back("
754894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << getCppName(CP->getOperand(i)) << ");";
755894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out);
756894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
757894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "Constant* " << constName << " = ConstantVector::get("
758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << typeName << ", " << constName << "_elems);";
759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (isa<UndefValue>(CV)) {
760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "UndefValue* " << constName << " = UndefValue::get("
761894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << typeName << ");";
762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
763894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (CE->getOpcode() == Instruction::GetElementPtr) {
764894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "std::vector<Constant*> " << constName << "_indices;";
765894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out);
766894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      printConstant(CE->getOperand(0));
767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (unsigned i = 1; i < CE->getNumOperands(); ++i ) {
768894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        printConstant(CE->getOperand(i));
769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << constName << "_indices.push_back("
770894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            << getCppName(CE->getOperand(i)) << ");";
771894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        nl(Out);
772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "Constant* " << constName
774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << " = ConstantExpr::getGetElementPtr("
775894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << getCppName(CE->getOperand(0)) << ", "
77619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          << constName << "_indices);";
777894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else if (CE->isCast()) {
778894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      printConstant(CE->getOperand(0));
779894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "Constant* " << constName << " = ConstantExpr::getCast(";
780894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      switch (CE->getOpcode()) {
781894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      default: llvm_unreachable("Invalid cast opcode");
782894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::Trunc: Out << "Instruction::Trunc"; break;
783894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::ZExt:  Out << "Instruction::ZExt"; break;
784894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::SExt:  Out << "Instruction::SExt"; break;
785894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::FPTrunc:  Out << "Instruction::FPTrunc"; break;
786894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::FPExt:  Out << "Instruction::FPExt"; break;
787894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::FPToUI:  Out << "Instruction::FPToUI"; break;
788894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::FPToSI:  Out << "Instruction::FPToSI"; break;
789894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::UIToFP:  Out << "Instruction::UIToFP"; break;
790894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::SIToFP:  Out << "Instruction::SIToFP"; break;
791894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::PtrToInt:  Out << "Instruction::PtrToInt"; break;
792894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::IntToPtr:  Out << "Instruction::IntToPtr"; break;
793894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::BitCast:  Out << "Instruction::BitCast"; break;
794894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
795894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ", " << getCppName(CE->getOperand(0)) << ", "
796894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << getCppName(CE->getType()) << ");";
797894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
798894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned N = CE->getNumOperands();
799894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (unsigned i = 0; i < N; ++i ) {
800894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        printConstant(CE->getOperand(i));
801894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
802894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "Constant* " << constName << " = ConstantExpr::";
803894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      switch (CE->getOpcode()) {
804894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::Add:    Out << "getAdd(";  break;
805894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::FAdd:   Out << "getFAdd(";  break;
806894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::Sub:    Out << "getSub("; break;
807894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::FSub:   Out << "getFSub("; break;
808894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::Mul:    Out << "getMul("; break;
809894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::FMul:   Out << "getFMul("; break;
810894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::UDiv:   Out << "getUDiv("; break;
811894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::SDiv:   Out << "getSDiv("; break;
812894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::FDiv:   Out << "getFDiv("; break;
813894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::URem:   Out << "getURem("; break;
814894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::SRem:   Out << "getSRem("; break;
815894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::FRem:   Out << "getFRem("; break;
816894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::And:    Out << "getAnd("; break;
817894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::Or:     Out << "getOr("; break;
818894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::Xor:    Out << "getXor("; break;
819894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::ICmp:
820894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << "getICmp(ICmpInst::ICMP_";
821894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        switch (CE->getPredicate()) {
822894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case ICmpInst::ICMP_EQ:  Out << "EQ"; break;
823894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case ICmpInst::ICMP_NE:  Out << "NE"; break;
824894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case ICmpInst::ICMP_SLT: Out << "SLT"; break;
825894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case ICmpInst::ICMP_ULT: Out << "ULT"; break;
826894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case ICmpInst::ICMP_SGT: Out << "SGT"; break;
827894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case ICmpInst::ICMP_UGT: Out << "UGT"; break;
828894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case ICmpInst::ICMP_SLE: Out << "SLE"; break;
829894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case ICmpInst::ICMP_ULE: Out << "ULE"; break;
830894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case ICmpInst::ICMP_SGE: Out << "SGE"; break;
831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case ICmpInst::ICMP_UGE: Out << "UGE"; break;
832894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        default: error("Invalid ICmp Predicate");
833894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
834894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
835894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::FCmp:
836894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << "getFCmp(FCmpInst::FCMP_";
837894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        switch (CE->getPredicate()) {
838894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case FCmpInst::FCMP_FALSE: Out << "FALSE"; break;
839894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case FCmpInst::FCMP_ORD:   Out << "ORD"; break;
840894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case FCmpInst::FCMP_UNO:   Out << "UNO"; break;
841894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case FCmpInst::FCMP_OEQ:   Out << "OEQ"; break;
842894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case FCmpInst::FCMP_UEQ:   Out << "UEQ"; break;
843894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case FCmpInst::FCMP_ONE:   Out << "ONE"; break;
844894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case FCmpInst::FCMP_UNE:   Out << "UNE"; break;
845894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case FCmpInst::FCMP_OLT:   Out << "OLT"; break;
846894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case FCmpInst::FCMP_ULT:   Out << "ULT"; break;
847894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case FCmpInst::FCMP_OGT:   Out << "OGT"; break;
848894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case FCmpInst::FCMP_UGT:   Out << "UGT"; break;
849894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case FCmpInst::FCMP_OLE:   Out << "OLE"; break;
850894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case FCmpInst::FCMP_ULE:   Out << "ULE"; break;
851894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case FCmpInst::FCMP_OGE:   Out << "OGE"; break;
852894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case FCmpInst::FCMP_UGE:   Out << "UGE"; break;
853894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case FCmpInst::FCMP_TRUE:  Out << "TRUE"; break;
854894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        default: error("Invalid FCmp Predicate");
855894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
856894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
857894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::Shl:     Out << "getShl("; break;
858894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::LShr:    Out << "getLShr("; break;
859894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::AShr:    Out << "getAShr("; break;
860894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::Select:  Out << "getSelect("; break;
861894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::ExtractElement: Out << "getExtractElement("; break;
862894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::InsertElement:  Out << "getInsertElement("; break;
863894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Instruction::ShuffleVector:  Out << "getShuffleVector("; break;
864894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      default:
865894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        error("Invalid constant expression");
866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
867894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
868894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << getCppName(CE->getOperand(0));
869894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (unsigned i = 1; i < CE->getNumOperands(); ++i)
870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << ", " << getCppName(CE->getOperand(i));
871894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ");";
872894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
873894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
874894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "Constant* " << constName << " = ";
875894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "BlockAddress::get(" << getOpName(BA->getBasicBlock()) << ");";
876894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
877894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    error("Bad Constant");
878894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "Constant* " << constName << " = 0; ";
879894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
880894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out);
881894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
882894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
883894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printConstants(const Module* M) {
884894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Traverse all the global variables looking for constant initializers
885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (Module::const_global_iterator I = TheModule->global_begin(),
886894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         E = TheModule->global_end(); I != E; ++I)
887894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (I->hasInitializer())
888894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      printConstant(I->getInitializer());
889894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
890894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Traverse the LLVM functions looking for constants
891894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
892894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       FI != FE; ++FI) {
893894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Add all of the basic blocks and instructions
894894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (Function::const_iterator BB = FI->begin(),
895894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           E = FI->end(); BB != E; ++BB) {
896894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
897894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           ++I) {
898894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        for (unsigned i = 0; i < I->getNumOperands(); ++i) {
899894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
900894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            printConstant(C);
901894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          }
902894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
903894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
904894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
905894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printVariableUses(const GlobalVariable *GV) {
909894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "// Type Definitions";
910894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out);
911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printType(GV->getType());
912894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (GV->hasInitializer()) {
91319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    const Constant *Init = GV->getInitializer();
914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printType(Init->getType());
91519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (const Function *F = dyn_cast<Function>(Init)) {
916894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out)<< "/ Function Declarations"; nl(Out);
917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      printFunctionHead(F);
91819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    } else if (const GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
919894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out) << "// Global Variable Declarations"; nl(Out);
920894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      printVariableHead(gv);
921894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
922894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out) << "// Global Variable Definitions"; nl(Out);
923894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      printVariableBody(gv);
924894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else  {
925894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out) << "// Constant Definitions"; nl(Out);
926894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      printConstant(Init);
927894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
928894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
929894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
930894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
931894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printVariableHead(const GlobalVariable *GV) {
932894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "GlobalVariable* " << getCppName(GV);
933894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (is_inline) {
934894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << " = mod->getGlobalVariable(mod->getContext(), ";
935894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printEscapedString(GV->getName());
936894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ", " << getCppName(GV->getType()->getElementType()) << ",true)";
937894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out) << "if (!" << getCppName(GV) << ") {";
938894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    in(); nl(Out) << getCppName(GV);
939894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
940894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << " = new GlobalVariable(/*Module=*/*mod, ";
941894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "/*Type=*/";
942894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printCppName(GV->getType()->getElementType());
943894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << ",";
944894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "/*isConstant=*/" << (GV->isConstant()?"true":"false");
945894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << ",";
946894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "/*Linkage=*/";
947894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printLinkageType(GV->getLinkage());
948894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << ",";
949894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "/*Initializer=*/0, ";
950894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (GV->hasInitializer()) {
951894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "// has initializer, specified below";
952894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
953894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "/*Name=*/\"";
954894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printEscapedString(GV->getName());
955894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "\");";
956894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out);
957894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
958894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (GV->hasSection()) {
959894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printCppName(GV);
960894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "->setSection(\"";
961894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printEscapedString(GV->getSection());
962894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\");";
963894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
964894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
965894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (GV->getAlignment()) {
966894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printCppName(GV);
967894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");";
968894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
969894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
970894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (GV->getVisibility() != GlobalValue::DefaultVisibility) {
971894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printCppName(GV);
972894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "->setVisibility(";
973894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printVisibilityType(GV->getVisibility());
974894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ");";
975894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
976894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
977894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (GV->isThreadLocal()) {
978894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printCppName(GV);
979894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "->setThreadLocal(true);";
980894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
981894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
982894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (is_inline) {
983894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    out(); Out << "}"; nl(Out);
984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
985894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
986894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
987894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printVariableBody(const GlobalVariable *GV) {
988894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (GV->hasInitializer()) {
989894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printCppName(GV);
990894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "->setInitializer(";
991894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << getCppName(GV->getInitializer()) << ");";
992894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
993894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
994894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
995894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
99619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstd::string CppWriter::getOpName(const Value* V) {
997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end())
998894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return getCppName(V);
999894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1000894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // See if its alread in the map of forward references, if so just return the
1001894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // name we already set up for it
1002894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ForwardRefMap::const_iterator I = ForwardRefs.find(V);
1003894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (I != ForwardRefs.end())
1004894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return I->second;
1005894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1006894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // This is a new forward reference. Generate a unique name for it
1007894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::string result(std::string("fwdref_") + utostr(uniqueNum++));
1008894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1009894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Yes, this is a hack. An Argument is the smallest instantiable value that
1010894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // we can make as a placeholder for the real value. We'll replace these
1011894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Argument instances later.
1012894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "Argument* " << result << " = new Argument("
1013894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      << getCppName(V->getType()) << ");";
1014894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out);
1015894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ForwardRefs[V] = result;
1016894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return result;
1017894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1018894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1019894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// printInstruction - This member is called for each Instruction in a function.
1020894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printInstruction(const Instruction *I,
1021894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 const std::string& bbname) {
1022894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::string iName(getCppName(I));
1023894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1024894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Before we emit this instruction, we need to take care of generating any
1025894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // forward references. So, we get the names of all the operands in advance
1026894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const unsigned Ops(I->getNumOperands());
1027894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::string* opNames = new std::string[Ops];
1028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i < Ops; i++)
1029894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    opNames[i] = getOpName(I->getOperand(i));
1030894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1031894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (I->getOpcode()) {
1032894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:
1033894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    error("Invalid instruction");
1034894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1035894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1036894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Ret: {
1037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const ReturnInst* ret =  cast<ReturnInst>(I);
1038894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "ReturnInst::Create(mod->getContext(), "
1039894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");";
1040894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1041894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1042894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Br: {
1043894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const BranchInst* br = cast<BranchInst>(I);
1044894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "BranchInst::Create(" ;
1045894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (br->getNumOperands() == 3) {
1046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << opNames[2] << ", "
1047894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << opNames[1] << ", "
1048894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << opNames[0] << ", ";
1049894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1050894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else if (br->getNumOperands() == 1) {
1051894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << opNames[0] << ", ";
1052894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
1053894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      error("Branch with 2 operands?");
1054894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1055894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << bbname << ");";
1056894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1057894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1058894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Switch: {
1059894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const SwitchInst *SI = cast<SwitchInst>(I);
1060894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "SwitchInst* " << iName << " = SwitchInst::Create("
106119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        << getOpName(SI->getCondition()) << ", "
106219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        << getOpName(SI->getDefaultDest()) << ", "
1063894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << SI->getNumCases() << ", " << bbname << ");";
1064894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
106519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned NumCases = SI->getNumCases();
106619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned i = 1; i < NumCases; ++i) {
106719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      const ConstantInt* CaseVal = SI->getCaseValue(i);
106819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      const BasicBlock* BB = SI->getSuccessor(i);
1069894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << iName << "->addCase("
107019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          << getOpName(CaseVal) << ", "
107119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          << getOpName(BB) << ");";
1072894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out);
1073894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1074894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1075894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1076894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::IndirectBr: {
1077894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const IndirectBrInst *IBI = cast<IndirectBrInst>(I);
1078894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "IndirectBrInst *" << iName << " = IndirectBrInst::Create("
1079894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << opNames[0] << ", " << IBI->getNumDestinations() << ");";
1080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
1081894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 1; i != IBI->getNumOperands(); ++i) {
1082894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << iName << "->addDestination(" << opNames[i] << ");";
1083894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out);
1084894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1085894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1086894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
108719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Instruction::Resume: {
108819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Out << "ResumeInst::Create(mod->getContext(), " << opNames[0]
108919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        << ", " << bbname << ");";
109019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    break;
109119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
1092894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Invoke: {
1093894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const InvokeInst* inv = cast<InvokeInst>(I);
1094894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "std::vector<Value*> " << iName << "_params;";
1095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
1096894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0; i < inv->getNumArgOperands(); ++i) {
1097894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << iName << "_params.push_back("
1098894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << getOpName(inv->getArgOperand(i)) << ");";
1099894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out);
1100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // FIXME: This shouldn't use magic numbers -3, -2, and -1.
1102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "InvokeInst *" << iName << " = InvokeInst::Create("
1103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << getOpName(inv->getCalledFunction()) << ", "
1104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << getOpName(inv->getNormalDest()) << ", "
1105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << getOpName(inv->getUnwindDest()) << ", "
110619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        << iName << "_params, \"";
1107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printEscapedString(inv->getName());
1108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\", " << bbname << ");";
1109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out) << iName << "->setCallingConv(";
1110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printCallingConv(inv->getCallingConv());
1111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ");";
1112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printAttributes(inv->getAttributes(), iName);
1113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << iName << "->setAttributes(" << iName << "_PAL);";
1114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
1115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Unwind: {
1118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "new UnwindInst("
1119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << bbname << ");";
1120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Unreachable: {
1123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "new UnreachableInst("
1124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << "mod->getContext(), "
1125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << bbname << ");";
1126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Add:
1129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FAdd:
1130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Sub:
1131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FSub:
1132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Mul:
1133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FMul:
1134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::UDiv:
1135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::SDiv:
1136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FDiv:
1137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::URem:
1138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::SRem:
1139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FRem:
1140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::And:
1141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Or:
1142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Xor:
1143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Shl:
1144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::LShr:
1145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::AShr:{
1146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "BinaryOperator* " << iName << " = BinaryOperator::Create(";
1147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (I->getOpcode()) {
1148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::Add: Out << "Instruction::Add"; break;
1149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::FAdd: Out << "Instruction::FAdd"; break;
1150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::Sub: Out << "Instruction::Sub"; break;
1151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::FSub: Out << "Instruction::FSub"; break;
1152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::Mul: Out << "Instruction::Mul"; break;
1153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::FMul: Out << "Instruction::FMul"; break;
1154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::UDiv:Out << "Instruction::UDiv"; break;
1155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::SDiv:Out << "Instruction::SDiv"; break;
1156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::FDiv:Out << "Instruction::FDiv"; break;
1157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::URem:Out << "Instruction::URem"; break;
1158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::SRem:Out << "Instruction::SRem"; break;
1159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::FRem:Out << "Instruction::FRem"; break;
1160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::And: Out << "Instruction::And"; break;
1161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::Or:  Out << "Instruction::Or";  break;
1162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::Xor: Out << "Instruction::Xor"; break;
1163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::Shl: Out << "Instruction::Shl"; break;
1164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::LShr:Out << "Instruction::LShr"; break;
1165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::AShr:Out << "Instruction::AShr"; break;
1166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    default: Out << "Instruction::BadOpCode"; break;
1167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printEscapedString(I->getName());
1170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\", " << bbname << ");";
1171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FCmp: {
1174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "FCmpInst* " << iName << " = new FCmpInst(*" << bbname << ", ";
1175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (cast<FCmpInst>(I)->getPredicate()) {
1176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case FCmpInst::FCMP_FALSE: Out << "FCmpInst::FCMP_FALSE"; break;
1177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case FCmpInst::FCMP_OEQ  : Out << "FCmpInst::FCMP_OEQ"; break;
1178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case FCmpInst::FCMP_OGT  : Out << "FCmpInst::FCMP_OGT"; break;
1179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case FCmpInst::FCMP_OGE  : Out << "FCmpInst::FCMP_OGE"; break;
1180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case FCmpInst::FCMP_OLT  : Out << "FCmpInst::FCMP_OLT"; break;
1181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case FCmpInst::FCMP_OLE  : Out << "FCmpInst::FCMP_OLE"; break;
1182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case FCmpInst::FCMP_ONE  : Out << "FCmpInst::FCMP_ONE"; break;
1183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case FCmpInst::FCMP_ORD  : Out << "FCmpInst::FCMP_ORD"; break;
1184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case FCmpInst::FCMP_UNO  : Out << "FCmpInst::FCMP_UNO"; break;
1185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case FCmpInst::FCMP_UEQ  : Out << "FCmpInst::FCMP_UEQ"; break;
1186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case FCmpInst::FCMP_UGT  : Out << "FCmpInst::FCMP_UGT"; break;
1187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case FCmpInst::FCMP_UGE  : Out << "FCmpInst::FCMP_UGE"; break;
1188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case FCmpInst::FCMP_ULT  : Out << "FCmpInst::FCMP_ULT"; break;
1189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case FCmpInst::FCMP_ULE  : Out << "FCmpInst::FCMP_ULE"; break;
1190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case FCmpInst::FCMP_UNE  : Out << "FCmpInst::FCMP_UNE"; break;
1191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case FCmpInst::FCMP_TRUE : Out << "FCmpInst::FCMP_TRUE"; break;
1192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    default: Out << "FCmpInst::BAD_ICMP_PREDICATE"; break;
1193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printEscapedString(I->getName());
1196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\");";
1197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::ICmp: {
1200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "ICmpInst* " << iName << " = new ICmpInst(*" << bbname << ", ";
1201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (cast<ICmpInst>(I)->getPredicate()) {
1202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ICmpInst::ICMP_EQ:  Out << "ICmpInst::ICMP_EQ";  break;
1203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ICmpInst::ICMP_NE:  Out << "ICmpInst::ICMP_NE";  break;
1204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ICmpInst::ICMP_ULE: Out << "ICmpInst::ICMP_ULE"; break;
1205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ICmpInst::ICMP_SLE: Out << "ICmpInst::ICMP_SLE"; break;
1206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ICmpInst::ICMP_UGE: Out << "ICmpInst::ICMP_UGE"; break;
1207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ICmpInst::ICMP_SGE: Out << "ICmpInst::ICMP_SGE"; break;
1208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ICmpInst::ICMP_ULT: Out << "ICmpInst::ICMP_ULT"; break;
1209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ICmpInst::ICMP_SLT: Out << "ICmpInst::ICMP_SLT"; break;
1210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ICmpInst::ICMP_UGT: Out << "ICmpInst::ICMP_UGT"; break;
1211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case ICmpInst::ICMP_SGT: Out << "ICmpInst::ICMP_SGT"; break;
1212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    default: Out << "ICmpInst::BAD_ICMP_PREDICATE"; break;
1213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printEscapedString(I->getName());
1216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\");";
1217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Alloca: {
1220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const AllocaInst* allocaI = cast<AllocaInst>(I);
1221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "AllocaInst* " << iName << " = new AllocaInst("
1222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << getCppName(allocaI->getAllocatedType()) << ", ";
1223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (allocaI->isArrayAllocation())
1224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << opNames[0] << ", ";
1225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\"";
1226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printEscapedString(allocaI->getName());
1227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\", " << bbname << ");";
1228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (allocaI->getAlignment())
1229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out) << iName << "->setAlignment("
1230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << allocaI->getAlignment() << ");";
1231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Load: {
1234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const LoadInst* load = cast<LoadInst>(I);
1235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "LoadInst* " << iName << " = new LoadInst("
1236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << opNames[0] << ", \"";
1237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printEscapedString(load->getName());
1238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\", " << (load->isVolatile() ? "true" : "false" )
1239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << ", " << bbname << ");";
1240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Store: {
1243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const StoreInst* store = cast<StoreInst>(I);
1244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << " new StoreInst("
1245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << opNames[0] << ", "
1246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << opNames[1] << ", "
1247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << (store->isVolatile() ? "true" : "false")
1248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << ", " << bbname << ");";
1249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::GetElementPtr: {
1252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
1253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (gep->getNumOperands() <= 2) {
1254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "GetElementPtrInst* " << iName << " = GetElementPtrInst::Create("
1255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << opNames[0];
1256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (gep->getNumOperands() == 2)
1257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << ", " << opNames[1];
1258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
1259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "std::vector<Value*> " << iName << "_indices;";
1260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out);
1261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
1262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << iName << "_indices.push_back("
1263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            << opNames[i] << ");";
1264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        nl(Out);
1265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "Instruction* " << iName << " = GetElementPtrInst::Create("
126719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          << opNames[0] << ", " << iName << "_indices";
1268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ", \"";
1270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printEscapedString(gep->getName());
1271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\", " << bbname << ");";
1272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::PHI: {
1275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const PHINode* phi = cast<PHINode>(I);
1276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "PHINode* " << iName << " = PHINode::Create("
127819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        << getCppName(phi->getType()) << ", "
127919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        << phi->getNumIncomingValues() << ", \"";
1280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printEscapedString(phi->getName());
1281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\", " << bbname << ");";
1282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
128319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned i = 0; i < phi->getNumIncomingValues(); ++i) {
1284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << iName << "->addIncoming("
128519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          << opNames[PHINode::getOperandNumForIncomingValue(i)] << ", "
128619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          << getOpName(phi->getIncomingBlock(i)) << ");";
1287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out);
1288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Trunc:
1292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::ZExt:
1293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::SExt:
1294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FPTrunc:
1295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FPExt:
1296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FPToUI:
1297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::FPToSI:
1298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::UIToFP:
1299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::SIToFP:
1300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::PtrToInt:
1301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::IntToPtr:
1302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::BitCast: {
1303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const CastInst* cst = cast<CastInst>(I);
1304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "CastInst* " << iName << " = new ";
1305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (I->getOpcode()) {
1306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::Trunc:    Out << "TruncInst"; break;
1307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::ZExt:     Out << "ZExtInst"; break;
1308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::SExt:     Out << "SExtInst"; break;
1309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::FPTrunc:  Out << "FPTruncInst"; break;
1310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::FPExt:    Out << "FPExtInst"; break;
1311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::FPToUI:   Out << "FPToUIInst"; break;
1312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::FPToSI:   Out << "FPToSIInst"; break;
1313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::UIToFP:   Out << "UIToFPInst"; break;
1314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::SIToFP:   Out << "SIToFPInst"; break;
1315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::PtrToInt: Out << "PtrToIntInst"; break;
1316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::IntToPtr: Out << "IntToPtrInst"; break;
1317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::BitCast:  Out << "BitCastInst"; break;
131819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    default: assert(0 && "Unreachable"); break;
1319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "(" << opNames[0] << ", "
1321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << getCppName(cst->getType()) << ", \"";
1322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printEscapedString(cst->getName());
1323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\", " << bbname << ");";
1324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Call: {
1327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const CallInst* call = cast<CallInst>(I);
1328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (const InlineAsm* ila = dyn_cast<InlineAsm>(call->getCalledValue())) {
1329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "InlineAsm* " << getCppName(ila) << " = InlineAsm::get("
1330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << getCppName(ila->getFunctionType()) << ", \""
1331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << ila->getAsmString() << "\", \""
1332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << ila->getConstraintString() << "\","
1333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << (ila->hasSideEffects() ? "true" : "false") << ");";
1334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out);
1335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (call->getNumArgOperands() > 1) {
1337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "std::vector<Value*> " << iName << "_params;";
1338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out);
1339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (unsigned i = 0; i < call->getNumArgOperands(); ++i) {
1340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << iName << "_params.push_back(" << opNames[i] << ");";
1341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        nl(Out);
1342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "CallInst* " << iName << " = CallInst::Create("
1344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << opNames[call->getNumArgOperands()] << ", "
134519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          << iName << "_params, \"";
1346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else if (call->getNumArgOperands() == 1) {
1347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "CallInst* " << iName << " = CallInst::Create("
1348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << opNames[call->getNumArgOperands()] << ", " << opNames[0] << ", \"";
1349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
1350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "CallInst* " << iName << " = CallInst::Create("
1351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << opNames[call->getNumArgOperands()] << ", \"";
1352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printEscapedString(call->getName());
1354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\", " << bbname << ");";
1355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out) << iName << "->setCallingConv(";
1356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printCallingConv(call->getCallingConv());
1357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ");";
1358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out) << iName << "->setTailCall("
1359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << (call->isTailCall() ? "true" : "false");
1360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ");";
1361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
1362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printAttributes(call->getAttributes(), iName);
1363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << iName << "->setAttributes(" << iName << "_PAL);";
1364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
1365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::Select: {
1368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const SelectInst* sel = cast<SelectInst>(I);
1369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "SelectInst* " << getCppName(sel) << " = SelectInst::Create(";
1370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
1371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printEscapedString(sel->getName());
1372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\", " << bbname << ");";
1373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::UserOp1:
1376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// FALL THROUGH
1377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::UserOp2: {
1378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// FIXME: What should be done here?
1379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::VAArg: {
1382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const VAArgInst* va = cast<VAArgInst>(I);
1383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "VAArgInst* " << getCppName(va) << " = new VAArgInst("
1384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << opNames[0] << ", " << getCppName(va->getType()) << ", \"";
1385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printEscapedString(va->getName());
1386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\", " << bbname << ");";
1387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::ExtractElement: {
1390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const ExtractElementInst* eei = cast<ExtractElementInst>(I);
1391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "ExtractElementInst* " << getCppName(eei)
1392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << " = new ExtractElementInst(" << opNames[0]
1393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << ", " << opNames[1] << ", \"";
1394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printEscapedString(eei->getName());
1395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\", " << bbname << ");";
1396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::InsertElement: {
1399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const InsertElementInst* iei = cast<InsertElementInst>(I);
1400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "InsertElementInst* " << getCppName(iei)
1401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << " = InsertElementInst::Create(" << opNames[0]
1402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << ", " << opNames[1] << ", " << opNames[2] << ", \"";
1403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printEscapedString(iei->getName());
1404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\", " << bbname << ");";
1405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::ShuffleVector: {
1408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I);
1409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "ShuffleVectorInst* " << getCppName(svi)
1410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << " = new ShuffleVectorInst(" << opNames[0]
1411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << ", " << opNames[1] << ", " << opNames[2] << ", \"";
1412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printEscapedString(svi->getName());
1413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\", " << bbname << ");";
1414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::ExtractValue: {
1417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const ExtractValueInst *evi = cast<ExtractValueInst>(I);
1418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "std::vector<unsigned> " << iName << "_indices;";
1419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
1420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0; i < evi->getNumIndices(); ++i) {
1421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << iName << "_indices.push_back("
1422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << evi->idx_begin()[i] << ");";
1423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out);
1424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "ExtractValueInst* " << getCppName(evi)
1426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << " = ExtractValueInst::Create(" << opNames[0]
1427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << ", "
142819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        << iName << "_indices, \"";
1429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printEscapedString(evi->getName());
1430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\", " << bbname << ");";
1431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1433894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Instruction::InsertValue: {
1434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const InsertValueInst *ivi = cast<InsertValueInst>(I);
1435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "std::vector<unsigned> " << iName << "_indices;";
1436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
1437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0; i < ivi->getNumIndices(); ++i) {
1438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << iName << "_indices.push_back("
1439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << ivi->idx_begin()[i] << ");";
1440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out);
1441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "InsertValueInst* " << getCppName(ivi)
1443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << " = InsertValueInst::Create(" << opNames[0]
1444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << ", " << opNames[1] << ", "
144519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        << iName << "_indices, \"";
1446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printEscapedString(ivi->getName());
1447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\", " << bbname << ");";
1448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DefinedValues.insert(I);
1452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out);
1453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  delete [] opNames;
1454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Print out the types, constants and declarations needed by one function
1457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printFunctionUses(const Function* F) {
1458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "// Type Definitions"; nl(Out);
1459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!is_inline) {
1460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Print the function's return type
1461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printType(F->getReturnType());
1462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Print the function's function type
1464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printType(F->getFunctionType());
1465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Print the types of each of the function's arguments
1467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         AI != AE; ++AI) {
1469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      printType(AI->getType());
1470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1473894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Print type definitions for every type referenced by an instruction and
1474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // make a note of any global values or constants that are referenced
1475894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallPtrSet<GlobalValue*,64> gvs;
1476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallPtrSet<Constant*,64> consts;
1477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (Function::const_iterator BB = F->begin(), BE = F->end();
1478894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       BB != BE; ++BB){
1479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1480894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         I != E; ++I) {
1481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Print the type of the instruction itself
1482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      printType(I->getType());
1483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Print the type of each of the instruction's operands
1485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Value* operand = I->getOperand(i);
1487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        printType(operand->getType());
1488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // If the operand references a GVal or Constant, make a note of it
1490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (GlobalValue* GV = dyn_cast<GlobalValue>(operand)) {
1491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          gvs.insert(GV);
149219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          if (GenerationType != GenFunction)
149319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
149419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman              if (GVar->hasInitializer())
149519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                consts.insert(GVar->getInitializer());
149619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        } else if (Constant* C = dyn_cast<Constant>(operand)) {
1497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          consts.insert(C);
149819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          for (unsigned j = 0; j < C->getNumOperands(); ++j) {
149919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            // If the operand references a GVal or Constant, make a note of it
150019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            Value* operand = C->getOperand(j);
150119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            printType(operand->getType());
150219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            if (GlobalValue* GV = dyn_cast<GlobalValue>(operand)) {
150319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman              gvs.insert(GV);
150419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman              if (GenerationType != GenFunction)
150519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
150619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                  if (GVar->hasInitializer())
150719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                    consts.insert(GVar->getInitializer());
150819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            }
150919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          }
151019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        }
1511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Print the function declarations for any functions encountered
1516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "// Function Declarations"; nl(Out);
1517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
1518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       I != E; ++I) {
1519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Function* Fun = dyn_cast<Function>(*I)) {
1520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!is_inline || Fun != F)
1521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        printFunctionHead(Fun);
1522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Print the global variable declarations for any variables encountered
1526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "// Global Variable Declarations"; nl(Out);
1527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
1528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       I != E; ++I) {
1529894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (GlobalVariable* F = dyn_cast<GlobalVariable>(*I))
1530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      printVariableHead(F);
1531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1532894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
153319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Print the constants found
1534894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "// Constant Definitions"; nl(Out);
1535894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (SmallPtrSet<Constant*,64>::iterator I = consts.begin(),
1536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         E = consts.end(); I != E; ++I) {
1537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printConstant(*I);
1538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Process the global variables definitions now that all the constants have
1541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // been emitted. These definitions just couple the gvars with their constant
1542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // initializers.
154319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (GenerationType != GenFunction) {
154419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    nl(Out) << "// Global Variable Definitions"; nl(Out);
154519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
154619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         I != E; ++I) {
154719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I))
154819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        printVariableBody(GV);
154919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printFunctionHead(const Function* F) {
1554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "Function* " << getCppName(F);
155519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Out << " = mod->getFunction(\"";
155619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  printEscapedString(F->getName());
155719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Out << "\");";
155819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  nl(Out) << "if (!" << getCppName(F) << ") {";
155919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  nl(Out) << getCppName(F);
156019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out<< " = Function::Create(";
1562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ",";
1563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "/*Linkage=*/";
1564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printLinkageType(F->getLinkage());
1565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << ",";
1566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "/*Name=*/\"";
1567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printEscapedString(F->getName());
1568894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "\", mod); " << (F->isDeclaration()? "// (external, no body)" : "");
1569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out,-1);
1570894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printCppName(F);
1571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "->setCallingConv(";
1572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printCallingConv(F->getCallingConv());
1573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << ");";
1574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out);
1575894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (F->hasSection()) {
1576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printCppName(F);
1577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "->setSection(\"" << F->getSection() << "\");";
1578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
1579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (F->getAlignment()) {
1581894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printCppName(F);
1582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "->setAlignment(" << F->getAlignment() << ");";
1583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
1584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (F->getVisibility() != GlobalValue::DefaultVisibility) {
1586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printCppName(F);
1587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "->setVisibility(";
1588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printVisibilityType(F->getVisibility());
1589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ");";
1590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
1591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (F->hasGC()) {
1593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printCppName(F);
1594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "->setGC(\"" << F->getGC() << "\");";
1595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
1596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
159719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Out << "}";
159819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  nl(Out);
1599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printAttributes(F->getAttributes(), getCppName(F));
1600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printCppName(F);
1601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "->setAttributes(" << getCppName(F) << "_PAL);";
1602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out);
1603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printFunctionBody(const Function *F) {
1606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (F->isDeclaration())
1607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return; // external functions have no bodies.
1608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Clear the DefinedValues and ForwardRefs maps because we can't have
1610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // cross-function forward refs
1611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ForwardRefs.clear();
1612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DefinedValues.clear();
1613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Create all the argument values
1615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!is_inline) {
1616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!F->arg_empty()) {
1617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "Function::arg_iterator args = " << getCppName(F)
1618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << "->arg_begin();";
1619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out);
1620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         AI != AE; ++AI) {
1623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "Value* " << getCppName(AI) << " = args++;";
1624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out);
1625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (AI->hasName()) {
1626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << getCppName(AI) << "->setName(\"" << AI->getName() << "\");";
1627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        nl(Out);
1628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1631894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Create all the basic blocks
1633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out);
1634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (Function::const_iterator BI = F->begin(), BE = F->end();
1635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       BI != BE; ++BI) {
1636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::string bbname(getCppName(BI));
1637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "BasicBlock* " << bbname <<
1638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           " = BasicBlock::Create(mod->getContext(), \"";
1639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (BI->hasName())
1640894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      printEscapedString(BI->getName());
1641894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\"," << getCppName(BI->getParent()) << ",0);";
1642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
1643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Output all of its basic blocks... for the function
1646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (Function::const_iterator BI = F->begin(), BE = F->end();
1647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       BI != BE; ++BI) {
1648894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::string bbname(getCppName(BI));
1649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")";
1650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
1651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1652894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Output all of the instructions in the basic block...
1653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
1654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         I != E; ++I) {
1655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      printInstruction(I,bbname);
1656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1658894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1659894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Loop over the ForwardRefs and resolve them now that all instructions
1660894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // are generated.
1661894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!ForwardRefs.empty()) {
1662894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out) << "// Resolve Forward References";
1663894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
1664894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  while (!ForwardRefs.empty()) {
1667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ForwardRefMap::iterator I = ForwardRefs.begin();
1668894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << I->second << "->replaceAllUsesWith("
1669894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << getCppName(I->first) << "); delete " << I->second << ";";
1670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
1671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ForwardRefs.erase(I);
1672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printInline(const std::string& fname,
1676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            const std::string& func) {
1677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Function* F = TheModule->getFunction(func);
1678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!F) {
1679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    error(std::string("Function '") + func + "' not found in input module");
1680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
1681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (F->isDeclaration()) {
1683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    error(std::string("Function '") + func + "' is external!");
1684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
1685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *"
1687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          << getCppName(F);
1688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned arg_count = 1;
1689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       AI != AE; ++AI) {
1691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ", Value* arg_" << arg_count;
1692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << ") {";
1694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out);
1695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  is_inline = true;
1696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printFunctionUses(F);
1697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printFunctionBody(F);
1698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  is_inline = false;
1699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "return " << getCppName(F->begin()) << ";";
1700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "}";
1701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out);
1702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printModuleBody() {
1705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Print out all the type definitions
1706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "// Type Definitions"; nl(Out);
1707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printTypes(TheModule);
1708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Functions can call each other and global variables can reference them so
1710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // define all the functions first before emitting their function bodies.
1711894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "// Function Declarations"; nl(Out);
1712894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1713894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       I != E; ++I)
1714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printFunctionHead(I);
1715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Process the global variables declarations. We can't initialze them until
1717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // after the constants are printed so just print a header for each global
1718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "// Global Variable Declarations\n"; nl(Out);
1719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (Module::const_global_iterator I = TheModule->global_begin(),
1720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         E = TheModule->global_end(); I != E; ++I) {
1721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printVariableHead(I);
1722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Print out all the constants definitions. Constants don't recurse except
1725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // through GlobalValues. All GlobalValues have been declared at this point
1726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // so we can proceed to generate the constants.
1727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "// Constant Definitions"; nl(Out);
1728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printConstants(TheModule);
1729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Process the global variables definitions now that all the constants have
1731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // been emitted. These definitions just couple the gvars with their constant
1732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // initializers.
1733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "// Global Variable Definitions"; nl(Out);
1734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (Module::const_global_iterator I = TheModule->global_begin(),
1735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         E = TheModule->global_end(); I != E; ++I) {
1736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printVariableBody(I);
1737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Finally, we can safely put out all of the function bodies.
1740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "// Function Definitions"; nl(Out);
1741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       I != E; ++I) {
1743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!I->isDeclaration()) {
1744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I)
1745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              << ")";
1746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out) << "{";
1747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out,1);
1748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      printFunctionBody(I);
1749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out,-1) << "}";
1750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      nl(Out);
1751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1753894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1754894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1755894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printProgram(const std::string& fname,
1756894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             const std::string& mName) {
1757894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "#include <llvm/LLVMContext.h>\n";
1758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "#include <llvm/Module.h>\n";
1759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "#include <llvm/DerivedTypes.h>\n";
1760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "#include <llvm/Constants.h>\n";
1761894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "#include <llvm/GlobalVariable.h>\n";
1762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "#include <llvm/Function.h>\n";
1763894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "#include <llvm/CallingConv.h>\n";
1764894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "#include <llvm/BasicBlock.h>\n";
1765894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "#include <llvm/Instructions.h>\n";
1766894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "#include <llvm/InlineAsm.h>\n";
1767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "#include <llvm/Support/FormattedStream.h>\n";
1768894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "#include <llvm/Support/MathExtras.h>\n";
1769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "#include <llvm/Pass.h>\n";
1770894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "#include <llvm/PassManager.h>\n";
1771894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "#include <llvm/ADT/SmallVector.h>\n";
1772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "#include <llvm/Analysis/Verifier.h>\n";
1773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "#include <llvm/Assembly/PrintModulePass.h>\n";
1774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "#include <algorithm>\n";
1775894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "using namespace llvm;\n\n";
1776894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "Module* " << fname << "();\n\n";
1777894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "int main(int argc, char**argv) {\n";
1778894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "  Module* Mod = " << fname << "();\n";
1779894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "  verifyModule(*Mod, PrintMessageAction);\n";
1780894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "  PassManager PM;\n";
1781894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "  PM.add(createPrintModulePass(&outs()));\n";
1782894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "  PM.run(*Mod);\n";
1783894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "  return 0;\n";
1784894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "}\n\n";
1785894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printModule(fname,mName);
1786894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1787894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1788894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printModule(const std::string& fname,
1789894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            const std::string& mName) {
1790894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "Module* " << fname << "() {";
1791894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out,1) << "// Module Construction";
1792894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "Module* mod = new Module(\"";
1793894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printEscapedString(mName);
1794894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "\", getGlobalContext());";
1795894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!TheModule->getTargetTriple().empty()) {
1796894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out) << "mod->setDataLayout(\"" << TheModule->getDataLayout() << "\");";
1797894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1798894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!TheModule->getTargetTriple().empty()) {
1799894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out) << "mod->setTargetTriple(\"" << TheModule->getTargetTriple()
1800894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            << "\");";
1801894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1802894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1803894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!TheModule->getModuleInlineAsm().empty()) {
1804894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out) << "mod->setModuleInlineAsm(\"";
1805894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printEscapedString(TheModule->getModuleInlineAsm());
1806894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\");";
1807894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1808894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out);
1809894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1810894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Loop over the dependent libraries and emit them.
1811894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Module::lib_iterator LI = TheModule->lib_begin();
1812894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Module::lib_iterator LE = TheModule->lib_end();
1813894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  while (LI != LE) {
1814894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "mod->addLibrary(\"" << *LI << "\");";
1815894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    nl(Out);
1816894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ++LI;
1817894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1818894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printModuleBody();
1819894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out) << "return mod;";
1820894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out,-1) << "}";
1821894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  nl(Out);
1822894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1823894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1824894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printContents(const std::string& fname,
1825894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              const std::string& mName) {
1826894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "\nModule* " << fname << "(Module *mod) {\n";
1827894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "\nmod->setModuleIdentifier(\"";
1828894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printEscapedString(mName);
1829894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "\");\n";
1830894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printModuleBody();
1831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "\nreturn mod;\n";
1832894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "\n}\n";
1833894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1834894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1835894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printFunction(const std::string& fname,
1836894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              const std::string& funcName) {
1837894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Function* F = TheModule->getFunction(funcName);
1838894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!F) {
1839894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    error(std::string("Function '") + funcName + "' not found in input module");
1840894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
1841894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1842894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "\nFunction* " << fname << "(Module *mod) {\n";
1843894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printFunctionUses(F);
1844894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printFunctionHead(F);
1845894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printFunctionBody(F);
1846894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "return " << getCppName(F) << ";\n";
1847894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "}\n";
1848894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1849894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1850894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printFunctions() {
1851894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Module::FunctionListType &funcs = TheModule->getFunctionList();
1852894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Module::const_iterator I  = funcs.begin();
1853894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Module::const_iterator IE = funcs.end();
1854894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1855894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (; I != IE; ++I) {
1856894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const Function &func = *I;
1857894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!func.isDeclaration()) {
1858894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      std::string name("define_");
1859894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      name += func.getName();
1860894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      printFunction(name, func.getName());
1861894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1862894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1863894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1864894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1865894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CppWriter::printVariable(const std::string& fname,
1866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              const std::string& varName) {
1867894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const GlobalVariable* GV = TheModule->getNamedGlobal(varName);
1868894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1869894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!GV) {
1870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    error(std::string("Variable '") + varName + "' not found in input module");
1871894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
1872894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1873894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n";
1874894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printVariableUses(GV);
1875894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printVariableHead(GV);
1876894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printVariableBody(GV);
1877894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "return " << getCppName(GV) << ";\n";
1878894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "}\n";
1879894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1880894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
188119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid CppWriter::printType(const std::string &fname,
188219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                          const std::string &typeName) {
188319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Type* Ty = TheModule->getTypeByName(typeName);
1884894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!Ty) {
1885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    error(std::string("Type '") + typeName + "' not found in input module");
1886894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
1887894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1888894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "\nType* " << fname << "(Module *mod) {\n";
1889894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printType(Ty);
1890894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "return " << getCppName(Ty) << ";\n";
1891894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "}\n";
1892894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1893894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1894894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool CppWriter::runOnModule(Module &M) {
1895894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  TheModule = &M;
1896894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1897894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Emit a header
1898894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n";
1899894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1900894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the name of the function we're supposed to generate
1901894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::string fname = FuncName.getValue();
1902894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1903894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the name of the thing we are to generate
1904894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::string tgtname = NameToGenerate.getValue();
1905894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (GenerationType == GenModule ||
1906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GenerationType == GenContents ||
1907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GenerationType == GenProgram ||
1908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GenerationType == GenFunctions) {
1909894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (tgtname == "!bad!") {
1910894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (M.getModuleIdentifier() == "-")
1911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        tgtname = "<stdin>";
1912894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else
1913894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        tgtname = M.getModuleIdentifier();
1914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1915894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (tgtname == "!bad!")
1916894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    error("You must use the -for option with -gen-{function,variable,type}");
1917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1918894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (WhatToGenerate(GenerationType)) {
1919894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman   case GenProgram:
1920894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (fname.empty())
1921894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      fname = "makeLLVMModule";
1922894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printProgram(fname,tgtname);
1923894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1924894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman   case GenModule:
1925894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (fname.empty())
1926894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      fname = "makeLLVMModule";
1927894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printModule(fname,tgtname);
1928894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1929894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman   case GenContents:
1930894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (fname.empty())
1931894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      fname = "makeLLVMModuleContents";
1932894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printContents(fname,tgtname);
1933894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1934894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman   case GenFunction:
1935894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (fname.empty())
1936894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      fname = "makeLLVMFunction";
1937894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printFunction(fname,tgtname);
1938894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1939894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman   case GenFunctions:
1940894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printFunctions();
1941894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1942894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman   case GenInline:
1943894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (fname.empty())
1944894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      fname = "makeLLVMInline";
1945894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printInline(fname,tgtname);
1946894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1947894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman   case GenVariable:
1948894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (fname.empty())
1949894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      fname = "makeLLVMVariable";
1950894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printVariable(fname,tgtname);
1951894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1952894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman   case GenType:
1953894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (fname.empty())
1954894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      fname = "makeLLVMType";
1955894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printType(fname,tgtname);
1956894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1957894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman   default:
1958894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    error("Invalid generation option");
1959894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1960894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1961894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
1962894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1963894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1964894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanchar CppWriter::ID = 0;
1965894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1966894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
1967894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                       External Interface declaration
1968894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
1969894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1970894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool CPPTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
1971894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           formatted_raw_ostream &o,
1972894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           CodeGenFileType FileType,
1973894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           CodeGenOpt::Level OptLevel,
1974894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           bool DisableVerify) {
1975894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (FileType != TargetMachine::CGFT_AssemblyFile) return true;
1976894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  PM.add(new CppWriter(o));
1977894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
1978894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1979