code.c 22.5 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
#include "desc.h"
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
10
#include "gc.h"
11

12
13
// For compressing the source code a bit

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
14
15
#define instackb(addr) ((char*)addr >= (char*) &stack_b[0] && (char*)addr < (char*) &stack_b[STACK_SIZE_B])

16
17
18
19
#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); \
20
        if(dst != NULL){ \
21
        dst->desc = (Desc*) __FORWARD_PTR__; \
22
23
        dst->_forward_ptr = thunk; \
        }
24

Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
25
#define placeholder() \
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
26
        push_a(alloc_b());
27

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
28
#define arg_from_code(descarg, arg) \
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
29
        if(((FunEntry*) (descarg))->boxing & argmask) \
30
        { \
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
31
            placeholder(); \
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
32
            exec(arg, frame_ptr, stack_top_a); \
33
        } \
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
34
35
36
37
38
        else if(((FunEntry*) (descarg))->strictness & argmask) \
        { \
            push_a(NULL); \
            exec(arg, frame_ptr, stack_top_a); \
        } \
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
struct Thunk* create_thunk_app_dyn(Code* expr, int frame_ptr)
{
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
61
    push_a(local(frame_ptr, ((AppEntry*)expr)->var.index));
62

Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
63
64
65
66
67
68
69
    int tmp = gc_enabled;
    gc_enabled = 0;
    peek_a()->desc->eval();
    gc_enabled = tmp;
    
    Thunk* basethunk = pop_a();
    
70
71
72
    Desc* slice =
            get_slice(basethunk->desc->type == FT_SLICE ?
                      ((SliceEntry*) basethunk->desc)->forward_ptr : basethunk->desc, basethunk->desc->arity + expr->nr_args);
73

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
74
75
    Thunk* thunk = (Thunk*) alloc_heap(slice->thunk_size);
    thunk->desc = slice;
76

77
78
79
80
81
82
    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] 
83
                = ((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], frame_ptr);
84
    }
85
86
87
88
89
90
91
92
93

    return thunk;    
}

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

94
struct Thunk* create_thunk_var_unboxed(Code* expr, int frame_ptr)
95
96
97
{
    Thunk* arg = local(frame_ptr, ((VarEntry*) expr)->index);

98
99
100
101
    // TODO: check if its on heap
    Thunk* target = (Thunk*) alloc_heap(sizeof (Thunk));
    memcpy(target, arg, sizeof(Thunk));
    return target;
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
        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:
133
        case CT_VAR_STRICT:            
134
135
            code->create_thunk = create_thunk_var;
            break;
136
137
        case CT_VAR_UNBOXED:
            code->create_thunk = create_thunk_var_unboxed;
138
139
140
141
            break;
        case CT_THUNK:
            code->create_thunk = create_thunk_thunk;
            break;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
142
143
        case CT_SELECT_ADT:
        case CT_SELECT_LIT:    
144
        case CT_IF:
145
146
147
            code->create_thunk = NULL;
            break;            
    }
148
149
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
150
void exec(Code* expr, int frame_ptr, int root_frame_ptr)
151
{
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
152
153
    if(heap_curr > gc_trigger) gc();
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
154
155
    int root_frame_ptr_b = stack_top_b;    
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
156
    while(1)
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
157
    {        
158
        assert(expr != NULL);
159
        assert(stack_top_a < STACK_SIZE_A);
160
        assert(stack_top_b < STACK_SIZE_B);
161
162
163

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

            destroy_stack_frame(root_frame_ptr);
            destroy_stack_frame_b(root_frame_ptr_b);
            return;                        
        }
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
        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);            
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
219
            placeholder();                       
220
221
222
223
224
225
226
227
228
            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:
        {            
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
229
            placeholder();                     
230
231
232
233
234
235
236
237
238
239
            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:
        {            
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
240
            placeholder();                        
241
242
243
244
245
246
247
248
249
250
251
            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));
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
252
            placeholder();                       
253
254
255
256
257
258
259
            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;                                    
        }        
260
        case CT_APP_PRIM2:
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
261
262
        {           
            placeholder();                       
263
            exec(((AppEntry*) expr)->args[0], frame_ptr, stack_top_a);
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
264
            placeholder();                        
265
            exec(((AppEntry*) expr)->args[1], frame_ptr, stack_top_a);
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
266
            
267
            ((PrimEntry*) ((AppEntry*) expr)->f)->exec(root_frame_ptr);
268

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
269
270
271
272
            destroy_stack_frame(root_frame_ptr);
            destroy_stack_frame_b(root_frame_ptr_b);
            return;            
        }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
