symcpu.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. {
  2. Copyright (c) 2014 by Florian Klaempfl
  3. Symbol table overrides for 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 symcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. aasmdata,
  23. symtype,
  24. symdef,symsym;
  25. type
  26. { defs }
  27. tcpufiledef = class(tfiledef)
  28. end;
  29. tcpufiledefclass = class of tcpufiledef;
  30. tcpuvariantdef = class(tvariantdef)
  31. end;
  32. tcpuvariantdefclass = class of tcpuvariantdef;
  33. tcpuformaldef = class(tformaldef)
  34. end;
  35. tcpuformaldefclass = class of tcpuformaldef;
  36. tcpuforwarddef = class(tforwarddef)
  37. end;
  38. tcpuforwarddefclass = class of tcpuforwarddef;
  39. tcpuundefineddef = class(tundefineddef)
  40. end;
  41. tcpuundefineddefclass = class of tcpuundefineddef;
  42. tcpuerrordef = class(terrordef)
  43. end;
  44. tcpuerrordefclass = class of tcpuerrordef;
  45. tcpupointerdef = class(tpointerdef)
  46. end;
  47. tcpupointerdefclass = class of tcpupointerdef;
  48. tcpurecorddef = class(trecorddef)
  49. end;
  50. tcpurecorddefclass = class of tcpurecorddef;
  51. tcpuimplementedinterface = class(timplementedinterface)
  52. end;
  53. tcpuimplementedinterfaceclass = class of tcpuimplementedinterface;
  54. tcpuobjectdef = class(tobjectdef)
  55. end;
  56. tcpuobjectdefclass = class of tcpuobjectdef;
  57. tcpuclassrefdef = class(tclassrefdef)
  58. end;
  59. tcpuclassrefdefclass = class of tcpuclassrefdef;
  60. tcpuarraydef = class(tarraydef)
  61. end;
  62. tcpuarraydefclass = class of tcpuarraydef;
  63. tcpuorddef = class(torddef)
  64. end;
  65. tcpuorddefclass = class of tcpuorddef;
  66. tcpufloatdef = class(tfloatdef)
  67. end;
  68. tcpufloatdefclass = class of tcpufloatdef;
  69. tcpuprocvardef = class(tprocvardef)
  70. protected
  71. procedure ppuwrite_platform(ppufile: tcompilerppufile); override;
  72. procedure ppuload_platform(ppufile: tcompilerppufile); override;
  73. public
  74. { class representing this procvar on the Java side }
  75. classdef : tobjectdef;
  76. classdefderef : tderef;
  77. procedure buildderef;override;
  78. procedure deref;override;
  79. function getcopy: tstoreddef; override;
  80. end;
  81. tcpuprocvardefclass = class of tcpuprocvardef;
  82. tcpuprocdef = class(tprocdef)
  83. { generated assembler code; used by JVM backend so it can afterwards
  84. easily write out all methods grouped per class }
  85. exprasmlist : TAsmList;
  86. function jvmmangledbasename(signature: boolean): TSymStr;
  87. function mangledname: TSymStr; override;
  88. destructor destroy; override;
  89. end;
  90. tcpuprocdefclass = class of tcpuprocdef;
  91. tcpustringdef = class(tstringdef)
  92. end;
  93. tcpustringdefclass = class of tcpustringdef;
  94. tcpuenumdef = class(tenumdef)
  95. protected
  96. procedure ppuload_platform(ppufile: tcompilerppufile); override;
  97. procedure ppuwrite_platform(ppufile: tcompilerppufile); override;
  98. public
  99. { class representing this enum on the Java side }
  100. classdef : tobjectdef;
  101. classdefderef : tderef;
  102. function getcopy: tstoreddef; override;
  103. procedure buildderef; override;
  104. procedure deref; override;
  105. end;
  106. tcpuenumdefclass = class of tcpuenumdef;
  107. tcpusetdef = class(tsetdef)
  108. end;
  109. tcpusetdefclass = class of tcpusetdef;
  110. { syms }
  111. tcpulabelsym = class(tlabelsym)
  112. end;
  113. tcpulabelsymclass = class of tcpulabelsym;
  114. tcpuunitsym = class(tunitsym)
  115. end;
  116. tcpuunitsymclass = class of tcpuunitsym;
  117. tcpunamespacesym = class(tnamespacesym)
  118. end;
  119. tcpunamespacesymclass = class of tcpunamespacesym;
  120. tcpuprocsym = class(tprocsym)
  121. procedure check_forward; override;
  122. end;
  123. tcpuprocsymclass = class of tcpuprocsym;
  124. tcpuypesym = class(ttypesym)
  125. end;
  126. tcpuypesymclass = class of tcpuypesym;
  127. tcpufieldvarsym = class(tfieldvarsym)
  128. procedure set_externalname(const s: string); override;
  129. function mangledname: TSymStr; override;
  130. end;
  131. tcpufieldvarsymclass = class of tcpufieldvarsym;
  132. tcpulocalvarsym = class(tlocalvarsym)
  133. end;
  134. tcpulocalvarsymclass = class of tcpulocalvarsym;
  135. tcpuparavarsym = class(tparavarsym)
  136. end;
  137. tcpuparavarsymclass = class of tcpuparavarsym;
  138. tcpustaticvarsym = class(tstaticvarsym)
  139. procedure set_mangledname(const s: TSymStr); override;
  140. function mangledname: TSymStr; override;
  141. end;
  142. tcpustaticvarsymclass = class of tcpustaticvarsym;
  143. tcpuabsolutevarsym = class(tabsolutevarsym)
  144. end;
  145. tcpuabsolutevarsymclass = class of tcpuabsolutevarsym;
  146. tcpupropertysym = class(tpropertysym)
  147. end;
  148. tcpupropertysymclass = class of tcpupropertysym;
  149. tcpuconstsym = class(tconstsym)
  150. end;
  151. tcpuconstsymclass = class of tcpuconstsym;
  152. tcpuenumsym = class(tenumsym)
  153. end;
  154. tcpuenumsymclass = class of tcpuenumsym;
  155. tcpusyssym = class(tsyssym)
  156. end;
  157. tcpusyssymclass = class of tcpusyssym;
  158. const
  159. pbestrealtype : ^tdef = @s64floattype;
  160. implementation
  161. uses
  162. verbose,cutils,cclasses,
  163. symconst,symbase,jvmdef,
  164. paramgr;
  165. {****************************************************************************
  166. tcpuenumdef
  167. ****************************************************************************}
  168. procedure tcpuenumdef.ppuload_platform(ppufile: tcompilerppufile);
  169. begin
  170. inherited;
  171. ppufile.getderef(classdefderef);
  172. end;
  173. procedure tcpuenumdef.ppuwrite_platform(ppufile: tcompilerppufile);
  174. begin
  175. inherited;
  176. ppufile.putderef(classdefderef);
  177. end;
  178. function tcpuenumdef.getcopy: tstoreddef;
  179. begin
  180. result:=inherited;
  181. tcpuenumdef(result).classdef:=classdef;
  182. end;
  183. procedure tcpuenumdef.buildderef;
  184. begin
  185. inherited;
  186. classdefderef.build(classdef);
  187. end;
  188. procedure tcpuenumdef.deref;
  189. begin
  190. inherited;
  191. classdef:=tobjectdef(classdefderef.resolve);
  192. end;
  193. {****************************************************************************
  194. tcpuprocdef
  195. ****************************************************************************}
  196. function tcpuprocdef.jvmmangledbasename(signature: boolean): TSymStr;
  197. var
  198. vs: tparavarsym;
  199. i: longint;
  200. founderror: tdef;
  201. tmpresult: TSymStr;
  202. container: tsymtable;
  203. begin
  204. { format:
  205. * method definition (in Jasmin):
  206. (private|protected|public) [static] method(parametertypes)returntype
  207. * method invocation
  208. package/class/method(parametertypes)returntype
  209. -> store common part: method(parametertypes)returntype and
  210. adorn as required when using it.
  211. }
  212. if not signature then
  213. begin
  214. { method name }
  215. { special names for constructors and class constructors }
  216. if proctypeoption=potype_constructor then
  217. tmpresult:='<init>'
  218. else if proctypeoption in [potype_class_constructor,potype_unitinit] then
  219. tmpresult:='<clinit>'
  220. else if po_has_importname in procoptions then
  221. begin
  222. if assigned(import_name) then
  223. tmpresult:=import_name^
  224. else
  225. internalerror(2010122608);
  226. end
  227. else
  228. begin
  229. tmpresult:=procsym.realname;
  230. if tmpresult[1]='$' then
  231. tmpresult:=copy(tmpresult,2,length(tmpresult)-1);
  232. { nested functions }
  233. container:=owner;
  234. while container.symtabletype=localsymtable do
  235. begin
  236. tmpresult:='$'+tprocdef(owner.defowner).procsym.realname+'$'+tostr(tprocdef(owner.defowner).procsym.symid)+'$'+tmpresult;
  237. container:=container.defowner.owner;
  238. end;
  239. end;
  240. end
  241. else
  242. tmpresult:='';
  243. { parameter types }
  244. tmpresult:=tmpresult+'(';
  245. { not the case for the main program (not required for defaultmangledname
  246. because setmangledname() is called for the main program; in case of
  247. the JVM, this only sets the importname, however) }
  248. if assigned(paras) then
  249. begin
  250. init_paraloc_info(callerside);
  251. for i:=0 to paras.count-1 do
  252. begin
  253. vs:=tparavarsym(paras[i]);
  254. { function result is not part of the mangled name }
  255. if vo_is_funcret in vs.varoptions then
  256. continue;
  257. { self pointer neither, except for class methods (the JVM only
  258. supports static class methods natively, so the self pointer
  259. here is a regular parameter as far as the JVM is concerned }
  260. if not(po_classmethod in procoptions) and
  261. (vo_is_self in vs.varoptions) then
  262. continue;
  263. { passing by reference is emulated by passing an array of one
  264. element containing the value; for types that aren't pointers
  265. in regular Pascal, simply passing the underlying pointer type
  266. does achieve regular call-by-reference semantics though;
  267. formaldefs always have to be passed like that because their
  268. contents can be replaced }
  269. if paramanager.push_copyout_param(vs.varspez,vs.vardef,proccalloption) then
  270. tmpresult:=tmpresult+'[';
  271. { Add the parameter type. }
  272. if not jvmaddencodedtype(vs.vardef,false,tmpresult,signature,founderror) then
  273. { an internalerror here is also triggered in case of errors in the source code }
  274. tmpresult:='<error>';
  275. end;
  276. end;
  277. tmpresult:=tmpresult+')';
  278. { And the type of the function result (void in case of a procedure and
  279. constructor). }
  280. if (proctypeoption in [potype_constructor,potype_class_constructor]) then
  281. jvmaddencodedtype(voidtype,false,tmpresult,signature,founderror)
  282. else if not jvmaddencodedtype(returndef,false,tmpresult,signature,founderror) then
  283. { an internalerror here is also triggered in case of errors in the source code }
  284. tmpresult:='<error>';
  285. result:=tmpresult;
  286. end;
  287. function tcpuprocdef.mangledname: TSymStr;
  288. begin
  289. if _mangledname='' then
  290. begin
  291. result:=jvmmangledbasename(false);
  292. if (po_has_importdll in procoptions) then
  293. begin
  294. { import_dll comes from "external 'import_dll_name' name 'external_name'" }
  295. if assigned(import_dll) then
  296. result:=import_dll^+'/'+result
  297. else
  298. internalerror(2010122607);
  299. end
  300. else
  301. jvmaddtypeownerprefix(owner,mangledname);
  302. _mangledname:=result;
  303. end
  304. else
  305. result:=_mangledname;
  306. end;
  307. destructor tcpuprocdef.destroy;
  308. begin
  309. exprasmlist.free;
  310. inherited destroy;
  311. end;
  312. {****************************************************************************
  313. tcpuprocvardef
  314. ****************************************************************************}
  315. procedure tcpuprocvardef.ppuwrite_platform(ppufile: tcompilerppufile);
  316. begin
  317. inherited;
  318. ppufile.putderef(classdefderef);
  319. end;
  320. procedure tcpuprocvardef.ppuload_platform(ppufile: tcompilerppufile);
  321. begin
  322. inherited;
  323. ppufile.getderef(classdefderef);
  324. end;
  325. procedure tcpuprocvardef.buildderef;
  326. begin
  327. inherited buildderef;
  328. classdefderef.build(classdef);
  329. end;
  330. procedure tcpuprocvardef.deref;
  331. begin
  332. inherited deref;
  333. classdef:=tobjectdef(classdefderef.resolve);
  334. end;
  335. function tcpuprocvardef.getcopy: tstoreddef;
  336. begin
  337. result:=inherited;
  338. tcpuprocvardef(result).classdef:=classdef;
  339. end;
  340. {****************************************************************************
  341. tcpuprocsym
  342. ****************************************************************************}
  343. procedure tcpuprocsym.check_forward;
  344. var
  345. curri, checki: longint;
  346. currpd, checkpd: tprocdef;
  347. begin
  348. inherited;
  349. { check for conflicts based on mangled name, because several FPC
  350. types/constructs map to the same JVM mangled name }
  351. for curri:=0 to FProcdefList.Count-2 do
  352. begin
  353. currpd:=tprocdef(FProcdefList[curri]);
  354. if (po_external in currpd.procoptions) or
  355. (currpd.proccalloption=pocall_internproc) then
  356. continue;
  357. for checki:=curri+1 to FProcdefList.Count-1 do
  358. begin
  359. checkpd:=tprocdef(FProcdefList[checki]);
  360. if po_external in checkpd.procoptions then
  361. continue;
  362. if currpd.mangledname=checkpd.mangledname then
  363. begin
  364. MessagePos(checkpd.fileinfo,parser_e_overloaded_have_same_mangled_name);
  365. MessagePos1(currpd.fileinfo,sym_h_param_list,currpd.customprocname([pno_mangledname]));
  366. MessagePos1(checkpd.fileinfo,sym_h_param_list,checkpd.customprocname([pno_mangledname]));
  367. end;
  368. end;
  369. end;
  370. inherited;
  371. end;
  372. {****************************************************************************
  373. tcpustaticvarsym
  374. ****************************************************************************}
  375. procedure tcpustaticvarsym.set_mangledname(const s: TSymStr);
  376. begin
  377. inherited;
  378. _mangledname:=jvmmangledbasename(self,s,false);
  379. jvmaddtypeownerprefix(owner,_mangledname);
  380. end;
  381. function tcpustaticvarsym.mangledname: TSymStr;
  382. begin
  383. if _mangledname='' then
  384. begin
  385. if _mangledbasename='' then
  386. _mangledname:=jvmmangledbasename(self,false)
  387. else
  388. _mangledname:=jvmmangledbasename(self,_mangledbasename,false);
  389. jvmaddtypeownerprefix(owner,_mangledname);
  390. end;
  391. result:=_mangledname;
  392. end;
  393. {****************************************************************************
  394. tcpufieldvarsym
  395. ****************************************************************************}
  396. procedure tcpufieldvarsym.set_externalname(const s: string);
  397. begin
  398. { make sure it is recalculated }
  399. cachedmangledname:='';
  400. if is_java_class_or_interface(tdef(owner.defowner)) then
  401. begin
  402. externalname:=stringdup(s);
  403. include(varoptions,vo_has_mangledname);
  404. end
  405. else
  406. internalerror(2011031201);
  407. end;
  408. function tcpufieldvarsym.mangledname: TSymStr;
  409. begin
  410. if is_java_class_or_interface(tdef(owner.defowner)) or
  411. (tdef(owner.defowner).typ=recorddef) then
  412. begin
  413. if cachedmangledname<>'' then
  414. result:=cachedmangledname
  415. else
  416. begin
  417. result:=jvmmangledbasename(self,false);
  418. jvmaddtypeownerprefix(owner,result);
  419. cachedmangledname:=result;
  420. end;
  421. end
  422. else
  423. result:=inherited;
  424. end;
  425. begin
  426. { used tdef classes }
  427. cfiledef:=tcpufiledef;
  428. cvariantdef:=tcpuvariantdef;
  429. cformaldef:=tcpuformaldef;
  430. cforwarddef:=tcpuforwarddef;
  431. cundefineddef:=tcpuundefineddef;
  432. cerrordef:=tcpuerrordef;
  433. cpointerdef:=tcpupointerdef;
  434. crecorddef:=tcpurecorddef;
  435. cimplementedinterface:=tcpuimplementedinterface;
  436. cobjectdef:=tcpuobjectdef;
  437. cclassrefdef:=tcpuclassrefdef;
  438. carraydef:=tcpuarraydef;
  439. corddef:=tcpuorddef;
  440. cfloatdef:=tcpufloatdef;
  441. cprocvardef:=tcpuprocvardef;
  442. cprocdef:=tcpuprocdef;
  443. cstringdef:=tcpustringdef;
  444. cenumdef:=tcpuenumdef;
  445. csetdef:=tcpusetdef;
  446. { used tsym classes }
  447. clabelsym:=tcpulabelsym;
  448. cunitsym:=tcpuunitsym;
  449. cnamespacesym:=tcpunamespacesym;
  450. cprocsym:=tcpuprocsym;
  451. ctypesym:=tcpuypesym;
  452. cfieldvarsym:=tcpufieldvarsym;
  453. clocalvarsym:=tcpulocalvarsym;
  454. cparavarsym:=tcpuparavarsym;
  455. cstaticvarsym:=tcpustaticvarsym;
  456. cabsolutevarsym:=tcpuabsolutevarsym;
  457. cpropertysym:=tcpupropertysym;
  458. cconstsym:=tcpuconstsym;
  459. cenumsym:=tcpuenumsym;
  460. csyssym:=tcpusyssym;
  461. end.