code.c 6.12 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"

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
10
void exec(Code* expr, int frame_ptr, int root_frame_ptr, Thunk* target, bool force)
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
11
{
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
12
13
    assert(expr != NULL);

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
14
15
16
17
    while(1)
    {
        switch (expr->type) {
        case CT_LIT:
18
19
            prepare_return(root_frame_ptr, 
                    updateT(target, &((LitEntry*) expr)->thunk));
20
            return;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
21
22
        case CT_VAR:
            if (expr->local_type == VAR_LOCAL) {
23
                Thunk* thunk = forward_to(target, local(frame_ptr, ((VarEntry*) expr)->index));
24
25
                if(force) thunk = eval(thunk);
                prepare_return(root_frame_ptr, thunk);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
26
27
                return;
            }else{
28
29
30
31
32
33
34
35
                Desc* slice = get_slice(((VarEntry*) expr)->f, 0);
                
                if(force &&  slice->type == FT_FUN)
                {
                    expr = ((FunEntry*)slice)->body;
                    continue;
                }
                else
36
37
38
                {                    
                    prepare_return(root_frame_ptr, 
                            updateF(target, get_slice(((VarEntry*) expr)->f, 0)));
39
40
                    return;
                }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
41
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
42
        case CT_APP:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
43
        {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
44
45
            // TODO: check over application
            // TODO: enforce strictness in ADT/Record
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
46

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

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
49
50
51
            if (var->base.local_type == VAR_LOCAL) 
            {
                Thunk* basethunk = eval(local(frame_ptr, var->index));
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
52

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
53
54
55
                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
56

57
                Thunk* thunk = updateF(target, slice);
58

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

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
61
62
63
                for (int i = 0; i < basethunk->desc->arity; i++) {
                    thunk->_args[i] = basethunk->_args[i];
                }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
64
65

                for (int i = 0; i < expr->nr_args; i++) {
66
                    push_a(NULL);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
67
68
                    exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a, NULL, false);
                    thunk->_args[basethunk->desc->arity + i] = pop_a();
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
69
                }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
70

71
                prepare_return(root_frame_ptr, thunk);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
72
                return;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
73
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
74
75
76
            else
            {
                Desc* slice = get_slice(var->f, expr->nr_args);
77
                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
78
79
80
81
                if (force && slice->type == FT_PRIM) {
                    Thunk args[expr->nr_args];

                    for (int i = 0; i < expr->nr_args; i++) {
82
                        push_a(&args[i]);                        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
83
84
85
                        exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a, &args[i], true);
                    }

86
87
                    prepare_return(root_frame_ptr, 
                            ((PrimEntry*) slice)->exec(target));
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
88
89
90
91
                    
                    return;
                }
                else if (force && slice->type == FT_FUN) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
92

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
93
                    int new_frame_ptr = stack_top_a;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
94

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
95
                    for (int i = 0; i < expr->nr_args; i++) {
96
                        push_a(NULL);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
97
98
                        exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a, NULL, is_strict_fun_arg((FunEntry*) slice, i));
                    }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
99

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
100
101
102
103
104
                    expr = ((FunEntry*) slice)->body;
                    frame_ptr = new_frame_ptr;
                    continue;
                }
                else {
105
                    Thunk* thunk = updateF(target, slice);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
106
107
108
109

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

                    for (int i = 0; i < expr->nr_args; i++) {
110
                        push_a(NULL);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
111
112
113
114
                        exec(((AppEntry*) expr)->args[i], frame_ptr, stack_top_a, NULL, false);
                        thunk->_args[i] = pop_a();
                    }
                    
115
                    prepare_return(root_frame_ptr, thunk);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
116
                    return;                    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
117
                }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
118
119
            }
            break;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
120
        }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
121
122
        case CT_SELECT:
        {
123
            push_a(NULL);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
124
125
            exec(((SelectEntry*) expr)->expr, frame_ptr, stack_top_a, NULL, true);
            Thunk* pattern = eval(pop_a());
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
126

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
127
128
129
130
131
            bool handled = false;
            
            for (int i = 0; i < expr->nr_cases; i++) {
                SelectCaseEntry* caseEntry = &((SelectEntry*) expr)->cases[i];

132
133
                assert(pattern->desc->type == FT_ADT);
                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
134
135
136
137
138
139
140
                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]);
141
                    }                  
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
142
143
144
145
146
147
148
149
150
151
                }
                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
152
153
            }

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
154
155
156
157
158
159
160
161
162
163
            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
164

165
            push_a(&tmp);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
166
167
            exec(((IfEntry*) expr)->cond, frame_ptr, stack_top_a, &tmp, true);
            Thunk* cond = eval(pop_a());
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
168

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
169
170
171
172
173
174
175
176
            if (readB(cond)) {
                expr = ((IfEntry*) expr)->texpr;
                continue;                
            }
            else {
                expr = ((IfEntry*) expr)->fexpr;
                continue;     
            }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
177
        }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
178
179
180
        default:
            printf("Exec: Unhandled CODE type");
            exit(-1);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
181
        }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
182
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
183
    }
184
}