njvmcon.pas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. {
  2. Copyright (c) 1998-2011 by Florian Klaempfl and Jonas Maebe
  3. Generate assembler for constant nodes 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 njvmcon;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. symtype,
  22. node,ncon,ncgcon;
  23. type
  24. tjvmrealconstnode = class(tcgrealconstnode)
  25. procedure pass_generate_code;override;
  26. end;
  27. tjvmstringconstnode = class(tstringconstnode)
  28. function pass_1: tnode; override;
  29. procedure pass_generate_code;override;
  30. end;
  31. implementation
  32. uses
  33. globtype,cutils,widestr,verbose,
  34. symdef,symsym,symtable,symconst,
  35. aasmdata,aasmcpu,defutil,
  36. ncal,
  37. cgbase,hlcgobj,hlcgcpu,cgutils,cpubase
  38. ;
  39. {*****************************************************************************
  40. TJVMREALCONSTNODE
  41. *****************************************************************************}
  42. procedure tjvmrealconstnode.pass_generate_code;
  43. begin
  44. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  45. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  46. thlcgjvm(hlcg).a_loadfpu_const_stack(current_asmdata.CurrAsmList,resultdef,value_real);
  47. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  48. end;
  49. { tcgstringconstnode }
  50. function tjvmstringconstnode.pass_1: tnode;
  51. var
  52. astrclass: tobjectdef;
  53. psym: tsym;
  54. pw: pcompilerwidestring;
  55. begin
  56. { all Java strings are utf-16. However, there is no way to
  57. declare a constant array of bytes (or any other type), those
  58. have to be constructed by declaring a final field and then
  59. initialising them in the class constructor element per
  60. element. We therefore put the straight ASCII values into
  61. the UTF-16 string, and then at run time extract those and
  62. store them in an Ansistring/AnsiChar array }
  63. result:=inherited pass_1;
  64. if assigned(result) or
  65. (cst_type in [cst_unicodestring,cst_widestring]) then
  66. exit;
  67. { convert the constant into a widestring representation without any
  68. code page conversion }
  69. initwidestring(pw);
  70. ascii2unicode(value_str,len,pw,false);
  71. ansistringdispose(value_str,len);
  72. pcompilerwidestring(value_str):=pw;
  73. cst_type:=cst_unicodestring;
  74. { and now add a node to convert the data into ansistring format at
  75. run time }
  76. astrclass:=tobjectdef(search_system_type('ANSISTRINGCLASS').typedef);
  77. psym:=search_struct_member(astrclass,'CREATEFROMLITERALSTRINGBYTES');
  78. if not assigned(psym) or
  79. (psym.typ<>procsym) then
  80. internalerror(2011052001);
  81. { since self will be freed, have to make a copy }
  82. result:=ccallnode.create(ccallparanode.create(self.getcopy,nil),
  83. tprocsym(psym),psym.owner,nil,[]);
  84. end;
  85. procedure tjvmstringconstnode.pass_generate_code;
  86. begin
  87. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  88. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  89. case cst_type of
  90. cst_ansistring:
  91. begin
  92. current_asmdata.CurrAsmList.concat(taicpu.op_string(a_ldc,len,value_str));
  93. end;
  94. cst_shortstring,
  95. cst_conststring:
  96. current_asmdata.CurrAsmList.concat(taicpu.op_string(a_ldc,len,value_str));
  97. cst_unicodestring,
  98. cst_widestring:
  99. current_asmdata.CurrAsmList.concat(taicpu.op_wstring(a_ldc,pcompilerwidestring(value_str)));
  100. end;
  101. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  102. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  103. end;
  104. begin
  105. crealconstnode:=tjvmrealconstnode;
  106. cstringconstnode:=tjvmstringconstnode;
  107. end.