~ubuntu-branches/ubuntu/trusty/llvm-toolchain-snapshot/trusty-201310232150

« back to all changes in this revision

Viewing changes to lib/IR/AsmWriter.cpp

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-27 15:01:57 UTC
  • mfrom: (0.10.1) (0.9.1) (0.8.1) (0.7.1) (0.6.1) (0.5.2)
  • Revision ID: package-import@ubuntu.com-20130527150157-tdkrsjpuvht7v0qx
Tags: 1:3.4~svn182733-1~exp1
* New snapshot release (3.4 release)
* Add a symlink of libLLVM-3.4.so.1 to usr/lib/llvm-3.4/lib/libLLVM-3.4.so
    to fix make the llvm-config-3.4 --libdir work (Closes: #708677)
  * Various packages rename to allow co installations:
    * libclang1 => libclang1-3.4
    * libclang1-dbg => libclang1-3.4-dbg
    * libclang-dev => libclang-3.4-dev
    * libclang-common-dev => libclang-common-3.4-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
//
15
15
//===----------------------------------------------------------------------===//
16
16
 
 
17
#include "AsmWriter.h"
 
18
 
17
19
#include "llvm/Assembly/Writer.h"
18
20
#include "llvm/ADT/DenseMap.h"
19
21
#include "llvm/ADT/STLExtras.h"
33
35
#include "llvm/IR/TypeFinder.h"
34
36
#include "llvm/IR/ValueSymbolTable.h"
35
37
#include "llvm/Support/CFG.h"
36
 
#include "llvm/Support/CommandLine.h"
37
38
#include "llvm/Support/Debug.h"
38
39
#include "llvm/Support/Dwarf.h"
39
40
#include "llvm/Support/ErrorHandling.h"
40
41
#include "llvm/Support/FormattedStream.h"
41
42
#include "llvm/Support/MathExtras.h"
 
43
 
42
44
#include <algorithm>
43
45
#include <cctype>
44
46
using namespace llvm;
45
47
 
46
 
static cl::opt<bool>
47
 
OldStyleAttrSyntax("enable-old-style-attr-syntax",
48
 
    cl::desc("Output attributes on functions rather than in attribute groups"),
49
 
    cl::Hidden,
50
 
    cl::init(false));
51
 
 
52
48
// Make virtual table appear in this compilation unit.
53
49
AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
54
50
 
160
156
                isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
161
157
}
162
158
 
163
 
//===----------------------------------------------------------------------===//
164
 
// TypePrinting Class: Type printing machinery
165
 
//===----------------------------------------------------------------------===//
166
 
 
167
 
/// TypePrinting - Type printing machinery.
168
 
namespace {
169
 
class TypePrinting {
170
 
  TypePrinting(const TypePrinting &) LLVM_DELETED_FUNCTION;
171
 
  void operator=(const TypePrinting&) LLVM_DELETED_FUNCTION;
172
 
public:
173
 
 
174
 
  /// NamedTypes - The named types that are used by the current module.
175
 
  TypeFinder NamedTypes;
176
 
 
177
 
  /// NumberedTypes - The numbered types, along with their value.
178
 
  DenseMap<StructType*, unsigned> NumberedTypes;
179
 
 
180
 
 
181
 
  TypePrinting() {}
182
 
  ~TypePrinting() {}
183
 
 
184
 
  void incorporateTypes(const Module &M);
185
 
 
186
 
  void print(Type *Ty, raw_ostream &OS);
187
 
 
188
 
  void printStructBody(StructType *Ty, raw_ostream &OS);
189
 
};
190
 
} // end anonymous namespace.
191
 
 
 
