summaryrefslogtreecommitdiff
path: root/interpret.c
blob: 371d372ebe7613e9fb05f61211c29a66543201e3 (plain)
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/* Oort
 * Copyright 2007, Soren Sandmann Pedersen
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include <stdlib.h>
#include "ast.h"

static void
read_value (gpointer address, ast_type_spec_t *type, value_t *result)
{
    switch (type->common.type)
    {
    case AST_INT32_TYPE:
	result->int32_val = *(gint32 *)address;
	break;

    case AST_DOUBLE_TYPE:
	result->double_val = *(double *)address;
	break;

    case AST_BOOL_TYPE:
	result->bool_val = *(gboolean *)address;
	break;

    case AST_FUNCTION_TYPE:
	result->closure_val = *(closure_t *)address;
	break;

    case AST_LABEL_TYPE:
	result->label_val = *(label_t *)address;
	break;

    case AST_ARRAY_TYPE:
    case AST_NULL_TYPE:
    case AST_STRING_TYPE:
    case AST_OBJECT_TYPE:
	result->pointer_val = *(gpointer *)address;
	break;

    case AST_IDENTIFIER_TYPE:
    case AST_INFERRED_TYPE:
    case AST_VOID_TYPE:
	g_assert_not_reached();
	break;
    }
}

static void
write_value (gpointer address, ast_type_spec_t *type, value_t *value)
{
    switch (type->common.type)
    {
    case AST_INT32_TYPE:
#if 0
	g_print (".. storing iiinteger %d\n", value->int32_val);
#endif
	*(gint32 *)address = value->int32_val;
	break;

    case AST_DOUBLE_TYPE:
	*(double *)address = value->double_val;
	break;

    case AST_BOOL_TYPE:
	*(gboolean *)address = value->bool_val;
	break;

    case AST_FUNCTION_TYPE:
	*(closure_t *)address = value->closure_val;
	break;

    case AST_LABEL_TYPE:
	*(label_t *)address = value->label_val;
	break;

    case AST_NULL_TYPE:
    case AST_OBJECT_TYPE:
    case AST_ARRAY_TYPE:
    case AST_STRING_TYPE:
	*(gpointer *)address = value->pointer_val;
	break;

    case AST_VOID_TYPE:
    case AST_IDENTIFIER_TYPE:
    case AST_INFERRED_TYPE:
	g_assert_not_reached();
	break;
    }
}

typedef struct stack_node_t stack_node_t;
struct stack_node_t 
{
    value_t		value;
    stack_node_t	*next;
};

__attribute__((warn_unused_result))
static stack_node_t *
stack_pop (stack_node_t *stack, value_t *value)
{
    *value = stack->value;

    return stack->next;
}

__attribute__((warn_unused_result))
static stack_node_t *
stack_push (stack_node_t *stack, value_t val)
{
    stack_node_t *new;

    new = gc_alloc (sizeof (stack_node_t));
    new->value = val;
    new->next = stack;

    return new;
}

static gpointer
get_outer (gpointer env, int cur_level, int def_level)
{
    int nesting_delta = cur_level - def_level;
    int i;

    g_assert (cur_level >= def_level);
    g_assert (nesting_delta >= 0);

    for (i = 0; i < nesting_delta; ++i)
	env = *(guchar **)env;

    return env;
}

static gpointer
var_address (ast_variable_definition_t *var,
	     int			level,
	     guchar                    *env)
{
    return var->offset + get_outer (env, level, var->level);
}

__attribute__((warn_unused_result))
static stack_node_t *
interpret_binary_operator (stack_node_t *stack,
			   binop_node_t	*binop)
{
    value_t right;
    value_t left;
    value_t result;

    stack = stack_pop (stack, &right);
    stack = stack_pop (stack, &left);

    evaluate_binary_operator (binop->operator,
			      binop->left_type, &left,
			      binop->right_type, &right,
			      binop->type_spec, &result);

    return stack_push (stack, result);
}

__attribute__((warn_unused_result))
static stack_node_t *
interpret_unary_operator (stack_node_t		*stack,
			  unary_node_t		*unary)
{
    value_t child;
    value_t result;

    stack = stack_pop (stack, &child);

    evaluate_unary_operator (unary->operator,
			     unary->child_type, &child,
			     unary->type_spec, &result);

    return stack_push (stack, result);
}

void
interpret (ast_t *ast)
{
    ast_program_t *program = &ast->program;
    guchar *env;
    node_t *current, *old_next;
    stack_node_t *stack = NULL;
    value_t val;
    closure_t *closure;
    guchar *old_env;
    guchar *obj;
    gpointer location;
    int i;

    gc_init ();

    env = gc_alloc (program->env_size);

    // g_print ("size of env: %d\n", program->env_size);
    
    current = program->enter;
    while (current)
    {
	node_t *next = current->common.next;

	switch (current->common.type)
	{
	case NODE_GOTO:
	    next = (node_t *)current->goto_.label;
	    break;

	case NODE_DYN_GOTO:
	    stack = stack_pop (stack, &val);

	    env = val.label_val.env;
	    next = (node_t *)val.label_val.label;
	    break;

	case NODE_IF:
	    stack = stack_pop (stack, &val);
	    if (val.bool_val == current->if_.sense)
		next = (node_t *)current->if_.taken;
	    break;

	case NODE_BINOP:
	    stack = interpret_binary_operator (stack, &current->binop);
	    break;

	case NODE_UNARY:
	    stack = interpret_unary_operator (stack, &current->unary);
	    break;

	case NODE_LITERAL:
	    stack = stack_push (stack, current->literal.value);
	    break;

	case NODE_CLOSURE:
	    val.closure_val.function = current->closure.function;
	    val.closure_val.env =
		get_outer (env, current->closure.expression->common.level,
			   current->closure.function->level);
	    stack = stack_push (stack, val);
	    break;

	case NODE_DYN_LABEL:
	    val.label_val.label = current->dyn_label.label;
	    val.label_val.env =
		get_outer (env,
			   current->dyn_label.expression->common.level,
			   current->dyn_label.definition->level);
	    stack = stack_push (stack, val);
	    break;

	case NODE_DYN_CLOSURE:
	    stack = stack_pop (stack, &val);
	    val.closure_val.env = val.pointer_val;
	    val.closure_val.function = current->dyn_closure.function;
	    stack = stack_push (stack, val);
	    break;

	case NODE_TO_STRING:
	    stack = stack_pop (stack, &val);

	    val.pointer_val = value_to_string (
		&val, current->to_string.type_spec);
	    
	    stack = stack_push (stack, val);
	    break;
	    
	case NODE_PRINT:
	    for (i = 0; i < current->print.n_exprs; ++i)
	    {
		stack = stack_pop (stack, &val);
		
		g_print ("%s", (char *)val.pointer_val);
		if (i < current->print.n_exprs - 1)
		    g_print (" ");
	    }
	    g_print ("\n");
	    break;

	case NODE_POP:
	    stack = stack_pop (stack, &val);
	    break;

	case NODE_DUP:
#if 0
	    g_print ("dup\n");
#endif
	    stack = stack_pop (stack, &val);
	    stack = stack_push (stack, val);
	    stack = stack_push (stack, val);
	    break;

	case NODE_INIT:
	case NODE_NOP:
	case NODE_FUN_REF:
	    break;

	case NODE_PROLOG:
	    break;

	case NODE_LABEL:
	    break;

	case NODE_CALL:
	    /* Save old */
	    old_env = env;
	    old_next = next;

	    stack = stack_pop (stack, &val);
	    closure = &val.closure_val;

	    if (!closure->function)
		g_error ("Attempting to run a NULL closure\n");

	    /* Make new environment */
	    env = gc_alloc (closure->function->env_size);
	    *((gpointer *)env + 0) = closure->env;	/* outer */
	    *((gpointer *)env + 1) = old_env;
	    *((gpointer *)env + 2) = old_next;

