diff options
author | Chris Lattner <sabre@nondot.org> | 2001-09-10 07:58:01 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2001-09-10 07:58:01 +0000 |
commit | 70cc3397f84c2e1fd69c059a0ef89e398e847b00 (patch) | |
tree | ca2156daf75e4abf788d92925bdce4063da36e58 /lib/VMCore | |
parent | 7720c8e1a7a252e983e3f3e7f841d7901dfea80c (diff) |
Implement global variable support
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@530 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/VMCore')
-rw-r--r-- | lib/VMCore/AsmWriter.cpp | 16 | ||||
-rw-r--r-- | lib/VMCore/Function.cpp | 31 | ||||
-rw-r--r-- | lib/VMCore/Module.cpp | 12 | ||||
-rw-r--r-- | lib/VMCore/SlotCalculator.cpp | 15 |
4 files changed, 67 insertions, 7 deletions
diff --git a/lib/VMCore/AsmWriter.cpp b/lib/VMCore/AsmWriter.cpp index a8951e61d1..9056865849 100644 --- a/lib/VMCore/AsmWriter.cpp +++ b/lib/VMCore/AsmWriter.cpp @@ -14,6 +14,7 @@ #include "llvm/Analysis/SlotCalculator.h" #include "llvm/Module.h" #include "llvm/Method.h" +#include "llvm/GlobalVariable.h" #include "llvm/BasicBlock.h" #include "llvm/ConstPoolVals.h" #include "llvm/iOther.h" @@ -81,6 +82,7 @@ public: } inline void write(const Module *M) { processModule(M); } + inline void write(const GlobalVariable *G) { processGlobal(G); } inline void write(const Method *M) { processMethod(M); } inline void write(const BasicBlock *BB) { processBasicBlock(BB); } inline void write(const Instruction *I) { processInstruction(I); } @@ -90,11 +92,12 @@ private : void processModule(const Module *M); void processSymbolTable(const SymbolTable &ST); void processConstant(const ConstPoolVal *CPV); + void processGlobal(const GlobalVariable *GV); void processMethod(const Method *M); void processMethodArgument(const MethodArgument *MA); void processBasicBlock(const BasicBlock *BB); void processInstruction(const Instruction *I); - + void writeOperand(const Value *Op, bool PrintType, bool PrintName = true); }; @@ -109,13 +112,22 @@ void AssemblyWriter::processModule(const Module *M) { // Loop over the symbol table, emitting all named constants... if (M->hasSymbolTable()) processSymbolTable(*M->getSymbolTable()); - + + for_each(M->gbegin(), M->gend(), + bind_obj(this, &AssemblyWriter::processGlobal)); + Out << "implementation\n"; // Output all of the methods... for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::processMethod)); } +void AssemblyWriter::processGlobal(const GlobalVariable *GV) { + Out << "global "; + if (GV->hasName()) Out << "%" << GV->getName() << " = "; + Out << GV->getType()->getDescription() << endl; +} + // processSymbolTable - Run through symbol table looking for named constants // if a named constant is found, emit it's declaration... diff --git a/lib/VMCore/Function.cpp b/lib/VMCore/Function.cpp index bd342a2473..0ec6411323 100644 --- a/lib/VMCore/Function.cpp +++ b/lib/VMCore/Function.cpp @@ -1,6 +1,7 @@ //===-- Method.cpp - Implement the Method class ------------------*- C++ -*--=// // -// This file implements the Method class for the VMCore library. +// This file implements the Method & GlobalVariable classes for the VMCore +// library. // //===----------------------------------------------------------------------===// @@ -9,9 +10,15 @@ #include "llvm/SymbolTable.h" #include "llvm/Module.h" #include "llvm/Method.h" +#include "llvm/GlobalVariable.h" #include "llvm/BasicBlock.h" #include "llvm/iOther.h" +//===----------------------------------------------------------------------===// +// Method Implementation +//===----------------------------------------------------------------------===// + + // Instantiate Templates - This ugliness is the price we have to pay // for having a ValueHolderImpl.h file seperate from ValueHolder.h! :( // @@ -74,3 +81,25 @@ const MethodType *Method::getMethodType() const { void Method::dropAllReferences() { for_each(begin(), end(), std::mem_fun(&BasicBlock::dropAllReferences)); } + +//===----------------------------------------------------------------------===// +// GlobalVariable Implementation +//===----------------------------------------------------------------------===// + +GlobalVariable::GlobalVariable(const Type *Ty, const string &Name = "") + : Value(Ty, Value::GlobalVal, Name), Parent(0) { + assert(Ty->isPointerType() && + (!Ty->isPointerType()->isArrayType() || // No unsized array pointers + Ty->isPointerType()->isArrayType()->isSized()) && + "Global Variables must be pointers to a sized type!"); +} + +// Specialize setName to take care of symbol table majik +void GlobalVariable::setName(const string &name, SymbolTable *ST) { + Module *P; + assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) && + "Invalid symtab argument!"); + if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this); + Value::setName(name); + if (P && getName() != "") P->getSymbolTableSure()->insert(this); +} diff --git a/lib/VMCore/Module.cpp b/lib/VMCore/Module.cpp index 4ab8bb06fc..5fc11d1b10 100644 --- a/lib/VMCore/Module.cpp +++ b/lib/VMCore/Module.cpp @@ -6,6 +6,7 @@ #include "llvm/Module.h" #include "llvm/Method.h" +#include "llvm/GlobalVariable.h" #include "llvm/BasicBlock.h" #include "llvm/InstrTypes.h" #include "llvm/ValueHolderImpl.h" @@ -15,15 +16,18 @@ // Instantiate Templates - This ugliness is the price we have to pay // for having a DefHolderImpl.h file seperate from DefHolder.h! :( // +template class ValueHolder<GlobalVariable, Module, Module>; template class ValueHolder<Method, Module, Module>; Module::Module() : Value(Type::VoidTy, Value::ModuleVal, ""), SymTabValue(this), - MethodList(this, this) { + GlobalList(this, this), MethodList(this, this) { } Module::~Module() { dropAllReferences(); + GlobalList.delete_all(); + GlobalList.setParent(0); MethodList.delete_all(); MethodList.setParent(0); } @@ -46,6 +50,12 @@ void Module::dropAllReferences() { // reduceApply - Apply the specified function to all of the methods in this // module. The result values are or'd together and the result is returned. // +bool Module::reduceApply(bool (*Func)(GlobalVariable*)) { + return reduce_apply_bool(gbegin(), gend(), Func); +} +bool Module::reduceApply(bool (*Func)(const GlobalVariable*)) const { + return reduce_apply_bool(gbegin(), gend(), Func); +} bool Module::reduceApply(bool (*Func)(Method*)) { return reduce_apply_bool(begin(), end(), Func); } diff --git a/lib/VMCore/SlotCalculator.cpp b/lib/VMCore/SlotCalculator.cpp index 9af5a387e1..cc7d4e56df 100644 --- a/lib/VMCore/SlotCalculator.cpp +++ b/lib/VMCore/SlotCalculator.cpp @@ -12,6 +12,7 @@ #include "llvm/Analysis/SlotCalculator.h" #include "llvm/Analysis/ConstantsScanner.h" #include "llvm/Method.h" +#include "llvm/GlobalVariable.h" #include "llvm/Module.h" #include "llvm/BasicBlock.h" #include "llvm/ConstPoolVals.h" @@ -68,13 +69,21 @@ SlotCalculator::SlotCalculator(const Method *M, bool IgnoreNamed) { // void SlotCalculator::processModule() { SC_DEBUG("begin processModule!\n"); - // Currently, the only module level declarations are methods and method - // prototypes. We simply scavenge the types out of the methods, then add the - // methods themselves to the value table... + + // Add all of the global variables to the value table... + // + for_each(TheModule->gbegin(), TheModule->gend(), + bind_obj(this, &SlotCalculator::insertValue)); + + // Scavenge the types out of the methods, then add the methods themselves to + // the value table... // for_each(TheModule->begin(), TheModule->end(), // Insert methods... bind_obj(this, &SlotCalculator::insertValue)); + // Insert constants that are named at module level into the slot pool so that + // the module symbol table can refer to them... + // if (TheModule->hasSymbolTable() && !IgnoreNamedNodes) { SC_DEBUG("Inserting SymbolTable values:\n"); processSymbolTable(TheModule->getSymbolTable()); |