aboutsummaryrefslogtreecommitdiff
path: root/src/interpret.c
blob: 580303e404f6c1e0d02cc345e8d7bc5941a8de6a (plain) (blame)
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include <math.h>
#include <stdio.h>

#include <posthaste/execute.h>
#include <posthaste/lower.h>
#include <posthaste/utils.h>
#include <posthaste/date.h>
#include <posthaste/vec.h>

static int64_t load(struct loc l, size_t sp, struct vec *stack,
                    struct vec *globals)
{
	/* extra runtime branches is another reason this bytecode instruction
	 * set is slow to interpret. To get rid of this if (), one
	 * option would be to add LOAD/STORE_GLOBAL instructions that move data
	 * between the global array and the stack, but this would make the JIT
	 * slower */
	if (l.l)
		return vect_at(int64_t, *stack, l.o + sp);

	return vect_at(int64_t, *globals, l.o);
}

static void store(struct loc l, int64_t v, size_t sp, struct vec *stack,
                  struct vec *globals)
{
	if (l.l) {
		vect_at(int64_t, *stack, l.o + sp) = v;
		return;
	}

	vect_at(int64_t, *globals, l.o) = v;
}

static int64_t interpret_func(struct fn *f, struct vec *args, size_t sp,
                              struct vec *stack, struct vec *globals)
{
	/* move args to formal locations */
	size_t formal = 0;
	foreach_vec(ai, *args) {
		int64_t a = vect_at(int64_t, *args, ai);
		vect_at(int64_t, *stack, sp + formal) = a;
		formal += 1;
	}

	vec_reset(args);

	/* load/store from location, selecting between global array and stack. Assumes
	 * parameter names, generally frowned upon. */
#define get(l) \
	load(l, sp, stack, globals)

#define put(l, v) \
	store(l, v, sp, stack, globals)

	/* use computed gotos to avoid extra overhead of loops/switch statements.
	 * Usually I'm not a huge fan of massive single functions and from a
	 * stylistic viewpoint maybe prefer breaking each instruction out into
	 * its own procedure, but this at least saves a bit of typing */
#define LINK(x) [x] = &&CASE_##x
	static void *labels[] = {
		LINK(LABEL),
		LINK(CALL),
		LINK(MOVE),
		LINK(ADD),
		LINK(SUB),
		LINK(MUL),
		LINK(DIV),
		LINK(ARG),
		LINK(RETVAL),
		LINK(PRINT_STRING),
		LINK(PRINT_INT),
		LINK(PRINT_BOOL),
		LINK(PRINT_DATE),
		LINK(PRINT_NEWLINE),
		LINK(PRINT_SPACE),
		LINK(DATE_ADD),
		LINK(DATE_SUB),
		LINK(DATE_DIFF),
		LINK(STORE_DAY),
		LINK(STORE_MONTH),
		LINK(STORE_YEAR),
		LINK(LOAD_DAY),
		LINK(LOAD_MONTH),
		LINK(LOAD_YEAR),
		LINK(LOAD_WEEKDAY),
		LINK(LOAD_WEEKNUM),
		LINK(TODAY),
		LINK(RET),
		LINK(STOP),
		LINK(CONST),
		LINK(EQ),
		LINK(LT),
		LINK(NEG),
		LINK(B),
		LINK(BZ),
		LINK(J),
	};
#undef LINK

#define JUMP() i = vect_at(struct insn, insns, pc); goto *labels[i.k];
#define DISPATCH() pc++; JUMP();

#define CASE(x) CASE_##x

	size_t pc = 0;
	int64_t retval = 0;
	struct vec insns = f->insns;
	struct insn i = {0};
	/* jump to first instruction */
	JUMP();

	CASE(STOP) : {return 0;}
	CASE(RET) : {return get(i.i0);}

	CASE(LABEL) : {DISPATCH();}
	CASE(RETVAL) : {put(i.o, retval); DISPATCH();}

