njvmcon.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. globtype,aasmbase,
  22. symtype,
  23. node,ncal,ncon,ncgcon;
  24. type
  25. tjvmordconstnode = class(tcgordconstnode)
  26. { normally, we convert the enum constant into a load of the
  27. appropriate enum class field in pass_1. In some cases (array index),
  28. we want to keep it as an enum constant however }
  29. enumconstok: boolean;
  30. function pass_1: tnode; override;
  31. function docompare(p: tnode): boolean; override;
  32. function dogetcopy: tnode; override;
  33. end;
  34. tjvmrealconstnode = class(tcgrealconstnode)
  35. procedure pass_generate_code;override;
  36. end;
  37. tjvmstringconstnode = class(tstringconstnode)
  38. function pass_1: tnode; override;
  39. procedure pass_generate_code;override;
  40. end;
  41. tjvmsetconsttype = (
  42. { create symbol for the set constant; the symbol will be initialized
  43. in the class constructor/unit init code (default) }
  44. sct_constsymbol,
  45. { normally, we convert the set constant into a constructor/factory
  46. method to create a set instance. In some cases (simple "in"
  47. expressions, adding an element to an empty set, ...) we want to
  48. keep the set constant instead }
  49. sct_notransform,
  50. { actually construct a JUBitSet/JUEnumSet that contains the set value
  51. (for initializing the sets contstants) }
  52. sct_construct
  53. );
  54. tjvmsetconstnode = class(tcgsetconstnode)
  55. setconsttype: tjvmsetconsttype;
  56. function pass_1: tnode; override;
  57. procedure pass_generate_code; override;
  58. constructor create(s : pconstset;def:tdef);override;
  59. function docompare(p: tnode): boolean; override;
  60. function dogetcopy: tnode; override;
  61. protected
  62. function emitvarsetconst: tasmsymbol; override;
  63. { in case the set has only a single run of consecutive elements,
  64. this function will return its starting index and length }
  65. function find_single_elements_run(from: longint; out start, len: longint): boolean;
  66. function buildbitset: tnode;
  67. function buildenumset(const eledef: tdef): tnode;
  68. function buildsetfromstring(const helpername: string; otherparas: tcallparanode): tnode;
  69. end;
  70. implementation
  71. uses
  72. cutils,widestr,verbose,constexp,fmodule,
  73. symdef,symsym,symtable,symconst,
  74. aasmdata,aasmcpu,defutil,
  75. nutils,ncnv,nld,nmem,pjvm,pass_1,
  76. cgbase,hlcgobj,hlcgcpu,cgutils,cpubase
  77. ;
  78. {*****************************************************************************
  79. TJVMORDCONSTNODE
  80. *****************************************************************************}
  81. function tjvmordconstnode.pass_1: tnode;
  82. var
  83. basedef: tenumdef;
  84. sym: tenumsym;
  85. classfield: tsym;
  86. begin
  87. if (resultdef.typ<>enumdef) or
  88. enumconstok then
  89. begin
  90. result:=inherited pass_1;
  91. exit;
  92. end;
  93. { convert into JVM class instance }
  94. { a) find the enumsym corresponding to the value (may not exist in case
  95. of an explicit typecast of an integer -> error) }
  96. sym:=nil;
  97. sym:=tenumsym(tenumdef(resultdef).int2enumsym(int64(value)));
  98. if not assigned(sym) then
  99. begin
  100. Message(parser_e_range_check_error);
  101. result:=nil;
  102. exit;
  103. end;
  104. { b) find the corresponding class field }
  105. basedef:=tenumdef(resultdef).getbasedef;
  106. classfield:=search_struct_member(basedef.classdef,sym.name);
  107. { c) create loadnode of the field }
  108. result:=nil;
  109. if not handle_staticfield_access(classfield,false,result) then
  110. internalerror(2011062606);
  111. end;
  112. function tjvmordconstnode.docompare(p: tnode): boolean;
  113. begin
  114. result:=inherited docompare(p);
  115. if result then
  116. result:=(enumconstok=tjvmordconstnode(p).enumconstok);
  117. end;
  118. function tjvmordconstnode.dogetcopy: tnode;
  119. begin
  120. result:=inherited dogetcopy;
  121. tjvmordconstnode(result).enumconstok:=enumconstok;
  122. end;
  123. {*****************************************************************************
  124. TJVMREALCONSTNODE
  125. *****************************************************************************}
  126. procedure tjvmrealconstnode.pass_generate_code;
  127. begin
  128. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  129. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  130. thlcgjvm(hlcg).a_loadfpu_const_stack(current_asmdata.CurrAsmList,resultdef,value_real);
  131. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  132. end;
  133. { tcgstringconstnode }
  134. function tjvmstringconstnode.pass_1: tnode;
  135. var
  136. strclass: tobjectdef;
  137. psym: tsym;
  138. pw: pcompilerwidestring;
  139. begin
  140. { all Java strings are utf-16. However, there is no way to
  141. declare a constant array of bytes (or any other type), those
  142. have to be constructed by declaring a final field and then
  143. initialising them in the class constructor element per
  144. element. We therefore put the straight ASCII values into
  145. the UTF-16 string, and then at run time extract those and
  146. store them in an Ansistring/AnsiChar array }
  147. result:=inherited pass_1;
  148. if assigned(result) or
  149. (cst_type in [cst_unicodestring,cst_widestring]) then
  150. exit;
  151. { convert the constant into a widestring representation without any
  152. code page conversion }
  153. initwidestring(pw);
  154. ascii2unicode(value_str,len,pw,false);
  155. ansistringdispose(value_str,len);
  156. pcompilerwidestring(value_str):=pw;
  157. { and now add a node to convert the data into ansistring format at
  158. run time }
  159. case cst_type of
  160. cst_ansistring:
  161. strclass:=tobjectdef(search_system_type('ANSISTRINGCLASS').typedef);
  162. cst_shortstring:
  163. strclass:=tobjectdef(search_system_type('SHORTSTRINGCLASS').typedef);
  164. cst_conststring:
  165. { used for array of char }
  166. strclass:=tobjectdef(search_system_type('ANSICHARARRAYCLASS').typedef);
  167. else
  168. internalerror(2011052401);
  169. end;
  170. cst_type:=cst_unicodestring;
  171. psym:=search_struct_member(strclass,'CREATEFROMLITERALSTRINGBYTES');
  172. if not assigned(psym) or
  173. (psym.typ<>procsym) then
  174. internalerror(2011052001);
  175. { since self will be freed, have to make a copy }
  176. result:=ccallnode.create(ccallparanode.create(self.getcopy,nil),
  177. tprocsym(psym),psym.owner,nil,[]);
  178. end;
  179. procedure tjvmstringconstnode.pass_generate_code;
  180. begin
  181. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  182. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  183. case cst_type of
  184. cst_ansistring:
  185. begin
  186. current_asmdata.CurrAsmList.concat(taicpu.op_string(a_ldc,len,value_str));
  187. end;
  188. cst_shortstring,
  189. cst_conststring:
  190. current_asmdata.CurrAsmList.concat(taicpu.op_string(a_ldc,len,value_str));
  191. cst_unicodestring,
  192. cst_widestring:
  193. current_asmdata.CurrAsmList.concat(taicpu.op_wstring(a_ldc,pcompilerwidestring(value_str)));
  194. end;
  195. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  196. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  197. end;
  198. {*****************************************************************************
  199. TJVMSETCONSTNODE
  200. *****************************************************************************}
  201. function tjvmsetconstnode.buildsetfromstring(const helpername: string; otherparas: tcallparanode): tnode;
  202. var
  203. pw: pcompilerwidestring;
  204. wc: tcompilerwidechar;
  205. i, j, bit, nulls: longint;
  206. begin
  207. initwidestring(pw);
  208. nulls:=0;
  209. for i:=0 to 15 do
  210. begin
  211. wc:=0;
  212. for bit:=0 to 15 do
  213. if (i*16+bit) in value_set^ then
  214. wc:=wc or (1 shl (15-bit));
  215. { don't add trailing zeroes }
  216. if wc=0 then
  217. inc(nulls)
  218. else
  219. begin
  220. for j:=1 to nulls do
  221. concatwidestringchar(pw,0);
  222. nulls:=0;
  223. concatwidestringchar(pw,wc);
  224. end;
  225. end;
  226. result:=ccallnode.createintern(helpername,
  227. ccallparanode.create(cstringconstnode.createwstr(pw),otherparas));
  228. donewidestring(pw);
  229. end;
  230. function tjvmsetconstnode.buildbitset: tnode;
  231. var
  232. mp: tnode;
  233. begin
  234. if value_set^=[] then
  235. begin
  236. mp:=cloadvmtaddrnode.create(ctypenode.create(java_jubitset));
  237. result:=ccallnode.createinternmethod(mp,'CREATE',nil);
  238. exit;
  239. end;
  240. result:=buildsetfromstring('fpc_bitset_from_string',nil);
  241. end;
  242. function tjvmsetconstnode.buildenumset(const eledef: tdef): tnode;
  243. var
  244. stopnode: tnode;
  245. startnode: tnode;
  246. mp: tnode;
  247. len: longint;
  248. start: longint;
  249. enumele: tnode;
  250. paras: tcallparanode;
  251. hassinglerun: boolean;
  252. begin
  253. hassinglerun:=find_single_elements_run(0, start, len);
  254. mp:=cloadvmtaddrnode.create(ctypenode.create(java_juenumset));
  255. if hassinglerun then
  256. begin
  257. if len=0 then
  258. begin
  259. enumele:=cloadvmtaddrnode.create(ctypenode.create(tenumdef(eledef).getbasedef.classdef));
  260. inserttypeconv_explicit(enumele,search_system_type('JLCLASS').typedef);
  261. paras:=ccallparanode.create(enumele,nil);
  262. result:=ccallnode.createinternmethod(mp,'NONEOF',paras)
  263. end
  264. else
  265. begin
  266. startnode:=cordconstnode.create(start,eledef,false);
  267. { immediately firstpass so the enum gets translated into a JLEnum
  268. instance }
  269. firstpass(startnode);
  270. if len=1 then
  271. result:=ccallnode.createinternmethod(mp,'OF',ccallparanode.create(startnode,nil))
  272. else
  273. begin
  274. stopnode:=cordconstnode.create(start+len-1,eledef,false);
  275. firstpass(stopnode);
  276. result:=ccallnode.createinternmethod(mp,'RANGE',ccallparanode.create(stopnode,ccallparanode.create(startnode,nil)));
  277. end
  278. end
  279. end
  280. else
  281. begin
  282. enumele:=cordconstnode.create(tenumsym(tenumdef(eledef).symtable.symlist[0]).value,eledef,false);
  283. firstpass(enumele);
  284. paras:=ccallparanode.create(enumele,nil);
  285. result:=buildsetfromstring('fpc_enumset_from_string',paras);
  286. end;
  287. end;
  288. function tjvmsetconstnode.pass_1: tnode;
  289. var
  290. eledef: tdef;
  291. begin
  292. { we want set constants to be global, so we can reuse them. However,
  293. if the set's elementdef is local, we can't do that since a global
  294. symbol cannot have a local definition (the compiler will crash when
  295. loading the ppu file afterwards) }
  296. if tsetdef(resultdef).elementdef.owner.symtabletype=localsymtable then
  297. setconsttype:=sct_construct;
  298. result:=nil;
  299. case setconsttype of
  300. (*
  301. sct_constsymbol:
  302. begin
  303. { normally a codegen pass routine, but we have to insert a typed
  304. const in case the set constant does not exist yet, and that
  305. should happen in pass_1 (especially since it involves creating
  306. new nodes, which may even have to be tacked on to this code in
  307. case it's the unit initialization code) }
  308. handlevarsetconst;
  309. { no smallsets }
  310. expectloc:=LOC_CREFERENCE;
  311. end;
  312. *)
  313. sct_notransform:
  314. begin
  315. result:=inherited pass_1;
  316. { no smallsets }
  317. expectloc:=LOC_CREFERENCE;
  318. end;
  319. sct_constsymbol,
  320. sct_construct:
  321. begin
  322. eledef:=tsetdef(resultdef).elementdef;
  323. { empty sets don't have an element type, so we don't know whether we
  324. have to constructor a bitset or enumset (and of which type) }
  325. if not assigned(eledef) then
  326. internalerror(2011070202);
  327. if eledef.typ=enumdef then
  328. begin
  329. result:=buildenumset(eledef);
  330. end
  331. else
  332. begin
  333. result:=buildbitset;
  334. end;
  335. inserttypeconv_explicit(result,getpointerdef(resultdef));
  336. result:=cderefnode.create(result);
  337. end;
  338. else
  339. internalerror(2011060301);
  340. end;
  341. end;
  342. procedure tjvmsetconstnode.pass_generate_code;
  343. begin
  344. case setconsttype of
  345. sct_constsymbol:
  346. begin
  347. { all sets are varsets for the JVM target, no setbase differences }
  348. handlevarsetconst;
  349. end;
  350. else
  351. { must be handled in pass_1 or otherwise transformed }
  352. internalerror(2011070201)
  353. end;
  354. end;
  355. constructor tjvmsetconstnode.create(s: pconstset; def: tdef);
  356. begin
  357. inherited create(s, def);
  358. setconsttype:=sct_constsymbol;
  359. end;
  360. function tjvmsetconstnode.docompare(p: tnode): boolean;
  361. begin
  362. result:=
  363. inherited docompare(p) and
  364. (setconsttype=tjvmsetconstnode(p).setconsttype);
  365. end;
  366. function tjvmsetconstnode.dogetcopy: tnode;
  367. begin
  368. result:=inherited dogetcopy;
  369. tjvmsetconstnode(result).setconsttype:=setconsttype;
  370. end;
  371. function tjvmsetconstnode.emitvarsetconst: tasmsymbol;
  372. var
  373. csym: tconstsym;
  374. ssym: tstaticvarsym;
  375. ps: pnormalset;
  376. begin
  377. { add a read-only typed constant }
  378. new(ps);
  379. ps^:=value_set^;
  380. csym:=tconstsym.create_ptr('_$setconst'+tostr(current_module.symlist.count),constset,ps,resultdef);
  381. csym.visibility:=vis_private;
  382. include(csym.symoptions,sp_internal);
  383. current_module.localsymtable.insert(csym);
  384. { generate assignment of the constant to the typed constant symbol }
  385. ssym:=jvm_add_typed_const_initializer(csym);
  386. result:=current_asmdata.RefAsmSymbol(ssym.mangledname);
  387. end;
  388. function tjvmsetconstnode.find_single_elements_run(from: longint; out start, len: longint): boolean;
  389. var
  390. i: longint;
  391. begin
  392. i:=from;
  393. result:=true;
  394. { find first element in set }
  395. while (i<=255) and
  396. not(i in value_set^) do
  397. inc(i);
  398. start:=i;
  399. { go to end of the run }
  400. while (i<=255) and
  401. (i in value_set^) do
  402. inc(i);
  403. len:=i-start;
  404. { rest must be unset }
  405. while (i<=255) and
  406. not(i in value_set^) do
  407. inc(i);
  408. if i<>256 then
  409. result:=false;
  410. end;
  411. begin
  412. cordconstnode:=tjvmordconstnode;
  413. crealconstnode:=tjvmrealconstnode;
  414. cstringconstnode:=tjvmstringconstnode;
  415. csetconstnode:=tjvmsetconstnode;
  416. end.