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