ncgrtti.pas 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  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;indirect:boolean):tasmsymbol;
  47. function get_rtti_label_ord2str(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  48. function get_rtti_label_str2ord(def:tdef;rt:trttitype;indirect:boolean):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(aitconst_ptr,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(aitconst_ptr,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(aitconst_ptr,
  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(aitconst_ptr,0));
  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. { element type }
  596. if def.elementdef.needs_inittable then
  597. write_rtti_reference(def.elementdef,rt)
  598. else
  599. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_pint(0));
  600. { write unit name }
  601. write_string(current_module.realmodulename^);
  602. end;
  603. end;
  604. procedure classrefdef_rtti(def:tclassrefdef);
  605. begin
  606. write_header(def,tkClassRef);
  607. maybe_write_align;
  608. write_rtti_reference(def.pointeddef,rt);
  609. end;
  610. procedure pointerdef_rtti(def:tpointerdef);
  611. begin
  612. write_header(def,tkPointer);
  613. maybe_write_align;
  614. write_rtti_reference(def.pointeddef,rt);
  615. end;
  616. procedure recorddef_rtti(def:trecorddef);
  617. begin
  618. write_header(def,tkRecord);
  619. maybe_write_align;
  620. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  621. fields_write_rtti_data(def,rt);
  622. end;
  623. procedure procvardef_rtti(def:tprocvardef);
  624. const
  625. ProcCallOptionToCallConv: array[tproccalloption] of byte = (
  626. { pocall_none } 0,
  627. { pocall_cdecl } 1,
  628. { pocall_cppdecl } 5,
  629. { pocall_far16 } 6,
  630. { pocall_oldfpccall } 7,
  631. { pocall_internproc } 8,
  632. { pocall_syscall } 9,
  633. { pocall_pascal } 2,
  634. { pocall_register } 0,
  635. { pocall_safecall } 4,
  636. { pocall_stdcall } 3,
  637. { pocall_softfloat } 10,
  638. { pocall_mwpascal } 11,
  639. { pocall_interrupt } 12
  640. );
  641. procedure write_param_flag(parasym:tparavarsym);
  642. var
  643. paraspec : byte;
  644. begin
  645. case parasym.varspez of
  646. vs_value : paraspec := 0;
  647. vs_const : paraspec := pfConst;
  648. vs_var : paraspec := pfVar;
  649. vs_out : paraspec := pfOut;
  650. vs_constref: paraspec := pfConstRef;
  651. else
  652. internalerror(2013112904);
  653. end;
  654. { Kylix also seems to always add both pfArray and pfReference
  655. in this case
  656. }
  657. if is_open_array(parasym.vardef) then
  658. paraspec:=paraspec or pfArray or pfReference;
  659. { and these for classes and interfaces (maybe because they
  660. are themselves addresses?)
  661. }
  662. if is_class_or_interface(parasym.vardef) then
  663. paraspec:=paraspec or pfAddress;
  664. { set bits run from the highest to the lowest bit on
  665. big endian systems
  666. }
  667. if (target_info.endian = endian_big) then
  668. paraspec:=reverse_byte(paraspec);
  669. { write flags for current parameter }
  670. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(paraspec));
  671. end;
  672. procedure write_para(parasym:tparavarsym);
  673. begin
  674. { only store user visible parameters }
  675. if not(vo_is_hidden_para in parasym.varoptions) then
  676. begin
  677. { write flags for current parameter }
  678. write_param_flag(parasym);
  679. { write name of current parameter }
  680. write_string(parasym.realname);
  681. { write name of type of current parameter }
  682. write_rtti_name(parasym.vardef);
  683. end;
  684. end;
  685. procedure write_procedure_param(parasym:tparavarsym);
  686. begin
  687. { only store user visible parameters }
  688. if not(vo_is_hidden_para in parasym.varoptions) then
  689. begin
  690. { write flags for current parameter }
  691. write_param_flag(parasym);
  692. maybe_write_align;
  693. { write param type }
  694. write_rtti_reference(parasym.vardef,fullrtti);
  695. { write name of current parameter }
  696. write_string(parasym.realname);
  697. end;
  698. end;
  699. var
  700. methodkind : byte;
  701. i : integer;
  702. begin
  703. if po_methodpointer in def.procoptions then
  704. begin
  705. { write method id and name }
  706. write_header(def,tkMethod);
  707. maybe_write_align;
  708. { write kind of method }
  709. case def.proctypeoption of
  710. potype_constructor: methodkind:=mkConstructor;
  711. potype_destructor: methodkind:=mkDestructor;
  712. potype_class_constructor: methodkind:=mkClassConstructor;
  713. potype_class_destructor: methodkind:=mkClassDestructor;
  714. potype_operator: methodkind:=mkOperatorOverload;
  715. potype_procedure:
  716. if po_classmethod in def.procoptions then
  717. methodkind:=mkClassProcedure
  718. else
  719. methodkind:=mkProcedure;
  720. potype_function:
  721. if po_classmethod in def.procoptions then
  722. methodkind:=mkClassFunction
  723. else
  724. methodkind:=mkFunction;
  725. else
  726. begin
  727. if def.returndef = voidtype then
  728. methodkind:=mkProcedure
  729. else
  730. methodkind:=mkFunction;
  731. end;
  732. end;
  733. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(methodkind));
  734. { write parameter info. The parameters must be written in reverse order
  735. if this method uses right to left parameter pushing! }
  736. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.maxparacount));
  737. for i:=0 to def.paras.count-1 do
  738. write_para(tparavarsym(def.paras[i]));
  739. if (methodkind=mkFunction) or (methodkind=mkClassFunction) then
  740. begin
  741. { write name of result type }
  742. write_rtti_name(def.returndef);
  743. maybe_write_align;
  744. { write result typeinfo }
  745. write_rtti_reference(def.returndef,fullrtti);
  746. end;
  747. { write calling convention }
  748. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(ProcCallOptionToCallConv[def.proccalloption]));
  749. maybe_write_align;
  750. { write params typeinfo }
  751. for i:=0 to def.paras.count-1 do
  752. if not(vo_is_hidden_para in tparavarsym(def.paras[i]).varoptions) then
  753. write_rtti_reference(tparavarsym(def.paras[i]).vardef,fullrtti);
  754. end
  755. else
  756. begin
  757. write_header(def,tkProcvar);
  758. maybe_write_align;
  759. { flags }
  760. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(0));
  761. //maybe_write_align; // aligning between bytes is not necessary
  762. { write calling convention }
  763. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(ProcCallOptionToCallConv[def.proccalloption]));
  764. maybe_write_align;
  765. { write result typeinfo }
  766. write_rtti_reference(def.returndef,fullrtti);
  767. { write parameter count }
  768. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.maxparacount));
  769. for i:=0 to def.paras.count-1 do
  770. begin
  771. maybe_write_align;
  772. write_procedure_param(tparavarsym(def.paras[i]));
  773. end;
  774. end;
  775. end;
  776. procedure objectdef_rtti(def:tobjectdef);
  777. procedure objectdef_rtti_fields(def:tobjectdef);
  778. begin
  779. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  780. fields_write_rtti_data(def,rt);
  781. end;
  782. procedure objectdef_rtti_interface_init(def:tobjectdef);
  783. begin
  784. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(def.size));
  785. end;
  786. procedure objectdef_rtti_class_full(def:tobjectdef);
  787. var
  788. propnamelist : TFPHashObjectList;
  789. begin
  790. { Collect unique property names with nameindex }
  791. propnamelist:=TFPHashObjectList.Create;
  792. collect_propnamelist(propnamelist,def);
  793. if not is_objectpascal_helper(def) then
  794. if (oo_has_vmt in def.objectoptions) then
  795. current_asmdata.asmlists[al_rtti].concat(Tai_const.Createname(def.vmt_mangledname,AT_DATA,0))
  796. else
  797. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_nil_dataptr);
  798. { write parent typeinfo }
  799. write_rtti_reference(def.childof,fullrtti);
  800. { write typeinfo of extended type }
  801. if is_objectpascal_helper(def) then
  802. if assigned(def.extendeddef) then
  803. write_rtti_reference(def.extendeddef,fullrtti)
  804. else
  805. InternalError(2011033001);
  806. { total number of unique properties }
  807. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(propnamelist.count));
  808. { write unit name }
  809. write_string(current_module.realmodulename^);
  810. maybe_write_align;
  811. { write published properties for this object }
  812. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(published_properties_count(def.symtable)));
  813. maybe_write_align;
  814. published_properties_write_rtti_data(propnamelist,def.symtable);
  815. propnamelist.free;
  816. end;
  817. procedure objectdef_rtti_interface_full(def:tobjectdef);
  818. var
  819. i : longint;
  820. propnamelist : TFPHashObjectList;
  821. { if changed to a set, make sure it's still a byte large, and
  822. swap appropriately when cross-compiling
  823. }
  824. IntfFlags: byte;
  825. begin
  826. { Collect unique property names with nameindex }
  827. propnamelist:=TFPHashObjectList.Create;
  828. collect_propnamelist(propnamelist,def);
  829. { write parent typeinfo }
  830. write_rtti_reference(def.childof,fullrtti);
  831. { interface: write flags, iid and iidstr }
  832. IntfFlags:=0;
  833. if assigned(def.iidguid) then
  834. IntfFlags:=IntfFlags or (1 shl ord(ifHasGuid));
  835. if assigned(def.iidstr) then
  836. IntfFlags:=IntfFlags or (1 shl ord(ifHasStrGUID));
  837. if (def.objecttype=odt_dispinterface) then
  838. IntfFlags:=IntfFlags or (1 shl ord(ifDispInterface));
  839. if (target_info.endian=endian_big) then
  840. IntfFlags:=reverse_byte(IntfFlags);
  841. {
  842. ifDispatch, }
  843. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(IntfFlags));
  844. maybe_write_align;
  845. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(def.iidguid^.D1)));
  846. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(def.iidguid^.D2));
  847. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(def.iidguid^.D3));
  848. for i:=Low(def.iidguid^.D4) to High(def.iidguid^.D4) do
  849. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(def.iidguid^.D4[i]));
  850. { write unit name }
  851. write_string(current_module.realmodulename^);
  852. maybe_write_align;
  853. { write iidstr }
  854. if assigned(def.iidstr) then
  855. write_string(def.iidstr^)
  856. else
  857. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(0));
  858. maybe_write_align;
  859. { write published properties for this object }
  860. published_properties_write_rtti_data(propnamelist,def.symtable);
  861. propnamelist.free;
  862. end;
  863. begin
  864. case def.objecttype of
  865. odt_class:
  866. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkclass));
  867. odt_object:
  868. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkobject));
  869. odt_dispinterface,
  870. odt_interfacecom:
  871. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkinterface));
  872. odt_interfacecorba:
  873. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkinterfaceCorba));
  874. odt_helper:
  875. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkhelper));
  876. else
  877. internalerror(200611034);
  878. end;
  879. { generate the name }
  880. write_string(def.objrealname^);
  881. maybe_write_align;
  882. case rt of
  883. initrtti :
  884. begin
  885. if def.objecttype in [odt_class,odt_object,odt_helper] then
  886. objectdef_rtti_fields(def)
  887. else
  888. objectdef_rtti_interface_init(def);
  889. end;
  890. fullrtti :
  891. begin
  892. case def.objecttype of
  893. odt_helper,
  894. odt_class:
  895. objectdef_rtti_class_full(def);
  896. odt_object:
  897. objectdef_rtti_fields(def);
  898. else
  899. objectdef_rtti_interface_full(def);
  900. end;
  901. end;
  902. end;
  903. end;
  904. begin
  905. case def.typ of
  906. variantdef :
  907. variantdef_rtti(tvariantdef(def));
  908. stringdef :
  909. stringdef_rtti(tstringdef(def));
  910. enumdef :
  911. enumdef_rtti(tenumdef(def));
  912. orddef :
  913. orddef_rtti(torddef(def));
  914. floatdef :
  915. floatdef_rtti(tfloatdef(def));
  916. setdef :
  917. setdef_rtti(tsetdef(def));
  918. procvardef :
  919. procvardef_rtti(tprocvardef(def));
  920. arraydef :
  921. begin
  922. if ado_IsBitPacked in tarraydef(def).arrayoptions then
  923. unknown_rtti(tstoreddef(def))
  924. else
  925. arraydef_rtti(tarraydef(def));
  926. end;
  927. recorddef :
  928. begin
  929. if trecorddef(def).is_packed then
  930. unknown_rtti(tstoreddef(def))
  931. else
  932. recorddef_rtti(trecorddef(def));
  933. end;
  934. objectdef :
  935. objectdef_rtti(tobjectdef(def));
  936. classrefdef :
  937. classrefdef_rtti(tclassrefdef(def));
  938. pointerdef :
  939. pointerdef_rtti(tpointerdef(def));
  940. else
  941. unknown_rtti(tstoreddef(def));
  942. end;
  943. end;
  944. procedure TRTTIWriter.write_rtti_extrasyms(def:Tdef;rt:Trttitype;mainrtti:Tasmsymbol);
  945. type Penumsym = ^Tenumsym;
  946. function enumdef_rtti_calcstringtablestart(const def : Tenumdef) : integer;
  947. begin
  948. { the alignment calls must correspond to the ones used during generating the
  949. actual data structure created elsewhere in this file }
  950. result:=1;
  951. if assigned(def.typesym) then
  952. inc(result,length(def.typesym.realname)+1)
  953. else
  954. inc(result);
  955. if (tf_requires_proper_alignment in target_info.flags) then
  956. result:=align(result,sizeof(Tconstptruint));
  957. inc(result);
  958. if (tf_requires_proper_alignment in target_info.flags) then
  959. result:=align(result,sizeof(Tconstptruint));
  960. inc(result, sizeof(longint) * 2);
  961. if (tf_requires_proper_alignment in target_info.flags) then
  962. result:=align(result,sizeof(Tconstptruint));
  963. inc(result, sizeof(pint));
  964. end;
  965. { Writes a helper table for accelerated conversion of ordinal enum values to strings.
  966. If you change something in this method, make sure to adapt the corresponding code
  967. in sstrings.inc. }
  968. procedure enumdef_rtti_ord2stringindex(const sym_count:longint; const offsets:plongint; const syms:Penumsym; const st:longint);
  969. var rttilab:Tasmsymbol;
  970. h,i,o,prev_value:longint;
  971. rttilabind : tasmsymbol;
  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,false)+'_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. { write the indirect symbol }
  1044. rttilabind:=current_asmdata.DefineAsmSymbol(tstoreddef(def).rtti_mangledname(rt,false)+'_o2s'+indirect_suffix,AB_GLOBAL,AT_DATA);
  1045. current_asmdata.asmlists[al_rtti].concat(Tai_symbol.Create_Global(rttilabind,0));
  1046. current_asmdata.asmlists[al_rtti].concat(Tai_const.Createname(rttilab.name,AT_DATA,0));
  1047. current_asmdata.asmlists[al_rtti].concat(tai_symbol_end.Create(rttilabind));
  1048. end;
  1049. end;
  1050. { Writes a helper table for accelerated conversion of string to ordinal enum values.
  1051. If you change something in this method, make sure to adapt the corresponding code
  1052. in sstrings.inc. }
  1053. procedure enumdef_rtti_string2ordindex(const sym_count:longint; const offsets:plongint; const syms:Penumsym; const st:longint);
  1054. var rttilab:Tasmsymbol;
  1055. rttilabind : tasmsymbol;
  1056. i:longint;
  1057. begin
  1058. { write rtti data }
  1059. with current_asmdata do
  1060. begin
  1061. rttilab:=defineasmsymbol(Tstoreddef(def).rtti_mangledname(rt,false)+'_s2o',AB_GLOBAL,AT_DATA);
  1062. maybe_new_object_file(asmlists[al_rtti]);
  1063. new_section(asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(pint)));
  1064. asmlists[al_rtti].concat(Tai_symbol.create_global(rttilab,0));
  1065. asmlists[al_rtti].concat(Tai_const.create_32bit(sym_count));
  1066. { need to align the entry record according to the largest member }
  1067. maybe_write_align;
  1068. for i:=0 to sym_count-1 do
  1069. begin
  1070. if (tf_requires_proper_alignment in target_info.flags) then
  1071. current_asmdata.asmlists[al_rtti].concat(cai_align.Create(4)); // necessary?
  1072. asmlists[al_rtti].concat(Tai_const.create_32bit(syms[i].value));
  1073. maybe_write_align;
  1074. asmlists[al_rtti].concat(Tai_const.create_sym_offset(mainrtti,st+offsets[i]));
  1075. end;
  1076. asmlists[al_rtti].concat(Tai_symbol_end.create(rttilab));
  1077. { write the indirect symbol }
  1078. rttilabind:=current_asmdata.DefineAsmSymbol(tstoreddef(def).rtti_mangledname(rt,false)+'_s2o'+indirect_suffix,AB_GLOBAL,AT_DATA);
  1079. current_asmdata.asmlists[al_rtti].concat(Tai_symbol.Create_Global(rttilabind,0));
  1080. current_asmdata.asmlists[al_rtti].concat(Tai_const.Createname(rttilab.name,AT_DATA,0));
  1081. current_asmdata.asmlists[al_rtti].concat(tai_symbol_end.Create(rttilabind));
  1082. end;
  1083. end;
  1084. procedure enumdef_rtti_extrasyms(def:Tenumdef);
  1085. var
  1086. t:Tenumsym;
  1087. syms:Penumsym;
  1088. sym_count,sym_alloc:sizeuint;
  1089. offsets:^longint;
  1090. h,i,p,o,st:longint;
  1091. begin
  1092. {Random access needed, put in array.}
  1093. getmem(syms,64*sizeof(Tenumsym));
  1094. getmem(offsets,64*sizeof(longint));
  1095. sym_count:=0;
  1096. sym_alloc:=64;
  1097. st:=0;
  1098. for i := 0 to def.symtable.SymList.Count - 1 do
  1099. begin
  1100. t:=tenumsym(def.symtable.SymList[i]);
  1101. if t.value<def.minval then
  1102. continue
  1103. else
  1104. if t.value>def.maxval then
  1105. break;
  1106. if sym_count>=sym_alloc then
  1107. begin
  1108. reallocmem(syms,2*sym_alloc*sizeof(Tenumsym));
  1109. reallocmem(offsets,2*sym_alloc*sizeof(longint));
  1110. sym_alloc:=sym_alloc*2;
  1111. end;
  1112. syms[sym_count]:=t;
  1113. offsets[sym_count]:=st;
  1114. inc(sym_count);
  1115. st:=st+length(t.realname)+1;
  1116. end;
  1117. {Sort the syms by enum name}
  1118. if sym_count>=2 then
  1119. begin
  1120. p:=1;
  1121. while 2*p<sym_count do
  1122. p:=2*p;
  1123. while p<>0 do
  1124. begin
  1125. for h:=p to sym_count-1 do
  1126. begin
  1127. i:=h;
  1128. t:=syms[i];
  1129. o:=offsets[i];
  1130. repeat
  1131. if syms[i-p].name<=t.name then
  1132. break;
  1133. syms[i]:=syms[i-p];
  1134. offsets[i]:=offsets[i-p];
  1135. dec(i,p);
  1136. until i<p;
  1137. syms[i]:=t;
  1138. offsets[i]:=o;
  1139. end;
  1140. p:=p shr 1;
  1141. end;
  1142. end;
  1143. st:=enumdef_rtti_calcstringtablestart(def);
  1144. enumdef_rtti_string2ordindex(sym_count,offsets,syms,st);
  1145. { Sort the syms by enum value }
  1146. if sym_count>=2 then
  1147. begin
  1148. p:=1;
  1149. while 2*p<sym_count do
  1150. p:=2*p;
  1151. while p<>0 do
  1152. begin
  1153. for h:=p to sym_count-1 do
  1154. begin
  1155. i:=h;
  1156. t:=syms[i];
  1157. o:=offsets[i];
  1158. repeat
  1159. if syms[i-p].value<=t.value then
  1160. break;
  1161. syms[i]:=syms[i-p];
  1162. offsets[i]:=offsets[i-p];
  1163. dec(i,p);
  1164. until i<p;
  1165. syms[i]:=t;
  1166. offsets[i]:=o;
  1167. end;
  1168. p:=p shr 1;
  1169. end;
  1170. end;
  1171. enumdef_rtti_ord2stringindex(sym_count,offsets,syms,st);
  1172. freemem(syms);
  1173. freemem(offsets);
  1174. end;
  1175. begin
  1176. case def.typ of
  1177. enumdef:
  1178. if rt=fullrtti then
  1179. begin
  1180. enumdef_rtti_extrasyms(Tenumdef(def));
  1181. end;
  1182. end;
  1183. end;
  1184. procedure TRTTIWriter.write_child_rtti_data(def:tdef;rt:trttitype);
  1185. begin
  1186. case def.typ of
  1187. enumdef :
  1188. if assigned(tenumdef(def).basedef) then
  1189. write_rtti(tenumdef(def).basedef,rt);
  1190. setdef :
  1191. write_rtti(tsetdef(def).elementdef,rt);
  1192. arraydef :
  1193. begin
  1194. write_rtti(tarraydef(def).rangedef,rt);
  1195. write_rtti(tarraydef(def).elementdef,rt);
  1196. end;
  1197. recorddef :
  1198. fields_write_rtti(trecorddef(def).symtable,rt);
  1199. objectdef :
  1200. begin
  1201. if assigned(tobjectdef(def).childof) then
  1202. write_rtti(tobjectdef(def).childof,rt);
  1203. if (rt=initrtti) or (tobjectdef(def).objecttype=odt_object) then
  1204. fields_write_rtti(tobjectdef(def).symtable,rt)
  1205. else
  1206. published_write_rtti(tobjectdef(def).symtable,rt);
  1207. end;
  1208. classrefdef,
  1209. pointerdef:
  1210. if not is_objc_class_or_protocol(tabstractpointerdef(def).pointeddef) then
  1211. write_rtti(tabstractpointerdef(def).pointeddef,rt);
  1212. procvardef:
  1213. params_write_rtti(tabstractprocdef(def),rt);
  1214. end;
  1215. end;
  1216. procedure TRTTIWriter.write_rtti_reference(def:tdef;rt:trttitype);
  1217. begin
  1218. if not assigned(def) or is_void(def) or ((rt<>initrtti) and is_objc_class_or_protocol(def)) then
  1219. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_nil_dataptr)
  1220. else
  1221. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(ref_rtti(def,rt)));
  1222. end;
  1223. function TRTTIWriter.ref_rtti(def:tdef;rt:trttitype):tasmsymbol;
  1224. begin
  1225. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt,true),AT_DATA);
  1226. if (cs_create_pic in current_settings.moduleswitches) and
  1227. assigned(current_procinfo) then
  1228. include(current_procinfo.flags,pi_needs_got);
  1229. end;
  1230. procedure TRTTIWriter.write_rtti(def:tdef;rt:trttitype);
  1231. var
  1232. rttilab,
  1233. rttilabind : tasmsymbol;
  1234. begin
  1235. { only write rtti of definitions from the current module }
  1236. if not findunitsymtable(def.owner).iscurrentunit then
  1237. exit;
  1238. if (df_generic in def.defoptions) or
  1239. (
  1240. (def.typ=undefineddef) and
  1241. (sp_generic_dummy in def.typesym.symoptions)
  1242. ) then
  1243. exit;
  1244. { check if separate initrtti is actually needed }
  1245. if (rt=initrtti) and (not def.needs_separate_initrtti) then
  1246. rt:=fullrtti;
  1247. { prevent recursion }
  1248. if rttidefstate[rt] in def.defstates then
  1249. exit;
  1250. include(def.defstates,rttidefstate[rt]);
  1251. { write first all dependencies }
  1252. write_child_rtti_data(def,rt);
  1253. { write rtti data }
  1254. rttilab:=current_asmdata.DefineAsmSymbol(tstoreddef(def).rtti_mangledname(rt,false),AB_GLOBAL,AT_DATA);
  1255. maybe_new_object_file(current_asmdata.asmlists[al_rtti]);
  1256. new_section(current_asmdata.asmlists[al_rtti],sec_rodata,rttilab.name,const_align(sizeof(pint)));
  1257. current_asmdata.asmlists[al_rtti].concat(Tai_symbol.Create_global(rttilab,0));
  1258. write_rtti_data(def,rt);
  1259. current_asmdata.asmlists[al_rtti].concat(Tai_symbol_end.Create(rttilab));
  1260. write_rtti_extrasyms(def,rt,rttilab);
  1261. { write the indirect symbol }
  1262. rttilabind:=current_asmdata.DefineAsmSymbol(tstoreddef(def).rtti_mangledname(rt,true),AB_GLOBAL,AT_DATA);
  1263. current_asmdata.asmlists[al_rtti].concat(Tai_symbol.Create_Global(rttilabind,0));
  1264. current_asmdata.asmlists[al_rtti].concat(Tai_const.Createname(rttilab.name,AT_DATA,0));
  1265. current_asmdata.asmlists[al_rtti].concat(tai_symbol_end.Create(rttilabind));
  1266. end;
  1267. function TRTTIWriter.get_rtti_label(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  1268. begin
  1269. result:=current_asmdata.RefAsmSymbol(def.rtti_mangledname(rt,indirect),AT_DATA);
  1270. if (cs_create_pic in current_settings.moduleswitches) and
  1271. assigned(current_procinfo) then
  1272. include(current_procinfo.flags,pi_needs_got);
  1273. end;
  1274. function TRTTIWriter.get_rtti_label_ord2str(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  1275. var
  1276. name : tsymstr;
  1277. begin
  1278. name:=def.rtti_mangledname(rt,false)+'_o2s';
  1279. if indirect then
  1280. name:=name+indirect_suffix;
  1281. result:=current_asmdata.RefAsmSymbol(name,AT_DATA);
  1282. if (cs_create_pic in current_settings.moduleswitches) and
  1283. assigned(current_procinfo) then
  1284. include(current_procinfo.flags,pi_needs_got);
  1285. end;
  1286. function TRTTIWriter.get_rtti_label_str2ord(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  1287. var
  1288. name : tsymstr;
  1289. begin
  1290. name:=def.rtti_mangledname(rt,false)+'_s2o';
  1291. if indirect then
  1292. name:=name+indirect_suffix;
  1293. result:=current_asmdata.RefAsmSymbol(name,AT_DATA);
  1294. if (cs_create_pic in current_settings.moduleswitches) and
  1295. assigned(current_procinfo) then
  1296. include(current_procinfo.flags,pi_needs_got);
  1297. end;
  1298. end.