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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 HandleScope scope(isolate); | 62 HandleScope scope(isolate); |
63 WasmJs::InstallWasmFunctionMap(isolate, isolate->native_context()); | 63 WasmJs::InstallWasmFunctionMap(isolate, isolate->native_context()); |
64 int32_t result = | 64 int32_t result = |
65 CompileAndRunWasmModule(isolate, data, data + arraysize(data)); | 65 CompileAndRunWasmModule(isolate, data, data + arraysize(data)); |
66 CHECK_EQ(99, result); | 66 CHECK_EQ(99, result); |
67 } | 67 } |
68 | 68 |
69 | 69 |
70 TEST(Run_WasmModule_Return114) { | 70 TEST(Run_WasmModule_Return114) { |
71 static const int32_t kReturnValue = 114; | 71 static const int32_t kReturnValue = 114; |
72 Zone zone; | 72 ZoneForTesting zone; |
73 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 73 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
74 uint16_t f_index = builder->AddFunction(); | 74 uint16_t f_index = builder->AddFunction(); |
75 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 75 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
76 f->ReturnType(kAstI32); | 76 f->ReturnType(kAstI32); |
77 f->Exported(1); | 77 f->Exported(1); |
78 byte code[] = {WASM_I8(kReturnValue)}; | 78 byte code[] = {WASM_I8(kReturnValue)}; |
79 f->EmitCode(code, sizeof(code)); | 79 f->EmitCode(code, sizeof(code)); |
80 WasmModuleWriter* writer = builder->Build(&zone); | 80 WasmModuleWriter* writer = builder->Build(&zone); |
81 TestModule(writer->WriteTo(&zone), kReturnValue); | 81 TestModule(writer->WriteTo(&zone), kReturnValue); |
82 } | 82 } |
83 | 83 |
84 | 84 |
85 TEST(Run_WasmModule_CallAdd) { | 85 TEST(Run_WasmModule_CallAdd) { |
86 Zone zone; | 86 ZoneForTesting zone; |
87 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 87 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
88 uint16_t f1_index = builder->AddFunction(); | 88 uint16_t f1_index = builder->AddFunction(); |
89 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); | 89 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); |
90 f->ReturnType(kAstI32); | 90 f->ReturnType(kAstI32); |
91 uint16_t param1 = f->AddParam(kAstI32); | 91 uint16_t param1 = f->AddParam(kAstI32); |
92 uint16_t param2 = f->AddParam(kAstI32); | 92 uint16_t param2 = f->AddParam(kAstI32); |
93 byte code1[] = {WASM_I32_ADD(WASM_GET_LOCAL(param1), WASM_GET_LOCAL(param2))}; | 93 byte code1[] = {WASM_I32_ADD(WASM_GET_LOCAL(param1), WASM_GET_LOCAL(param2))}; |
94 uint32_t local_indices1[] = {2, 4}; | 94 uint32_t local_indices1[] = {2, 4}; |
95 f->EmitCode(code1, sizeof(code1), local_indices1, sizeof(local_indices1) / 4); | 95 f->EmitCode(code1, sizeof(code1), local_indices1, sizeof(local_indices1) / 4); |
96 uint16_t f2_index = builder->AddFunction(); | 96 uint16_t f2_index = builder->AddFunction(); |
97 f = builder->FunctionAt(f2_index); | 97 f = builder->FunctionAt(f2_index); |
98 f->ReturnType(kAstI32); | 98 f->ReturnType(kAstI32); |
99 f->Exported(1); | 99 f->Exported(1); |
100 byte code2[] = {WASM_CALL_FUNCTION(f1_index, WASM_I8(77), WASM_I8(22))}; | 100 byte code2[] = {WASM_CALL_FUNCTION(f1_index, WASM_I8(77), WASM_I8(22))}; |
101 f->EmitCode(code2, sizeof(code2)); | 101 f->EmitCode(code2, sizeof(code2)); |
102 WasmModuleWriter* writer = builder->Build(&zone); | 102 WasmModuleWriter* writer = builder->Build(&zone); |
103 TestModule(writer->WriteTo(&zone), 99); | 103 TestModule(writer->WriteTo(&zone), 99); |
104 } | 104 } |
105 | 105 |
106 | 106 |
107 TEST(Run_WasmModule_ReadLoadedDataSegment) { | 107 TEST(Run_WasmModule_ReadLoadedDataSegment) { |
108 static const byte kDataSegmentDest0 = 12; | 108 static const byte kDataSegmentDest0 = 12; |
109 Zone zone; | 109 ZoneForTesting zone; |
110 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 110 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
111 uint16_t f_index = builder->AddFunction(); | 111 uint16_t f_index = builder->AddFunction(); |
112 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 112 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
113 f->ReturnType(kAstI32); | 113 f->ReturnType(kAstI32); |
114 f->Exported(1); | 114 f->Exported(1); |
115 byte code[] = { | 115 byte code[] = { |
116 WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kDataSegmentDest0))}; | 116 WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kDataSegmentDest0))}; |
117 f->EmitCode(code, sizeof(code)); | 117 f->EmitCode(code, sizeof(code)); |
118 byte data[] = {0xaa, 0xbb, 0xcc, 0xdd}; | 118 byte data[] = {0xaa, 0xbb, 0xcc, 0xdd}; |
119 builder->AddDataSegment(new (&zone) WasmDataSegmentEncoder( | 119 builder->AddDataSegment(new (&zone) WasmDataSegmentEncoder( |
120 &zone, data, sizeof(data), kDataSegmentDest0)); | 120 &zone, data, sizeof(data), kDataSegmentDest0)); |
121 WasmModuleWriter* writer = builder->Build(&zone); | 121 WasmModuleWriter* writer = builder->Build(&zone); |
122 TestModule(writer->WriteTo(&zone), 0xddccbbaa); | 122 TestModule(writer->WriteTo(&zone), 0xddccbbaa); |
123 } | 123 } |
124 | 124 |
125 TEST(Run_WasmModule_CheckMemoryIsZero) { | 125 TEST(Run_WasmModule_CheckMemoryIsZero) { |
126 static const int kCheckSize = 16 * 1024; | 126 static const int kCheckSize = 16 * 1024; |
127 Zone zone; | 127 ZoneForTesting zone; |
128 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 128 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
129 uint16_t f_index = builder->AddFunction(); | 129 uint16_t f_index = builder->AddFunction(); |
130 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 130 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
131 f->ReturnType(kAstI32); | 131 f->ReturnType(kAstI32); |
132 uint16_t localIndex = f->AddLocal(kAstI32); | 132 uint16_t localIndex = f->AddLocal(kAstI32); |
133 f->Exported(1); | 133 f->Exported(1); |
134 byte code[] = {WASM_BLOCK( | 134 byte code[] = {WASM_BLOCK( |
135 2, | 135 2, |
136 WASM_WHILE( | 136 WASM_WHILE( |
137 WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I32V_3(kCheckSize)), | 137 WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I32V_3(kCheckSize)), |
138 WASM_IF_ELSE( | 138 WASM_IF_ELSE( |
139 WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(localIndex)), | 139 WASM_LOAD_MEM(MachineType::Int32(), WASM_GET_LOCAL(localIndex)), |
140 WASM_BRV(2, WASM_I8(-1)), WASM_INC_LOCAL_BY(localIndex, 4))), | 140 WASM_BRV(2, WASM_I8(-1)), WASM_INC_LOCAL_BY(localIndex, 4))), |
141 WASM_I8(11))}; | 141 WASM_I8(11))}; |
142 f->EmitCode(code, sizeof(code), nullptr, 0); | 142 f->EmitCode(code, sizeof(code), nullptr, 0); |
143 WasmModuleWriter* writer = builder->Build(&zone); | 143 WasmModuleWriter* writer = builder->Build(&zone); |
144 TestModule(writer->WriteTo(&zone), 11); | 144 TestModule(writer->WriteTo(&zone), 11); |
145 } | 145 } |
146 | 146 |
147 TEST(Run_WasmModule_CallMain_recursive) { | 147 TEST(Run_WasmModule_CallMain_recursive) { |
148 Zone zone; | 148 ZoneForTesting zone; |
149 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 149 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
150 uint16_t f_index = builder->AddFunction(); | 150 uint16_t f_index = builder->AddFunction(); |
151 WasmFunctionBuilder* f = builder->FunctionAt(f_index); | 151 WasmFunctionBuilder* f = builder->FunctionAt(f_index); |
152 f->ReturnType(kAstI32); | 152 f->ReturnType(kAstI32); |
153 uint16_t localIndex = f->AddLocal(kAstI32); | 153 uint16_t localIndex = f->AddLocal(kAstI32); |
154 f->Exported(1); | 154 f->Exported(1); |
155 byte code[] = {WASM_BLOCK( | 155 byte code[] = {WASM_BLOCK( |
156 2, WASM_SET_LOCAL(localIndex, | 156 2, WASM_SET_LOCAL(localIndex, |
157 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), | 157 WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), |
158 WASM_IF_ELSE(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I8(5)), | 158 WASM_IF_ELSE(WASM_I32_LTS(WASM_GET_LOCAL(localIndex), WASM_I8(5)), |
159 WASM_BLOCK(2, WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, | 159 WASM_BLOCK(2, WASM_STORE_MEM(MachineType::Int32(), WASM_ZERO, |
160 WASM_INC_LOCAL(localIndex)), | 160 WASM_INC_LOCAL(localIndex)), |
161 WASM_BRV(1, WASM_CALL_FUNCTION0(0))), | 161 WASM_BRV(1, WASM_CALL_FUNCTION0(0))), |
162 WASM_BRV(0, WASM_I8(55))))}; | 162 WASM_BRV(0, WASM_I8(55))))}; |
163 f->EmitCode(code, sizeof(code), nullptr, 0); | 163 f->EmitCode(code, sizeof(code), nullptr, 0); |
164 WasmModuleWriter* writer = builder->Build(&zone); | 164 WasmModuleWriter* writer = builder->Build(&zone); |
165 TestModule(writer->WriteTo(&zone), 55); | 165 TestModule(writer->WriteTo(&zone), 55); |
166 } | 166 } |
167 | 167 |
168 TEST(Run_WasmModule_Global) { | 168 TEST(Run_WasmModule_Global) { |
169 Zone zone; | 169 ZoneForTesting zone; |
170 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 170 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
171 uint32_t global1 = builder->AddGlobal(MachineType::Int32(), 0); | 171 uint32_t global1 = builder->AddGlobal(MachineType::Int32(), 0); |
172 uint32_t global2 = builder->AddGlobal(MachineType::Int32(), 0); | 172 uint32_t global2 = builder->AddGlobal(MachineType::Int32(), 0); |
173 uint16_t f1_index = builder->AddFunction(); | 173 uint16_t f1_index = builder->AddFunction(); |
174 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); | 174 WasmFunctionBuilder* f = builder->FunctionAt(f1_index); |
175 f->ReturnType(kAstI32); | 175 f->ReturnType(kAstI32); |
176 byte code1[] = { | 176 byte code1[] = { |
177 WASM_I32_ADD(WASM_LOAD_GLOBAL(global1), WASM_LOAD_GLOBAL(global2))}; | 177 WASM_I32_ADD(WASM_LOAD_GLOBAL(global1), WASM_LOAD_GLOBAL(global2))}; |
178 f->EmitCode(code1, sizeof(code1)); | 178 f->EmitCode(code1, sizeof(code1)); |
179 uint16_t f2_index = builder->AddFunction(); | 179 uint16_t f2_index = builder->AddFunction(); |
180 f = builder->FunctionAt(f2_index); | 180 f = builder->FunctionAt(f2_index); |
181 f->ReturnType(kAstI32); | 181 f->ReturnType(kAstI32); |
182 f->Exported(1); | 182 f->Exported(1); |
183 byte code2[] = {WASM_STORE_GLOBAL(global1, WASM_I32V_1(56)), | 183 byte code2[] = {WASM_STORE_GLOBAL(global1, WASM_I32V_1(56)), |
184 WASM_STORE_GLOBAL(global2, WASM_I32V_1(41)), | 184 WASM_STORE_GLOBAL(global2, WASM_I32V_1(41)), |
185 WASM_RETURN(WASM_CALL_FUNCTION0(f1_index))}; | 185 WASM_RETURN(WASM_CALL_FUNCTION0(f1_index))}; |
186 f->EmitCode(code2, sizeof(code2)); | 186 f->EmitCode(code2, sizeof(code2)); |
187 WasmModuleWriter* writer = builder->Build(&zone); | 187 WasmModuleWriter* writer = builder->Build(&zone); |
188 TestModule(writer->WriteTo(&zone), 97); | 188 TestModule(writer->WriteTo(&zone), 97); |
189 } | 189 } |
OLD | NEW |