Subzero: Update tests and build scripts for sandboxing.

Spec2k now runs sandboxed, using all filetypes (asm, iasm, obj).

The new build-runtime.py script builds the native and sandboxed runtimes in build/runtime/ .  The various Subzero driver scripts are updated to use that.

Fixes a stack frame bug in sandboxed mode when the integrated assembler is used.  The stack adjustment for setting up a function call wasn't being rolled back for the second emitIAS() pass, so stack variables passed as arguments to the callee were being copied from the wrong stack slot.

Notes:

1. The hybrid Subzero/llc bisection debugging builds probably do not work as intended for -filetype=obj since the ELF emitter doesn't yet support -ffunction-sections.  (This was also true for non-sandboxed hybrid builds.)

2. The cross tests have not yet been adapted for testing sandboxing.  I'd prefer to first make progress on https://code.google.com/p/nativeclient/issues/detail?id=4085 in order to avoid blindly doubling the number of tests.

BUG= https://code.google.com/p/nativeclient/issues/detail?id=4079
R=jvoung@chromium.org

Review URL: https://codereview.chromium.org/944333002
diff --git a/src/IceCfgNode.cpp b/src/IceCfgNode.cpp
index c7a3d81..02b9aac 100644
--- a/src/IceCfgNode.cpp
+++ b/src/IceCfgNode.cpp
@@ -901,8 +901,9 @@
   BundleEmitHelper &operator=(const BundleEmitHelper &) = delete;
 
 public:
-  BundleEmitHelper(Assembler *Asm, const InstList &Insts)
-      : Asm(Asm), End(Insts.end()), BundleLockStart(End),
+  BundleEmitHelper(Assembler *Asm, TargetLowering *Target,
+                   const InstList &Insts)
+      : Asm(Asm), Target(Target), End(Insts.end()), BundleLockStart(End),
         BundleSize(1 << Asm->getBundleAlignLog2Bytes()),
         BundleMaskLo(BundleSize - 1), BundleMaskHi(~BundleMaskLo),
         SizeSnapshotPre(0), SizeSnapshotPost(0) {}
@@ -948,6 +949,7 @@
     BundleLockStart = I;
     SizeSnapshotPre = Asm->getBufferSize();
     Asm->setPreliminary(true);
+    Target->snapshotEmitState();
     assert(isInBundleLockRegion());
   }
   // Update bookkeeping when the bundle_unlock instruction is
@@ -989,10 +991,12 @@
     assert(isInBundleLockRegion());
     Asm->setBufferSize(SizeSnapshotPre);
     Asm->setPreliminary(false);
+    Target->rollbackEmitState();
   }
 
 private:
   Assembler *const Asm;
+  TargetLowering *const Target;
   // End is a sentinel value such that BundleLockStart==End implies
   // that we are not in a bundle_lock region.
   const InstList::const_iterator End;
@@ -1047,7 +1051,7 @@
   // of label bindings, label links, and relocation fixups.  Instead,
   // the first pass just disables all mutation of that state.
 
-  BundleEmitHelper Helper(Asm, Insts);
+  BundleEmitHelper Helper(Asm, Func->getTarget(), Insts);
   InstList::const_iterator End = Insts.end();
   // Retrying indicates that we had to roll back to the bundle_lock
   // instruction to apply padding before the bundle_lock sequence.
diff --git a/src/IceTargetLowering.cpp b/src/IceTargetLowering.cpp
index 461b086..d2da333 100644
--- a/src/IceTargetLowering.cpp
+++ b/src/IceTargetLowering.cpp
@@ -105,7 +105,7 @@
     : Func(Func), Ctx(Func->getContext()),
       RandomizeRegisterAllocation(CLRandomizeRegisterAllocation),
       HasComputedFrame(false), CallsReturnsTwice(false), StackAdjustment(0),
-      Context() {}
+      Context(), SnapshotStackAdjustment(0) {}
 
 std::unique_ptr<Assembler> TargetLowering::createAssembler(TargetArch Target,
                                                            Cfg *Func) {
diff --git a/src/IceTargetLowering.h b/src/IceTargetLowering.h
index 92eed51..2ad2685 100644
--- a/src/IceTargetLowering.h
+++ b/src/IceTargetLowering.h
@@ -192,6 +192,17 @@
       llvm::SmallVectorImpl<int32_t> &Permutation,
       const llvm::SmallBitVector &ExcludeRegisters) const = 0;
 
+  // Save/restore any mutable state for the situation where code
+  // emission needs multiple passes, such as sandboxing or relaxation.
+  // Subclasses may provide their own implementation, but should be
+  // sure to also call the parent class's methods.
+  virtual void snapshotEmitState() {
+    SnapshotStackAdjustment = StackAdjustment;
+  }
+  virtual void rollbackEmitState() {
+    StackAdjustment = SnapshotStackAdjustment;
+  }
+
   virtual void emitVariable(const Variable *Var) const = 0;
 
   // Performs target-specific argument lowering.
@@ -239,6 +250,9 @@
   // natural location, as arguments are pushed for a function call.
   int32_t StackAdjustment;
   LoweringContext Context;
+
+private:
+  int32_t SnapshotStackAdjustment;
 };
 
 // TargetDataLowering is used for "lowering" data including initializers
diff --git a/src/IceTargetLoweringX8632.cpp b/src/IceTargetLoweringX8632.cpp
index 13476c8..32102df 100644
--- a/src/IceTargetLoweringX8632.cpp
+++ b/src/IceTargetLoweringX8632.cpp
@@ -4118,9 +4118,7 @@
   }
 }
 
-void TargetX8632::lowerUnreachable(const InstUnreachable * /*Inst*/) {
-  _ud2();
-}
+void TargetX8632::lowerUnreachable(const InstUnreachable * /*Inst*/) { _ud2(); }
 
 // Turn an i64 Phi instruction into a pair of i32 Phi instructions, to
 // preserve integrity of liveness analysis.  Undef values are also