159
 
 
160
namespace llvm {
192
161
 
193
162
void TypePrinting::incorporateTypes(const Module &M) {
194
163
  NamedTypes.run(M, false);
320
289
    OS << '>';
321
290
}
322
291
 
323
 
 
324
 
 
325
292
//===----------------------------------------------------------------------===//
326
293
// SlotTracker Class: Enumerate slot numbers for unnamed values
327
294
//===----------------------------------------------------------------------===//
328
 
 
329
 
namespace {
330
 
 
331
295
/// This class provides computation of slot numbers for LLVM Assembly writing.
332
296
///
333
297
class SlotTracker {
425
389
  void operator=(const SlotTracker &) LLVM_DELETED_FUNCTION;
426
390
};
427
391
 
428
 
}  // end anonymous namespace
429
 
 
 
392
SlotTracker *createSlotTracker(const Module *M) {
 
393
  return new SlotTracker(M);
 
394
}
430
395
 
431
396
static SlotTracker *createSlotTracker(const Value *V) {
432
397
  if (const Argument *FA = dyn_cast<Argument>(V))
1207
1172
    Out << "<badref>";
1208
1173
}
1209
1174
 
1210
 
void llvm::WriteAsOperand(raw_ostream &Out, const Value *V,
1211
 
                          bool PrintType, const Module *Context) {
 
1175
void WriteAsOperand(raw_ostream &Out, const Value *V,
 
1176
                    bool PrintType, const Module *Context) {
1212
1177
 
1213
1178
  // Fast path: Don't construct and populate a TypePrinting object if we
1214
1179
  // won't be needing any types printed.
1232
1197
  WriteAsOperandInternal(Out, V, &TypePrinter, 0, Context);
1233
1198
}
1234
1199
 
1235
 
namespace {
1236
 
 
1237
 
class AssemblyWriter {
1238
 
  formatted_raw_ostream &Out;
1239
 
  SlotTracker &Machine;
1240
 
  const Module *TheModule;
1241
 
  TypePrinting TypePrinter;
1242
 
  AssemblyAnnotationWriter *AnnotationWriter;
1243
 
 
1244
 
public:
1245
 
  inline AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
1246
 
                        const Module *M,
1247
 
                        AssemblyAnnotationWriter *AAW)
1248
 
    : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
1249
 
    if (M)
1250
 
      TypePrinter.incorporateTypes(*M);
1251
 
  }
1252
 
 
1253
 
  void printMDNodeBody(const MDNode *MD);
1254
 
  void printNamedMDNode(const NamedMDNode *NMD);
1255
 
 
1256
 
  void printModule(const Module *M);
1257
 
 
1258
 
  void writeOperand(const Value *Op, bool PrintType);
1259
 
  void writeParamOperand(const Value *Operand, AttributeSet Attrs,unsigned Idx);
1260
 
  void writeAtomic(AtomicOrdering Ordering, SynchronizationScope SynchScope);
1261
 
 
1262
 
  void writeAllMDNodes();
1263
 
  void writeAllAttributeGroups();
1264
 
 
1265
 
  void printTypeIdentities();
1266
 
  void printGlobal(const GlobalVariable *GV);
1267
 
  void printAlias(const GlobalAlias *GV);
1268
 
  void printFunction(const Function *F);
1269
 
  void printArgument(const Argument *FA, AttributeSet Attrs, unsigned Idx);
1270
 
  void printBasicBlock(const BasicBlock *BB);
1271
 
  void printInstruction(const Instruction &I);
1272
 
 
1273
 
private:
1274
 
  // printInfoComment - Print a little comment after the instruction indicating
1275
 
  // which slot it occupies.
1276
 
  void printInfoComment(const Value &V);
1277
 
};
1278
 
}  // end of anonymous namespace
 
1200
void AssemblyWriter::init() {
 
1201
  if (TheModule)
 
1202
    TypePrinter.incorporateTypes(*TheModule);
 
1203
}
 
1204
 
 
1205
 
 
1206
AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
 
1207
                               const Module *M,
 
1208
                               AssemblyAnnotationWriter *AAW)
 
1209
  : Out(o), TheModule(M), Machine(Mac), AnnotationWriter(AAW) {
 
1210
  init();
 
1211
}
 
1212
 
 
1213
AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, const Module *M,
 
1214
                               AssemblyAnnotationWriter *AAW)
 
1215
  : Out(o), TheModule(M), ModuleSlotTracker(createSlotTracker(M)),
 
1216
    Machine(*ModuleSlotTracker), AnnotationWriter(AAW) {
 
1217
  init();
 
1218
}
 
1219
 
 
1220
AssemblyWriter::~AssemblyWriter() { }
1279
1221
 
