It turns out most of the thumb2 instructions are not allowed to touch SP. The semanti...
[llvm/avr.git] / lib / VMCore / Module.cpp
blobe06e79a026820e96dbb163e5c67f3dfba14d7608
1 //===-- Module.cpp - Implement the Module class ---------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Module class for the VMCore library.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Module.h"
15 #include "llvm/InstrTypes.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/LLVMContext.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/Support/LeakDetector.h"
22 #include "SymbolTableListTraitsImpl.h"
23 #include "llvm/TypeSymbolTable.h"
24 #include <algorithm>
25 #include <cstdarg>
26 #include <cstdlib>
27 using namespace llvm;
29 //===----------------------------------------------------------------------===//
30 // Methods to implement the globals and functions lists.
33 GlobalVariable *ilist_traits<GlobalVariable>::createSentinel() {
34 GlobalVariable *Ret = new GlobalVariable(getGlobalContext(), Type::Int32Ty,
35 false, GlobalValue::ExternalLinkage);
36 // This should not be garbage monitored.
37 LeakDetector::removeGarbageObject(Ret);
38 return Ret;
40 GlobalAlias *ilist_traits<GlobalAlias>::createSentinel() {
41 GlobalAlias *Ret = new GlobalAlias(Type::Int32Ty,
42 GlobalValue::ExternalLinkage);
43 // This should not be garbage monitored.
44 LeakDetector::removeGarbageObject(Ret);
45 return Ret;
48 // Explicit instantiations of SymbolTableListTraits since some of the methods
49 // are not in the public header file.
50 template class SymbolTableListTraits<GlobalVariable, Module>;
51 template class SymbolTableListTraits<Function, Module>;
52 template class SymbolTableListTraits<GlobalAlias, Module>;
54 //===----------------------------------------------------------------------===//
55 // Primitive Module methods.
58 Module::Module(const StringRef &MID, LLVMContext& C)
59 : Context(C), ModuleID(MID), DataLayout("") {
60 ValSymTab = new ValueSymbolTable();
61 TypeSymTab = new TypeSymbolTable();
64 Module::~Module() {
65 dropAllReferences();
66 GlobalList.clear();
67 FunctionList.clear();
68 AliasList.clear();
69 LibraryList.clear();
70 NamedMDList.clear();
71 delete ValSymTab;
72 delete TypeSymTab;
75 /// Target endian information...
76 Module::Endianness Module::getEndianness() const {
77 std::string temp = DataLayout;
78 Module::Endianness ret = AnyEndianness;
80 while (!temp.empty()) {
81 std::string token = getToken(temp, "-");
83 if (token[0] == 'e') {
84 ret = LittleEndian;
85 } else if (token[0] == 'E') {
86 ret = BigEndian;
90 return ret;
93 /// Target Pointer Size information...
94 Module::PointerSize Module::getPointerSize() const {
95 std::string temp = DataLayout;
96 Module::PointerSize ret = AnyPointerSize;
98 while (!temp.empty()) {
99 std::string token = getToken(temp, "-");
100 char signal = getToken(token, ":")[0];
102 if (signal == 'p') {
103 int size = atoi(getToken(token, ":").c_str());
104 if (size == 32)
105 ret = Pointer32;
106 else if (size == 64)
107 ret = Pointer64;
111 return ret;
114 /// getNamedValue - Return the first global value in the module with
115 /// the specified name, of arbitrary type. This method returns null
116 /// if a global with the specified name is not found.
117 GlobalValue *Module::getNamedValue(const StringRef &Name) const {
118 return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name));
121 //===----------------------------------------------------------------------===//
122 // Methods for easy access to the functions in the module.
125 // getOrInsertFunction - Look up the specified function in the module symbol
126 // table. If it does not exist, add a prototype for the function and return
127 // it. This is nice because it allows most passes to get away with not handling
128 // the symbol table directly for this common task.
130 Constant *Module::getOrInsertFunction(const StringRef &Name,
131 const FunctionType *Ty,
132 AttrListPtr AttributeList) {
133 // See if we have a definition for the specified function already.
134 GlobalValue *F = getNamedValue(Name);
135 if (F == 0) {
136 // Nope, add it
137 Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
138 if (!New->isIntrinsic()) // Intrinsics get attrs set on construction
139 New->setAttributes(AttributeList);
140 FunctionList.push_back(New);
141 return New; // Return the new prototype.
144 // Okay, the function exists. Does it have externally visible linkage?
145 if (F->hasLocalLinkage()) {
146 // Clear the function's name.
147 F->setName("");
148 // Retry, now there won't be a conflict.
149 Constant *NewF = getOrInsertFunction(Name, Ty);
150 F->setName(Name);
151 return NewF;
154 // If the function exists but has the wrong type, return a bitcast to the
155 // right type.
156 if (F->getType() != PointerType::getUnqual(Ty))
157 return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty));
159 // Otherwise, we just found the existing function or a prototype.
160 return F;
163 Constant *Module::getOrInsertTargetIntrinsic(const StringRef &Name,
164 const FunctionType *Ty,
165 AttrListPtr AttributeList) {
166 // See if we have a definition for the specified function already.
167 GlobalValue *F = getNamedValue(Name);
168 if (F == 0) {
169 // Nope, add it
170 Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
171 New->setAttributes(AttributeList);
172 FunctionList.push_back(New);
173 return New; // Return the new prototype.
176 // Otherwise, we just found the existing function or a prototype.
177 return F;
180 Constant *Module::getOrInsertFunction(const StringRef &Name,
181 const FunctionType *Ty) {
182 AttrListPtr AttributeList = AttrListPtr::get((AttributeWithIndex *)0, 0);
183 return getOrInsertFunction(Name, Ty, AttributeList);
186 // getOrInsertFunction - Look up the specified function in the module symbol
187 // table. If it does not exist, add a prototype for the function and return it.
188 // This version of the method takes a null terminated list of function
189 // arguments, which makes it easier for clients to use.
191 Constant *Module::getOrInsertFunction(const StringRef &Name,
192 AttrListPtr AttributeList,
193 const Type *RetTy, ...) {
194 va_list Args;
195 va_start(Args, RetTy);
197 // Build the list of argument types...
198 std::vector<const Type*> ArgTys;
199 while (const Type *ArgTy = va_arg(Args, const Type*))
200 ArgTys.push_back(ArgTy);
202 va_end(Args);
204 // Build the function type and chain to the other getOrInsertFunction...
205 return getOrInsertFunction(Name,
206 FunctionType::get(RetTy, ArgTys, false),
207 AttributeList);
210 Constant *Module::getOrInsertFunction(const StringRef &Name,
211 const Type *RetTy, ...) {
212 va_list Args;
213 va_start(Args, RetTy);
215 // Build the list of argument types...
216 std::vector<const Type*> ArgTys;
217 while (const Type *ArgTy = va_arg(Args, const Type*))
218 ArgTys.push_back(ArgTy);
220 va_end(Args);
222 // Build the function type and chain to the other getOrInsertFunction...
223 return getOrInsertFunction(Name,
224 FunctionType::get(RetTy, ArgTys, false),
225 AttrListPtr::get((AttributeWithIndex *)0, 0));
228 // getFunction - Look up the specified function in the module symbol table.
229 // If it does not exist, return null.
231 Function *Module::getFunction(const StringRef &Name) const {
232 return dyn_cast_or_null<Function>(getNamedValue(Name));
235 //===----------------------------------------------------------------------===//
236 // Methods for easy access to the global variables in the module.
239 /// getGlobalVariable - Look up the specified global variable in the module
240 /// symbol table. If it does not exist, return null. The type argument
241 /// should be the underlying type of the global, i.e., it should not have
242 /// the top-level PointerType, which represents the address of the global.
243 /// If AllowLocal is set to true, this function will return types that
244 /// have an local. By default, these types are not returned.
246 GlobalVariable *Module::getGlobalVariable(const StringRef &Name,
247 bool AllowLocal) const {
248 if (GlobalVariable *Result =
249 dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
250 if (AllowLocal || !Result->hasLocalLinkage())
251 return Result;
252 return 0;
255 /// getOrInsertGlobal - Look up the specified global in the module symbol table.
256 /// 1. If it does not exist, add a declaration of the global and return it.
257 /// 2. Else, the global exists but has the wrong type: return the function
258 /// with a constantexpr cast to the right type.
259 /// 3. Finally, if the existing global is the correct delclaration, return the
260 /// existing global.
261 Constant *Module::getOrInsertGlobal(const StringRef &Name, const Type *Ty) {
262 // See if we have a definition for the specified global already.
263 GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name));
264 if (GV == 0) {
265 // Nope, add it
266 GlobalVariable *New =
267 new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
268 0, Name);
269 return New; // Return the new declaration.
272 // If the variable exists but has the wrong type, return a bitcast to the
273 // right type.
274 if (GV->getType() != PointerType::getUnqual(Ty))
275 return ConstantExpr::getBitCast(GV, PointerType::getUnqual(Ty));
277 // Otherwise, we just found the existing function or a prototype.
278 return GV;
281 //===----------------------------------------------------------------------===//
282 // Methods for easy access to the global variables in the module.
285 // getNamedAlias - Look up the specified global in the module symbol table.
286 // If it does not exist, return null.
288 GlobalAlias *Module::getNamedAlias(const StringRef &Name) const {
289 return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name));
292 /// getNamedMetadata - Return the first NamedMDNode in the module with the
293 /// specified name. This method returns null if a NamedMDNode with the
294 //// specified name is not found.
295 NamedMDNode *Module::getNamedMetadata(const StringRef &Name) const {
296 return dyn_cast_or_null<NamedMDNode>(getValueSymbolTable().lookup(Name));
299 /// getOrInsertNamedMetadata - Return the first named MDNode in the module
300 /// with the specified name. This method returns a new NamedMDNode if a
301 /// NamedMDNode with the specified name is not found.
302 NamedMDNode *Module::getOrInsertNamedMetadata(const StringRef &Name) {
303 NamedMDNode *NMD =
304 dyn_cast_or_null<NamedMDNode>(getValueSymbolTable().lookup(Name));
305 if (!NMD)
306 NMD = NamedMDNode::Create(Name, NULL, 0, this);
307 return NMD;
310 //===----------------------------------------------------------------------===//
311 // Methods for easy access to the types in the module.
315 // addTypeName - Insert an entry in the symbol table mapping Str to Type. If
316 // there is already an entry for this name, true is returned and the symbol
317 // table is not modified.
319 bool Module::addTypeName(const StringRef &Name, const Type *Ty) {
320 TypeSymbolTable &ST = getTypeSymbolTable();
322 if (ST.lookup(Name)) return true; // Already in symtab...
324 // Not in symbol table? Set the name with the Symtab as an argument so the
325 // type knows what to update...
326 ST.insert(Name, Ty);
328 return false;
331 /// getTypeByName - Return the type with the specified name in this module, or
332 /// null if there is none by that name.
333 const Type *Module::getTypeByName(const StringRef &Name) const {
334 const TypeSymbolTable &ST = getTypeSymbolTable();
335 return cast_or_null<Type>(ST.lookup(Name));
338 // getTypeName - If there is at least one entry in the symbol table for the
339 // specified type, return it.
341 std::string Module::getTypeName(const Type *Ty) const {
342 const TypeSymbolTable &ST = getTypeSymbolTable();
344 TypeSymbolTable::const_iterator TI = ST.begin();
345 TypeSymbolTable::const_iterator TE = ST.end();
346 if ( TI == TE ) return ""; // No names for types
348 while (TI != TE && TI->second != Ty)
349 ++TI;
351 if (TI != TE) // Must have found an entry!
352 return TI->first;
353 return ""; // Must not have found anything...
356 //===----------------------------------------------------------------------===//
357 // Other module related stuff.
361 // dropAllReferences() - This function causes all the subelementss to "let go"
362 // of all references that they are maintaining. This allows one to 'delete' a
363 // whole module at a time, even though there may be circular references... first
364 // all references are dropped, and all use counts go to zero. Then everything
365 // is deleted for real. Note that no operations are valid on an object that
366 // has "dropped all references", except operator delete.
368 void Module::dropAllReferences() {
369 for(Module::iterator I = begin(), E = end(); I != E; ++I)
370 I->dropAllReferences();
372 for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I)
373 I->dropAllReferences();
375 for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I)
376 I->dropAllReferences();
379 void Module::addLibrary(const StringRef& Lib) {
380 for (Module::lib_iterator I = lib_begin(), E = lib_end(); I != E; ++I)
381 if (*I == Lib)
382 return;
383 LibraryList.push_back(Lib);
386 void Module::removeLibrary(const StringRef& Lib) {
387 LibraryListType::iterator I = LibraryList.begin();
388 LibraryListType::iterator E = LibraryList.end();
389 for (;I != E; ++I)
390 if (*I == Lib) {
391 LibraryList.erase(I);
392 return;