Browse Source

[PATCH 18/83] adding processing of while loops

From b0d2b953afc176025c047aa3b72ff476c8082a78 Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <[email protected]>
Date: Tue, 10 Sep 2019 17:21:39 -0400

git-svn-id: branches/wasm@45895 -
nickysn 5 years ago
parent
commit
2f26ac36f4
1 changed files with 33 additions and 2 deletions
  1. 33 2
      compiler/wasm/nwasmflw.pas

+ 33 - 2
compiler/wasm/nwasmflw.pas

@@ -19,7 +19,13 @@ type
    public
      procedure pass_generate_code;override;
    end;
-   tifnodeclass = class of tifnode;
+
+   { twasmwhilerepeatnode }
+
+   twasmwhilerepeatnode = class(tcgwhilerepeatnode)
+   public
+     procedure pass_generate_code;override;
+   end;
 
 implementation
 
@@ -32,6 +38,30 @@ uses
   tgobj,paramgr,
   cgutils,hlcgobj,hlcgcpu;
 
+{ twasmwhilerepeatnode }
+
+procedure twasmwhilerepeatnode.pass_generate_code;
+begin
+  current_asmdata.CurrAsmList.concat(taicpu.op_none(a_block));
+  current_asmdata.CurrAsmList.concat(taicpu.op_none(a_loop));
+
+  secondpass(left);
+
+  // reversing the condition
+  // todo: there should be a better approach
+  current_asmdata.CurrAsmList.concat(taicpu.op_const(a_i32_const,1) );
+  current_asmdata.CurrAsmList.concat(taicpu.op_none(a_i32_xor) );
+
+  current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,1) );
+
+  secondpass(right);
+
+  current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br,0) );
+
+  current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end));
+  current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end));
+end;
+
 { twasmifnode }
 
 procedure twasmifnode.pass_generate_code;
@@ -80,6 +110,7 @@ initialization
    //ctryexceptnode:=tjvmtryexceptnode;
    //ctryfinallynode:=tjvmtryfinallynode;
    //connode:=tjvmonnode;
-  cifnode:=twasmifnode;
+   cifnode:=twasmifnode;
+   cwhilerepeatnode:=twasmwhilerepeatnode;
 
 end.