Modify linear scan register allocator to use the two-address
[oota-llvm.git] / tools / llvm-nm / llvm-nm.cpp
1 //===-- llvm-nm.cpp - Symbol table dumping utility for llvm ---------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This program is a utility that works like traditional Unix "nm",
11 // that is, it prints out the names of symbols in a bytecode file,
12 // along with some information about each symbol.
13 // 
14 // This "nm" does not print symbols' addresses. It supports many of
15 // the features of GNU "nm", including its different output formats.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "llvm/Module.h"
20 #include "llvm/Bytecode/Reader.h"
21 #include "Support/CommandLine.h"
22 #include "Support/FileUtilities.h"
23 #include <cctype>
24 #include <cstring>
25
26 using namespace llvm;
27
28 namespace {
29   enum OutputFormatTy { bsd, sysv, posix };
30   cl::opt<OutputFormatTy>
31   OutputFormat("format",
32        cl::desc("Specify output format"),
33          cl::values(clEnumVal(bsd,   "BSD format"),
34                     clEnumVal(sysv,  "System V format"),
35                     clEnumVal(posix, "POSIX.2 format"), 0), cl::init(bsd));
36   cl::alias OutputFormat2("f", cl::desc("Alias for --format"),
37                           cl::aliasopt(OutputFormat));
38
39   cl::list<std::string> 
40   InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
41                  cl::ZeroOrMore);
42
43   cl::opt<bool> UndefinedOnly("undefined-only",
44                               cl::desc("Show only undefined symbols"));
45   cl::alias UndefinedOnly2("u", cl::desc("Alias for --undefined-only"),
46                            cl::aliasopt(UndefinedOnly));
47
48   cl::opt<bool> DefinedOnly("defined-only",
49                             cl::desc("Show only defined symbols"));
50
51   cl::opt<bool> ExternalOnly("extern-only",
52                              cl::desc("Show only external symbols"));
53   cl::alias ExternalOnly2("g", cl::desc("Alias for --extern-only"),
54                           cl::aliasopt(ExternalOnly));
55
56   cl::opt<bool> BSDFormat("B", cl::desc("Alias for --format=bsd"));
57   cl::opt<bool> POSIXFormat("P", cl::desc("Alias for --format=posix"));
58
59   bool MultipleFiles = false;
60
61   std::string ToolName;
62 };
63
64 char TypeCharForSymbol (GlobalValue &GV) {
65   if (GV.isExternal ())                                     return 'U';
66   if (GV.hasLinkOnceLinkage ())                             return 'C';
67   if (GV.hasWeakLinkage ())                                 return 'W';
68   if (isa<Function> (GV) && GV.hasInternalLinkage ())       return 't';
69   if (isa<Function> (GV))                                   return 'T';
70   if (isa<GlobalVariable> (GV) && GV.hasInternalLinkage ()) return 'd';
71   if (isa<GlobalVariable> (GV))                             return 'D';
72                                                             return '?';
73 }
74
75 void DumpSymbolNameForGlobalValue (GlobalValue &GV) {
76   const std::string SymbolAddrStr = "        "; // Not used yet...
77   char TypeChar = TypeCharForSymbol (GV);
78   if ((TypeChar != 'U') && UndefinedOnly)
79     return;
80   if ((TypeChar == 'U') && DefinedOnly)
81     return;
82   if (GV.hasInternalLinkage () && ExternalOnly)
83     return;
84   if (OutputFormat == posix) {
85     std::cout << GV.getName () << " " << TypeCharForSymbol (GV) << " "
86               << SymbolAddrStr << "\n";
87   } else if (OutputFormat == bsd) {
88     std::cout << SymbolAddrStr << " " << TypeCharForSymbol (GV) << " "
89               << GV.getName () << "\n";
90   } else if (OutputFormat == sysv) {
91     std::string PaddedName (GV.getName ());
92     while (PaddedName.length () < 20)
93       PaddedName += " ";
94     std::cout << PaddedName << "|" << SymbolAddrStr << "|   "
95               << TypeCharForSymbol (GV)
96               << "  |                  |      |     |\n";
97   }
98 }
99
100 void DumpSymbolNamesFromModule (Module *M) {
101   const std::string &Filename = M->getModuleIdentifier ();
102   if (OutputFormat == posix && MultipleFiles) {
103     std::cout << Filename << ":\n";
104   } else if (OutputFormat == bsd && MultipleFiles) {
105     std::cout << "\n" << Filename << ":\n";
106   } else if (OutputFormat == sysv) {
107     std::cout << "\n\nSymbols from " << Filename << ":\n\n"
108               << "Name                  Value   Class        Type"
109               << "         Size   Line  Section\n";
110   }
111   std::for_each (M->begin (), M->end (), DumpSymbolNameForGlobalValue);
112   std::for_each (M->gbegin (), M->gend (), DumpSymbolNameForGlobalValue);
113 }
114
115 void DumpSymbolNamesFromFile (std::string &Filename) {
116   std::string ErrorMessage;
117   if (Filename != "-" && !FileOpenable (Filename)) {
118     std::cerr << ToolName << ": " << Filename << ": " << strerror (errno)
119               << "\n";
120     return;
121   }
122   // Note: Currently we do not support reading an archive from stdin.
123   if (Filename == "-" || IsBytecode (Filename)) {
124     Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
125     if (Result) {
126       DumpSymbolNamesFromModule (Result);
127     } else {
128       std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
129       return;
130     }
131   } else if (IsArchive (Filename)) {
132     std::vector<Module *> Modules;
133     if (ReadArchiveFile (Filename, Modules, &ErrorMessage)) {
134       std::cerr << ToolName << ": " << Filename << ": "
135                 << ErrorMessage << "\n";
136       return;
137     }
138     MultipleFiles = true;
139     std::for_each (Modules.begin (), Modules.end (), DumpSymbolNamesFromModule);
140   } else {
141     std::cerr << ToolName << ": " << Filename << ": "
142               << "unrecognizable file type\n";
143     return;
144   }
145 }
146
147 int main(int argc, char **argv) {
148   cl::ParseCommandLineOptions(argc, argv, " llvm symbol table dumper\n");
149   ToolName = argv[0];
150   if (BSDFormat) OutputFormat = bsd;
151   if (POSIXFormat) OutputFormat = posix;
152
153   switch (InputFilenames.size()) {
154   case 0: InputFilenames.push_back("-");
155   case 1: break;
156   default: MultipleFiles = true;
157   }
158
159   std::for_each (InputFilenames.begin (), InputFilenames.end (),
160                  DumpSymbolNamesFromFile);
161   return 0;
162 }