code.c 28 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])

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
16
#define follow_thunk(thunk) while (thunk->desc == (Desc*) __FORWARD_PTR__) thunk = thunk->_forward_ptr;
17
18
19

#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 \
        { \
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
41
            arg->create_thunk(arg, &stack_a[stack_top_a++], frame_ptr); \
42
43
44
        } \
        argmask <<= 1;

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

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

53
    for (int i = 0; i < expr->nr_args; i++) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
54
55
         ((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], &thunk->_args[i], frame_ptr);
    }                     
56
}
57

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
58
void create_thunk_app_dyn(Code* expr, Thunk** target, int frame_ptr)
59
{
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
60
    push_a(local(frame_ptr, ((AppEntry*)expr)->var.index));
61

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

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
73
74
    Thunk* thunk = (Thunk*) alloc_heap(slice->thunk_size);
    thunk->desc = slice;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
75
76
    *target = thunk;
    
77
78
79
80
81
    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
82
83
        ((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], &thunk->_args[basethunk->desc->arity + i], frame_ptr);
    } 
84
85
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
86
void create_thunk_var(Code* expr, Thunk** target, int frame_ptr)
87
{
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
88
    *target = local(frame_ptr, ((VarEntry*) expr)->index);    
89
90
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
91
void create_thunk_var_unboxed(Code* expr, Thunk** target, int frame_ptr)
92
93
94
{
    Thunk* arg = local(frame_ptr, ((VarEntry*) expr)->index);

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
95
96
97
    if(instackb(arg))
    {
        // The likely case
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
98
99
100
        Thunk* newthunk = (Thunk*) alloc_heap(sizeof (Thunk));
        memcpy(newthunk, arg, sizeof(Thunk));
        *target = newthunk;        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
101
102
103
    }
    else
    {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
104
        *target = arg;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
105
    }
106
107
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
108
void create_thunk_thunk(Code* expr, Thunk** target, int frame_ptr)
109
{
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
110
    *target = &((ThunkEntry*) expr)->thunk;
111
112
}

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

151
152
153
154
155
156
157
// 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
158
void exec(Code* expr, int frame_ptr, int root_frame_ptr)
159
{
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
160
161
    if(heap_curr > gc_trigger) gc();
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
162
163
    int root_frame_ptr_b = stack_top_b;    
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
164
    while(1)
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
165
    {        
166
        assert(expr != NULL);
167
        assert(stack_top_a < STACK_SIZE_A);
168
        assert(stack_top_b < STACK_SIZE_B);
169
170
171

        // TODO: check over application
        // TODO: enforce strictness in ADT/Record
172
        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
173
        switch (expr->type) {
174
        case CT_APP_PRIM1:
175
        {            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
176
177
            switch(expr->arg_pattern)
            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
178
179
180
181
182
183
184
185
186
187
            case 1:
                push_a(local(frame_ptr, ((VarEntry*) ((AppEntry*) expr)->args[0])->index));  
                break;
            case 2:
                push_a(&((ThunkEntry*) ((AppEntry*) expr)->args[0])->thunk);            
                break;
            default:
                placeholder();                        
                exec(((AppEntry*) expr)->args[0], frame_ptr, stack_top_a);
                break;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
188
            }
189
190
191
192
193
194
195
         
            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);
            }
                        
196
197
198
199
200
201
202
            ((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_PRIM2:
203
        {                       
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
204
205
            // careful, "exec" may trigger garbage collection
            // read local variables only after the last exec            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
206
207
            switch(expr->arg_pattern)
            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
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
            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;                    
            default:
                placeholder();                       
                exec(((AppEntry*) expr)->args[0], frame_ptr, stack_top_a);
                placeholder();                        
                exec(((AppEntry*) expr)->args[1], frame_ptr, stack_top_a);                    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
245
            }
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
246
            
247
248
249
250
251
252
            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);
            }
                        
253
            ((PrimEntry*) ((AppEntry*) expr)->f)->exec(root_frame_ptr);
254

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
255
256
257
258
            destroy_stack_frame(root_frame_ptr);
            destroy_stack_frame_b(root_frame_ptr_b);
            return;            
        }
259
260
        case CT_APP_PRIM:
        {           
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
            PrimEntry* desc = (PrimEntry*) ((AppEntry*) expr)->f;
            int argmask = 1;            
            
            for (int i = 0; i < desc->base.arity; i++) {

                if(desc->boxingMap & argmask)
                {
                    placeholder();
                    exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a);
                }
                else
                {
                    push_a(NULL);
                    exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a);
                }

                argmask <<= 1;            
            }

280
281
282
283
284
285
            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
286
287
288
289
290
            desc->exec(root_frame_ptr);
            
            destroy_stack_frame(root_frame_ptr);
            destroy_stack_frame_b(root_frame_ptr_b);
            return;            
291
        }        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
