code.c 18.7 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
{
47
    Thunk* thunk = createF(((AppEntry*) expr)->f);
48

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

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

55
56
    return thunk;                        
}
57

58
59
60
61
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);
62

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

67
    Thunk* thunk = createF(slice);
68

69
70
71
72
73
74
    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] 
75
                = ((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], frame_ptr);
76
    }
77
78
79
80
81
82
83
84
85
86
87
88
89
90

    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);

    if(arg->desc->unboxable)
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
91
    {
92
93
94
95
96
        return createT(arg);
    }
    else
    {
        return arg;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
97
    }
98
99
100
101
102
103
104
}

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

105
void set_create_thunk_fun(Code* code)
106
{
107
108
109
    switch(code->type)
    {
        case CT_APP_PRIM1:
110
        case CT_APP_PRIM_S:
111
        case CT_APP_PRIM2:
112
113
114
115
116
117
118
        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:
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
        case CT_APP_FUN:
        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;
135
136
        case CT_SELECT:
        case CT_IF:
137
138
139
            code->create_thunk = NULL;
            break;            
    }
140
141
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
142
void exec(Code* expr, int frame_ptr, int root_frame_ptr)
143
{
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
144
145
    int root_frame_ptr_b = stack_top_b;    
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
146
147
    while(1)
    {
148
        assert(expr != NULL);
149
        assert(stack_top_a < STACK_SIZE_A);
150
        assert(stack_top_b < STACK_SIZE_B);
151
152
153

        // TODO: check over application
        // TODO: enforce strictness in ADT/Record
154
        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
155
        switch (expr->type) {
156
157
158
159
        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
160
            
161
162
163
164
165
166
            ((PrimEntry*) ((AppEntry*) expr)->f)->exec(root_frame_ptr);

            destroy_stack_frame(root_frame_ptr);
            destroy_stack_frame_b(root_frame_ptr_b);
            return;                        
        }
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
        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;                                    
        }        
250
251
252
253
254
255
256
257
        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);
258

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
259
260
261
262
263
            destroy_stack_frame(root_frame_ptr);
            destroy_stack_frame_b(root_frame_ptr_b);
            return;            
        }
        case CT_APP_FUN:
264
        {            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
265
            Desc* slice = ((AppEntry*) expr)->f;
266

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
267
268
269
270
271
            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
272
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
273

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
274
275
276
277
278
279
280
            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
281

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
282
            Thunk* thunk = updateF(get_dst(root_frame_ptr), slice);
283

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
284
285
286
287
            assert(thunk->desc->arity == expr->nr_args);

            for (int i = 0; i < expr->nr_args; i++) {
                thunk->_args[i]  
288
                        = ((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], frame_ptr);
289
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
290
291
292
293
294
295

            set_return(root_frame_ptr, thunk);
            destroy_stack_frame(root_frame_ptr);
            destroy_stack_frame_b(root_frame_ptr_b);
            return;                                
        }
296
        case CT_APP_DYN:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
297
        {
298
            Thunk* basethunk = local(frame_ptr, ((AppEntry*)expr)->var.index);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
299
            if(!basethunk->desc->hnf) basethunk = eval(basethunk);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
300

301
302
303
304
305
            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) {
306
307
            case FT_PRIM1:
            case FT_PRIM2:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
308
            {
309
                for (int i = 0; i < basethunk->desc->arity; i++) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
310
311
312
313
314
315
316
317
                    if(basethunk->_args[i]->desc->hnf)
                    {
                        push_a(basethunk->_args[i]);
                    }
                    else
                    {
                        push_a(eval(basethunk->_args[i]));   
                    }
318
                }
319

320
                for (int i = 0; i < expr->nr_args; i++) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
321
                    push_a(alloc_b());
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
322
                    exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a);
323
324
325
                } 

                ((PrimEntry*) slice)->exec(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
326
                destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
327
                destroy_stack_frame_b(root_frame_ptr_b);                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
328
329
                return;                    
            }
330
331
332
333
334
335
336
337
338
339
340
341
            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]);    
                } 
342

343
344
345
346
347
348
349
350
351
352
353
354
                expr = ((FunEntry*) slice)->body;
                frame_ptr = new_frame_ptr;
                continue;                    
            }
            case FT_SLICE:
            case FT_ADT:
            case FT_RECORD:
            {
                Thunk* thunk = updateF(get_dst(root_frame_ptr), slice);

                assert(thunk->desc->arity == basethunk->desc->arity + expr->nr_args);            

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
355
                memcpy(&thunk->_args, &basethunk->_args, sizeof(Thunk*) * basethunk->desc->arity);
356
357
358

                for (int i = 0; i < expr->nr_args; i++) {
                    thunk->_args[basethunk->desc->arity + i] 
359
                            = ((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], frame_ptr);
360
361
362
363
                }

                set_return(root_frame_ptr, thunk);
                destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
364
                destroy_stack_frame_b(root_frame_ptr_b);
365
                return;                    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
366
            }
367
368
369
370
371
372
373
374
375
            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
