code.c 5.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 "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
    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:
        {
35
            Thunk* var = arg(frame_ptr, ((VarEntry*) expr)->index);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
36
37
38
39
            return forward_to(target, var);
        }
        case VAR_LOCAL:
        {
40
            Thunk* var = local(frame_ptr, ((VarEntry*) expr)->index + 1);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
41
42
43
44
45
            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
            if (force && slice->type == FT_PRIM) {
                Thunk args[expr->nr_args];
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
61
				
62
                int old_top = stack_top_a;				
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
63
				
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
64
                for (int i = 0; i < expr->nr_args; i++) {
65
                    push_a(exec(((AppEntry*) expr)->args[i], frame_ptr, &args[i], true));
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
66
67
68
69
                }

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

70
                stack_top_a = old_top;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
71
72
73
            }
            else if (force && slice->type == FT_FUN) {

74
75
76
77
                int old_top = stack_top_a;

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

80
                thunk = exec(((FunEntry*) slice)->body, stack_top_a - 1, target, true);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
81

82
                stack_top_a = old_top;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
83
84
85
            }
            else {
                thunk = updateF(target, slice);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
86
87
88
                
                assert(thunk->desc->arity == expr->nr_args);
                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
89
90
91
92
93
94
95
96
                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:
        {
97
            Thunk* basethunk = eval(arg(frame_ptr, var->index));
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
98
99
100
101

            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
102
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
103
104
            thunk = updateF(target, slice);

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
105
106
            assert(thunk->desc->arity == basethunk->desc->arity + expr->nr_args);            
            
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
            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
125
        Thunk* p = pattern;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
126
127
128
129
130
131
132
133
134
135
136
137
        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++) {
138
                    push_a(pattern->_args[i]);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
139
140
141
142
                }

                // Fall through on purpose
            case SC_DEFAULT:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
143
                return exec(caseEntry->body, frame_ptr, target, force);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
144
145
146
147
148
149
150
151
152
153
154
            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
155
156
157
    {        
        Thunk tmp;
        tmp.desc = (Desc*) __BOOL__;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
158

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
159
        Thunk* cond = exec(((IfEntry*) expr)->cond, frame_ptr, &tmp, true);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
160
161
162
163
164
165
166
167
168
169
170
171
172
        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);
    }
173
}