	CASE(J) : {pc = i.v; JUMP();}

	CASE(B) : {
		int64_t i0 = get(i.i0);
		if (i0) {
			pc = i.v;
			JUMP();
		}

		DISPATCH();
	}

	CASE(BZ) : {
		int64_t i0 = get(i.i0);
		if (!i0) {
			pc = i.v;
			JUMP();
		}

		DISPATCH();
	}

	CASE(ARG) : {
		int64_t a = get(i.i0);
		vect_append(int64_t, *args, &a);
		DISPATCH();
	}

	CASE(CALL) : {
		struct fn *cf = find_fn(i.v);
		assert(cf);

		retval = interpret_func(cf, args, sp + f->max_sp + 1, stack,
		                        globals);
		DISPATCH();
	}

	CASE(MOVE) : {
		int64_t i0 = get(i.i0);
		put(i.o, i0);
		DISPATCH();
	}


	CASE(ADD) : {
		int64_t i0 = get(i.i0);
		int64_t i1 = get(i.i1);
		int64_t o = i0 + i1;
		put(i.o, o);
		DISPATCH();
	}

	CASE(SUB) : {
		int64_t i0 = get(i.i0);
		int64_t i1 = get(i.i1);
		int64_t o = i0 - i1;
		put(i.o, o);
		DISPATCH();
	}

	CASE(MUL) : {
		int64_t i0 = get(i.i0);
		int64_t i1 = get(i.i1);
		int64_t o = i0 * i1;
		put(i.o, o);
		DISPATCH();
	}

	CASE(DIV) : {
		int64_t i0 = get(i.i0);
		int64_t i1 = get(i.i1);
		int64_t o = i0 / i1;
		put(i.o, o);
		DISPATCH();
	}

	CASE(CONST) : {
		put(i.o, i.v);
		DISPATCH();
	}

	CASE(PRINT_DATE) : {
		int64_t i0 = get(i.i0);
		char str[11] = {0};
		date_to_string(str, i0);
		printf("%s", str);
		DISPATCH();
	}

	CASE(PRINT_INT) : {
		int64_t i0 = get(i.i0);
		printf("%lli", (long long)i0);
		DISPATCH();
	}

	CASE(PRINT_BOOL) : {
		int64_t i0 = get(i.i0);
		printf("%s", i0 ? "true" : "false");
		DISPATCH();
	}

	CASE(PRINT_STRING) : {
		int64_t i0 = get(i.i0);
		printf("%s", (const char *)i0);
		DISPATCH();
	}

	CASE(PRINT_NEWLINE) : {
		putchar('\n');
		DISPATCH();
	}

	CASE(PRINT_SPACE) : {
		putchar(' ');
		DISPATCH();
	}

	CASE(LOAD_DAY) : {
		int64_t i0 = get(i.i0);
		unsigned day = 0;
		date_split((ph_date_t)i0, NULL, NULL, &day);
		put(i.o, (int64_t)day);
		DISPATCH();
	}

	CASE(LOAD_MONTH) : {
		int64_t i0 = get(i.i0);
		unsigned month = 0;
		date_split((ph_date_t)i0, NULL, &month, NULL);
		put(i.o, (int64_t)month);
		DISPATCH();
	}

	CASE(LOAD_YEAR) : {
		int64_t i0 = get(i.i0);
		unsigned year = 0;
		date_split((ph_date_t)i0, &year, NULL, NULL);
		put(i.o, (int64_t)year);
		DISPATCH();
	}

	CASE(LOAD_WEEKDAY) : {
		int64_t i0 = get(i.i0);
		struct tm time = tm_from_date((ph_date_t)i0);
		put(i.o, (int64_t)time.tm_wday);
		DISPATCH();
	}

	CASE(LOAD_WEEKNUM) : {
		int64_t i0 = get(i.i0);
		struct tm time = tm_from_date((ph_date_t)i0);
		put(i.o, time.tm_yday / 7);
		DISPATCH();
	}

