[llvm-commits] CVS: llvm-java/lib/Compiler/Locals.h Locals.cpp Compiler.cpp

Alkis Evlogimenos alkis at cs.uiuc.edu
Mon Jan 17 15:00:14 PST 2005



Changes in directory llvm-java/lib/Compiler:

Locals.h updated: 1.1 -> 1.2
Locals.cpp updated: 1.5 -> 1.6
Compiler.cpp updated: 1.184 -> 1.185
---
Log message:

Make each java local variable slot be a map of type->alloca's. This
allows us to compile java bytecode correctly when a local used in a
block when it was previously defined in two different predecessors. We
now compile Lists1 and Lists2 correctly.


---
Diffs of the changes:  (+69 -36)

Index: llvm-java/lib/Compiler/Locals.h
diff -u llvm-java/lib/Compiler/Locals.h:1.1 llvm-java/lib/Compiler/Locals.h:1.2
--- llvm-java/lib/Compiler/Locals.h:1.1	Fri Oct 15 21:38:07 2004
+++ llvm-java/lib/Compiler/Locals.h	Mon Jan 17 17:00:04 2005
@@ -16,12 +16,14 @@
 #define LLVM_JAVA_LOCALS_H
 
 #include <llvm/Java/Bytecode.h>
+#include <map>
 #include <vector>
 
 namespace llvm {
 
   class AllocaInst;
   class BasicBlock;
+  class Type;
   class Value;
 
 } // namespace llvm
@@ -29,7 +31,8 @@
 namespace llvm { namespace Java {
 
   class Locals {
-    std::vector<AllocaInst*> TheLocals;
+    typedef std::map<const Type*, AllocaInst*> SlotMap;
+    std::vector<SlotMap> TheLocals;
 
   public:
     explicit Locals(unsigned maxLocals);
@@ -42,7 +45,7 @@
     /// @brief - Loads the value of the \c i'th local variable and
     /// appends any instructions to implement this to \c insertAtEnd
     /// BasicBlock
-    Value* load(unsigned i, BasicBlock* insertAtEnd);
+    Value* load(unsigned i, const Type* type, BasicBlock* insertAtEnd);
   };
 
 } } // namespace llvm::Java


Index: llvm-java/lib/Compiler/Locals.cpp
diff -u llvm-java/lib/Compiler/Locals.cpp:1.5 llvm-java/lib/Compiler/Locals.cpp:1.6
--- llvm-java/lib/Compiler/Locals.cpp:1.5	Mon Jan 17 12:21:03 2005
+++ llvm-java/lib/Compiler/Locals.cpp	Mon Jan 17 17:00:04 2005
@@ -37,29 +37,46 @@
     value = new CastInst(value, ObjectBaseRefTy,
                          "to-object-base", insertAtEnd);
 
