prim.c 10.5 KB
Newer Older
1
#include <string.h>
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
2
#include <stdio.h>
3
4
5

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

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

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

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
17
void __subI(int dst_idx) {
18
    Thunk* target = get_dst(dst_idx);
19
20
    target->desc = (Desc*) __INT__;
    target->_int = readI(arg(2)) - readI(arg(1));
21
22
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
23
void __multI(int dst_idx) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
24
25
26
27
28
    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
29
void __divI(int dst_idx) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
30
31
32
33
34
    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
35
void __gtI(int dst_idx) {
36
    Thunk* target = get_dst(dst_idx);
37
    target->desc = (Desc*) __BOOL__;
38
    target->_int = readI(arg(2)) > readI(arg(1));
39
40
}

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

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

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

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

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

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

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

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

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

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

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
101
102
103
104
105
106
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
107
108
109
110
111
112
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
113
114
115
116
117
118
void __I2C(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __CHAR__;
    target->_int = (char) readI(arg(1));
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
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
150
151
152
153
154
155
156
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__;
157
    target->_int = (char) chars[pos->_int];
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
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
194
195
196
197
198
199
200
}

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++)
    {
201
        target->_array._chars[i] = (char) ch->_int;    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
    }
    
    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);

231
    target->_array._chars[idx->_int] = (char) ch->_int;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
232
233
234
235
236
237
238
239
240
241
242
243
    
    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;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
244
    int full_length;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
245
246
247
248
    
    if(str->desc == (Desc*) __STRING_PTR__)
    {
        chars = str->_string_ptr->chars;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
249
        full_length = str->_string_ptr->length;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
250
251
252
253
    }
    else
    {
        chars = str->_array._chars;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
254
        full_length = str->_array.length;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
255
256
    }

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
257
258
    int length = idx2->_int - idx1->_int + 1;
    if(idx1->_int  + length > full_length) length = full_length - idx1->_int;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
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
333
334
335
336
337
338
339
340
341
342
343
    
    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;
    
344
345
    int i = 0;
    while(i<length1 && eq)
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
346
    {
347
348
        eq = chars1[i] == chars2[i];
        i++;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
349
350
351
    }

    target->desc = (Desc*) __BOOL__;
352
    target->_int = eq;    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
353
354
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
void __C2S(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    Thunk* ch = arg(1);

    target = string_create(target, 1);
    target->_array._chars[0] = (char) ch->_int;    
    set_return(dst_idx, target);
}

void __trace(int dst_idx)
{
    Thunk* str = 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;
    }

    for(int i=0; i<length; i++)
    {
        printf("%c", chars[i]);
    }
}

388
void add_prim(int arity, int boxingMap, char* name, void (*exec)(int)) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
389
390
391
392
393
394
    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);
395
    entry->base.type = FT_PRIM;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
396
    entry->base.arity = arity;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
397
398
    entry->base.thunk_size = thunk_size_f(arity);    
    entry->base.hnf = false;
399
    entry->boxingMap = boxingMap;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
400
401
402
403
404
405
406
407
408
409
    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);
410
411
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
412
void init_prim() {
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
    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);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
429
430
    add_prim(1, 0b001, "C2I", &__C2I);
    add_prim(1, 0b001, "I2C", &__I2C);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
431
432
433
434
435
436
437
438
439
    
    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);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
440
441
442
    add_prim(1, 0b001, "C2S", &__C2S);    
    
    add_prim(1, 0b000, "_trace", &__trace);
443
}