blob: 609da7acba41a49aee7834459cce0addd4273945 [file] [log] [blame]
Eric Holk29acb572016-04-22 09:34:41 -07001//===- subzero/runtime/wasm-runtime.cpp - Subzero WASM runtime source -----===//
Eric Holk67c7c412016-04-15 13:05:37 -07002//
3// The Subzero Code Generator
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the system calls required by the libc that is included
11// in WebAssembly programs.
12//
13//===----------------------------------------------------------------------===//
14
Eric Holk29acb572016-04-22 09:34:41 -070015#include <cmath>
16
17namespace env {
18double floor(double X) { return std::floor(X); }
19
20float floor(float X) { return std::floor(X); }
21}
22
23// TODO (eholk): move the C parts outside and use C++ name mangling.
24extern "C" {
Eric Holk67c7c412016-04-15 13:05:37 -070025#include <errno.h>
Eric Holk085bdae2016-04-18 15:08:19 -070026#include <fcntl.h>
Eric Holk29acb572016-04-22 09:34:41 -070027#include <math.h>
Eric Holk67c7c412016-04-15 13:05:37 -070028#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Eric Holk085bdae2016-04-18 15:08:19 -070031#include <sys/types.h>
32#include <sys/stat.h>
Eric Holk67c7c412016-04-15 13:05:37 -070033#include <unistd.h>
34
35extern char WASM_MEMORY[];
36
37void env$$abort() {
38 fprintf(stderr, "Aborting...\n");
39 abort();
40}
41
42void env$$_abort() { env$$abort(); }
43
Eric Holk29acb572016-04-22 09:34:41 -070044double env$$floor_f(float X) { return env::floor(X); }
45double env$$floor_d(double X) { return env::floor(X); }
46
John Portoa4d100a2016-04-18 15:32:27 -070047void env$$exit(int Status) { exit(Status); }
Eric Holk085bdae2016-04-18 15:08:19 -070048void env$$_exit(int Status) { env$$exit(Status); }
49
Eric Holk67c7c412016-04-15 13:05:37 -070050#define UNIMPLEMENTED(f) \
51 void env$$##f() { \
52 fprintf(stderr, "Unimplemented: " #f "\n"); \
53 abort(); \
54 }
55
56UNIMPLEMENTED(sbrk)
Eric Holk085bdae2016-04-18 15:08:19 -070057UNIMPLEMENTED(setjmp)
58UNIMPLEMENTED(longjmp)
59UNIMPLEMENTED(__assert_fail)
60UNIMPLEMENTED(__builtin_malloc)
Eric Holk29acb572016-04-22 09:34:41 -070061UNIMPLEMENTED(__builtin_isinff)
62UNIMPLEMENTED(__builtin_isinfl)
Eric Holk085bdae2016-04-18 15:08:19 -070063UNIMPLEMENTED(__builtin_apply)
64UNIMPLEMENTED(__builtin_apply_args)
Eric Holk67c7c412016-04-15 13:05:37 -070065UNIMPLEMENTED(pthread_cleanup_push)
66UNIMPLEMENTED(pthread_cleanup_pop)
67UNIMPLEMENTED(pthread_self)
Eric Holk085bdae2016-04-18 15:08:19 -070068UNIMPLEMENTED(__floatditf)
69UNIMPLEMENTED(__floatsitf)
70UNIMPLEMENTED(__fixtfdi)
71UNIMPLEMENTED(__fixtfsi)
72UNIMPLEMENTED(__fixsfti)
73UNIMPLEMENTED(__netf2)
74UNIMPLEMENTED(__getf2)
75UNIMPLEMENTED(__eqtf2)
76UNIMPLEMENTED(__lttf2)
77UNIMPLEMENTED(__addtf3)
78UNIMPLEMENTED(__subtf3)
79UNIMPLEMENTED(__divtf3)
80UNIMPLEMENTED(__multf3)
81UNIMPLEMENTED(__multi3)
Eric Holk67c7c412016-04-15 13:05:37 -070082UNIMPLEMENTED(__lock)
83UNIMPLEMENTED(__unlock)
Eric Holk29acb572016-04-22 09:34:41 -070084UNIMPLEMENTED(__syscall6) // sys_close
85UNIMPLEMENTED(__syscall140) // sys_llseek
86UNIMPLEMENTED(__syscall192) // sys_mmap?
87UNIMPLEMENTED(__unordtf2)
88UNIMPLEMENTED(__fixunstfsi)
89UNIMPLEMENTED(__floatunsitf)
90UNIMPLEMENTED(__extenddftf2)
Eric Holk67c7c412016-04-15 13:05:37 -070091
92void *wasmPtr(int Index) {
93 // TODO (eholk): get the mask from the WASM file.
Eric Holk29acb572016-04-22 09:34:41 -070094 const int MASK = 0xffffff;
Eric Holk67c7c412016-04-15 13:05:37 -070095 Index &= MASK;
96
97 return WASM_MEMORY + Index;
98}
99
Eric Holk085bdae2016-04-18 15:08:19 -0700100extern int __szwasm_main(int, const char **);
Eric Holk67c7c412016-04-15 13:05:37 -0700101
102#define WASM_REF(Type, Index) ((Type *)wasmPtr(Index))
103#define WASM_DEREF(Type, Index) (*WASM_REF(Type, Index))
104
Eric Holk085bdae2016-04-18 15:08:19 -0700105int main(int argc, const char **argv) { return __szwasm_main(argc, argv); }
Eric Holk67c7c412016-04-15 13:05:37 -0700106
Eric Holk29acb572016-04-22 09:34:41 -0700107int env$$abs(int a) { return abs(a); }
108
109double env$$pow(double x, double y) { return pow(x, y); }
110
Eric Holk67c7c412016-04-15 13:05:37 -0700111/// sys_write
112int env$$__syscall4(int Which, int VarArgs) {
113 int Fd = WASM_DEREF(int, VarArgs + 0 * sizeof(int));
114 int Buffer = WASM_DEREF(int, VarArgs + 1 * sizeof(int));
115 int Length = WASM_DEREF(int, VarArgs + 2 * sizeof(int));
116
117 return write(Fd, WASM_REF(char *, Buffer), Length);
118}
119
Eric Holk085bdae2016-04-18 15:08:19 -0700120/// sys_open
121int env$$__syscall5(int Which, int VarArgs) {
122 int WasmPath = WASM_DEREF(int, VarArgs);
123 int Flags = WASM_DEREF(int, VarArgs + 4);
124 int Mode = WASM_DEREF(int, VarArgs + 8);
125 const char *Path = WASM_REF(char, WasmPath);
126
127 fprintf(stderr, "sys_open(%s, %d, %d)\n", Path, Flags, Mode);
128
129 return open(Path, Flags, Mode);
130}
131
Eric Holk29acb572016-04-22 09:34:41 -0700132/// sys_getpid
133int env$$__syscall20(int Which, int VarArgs) {
134 (void)Which;
135 (void)VarArgs;
136
137 return getpid();
138}
139
Eric Holk67c7c412016-04-15 13:05:37 -0700140/// sys_ioctl
141int env$$__syscall54(int A, int B) {
142 int Fd = WASM_DEREF(int, B + 0 * sizeof(int));
143 int Op = WASM_DEREF(int, B + 1 * sizeof(int));
144 int ArgP = WASM_DEREF(int, B + 2 * sizeof(int));
145 // TODO (eholk): implement sys_ioctl
146 return -ENOTTY;
147}
148
Eric Holk29acb572016-04-22 09:34:41 -0700149/// sys_write
Eric Holk67c7c412016-04-15 13:05:37 -0700150int env$$__syscall146(int Which, int VarArgs) {
Eric Holk67c7c412016-04-15 13:05:37 -0700151
152 int Fd = WASM_DEREF(int, VarArgs);
153 int Iov = WASM_DEREF(int, VarArgs + sizeof(int));
154 int Iovcnt = WASM_DEREF(int, VarArgs + 2 * sizeof(int));
155
Eric Holk67c7c412016-04-15 13:05:37 -0700156 int Count = 0;
157
158 for (int I = 0; I < Iovcnt; ++I) {
159 void *Ptr = WASM_REF(void, WASM_DEREF(int, Iov + I * 8));
160 int Length = WASM_DEREF(int, Iov + I * 8 + 4);
161
Eric Holk67c7c412016-04-15 13:05:37 -0700162 int Curr = write(Fd, Ptr, Length);
163
Eric Holk67c7c412016-04-15 13:05:37 -0700164 if (Curr < 0) {
165 return -1;
166 }
167 Count += Curr;
168 }
Eric Holk67c7c412016-04-15 13:05:37 -0700169 return Count;
170}
Eric Holk29acb572016-04-22 09:34:41 -0700171} // end of extern "C"