code.c 29.4 KB
Newer Older
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
1
2
3
4
5
6
7
/*
 * Some properties: 
 * - get_dst(root_frame_ptr) can be NULL, but never a FORWARD_PTR (at least I hope so)
 *   
 *
 */

8
9
#include <stdio.h>
#include <stdlib.h>
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
10
#include <stdbool.h>
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
11
#include <strings.h>
12

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
13
#include "debug.h"
14
#include "code.h"
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
15
#include "mem.h"
16
#include "desc.h"
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
17
#include "gc.h"
18

19
20
// For compressing the source code a bit

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

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
23
#define follow_thunk(thunk) while (thunk->desc == (Desc*) __FORWARD_PTR__) thunk = thunk->_forward_ptr;
24
25
26

#define forward_thunk(thunk, frame_ptr) \
        Thunk* dst = get_dst(frame_ptr); \
27
        if(dst != NULL){ \
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
28
29
            dst->desc = (Desc*) __FORWARD_PTR__; \
            dst->_forward_ptr = thunk; \
30
        }
31

Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
32
#define placeholder() \
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
33
        push_a(alloc_b());
34

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
35
#define arg_from_code(descarg, arg) \
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
36
        if(((FunEntry*) (descarg))->boxing & argmask) \
37
        { \
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
38
            placeholder(); \
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
39
            exec(arg, frame_ptr, stack_top_a); \
40
        } \
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
41
42
43
44
45
        else if(((FunEntry*) (descarg))->strictness & argmask) \
        { \
            push_a(NULL); \
            exec(arg, frame_ptr, stack_top_a); \
        } \
46
47
        else \
        { \
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
48
            arg->create_thunk(arg, &stack_a[stack_top_a++], frame_ptr); \
49
50
51
        } \
        argmask <<= 1;

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
52
void create_thunk_app_static(Code* expr, Thunk** target, int frame_ptr)
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
53
{
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
54
    Thunk* thunk = (Thunk*) alloc_heap(((AppEntry*) expr)->f->thunk_size);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
55
    *target = thunk;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
56
    thunk->desc = ((AppEntry*) expr)->f;
57

58
    assert(thunk->desc->arity == expr->nr_args);
59

60
    for (int i = 0; i < expr->nr_args; i++) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
61
62
         ((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], &thunk->_args[i], frame_ptr);
    }                     
63
}
64

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
65
void create_thunk_app_dyn(Code* expr, Thunk** target, int frame_ptr)
66
{
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
67
    push_a(local(frame_ptr, ((AppEntry*)expr)->var.index));
68

Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
69
70
71
72
73
74
75
    int tmp = gc_enabled;
    gc_enabled = 0;
    peek_a()->desc->eval();
    gc_enabled = tmp;
    
    Thunk* basethunk = pop_a();
    
76
77
78
    Desc* slice =
            get_slice(basethunk->desc->type == FT_SLICE ?
                      ((SliceEntry*) basethunk->desc)->forward_ptr : basethunk->desc, basethunk->desc->arity + expr->nr_args);
79

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
80
81
    Thunk* thunk = (Thunk*) alloc_heap(slice->thunk_size);
    thunk->desc = slice;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
82
83
    *target = thunk;
    
84
85
86
87
88
    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++) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
89
90
        ((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], &thunk->_args[basethunk->desc->arity + i], frame_ptr);
    } 