-  if (!TheLocals[i] ||
-      TheLocals[i]->getType()->getElementType() != value->getType()) {
+  // Values of jboolean, jbyte, jchar and jshort are extended to a
+  // jint when stored in a local slot.
+  else if (valueTy == Type::BoolTy ||
+	   valueTy == Type::SByteTy ||
+	   valueTy == Type::UShortTy ||
+	   valueTy == Type::ShortTy)
+    value = new CastInst(value, Type::IntTy, "int-extend", insertAtEnd);
+
+  valueTy = value->getType();
+
+  SlotMap& slotMap = TheLocals[i];
+  SlotMap::iterator it = slotMap.find(valueTy);
+
+  if (it == slotMap.end()) {
     // Insert the alloca at the beginning of the entry block.
     BasicBlock* entry = &insertAtEnd->getParent()->getEntryBlock();
+    AllocaInst* alloca;
     if (entry->empty())
-      TheLocals[i] = new AllocaInst(
+      alloca = new AllocaInst(
         value->getType(),
         NULL,
         "local" + utostr(i),
         entry);
     else
-      TheLocals[i] = new AllocaInst(
+      alloca = new AllocaInst(
         value->getType(),
         NULL,
         "local" + utostr(i),
         &entry->front());
+    it = slotMap.insert(it, std::make_pair(valueTy, alloca));
   }
 
-  new StoreInst(value, TheLocals[i], insertAtEnd);
+  new StoreInst(value, it->second, insertAtEnd);
 }
 
-llvm::Value* Locals::load(unsigned i, BasicBlock* insertAtEnd)
+llvm::Value* Locals::load(unsigned i, const Type* type, BasicBlock* insertAtEnd)
 {
-  assert(TheLocals[i] && "Attempt to load a non initialized global!");
-  return new LoadInst(TheLocals[i], "load", insertAtEnd);
+  SlotMap& slotMap = TheLocals[i];
+  SlotMap::iterator it = slotMap.find(type);
+
+  assert(it != slotMap.end() && "Attempt to load a non initialized global!");
+  return new LoadInst(it->second, "load", insertAtEnd);
 }


Index: llvm-java/lib/Compiler/Compiler.cpp
diff -u llvm-java/lib/Compiler/Compiler.cpp:1.184 llvm-java/lib/Compiler/Compiler.cpp:1.185
--- llvm-java/lib/Compiler/Compiler.cpp:1.184	Mon Jan 17 12:21:03 2005
+++ llvm-java/lib/Compiler/Compiler.cpp	Mon Jan 17 17:00:04 2005
@@ -77,8 +77,8 @@
     ClassFile* cf_;
     std::auto_ptr<BasicBlockBuilder> bbBuilder_;
     std::list<BasicBlock*> bbWorkList_;
-    typedef std::map<BasicBlock*, std::pair<Locals,OperandStack> > BBInfoMap;
-    BBInfoMap bbInfoMap_;
+    typedef std::map<BasicBlock*, OperandStack> OpStackMap;
+    OpStackMap opStackMap_;
     BasicBlock* currentBB_;
     Locals* currentLocals_;
     OperandStack* currentOpStack_;
@@ -1270,6 +1270,8 @@
           classMethodDesc.find("java/lang/Integer") != 0 &&
           classMethodDesc.find("java/lang/Long") != 0 &&
           classMethodDesc.find("java/lang/Short") != 0 &&
+          classMethodDesc.find("java/lang/StringBuffer") != 0 &&
+          classMethodDesc.find("java/util/IndexOutOfBoundsException") != 0 &&
           classMethodDesc.find("java/util/NoSuchElementException") != 0 &&
           classMethodDesc.find("java/util/AbstractCollection") != 0 &&
           classMethodDesc.find("java/util/AbstractList") != 0 &&
@@ -1284,7 +1286,7 @@
 
       Java::CodeAttribute* codeAttr = method->getCodeAttribute();
 
-      bbInfoMap_.clear();
+      opStackMap_.clear();
       bbBuilder_.reset(new BasicBlockBuilder(function, codeAttr));
 
       // Put arguments into locals.
@@ -1302,10 +1304,10 @@
       // NOTE: We create an operand stack one size too big because we
       // push extra values on the stack to simplify code generation
       // (see implementation of ifne).
-      bbInfoMap_.insert(
+      currentLocals_ = &locals;
+      opStackMap_.insert(
         std::make_pair(&function->getEntryBlock(),
-                       std::make_pair(locals,
-                                      OperandStack(codeAttr->getMaxStack()+1))));
+                       OperandStack(codeAttr->getMaxStack()+1)));
 
       // Insert the entry block to the work list.
       bbWorkList_.push_back(&function->getEntryBlock());
@@ -1315,13 +1317,11 @@
         currentBB_ = bbWorkList_.front();
         bbWorkList_.pop_front();
 
-        BBInfoMap::iterator bbInfo = bbInfoMap_.find(currentBB_);
-        assert(bbInfo != bbInfoMap_.end() &&
-               "Unknown entry operand stack and locals for basic block in "
-               "work list!");
+        OpStackMap::iterator opStack = opStackMap_.find(currentBB_);
+        assert(opStack != opStackMap_.end() &&
+               "Unknown operand stack for basic block in work list!");
 
-        currentLocals_ = &bbInfo->second.first;
-        currentOpStack_ = &bbInfo->second.second;
+        currentOpStack_ = &opStack->second;
 
         unsigned start, end;
         tie(start, end) = bbBuilder_->getBytecodeIndices(currentBB_);
@@ -1344,12 +1344,10 @@
                SI = succ_begin(currentBB_), SE = succ_end(currentBB_);
              SI != SE; ++SI) {
           BasicBlock* Succ = *SI;
-          BBInfoMap::iterator bbSuccInfo = bbInfoMap_.lower_bound(Succ);
-          if (bbSuccInfo == bbInfoMap_.end() || bbSuccInfo->first != Succ) {
-            bbInfoMap_.insert(bbSuccInfo,
-                              std::make_pair(Succ,
-                                             std::make_pair(*currentLocals_,
-                                                            *currentOpStack_)));
+          OpStackMap::iterator succOpStack = opStackMap_.lower_bound(Succ);
+          if (succOpStack == opStackMap_.end() || succOpStack->first != Succ) {
+            opStackMap_.insert(succOpStack,
+                              std::make_pair(Succ, *currentOpStack_));
             bbWorkList_.push_back(Succ);
           }
         }
@@ -1528,14 +1526,14 @@
       do_ldc(index);
     }
 
