From d033a09569b811052d46740ac5b3fa64633d028b Mon Sep 17 00:00:00 2001 From: Nicolas Cannasse Date: Sat, 26 Aug 2023 15:41:32 +0200 Subject: [PATCH] [hl] added $prefetch support --- src/generators/genhl.ml | 15 +++++++++++++++ src/generators/hl2c.ml | 9 +++++++++ src/generators/hlcode.ml | 3 +++ src/generators/hlinterp.ml | 4 +++- src/generators/hlopt.ml | 5 +++++ 5 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/generators/genhl.ml b/src/generators/genhl.ml index b0724c69f39..dee561b888b 100644 --- a/src/generators/genhl.ml +++ b/src/generators/genhl.ml @@ -310,6 +310,12 @@ let unsigned_op e1 e2 = in is_unsigned e1 && is_unsigned e2 +let rec get_const e = + match e.eexpr with + | TConst c -> c + | TParenthesis e | TCast (e,_) -> get_const e + | _ -> abort "Should be a constant" e.epos + let set_curpos ctx p = ctx.m.mcurpos <- p @@ -2067,6 +2073,15 @@ and eval_expr ctx e = free ctx rfile; free ctx min; r + | "$prefetch", [value; mode] -> + let mode = (match get_const mode with + | TInt m -> Int32.to_int m + | _ -> abort "Constant mode required" e.epos + ) in + (match get_access ctx value with + | AInstanceField (f, index) -> op ctx (OPrefetch (eval_expr ctx f, index + 1, mode)) + | _ -> op ctx (OPrefetch (eval_expr ctx value, 0, mode))); + alloc_tmp ctx HVoid | _ -> abort ("Unknown native call " ^ s) e.epos) | TEnumIndex v -> diff --git a/src/generators/hl2c.ml b/src/generators/hl2c.ml index af03a261786..d5b94f0ddff 100644 --- a/src/generators/hl2c.ml +++ b/src/generators/hl2c.ml @@ -1087,6 +1087,15 @@ let generate_function ctx f = sexpr "%s = %s + %s" (reg r) (reg r2) (reg off) | ONop _ -> () + | OPrefetch (r,fid,mode) -> + let expr = (if fid = 0 then reg r else (match rtype r with + | HObj o | HStruct o -> + let name, t = resolve_field o (fid - 1) in + Printf.sprintf "%s->%s" (reg r) name + | _ -> + Globals.die "" __LOC__ + )) in + sexpr "__hl_prefetch_m%d(%s)" mode expr ) f.code; flush_options (Array.length f.code); unblock(); diff --git a/src/generators/hlcode.ml b/src/generators/hlcode.ml index 733a6b10e05..d40313f9c26 100644 --- a/src/generators/hlcode.ml +++ b/src/generators/hlcode.ml @@ -201,6 +201,7 @@ type opcode = | ORefData of reg * reg | ORefOffset of reg * reg * reg | ONop of string + | OPrefetch of reg * field index * int type fundecl = { fpath : string * string; @@ -572,6 +573,8 @@ let ostr fstr o = | ORefData (r,d) -> Printf.sprintf "refdata %d, %d" r d | ORefOffset (r,r2,off) -> Printf.sprintf "refoffset %d, %d, %d" r r2 off | ONop s -> if s = "" then "nop" else "nop " ^ s + | OPrefetch (r,f,mode) -> Printf.sprintf "prefetch %d[%d] %d" r f mode + let fundecl_name f = if snd f.fpath = "" then "fun$" ^ (string_of_int f.findex) else (fst f.fpath) ^ "." ^ (snd f.fpath) let dump pr code = diff --git a/src/generators/hlinterp.ml b/src/generators/hlinterp.ml index 785fca09b6e..e50714f7ee4 100644 --- a/src/generators/hlinterp.ml +++ b/src/generators/hlinterp.ml @@ -1154,7 +1154,7 @@ let interp ctx f args = (match get r2, get off with | VRef (RArray (a,pos),t), VInt i -> set r (VRef (RArray (a,pos + Int32.to_int i),t)) | _ -> Globals.die "" __LOC__) - | ONop _ -> + | ONop _ | OPrefetch _ -> () ); loop() @@ -2547,6 +2547,8 @@ let check code macros = reg off HI32; | ONop _ -> (); + | OPrefetch (r,f,_) -> + if f = 0 then ignore(rtype r) else ignore(tfield r (f - 1) false) ) f.code (* TODO : check that all path correctly initialize NULL values and reach a return *) in diff --git a/src/generators/hlopt.ml b/src/generators/hlopt.ml index 81059d76275..c11db0161aa 100644 --- a/src/generators/hlopt.ml +++ b/src/generators/hlopt.ml @@ -164,6 +164,8 @@ let opcode_fx frw op = write r; | ONop _ -> () + | OPrefetch (r,_,_) -> + read r let opcode_eq a b = match a, b with @@ -432,6 +434,9 @@ let opcode_map read write op = ORefOffset (write r,r2,off); | ONop _ -> op + | OPrefetch (r, fid, mode) -> + let r2 = read r in + OPrefetch (r2, fid, mode) (* build code graph *)