Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef WASM_RUN_UTILS_H | 5 #ifndef WASM_RUN_UTILS_H |
| 6 #define WASM_RUN_UTILS_H | 6 #define WASM_RUN_UTILS_H |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 #include <string.h> | 10 #include <string.h> |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 140 | 140 |
| 141 template <typename T> | 141 template <typename T> |
| 142 T* raw_mem_end() { | 142 T* raw_mem_end() { |
| 143 DCHECK(instance->mem_start); | 143 DCHECK(instance->mem_start); |
| 144 return reinterpret_cast<T*>(instance->mem_start + instance->mem_size); | 144 return reinterpret_cast<T*>(instance->mem_start + instance->mem_size); |
| 145 } | 145 } |
| 146 | 146 |
| 147 template <typename T> | 147 template <typename T> |
| 148 T raw_mem_at(int i) { | 148 T raw_mem_at(int i) { |
| 149 DCHECK(instance->mem_start); | 149 DCHECK(instance->mem_start); |
| 150 return reinterpret_cast<T*>(instance->mem_start)[i]; | 150 return ReadMemory(&(reinterpret_cast<T*>(instance->mem_start)[i])); |
| 151 } | 151 } |
| 152 | 152 |
| 153 template <typename T> | 153 template <typename T> |
| 154 T raw_val_at(int i) { | 154 T raw_val_at(int i) { |
| 155 T val; | 155 return ReadMemory(reinterpret_cast<T*>(instance->mem_start + i)); |
| 156 memcpy(&val, reinterpret_cast<void*>(instance->mem_start + i), sizeof(T)); | 156 } |
| 157 return val; | 157 |
| 158 template <typename T> | |
| 159 void WriteMemory(T* p, T val) { | |
|
titzer
2016/06/06 07:44:00
Can we unify these routines with the others?
ivica.bogosavljevic
2016/06/13 14:59:02
We could use ReadLittleEndianValue and WriteLittle
| |
| 160 #if defined(V8_TARGET_LITTLE_ENDIAN) | |
| 161 memcpy(p, &val, sizeof(T)); | |
| 162 #elif defined(V8_TARGET_BIG_ENDIAN) | |
| 163 byte* src = reinterpret_cast<byte*>(&val); | |
| 164 byte* dst = reinterpret_cast<byte*>(p); | |
| 165 for (int i = 0; i < sizeof(T); i++) { | |
| 166 dst[i] = src[sizeof(T) - i - 1]; | |
| 167 } | |
| 168 #else | |
| 169 #error Unsupported endianness | |
| 170 #endif | |
|
ivica.bogosavljevic
2016/06/13 14:59:02
For myself: Commented code, needs to be removed
| |
| 171 // byte * tmp = reinterpret_cast<byte*>(p); | |
| 172 // T v = val; | |
| 173 // for (int i = 0; i < sizeof(T); i++) { | |
| 174 // tmp[i] = v & 0xff; | |
| 175 // v = v >> 8; | |
| 176 // } | |
| 177 } | |
| 178 | |
| 179 template <typename T> | |
| 180 T ReadMemory(T* p) { | |
| 181 T ret; | |
| 182 #if defined(V8_TARGET_LITTLE_ENDIAN) | |
| 183 memcpy(&ret, p, sizeof(T)); | |
| 184 #elif defined(V8_TARGET_BIG_ENDIAN) | |
| 185 byte* src = reinterpret_cast<byte*>(p); | |
| 186 byte* dst = reinterpret_cast<byte*>(&ret); | |
| 187 for (int i = 0; i < sizeof(T); i++) { | |
| 188 dst[i] = src[sizeof(T) - i - 1]; | |
| 189 } | |
| 190 #else | |
| 191 #error Unsupported endianness | |
| 192 #endif | |
| 193 return ret; | |
| 194 // T val = 0; | |
| 195 // byte * tmp = reinterpret_cast<byte*>(p); | |
| 196 // for (int i = sizeof(T) - 1; i >= 0; i--) { | |
| 197 // val = val << 8; | |
| 198 // val = val | tmp[i]; | |
| 199 // } | |
| 200 // return val; | |
| 158 } | 201 } |
| 159 | 202 |
| 160 // Zero-initialize the memory. | 203 // Zero-initialize the memory. |
| 161 void BlankMemory() { | 204 void BlankMemory() { |
| 162 byte* raw = raw_mem_start<byte>(); | 205 byte* raw = raw_mem_start<byte>(); |
| 163 memset(raw, 0, instance->mem_size); | 206 memset(raw, 0, instance->mem_size); |
| 164 } | 207 } |
| 165 | 208 |
| 166 // Pseudo-randomly intialize the memory. | 209 // Pseudo-randomly intialize the memory. |
| 167 void RandomizeMemory(unsigned int seed = 88) { | 210 void RandomizeMemory(unsigned int seed = 88) { |
| (...skipping 594 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 762 // interpreter. | 805 // interpreter. |
| 763 #define WASM_EXEC_TEST(name) \ | 806 #define WASM_EXEC_TEST(name) \ |
| 764 void RunWasm_##name(WasmExecutionMode execution_mode); \ | 807 void RunWasm_##name(WasmExecutionMode execution_mode); \ |
| 765 TEST(RunWasmCompiled_##name) { RunWasm_##name(kExecuteCompiled); } \ | 808 TEST(RunWasmCompiled_##name) { RunWasm_##name(kExecuteCompiled); } \ |
| 766 TEST(RunWasmInterpreted_##name) { RunWasm_##name(kExecuteInterpreted); } \ | 809 TEST(RunWasmInterpreted_##name) { RunWasm_##name(kExecuteInterpreted); } \ |
| 767 void RunWasm_##name(WasmExecutionMode execution_mode) | 810 void RunWasm_##name(WasmExecutionMode execution_mode) |
| 768 | 811 |
| 769 } // namespace | 812 } // namespace |
| 770 | 813 |
| 771 #endif | 814 #endif |
| OLD | NEW |