-    void do_iload(unsigned index) { do_load_common(index); }
-    void do_lload(unsigned index) { do_load_common(index); }
-    void do_fload(unsigned index) { do_load_common(index); }
-    void do_dload(unsigned index) { do_load_common(index); }
-    void do_aload(unsigned index) { do_load_common(index); }
+    void do_iload(unsigned index) { do_load_common(index, Type::IntTy); }
+    void do_lload(unsigned index) { do_load_common(index, Type::LongTy); }
+    void do_fload(unsigned index) { do_load_common(index, Type::FloatTy); }
+    void do_dload(unsigned index) { do_load_common(index, Type::DoubleTy); }
+    void do_aload(unsigned index) { do_load_common(index, ObjectBaseRefTy); }
 
-    void do_load_common(unsigned index) {
-      Value* val = currentLocals_->load(index, currentBB_);
+    void do_load_common(unsigned index, Type* type) {
+      Value* val = currentLocals_->load(index, type, currentBB_);
       push(val);
     }
 
@@ -1806,7 +1804,7 @@
     }
 
     void do_iinc(unsigned index, int amount) {
-      Value* v = currentLocals_->load(index, currentBB_);
+      Value* v = currentLocals_->load(index, Type::IntTy, currentBB_);
       Value* a = ConstantSInt::get(Type::IntTy, amount);
       v = BinaryOperator::createAdd(v, a, TMP, currentBB_);
       currentLocals_->store(index, v, currentBB_);
@@ -2019,6 +2017,21 @@
       const FunctionType* funTy =
         cast<FunctionType>(funPtrTy->getElementType());
 
+      // Trace function calls.
+//       llvm::Constant* functionNameArray = ConstantArray::get(fun->getName());
+//       GlobalVariable* functionName =
+//         new GlobalVariable(functionNameArray->getType(),
+//                            true,
+//                            GlobalVariable::ExternalLinkage,
+//                            functionNameArray,
+//                            fun->getName() + "name",
+//                            &module_);
+//       Function* puts =
+//         module_.getOrInsertFunction("puts",
+//                                     Type::VoidTy,
+//                                     functionName->getType(), NULL);
+//       new CallInst(puts, std::vector<Value*>(1, functionName), "", currentBB_);
+
       if (funTy->getReturnType() == Type::VoidTy)
         new CallInst(fun, params, "", currentBB_);
       else {






More information about the llvm-commits mailing list