njvmcon.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. globals,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. pw: pcompilerwidestring;
  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(pw);
  155. ascii2unicode(value_str,len,current_settings.sourcecodepage,pw,false);
  156. ansistringdispose(value_str,len);
  157. pcompilerwidestring(value_str):=pw;
  158. { and now add a node to convert the data into ansistring format at
  159. run time }
  160. wasansi:=false;
  161. case cst_type of
  162. cst_ansistring:
  163. begin
  164. if len=0 then
  165. begin
  166. { we have to use nil rather than an empty string, because an
  167. empty string has a code page and this messes up the code
  168. page selection logic in the RTL }
  169. exit;
  170. end;
  171. strclass:=tobjectdef(search_system_type('ANSISTRINGCLASS').typedef);
  172. wasansi:=true;
  173. end;
  174. cst_shortstring:
  175. strclass:=tobjectdef(search_system_type('SHORTSTRINGCLASS').typedef);
  176. cst_conststring:
  177. { used for array of char }
  178. strclass:=tobjectdef(search_system_type('ANSICHARARRAYCLASS').typedef);
  179. else
  180. internalerror(2011052401);
  181. end;
  182. cst_type:=cst_unicodestring;
  183. paras:=ccallparanode.create(self.getcopy,nil);
  184. if wasansi then
  185. paras:=ccallparanode.create(
  186. genintconstnode(tstringdef(resultdef).encoding),paras);
  187. { since self will be freed, have to make a copy }
  188. result:=ccallnode.createinternmethodres(
  189. cloadvmtaddrnode.create(ctypenode.create(strclass)),
  190. 'CREATEFROMLITERALSTRINGBYTES',paras,resultdef);
  191. end;
  192. procedure tjvmstringconstnode.pass_generate_code;
  193. begin
  194. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  195. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  196. case cst_type of
  197. cst_ansistring:
  198. begin
  199. if len<>0 then
  200. internalerror(2012052604);
  201. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,resultdef,0,location.register);
  202. { done }
  203. exit;
  204. end;
  205. cst_shortstring,
  206. cst_conststring:
  207. internalerror(2012052601);
  208. cst_unicodestring,
  209. cst_widestring:
  210. current_asmdata.CurrAsmList.concat(taicpu.op_wstring(a_ldc,pcompilerwidestring(value_str)));
  211. else
  212. internalerror(2012052602);
  213. end;
  214. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  215. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  216. end;
  217. {*****************************************************************************
  218. TJVMSETCONSTNODE
  219. *****************************************************************************}
  220. function tjvmsetconstnode.buildsetfromstring(const helpername: string; otherparas: tcallparanode): tnode;
  221. var
  222. pw: pcompilerwidestring;
  223. wc: tcompilerwidechar;
  224. i, j, bit, nulls: longint;
  225. begin
  226. initwidestring(pw);
  227. nulls:=0;
  228. for i:=0 to 15 do
  229. begin
  230. wc:=0;
  231. for bit:=0 to 15 do
  232. if (i*16+bit) in value_set^ then
  233. wc:=wc or (1 shl (15-bit));
  234. { don't add trailing zeroes }
  235. if wc=0 then
  236. inc(nulls)
  237. else
  238. begin
  239. for j:=1 to nulls do
  240. concatwidestringchar(pw,0);
  241. nulls:=0;
  242. concatwidestringchar(pw,wc);
  243. end;
  244. end;
  245. result:=ccallnode.createintern(helpername,
  246. ccallparanode.create(cstringconstnode.createunistr(pw),otherparas));
  247. donewidestring(pw);
  248. end;
  249. function tjvmsetconstnode.buildbitset: tnode;
  250. var
  251. mp: tnode;
  252. begin
  253. if value_set^=[] then
  254. begin
  255. mp:=cloadvmtaddrnode.create(ctypenode.create(java_jubitset));
  256. result:=ccallnode.createinternmethod(mp,'CREATE',nil);
  257. exit;
  258. end;
  259. result:=buildsetfromstring('fpc_bitset_from_string',nil);
  260. end;
  261. function tjvmsetconstnode.buildenumset(const eledef: tdef): tnode;
  262. var
  263. stopnode: tnode;
  264. startnode: tnode;
  265. mp: tnode;
  266. len: longint;
  267. start: longint;
  268. enumele: tnode;
  269. paras: tcallparanode;
  270. hassinglerun: boolean;
  271. begin
  272. hassinglerun:=find_single_elements_run(0, start, len);
  273. if hassinglerun then
  274. begin
  275. mp:=cloadvmtaddrnode.create(ctypenode.create(java_juenumset));
  276. if len=0 then
  277. begin
  278. enumele:=cloadvmtaddrnode.create(ctypenode.create(tenumdef(eledef).getbasedef.classdef));
  279. inserttypeconv_explicit(enumele,search_system_type('JLCLASS').typedef);
  280. paras:=ccallparanode.create(enumele,nil);
  281. result:=ccallnode.createinternmethod(mp,'NONEOF',paras)
  282. end
  283. else
  284. begin
  285. startnode:=cordconstnode.create(start,eledef,false);
  286. { immediately firstpass so the enum gets translated into a JLEnum
  287. instance }
  288. firstpass(startnode);
  289. if len=1 then
  290. result:=ccallnode.createinternmethod(mp,'OF',ccallparanode.create(startnode,nil))
  291. else
  292. begin
  293. stopnode:=cordconstnode.create(start+len-1,eledef,false);
  294. firstpass(stopnode);
  295. result:=ccallnode.createinternmethod(mp,'RANGE',ccallparanode.create(stopnode,ccallparanode.create(startnode,nil)));
  296. end
  297. end
  298. end
  299. else
  300. begin
  301. enumele:=cordconstnode.create(tenumsym(tenumdef(eledef).symtable.symlist[0]).value,eledef,false);
  302. firstpass(enumele);
  303. paras:=ccallparanode.create(enumele,nil);
  304. result:=buildsetfromstring('fpc_enumset_from_string',paras);
  305. end;
  306. end;
  307. function tjvmsetconstnode.pass_1: tnode;
  308. var
  309. eledef: tdef;
  310. begin
  311. { we want set constants to be global, so we can reuse them. However,
  312. if the set's elementdef is local, we can't do that since a global
  313. symbol cannot have a local definition (the compiler will crash when
  314. loading the ppu file afterwards) }
  315. if tsetdef(resultdef).elementdef.owner.symtabletype=localsymtable then
  316. setconsttype:=sct_construct;
  317. result:=nil;
  318. case setconsttype of
  319. (*
  320. sct_constsymbol:
  321. begin
  322. { normally a codegen pass routine, but we have to insert a typed
  323. const in case the set constant does not exist yet, and that
  324. should happen in pass_1 (especially since it involves creating
  325. new nodes, which may even have to be tacked on to this code in
  326. case it's the unit initialization code) }
  327. handlevarsetconst;
  328. { no smallsets }
  329. expectloc:=LOC_CREFERENCE;
  330. end;
  331. *)
  332. sct_notransform:
  333. begin
  334. result:=inherited pass_1;
  335. { no smallsets }
  336. expectloc:=LOC_CREFERENCE;
  337. end;
  338. sct_constsymbol,
  339. sct_construct:
  340. begin
  341. eledef:=tsetdef(resultdef).elementdef;
  342. { empty sets don't have an element type, so we don't know whether we
  343. have to constructor a bitset or enumset (and of which type) }
  344. if not assigned(eledef) then
  345. internalerror(2011070202);
  346. if eledef.typ=enumdef then
  347. begin
  348. result:=buildenumset(eledef);
  349. end
  350. else
  351. begin
  352. result:=buildbitset;
  353. end;
  354. inserttypeconv_explicit(result,getpointerdef(resultdef));
  355. result:=cderefnode.create(result);
  356. end;
  357. else
  358. internalerror(2011060301);
  359. end;
  360. end;
  361. procedure tjvmsetconstnode.pass_generate_code;
  362. begin
  363. case setconsttype of
  364. sct_constsymbol:
  365. begin
  366. { all sets are varsets for the JVM target, no setbase differences }
  367. handlevarsetconst;
  368. end;
  369. else
  370. { must be handled in pass_1 or otherwise transformed }
  371. internalerror(2011070201)
  372. end;
  373. end;
  374. constructor tjvmsetconstnode.create(s: pconstset; def: tdef);
  375. begin
  376. inherited create(s, def);
  377. setconsttype:=sct_constsymbol;
  378. end;
  379. function tjvmsetconstnode.docompare(p: tnode): boolean;
  380. begin
  381. result:=
  382. inherited docompare(p) and
  383. (setconsttype=tjvmsetconstnode(p).setconsttype);
  384. end;
  385. function tjvmsetconstnode.dogetcopy: tnode;
  386. begin
  387. result:=inherited dogetcopy;
  388. tjvmsetconstnode(result).setconsttype:=setconsttype;
  389. end;
  390. function tjvmsetconstnode.emitvarsetconst: tasmsymbol;
  391. var
  392. csym: tconstsym;
  393. ssym: tstaticvarsym;
  394. ps: pnormalset;
  395. begin
  396. { add a read-only typed constant }
  397. new(ps);
  398. ps^:=value_set^;
  399. csym:=tconstsym.create_ptr('_$setconst'+tostr(current_module.symlist.count),constset,ps,resultdef);
  400. csym.visibility:=vis_private;
  401. include(csym.symoptions,sp_internal);
  402. current_module.localsymtable.insert(csym);
  403. { generate assignment of the constant to the typed constant symbol }
  404. ssym:=jvm_add_typed_const_initializer(csym);
  405. result:=current_asmdata.RefAsmSymbol(ssym.mangledname);
  406. end;
  407. function tjvmsetconstnode.find_single_elements_run(from: longint; out start, len: longint): boolean;
  408. var
  409. i: longint;
  410. begin
  411. i:=from;
  412. result:=true;
  413. { find first element in set }
  414. while (i<=255) and
  415. not(i in value_set^) do
  416. inc(i);
  417. start:=i;
  418. { go to end of the run }
  419. while (i<=255) and
  420. (i in value_set^) do
  421. inc(i);
  422. len:=i-start;
  423. { rest must be unset }
  424. while (i<=255) and
  425. not(i in value_set^) do
  426. inc(i);
  427. if i<>256 then
  428. result:=false;
  429. end;
  430. begin
  431. cordconstnode:=tjvmordconstnode;
  432. crealconstnode:=tjvmrealconstnode;
  433. cstringconstnode:=tjvmstringconstnode;
  434. csetconstnode:=tjvmsetconstnode;
  435. end.