forked from wasm3/wasm3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm3_host.c
268 lines (186 loc) · 6.28 KB
/
m3_host.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//
// m3_host.c
// m3
//
// Created by Steven Massey on 4/28/19.
// Copyright © 2019 Steven Massey. All rights reserved.
//
#include "m3_host.h"
#include "m3_core.h"
#include "m3_env.h"
#include "m3_module.h"
#include "m3_exception.h"
#include <stdio.h>
#include <assert.h>
#if !defined(WIN32) && !defined(PLATFORMIO) && !defined(WM_W600) && !defined(ANDROID) && !defined(PARTICLE)
#include <unistd.h>
#include <sys/ioctl.h>
#endif
void m3_printf (cstr_t i_format, const void * i_varArgs)
{
char format [256];
char output [256];
size_t formatLength = strlen (i_format) + 1;
char * buffer = formatLength <= sizeof(format) ? format : (char*)malloc (formatLength);
size_t numArgs = 0;
char * p = 0;
if (buffer)
{
memcpy (buffer, i_format, formatLength);
char * p = buffer + formatLength - 1;
// while (p >= buffer)
// {
// if (*p == '%')
// {
//
// }
// }
// cstr_t f = i_format;
// while (* f)
// {
// if (* f == '%')
// ++argCount;
// ++f;
// }
// printf (i_format, i_varArgs [0], i_varArgs [0]);
// printf ("printf!!!!\n");
printf (format);
if (buffer != format)
free (buffer);
}
}
void m3_abort (i32 i_dunno)
{
// FIX: return trap
abort ();
}
i32 m3_malloc (IM3Module i_module, i32 i_size)
{
i32 heapOffset = AllocateHeap (& i_module->memory, i_size);
printf ("malloc module: %s size: %d off: %d %p\n", i_module->name, i_size, heapOffset, i_module->memory.wasmPages + heapOffset);
return heapOffset;
}
void m3_free (IM3Module i_module, i32 i_data)
{
printf ("malloc free: %s\n", i_module->name);
}
void * m3_memset (void * i_ptr, i32 i_value, i32 i_size)
{
memset (i_ptr, i_value, i_size);
return i_ptr;
}
void * m3_memcpy (void * o_dst, void * i_src, i32 i_size)
{
return memcpy (o_dst, i_src, i_size);
}
i32 m3_fopen (IM3Module i_module, ccstr_t i_path, ccstr_t i_mode)
{
i32 offset = 0;
printf ("fopen: %s '%s'\n", i_path, i_mode);
FILE * file = fopen (i_path, i_mode);
if (file)
{
offset = AllocateHeap (& i_module->memory, sizeof (FILE *));
void ** ptr = (void **) (i_module->memory.wasmPages + offset);
* ptr = file;
}
return offset;
}
// HACK: just getting some code running. stderr pointer needs to be statically placed at init
i32 m3_getStderr (IM3Module i_module)
{
i32 std = AllocateHeap (& i_module->memory, sizeof (FILE *));
i32 offset = AllocateHeap (& i_module->memory, sizeof (i32));
i32 * ptr = (i32 *) (i_module->memory.wasmPages + offset);
* ptr = std;
void ** file = (void **) (i_module->memory.wasmPages + std);
*file = stderr;
return offset;
}
// TODO: system calls should be able to return traps. make return first arg.
i32 m3_fread (void * io_ptr, i32 i_size, i32 i_count, FILE * i_file)
{
FILE * file = (FILE *)(* (void **) i_file);
return (i32) fread (io_ptr, i_size, i_count, file);
}
i32 m3_fwrite (void * i_ptr, i32 i_size, i32 i_count, FILE * i_file)
{
FILE * file = (FILE *)(* (void **) i_file);
return (i32) fwrite (i_ptr, i_size, i_count, file);
}
i32 m3_write (i32 i_fd, const void * i_data, i32 i_count)
{
#if !defined(WIN32) && !defined(PLATFORMIO) && !defined(WM_W600) && !defined(ANDROID) && !defined(PARTICLE)
return (i32) write (i_fd, i_data, i_count);
#endif
return 0;
}
M3Result EmbedHost (IM3Runtime i_runtime)
{
M3Result result = c_m3Err_none;
return result;
}
double TestReturn (int32_t i_value)
{
return i_value / 10.;
}
void m3StdOut (const char * const i_string)
{
printf ("m3_out: %s", i_string);
}
void m3Out_f64 (double i_value)
{
printf ("%lf\n", i_value);
}
void m3Out_i32 (i32 i_value)
{
printf ("m3_out: %d %u\n", i_value, (u32) i_value);
}
void m3TestOut (int32_t i_int0, double i_double, int32_t i_int1)
{
printf ("0: %d, 1: %lf, 2: %d\n", i_int0, i_double, i_int1);
}
void m3Export (const void * i_data, i32 i_size)
{
f64 v = * (f64 *) i_data;
// printf ("%lf\n", v);
printf ("exporting: %p %d bytes\n", i_data, i_size);
FILE * f = fopen ("wasm.mandel.ppm", "wb");
const char * header = "P6\n1200 800\n255\n";
fwrite (header, 1, strlen (header), f);
fwrite (i_data, 1, i_size, f);
fclose (f);
}
m3ret_t m3_exit (i32 i_code)
{
printf ("exit (%d)\n", i_code);
return c_m3Err_trapExit;
}
static
M3Result SuppressLookupFailure (M3Result i_result)
{
if (i_result == c_m3Err_functionLookupFailed)
return c_m3Err_none;
else
return i_result;
}
M3Result m3_LinkCStd (IM3Module io_module)
{
M3Result result = c_m3Err_none;
_ (SuppressLookupFailure (m3_LinkFunction (io_module, "_printf", "v(**)", (void *) m3_printf)));
_ (SuppressLookupFailure (m3_LinkFunction (io_module, "_malloc", "i(Mi)", (void *) m3_malloc)));
_ (SuppressLookupFailure (m3_LinkFunction (io_module, "_free", "v(Mi)", (void *) m3_free)));
_ (SuppressLookupFailure (m3_LinkFunction (io_module, "_memset", "*(*ii)", (void *) m3_memset)));
_ (SuppressLookupFailure (m3_LinkFunction (io_module, "_memcpy", "*(**i)", (void *) m3_memcpy)));
_ (SuppressLookupFailure (m3_LinkFunction (io_module, "_fopen", "i(M**)", (void *) m3_fopen)));
_ (SuppressLookupFailure (m3_LinkFunction (io_module, "_fread", "i(*ii*)", (void *) m3_fread)));
_ (SuppressLookupFailure (m3_LinkFunction (io_module, "_fwrite", "i(*ii*)", (void *) m3_fwrite)));
_ (SuppressLookupFailure (m3_LinkFunction (io_module, "_write", "i(i*i)", (void *) m3_write)));
#if !defined(WIN32) && !defined(PLATFORMIO) && !defined(WM_W600) && !defined(ANDROID) && !defined(PARTICLE)
_ (SuppressLookupFailure (m3_LinkFunction (io_module, "_ioctl", "i(ii*)", (void *) ioctl)));
#endif
_ (SuppressLookupFailure (m3_LinkFunction (io_module, "_exit", "Tv(i)", (void *) m3_exit)));
_ (SuppressLookupFailure (m3_LinkFunction (io_module, "_perror", "v(*)", (void *) perror)));
_ (SuppressLookupFailure (m3_LinkFunction (io_module, "g$_stderr", "i(M)", (void *) m3_getStderr)));
_catch: return result;
}