	CASE(STORE_DAY) : {
		int64_t i0 = get(i.i0);
		int64_t i1 = get(i.i1);

		unsigned year = 0;
		unsigned month = 0;
		date_split((ph_date_t)i0, &year, &month, NULL);
		ph_date_t date = date_from_numbers(year, month, i1);
		put(i.o, (int64_t)date);
		DISPATCH();
	}

	CASE(STORE_MONTH) : {
		int64_t i0 = get(i.i0);
		int64_t i1 = get(i.i1);

		unsigned year = 0;
		unsigned day = 0;
		date_split((ph_date_t)i0, &year, NULL, &day);
		ph_date_t date = date_from_numbers(year, i1, day);
		put(i.o, (int64_t)date);
		DISPATCH();
	}

	CASE(STORE_YEAR) : {
		int64_t i0 = get(i.i0);
		int64_t i1 = get(i.i1);

		unsigned month = 0;
		unsigned day = 0;
		date_split((ph_date_t)i0, NULL, &month, &day);
		ph_date_t date = date_from_numbers(i1, month, day);
		put(i.o, (int64_t)date);
		DISPATCH();
	}

	CASE(DATE_ADD) : {
		int64_t i0 = get(i.i0);
		int64_t i1 = get(i.i1);

		struct tm time = tm_from_date((ph_date_t)i0);
		time.tm_mday += i1;
		mktime(&time);

		ph_date_t date = date_from_tm(time);
		put(i.o, (int64_t)date);
		DISPATCH();
	}

	CASE(DATE_SUB) : {
		int64_t i0 = get(i.i0);
		int64_t i1 = get(i.i1);

		struct tm time = tm_from_date((ph_date_t)i0);
		time.tm_mday -= i1;
		mktime(&time);

		ph_date_t date = date_from_tm(time);
		put(i.o, (int64_t)date);
		DISPATCH();
	}

	CASE(DATE_DIFF) : {
		int64_t i0 = get(i.i0);
		int64_t i1 = get(i.i1);

		struct tm time0 = tm_from_date((ph_date_t)i0);
		struct tm time1 = tm_from_date((ph_date_t)i1);

		/* close enough at least */
		time_t t0 = mktime(&time0);
		time_t t1 = mktime(&time1);
		double seconds = difftime(t0, t1);
		int64_t days = round(seconds / 86400);
		put(i.o, days);
		DISPATCH();
	}

	CASE(TODAY) : {
		ph_date_t date = current_date();
		put(i.o, (int64_t)date);
		DISPATCH();
	}

	CASE(EQ) : {
		int64_t i0 = get(i.i0);
		int64_t i1 = get(i.i1);
		int64_t b = i0 == i1;
		put(i.o, b);
		DISPATCH();
	}

	CASE(LT) : {
		int64_t i0 = get(i.i0);
		int64_t i1 = get(i.i1);
		int64_t b = i0 < i1;
		put(i.o, b);
		DISPATCH();
	}

	CASE(NEG) : {
		int64_t i0 = get(i.i0);
		put(i.o, -i0);
		DISPATCH();
	}

#undef CASE
#undef JUMP
#undef DISPATCH
#undef get
#undef put
}

void interpret()
{
	struct fn *f = find_fn(0);
	assert(f);

	struct vec stack = vec_create(sizeof(int64_t));
	/* arbitrary amount of stack space, could potentially even be expanded
	 * if we run out but that's currently not implemented */
	vec_reserve(&stack, 65535);

	struct vec globals = vec_create(sizeof(int64_t));
	vec_reserve(&globals, num_globals());

	struct vec args = vec_create(sizeof(int64_t));

	interpret_func(f, &args, 0, &stack, &globals);

	vec_destroy(&args);
	vec_destroy(&stack);
	vec_destroy(&globals);
}