ncgrtti.pas 49 KB

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