-
Notifications
You must be signed in to change notification settings - Fork 5
/
metaRuntime.jl
815 lines (677 loc) · 18.7 KB
/
metaRuntime.jl
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
741
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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
#=
The MetaModelica runtime. Partly automatically generated by the transpiler
Functions that are named #= Defined in the runtime =#
are defined in the C runtime in the compiler and interfaces Boehm GC.
These functions should be remimplemented here or removed all together =#
struct MetaModelicaGeneralException <: MetaModelicaException
msg::Any
end
"""
SOME
Optional with a value T
"""
struct SOME{T}
data::T
end
"""The optional type is defined as the Union of SOME{T} and the Nothing type. """
const Option{T} = Union{SOME{T},Nothing}
""" NONE is defined as nothing. """
const NONE() = Nothing()
Base.convert(::Type{Option{S}}, x::SOME{T}) where {S,T<:S} =
let
SOME{S}(convert(S, x.data))
end
Base.convert(::Type{Option{T}}, nothing) where {T} =
let
Nothing()
end
""" Identity case """
Base.convert(::Type{Union{Nothing,SOME{T}}}, x::Union{Nothing,SOME{T}}) where {T} =
let
x
end
Base.convert(::Type{Union{Nothing,SOME{S}}}, x::SOME{S}) where {S} =
let
x
end
""" Logically combine two Booleans with 'and' operator """
function boolAnd(b1::Bool, b2::Bool)::Bool
b = b1 && b2
end
""" Logically combine two Booleans with 'or' operator """
function boolOr(b1::Bool, b2::Bool)::Bool
b = b1 || b2
end
""" Logically invert Boolean value using 'not' operator """
function boolNot(b::Bool)::Bool
nb = !b
end
""" Compares two Booleans """
function boolEq(b1::Bool, b2::Bool)::Bool
b1 === b2
end
""" Returns \\\"true\\\" or \\\"false\\\" string """
function boolString(b::Bool)::String
str = if (b)
"true"
else
"false"
end
end
""" Adds two Integer values """
function intAdd(i1::ModelicaInteger, i2::ModelicaInteger)
local i::ModelicaInteger
i = i1 + i2
end
""" Subtracts two Integer values """
function intSub(i1::ModelicaInteger, i2::ModelicaInteger)
local i::ModelicaInteger
i = i1 - i2
end
""" Multiplies two Integer values """
function intMul(i1::ModelicaInteger, i2::ModelicaInteger)
local i::ModelicaInteger
i = i1 * i2
end
""" Divides two Integer values """
function intDiv(i1::ModelicaInteger, i2::ModelicaInteger)
local i::ModelicaInteger
i = div(i1, i2)
end
""" Calculates remainder of Integer division i1/i2 """
function intMod(i1::ModelicaInteger, i2::ModelicaInteger)
local i::ModelicaInteger
i = mod(i1, i2)
end
""" Returns the bigger one of two Integer values """
function intMax(i1::ModelicaInteger, i2::ModelicaInteger)
local i::ModelicaInteger
i = max(i1, i2)
end
""" Returns the smaller one of two Integer values """
function intMin(i1::ModelicaInteger, i2::ModelicaInteger)
local i::ModelicaInteger
i = min(i1, i2)
end
""" Returns the absolute value of Integer i """
function intAbs(i::ModelicaInteger)
local oi::ModelicaInteger
oi = abs(i)
end
""" Returns negative value of Integer i """
function intNeg(i::ModelicaInteger)
local oi::ModelicaInteger
oi = -i
end
""" Returns whether Integer i1 is smaller than Integer i2 """
function intLt(i1::ModelicaInteger, i2::ModelicaInteger)::Bool
local b::Bool
b = i1 < i2
end
""" Returns whether Integer i1 is smaller than or equal to Integer i2 """
function intLe(i1::ModelicaInteger, i2::ModelicaInteger)::Bool
local b::Bool
b = i1 <= i2
b
end
""" Returns whether Integer i1 is equal to Integer i2 """
function intEq(i1::ModelicaInteger, i2::ModelicaInteger)::Bool
local b::Bool
b = i1 == i2
b
end
""" Returns whether Integer i1 is not equal to Integer i2 """
function intNe(i1::ModelicaInteger, i2::ModelicaInteger)::Bool
local b::Bool
b = i1 != i2
b
end
""" Returns whether Integer i1 is greater than or equal to Integer i2 """
function intGe(i1::ModelicaInteger, i2::ModelicaInteger)::Bool
local b::Bool
b = i1 >= i2
b
end
""" Returns whether Integer i1 is greater than Integer i2 """
function intGt(i1::ModelicaInteger, i2::ModelicaInteger)::Bool
local b::Bool
b = i1 > i2
b
end
""" Returns bitwise inverted Integer number of i """
function intBitNot(i::ModelicaInteger)
local o::ModelicaInteger = ~i
o
end
""" Returns bitwise \'and\' of Integers i1 and i2 """
function intBitAnd(i1::ModelicaInteger, i2::ModelicaInteger)
local o::ModelicaInteger = i1 & i2
o
end
""" Returns bitwise 'or' of Integers i1 and i2 """
function intBitOr(i1::ModelicaInteger, i2::ModelicaInteger)
local o::ModelicaInteger = i1 | i2
o
end
""" Returns bitwise 'xor' of Integers i1 and i2 """
function intBitXor(i1::ModelicaInteger, i2::ModelicaInteger)
local o::ModelicaInteger = i1 ⊻ i2
o
end
""" Returns bitwise left shift of Integer i by s bits """
function intBitLShift(i::ModelicaInteger, s::ModelicaInteger)
local o::ModelicaInteger = i << s
o
end
""" Returns bitwise right shift of Integer i by s bits """
function intBitRShift(i::ModelicaInteger, s::ModelicaInteger)
local o::ModelicaInteger = i >> s
o
end
""" Converts Integer to Real """
function intReal(i::ModelicaInteger)::Float64
Float64(i)
end
""" Converts Integer to String """
function intString(i::ModelicaInteger)::String
string(i)
end
function realAdd(r1::ModelicaReal, r2::ModelicaReal)::Float64
local r::ModelicaReal
r = r1 + r2
r
end
function realSub(r1::ModelicaReal, r2::ModelicaReal)::Float64
local r::ModelicaReal
r = r1 - r2
r
end
function realMul(r1::ModelicaReal, r2::ModelicaReal)::ModelicaReal
local r::ModelicaReal
r = r1 * r2
r
end
function realDiv(r1::ModelicaReal, r2::ModelicaReal)::ModelicaReal
local r::ModelicaReal
r = r1 / r2
r
end
function realMod(r1::ModelicaReal, r2::ModelicaReal)::ModelicaReal
local r::ModelicaReal
r = mod(r1, r2)
r
end
function realPow(r1::ModelicaReal, r2::ModelicaReal)::ModelicaReal
local r::ModelicaReal
r = r1^r2
r
end
function realMax(r1::ModelicaReal, r2::ModelicaReal)::ModelicaReal
local r::ModelicaReal
r = max(r1, r2)
end
function realMin(r1::ModelicaReal, r2::ModelicaReal)::ModelicaReal
local r::ModelicaReal
r = min(r1, r2)
end
function realAbs(x::ModelicaReal)::ModelicaReal
local y::ModelicaReal
y = abs(x)
end
function realNeg(x::ModelicaReal)::ModelicaReal
local y::ModelicaReal
y = -x
end
function realLt(x1::ModelicaReal, x2::ModelicaReal)::Bool
local b::Bool
b = x1 < x2
end
function realLe(x1::ModelicaReal, x2::ModelicaReal)::Bool
local b::Bool
b = x1 <= x2
end
function realEq(x1::ModelicaReal, x2::ModelicaReal)::Bool
local b::Bool
b = x1 == x2
end
function realNe(x1::ModelicaReal, x2::ModelicaReal)::Bool
local b::Bool
b = x1 != x2
end
function realGe(x1::ModelicaReal, x2::ModelicaReal)::Bool
local b::Bool
b = x1 >= x2
b
end
function realGt(x1::ModelicaReal, x2::ModelicaReal)::Bool
local b::Bool
b = x1 > x2
b
end
function realInt(r::ModelicaReal)
local i::ModelicaInteger
i = Integer(trunc(r))
end
function realString(r::ModelicaReal)::String
local str::String = string(r)
str
end
function stringCharInt(ch::String)
local i::ModelicaInteger = Int64(ch[1])
i
end
function intStringChar(i::ModelicaInteger)::String
local ch::String = string(Char(i))
ch
end
function stringInt(str::String)
local i::ModelicaInteger = Int64(str)
i
end
""" This function fails unless the whole string can be consumed by strtod without
setting errno. For more details, see man 3 strtod """
function stringReal(str::String)::ModelicaReal
local r::ModelicaReal = parse(Float64, str)
r
end
""" O(str) """
function stringListStringChar(str::String)::List{String}
local chars::List{String} = nil
for i = length(chars):-1:1
chars = _cons(string(str[i]), chars)
end
chars
end
""" O(str) """
function stringAppendList(strs::List)::String
local str::String = ""
for n in strs
str = str + n
end
str
end
""" O(str)
Takes a list of strings and a string delimiter and appends all
list elements with the string delimiter inserted between elements.
Example: stringDelimitList({\"x\",\"y\",\"z\"}, \", \") => \"x, y, z\"
"""
function stringDelimitList(strs::List, delimiter::String)::String
buffer = IOBuffer()
for (i,n) in enumerate(strs)
if i == 1
print(buffer, n)
else
print(buffer, delimiter)
print(buffer, n)
end
end
return String(take!(buffer))#str
end
stringDelimitList(lst::Nil, delim::String) = ""
""" O(1) """
function stringLength(str::String)
length(str)
end
""" O(1) """
function stringEmpty(str::String)::Bool
local isEmpty::Bool
isEmpty = stringLength(str) == 0
end
""" O(1) """
function stringGet(str::String, index::ModelicaInteger)
str[index]
end
""" O(1) """
function stringGetStringChar(str::String, index::ModelicaInteger)::String
if index < 0
println("stringGetStringChar: index < 0!")
fail()
end
local ch::String = string(str[index])
ch
end
""" O(n) """
function stringUpdateStringChar(str::String, newch::String, index::ModelicaInteger)::String
local news::String = str
if index < 0
println("stringUpdateStringChar: index < 0!")
fail()
end
news[index] = newch[1]
news
end
""" O(s1+s2) """
function stringAppend(s1::String, s2::String)::String
s1 * s2
end
""" O(N) """
function stringEq(s1::String, s2::String)::Bool
s1 == s2
end
""" O(N) """
function stringEqual(s1::String, s2::String)::Bool
s1 == s2
end
function stringCompare(s1::String, s2::String)
local res = cmp(s1, s2)
return res
end
function myhash(s::String)
local h::ModelicaInteger = mod(hash(s), typemax(ModelicaInteger))
h
end
function stringHash(str::String)
local h::ModelicaInteger = ModelicaInteger(myhash(str))
h
end
#= TODO: Defined in the runtime =#
function stringHashDjb2(str::String)
local h::ModelicaInteger = ModelicaInteger(myhash(str))
h
end
""" Does hashing+modulo without intermediate results. """
function stringHashDjb2Mod(str::String, m::ModelicaInteger)
local h::ModelicaInteger = mod(ModelicaInteger(myhash(str)), m)
h
end
function stringHashSdbm(str::String)
local h::ModelicaInteger = ModelicaInteger(myhash(str))
h
end
function substring(str::String, start::ModelicaInteger, stop::ModelicaInteger)::String #= stop index, first character is 1 =#
if start < 0
println("substring: start < 0!")
fail()
end
local out = str[start:stop]
out
end
""" O(1) ? """
function arrayLength(arr::Array{T}) where {T}
length(arr)
end
""" O(1) """
function arrayEmpty(arr::Array{A})::Bool where {A}
length(arr) == 0
end
""" O(1) """
function arrayGet(arr::Array{A}, index::ModelicaInteger) where {A}
if index < 0
println("arrayGet: index < 0!")
fail()
end
arr[index]
end
""" O(size) """
function arrayCreate(size::ModelicaInteger, initialValue::A) where {A}
fill(initialValue, size)
end
""" O(N) """
function arrayList(arr::Array{T}) where {T}
local lst::List{T} = nil
for i = length(arr):-1:1
lst = Cons{T}(arr[i], lst)
end
lst
end
""" O(n) """
function listArray(lst::Cons{T}) where {T}
local N = length(lst)
local arr::Vector{T} = Vector{T}(undef, N)
i = 1
while lst !== nil
arr[i] = lst.head
i += 1
lst = lst.tail
end
return arr
end
""" O(1) """
function listArray(lst::Nil)
[]
end
"""
O(n)
Same as listArray but with a dummy argument to specify the type.
"""
function listArray(lst::Cons{T}, ty) where {T}
local arr = Vector{ty}(undef, length(lst))
for i in lst
arr[i]
end
return arr
end
"""
Same as listArray but with a dummy argument to specify the type.
"""
function listArray(lst::Nil, ty)
ty[]
end
""" O(1) """
function arrayUpdate(arr::Array{A}, index::ModelicaInteger,
newValue::B)::Array{A} where {A,B}
#local newArray::Array{A} = arr #= same as the input array; used for folding =#
arr[index] = newValue
#= Defined in the runtime =#
arr #= same as the input array; used for folding =#
end
""" O(n) """
function arrayCopy(arr::Array{A})::Array{A} where {A}
copy(arr)
end
""" Appends arr2 to arr1. O(length(arr1) + length(arr2)).
Note that this operation is *not* destructive, i.e. a new array is created. """
function arrayAppend(arr1::Array{A}, arr2::Array{A})::Array{A} where {A}
local arr::Array{A}
@error "Defined in the runtime"
fail()
end
"""
Returns the string representation of any value.
Rather slow; only use this for debugging!
"""
function anyString(a::A) where {A}
string(dump(a))
end
""" print(anyString(a)), but to stderr """
function printAny(a::A) where {A}
println(dump(a))
end
""" For RML compatibility """
function debug_print(str::String, a::A) where {A}
#= Defined in the runtime =#
println(str)
@show a
end
let tickCounter = 0
global function tick()
tickCounter += 1
end
global function resetTick()
tickCounter = 0
end
end
function equality(a1::A1, a2::A2) where {A1,A2}
#= Defined in the runtime =#
if !valueEq(a1, a2)
fail()
end
end
# cannot use nothing in the globalRoots array
# as NONE() is nothing so we use the struct below
# to signal when an element is not set
""" Sets the index of the root variable with index 1..1024
This is a global mutable value and should be used sparingly.
You are recommended not to use "missing" the runtime system treats this values as uninitialized and fail getGlobalRoot later on.
"""
const global globalRoots::Vector{Any} = Vector{Any}(missing, 1024)
function setGlobalRoot(index::ModelicaInteger, value::T) where {T}
if index > 1023 || index < 0
fail()
end
globalRoots[index+1] = value
end
function getGlobalRoot(index::ModelicaInteger)
if index > 1023 || index < 0
fail()
end
val = globalRoots[index+1]
if ismissing(val)
fail()
end
val
end
""" The return-value is compiler-dependent on the runtime implementation of
boxed values. The number of bits reserved for the constructor is generally
between 6 and 8 bits. """
function valueConstructor(value::A) where {A}
# hack! hack! hack!
local ctor::ModelicaInteger = myhash(string(typeof(value)))
ctor
end
""" The number of slots a boxed value has. This is dependent on sizeof(void*)
on the architecture in question. """
function valueSlots(value::A) where {A}
local slots::ModelicaInteger = 0
try
slots = nfields(value)
catch ex
# do nothing
end
slots
end
""" Structural equality """
function valueEq(a1::A, a2::B)::Bool where {A,B}
local b::Bool = @match (a1, a2) begin
(SOME(x1), SOME(x2)) => valueEq(x1, x2)
(_, _) => a1 === a2
end
b
end
""" a1 > a2? """
function valueCompare(a1::A, a2::A) where {A}
local i::ModelicaInteger = if valueConstructor(a1) < valueConstructor(a2)
-1
elseif valueConstructor(a1) > valueConstructor(a2)
1
else
0
end
i #= -1, 0, 1 =#
end
function valueHashMod(value::A, mod::ModelicaInteger) where {A}
local h::ModelicaInteger = mod(ModelicaInteger(myhash(string(value))), m)
h
end
""" This is a very fast comparison of two values which only checks if the pointers are equal. """
function referenceEq(a1::A1, a2::A2)::Bool where {A1,A2}
#TODO: Should be like this?
a1 === a2
end
""" Returns the pointer address of a reference as a hexadecimal string that can
be used for debugging. """
function referencePointerString(ref::A)::String where {A}
local str::String
@assert false "not implemented"
str
end
""" Use the diff to compare two time samples to each other. Not very accurate. """
function clock()::ModelicaReal
local t::ModelicaReal
@assert false "not implemented"
t
end
""" Returns true if the input is NONE() """
function isNone(opt::Option{A})::Bool where {A}
(opt === nothing) # isa(opt, NONE))
end
""" Returns true if the input is SOME() """
function isSome(opt::Option{A})::Bool where {A}
isa(opt, SOME)
end
function listStringCharString(strs::List{String})::String
local str::String
@assert false "not implemented"
str
end
function stringCharListString(strs::List{String})::String
local str::String = ""
for s in strs
str = str + s
end
str
end
function fail()
throw(MetaModelicaGeneralException("Runtime defined generic Meta Modelica failure"))
end
function fail(msg::String)
throw(MetaModelicaGeneralException(msg))
end
""" Sets the stack overflow signal to the given value and returns the old one """
function setStackOverflowSignal(inSignal::Bool)::Bool
local outSignal::Bool
outSignal = inSignal
outSignal
end
function referenceDebugString(functionSymbol::A)::String where {A}
local name::String
@assert false "not implemented"
name
end
""" TODO: I am far from sure that this will fly.. in Julia.
The code generated from the transpiler is correct however"""
function isPresent(ident::T)::Bool where {T}
local b::Bool
b = true
end
#= The Info attribute provides location information for elements and classes. =#
@Uniontype SourceInfo begin
@Record SOURCEINFO begin
fileName::String #= fileName where the class is defined in =#
isReadOnly::Bool #= isReadOnly : (true|false). Should be true for libraries =#
lineNumberStart::ModelicaInteger #= lineNumberStart =#
columnNumberStart::ModelicaInteger #= columnNumberStart =#
lineNumberEnd::ModelicaInteger #= lineNumberEnd =#
columnNumberEnd::ModelicaInteger #= columnNumberEnd =#
lastModification::ModelicaReal #= mtime in stat(2), stored as a double for increased precision on 32-bit platforms =#
end
end
SOURCEINFO(fileName::String, isReadOnly::Bool, lineNumberStart::ModelicaInteger,
columnNumberSTart::ModelicaInteger, lineNumberEnd::ModelicaInteger,
columnNumberEnd::ModelicaInteger) =
let
#=No source info=#
SOURCEINFO(fileName, isReadOnly, lineNumberStart, columnNumberSTart, lineNumberEnd,
columnNumberEnd, 0.0)
end
function sourceInfo()::SourceInfo
local info::SourceInfo
#= Defined in the runtime =#
SOURCEINFO("", true, 1, 2, 3, 4, 0.0)
end
Base.:+(x::String, y::String) =
let
x * y
end
""" Imports and prints if the import is sucessful """
macro importDBG(moduleName)
quote
import $moduleName
x = string(@__MODULE__)
y = string($(esc(moduleName)))
println("Importing " * y * " in " * x)
end
end
const NOT_IMPLEMENTED_MSG::String = "__NOT_IMPLEMENTED__"
function getInstanceName()::String
return NOT_IMPLEMENTED_MSG
end
function StringFunction(i::Int64)::String
intString(i)
end
function StringFunction(r::Float64)::String
realString(r)
end
function String(arg)::String
return string(arg)
end