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

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

10
struct Thunk* create_thunk(Code* expr, int frame_ptr, int root_frame_ptr)
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
11
{
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
12
    assert(expr != NULL);
13
14
15
16
    
    switch (expr->type) {
    case CT_LIT:
        destroy_stack_frame(root_frame_ptr);
17
        return updateT(NULL, &((LitEntry*) expr)->thunk);
18
19
    case CT_VAR:
        if (expr->local_type == VAR_LOCAL) {
20
            Thunk* thunk = forward_to(NULL, local(frame_ptr, ((VarEntry*) expr)->index));
21
            destroy_stack_frame(root_frame_ptr);                        
22
            return thunk;
23
24
        }else{
            destroy_stack_frame(root_frame_ptr);
25
            return updateF(NULL, get_slice(((VarEntry*) expr)->f, 0));
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
        }
    case CT_APP:
    {
        // TODO: check over application
        // TODO: enforce strictness in ADT/Record

        VarEntry* var = &((AppEntry*) expr)->var;

        if (var->base.local_type == VAR_LOCAL) 
        {
            // TODO: force

            Thunk* basethunk = eval(local(frame_ptr, var->index));

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

44
            Thunk* thunk = updateF(NULL, slice);
45
46
47
48
49
50
51
52

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

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

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

            destroy_stack_frame(root_frame_ptr);
58
            return thunk;
59
60
61
        }
        else
        {
62
            Thunk* thunk = updateF(NULL, get_slice(var->f, expr->nr_args));
63
64
65
66

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

            for (int i = 0; i < expr->nr_args; i++) {
67
                thunk->_args[i] = create_thunk(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a);
68
69
70
            }

            destroy_stack_frame(root_frame_ptr);
71
            return thunk;                    
72
73
74
75
76
        }
        break;
    }
    }
}
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
77

