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 | 15 |
16 using namespace v8::base; | 16 using namespace v8::base; |
17 using namespace v8::internal; | 17 using namespace v8::internal; |
18 using namespace v8::internal::compiler; | 18 using namespace v8::internal::compiler; |
19 using namespace v8::internal::wasm; | 19 using namespace v8::internal::wasm; |
20 | 20 |
21 | |
22 // TODO(titzer): fix arm64 frame alignment. | |
23 namespace { | 21 namespace { |
24 void TestModule(WasmModuleIndex* module, int32_t expected_result) { | 22 void TestModule(WasmModuleIndex* module, int32_t expected_result) { |
25 Isolate* isolate = CcTest::InitIsolateOnce(); | 23 Isolate* isolate = CcTest::InitIsolateOnce(); |
26 HandleScope scope(isolate); | 24 HandleScope scope(isolate); |
27 WasmJs::InstallWasmFunctionMap(isolate, isolate->native_context()); | 25 WasmJs::InstallWasmFunctionMap(isolate, isolate->native_context()); |
28 int32_t result = | 26 int32_t result = |
29 CompileAndRunWasmModule(isolate, module->Begin(), module->End()); | 27 CompileAndRunWasmModule(isolate, module->Begin(), module->End()); |
30 CHECK_EQ(expected_result, result); | 28 CHECK_EQ(expected_result, result); |
31 } | 29 } |
32 } // namespace | 30 } // namespace |
33 | 31 |
34 | |
35 // A raw test that skips the WasmModuleBuilder. | |
36 TEST(Run_WasmModule_CallAdd_rev) { | |
37 static const byte data[] = { | |
38 WASM_MODULE_HEADER, | |
39 // sig#0 ------------------------------------------ | |
40 WASM_SECTION_SIGNATURES_SIZE + 7, // Section size. | |
41 WASM_SECTION_SIGNATURES, 2, // -- | |
42 0, kLocalI32, // void -> int | |
43 2, kLocalI32, kLocalI32, kLocalI32, // int,int -> int | |
44 // func#0 (main) ---------------------------------- | |
45 WASM_SECTION_FUNCTIONS_SIZE + 25, WASM_SECTION_FUNCTIONS, 2, | |
46 kDeclFunctionExport, 0, 0, // sig index | |
47 8, 0, // body size | |
48 0, // locals | |
49 kExprI8Const, 77, // -- | |
50 kExprI8Const, 22, // -- | |
51 kExprCallFunction, 2, 1, // -- | |
52 // func#1 ----------------------------------------- | |
53 0, // no name, not exported | |
54 1, 0, // sig index | |
55 6, 0, // body size | |
56 0, // locals | |
57 kExprGetLocal, 0, // -- | |
58 kExprGetLocal, 1, // -- | |
59 kExprI32Add, // -- | |
60 }; | |
61 | |
62 Isolate* isolate = CcTest::InitIsolateOnce(); | |
63 HandleScope scope(isolate); | |
64 WasmJs::InstallWasmFunctionMap(isolate, isolate->native_context()); | |
65 int32_t result = | |
66 CompileAndRunWasmModule(isolate, data, data + arraysize(data)); | |
67 CHECK_EQ(99, result); | |
68 } | |
69 | |
70 | |
71 TEST(Run_WasmModule_Return114) { | 32 TEST(Run_WasmModule_Return114) { |
72 static const int32_t kReturnValue = 114; | 33 static const int32_t kReturnValue = 114; |
73 v8::base::AccountingAllocator allocator; | 34 v8::base::AccountingAllocator allocator; |
74 Zone zone(&allocator); | 35 Zone zone(&allocator); |
75 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 36 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
76 uint16_t f_index = builder->AddFunction(); | 37 uint16_t f_index = builder->AddFunction(); |
77 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 38 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
78 f->ReturnType(kAstI32); | 39 f->ReturnType(kAstI32); |
79 f->Exported(1); | 40 f->Exported(1); |
80 byte code[] = {WASM_I8(kReturnValue)}; | 41 byte code[] = {WASM_I8(kReturnValue)}; |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 f = builder->FunctionAt(f2_index); | 147 f = builder->FunctionAt(f2_index); |
187 f->ReturnType(kAstI32); | 148 f->ReturnType(kAstI32); |
188 f->Exported(1); | 149 f->Exported(1); |
189 byte code2[] = {WASM_STORE_GLOBAL(global1, WASM_I32V_1(56)), | 150 byte code2[] = {WASM_STORE_GLOBAL(global1, WASM_I32V_1(56)), |
190 WASM_STORE_GLOBAL(global2, WASM_I32V_1(41)), | 151 WASM_STORE_GLOBAL(global2, WASM_I32V_1(41)), |
191 WASM_RETURN1(WASM_CALL_FUNCTION0(f1_index))}; | 152 WASM_RETURN1(WASM_CALL_FUNCTION0(f1_index))}; |
192 f->EmitCode(code2, sizeof(code2)); | 153 f->EmitCode(code2, sizeof(code2)); |
193 WasmModuleWriter* writer = builder->Build(&zone); | 154 WasmModuleWriter* writer = builder->Build(&zone); |
194 TestModule(writer->WriteTo(&zone), 97); | 155 TestModule(writer->WriteTo(&zone), 97); |
195 } | 156 } |
OLD | NEW |