symcpu.pas 13 KB

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