376
            Thunk* arg = local(frame_ptr, ((VarEntry*) expr)->index);
377
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
378
            assert(is_hnf(arg));
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
379

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
380
381
382
383
384
385
386
387
388
389
            if(arg->desc->unboxable)
            {
                arg = updateT(get_dst(root_frame_ptr), arg);
            }
            else
            {
                forward_thunk(arg, root_frame_ptr);
            }
            
            set_return(root_frame_ptr, arg);
390
            destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
391
            destroy_stack_frame_b(root_frame_ptr_b);            
392
393
394
395
396
            return;                    
        }
        case CT_VAR:
        {
            Thunk* thunk = local(frame_ptr, ((VarEntry*) expr)->index);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
397

398
399
400
            follow_thunk(thunk);
            forward_thunk(thunk, root_frame_ptr);            
            set_return(root_frame_ptr, thunk);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
401

402
403
404
            switch(thunk->desc->type) {
            case FT_FUN:
            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
405
406
                // Destroy stack frame before eval, it is not needed any more
                // Greatly reduces stack usage
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
407
408
                destroy_stack_frame(root_frame_ptr);
                destroy_stack_frame_b(root_frame_ptr_b);  
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
409
410
411
412
                frame_ptr = stack_top_a;
                // Here frame_ptr == root_frame_ptr

                int argmask = 1;
413

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
414
                for (int i = 0; i < thunk->desc->arity; i++) {
415
                    arg_from_thunk(thunk->desc, thunk->_args[i]);
416
                }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
417
418
419

                expr = ((FunEntry*) thunk->desc)->body;
                continue;
420
            }
421
422
            case FT_PRIM1:
            case FT_PRIM2:
423
            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
424
425
426
427
428
429

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

                ((PrimEntry*) thunk->desc)->exec(root_frame_ptr);
430
431
                
                destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
432
                destroy_stack_frame_b(root_frame_ptr_b);
433
434
435
436
437
438
439
440
441
442
                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
443
                destroy_stack_frame_b(root_frame_ptr_b);
444
445
                return;
            }            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
446
447
448
        }
        case CT_THUNK:
        {
449
450
            Thunk* thunk = &((ThunkEntry*) expr)->thunk;            
            forward_thunk(thunk, root_frame_ptr);            
451
            set_return(root_frame_ptr, thunk);
452
            destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
453
            destroy_stack_frame_b(root_frame_ptr_b);
454
            return;               
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
455
        }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
456
457
        case CT_SELECT:
        {
458
459
460
            Thunk* pattern = alloc_b();
            pattern->desc = (Desc*) __STACK_PLACEHOLDER__;
            push_a(pattern);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
461
            exec(((SelectEntry*) expr)->expr, frame_ptr, stack_top_a);
462
            pattern = pop_a();
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
463

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
464
465
            assert(is_hnf(pattern));
            assert(pattern->desc->type == FT_ADT);            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
466

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
467
468
469
470
            bool handled = false;
            
            for (int i = 0; i < expr->nr_cases; i++) {
                SelectCaseEntry* caseEntry = &((SelectEntry*) expr)->cases[i];
471
                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
472
473
474
475
476
477
478
                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]);
479
                    }                  
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
480
                }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
481
482
483
484
                else if(caseEntry->type == SC_DEFAULT)
                {
                    // accept it
                }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
485
486
487
488
489
490
491
492
493
                else if (caseEntry->type == SC_LIT) {
                    printf("Exec: Unhandled entry type in CT_SELECT (SC_LIT)");
                    exit(-1);
                }
                
                // must be SC_DEFAULT now
                handled = true;
                expr = caseEntry->body;
                break;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
494
495
            }

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
496
497
498
499
500
501
502
503
            if(handled) continue;
            
            printf("Exec: no select cases matches");
            print(pattern, false);
            exit(-1);
        }
        case CT_IF:
        {        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
504
            push_a(alloc_b());
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
505
            exec(((IfEntry*) expr)->cond, frame_ptr, stack_top_a);
506
            Thunk* cond = pop_a();
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
507
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
508
509
510
511
512
513
514
515
            if (readB(cond)) {
                expr = ((IfEntry*) expr)->texpr;
                continue;                
            }
            else {
                expr = ((IfEntry*) expr)->fexpr;
                continue;     
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
516
        }
517
518
519
520
521
522
        }
    }
}

struct Thunk* eval(Thunk* thunk) {
    assert(thunk != NULL);    
523
        
524
525
526
527
528
529
530
531
532
533
534
535
536
    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
537
        exec(((FunEntry*) thunk->desc)->body, frame_ptr, frame_ptr);
538
539
540
        thunk = pop_a();
        return thunk;
    }
541
542
    case FT_PRIM1:
    case FT_PRIM2:
543
544
545
546
547
    {
        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
548
549
550
551
552
553
554
555
            if(thunk->_args[i]->desc->hnf)
            {
                push_a(thunk->_args[i]);
            }
            else
            {
                push_a(eval(thunk->_args[i]));
            }
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
        }

        ((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
572
    }
573
}