| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stdlib.h> | 5 #include <stdlib.h> |
| 6 #include <string.h> | 6 #include <string.h> |
| 7 | 7 |
| 8 #include "src/wasm/encoder.h" | 8 #include "src/wasm/encoder.h" |
| 9 #include "src/wasm/wasm-macro-gen.h" | 9 #include "src/wasm/wasm-macro-gen.h" |
| 10 #include "src/wasm/wasm-module.h" | 10 #include "src/wasm/wasm-module.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 namespace { | 21 namespace { |
| 22 void TestModule(WasmModuleIndex* module, int32_t expected_result) { | 22 void TestModule(WasmModuleIndex* module, int32_t expected_result) { |
| 23 Isolate* isolate = CcTest::InitIsolateOnce(); | 23 Isolate* isolate = CcTest::InitIsolateOnce(); |
| 24 int32_t result = | 24 int32_t result = |
| 25 CompileAndRunWasmModule(isolate, module->Begin(), module->End()); | 25 CompileAndRunWasmModule(isolate, module->Begin(), module->End()); |
| 26 CHECK_EQ(expected_result, result); | 26 CHECK_EQ(expected_result, result); |
| 27 } | 27 } |
| 28 } // namespace | 28 } // namespace |
| 29 | 29 |
| 30 | 30 |
| 31 // TODO(tizer): Figure out why this crashes with PPC. |
| 32 #if !defined(V8_TARGET_ARCH_PPC) && !defined(V8_TARGET_ARCH_PPC64) |
| 33 |
| 31 // A raw test that skips the WasmModuleBuilder. | 34 // A raw test that skips the WasmModuleBuilder. |
| 32 TEST(Run_WasmModule_CallAdd_rev) { | 35 TEST(Run_WasmModule_CallAdd_rev) { |
| 33 static const byte data[] = { | 36 static const byte data[] = { |
| 34 // sig#0 ------------------------------------------ | 37 // sig#0 ------------------------------------------ |
| 35 kDeclSignatures, 2, 0, kLocalI32, // void -> int | 38 kDeclSignatures, 2, 0, kLocalI32, // void -> int |
| 36 2, kLocalI32, kLocalI32, kLocalI32, // int,int -> int | 39 2, kLocalI32, kLocalI32, kLocalI32, // int,int -> int |
| 37 // func#0 (main) ---------------------------------- | 40 // func#0 (main) ---------------------------------- |
| 38 kDeclFunctions, 2, kDeclFunctionExport, 0, 0, // sig index | 41 kDeclFunctions, 2, kDeclFunctionExport, 0, 0, // sig index |
| 39 6, 0, // body size | 42 6, 0, // body size |
| 40 kExprCallFunction, 1, // -- | 43 kExprCallFunction, 1, // -- |
| 41 kExprI8Const, 77, // -- | 44 kExprI8Const, 77, // -- |
| 42 kExprI8Const, 22, // -- | 45 kExprI8Const, 22, // -- |
| 43 // func#1 ----------------------------------------- | 46 // func#1 ----------------------------------------- |
| 44 0, // no name, not exported | 47 0, // no name, not exported |
| 45 1, 0, // sig index | 48 1, 0, // sig index |
| 46 5, 0, // body size | 49 5, 0, // body size |
| 47 kExprI32Add, // -- | 50 kExprI32Add, // -- |
| 48 kExprGetLocal, 0, // -- | 51 kExprGetLocal, 0, // -- |
| 49 kExprGetLocal, 1, // -- | 52 kExprGetLocal, 1, // -- |
| 50 }; | 53 }; |
| 51 | 54 |
| 52 Isolate* isolate = CcTest::InitIsolateOnce(); | 55 Isolate* isolate = CcTest::InitIsolateOnce(); |
| 53 int32_t result = | 56 int32_t result = |
| 54 CompileAndRunWasmModule(isolate, data, data + arraysize(data)); | 57 CompileAndRunWasmModule(isolate, data, data + arraysize(data)); |
| 55 CHECK_EQ(99, result); | 58 CHECK_EQ(99, result); |
| 56 } | 59 } |
| 57 | 60 |
| 61 #endif |
| 62 |
| 58 | 63 |
| 59 TEST(Run_WasmModule_Return114) { | 64 TEST(Run_WasmModule_Return114) { |
| 60 static const int32_t kReturnValue = 114; | 65 static const int32_t kReturnValue = 114; |
| 61 Zone zone; | 66 Zone zone; |
| 62 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 67 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 63 uint16_t f_index = builder->AddFunction(); | 68 uint16_t f_index = builder->AddFunction(); |
| 64 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 69 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
| 65 f->ReturnType(kAstI32); | 70 f->ReturnType(kAstI32); |
| 66 f->Exported(1); | 71 f->Exported(1); |
| 67 byte code[] = {WASM_I8(kReturnValue)}; | 72 byte code[] = {WASM_I8(kReturnValue)}; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 WASM_BRV(2, WASM_I8(-1)), WASM_INC_LOCAL_BY(localIndex, 4))), | 144 WASM_BRV(2, WASM_I8(-1)), WASM_INC_LOCAL_BY(localIndex, 4))), |
| 140 WASM_I8(11))}; | 145 WASM_I8(11))}; |
| 141 uint32_t local_indices[] = {7, 19, 25, 28}; | 146 uint32_t local_indices[] = {7, 19, 25, 28}; |
| 142 f->EmitCode(code, sizeof(code), local_indices, sizeof(local_indices) / 4); | 147 f->EmitCode(code, sizeof(code), local_indices, sizeof(local_indices) / 4); |
| 143 WasmModuleWriter* writer = builder->Build(&zone); | 148 WasmModuleWriter* writer = builder->Build(&zone); |
| 144 TestModule(writer->WriteTo(&zone), 11); | 149 TestModule(writer->WriteTo(&zone), 11); |
| 145 } | 150 } |
| 146 #endif | 151 #endif |
| 147 | 152 |
| 148 | 153 |
| 149 #if !defined(V8_WITH_ASAN) | 154 #if !defined(V8_WITH_ASAN) && !defined(V8_TARGET_ARCH_PPC) && \ |
| 155 !defined(V8_TARGET_ARCH_PPC64) |
| 156 // TODO(tizer): Figure out why this crashes with PPC. |
| 150 // TODO(bradnelson): Figure out why this crashes under asan. | 157 // TODO(bradnelson): Figure out why this crashes under asan. |
| 151 TEST(Run_WasmModule_CallMain_recursive) { | 158 TEST(Run_WasmModule_CallMain_recursive) { |
| 152 Zone zone; | 159 Zone zone; |
| 153 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 160 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 154 uint16_t f_index = builder->AddFunction(); | 161 uint16_t f_index = builder->AddFunction(); |
| 155 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 162 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
| 156 f->ReturnType(kAstI32); | 163 f->ReturnType(kAstI32); |
| 157 uint16_t localIndex = f->AddLocal(kAstI32); | 164 uint16_t localIndex = f->AddLocal(kAstI32); |
| 158 f->Exported(1); | 165 f->Exported(1); |
| 159 byte code[] = {WASM_BLOCK( | 166 byte code[] = {WASM_BLOCK( |
| 160 2, WASM_SET_LOCAL(localIndex, | 167 2, WASM_SET_LOCAL(localIndex, |
| 161 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), | 168 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), |
| 162 WASM_IF_ELSE(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I8(5)), | 169 WASM_IF_ELSE(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I8(5)), |
| 163 WASM_BLOCK(2, WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, | 170 WASM_BLOCK(2, WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, |
| 164 WASM_INC_LOCAL(localIndex)), | 171 WASM_INC_LOCAL(localIndex)), |
| 165 WASM_BRV(1, WASM_CALL_FUNCTION0(0))), | 172 WASM_BRV(1, WASM_CALL_FUNCTION0(0))), |
| 166 WASM_BRV(0, WASM_I8(55))))}; | 173 WASM_BRV(0, WASM_I8(55))))}; |
| 167 uint32_t local_indices[] = {3, 11, 21, 24}; | 174 uint32_t local_indices[] = {3, 11, 21, 24}; |
| 168 f->EmitCode(code, sizeof(code), local_indices, sizeof(local_indices) / 4); | 175 f->EmitCode(code, sizeof(code), local_indices, sizeof(local_indices) / 4); |
| 169 WasmModuleWriter* writer = builder->Build(&zone); | 176 WasmModuleWriter* writer = builder->Build(&zone); |
| 170 TestModule(writer->WriteTo(&zone), 55); | 177 TestModule(writer->WriteTo(&zone), 55); |
| 171 } | 178 } |
| 172 #endif | 179 #endif |
| 173 | 180 |
| 174 | 181 |
| 175 #if !defined(V8_WITH_ASAN) | 182 #if !defined(V8_WITH_ASAN) && !defined(V8_TARGET_ARCH_PPC) && \ |
| 183 !defined(V8_TARGET_ARCH_PPC64) |
| 184 // TODO(tizer): Figure out why this fails with PPC. |
| 176 // TODO(bradnelson): Figure out why this crashes under asan. | 185 // TODO(bradnelson): Figure out why this crashes under asan. |
| 177 TEST(Run_WasmModule_Global) { | 186 TEST(Run_WasmModule_Global) { |
| 178 Zone zone; | 187 Zone zone; |
| 179 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 188 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 180 uint32_t global1 = builder->AddGlobal(MachineType::Int32(), 0); | 189 uint32_t global1 = builder->AddGlobal(MachineType::Int32(), 0); |
| 181 uint32_t global2 = builder->AddGlobal(MachineType::Int32(), 0); | 190 uint32_t global2 = builder->AddGlobal(MachineType::Int32(), 0); |
| 182 uint16_t f1_index = builder->AddFunction(); | 191 uint16_t f1_index = builder->AddFunction(); |
| 183 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); | 192 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); |
| 184 f->ReturnType(kAstI32); | 193 f->ReturnType(kAstI32); |
| 185 byte code1[] = { | 194 byte code1[] = { |
| 186 WASM_I32_ADD(WASM_LOAD_GLOBAL(global1), WASM_LOAD_GLOBAL(global2))}; | 195 WASM_I32_ADD(WASM_LOAD_GLOBAL(global1), WASM_LOAD_GLOBAL(global2))}; |
| 187 f->EmitCode(code1, sizeof(code1)); | 196 f->EmitCode(code1, sizeof(code1)); |
| 188 uint16_t f2_index = builder->AddFunction(); | 197 uint16_t f2_index = builder->AddFunction(); |
| 189 f = builder->FunctionAt(f2_index); | 198 f = builder->FunctionAt(f2_index); |
| 190 f->ReturnType(kAstI32); | 199 f->ReturnType(kAstI32); |
| 191 f->Exported(1); | 200 f->Exported(1); |
| 192 byte code2[] = {WASM_STORE_GLOBAL(global1, WASM_I32(56)), | 201 byte code2[] = {WASM_STORE_GLOBAL(global1, WASM_I32(56)), |
| 193 WASM_STORE_GLOBAL(global2, WASM_I32(41)), | 202 WASM_STORE_GLOBAL(global2, WASM_I32(41)), |
| 194 WASM_RETURN(WASM_CALL_FUNCTION0(f1_index))}; | 203 WASM_RETURN(WASM_CALL_FUNCTION0(f1_index))}; |
| 195 f->EmitCode(code2, sizeof(code2)); | 204 f->EmitCode(code2, sizeof(code2)); |
| 196 WasmModuleWriter* writer = builder->Build(&zone); | 205 WasmModuleWriter* writer = builder->Build(&zone); |
| 197 TestModule(writer->WriteTo(&zone), 97); | 206 TestModule(writer->WriteTo(&zone), 97); |
| 198 } | 207 } |
| 199 #endif | 208 #endif |
| OLD | NEW |