273
274
275
276
277
278
279
280
281
282
283
284
285
286
        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;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
287
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
288
289
290
291
292
293
294
295
296
            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
297
        case CT_APP_FUN:
298
        {            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
299
            Desc* slice = ((AppEntry*) expr)->f;
300

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
301
302
            int new_frame_ptr = stack_top_a;
            int argmask = 1;
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
303
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
304
305
            for (int i = 0; i < expr->nr_args; i++) {
                arg_from_code(slice, ((AppEntry*) expr)->args[i]);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
306
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
307

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
308
309
310
311
312
313
314
            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
315
316
317
            Thunk* thunk = get_dst(root_frame_ptr);
            int newsize = slice->thunk_size;
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
318
319
320
321
322
323
            if (thunk == NULL)
            {
                thunk = (Thunk*) alloc_heap(newsize);
                set_return(root_frame_ptr, thunk);
            }
            else if (newsize < sizeof(Thunk) || thunk->desc->thunk_size < newsize) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
324
325
326
                Thunk* target = thunk;
                thunk = (Thunk*) alloc_heap(newsize);
                target->desc = (Desc*) __FORWARD_PTR__;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
327
328
                target->_forward_ptr = thunk;
                set_return(root_frame_ptr, thunk);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
329
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
330

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
331
332
            thunk->desc = slice;
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
333
            assert(thunk->desc->arity == expr->nr_args);
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
334
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
335
336
            for (int i = 0; i < expr->nr_args; i++) {
                thunk->_args[i]  
337
                        = ((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], frame_ptr);
338
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
339
340
341
342
343

            destroy_stack_frame(root_frame_ptr);
            destroy_stack_frame_b(root_frame_ptr_b);
            return;                                
        }
344
        case CT_APP_DYN:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
345
        {
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
346
347
348
349
350
            push_a(local(frame_ptr, ((AppEntry*)expr)->var.index));

            Thunk** bt = &peek_a();
            (*bt)->desc->eval();
            
351
            Desc* slice =
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
352
353
                    get_slice((*bt)->desc->type == FT_SLICE ?
                              ((SliceEntry*) (*bt)->desc)->forward_ptr : (*bt)->desc, (*bt)->desc->arity + expr->nr_args);
354
355

            switch(slice->type) {
356
357
            case FT_PRIM1:
            case FT_PRIM2:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
358
            {
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
359
360
361
362
                for (int i = 0; i < (*bt)->desc->arity; i++) {
                    push_a((*bt)->_args[i]);
                    /*if(!basethunk->_args[i]->desc->hnf)*/ (*bt)->_args[i]->desc->eval();
                    
363
                }
364

365
                for (int i = 0; i < expr->nr_args; i++) {
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
366
                    placeholder();
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
367
                    exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a);
368
369
370
                } 

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

Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
380
381
382
383
384
385
386
                for (int i = 0; i < (*bt)->desc->arity; i++) {                    
                    push_a((*bt)->_args[i]);
                    if(((FunEntry*) (slice))->strictness & argmask /*&& !arg->desc->hnf*/) 
                    { 
                        (*bt)->_args[i]->desc->eval(); 
                    } 
                    argmask <<= 1;
387
388
389
390
391
                }

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

393
394
395
396
397
398
399
400
                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
401
402
403
                Thunk* thunk = get_dst(root_frame_ptr);
                int newsize = slice->thunk_size;

404
                if (thunk != NULL && thunk->desc->thunk_size < newsize) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
405
406
407
                    Thunk* target = thunk;
                    thunk = (Thunk*) alloc_heap(newsize);
                    target->desc = (Desc*) __FORWARD_PTR__;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
408
409
                    target->_forward_ptr = thunk;
                    set_return(root_frame_ptr, thunk);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
410
                }
411

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
412
413
                thunk->desc = slice;
                
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
414
                assert(thunk->desc->arity == (*bt)->desc->arity + expr->nr_args);            
415

Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
416
                memcpy(&thunk->_args, &(*bt)->_args, sizeof(Thunk*) * (*bt)->desc->arity);
417
418

                for (int i = 0; i < expr->nr_args; i++) {
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
419
                    thunk->_args[(*bt)->desc->arity + i] 
420
                            = ((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], frame_ptr);
421
422
423
                }

                destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
424
                destroy_stack_frame_b(root_frame_ptr_b);
425
                return;                    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
426
            }
427
428
429
430
431
432
433
434
            case FT_BOXED_LIT:
                abort("Literal unexpected here");                
            case FT_CAF:
            case FT_CAF_REDUCED:
                not_implemented("CAF");
            }
        }
        case CT_VAR_STRICT:
