prim.c 20.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));
}

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
98
void __gtI(int dst_idx) {
99
    Thunk* target = get_dst(dst_idx);
100
    target->desc = (Desc*) __BOOL__;
101
    target->_int = readI(arg(2)) > readI(arg(1));
102
103
}

104
105
106
void __geI(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __BOOL__;
107
    target->_int = readI(arg(2)) >= readI(arg(1));
108
109
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
110
void __ltI(int dst_idx) {
111
    Thunk* target = get_dst(dst_idx);
112
    target->desc = (Desc*) __BOOL__;
113
    target->_int = readI(arg(2)) < readI(arg(1));
114
115
}

Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
116
117
118
void __geC(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __BOOL__;
119
    target->_int = readC(arg(2)) >= readC(arg(1));
Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
120
121
}

122
123
124
125
126
127
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
128
129
130
131
132
133
void __ltR(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __BOOL__;
    target->_int = readR(arg(2)) < readR(arg(1));
}

134
135
void __eqI(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
136
    target->desc = (Desc*) __BOOL__;
137
    target->_int = readI(arg(2)) == readI(arg(1));
138
139
}

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

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

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

158
159
void __not(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
160
    target->desc = (Desc*) __BOOL__;
161
    target->_int = !readB(arg(1));
162
163
}

Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
164
165
166
void __and(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __BOOL__;
167
    target->_int = readB(arg(2)) && readB(arg(1));
Laszlo Domoszlai's avatar
add gc    
Laszlo Domoszlai committed
168
169
170
171
172
}

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

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
176
177
178
179
180
181
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
182
183
184
185
186
187
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
188
189
190
191
192
193
void __I2C(int dst_idx) {
    Thunk* target = get_dst(dst_idx);
    target->desc = (Desc*) __CHAR__;
    target->_int = (char) readI(arg(1));
}

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
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
260
261
262
263
264
265
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
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
286
287
288
289
290
291
292
293
294
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
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__;
313
    target->_int = (char) chars[pos->_int];
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
314
315
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363

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];
}

364
Thunk* array_create(Thunk* target, int elementsize, int len, ArrayElementType type)
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
365
{
366
    int newsize = sizeof (Desc) + sizeof (Array) + len * elementsize;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
367
368
369
370
371
372
373
374
375
376
377
378
379
380
    
    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__;
381
382
383
    target->_array.type = type;
    target->_array.is_boxed = type != AET_OTHER;
    target->_array.bytes_per_elem = elementsize;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
384
385
386
387
388
    target->_array.length = len;
    
    return target;
}

389
390
391
392
393
Thunk* string_create(Thunk* target, int len)
{
    return array_create(target, 1, len, AET_CHAR);
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
394
395
396
397
398
399
400
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));   
}

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
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
429
430
431
432
433
434
435
436
437
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++)
    {
438
        target->_array._chars[i] = (char) ch->_int;    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
    }
    
    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);

468
    target->_array._chars[idx->_int] = (char) ch->_int;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
469
470
471
472
    
    set_return(dst_idx, target);   
}

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
515
516
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
517
518
519
520
521
522
523
524
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
525
    int full_length;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
526
527
528
529
    
    if(str->desc == (Desc*) __STRING_PTR__)
    {
        chars = str->_string_ptr->chars;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
530
        full_length = str->_string_ptr->length;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
531
532
533
534
    }
    else
    {
        chars = str->_array._chars;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
535
        full_length = str->_array.length;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
536
537
    }

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
538
539
    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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
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
    
    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;
    
625
626
    int i = 0;
    while(i<length1 && eq)
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
627
    {
628
629
        eq = chars1[i] == chars2[i];
        i++;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
630
631
632
    }

    target->desc = (Desc*) __BOOL__;
633
    target->_int = eq;    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
634
635
}

636
637
638
639
640
641
642
643
644
645
646
647
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
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
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
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]);
    }
}

726
727
728
729
730
731
void __abort(int dst_idx)
{
    __trace(dst_idx);
    exit(-1);
}

Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
732
void add_prim(int arity, int boxingMap, int unboxableReturn,  char* name, void (*exec)(int)) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
733
734
735
736
737
738
    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);
739
    entry->base.type = FT_PRIM;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
740
    entry->base.arity = arity;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
741
742
    entry->base.thunk_size = thunk_size_f(arity);    
    entry->base.hnf = false;
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
743
    entry->base.unboxable_return = unboxableReturn;
744
    entry->boxingMap = boxingMap;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
745
746
747
748
749
750
751
752
753
754
    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);
755
756
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
757
void init_prim() {
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
758
759
760
761
762
763
764
765
766
    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);
767
768
769
    
    add_prim(2, 0b011, 1, "geC", &__geC);    
    add_prim(2, 0b011, 1, "ltC", &__ltC);
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
770
    add_prim(2, 0b011, 1, "eqC", &__eqC);
771
772
773
774

    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
775
776
777
778
779
780
781
782
    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);
    add_prim(2, 0b011, 1, "ltR", &__ltR);
783
784
    
    add_prim(2, 0b011, 1, "eqB", &__eqB);
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
785
786
787
788
    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);
789
    
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
790
    add_prim(1, 0b001, 1, "C2I", &__C2I);
791
792
    add_prim(1, 0b001, 1, "R2I", &__R2I);
    add_prim(1, 0b001, 1, "S2I", &__S2I);
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
793
    add_prim(1, 0b001, 1, "I2C", &__I2C);
794
795
796
797
798
    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);
            
799
800
801
802
803
804
805
806
807
808
809
810
    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);

    add_prim(3, 0b010, 0, "array_update", &__array_update);
    add_prim(3, 0b010, 0, "array_update_lazy", &__array_update);
    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
811
812
813
814
815
816
817
818
819
820
821
822
    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
823
824
825
826
827
828
829
830
    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);
831
    add_prim(2, 0b000, 1, "ltS", &__ltS);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
832
    
Laszlo Domoszlai's avatar
fix bug    
Laszlo Domoszlai committed
833
    add_prim(1, 0b000, 0, "_trace", &__trace);
834
    add_prim(1, 0b000, 0, "abort", &__abort);
835
}