ncgrtti.pas 54 KB

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