nwasmflw.pas 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. unit nwasmflw;
  2. interface
  3. uses
  4. aasmbase,node,nflw,ncgflw;
  5. type
  6. { twasmifnode }
  7. // Wasm doesn't have any jump(+offset) operations
  8. // It only provide structured blockes to handle jumps
  9. // (It's possible to jump-out-of-block at any time)
  10. // "If" is also implemented as a block, identical to high-level language.
  11. // Another thing to consider is "if" block also "returns" a value on the stack.
  12. // Such value should be substituteed (it's hard-coded to be type i32)
  13. twasmifnode = class(tcgifnode)
  14. public
  15. procedure pass_generate_code;override;
  16. end;
  17. tifnodeclass = class of tifnode;
  18. implementation
  19. uses
  20. verbose,globals,systems,globtype,constexp,
  21. symconst,symdef,symsym,aasmtai,aasmdata,aasmcpu,defutil,defcmp,
  22. procinfo,cgbase,pass_1,pass_2,parabase,
  23. cpubase,cpuinfo,
  24. nbas,nld,ncon,ncnv,
  25. tgobj,paramgr,
  26. cgutils,hlcgobj,hlcgcpu;
  27. { twasmifnode }
  28. procedure twasmifnode.pass_generate_code;
  29. var
  30. oldflowcontrol: tflowcontrol;
  31. begin
  32. // left - condition
  33. // right - then
  34. // t1 - else (optional)
  35. //todo: MOVE all current_asm_data actions to Wasm HL CodeGen
  36. secondpass(left); // condition exprssions
  37. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_if)); // IF
  38. secondpass(right); // then branchs
  39. if Assigned(t1) then // else branch
  40. begin
  41. // 0 const on stack if used to return IF value
  42. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_i32_const, 0));
  43. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_else));
  44. secondpass(t1);
  45. end
  46. else // else dummy-branch
  47. begin
  48. // dummy else branch! todo: to be removed, when it's decided
  49. // how to handle typeless-IF instructions (If without else)
  50. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_i32_const, 0));
  51. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_else));
  52. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_nop));
  53. end;
  54. // 0 const on stack if used to return IF value
  55. current_asmdata.CurrAsmList.concat(taicpu.op_const(a_i32_const, 0));
  56. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end));
  57. // clearing IF return value
  58. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_drop));
  59. end;
  60. initialization
  61. //cfornode:=tjvmfornode;
  62. //craisenode:=tjvmraisenode;
  63. //ctryexceptnode:=tjvmtryexceptnode;
  64. //ctryfinallynode:=tjvmtryfinallynode;
  65. //connode:=tjvmonnode;
  66. cifnode:=twasmifnode;
  67. end.