nwasmflw.pas 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. {
  2. Copyright (c) 2019 by Dmitry Boyarintsev
  3. Generate assembler for nodes that influence the flow for the JVM
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nwasmflw;
  18. interface
  19. uses
  20. aasmbase,node,nflw,ncgflw;
  21. type
  22. { twasmifnode }
  23. // Wasm doesn't have any jump(+offset) operations
  24. // It only provide structured blockes to handle jumps
  25. // (It's possible to jump-out-of-block at any time)
  26. // "If" is also implemented as a block, identical to high-level language.
  27. // Another thing to consider is "if" block also "returns" a value on the stack.
  28. // Such value should be substituteed (it's hard-coded to be type i32)
  29. twasmifnode = class(tcgifnode)
  30. public
  31. procedure pass_generate_code;override;
  32. end;
  33. { twasmwhilerepeatnode }
  34. twasmwhilerepeatnode = class(tcgwhilerepeatnode)
  35. public
  36. procedure pass_generate_code_condition;
  37. procedure pass_generate_code;override;
  38. end;
  39. implementation
  40. uses
  41. verbose,globals,systems,globtype,constexp,
  42. symconst,symdef,symsym,aasmtai,aasmdata,aasmcpu,defutil,defcmp,
  43. procinfo,cgbase,pass_1,pass_2,parabase,
  44. cpubase,cpuinfo,
  45. nbas,nld,ncon,ncnv,
  46. tgobj,paramgr,
  47. cgutils,hlcgobj,hlcgcpu;
  48. { twasmwhilerepeatnode }
  49. procedure twasmwhilerepeatnode.pass_generate_code_condition;
  50. begin
  51. secondpass(left);
  52. // reversing the condition
  53. // todo: there should be a better approach
  54. if not (lnf_checknegate in loopflags) then begin
  55. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_i32_const,1) );
  56. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_i32_xor) );
  57. end;
  58. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br_if,1) );
  59. end;
  60. procedure twasmwhilerepeatnode.pass_generate_code;
  61. begin
  62. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_block));
  63. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_loop));
  64. if lnf_testatbegin in loopflags then
  65. pass_generate_code_condition;
  66. secondpass(right);
  67. if not (lnf_testatbegin in loopflags) then
  68. pass_generate_code_condition;
  69. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br,0) );
  70. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end));
  71. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end));
  72. end;
  73. { twasmifnode }
  74. procedure twasmifnode.pass_generate_code;
  75. var
  76. oldflowcontrol: tflowcontrol;
  77. begin
  78. // left - condition
  79. // right - then
  80. // t1 - else (optional)
  81. //todo: MOVE all current_asm_data actions to Wasm HL CodeGen
  82. secondpass(left); // condition exprssions
  83. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_if)); // IF
  84. secondpass(right); // then branchs
  85. if Assigned(t1) then // else branch
  86. begin
  87. // 0 const on stack if used to return IF value
  88. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_i32_const, 0));
  89. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_else));
  90. secondpass(t1);
  91. end
  92. else // else dummy-branch
  93. begin
  94. // dummy else branch! todo: to be removed, when it's decided
  95. // how to handle typeless-IF instructions (If without else)
  96. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_i32_const, 0));
  97. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_else));
  98. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_nop));
  99. end;
  100. // 0 const on stack if used to return IF value
  101. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_i32_const, 0));
  102. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end));
  103. // clearing IF return value
  104. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_drop));
  105. end;
  106. initialization
  107. //cfornode:=tjvmfornode;
  108. //craisenode:=tjvmraisenode;
  109. //ctryexceptnode:=tjvmtryexceptnode;
  110. //ctryfinallynode:=tjvmtryfinallynode;
  111. //connode:=tjvmonnode;
  112. cifnode:=twasmifnode;
  113. cwhilerepeatnode:=twasmwhilerepeatnode;
  114. end.