ncgrtti.pas 54 KB

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