prim.c 4.21 KB
Newer Older
1
2
3
4
#include <string.h>

#include "prim.h"
#include "desc.h"
5
#include "thunk.h"
6
7
#include "mem.h"

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
8
9
#define arg(idx) stack_a[stack_top_a - idx]

10
11
void __add(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
12
13
14
15
16
17
18
19
20

    while(target->desc == (Desc*) __FORWARD_PTR__) target = target->_forward_ptr;
    
    if(target->desc->type == FT_ADT)
    {
        int i =0;
    }


21
22
    target->desc = (Desc*) __INT__;
    target->_int = readI(arg(2)) + readI(arg(1));
23
24
}

25
26
void __sub(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
27
28
    target->desc = (Desc*) __INT__;
    target->_int = readI(arg(2)) - readI(arg(1));
29
30
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
31
32
33
34
35
36
37
38
39
40
41
42
void __mult(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __INT__;
    target->_int = readI(arg(2)) * readI(arg(1));
}

void __div(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __INT__;
    target->_int = readI(arg(2)) / readI(arg(1));
}

43
44
void __gt(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
45
46
    target->desc = (Desc*) __BOOL__;
    target->_bool = readI(arg(2)) > readI(arg(1));
47
48
}

49
50
void __lt(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
51
52
    target->desc = (Desc*) __BOOL__;
    target->_bool = readI(arg(2)) < readI(arg(1));
53
54
}

Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
55
56
57
58
59
60
void __geC(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __BOOL__;
    target->_bool = readC(arg(2)) >= readC(arg(1));
}

61
62
void __eqI(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
Laszlo Domoszlai's avatar
GC    
Laszlo Domoszlai committed
63
64
65
66
67
68
    
    if(target->desc != NULL && target->desc->type == FT_ADT)
    {
        int i =0;
    }
    
69
70
    target->desc = (Desc*) __BOOL__;
    target->_bool = readI(arg(2)) == readI(arg(1));
71
72
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
73
74
75
76
77
78
void __neqI(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __BOOL__;
    target->_bool = readI(arg(2)) != readI(arg(1));
}

79
80
void __eqB(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
81
82
    target->desc = (Desc*) __BOOL__;
    target->_bool = readB(arg(2)) == readB(arg(1));
83
84
}

Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
85
86
87
88
89
90
void __eqC(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __CHAR__;
    target->_bool = readC(arg(2)) == readC(arg(1));
}

91
92
void __not(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
93
94
    target->desc = (Desc*) __BOOL__;
    target->_bool = !readB(arg(1));
95
96
}

Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
97
98
99
100
101
102
103
104
105
106
107
108
void __and(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __BOOL__;
    target->_bool = readB(arg(2)) && readB(arg(1));
}

void __or(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __BOOL__;
    target->_bool = readB(arg(2)) || readB(arg(1));
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
109
110
111
112
113
114
void __mod(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __INT__;
    target->_int = readB(arg(2)) % readB(arg(1));
}

Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
115
116
117
118
119
120
void __C2I(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __INT__;
    target->_int = readC(arg(1));
}

121
void add_prim(int arity, int strictness, char* name, void (*exec)(int)) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
122
123
124
125
126
127
    int nameLength = strlen(name);

    // before the PrimEntry there are "arity" number of SliceEntries
    SliceEntry* entry_base = (SliceEntry*) alloc_desc(sizeof (SliceEntry) * arity + sizeof (PrimEntry) + nameLength + 1);

    PrimEntry* entry = (PrimEntry*) (entry_base + arity);
128
    entry->base.type = arity == 1 ? FT_PRIM1 : FT_PRIM2;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
129
130
    entry->base.arity = arity;
    entry->strictness = strictness;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
131
132
    entry->base.thunk_size = thunk_size_f(arity);    
    entry->base.hnf = false;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
133
134
135
136
137
138
139
140
141
142
    entry->exec = exec;

    // TODO: should it be copied at all?
    memcpy(entry->name, name, nameLength);
    entry->name[nameLength] = '\0';

    // generate slices. avoid function call if arity is zero
    if (arity > 0) gen_slices(entry_base, (Desc*) entry, arity);

    add_desc(entry->name, (Desc*) entry);
143
144
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
145
146
147
void init_prim() {
    add_prim(2, 3, "add", &__add);
    add_prim(2, 3, "sub", &__sub);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
148
149
    add_prim(2, 3, "mult", &__mult);
    add_prim(2, 3, "div", &__div);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
150
    add_prim(2, 3, "gt", &__gt);
Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
151
    add_prim(2, 3, "geC", &__geC);    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
152
153
    add_prim(2, 3, "lt", &__lt);
    add_prim(2, 3, "eqI", &__eqI);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
154
    add_prim(2, 3, "neqI", &__neqI);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
155
    add_prim(2, 3, "eqB", &__eqB);
Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
156
157
158
159
160
161
    add_prim(2, 3, "eqC", &__eqC);
    add_prim(1, 1, "not", &__not);
    add_prim(2, 3, "and", &__and); 
    add_prim(2, 3, "or", &__or); 
    add_prim(2, 3, "mod", &__mod);
    add_prim(1, 1, "C2I", &__C2I);    
162
}