ncgrtti.pas 54 KB

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