code.c 22 KB
Newer Older
1
2
#include <stdio.h>
#include <stdlib.h>
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
3
#include <stdbool.h>
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
4
#include <strings.h>
5

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
6
#include "debug.h"
7
#include "code.h"
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
8
#include "mem.h"
9
10
#include "desc.h"

11
12
13
14
15
16
// For compressing the source code a bit

#define follow_thunk(thunk) if (thunk->desc == (Desc*) __FORWARD_PTR__) thunk = thunk->_forward_ptr;

#define forward_thunk(thunk, frame_ptr) \
        Thunk* dst = get_dst(frame_ptr); \
17
18
        dst->desc = (Desc*) __FORWARD_PTR__; \
        dst->_forward_ptr = thunk; 
19

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
20
21
#define arg_from_thunk(descarg, arg) \
        if(((FunEntry*) (descarg))->strictness & argmask && !arg->desc->hnf) \
22
23
24
25
26
27
28
29
30
        { \
            push_a(eval(arg)); \
        } \
        else \
        { \
            push_a(arg); \
        } \
        argmask <<= 1;

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
31
32
#define arg_from_code(descarg, arg) \
        if(((FunEntry*) (descarg))->strictness & argmask) \
33
        { \
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
34
35
36
            Thunk* phl = alloc_b(); \
            phl->desc = (Desc*) __STACK_PLACEHOLDER__; \
            push_a(phl); \
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
37
            exec(arg, frame_ptr, stack_top_a); \
38
39
40
        } \
        else \
        { \
41
            push_a(arg->create_thunk(arg, frame_ptr)); \
42
43
44
        } \
        argmask <<= 1;

45
struct Thunk* create_thunk_app_static(Code* expr, int frame_ptr)
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
46
{
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
47
48
    Thunk* thunk = (Thunk*) alloc_heap(((AppEntry*) expr)->f->thunk_size);
    thunk->desc = ((AppEntry*) expr)->f;
49

50
    assert(thunk->desc->arity == expr->nr_args);
51

52
    for (int i = 0; i < expr->nr_args; i++) {
53
        thunk->_args[i] = ((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], frame_ptr);
54
    }
55

56
57
    return thunk;                        
}
58

59
60
61
62
struct Thunk* create_thunk_app_dyn(Code* expr, int frame_ptr)
{
    Thunk* basethunk = local(frame_ptr, ((AppEntry*)expr)->var.index);
    if(!basethunk->desc->hnf) basethunk = eval(basethunk);
63

64
65
66
    Desc* slice =
            get_slice(basethunk->desc->type == FT_SLICE ?
                      ((SliceEntry*) basethunk->desc)->forward_ptr : basethunk->desc, basethunk->desc->arity + expr->nr_args);
67

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
68
69
    Thunk* thunk = (Thunk*) alloc_heap(slice->thunk_size);
    thunk->desc = slice;
70

71
72
73
74
75
76
    assert(thunk->desc->arity == basethunk->desc->arity + expr->nr_args);            

    memcpy(&thunk->_args, &basethunk->_args, sizeof(Thunk*) * basethunk->desc->arity);

    for (int i = 0; i < expr->nr_args; i++) {
        thunk->_args[basethunk->desc->arity + i] 
77
                = ((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], frame_ptr);
78
    }
79
80
81
82
83
84
85
86
87
88
89
90
91

    return thunk;    
}

struct Thunk* create_thunk_var(Code* expr, int frame_ptr)
{
    return local(frame_ptr, ((VarEntry*) expr)->index);    
}

struct Thunk* create_thunk_var_strict(Code* expr, int frame_ptr)
{
    Thunk* arg = local(frame_ptr, ((VarEntry*) expr)->index);

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
92
    if(arg->desc->unboxable) // unboxable means it is on the B stack
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
93
    {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
94
95
96
        Thunk* target = (Thunk*) alloc_heap(sizeof (Thunk));
        memcpy(target, arg, sizeof(Thunk));
        return target;
97
98
99
100
    }
    else
    {
        return arg;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
101
    }
102
103
104
105
106
107
108
}

