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

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

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

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

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

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

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
void __addR(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __REAL__;
    target->_real = readR(arg(2)) + readR(arg(1));
}

void __subR(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __REAL__;
    target->_real = readR(arg(2)) - readR(arg(1));
}

void __multR(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __REAL__;
    target->_real = readR(arg(2)) * readR(arg(1));
}

void __divR(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __REAL__;
    target->_real = readR(arg(2)) / readR(arg(1));
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
60
void __gtI(int dst_idx) {
61
    Thunk* target = get_dst(dst_idx);
62
    target->desc = (Desc*) __BOOL__;
63
    target->_int = readI(arg(2)) > readI(arg(1));
64
65
}

66
67
68
void __geI(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __BOOL__;
69
    target->_int = readI(arg(2)) >= readI(arg(1));
70
71
}

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

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

84
85
86
87
88
89
void __ltC(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __BOOL__;
    target->_int = readC(arg(2)) < readC(arg(1));
}

90
91
void __eqI(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
92
    target->desc = (Desc*) __BOOL__;
93
    target->_int = readI(arg(2)) == readI(arg(1));
94
95
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
96
97
98
void __neqI(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __BOOL__;
99
    target->_int = readI(arg(2)) != readI(arg(1));
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
100
101
}

102
103
void __eqB(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
104
    target->desc = (Desc*) __BOOL__;
105
    target->_int = readB(arg(2)) == readB(arg(1));
106
107
}

Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
108
109
void __eqC(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
110
    target->desc = (Desc*) __BOOL__;
111
    target->_int = readC(arg(2)) == readC(arg(1));
Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
112
113
}

114
115
void __not(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
116
    target->desc = (Desc*) __BOOL__;
117
    target->_int = !readB(arg(1));
118
119
}

Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
120
121
122
void __and(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __BOOL__;
123
    target->_int = readB(arg(2)) && readB(arg(1));
Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
124
125
126
127
128
}

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

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
132
133
134
135
136
137
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
138
139
140
141
142
143
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
144
145
146
147
148
149
void __I2C(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __CHAR__;
    target->_int = (char) readI(arg(1));
}

150
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
194
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
void __I2R(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __REAL__;
    target->_real = readI(arg(1));
}

void __R2R(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __REAL__;
    target->_real = readR(arg(1));
}

void __R2I(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __INT__;
    target->_int = (int) readR(arg(1));
}

void __S2R(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;
    }

    char buff[1080];
    if(length > 1079) length = 1079;
    memcpy(buff, chars, length);
    buff[length] = '\0';
    
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __REAL__;
    target->_real = strtod(buff, NULL);
}

void __S2I(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;
    }

    char buff[20];
    if(length > 19) length = 19;
    memcpy(buff, chars, length);
    buff[length] = '\0';
    
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __INT__;
    target->_int = strtol(buff, NULL, 10);
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
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__;
260
    target->_int = (char) chars[pos->_int];
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
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
}

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++)
    {
304
        target->_array._chars[i] = (char) ch->_int;    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
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
    }
    
    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);

334
    target->_array._chars[idx->_int] = (char) ch->_int;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
335
336
337
338
339
340
341
342
343
344
345
346
    
    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
347
    int full_length;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
348
349
350
351
    
    if(str->desc == (Desc*) __STRING_PTR__)
    {
        chars = str->_string_ptr->chars;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
352
        full_length = str->_string_ptr->length;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
353
354
355
356
    }
    else
    {
        chars = str->_array._chars;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
357
        full_length = str->_array.length;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
358
359
    }

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
360
361
    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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
    
    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;
    
447
448
    int i = 0;
    while(i<length1 && eq)
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
449
    {
450
451
        eq = chars1[i] == chars2[i];
        i++;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
452
453
454
    }

    target->desc = (Desc*) __BOOL__;
455
    target->_int = eq;    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
456
457
}

