symcpu.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. end;
  122. tcpuprocsymclass = class of tcpuprocsym;
  123. tcpuypesym = class(ttypesym)
  124. end;
  125. tcpuypesymclass = class of tcpuypesym;
  126. tcpufieldvarsym = class(tfieldvarsym)
  127. procedure set_externalname(const s: string); override;
  128. function mangledname: TSymStr; override;
  129. end;
  130. tcpufieldvarsymclass = class of tcpufieldvarsym;
  131. tcpulocalvarsym = class(tlocalvarsym)
  132. end;
  133. tcpulocalvarsymclass = class of tcpulocalvarsym;
  134. tcpuparavarsym = class(tparavarsym)
  135. end;
  136. tcpuparavarsymclass = class of tcpuparavarsym;
  137. tcpustaticvarsym = class(tstaticvarsym)
  138. procedure set_mangledname(const s: TSymStr); override;
  139. function mangledname: TSymStr; override;
  140. end;
  141. tcpustaticvarsymclass = class of tcpustaticvarsym;
  142. tcpuabsolutevarsym = class(tabsolutevarsym)
  143. end;
  144. tcpuabsolutevarsymclass = class of tcpuabsolutevarsym;
  145. tcpupropertysym = class(tpropertysym)
  146. end;
  147. tcpupropertysymclass = class of tcpupropertysym;
  148. tcpuconstsym = class(tconstsym)
  149. end;
  150. tcpuconstsymclass = class of tcpuconstsym;
  151. tcpuenumsym = class(tenumsym)
  152. end;
  153. tcpuenumsymclass = class of tcpuenumsym;
  154. tcpusyssym = class(tsyssym)
  155. end;
  156. tcpusyssymclass = class of tcpusyssym;
  157. const
  158. pbestrealtype : ^tdef = @s64floattype;
  159. implementation
  160. uses
  161. verbose,cutils,
  162. symconst,symbase,jvmdef,
  163. paramgr;
  164. procedure tcpuenumdef.ppuload_platform(ppufile: tcompilerppufile);
  165. begin
  166. inherited;
  167. ppufile.getderef(classdefderef);
  168. end;
  169. procedure tcpuenumdef.ppuwrite_platform(ppufile: tcompilerppufile);
  170. begin
  171. inherited;
  172. ppufile.putderef(classdefderef);
  173. end;
  174. function tcpuenumdef.getcopy: tstoreddef;
  175. begin
  176. result:=inherited;
  177. tcpuenumdef(result).classdef:=classdef;
  178. end;
  179. procedure tcpuenumdef.buildderef;
  180. begin
  181. inherited;
  182. classdefderef.build(classdef);
  183. end;
  184. procedure tcpuenumdef.deref;
  185. begin
  186. inherited;
  187. classdef:=tobjectdef(classdefderef.resolve);
  188. end;
  189. {****************************************************************************
  190. tcpuprocdef
  191. ****************************************************************************}
  192. function tcpuprocdef.jvmmangledbasename(signature: boolean): TSymStr;
  193. var
  194. vs: tparavarsym;
  195. i: longint;
  196. founderror: tdef;
  197. tmpresult: TSymStr;
  198. container: tsymtable;
  199. begin
  200. { format:
  201. * method definition (in Jasmin):
  202. (private|protected|public) [static] method(parametertypes)returntype
  203. * method invocation
  204. package/class/method(parametertypes)returntype
  205. -> store common part: method(parametertypes)returntype and
  206. adorn as required when using it.
  207. }
  208. if not signature then
  209. begin
  210. { method name }
  211. { special names for constructors and class constructors }
  212. if proctypeoption=potype_constructor then
  213. tmpresult:='<init>'
  214. else if proctypeoption in [potype_class_constructor,potype_unitinit] then
  215. tmpresult:='<clinit>'
  216. else if po_has_importname in procoptions then
  217. begin
  218. if assigned(import_name) then
  219. tmpresult:=import_name^
  220. else
  221. internalerror(2010122608);
  222. end
  223. else
  224. begin
  225. tmpresult:=procsym.realname;
  226. if tmpresult[1]='$' then
  227. tmpresult:=copy(tmpresult,2,length(tmpresult)-1);
  228. { nested functions }
  229. container:=owner;
  230. while container.symtabletype=localsymtable do
  231. begin
  232. tmpresult:='$'+tprocdef(owner.defowner).procsym.realname+'$'+tostr(tprocdef(owner.defowner).procsym.symid)+'$'+tmpresult;
  233. container:=container.defowner.owner;
  234. end;
  235. end;
  236. end
  237. else
  238. tmpresult:='';
  239. { parameter types }
  240. tmpresult:=tmpresult+'(';
  241. { not the case for the main program (not required for defaultmangledname
  242. because setmangledname() is called for the main program; in case of
  243. the JVM, this only sets the importname, however) }
  244. if assigned(paras) then
  245. begin
  246. init_paraloc_info(callerside);
  247. for i:=0 to paras.count-1 do
  248. begin
  249. vs:=tparavarsym(paras[i]);
  250. { function result is not part of the mangled name }
  251. if vo_is_funcret in vs.varoptions then
  252. continue;
  253. { self pointer neither, except for class methods (the JVM only
  254. supports static class methods natively, so the self pointer
  255. here is a regular parameter as far as the JVM is concerned }
  256. if not(po_classmethod in procoptions) and
  257. (vo_is_self in vs.varoptions) then
  258. continue;
  259. { passing by reference is emulated by passing an array of one
  260. element containing the value; for types that aren't pointers
  261. in regular Pascal, simply passing the underlying pointer type
  262. does achieve regular call-by-reference semantics though;
  263. formaldefs always have to be passed like that because their
  264. contents can be replaced }
  265. if paramanager.push_copyout_param(vs.varspez,vs.vardef,proccalloption) then
  266. tmpresult:=tmpresult+'[';
  267. { Add the parameter type. }
  268. if not jvmaddencodedtype(vs.vardef,false,tmpresult,signature,founderror) then
  269. { an internalerror here is also triggered in case of errors in the source code }
  270. tmpresult:='<error>';
  271. end;
  272. end;
  273. tmpresult:=tmpresult+')';
  274. { And the type of the function result (void in case of a procedure and
  275. constructor). }
  276. if (proctypeoption in [potype_constructor,potype_class_constructor]) then
  277. jvmaddencodedtype(voidtype,false,tmpresult,signature,founderror)
  278. else if not jvmaddencodedtype(returndef,false,tmpresult,signature,founderror) then
  279. { an internalerror here is also triggered in case of errors in the source code }
  280. tmpresult:='<error>';
  281. result:=tmpresult;
  282. end;
  283. function tcpuprocdef.mangledname: TSymStr;
  284. begin
  285. if _mangledname='' then
  286. begin
  287. result:=jvmmangledbasename(false);
  288. if (po_has_importdll in procoptions) then
  289. begin
  290. { import_dll comes from "external 'import_dll_name' name 'external_name'" }
  291. if assigned(import_dll) then
  292. result:=import_dll^+'/'+result
  293. else
  294. internalerror(2010122607);
  295. end
  296. else
  297. jvmaddtypeownerprefix(owner,mangledname);
  298. _mangledname:=result;
  299. end
  300. else
  301. result:=_mangledname;
  302. end;
  303. destructor tcpuprocdef.destroy;
  304. begin
  305. exprasmlist.free;
  306. inherited destroy;
  307. end;
  308. {****************************************************************************
  309. tcpuprocvardef
  310. ****************************************************************************}
  311. procedure tcpuprocvardef.ppuwrite_platform(ppufile: tcompilerppufile);
  312. begin
  313. inherited;
  314. ppufile.putderef(classdefderef);
  315. end;
  316. procedure tcpuprocvardef.ppuload_platform(ppufile: tcompilerppufile);
  317. begin
  318. inherited;
  319. ppufile.getderef(classdefderef);
  320. end;
  321. procedure tcpuprocvardef.buildderef;
  322. begin
  323. inherited buildderef;
  324. classdefderef.build(classdef);
  325. end;
  326. procedure tcpuprocvardef.deref;
  327. begin
  328. inherited deref;
  329. classdef:=tobjectdef(classdefderef.resolve);
  330. end;
  331. function tcpuprocvardef.getcopy: tstoreddef;
  332. begin
  333. result:=inherited;
  334. tcpuprocvardef(result).classdef:=classdef;
  335. end;
  336. {****************************************************************************
  337. tcpustaticvarsym
  338. ****************************************************************************}
  339. procedure tcpustaticvarsym.set_mangledname(const s: TSymStr);
  340. begin
  341. inherited;
  342. _mangledname:=jvmmangledbasename(self,s,false);
  343. jvmaddtypeownerprefix(owner,_mangledname);
  344. end;
  345. function tcpustaticvarsym.mangledname: TSymStr;
  346. begin
  347. if _mangledname='' then
  348. begin
  349. if _mangledbasename='' then
  350. _mangledname:=jvmmangledbasename(self,false)
  351. else
  352. _mangledname:=jvmmangledbasename(self,_mangledbasename,false);
  353. jvmaddtypeownerprefix(owner,_mangledname);
  354. end;
  355. result:=_mangledname;
  356. end;
  357. {****************************************************************************
  358. tcpufieldvarsym
  359. ****************************************************************************}
  360. procedure tcpufieldvarsym.set_externalname(const s: string);
  361. begin
  362. { make sure it is recalculated }
  363. cachedmangledname:='';
  364. if is_java_class_or_interface(tdef(owner.defowner)) then
  365. begin
  366. externalname:=stringdup(s);
  367. include(varoptions,vo_has_mangledname);
  368. end
  369. else
  370. internalerror(2011031201);
  371. end;
  372. function tcpufieldvarsym.mangledname: TSymStr;
  373. begin
  374. if is_java_class_or_interface(tdef(owner.defowner)) or
  375. (tdef(owner.defowner).typ=recorddef) then
  376. begin
  377. if cachedmangledname<>'' then
  378. result:=cachedmangledname
  379. else
  380. begin
  381. result:=jvmmangledbasename(self,false);
  382. jvmaddtypeownerprefix(owner,result);
  383. cachedmangledname:=result;
  384. end;
  385. end
  386. else
  387. result:=inherited;
  388. end;
  389. begin
  390. { used tdef classes }
  391. cfiledef:=tcpufiledef;
  392. cvariantdef:=tcpuvariantdef;
  393. cformaldef:=tcpuformaldef;
  394. cforwarddef:=tcpuforwarddef;
  395. cundefineddef:=tcpuundefineddef;
  396. cerrordef:=tcpuerrordef;
  397. cpointerdef:=tcpupointerdef;
  398. crecorddef:=tcpurecorddef;
  399. cimplementedinterface:=tcpuimplementedinterface;
  400. cobjectdef:=tcpuobjectdef;
  401. cclassrefdef:=tcpuclassrefdef;
  402. carraydef:=tcpuarraydef;
  403. corddef:=tcpuorddef;
  404. cfloatdef:=tcpufloatdef;
  405. cprocvardef:=tcpuprocvardef;
  406. cprocdef:=tcpuprocdef;
  407. cstringdef:=tcpustringdef;
  408. cenumdef:=tcpuenumdef;
  409. csetdef:=tcpusetdef;
  410. { used tsym classes }
  411. clabelsym:=tcpulabelsym;
  412. cunitsym:=tcpuunitsym;
  413. cnamespacesym:=tcpunamespacesym;
  414. cprocsym:=tcpuprocsym;
  415. ctypesym:=tcpuypesym;
  416. cfieldvarsym:=tcpufieldvarsym;
  417. clocalvarsym:=tcpulocalvarsym;
  418. cparavarsym:=tcpuparavarsym;
  419. cstaticvarsym:=tcpustaticvarsym;
  420. cabsolutevarsym:=tcpuabsolutevarsym;
  421. cpropertysym:=tcpupropertysym;
  422. cconstsym:=tcpuconstsym;
  423. cenumsym:=tcpuenumsym;
  424. csyssym:=tcpusyssym;
  425. end.