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

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

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

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

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

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
25
void __multI(int dst_idx) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
26
27
28
29
30
    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
31
void __divI(int dst_idx) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
32
33
34
35
36
    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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
void __bitxorI(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __INT__;
    target->_int = readI(arg(2)) ^ readI(arg(1));
}

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

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

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

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

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

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
97
98
99
100
101
102
void __negR(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __REAL__;
    target->_real = -readR(arg(1));
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
103
104
105
106
107
108
109
110
111
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
void __cos(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __REAL__;
    target->_real = cos(readR(arg(1)));
}

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

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

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

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

void __absR(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __REAL__;
    double val = readR(arg(1));
    target->_real = val < 0 ? -val : val;
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
140
void __gtI(int dst_idx) {
141
    Thunk* target = get_dst(dst_idx);
142
    target->desc = (Desc*) __BOOL__;
143
    target->_int = readI(arg(2)) > readI(arg(1));
144
145
}

146
147
148
void __geI(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __BOOL__;
149
    target->_int = readI(arg(2)) >= readI(arg(1));
150
151
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
152
void __ltI(int dst_idx) {
153
    Thunk* target = get_dst(dst_idx);
154
    target->desc = (Desc*) __BOOL__;
155
    target->_int = readI(arg(2)) < readI(arg(1));
156
157
}

Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
158
159
160
void __geC(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __BOOL__;
161
    target->_int = readC(arg(2)) >= readC(arg(1));
Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
162
163
}

164
165
166
167
168
169
void __ltC(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __BOOL__;
    target->_int = readC(arg(2)) < readC(arg(1));
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
170
171
172
173
174
175
void __ltR(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __BOOL__;
    target->_int = readR(arg(2)) < readR(arg(1));
}

176
177
void __eqI(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
178
    target->desc = (Desc*) __BOOL__;
179
    target->_int = readI(arg(2)) == readI(arg(1));
180
181
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
182
183
184
185
186
187
void __eqR(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __BOOL__;
    target->_int = readR(arg(2)) == readR(arg(1));
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
188
189
190
void __neqI(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __BOOL__;
191
    target->_int = readI(arg(2)) != readI(arg(1));
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
192
193
}

194
195
void __eqB(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
196
    target->desc = (Desc*) __BOOL__;
197
    target->_int = readB(arg(2)) == readB(arg(1));
198
199
}

Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
200
201
void __eqC(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
202
    target->desc = (Desc*) __BOOL__;
203
    target->_int = readC(arg(2)) == readC(arg(1));
Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
204
205
}

206
207
void __not(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
208
    target->desc = (Desc*) __BOOL__;
209
    target->_int = !readB(arg(1));
210
211
}

Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
212
213
214
void __and(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __BOOL__;
215
    target->_int = readB(arg(2)) && readB(arg(1));
Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
216
217
218
219
220
}

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

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
224
225
226
227
228
229
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
230
231
232
233
234
235
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
236
237
238
239
240
241
void __I2C(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __CHAR__;
    target->_int = (char) readI(arg(1));
}

242
243
244
245
246
247
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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
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;
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
334
335
336
337
338
339
340
341
342
void __array_size(int dst_idx)
{
    Thunk* target = get_dst(dst_idx);
    Thunk* arr = arg(1);
    
    target->desc = (Desc*) __INT__;
    target->_int = arr->_array.length;
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
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__;
361
    target->_int = (char) chars[pos->_int];
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
362
363
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
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

void __array_select(int dst_idx)
{
    Thunk* target = get_dst(dst_idx);
    Thunk* arr = arg(2);
    Thunk* pos = arg(1);

    Thunk* elem = arr->_array._elems[pos->_int];
    
    if(target != NULL)
    {
        target->desc = (Desc*) __FORWARD_PTR__;
        target->_forward_ptr = elem;
    }
    
    set_return(dst_idx, elem);
}

void __array_select_b_b(int dst_idx)
{
    Thunk* target = get_dst(dst_idx);
    Thunk* arr = arg(2);
    Thunk* pos = arg(1);

    target->desc = (Desc*) __BOOL__;
    target->_int = (int) arr->_array._bools[pos->_int];
}

void __array_select_b_i(int dst_idx)
{
    Thunk* target = get_dst(dst_idx);
    Thunk* arr = arg(2);
    Thunk* pos = arg(1);

    target->desc = (Desc*) __INT__;
    target->_int = arr->_array._ints[pos->_int];
}

void __array_select_b_r(int dst_idx)
{
    Thunk* target = get_dst(dst_idx);
    Thunk* arr = arg(2);
    Thunk* pos = arg(1);

    target->desc = (Desc*) __REAL__;
    target->_real = arr->_array._reals[pos->_int];
}

412
Thunk* array_create(Thunk* target, int elementsize, int len, ArrayElementType type)
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
413
{
414
    int newsize = sizeof (Desc) + sizeof (Array) + len * elementsize;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
415
416
417
418
419
420
421
422
423
424
425
426
427
428
    
    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__;
429
430
431
    target->_array.type = type;
    target->_array.is_boxed = type != AET_OTHER;
    target->_array.bytes_per_elem = elementsize;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
432
433
434
435
436
    target->_array.length = len;
    
    return target;
}

437
438
439
440
441
Thunk* string_create(Thunk* target, int len)
{
    return array_create(target, 1, len, AET_CHAR);
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
442
443
444
445
446
447
448
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));   
}

449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
void __array_create1(int dst_idx)
{
    Thunk* target = get_dst(dst_idx);
    Thunk* len = arg(1);
    set_return(dst_idx, array_create(target, sizeof(void*), len->_int, AET_OTHER));   
}

void __array_create1_b_i(int dst_idx)
{
    Thunk* target = get_dst(dst_idx);
    Thunk* len = arg(1);
    set_return(dst_idx, array_create(target, sizeof(int), len->_int, AET_INT));   
}

void __array_create1_b_b(int dst_idx)
{
    Thunk* target = get_dst(dst_idx);
    Thunk* len = arg(1);
    set_return(dst_idx, array_create(target, sizeof(char), len->_int, AET_BOOL));   
}

void __array_create1_b_r(int dst_idx)
{
    Thunk* target = get_dst(dst_idx);
    Thunk* len = arg(1);
    set_return(dst_idx, array_create(target, sizeof(double), len->_int, AET_REAL));   
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
477
478
479
480
481
482
483
484
485
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++)
    {
486
        target->_array._chars[i] = (char) ch->_int;    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
487
488
489
490
491
    }
    
    set_return(dst_idx, target);   
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
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
548
549
550
551
void __array_create2(int dst_idx)
{
    Thunk* target = get_dst(dst_idx);
    Thunk* len = arg(2);
    Thunk* def = arg(1);
    
    target = array_create(target, sizeof(Thunk*), len->_int, AET_OTHER);
    for(int i=0; i<len->_int; i++)
    {
        target->_array._elems[i] = def;    
    }
    
    set_return(dst_idx, target);   
}

void __array_create2_b_i(int dst_idx)
{
    Thunk* target = get_dst(dst_idx);
    Thunk* len = arg(2);
    Thunk* def = arg(1);
    
    target = array_create(target, sizeof(int), len->_int, AET_INT);
    for(int i=0; i<len->_int; i++)
    {
        target->_array._ints[i] = def->_int;    
    }
    
    set_return(dst_idx, target);   
}

void __array_create2_b_b(int dst_idx)
{
    Thunk* target = get_dst(dst_idx);
    Thunk* len = arg(2);
    Thunk* def = arg(1);
    
    target = array_create(target, sizeof(char), len->_int, AET_BOOL);
    for(int i=0; i<len->_int; i++)
    {
        target->_array._bools[i] = def->_int;    
    }
    
    set_return(dst_idx, target);   
}

void __array_create2_b_r(int dst_idx)
{
    Thunk* target = get_dst(dst_idx);
    Thunk* len = arg(2);
    Thunk* def = arg(1);
    
    target = array_create(target, sizeof(double), len->_int, AET_REAL);
    for(int i=0; i<len->_int; i++)
    {
        target->_array._reals[i] = def->_real;    
    }
    
    set_return(dst_idx, target);   
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
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);

576
    target->_array._chars[idx->_int] = (char) ch->_int;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
577
578
579
580
    
    set_return(dst_idx, target);   
}

581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
void __array_update(int dst_idx)
{
    Thunk* arr = arg(3);
    Thunk* idx = arg(2);
    Thunk* elem = arg(1);
            
    arr->_array._elems[idx->_int] = elem;
    
    set_return(dst_idx, arr);   
}

void __array_update_b_i(int dst_idx)
{
    Thunk* arr = arg(3);
    Thunk* idx = arg(2);
    Thunk* elem = arg(1);
            
    arr->_array._ints[idx->_int] = elem->_int;
    
    set_return(dst_idx, arr);   
}

void __array_update_b_b(int dst_idx)
{
    Thunk* arr = arg(3);
    Thunk* idx = arg(2);
    Thunk* elem = arg(1);
            
    arr->_array._bools[idx->_int] = (unsigned char) elem->_int;
    
    set_return(dst_idx, arr);   
}

void __array_update_b_r(int dst_idx)
{
    Thunk* arr = arg(3);
    Thunk* idx = arg(2);
    Thunk* elem = arg(1);
            
    arr->_array._reals[idx->_int] = elem->_real;
    
    set_return(dst_idx, arr);   
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
625
626
627
628
629
630
631
632
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
633
    int full_length;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
634
635
636
637
    
    if(str->desc == (Desc*) __STRING_PTR__)
    {
        chars = str->_string_ptr->chars;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
638
        full_length = str->_string_ptr->length;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
639
640
641
642
    }
    else
    {
        chars = str->_array._chars;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
643
        full_length = str->_array.length;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
644
645
    }

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
646
647
    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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
    
    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;
    
733
734
    int i = 0;
    while(i<length1 && eq)
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
735
    {
736
737
        eq = chars1[i] == chars2[i];
        i++;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
738
739
740
    }

    target->desc = (Desc*) __BOOL__;
741
    target->_int = eq;    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
742
743
}

744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
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
801
802
803
804
805
806
807
808
809
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);
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
void __I2S(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    Thunk* i = arg(1);

    char buff[20];
    itoa(i->_int, buff, 10);
    int len = strlen(buff);
    
    target = string_create(target, len);
    memcpy(target->_string_ptr->chars, buff, len);
    set_return(dst_idx, target);
}

void __R2S(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    Thunk* r = arg(1);

    char buff[1080];
    snprintf(buff,1080,"%f",r->_real);
    int len = strlen(buff);
    
    target = string_create(target, len);
    memcpy(target->_string_ptr->chars, buff, len);
    set_return(dst_idx, target);
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
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]);
    }
}

