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