292
293
294
295
296
297
298
299
300
301
302
303
304
305
        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;
306
                        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
307
308
309
310
311
312
313
314
315
            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
316
        case CT_APP_FUN:
317
        {            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
318
            Desc* slice = ((AppEntry*) expr)->f;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
319
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
320
321
            int new_frame_ptr = stack_top_a;
            int argmask = 1;
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
322
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
323
324
            for (int i = 0; i < expr->nr_args; i++) {
                arg_from_code(slice, ((AppEntry*) expr)->args[i]);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
325
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
326

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
327
328
329
330
            expr = ((FunEntry*) slice)->body;
            frame_ptr = new_frame_ptr;
            continue;            
        }
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
        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
349
350
351
        case CT_APP_THUNK:
        {
            Desc* slice = ((AppEntry*) expr)->f;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
352
            Thunk* thunk = get_dst(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
353
354
            
            // no need to check for array length, thunks in HNF are never overwritten
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
355
356
            int newsize = slice->thunk_size;
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
357
358
359
360
361
            if (thunk == NULL)
            {
                thunk = (Thunk*) alloc_heap(newsize);
                set_return(root_frame_ptr, thunk);
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
362
            else if (thunk->desc->thunk_size < newsize) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
363
364
365
                Thunk* target = thunk;
                thunk = (Thunk*) alloc_heap(newsize);
                target->desc = (Desc*) __FORWARD_PTR__;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
366
367
                target->_forward_ptr = thunk;
                set_return(root_frame_ptr, thunk);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
368
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
369

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
370
371
            thunk->desc = slice;
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
372
            assert(thunk->desc->arity == expr->nr_args);
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
373
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
374
375
            for (int i = 0; i < expr->nr_args; i++) {  
                ((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], &thunk->_args[i], frame_ptr);
376
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
377
378
379
380
381

            destroy_stack_frame(root_frame_ptr);
            destroy_stack_frame_b(root_frame_ptr_b);
            return;                                
        }
382
        case CT_APP_DYN:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
383
        {
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
384
385
386
387
388
            push_a(local(frame_ptr, ((AppEntry*)expr)->var.index));

            Thunk** bt = &peek_a();
            (*bt)->desc->eval();
            
389
390
391
392
393
394
395
396
397
398
399
            
            Desc* baseDesc = (*bt)->desc->type == FT_SLICE ?
                              ((SliceEntry*) (*bt)->desc)->forward_ptr : (*bt)->desc;
            
            int newArity = (*bt)->desc->arity + expr->nr_args;
            
            if(newArity > baseDesc->arity)
            {
                
            }
            
400
            Desc* slice =
401
402
                    get_slice(baseDesc, newArity);
            
403
            switch(slice->type) {
404
            case FT_PRIM:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
405
            {
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
406
407
408
409
                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();
                    
410
                }
411

412
                for (int i = 0; i < expr->nr_args; i++) {
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
413
                    placeholder();
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
414
                    exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a);
415
416
                } 

417
418
419
420
421
422
                if(get_dst(root_frame_ptr) == NULL && slice->unboxable_return)
                {
                    Thunk* tmp = (Thunk*) alloc_heap(sizeof(Thunk));
                    set_return(root_frame_ptr, tmp);
                }
                                
423
                ((PrimEntry*) slice)->exec(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
424
                destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
425
                destroy_stack_frame_b(root_frame_ptr_b);                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
426
427
                return;                    
            }
428
429
430
431
432
            case FT_FUN:
            {
                int new_frame_ptr = stack_top_a;                    
                int argmask = 1;

Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
433
434
435
436
437
438
439
                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;
440
441
442
443
444
                }

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

446
447
448
449
450
451
452
453
                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
454
                Thunk* thunk = get_dst(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
455
                // no need to check for array length, thunks in HNF are never overwritten
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
456
457
                int newsize = slice->thunk_size;

Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
458
459
460
461
462
463
                if (thunk == NULL)
                {
                    thunk = (Thunk*) alloc_heap(newsize);
                    set_return(root_frame_ptr, thunk);
                }
                else if (thunk->desc->thunk_size < newsize) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
464
465
466
                    Thunk* target = thunk;
                    thunk = (Thunk*) alloc_heap(newsize);
                    target->desc = (Desc*) __FORWARD_PTR__;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
467
468
                    target->_forward_ptr = thunk;
                    set_return(root_frame_ptr, thunk);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
469
                }
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
470
                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
471
472
                thunk->desc = slice;
                
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
473
                assert(thunk->desc->arity == (*bt)->desc->arity + expr->nr_args);            
474

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

                for (int i = 0; i < expr->nr_args; i++) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
478
                    ((AppEntry*) expr)->args[i]->create_thunk(((AppEntry*) expr)->args[i], &thunk->_args[(*bt)->desc->arity + i], frame_ptr);
479
480
481
                }

                destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
482
                destroy_stack_frame_b(root_frame_ptr_b);
483
                return;                    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
484
            }
485
486
487
488
489
490
491
492
            case FT_BOXED_LIT:
                abort("Literal unexpected here");                
            case FT_CAF:
            case FT_CAF_REDUCED:
                not_implemented("CAF");
            }
        }
        case CT_VAR_STRICT:
493
        case CT_VAR_UNBOXED:            
494
        {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
495
            Thunk* arg = local(frame_ptr, ((VarEntry*) expr)->index);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
496
497
            follow_thunk(arg);
                        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
498
            assert(is_hnf(arg));
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
499

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
500
            // no need to check for array length, thunks in HNF are never overwritten
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
501
            if(get_dst(root_frame_ptr) != NULL && arg->desc->thunk_size <= sizeof(Thunk))
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
502
            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
503
                memcpy(get_dst(root_frame_ptr), arg, sizeof(Thunk));
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
504
505
506
507
            }
            else
            {
                forward_thunk(arg, root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
508
                set_return(root_frame_ptr, arg);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
509
510
            }
            
511
            destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
512
            destroy_stack_frame_b(root_frame_ptr_b);            
513
514
515
516
517
            return;                    
        }
        case CT_VAR:
        {
            Thunk* thunk = local(frame_ptr, ((VarEntry*) expr)->index);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
518

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
519
520
            assert(!instackb(thunk));
            
521
522
523
            follow_thunk(thunk);
            forward_thunk(thunk, root_frame_ptr);            
            set_return(root_frame_ptr, thunk);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
524

525
526
527
            switch(thunk->desc->type) {
            case FT_FUN:
            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
528
529
                // Destroy stack frame before eval, it is not needed any more
                // Greatly reduces stack usage
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
530
531
                destroy_stack_frame(root_frame_ptr);
                destroy_stack_frame_b(root_frame_ptr_b);  
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
532
                                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
533
534
535
536
                frame_ptr = stack_top_a;
                // Here frame_ptr == root_frame_ptr

                int argmask = 1;
537

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
538
                for (int i = 0; i < thunk->desc->arity; i++) {
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
539
540
541
542
543
544
545
                    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;
546
                }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
547
548
549

                expr = ((FunEntry*) thunk->desc)->body;
                continue;
550
            }
551
            case FT_PRIM:
552
            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
553
554

                for (int i = 0; i < thunk->desc->arity; i++) {
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
555
556
557
                    push_a(thunk->_args[i]);
                    thunk->_args[i]->desc->eval();
                    thunk = stack_a[root_frame_ptr-1];
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
558
559
560
                }

                ((PrimEntry*) thunk->desc)->exec(root_frame_ptr);
561
562
                
                destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
563
                destroy_stack_frame_b(root_frame_ptr_b);                
564
565
566
567
568
569
570
571
572
573
                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
574
                destroy_stack_frame_b(root_frame_ptr_b);
575
576
                return;
            }            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
577
578
579
        }
        case CT_THUNK:
        {
580
            Thunk* thunk = &((ThunkEntry*) expr)->thunk;            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
            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);
            }

