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

Optimize wasm interpreter, step 1: syntax tree better suited for...

Optimize wasm interpreter, step 1: syntax tree better suited for optimizations; reduce number of wraps
parent 2c04e70d
Pipeline #23324 failed with stages
in 20 minutes and 14 seconds
......@@ -122,7 +122,7 @@ int add_strtab_section(Elf *e) {
uint64_t *find_start_of_br_table(uint64_t *code, unsigned int size) {
unsigned int optimum=0;
unsigned int opt_size=1;
unsigned int opt_size=10; /* to decrease verbosity */
uint8_t *code8=(uint8_t*)code;
size<<=3;
......@@ -134,7 +134,7 @@ uint64_t *find_start_of_br_table(uint64_t *code, unsigned int size) {
for (; j<(size-i)>>3; j++)
if ((code[j]>>24)!=this_high)
break;
if (j > opt_size) {
if (j >= opt_size) {
opt_size=j;
optimum=i;
fprintf(stderr,"new optimum %d at %d: 0x%010lx......\n",opt_size,optimum,this_high);
......@@ -155,7 +155,7 @@ char *find_movabs_of_br_table(char *code, unsigned int size, uint64_t high_bytes
if (code[i]==0x48)
return &code[i];
if (code[i]==0x49)
return &code[i+1];
return &code[i];
}
}
......
......@@ -4,6 +4,9 @@ import StdEnv
import StdMaybe
import interpretergen
:: THWord :== TWord
THWord :== TWord
:: Target
:: Expr t
......@@ -18,22 +21,17 @@ instr_mulUUL :: !Target -> Target
instr_RtoAC :: !Target -> Target
lit_word :: !Int -> Expr TWord
lit_hword :== lit_word
lit_char :: !Char -> Expr TChar
lit_short :: !Int -> Expr TShort
lit_int :: !Int -> Expr TInt
instance to_word TChar
instance to_word TInt
instance to_word TShort
instance to_word (TPtr t)
instance to_word TReal
instance to_word TWord, TChar, TInt, TShort, (TPtr t), TReal
to_hword :== to_word
instance to_bool TWord
instance to_char TWord
instance to_int TWord
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)
......@@ -45,15 +43,14 @@ instance / (Expr t)
instance ^ (Expr TReal)
(%.) infixl 6 :: !(Expr TInt) !(Expr TInt) -> Expr TInt
(==.) infix 4 :: !(Expr a) !(Expr a) -> Expr TWord
(<>.) infix 4 :: !(Expr a) !(Expr a) -> Expr TWord
(<.) infix 4 :: !(Expr a) !(Expr a) -> Expr TWord
(>.) infix 4 :: !(Expr a) !(Expr a) -> Expr TWord
(<=.) infix 4 :: !(Expr a) !(Expr a) -> Expr TWord
(>=.) infix 4 :: !(Expr a) !(Expr a) -> Expr TWord
(==.) infix 4 :: !(Expr a) !(Expr a) -> Expr TBool
(<>.) infix 4 :: !(Expr a) !(Expr a) -> Expr TBool
(<.) infix 4 :: !(Expr a) !(Expr a) -> Expr TBool
(>.) infix 4 :: !(Expr a) !(Expr a) -> Expr TBool
(<=.) infix 4 :: !(Expr a) !(Expr a) -> Expr TBool
(>=.) infix 4 :: !(Expr a) !(Expr a) -> Expr TBool
(&&.) infixr 3 :: !(Expr TWord) !(Expr TWord) -> Expr TWord
notB :: !(Expr TWord) -> Expr TWord
(&&.) infixr 3 :: !(Expr TBool) !(Expr TBool) -> Expr TBool
(&.) infixl 6 :: !(Expr TWord) !(Expr TWord) -> Expr TWord
(|.) infixl 6 :: !(Expr TWord) !(Expr TWord) -> Expr TWord
......@@ -81,7 +78,7 @@ RtoI :: !(Expr TReal) -> Expr TInt
if_i64_or_i32 :: !(Target -> Target) !(Target -> Target) !Target -> Target
if_i64_or_i32_expr :: !(Expr t) !(Expr t) -> Expr t
if_expr :: !(Expr TWord) !(Expr t) !(Expr t) -> Expr t
if_expr :: !(Expr TBool) !(Expr t) !(Expr t) -> Expr t
begin_instruction :: !String !Target -> Target
end_instruction :: !Target -> Target
......@@ -97,7 +94,7 @@ new_local :: !t !(Expr t) !((Expr t) Target -> Target) !Target -> Target | typen
class (.=) infix 2 v e :: !(Expr v) !(Expr e) !Target -> Target
instance .=
TWord TWord, TWord TChar, TWord TInt, TWord TShort,
TWord TWord, TWord TBool, TWord TChar, TWord TInt, TWord TShort,
TChar TChar,
TInt TInt, TInt TWord,
(TPtr t) (TPtr u) // NB/TODO: no checking on child types!
......@@ -123,15 +120,15 @@ instance @? Int, (Expr t)
begin_block :: !Target -> Target
end_block :: !Target -> Target
while_do :: !(Expr TWord) !(Target -> Target) !Target -> Target
while_do :: !(Expr TBool) !(Target -> Target) !Target -> Target
break :: !Target -> Target
if_then_else ::
!(Expr TWord) !(Target -> Target)
![(Expr TWord, Target -> Target)]
!(Expr TBool) !(Target -> Target)
![(Expr TBool, Target -> Target)]
!(Maybe (Target -> Target))
!Target -> Target
if_break_else :: !(Expr TWord) !(Target -> Target) !Target -> Target
if_break_else :: !(Expr TBool) !(Target -> Target) !Target -> Target
class ensure_hp s :: !s !Target -> Target
instance ensure_hp (Expr TWord)
......@@ -148,7 +145,7 @@ INT_ptr :: Expr TWord
REAL_ptr :: Expr TWord
ARRAY__ptr :: Expr TWord
STRING__ptr :: Expr TWord
jmp_ap_ptr :: !Int -> Expr TWord
jmp_ap_ptr :: !Int -> Expr (TPtr TWord)
cycle_ptr :: Expr TWord
indirection_ptr :: Expr TWord
dNil_ptr :: Expr TWord
......@@ -157,7 +154,7 @@ static_character :: !(Expr TChar) -> Expr TWord
static_boolean :: !(Expr TWord) -> Expr TWord
caf_list :: Expr (TPtr (TPtr TWord))
push_c :: !(Expr TWord) !Target -> Target
push_c :: !(Expr (TPtr TWord)) !Target -> Target
pop_pc_from_c :: !Target -> Target
memcpy :: !(Expr (TPtr a)) !(Expr (TPtr b)) !(Expr TWord) !Target -> Target
......
......@@ -194,12 +194,20 @@ lit_short i = toString i
lit_int :: !Int -> Expr TInt
lit_int i = toString i
instance to_word TWord where to_word e = e
instance to_word TChar where to_word e = "(BC_WORD)("+-+e+-+")"
instance to_word TInt where to_word e = "(BC_WORD)("+-+e+-+")"
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_bool TWord
where
to_bool e = e`
where
e` :: Expr TBool
e` = e
instance to_char TWord where to_char e = "(char)("+-+e+-+")"
instance to_int TWord where to_int e = "(BC_WORD_S)("+-+e+-+")"
......@@ -222,30 +230,27 @@ instance ^ (Expr TReal) where ^ a b = "pow("+-+a+-+","+-+b+-+")"
(%.) infixl 6 :: !(Expr TInt) !(Expr TInt) -> Expr TInt
(%.) a b = "("+-+a+-+"%"+-+b+-+")"
(==.) infix 4 :: !(Expr a) !(Expr a) -> Expr TWord
(==.) infix 4 :: !(Expr a) !(Expr a) -> Expr TBool
(==.) a b = "("+-+a+-+"=="+-+b+-+")"
(<>.) infix 4 :: !(Expr a) !(Expr a) -> Expr TWord
(<>.) infix 4 :: !(Expr a) !(Expr a) -> Expr TBool
(<>.) a b = "("+-+a+-+"!="+-+b+-+")"
(<.) infix 4 :: !(Expr a) !(Expr a) -> Expr TWord
(<.) infix 4 :: !(Expr a) !(Expr a) -> Expr TBool
(<.) a b = "("+-+a+-+"<"+-+b+-+")"
(>.) infix 4 :: !(Expr a) !(Expr a) -> Expr TWord
(>.) infix 4 :: !(Expr a) !(Expr a) -> Expr TBool
(>.) a b = "("+-+a+-+">"+-+b+-+")"
(<=.) infix 4 :: !(Expr a) !(Expr a) -> Expr TWord
(<=.) infix 4 :: !(Expr a) !(Expr a) -> Expr TBool
(<=.) a b = "("+-+a+-+"<="+-+b+-+")"
(>=.) infix 4 :: !(Expr a) !(Expr a) -> Expr TWord
(>=.) infix 4 :: !(Expr a) !(Expr a) -> Expr TBool
(>=.) a b = "("+-+a+-+">="+-+b+-+")"
(&&.) infixr 3 :: !(Expr TWord) !(Expr TWord) -> Expr TWord
(&&.) infixr 3 :: !(Expr TBool) !(Expr TBool) -> Expr TBool
(&&.) a b = "("+-+a+-+"&&"+-+b+-+")"
notB :: !(Expr TWord) -> Expr TWord
notB a = "!("+-+a+-+")"
(&.) infixl 6 :: !(Expr TWord) !(Expr TWord) -> Expr TWord
(&.) a b = "("+-+a+-+"&"+-+b+-+")"
......@@ -315,7 +320,7 @@ if_i64_or_i32 i64 i32 t = append "#endif" (i32 (append "#else" (i64 (append "#if
if_i64_or_i32_expr :: !(Expr t) !(Expr t) -> Expr t
if_i64_or_i32_expr a b = "IF_INT_64_OR_32("+-+a+-+","+-+b+-+")"
if_expr :: !(Expr TWord) !(Expr t) !(Expr t) -> Expr t
if_expr :: !(Expr TBool) !(Expr t) !(Expr t) -> Expr t
if_expr c t e = "("+-+c+-+" ? "+-+t+-+" : "+-+e+-+")"
begin_instruction :: !String !Target -> Target
......@@ -349,6 +354,7 @@ 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 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
......@@ -399,15 +405,15 @@ begin_block t = append "\tdo {" t
end_block :: !Target -> Target
end_block t = append "\t} while (0);" t
while_do :: !(Expr TWord) !(Target -> Target) !Target -> Target
while_do :: !(Expr TBool) !(Target -> Target) !Target -> Target
while_do c f t = append "\t}" (f (append ("\twhile ("+-+c+-+") {") t))
break :: !Target -> Target
break t = append "\tbreak;" t
if_then_else ::
!(Expr TWord) !(Target -> Target)
![(Expr TWord, Target -> Target)]
!(Expr TBool) !(Target -> Target)
![(Expr TBool, Target -> Target)]
!(Maybe (Target -> Target))
!Target -> Target
if_then_else c then elifs else t
......@@ -418,7 +424,7 @@ if_then_else c then elifs else t
-> append "\t}" (e (append "\t} else {" (drop_last_line t)))
-> t
if_break_else :: !(Expr TWord) !(Target -> Target) !Target -> Target
if_break_else :: !(Expr TBool) !(Target -> Target) !Target -> Target
if_break_else c else t = concat_up_to_mark (else (append ("\t\tif ("+-+c+-+") break;") (mark t)))
instance ensure_hp (Expr TWord) where ensure_hp i t = append ("\tNEED_HEAP("+-+i+-+");") t
......@@ -454,7 +460,7 @@ ARRAY__ptr = "(BC_WORD)&__ARRAY__"
STRING__ptr :: Expr TWord
STRING__ptr = "(BC_WORD)&__STRING__"
jmp_ap_ptr :: !Int -> Expr TWord
jmp_ap_ptr :: !Int -> Expr (TPtr TWord)
jmp_ap_ptr i = "(BC_WORD)&Fjmp_ap["+-+toString i+-+"]"
cycle_ptr :: Expr TWord
......@@ -478,8 +484,8 @@ static_boolean b = "(BC_WORD)&static_booleans[("+-+b+-+") ? 2 : 0]"
caf_list :: Expr (TPtr (TPtr TWord))
caf_list = "caf_list"
push_c :: !(Expr TWord) !Target -> Target
push_c v t = append ("\t*++csp="+-+v+-+";") t
push_c :: !(Expr (TPtr TWord)) !Target -> Target
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
......
......@@ -4,6 +4,14 @@ import StdEnv
import StdMaybe
import interpretergen
:: THWord = THWord
:: Type
class wasm_type a :: !a -> Type
instance wasm_type TWord, THWord, TBool, TChar, TShort, TInt, TReal, (TPtr t)
:: Target
:: Expr t
......@@ -18,22 +26,19 @@ instr_mulUUL :: !Target -> Target
instr_RtoAC :: !Target -> Target
lit_word :: !Int -> Expr TWord
lit_hword :: !Int -> Expr THWord
lit_char :: !Char -> Expr TChar
lit_short :: !Int -> Expr TShort
lit_int :: !Int -> Expr TInt
instance to_word TChar
instance to_word TInt
instance to_word TShort
instance to_word (TPtr t)
instance to_word TReal
class to_hword t :: !(Expr t) -> Expr THWord
instance to_word THWord, TChar, TInt, TShort, (TPtr t), TReal
instance to_hword TWord, THWord, TShort
instance to_bool TWord
instance to_char TWord
instance to_int TWord
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)
......@@ -45,15 +50,14 @@ instance / (Expr t)
instance ^ (Expr TReal)
(%.) infixl 6 :: !(Expr TInt) !(Expr TInt) -> Expr TInt
(==.) infix 4 :: !(Expr a) !(Expr a) -> Expr TWord
(<>.) infix 4 :: !(Expr a) !(Expr a) -> Expr TWord
(<.) infix 4 :: !(Expr a) !(Expr a) -> Expr TWord
(>.) infix 4 :: !(Expr a) !(Expr a) -> Expr TWord
(<=.) infix 4 :: !(Expr a) !(Expr a) -> Expr TWord
(>=.) infix 4 :: !(Expr a) !(Expr a) -> Expr TWord
(==.) infix 4 :: !(Expr a) !(Expr a) -> Expr TBool
(<>.) infix 4 :: !(Expr a) !(Expr a) -> Expr TBool
(<.) infix 4 :: !(Expr a) !(Expr a) -> Expr TBool
(>.) infix 4 :: !(Expr a) !(Expr a) -> Expr TBool
(<=.) infix 4 :: !(Expr a) !(Expr a) -> Expr TBool
(>=.) infix 4 :: !(Expr a) !(Expr a) -> Expr TBool
(&&.) infixr 3 :: !(Expr TWord) !(Expr TWord) -> Expr TWord
notB :: !(Expr TWord) -> Expr TWord
(&&.) infixr 3 :: !(Expr TBool) !(Expr TBool) -> Expr TBool
(&.) infixl 6 :: !(Expr TWord) !(Expr TWord) -> Expr TWord
(|.) infixl 6 :: !(Expr TWord) !(Expr TWord) -> Expr TWord
......@@ -81,7 +85,7 @@ RtoI :: !(Expr TReal) -> Expr TInt
if_i64_or_i32 :: !(Target -> Target) !(Target -> Target) !Target -> Target
if_i64_or_i32_expr :: !(Expr t) !(Expr t) -> Expr t
if_expr :: !(Expr TWord) !(Expr t) !(Expr t) -> Expr t | typename t
if_expr :: !(Expr TBool) !(Expr t) !(Expr t) -> Expr t
begin_instruction :: !String !Target -> Target
end_instruction :: !Target -> Target
......@@ -91,32 +95,30 @@ nop :: !Target -> Target
(:.) infixr 1 :: !(Target -> Target) !(Target -> Target) !Target -> Target
class typename a :: !a -> String
instance typename TWord, TChar, TShort, TInt, TReal, (TPtr t) | typename t
new_local :: !t !(Expr t) !((Expr t) Target -> Target) !Target -> Target | typename t
new_local :: !t !(Expr t) !((Expr t) Target -> Target) !Target -> Target | wasm_type t
class (.=) infix 2 v e :: !(Expr v) !(Expr e) !Target -> Target
instance .=
TWord TWord, TWord TChar, TWord TInt, TWord TShort,
TWord TWord, TWord THWord, TWord TBool, TWord TChar, TWord TInt, TWord TShort,
THWord THWord,
TChar TChar,
TInt TInt, TInt TWord,
(TPtr t) (TPtr u) // NB/TODO: no checking on child types!
(TPtr t) (TPtr u)
class (+=) infix 2 v e :: !(Expr v) !(Expr e) !Target -> Target
instance += TWord TWord
instance += TWord TWord, THWord THWord
class (-=) infix 2 v e :: !(Expr v) !(Expr e) !Target -> Target
instance -= TWord TWord, TShort TShort
instance -= TWord TWord, THWord THWord, TShort TShort
class advance_ptr i :: !(Expr (TPtr v)) !i !Target -> Target | typename v
instance advance_ptr Int, (Expr w)
class advance_ptr i :: !(Expr (TPtr v)) !i !Target -> Target | wasm_type v
instance advance_ptr Int, (Expr THWord)
class rewind_ptr i :: !(Expr (TPtr v)) !i !Target -> Target | typename v
instance rewind_ptr Int, (Expr w)
class rewind_ptr i :: !(Expr (TPtr v)) !i !Target -> Target | wasm_type v
instance rewind_ptr Int, (Expr THWord)
class (@) infix 8 a :: !(Expr (TPtr t)) !a -> Expr t | typename t
class (@?) infix 8 a :: !(Expr (TPtr t)) !a -> Expr (TPtr t) | typename t
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
instance @ Int, (Expr t)
instance @? Int, (Expr t)
......@@ -124,19 +126,18 @@ instance @? Int, (Expr t)
begin_block :: !Target -> Target
end_block :: !Target -> Target
while_do :: !(Expr TWord) !(Target -> Target) !Target -> Target
while_do :: !(Expr TBool) !(Target -> Target) !Target -> Target
break :: !Target -> Target
if_then_else ::
!(Expr TWord) !(Target -> Target)
![(Expr TWord, Target -> Target)]
!(Expr TBool) !(Target -> Target)
![(Expr TBool, Target -> Target)]
!(Maybe (Target -> Target))
!Target -> Target
if_break_else :: !(Expr TWord) !(Target -> Target) !Target -> Target
if_break_else :: !(Expr TBool) !(Target -> Target) !Target -> Target
class ensure_hp s :: !s !Target -> Target
instance ensure_hp (Expr TWord)
instance ensure_hp Int
instance ensure_hp Int, (Expr t) | to_hword t /* should be only THWord TODO */
A :: Expr (TPtr TWord)
B :: Expr (TPtr TWord)
......@@ -149,7 +150,7 @@ INT_ptr :: Expr TWord
REAL_ptr :: Expr TWord
ARRAY__ptr :: Expr TWord
STRING__ptr :: Expr TWord
jmp_ap_ptr :: !Int -> Expr TWord
jmp_ap_ptr :: !Int -> Expr (TPtr TWord)
cycle_ptr :: Expr TWord
indirection_ptr :: Expr TWord
dNil_ptr :: Expr TWord
......@@ -158,11 +159,11 @@ static_character :: !(Expr TChar) -> Expr TWord
static_boolean :: !(Expr TWord) -> Expr TWord
caf_list :: Expr (TPtr (TPtr TWord))
push_c :: !(Expr TWord) !Target -> Target
push_c :: !(Expr (TPtr TWord)) !Target -> Target
pop_pc_from_c :: !Target -> Target
memcpy :: !(Expr (TPtr a)) !(Expr (TPtr b)) !(Expr TWord) !Target -> Target
strncmp :: !(Expr (TPtr TChar)) !(Expr (TPtr TChar)) !(Expr TWord) -> Expr TInt
memcpy :: !(Expr (TPtr a)) !(Expr (TPtr b)) !(Expr THWord) !Target -> Target
strncmp :: !(Expr (TPtr TChar)) !(Expr (TPtr TChar)) !(Expr THWord) -> Expr TInt
putchar :: !(Expr TChar) !Target -> Target
print_bool :: !(Expr TWord) !Target -> Target
......
This diff is collapsed.
......@@ -9,6 +9,7 @@ from target import :: Expr
}
:: TWord = TWord
:: TBool = TBool
:: TChar = TChar
:: TShort = TShort
:: TInt = TInt
......@@ -17,6 +18,7 @@ from target import :: Expr
:: TPtr t = TPtr !t
class to_word t :: !(Expr t) -> Expr TWord
class to_bool t :: !(Expr t) -> Expr TBool
class to_char t :: !(Expr t) -> Expr TChar
class to_short t :: !(Expr t) -> Expr TShort
class to_int t :: !(Expr t) -> Expr TInt
......
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