struct Thunk* create_thunk_thunk(Code* expr, int frame_ptr)
{
    return &((ThunkEntry*) expr)->thunk;
}

109
void set_create_thunk_fun(Code* code)
110
{
111
112
113
    switch(code->type)
    {
        case CT_APP_PRIM1:
114
        case CT_APP_PRIM_S:
115
        case CT_APP_PRIM2:
116
117
118
119
120
121
122
        case CT_APP_PRIM_ST:
        case CT_APP_PRIM_TS:
        case CT_APP_PRIM_SS:
        case CT_APP_PRIM_AT:
        case CT_APP_PRIM_TA:
        case CT_APP_PRIM_AS:
        case CT_APP_PRIM_SA:
123
        case CT_APP_FUN:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
124
125
        case CT_APP_FUN1:
        case CT_APP_FUN2:
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
        case CT_APP_THUNK:
            code->create_thunk = create_thunk_app_static;
            break;
        case CT_APP_DYN:
            code->create_thunk = create_thunk_app_dyn;
            break;
        case CT_VAR:
            code->create_thunk = create_thunk_var;
            break;
        case CT_VAR_STRICT:
            code->create_thunk = create_thunk_var_strict;
            break;
        case CT_THUNK:
            code->create_thunk = create_thunk_thunk;
            break;
141
142
        case CT_SELECT:
        case CT_IF:
143
144
145
            code->create_thunk = NULL;
            break;            
    }
146
147
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
148
void exec(Code* expr, int frame_ptr, int root_frame_ptr)
149
{
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
150
151
    int root_frame_ptr_b = stack_top_b;    
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
152
153
    while(1)
    {
154
        assert(expr != NULL);
155
        assert(stack_top_a < STACK_SIZE_A);
156
        assert(stack_top_b < STACK_SIZE_B);
157
158
159

        // TODO: check over application
        // TODO: enforce strictness in ADT/Record
160
        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
161
        switch (expr->type) {
162
163
164
165
        case CT_APP_PRIM1:
        {
            push_a(alloc_b());                        
            exec(((AppEntry*) expr)->args[0], frame_ptr, stack_top_a);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
166
            
167
168
169
170
171
172
            ((PrimEntry*) ((AppEntry*) expr)->f)->exec(root_frame_ptr);

            destroy_stack_frame(root_frame_ptr);
            destroy_stack_frame_b(root_frame_ptr_b);
            return;                        
        }
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
        case CT_APP_PRIM_S:
        {
            push_a(local(frame_ptr, ((VarEntry*) ((AppEntry*) expr)->args[0])->index));                        
            ((PrimEntry*) ((AppEntry*) expr)->f)->exec(root_frame_ptr);

            destroy_stack_frame(root_frame_ptr);
            destroy_stack_frame_b(root_frame_ptr_b);
            return;                        
        }
        case CT_APP_PRIM_ST:
        {            
            push_a(local(frame_ptr, ((VarEntry*) ((AppEntry*) expr)->args[0])->index));                        
            push_a(&((ThunkEntry*) ((AppEntry*) expr)->args[1])->thunk);                        
            ((PrimEntry*) ((AppEntry*) expr)->f)->exec(root_frame_ptr);

            destroy_stack_frame(root_frame_ptr);
            destroy_stack_frame_b(root_frame_ptr_b);
            return;                                    
        }        
        case CT_APP_PRIM_TS:
        {            
            push_a(&((ThunkEntry*) ((AppEntry*) expr)->args[0])->thunk);            
            push_a(local(frame_ptr, ((VarEntry*) ((AppEntry*) expr)->args[1])->index));                        
            ((PrimEntry*) ((AppEntry*) expr)->f)->exec(root_frame_ptr);

            destroy_stack_frame(root_frame_ptr);
            destroy_stack_frame_b(root_frame_ptr_b);
            return;                                    
        }        
        case CT_APP_PRIM_SS:
        {            
            push_a(local(frame_ptr, ((VarEntry*) ((AppEntry*) expr)->args[0])->index));
            push_a(local(frame_ptr, ((VarEntry*) ((AppEntry*) expr)->args[1])->index));
            ((PrimEntry*) ((AppEntry*) expr)->f)->exec(root_frame_ptr);

            destroy_stack_frame(root_frame_ptr);
            destroy_stack_frame_b(root_frame_ptr_b);
            return;                                    
        }        
        case CT_APP_PRIM_TA:
        {            
            push_a(&((ThunkEntry*) ((AppEntry*) expr)->args[0])->thunk);            
            push_a(alloc_b());                        
            exec(((AppEntry*) expr)->args[1], frame_ptr, stack_top_a);
            ((PrimEntry*) ((AppEntry*) expr)->f)->exec(root_frame_ptr);

            destroy_stack_frame(root_frame_ptr);
            destroy_stack_frame_b(root_frame_ptr_b);
            return;                                    
        }        
        case CT_APP_PRIM_AT:
        {            
            push_a(alloc_b());                        
            exec(((AppEntry*) expr)->args[0], frame_ptr, stack_top_a);
            push_a(&((ThunkEntry*) ((AppEntry*) expr)->args[1])->thunk);            
            ((PrimEntry*) ((AppEntry*) expr)->f)->exec(root_frame_ptr);

            destroy_stack_frame(root_frame_ptr);
            destroy_stack_frame_b(root_frame_ptr_b);
            return;                                    
        }        
        case CT_APP_PRIM_AS:
        {            
            push_a(alloc_b());                        
            exec(((AppEntry*) expr)->args[0], frame_ptr, stack_top_a);
            push_a(local(frame_ptr, ((VarEntry*) ((AppEntry*) expr)->args[1])->index));
            ((PrimEntry*) ((AppEntry*) expr)->f)->exec(root_frame_ptr);

            destroy_stack_frame(root_frame_ptr);
            destroy_stack_frame_b(root_frame_ptr_b);
            return;                                    
        }        
        case CT_APP_PRIM_SA:
        {            
            push_a(local(frame_ptr, ((VarEntry*) ((AppEntry*) expr)->args[0])->index));
            push_a(alloc_b());                        
            exec(((AppEntry*) expr)->args[1], frame_ptr, stack_top_a);
            ((PrimEntry*) ((AppEntry*) expr)->f)->exec(root_frame_ptr);

            destroy_stack_frame(root_frame_ptr);
            destroy_stack_frame_b(root_frame_ptr_b);
            return;                                    
        }        
256
257
258
259
260
261
262
263
        case CT_APP_PRIM2:
        {            
            push_a(alloc_b());                        
            exec(((AppEntry*) expr)->args[0], frame_ptr, stack_top_a);
            push_a(alloc_b());                        
            exec(((AppEntry*) expr)->args[1], frame_ptr, stack_top_a);

            ((PrimEntry*) ((AppEntry*) expr)->f)->exec(root_frame_ptr);
264

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
265
266
267
268
            destroy_stack_frame(root_frame_ptr);
            destroy_stack_frame_b(root_frame_ptr_b);
            return;            
        }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
        case CT_APP_FUN1:
        {
            Desc* slice = ((AppEntry*) expr)->f;

            int argmask = 1;            
            arg_from_code(slice, ((AppEntry*) expr)->args[0]);
              
            expr = ((FunEntry*) slice)->body;
            frame_ptr = stack_top_a - 1;
            continue;                        
        }        
        case CT_APP_FUN2:
        {
            Desc* slice = ((AppEntry*) expr)->f;

            int argmask = 1;
            
            arg_from_code(slice, ((AppEntry*) expr)->args[0]);
            arg_from_code(slice, ((AppEntry*) expr)->args[1]);
              
            expr = ((FunEntry*) slice)->body;
            frame_ptr = stack_top_a - 2;
            continue;                        
        }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
293
        case CT_APP_FUN:
294
        {            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
295
            Desc* slice = ((AppEntry*) expr)->f;
296

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
297
298
299
300
301
            int new_frame_ptr = stack_top_a;
            int argmask = 1;

            for (int i = 0; i < expr->nr_args; i++) {
                arg_from_code(slice, ((AppEntry*) expr)->args[i]);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
302
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
303

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
304
305
306
307
308
309
310
            expr = ((FunEntry*) slice)->body;
            frame_ptr = new_frame_ptr;
            continue;            
        }
        case CT_APP_THUNK:
        {
            Desc* slice = ((AppEntry*) expr)->f;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
311
312
313
314
315
316
317
            Thunk* thunk = get_dst(root_frame_ptr);
            int newsize = slice->thunk_size;
            
            if (thunk->desc->thunk_size < newsize) {
                Thunk* target = thunk;
                thunk = (Thunk*) alloc_heap(newsize);
                target->desc = (Desc*) __FORWARD_PTR__;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
318
319
                target->_forward_ptr = thunk;
                set_return(root_frame_ptr, thunk);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
320
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
321

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
322
323
            thunk->desc = slice;
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
324
325
326
327
            assert(thunk->desc->arity == expr->nr_args);

            for (int i = 0; i < expr->nr_args; i++) {
                thunk->_args[i]  
328
                        = ((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], frame_ptr);
329
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
330
331
332
333
334

            destroy_stack_frame(root_frame_ptr);
            destroy_stack_frame_b(root_frame_ptr_b);
            return;                                
        }
335
        case CT_APP_DYN:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
336
        {
337
            Thunk* basethunk = local(frame_ptr, ((AppEntry*)expr)->var.index);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
338
            if(!basethunk->desc->hnf) basethunk = eval(basethunk);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
339

340
341
342
343
344
            Desc* slice =
                    get_slice(basethunk->desc->type == FT_SLICE ?
                              ((SliceEntry*) basethunk->desc)->forward_ptr : basethunk->desc, basethunk->desc->arity + expr->nr_args);

            switch(slice->type) {
345
346
            case FT_PRIM1:
            case FT_PRIM2:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
347
            {
348
                for (int i = 0; i < basethunk->desc->arity; i++) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
349
350
351
352
353
354
355
356
                    if(basethunk->_args[i]->desc->hnf)
                    {
                        push_a(basethunk->_args[i]);
                    }
                    else
                    {
                        push_a(eval(basethunk->_args[i]));   
                    }
357
                }
358

359
                for (int i = 0; i < expr->nr_args; i++) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
360
                    push_a(alloc_b());
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
361
                    exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a);
362
363
364
                } 

                ((PrimEntry*) slice)->exec(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
365
                destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
366
                destroy_stack_frame_b(root_frame_ptr_b);                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
367
368
                return;                    
            }
369
370
371
372
373
374
375
376
377
378
379
380
            case FT_FUN:
            {
                int new_frame_ptr = stack_top_a;                    
                int argmask = 1;

                for (int i = 0; i < basethunk->desc->arity; i++) {
                    arg_from_thunk(slice, basethunk->_args[i])
                }

                for (int i = 0; i < expr->nr_args; i++) {
                    arg_from_code(slice, ((AppEntry*) expr)->args[i]);    
                } 
381

382
383
384
385
386
387
388
389
                expr = ((FunEntry*) slice)->body;
                frame_ptr = new_frame_ptr;
                continue;                    
            }
            case FT_SLICE:
            case FT_ADT:
            case FT_RECORD:
            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
390
391
392
393
394
395
396
                Thunk* thunk = get_dst(root_frame_ptr);
                int newsize = slice->thunk_size;

                if (thunk->desc->thunk_size < newsize) {
                    Thunk* target = thunk;
                    thunk = (Thunk*) alloc_heap(newsize);
                    target->desc = (Desc*) __FORWARD_PTR__;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
397
398
                    target->_forward_ptr = thunk;
                    set_return(root_frame_ptr, thunk);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
399
                }
400

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
401
402
                thunk->desc = slice;
                
403
404
                assert(thunk->desc->arity == basethunk->desc->arity + expr->nr_args);            

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
405
                memcpy(&thunk->_args, &basethunk->_args, sizeof(Thunk*) * basethunk->desc->arity);
406
407
408

                for (int i = 0; i < expr->nr_args; i++) {
                    thunk->_args[basethunk->desc->arity + i] 
409
                            = ((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], frame_ptr);
410
411
412
                }

                destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
413
                destroy_stack_frame_b(root_frame_ptr_b);
414
                return;                    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
415
            }
416
417
418
419
420
421
422
423
424
            case FT_BOXED_LIT:
                abort("Literal unexpected here");                
            case FT_CAF:
            case FT_CAF_REDUCED:
                not_implemented("CAF");
            }
        }
        case CT_VAR_STRICT:
        {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
425
            Thunk* arg = local(frame_ptr, ((VarEntry*) expr)->index);
426
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
427
            assert(is_hnf(arg));
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
428

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
429
            // TODO: check how often happens
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
430
431
            if(arg->desc->unboxable)
            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
432
                memcpy(get_dst(root_frame_ptr), arg, sizeof(Thunk));
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
433
434
435
436
            }
            else
            {
                forward_thunk(arg, root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
437
                set_return(root_frame_ptr, arg);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
438
439
            }
            
440
            destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
441
            destroy_stack_frame_b(root_frame_ptr_b);            
442
443
444
445
446
            return;                    
        }
        case CT_VAR:
        {
            Thunk* thunk = local(frame_ptr, ((VarEntry*) expr)->index);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
447

448
449
450
            follow_thunk(thunk);
            forward_thunk(thunk, root_frame_ptr);            
            set_return(root_frame_ptr, thunk);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
451

452
453
454
            switch(thunk->desc->type) {
            case FT_FUN:
            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
455
456
                // Destroy stack frame before eval, it is not needed any more
                // Greatly reduces stack usage
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
457
458
                destroy_stack_frame(root_frame_ptr);
                destroy_stack_frame_b(root_frame_ptr_b);  
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
459
460
461
462
                frame_ptr = stack_top_a;
                // Here frame_ptr == root_frame_ptr

                int argmask = 1;
463

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
464
                for (int i = 0; i < thunk->desc->arity; i++) {
465
                    arg_from_thunk(thunk->desc, thunk->_args[i]);
466
                }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
467
468
469

                expr = ((FunEntry*) thunk->desc)->body;
                continue;
470
            }
471
472
            case FT_PRIM1:
            case FT_PRIM2:
473
            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
474
475
476
477
478
479

                for (int i = 0; i < thunk->desc->arity; i++) {
                    push_a(eval(thunk->_args[i]));
                }

                ((PrimEntry*) thunk->desc)->exec(root_frame_ptr);
480
481
                
                destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
482
                destroy_stack_frame_b(root_frame_ptr_b);
483
484
485
486
487
488
489
490
491
492
                return;
            }
            case FT_CAF:
            case FT_CAF_REDUCED:
                not_implemented("CAF");            
            case FT_SLICE:
            case FT_ADT:
            case FT_RECORD:
            case FT_BOXED_LIT:
                destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
493
                destroy_stack_frame_b(root_frame_ptr_b);
494
495
                return;
            }            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
496
497
498
        }
        case CT_THUNK:
        {
499
500
            Thunk* thunk = &((ThunkEntry*) expr)->thunk;            
            forward_thunk(thunk, root_frame_ptr);            
501
            set_return(root_frame_ptr, thunk);
502
            destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
503
            destroy_stack_frame_b(root_frame_ptr_b);
504
            return;               
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
505
        }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
506
        case CT_SELECT:
507
508
509
        {                        
            bool handled = false;
            
510
511
512
            Thunk* pattern = alloc_b();
            pattern->desc = (Desc*) __STACK_PLACEHOLDER__;
            push_a(pattern);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
513
            exec(((SelectEntry*) expr)->expr, frame_ptr, stack_top_a);
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
            pattern = pop_a();            
            
            int i = 0;

            do
            {                
                assert(is_hnf(pattern));
                                
                for (;i < expr->nr_cases; i++) {
                    SelectCaseEntry* caseEntry = &((SelectEntry*) expr)->cases[i];
                    
                    if (caseEntry->type == SC_CONS) {

                        // Pattern match
                        if ((Desc*) caseEntry->cons != pattern->desc) continue;

                        // Put the constructor arguments to the stack if matches
                        for (int i = 0; i < pattern->desc->arity; i++) {
                            push_a(pattern->_args[i]);
                        }                  
                    }
                    else if(caseEntry->type == SC_DEFAULT)
                    {
                        // accept it
                    }
                    else if (caseEntry->type == SC_LIT) {
                        assert(caseEntry->lit->thunk.desc != (Desc*) __INT__);
                                        
                        if(caseEntry->lit->thunk._int != pattern->_int) continue;
                    }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
544

545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
                    // must be SC_DEFAULT now
                    expr = caseEntry->body;
                    
                    if(expr->type == CT_SELECT)
                    {
                        i=-1;
                        
                        ((SelectEntry*) expr)->saved_pattern = pattern;
                        
                        pattern = alloc_b();
                        pattern->desc = (Desc*) __STACK_PLACEHOLDER__;
                        push_a(pattern);
                        exec(((SelectEntry*) expr)->expr, frame_ptr, stack_top_a);
                        pattern = pop_a();                        
                        
                        continue;
                    }
                    
                    handled = true;                    
                    break;
                }                    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
566

567
                if(!handled)
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
568
                {
569
570
571
572
573
574
575
576
577
578
579
580
                    if(((SelectEntry*) expr)->fallback != NULL)
                    {
                        expr = (Code*) ((SelectEntry*) expr)->fallback;
                        i = ((SelectEntry*) expr)->fallbackidx;
                        pattern = ((SelectEntry*) expr)->saved_pattern;
                    }
                    else
                    {
                        printf("Exec: no select cases matches");
                        print(pattern, false);
                        exit(-1);                                                
                    }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
581
                }
582
583
            }            
            while(!handled);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
584
            
585
            continue;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
586
587
588
        }
        case CT_IF:
        {        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
589
            push_a(alloc_b());
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
590
            exec(((IfEntry*) expr)->cond, frame_ptr, stack_top_a);
591
            Thunk* cond = pop_a();
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
592
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
593
594
595
596
597
598
599
600
            if (readB(cond)) {
                expr = ((IfEntry*) expr)->texpr;
                continue;                
            }
            else {
                expr = ((IfEntry*) expr)->fexpr;
                continue;     
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
601
        }
602
603
604
605
606
607
        }
    }
}

struct Thunk* eval(Thunk* thunk) {
    assert(thunk != NULL);    
608
        
609
610
611
612
613
614
615
616
617
618
619
620
621
    follow_thunk(thunk);
    
    switch(thunk->desc->type) {
    case FT_FUN:
    {
        push_a(thunk);
        int frame_ptr = stack_top_a;
        int argmask = 1;
        
        for (int i = 0; i < thunk->desc->arity; i++) {
            arg_from_thunk(thunk->desc, thunk->_args[i]);
        }
        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
622
        exec(((FunEntry*) thunk->desc)->body, frame_ptr, frame_ptr);
623
624
625
        thunk = pop_a();
        return thunk;
    }
626
627
    case FT_PRIM1:
    case FT_PRIM2:
628
629
630
631
632
    {
        push_a(thunk);
        int frame_ptr = stack_top_a;

        for (int i = 0; i < thunk->desc->arity; i++) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
633
634
635
636
637
638
639
640
            if(thunk->_args[i]->desc->hnf)
            {
                push_a(thunk->_args[i]);
            }
            else
            {
                push_a(eval(thunk->_args[i]));
            }
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
        }

        ((PrimEntry*) thunk->desc)->exec(frame_ptr);        
        
        stack_top_a = frame_ptr;        
        thunk = pop_a();
        return thunk;
    }
    case FT_CAF:
    case FT_CAF_REDUCED:
        not_implemented("CAF");            
    case FT_SLICE:
    case FT_ADT:
    case FT_RECORD:
    case FT_BOXED_LIT:
        return thunk;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
657
    }
658
}