njvmcon.pas 17 KB

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