597
            destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
598
            destroy_stack_frame_b(root_frame_ptr_b);
599
            return;               
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
600
        }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
601
602
        case CT_SELECT_LIT:
        {
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
603
            placeholder();
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
604
            exec(((SelectEntry*) expr)->expr, frame_ptr, stack_top_a);
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
605
            Thunk* lit = pop_a();
606
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
607
608
609
610
            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
611

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
612
613
                // NULL means "default", we accept it anyway
                if(caseEntry->lit != NULL)
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
614
                {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
615
                    assert(caseEntry->lit->thunk.desc == (Desc*) __INT__ ||
616
617
618
                           caseEntry->lit->thunk.desc == (Desc*) __BOOL__ || 
                           caseEntry->lit->thunk.desc == (Desc*) __CHAR__);

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
619
                    if(caseEntry->lit->thunk._int != lit->_int) continue;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
620
                }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
621
                
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
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
                // 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
683
684
685
686
687
688
689
690
                // must be SC_DEFAULT now
                handled = true;
                expr = caseEntry->body;
                break;
            }
            
            if(handled) continue;
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
691
692
693
694
695
696
697
698
699
            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
700
701
702
        }
        case CT_SELECT_ADT:
        {                        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
703
704
            SelectEntry* select = (SelectEntry*) expr;
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
705
            push_a(NULL);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
706
            exec(select->expr, frame_ptr, stack_top_a);
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
707
            Thunk* cons = pop_a();            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
708
709
            
            expr = select->bodies[((ADTEntry*)cons->desc)->idx];
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
710
711
712
713
714
715
716
717
718
            
            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
719
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
720
721
722
723
724
725
726
727
728
            if(select->fallback != NULL)
            {
                stack_top_a -= select->fallback_nrargs;
                expr = select->fallback;
                
                continue;
            }
            
            abort("no match");
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
729
        }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
        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
