-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpz_generic_builtin.cpp
More file actions
356 lines (281 loc) · 9.5 KB
/
pz_generic_builtin.cpp
File metadata and controls
356 lines (281 loc) · 9.5 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Plasma bytecode exection (generic portable version)
* vim: ts=4 sw=4 et
*
* Copyright (C) Plasma Team
* Distributed under the terms of the MIT license, see ../LICENSE.code
*/
#include "pz_common.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "pz_interp.h"
#include "pz_gc.h"
#include "pz_generic_run.h"
#include "pz_string.h"
namespace pz {
template<typename T>
static
uintptr_t Box(T v, GCCapability &gc) {
T *ptr = reinterpret_cast<T*>(gc.alloc_bytes(sizeof(T)));
*ptr = v;
return reinterpret_cast<uintptr_t>(ptr);
}
/*
* Imported procedures
*
**********************/
unsigned pz_builtin_print_func(void * void_stack, unsigned sp)
{
StackValue * stack = static_cast<StackValue *>(void_stack);
String::from_ptr(stack[sp--].ptr).print();
return sp;
}
unsigned pz_builtin_readline_func(void * void_stack, unsigned sp,
AbstractGCTracer & gc_trace)
{
const uint32_t READLINE_BUFFER_SIZE = 128;
StackValue * stack = static_cast<StackValue *>(void_stack);
NoGCScope nogc(gc_trace);
bool got_eof = false;
String str("");
do {
FlatString *fs = FlatString::New(nogc, READLINE_BUFFER_SIZE);
char *res = fgets(fs->buffer(), READLINE_BUFFER_SIZE, stdin);
if (!res) {
if (ferror(stdin)) {
perror("stdin");
exit(PZ_EXIT_RUNTIME_ERROR);
} else if (feof(stdin)) {
got_eof = true;
break;
}
// unreachable
assert(false);
}
int read_len = strlen(fs->buffer());
if (read_len == 0) {
// We don't need to process an empty string.
break;
}
fs->fixSize(strlen(fs->buffer()));
if (fs->length() > 0 && fs->buffer()[fs->length()-1] == '\n') {
// Remove the newline character
// TODO: If string had a way to set chars then we can simplify
// this by doing the operation on string and having a single
// call to append.
fs->buffer()[fs->length()-1] = 0;
fs->fixSize(fs->length()-1);
str = String::append(nogc, str, String(fs));
break;
}
str = String::append(nogc, str, String(fs));
if (fs->length() != (READLINE_BUFFER_SIZE - 1)) {
break;
}
} while(true);
nogc.abort_if_oom("reading stdin");
sp++;
if (got_eof && str.isEmpty()) {
stack[sp].uptr = 0;
return sp;
}
// Tag the pointer for the Ok constructor.
stack[sp].uptr = Box(str.ptr(), nogc) | 1;
return sp;
}
/*
* Long enough for a 32 bit value, plus a sign, plus a null termination
* byte.
*/
#define INT_TO_STRING_BUFFER_SIZE 11
unsigned pz_builtin_int_to_string_func(void * void_stack, unsigned sp,
AbstractGCTracer & gc_trace)
{
StackValue * stack = static_cast<StackValue *>(void_stack);
int32_t num = stack[sp].s32;
FlatString * string = FlatString::New(gc_trace, INT_TO_STRING_BUFFER_SIZE);
int result = snprintf(string->buffer(),
INT_TO_STRING_BUFFER_SIZE, "%d", (int)num);
if ((result < 0) || (result > (INT_TO_STRING_BUFFER_SIZE - 1))) {
stack[sp].ptr = NULL;
} else {
string->fixSize(result);
stack[sp].ptr = string;
}
return sp;
}
unsigned pz_builtin_setenv_func(void * void_stack, unsigned sp)
{
StackValue * stack = static_cast<StackValue *>(void_stack);
const String value = String::from_ptr(stack[sp--].ptr);
const String name = String::from_ptr(stack[sp--].ptr);
int result = setenv(name.c_str(), value.c_str(), 1);
stack[++sp].u32 = !result;
return sp;
}
unsigned pz_builtin_gettimeofday_func(void * void_stack, unsigned sp)
{
StackValue * stack = static_cast<StackValue *>(void_stack);
struct timeval tv;
int res = gettimeofday(&tv, NULL);
stack[++sp].u32 = res == 0 ? 1 : 0;
// This is aweful, but Plasma itself doesn't handle other inttypes yet.
stack[++sp].u32 = (uint32_t)tv.tv_sec;
stack[++sp].u32 = (uint32_t)tv.tv_usec;
return sp;
}
unsigned pz_builtin_string_concat_func(void * void_stack, unsigned sp,
AbstractGCTracer & gc_trace)
{
StackValue * stack = static_cast<StackValue *>(void_stack);
const String s2 = String::from_ptr(stack[sp--].ptr);
const String s1 = String::from_ptr(stack[sp].ptr);
String s = String::append(gc_trace, s1, s2);
stack[sp].ptr = s.ptr();
return sp;
}
unsigned pz_builtin_die_func(void * void_stack, unsigned sp)
{
StackValue * stack = static_cast<StackValue *>(void_stack);
const String s = String::from_ptr(stack[sp].ptr);
fprintf(stderr, "Die: %s\n", s.c_str());
exit(1);
}
unsigned pz_builtin_set_parameter_func(void * void_stack, unsigned sp, PZ & pz)
{
StackValue * stack = static_cast<StackValue *>(void_stack);
// int32_t value = stack[sp].s32;
const String name = String::from_ptr(stack[sp - 1].ptr);
/*
* There are no parameters defined.
*/
fprintf(stderr, "No such parameter '%s'\n", name.c_str());
int32_t result = 0;
sp--;
stack[sp].sptr = result;
return sp;
}
unsigned pz_builtin_get_parameter_func(void * void_stack, unsigned sp, PZ & pz)
{
StackValue * stack = static_cast<StackValue *>(void_stack);
const String name = String::from_ptr(stack[sp].ptr);
int32_t result;
int32_t value;
if (name.equals(String("heap_usage"))) {
value = heap_get_usage(&pz.heap());
result = 1;
} else if (name.equals(String("heap_collections"))) {
value = heap_get_collections(&pz.heap());
result = 1;
} else {
fprintf(stderr, "No such parameter '%s'.\n", name.c_str());
result = 0;
value = 0;
}
stack[sp].sptr = result;
stack[sp + 1].sptr = value;
sp++;
return sp;
}
unsigned pz_builtin_codepoint_category(void * void_stack, unsigned sp)
{
// TODO use a unicode library. While POSIX is locale-aware it does not
// handle characters outside the current locale, but applications may
// use more than a single langauge at a time.
StackValue * stack = static_cast<StackValue *>(void_stack);
CodePoint32 c = stack[sp].u32;
// TODO: Use a proper FFI so we don't need to guess type tags.
stack[sp].uptr = isspace(c) ? 0 : 1;
return sp;
}
unsigned pz_builtin_codepoint_to_string(void * void_stack, unsigned sp,
AbstractGCTracer & gc)
{
StackValue * stack = static_cast<StackValue *>(void_stack);
CodePoint32 c = stack[sp].u32;
FlatString *fs = FlatString::New(gc, 1);
if (c > CHAR_MAX) {
fprintf(stderr, "Unicode not supported yet\n");
abort();
}
fs->buffer()[0] = c;
stack[sp].ptr = String(fs).ptr();
return sp;
}
unsigned pz_builtin_strpos_forward(void * void_stack, unsigned sp,
AbstractGCTracer &gc)
{
StackValue * stack = static_cast<StackValue *>(void_stack);
const StringPos* pos = reinterpret_cast<const StringPos*>(stack[sp].ptr);
stack[sp].ptr = pos->forward(gc);
return sp;
}
unsigned pz_builtin_strpos_backward(void * void_stack, unsigned sp,
AbstractGCTracer &gc)
{
StackValue * stack = static_cast<StackValue *>(void_stack);
const StringPos* pos = reinterpret_cast<const StringPos*>(stack[sp].ptr);
stack[sp].ptr = pos->backward(gc);
return sp;
}
unsigned pz_builtin_strpos_next_char(void * void_stack, unsigned sp,
AbstractGCTracer & gc)
{
StackValue * stack = static_cast<StackValue *>(void_stack);
const StringPos* pos = reinterpret_cast<const StringPos*>(stack[sp].ptr);
// XXX add pointer tagging macros.
if (pos->at_end()) {
stack[sp].uptr = 0;
} else {
stack[sp].uptr = Box(pos->next_char(), gc) | 1;
}
return sp;
}
unsigned pz_builtin_strpos_prev_char(void * void_stack, unsigned sp,
AbstractGCTracer & gc)
{
StackValue * stack = static_cast<StackValue *>(void_stack);
const StringPos* pos = reinterpret_cast<const StringPos*>(stack[sp].ptr);
if (pos->at_beginning()) {
stack[sp].uptr = 0;
} else {
stack[sp].uptr = Box(pos->prev_char(), gc) | 1;
}
return sp;
}
unsigned pz_builtin_string_begin(void * void_stack, unsigned sp,
AbstractGCTracer & gc)
{
StackValue * stack = static_cast<StackValue *>(void_stack);
const String string = String::from_ptr(stack[sp].ptr);
stack[sp].ptr = string.begin(gc);
return sp;
}
unsigned pz_builtin_string_end(void * void_stack, unsigned sp,
AbstractGCTracer & gc)
{
StackValue * stack = static_cast<StackValue *>(void_stack);
const String string = String::from_ptr(stack[sp].ptr);
stack[sp].ptr = string.end(gc);
return sp;
}
unsigned pz_builtin_string_substring(void * void_stack, unsigned sp,
AbstractGCTracer & gc)
{
StackValue * stack = static_cast<StackValue *>(void_stack);
const StringPos* pos2 = reinterpret_cast<const StringPos*>(stack[sp--].ptr);
const StringPos* pos1 = reinterpret_cast<const StringPos*>(stack[sp].ptr);
const String str = String::substring(gc, pos1, pos2);
stack[sp].ptr = str.ptr();
return sp;
}
unsigned pz_builtin_string_equals(void * void_stack, unsigned sp) {
StackValue * stack = static_cast<StackValue *>(void_stack);
const String str1 = String::from_ptr(stack[sp--].ptr);
const String str2 = String::from_ptr(stack[sp].ptr);
stack[sp].uptr = str1.equals(str2);
return sp;
}
} // namespace pz