91
92
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
93
void create_thunk_var(Code* expr, Thunk** target, int frame_ptr)
94
{
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
95
    *target = local(frame_ptr, ((VarEntry*) expr)->index);    
96
97
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
98
void create_thunk_var_unboxed(Code* expr, Thunk** target, int frame_ptr)
99
100
101
{
    Thunk* arg = local(frame_ptr, ((VarEntry*) expr)->index);

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
102
103
104
    if(instackb(arg))
    {
        // The likely case
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
105
106
107
        Thunk* newthunk = (Thunk*) alloc_heap(sizeof (Thunk));
        memcpy(newthunk, arg, sizeof(Thunk));
        *target = newthunk;        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
108
109
110
    }
    else
    {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
111
        *target = arg;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
112
    }
113
114
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
115
void create_thunk_thunk(Code* expr, Thunk** target, int frame_ptr)
116
{
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
117
    *target = &((ThunkEntry*) expr)->thunk;
118
119
}

120
void set_create_thunk_fun(Code* code)
121
{
122
123
    switch(code->type)
    {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
124
        case CT_APP_PRIM_FAST:
125
        case CT_APP_PRIM:            
126
        case CT_APP_FUN:
127
        case CT_APP_FUN_TR:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
128
129
        case CT_APP_FUN1:
        case CT_APP_FUN2:
130
131
132
133
134
135
136
        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:
137
        case CT_VAR_STRICT:            
138
139
            code->create_thunk = create_thunk_var;
            break;
140
141
        case CT_VAR_UNBOXED:
            code->create_thunk = create_thunk_var_unboxed;
142
143
144
145
            break;
        case CT_THUNK:
            code->create_thunk = create_thunk_thunk;
            break;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
146
147
        case CT_SELECT_ADT:
        case CT_SELECT_LIT:    
148
        case CT_SELECT_STR:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
149
        case CT_SELECT_REC:
150
        case CT_IF:
151
        case CT_LET:
152
153
154
            code->create_thunk = NULL;
            break;            
    }
155
156
}

157
158
159
160
161
162
163
// eval: frame_ptr, frame_ptr
// start: stack_top_a, stack_top_a
// otherwise: frame_ptr, stack_top_a

// frame_ptr: first arguments
// root_frame_ptr: place of the result

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
164
void exec(Code* expr, int frame_ptr, int root_frame_ptr)
165
{
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
166
167
    if(heap_curr > gc_trigger) gc();
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
168
169
    int root_frame_ptr_b = stack_top_b;    
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
170
    while(1)
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
171
    {        
172
        assert(expr != NULL);
173
        assert(stack_top_a < STACK_SIZE_A);
174
        assert(stack_top_b < STACK_SIZE_B);
175
176
177

        // TODO: check over application
        // TODO: enforce strictness in ADT/Record
178
        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
179
        switch (expr->type) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
180
        case CT_APP_PRIM_FAST:
181
        {                       
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
182
183
            // careful, "exec" may trigger garbage collection
            // read local variables only after the last exec            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
184
185
            switch(expr->arg_pattern)
            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
186
187
188
189
190
191
            case 0:
                placeholder();                       
                exec(((AppEntry*) expr)->args[0], frame_ptr, stack_top_a);
                placeholder();                        
                exec(((AppEntry*) expr)->args[1], frame_ptr, stack_top_a);
                break;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
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
            case 1:
                push_a(local(frame_ptr, ((VarEntry*) ((AppEntry*) expr)->args[0])->index));  
                push_a(&((ThunkEntry*) ((AppEntry*) expr)->args[1])->thunk);
                break;
            case 2:
                push_a(&((ThunkEntry*) ((AppEntry*) expr)->args[0])->thunk);            
                push_a(local(frame_ptr, ((VarEntry*) ((AppEntry*) expr)->args[1])->index));                        
                break;
            case 3:
                push_a(local(frame_ptr, ((VarEntry*) ((AppEntry*) expr)->args[0])->index));
                push_a(local(frame_ptr, ((VarEntry*) ((AppEntry*) expr)->args[1])->index));
                break;                    
            case 4:
                push_a(&((ThunkEntry*) ((AppEntry*) expr)->args[0])->thunk);            
                placeholder();                       
                exec(((AppEntry*) expr)->args[1], frame_ptr, stack_top_a);                    
                break;                    
            case 5:
                placeholder();                     
                exec(((AppEntry*) expr)->args[0], frame_ptr, stack_top_a);
                push_a(&((ThunkEntry*) ((AppEntry*) expr)->args[1])->thunk);                                
                break;                    
            case 6:
                push_a(local(frame_ptr, ((VarEntry*) ((AppEntry*) expr)->args[0])->index));
                placeholder();                       
                exec(((AppEntry*) expr)->args[1], frame_ptr, stack_top_a);
                break;                    
            case 7:
                placeholder();                        
                exec(((AppEntry*) expr)->args[0], frame_ptr, stack_top_a);
                push_a(local(frame_ptr, ((VarEntry*) ((AppEntry*) expr)->args[1])->index));
                break;                    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
224
225
226
227
228
229
230
            case 8:
                push_a(local(frame_ptr, ((VarEntry*) ((AppEntry*) expr)->args[0])->index));  
                break;
            case 9:
                push_a(&((ThunkEntry*) ((AppEntry*) expr)->args[0])->thunk);            
                break;
            default: // 10
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
231
                placeholder();                        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
232
233
                exec(((AppEntry*) expr)->args[0], frame_ptr, stack_top_a);
                break;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
234
            }
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
235
            
236
237
238
239
240
241
            if(get_dst(root_frame_ptr) == NULL && (((AppEntry*) expr)->f)->unboxable_return)
            {
                Thunk* tmp = (Thunk*) alloc_heap(sizeof(Thunk));
                set_return(root_frame_ptr, tmp);
            }
                        
242
            ((PrimEntry*) ((AppEntry*) expr)->f)->exec(root_frame_ptr);
243

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
244
245
246
247
            destroy_stack_frame(root_frame_ptr);
            destroy_stack_frame_b(root_frame_ptr_b);
            return;            
        }
248
249
        case CT_APP_PRIM:
        {           
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
250
251
252
253
254
255
256
257
258
259
260
261
262
            PrimEntry* desc = (PrimEntry*) ((AppEntry*) expr)->f;
            int argmask = 1;            
            
            for (int i = 0; i < desc->base.arity; i++) {

                if(desc->boxingMap & argmask)
                {
                    placeholder();
                }
                else
                {
                    push_a(NULL);
                }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
263
264
                
                exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
265
266
267
                argmask <<= 1;            
            }

268
269
270
271
272
273
            if(get_dst(root_frame_ptr) == NULL && (((AppEntry*) expr)->f)->unboxable_return)
            {
                Thunk* tmp = (Thunk*) alloc_heap(sizeof(Thunk));
                set_return(root_frame_ptr, tmp);
            }
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
274
275
276
277
278
            desc->exec(root_frame_ptr);
            
            destroy_stack_frame(root_frame_ptr);
            destroy_stack_frame_b(root_frame_ptr_b);
            return;            
279
        }        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
280
281
282
283
284
285
286
287
288
289
290
291
292
293
        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;
294
                        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
295
296
297
298
299
300
301
302
303
            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
304
        case CT_APP_FUN:
305
        {            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
306
            Desc* slice = ((AppEntry*) expr)->f;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
307
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
308
309
            int new_frame_ptr = stack_top_a;
            int argmask = 1;
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
310
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
311
312
            for (int i = 0; i < expr->nr_args; i++) {
                arg_from_code(slice, ((AppEntry*) expr)->args[i]);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
313
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
314

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
315
316
317
318
            expr = ((FunEntry*) slice)->body;
            frame_ptr = new_frame_ptr;
            continue;            
        }
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
        case CT_APP_FUN_TR:
        {            
            Desc* slice = ((AppEntry*) expr)->f;
            
            // TODO: B stack?
            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]);
            }

            memcpy(&stack_a[frame_ptr], &stack_a[new_frame_ptr], sizeof(void*) * expr->nr_args);
            stack_top_a = frame_ptr + expr->nr_args;
            
            expr = ((FunEntry*) slice)->body;
            continue;            
        }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
337
338
339
        case CT_APP_THUNK:
        {
            Desc* slice = ((AppEntry*) expr)->f;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
340
            Thunk* thunk = get_dst(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
341
342
            
            // no need to check for array length, thunks in HNF are never overwritten
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
343
344
            int newsize = slice->thunk_size;
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
345
346
347
348
            if (thunk == NULL)
            {
                thunk = (Thunk*) alloc_heap(newsize);
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
349
350
            else if (thunk->desc->thunk_size < newsize) 
            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
351
352
353
                Thunk* target = thunk;
                thunk = (Thunk*) alloc_heap(newsize);
                target->desc = (Desc*) __FORWARD_PTR__;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
354
                target->_forward_ptr = thunk;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
355
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
356

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
357
            set_return(root_frame_ptr, thunk);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
358
359
            thunk->desc = slice;
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
360
            assert(thunk->desc->arity == expr->nr_args);
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
361
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
362
363
            for (int i = 0; i < expr->nr_args; i++) {  
                ((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], &thunk->_args[i], frame_ptr);
364
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
365
366
367
368
369

            destroy_stack_frame(root_frame_ptr);
            destroy_stack_frame_b(root_frame_ptr_b);
            return;                                
        }
370
        case CT_APP_DYN:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
371
        {
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
372
373
374
            push_a(local(frame_ptr, ((AppEntry*)expr)->var.index));

            Thunk** bt = &peek_a();
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
375
            (*bt)->desc->eval(); // eval follows FORWARD_PTRs
376
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
377
            // The original fun arity if it is a slice
378
379
380
            Desc* baseDesc = (*bt)->desc->type == FT_SLICE ?
                              ((SliceEntry*) (*bt)->desc)->forward_ptr : (*bt)->desc;
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
381
382
            int totalArity = (*bt)->desc->arity + expr->nr_args;            
            int usedNrArgs = 0;
383
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
384
385
386
            Desc* slice;
            
            while(totalArity > baseDesc->arity)
387
            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
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
                // must be an FT_FUN
                                
                Desc* d = (*bt)->desc;
                
                int remainingNrArgs = totalArity - baseDesc->arity;        
                slice = get_slice(baseDesc, baseDesc->arity);
              
                int new_frame_ptr = stack_top_a;
                                                
                int argmask = 1;
                
                for (int i = 0; i < (*bt)->desc->arity; i++) {
                    push_a((*bt)->_args[i]);
                    if(((FunEntry*) (slice))->strictness & argmask) 
                    { 
                        (*bt)->_args[i]->desc->eval(); 
                    } 
                    argmask <<= 1;
                }

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

                // VERY important, otherwise we overwrite the original thunk with something else
                set_return(new_frame_ptr, NULL);
                exec(((FunEntry*) slice)->body, new_frame_ptr, new_frame_ptr);
                                
                totalArity = (*bt)->desc->arity + expr->nr_args - usedNrArgs;
418
                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
419
420
                baseDesc = (*bt)->desc->type == FT_SLICE ?
                              ((SliceEntry*) (*bt)->desc)->forward_ptr : (*bt)->desc;                
421
422
            }
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
423
            slice = get_slice(baseDesc, totalArity);
424
            
425
            switch(slice->type) {
426
            case FT_PRIM:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
427
            {
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
428
429
                for (int i = 0; i < (*bt)->desc->arity; i++) {
                    push_a((*bt)->_args[i]);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
430
                    (*bt)->_args[i]->desc->eval();
431
                }
432

Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
433
                int argmask = 1 << (*bt)->desc->arity;  
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
434

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
435
                for (int i = usedNrArgs; i < expr->nr_args; i++) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
436
437
438
439
440
441
442
443
444
445
                    
                    if(((PrimEntry*) slice)->boxingMap & argmask)
                    {
                        placeholder();
                    }
                    else
                    {
                        push_a(NULL);
                    }

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
446
                    exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
447
448
                    argmask <<= 1;            
                }
449

450
451
452
453
454
455
                if(get_dst(root_frame_ptr) == NULL && slice->unboxable_return)
                {
                    Thunk* tmp = (Thunk*) alloc_heap(sizeof(Thunk));
                    set_return(root_frame_ptr, tmp);
                }
                                
456
                ((PrimEntry*) slice)->exec(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
457
                destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
458
                destroy_stack_frame_b(root_frame_ptr_b);                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
459
460
                return;                    
            }
461
462
463
464
465
            case FT_FUN:
            {
                int new_frame_ptr = stack_top_a;                    
                int argmask = 1;

Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
466
467
                for (int i = 0; i < (*bt)->desc->arity; i++) {                    
                    push_a((*bt)->_args[i]);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
468
                    if(((FunEntry*) (slice))->strictness & argmask) 
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
469
470
471
472
                    { 
                        (*bt)->_args[i]->desc->eval(); 
                    } 
                    argmask <<= 1;
473
                }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
474
475
                
                for (int i = usedNrArgs; i < expr->nr_args; i++) {
476
477
                    arg_from_code(slice, ((AppEntry*) expr)->args[i]);    
                } 
478

479
480
481
482
483
484
485
486
                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
487
                Thunk* thunk = get_dst(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
488
                // no need to check for array length, thunks in HNF are never overwritten
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
489
490
                int newsize = slice->thunk_size;

Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
491
492
493
494
                if (thunk == NULL)
                {
                    thunk = (Thunk*) alloc_heap(newsize);
                }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
495
496
                else if (thunk->desc->thunk_size < newsize) 
                {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
497
498
499
                    Thunk* target = thunk;
                    thunk = (Thunk*) alloc_heap(newsize);
                    target->desc = (Desc*) __FORWARD_PTR__;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
500
                    target->_forward_ptr = thunk;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
501
                }
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
502
                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
503
                set_return(root_frame_ptr, thunk);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
504
505
                thunk->desc = slice;
                
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
506
                assert(thunk->desc->arity == (*bt)->desc->arity + expr->nr_args);            
507

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

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
510
                for (int i = usedNrArgs; i < expr->nr_args; i++) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
511
                    ((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], &thunk->_args[(*bt)->desc->arity + i], frame_ptr);
512
513
514
                }

                destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
515
                destroy_stack_frame_b(root_frame_ptr_b);
516
                return;                    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
517
            }
518
519
520
521
522
523
524
525
            case FT_BOXED_LIT:
                abort("Literal unexpected here");                
            case FT_CAF:
            case FT_CAF_REDUCED:
                not_implemented("CAF");
            }
        }
        case CT_VAR_STRICT:
526
        case CT_VAR_UNBOXED:            
527
        {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
528
            Thunk* arg = local(frame_ptr, ((VarEntry*) expr)->index);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
529
530
            follow_thunk(arg);
                        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
531
            assert(is_hnf(arg));
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
532

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
533
            // no need to check for array length, thunks in HNF are never overwritten
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
534
            if(get_dst(root_frame_ptr) != NULL && arg->desc->thunk_size <= sizeof(Thunk))
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
535
            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
536
                memcpy(get_dst(root_frame_ptr), arg, sizeof(Thunk));
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
537
538
539
540
            }
            else
            {
                forward_thunk(arg, root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
541
                set_return(root_frame_ptr, arg);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
542
543
            }
            
544
            destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
545
            destroy_stack_frame_b(root_frame_ptr_b);            
546
547
548
549
550
            return;                    
        }
        case CT_VAR:
        {
            Thunk* thunk = local(frame_ptr, ((VarEntry*) expr)->index);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
551

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
552
553
            assert(!instackb(thunk));
            
554
555
556
            follow_thunk(thunk);
            forward_thunk(thunk, root_frame_ptr);            
            set_return(root_frame_ptr, thunk);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
557

558
559
560
            switch(thunk->desc->type) {
            case FT_FUN:
            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
561
562
                // Destroy stack frame before eval, it is not needed any more
                // Greatly reduces stack usage
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
563
564
                destroy_stack_frame(root_frame_ptr);
                destroy_stack_frame_b(root_frame_ptr_b);  
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
565
                                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
566
567
568
569
                frame_ptr = stack_top_a;
                // Here frame_ptr == root_frame_ptr

                int argmask = 1;
570

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
571
                for (int i = 0; i < thunk->desc->arity; i++) {
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
572
                    push_a(thunk->_args[i]);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
573
                    if(((FunEntry*) (thunk->desc))->strictness & argmask) 
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
574
575
576
577
578
                    { 
                        thunk->_args[i]->desc->eval(); 
                        thunk = stack_a[root_frame_ptr-1];
                    }
                    argmask <<= 1;
579
                }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
580
581
582

                expr = ((FunEntry*) thunk->desc)->body;
                continue;
583
            }
584
            case FT_PRIM:
585
            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
586
587
                int argmask = 1;
                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
588
                for (int i = 0; i < thunk->desc->arity; i++) {
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
589
                    push_a(thunk->_args[i]);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
590
                    if(((PrimEntry*)thunk->desc)->boxingMap & argmask) thunk->_args[i]->desc->eval();
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
591
                    thunk = stack_a[root_frame_ptr-1];
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
592
593
                }

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
594
595
                // get_dst(root_frame_ptr) cannot be NULL, no need to check
                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
596
                ((PrimEntry*) thunk->desc)->exec(root_frame_ptr);
597
598
                
                destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
599
                destroy_stack_frame_b(root_frame_ptr_b);                
600
601
602
603
604
605
606
607
608
609
                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
610
                destroy_stack_frame_b(root_frame_ptr_b);
611
612
                return;
            }            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
613
614
615
        }
        case CT_THUNK:
        {
616
            Thunk* thunk = &((ThunkEntry*) expr)->thunk;            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
            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);
            }

633
            destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
634
            destroy_stack_frame_b(root_frame_ptr_b);
635
            return;               
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
636
        }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
