Upgrade V8 to 5.1.281.57  DO NOT MERGE

FPIIM-449

Change-Id: Id981b686b4d587ac31697662eb98bb34be42ad90
(cherry picked from commit 3b9bc31999c9787eb726ecdbfd5796bfdec32a18)
diff --git a/test/cctest/wasm/wasm-run-utils.h b/test/cctest/wasm/wasm-run-utils.h
index 7ee3981..1e85f46 100644
--- a/test/cctest/wasm/wasm-run-utils.h
+++ b/test/cctest/wasm/wasm-run-utils.h
@@ -51,22 +51,18 @@
 #define WASM_RUNNER_MAX_NUM_PARAMETERS 4
 #define WASM_WRAPPER_RETURN_VALUE 8754
 
+#define BUILD(r, ...)                      \
+  do {                                     \
+    byte code[] = {__VA_ARGS__};           \
+    r.Build(code, code + arraysize(code)); \
+  } while (false)
+
 namespace {
 using namespace v8::base;
 using namespace v8::internal;
 using namespace v8::internal::compiler;
 using namespace v8::internal::wasm;
 
-inline void init_env(FunctionEnv* env, FunctionSig* sig) {
-  env->module = nullptr;
-  env->sig = sig;
-  env->local_i32_count = 0;
-  env->local_i64_count = 0;
-  env->local_f32_count = 0;
-  env->local_f64_count = 0;
-  env->SumLocals();
-}
-
 const uint32_t kMaxGlobalsSize = 128;
 
 // A helper for module environments that adds the ability to allocate memory
@@ -83,9 +79,8 @@
     instance->globals_size = kMaxGlobalsSize;
     instance->mem_start = nullptr;
     instance->mem_size = 0;
-    instance->function_code = nullptr;
     linker = nullptr;
-    asm_js = false;
+    origin = kWasmOrigin;
     memset(global_data, 0, sizeof(global_data));
   }
 
@@ -93,9 +88,6 @@
     if (instance->mem_start) {
       free(instance->mem_start);
     }
-    if (instance->function_code) {
-      delete instance->function_code;
-    }
   }
 
   byte* AddMemory(size_t size) {
@@ -121,11 +113,8 @@
   }
 
   byte AddSignature(FunctionSig* sig) {
-    if (!module->signatures) {
-      module->signatures = new std::vector<FunctionSig*>();
-    }
-    module->signatures->push_back(sig);
-    size_t size = module->signatures->size();
+    module->signatures.push_back(sig);
+    size_t size = module->signatures.size();
     CHECK(size < 127);
     return static_cast<byte>(size - 1);
   }
