ncgrtti.pas 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Routines for the code generation of RTTI data structures
  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 ncgrtti;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,constexp,
  22. aasmbase,
  23. symbase,symconst,symtype,symdef;
  24. type
  25. { TRTTIWriter }
  26. TRTTIWriter=class
  27. private
  28. function fields_count(st:tsymtable;rt:trttitype):longint;
  29. procedure fields_write_rtti(st:tsymtable;rt:trttitype);
  30. procedure fields_write_rtti_data(st:tsymtable;rt:trttitype);
  31. procedure write_rtti_extrasyms(def:Tdef;rt:Trttitype;mainrtti:Tasmsymbol);
  32. procedure published_write_rtti(st:tsymtable;rt:trttitype);
  33. function published_properties_count(st:tsymtable):longint;
  34. procedure published_properties_write_rtti_data(propnamelist:TFPHashObjectList;st:tsymtable);
  35. procedure collect_propnamelist(propnamelist:TFPHashObjectList;objdef:tobjectdef);
  36. procedure write_rtti_name(def:tdef);
  37. procedure write_rtti_data(def:tdef;rt:trttitype);
  38. procedure write_child_rtti_data(def:tdef;rt:trttitype);
  39. function ref_rtti(def:tdef;rt:trttitype):tasmsymbol;
  40. public
  41. procedure write_rtti(def:tdef;rt:trttitype);
  42. function get_rtti_label(def:tdef;rt:trttitype):tasmsymbol;
  43. function get_rtti_label_ord2str(def:tdef;rt:trttitype):tasmsymbol;
  44. function get_rtti_label_str2ord(def:tdef;rt:trttitype):tasmsymbol;
  45. end;
  46. var
  47. RTTIWriter : TRTTIWriter;
  48. implementation
  49. uses
  50. cutils,
  51. globals,globtype,verbose,systems,
  52. fmodule,
  53. symsym,
  54. aasmtai,aasmdata,
  55. defutil,
  56. wpobase
  57. ;
  58. const
  59. rttidefstate : array[trttitype] of tdefstate =
  60. (ds_rtti_table_written,ds_init_table_written,
  61. { Objective-C related, does not pass here }
  62. symconst.ds_none,symconst.ds_none,
  63. symconst.ds_none,symconst.ds_none);
  64. type
  65. TPropNameListItem = class(TFPHashObject)
  66. propindex : longint;
  67. propowner : TSymtable;
  68. end;
  69. {***************************************************************************
  70. TRTTIWriter
  71. ***************************************************************************}
  72. procedure TRTTIWriter.write_rtti_name(def:tdef);
  73. var
  74. hs : string;
  75. begin
  76. if is_open_array(def) then
  77. { open arrays never have a typesym with a name, since you cannot
  78. define an "open array type". Kylix prints the type of the
  79. elements in the array in this case (so together with the pfArray
  80. flag, you can reconstruct the full typename, I assume (JM))
  81. }
  82. def:=tarraydef(def).elementdef;
  83. { name }
  84. if assigned(def.typesym) then
  85. begin
  86. hs:=ttypesym(def.typesym).realname;
  87. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(chr(length(hs))+hs));
  88. end
  89. else
  90. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(#0));
  91. end;
  92. function TRTTIWriter.fields_count(st:tsymtable;rt:trttitype):longint;
  93. var
  94. i : longint;
  95. sym : tsym;
  96. begin
  97. result:=0;
  98. for i:=0 to st.SymList.Count-1 do
  99. begin
  100. sym:=tsym(st.SymList[i]);
  101. if (rt=fullrtti) or
  102. (
  103. (tsym(sym).typ=fieldvarsym) and
  104. tfieldvarsym(sym).vardef.needs_inittable
  105. ) then
  106. inc(result);
  107. end;
  108. end;
  109. procedure TRTTIWriter.fields_write_rtti_data(st:tsymtable;rt:trttitype);
  110. var
  111. i : longint;
  112. sym : tsym;
  113. begin
  114. for i:=0 to st.SymList.Count-1 do
  115. begin
  116. sym:=tsym(st.SymList[i]);
  117. if (rt=fullrtti) or
  118. (
  119. (tsym(sym).typ=fieldvarsym) and
  120. tfieldvarsym(sym).vardef.needs_inittable
  121. ) then
  122. begin
  123. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(tfieldvarsym(sym).vardef,rt)));
  124. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tfieldvarsym(sym).fieldoffset));
  125. end;
  126. end;
  127. end;
  128. procedure TRTTIWriter.fields_write_rtti(st:tsymtable;rt:trttitype);
  129. var
  130. i : longint;
  131. sym : tsym;
  132. begin
  133. for i:=0 to st.SymList.Count-1 do
  134. begin
  135. sym:=tsym(st.SymList[i]);
  136. if (rt=fullrtti) or
  137. (
  138. (tsym(sym).typ=fieldvarsym) and
  139. tfieldvarsym(sym).vardef.needs_inittable
  140. ) then
  141. write_rtti(tfieldvarsym(sym).vardef,rt);
  142. end;
  143. end;
  144. procedure TRTTIWriter.published_write_rtti(st:tsymtable;rt:trttitype);
  145. var
  146. i : longint;
  147. sym : tsym;
  148. begin
  149. for i:=0 to st.SymList.Count-1 do
  150. begin
  151. sym:=tsym(st.SymList[i]);
  152. if (sym.visibility=vis_published) then
  153. begin
  154. case tsym(sym).typ of
  155. propertysym:
  156. write_rtti(tpropertysym(sym).propdef,rt);
  157. fieldvarsym:
  158. write_rtti(tfieldvarsym(sym).vardef,rt);
  159. end;
  160. end;
  161. end;
  162. end;
  163. function TRTTIWriter.published_properties_count(st:tsymtable):longint;
  164. var
  165. i : longint;
  166. sym : tsym;
  167. begin
  168. result:=0;
  169. for i:=0 to st.SymList.Count-1 do
  170. begin
  171. sym:=tsym(st.SymList[i]);
  172. if (tsym(sym).typ=propertysym) and
  173. (sym.visibility=vis_published) then
  174. inc(result);
  175. end;
  176. end;
  177. procedure TRTTIWriter.collect_propnamelist(propnamelist:TFPHashObjectList;objdef:tobjectdef);
  178. var
  179. i : longint;
  180. sym : tsym;
  181. pn : tpropnamelistitem;
  182. begin
  183. if assigned(objdef.childof) then
  184. collect_propnamelist(propnamelist,objdef.childof);
  185. for i:=0 to objdef.symtable.SymList.Count-1 do
  186. begin
  187. sym:=tsym(objdef.symtable.SymList[i]);
  188. if (tsym(sym).typ=propertysym) and
  189. (sym.visibility=vis_published) then
  190. begin
  191. pn:=TPropNameListItem(propnamelist.Find(tsym(sym).name));
  192. if not assigned(pn) then
  193. begin
  194. pn:=tpropnamelistitem.create(propnamelist,tsym(sym).name);
  195. pn.propindex:=propnamelist.count-1;
  196. pn.propowner:=tsym(sym).owner;
  197. end;
  198. end;
  199. end;
  200. end;
  201. procedure TRTTIWriter.published_properties_write_rtti_data(propnamelist:TFPHashObjectList;st:tsymtable);
  202. var
  203. i : longint;
  204. sym : tsym;
  205. proctypesinfo : byte;
  206. propnameitem : tpropnamelistitem;
  207. procedure writeaccessproc(pap:tpropaccesslisttypes; shiftvalue : byte; unsetvalue: byte);
  208. var
  209. typvalue : byte;
  210. hp : ppropaccesslistitem;
  211. address,space : longint;
  212. def : tdef;
  213. hpropsym : tpropertysym;
  214. propaccesslist : tpropaccesslist;
  215. begin
  216. hpropsym:=tpropertysym(sym);
  217. repeat
  218. propaccesslist:=hpropsym.propaccesslist[pap];
  219. if not propaccesslist.empty then
  220. break;
  221. hpropsym:=hpropsym.overridenpropsym;
  222. until not assigned(hpropsym);
  223. if not(assigned(propaccesslist) and assigned(propaccesslist.firstsym)) then
  224. begin
  225. current_asmdata.asmlists[al_rtti].concat(Tai_const.create(aitconst_ptr,unsetvalue));
  226. typvalue:=3;
  227. end
  228. else if propaccesslist.firstsym^.sym.typ=fieldvarsym then
  229. begin
  230. address:=0;
  231. hp:=propaccesslist.firstsym;
  232. def:=nil;
  233. while assigned(hp) do
  234. begin
  235. case hp^.sltype of
  236. sl_load :
  237. begin
  238. def:=tfieldvarsym(hp^.sym).vardef;
  239. inc(address,tfieldvarsym(hp^.sym).fieldoffset);
  240. end;
  241. sl_subscript :
  242. begin
  243. if not(assigned(def) and
  244. ((def.typ=recorddef) or
  245. is_object(def))) then
  246. internalerror(200402171);
  247. inc(address,tfieldvarsym(hp^.sym).fieldoffset);
  248. def:=tfieldvarsym(hp^.sym).vardef;
  249. end;
  250. sl_vec :
  251. begin
  252. if not(assigned(def) and (def.typ=arraydef)) then
  253. internalerror(200402172);
  254. def:=tarraydef(def).elementdef;
  255. {Hp.value is a Tconstexprint, which can be rather large,
  256. sanity check for longint overflow.}
  257. space:=(high(address)-address) div def.size;
  258. if int64(space)<hp^.value then
  259. internalerror(200706101);
  260. inc(address,int64(def.size*hp^.value));
  261. end;
  262. end;
  263. hp:=hp^.next;
  264. end;
  265. current_asmdata.asmlists[al_rtti].concat(Tai_const.create(aitconst_ptr,address));
  266. typvalue:=0;
  267. end
  268. else
  269. begin
  270. { When there was an error then procdef is not assigned }
  271. if not assigned(propaccesslist.procdef) then
  272. exit;
  273. if not(po_virtualmethod in tprocdef(propaccesslist.procdef).procoptions) then
  274. begin
  275. current_asmdata.asmlists[al_rtti].concat(Tai_const.createname(tprocdef(propaccesslist.procdef).mangledname,0));
  276. typvalue:=1;
  277. end
  278. else
  279. begin
  280. { virtual method, write vmt offset }
  281. current_asmdata.asmlists[al_rtti].concat(Tai_const.create(aitconst_ptr,
  282. tprocdef(propaccesslist.procdef)._class.vmtmethodoffset(tprocdef(propaccesslist.procdef).extnumber)));
  283. { register for wpo }
  284. tprocdef(propaccesslist.procdef)._class.register_vmt_call(tprocdef(propaccesslist.procdef).extnumber);
  285. {$ifdef vtentry}
  286. { not sure if we can insert those vtentry symbols safely here }
  287. {$error register methods used for published properties}
  288. {$endif vtentry}
  289. typvalue:=2;
  290. end;
  291. end;
  292. proctypesinfo:=proctypesinfo or (typvalue shl shiftvalue);
  293. end;
  294. begin
  295. for i:=0 to st.SymList.Count-1 do
  296. begin
  297. sym:=tsym(st.SymList[i]);
  298. if (sym.typ=propertysym) and
  299. (sym.visibility=vis_published) then
  300. begin
  301. if ppo_indexed in tpropertysym(sym).propoptions then
  302. proctypesinfo:=$40
  303. else
  304. proctypesinfo:=0;
  305. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(tpropertysym(sym).propdef,fullrtti)));
  306. writeaccessproc(palt_read,0,0);
  307. writeaccessproc(palt_write,2,0);
  308. { is it stored ? }
  309. if not(ppo_stored in tpropertysym(sym).propoptions) then
  310. begin
  311. { no, so put a constant zero }
  312. current_asmdata.asmlists[al_rtti].concat(Tai_const.create(aitconst_ptr,0));
  313. proctypesinfo:=proctypesinfo or (3 shl 4);
  314. end
  315. else
  316. writeaccessproc(palt_stored,4,1); { maybe; if no procedure put a constant 1 (=true) }
  317. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tpropertysym(sym).index));
  318. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tpropertysym(sym).default));
  319. propnameitem:=TPropNameListItem(propnamelist.Find(tpropertysym(sym).name));
  320. if not assigned(propnameitem) then
  321. internalerror(200512201);
  322. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(propnameitem.propindex));
  323. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(proctypesinfo));
  324. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(tpropertysym(sym).realname)));
  325. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(tpropertysym(sym).realname));
  326. if (tf_requires_proper_alignment in target_info.flags) then
  327. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  328. end;
  329. end;
  330. end;
  331. procedure TRTTIWriter.write_rtti_data(def:tdef;rt:trttitype);
  332. procedure unknown_rtti(def:tstoreddef);
  333. begin
  334. current_asmdata.asmlists[al_rtti].concat(tai_const.create_8bit(tkUnknown));
  335. write_rtti_name(def);
  336. end;
  337. procedure variantdef_rtti(def:tvariantdef);
  338. begin
  339. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkVariant));
  340. end;
  341. procedure stringdef_rtti(def:tstringdef);
  342. begin
  343. case def.stringtype of
  344. st_ansistring:
  345. begin
  346. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkAString));
  347. write_rtti_name(def);
  348. end;
  349. st_widestring:
  350. begin
  351. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkWString));
  352. write_rtti_name(def);
  353. end;
  354. st_unicodestring:
  355. begin
  356. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkUString));
  357. write_rtti_name(def);
  358. end;
  359. st_longstring:
  360. begin
  361. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkLString));
  362. write_rtti_name(def);
  363. end;
  364. st_shortstring:
  365. begin
  366. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkSString));
  367. write_rtti_name(def);
  368. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.len));
  369. if (tf_requires_proper_alignment in target_info.flags) then
  370. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  371. end;
  372. end;
  373. end;
  374. procedure enumdef_rtti(def:tenumdef);
  375. var
  376. hp : tenumsym;
  377. begin
  378. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkEnumeration));
  379. write_rtti_name(def);
  380. if (tf_requires_proper_alignment in target_info.flags) then
  381. current_asmdata.asmlists[al_rtti].concat(Cai_align.Create(sizeof(TConstPtrUInt)));
  382. case longint(def.size) of
  383. 1 :
  384. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUByte));
  385. 2 :
  386. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUWord));
  387. 4 :
  388. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otULong));
  389. end;
  390. if (tf_requires_proper_alignment in target_info.flags) then
  391. current_asmdata.asmlists[al_rtti].concat(Cai_align.Create(longint(def.size)));
  392. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.min));
  393. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.max));
  394. if (tf_requires_proper_alignment in target_info.flags) then
  395. current_asmdata.asmlists[al_rtti].concat(Cai_align.Create(sizeof(TConstPtrUint)));
  396. if assigned(def.basedef) then
  397. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.basedef,rt)))
  398. else
  399. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  400. hp:=tenumsym(def.firstenum);
  401. while assigned(hp) do
  402. begin
  403. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(hp.realname)));
  404. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(hp.realname));
  405. hp:=hp.nextenum;
  406. end;
  407. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(0));
  408. end;
  409. procedure orddef_rtti(def:torddef);
  410. procedure dointeger;
  411. const
  412. trans : array[tordtype] of byte =
  413. (otUByte{otNone},
  414. otUByte,otUWord,otULong,otUByte{otNone},
  415. otSByte,otSWord,otSLong,otUByte{otNone},
  416. otUByte,otSByte,otSWord,otSLong,otSByte,
  417. otUByte,otUWord,otUByte);
  418. begin
  419. write_rtti_name(def);
  420. if (tf_requires_proper_alignment in target_info.flags) then
  421. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  422. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(byte(trans[def.ordtype])));
  423. if (tf_requires_proper_alignment in target_info.flags) then
  424. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  425. {Convert to longint to smuggle values in high(longint)+1..high(cardinal) into asmlist.}
  426. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(def.low.svalue)));
  427. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(def.high.svalue)));
  428. end;
  429. begin
  430. case def.ordtype of
  431. s64bit :
  432. begin
  433. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkInt64));
  434. write_rtti_name(def);
  435. if (tf_requires_proper_alignment in target_info.flags) then
  436. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  437. { low }
  438. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(def.low.svalue));
  439. { high }
  440. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(def.high.svalue));
  441. end;
  442. u64bit :
  443. begin
  444. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkQWord));
  445. write_rtti_name(def);
  446. if (tf_requires_proper_alignment in target_info.flags) then
  447. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  448. {use svalue because Create_64bit accepts int64, prevents range checks}
  449. { low }
  450. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(def.low.svalue));
  451. { high }
  452. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(def.high.svalue));
  453. end;
  454. pasbool:
  455. begin
  456. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkBool));
  457. dointeger;
  458. end;
  459. uchar:
  460. begin
  461. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkChar));
  462. dointeger;
  463. end;
  464. uwidechar:
  465. begin
  466. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkWChar));
  467. dointeger;
  468. end;
  469. scurrency:
  470. begin
  471. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkFloat));
  472. write_rtti_name(def);
  473. if (tf_requires_proper_alignment in target_info.flags) then
  474. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  475. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(ftCurr));
  476. end;
  477. else
  478. begin
  479. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkInteger));
  480. dointeger;
  481. end;
  482. end;
  483. end;
  484. procedure floatdef_rtti(def:tfloatdef);
  485. const
  486. {tfloattype = (s32real,s64real,s80real,s64bit,s128bit);}
  487. translate : array[tfloattype] of byte =
  488. (ftSingle,ftDouble,ftExtended,ftComp,ftCurr,ftFloat128);
  489. begin
  490. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkFloat));
  491. write_rtti_name(def);
  492. if (tf_requires_proper_alignment in target_info.flags) then
  493. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  494. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(translate[def.floattype]));
  495. end;
  496. procedure setdef_rtti(def:tsetdef);
  497. begin
  498. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkSet));
  499. write_rtti_name(def);
  500. if (tf_requires_proper_alignment in target_info.flags) then
  501. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  502. case def.size of
  503. 1:
  504. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUByte));
  505. 2:
  506. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUWord));
  507. 4:
  508. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otULong));
  509. else
  510. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUByte));
  511. end;
  512. if (tf_requires_proper_alignment in target_info.flags) then
  513. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  514. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.elementdef,rt)));
  515. end;
  516. procedure arraydef_rtti(def:tarraydef);
  517. begin
  518. if ado_IsDynamicArray in def.arrayoptions then
  519. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkdynarray))
  520. else
  521. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkarray));
  522. write_rtti_name(def);
  523. if (tf_requires_proper_alignment in target_info.flags) then
  524. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  525. { size of elements }
  526. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_pint(def.elesize));
  527. if not(ado_IsDynamicArray in def.arrayoptions) then
  528. begin
  529. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_pint(pint(def.elecount)));
  530. { element type }
  531. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.elementdef,rt)));
  532. end
  533. else
  534. { write a delphi almost compatible dyn. array entry:
  535. there are two types, eltype and eltype2, the latter is nil if the element type needs
  536. no finalization, the former is always valid, delphi has this swapped, but for
  537. compatibility with older fpc versions we do it different, to be delphi compatible,
  538. the names are swapped in typinfo.pp
  539. }
  540. begin
  541. { element type }
  542. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.elementdef,rt)));
  543. end;
  544. { variant type }
  545. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tstoreddef(def.elementdef).getvardef));
  546. if ado_IsDynamicArray in def.arrayoptions then
  547. begin
  548. { element type }
  549. if def.elementdef.needs_inittable then
  550. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.elementdef,rt)))
  551. else
  552. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_pint(0));
  553. { dummy DynUnitName }
  554. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(0));
  555. end;
  556. end;
  557. procedure recorddef_rtti(def:trecorddef);
  558. var
  559. fieldcnt : longint;
  560. begin
  561. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkrecord));
  562. write_rtti_name(def);
  563. if (tf_requires_proper_alignment in target_info.flags) then
  564. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  565. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  566. fieldcnt:=fields_count(def.symtable,rt);
  567. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(fieldcnt));
  568. fields_write_rtti_data(def.symtable,rt);
  569. end;
  570. procedure procvardef_rtti(def:tprocvardef);
  571. procedure write_para(parasym:tparavarsym);
  572. var
  573. paraspec : byte;
  574. begin
  575. { only store user visible parameters }
  576. if not(vo_is_hidden_para in parasym.varoptions) then
  577. begin
  578. case parasym.varspez of
  579. vs_value: paraspec := 0;
  580. vs_const: paraspec := pfConst;
  581. vs_var : paraspec := pfVar;
  582. vs_out : paraspec := pfOut;
  583. end;
  584. { Kylix also seems to always add both pfArray and pfReference
  585. in this case
  586. }
  587. if is_open_array(parasym.vardef) then
  588. paraspec:=paraspec or pfArray or pfReference;
  589. { and these for classes and interfaces (maybe because they
  590. are themselves addresses?)
  591. }
  592. if is_class_or_interface(parasym.vardef) then
  593. paraspec:=paraspec or pfAddress;
  594. { set bits run from the highest to the lowest bit on
  595. big endian systems
  596. }
  597. if (target_info.endian = endian_big) then
  598. paraspec:=reverse_byte(paraspec);
  599. { write flags for current parameter }
  600. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(paraspec));
  601. { write name of current parameter }
  602. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(parasym.realname)));
  603. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(parasym.realname));
  604. { write name of type of current parameter }
  605. write_rtti_name(parasym.vardef);
  606. end;
  607. end;
  608. var
  609. methodkind : byte;
  610. i : integer;
  611. begin
  612. if po_methodpointer in def.procoptions then
  613. begin
  614. { write method id and name }
  615. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkmethod));
  616. write_rtti_name(def);
  617. if (tf_requires_proper_alignment in target_info.flags) then
  618. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  619. { write kind of method (can only be function or procedure)}
  620. if def.returndef = voidtype then
  621. methodkind := mkProcedure
  622. else
  623. methodkind := mkFunction;
  624. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(methodkind));
  625. { write parameter info. The parameters must be written in reverse order
  626. if this method uses right to left parameter pushing! }
  627. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.maxparacount));
  628. for i:=0 to def.paras.count-1 do
  629. write_para(tparavarsym(def.paras[i]));
  630. { write name of result type }
  631. write_rtti_name(def.returndef);
  632. end
  633. else
  634. begin
  635. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkprocvar));
  636. write_rtti_name(def);
  637. end;
  638. end;
  639. procedure objectdef_rtti(def:tobjectdef);
  640. procedure objectdef_rtti_class_init(def:tobjectdef);
  641. begin
  642. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  643. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(fields_count(def.symtable,rt)));
  644. fields_write_rtti_data(def.symtable,rt);
  645. end;
  646. procedure objectdef_rtti_interface_init(def:tobjectdef);
  647. begin
  648. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  649. end;
  650. procedure objectdef_rtti_class_full(def:tobjectdef);
  651. var
  652. propnamelist : TFPHashObjectList;
  653. begin
  654. { Collect unique property names with nameindex }
  655. propnamelist:=TFPHashObjectList.Create;
  656. collect_propnamelist(propnamelist,def);
  657. if (oo_has_vmt in def.objectoptions) then
  658. current_asmdata.asmlists[al_rtti].concat(Tai_const.Createname(def.vmt_mangledname,0))
  659. else
  660. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  661. { write parent typeinfo }
  662. if assigned(def.childof) and
  663. (oo_can_have_published in def.childof.objectoptions) then
  664. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.childof,fullrtti)))
  665. else
  666. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  667. { total number of unique properties }
  668. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(propnamelist.count));
  669. { write unit name }
  670. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(current_module.realmodulename^)));
  671. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(current_module.realmodulename^));
  672. if (tf_requires_proper_alignment in target_info.flags) then
  673. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  674. { write published properties for this object }
  675. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(published_properties_count(def.symtable)));
  676. if (tf_requires_proper_alignment in target_info.flags) then
  677. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  678. published_properties_write_rtti_data(propnamelist,def.symtable);
  679. propnamelist.free;
  680. end;
  681. procedure objectdef_rtti_interface_full(def:tobjectdef);
  682. var
  683. i : longint;
  684. propnamelist : TFPHashObjectList;
  685. { if changed to a set, make sure it's still a byte large, and
  686. swap appropriately when cross-compiling
  687. }
  688. IntfFlags: byte;
  689. begin
  690. { Collect unique property names with nameindex }
  691. propnamelist:=TFPHashObjectList.Create;
  692. collect_propnamelist(propnamelist,def);
  693. { write parent typeinfo }
  694. if assigned(def.childof) then
  695. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.childof,fullrtti)))
  696. else
  697. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  698. { interface: write flags, iid and iidstr }
  699. IntfFlags:=0;
  700. if assigned(def.iidguid) then
  701. IntfFlags:=IntfFlags or (1 shl ord(ifHasGuid));
  702. if assigned(def.iidstr) then
  703. IntfFlags:=IntfFlags or (1 shl ord(ifHasStrGUID));
  704. if (def.objecttype=odt_dispinterface) then
  705. IntfFlags:=IntfFlags or (1 shl ord(ifDispInterface));
  706. if (target_info.endian=endian_big) then
  707. IntfFlags:=reverse_byte(IntfFlags);
  708. {
  709. ifDispatch, }
  710. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(IntfFlags));
  711. if (tf_requires_proper_alignment in target_info.flags) then
  712. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  713. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(def.iidguid^.D1)));
  714. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(def.iidguid^.D2));
  715. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(def.iidguid^.D3));
  716. for i:=Low(def.iidguid^.D4) to High(def.iidguid^.D4) do
  717. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.iidguid^.D4[i]));
  718. { write unit name }
  719. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(current_module.realmodulename^)));
  720. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(current_module.realmodulename^));
  721. if (tf_requires_proper_alignment in target_info.flags) then
  722. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  723. { write iidstr }
  724. if assigned(def.iidstr) then
  725. begin
  726. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(def.iidstr^)));
  727. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(def.iidstr^));
  728. end
  729. else
  730. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(0));
  731. if (tf_requires_proper_alignment in target_info.flags) then
  732. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  733. { write published properties for this object }
  734. published_properties_write_rtti_data(propnamelist,def.symtable);
  735. propnamelist.free;
  736. end;
  737. begin
  738. case def.objecttype of
  739. odt_class:
  740. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkclass));
  741. odt_object:
  742. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkobject));
  743. odt_dispinterface,
  744. odt_interfacecom:
  745. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkinterface));
  746. odt_interfacecorba:
  747. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkinterfaceCorba));
  748. else
  749. internalerror(200611034);
  750. end;
  751. { generate the name }
  752. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(def.objrealname^)));
  753. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(def.objrealname^));
  754. if (tf_requires_proper_alignment in target_info.flags) then
  755. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  756. case rt of
  757. initrtti :
  758. begin
  759. if def.objecttype in [odt_class,odt_object] then
  760. objectdef_rtti_class_init(def)
  761. else
  762. objectdef_rtti_interface_init(def);
  763. end;
  764. fullrtti :
  765. begin
  766. if def.objecttype in [odt_class,odt_object] then
  767. objectdef_rtti_class_full(def)
  768. else
  769. objectdef_rtti_interface_full(def);
  770. end;
  771. end;
  772. end;
  773. begin
  774. case def.typ of
  775. variantdef :
  776. variantdef_rtti(tvariantdef(def));
  777. stringdef :
  778. stringdef_rtti(tstringdef(def));
  779. enumdef :
  780. enumdef_rtti(tenumdef(def));
  781. orddef :
  782. orddef_rtti(torddef(def));
  783. floatdef :
  784. floatdef_rtti(tfloatdef(def));
  785. setdef :
  786. setdef_rtti(tsetdef(def));
  787. procvardef :
  788. procvardef_rtti(tprocvardef(def));
  789. arraydef :
  790. begin
  791. if ado_IsBitPacked in tarraydef(def).arrayoptions then
  792. unknown_rtti(tstoreddef(def))
  793. else
  794. arraydef_rtti(tarraydef(def));
  795. end;
  796. recorddef :
  797. begin
  798. if trecorddef(def).is_packed then
  799. unknown_rtti(tstoreddef(def))
  800. else
  801. recorddef_rtti(trecorddef(def));
  802. end;
  803. objectdef :
  804. objectdef_rtti(tobjectdef(def));
  805. else
  806. unknown_rtti(tstoreddef(def));
  807. end;
  808. end;
  809. procedure TRTTIWriter.write_rtti_extrasyms(def:Tdef;rt:Trttitype;mainrtti:Tasmsymbol);
  810. procedure enumdef_rtti_ord2stringindex(def:Tenumdef);
  811. var rttilab:Tasmsymbol;
  812. t:Tenumsym;
  813. syms:^Tenumsym;
  814. offsets:^longint;
  815. sym_count,sym_alloc:longint;
  816. h,i,p,o,st:longint;
  817. mode:(lookup,search); {Modify with care, ordinal value of enum is written.}
  818. r:single; {Must be real type because of integer overflow risk.}
  819. begin
  820. {Random access needed, put in array.}
  821. getmem(syms,64*sizeof(Tenumsym));
  822. getmem(offsets,64*sizeof(longint));
  823. sym_count:=0;
  824. sym_alloc:=64;
  825. st:=0;
  826. t:=Tenumsym(def.firstenum);
  827. while assigned(t) do
  828. begin
  829. if sym_count>=sym_alloc then
  830. begin
  831. reallocmem(syms,2*sym_alloc*sizeof(Tenumsym));
  832. reallocmem(offsets,2*sym_alloc*sizeof(longint));
  833. sym_alloc:=sym_alloc*2;
  834. end;
  835. syms[sym_count]:=t;
  836. offsets[sym_count]:=st;
  837. inc(sym_count);
  838. st:=st+length(t.realname)+1;
  839. t:=t.nextenum;
  840. end;
  841. {Sort the syms by enum value}
  842. if sym_count>=2 then
  843. begin
  844. p:=1;
  845. while 2*p<sym_count do
  846. p:=2*p;
  847. while p<>0 do
  848. begin
  849. for h:=p to sym_count-1 do
  850. begin
  851. i:=h;
  852. t:=syms[i];
  853. o:=offsets[i];
  854. repeat
  855. if syms[i-p].value<=t.value then
  856. break;
  857. syms[i]:=syms[i-p];
  858. offsets[i]:=offsets[i-p];
  859. dec(i,p);
  860. until i<p;
  861. syms[i]:=t;
  862. offsets[i]:=o;
  863. end;
  864. p:=p shr 1;
  865. end;
  866. end;
  867. {Decide wether a lookup array is size efficient.}
  868. mode:=lookup;
  869. if sym_count>0 then
  870. begin
  871. i:=1;
  872. r:=0;
  873. h:=syms[0].value; {Next expected enum value is min.}
  874. while i<sym_count do
  875. begin
  876. {Calculate size of hole between values. Avoid integer overflows.}
  877. r:=r+(single(syms[i].value)-single(h))-1;
  878. h:=syms[i].value;
  879. inc(i);
  880. end;
  881. if r>sym_count then
  882. mode:=search; {Don't waste more than 50% space.}
  883. end;
  884. {Calculate start of string table.}
  885. st:=1;
  886. if assigned(def.typesym) then
  887. inc(st,length(def.typesym.realname)+1)
  888. else
  889. inc(st);
  890. if (tf_requires_proper_alignment in target_info.flags) then
  891. st:=align(st,sizeof(Tconstptruint));
  892. inc(st);
  893. if (tf_requires_proper_alignment in target_info.flags) then
  894. st:=align(st,sizeof(Tconstptruint));
  895. inc(st,8+sizeof(pint));
  896. { write rtti data }
  897. with current_asmdata do
  898. begin
  899. rttilab:=defineasmsymbol(Tstoreddef(def).rtti_mangledname(rt)+'_o2s',AB_GLOBAL,AT_DATA);
  900. maybe_new_object_file(asmlists[al_rtti]);
  901. new_section(asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(pint)));
  902. asmlists[al_rtti].concat(Tai_symbol.create_global(rttilab,0));
  903. asmlists[al_rtti].concat(Tai_const.create_32bit(longint(mode)));
  904. if mode=lookup then
  905. begin
  906. if (tf_requires_proper_alignment in target_info.flags) then
  907. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  908. o:=syms[0].value; {Start with min value.}
  909. for i:=0 to sym_count-1 do
  910. begin
  911. while o<syms[i].value do
  912. begin
  913. asmlists[al_rtti].concat(Tai_const.create_pint(0));
  914. inc(o);
  915. end;
  916. inc(o);
  917. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  918. end;
  919. end
  920. else
  921. begin
  922. asmlists[al_rtti].concat(Tai_const.create_32bit(sym_count));
  923. for i:=0 to sym_count-1 do
  924. begin
  925. if (tf_requires_proper_alignment in target_info.flags) then
  926. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(4));
  927. asmlists[al_rtti].concat(Tai_const.create_32bit(syms[i].value));
  928. if (tf_requires_proper_alignment in target_info.flags) then
  929. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  930. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  931. end;
  932. end;
  933. asmlists[al_rtti].concat(Tai_symbol_end.create(rttilab));
  934. end;
  935. freemem(syms);
  936. freemem(offsets);
  937. end;
  938. procedure enumdef_rtti_string2ordindex(def:Tenumdef);
  939. var rttilab:Tasmsymbol;
  940. t:Tenumsym;
  941. syms:^Tenumsym;
  942. offsets:^longint;
  943. sym_count,sym_alloc:longint;
  944. h,i,p,o,st:longint;
  945. begin
  946. {Random access needed, put in array.}
  947. getmem(syms,64*sizeof(Tenumsym));
  948. getmem(offsets,64*sizeof(longint));
  949. sym_count:=0;
  950. sym_alloc:=64;
  951. st:=0;
  952. t:=Tenumsym(def.firstenum);
  953. while assigned(t) do
  954. begin
  955. if sym_count>=sym_alloc then
  956. begin
  957. reallocmem(syms,2*sym_alloc*sizeof(Tenumsym));
  958. reallocmem(offsets,2*sym_alloc*sizeof(longint));
  959. sym_alloc:=sym_alloc*2;
  960. end;
  961. syms[sym_count]:=t;
  962. offsets[sym_count]:=st;
  963. inc(sym_count);
  964. st:=st+length(t.realname)+1;
  965. t:=t.nextenum;
  966. end;
  967. {Sort the syms by enum name}
  968. if sym_count>=2 then
  969. begin
  970. p:=1;
  971. while 2*p<sym_count do
  972. p:=2*p;
  973. while p<>0 do
  974. begin
  975. for h:=p to sym_count-1 do
  976. begin
  977. i:=h;
  978. t:=syms[i];
  979. o:=offsets[i];
  980. repeat
  981. if syms[i-p].name<=t.name then
  982. break;
  983. syms[i]:=syms[i-p];
  984. offsets[i]:=offsets[i-p];
  985. dec(i,p);
  986. until i<p;
  987. syms[i]:=t;
  988. offsets[i]:=o;
  989. end;
  990. p:=p shr 1;
  991. end;
  992. end;
  993. {Calculate start of string table.}
  994. st:=1;
  995. if assigned(def.typesym) then
  996. inc(st,length(def.typesym.realname)+1)
  997. else
  998. inc(st);
  999. if (tf_requires_proper_alignment in target_info.flags) then
  1000. st:=align(st,sizeof(Tconstptruint));
  1001. inc(st);
  1002. if (tf_requires_proper_alignment in target_info.flags) then
  1003. st:=align(st,sizeof(Tconstptruint));
  1004. inc(st,8+sizeof(pint));
  1005. { write rtti data }
  1006. with current_asmdata do
  1007. begin
  1008. rttilab:=defineasmsymbol(Tstoreddef(def).rtti_mangledname(rt)+'_s2o',AB_GLOBAL,AT_DATA);
  1009. maybe_new_object_file(asmlists[al_rtti]);
  1010. new_section(asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(pint)));
  1011. asmlists[al_rtti].concat(Tai_symbol.create_global(rttilab,0));
  1012. asmlists[al_rtti].concat(Tai_const.create_32bit(sym_count));
  1013. for i:=0 to sym_count-1 do
  1014. begin
  1015. if (tf_requires_proper_alignment in target_info.flags) then
  1016. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(4));
  1017. asmlists[al_rtti].concat(Tai_const.create_32bit(syms[i].value));
  1018. if (tf_requires_proper_alignment in target_info.flags) then
  1019. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  1020. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  1021. end;
  1022. asmlists[al_rtti].concat(Tai_symbol_end.create(rttilab));
  1023. end;
  1024. freemem(syms);
  1025. freemem(offsets);
  1026. end;
  1027. begin
  1028. case def.typ of
  1029. enumdef:
  1030. if rt=fullrtti then
  1031. begin
  1032. enumdef_rtti_ord2stringindex(Tenumdef(def));
  1033. enumdef_rtti_string2ordindex(Tenumdef(def));
  1034. end;
  1035. end;
  1036. end;
  1037. procedure TRTTIWriter.write_child_rtti_data(def:tdef;rt:trttitype);
  1038. begin
  1039. case def.typ of
  1040. enumdef :
  1041. if assigned(tenumdef(def).basedef) then
  1042. write_rtti(tenumdef(def).basedef,rt);
  1043. setdef :
  1044. write_rtti(tsetdef(def).elementdef,rt);
  1045. arraydef :
  1046. write_rtti(tarraydef(def).elementdef,rt);
  1047. recorddef :
  1048. fields_write_rtti(trecorddef(def).symtable,rt);
  1049. objectdef :
  1050. begin
  1051. if assigned(tobjectdef(def).childof) then
  1052. write_rtti(tobjectdef(def).childof,rt);
  1053. if rt=initrtti then
  1054. fields_write_rtti(tobjectdef(def).symtable,rt)
  1055. else
  1056. published_write_rtti(tobjectdef(def).symtable,rt);
  1057. end;
  1058. end;
  1059. end;
  1060. function TRTTIWriter.ref_rtti(def:tdef;rt:trttitype):tasmsymbol;
  1061. begin
  1062. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt));
  1063. end;
  1064. procedure TRTTIWriter.write_rtti(def:tdef;rt:trttitype);
  1065. var
  1066. rttilab : tasmsymbol;
  1067. begin
  1068. { only write rtti of definitions from the current module }
  1069. if not findunitsymtable(def.owner).iscurrentunit then
  1070. exit;
  1071. { prevent recursion }
  1072. if rttidefstate[rt] in def.defstates then
  1073. exit;
  1074. include(def.defstates,rttidefstate[rt]);
  1075. { write first all dependencies }
  1076. write_child_rtti_data(def,rt);
  1077. { write rtti data }
  1078. rttilab:=current_asmdata.DefineAsmSymbol(tstoreddef(def).rtti_mangledname(rt),AB_GLOBAL,AT_DATA);
  1079. maybe_new_object_file(current_asmdata.asmlists[al_rtti]);
  1080. new_section(current_asmdata.asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(pint)));
  1081. current_asmdata.asmlists[al_rtti].concat(Tai_symbol.Create_global(rttilab,0));
  1082. write_rtti_data(def,rt);
  1083. current_asmdata.asmlists[al_rtti].concat(Tai_symbol_end.Create(rttilab));
  1084. write_rtti_extrasyms(def,rt,rttilab);
  1085. end;
  1086. function TRTTIWriter.get_rtti_label(def:tdef;rt:trttitype):tasmsymbol;
  1087. begin
  1088. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt));
  1089. end;
  1090. function TRTTIWriter.get_rtti_label_ord2str(def:tdef;rt:trttitype):tasmsymbol;
  1091. begin
  1092. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt)+'_o2s');
  1093. end;
  1094. function TRTTIWriter.get_rtti_label_str2ord(def:tdef;rt:trttitype):tasmsymbol;
  1095. begin
  1096. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt)+'_s2o');
  1097. end;
  1098. end.