ncgrtti.pas 48 KB

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