njvmcon.pas 16 KB

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