#if 0
	    g_print ("calling function %p (%s)\n", closure->function, closure->function->name);
	    g_print ("enter: %p\n", closure->function->enter);
#endif

	    /* Copy parameters to environment */
	    for (i = 0; closure->function->parameters[i]; ++i)
	    {
		ast_variable_definition_t *parameter;
		value_t value;

		parameter = closure->function->parameters[i];
		stack = stack_pop (stack, &value);

		write_value (
		    env + parameter->offset, parameter->type_spec, &value);
	    }

	    *((gpointer *)env + 3) = stack;
	    stack = NULL;
	    next = closure->function->enter;
	    break;

	case NODE_NEW:
	    obj = gc_alloc (current->new.class->env_size);
	    *(gpointer *)(obj + 0) = get_outer (
		env, current->new.expression->common.level,
		current->new.class->level);

	    val.pointer_val = obj;
	    stack = stack_push (stack, val);
	    break;

	case NODE_NEW_ARRAY:
	    stack = stack_pop (stack, &val);
	    val.pointer_val = gc_alloc (
		val.int32_val * current->new_array.element_size);
	    stack = stack_push (stack, val);
	    break;

	case NODE_RETURN:
	    stack = stack_pop (stack, &val);

	    g_assert (stack == NULL);

	    next = *((gpointer *)env + 2);
	    stack = *((gpointer *)env + 3);
	    env = *((gpointer *)env + 1);

	    stack = stack_push (stack, val);
	    break;

	case NODE_THIS:
	    /* The 'this' value is basically an argument to the method.
	     * The method is one level inside the class, and the arguments are
	     * one level inside the method. So the level of 'this'
	     * is class-level + 2.
	     */
	    location =
		get_outer (env, current->this.expression->common.level,
			   current->this.class->level + 2);

	    read_value (location, current->this.expression->common.type_spec,
			&val);

	    stack = stack_push (stack, val);
	    break;

	case NODE_STORE:
	    location = var_address (current->store.definition,
				    current->store.expression->common.level,
				    env);
	    stack = stack_pop (stack, &val);
	    write_value (location, current->store.definition->type_spec, &val);
	    break;

	case NODE_STORE_IND:
