ncgrtti.pas 48 KB

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