nwasmadd.pas 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. {
  2. Copyright (c) 2019 by Dmitry Boyarintsev based on JVM by Jonas Maebe
  3. Code generation for add nodes on the WebAssembly
  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 nwasmadd;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cgbase,
  22. node,ncgadd,cpubase, globals, pass_2;
  23. type
  24. { twasmaddnode }
  25. twasmaddnode = class(tcgaddnode)
  26. protected
  27. procedure second_generic_compare(unsigned: boolean);
  28. procedure pass_left_right;override;
  29. //procedure second_addfloat;override;
  30. procedure second_cmpfloat;override;
  31. procedure second_cmpboolean;override;
  32. procedure second_cmp64bit;override;
  33. procedure second_add64bit; override;
  34. procedure second_cmpordinal;override;
  35. // special treatement for short-boolean expressions
  36. // using IF block, instead of direct labels
  37. procedure second_addboolean; override;
  38. end;
  39. implementation
  40. uses
  41. systems,
  42. cutils,verbose,constexp,globtype,compinnr,
  43. symconst,symtable,symdef,symcpu,
  44. paramgr,procinfo,pass_1,
  45. aasmbase,aasmtai,aasmdata,aasmcpu,defutil,
  46. hlcgobj,hlcgcpu,cgutils,
  47. cpupara,
  48. nbas,ncon,nset,nadd,ncal,ncnv,ninl,nld,nmat,nmem,
  49. //njvmcon,
  50. cgobj;
  51. {*****************************************************************************
  52. tjvmaddnode
  53. *****************************************************************************}
  54. procedure twasmaddnode.pass_left_right;
  55. begin
  56. //if not((nodetype in [orn,andn]) and
  57. // is_boolean(left.resultdef)) then
  58. // swapleftright;
  59. inherited pass_left_right;
  60. end;
  61. procedure twasmaddnode.second_cmpfloat;
  62. //var
  63. // truelabel,
  64. // falselabel: tasmlabel;
  65. // op: tasmop;
  66. // cmpop: TOpCmp;
  67. begin
  68. //truelabel:=nil;
  69. //falselabel:=nil;
  70. //pass_left_right;
  71. //{ swap the operands to make it easier for the optimizer to optimize
  72. // the operand stack slot reloading in case both are in a register }
  73. //if (left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
  74. // (right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  75. // swapleftright;
  76. //cmpop:=cmpnode2topcmp(false);
  77. //if (nf_swapped in flags) then
  78. // cmpop:=swap_opcmp(cmpop);
  79. //
  80. //current_asmdata.getjumplabel(truelabel);
  81. //current_asmdata.getjumplabel(falselabel);
  82. //location_reset_jump(location,truelabel,falselabel);
  83. //
  84. //thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  85. //thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  86. //
  87. //{ compares two floating point values and puts 1/0/-1 on stack depending
  88. // on whether value1 >/=/< value2 }
  89. //if left.location.size=OS_F64 then
  90. // { make sure that comparisons with NaNs always return false for </> }
  91. // if nodetype in [ltn,lten] then
  92. // op:=a_dcmpg
  93. // else
  94. // op:=a_dcmpl
  95. //else if nodetype in [ltn,lten] then
  96. // op:=a_fcmpg
  97. //else
  98. // op:=a_fcmpl;
  99. //current_asmdata.CurrAsmList.concat(taicpu.op_none(op));
  100. //thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,(1+ord(left.location.size=OS_F64))*2-1);
  101. //
  102. //current_asmdata.CurrAsmList.concat(taicpu.op_sym(opcmp2if[cmpop],location.truelabel));
  103. //thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  104. //hlcg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  105. end;
  106. procedure twasmaddnode.second_cmpboolean;
  107. begin
  108. //second_generic_compare(true);
  109. end;
  110. procedure twasmaddnode.second_cmp64bit;
  111. begin
  112. //second_generic_compare(not is_signed(left.resultdef));
  113. end;
  114. procedure twasmaddnode.second_add64bit;
  115. begin
  116. //second_opordinal;
  117. end;
  118. procedure twasmaddnode.second_cmpordinal;
  119. begin
  120. second_generic_compare(not is_signed(left.resultdef));
  121. end;
  122. procedure twasmaddnode.second_addboolean;
  123. begin
  124. if (nodetype in [orn,andn]) and
  125. (not(cs_full_boolean_eval in current_settings.localswitches) or
  126. (nf_short_bool in flags)) then
  127. begin
  128. secondpass(left);
  129. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_if) );
  130. case nodetype of
  131. andn :
  132. begin
  133. // inside of IF (the condition evaluated as true)
  134. // for "and" must evaluate the right section
  135. secondpass(right);
  136. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_else) );
  137. // inside of ELSE (the condition evaluated as false)
  138. // for "and" must end evaluation immediately
  139. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i32_const, 0) );
  140. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_end) );
  141. end;
  142. orn :
  143. begin
  144. // inside of IF (the condition evaluated as true)
  145. // for "or" must end evalaution immediately - satified!
  146. current_asmdata.CurrAsmList.Concat( taicpu.op_const(a_i32_const, 1) );
  147. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_else) );
  148. // inside of ELSE (the condition evaluated as false)
  149. // for "or" must evaluate the right part
  150. secondpass(right);
  151. // inside of ELSE (the condition evaluated as false)
  152. // for "and" must end evaluation immediately
  153. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_end) );
  154. end;
  155. else
  156. Internalerror(2019091902);
  157. end;
  158. // todo: need to reset location to the stack?
  159. //Internalerror(2019091901);
  160. end else
  161. inherited;
  162. end;
  163. procedure twasmaddnode.second_generic_compare(unsigned: boolean);
  164. var
  165. truelabel,
  166. falselabel: tasmlabel;
  167. cmpop: TOpCmp;
  168. begin
  169. truelabel:=nil;
  170. falselabel:=nil;
  171. pass_left_right;
  172. { swap the operands to make it easier for the optimizer to optimize
  173. the operand stack slot reloading in case both are in a register }
  174. if (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  175. (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  176. swapleftright;
  177. cmpop:=cmpnode2topcmp(unsigned);
  178. if (nf_swapped in flags) then
  179. cmpop:=swap_opcmp(cmpop);
  180. // must generate those labels...
  181. current_asmdata.getjumplabel(truelabel);
  182. current_asmdata.getjumplabel(falselabel);
  183. location_reset_jump(location,truelabel,falselabel);
  184. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  185. hlcg.a_cmp_loc_reg_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location,left.location.register,location.truelabel)
  186. else case right.location.loc of
  187. LOC_REGISTER,LOC_CREGISTER:
  188. hlcg.a_cmp_reg_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.register,left.location,location.truelabel);
  189. LOC_REFERENCE,LOC_CREFERENCE:
  190. hlcg.a_cmp_ref_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.reference,left.location,location.truelabel);
  191. LOC_CONSTANT:
  192. hlcg.a_cmp_const_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.value,left.location,location.truelabel);
  193. else
  194. internalerror(2011010413);
  195. end;
  196. end;
  197. begin
  198. caddnode:=twasmaddnode;
  199. end.