#if 0
	    g_print ("store ind\n");
#endif
	    stack = stack_pop (stack, &val);		/* env */
	    /* No need to compute the environment, since it's already
	     * computed for us in the val.pointer_val
	     */
	    location = val.pointer_val + current->load.definition->offset;
	    stack = stack_pop (stack, &val);		/* value */
	    write_value (location,
			 current->store_ind.definition->type_spec, &val);
	    break;

	case NODE_STORE_ARRAY:
	    stack = stack_pop (stack, &val);		/* array */

	    location = val.pointer_val;

	    stack = stack_pop (stack, &val);		/* index */

	    location += ast_type_spec_get_size (current->store_array.array->child_type) * val.int32_val;

	    stack = stack_pop (stack, &val);		/* value */

	    write_value (location, current->store_array.array->child_type, &val);
	    break;

	case NODE_LOAD:
	    location = var_address (
		current->load.definition,
		current->load.expression->common.level, env);

	    if (location == NULL)
	    {
		g_print ("array is NULL exception\n");
		exit (1);
	    }
#if 0
	    g_print ("loading %s at %p\n",
		     current->load.definition->name, location);
#endif
	    read_value (location, current->load.definition->type_spec, &val);
	    stack = stack_push (stack, val);
	    break;

	case NODE_LOAD_ARRAY:
	    stack = stack_pop (stack, &val);		/* array */

	    location = val.pointer_val;

	    stack = stack_pop (stack, &val);
	    if (location == NULL)
	    {
		g_print ("array is NULL exception\n");
		exit (1);
	    }

	    location += ast_type_spec_get_size (current->load_array.array->child_type) * val.int32_val;

	    read_value (location, current->load_array.array->child_type, &val);

	    stack = stack_push (stack, val);
	    break;

	case NODE_LOAD_IND:
#if 0
	    g_print ("loading %s indirectly\n",
		     current->load_ind.definition->name);
#endif
	    stack = stack_pop (stack, &val);
	    if (val.pointer_val == NULL)
	    {
		g_print ("NULL pointer exception\n");
		exit (1);
	    }
	    
	    /* No need to compute the environment, since it's already
	     * computed for us in the val.pointer_val
	     */
	    location = val.pointer_val + current->load.definition->offset;
	    read_value (location,
			current->load_ind.definition->type_spec, &val);
	    stack = stack_push (stack, val);
	    break;
	}

	current = next;
    }

    g_assert (stack == NULL);
}