s/Method/Function
[oota-llvm.git] / lib / ExecutionEngine / Interpreter / Execution.cpp
index d92764e49a6ce4fa4621dbcad900f94782ce693f..2260625b66b8025a1b318f5d10f5c71d88711e83 100644 (file)
@@ -26,7 +26,8 @@ using std::cerr;
 
 cl::Flag   QuietMode ("quiet"  , "Do not emit any non-program output");
 cl::Alias  QuietModeA("q"      , "Alias for -quiet", cl::NoFlags, QuietMode);
-
+cl::Flag   ArrayChecksEnabled("array-checks", "Enable array bound checks");
+cl::Flag   AbortOnExceptions("abort-on-exception", "Halt execution on a machine exception");
 
 // Create a TargetData structure to handle memory addressing and size/alignment
 // computations
@@ -740,15 +741,16 @@ void Interpreter::executeAllocInst(AllocationInst *I, ExecutionContext &SF) {
   }
 
   // Allocate enough memory to hold the type...
-  GenericValue Result;
   // FIXME: Don't use CALLOC, use a tainted malloc.
-  Result.PointerVal = (PointerTy)calloc(NumElements, TD.getTypeSize(Ty));
+  void *Memory = calloc(NumElements, TD.getTypeSize(Ty));
+
+  GenericValue Result;
+  Result.PointerVal = (PointerTy)Memory;
   assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
   SetValue(I, Result, SF);
 
-  if (I->getOpcode() == Instruction::Alloca) {
-    // TODO: FIXME: alloca should keep track of memory to free it later...
-  }
+  if (I->getOpcode() == Instruction::Alloca)
+    ECStack.back().Allocas.add(Memory);
 }
 
 static void executeFreeInst(FreeInst *I, ExecutionContext &SF) {
@@ -797,13 +799,13 @@ static PointerTy getElementOffset(MemAccessInst *I, ExecutionContext &SF) {
       assert(I->getOperand(ArgOff)->getType() == Type::UIntTy);
       unsigned Idx = getOperandValue(I->getOperand(ArgOff++), SF).UIntVal;
       if (const ArrayType *AT = dyn_cast<ArrayType>(ST))
-        if (Idx >= AT->getNumElements()) {
+        if (Idx >= AT->getNumElements() && ArrayChecksEnabled) {
           cerr << "Out of range memory access to element #" << Idx
                << " of a " << AT->getNumElements() << " element array."
                << " Subscript #" << (ArgOff-I->getFirstIndexOperandNumber())
                << "\n";
           // Get outta here!!!
-          siglongjmp(SignalRecoverBuffer, -1);
+          siglongjmp(SignalRecoverBuffer, SIGTRAP);
         }
 
       Ty = ST->getElementType();
@@ -1026,17 +1028,17 @@ MethodInfo::MethodInfo(Method *M) : Annotation(MethodInfoAID) {
   // Assign slot numbers to the method arguments...
   const Method::ArgumentListType &ArgList = M->getArgumentList();
   for (Method::ArgumentListType::const_iterator AI = ArgList.begin(), 
-        AE = ArgList.end(); AI != AE; ++AI) {
-    MethodArgument *MA = *AI;
-    MA->addAnnotation(new SlotNumber(getValueSlot(MA)));
-  }
+        AE = ArgList.end(); AI != AE; ++AI)
+    (*AI)->addAnnotation(new SlotNumber(getValueSlot(*AI)));
 
   // Iterate over all of the instructions...
   unsigned InstNum = 0;
-  for (Method::inst_iterator MI = M->inst_begin(), ME = M->inst_end();
-       MI != ME; ++MI) {
-    Instruction *I = *MI;                          // For each instruction...
-    I->addAnnotation(new InstNumber(++InstNum, getValueSlot(I))); // Add Annote
+  for (Method::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI) {
+    BasicBlock *BB = *MI;
+    for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II){
+      Instruction *I = *II;          // For each instruction... Add Annote
+      I->addAnnotation(new InstNumber(++InstNum, getValueSlot(I)));
+    }
   }
 }
 
@@ -1134,9 +1136,13 @@ bool Interpreter::executeInstruction() {
   //
   if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
     --SF.CurInst;   // Back up to erroring instruction
-    if (SigNo != SIGINT && SigNo != -1) {
+    if (SigNo != SIGINT) {
       cout << "EXCEPTION OCCURRED [" << _sys_siglistp[SigNo] << "]:\n";
       printStackTrace();
+      // If -abort-on-exception was specified, terminate LLI instead of trying
+      // to debug it.
+      //
+      if (AbortOnExceptions) exit(1);
     } else if (SigNo == SIGINT) {
       cout << "CTRL-C Detected, execution halted.\n";
     }