Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added native carray access with hl 1.14 #11299

Merged
merged 1 commit into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/generators/genhl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ type access =
| AInstanceProto of texpr * field index
| AInstanceField of texpr * field index
| AArray of reg * (ttype * ttype) * reg
| ACArray of reg * ttype * reg
| AVirtualMethod of texpr * field index
| ADynamic of texpr * string index
| AEnum of tenum * field index
Expand Down Expand Up @@ -1377,6 +1378,13 @@ and get_access ctx e =
free ctx a;
let t = to_type ctx t in
AArray (a,(t,t),i)
| TInst ({ cl_path = ["hl"],"Abstract" },[TInst({ cl_kind = KExpr (EConst (String("hl_carray",_)),_) },_)]) ->
let a = eval_null_check ctx a in
hold ctx a;
let i = eval_to ctx i HI32 in
free ctx a;
let t = to_type ctx e.etype in
ACArray (a,t,i)
| TAbstract (a,pl) ->
loop (Abstract.get_underlying_type a pl)
| _ ->
Expand Down Expand Up @@ -1894,7 +1902,13 @@ and eval_expr ctx e =
r
| "$asize", [e] ->
let r = alloc_tmp ctx HI32 in
op ctx (OArraySize (r, eval_to ctx e HArray));
(match follow e.etype with
| TInst ({cl_path=["hl"],"Abstract"},[TInst({ cl_kind = KExpr (EConst (String("hl_carray",_)),_) },_)]) ->
let arr = eval_expr ctx e in
op ctx (ONullCheck arr);
op ctx (OArraySize (r, arr))
| _ ->
op ctx (OArraySize (r, eval_to ctx e HArray)));
r
| "$aalloc", [esize] ->
let et = (match follow e.etype with TAbstract ({ a_path = ["hl"],"NativeArray" },[t]) -> to_type ctx t | _ -> invalid()) in
Expand Down Expand Up @@ -2208,7 +2222,7 @@ and eval_expr ctx e =
ignore(make_fun ctx ("","") fid f None None);
end;
op ctx (OStaticClosure (r,fid));
| ANone | ALocal _ | AArray _ | ACaptured _ ->
| ANone | ALocal _ | AArray _ | ACaptured _ | ACArray _ ->
abort "Invalid access" e.epos);
let to_t = to_type ctx e.etype in
(match to_t with
Expand Down Expand Up @@ -2452,7 +2466,7 @@ and eval_expr ctx e =
let r = value() in
op ctx (OSetEnumField (ctx.m.mcaptreg,index,r));
r
| AEnum _ | ANone | AInstanceFun _ | AInstanceProto _ | AStaticFun _ | AVirtualMethod _ ->
| AEnum _ | ANone | AInstanceFun _ | AInstanceProto _ | AStaticFun _ | AVirtualMethod _ | ACArray _ ->
die "" __LOC__)
| OpBoolOr ->
let r = alloc_tmp ctx HBool in
Expand Down Expand Up @@ -2727,6 +2741,10 @@ and eval_expr ctx e =
(match get_access ctx e with
| AArray (a,at,idx) ->
array_read ctx a at idx e.epos
| ACArray (a,t,idx) ->
let tmp = alloc_tmp ctx t in
op ctx (OGetArray (tmp,a,idx));
tmp
| _ ->
die "" __LOC__)
| TMeta (_,e) ->
Expand Down Expand Up @@ -3046,7 +3064,7 @@ and gen_assign_op ctx acc e1 f =
free ctx robj;
op ctx (ODynSet (robj,fid,r));
r
| ANone | ALocal _ | AStaticFun _ | AInstanceFun _ | AInstanceProto _ | AVirtualMethod _ | AEnum _ ->
| ANone | ALocal _ | AStaticFun _ | AInstanceFun _ | AInstanceProto _ | AVirtualMethod _ | AEnum _ | ACArray _ ->
die "" __LOC__

and build_capture_vars ctx f =
Expand Down
4 changes: 2 additions & 2 deletions src/generators/hlinterp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2436,7 +2436,7 @@ let check code macros =
| ORethrow r ->
reg r HDyn
| OGetArray (v,a,i) ->
reg a HArray;
(match rtype a with HAbstract ("hl_carray",_) -> () | _ -> reg a HArray);
reg i HI32;
ignore(rtype v);
| OGetUI8 (r,b,p) | OGetUI16(r,b,p) ->
Expand Down Expand Up @@ -2466,7 +2466,7 @@ let check code macros =
ignore(rtype a);
ignore(rtype b);
| OArraySize (r,a) ->
reg a HArray;
(match rtype a with HAbstract ("hl_carray",_) -> () | _ -> reg a HArray);
reg r HI32
| OType (r,_) ->
reg r HType
Expand Down
5 changes: 5 additions & 0 deletions src/generators/hlopt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,11 @@ let _optimize (f:fundecl) =
| OGetThis (r,fid) when (match f.regs.(r) with HStruct _ -> true | _ -> false) ->
do_write r;
if is_packed_field 0 fid then state.(r).rnullcheck <- true;
| OGetArray (r,arr,idx) ->
do_read arr;
do_read idx;
do_write r;
(match f.regs.(arr) with HAbstract _ -> state.(r).rnullcheck <- true | _ -> ());
| _ ->
opcode_fx (fun r read ->
if read then do_read r else do_write r
Expand Down
7 changes: 6 additions & 1 deletion std/hl/CArray.hx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ abstract CArray<T>(Abstract<"hl_carray">) {

public var length(get,never) : Int;

inline function get_length() return getLen(cast this);

#if (hl_ver >= version("1.14.0"))
inline function get_length() return untyped $asize(this);
@:arrayAccess inline function get( index : Int ) : T return untyped this[index];
#else
inline function get_length() return getLen(cast this);
@:arrayAccess inline function get( index : Int ) : T return getIndex(cast this, index);
#end

public static function alloc<T>( cl : Class<T>, size : Int ) : CArray<T> {
return cast alloc_carray( (cast cl:BaseType).__type__ , size );
Expand Down
Loading