code.c 4.82 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
    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:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
29
30
31
32
        if (expr->local_type == VAR_LOCAL) {
            Thunk* var = local(frame_ptr, ((VarEntry*) expr)->index);
            return forward_to(target, var);            
        }else{
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
33
34
35
36
            return updateF(target, get_slice(((VarEntry*) expr)->f, 0));
        }
        break;
    case CT_APP:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
37
        
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
38
39
40
41
42
43
44
        // TODO: check over application
        // TODO: enforce strictness in ADT/Record

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

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
        if (var->base.local_type == VAR_LOCAL) {
            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);
            
            thunk = updateF(target, slice);

            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++) {
                thunk->_args[basethunk->desc->arity + i] = exec(((AppEntry*) expr)->args[i], frame_ptr, NULL, false);
            }

            return thunk;            
        }
        else
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
67
68
        {
            Desc* slice = get_slice(var->f, expr->nr_args);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
69
                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
70
71
            if (force && slice->type == FT_PRIM) {
                Thunk args[expr->nr_args];
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
72
				
73
                int old_top = stack_top_a;				
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
74
				
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
75
                for (int i = 0; i < expr->nr_args; i++) {
76
                    push_a(exec(((AppEntry*) expr)->args[i], frame_ptr, &args[i], true));
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
77
78
79
80
                }

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

81
                stack_top_a = old_top;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
82
83
84
            }
            else if (force && slice->type == FT_FUN) {

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
85
                int new_frame_ptr = stack_top_a;
86
87
88

                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
89
90
                }

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
91
                thunk = exec(((FunEntry*) slice)->body, new_frame_ptr, target, true);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
92

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
93
                stack_top_a = new_frame_ptr;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
94
95
96
            }
            else {
                thunk = updateF(target, slice);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
97
98
99
                
                assert(thunk->desc->arity == expr->nr_args);
                
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
                for (int i = 0; i < expr->nr_args; i++) {
                    thunk->_args[i] = exec(((AppEntry*) expr)->args[i], frame_ptr, NULL, false);
                }
            }
            return thunk;
        }
        break;
    case CT_SELECT:
    {
        Thunk* pattern = exec(((SelectEntry*) expr)->expr, frame_ptr, NULL, true);
        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++) {
122
                    push_a(pattern->_args[i]);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
123
124
125
126
                }

                // Fall through on purpose
            case SC_DEFAULT:
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
127
                return exec(caseEntry->body, frame_ptr, target, force);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
128
129
130
131
132
133
134
135
136
137
138
            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
139
140
141
    {        
        Thunk tmp;
        tmp.desc = (Desc*) __BOOL__;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
142

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
143
        Thunk* cond = exec(((IfEntry*) expr)->cond, frame_ptr, &tmp, true);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
144
145
146
147
148
149
150
151
152
153
154
155
156
        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);
    }
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
157
158
    
    return NULL;
159
}