752
753
        case CT_IF:
        {        
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
754
            placeholder();
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
755
            exec(((IfEntry*) expr)->cond, frame_ptr, stack_top_a);
756
            Thunk* cond = pop_a();
757
            
758
            // safe to do it before read as nothing can overwrite it in between
759
760
            stack_top_b--;
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
761
762
763
764
765
766
767
768
            if (readB(cond)) {
                expr = ((IfEntry*) expr)->texpr;
                continue;                
            }
            else {
                expr = ((IfEntry*) expr)->fexpr;
                continue;     
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
769
        }
770
771
772
773
774
775
776
777
778
        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
779
                    binding->body->create_thunk(binding->body, &stack_a[stack_top_a++], frame_ptr);
780
781
782
783
784
785
786
787
788
789
790
                }
                else // strict (including unboxed))
                {
                    push_a(NULL);
                    exec(binding->body, frame_ptr, stack_top_a);
                }
            }
            
            expr = ((LetEntry*) expr)->body;
            continue;
        }
791
792
793
794
        }
    }
}

Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
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 
813
814
        }
        
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
815
        argmask <<= 1;
816
817
    }

Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
818
819
    exec(((FunEntry*) thunk->desc)->body, frame_ptr, frame_ptr);
}
820

Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
821
822
823
824
825
826
827
828
829
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 
830
    }
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865

    ((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;
866
        case FT_PRIM:
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
867
868
            desc->eval = eval_prim;
            break;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
869
    }
870
}