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

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

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

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

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

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

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


46
struct Thunk* create_thunk(Code* expr, int frame_ptr)
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
47
{
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
48
    assert(expr != NULL);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
49
        
50
51
52
    // TODO: check over application
    // TODO: enforce strictness in ADT/Record
        
53
54
55
    switch (expr->type) {
    case CT_APP:
    {
56
        Thunk* thunk = createF(((AppEntry*) expr)->f);
57

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

60
61
62
        for (int i = 0; i < expr->nr_args; i++) {
            thunk->_args[i] = create_thunk(((AppEntry*) expr)->args[i], frame_ptr);
        }
63

64
65
66
67
68
        return thunk;                    
    }
    case CT_APP_DYN:
    {
        Thunk* basethunk = local(frame_ptr, ((AppEntry*)expr)->var.index);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
69
        if(!basethunk->desc->hnf) basethunk = eval(basethunk);
70

71
72
73
        Desc* slice =
                get_slice(basethunk->desc->type == FT_SLICE ?
                          ((SliceEntry*) basethunk->desc)->forward_ptr : basethunk->desc, basethunk->desc->arity + expr->nr_args);
74

75
        Thunk* thunk = createF(slice);
76

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

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
79
80
        memcpy(&thunk->_args, &basethunk->_args, sizeof(Thunk*) * basethunk->desc->arity);
        
81
82
83
        for (int i = 0; i < expr->nr_args; i++) {
            thunk->_args[basethunk->desc->arity + i] 
                    = create_thunk(((AppEntry*) expr)->args[i], frame_ptr);
84
        }
85
86
87

        return thunk;
    }
88
    case CT_VAR:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
89
        return local(frame_ptr, ((VarEntry*) expr)->index);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
90
91
92
93
94
95
96
97
98
99
100
101
102
    case CT_VAR_STRICT: 
    {
        Thunk* arg = local(frame_ptr, ((VarEntry*) expr)->index);
        
        if(arg->desc->unboxable)
        {
            return createT(arg);
        }
        else
        {
            return arg;
        }
    }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
103
    case CT_THUNK:
104
105
106
107
108
        return &((ThunkEntry*) expr)->thunk;
    case CT_SELECT:
    case CT_IF:
        // Only here to avoid intervalum check at switch 
        abort("Unexpected code type");
109
110
    }
}
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
111

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
112
void exec(Code* expr, int frame_ptr, int root_frame_ptr)
113
{
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
114
115
    int root_frame_ptr_b = stack_top_b;    
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
116
117
    while(1)
    {
118
        assert(expr != NULL);
119
        assert(stack_top_a < STACK_SIZE_A);
120
121
122

        // TODO: check over application
        // TODO: enforce strictness in ADT/Record
123
        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
124
125
        switch (expr->type) {
        case CT_APP:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
126
        {
127
            Desc* slice = ((AppEntry*) expr)->f;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
128

129
130
            switch (slice->type) {
            case FT_PRIM:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
131
            {
132
                for (int i = 0; i < expr->nr_args; i++) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
133
                    push_a(alloc_b());                        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
134
                    exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
135
                }
136
137
138

                ((PrimEntry*) slice)->exec(root_frame_ptr);
                destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
139
                destroy_stack_frame_b(root_frame_ptr_b);
140
141
142
143
144
145
146
147
148
                return;
            }
            case FT_FUN:
            {
                int new_frame_ptr = stack_top_a;
                int argmask = 1;

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

                expr = ((FunEntry*) slice)->body;
                frame_ptr = new_frame_ptr;
                continue;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
154
            }
155
156
157
            case FT_SLICE:
            case FT_ADT:
            case FT_RECORD:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
158
            {
159
                Thunk* thunk = updateF(get_dst(root_frame_ptr), slice);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
160

161
                assert(thunk->desc->arity == expr->nr_args);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
162

163
164
165
                for (int i = 0; i < expr->nr_args; i++) {
                    thunk->_args[i] 
                            = create_thunk(((AppEntry*) expr)->args[i], frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
166
                }
167
168
169

                set_return(root_frame_ptr, thunk);
                destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
170
                destroy_stack_frame_b(root_frame_ptr_b);
171
                return;                    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
172
            }
173
174
175
176
177
178
179
180
            case FT_BOXED_LIT:
                abort("Literal unexpected here");
            case FT_CAF:
            case FT_CAF_REDUCED:
                not_implemented("CAF");
            }
        }                        
        case CT_APP_DYN:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
181
        {
182
            Thunk* basethunk = local(frame_ptr, ((AppEntry*)expr)->var.index);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
183
            if(!basethunk->desc->hnf) basethunk = eval(basethunk);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
184

185
186
187
188
189
190
            Desc* slice =
                    get_slice(basethunk->desc->type == FT_SLICE ?
                              ((SliceEntry*) basethunk->desc)->forward_ptr : basethunk->desc, basethunk->desc->arity + expr->nr_args);

            switch(slice->type) {
            case FT_PRIM:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
191
            {
192
                for (int i = 0; i < basethunk->desc->arity; i++) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
193
194
195
196
197
198
199
200
                    if(basethunk->_args[i]->desc->hnf)
                    {
                        push_a(basethunk->_args[i]);
                    }
                    else
                    {
                        push_a(eval(basethunk->_args[i]));   
                    }
201
                }
202

203
                for (int i = 0; i < expr->nr_args; i++) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
204
                    push_a(alloc_b());
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
205
                    exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a);
206
207
208
                } 

                ((PrimEntry*) slice)->exec(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
209
                destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
210
                destroy_stack_frame_b(root_frame_ptr_b);                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
211
212
                return;                    
            }
213
214
215
216
217
218
219
220
221
222
223
224
            case FT_FUN:
            {
                int new_frame_ptr = stack_top_a;                    
                int argmask = 1;

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

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

226
227
228
229
230
231
232
233
234
235
236
237
                expr = ((FunEntry*) slice)->body;
                frame_ptr = new_frame_ptr;
                continue;                    
            }
            case FT_SLICE:
            case FT_ADT:
            case FT_RECORD:
            {
                Thunk* thunk = updateF(get_dst(root_frame_ptr), slice);

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

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
238
                memcpy(&thunk->_args, &basethunk->_args, sizeof(Thunk*) * basethunk->desc->arity);
239
240
241
242
243
244
245
246

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

                set_return(root_frame_ptr, thunk);
                destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
247
                destroy_stack_frame_b(root_frame_ptr_b);
248
                return;                    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
249
            }
250
251
252
253
254
255
256
257
258
            case FT_BOXED_LIT:
                abort("Literal unexpected here");                
            case FT_CAF:
            case FT_CAF_REDUCED:
                not_implemented("CAF");
            }
        }
        case CT_VAR_STRICT:
        {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
259
            Thunk* arg = local(frame_ptr, ((VarEntry*) expr)->index);
260
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
261
            assert(is_hnf(arg));
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
262

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
263
264
265
266
267
268
269
270
271
272
            if(arg->desc->unboxable)
            {
                arg = updateT(get_dst(root_frame_ptr), arg);
            }
            else
            {
                forward_thunk(arg, root_frame_ptr);
            }
            
            set_return(root_frame_ptr, arg);
273
            destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
274
            destroy_stack_frame_b(root_frame_ptr_b);            
275
276
277
278
279
            return;                    
        }
        case CT_VAR:
        {
            Thunk* thunk = local(frame_ptr, ((VarEntry*) expr)->index);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
280

281
282
283
            follow_thunk(thunk);
            forward_thunk(thunk, root_frame_ptr);            
            set_return(root_frame_ptr, thunk);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
284

285
286
287
            switch(thunk->desc->type) {
            case FT_FUN:
            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
288
289
                // Destroy stack frame before eval, it is not needed any more
                // Greatly reduces stack usage
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
290
291
                destroy_stack_frame(root_frame_ptr);
                destroy_stack_frame_b(root_frame_ptr_b);  
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
292
293
294
295
                frame_ptr = stack_top_a;
                // Here frame_ptr == root_frame_ptr

                int argmask = 1;
296

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
297
                for (int i = 0; i < thunk->desc->arity; i++) {
298
                    arg_from_thunk(thunk->desc, thunk->_args[i]);
299
                }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
300
301
302

                expr = ((FunEntry*) thunk->desc)->body;
                continue;
303
            }
304
305
            case FT_PRIM:
            {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
306
307
308
309
310
311

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

                ((PrimEntry*) thunk->desc)->exec(root_frame_ptr);
312
313
                
                destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
314
                destroy_stack_frame_b(root_frame_ptr_b);
315
316
317
318
319
320
321
322
323
324
                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
325
                destroy_stack_frame_b(root_frame_ptr_b);
326
327
                return;
            }            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
328
329
330
        }
        case CT_THUNK:
        {
331
332
            Thunk* thunk = &((ThunkEntry*) expr)->thunk;            
            forward_thunk(thunk, root_frame_ptr);            
333
            set_return(root_frame_ptr, thunk);
334
            destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
335
            destroy_stack_frame_b(root_frame_ptr_b);
336
            return;               
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
337
        }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
338
339
        case CT_SELECT:
        {
340
341
342
            Thunk* pattern = alloc_b();
            pattern->desc = (Desc*) __STACK_PLACEHOLDER__;
            push_a(pattern);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
343
            exec(((SelectEntry*) expr)->expr, frame_ptr, stack_top_a);
344
            pattern = pop_a();
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
345

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
346
347
            assert(is_hnf(pattern));
            assert(pattern->desc->type == FT_ADT);            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
348

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
349
350
351
352
            bool handled = false;
            
            for (int i = 0; i < expr->nr_cases; i++) {
                SelectCaseEntry* caseEntry = &((SelectEntry*) expr)->cases[i];
353
                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
354
355
356
357
358
359
360
                if (caseEntry->type == SC_CONS) {
                    // Pattern match
                    if ((Desc*) caseEntry->cons != pattern->desc) continue;

                    // Put the constructor arguments to the stack if matches
                    for (int i = 0; i < pattern->desc->arity; i++) {
                        push_a(pattern->_args[i]);
361
                    }                  
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
362
363
364
365
366
367
368
369
370
371
                }
                else if (caseEntry->type == SC_LIT) {
                    printf("Exec: Unhandled entry type in CT_SELECT (SC_LIT)");
                    exit(-1);
                }
                
                // must be SC_DEFAULT now
                handled = true;
                expr = caseEntry->body;
                break;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
372
373
            }

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
374
375
376
377
378
379
380
381
            if(handled) continue;
            
            printf("Exec: no select cases matches");
            print(pattern, false);
            exit(-1);
        }
        case CT_IF:
        {        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
382
            push_a(alloc_b());
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
383
            exec(((IfEntry*) expr)->cond, frame_ptr, stack_top_a);
384
            Thunk* cond = pop_a();
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
385
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
386
387
388
389
390
391
392
393
            if (readB(cond)) {
                expr = ((IfEntry*) expr)->texpr;
                continue;                
            }
            else {
                expr = ((IfEntry*) expr)->fexpr;
                continue;     
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
394
        }
395
396
397
398
399
400
        }
    }
}

struct Thunk* eval(Thunk* thunk) {
    assert(thunk != NULL);    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
401
    
402
403
404
405
406
407
408
409
410
411
412
413
414
    follow_thunk(thunk);
    
    switch(thunk->desc->type) {
    case FT_FUN:
    {
        push_a(thunk);
        int frame_ptr = stack_top_a;
        int argmask = 1;
        
        for (int i = 0; i < thunk->desc->arity; i++) {
            arg_from_thunk(thunk->desc, thunk->_args[i]);
        }
        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
415
        exec(((FunEntry*) thunk->desc)->body, frame_ptr, frame_ptr);
416
417
418
419
420
421
422
423
424
        thunk = pop_a();
        return thunk;
    }
    case FT_PRIM:
    {
        push_a(thunk);
        int frame_ptr = stack_top_a;

        for (int i = 0; i < thunk->desc->arity; i++) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
425
426
427
428
429
430
431
432
            if(thunk->_args[i]->desc->hnf)
            {
                push_a(thunk->_args[i]);
            }
            else
            {
                push_a(eval(thunk->_args[i]));
            }
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
        }

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