860
861
862
863
864
865
void __abort(int dst_idx)
{
    __trace(dst_idx);
    exit(-1);
}

Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
866
void add_prim(int arity, int boxingMap, int unboxableReturn,  char* name, void (*exec)(int)) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
867
868
869
870
871
872
    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);
873
    entry->base.type = FT_PRIM;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
874
    entry->base.arity = arity;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
875
876
    entry->base.thunk_size = thunk_size_f(arity);    
    entry->base.hnf = false;
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
877
    entry->base.unboxable_return = unboxableReturn;
878
    entry->boxingMap = boxingMap;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
879
880
881
882
883
884
885
886
887
888
    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);
889
890
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
891
void init_prim() {
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
892
893
894
895
896
897
898
899
900
    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);
901
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
902
903
904
905
906
907
908
    add_prim(2, 0b011, 1, "bitxor", &__bitxorI);
    add_prim(2, 0b011, 1, "bitand", &__bitandI);
    add_prim(2, 0b011, 1, "bitor", &__bitorI);
    add_prim(1, 0b001, 1, "bitnot", &__bitnotI);
    add_prim(2, 0b011, 1, "shiftleft", &__shiftleftI);
    add_prim(2, 0b011, 1, "shiftright", &__shiftrightI);
    
909
910
    add_prim(2, 0b011, 1, "geC", &__geC);    
    add_prim(2, 0b011, 1, "ltC", &__ltC);
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
911
    add_prim(2, 0b011, 1, "eqC", &__eqC);
