ncgrtti.pas 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  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. const
  572. ProcCallOptionToCallConv: array[tproccalloption] of byte = (
  573. { pocall_none } 0,
  574. { pocall_cdecl } 1,
  575. { pocall_cppdecl } 5,
  576. { pocall_far16 } 6,
  577. { pocall_oldfpccall } 7,
  578. { pocall_internproc } 8,
  579. { pocall_syscall } 9,
  580. { pocall_pascal } 2,
  581. { pocall_register } 0,
  582. { pocall_safecall } 4,
  583. { pocall_stdcall } 3,
  584. { pocall_softfloat } 10,
  585. { pocall_mwpascal } 11
  586. );
  587. procedure write_para(parasym:tparavarsym);
  588. var
  589. paraspec : byte;
  590. begin
  591. { only store user visible parameters }
  592. if not(vo_is_hidden_para in parasym.varoptions) then
  593. begin
  594. case parasym.varspez of
  595. vs_value: paraspec := 0;
  596. vs_const: paraspec := pfConst;
  597. vs_var : paraspec := pfVar;
  598. vs_out : paraspec := pfOut;
  599. end;
  600. { Kylix also seems to always add both pfArray and pfReference
  601. in this case
  602. }
  603. if is_open_array(parasym.vardef) then
  604. paraspec:=paraspec or pfArray or pfReference;
  605. { and these for classes and interfaces (maybe because they
  606. are themselves addresses?)
  607. }
  608. if is_class_or_interface(parasym.vardef) then
  609. paraspec:=paraspec or pfAddress;
  610. { set bits run from the highest to the lowest bit on
  611. big endian systems
  612. }
  613. if (target_info.endian = endian_big) then
  614. paraspec:=reverse_byte(paraspec);
  615. { write flags for current parameter }
  616. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(paraspec));
  617. { write name of current parameter }
  618. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(parasym.realname)));
  619. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(parasym.realname));
  620. { write name of type of current parameter }
  621. write_rtti_name(parasym.vardef);
  622. end;
  623. end;
  624. var
  625. methodkind : byte;
  626. i : integer;
  627. begin
  628. if po_methodpointer in def.procoptions then
  629. begin
  630. { write method id and name }
  631. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkmethod));
  632. write_rtti_name(def);
  633. if (tf_requires_proper_alignment in target_info.flags) then
  634. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  635. { write kind of method (can only be function or procedure)}
  636. if def.returndef = voidtype then
  637. methodkind := mkProcedure
  638. else
  639. methodkind := mkFunction;
  640. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(methodkind));
  641. { write parameter info. The parameters must be written in reverse order
  642. if this method uses right to left parameter pushing! }
  643. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.maxparacount));
  644. for i:=0 to def.paras.count-1 do
  645. write_para(tparavarsym(def.paras[i]));
  646. if methodkind=mkFunction then
  647. begin
  648. { write name of result type }
  649. write_rtti_name(def.returndef);
  650. { write result typeinfo }
  651. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.returndef,fullrtti)))
  652. end;
  653. { write calling convention }
  654. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(ProcCallOptionToCallConv[def.proccalloption]));
  655. { write params typeinfo }
  656. for i:=0 to def.paras.count-1 do
  657. if not(vo_is_hidden_para in tparavarsym(def.paras[i]).varoptions) then
  658. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(tparavarsym(def.paras[i]).vardef,fullrtti)));
  659. end
  660. else
  661. begin
  662. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkprocvar));
  663. write_rtti_name(def);
  664. end;
  665. end;
  666. procedure objectdef_rtti(def:tobjectdef);
  667. procedure objectdef_rtti_class_init(def:tobjectdef);
  668. begin
  669. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  670. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(fields_count(def.symtable,rt)));
  671. fields_write_rtti_data(def.symtable,rt);
  672. end;
  673. procedure objectdef_rtti_interface_init(def:tobjectdef);
  674. begin
  675. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  676. end;
  677. procedure objectdef_rtti_class_full(def:tobjectdef);
  678. var
  679. propnamelist : TFPHashObjectList;
  680. begin
  681. { Collect unique property names with nameindex }
  682. propnamelist:=TFPHashObjectList.Create;
  683. collect_propnamelist(propnamelist,def);
  684. if (oo_has_vmt in def.objectoptions) then
  685. current_asmdata.asmlists[al_rtti].concat(Tai_const.Createname(def.vmt_mangledname,0))
  686. else
  687. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  688. { write parent typeinfo }
  689. if assigned(def.childof) then
  690. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.childof,fullrtti)))
  691. else
  692. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  693. { total number of unique properties }
  694. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(propnamelist.count));
  695. { write unit name }
  696. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(current_module.realmodulename^)));
  697. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(current_module.realmodulename^));
  698. if (tf_requires_proper_alignment in target_info.flags) then
  699. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  700. { write published properties for this object }
  701. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(published_properties_count(def.symtable)));
  702. if (tf_requires_proper_alignment in target_info.flags) then
  703. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  704. published_properties_write_rtti_data(propnamelist,def.symtable);
  705. propnamelist.free;
  706. end;
  707. procedure objectdef_rtti_interface_full(def:tobjectdef);
  708. var
  709. i : longint;
  710. propnamelist : TFPHashObjectList;
  711. { if changed to a set, make sure it's still a byte large, and
  712. swap appropriately when cross-compiling
  713. }
  714. IntfFlags: byte;
  715. begin
  716. { Collect unique property names with nameindex }
  717. propnamelist:=TFPHashObjectList.Create;
  718. collect_propnamelist(propnamelist,def);
  719. { write parent typeinfo }
  720. if assigned(def.childof) then
  721. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def.childof,fullrtti)))
  722. else
  723. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  724. { interface: write flags, iid and iidstr }
  725. IntfFlags:=0;
  726. if assigned(def.iidguid) then
  727. IntfFlags:=IntfFlags or (1 shl ord(ifHasGuid));
  728. if assigned(def.iidstr) then
  729. IntfFlags:=IntfFlags or (1 shl ord(ifHasStrGUID));
  730. if (def.objecttype=odt_dispinterface) then
  731. IntfFlags:=IntfFlags or (1 shl ord(ifDispInterface));
  732. if (target_info.endian=endian_big) then
  733. IntfFlags:=reverse_byte(IntfFlags);
  734. {
  735. ifDispatch, }
  736. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(IntfFlags));
  737. if (tf_requires_proper_alignment in target_info.flags) then
  738. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  739. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(def.iidguid^.D1)));
  740. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(def.iidguid^.D2));
  741. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(def.iidguid^.D3));
  742. for i:=Low(def.iidguid^.D4) to High(def.iidguid^.D4) do
  743. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.iidguid^.D4[i]));
  744. { write unit name }
  745. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(current_module.realmodulename^)));
  746. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(current_module.realmodulename^));
  747. if (tf_requires_proper_alignment in target_info.flags) then
  748. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  749. { write iidstr }
  750. if assigned(def.iidstr) then
  751. begin
  752. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(def.iidstr^)));
  753. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(def.iidstr^));
  754. end
  755. else
  756. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(0));
  757. if (tf_requires_proper_alignment in target_info.flags) then
  758. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  759. { write published properties for this object }
  760. published_properties_write_rtti_data(propnamelist,def.symtable);
  761. propnamelist.free;
  762. end;
  763. begin
  764. case def.objecttype of
  765. odt_class:
  766. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkclass));
  767. odt_object:
  768. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkobject));
  769. odt_dispinterface,
  770. odt_interfacecom:
  771. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkinterface));
  772. odt_interfacecorba:
  773. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkinterfaceCorba));
  774. else
  775. internalerror(200611034);
  776. end;
  777. { generate the name }
  778. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(def.objrealname^)));
  779. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(def.objrealname^));
  780. if (tf_requires_proper_alignment in target_info.flags) then
  781. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  782. case rt of
  783. initrtti :
  784. begin
  785. if def.objecttype in [odt_class,odt_object] then
  786. objectdef_rtti_class_init(def)
  787. else
  788. objectdef_rtti_interface_init(def);
  789. end;
  790. fullrtti :
  791. begin
  792. if def.objecttype in [odt_class,odt_object] then
  793. objectdef_rtti_class_full(def)
  794. else
  795. objectdef_rtti_interface_full(def);
  796. end;
  797. end;
  798. end;
  799. begin
  800. case def.typ of
  801. variantdef :
  802. variantdef_rtti(tvariantdef(def));
  803. stringdef :
  804. stringdef_rtti(tstringdef(def));
  805. enumdef :
  806. enumdef_rtti(tenumdef(def));
  807. orddef :
  808. orddef_rtti(torddef(def));
  809. floatdef :
  810. floatdef_rtti(tfloatdef(def));
  811. setdef :
  812. setdef_rtti(tsetdef(def));
  813. procvardef :
  814. procvardef_rtti(tprocvardef(def));
  815. arraydef :
  816. begin
  817. if ado_IsBitPacked in tarraydef(def).arrayoptions then
  818. unknown_rtti(tstoreddef(def))
  819. else
  820. arraydef_rtti(tarraydef(def));
  821. end;
  822. recorddef :
  823. begin
  824. if trecorddef(def).is_packed then
  825. unknown_rtti(tstoreddef(def))
  826. else
  827. recorddef_rtti(trecorddef(def));
  828. end;
  829. objectdef :
  830. objectdef_rtti(tobjectdef(def));
  831. else
  832. unknown_rtti(tstoreddef(def));
  833. end;
  834. end;
  835. procedure TRTTIWriter.write_rtti_extrasyms(def:Tdef;rt:Trttitype;mainrtti:Tasmsymbol);
  836. procedure enumdef_rtti_ord2stringindex(def:Tenumdef);
  837. var rttilab:Tasmsymbol;
  838. t:Tenumsym;
  839. syms:^Tenumsym;
  840. offsets:^longint;
  841. sym_count,sym_alloc:longint;
  842. h,i,p,o,st:longint;
  843. mode:(lookup,search); {Modify with care, ordinal value of enum is written.}
  844. r:single; {Must be real type because of integer overflow risk.}
  845. begin
  846. {Random access needed, put in array.}
  847. getmem(syms,64*sizeof(Tenumsym));
  848. getmem(offsets,64*sizeof(longint));
  849. sym_count:=0;
  850. sym_alloc:=64;
  851. st:=0;
  852. t:=Tenumsym(def.firstenum);
  853. while assigned(t) do
  854. begin
  855. if sym_count>=sym_alloc then
  856. begin
  857. reallocmem(syms,2*sym_alloc*sizeof(Tenumsym));
  858. reallocmem(offsets,2*sym_alloc*sizeof(longint));
  859. sym_alloc:=sym_alloc*2;
  860. end;
  861. syms[sym_count]:=t;
  862. offsets[sym_count]:=st;
  863. inc(sym_count);
  864. st:=st+length(t.realname)+1;
  865. t:=t.nextenum;
  866. end;
  867. {Sort the syms by enum value}
  868. if sym_count>=2 then
  869. begin
  870. p:=1;
  871. while 2*p<sym_count do
  872. p:=2*p;
  873. while p<>0 do
  874. begin
  875. for h:=p to sym_count-1 do
  876. begin
  877. i:=h;
  878. t:=syms[i];
  879. o:=offsets[i];
  880. repeat
  881. if syms[i-p].value<=t.value then
  882. break;
  883. syms[i]:=syms[i-p];
  884. offsets[i]:=offsets[i-p];
  885. dec(i,p);
  886. until i<p;
  887. syms[i]:=t;
  888. offsets[i]:=o;
  889. end;
  890. p:=p shr 1;
  891. end;
  892. end;
  893. {Decide wether a lookup array is size efficient.}
  894. mode:=lookup;
  895. if sym_count>0 then
  896. begin
  897. i:=1;
  898. r:=0;
  899. h:=syms[0].value; {Next expected enum value is min.}
  900. while i<sym_count do
  901. begin
  902. {Calculate size of hole between values. Avoid integer overflows.}
  903. r:=r+(single(syms[i].value)-single(h))-1;
  904. h:=syms[i].value;
  905. inc(i);
  906. end;
  907. if r>sym_count then
  908. mode:=search; {Don't waste more than 50% space.}
  909. end;
  910. {Calculate start of string table.}
  911. st:=1;
  912. if assigned(def.typesym) then
  913. inc(st,length(def.typesym.realname)+1)
  914. else
  915. inc(st);
  916. if (tf_requires_proper_alignment in target_info.flags) then
  917. st:=align(st,sizeof(Tconstptruint));
  918. inc(st);
  919. if (tf_requires_proper_alignment in target_info.flags) then
  920. st:=align(st,sizeof(Tconstptruint));
  921. inc(st,8+sizeof(pint));
  922. { write rtti data }
  923. with current_asmdata do
  924. begin
  925. rttilab:=defineasmsymbol(Tstoreddef(def).rtti_mangledname(rt)+'_o2s',AB_GLOBAL,AT_DATA);
  926. maybe_new_object_file(asmlists[al_rtti]);
  927. new_section(asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(pint)));
  928. asmlists[al_rtti].concat(Tai_symbol.create_global(rttilab,0));
  929. asmlists[al_rtti].concat(Tai_const.create_32bit(longint(mode)));
  930. if mode=lookup then
  931. begin
  932. if (tf_requires_proper_alignment in target_info.flags) then
  933. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  934. o:=syms[0].value; {Start with min value.}
  935. for i:=0 to sym_count-1 do
  936. begin
  937. while o<syms[i].value do
  938. begin
  939. asmlists[al_rtti].concat(Tai_const.create_pint(0));
  940. inc(o);
  941. end;
  942. inc(o);
  943. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  944. end;
  945. end
  946. else
  947. begin
  948. asmlists[al_rtti].concat(Tai_const.create_32bit(sym_count));
  949. for i:=0 to sym_count-1 do
  950. begin
  951. if (tf_requires_proper_alignment in target_info.flags) then
  952. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(4));
  953. asmlists[al_rtti].concat(Tai_const.create_32bit(syms[i].value));
  954. if (tf_requires_proper_alignment in target_info.flags) then
  955. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  956. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  957. end;
  958. end;
  959. asmlists[al_rtti].concat(Tai_symbol_end.create(rttilab));
  960. end;
  961. freemem(syms);
  962. freemem(offsets);
  963. end;
  964. procedure enumdef_rtti_string2ordindex(def:Tenumdef);
  965. var rttilab:Tasmsymbol;
  966. t:Tenumsym;
  967. syms:^Tenumsym;
  968. offsets:^longint;
  969. sym_count,sym_alloc:longint;
  970. h,i,p,o,st:longint;
  971. begin
  972. {Random access needed, put in array.}
  973. getmem(syms,64*sizeof(Tenumsym));
  974. getmem(offsets,64*sizeof(longint));
  975. sym_count:=0;
  976. sym_alloc:=64;
  977. st:=0;
  978. t:=Tenumsym(def.firstenum);
  979. while assigned(t) do
  980. begin
  981. if sym_count>=sym_alloc then
  982. begin
  983. reallocmem(syms,2*sym_alloc*sizeof(Tenumsym));
  984. reallocmem(offsets,2*sym_alloc*sizeof(longint));
  985. sym_alloc:=sym_alloc*2;
  986. end;
  987. syms[sym_count]:=t;
  988. offsets[sym_count]:=st;
  989. inc(sym_count);
  990. st:=st+length(t.realname)+1;
  991. t:=t.nextenum;
  992. end;
  993. {Sort the syms by enum name}
  994. if sym_count>=2 then
  995. begin
  996. p:=1;
  997. while 2*p<sym_count do
  998. p:=2*p;
  999. while p<>0 do
  1000. begin
  1001. for h:=p to sym_count-1 do
  1002. begin
  1003. i:=h;
  1004. t:=syms[i];
  1005. o:=offsets[i];
  1006. repeat
  1007. if syms[i-p].name<=t.name then
  1008. break;
  1009. syms[i]:=syms[i-p];
  1010. offsets[i]:=offsets[i-p];
  1011. dec(i,p);
  1012. until i<p;
  1013. syms[i]:=t;
  1014. offsets[i]:=o;
  1015. end;
  1016. p:=p shr 1;
  1017. end;
  1018. end;
  1019. {Calculate start of string table.}
  1020. st:=1;
  1021. if assigned(def.typesym) then
  1022. inc(st,length(def.typesym.realname)+1)
  1023. else
  1024. inc(st);
  1025. if (tf_requires_proper_alignment in target_info.flags) then
  1026. st:=align(st,sizeof(Tconstptruint));
  1027. inc(st);
  1028. if (tf_requires_proper_alignment in target_info.flags) then
  1029. st:=align(st,sizeof(Tconstptruint));
  1030. inc(st,8+sizeof(pint));
  1031. { write rtti data }
  1032. with current_asmdata do
  1033. begin
  1034. rttilab:=defineasmsymbol(Tstoreddef(def).rtti_mangledname(rt)+'_s2o',AB_GLOBAL,AT_DATA);
  1035. maybe_new_object_file(asmlists[al_rtti]);
  1036. new_section(asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(pint)));
  1037. asmlists[al_rtti].concat(Tai_symbol.create_global(rttilab,0));
  1038. asmlists[al_rtti].concat(Tai_const.create_32bit(sym_count));
  1039. for i:=0 to sym_count-1 do
  1040. begin
  1041. if (tf_requires_proper_alignment in target_info.flags) then
  1042. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(4));
  1043. asmlists[al_rtti].concat(Tai_const.create_32bit(syms[i].value));
  1044. if (tf_requires_proper_alignment in target_info.flags) then
  1045. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(sizeof(TConstPtrUInt)));
  1046. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  1047. end;
  1048. asmlists[al_rtti].concat(Tai_symbol_end.create(rttilab));
  1049. end;
  1050. freemem(syms);
  1051. freemem(offsets);
  1052. end;
  1053. begin
  1054. case def.typ of
  1055. enumdef:
  1056. if rt=fullrtti then
  1057. begin
  1058. enumdef_rtti_ord2stringindex(Tenumdef(def));
  1059. enumdef_rtti_string2ordindex(Tenumdef(def));
  1060. end;
  1061. end;
  1062. end;
  1063. procedure TRTTIWriter.write_child_rtti_data(def:tdef;rt:trttitype);
  1064. begin
  1065. case def.typ of
  1066. enumdef :
  1067. if assigned(tenumdef(def).basedef) then
  1068. write_rtti(tenumdef(def).basedef,rt);
  1069. setdef :
  1070. write_rtti(tsetdef(def).elementdef,rt);
  1071. arraydef :
  1072. write_rtti(tarraydef(def).elementdef,rt);
  1073. recorddef :
  1074. fields_write_rtti(trecorddef(def).symtable,rt);
  1075. objectdef :
  1076. begin
  1077. if assigned(tobjectdef(def).childof) then
  1078. write_rtti(tobjectdef(def).childof,rt);
  1079. if rt=initrtti then
  1080. fields_write_rtti(tobjectdef(def).symtable,rt)
  1081. else
  1082. published_write_rtti(tobjectdef(def).symtable,rt);
  1083. end;
  1084. end;
  1085. end;
  1086. function TRTTIWriter.ref_rtti(def:tdef;rt:trttitype):tasmsymbol;
  1087. begin
  1088. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt));
  1089. end;
  1090. procedure TRTTIWriter.write_rtti(def:tdef;rt:trttitype);
  1091. var
  1092. rttilab : tasmsymbol;
  1093. begin
  1094. { only write rtti of definitions from the current module }
  1095. if not findunitsymtable(def.owner).iscurrentunit then
  1096. exit;
  1097. { prevent recursion }
  1098. if rttidefstate[rt] in def.defstates then
  1099. exit;
  1100. include(def.defstates,rttidefstate[rt]);
  1101. { write first all dependencies }
  1102. write_child_rtti_data(def,rt);
  1103. { write rtti data }
  1104. rttilab:=current_asmdata.DefineAsmSymbol(tstoreddef(def).rtti_mangledname(rt),AB_GLOBAL,AT_DATA);
  1105. maybe_new_object_file(current_asmdata.asmlists[al_rtti]);
  1106. new_section(current_asmdata.asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(pint)));
  1107. current_asmdata.asmlists[al_rtti].concat(Tai_symbol.Create_global(rttilab,0));
  1108. write_rtti_data(def,rt);
  1109. current_asmdata.asmlists[al_rtti].concat(Tai_symbol_end.Create(rttilab));
  1110. write_rtti_extrasyms(def,rt,rttilab);
  1111. end;
  1112. function TRTTIWriter.get_rtti_label(def:tdef;rt:trttitype):tasmsymbol;
  1113. begin
  1114. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt));
  1115. end;
  1116. function TRTTIWriter.get_rtti_label_ord2str(def:tdef;rt:trttitype):tasmsymbol;
  1117. begin
  1118. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt)+'_o2s');
  1119. end;
  1120. function TRTTIWriter.get_rtti_label_str2ord(def:tdef;rt:trttitype):tasmsymbol;
  1121. begin
  1122. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt)+'_s2o');
  1123. end;
  1124. end.