435
        case CT_VAR_UNBOXED:            
436
        {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
437
            Thunk* arg = local(frame_ptr, ((VarEntry*) expr)->index);
438
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
439
            assert(is_hnf(arg));
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
440

441
            if(get_dst(root_frame_ptr) != NULL && arg->desc->unboxable)
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
442
            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
443
                memcpy(get_dst(root_frame_ptr), arg, sizeof(Thunk));
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
444
445
446
447
            }
            else
            {
                forward_thunk(arg, root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
448
                set_return(root_frame_ptr, arg);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
449
450
            }
            
451
            destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
452
            destroy_stack_frame_b(root_frame_ptr_b);            
453
454
455
456
457
            return;                    
        }
        case CT_VAR:
        {
            Thunk* thunk = local(frame_ptr, ((VarEntry*) expr)->index);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
458

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
459
460
            assert(!instackb(thunk));
            
461
462
463
            follow_thunk(thunk);
            forward_thunk(thunk, root_frame_ptr);            
            set_return(root_frame_ptr, thunk);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
464

465
466
467
            switch(thunk->desc->type) {
            case FT_FUN:
            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
468
469
                // Destroy stack frame before eval, it is not needed any more
                // Greatly reduces stack usage
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
470
471
                destroy_stack_frame(root_frame_ptr);
                destroy_stack_frame_b(root_frame_ptr_b);  
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
472
                                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
473
474
475
476
                frame_ptr = stack_top_a;
                // Here frame_ptr == root_frame_ptr

                int argmask = 1;
477

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
478
                for (int i = 0; i < thunk->desc->arity; i++) {
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
479
480
481
482
483
484
485
                    push_a(thunk->_args[i]);
                    if(((FunEntry*) (thunk->desc))->strictness & argmask /*&& !arg->desc->hnf*/) 
                    { 
                        thunk->_args[i]->desc->eval(); 
                        thunk = stack_a[root_frame_ptr-1];
                    }
                    argmask <<= 1;
486
                }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
487
488
489

                expr = ((FunEntry*) thunk->desc)->body;
                continue;
490
            }
491
492
            case FT_PRIM1:
            case FT_PRIM2:
493
            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
494
495

                for (int i = 0; i < thunk->desc->arity; i++) {
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
496
497
498
                    push_a(thunk->_args[i]);
                    thunk->_args[i]->desc->eval();
                    thunk = stack_a[root_frame_ptr-1];
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
499
500
501
                }

                ((PrimEntry*) thunk->desc)->exec(root_frame_ptr);
502
503
                
                destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
504
                destroy_stack_frame_b(root_frame_ptr_b);                
505
506
507
508
509
510
511
512
513
514
                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
515
                destroy_stack_frame_b(root_frame_ptr_b);
516
517
                return;
            }            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
518
519
520
        }
        case CT_THUNK:
        {
521
            Thunk* thunk = &((ThunkEntry*) expr)->thunk;            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
            Thunk* dst = get_dst(root_frame_ptr);

            if(dst != NULL)
            {
                memcpy(dst, thunk, sizeof(Thunk));
                
                if(!instackb(dst))
                {
                    set_return(root_frame_ptr, thunk);
                }                
            }
            else
            {
                set_return(root_frame_ptr, thunk);
            }

538
            destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
539
            destroy_stack_frame_b(root_frame_ptr_b);
540
            return;               
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
541
        }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
