ncgrtti.pas 48 KB

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