637
638
        case CT_SELECT_LIT:
        {
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
639
            placeholder();
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
640
            exec(((SelectEntry*) expr)->expr, frame_ptr, stack_top_a);
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
641
            Thunk* lit = pop_a();
642
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
643
644
645
646
            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
647

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
648
649
                // NULL means "default", we accept it anyway
                if(caseEntry->lit != NULL)
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
650
                {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
651
                    assert(caseEntry->lit->thunk.desc == (Desc*) __INT__ ||
652
653
654
                           caseEntry->lit->thunk.desc == (Desc*) __BOOL__ || 
                           caseEntry->lit->thunk.desc == (Desc*) __CHAR__);

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
655
                    if(caseEntry->lit->thunk._int != lit->_int) continue;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
656
                }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
657
                
658
659
660
661
662
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
                // must be SC_DEFAULT now
                handled = true;
                expr = caseEntry->body;
                break;
            }
            
            if(handled) continue;
            
            if(((SelectEntry*) expr)->fallback != NULL)
            {
                stack_top_a -= ((SelectEntry*) expr)->fallback_nrargs;
                expr = ((SelectEntry*) expr)->fallback;
                
                continue;
            }
            
            abort("no match");
        }
        case CT_SELECT_STR:
        {                        
            SelectEntry* select = (SelectEntry*) expr;
            
            push_a(NULL);
            exec(select->expr, frame_ptr, stack_top_a);
            Thunk* str = pop_a();
            
            int length;
            char* chars;

            if(str->desc == (Desc*) __STRING_PTR__)
            {
                chars = str->_string_ptr->chars;
                length = str->_string_ptr->length;
            }
            else
            {
                chars = str->_array._chars;
                length = str->_array.length;
            }
            
            bool handled = false;
            
            for (int i = 0; i < expr->nr_cases; i++) {
                SelectLitCaseEntry* caseEntry = &((SelectEntry*) expr)->cases[i];

                // NULL means "default", we accept it anyway
                if(caseEntry->lit != NULL)
                {
                    assert(caseEntry->lit->thunk.desc == (Desc*) __STRING_PTR__); 

                    int eq = length == caseEntry->lit->thunk._string_ptr->length;
                    int i = 0;
                    while(i<length && eq)
                    {
                        eq = chars[i] == caseEntry->lit->thunk._string_ptr->chars[i];
                        i++;
                    }
                    
                    if(!eq) continue;
                }
                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
719
720
721
722
723
724
725
726
                // must be SC_DEFAULT now
                handled = true;
                expr = caseEntry->body;
                break;
            }
            
            if(handled) continue;
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
727
728
729
730
731
732
733
734
735
            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
736
737
738
        }
        case CT_SELECT_ADT:
        {                        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
739
740
            SelectEntry* select = (SelectEntry*) expr;
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
741
            push_a(NULL);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
742
            exec(select->expr, frame_ptr, stack_top_a);
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
743
            Thunk* cons = pop_a();            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
744
745
            
            expr = select->bodies[((ADTEntry*)cons->desc)->idx];
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
746
747
748
749
750
751
752
753
754
            
            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
755
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
756
757
758
759
760
761
762
763
764
            if(select->fallback != NULL)
            {
                stack_top_a -= select->fallback_nrargs;
                expr = select->fallback;
                
                continue;
            }
            
            abort("no match");
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
765
        }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
        case CT_SELECT_REC:
        {                        
            SelectEntry* select = (SelectEntry*) expr;
            
            push_a(NULL);
            exec(select->expr, frame_ptr, stack_top_a);
            Thunk* cons = pop_a();            
            
            expr = select->bodies[0];
            
            if(expr != NULL)
            {
                for (int i = 0; i < cons->desc->arity; i++) {
                    push_a(cons->_args[i]);
                }                  
                
                continue;
            }
            
            // This cannot happen in record field selection            
            abort("no match");
        }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
