ncgrtti.pas 55 KB

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