Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 //===- subzero/runtime/wasm-runtime.c - 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 <cmath> | |
| 16 | |
| 17 namespace env { | |
| 18 double floor(double X) { return std::floor(X); } | |
| 19 | |
| 20 float floor(float X) { return std::floor(X); } | |
| 21 } | |
| 22 | |
| 23 // TODO (eholk): move the C parts outside and use C++ name mangling. | |
| 24 extern "C" { | |
| 15 #include <errno.h> | 25 #include <errno.h> |
| 16 #include <fcntl.h> | 26 #include <fcntl.h> |
| 27 #include <math.h> | |
| 17 #include <stdio.h> | 28 #include <stdio.h> |
| 18 #include <stdlib.h> | 29 #include <stdlib.h> |
| 19 #include <string.h> | 30 #include <string.h> |
| 20 #include <sys/types.h> | 31 #include <sys/types.h> |
| 21 #include <sys/stat.h> | 32 #include <sys/stat.h> |
| 22 #include <unistd.h> | 33 #include <unistd.h> |
| 23 | 34 |
| 24 extern char WASM_MEMORY[]; | 35 extern char WASM_MEMORY[]; |
| 25 | 36 |
| 26 void env$$abort() { | 37 void env$$abort() { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 37 void env$$##f() { \ | 48 void env$$##f() { \ |
| 38 fprintf(stderr, "Unimplemented: " #f "\n"); \ | 49 fprintf(stderr, "Unimplemented: " #f "\n"); \ |
| 39 abort(); \ | 50 abort(); \ |
| 40 } | 51 } |
| 41 | 52 |
| 42 UNIMPLEMENTED(sbrk) | 53 UNIMPLEMENTED(sbrk) |
| 43 UNIMPLEMENTED(setjmp) | 54 UNIMPLEMENTED(setjmp) |
| 44 UNIMPLEMENTED(longjmp) | 55 UNIMPLEMENTED(longjmp) |
| 45 UNIMPLEMENTED(__assert_fail) | 56 UNIMPLEMENTED(__assert_fail) |
| 46 UNIMPLEMENTED(__builtin_malloc) | 57 UNIMPLEMENTED(__builtin_malloc) |
| 58 UNIMPLEMENTED(__builtin_isinff) | |
| 59 UNIMPLEMENTED(__builtin_isinfl) | |
| 47 UNIMPLEMENTED(__builtin_apply) | 60 UNIMPLEMENTED(__builtin_apply) |
| 48 UNIMPLEMENTED(__builtin_apply_args) | 61 UNIMPLEMENTED(__builtin_apply_args) |
| 49 UNIMPLEMENTED(pthread_cleanup_push) | 62 UNIMPLEMENTED(pthread_cleanup_push) |
| 50 UNIMPLEMENTED(pthread_cleanup_pop) | 63 UNIMPLEMENTED(pthread_cleanup_pop) |
| 51 UNIMPLEMENTED(pthread_self) | 64 UNIMPLEMENTED(pthread_self) |
| 52 UNIMPLEMENTED(abs) | |
| 53 UNIMPLEMENTED(__floatditf) | 65 UNIMPLEMENTED(__floatditf) |
| 54 UNIMPLEMENTED(__floatsitf) | 66 UNIMPLEMENTED(__floatsitf) |
| 55 UNIMPLEMENTED(__fixtfdi) | 67 UNIMPLEMENTED(__fixtfdi) |
| 56 UNIMPLEMENTED(__fixtfsi) | 68 UNIMPLEMENTED(__fixtfsi) |
| 57 UNIMPLEMENTED(__fixsfti) | 69 UNIMPLEMENTED(__fixsfti) |
| 58 UNIMPLEMENTED(__netf2) | 70 UNIMPLEMENTED(__netf2) |
| 59 UNIMPLEMENTED(__getf2) | 71 UNIMPLEMENTED(__getf2) |
| 60 UNIMPLEMENTED(__eqtf2) | 72 UNIMPLEMENTED(__eqtf2) |
| 61 UNIMPLEMENTED(__lttf2) | 73 UNIMPLEMENTED(__lttf2) |
| 62 UNIMPLEMENTED(__addtf3) | 74 UNIMPLEMENTED(__addtf3) |
| 63 UNIMPLEMENTED(__subtf3) | 75 UNIMPLEMENTED(__subtf3) |
| 64 UNIMPLEMENTED(__divtf3) | 76 UNIMPLEMENTED(__divtf3) |
| 65 UNIMPLEMENTED(__multf3) | 77 UNIMPLEMENTED(__multf3) |
| 66 UNIMPLEMENTED(__multi3) | 78 UNIMPLEMENTED(__multi3) |
| 67 UNIMPLEMENTED(__lock) | 79 UNIMPLEMENTED(__lock) |
| 68 UNIMPLEMENTED(__unlock) | 80 UNIMPLEMENTED(__unlock) |
| 69 UNIMPLEMENTED(__syscall6) | 81 UNIMPLEMENTED(__syscall6) // sys_close |
| 70 UNIMPLEMENTED(__syscall20) | 82 UNIMPLEMENTED(__syscall140) // sys_llseek |
| 71 UNIMPLEMENTED(__syscall140) | 83 UNIMPLEMENTED(__syscall192) // sys_mmap? |
| 72 UNIMPLEMENTED(__syscall192) | 84 UNIMPLEMENTED(__unordtf2) |
| 85 UNIMPLEMENTED(__fixunstfsi) | |
| 86 UNIMPLEMENTED(__floatunsitf) | |
| 87 UNIMPLEMENTED(__extenddftf2) | |
| 73 | 88 |
| 74 void *wasmPtr(int Index) { | 89 void *wasmPtr(int Index) { |
| 75 // TODO (eholk): get the mask from the WASM file. | 90 // TODO (eholk): get the mask from the WASM file. |
| 76 const int MASK = 0x3fffff; | 91 const int MASK = 0xffffff; |
| 77 Index &= MASK; | 92 Index &= MASK; |
| 78 | 93 |
| 79 return WASM_MEMORY + Index; | 94 return WASM_MEMORY + Index; |
| 80 } | 95 } |
| 81 | 96 |
| 82 extern int __szwasm_main(int, const char **); | 97 extern int __szwasm_main(int, const char **); |
| 83 | 98 |
| 84 #define WASM_REF(Type, Index) ((Type *)wasmPtr(Index)) | 99 #define WASM_REF(Type, Index) ((Type *)wasmPtr(Index)) |
| 85 #define WASM_DEREF(Type, Index) (*WASM_REF(Type, Index)) | 100 #define WASM_DEREF(Type, Index) (*WASM_REF(Type, Index)) |
| 86 | 101 |
| 87 int main(int argc, const char **argv) { return __szwasm_main(argc, argv); } | 102 int main(int argc, const char **argv) { return __szwasm_main(argc, argv); } |
| 88 | 103 |
| 104 int env$$abs(int a) { return abs(a); } | |
| 105 | |
| 106 double env$$pow(double x, double y) { return pow(x, y); } | |
| 107 | |
| 89 /// sys_write | 108 /// sys_write |
| 90 int env$$__syscall4(int Which, int VarArgs) { | 109 int env$$__syscall4(int Which, int VarArgs) { |
| 91 int Fd = WASM_DEREF(int, VarArgs + 0 * sizeof(int)); | 110 int Fd = WASM_DEREF(int, VarArgs + 0 * sizeof(int)); |
| 92 int Buffer = WASM_DEREF(int, VarArgs + 1 * sizeof(int)); | 111 int Buffer = WASM_DEREF(int, VarArgs + 1 * sizeof(int)); |
| 93 int Length = WASM_DEREF(int, VarArgs + 2 * sizeof(int)); | 112 int Length = WASM_DEREF(int, VarArgs + 2 * sizeof(int)); |
| 94 | 113 |
| 95 return write(Fd, WASM_REF(char *, Buffer), Length); | 114 return write(Fd, WASM_REF(char *, Buffer), Length); |
| 96 } | 115 } |
| 97 | 116 |
| 98 /// sys_open | 117 /// sys_open |
| 99 int env$$__syscall5(int Which, int VarArgs) { | 118 int env$$__syscall5(int Which, int VarArgs) { |
| 100 int WasmPath = WASM_DEREF(int, VarArgs); | 119 int WasmPath = WASM_DEREF(int, VarArgs); |
| 101 int Flags = WASM_DEREF(int, VarArgs + 4); | 120 int Flags = WASM_DEREF(int, VarArgs + 4); |
| 102 int Mode = WASM_DEREF(int, VarArgs + 8); | 121 int Mode = WASM_DEREF(int, VarArgs + 8); |
| 103 const char *Path = WASM_REF(char, WasmPath); | 122 const char *Path = WASM_REF(char, WasmPath); |
| 104 | 123 |
| 105 fprintf(stderr, "sys_open(%s, %d, %d)\n", Path, Flags, Mode); | 124 fprintf(stderr, "sys_open(%s, %d, %d)\n", Path, Flags, Mode); |
| 106 | 125 |
| 107 return open(Path, Flags, Mode); | 126 return open(Path, Flags, Mode); |
| 108 } | 127 } |
| 109 | 128 |
| 129 /// sys_getpid | |
| 130 int env$$__syscall20(int Which, int VarArgs) { | |
| 131 (void)Which; | |
| 132 (void)VarArgs; | |
| 133 | |
| 134 return getpid(); | |
| 135 } | |
| 136 | |
| 110 /// sys_ioctl | 137 /// sys_ioctl |
| 111 int env$$__syscall54(int A, int B) { | 138 int env$$__syscall54(int A, int B) { |
| 112 int Fd = WASM_DEREF(int, B + 0 * sizeof(int)); | 139 int Fd = WASM_DEREF(int, B + 0 * sizeof(int)); |
| 113 int Op = WASM_DEREF(int, B + 1 * sizeof(int)); | 140 int Op = WASM_DEREF(int, B + 1 * sizeof(int)); |
| 114 int ArgP = WASM_DEREF(int, B + 2 * sizeof(int)); | 141 int ArgP = WASM_DEREF(int, B + 2 * sizeof(int)); |
| 115 // TODO (eholk): implement sys_ioctl | 142 // TODO (eholk): implement sys_ioctl |
| 116 return -ENOTTY; | 143 return -ENOTTY; |
| 117 } | 144 } |
| 118 | 145 |
| 146 /// sys_write | |
| 119 int env$$__syscall146(int Which, int VarArgs) { | 147 int env$$__syscall146(int Which, int VarArgs) { |
| 120 fprintf(stderr, "syscall146\n"); | |
| 121 | 148 |
| 122 int Fd = WASM_DEREF(int, VarArgs); | 149 int Fd = WASM_DEREF(int, VarArgs); |
| 123 int Iov = WASM_DEREF(int, VarArgs + sizeof(int)); | 150 int Iov = WASM_DEREF(int, VarArgs + sizeof(int)); |
| 124 int Iovcnt = WASM_DEREF(int, VarArgs + 2 * sizeof(int)); | 151 int Iovcnt = WASM_DEREF(int, VarArgs + 2 * sizeof(int)); |
| 125 | 152 |
| 126 fprintf(stderr, " Fd=%d, Iov=%d (%p), Iovcnt=%d\n", Fd, Iov, wasmPtr(Iov), | |
| 127 Iovcnt); | |
| 128 | |
| 129 int Count = 0; | 153 int Count = 0; |
| 130 | 154 |
| 131 for (int I = 0; I < Iovcnt; ++I) { | 155 for (int I = 0; I < Iovcnt; ++I) { |
| 132 void *Ptr = WASM_REF(void, WASM_DEREF(int, Iov + I * 8)); | 156 void *Ptr = WASM_REF(void, WASM_DEREF(int, Iov + I * 8)); |
| 133 int Length = WASM_DEREF(int, Iov + I * 8 + 4); | 157 int Length = WASM_DEREF(int, Iov + I * 8 + 4); |
| 134 | 158 |
| 135 fprintf(stderr, " [%d] write(%d, %p, %d) = ", I, Fd, Ptr, Length); | |
| 136 int Curr = write(Fd, Ptr, Length); | 159 int Curr = write(Fd, Ptr, Length); |
| 137 | 160 |
| 138 fprintf(stderr, "%d\n", Curr); | |
| 139 | |
| 140 if (Curr < 0) { | 161 if (Curr < 0) { |
| 141 return -1; | 162 return -1; |
| 142 } | 163 } |
| 143 Count += Curr; | 164 Count += Curr; |
| 144 } | 165 } |
| 145 fprintf(stderr, " Count = %d\n", Count); | |
| 146 return Count; | 166 return Count; |
| 147 } | 167 } |
| 168 } | |
|
John
2016/04/20 20:04:59
maybe
} // end of extern "C"
Check with Jim.
Eric Holk
2016/04/20 20:25:35
Done.
| |
| OLD | NEW |