njvmcon.pas 17 KB

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