prim.c 3.96 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);
12
13
    target->desc = (Desc*) __INT__;
    target->_int = readI(arg(2)) + readI(arg(1));
14
15
}

16
17
void __sub(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
18
19
    target->desc = (Desc*) __INT__;
    target->_int = readI(arg(2)) - readI(arg(1));
20
21
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
22
23
24
25
26
27
28
29
30
31
32
33
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));
}

34
35
void __gt(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
36
37
    target->desc = (Desc*) __BOOL__;
    target->_bool = readI(arg(2)) > readI(arg(1));
38
39
}

40
41
void __lt(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
42
43
    target->desc = (Desc*) __BOOL__;
    target->_bool = readI(arg(2)) < readI(arg(1));
44
45
}

Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
46
47
48
49
50
51
void __geC(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __BOOL__;
    target->_bool = readC(arg(2)) >= readC(arg(1));
}

52
53
void __eqI(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
54
55
    target->desc = (Desc*) __BOOL__;
    target->_bool = readI(arg(2)) == readI(arg(1));
56
57
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
58
59
60
61
62
63
void __neqI(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __BOOL__;
    target->_bool = readI(arg(2)) != readI(arg(1));
}

64
65
void __eqB(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
66
67
    target->desc = (Desc*) __BOOL__;
    target->_bool = readB(arg(2)) == readB(arg(1));
68
69
}

Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
70
71
72
73
74
75
void __eqC(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __CHAR__;
    target->_bool = readC(arg(2)) == readC(arg(1));
}

76
77
void __not(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
78
79
    target->desc = (Desc*) __BOOL__;
    target->_bool = !readB(arg(1));
80
81
}

Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
82
83
84
85
86
87
88
89
90
91
92
93
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
94
95
96
97
98
99
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
100
101
102
103
104
105
void __C2I(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __INT__;
    target->_int = readC(arg(1));
}

106
void add_prim(int arity, int strictness, char* name, void (*exec)(int)) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
107
108
109
110
111
112
    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);
113
    entry->base.type = arity == 1 ? FT_PRIM1 : FT_PRIM2;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
114
115
    entry->base.arity = arity;
    entry->strictness = strictness;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
116
117
    entry->base.thunk_size = thunk_size_f(arity);    
    entry->base.hnf = false;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
118
119
120
121
122
123
124
125
126
127
    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);
128
129
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
130
131
132
void init_prim() {
    add_prim(2, 3, "add", &__add);
    add_prim(2, 3, "sub", &__sub);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
133
134
    add_prim(2, 3, "mult", &__mult);
    add_prim(2, 3, "div", &__div);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
135
    add_prim(2, 3, "gt", &__gt);
Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
136
    add_prim(2, 3, "geC", &__geC);    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
137
138
    add_prim(2, 3, "lt", &__lt);
    add_prim(2, 3, "eqI", &__eqI);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
139
    add_prim(2, 3, "neqI", &__neqI);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
140
    add_prim(2, 3, "eqB", &__eqB);
Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
141
142
143
144
145
146
    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);    
147
}