ncgrtti.pas 48 KB

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