458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
void __ltS(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 result = length1 == 0;
    
    if(length1 > 0 && length2 > 0)
    {
        result = length1 < length2;
        
        int i = 0;
        while(i<length1)
        {
            if(chars1[i] != chars2[i])
            {
                result = chars1[i] < chars2[i];
                break;
            }
            
            i++;
        }
    }
    
    target->desc = (Desc*) __BOOL__;
    target->_int = result;    
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
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]);
    }
}

548
549
550
551
552
553
void __abort(int dst_idx)
{
    __trace(dst_idx);
    exit(-1);
}

Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
554
void add_prim(int arity, int boxingMap, int unboxableReturn,  char* name, void (*exec)(int)) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
555
556
557
558
559
560
    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);
561
    entry->base.type = FT_PRIM;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
562
    entry->base.arity = arity;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
563
564
    entry->base.thunk_size = thunk_size_f(arity);    
    entry->base.hnf = false;
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
565
    entry->base.unboxable_return = unboxableReturn;
566
    entry->boxingMap = boxingMap;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
567
568
569
570
571
572
573
574
575
576
    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);
577
578
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
579
void init_prim() {
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
580
581
582
583
584
585
586
587
588
    add_prim(2, 0b011, 1, "addI", &__addI);
    add_prim(2, 0b011, 1, "subI", &__subI);
    add_prim(2, 0b011, 1, "multI", &__multI);
    add_prim(2, 0b011, 1, "divI", &__divI);
    add_prim(2, 0b011, 1, "gtI", &__gtI);
    add_prim(2, 0b011, 1, "geI", &__geI);
    add_prim(2, 0b011, 1, "ltI", &__ltI);
    add_prim(2, 0b011, 1, "eqI", &__eqI);
    add_prim(2, 0b011, 1, "neqI", &__neqI);
589
590
591
    
    add_prim(2, 0b011, 1, "geC", &__geC);    
    add_prim(2, 0b011, 1, "ltC", &__ltC);
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
592
    add_prim(2, 0b011, 1, "eqC", &__eqC);
593
594
595
596
597
598
599

    add_prim(2, 0b011, 1, "addR", &__addR);
    add_prim(2, 0b011, 1, "subR", &__subR);
    add_prim(2, 0b011, 1, "multR", &__multR);
    add_prim(2, 0b011, 1, "divR", &__divR);    
    
    add_prim(2, 0b011, 1, "eqB", &__eqB);
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
600
601
602
603
    add_prim(1, 0b001, 1, "not", &__not);
    add_prim(2, 0b011, 1, "and", &__and); 
    add_prim(2, 0b011, 1, "or", &__or); 
    add_prim(2, 0b011, 1, "mod", &__mod);
604
    
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
605
    add_prim(1, 0b001, 1, "C2I", &__C2I);
606
607
    add_prim(1, 0b001, 1, "R2I", &__R2I);
    add_prim(1, 0b001, 1, "S2I", &__S2I);
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
608
    add_prim(1, 0b001, 1, "I2C", &__I2C);
609
610
611
612
613
    add_prim(1, 0b001, 1, "I2R", &__I2R);
    add_prim(1, 0b001, 1, "R2R", &__R2R);
    add_prim(1, 0b000, 0, "S2R", &__S2R);
    add_prim(1, 0b001, 0, "C2S", &__C2S);
            
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
614
615
616
617
618
619
620
621
    add_prim(1, 0b000, 1, "string_size", &__string_size);
    add_prim(2, 0b010, 1, "string_select", &__string_select);
    add_prim(1, 0b001, 0, "string_create1", &__string_create1);
    add_prim(2, 0b011, 0, "string_create2", &__string_create2);    
    add_prim(3, 0b110, 0, "string_update", &__string_update);
    add_prim(3, 0b110, 0, "string_slice", &__string_slice);
    add_prim(2, 0b000, 0, "string_append", &__string_append);
    add_prim(2, 0b000, 1, "eqS", &__eqS);
622
    add_prim(2, 0b000, 1, "ltS", &__ltS);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
623
    
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
624
    add_prim(1, 0b000, 0, "_trace", &__trace);
625
    add_prim(1, 0b000, 0, "abort", &__abort);
626
}