@@ -171,23 +160,21 @@
   }
 
   int AddFunction(FunctionSig* sig, Handle<Code> code) {
-    if (module->functions == nullptr) {
-      module->functions = new std::vector<WasmFunction>();
+    if (module->functions.size() == 0) {
       // TODO(titzer): Reserving space here to avoid the underlying WasmFunction
       // structs from moving.
-      module->functions->reserve(kMaxFunctions);
-      instance->function_code = new std::vector<Handle<Code>>();
+      module->functions.reserve(kMaxFunctions);
     }
-    uint32_t index = static_cast<uint32_t>(module->functions->size());
-    module->functions->push_back(
-        {sig, index, 0, 0, 0, 0, 0, 0, 0, false, false});
-    instance->function_code->push_back(code);
+    uint32_t index = static_cast<uint32_t>(module->functions.size());
+    module->functions.push_back(
+        {sig, index, 0, 0, 0, 0, 0, 0, 0, 0, 0, false, false});
+    instance->function_code.push_back(code);
     DCHECK_LT(index, kMaxFunctions);  // limited for testing.
     return index;
   }
 
   void SetFunctionCode(uint32_t index, Handle<Code> code) {
-    instance->function_code->at(index) = code;
+    instance->function_code[index] = code;
   }
 
   void AddIndirectFunctionTable(int* functions, int table_size) {
@@ -195,21 +182,21 @@
     Handle<FixedArray> fixed =
         isolate->factory()->NewFixedArray(2 * table_size);
     instance->function_table = fixed;
-    module->function_table = new std::vector<uint16_t>();
+    DCHECK_EQ(0u, module->function_table.size());
     for (int i = 0; i < table_size; i++) {
-      module->function_table->push_back(functions[i]);
+      module->function_table.push_back(functions[i]);
     }
   }
 
   void PopulateIndirectFunctionTable() {
     if (instance->function_table.is_null()) return;
-    int table_size = static_cast<int>(module->function_table->size());
+    int table_size = static_cast<int>(module->function_table.size());
     for (int i = 0; i < table_size; i++) {
-      int function_index = module->function_table->at(i);
-      WasmFunction* function = &module->functions->at(function_index);
+      int function_index = module->function_table[i];
+      WasmFunction* function = &module->functions[function_index];
       instance->function_table->set(i, Smi::FromInt(function->sig_index));
-      instance->function_table->set(
-          i + table_size, *instance->function_code->at(function_index));
+      instance->function_table->set(i + table_size,
+                                    *instance->function_code[function_index]);
     }
   }
 
@@ -220,24 +207,22 @@
   V8_ALIGNED(8) byte global_data[kMaxGlobalsSize];  // preallocated global data.
 
   WasmGlobal* AddGlobal(MachineType mem_type) {
-    if (!module->globals) {
-      module->globals = new std::vector<WasmGlobal>();
-    }
     byte size = WasmOpcodes::MemSize(mem_type);
     global_offset = (global_offset + size - 1) & ~(size - 1);  // align
-    module->globals->push_back({0, mem_type, global_offset, false});
+    module->globals.push_back({0, 0, mem_type, global_offset, false});
     global_offset += size;
     // limit number of globals.
     CHECK_LT(global_offset, kMaxGlobalsSize);
-    return &module->globals->back();
+    return &module->globals.back();
   }
 };
 
