Implement global variable support
authorChris Lattner <sabre@nondot.org>
Mon, 10 Sep 2001 07:58:01 +0000 (07:58 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 10 Sep 2001 07:58:01 +0000 (07:58 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@530 91177308-0d34-0410-b5e6-96231b3b80d8

14 files changed:
include/llvm/Function.h
include/llvm/GlobalVariable.h [new file with mode: 0644]
include/llvm/Module.h
include/llvm/Value.h
lib/AsmParser/Lexer.l
lib/AsmParser/llvmAsmParser.y
lib/Bytecode/Reader/Reader.cpp
lib/Bytecode/Writer/SlotCalculator.cpp
lib/Bytecode/Writer/Writer.cpp
lib/VMCore/AsmWriter.cpp
lib/VMCore/Function.cpp
lib/VMCore/Module.cpp
lib/VMCore/SlotCalculator.cpp
test/Feature/globalvars.ll [new file with mode: 0644]

index b1d38dda4f996b0e5457363b1fc972aa0562000a..2be84d4e4cce92561999c980a364e8860237f062 100644 (file)
@@ -40,7 +40,7 @@ private:
 
   Module *Parent;                  // The module that contains this method
 
-  friend class ValueHolder<Method,Module, Module>;
+  friend class ValueHolder<Method, Module, Module>;
   void setParent(Module *parent);
 
 public:
diff --git a/include/llvm/GlobalVariable.h b/include/llvm/GlobalVariable.h
new file mode 100644 (file)
index 0000000..fd97989
--- /dev/null
@@ -0,0 +1,34 @@
+//===-- llvm/Global.h - Class to represent a global variable -----*- C++ -*--=//
+//
+// This file contains the declaration of the GlobalVariable class, which
+// represents a single global variable in the VM.
+//
+// Global variables are constant pointers that refer to hunks of space that are
+// allocated by either the VM, or by the linker in a static compiler.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_GLOBAL_VARIABLE_H
+#define LLVM_GLOBAL_VARIABLE_H
+
+#include "llvm/Value.h"
+class Module;
+
+class GlobalVariable : public Value {
+  Module *Parent;                  // The module that contains this method
+
+  friend class ValueHolder<GlobalVariable, Module, Module>;
+  void setParent(Module *parent) { Parent = parent; }
+
+public:
+  GlobalVariable(const Type *Ty, const string &Name = "");
+  ~GlobalVariable() {}
+
+  // Specialize setName to handle symbol table majik...
+  virtual void setName(const string &name, SymbolTable *ST = 0);
+
+  inline       Module *getParent()       { return Parent; }
+  inline const Module *getParent() const { return Parent; }
+};
+
+#endif
index 866f5e0e9e08778e50b45d220dab7f0731ee246e..bf710d1223da671c1eac97c4fcc4eb05f807b280 100644 (file)
 #include "llvm/SymTabValue.h"
 #include "llvm/ValueHolder.h"
 class Method;
+class GlobalVariable;
 
 class Module : public Value, public SymTabValue {
 public:
+  typedef ValueHolder<GlobalVariable, Module, Module> GlobalListType;
   typedef ValueHolder<Method, Module, Module> MethodListType;
 
+  // Global Variable iterators...
+  typedef GlobalListType::iterator                        giterator;
+  typedef GlobalListType::const_iterator            const_giterator;
+  typedef reverse_iterator<giterator>             reverse_giterator;
+  typedef reverse_iterator<const_giterator> const_reverse_giterator;
+
   // Method iterators...
-  typedef MethodListType::iterator iterator;
-  typedef MethodListType::const_iterator const_iterator;
+  typedef MethodListType::iterator                       iterator;
+  typedef MethodListType::const_iterator           const_iterator;
   typedef reverse_iterator<const_iterator> const_reverse_iterator;
   typedef reverse_iterator<iterator>             reverse_iterator;
+
 private:
+  GlobalListType GlobalList;     // The Global Variables
   MethodListType MethodList;     // The Methods
 
 public:
@@ -32,17 +42,40 @@ public:
   // 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 reduceApply(bool (*Func)(GlobalVariable*));
+  bool reduceApply(bool (*Func)(const GlobalVariable*)) const;
   bool reduceApply(bool (*Func)(Method*));
   bool reduceApply(bool (*Func)(const Method*)) const;
 
 
   // Get the underlying elements of the Module...
+  inline const GlobalListType &getGlobalList() const  { return GlobalList; }
+  inline       GlobalListType &getGlobalList()        { return GlobalList; }
   inline const MethodListType &getMethodList() const  { return MethodList; }
   inline       MethodListType &getMethodList()        { return MethodList; }
 
   //===--------------------------------------------------------------------===//
   // Module iterator forwarding functions
   //
+  inline giterator                gbegin()       { return GlobalList.begin(); }
+  inline const_giterator          gbegin() const { return GlobalList.begin(); }
+  inline giterator                gend  ()       { return GlobalList.end();   }
+  inline const_giterator          gend  () const { return GlobalList.end();   }
+
+  inline reverse_giterator       grbegin()       { return GlobalList.rbegin(); }
+  inline const_reverse_giterator grbegin() const { return GlobalList.rbegin(); }
+  inline reverse_giterator       grend  ()       { return GlobalList.rend();   }
+  inline const_reverse_giterator grend  () const { return GlobalList.rend();   }
+
+  inline unsigned                  gsize() const { return GlobalList.size(); }
+  inline bool                     gempty() const { return GlobalList.empty(); }
+  inline const GlobalVariable    *gfront() const { return GlobalList.front(); }
+  inline       GlobalVariable    *gfront()       { return GlobalList.front(); }
+  inline const GlobalVariable     *gback() const { return GlobalList.back(); }
+  inline       GlobalVariable     *gback()       { return GlobalList.back(); }
+
+
+
   inline iterator                begin()       { return MethodList.begin(); }
   inline const_iterator          begin() const { return MethodList.begin(); }
   inline iterator                end  ()       { return MethodList.end();   }
index 889551d0cda94ba4f7251039649137ad806220cd..dd97d3577d762e9baddea8785faed68928921c86 100644 (file)
@@ -19,6 +19,7 @@ class MethodArgument;
 class Instruction;
 class BasicBlock;
 class Method;
+class GlobalVariable;
 class Module;
 class SymbolTable;
 template<class ValueSubclass, class ItemParentType, class SymTabType> 
@@ -36,9 +37,9 @@ public:
     ConstantVal,            // This is an instance of ConstPoolVal
     MethodArgumentVal,      // This is an instance of MethodArgument
     InstructionVal,         // This is an instance of Instruction
-
     BasicBlockVal,          // This is an instance of BasicBlock
     MethodVal,              // This is an instance of Method
+    GlobalVal,              // This is an instance of GlobalVariable
     ModuleVal,              // This is an instance of Module
   };
 
@@ -102,6 +103,7 @@ public:
   CAST_FN(Instruction   ,       Instruction   )
   CAST_FN(BasicBlock    ,       BasicBlock    )
   CAST_FN(Method        ,       Method        )
+  CAST_FN(Global        ,       GlobalVariable)
   CAST_FN(Module        ,       Module        )
 #undef CAST_FN
 
index 9c3f947c8965ff5286e4250cf57754108894a34d..6a650a367b5647f0a48215b2fbc9bb0a1eeacd0e 100644 (file)
@@ -122,6 +122,7 @@ end             { return END; }
 true            { return TRUE;  }
 false           { return FALSE; }
 declare         { return DECLARE; }
+global          { return GLOBAL; }
 implementation  { return IMPLEMENTATION; }
 \.\.\.          { return DOTDOTDOT; }
 string          { return STRING; }
index 9971050f3a2323a2a2314d18c9802896d1ae4d8b..0568b336ae552662a26857bcfc41fa1bd3306aa3 100644 (file)
 
 %{
 #include "ParserInternals.h"
-#include "llvm/BasicBlock.h"
-#include "llvm/Method.h"
+#include "llvm/Assembly/Parser.h"
 #include "llvm/SymbolTable.h"
 #include "llvm/Module.h"
-#include "llvm/Type.h"
+#include "llvm/GlobalVariable.h"
+#include "llvm/Method.h"
+#include "llvm/BasicBlock.h"
 #include "llvm/DerivedTypes.h"
-#include "llvm/Assembly/Parser.h"
 #include "llvm/iTerminators.h"
 #include "llvm/iMemory.h"
 #include "llvm/CFG.h"         // TODO: Change this when we have a DF.h
@@ -568,7 +568,7 @@ Module *RunVMAsmParser(const string &Filename, FILE *F) {
 %type  <StrVal>  OptVAR_ID OptAssign
 
 
-%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE TO DOTDOTDOT STRING
+%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL TO DOTDOTDOT STRING
 
 // Basic Block Terminating Operators 
 %token <TermOpVal> RET BR SWITCH
@@ -871,15 +871,23 @@ ConstPool : ConstPool OptAssign ConstVal {
   }
   | ConstPool MethodProto {            // Method prototypes can be in const pool
   }
-/*
-  | ConstPool OptAssign GlobalDecl {     // Global declarations appear in CP
-    if ($2) {
-      setValueName($3, $2);
-      free($2);
+  | ConstPool GLOBAL OptAssign Types { // Global declarations appear in CP
+    if (!$4->get()->isPointerType() || 
+       (((PointerType*)$4->get())->isArrayType() && 
+        ((PointerType*)$4->get())->isArrayType()->isUnsized())) {
+      ThrowException("Type '" + $4->get()->getDescription() +
+                    "' is not a pointer to a sized type!");
     }
-    //CurModule.CurrentModule->
+
+    GlobalVariable *GV = new GlobalVariable(*$4);
+    delete $4;
+    if ($3) {
+      setValueName(GV, $3);
+      free($3);
+    }
+    CurModule.CurrentModule->getGlobalList().push_back(GV);
+    InsertValue(GV, CurModule.Values);
   }
-*/
   | /* empty: end of list */ { 
   }
 
index 540768383acaba9e4f96dabdfd02df4fb87e4d64..eab576e541b9e541058d1cc1c7cdd5a4bfb7b9e6 100644 (file)
@@ -12,6 +12,7 @@
 
 #include "llvm/Bytecode/Reader.h"
 #include "llvm/Bytecode/Format.h"
+#include "llvm/GlobalVariable.h"
 #include "llvm/Module.h"
 #include "llvm/BasicBlock.h"
 #include "llvm/DerivedTypes.h"
@@ -312,10 +313,28 @@ bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
 
 bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
                                          Module *C) {
-
   if (!MethodSignatureList.empty()) 
     return failure(true);  // Two ModuleGlobal blocks?
 
+  // Read global variables...
+  unsigned VarType;
+  if (read_vbr(Buf, End, VarType)) return failure(true);
+  while (VarType != Type::VoidTyID) { // List is terminated by Void
+    const Type *Ty = getType(VarType);
+    if (!Ty || !Ty->isPointerType()) { 
+      cerr << "Global not pointer type!  Ty = " << Ty << endl;
+      return failure(true); 
+    }
+
+    // Create the global variable...
+    GlobalVariable *GV = new GlobalVariable(Ty);
+    insertValue(GV, ModuleValues);
+    C->getGlobalList().push_back(GV);
+
+    if (read_vbr(Buf, End, VarType)) return failure(true);
+    BCR_TRACE(2, "Global Variable of type: " << Ty->getDescription() << endl);
+  }
+
   // Read the method signatures for all of the methods that are coming, and 
   // create fillers in the Value tables.
   unsigned MethSignature;
@@ -324,7 +343,6 @@ bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
     const Type *Ty = getType(MethSignature);
     if (!Ty || !Ty->isMethodType()) { 
       cerr << "Method not meth type!  Ty = " << Ty << endl;
-      if (Ty) cerr << Ty->getName(); else cerr << MethSignature; cerr << endl; 
       return failure(true); 
     }
 
index 9af5a387e1cfdea08b8762f360b4426503dc2b60..cc7d4e56df3e39b1e34896c630e2c23d10a5a712 100644 (file)
@@ -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());
index 423514582293fe29fa65171422e4a522d0db8c68..e6562f5fb94ffd831417ea4ad8efea7e73b72cfb 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "WriterInternals.h"
 #include "llvm/Module.h"
+#include "llvm/GlobalVariable.h"
 #include "llvm/Method.h"
 #include "llvm/BasicBlock.h"
 #include "llvm/ConstPoolVals.h"
@@ -117,7 +118,15 @@ void BytecodeWriter::outputConstants(bool isMethod) {
 void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
   BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out);
   
-  // Output the types of the methods in this class
+  // Output the types for the global variables in the module...
+  for (Module::const_giterator I = M->gbegin(), End = M->gend(); I != End;++I) {
+    int Slot = Table.getValSlot((*I)->getType());
+    assert(Slot != -1 && "Module global vars is broken!");
+    output_vbr((unsigned)Slot, Out);
+  }
+  output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
+
+  // Output the types of the methods in this module...
   for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) {
     int Slot = Table.getValSlot((*I)->getType());
     assert(Slot != -1 && "Module const pool is broken!");
@@ -125,6 +134,8 @@ void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
     output_vbr((unsigned)Slot, Out);
   }
   output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
+
+
   align32(Out);
 }
 
index a8951e61d1e7bf0e848fefe98ff4bdecb4d9dcc4..9056865849ac79168416e4654bed9f9b7c7c32ba 100644 (file)
@@ -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...
index bd342a2473c46eb0e59869b9ceffb728957ead7b..0ec64113230441f0e8ea9fe219db52ef7eeaae79 100644 (file)
@@ -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);
+}
index 4ab8bb06fc6281c31999e34818c71a4b6dc7f915..5fc11d1b10fcfb801487f21ea351d33f2bf9f381 100644 (file)
@@ -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"
 // 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);
 }
index 9af5a387e1cfdea08b8762f360b4426503dc2b60..cc7d4e56df3e39b1e34896c630e2c23d10a5a712 100644 (file)
@@ -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());
diff --git a/test/Feature/globalvars.ll b/test/Feature/globalvars.ll
new file mode 100644 (file)
index 0000000..ef3f336
--- /dev/null
@@ -0,0 +1,14 @@
+
+global %MyVar     = int *
+global %MyIntList = { \2 *, int } *
+global int *     ; int*:0
+
+implementation
+
+int "foo"(int %blah)
+begin
+       store int 5, int *%MyVar
+       store int 12, { \2 *, int } * %MyIntList, ubyte 1
+       ret int %blah
+end
+