| 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-js.h" | 9 #include "src/wasm/wasm-js.h" |
| 10 #include "src/wasm/wasm-macro-gen.h" | 10 #include "src/wasm/wasm-macro-gen.h" |
| 11 #include "src/wasm/wasm-module.h" | 11 #include "src/wasm/wasm-module.h" |
| 12 #include "src/wasm/wasm-opcodes.h" | 12 #include "src/wasm/wasm-opcodes.h" |
| 13 | 13 |
| 14 #include "test/cctest/cctest.h" | 14 #include "test/cctest/cctest.h" |
| 15 #include "test/cctest/wasm/test-signatures.h" | 15 #include "test/cctest/wasm/test-signatures.h" |
| 16 | 16 |
| 17 using namespace v8::base; | 17 using namespace v8::base; |
| 18 using namespace v8::internal; | 18 using namespace v8::internal; |
| 19 using namespace v8::internal::compiler; | 19 using namespace v8::internal::compiler; |
| 20 using namespace v8::internal::wasm; | 20 using namespace v8::internal::wasm; |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 void TestModule(Zone* zone, WasmModuleBuilder* builder, | 23 void TestModule(Zone* zone, WasmModuleBuilder* builder, |
| 24 int32_t expected_result) { | 24 int32_t expected_result) { |
| 25 ZoneBuffer buffer(zone); | 25 ZoneBuffer buffer(zone); |
| 26 WasmModuleWriter* writer = builder->Build(zone); | 26 builder->WriteTo(buffer); |
| 27 writer->WriteTo(buffer); | |
| 28 | 27 |
| 29 Isolate* isolate = CcTest::InitIsolateOnce(); | 28 Isolate* isolate = CcTest::InitIsolateOnce(); |
| 30 HandleScope scope(isolate); | 29 HandleScope scope(isolate); |
| 31 WasmJs::InstallWasmFunctionMap(isolate, isolate->native_context()); | 30 WasmJs::InstallWasmFunctionMap(isolate, isolate->native_context()); |
| 32 int32_t result = | 31 int32_t result = |
| 33 CompileAndRunWasmModule(isolate, buffer.begin(), buffer.end()); | 32 CompileAndRunWasmModule(isolate, buffer.begin(), buffer.end()); |
| 34 CHECK_EQ(expected_result, result); | 33 CHECK_EQ(expected_result, result); |
| 35 } | 34 } |
| 36 } // namespace | 35 } // namespace |
| 37 | 36 |
| 38 TEST(Run_WasmModule_Return114) { | 37 TEST(Run_WasmModule_Return114) { |
| 39 static const int32_t kReturnValue = 114; | 38 static const int32_t kReturnValue = 114; |
| 40 TestSignatures sigs; | 39 TestSignatures sigs; |
| 41 v8::base::AccountingAllocator allocator; | 40 v8::base::AccountingAllocator allocator; |
| 42 Zone zone(&allocator); | 41 Zone zone(&allocator); |
| 43 | 42 |
| 44 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 43 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 45 uint16_t f_index = builder->AddFunction(); | 44 uint16_t f_index = builder->AddFunction(); |
| 46 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 45 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
| 47 f->SetSignature(sigs.i_v()); | 46 f->SetSignature(sigs.i_v()); |
| 48 f->Exported(1); | 47 f->SetExported(); |
| 49 byte code[] = {WASM_I8(kReturnValue)}; | 48 byte code[] = {WASM_I8(kReturnValue)}; |
| 50 f->EmitCode(code, sizeof(code)); | 49 f->EmitCode(code, sizeof(code)); |
| 51 TestModule(&zone, builder, kReturnValue); | 50 TestModule(&zone, builder, kReturnValue); |
| 52 } | 51 } |
| 53 | 52 |
| 54 TEST(Run_WasmModule_CallAdd) { | 53 TEST(Run_WasmModule_CallAdd) { |
| 55 v8::base::AccountingAllocator allocator; | 54 v8::base::AccountingAllocator allocator; |
| 56 Zone zone(&allocator); | 55 Zone zone(&allocator); |
| 57 TestSignatures sigs; | 56 TestSignatures sigs; |
| 58 | 57 |
| 59 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 58 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 60 | 59 |
| 61 uint16_t f1_index = builder->AddFunction(); | 60 uint16_t f1_index = builder->AddFunction(); |
| 62 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); | 61 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); |
| 63 f->SetSignature(sigs.i_ii()); | 62 f->SetSignature(sigs.i_ii()); |
| 64 uint16_t param1 = 0; | 63 uint16_t param1 = 0; |
| 65 uint16_t param2 = 1; | 64 uint16_t param2 = 1; |
| 66 byte code1[] = {WASM_I32_ADD(WASM_GET_LOCAL(param1), WASM_GET_LOCAL(param2))}; | 65 byte code1[] = {WASM_I32_ADD(WASM_GET_LOCAL(param1), WASM_GET_LOCAL(param2))}; |
| 67 f->EmitCode(code1, sizeof(code1)); | 66 f->EmitCode(code1, sizeof(code1)); |
| 68 | 67 |
| 69 uint16_t f2_index = builder->AddFunction(); | 68 uint16_t f2_index = builder->AddFunction(); |
| 70 f = builder->FunctionAt(f2_index); | 69 f = builder->FunctionAt(f2_index); |
| 71 f->SetSignature(sigs.i_v()); | 70 f->SetSignature(sigs.i_v()); |
| 72 | 71 |
| 73 f->Exported(1); | 72 f->SetExported(); |
| 74 byte code2[] = {WASM_CALL_FUNCTION2(f1_index, WASM_I8(77), WASM_I8(22))}; | 73 byte code2[] = {WASM_CALL_FUNCTION2(f1_index, WASM_I8(77), WASM_I8(22))}; |
| 75 f->EmitCode(code2, sizeof(code2)); | 74 f->EmitCode(code2, sizeof(code2)); |
| 76 TestModule(&zone, builder, 99); | 75 TestModule(&zone, builder, 99); |
| 77 } | 76 } |
| 78 | 77 |
| 79 TEST(Run_WasmModule_ReadLoadedDataSegment) { | 78 TEST(Run_WasmModule_ReadLoadedDataSegment) { |
| 80 static const byte kDataSegmentDest0 = 12; | 79 static const byte kDataSegmentDest0 = 12; |
| 81 v8::base::AccountingAllocator allocator; | 80 v8::base::AccountingAllocator allocator; |
| 82 Zone zone(&allocator); | 81 Zone zone(&allocator); |
| 83 TestSignatures sigs; | 82 TestSignatures sigs; |
| 84 | 83 |
| 85 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 84 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 86 uint16_t f_index = builder->AddFunction(); | 85 uint16_t f_index = builder->AddFunction(); |
| 87 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 86 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
| 88 f->SetSignature(sigs.i_v()); | 87 f->SetSignature(sigs.i_v()); |
| 89 | 88 |
| 90 f->Exported(1); | 89 f->SetExported(); |
| 91 byte code[] = { | 90 byte code[] = { |
| 92 WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kDataSegmentDest0))}; | 91 WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kDataSegmentDest0))}; |
| 93 f->EmitCode(code, sizeof(code)); | 92 f->EmitCode(code, sizeof(code)); |
| 94 byte data[] = {0xaa, 0xbb, 0xcc, 0xdd}; | 93 byte data[] = {0xaa, 0xbb, 0xcc, 0xdd}; |
| 95 builder->AddDataSegment(new (&zone) WasmDataSegmentEncoder( | 94 builder->AddDataSegment(new (&zone) WasmDataSegmentEncoder( |
| 96 &zone, data, sizeof(data), kDataSegmentDest0)); | 95 &zone, data, sizeof(data), kDataSegmentDest0)); |
| 97 TestModule(&zone, builder, 0xddccbbaa); | 96 TestModule(&zone, builder, 0xddccbbaa); |
| 98 } | 97 } |
| 99 | 98 |
| 100 TEST(Run_WasmModule_CheckMemoryIsZero) { | 99 TEST(Run_WasmModule_CheckMemoryIsZero) { |
| 101 static const int kCheckSize = 16 * 1024; | 100 static const int kCheckSize = 16 * 1024; |
| 102 v8::base::AccountingAllocator allocator; | 101 v8::base::AccountingAllocator allocator; |
| 103 Zone zone(&allocator); | 102 Zone zone(&allocator); |
| 104 TestSignatures sigs; | 103 TestSignatures sigs; |
| 105 | 104 |
| 106 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 105 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 107 uint16_t f_index = builder->AddFunction(); | 106 uint16_t f_index = builder->AddFunction(); |
| 108 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 107 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
| 109 f->SetSignature(sigs.i_v()); | 108 f->SetSignature(sigs.i_v()); |
| 110 | 109 |
| 111 uint16_t localIndex = f->AddLocal(kAstI32); | 110 uint16_t localIndex = f->AddLocal(kAstI32); |
| 112 f->Exported(1); | 111 f->SetExported(); |
| 113 byte code[] = {WASM_BLOCK( | 112 byte code[] = {WASM_BLOCK( |
| 114 2, | 113 2, |
| 115 WASM_WHILE( | 114 WASM_WHILE( |
| 116 WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I32V_3(kCheckSize)), | 115 WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I32V_3(kCheckSize)), |
| 117 WASM_IF_ELSE( | 116 WASM_IF_ELSE( |
| 118 WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(localIndex)), | 117 WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(localIndex)), |
| 119 WASM_BRV(2, WASM_I8(-1)), WASM_INC_LOCAL_BY(localIndex, 4))), | 118 WASM_BRV(2, WASM_I8(-1)), WASM_INC_LOCAL_BY(localIndex, 4))), |
| 120 WASM_I8(11))}; | 119 WASM_I8(11))}; |
| 121 f->EmitCode(code, sizeof(code)); | 120 f->EmitCode(code, sizeof(code)); |
| 122 TestModule(&zone, builder, 11); | 121 TestModule(&zone, builder, 11); |
| 123 } | 122 } |
| 124 | 123 |
| 125 TEST(Run_WasmModule_CallMain_recursive) { | 124 TEST(Run_WasmModule_CallMain_recursive) { |
| 126 v8::base::AccountingAllocator allocator; | 125 v8::base::AccountingAllocator allocator; |
| 127 Zone zone(&allocator); | 126 Zone zone(&allocator); |
| 128 TestSignatures sigs; | 127 TestSignatures sigs; |
| 129 | 128 |
| 130 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 129 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 131 uint16_t f_index = builder->AddFunction(); | 130 uint16_t f_index = builder->AddFunction(); |
| 132 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 131 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
| 133 f->SetSignature(sigs.i_v()); | 132 f->SetSignature(sigs.i_v()); |
| 134 | 133 |
| 135 uint16_t localIndex = f->AddLocal(kAstI32); | 134 uint16_t localIndex = f->AddLocal(kAstI32); |
| 136 f->Exported(1); | 135 f->SetExported(); |
| 137 byte code[] = {WASM_BLOCK( | 136 byte code[] = {WASM_BLOCK( |
| 138 2, WASM_SET_LOCAL(localIndex, | 137 2, WASM_SET_LOCAL(localIndex, |
| 139 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), | 138 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), |
| 140 WASM_IF_ELSE(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I8(5)), | 139 WASM_IF_ELSE(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I8(5)), |
| 141 WASM_BLOCK(2, WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, | 140 WASM_BLOCK(2, WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, |
| 142 WASM_INC_LOCAL(localIndex)), | 141 WASM_INC_LOCAL(localIndex)), |
| 143 WASM_BRV(1, WASM_CALL_FUNCTION0(0))), | 142 WASM_BRV(1, WASM_CALL_FUNCTION0(0))), |
| 144 WASM_BRV(0, WASM_I8(55))))}; | 143 WASM_BRV(0, WASM_I8(55))))}; |
| 145 f->EmitCode(code, sizeof(code)); | 144 f->EmitCode(code, sizeof(code)); |
| 146 TestModule(&zone, builder, 55); | 145 TestModule(&zone, builder, 55); |
| 147 } | 146 } |
| 148 | 147 |
| 149 TEST(Run_WasmModule_Global) { | 148 TEST(Run_WasmModule_Global) { |
| 150 v8::base::AccountingAllocator allocator; | 149 v8::base::AccountingAllocator allocator; |
| 151 Zone zone(&allocator); | 150 Zone zone(&allocator); |
| 152 TestSignatures sigs; | 151 TestSignatures sigs; |
| 153 | 152 |
| 154 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 153 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 155 uint32_t global1 = builder->AddGlobal(MachineType::Int32(), 0); | 154 uint32_t global1 = builder->AddGlobal(MachineType::Int32(), 0); |
| 156 uint32_t global2 = builder->AddGlobal(MachineType::Int32(), 0); | 155 uint32_t global2 = builder->AddGlobal(MachineType::Int32(), 0); |
| 157 uint16_t f1_index = builder->AddFunction(); | 156 uint16_t f1_index = builder->AddFunction(); |
| 158 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); | 157 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); |
| 159 f->SetSignature(sigs.i_v()); | 158 f->SetSignature(sigs.i_v()); |
| 160 byte code1[] = { | 159 byte code1[] = { |
| 161 WASM_I32_ADD(WASM_LOAD_GLOBAL(global1), WASM_LOAD_GLOBAL(global2))}; | 160 WASM_I32_ADD(WASM_LOAD_GLOBAL(global1), WASM_LOAD_GLOBAL(global2))}; |
| 162 f->EmitCode(code1, sizeof(code1)); | 161 f->EmitCode(code1, sizeof(code1)); |
| 163 uint16_t f2_index = builder->AddFunction(); | 162 uint16_t f2_index = builder->AddFunction(); |
| 164 f = builder->FunctionAt(f2_index); | 163 f = builder->FunctionAt(f2_index); |
| 165 f->SetSignature(sigs.i_v()); | 164 f->SetSignature(sigs.i_v()); |
| 166 f->Exported(1); | 165 f->SetExported(); |
| 167 byte code2[] = {WASM_STORE_GLOBAL(global1, WASM_I32V_1(56)), | 166 byte code2[] = {WASM_STORE_GLOBAL(global1, WASM_I32V_1(56)), |
| 168 WASM_STORE_GLOBAL(global2, WASM_I32V_1(41)), | 167 WASM_STORE_GLOBAL(global2, WASM_I32V_1(41)), |
| 169 WASM_RETURN1(WASM_CALL_FUNCTION0(f1_index))}; | 168 WASM_RETURN1(WASM_CALL_FUNCTION0(f1_index))}; |
| 170 f->EmitCode(code2, sizeof(code2)); | 169 f->EmitCode(code2, sizeof(code2)); |
| 171 TestModule(&zone, builder, 97); | 170 TestModule(&zone, builder, 97); |
| 172 } | 171 } |
| OLD | NEW |