njvmcon.pas 17 KB

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