Commit f650fc7b authored by Camil Staps's avatar Camil Staps 🍃

Interpreter generator: rename HWord to PtrOffset

parent 1ff8b9dc
Pipeline #23825 failed with stages
in 13 minutes and 47 seconds
......@@ -18,13 +18,12 @@ instr_mulUUL :: !Target -> Target
instr_RtoAC :: !Target -> Target
lit_word :: !Int -> Expr TWord
lit_hword :: !Int -> Expr THWord
lit_hword :: !Int -> Expr TPtrOffset
lit_char :: !Char -> Expr TChar
lit_short :: !Int -> Expr TShort
lit_int :: !Int -> Expr TInt
instance to_word TWord, TChar, TInt, TShort, (TPtr t), TReal
instance to_hword TWord, THWord, TShort
instance to_bool TWord
instance to_char TWord
instance to_int TWord
......@@ -32,6 +31,7 @@ instance to_real TWord
instance to_word_ptr TWord, (TPtr t)
instance to_char_ptr TWord, (TPtr t)
instance to_short_ptr TWord, (TPtr t)
instance to_ptr_offset TWord, TPtrOffset, TShort
instance + (Expr t)
instance - (Expr t)
......@@ -86,22 +86,22 @@ nop :: !Target -> Target
(:.) infixr 1 :: !(Target -> Target) !(Target -> Target) !Target -> Target
class typename t :: t -> String
instance typename TWord, THWord, TChar, TShort, TInt, TReal, (TPtr t) | typename t
instance typename TWord, TPtrOffset, TChar, TShort, TInt, TReal, (TPtr t) | typename t
new_local :: !t !(Expr t) !((Expr t) Target -> Target) !Target -> Target | typename t
class (.=) infix 2 v e :: !(Expr v) !(Expr e) !Target -> Target
instance .=
TWord TWord, TWord THWord, TWord TBool, TWord TChar, TWord TInt, TWord TShort,
THWord THWord,
TWord TWord, TWord TPtrOffset, TWord TBool, TWord TChar, TWord TInt, TWord TShort,
TPtrOffset TPtrOffset,
TChar TChar,
TInt TInt, TInt TWord,
(TPtr t) (TPtr u) // NB/TODO: no checking on child types!
class (+=) infix 2 v e :: !(Expr v) !(Expr e) !Target -> Target
instance += TWord TWord, THWord THWord
instance += TWord TWord, TPtrOffset TPtrOffset
class (-=) infix 2 v e :: !(Expr v) !(Expr e) !Target -> Target
instance -= TWord TWord, THWord THWord, TShort TShort
instance -= TWord TWord, TPtrOffset TPtrOffset, TShort TShort
class advance_ptr i :: !(Expr (TPtr v)) !i !Target -> Target
instance advance_ptr Int, (Expr w)
......@@ -154,8 +154,8 @@ caf_list :: Expr (TPtr TWord)
push_c :: !(Expr (TPtr TWord)) !Target -> Target
pop_pc_from_c :: !Target -> Target
memcpy :: !(Expr (TPtr a)) !(Expr (TPtr b)) !(Expr THWord) !Target -> Target
strncmp :: !(Expr (TPtr TChar)) !(Expr (TPtr TChar)) !(Expr THWord) -> Expr TInt
memcpy :: !(Expr (TPtr a)) !(Expr (TPtr b)) !(Expr TPtrOffset) !Target -> Target
strncmp :: !(Expr (TPtr TChar)) !(Expr (TPtr TChar)) !(Expr TPtrOffset) -> Expr TInt
putchar :: !(Expr TChar) !Target -> Target
print_bool :: !(Expr TWord) !Target -> Target
......
......@@ -185,7 +185,7 @@ instr_RtoAC t = foldl (flip append) t
lit_word :: !Int -> Expr TWord
lit_word i = toString i
lit_hword :: !Int -> Expr THWord
lit_hword :: !Int -> Expr TPtrOffset
lit_hword i = toString i
lit_char :: !Char -> Expr TChar
......@@ -204,10 +204,6 @@ instance to_word TShort where to_word e = "(BC_WORD)("+-+e+-+")"
instance to_word (TPtr t) where to_word e = "(BC_WORD)("+-+e+-+")"
instance to_word TReal where to_word e = "*(BC_WORD*)&("+-+e+-+")"
instance to_hword TWord where to_hword w = "(int)("+-+w+-+")"
instance to_hword THWord where to_hword w = w
instance to_hword TShort where to_hword s = "(int)("+-+s+-+")"
instance to_bool TWord
where
to_bool e = e`
......@@ -228,6 +224,10 @@ instance to_char_ptr (TPtr t) where to_char_ptr e = "(char*)("+-+e+-+")"
instance to_short_ptr TWord where to_short_ptr e = "(int16_t*)("+-+e+-+")"
instance to_short_ptr (TPtr t) where to_short_ptr e = "(int16_t*)("+-+e+-+")"
instance to_ptr_offset TWord where to_ptr_offset w = "(int)("+-+w+-+")"
instance to_ptr_offset TPtrOffset where to_ptr_offset w = w
instance to_ptr_offset TShort where to_ptr_offset s = "(int)("+-+s+-+")"
instance + (Expr t) where + a b = "("+-+a+-+"+"+-+b+-+")"
instance - (Expr t) where - a b = "("+-+a+-+"-"+-+b+-+")"
instance * (Expr t) where * a b = "("+-+a+-+"*"+-+b+-+")"
......@@ -346,7 +346,7 @@ nop t = t
(:.) first then t = then (first t)
instance typename TWord where typename _ = "BC_WORD"
instance typename THWord where typename _ = "int"
instance typename TPtrOffset where typename _ = "int"
instance typename TChar where typename _ = "char"
instance typename TShort where typename _ = "int16_t"
instance typename TInt where typename _ = "BC_WORD_S"
......@@ -362,12 +362,12 @@ set :: !(Expr v) !(Expr e) !Target -> Target
set v e t = append ("\t"+-+v+-+"="+-+e+-+";") t
instance .= TWord TWord where .= v e t = set v e t
instance .= TWord THWord where .= v e t = set v e t
instance .= TWord TPtrOffset where .= v e t = set v e t
instance .= TWord TBool where .= v e t = set v e t
instance .= TWord TChar where .= v e t = set v e t
instance .= TWord TInt where .= v e t = set v e t
instance .= TWord TShort where .= v e t = set v e t
instance .= THWord THWord where .= v e t = set v e t
instance .= TPtrOffset TPtrOffset where .= v e t = set v e t
instance .= TChar TChar where .= v e t = set v e t
instance .= TInt TInt where .= v e t = set v e t
instance .= TInt TWord where .= v e t = set v e t
......@@ -380,7 +380,7 @@ add_local v e t = case e of
e -> append ("\t"+-+v+-+"+="+-+e+-+";") t
instance += TWord TWord where += v e t = add_local v e t
instance += THWord THWord where += v e t = add_local v e t
instance += TPtrOffset TPtrOffset where += v e t = add_local v e t
sub_local :: !(Expr v) !(Expr e) !Target -> Target
sub_local v e t = case e of
......@@ -389,7 +389,7 @@ sub_local v e t = case e of
e -> append ("\t"+-+v+-+"-="+-+e+-+";") t
instance -= TWord TWord where -= v e t = sub_local v e t
instance -= THWord THWord where -= v e t = sub_local v e t
instance -= TPtrOffset TPtrOffset where -= v e t = sub_local v e t
instance -= TShort TShort where -= v e t = sub_local v e t
instance advance_ptr Int where advance_ptr v e t = add_local v (toString e) t
......@@ -501,10 +501,10 @@ push_c v t = append ("\t*++csp=(BC_WORD)"+-+v+-+";") t
pop_pc_from_c :: !Target -> Target
pop_pc_from_c t = append "\tpc=(BC_WORD*)*csp--;" t
memcpy :: !(Expr (TPtr a)) !(Expr (TPtr b)) !(Expr THWord) !Target -> Target
memcpy :: !(Expr (TPtr a)) !(Expr (TPtr b)) !(Expr TPtrOffset) !Target -> Target
memcpy d s n t = append ("\tmemcpy("+-+d+-+","+-+s+-+","+-+n+-+");") t
strncmp :: !(Expr (TPtr TChar)) !(Expr (TPtr TChar)) !(Expr THWord) -> Expr TInt
strncmp :: !(Expr (TPtr TChar)) !(Expr (TPtr TChar)) !(Expr TPtrOffset) -> Expr TInt
strncmp s1 s2 n = "strncmp("+-+s1+-+","+-+s2+-+","+-+n+-+")"
putchar :: !(Expr TChar) !Target -> Target
......
......@@ -8,7 +8,7 @@ import interpretergen
class wasm_type a :: !a -> Type
instance wasm_type TWord, THWord, TBool, TChar, TShort, TInt, TReal, (TPtr t)
instance wasm_type TWord, TPtrOffset, TBool, TChar, TShort, TInt, TReal, (TPtr t)
:: Target
:: Expr t
......@@ -24,13 +24,12 @@ instr_mulUUL :: !Target -> Target
instr_RtoAC :: !Target -> Target
lit_word :: !Int -> Expr TWord
lit_hword :: !Int -> Expr THWord
lit_hword :: !Int -> Expr TPtrOffset
lit_char :: !Char -> Expr TChar
lit_short :: !Int -> Expr TShort
lit_int :: !Int -> Expr TInt
instance to_word THWord, TChar, TInt, TShort, (TPtr t), TReal
instance to_hword TWord, THWord, TShort
instance to_word TPtrOffset, TChar, TInt, TShort, (TPtr t), TReal
instance to_bool TWord
instance to_char TWord
instance to_int TWord
......@@ -38,6 +37,7 @@ instance to_real TWord
instance to_word_ptr TWord, (TPtr t)
instance to_char_ptr TWord, (TPtr t)
instance to_short_ptr TWord, (TPtr t)
instance to_ptr_offset TWord, TPtrOffset, TShort
instance + (Expr t)
instance - (Expr t)
......@@ -95,23 +95,23 @@ new_local :: !t !(Expr t) !((Expr t) Target -> Target) !Target -> Target | wasm_
class (.=) infix 2 v e :: !(Expr v) !(Expr e) !Target -> Target
instance .=
TWord TWord, TWord THWord, TWord TBool, TWord TChar, TWord TInt, TWord TShort,
THWord THWord,
TWord TWord, TWord TPtrOffset, TWord TBool, TWord TChar, TWord TInt, TWord TShort,
TPtrOffset TPtrOffset,
TChar TChar,
TInt TInt, TInt TWord,
(TPtr t) (TPtr u)
class (+=) infix 2 v e :: !(Expr v) !(Expr e) !Target -> Target
instance += TWord TWord, THWord THWord
instance += TWord TWord, TPtrOffset TPtrOffset
class (-=) infix 2 v e :: !(Expr v) !(Expr e) !Target -> Target
instance -= TWord TWord, THWord THWord, TShort TShort
instance -= TWord TWord, TPtrOffset TPtrOffset, TShort TShort
class advance_ptr i :: !(Expr (TPtr v)) !i !Target -> Target | wasm_type v
instance advance_ptr Int, (Expr THWord)
instance advance_ptr Int, (Expr TPtrOffset)
class rewind_ptr i :: !(Expr (TPtr v)) !i !Target -> Target | wasm_type v
instance rewind_ptr Int, (Expr THWord)
instance rewind_ptr Int, (Expr TPtrOffset)
class (@) infix 8 a :: !(Expr (TPtr t)) !a -> Expr t | wasm_type t
class (@?) infix 8 a :: !(Expr (TPtr t)) !a -> Expr (TPtr t) | wasm_type t
......@@ -133,7 +133,7 @@ if_then_else ::
if_break_else :: !(Expr TBool) !(Target -> Target) !Target -> Target
class ensure_hp s :: !s !Target -> Target
instance ensure_hp Int, (Expr t) | to_hword t
instance ensure_hp Int, (Expr t) | to_ptr_offset t
A :: Expr (TPtr TWord)
B :: Expr (TPtr TWord)
......@@ -158,8 +158,8 @@ caf_list :: Expr (TPtr TWord)
push_c :: !(Expr (TPtr TWord)) !Target -> Target
pop_pc_from_c :: !Target -> Target
memcpy :: !(Expr (TPtr a)) !(Expr (TPtr b)) !(Expr THWord) !Target -> Target
strncmp :: !(Expr (TPtr TChar)) !(Expr (TPtr TChar)) !(Expr THWord) -> Expr TInt
memcpy :: !(Expr (TPtr a)) !(Expr (TPtr b)) !(Expr TPtrOffset) !Target -> Target
strncmp :: !(Expr (TPtr TChar)) !(Expr (TPtr TChar)) !(Expr TPtrOffset) -> Expr TInt
putchar :: !(Expr TChar) !Target -> Target
print_bool :: !(Expr TWord) !Target -> Target
......
......@@ -36,7 +36,7 @@ where
I16 -> "i16"
instance wasm_type TWord where wasm_type _ = I64
instance wasm_type THWord where wasm_type _ = I32
instance wasm_type TPtrOffset where wasm_type _ = I32
instance wasm_type TBool where wasm_type _ = I32
instance wasm_type TChar where wasm_type _ = I8
instance wasm_type TShort where wasm_type _ = I16
......@@ -486,8 +486,8 @@ instr_mulUUL t = instr_unimplemented t // TODO
instr_RtoAC :: !Target -> Target
instr_RtoAC t = (
new_local TReal (to_real (B @ 0)) \r ->
new_local THWord (Ecall "clean_RtoAC_words_needed" (r -- ELNil)) \lw ->
//ensure_hp (lw ::: THWord) :. // TODO
new_local TPtrOffset (Ecall "clean_RtoAC_words_needed" (r -- ELNil)) \lw ->
//ensure_hp (lw ::: TPtrOffset) :. // TODO
A @ 1 .= to_word Hp :.
Hp .= (Ecall "clean_RtoAC" (Hp -- r -- ELNil) ::: TPtr TWord) :.
advance_ptr Pc 1 :.
......@@ -501,7 +501,7 @@ where
lit_word :: !Int -> Expr TWord
lit_word w = Econst I64 w
lit_hword :: !Int -> Expr THWord
lit_hword :: !Int -> Expr TPtrOffset
lit_hword w = Econst I32 w
lit_char :: !Char -> Expr TChar
......@@ -514,17 +514,13 @@ lit_int :: !Int -> Expr TInt
lit_int i = Econst I64 i
instance to_word TBool where to_word c = Eextend I64 I32 c
instance to_word THWord where to_word c = Eextend I64 I32 c
instance to_word TPtrOffset where to_word c = Eextend I64 I32 c
instance to_word TChar where to_word c = cast_expr c
instance to_word TInt where to_word i = cast_expr i
instance to_word TShort where to_word s = cast_expr s
instance to_word (TPtr t) where to_word p = Eextend I64 I32 p
instance to_word TReal where to_word r = Ereinterpret I64 F64 r
instance to_hword TWord where to_hword w = Ewrap I32 I64 w
instance to_hword THWord where to_hword w = w
instance to_hword TShort where to_hword s = Ewrap I32 I64 s
instance to_bool TWord where to_bool w = Ewrap I32 I64 w
instance to_char TWord where to_char w = cast_expr w
......@@ -540,6 +536,10 @@ instance to_char_ptr (TPtr t) where to_char_ptr p = cast_expr p
instance to_short_ptr TWord where to_short_ptr w = Ewrap I32 I64 w
instance to_short_ptr (TPtr t) where to_short_ptr p = cast_expr p
instance to_ptr_offset TWord where to_ptr_offset w = Ewrap I32 I64 w
instance to_ptr_offset TPtrOffset where to_ptr_offset w = w
instance to_ptr_offset TShort where to_ptr_offset s = Ewrap I32 I64 s
cast_expr :: !Ex -> Ex
cast_expr e = e
......@@ -675,13 +675,13 @@ where
-> Estore vartype loadtype offset addr e
instance .= TWord TWord where .= v e t = set v e t
instance .= TWord THWord where .= v e t = set v (to_word e) t
instance .= TWord TPtrOffset where .= v e t = set v (to_word e) t
instance .= TWord TBool where .= v e t = set v (to_word e) t
instance .= TWord TChar where .= v e t = set v (to_word e) t
instance .= TWord TInt where .= v e t = set v (to_word e) t
instance .= TWord TShort where .= v e t = set v (to_word e) t
instance .= THWord THWord where .= v e t = set v e t
instance .= TPtrOffset TPtrOffset where .= v e t = set v e t
instance .= TChar TChar where .= v e t = set v e t
......@@ -719,10 +719,10 @@ var_sub v e t = case e of
ld = Eload localtype storetype Signed offset addr
instance += TWord TWord where += var val t = var_add var val t
instance += THWord THWord where += var val t = var_add var val t
instance += TPtrOffset TPtrOffset where += var val t = var_add var val t
instance -= TWord TWord where -= var val t = var_sub var val t
instance -= THWord THWord where -= var val t = var_sub var val t
instance -= TPtrOffset TPtrOffset where -= var val t = var_sub var val t
instance -= TShort TShort where -= var val t = var_sub var val t
instance advance_ptr Int
......@@ -731,7 +731,7 @@ where
where
w = type_width (wasm_type (get_type_of_ptr v))
instance advance_ptr (Expr THWord)
instance advance_ptr (Expr TPtrOffset)
where
advance_ptr v e t = var_add v (Eshl I32 e (Econst I32 s)) t
where
......@@ -743,7 +743,7 @@ where
where
w = type_width (wasm_type (get_type_of_ptr v))
instance rewind_ptr (Expr THWord)
instance rewind_ptr (Expr TPtrOffset)
where
rewind_ptr v e t = var_sub v (Eshl I32 e (Econst I32 s)) t
where
......@@ -838,14 +838,14 @@ where
if_break_else :: !(Expr TBool) !(Target -> Target) !Target -> Target
if_break_else c else t = else (append (Ebr_if 0 c) t)
instance ensure_hp (Expr t) | to_hword t
instance ensure_hp (Expr t) | to_ptr_offset t
where
ensure_hp i t = if_then_else
(Elt I32 Signed Hp_free (Econst I32 0))
(Hp_free += to_hword i :. append (Ebr "abc-gc"))
(Hp_free += to_ptr_offset i :. append (Ebr "abc-gc"))
[]
Nothing
((Hp_free .= Hp_free - to_hword i) t)
((Hp_free .= Hp_free - to_ptr_offset i) t)
instance ensure_hp Int
where
......@@ -856,7 +856,7 @@ where
Nothing
((Hp_free .= Hp_free - ie) t)
where
ie :: Expr THWord
ie :: Expr TPtrOffset
ie = Econst I32 i
A :: Expr (TPtr TWord)
......@@ -871,7 +871,7 @@ Pc = Ivar (Local "pc")
Hp :: Expr (TPtr TWord)
Hp = Ivar (Local "hp")
Hp_free :: Expr THWord
Hp_free :: Expr TPtrOffset
Hp_free = Ivar (Local "hp-free")
BOOL_ptr :: Expr TWord
......@@ -938,10 +938,10 @@ where
popped_pc :: Expr (TPtr TWord)
popped_pc = Eload I32 I32 DontCare 0 (Eget C)
memcpy :: !(Expr (TPtr a)) !(Expr (TPtr b)) !(Expr THWord) !Target -> Target
memcpy :: !(Expr (TPtr a)) !(Expr (TPtr b)) !(Expr TPtrOffset) !Target -> Target
memcpy d s n t = append (Ecall "clean_memcpy" (d -- s -- n -- ELNil)) t
strncmp :: !(Expr (TPtr TChar)) !(Expr (TPtr TChar)) !(Expr THWord) -> Expr TInt
strncmp :: !(Expr (TPtr TChar)) !(Expr (TPtr TChar)) !(Expr TPtrOffset) -> Expr TInt
strncmp s1 s2 n = Eextend I64 I32 (Ecall "clean_strncmp" (s1 -- s2 -- n -- ELNil))
putchar :: !(Expr TChar) !Target -> Target
......
......@@ -9,7 +9,6 @@ from target import :: Expr
}
:: TWord = TWord
:: THWord = THWord
:: TBool = TBool
:: TChar = TChar
:: TShort = TShort
......@@ -17,9 +16,9 @@ from target import :: Expr
:: TReal = TReal
:: TPtr t = TPtr !t
:: TPtrOffset = TPtrOffset
class to_word t :: !(Expr t) -> Expr TWord
class to_hword t :: !(Expr t) -> Expr THWord
class to_bool t :: !(Expr t) -> Expr TBool
class to_char t :: !(Expr t) -> Expr TChar
class to_short t :: !(Expr t) -> Expr TShort
......@@ -30,3 +29,5 @@ class to_word_ptr t :: !(Expr t) -> Expr (TPtr TWord)
class to_char_ptr t :: !(Expr t) -> Expr (TPtr TChar)
class to_short_ptr t :: !(Expr t) -> Expr (TPtr TShort)
class to_int_ptr t :: !(Expr t) -> Expr (TPtr TInt)
class to_ptr_offset t :: !(Expr t) -> Expr TPtrOffset
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment