code.c 5.36 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 <assert.h>
5
6

#include "code.h"
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
7
#include "desc.h"
8
#include "thunk.h"
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
9
#include "mem.h"
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
10
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
11
12
13
struct Thunk*
exec(Code* expr, int frame_ptr, Thunk* target, bool force)
{
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
14
15
    assert(expr != NULL);

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    switch (expr->type) {
    case CT_LIT:
        switch (expr->local_type) {
        case LIT_INT:
            return updateI(target, ((LitEntry*) expr)->_int);
        case LIT_BOOL:
            return updateB(target, ((LitEntry*) expr)->_bool);
        default:
            printf("Exec: Unhandled LIT type");
            exit(-1);
        }
        break;
    case CT_VAR:
        switch (expr->local_type) {
        case VAR_FN:
            return updateF(target, get_slice(((VarEntry*) expr)->f, 0));

        case VAR_ARG:
        {
            Thunk* var = stack[frame_ptr - ((VarEntry*) expr)->index];
            return forward_to(target, var);
        }
        case VAR_LOCAL:
        {
            Thunk* var = stack[frame_ptr + ((VarEntry*) expr)->index + 1];
            return forward_to(target, var);
        }
        }
        break;
    case CT_APP:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
46
        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
47
48
49
50
51
52
53
54
55
56
57
        // TODO: check over application
        // TODO: enforce strictness in ADT/Record

        VarEntry* var;
        var = ((AppEntry*) expr)->var;
        Thunk* thunk;

        switch (var->base.local_type) {
        case VAR_FN:
        {
            Desc* slice = get_slice(var->f, expr->nr_args);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
58
                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
            if (force && slice->type == FT_PRIM) {
                Thunk args[expr->nr_args];
                for (int i = 0; i < expr->nr_args; i++) {
                    exec(((AppEntry*) expr)->args[i], frame_ptr, &args[i], true);
                }

                int old_top = stack_top;

                for (int i = 0; i < expr->nr_args; i++) {
                    pushs(&args[i]);
                }

                thunk = ((PrimEntry*) slice)->exec(target);

                stack_top = old_top;
            }
            else if (force && slice->type == FT_FUN) {
                Thunk * args[expr->nr_args];
                for (int i = 0; i < expr->nr_args; i++) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
78
                    args[i] = exec(((AppEntry*) expr)->args[i], frame_ptr, NULL, is_strict_fun_arg((FunEntry*) slice, i));
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
79
80
81
82
                }

                int old_top = stack_top;

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
83
                for (int i = 0; i < expr->nr_args; i++) {   
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
84
85
86
                    pushs(args[i]);
                }

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
87
                thunk = exec(((FunEntry*) slice)->body, stack_top - 1, target, true);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
88
89
90
91
92

                stack_top = old_top;
            }
            else {
                thunk = updateF(target, slice);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
93
94
95
                
                assert(thunk->desc->arity == expr->nr_args);
                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
96
97
98
99
100
101
102
103
104
105
106
107
108
                for (int i = 0; i < expr->nr_args; i++) {
                    thunk->_args[i] = exec(((AppEntry*) expr)->args[i], frame_ptr, NULL, false);
                }
            }
            return thunk;
        }
        case VAR_ARG:
        {
            Thunk* basethunk = eval(stack[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);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
109
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
110
111
            thunk = updateF(target, slice);

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
112
113
            assert(thunk->desc->arity == basethunk->desc->arity + expr->nr_args);            
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
            for (int i = 0; i < basethunk->desc->arity; i++) {
                thunk->_args[i] = basethunk->_args[i];
            }

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

            return thunk;
        }
        default:
            printf("Exec: Unhandled VAR type in CT_APP");
            exit(-1);
        }
        break;
    case CT_SELECT:
    {
        Thunk* pattern = exec(((SelectEntry*) expr)->expr, frame_ptr, NULL, true);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
132
        Thunk* p = pattern;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
        pattern = eval(pattern);

        for (int i = 0; i < expr->nr_cases; i++) {
            SelectCaseEntry* caseEntry = &((SelectEntry*) expr)->cases[i];

            switch (caseEntry->type) {
            case 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++) {
                    pushs(pattern->_args[i]);
                }

                // Fall through on purpose
            case SC_DEFAULT:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
150
                return exec(caseEntry->body, frame_ptr, target, force);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
151
152
153
154
155
156
157
158
159
160
161
            default:
                printf("Exec: Unhandled entry type in CT_SELECT");
                exit(-1);
            }
        }

        printf("Exec: no select cases matches");
        print(pattern, false);
        exit(-1);
    }
    case CT_IF:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
162
163
164
    {        
        Thunk tmp;
        tmp.desc = (Desc*) __BOOL__;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
165

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
166
        Thunk* cond = exec(((IfEntry*) expr)->cond, frame_ptr, &tmp, true);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
167
168
169
170
171
172
173
174
175
176
177
178
179
        cond = eval(cond);

        if (readB(cond)) {
            return exec(((IfEntry*) expr)->texpr, frame_ptr, target, force);
        }
        else {
            return exec(((IfEntry*) expr)->fexpr, frame_ptr, target, force);
        }
    }
    default:
        printf("Exec: Unhandled CODE type");
        exit(-1);
    }
180
}