nwasminl.pas 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. {
  2. Copyright (c) 1998-2002, 2021 by Florian Klaempfl and Nikolay Nikolov
  3. Generate WebAssembly inline nodes
  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 nwasminl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ncginl;
  22. type
  23. { twasminlinenode }
  24. twasminlinenode = class(tcginlinenode)
  25. private
  26. procedure second_memory_size;
  27. procedure second_memory_grow;
  28. public
  29. function pass_typecheck_cpu: tnode; override;
  30. function first_cpu: tnode; override;
  31. procedure pass_generate_code_cpu; override;
  32. procedure second_length;override;
  33. end;
  34. implementation
  35. uses
  36. ninl,compinnr,
  37. cpubase,
  38. aasmbase,aasmdata,aasmcpu,
  39. cgbase,cgutils,
  40. hlcgobj,hlcgcpu,
  41. defutil,pass_2,
  42. symtype,symdef;
  43. {*****************************************************************************
  44. twasminlinenode
  45. *****************************************************************************}
  46. procedure twasminlinenode.second_memory_size;
  47. begin
  48. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_current_memory));
  49. thlcgwasm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  50. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  51. location.register:=hlcg.getregisterfordef(current_asmdata.CurrAsmList,resultdef);
  52. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  53. end;
  54. procedure twasminlinenode.second_memory_grow;
  55. begin
  56. secondpass(left);
  57. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  58. thlcgwasm(hlcg).a_load_reg_stack(current_asmdata.CurrAsmList,left.resultdef,left.location.register);
  59. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_grow_memory));
  60. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  61. location.register:=hlcg.getregisterfordef(current_asmdata.CurrAsmList,resultdef);
  62. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  63. end;
  64. function twasminlinenode.pass_typecheck_cpu: tnode;
  65. begin
  66. Result:=nil;
  67. case inlinenumber of
  68. in_wasm32_memory_size:
  69. begin
  70. CheckParameters(0);
  71. resultdef:=u32inttype;
  72. end;
  73. in_wasm32_memory_grow:
  74. begin
  75. CheckParameters(1);
  76. resultdef:=u32inttype;
  77. end;
  78. else
  79. Result:=inherited pass_typecheck_cpu;
  80. end;
  81. end;
  82. function twasminlinenode.first_cpu: tnode;
  83. begin
  84. Result:=nil;
  85. case inlinenumber of
  86. in_wasm32_memory_size,
  87. in_wasm32_memory_grow:
  88. expectloc:=LOC_REGISTER;
  89. else
  90. Result:=inherited first_cpu;
  91. end;
  92. end;
  93. procedure twasminlinenode.pass_generate_code_cpu;
  94. begin
  95. case inlinenumber of
  96. in_wasm32_memory_size:
  97. second_memory_size;
  98. in_wasm32_memory_grow:
  99. second_memory_grow;
  100. else
  101. inherited pass_generate_code_cpu;
  102. end;
  103. end;
  104. procedure twasminlinenode.second_length;
  105. var
  106. lendef : tdef;
  107. href : treference;
  108. extra_slots: LongInt;
  109. begin
  110. secondpass(left);
  111. if is_shortstring(left.resultdef) then
  112. begin
  113. location_copy(location,left.location);
  114. location.size:=OS_8;
  115. end
  116. else
  117. begin
  118. { length in ansi/wide strings and high in dynamic arrays is at offset -sizeof(pint) }
  119. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  120. thlcgwasm(hlcg).a_cmp_const_reg_stack(current_asmdata.CurrAsmList,left.resultdef,OC_EQ,0,left.location.register);
  121. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_if,TWasmFuncType.Create([],[wbt_i32])));
  122. thlcgwasm(hlcg).incblock;
  123. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  124. current_asmdata.CurrAsmList.Concat(taicpu.op_const(a_i32_const,0));
  125. thlcgwasm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  126. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_else) );
  127. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  128. { the length of a widestring is a 32 bit unsigned int. Since every
  129. character occupies 2 bytes, on a 32 bit platform you can express
  130. the maximum length using 31 bits. On a 64 bit platform, it may be
  131. 32 bits. This means that regardless of the platform, a location
  132. with size OS_SINT/ossinttype can hold the length without
  133. overflowing (this code returns an ossinttype value) }
  134. if is_widestring(left.resultdef) then
  135. lendef:=u32inttype
  136. else
  137. lendef:=ossinttype;
  138. { volatility of the ansistring/widestring refers to the volatility of the
  139. string pointer, not of the string data }
  140. hlcg.reference_reset_base(href,left.resultdef,left.location.register,-lendef.size,ctempposinvalid,lendef.alignment,[]);
  141. extra_slots:=thlcgwasm(hlcg).prepare_stack_for_ref(current_asmdata.CurrAsmList,href,false);
  142. thlcgwasm(hlcg).a_load_ref_stack(current_asmdata.CurrAsmList,lendef,href,extra_slots);
  143. if is_widestring(left.resultdef) then
  144. thlcgwasm(hlcg).a_op_const_stack(current_asmdata.CurrAsmList,OP_SHR,resultdef,1);
  145. { Dynamic arrays do not have their length attached but their maximum index }
  146. if is_dynamic_array(left.resultdef) then
  147. thlcgwasm(hlcg).a_op_const_stack(current_asmdata.CurrAsmList,OP_ADD,resultdef,1);
  148. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_end_if) );
  149. thlcgwasm(hlcg).decblock;
  150. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  151. location.register:=hlcg.getregisterfordef(current_asmdata.CurrAsmList,resultdef);
  152. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  153. end;
  154. end;
  155. begin
  156. cinlinenode:=twasminlinenode;
  157. end.