njvmcon.pas 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. tjvmordconstnode = class(tcgordconstnode)
  25. { normally, we convert the enum constant into a load of the
  26. appropriate enum class field in pass_1. In some cases (array index),
  27. we want to keep it as an enum constant however }
  28. enumconstok: boolean;
  29. function pass_1: tnode; override;
  30. function docompare(p: tnode): boolean; override;
  31. function dogetcopy: tnode; override;
  32. end;
  33. tjvmrealconstnode = class(tcgrealconstnode)
  34. procedure pass_generate_code;override;
  35. end;
  36. tjvmstringconstnode = class(tstringconstnode)
  37. function pass_1: tnode; override;
  38. procedure pass_generate_code;override;
  39. end;
  40. implementation
  41. uses
  42. globtype,cutils,widestr,verbose,constexp,
  43. symdef,symsym,symtable,symconst,
  44. aasmdata,aasmcpu,defutil,
  45. ncal,nld,
  46. cgbase,hlcgobj,hlcgcpu,cgutils,cpubase
  47. ;
  48. {*****************************************************************************
  49. TJVMORDCONSTNODE
  50. *****************************************************************************}
  51. function tjvmordconstnode.pass_1: tnode;
  52. var
  53. basedef: tenumdef;
  54. sym: tenumsym;
  55. classfield: tsym;
  56. i: longint;
  57. begin
  58. if (resultdef.typ<>enumdef) or
  59. enumconstok then
  60. begin
  61. result:=inherited pass_1;
  62. exit;
  63. end;
  64. { convert into JVM class instance }
  65. { a) find the enumsym corresponding to the value (may not exist in case
  66. of an explicit typecast of an integer -> error) }
  67. sym:=nil;
  68. basedef:=tenumdef(resultdef).getbasedef;
  69. for i:=0 to tenumdef(resultdef).symtable.symlist.count-1 do
  70. begin
  71. sym:=tenumsym(basedef.symtable.symlist[i]);
  72. if sym.value=value then
  73. break;
  74. sym:=nil;
  75. end;
  76. if not assigned(sym) then
  77. begin
  78. Message(parser_e_range_check_error);
  79. exit;
  80. end;
  81. { b) find the corresponding class field }
  82. classfield:=search_struct_member(basedef.classdef,sym.name);
  83. if not assigned(classfield) or
  84. (classfield.typ<>staticvarsym) then
  85. internalerror(2011062606);
  86. { c) create loadnode of the field }
  87. result:=cloadnode.create(classfield,classfield.owner);
  88. end;
  89. function tjvmordconstnode.docompare(p: tnode): boolean;
  90. begin
  91. result:=inherited docompare(p);
  92. if result then
  93. result:=(enumconstok=tjvmordconstnode(p).enumconstok);
  94. end;
  95. function tjvmordconstnode.dogetcopy: tnode;
  96. begin
  97. result:=inherited dogetcopy;
  98. tjvmordconstnode(result).enumconstok:=enumconstok;
  99. end;
  100. {*****************************************************************************
  101. TJVMREALCONSTNODE
  102. *****************************************************************************}
  103. procedure tjvmrealconstnode.pass_generate_code;
  104. begin
  105. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  106. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  107. thlcgjvm(hlcg).a_loadfpu_const_stack(current_asmdata.CurrAsmList,resultdef,value_real);
  108. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  109. end;
  110. { tcgstringconstnode }
  111. function tjvmstringconstnode.pass_1: tnode;
  112. var
  113. strclass: tobjectdef;
  114. psym: tsym;
  115. pw: pcompilerwidestring;
  116. begin
  117. { all Java strings are utf-16. However, there is no way to
  118. declare a constant array of bytes (or any other type), those
  119. have to be constructed by declaring a final field and then
  120. initialising them in the class constructor element per
  121. element. We therefore put the straight ASCII values into
  122. the UTF-16 string, and then at run time extract those and
  123. store them in an Ansistring/AnsiChar array }
  124. result:=inherited pass_1;
  125. if assigned(result) or
  126. (cst_type in [cst_unicodestring,cst_widestring]) then
  127. exit;
  128. { convert the constant into a widestring representation without any
  129. code page conversion }
  130. initwidestring(pw);
  131. ascii2unicode(value_str,len,pw,false);
  132. ansistringdispose(value_str,len);
  133. pcompilerwidestring(value_str):=pw;
  134. { and now add a node to convert the data into ansistring format at
  135. run time }
  136. case cst_type of
  137. cst_ansistring:
  138. strclass:=tobjectdef(search_system_type('ANSISTRINGCLASS').typedef);
  139. cst_shortstring:
  140. strclass:=tobjectdef(search_system_type('SHORTSTRINGCLASS').typedef);
  141. cst_conststring:
  142. { used for array of char }
  143. strclass:=tobjectdef(search_system_type('ANSICHARARRAYCLASS').typedef);
  144. else
  145. internalerror(2011052401);
  146. end;
  147. cst_type:=cst_unicodestring;
  148. psym:=search_struct_member(strclass,'CREATEFROMLITERALSTRINGBYTES');
  149. if not assigned(psym) or
  150. (psym.typ<>procsym) then
  151. internalerror(2011052001);
  152. { since self will be freed, have to make a copy }
  153. result:=ccallnode.create(ccallparanode.create(self.getcopy,nil),
  154. tprocsym(psym),psym.owner,nil,[]);
  155. end;
  156. procedure tjvmstringconstnode.pass_generate_code;
  157. begin
  158. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  159. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  160. case cst_type of
  161. cst_ansistring:
  162. begin
  163. current_asmdata.CurrAsmList.concat(taicpu.op_string(a_ldc,len,value_str));
  164. end;
  165. cst_shortstring,
  166. cst_conststring:
  167. current_asmdata.CurrAsmList.concat(taicpu.op_string(a_ldc,len,value_str));
  168. cst_unicodestring,
  169. cst_widestring:
  170. current_asmdata.CurrAsmList.concat(taicpu.op_wstring(a_ldc,pcompilerwidestring(value_str)));
  171. end;
  172. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  173. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  174. end;
  175. begin
  176. cordconstnode:=tjvmordconstnode;
  177. crealconstnode:=tjvmrealconstnode;
  178. cstringconstnode:=tjvmstringconstnode;
  179. end.