From ce742b5b4d7f9f750a40581d0219fe2f6d704f78 Mon Sep 17 00:00:00 2001 From: Nikolay Nikolov Date: Wed, 18 Oct 2023 03:45:02 +0300 Subject: [PATCH] * convert loop..end_loop blocks to flat list + branch instruction --- compiler/wasm32/aasmcpu.pas | 8 ++++++++ compiler/wasm32/cpupi.pas | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/compiler/wasm32/aasmcpu.pas b/compiler/wasm32/aasmcpu.pas index 22c0ccf9a9..0d6670412a 100644 --- a/compiler/wasm32/aasmcpu.pas +++ b/compiler/wasm32/aasmcpu.pas @@ -135,6 +135,7 @@ uses destructor Destroy; override; function getcopy:TLinkedListItem;override; procedure Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);override; + procedure ConvertToBr(list: TAsmList); procedure ConvertToFlatList(l: TAsmList);override; end; @@ -659,6 +660,13 @@ uses blockstack.pop; end; + procedure tai_wasmstruc_loop.ConvertToBr(list: TAsmList); + begin + list.concat(tai_label.create(GetLabel)); + list.concatList(inner_asmlist); + list.concat(taicpu.op_sym(a_br,GetLabel)); + end; + procedure tai_wasmstruc_loop.ConvertToFlatList(l: TAsmList); begin l.Concat(loop_instr); diff --git a/compiler/wasm32/cpupi.pas b/compiler/wasm32/cpupi.pas index 095edc2bcb..fe17a48b09 100644 --- a/compiler/wasm32/cpupi.pas +++ b/compiler/wasm32/cpupi.pas @@ -41,6 +41,7 @@ interface function ConvertBranchTargetNumbersToLabels(ai: tai; blockstack: twasmstruc_stack): TAsmMapFuncResult; function ConvertIfToBrIf(ai: tai; blockstack: twasmstruc_stack): TAsmMapFuncResult; + function ConvertLoopToBr(ai: tai; blockstack: twasmstruc_stack): TAsmMapFuncResult; { used for allocating locals during the postprocess_code stage (i.e. after register allocation) } function AllocWasmLocal(wbt: TWasmBasicType): Integer; @@ -389,6 +390,17 @@ implementation end; end; + function tcpuprocinfo.ConvertLoopToBr(ai: tai; blockstack: twasmstruc_stack): TAsmMapFuncResult; + begin + result.typ:=amfrtNoChange; + if (ai.typ=ait_wasm_structured_instruction) and (ai is tai_wasmstruc_loop) then + begin + result.typ:=amfrtNewList; + result.newlist:=TAsmList.Create; + tai_wasmstruc_loop(ai).ConvertToBr(result.newlist); + end; + end; + function tcpuprocinfo.AllocWasmLocal(wbt: TWasmBasicType): Integer; begin SetLength(FAllocatedLocals,Length(FAllocatedLocals)+1); @@ -656,6 +668,7 @@ implementation map_structured_asmlist(asmlist,@ConvertBranchTargetNumbersToLabels); map_structured_asmlist(asmlist,@ConvertIfToBrIf); + map_structured_asmlist(asmlist,@ConvertLoopToBr); l2:=TAsmList.Create; wasm_convert_to_flat_asmlist(asmlist,l2);