From da5080afbaf7123535b33ba052eb702611e8f84c Mon Sep 17 00:00:00 2001 From: Adrian Pop Date: Sat, 16 Nov 2019 00:14:22 +0200 Subject: [PATCH 1/2] implement more of the MM runtime --- src/metaRuntime.jl | 94 +++++++++++++++++++--------------------------- 1 file changed, 38 insertions(+), 56 deletions(-) diff --git a/src/metaRuntime.jl b/src/metaRuntime.jl index 7ac4baa..cccc249 100644 --- a/src/metaRuntime.jl +++ b/src/metaRuntime.jl @@ -165,52 +165,43 @@ end """ Returns bitwise inverted Integer number of i """ function intBitNot(i::ModelicaInteger)::ModelicaInteger - local o::ModelicaInteger - #= Defined in the runtime =# + local o::ModelicaInteger = ~i o end """ Returns bitwise \'and\' of Integers i1 and i2 """ function intBitAnd(i1::ModelicaInteger, i2::ModelicaInteger)::ModelicaInteger - local o::ModelicaInteger - #= Defined in the runtime =# + local o::ModelicaInteger = i1 & i2 o end """ Returns bitwise 'or' of Integers i1 and i2 """ function intBitOr(i1::ModelicaInteger, i2::ModelicaInteger)::ModelicaInteger - local o::ModelicaInteger - #= Defined in the runtime =# + local o::ModelicaInteger = i1 | i2 o end """ Returns bitwise 'xor' of Integers i1 and i2 """ function intBitXor(i1::ModelicaInteger, i2::ModelicaInteger)::ModelicaInteger - local o::ModelicaInteger - - #= Defined in the runtime =# + local o::ModelicaInteger = i1 ⊻ i2 o end """ Returns bitwise left shift of Integer i by s bits """ function intBitLShift(i::ModelicaInteger, s::ModelicaInteger)::ModelicaInteger - local o::ModelicaInteger - - #= Defined in the runtime =# + local o::ModelicaInteger = i << i o end """ Returns bitwise right shift of Integer i by s bits """ function intBitRShift(i::ModelicaInteger, s::ModelicaInteger)::ModelicaInteger - local o::ModelicaInteger - - #= Defined in the runtime =# + local o::ModelicaInteger = i >> s o end """ Converts Integer to Real """ function intReal(i::ModelicaInteger)::ModelicaReal - float(i) + Float64(i) end """ Converts Integer to String """ @@ -325,40 +316,33 @@ function realString(r::ModelicaReal)::String end function stringCharInt(ch::String)::ModelicaInteger - local i::ModelicaInteger - - #= Defined in the runtime =# + local i::ModelicaInteger = Int64(char(ch[1])) i end function intStringChar(i::ModelicaInteger)::String - local ch::String - - #= Defined in the runtime =# + local ch::String = string(char(i)) ch end function stringInt(str::String)::ModelicaInteger - local i::ModelicaInteger - - #= Defined in the runtime =# + 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 - - #= Defined in the runtime =# + local r::ModelicaReal = Float64(str) r end """ O(str) """ function stringListStringChar(str::String)::List{String} - local chars::List{String} - - #= Defined in the runtime =# + local chars::List{String} = nil + for i in length(chars):-1:1 + chars = _cons(string(str[i]), chars) + end chars end @@ -418,17 +402,14 @@ end """ O(1) """ function stringGetStringChar(str::String, index::ModelicaInteger)::String - local ch::String - - #= Defined in the runtime =# + local ch::String = string(str[index]) ch end """ O(n) """ function stringUpdateStringChar(str::String, newch::String, index::ModelicaInteger)::String - local news::String - - #= Defined in the runtime =# + local news::String = str + news[index] = newch[1] news end @@ -617,8 +598,8 @@ end boxed values. The number of bits reserved for the constructor is generally between 6 and 8 bits. """ function valueConstructor(value::A)::ModelicaInteger where {A} - local ctor::ModelicaInteger - #= Defined in the runtime =# + # hack! hack! hack! + local ctor::ModelicaInteger = integer(hash(string(typeof(value)))) ctor end @@ -626,7 +607,7 @@ end on the architecture in question. """ function valueSlots(value::A)::ModelicaInteger where {A} local slots::ModelicaInteger - #= Defined in the runtime =# + @assert false "not implemented, use valueEq to compare objects" slots end @@ -642,14 +623,19 @@ end """ a1 > a2? """ function valueCompare(a1::A, a2::A)::ModelicaInteger where {A} - local i #= -1, 0, 1 =#::ModelicaInteger - @assert (false) - #= Defined in the runtime =# + 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)::ModelicaInteger where {A} - local h::ModelicaInteger = mod(ModelicaInteger(myhash(value)), m) + local h::ModelicaInteger = mod(ModelicaInteger(myhash(string(value))), m) h end @@ -663,16 +649,14 @@ end be used for debugging. """ function referencePointerString(ref::A)::String where {A} local str::String - - #= Defined in the runtime =# + @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 - - #= Defined in the runtime =# + @assert false "not implemented" t end @@ -688,15 +672,15 @@ end function listStringCharString(strs::List{String})::String local str::String - - #= Defined in the runtime =# + @assert false "not implemented" str end function stringCharListString(strs::List{String})::String - local str::String - - #= Defined in the runtime =# + local str::String = "" + for s in strs + str = str + s + end str end @@ -714,15 +698,13 @@ end function referenceDebugString(functionSymbol::A)::String where {A} local name::String - - #= Defined in the runtime =# + @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 b end From b267174063c5a7ebe84ddaefda19346841392fb2 Mon Sep 17 00:00:00 2001 From: Adrian Pop Date: Wed, 29 Jan 2020 20:45:51 +0100 Subject: [PATCH 2/2] more fixes for the runtime --- src/matchcontinue.jl | 6 +++++- src/metaRuntime.jl | 10 +++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/matchcontinue.jl b/src/matchcontinue.jl index b86d52a..9a532d7 100644 --- a/src/matchcontinue.jl +++ b/src/matchcontinue.jl @@ -316,7 +316,11 @@ function handle_match_case(value, case, tail, asserts, matchcontinue::Bool) # showerror(stderr, e, catch_backtrace()) #end if !isa(e, MetaModelicaException) && !isa(e, ImmutableListException) - showerror(stderr, e, catch_backtrace()) + if isa(e, MatchFailure) + println("MatchFailure:" + e.msg) + else + showerror(stderr, e, catch_backtrace()) + end rethrow(e) end __omc_match_done = false diff --git a/src/metaRuntime.jl b/src/metaRuntime.jl index cccc249..f4adae3 100644 --- a/src/metaRuntime.jl +++ b/src/metaRuntime.jl @@ -599,15 +599,19 @@ boxed values. The number of bits reserved for the constructor is generally between 6 and 8 bits. """ function valueConstructor(value::A)::ModelicaInteger where {A} # hack! hack! hack! - local ctor::ModelicaInteger = integer(hash(string(typeof(value)))) + 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)::ModelicaInteger where {A} - local slots::ModelicaInteger - @assert false "not implemented, use valueEq to compare objects" + local slots::ModelicaInteger = 0 + try + slots = nfields(value) + catch ex + # do nothing + end slots end