-
-inline void TestBuildingGraph(Zone* zone, JSGraph* jsgraph, FunctionEnv* env,
-                              const byte* start, const byte* end) {
-  compiler::WasmGraphBuilder builder(zone, jsgraph, env->sig);
-  TreeResult result = BuildTFGraph(&builder, env, start, end);
+inline void TestBuildingGraph(Zone* zone, JSGraph* jsgraph, ModuleEnv* module,
+                              FunctionSig* sig, const byte* start,
+                              const byte* end) {
+  compiler::WasmGraphBuilder builder(zone, jsgraph, sig);
+  TreeResult result =
+      BuildTFGraph(zone->allocator(), &builder, module, sig, start, end);
   if (result.failed()) {
     ptrdiff_t pc = result.error_pc - result.start;
     ptrdiff_t pt = result.error_pt - result.start;
@@ -405,10 +390,9 @@
       : GraphAndBuilders(main_zone()),
         jsgraph(this->isolate(), this->graph(), this->common(), nullptr,
                 nullptr, this->machine()),
+        sig(sig),
         descriptor_(nullptr),
         testing_module_(module) {
-    init_env(&env, sig);
-    env.module = module;
     if (module) {
       // Get a new function from the testing module.
       function_ = nullptr;
@@ -426,12 +410,13 @@
   }
 
   JSGraph jsgraph;
-  FunctionEnv env;
+  FunctionSig* sig;
   // The call descriptor is initialized when the function is compiled.
   CallDescriptor* descriptor_;
   TestingModule* testing_module_;
   WasmFunction* function_;
   int function_index_;
+  LocalDeclEncoder local_decls;
 
   Isolate* isolate() { return main_isolate(); }
   Graph* graph() const { return main_graph_; }
@@ -440,31 +425,25 @@
   MachineOperatorBuilder* machine() { return &main_machine_; }
   void InitializeDescriptor() {
     if (descriptor_ == nullptr) {
-      descriptor_ = env.module->GetWasmCallDescriptor(main_zone(), env.sig);
+      descriptor_ = testing_module_->GetWasmCallDescriptor(main_zone(), sig);
     }
   }
   CallDescriptor* descriptor() { return descriptor_; }
 
   void Build(const byte* start, const byte* end) {
-    // Transfer local counts before compiling.
-    function()->local_i32_count = env.local_i32_count;
-    function()->local_i64_count = env.local_i64_count;
-    function()->local_f32_count = env.local_f32_count;
-    function()->local_f64_count = env.local_f64_count;
-
     // Build the TurboFan graph.
-    TestBuildingGraph(main_zone(), &jsgraph, &env, start, end);
+    local_decls.Prepend(&start, &end);
+    TestBuildingGraph(main_zone(), &jsgraph, testing_module_, sig, start, end);
+    delete[] start;
   }
 
   byte AllocateLocal(LocalType type) {
-    int result = static_cast<int>(env.total_locals);
-    env.AddLocals(type, 1);
-    byte b = static_cast<byte>(result);
-    CHECK_EQ(result, b);
-    return b;
+    uint32_t index = local_decls.AddLocals(1, type, sig);
+    byte result = static_cast<byte>(index);
+    DCHECK_EQ(index, result);
+    return result;
   }
 
-  // TODO(titzer): remove me.
   Handle<Code> Compile() {
     InitializeDescriptor();
     CallDescriptor* desc = descriptor_;
@@ -484,7 +463,6 @@
     return result;
   }
 
-  // TODO(titzer): remove me.
   uint32_t CompileAndAdd(uint16_t sig_index = 0) {
     CHECK(testing_module_);
     function()->sig_index = sig_index;
@@ -495,7 +473,7 @@
 
   WasmFunction* function() {
     if (function_) return function_;
-    return &testing_module_->module->functions->at(function_index_);
+    return &testing_module_->module->functions[function_index_];
   }
 };
 
@@ -508,8 +486,8 @@
              MachineType p1 = MachineType::None(),
              MachineType p2 = MachineType::None(),
              MachineType p3 = MachineType::None())
-      : compiled_(false),
-
+      : zone(&allocator_),
+        compiled_(false),
         signature_(MachineTypeForC<ReturnType>() == MachineType::None() ? 0 : 1,
                    GetParameterCount(p0, p1, p2, p3), storage_),
         compiler_(&signature_, nullptr) {
@@ -520,7 +498,8 @@
              MachineType p1 = MachineType::None(),
              MachineType p2 = MachineType::None(),
              MachineType p3 = MachineType::None())
-      : compiled_(false),
+      : zone(&allocator_),
+        compiled_(false),
         signature_(MachineTypeForC<ReturnType>() == MachineType::None() ? 0 : 1,
                    GetParameterCount(p0, p1, p2, p3), storage_),
         compiler_(&signature_, module) {
@@ -548,8 +527,6 @@
     wrapper_.Init(compiler_.descriptor(), p0, p1, p2, p3);
   }
 
-  FunctionEnv* env() { return &compiler_.env; }
-
   // Builds a graph from the given Wasm code and generates the machine
   // code and call wrapper for that graph. This method must not be called
   // more than once.
@@ -599,15 +576,10 @@
     return return_value;
   }
 
-  byte AllocateLocal(LocalType type) {
-    int result = static_cast<int>(env()->total_locals);
-    env()->AddLocals(type, 1);
-    byte b = static_cast<byte>(result);
-    CHECK_EQ(result, b);
-    return b;
-  }
+  byte AllocateLocal(LocalType type) { return compiler_.AllocateLocal(type); }
 
  protected:
+  v8::base::AccountingAllocator allocator_;
   Zone zone;
   bool compiled_;
   LocalType storage_[WASM_RUNNER_MAX_NUM_PARAMETERS];