542
543
        case CT_SELECT_LIT:
        {
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
544
            placeholder();
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
545
            exec(((SelectEntry*) expr)->expr, frame_ptr, stack_top_a);
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
546
            Thunk* lit = pop_a();
547
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
548
549
550
551
            bool handled = false;
            
            for (int i = 0; i < expr->nr_cases; i++) {
                SelectLitCaseEntry* caseEntry = &((SelectEntry*) expr)->cases[i];
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
552

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
553
554
                // NULL means "default", we accept it anyway
                if(caseEntry->lit != NULL)
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
555
                {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
556
557
558
                    assert(caseEntry->lit->thunk.desc != (Desc*) __INT__);
 
                    if(caseEntry->lit->thunk._int != lit->_int) continue;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
559
                }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
560
561
562
563
564
565
566
567
568
                
                // must be SC_DEFAULT now
                handled = true;
                expr = caseEntry->body;
                break;
            }
            
            if(handled) continue;
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
569
570
571
572
573
574
575
576
577
            if(((SelectEntry*) expr)->fallback != NULL)
            {
                stack_top_a -= ((SelectEntry*) expr)->fallback_nrargs;
                expr = ((SelectEntry*) expr)->fallback;
                
                continue;
            }
            
            abort("no match");
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
578
579
580
        }
        case CT_SELECT_ADT:
        {                        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
581
582
            SelectEntry* select = (SelectEntry*) expr;
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
583
            push_a(NULL);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
584
            exec(select->expr, frame_ptr, stack_top_a);
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
585
            Thunk* cons = pop_a();            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
586
587
            
            expr = select->bodies[((ADTEntry*)cons->desc)->idx];
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
588
589
590
591
592
593
594
595
596
            
            if(expr != NULL)
            {
                for (int i = 0; i < cons->desc->arity; i++) {
                    push_a(cons->_args[i]);
                }                  
                
                continue;
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
597
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
598
599
600
601
602
603
604
605
606
            if(select->fallback != NULL)
            {
                stack_top_a -= select->fallback_nrargs;
                expr = select->fallback;
                
                continue;
            }
            
            abort("no match");
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
607
608
609
        }
        case CT_IF:
        {        
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
610
            placeholder();
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
611
            exec(((IfEntry*) expr)->cond, frame_ptr, stack_top_a);
612
            Thunk* cond = pop_a();
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
613
               
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
614
615
616
617
618
619
620
621
            if (readB(cond)) {
                expr = ((IfEntry*) expr)->texpr;
                continue;                
            }
            else {
                expr = ((IfEntry*) expr)->fexpr;
                continue;     
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
622
        }
623
624
625
626
        }
    }
}

Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
void eval_hnf()
{
    return;
}

void eval_fun()
{
    Thunk* thunk = peek_a();    
    int frame_ptr = stack_top_a;
    int argmask = 1;

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

        if(((FunEntry*) (thunk->desc))->strictness & argmask)
        {
            thunk->_args[i]->desc->eval();
            thunk = stack_a[frame_ptr-1]; // refresh thunk ptr after eval 
645
646
        }
        
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
647
        argmask <<= 1;
648
649
    }

Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
650
651
    exec(((FunEntry*) thunk->desc)->body, frame_ptr, frame_ptr);
}
652

Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
653
654
655
656
657
658
659
660
661
void eval_prim()
{
    Thunk* thunk = peek_a();
    int frame_ptr = stack_top_a;

    for (int i = 0; i < thunk->desc->arity; i++) {
        push_a(thunk->_args[i]);        
        thunk->_args[i]->desc->eval();
        thunk = stack_a[frame_ptr-1]; // refresh thunk ptr after eval 
662
    }
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701

    ((PrimEntry*) thunk->desc)->exec(frame_ptr);        

    stack_top_a = frame_ptr;        
}

void eval_fwd_ptr()
{
    Thunk* thunk = pop_a();
    follow_thunk(thunk);
    push_a(thunk);
    thunk->desc->eval();
}   
   
void set_eval_fun(Desc* desc)
{
    if(desc == (Desc*) __FORWARD_PTR__)
    {
        desc->eval = eval_fwd_ptr;
        return;
    }
    
    switch(desc->type)
    {
        case FT_BOXED_LIT:
        case FT_RECORD:
        case FT_ADT:
        case FT_CAF:
        case FT_CAF_REDUCED:
        case FT_SLICE:
            desc->eval = eval_hnf;
            break;
        case FT_FUN:
            desc->eval = eval_fun;
            break;
        case FT_PRIM1:
        case FT_PRIM2:
            desc->eval = eval_prim;
            break;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
702
    }
703
}