nwasminl.pas 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. ncginl;
  22. type
  23. { twasminlinenode }
  24. twasminlinenode = class(tcginlinenode)
  25. public
  26. procedure second_length;override;
  27. end;
  28. implementation
  29. uses
  30. ninl,
  31. cpubase,
  32. aasmbase,aasmdata,aasmcpu,
  33. cgbase,cgutils,
  34. hlcgobj,hlcgcpu,
  35. defutil,pass_2,
  36. symtype,symdef;
  37. {*****************************************************************************
  38. twasminlinenode
  39. *****************************************************************************}
  40. procedure twasminlinenode.second_length;
  41. var
  42. lendef : tdef;
  43. href : treference;
  44. extra_slots: LongInt;
  45. begin
  46. secondpass(left);
  47. if is_shortstring(left.resultdef) then
  48. begin
  49. location_copy(location,left.location);
  50. location.size:=OS_8;
  51. end
  52. else
  53. begin
  54. { length in ansi/wide strings and high in dynamic arrays is at offset -sizeof(pint) }
  55. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  56. thlcgwasm(hlcg).a_cmp_const_reg_stack(current_asmdata.CurrAsmList,left.resultdef,OC_EQ,0,left.location.register);
  57. current_asmdata.CurrAsmList.Concat(taicpu.op_functype(a_if,TWasmFuncType.Create([],[wbt_i32])));
  58. thlcgwasm(hlcg).incblock;
  59. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  60. current_asmdata.CurrAsmList.Concat(taicpu.op_const(a_i32_const,0));
  61. thlcgwasm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  62. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_else) );
  63. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  64. { the length of a widestring is a 32 bit unsigned int. Since every
  65. character occupies 2 bytes, on a 32 bit platform you can express
  66. the maximum length using 31 bits. On a 64 bit platform, it may be
  67. 32 bits. This means that regardless of the platform, a location
  68. with size OS_SINT/ossinttype can hold the length without
  69. overflowing (this code returns an ossinttype value) }
  70. if is_widestring(left.resultdef) then
  71. lendef:=u32inttype
  72. else
  73. lendef:=ossinttype;
  74. { volatility of the ansistring/widestring refers to the volatility of the
  75. string pointer, not of the string data }
  76. hlcg.reference_reset_base(href,left.resultdef,left.location.register,-lendef.size,ctempposinvalid,lendef.alignment,[]);
  77. extra_slots:=thlcgwasm(hlcg).prepare_stack_for_ref(current_asmdata.CurrAsmList,href,false);
  78. thlcgwasm(hlcg).a_load_ref_stack(current_asmdata.CurrAsmList,lendef,href,extra_slots);
  79. if is_widestring(left.resultdef) then
  80. thlcgwasm(hlcg).a_op_const_stack(current_asmdata.CurrAsmList,OP_SHR,resultdef,1);
  81. { Dynamic arrays do not have their length attached but their maximum index }
  82. if is_dynamic_array(left.resultdef) then
  83. thlcgwasm(hlcg).a_op_const_stack(current_asmdata.CurrAsmList,OP_ADD,resultdef,1);
  84. current_asmdata.CurrAsmList.Concat( taicpu.op_none(a_end_if) );
  85. thlcgwasm(hlcg).decblock;
  86. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  87. location.register:=hlcg.getregisterfordef(current_asmdata.CurrAsmList,resultdef);
  88. thlcgwasm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,resultdef,location);
  89. end;
  90. end;
  91. begin
  92. cinlinenode:=twasminlinenode;
  93. end.