ncgrtti.pas 52 KB

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