prim.c 9.4 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]

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
10
void __addI(int dst_idx) {
11
    Thunk* target = get_dst(dst_idx);
12
13
    target->desc = (Desc*) __INT__;
    target->_int = readI(arg(2)) + readI(arg(1));
14
15
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
16
void __subI(int dst_idx) {
17
    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
void __multI(int dst_idx) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
23
24
25
26
27
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __INT__;
    target->_int = readI(arg(2)) * readI(arg(1));
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
28
void __divI(int dst_idx) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
29
30
31
32
33
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __INT__;
    target->_int = readI(arg(2)) / readI(arg(1));
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
34
void __gtI(int dst_idx) {
35
    Thunk* target = get_dst(dst_idx);
36
    target->desc = (Desc*) __BOOL__;
37
    target->_int = readI(arg(2)) > readI(arg(1));
38
39
}

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

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
46
void __ltI(int dst_idx) {
47
    Thunk* target = get_dst(dst_idx);
48
    target->desc = (Desc*) __BOOL__;
49
    target->_int = readI(arg(2)) < readI(arg(1));
50
51
}

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

58
59
void __eqI(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
60
    target->desc = (Desc*) __BOOL__;
61
    target->_int = readI(arg(2)) == readI(arg(1));
62
63
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
64
65
66
void __neqI(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __BOOL__;
67
    target->_int = readI(arg(2)) != readI(arg(1));
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
68
69
}

70
71
void __eqB(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
72
    target->desc = (Desc*) __BOOL__;
73
    target->_int = readB(arg(2)) == readB(arg(1));
74
75
}

Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
76
77
void __eqC(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
78
    target->desc = (Desc*) __BOOL__;
79
    target->_int = readC(arg(2)) == readC(arg(1));
Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
80
81
}

82
83
void __not(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
84
    target->desc = (Desc*) __BOOL__;
85
    target->_int = !readB(arg(1));
86
87
}

Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
88
89
90
void __and(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __BOOL__;
91
    target->_int = readB(arg(2)) && readB(arg(1));
Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
92
93
94
95
96
}

void __or(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __BOOL__;
97
    target->_int = readB(arg(2)) || readB(arg(1));
Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
98
99
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
100
101
102
103
104
105
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
106
107
108
109
110
111
void __C2I(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __INT__;
    target->_int = readC(arg(1));
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
void __string_size(int dst_idx)
{
    Thunk* target = get_dst(dst_idx);
    Thunk* arg = arg(1);
    
    int length;
    
    if(arg->desc == (Desc*) __STRING_PTR__)
    {
        length = arg->_string_ptr->length;
    }
    else
    {
        length = arg->_array.length;
    }
    
    target->desc = (Desc*) __INT__;
    target->_int = length;
}

void __string_select(int dst_idx)
{
    Thunk* target = get_dst(dst_idx);
    Thunk* str = arg(2);
    Thunk* pos = arg(1);
    
    char* chars;
    
    if(str->desc == (Desc*) __STRING_PTR__)
    {
        chars = str->_string_ptr->chars;
    }
    else
    {
        chars = str->_array._chars;
    }
    
    target->desc = (Desc*) __CHAR__;
150
    target->_int = (char) chars[pos->_int];
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
}

Thunk* string_create(Thunk* target, int len)
{
    int newsize = sizeof (Desc) + sizeof (Array) + len;
    
    if(target == NULL)
    {
        target = (Thunk*) alloc_heap(newsize);
    }
    else if(target->desc->thunk_size < newsize)
    {
        Thunk* tmp = (Thunk*) alloc_heap(newsize);
        target->desc = (Desc*) __FORWARD_PTR__;
        target->_forward_ptr = tmp;
        target = tmp;
    }

    target->desc = (Desc*) __ARRAY__;
    target->_array.is_string = true;
    target->_array.is_boxed = true;
    target->_array.bytes_per_elem = 1;
    target->_array.length = len;
    
    return target;
}

void __string_create1(int dst_idx)
{
    Thunk* target = get_dst(dst_idx);
    Thunk* len = arg(1);
    set_return(dst_idx, string_create(target, len->_int));   
}

void __string_create2(int dst_idx)
{
    Thunk* target = get_dst(dst_idx);
    Thunk* len = arg(2);
    Thunk* ch = arg(1);
    
    target = string_create(target, len->_int);
    for(int i=0; i<len->_int; i++)
    {
194
        target->_array._chars[i] = (char) ch->_int;    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
    }
    
    set_return(dst_idx, target);   
}

void __string_update(int dst_idx)
{
    Thunk* target = get_dst(dst_idx);
    Thunk* str = arg(3);
    Thunk* idx = arg(2);
    Thunk* ch = arg(1);
        
    int length;
    char* chars;
    
    if(str->desc == (Desc*) __STRING_PTR__)
    {
        chars = str->_string_ptr->chars;
        length = str->_string_ptr->length;
    }
    else
    {
        chars = str->_array._chars;
        length = str->_array.length;
    }
    
    target = string_create(target, length);
    memcpy(target->_array._chars, chars, length);

224
    target->_array._chars[idx->_int] = (char) ch->_int;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
    
    set_return(dst_idx, target);   
}

void __string_slice(int dst_idx)
{
    Thunk* target = get_dst(dst_idx);
    Thunk* str = arg(3);
    Thunk* idx1 = arg(2);
    Thunk* idx2 = arg(1);
        
    char* chars;
    
    if(str->desc == (Desc*) __STRING_PTR__)
    {
        chars = str->_string_ptr->chars;
    }
    else
    {
        chars = str->_array._chars;
    }

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
247
    int length = idx2->_int - idx1->_int;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
    
    target = string_create(target, length);
    memcpy(target->_array._chars, chars + idx1->_int, length);
    
    set_return(dst_idx, target);       
}

void __string_append(int dst_idx)
{
    Thunk* target = get_dst(dst_idx);
    Thunk* str1 = arg(2);
    Thunk* str2 = arg(1);
        
    int length1;
    char* chars1;
    
    if(str1->desc == (Desc*) __STRING_PTR__)
    {
        chars1 = str1->_string_ptr->chars;
        length1 = str1->_string_ptr->length;
    }
    else
    {
        chars1 = str1->_array._chars;
        length1 = str1->_array.length;
    }

    int length2;
    char* chars2;
    
    if(str2->desc == (Desc*) __STRING_PTR__)
    {
        chars2 = str2->_string_ptr->chars;
        length2 = str2->_string_ptr->length;
    }
    else
    {
        chars2 = str2->_array._chars;
        length2 = str2->_array.length;
    }
            
    target = string_create(target, length1 + length2);
    
    memcpy(target->_array._chars, chars1, length1);
    memcpy(target->_array._chars + length1, chars2, length2);
    
    set_return(dst_idx, target);       
}

void __eqS(int dst_idx)
{
    Thunk* target = get_dst(dst_idx);
    Thunk* str1 = arg(2);
    Thunk* str2 = arg(1);
        
    int length1;
    char* chars1;
    
    if(str1->desc == (Desc*) __STRING_PTR__)
    {
        chars1 = str1->_string_ptr->chars;
        length1 = str1->_string_ptr->length;
    }
    else
    {
        chars1 = str1->_array._chars;
        length1 = str1->_array.length;
    }

    int length2;
    char* chars2;
    
    if(str2->desc == (Desc*) __STRING_PTR__)
    {
        chars2 = str2->_string_ptr->chars;
        length2 = str2->_string_ptr->length;
    }
    else
    {
        chars2 = str2->_array._chars;
        length2 = str2->_array.length;
    }
          
    int eq = length1 == length2;
    
333
334
    int i = 0;
    while(i<length1 && eq)
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
335
    {
336
337
        eq = chars1[i] == chars2[i];
        i++;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
338
339
340
    }

    target->desc = (Desc*) __BOOL__;
341
    target->_int = eq;    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
342
343
}

344
void add_prim(int arity, int boxingMap, char* name, void (*exec)(int)) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
345
346
347
348
349
350
    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);
351
    entry->base.type = FT_PRIM;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
352
    entry->base.arity = arity;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
353
354
    entry->base.thunk_size = thunk_size_f(arity);    
    entry->base.hnf = false;
355
    entry->boxingMap = boxingMap;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
356
357
358
359
360
361
362
363
364
365
    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);
366
367
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
368
void init_prim() {
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
    add_prim(2, 0b011, "addI", &__addI);
    add_prim(2, 0b011, "subI", &__subI);
    add_prim(2, 0b011, "multI", &__multI);
    add_prim(2, 0b011, "divI", &__divI);
    add_prim(2, 0b011, "gtI", &__gtI);
    add_prim(2, 0b011, "geI", &__geI);
    add_prim(2, 0b011, "geC", &__geC);    
    add_prim(2, 0b011, "ltI", &__ltI);
    add_prim(2, 0b011, "eqI", &__eqI);
    add_prim(2, 0b011, "neqI", &__neqI);
    add_prim(2, 0b011, "eqB", &__eqB);
    add_prim(2, 0b011, "eqC", &__eqC);
    add_prim(1, 0b001, "not", &__not);
    add_prim(2, 0b011, "and", &__and); 
    add_prim(2, 0b011, "or", &__or); 
    add_prim(2, 0b011, "mod", &__mod);
    add_prim(1, 0b001, "C2I", &__C2I);    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
386
387
388
389
390
391
392
393
394
    
    add_prim(1, 0b000, "string_size", &__string_size);
    add_prim(2, 0b010, "string_select", &__string_select);
    add_prim(1, 0b001, "string_create1", &__string_create1);
    add_prim(2, 0b011, "string_create2", &__string_create2);    
    add_prim(3, 0b110, "string_update", &__string_update);
    add_prim(3, 0b110, "string_slice", &__string_slice);
    add_prim(2, 0b000, "string_append", &__string_append);
    add_prim(2, 0b000, "eqS", &__eqS);
395
}