1280
1222
void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
1281
1223
  if (Operand == 0) {
1385
1327
    printFunction(I);
1386
1328
 
1387
1329
  // Output all attribute groups.
1388
 
  if (!OldStyleAttrSyntax && !Machine.as_empty()) {
 
1330
  if (!Machine.as_empty()) {
1389
1331
    Out << '\n';
1390
1332
    writeAllAttributeGroups();
1391
1333
  }
1613
1555
    Out << "; Materializable\n";
1614
1556
 
1615
1557
  const AttributeSet &Attrs = F->getAttributes();
1616
 
  if (!OldStyleAttrSyntax && Attrs.hasAttributes(AttributeSet::FunctionIndex)) {
 
1558
  if (Attrs.hasAttributes(AttributeSet::FunctionIndex)) {
1617
1559
    AttributeSet AS = Attrs.getFnAttributes();
1618
 
    std::string AttrStr = AS.getAsString(AttributeSet::FunctionIndex, false);
 
1560
    std::string AttrStr;
 
1561
 
 
1562
    unsigned Idx = 0;
 
1563
    for (unsigned E = AS.getNumSlots(); Idx != E; ++Idx)
 
1564
      if (AS.getSlotIndex(Idx) == AttributeSet::FunctionIndex)
 
1565
        break;
 
1566
 
 
1567
    for (AttributeSet::iterator I = AS.begin(Idx), E = AS.end(Idx);
 
1568
         I != E; ++I) {
 
1569
      Attribute Attr = *I;
 
1570
      if (!Attr.isStringAttribute()) {
 
1571
        if (!AttrStr.empty()) AttrStr += ' ';
 
1572
        AttrStr += Attr.getAsString();
 
1573
      }
 
1574
    }
 
1575
 
1619
1576
    if (!AttrStr.empty())
1620
1577
      Out << "; Function Attrs: " << AttrStr << '\n';
1621
1578
  }
1677
1634
  Out << ')';
1678
1635
  if (F->hasUnnamedAddr())
1679
1636
    Out << " unnamed_addr";
1680
 
  if (!OldStyleAttrSyntax) {
1681
 
    if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
1682
 
      Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttributes());
1683
 
  } else {
1684
 
    AttributeSet AS = Attrs.getFnAttributes();
1685
 
    std::string AttrStr = AS.getAsString(AttributeSet::FunctionIndex, false);
1686
 
    if (!AttrStr.empty())
1687
 
      Out << ' ' << AttrStr;
1688
 
  }
 
1637
  if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
 
1638
    Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttributes());
1689
1639
  if (F->hasSection()) {
1690
1640
    Out << " section \"";
1691
1641
    PrintEscapedString(F->getSection(), Out);
1771
1721
 
1772
1722
  // Output all of the instructions in the basic block...
1773
1723
  for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
1774
 
    printInstruction(*I);
1775
 
    Out << '\n';
 
1724
    printInstructionLine(*I);
1776
1725
  }
1777
1726
 
1778
1727
  if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
1779
1728
}
1780
1729
 
 
1730
/// printInstructionLine - Print an instruction and a newline character.
 
1731
void AssemblyWriter::printInstructionLine(const Instruction &I) {
 
1732
  printInstruction(I);
 
1733
  Out << '\n';
 
1734
}
 
1735
 
1781
1736
/// printInfoComment - Print a little comment after the instruction indicating
1782
1737
/// which slot it occupies.
1783
1738
///
2092
2047
      unsigned Kind = InstMD[i].first;
2093
2048
       if (Kind < MDNames.size()) {
2094
2049
         Out << ", !" << MDNames[Kind];
2095
 
      } else {
2096
 
        Out << ", !<unknown kind #" << Kind << ">";
2097
 
      }
 
2050
       } else {
 
2051
         Out << ", !<unknown kind #" << Kind << ">";
 
2052
       }
2098
2053
      Out << ' ';
2099
2054
      WriteAsOperandInternal(Out, InstMD[i].second, &TypePrinter, &Machine,
2100
2055
                             TheModule);
2126
2081
  }
2127
2082
}
2128
2083
 
 
2084
void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) {
 
2085
  Out << '!' << Slot << " = metadata ";
 
2086
  printMDNodeBody(Node);
 
2087
}
 
2088
 
2129
2089
void AssemblyWriter::writeAllMDNodes() {
2130
2090
  SmallVector<const MDNode *, 16> Nodes;
2131
2091
  Nodes.resize(Machine.mdn_size());
2134
2094
    Nodes[I->second] = cast<MDNode>(I->first);
2135
2095
 
2136
2096
  for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
2137
 
    Out << '!' << i << " = metadata ";
2138
 
    printMDNodeBody(Nodes[i]);
 
2097
    writeMDNode(i, Nodes[i]);
2139
2098
  }
2140
2099
}
2141
2100
 
2156
2115
  for (std::vector<std::pair<AttributeSet, unsigned> >::iterator
2157
2116
         I = asVec.begin(), E = asVec.end(); I != E; ++I)
2158
2117
    Out << "attributes #" << I->second << " = { "
2159
 
        << I->first.getAsString(AttributeSet::FunctionIndex, true, true)
2160
 
        << " }\n";
 
2118
        << I->first.getAsString(AttributeSet::FunctionIndex, true) << " }\n";
2161
2119
}
2162
2120
 
 
2121
} // namespace llvm
 
2122
 
2163
2123
//===----------------------------------------------------------------------===//
2164
2124
//                       External Interface declarations
2165
2125
//===----------------------------------------------------------------------===//