/
meta_modelica_builtin.c
740 lines (670 loc) · 20.6 KB
/
meta_modelica_builtin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
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
98
99
100
101
102
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
140
141
142
143
144
145
146
147
148
149
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
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
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
344
345
346
347
348
349
350
351
352
353
354
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
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
447
448
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
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
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
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
625
626
627
628
629
630
631
632
633
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
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
733
734
735
736
737
738
739
740
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE
* GPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the OSMC (Open Source Modelica Consortium)
* Public License (OSMC-PL) are obtained from OSMC, either from the above
* address, from the URLs: http://www.openmodelica.org or
* http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica
* distribution. GNU version 3 is obtained from:
* http://www.gnu.org/copyleft/gpl.html. The New BSD License is obtained from:
* http://www.opensource.org/licenses/BSD-3-Clause.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS
* EXPRESSLY SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE
* CONDITIONS OF OSMC-PL.
*
*/
#include "meta_modelica_builtin.h"
#include "meta_modelica.h"
#include <float.h>
#include <limits.h>
#include <assert.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#if defined(_MSC_VER)
#include <float.h>
#define isinf(d) (!_finite(d) && !_isnan(d))
#define isnan _isnan
#define snprintf _snprintf
#endif
#define GEN_META_MODELICA_BUILTIN_BOXPTR
#include "meta_modelica_builtin_boxptr.h"
metamodelica_string intString(modelica_integer i)
{
/* 64-bit integer: 1+log_10(2**63)+1 = 20 digits max */
char buffer[22];
void *res;
if (i>=0 && i<=9) /* Small integers are used so much it makes sense to cache them */
return mmc_strings_len1['0'+i];
sprintf(buffer, "%ld", (long) i);
res = mmc_mk_scon(buffer);
MMC_CHECK_STRING(res);
return res;
}
/* String Character Conversion */
modelica_integer nobox_stringCharInt(threadData_t *threadData,metamodelica_string chr)
{
unsigned char c;
if (MMC_STRLEN(chr) != 1)
MMC_THROW_INTERNAL();
MMC_CHECK_STRING(chr);
return (unsigned char) MMC_STRINGDATA(chr)[0];
}
metamodelica_string nobox_intStringChar(threadData_t *threadData,modelica_integer ix)
{
char chr[2];
if (ix < 1 || ix > 255)
MMC_THROW_INTERNAL();
chr[0] = (char) ix;
chr[1] = '\0';
return mmc_mk_scon(chr);
}
/* String Operations */
modelica_integer nobox_stringInt(threadData_t *threadData,metamodelica_string s)
{
long res;
char *endptr,*str=MMC_STRINGDATA(s);
MMC_CHECK_STRING(s);
errno = 0;
res = strtol(str,&endptr,10);
if (errno != 0 || str == endptr)
MMC_THROW_INTERNAL();
if (*endptr != '\0')
MMC_THROW_INTERNAL();
if (res > INT_MAX || res < INT_MIN)
MMC_THROW_INTERNAL();
return res;
}
modelica_real nobox_stringReal(threadData_t *threadData,metamodelica_string s)
{
modelica_real res;
char *endptr,*str=MMC_STRINGDATA(s);
MMC_CHECK_STRING(s);
errno = 0;
res = strtod(str,&endptr);
if (errno != 0 || str == endptr)
MMC_THROW_INTERNAL();
if (*endptr != '\0')
MMC_THROW_INTERNAL();
return res;
}
/******************** String HASH Functions ********************/
/*
* adrpo 2008-12-02
* http://www.cse.yorku.ca/~oz/hash.html
* hash functions which could be useful to replace System__hash:
*/
/*** djb2 hash ***/
static inline unsigned long djb2_hash(const unsigned char *str)
{
unsigned long hash = 5381;
int c;
while (0 != (c = *str++)) hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
/*** sdbm hash ***/
static inline unsigned long sdbm_hash(const unsigned char* str)
{
unsigned long hash = 0;
int c;
while (0 != (c = *str++)) hash = c + (hash << 6) + (hash << 16) - hash;
return hash;
}
/* adrpo: really bad hash :) */
modelica_integer stringHash(metamodelica_string_const s)
{
const char* str = MMC_STRINGDATA(s);
long res = 0, i=0;
while (0 != (str[i])) { res += str[i]; i++; }
return res;
}
/* adrpo: see the comment above about djb2 hash */
modelica_integer stringHashDjb2(metamodelica_string_const s)
{
const char* str = MMC_STRINGDATA(s);
long res = djb2_hash((const unsigned char*)str);
res = labs(res);
/* fprintf(stderr, "stringHashDjb2 %s-> %ld %ld %ld\n", str, res, mmc_mk_icon(res), mmc_unbox_integer(mmc_mk_icon(res))); */
return res;
}
/* adrpo: see the comment above about djb2 hash */
modelica_integer stringHashDjb2Mod(metamodelica_string_const s, modelica_integer mod)
{
const char* str = MMC_STRINGDATA(s);
long res = djb2_hash((const unsigned char*)str) % (unsigned int) mod;
res = labs(res);
/* fprintf(stderr, "stringHashDjb2Mod %s %ld-> %ld %ld %ld\n", str, mod, res, mmc_mk_icon(res), mmc_unbox_integer(mmc_mk_icon(res))); */
return res;
}
modelica_metatype boxptr_stringHashDjb2Mod(threadData_t *threadData,modelica_metatype v,modelica_metatype mod)
{
modelica_integer modunbox = mmc_unbox_integer(mod);
if (modunbox < 1) {
MMC_THROW_INTERNAL();
}
return mmc_mk_icon(stringHashDjb2Mod(v,modunbox));
}
/* adrpo: see the comment above about sdbm hash */
modelica_integer stringHashSdbm(metamodelica_string_const s)
{
const char* str = MMC_STRINGDATA(s);
long res = sdbm_hash((const unsigned char*)str);
return res;
}
modelica_metatype boxptr_substring(threadData_t *threadData, metamodelica_string_const str, modelica_metatype boxstart, modelica_metatype boxstop)
{
unsigned header = 0, nwords;
long start = MMC_UNTAGFIXNUM(boxstart) - 1;
long stop = MMC_UNTAGFIXNUM(boxstop) - 1;
long totalLen = MMC_STRLEN(str), len = stop-start+1;
struct mmc_string *res;
char *tmp;
modelica_metatype p;
/* Bad indexes */
if (start < 0 || start >= totalLen || stop < start || stop >= totalLen) {
MMC_THROW_INTERNAL();
}
header = MMC_STRINGHDR(len);
nwords = MMC_HDRSLOTS(header) + 1;
res = (struct mmc_string *) mmc_alloc_words_atomic(nwords);
res->header = header;
tmp = (char*) res->data;
memcpy(tmp, MMC_STRINGDATA(str) + start, len);
tmp[len] = '\0';
p = MMC_TAGPTR(res);
MMC_CHECK_STRING(p);
return p;
}
metamodelica_string stringListStringChar(metamodelica_string s)
{
const char *str = MMC_STRINGDATA(s);
char chr[2] = {'\0', '\0'};
modelica_metatype res = NULL;
int i = 0;
MMC_CHECK_STRING(s);
res = mmc_mk_nil();
for (i=MMC_STRLEN(s)-1; i>=0; i--) {
chr[0] = str[i];
res = mmc_mk_cons(mmc_mk_scon(chr), res);
}
return res;
}
metamodelica_string stringAppendList(modelica_metatype lst)
{
/* fprintf(stderr, "stringAppendList(%s)\n", anyString(lst)); */
modelica_integer lstLen = 0, len = 0;
unsigned nbytes = 0, header = 0, nwords = 0;
modelica_metatype car = NULL, lstHead = NULL, lstTmp = NULL;
char *tmp = NULL;
struct mmc_string *res = NULL;
void *p = NULL;
lstLen = 0;
nbytes = 0;
lstHead = lst;
lstTmp = lst;
while (!listEmpty(lstTmp)) {
MMC_CHECK_STRING(MMC_CAR(lstTmp));
nbytes += MMC_STRLEN(MMC_CAR(lstTmp));
/* fprintf(stderr, "stringAppendList: Has success reading input %d: %s\n", lstLen, MMC_STRINGDATA(MMC_CAR(lst))); */
lstTmp = MMC_CDR(lstTmp);
lstLen++;
}
if (nbytes == 0) return mmc_emptystring;
if (lstLen == 1) return MMC_CAR(lstHead);
header = MMC_STRINGHDR(nbytes);
nwords = MMC_HDRSLOTS(header) + 1;
res = (struct mmc_string *) mmc_alloc_words_atomic(nwords);
res->header = header;
tmp = (char*) res->data;
nbytes = 0;
lstTmp = lstHead;
while (!listEmpty(lstTmp)) {
car = MMC_CAR(lstTmp);
len = MMC_STRLEN(car);
/* fprintf(stderr, "stringAppendList: %s %d %d\n", MMC_STRINGDATA(car), len, strlen(MMC_STRINGDATA(car))); */
/* Might be useful to check this when debugging. String literals are often done wrong :) */
MMC_DEBUG_ASSERT(len == strlen(MMC_STRINGDATA(car)));
memcpy(tmp+nbytes,MMC_STRINGDATA(car),len);
nbytes += len;
lstTmp = MMC_CDR(lstTmp);
}
tmp[nbytes] = '\0';
/* fprintf(stderr, "stringAppendList(%s)=>%s\n", anyString(lstHead), anyString(MMC_TAGPTR(res))); */
p = MMC_TAGPTR(res);
MMC_CHECK_STRING(p);
return p;
}
modelica_metatype stringDelimitList(modelica_metatype lst, metamodelica_string_const delimiter)
{
/* fprintf(stderr, "stringDelimitList(%s)\n", anyString(lst)); */
modelica_integer lstLen = 0, len = 0, lenDelimiter = 0;
unsigned nbytes = 0, header = 0, nwords = 0;
modelica_metatype car = NULL, lstHead = NULL, lstTmp = NULL;
char *tmp = 0, *delimiter_cstr = 0;
struct mmc_string *res = NULL;
void *p = NULL;
lstLen = 0;
nbytes = 0;
lstHead = lst;
lstTmp = lst;
while (!listEmpty(lstTmp)) {
MMC_CHECK_STRING(MMC_CAR(lstTmp));
nbytes += MMC_STRLEN(MMC_CAR(lstTmp));
/* fprintf(stderr, "stringDelimitList: Has success reading input %d: %s\n", lstLen, MMC_STRINGDATA(MMC_CAR(lst))); */
lstTmp = MMC_CDR(lstTmp);
lstLen++;
}
if (nbytes == 0) return mmc_emptystring;
if (lstLen == 1) return MMC_CAR(lstHead);
lenDelimiter = MMC_STRLEN(delimiter);
nbytes += (lstLen-1)*lenDelimiter;
delimiter_cstr = MMC_STRINGDATA(delimiter);
MMC_DEBUG_ASSERT(lenDelimiter == strlen(delimiter_cstr));
header = MMC_STRINGHDR(nbytes);
nwords = MMC_HDRSLOTS(header) + 1;
res = (struct mmc_string *) mmc_alloc_words_atomic(nwords);
res->header = header;
tmp = (char*) res->data;
nbytes = 0;
lstTmp = lstHead;
{ /* Unrolled first element (not delimiter in front) */
car = MMC_CAR(lstTmp);
len = MMC_STRLEN(car);
MMC_DEBUG_ASSERT(len == strlen(MMC_STRINGDATA(car)));
memcpy(tmp+nbytes,MMC_STRINGDATA(car),len);
nbytes += len;
lstTmp = MMC_CDR(lstTmp);
}
while (!listEmpty(lstTmp)) {
memcpy(tmp+nbytes,delimiter_cstr,lenDelimiter);
nbytes += lenDelimiter;
car = MMC_CAR(lstTmp);
len = MMC_STRLEN(car);
/* fprintf(stderr, "stringDelimitList: %s %d %d\n", MMC_STRINGDATA(car), len, strlen(MMC_STRINGDATA(car))); */
/* Might be useful to check this when debugging. String literals are often done wrong :) */
MMC_DEBUG_ASSERT(len == strlen(MMC_STRINGDATA(car)));
memcpy(tmp+nbytes,MMC_STRINGDATA(car),len);
nbytes += len;
lstTmp = MMC_CDR(lstTmp);
}
tmp[nbytes] = '\0';
/* fprintf(stderr, "stringDelimitList(%s)=>%s\n", anyString(lstHead), anyString(MMC_TAGPTR(res))); */
p = MMC_TAGPTR(res);
MMC_CHECK_STRING(p);
return p;
}
modelica_integer mmc_stringCompare(const void *str1, const void *str2)
{
int res;
MMC_CHECK_STRING(str1);
MMC_CHECK_STRING(str2);
res = strcmp(MMC_STRINGDATA(str1),MMC_STRINGDATA(str2));
if (res < 0)
return -1;
if (res > 0)
return 1;
return 0;
}
modelica_metatype boxptr_stringGetStringChar(threadData_t *threadData,metamodelica_string str, modelica_metatype iix)
{
modelica_metatype res;
int ix = MMC_UNTAGFIXNUM(iix);
MMC_CHECK_STRING(str);
if (ix < 1 || ix > (long) MMC_STRLEN(str))
MMC_THROW_INTERNAL();
return mmc_strings_len1[MMC_STRINGDATA(str)[ix-1]];
}
modelica_integer nobox_stringGet(threadData_t *threadData,metamodelica_string str, modelica_integer ix)
{
if (ix < 1 || ix > (long) MMC_STRLEN(str))
MMC_THROW_INTERNAL();
return MMC_STRINGDATA(str)[ix-1];
}
modelica_metatype boxptr_stringUpdateStringChar(threadData_t *threadData,metamodelica_string str, metamodelica_string c, modelica_metatype iix)
{
int ix = MMC_UNTAGFIXNUM(iix);
int length = 0;
unsigned header = MMC_GETHDR(str);
unsigned nwords = MMC_HDRSLOTS(header) + 1;
struct mmc_string *p = NULL;
void *res = NULL;
MMC_CHECK_STRING(str);
MMC_CHECK_STRING(c);
/* fprintf(stderr, "stringUpdateStringChar(%s,%s,%ld)\n", MMC_STRINGDATA(str),MMC_STRINGDATA(c),ix); */
if (ix < 1 || MMC_STRLEN(c) != 1)
MMC_THROW_INTERNAL();
length = MMC_STRLEN(str);
if (ix > length)
MMC_THROW_INTERNAL();
p = (struct mmc_string *) mmc_alloc_words_atomic(nwords);
p->header = header;
memcpy(p->data, MMC_STRINGDATA(str), length+1 /* include NULL */);
p->data[ix-1] = MMC_STRINGDATA(c)[0];
res = MMC_TAGPTR(p);
MMC_CHECK_STRING(res);
return res;
}
metamodelica_string_const stringAppend(metamodelica_string_const s1, metamodelica_string_const s2)
{
unsigned len1 = 0, len2 = 0, nbytes = 0, header = 0, nwords = 0;
void *res = NULL;
struct mmc_string *p = NULL;
MMC_CHECK_STRING(s1);
MMC_CHECK_STRING(s2);
/* fprintf(stderr, "stringAppend([%p] %s, [%p] %s)->\n", s1, anyString(s1), s2, anyString(s2)); fflush(NULL); */
len1 = MMC_STRLEN(s1);
len2 = MMC_STRLEN(s2);
nbytes = len1+len2;
header = MMC_STRINGHDR(nbytes);
nwords = MMC_HDRSLOTS(header) + 1;
p = (struct mmc_string *) mmc_alloc_words_atomic(nwords);
/* fprintf(stderr, "at address %p\n", MMC_TAGPTR(p)); fflush(NULL); */
p->header = header;
memcpy(p->data, MMC_STRINGDATA(s1), len1);
memcpy(p->data + len1, MMC_STRINGDATA(s2), len2 + 1);
res = MMC_TAGPTR(p);
MMC_CHECK_STRING(res);
/* fprintf(stderr, "-> %s\n", anyString(res)); fflush(NULL); */
return res;
}
/* List Operations */
modelica_metatype listReverse(modelica_metatype lst)
{
modelica_metatype res = NULL;
res = mmc_mk_nil();
while (!MMC_NILTEST(lst))
{
res = mmc_mk_cons(MMC_CAR(lst),res);
lst = MMC_CDR(lst);
}
return res;
}
modelica_metatype listReverseInPlace(modelica_metatype lst)
{
modelica_metatype prev = mmc_mk_nil();
while (!MMC_NILTEST(lst))
{
modelica_metatype oldcdr = MMC_CDR(lst);
MMC_CDR(lst) = prev;
prev = lst;
lst = oldcdr;
}
return prev;
}
modelica_metatype listAppend(modelica_metatype lst1,modelica_metatype lst2)
{
int length = 0, i = 0;
struct mmc_cons_struct *res = NULL;
struct mmc_cons_struct *p = NULL;
if (MMC_NILTEST(lst2)) /* If lst2 is empty, simply return lst1; huge performance gain for some uses of listAppend */
return lst1;
length = listLength(lst1);
if (length == 0) /* We need to check for empty lst1 */
return lst2;
res = (struct mmc_cons_struct*)mmc_alloc_words( length * 3 /*(sizeof(struct mmc_cons_struct)/sizeof(void*))*/ ); /* Do one single big alloc. It's cheaper */
for (i=0; i<length-1; i++) { /* Write all except the last element... */
struct mmc_cons_struct *p = res+i;
p->header = MMC_STRUCTHDR(2, MMC_CONS_CTOR);
p->data[0] = MMC_CAR(lst1);
p->data[1] = MMC_TAGPTR(res+i+1);
lst1 = MMC_CDR(lst1);
}
/* The last element is a bit special. It points to lst2. */
p = res+length-1;
p->header = MMC_STRUCTHDR(2, MMC_CONS_CTOR);
p->data[0] = MMC_CAR(lst1);
p->data[1] = lst2;
return MMC_TAGPTR(res);
}
modelica_integer listLength(modelica_metatype lst)
{
modelica_integer res = 0;
while (!MMC_NILTEST(lst))
{
lst = MMC_CDR(lst);
res++;
}
return res;
}
modelica_boolean listMember(modelica_metatype obj, modelica_metatype lst)
{
while (!MMC_NILTEST(lst))
{
if (valueEq(MMC_CAR(lst), obj)) {
return 1;
}
lst = MMC_CDR(lst);
}
return 0;
}
modelica_metatype boxptr_listGet(threadData_t *threadData,modelica_metatype lst, modelica_metatype ii)
{
modelica_metatype res;
int i = mmc_unbox_integer(ii);
if (i < 1)
MMC_THROW_INTERNAL();
while (!MMC_NILTEST(lst))
{
if (i == 1) {
return MMC_CAR(lst);
}
lst = MMC_CDR(lst);
i--;
}
MMC_THROW_INTERNAL(); /* List was not long enough */
}
modelica_metatype boxptr_listNth(threadData_t *threadData,modelica_metatype lst, modelica_metatype i)
{
return boxptr_listGet(threadData,lst,mmc_mk_icon(mmc_unbox_integer(i)+1));
}
modelica_metatype boxptr_listDelete(threadData_t *threadData, modelica_metatype lst, modelica_metatype iix)
{
/* TODO: If we assume the index exists we can do this in a much better way */
int ix = mmc_unbox_integer(iix);
modelica_metatype *tmpArr = NULL;
int i = 0;
if (ix <= 0) {
MMC_THROW_INTERNAL();
}
tmpArr = (modelica_metatype *) GC_malloc(sizeof(modelica_metatype)*(ix-1)); /* We know the size of the first part of the list */
if (tmpArr == NULL) {
fprintf(stderr, "%s:%d: malloc failed", __FILE__, __LINE__);
EXIT(1);
}
for (i=0; i<ix-1; i++) {
if (listEmpty(lst)) {
if (tmpArr) {
GC_free(tmpArr);
}
MMC_THROW_INTERNAL();
}
tmpArr[i] = MMC_CAR(lst);
lst = MMC_CDR(lst);
}
if (listEmpty(lst)) {
GC_free(tmpArr);
MMC_THROW_INTERNAL();
}
lst = MMC_CDR(lst);
for (i=ix-2; i>=0; i--) {
lst = mmc_mk_cons(tmpArr[i], lst);
}
GC_free(tmpArr);
return lst;
}
modelica_metatype boxptr_listRest(threadData_t *threadData, modelica_metatype lst)
{
if (!MMC_NILTEST(lst)) {
return MMC_CDR(lst);
}
MMC_THROW_INTERNAL();
}
modelica_metatype boxptr_listHead(threadData_t *threadData, modelica_metatype lst)
{
if (!MMC_NILTEST(lst)) {
return MMC_CAR(lst);
}
MMC_THROW_INTERNAL();
}
/* Array Operations */
modelica_metatype arrayList(modelica_metatype arr)
{
modelica_metatype result;
int nelts = MMC_HDRSLOTS(MMC_GETHDR(arr))-1;
void **vecp = MMC_STRUCTDATA(arr);
void *res = mmc_mk_nil();
for(; nelts >= 0; --nelts) {
res = mmc_mk_cons(vecp[nelts],res);
}
return res;
}
modelica_metatype listArray(modelica_metatype lst)
{
int nelts = listLength(lst);
void* arr = (struct mmc_struct*)mmc_mk_box_no_assign(nelts, MMC_ARRAY_TAG);
void **arrp = MMC_STRUCTDATA(arr);
int i = 0;
for(i=0; i<nelts; i++) {
arrp[i] = MMC_CAR(lst);
lst = MMC_CDR(lst);
}
return arr;
}
modelica_metatype arrayCopy(modelica_metatype arr)
{
int nelts = MMC_HDRSLOTS(MMC_GETHDR(arr));
void* res = (struct mmc_struct*)mmc_mk_box_no_assign(nelts, MMC_ARRAY_TAG);
void **arrp = MMC_STRUCTDATA(arr);
void **resp = MMC_STRUCTDATA(res);
int i = 0;
for(i=0; i<nelts; i++) {
resp[i] = arrp[i];
}
return res;
}
modelica_metatype arrayAppend(modelica_metatype arr1, modelica_metatype arr2)
{
int nelts1 = MMC_HDRSLOTS(MMC_GETHDR(arr1));
int nelts2 = MMC_HDRSLOTS(MMC_GETHDR(arr2));
void* res = (struct mmc_struct*)mmc_mk_box_no_assign(nelts1 + nelts2, MMC_ARRAY_TAG);
void **arr1p = MMC_STRUCTDATA(arr1);
void **arr2p = MMC_STRUCTDATA(arr2);
void **resp = MMC_STRUCTDATA(res);
int i;
for (i=0; i<nelts1; ++i) {
resp[i] = arr1p[i];
}
for (i=0; i<nelts2; ++i) {
resp[i+nelts1] = arr2p[i];
}
return res;
}
modelica_metatype boxptr_arrayNth(threadData_t *threadData,modelica_metatype arr,modelica_metatype ix)
{
return arrayGet(arr, mmc_unbox_integer(ix)+1);
}
/* Misc Operations */
modelica_integer tick(void)
{
static modelica_integer curTick = 0;
return curTick++;
}
void boxptr_print(threadData_t *threadData,modelica_metatype str)
{
fputs(MMC_STRINGDATA(str), stdout);
}
modelica_real mmc_clock(void)
{
static double start_t;
static int init = 1;
if (init) {
start_t = ((double)clock())/CLOCKS_PER_SEC;
init = 0;
return 0.0;
}
return (clock()-start_t)/CLOCKS_PER_SEC;
}
void boxptr_equality(threadData_t *threadData,modelica_metatype in1, modelica_metatype in2)
{
if (!valueEq(in1, in2)) {
/* fprintf(stderr, "%s != %s\n", anyString(in1), anyString(in2)); */
MMC_THROW_INTERNAL();
}
}
modelica_metatype nobox_getGlobalRoot(threadData_t *threadData, modelica_integer ix) {
void *val = 0;
if (ix < 0 || ix >= MMC_GC_GLOBAL_ROOTS_SIZE) {
MMC_THROW_INTERNAL();
} else if (ix > 8) {
val = mmc_GC_state->global_roots[ix];
} else {
val = threadData->localRoots[ix];
}
if (!val) {
MMC_THROW_INTERNAL();
}
return val;
}
void boxptr_setGlobalRoot(threadData_t *threadData, modelica_metatype i, modelica_metatype val) {
int ix = mmc_unbox_integer(i);
if (ix < 0 || ix >= MMC_GC_GLOBAL_ROOTS_SIZE) {
MMC_THROW_INTERNAL();
} else if (ix > 8) {
mmc_GC_state->global_roots[ix] = val;
} else {
threadData->localRoots[ix] = val;
}
}
modelica_real realMaxLit(void)
{
return DBL_MAX / 2048; /* in case some non-linear or ODE solver tries to add eps to this value */
}
modelica_integer intMaxLit(void)
{
return INT_MAX / 2;
}
modelica_boolean setStackOverflowSignal(modelica_boolean inSignal)
{
/* return for now what we got */
return inSignal;
}
#if defined(linux) || defined(__APPLE_CC__)
#include <execinfo.h>
metamodelica_string referenceDebugString(modelica_metatype fnptr)
{
void *res;
char **str = backtrace_symbols(&fnptr, 1);
if (str == 0) {
return mmc_mk_scon("Unknown symbol");
}
res = mmc_mk_scon(*str);
free(str);
return res;
}
#else
metamodelica_string referenceDebugString(modelica_metatype fnptr)
{
return mmc_mk_scon("Unknown symbol");
}
#endif
const char* SourceInfo_SOURCEINFO__desc__fields[7] = {"fileName","isReadOnly","lineNumberStart","columnNumberStart","lineNumberEnd","columnNumberEnd","lastEditTime"};
struct record_description SourceInfo_SOURCEINFO__desc = {
"SourceInfo_SOURCEINFO",
"SourceInfo.SOURCEINFO",
SourceInfo_SOURCEINFO__desc__fields
};