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