78
79
void exec(Code* expr, int frame_ptr, int root_frame_ptr)
{
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
80
81
    while(1)
    {
82
        assert(expr != NULL);
83
84
        assert(stack_top_a < STACK_SIZE_A);
        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
85
86
        switch (expr->type) {
        case CT_LIT:
87
            set_return(root_frame_ptr, updateT(get_dst(root_frame_ptr), &((LitEntry*) expr)->thunk));
88
            destroy_stack_frame(root_frame_ptr);
89
            return;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
90
91
        case CT_VAR:
            if (expr->local_type == VAR_LOCAL) {
92
                Thunk* thunk = forward_to(get_dst(root_frame_ptr), local(frame_ptr, ((VarEntry*) expr)->index));
93
94
95
                
                // Destroy stack frame before eval, it is not needed any more
                // Greatly reduces stack usage
96
97
                destroy_stack_frame(root_frame_ptr);
                set_return(root_frame_ptr, eval(thunk));
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
98
99
                return;
            }else{
100
101
102
                // Safe to destroy, the next call has no arguments
                destroy_stack_frame(root_frame_ptr);
                
103
104
                Desc* slice = get_slice(((VarEntry*) expr)->f, 0);
                
105
                if(slice->type == FT_FUN)
106
107
108
109
110
                {
                    expr = ((FunEntry*)slice)->body;
                    continue;
                }
                else
111
                {                    
112
                    set_return(root_frame_ptr, updateF(get_dst(root_frame_ptr), slice));
113
114
                    return;
                }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
115
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
116
        case CT_APP:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
117
        {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
118
119
            // TODO: check over application
            // TODO: enforce strictness in ADT/Record
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
120

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
121
            VarEntry* var = &((AppEntry*) expr)->var;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
122

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
123
124
            if (var->base.local_type == VAR_LOCAL) 
            {
125
126
                // TODO: force
                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
127
                Thunk* basethunk = eval(local(frame_ptr, var->index));
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
128

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
129
130
131
                Desc* slice =
                        get_slice(basethunk->desc->type == FT_SLICE ?
                                  ((SliceEntry*) basethunk->desc)->forward_ptr : basethunk->desc, basethunk->desc->arity + expr->nr_args);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
132

133
                Thunk* thunk = updateF(get_dst(root_frame_ptr), slice);
134

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
135
                assert(thunk->desc->arity == basethunk->desc->arity + expr->nr_args);            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
136

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
137
138
139
                for (int i = 0; i < basethunk->desc->arity; i++) {
                    thunk->_args[i] = basethunk->_args[i];
                }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
140
141

                for (int i = 0; i < expr->nr_args; i++) {
142
143
                    thunk->_args[basethunk->desc->arity + i] 
                            = create_thunk(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
144
                }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
145

146
147
                set_return(root_frame_ptr, thunk);
                destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
148
                return;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
149
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
150
151
152
            else
            {
                Desc* slice = get_slice(var->f, expr->nr_args);
153
                
154
                if (slice->type == FT_PRIM) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
155
156
157
                    Thunk args[expr->nr_args];

                    for (int i = 0; i < expr->nr_args; i++) {
158
                        push_a(&args[i]);                        
159
                        exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
160
161
                    }

162
                    ((PrimEntry*) slice)->exec(root_frame_ptr);
163
                    destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
164
165
                    return;
                }
166
                else if (slice->type == FT_FUN) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
167

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
168
                    int new_frame_ptr = stack_top_a;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
169

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
170
                    for (int i = 0; i < expr->nr_args; i++) {
171
                        
172
173
                        if(is_strict_fun_arg((FunEntry*) slice, i))
                        {
174
                            push_a(NULL);
175
176
177
178
                            exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a);
                        }
                        else
                        {
179
                          push_a(create_thunk(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a));  
180
                        }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
181
                    }
182
                    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
183
184
185
186
187
                    expr = ((FunEntry*) slice)->body;
                    frame_ptr = new_frame_ptr;
                    continue;
                }
                else {
188
                    Thunk* thunk = updateF(get_dst(root_frame_ptr), slice);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
189
190
191
192

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

                    for (int i = 0; i < expr->nr_args; i++) {
193
194
                        thunk->_args[i] 
                                = create_thunk(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
195
196
                    }
                    
197
198
                    set_return(root_frame_ptr, thunk);
                    destroy_stack_frame(root_frame_ptr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
199
                    return;                    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
200
                }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
201
202
            }
            break;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
203
        }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
204
205
        case CT_SELECT:
        {
206
            push_a(NULL);
207
            exec(((SelectEntry*) expr)->expr, frame_ptr, stack_top_a);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
208
            Thunk* pattern = eval(pop_a());
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
209

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
210
211
212
213
214
            bool handled = false;
            
            for (int i = 0; i < expr->nr_cases; i++) {
                SelectCaseEntry* caseEntry = &((SelectEntry*) expr)->cases[i];

215
216
                assert(pattern->desc->type == FT_ADT);
                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
217
218
219
220
221
222
223
                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]);
224
                    }                  
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
225
226
227
228
229
230
231
232
233
234
                }
                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
235
236
            }

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
237
238
239
240
241
242
243
244
245
246
            if(handled) continue;
            
            printf("Exec: no select cases matches");
            print(pattern, false);
            exit(-1);
        }
        case CT_IF:
        {        
            Thunk tmp;
            tmp.desc = (Desc*) __BOOL__;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
247

248
            push_a(&tmp);
249
            exec(((IfEntry*) expr)->cond, frame_ptr, stack_top_a);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
250
            Thunk* cond = eval(pop_a());
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
251

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
252
253
254
255
256
257
258
259
            if (readB(cond)) {
                expr = ((IfEntry*) expr)->texpr;
                continue;                
            }
            else {
                expr = ((IfEntry*) expr)->fexpr;
                continue;     
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
260
        }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
261
262
263
        default:
            printf("Exec: Unhandled CODE type");
            exit(-1);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
264
        }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
265
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
266
    }
267
}