912
913
914
915

    add_prim(2, 0b011, 1, "addR", &__addR);
    add_prim(2, 0b011, 1, "subR", &__subR);
    add_prim(2, 0b011, 1, "multR", &__multR);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
916
917
918
919
920
921
922
    add_prim(2, 0b011, 1, "divR", &__divR);
    add_prim(1, 0b001, 1, "cos", &__cos);
    add_prim(1, 0b001, 1, "sin", &__sin);
    add_prim(1, 0b001, 1, "tan", &__tan);
    add_prim(1, 0b001, 1, "acos", &__acos);
    add_prim(1, 0b001, 1, "atan", &__atan);
    add_prim(1, 0b001, 1, "absR", &__absR);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
923
    add_prim(1, 0b001, 1, "negR", &__negR);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
924
    add_prim(2, 0b011, 1, "ltR", &__ltR);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
925
    add_prim(2, 0b011, 1, "eqR", &__eqR);
926
927
    
    add_prim(2, 0b011, 1, "eqB", &__eqB);
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
928
929
930
931
    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);
932
    
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
933
    add_prim(1, 0b001, 1, "C2I", &__C2I);
934
935
    add_prim(1, 0b001, 1, "R2I", &__R2I);
    add_prim(1, 0b001, 1, "S2I", &__S2I);
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
936
    add_prim(1, 0b001, 1, "I2C", &__I2C);
