Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 //===- subzero/runtime/wasm-runtime.cpp - Subzero WASM runtime source -----===// | 1 //===- subzero/runtime/wasm-runtime.cpp - Subzero WASM runtime source -----===// |
| 2 // | 2 // |
| 3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 // | 9 // |
| 10 // This file implements the system calls required by the libc that is included | 10 // This file implements the system calls required by the libc that is included |
| 11 // in WebAssembly programs. | 11 // in WebAssembly programs. |
| 12 // | 12 // |
| 13 //===----------------------------------------------------------------------===// | 13 //===----------------------------------------------------------------------===// |
| 14 | 14 |
| 15 #include <algorithm> | |
| 15 #include <cassert> | 16 #include <cassert> |
| 16 #include <cmath> | 17 #include <cmath> |
| 18 #include <iostream> | |
| 19 #include <vector> | |
| 20 | |
| 17 #include <errno.h> | 21 #include <errno.h> |
| 18 #include <fcntl.h> | 22 #include <fcntl.h> |
| 19 #include <iostream> | |
| 20 #include <math.h> | 23 #include <math.h> |
| 21 #include <stdint.h> | 24 #include <stdint.h> |
| 22 #include <stdio.h> | 25 #include <stdio.h> |
| 23 #include <stdlib.h> | 26 #include <stdlib.h> |
| 24 #include <string.h> | 27 #include <string.h> |
| 25 #include <sys/ioctl.h> | 28 #include <sys/ioctl.h> |
| 26 #include <sys/types.h> | 29 #include <sys/types.h> |
| 27 #include <sys/stat.h> | 30 #include <sys/stat.h> |
| 28 #include <termios.h> | 31 #include <termios.h> |
| 29 #include <time.h> | 32 #include <time.h> |
| 30 #include <unistd.h> | 33 #include <unistd.h> |
| 31 | 34 |
| 32 #ifdef WASM_TRACE_RUNTIME | 35 #ifdef WASM_TRACE_RUNTIME |
| 33 #define TRACE_ENTRY() \ | 36 #define TRACE_ENTRY() \ |
| 34 { std::cerr << __func__ << "(...) = "; } | 37 { std::cerr << __func__ << "(...) = "; } |
| 35 template <typename T> T trace(T x) { | 38 template <typename T> T trace(T x) { |
| 36 std::cerr << x << std::endl; | 39 std::cerr << x << std::endl; |
| 37 return x; | 40 return x; |
| 38 } | 41 } |
| 39 void trace() { std::cerr << "(void)" << std::endl; } | 42 void trace() { std::cerr << "(void)" << std::endl; } |
| 40 #else | 43 #else |
| 41 #define TRACE_ENTRY() | 44 #define TRACE_ENTRY() |
| 42 template <typename T> T trace(T x) { return x; } | 45 template <typename T> T trace(T x) { return x; } |
| 43 void trace() {} | 46 void trace() {} |
| 44 #endif // WASM_TRACE_RUNTIME | 47 #endif // WASM_TRACE_RUNTIME |
| 45 | 48 |
| 46 extern "C" { | 49 extern "C" { |
| 47 extern char WASM_MEMORY[]; | 50 char *WASM_MEMORY; |
| 48 extern uint32_t WASM_DATA_SIZE; | 51 extern uint32_t WASM_DATA_SIZE; |
| 49 extern uint32_t WASM_NUM_PAGES; | 52 extern uint32_t WASM_NUM_PAGES; |
| 50 } // end of extern "C" | 53 } // end of extern "C" |
| 51 | 54 |
| 52 namespace { | 55 namespace { |
| 53 uint32_t HeapBreak; | 56 uint32_t HeapBreak; |
| 54 | 57 |
| 55 // TODO (eholk): make all of these constexpr. | 58 // TODO (eholk): make all of these constexpr. |
| 56 const uint32_t PageSizeLog2 = 16; | 59 const uint32_t PageSizeLog2 = 16; |
| 57 const uint32_t PageSize = 1 << PageSizeLog2; // 64KB | 60 const uint32_t PageSize = 1 << PageSizeLog2; // 64KB |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 void __Sz_bounds_fail() { | 133 void __Sz_bounds_fail() { |
| 131 std::cerr << "Bounds check failure" << std::endl; | 134 std::cerr << "Bounds check failure" << std::endl; |
| 132 abort(); | 135 abort(); |
| 133 } | 136 } |
| 134 | 137 |
| 135 void __Sz_indirect_fail() { | 138 void __Sz_indirect_fail() { |
| 136 std::cerr << "Invalid indirect call target" << std::endl; | 139 std::cerr << "Invalid indirect call target" << std::endl; |
| 137 abort(); | 140 abort(); |
| 138 } | 141 } |
| 139 | 142 |
| 143 extern char WASM_DATA_INIT[]; | |
| 144 | |
| 140 void env$$abort() { | 145 void env$$abort() { |
| 141 fprintf(stderr, "Aborting...\n"); | 146 fprintf(stderr, "Aborting...\n"); |
| 142 abort(); | 147 abort(); |
| 143 } | 148 } |
| 144 | 149 |
| 145 void env$$_abort() { env$$abort(); } | 150 void env$$_abort() { env$$abort(); } |
| 146 | 151 |
| 147 double env$$floor_f(float X) { | 152 double env$$floor_f(float X) { |
| 148 TRACE_ENTRY(); | 153 TRACE_ENTRY(); |
| 149 return env::floor(X); | 154 return env::floor(X); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 210 UNIMPLEMENTED(pthread_cleanup_push) | 215 UNIMPLEMENTED(pthread_cleanup_push) |
| 211 UNIMPLEMENTED(pthread_self) | 216 UNIMPLEMENTED(pthread_self) |
| 212 UNIMPLEMENTED(setjmp) | 217 UNIMPLEMENTED(setjmp) |
| 213 | 218 |
| 214 extern int __szwasm_main(int, WasmPtr<WasmCharPtr>); | 219 extern int __szwasm_main(int, WasmPtr<WasmCharPtr>); |
| 215 | 220 |
| 216 #define WASM_REF(Type, Index) (WasmPtr<Type>(Index).asPtr()) | 221 #define WASM_REF(Type, Index) (WasmPtr<Type>(Index).asPtr()) |
| 217 #define WASM_DEREF(Type, Index) (*WASM_REF(Type, Index)) | 222 #define WASM_DEREF(Type, Index) (*WASM_REF(Type, Index)) |
| 218 | 223 |
| 219 int main(int argc, const char **argv) { | 224 int main(int argc, const char **argv) { |
| 225 // Create the heap. | |
| 226 std::vector<char> WasmHeap(WASM_NUM_PAGES << PageSizeLog2); | |
| 227 WASM_MEMORY = &WasmHeap[0]; | |
|
John
2016/04/30 04:11:55
WasmHeap->data()?
Eric Holk
2016/05/02 17:42:37
Done.
| |
| 228 std::copy(WASM_DATA_INIT, WASM_DATA_INIT + WASM_DATA_SIZE, WasmHeap.begin()); | |
| 229 | |
| 220 // TODO (eholk): align these allocations correctly. | 230 // TODO (eholk): align these allocations correctly. |
| 221 | 231 |
| 222 // Allocate space for the global data. | 232 // Allocate space for the global data. |
| 223 HeapBreak = WASM_DATA_SIZE; | 233 HeapBreak = WASM_DATA_SIZE; |
| 224 GlobalData = WASM_REF(WasmData, HeapBreak); | 234 GlobalData = WASM_REF(WasmData, HeapBreak); |
| 225 HeapBreak += sizeof(WasmData); | 235 HeapBreak += sizeof(WasmData); |
| 226 | 236 |
| 227 // copy the command line arguments. | 237 // copy the command line arguments. |
| 228 WasmPtr<WasmCharPtr> WasmArgV = HeapBreak; | 238 WasmPtr<WasmCharPtr> WasmArgV = HeapBreak; |
| 229 WasmPtr<char> *WasmArgVPtr = WasmArgV.asPtr(); | 239 WasmPtr<char> *WasmArgVPtr = WasmArgV.asPtr(); |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 418 int env$$__syscall192(int Which, WasmArray<int> VarArgs) { | 428 int env$$__syscall192(int Which, WasmArray<int> VarArgs) { |
| 419 TRACE_ENTRY(); | 429 TRACE_ENTRY(); |
| 420 (void)Which; | 430 (void)Which; |
| 421 (void)VarArgs; | 431 (void)VarArgs; |
| 422 | 432 |
| 423 // TODO (eholk): figure out how to implement this. | 433 // TODO (eholk): figure out how to implement this. |
| 424 | 434 |
| 425 return trace(-ENOMEM); | 435 return trace(-ENOMEM); |
| 426 } | 436 } |
| 427 } // end of extern "C" | 437 } // end of extern "C" |
| OLD | NEW |