njvmcon.pas 17 KB

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