937
938
939
940
    add_prim(1, 0b001, 1, "I2R", &__I2R);
    add_prim(1, 0b001, 1, "R2R", &__R2R);
    add_prim(1, 0b000, 0, "S2R", &__S2R);
            
941
942
943
944
945
946
    add_prim(1, 0b001, 0, "array_create1", &__array_create1);
    add_prim(1, 0b001, 0, "array_create1_lazy", &__array_create1);
    add_prim(1, 0b001, 0, "array_create1_B_I", &__array_create1_b_i);
    add_prim(1, 0b001, 0, "array_create1_B_B", &__array_create1_b_b);
    add_prim(1, 0b001, 0, "array_create1_B_R", &__array_create1_b_r);

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
947
948
949
950
951
952
    add_prim(2, 0b001, 0, "array_create2", &__array_create2);
    add_prim(2, 0b001, 0, "array_create2_lazy", &__array_create2); // TODO: should not be strict
    add_prim(2, 0b011, 0, "array_create2_B_I", &__array_create2_b_i);
    add_prim(2, 0b011, 0, "array_create2_B_B", &__array_create2_b_b);
    add_prim(2, 0b011, 0, "array_create2_B_R", &__array_create2_b_r);
        
953
    add_prim(3, 0b010, 0, "array_update", &__array_update);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
954
    add_prim(3, 0b010, 0, "array_update_lazy", &__array_update); // TODO: should not be strict
955
956
957
958
    add_prim(3, 0b110, 0, "array_update_B_I", &__array_update_b_i);
    add_prim(3, 0b110, 0, "array_update_B_B", &__array_update_b_b);
    add_prim(3, 0b110, 0, "array_update_B_R", &__array_update_b_r);
    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
959
960
961
962
963
964
965
966
967
968
969
970
    add_prim(1, 0b000, 1, "array_size", &__array_size);
    add_prim(1, 0b000, 1, "array_size_lazy", &__array_size);
    add_prim(1, 0b000, 1, "array_size_B_I", &__array_size);
    add_prim(1, 0b000, 1, "array_size_B_B", &__array_size);
    add_prim(1, 0b000, 1, "array_size_B_R", &__array_size);
    
    add_prim(2, 0b010, 0, "array_select", &__array_select);
    add_prim(2, 0b010, 0, "array_select_lazy", &__array_select);
    add_prim(2, 0b010, 1, "array_select_B_I", &__array_select_b_i);
    add_prim(2, 0b010, 1, "array_select_B_B", &__array_select_b_b);
    add_prim(2, 0b010, 1, "array_select_B_R", &__array_select_b_r);
    
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
971
972
973
974
975
976
977
978
    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);
979
    add_prim(2, 0b000, 1, "ltS", &__ltS);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
980
981
982
    add_prim(1, 0b001, 0, "C2S", &__C2S);
    add_prim(1, 0b001, 0, "I2S", &__I2S);
    add_prim(1, 0b001, 0, "R2S", &__R2S);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
983
    
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
984
    add_prim(1, 0b000, 0, "_trace", &__trace);
985
    add_prim(1, 0b000, 0, "abort", &__abort);
986
}