788
789
        case CT_IF:
        {        
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
790
            placeholder();
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
791
            exec(((IfEntry*) expr)->cond, frame_ptr, stack_top_a);
792
            Thunk* cond = pop_a();
793
            
794
            // safe to do it before read as nothing can overwrite it in between
795
796
            stack_top_b--;
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
797
798
799
800
801
802
803
804
            if (readB(cond)) {
                expr = ((IfEntry*) expr)->texpr;
                continue;                
            }
            else {
                expr = ((IfEntry*) expr)->fexpr;
                continue;     
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
805
        }
806
807
808
809
810
811
812
813
814
        case CT_LET:
        {
            for(int i=0; i<expr->nr_bindings; i++)
            {
                LetBindingEntry* binding = ((LetEntry*) expr)->bindings[i];
                
                // Normal
                if(binding->type == 0)
                {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
815
                    binding->body->create_thunk(binding->body, &stack_a[stack_top_a++], frame_ptr);
816
817
818
819
820
821
822
823
824
825
826
                }
                else // strict (including unboxed))
                {
                    push_a(NULL);
                    exec(binding->body, frame_ptr, stack_top_a);
                }
            }
            
            expr = ((LetEntry*) expr)->body;
            continue;
        }
827
828
829
830
        }
    }
}

Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
831
832
833
834
835
836
837
838
839
840
void eval_hnf()
{
    return;
}

void eval_fun()
{
    Thunk* thunk = peek_a();    
    int frame_ptr = stack_top_a;
    int argmask = 1;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
841
    
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
842
843
844
845
846
847
848
    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 
849
850
        }
        
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
851
        argmask <<= 1;
852
853
    }

Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
854
855
    exec(((FunEntry*) thunk->desc)->body, frame_ptr, frame_ptr);
}
856

Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
857
858
859
860
861
862
863
864
865
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 
866
    }
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901

    ((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;
902
        case FT_PRIM:
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
903
904
            desc->eval = eval_prim;
            break;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
905
    }
906
}