ncgrtti.pas 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  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,aasmcnst,
  23. symbase,symconst,symtype,symdef,symsym;
  24. type
  25. { TRTTIWriter }
  26. TRTTIWriter=class
  27. private
  28. { required internal alignment of the rtti data }
  29. reqalign: shortint;
  30. { required packing of all structures except for ttypeinfo and tpropinfo,
  31. which always use packrecords 1 }
  32. defaultpacking: shortint;
  33. procedure fields_write_rtti(st:tsymtable;rt:trttitype);
  34. procedure params_write_rtti(def:tabstractprocdef;rt:trttitype);
  35. procedure fields_write_rtti_data(tcb: ttai_typedconstbuilder; def: tabstractrecorddef; rt: trttitype);
  36. procedure write_rtti_extrasyms(def:Tdef;rt:Trttitype;mainrtti:Tasmsymbol);
  37. procedure published_write_rtti(st:tsymtable;rt:trttitype);
  38. function published_properties_count(st:tsymtable):longint;
  39. procedure published_properties_write_rtti_data(tcb: ttai_typedconstbuilder; propnamelist: TFPHashObjectList; st: tsymtable);
  40. procedure collect_propnamelist(propnamelist:TFPHashObjectList;objdef:tobjectdef);
  41. { only use a direct reference if the referenced type can *only* reside
  42. in the same unit as the current one }
  43. function ref_rtti(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  44. procedure write_rtti_name(tcb: ttai_typedconstbuilder; def: tdef);
  45. procedure write_rtti_data(tcb: ttai_typedconstbuilder; def:tdef; rt: trttitype);
  46. procedure write_child_rtti_data(def:tdef;rt:trttitype);
  47. procedure write_rtti_reference(tcb: ttai_typedconstbuilder; def: tdef; rt: trttitype);
  48. procedure write_header(tcb: ttai_typedconstbuilder; def: tdef; typekind: byte);
  49. function write_methodkind(tcb:ttai_typedconstbuilder;def:tabstractprocdef):byte;
  50. procedure write_callconv(tcb:ttai_typedconstbuilder;def:tabstractprocdef);
  51. procedure write_paralocs(tcb:ttai_typedconstbuilder;parasym:tparavarsym);
  52. procedure write_param_flag(tcb:ttai_typedconstbuilder;parasym:tparavarsym);
  53. public
  54. constructor create;
  55. procedure write_rtti(def:tdef;rt:trttitype);
  56. function get_rtti_label(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  57. function get_rtti_label_ord2str(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  58. function get_rtti_label_str2ord(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  59. end;
  60. { generate RTTI and init tables }
  61. procedure write_persistent_type_info(st:tsymtable;is_global:boolean);
  62. var
  63. RTTIWriter : TRTTIWriter;
  64. implementation
  65. uses
  66. cutils,
  67. globals,globtype,verbose,systems,
  68. fmodule, procinfo,
  69. symtable,
  70. aasmtai,aasmdata,
  71. defutil,
  72. parabase,paramgr,
  73. wpobase
  74. ;
  75. const
  76. rttidefstate : array[trttitype] of tdefstate =
  77. (ds_rtti_table_written,ds_init_table_written,
  78. { Objective-C related, does not pass here }
  79. symconst.ds_none,symconst.ds_none,
  80. symconst.ds_none,symconst.ds_none);
  81. type
  82. TPropNameListItem = class(TFPHashObject)
  83. propindex : longint;
  84. propowner : TSymtable;
  85. end;
  86. procedure write_persistent_type_info(st: tsymtable; is_global: boolean);
  87. var
  88. i : longint;
  89. def : tdef;
  90. begin
  91. { no Delphi-style RTTI for managed platforms }
  92. if target_info.system in systems_managed_vm then
  93. exit;
  94. for i:=0 to st.DefList.Count-1 do
  95. begin
  96. def:=tdef(st.DefList[i]);
  97. { skip generics }
  98. if [df_generic,df_genconstraint]*def.defoptions<>[] then
  99. continue;
  100. case def.typ of
  101. recorddef:
  102. write_persistent_type_info(trecorddef(def).symtable,is_global);
  103. objectdef :
  104. begin
  105. { Skip forward defs }
  106. if (oo_is_forward in tobjectdef(def).objectoptions) then
  107. continue;
  108. write_persistent_type_info(tobjectdef(def).symtable,is_global);
  109. end;
  110. procdef :
  111. begin
  112. if assigned(tprocdef(def).localst) and
  113. (tprocdef(def).localst.symtabletype=localsymtable) then
  114. write_persistent_type_info(tprocdef(def).localst,false);
  115. if assigned(tprocdef(def).parast) then
  116. write_persistent_type_info(tprocdef(def).parast,false);
  117. end;
  118. errordef:
  119. { we shouldn't have come this far if we have an errordef somewhere }
  120. internalerror(2017010701);
  121. undefineddef:
  122. { don't write any RTTI for these }
  123. continue;
  124. end;
  125. { always generate persistent tables for types in the interface so
  126. they can be reused in other units and give always the same pointer
  127. location. }
  128. { Init }
  129. if (
  130. assigned(def.typesym) and
  131. is_global and
  132. not is_objc_class_or_protocol(def)
  133. ) or
  134. is_managed_type(def) or
  135. (ds_init_table_used in def.defstates) then
  136. RTTIWriter.write_rtti(def,initrtti);
  137. { RTTI }
  138. if (
  139. assigned(def.typesym) and
  140. is_global and
  141. not is_objc_class_or_protocol(def)
  142. ) or
  143. (ds_rtti_table_used in def.defstates) then
  144. RTTIWriter.write_rtti(def,fullrtti);
  145. end;
  146. end;
  147. {***************************************************************************
  148. TRTTIWriter
  149. ***************************************************************************}
  150. procedure TRTTIWriter.write_header(tcb: ttai_typedconstbuilder; def: tdef; typekind: byte);
  151. var
  152. name: shortstring;
  153. begin
  154. if assigned(def.typesym) then
  155. name:=ttypesym(def.typesym).realname
  156. else
  157. name:='';
  158. { TTypeInfo, always packed and doesn't need alignment }
  159. tcb.begin_anonymous_record(
  160. internaltypeprefixName[itp_rtti_header]+tostr(length(name)),1,1,
  161. targetinfos[target_info.system]^.alignment.recordalignmin,
  162. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  163. if def.typ=arraydef then
  164. InternalError(201012211);
  165. tcb.emit_tai(Tai_const.Create_8bit(typekind),u8inttype);
  166. tcb.emit_shortstring_const(name);
  167. tcb.end_anonymous_record;
  168. end;
  169. function TRTTIWriter.write_methodkind(tcb:ttai_typedconstbuilder;def:tabstractprocdef):byte;
  170. begin
  171. case def.proctypeoption of
  172. potype_constructor: result:=mkConstructor;
  173. potype_destructor: result:=mkDestructor;
  174. potype_class_constructor: result:=mkClassConstructor;
  175. potype_class_destructor: result:=mkClassDestructor;
  176. potype_operator: result:=mkOperatorOverload;
  177. potype_procedure:
  178. if po_classmethod in def.procoptions then
  179. result:=mkClassProcedure
  180. else
  181. result:=mkProcedure;
  182. potype_function:
  183. if po_classmethod in def.procoptions then
  184. result:=mkClassFunction
  185. else
  186. result:=mkFunction;
  187. else
  188. begin
  189. if def.returndef = voidtype then
  190. result:=mkProcedure
  191. else
  192. result:=mkFunction;
  193. end;
  194. end;
  195. tcb.emit_ord_const(result,u8inttype);
  196. end;
  197. procedure TRTTIWriter.write_callconv(tcb:ttai_typedconstbuilder;def:tabstractprocdef);
  198. const
  199. ProcCallOptionToCallConv: array[tproccalloption] of byte = (
  200. { pocall_none } 0,
  201. { pocall_cdecl } 1,
  202. { pocall_cppdecl } 5,
  203. { pocall_far16 } 6,
  204. { pocall_oldfpccall } 7,
  205. { pocall_internproc } 8,
  206. { pocall_syscall } 9,
  207. { pocall_pascal } 2,
  208. { pocall_register } 0,
  209. { pocall_safecall } 4,
  210. { pocall_stdcall } 3,
  211. { pocall_softfloat } 10,
  212. { pocall_mwpascal } 11,
  213. { pocall_interrupt } 12,
  214. { pocall_hardfloat } 13
  215. );
  216. begin
  217. tcb.emit_ord_const(ProcCallOptionToCallConv[def.proccalloption],u8inttype);
  218. end;
  219. procedure TRTTIWriter.write_paralocs(tcb:ttai_typedconstbuilder;parasym:tparavarsym);
  220. var
  221. locs : trttiparalocs;
  222. i : longint;
  223. begin
  224. locs:=paramanager.cgparalocs_to_rttiparalocs(parasym.paraloc[callerside].location);
  225. if length(locs)>high(byte) then
  226. internalerror(2017010601);
  227. tcb.emit_ord_const(length(locs),u8inttype);
  228. for i:=low(locs) to high(locs) do
  229. begin
  230. tcb.emit_ord_const(locs[i].loctype,u8inttype);
  231. tcb.emit_ord_const(locs[i].regsub,u8inttype);
  232. tcb.emit_ord_const(locs[i].regindex,u16inttype);
  233. { the corresponding type for aint is alusinttype }
  234. tcb.emit_ord_const(locs[i].offset,alusinttype);
  235. end;
  236. end;
  237. procedure TRTTIWriter.write_param_flag(tcb:ttai_typedconstbuilder;parasym:tparavarsym);
  238. var
  239. paraspec : word;
  240. begin
  241. case parasym.varspez of
  242. vs_value : paraspec := 0;
  243. vs_const : paraspec := pfConst;
  244. vs_var : paraspec := pfVar;
  245. vs_out : paraspec := pfOut;
  246. vs_constref: paraspec := pfConstRef;
  247. else
  248. internalerror(2013112904);
  249. end;
  250. { Kylix also seems to always add both pfArray and pfReference
  251. in this case
  252. }
  253. if is_open_array(parasym.vardef) then
  254. paraspec:=paraspec or pfArray or pfReference;
  255. { and these for classes and interfaces (maybe because they
  256. are themselves addresses?)
  257. }
  258. if is_class_or_interface(parasym.vardef) then
  259. paraspec:=paraspec or pfAddress;
  260. { flags for the hidden parameters }
  261. if vo_is_hidden_para in parasym.varoptions then
  262. paraspec:=paraspec or pfHidden;
  263. if vo_is_high_para in parasym.varoptions then
  264. paraspec:=paraspec or pfHigh;
  265. if vo_is_self in parasym.varoptions then
  266. paraspec:=paraspec or pfSelf;
  267. if vo_is_vmt in parasym.varoptions then
  268. paraspec:=paraspec or pfVmt;
  269. { set bits run from the highest to the lowest bit on
  270. big endian systems
  271. }
  272. if (target_info.endian = endian_big) then
  273. paraspec:=reverse_word(paraspec);
  274. { write flags for current parameter }
  275. tcb.emit_ord_const(paraspec,u16inttype);
  276. end;
  277. procedure TRTTIWriter.write_rtti_name(tcb: ttai_typedconstbuilder; def: tdef);
  278. begin
  279. if is_open_array(def) then
  280. { open arrays never have a typesym with a name, since you cannot
  281. define an "open array type". Kylix prints the type of the
  282. elements in the array in this case (so together with the pfArray
  283. flag, you can reconstruct the full typename, I assume (JM))
  284. }
  285. def:=tarraydef(def).elementdef;
  286. { name }
  287. if assigned(def.typesym) then
  288. tcb.emit_shortstring_const(ttypesym(def.typesym).realname)
  289. else
  290. tcb.emit_shortstring_const('');
  291. end;
  292. { writes a 32-bit count followed by array of field infos for given symtable }
  293. procedure TRTTIWriter.fields_write_rtti_data(tcb: ttai_typedconstbuilder; def: tabstractrecorddef; rt: trttitype);
  294. var
  295. i : longint;
  296. sym : tsym;
  297. fieldcnt: longint;
  298. st: tsymtable;
  299. fields: tfplist;
  300. parentrtti: boolean;
  301. begin
  302. fieldcnt:=0;
  303. parentrtti:=false;
  304. st:=def.symtable;
  305. fields:=tfplist.create;
  306. fields.capacity:=st.symlist.count+1;
  307. { For objects, treat parent (if any) as a field with offset 0. This
  308. provides correct handling of entire instance with RTL rtti routines. }
  309. if (def.typ=objectdef) and (tobjectdef(def).objecttype=odt_object) and
  310. Assigned(tobjectdef(def).childof) and
  311. ((rt=fullrtti) or (tobjectdef(def).childof.needs_inittable)) then
  312. begin
  313. parentrtti:=true;
  314. inc(fieldcnt);
  315. end;
  316. for i:=0 to st.SymList.Count-1 do
  317. begin
  318. sym:=tsym(st.SymList[i]);
  319. if (tsym(sym).typ=fieldvarsym) and
  320. not(sp_static in tsym(sym).symoptions) and
  321. (
  322. (rt=fullrtti) or
  323. tfieldvarsym(sym).vardef.needs_inittable
  324. ) and
  325. not is_objc_class_or_protocol(tfieldvarsym(sym).vardef) then
  326. begin
  327. fields.add(tfieldvarsym(sym));
  328. inc(fieldcnt);
  329. end;
  330. end;
  331. { insert field count before data }
  332. tcb.emit_ord_const(fieldcnt,u32inttype);
  333. { parent object? }
  334. if parentrtti then
  335. begin
  336. write_rtti_reference(tcb,tobjectdef(def).childof,rt);
  337. tcb.emit_ord_const(0,ptruinttype);
  338. end;
  339. { fields }
  340. for i:=0 to fields.count-1 do
  341. begin
  342. sym:=tsym(fields[i]);
  343. write_rtti_reference(tcb,tfieldvarsym(sym).vardef,rt);
  344. tcb.emit_ord_const(tfieldvarsym(sym).fieldoffset,ptruinttype);
  345. end;
  346. fields.free;
  347. end;
  348. procedure TRTTIWriter.fields_write_rtti(st:tsymtable;rt:trttitype);
  349. var
  350. i : longint;
  351. sym : tsym;
  352. begin
  353. for i:=0 to st.SymList.Count-1 do
  354. begin
  355. sym:=tsym(st.SymList[i]);
  356. if (tsym(sym).typ=fieldvarsym) and
  357. not(sp_static in tsym(sym).symoptions) and
  358. (
  359. (rt=fullrtti) or
  360. tfieldvarsym(sym).vardef.needs_inittable
  361. ) then
  362. write_rtti(tfieldvarsym(sym).vardef,rt);
  363. end;
  364. end;
  365. procedure TRTTIWriter.params_write_rtti(def:tabstractprocdef;rt:trttitype);
  366. var
  367. i : longint;
  368. sym : tparavarsym;
  369. begin
  370. for i:=0 to def.paras.count-1 do
  371. begin
  372. sym:=tparavarsym(def.paras[i]);
  373. if not (vo_is_hidden_para in sym.varoptions) then
  374. write_rtti(sym.vardef,rt);
  375. end;
  376. end;
  377. procedure TRTTIWriter.published_write_rtti(st:tsymtable;rt:trttitype);
  378. var
  379. i : longint;
  380. sym : tsym;
  381. begin
  382. for i:=0 to st.SymList.Count-1 do
  383. begin
  384. sym:=tsym(st.SymList[i]);
  385. if (sym.visibility=vis_published) then
  386. begin
  387. case tsym(sym).typ of
  388. propertysym:
  389. write_rtti(tpropertysym(sym).propdef,rt);
  390. fieldvarsym:
  391. write_rtti(tfieldvarsym(sym).vardef,rt);
  392. end;
  393. end;
  394. end;
  395. end;
  396. function TRTTIWriter.published_properties_count(st:tsymtable):longint;
  397. var
  398. i : longint;
  399. sym : tsym;
  400. begin
  401. result:=0;
  402. for i:=0 to st.SymList.Count-1 do
  403. begin
  404. sym:=tsym(st.SymList[i]);
  405. if (tsym(sym).typ=propertysym) and
  406. (sym.visibility=vis_published) then
  407. inc(result);
  408. end;
  409. end;
  410. procedure TRTTIWriter.collect_propnamelist(propnamelist:TFPHashObjectList;objdef:tobjectdef);
  411. var
  412. i : longint;
  413. sym : tsym;
  414. pn : tpropnamelistitem;
  415. begin
  416. if assigned(objdef.childof) then
  417. collect_propnamelist(propnamelist,objdef.childof);
  418. for i:=0 to objdef.symtable.SymList.Count-1 do
  419. begin
  420. sym:=tsym(objdef.symtable.SymList[i]);
  421. if (tsym(sym).typ=propertysym) and
  422. (sym.visibility=vis_published) then
  423. begin
  424. pn:=TPropNameListItem(propnamelist.Find(tsym(sym).name));
  425. if not assigned(pn) then
  426. begin
  427. pn:=tpropnamelistitem.create(propnamelist,tsym(sym).name);
  428. pn.propindex:=propnamelist.count-1;
  429. pn.propowner:=tsym(sym).owner;
  430. end;
  431. end;
  432. end;
  433. end;
  434. procedure TRTTIWriter.published_properties_write_rtti_data(tcb: ttai_typedconstbuilder; propnamelist:TFPHashObjectList;st:tsymtable);
  435. var
  436. i : longint;
  437. sym : tsym;
  438. proctypesinfo : byte;
  439. propnameitem : tpropnamelistitem;
  440. propdefname : string;
  441. procedure writeaccessproc(pap:tpropaccesslisttypes; shiftvalue : byte; unsetvalue: byte);
  442. var
  443. typvalue : byte;
  444. hp : ppropaccesslistitem;
  445. extnumber: longint;
  446. address,space : longint;
  447. def : tdef;
  448. hpropsym : tpropertysym;
  449. propaccesslist : tpropaccesslist;
  450. begin
  451. hpropsym:=tpropertysym(sym);
  452. repeat
  453. propaccesslist:=hpropsym.propaccesslist[pap];
  454. if not propaccesslist.empty then
  455. break;
  456. hpropsym:=hpropsym.overriddenpropsym;
  457. until not assigned(hpropsym);
  458. if not(assigned(propaccesslist) and assigned(propaccesslist.firstsym)) then
  459. begin
  460. tcb.emit_tai(Tai_const.Create_int_codeptr(unsetvalue),codeptruinttype);
  461. typvalue:=3;
  462. end
  463. else if propaccesslist.firstsym^.sym.typ=fieldvarsym then
  464. begin
  465. address:=0;
  466. hp:=propaccesslist.firstsym;
  467. def:=nil;
  468. while assigned(hp) do
  469. begin
  470. case hp^.sltype of
  471. sl_load :
  472. begin
  473. def:=tfieldvarsym(hp^.sym).vardef;
  474. inc(address,tfieldvarsym(hp^.sym).fieldoffset);
  475. end;
  476. sl_subscript :
  477. begin
  478. if not(assigned(def) and
  479. ((def.typ=recorddef) or
  480. is_object(def))) then
  481. internalerror(200402171);
  482. inc(address,tfieldvarsym(hp^.sym).fieldoffset);
  483. def:=tfieldvarsym(hp^.sym).vardef;
  484. end;
  485. sl_vec :
  486. begin
  487. if not(assigned(def) and (def.typ=arraydef)) then
  488. internalerror(200402172);
  489. def:=tarraydef(def).elementdef;
  490. {Hp.value is a Tconstexprint, which can be rather large,
  491. sanity check for longint overflow.}
  492. space:=(high(address)-address) div def.size;
  493. if int64(space)<hp^.value then
  494. internalerror(200706101);
  495. inc(address,int64(def.size*hp^.value));
  496. end;
  497. end;
  498. hp:=hp^.next;
  499. end;
  500. tcb.emit_tai(Tai_const.Create_int_codeptr(address),codeptruinttype);
  501. typvalue:=0;
  502. end
  503. else
  504. begin
  505. { When there was an error then procdef is not assigned }
  506. if not assigned(propaccesslist.procdef) then
  507. exit;
  508. if not(po_virtualmethod in tprocdef(propaccesslist.procdef).procoptions) or
  509. is_objectpascal_helper(tprocdef(propaccesslist.procdef).struct) then
  510. begin
  511. tcb.queue_init(codeptruinttype);
  512. tcb.queue_emit_proc(tprocdef(propaccesslist.procdef));
  513. typvalue:=1;
  514. end
  515. else
  516. begin
  517. { virtual method, write vmt offset }
  518. extnumber:=tprocdef(propaccesslist.procdef).extnumber;
  519. tcb.emit_tai(Tai_const.Create_int_codeptr(
  520. tobjectdef(tprocdef(propaccesslist.procdef).struct).vmtmethodoffset(extnumber)),
  521. codeptruinttype);
  522. { register for wpo }
  523. tobjectdef(tprocdef(propaccesslist.procdef).struct).register_vmt_call(extnumber);
  524. {$ifdef vtentry}
  525. { not sure if we can insert those vtentry symbols safely here }
  526. {$error register methods used for published properties}
  527. {$endif vtentry}
  528. typvalue:=2;
  529. end;
  530. end;
  531. proctypesinfo:=proctypesinfo or (typvalue shl shiftvalue);
  532. end;
  533. begin
  534. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,SizeOf(PtrInt)),
  535. targetinfos[target_info.system]^.alignment.recordalignmin,
  536. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  537. tcb.emit_ord_const(published_properties_count(st),u16inttype);
  538. for i:=0 to st.SymList.Count-1 do
  539. begin
  540. sym:=tsym(st.SymList[i]);
  541. if (sym.typ=propertysym) and
  542. (sym.visibility=vis_published) then
  543. begin
  544. { we can only easily reuse defs if the property is not stored,
  545. because otherwise the rtti layout depends on how the "stored"
  546. is defined (field, indexed expression, virtual method, ...) }
  547. if not(ppo_stored in tpropertysym(sym).propoptions) then
  548. propdefname:=internaltypeprefixName[itp_rtti_prop]+tostr(length(tpropertysym(sym).realname))
  549. else
  550. propdefname:='';
  551. { TPropInfo is a packed record (even on targets that require
  552. alignment), but it starts aligned }
  553. tcb.begin_anonymous_record(
  554. propdefname,
  555. 1,min(reqalign,SizeOf(PtrInt)),
  556. targetinfos[target_info.system]^.alignment.recordalignmin,
  557. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  558. if ppo_indexed in tpropertysym(sym).propoptions then
  559. proctypesinfo:=$40
  560. else
  561. proctypesinfo:=0;
  562. write_rtti_reference(tcb,tpropertysym(sym).propdef,fullrtti);
  563. writeaccessproc(palt_read,0,0);
  564. writeaccessproc(palt_write,2,0);
  565. { is it stored ? }
  566. if not(ppo_stored in tpropertysym(sym).propoptions) then
  567. begin
  568. { no, so put a constant zero }
  569. tcb.emit_tai(Tai_const.Create_nil_codeptr,codeptruinttype);
  570. proctypesinfo:=proctypesinfo or (3 shl 4);
  571. end
  572. else
  573. writeaccessproc(palt_stored,4,1); { maybe; if no procedure put a constant 1 (=true) }
  574. tcb.emit_ord_const(tpropertysym(sym).index,u32inttype);
  575. tcb.emit_ord_const(tpropertysym(sym).default,u32inttype);
  576. propnameitem:=TPropNameListItem(propnamelist.Find(tpropertysym(sym).name));
  577. if not assigned(propnameitem) then
  578. internalerror(200512201);
  579. tcb.emit_ord_const(propnameitem.propindex,u16inttype);
  580. tcb.emit_ord_const(proctypesinfo,u8inttype);
  581. tcb.emit_shortstring_const(tpropertysym(sym).realname);
  582. tcb.end_anonymous_record;
  583. end;
  584. end;
  585. tcb.end_anonymous_record;
  586. end;
  587. procedure TRTTIWriter.write_rtti_data(tcb: ttai_typedconstbuilder; def: tdef; rt: trttitype);
  588. procedure unknown_rtti(def:tstoreddef);
  589. begin
  590. tcb.emit_ord_const(tkUnknown,u8inttype);
  591. write_rtti_name(tcb,def);
  592. end;
  593. procedure variantdef_rtti(def:tvariantdef);
  594. begin
  595. write_header(tcb,def,tkVariant);
  596. end;
  597. procedure stringdef_rtti(def:tstringdef);
  598. begin
  599. case def.stringtype of
  600. st_ansistring:
  601. begin
  602. write_header(tcb,def,tkAString);
  603. { align }
  604. tcb.begin_anonymous_record(
  605. internaltypeprefixName[itp_rtti_ansistr],
  606. defaultpacking,reqalign,
  607. targetinfos[target_info.system]^.alignment.recordalignmin,
  608. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  609. tcb.emit_ord_const(def.encoding,u16inttype);
  610. tcb.end_anonymous_record;
  611. end;
  612. st_widestring:
  613. write_header(tcb,def,tkWString);
  614. st_unicodestring:
  615. write_header(tcb,def,tkUString);
  616. st_longstring:
  617. write_header(tcb,def,tkLString);
  618. st_shortstring:
  619. begin
  620. write_header(tcb,def,tkSString);
  621. tcb.emit_ord_const(def.len,u8inttype);
  622. end;
  623. end;
  624. end;
  625. procedure enumdef_rtti(def: tenumdef);
  626. var
  627. i : integer;
  628. hp : tenumsym;
  629. begin
  630. write_header(tcb,def,tkEnumeration);
  631. { align; the named fields are so that we can let the compiler
  632. calculate the string offsets later on }
  633. tcb.next_field_name:='size_start_rec';
  634. { add a typename so that it can be reused when writing the the s2o
  635. and o2s arrays for llvm (otherwise we have to write out the entire
  636. type definition every time we access an element from this record) }
  637. tcb.begin_anonymous_record(internaltypeprefixName[itp_rtti_enum_size_start_rec]+def.unique_id_str,defaultpacking,reqalign,
  638. targetinfos[target_info.system]^.alignment.recordalignmin,
  639. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  640. case longint(def.size) of
  641. 1 :
  642. tcb.emit_ord_const(otUByte,u8inttype);
  643. 2 :
  644. tcb.emit_ord_const(otUWord,u8inttype);
  645. 4 :
  646. tcb.emit_ord_const(otULong,u8inttype);
  647. end;
  648. { we need to align by Tconstptruint here to satisfy the alignment
  649. rules set by records: in the typinfo unit we overlay a TTypeData
  650. record on this data, which at the innermost variant record needs an
  651. alignment of TConstPtrUint due to e.g. the "CompType" member for
  652. tkSet (also the "BaseType" member for tkEnumeration).
  653. We need to adhere to this, otherwise things will break. }
  654. tcb.next_field_name:='min_max_rec';
  655. tcb.begin_anonymous_record(internaltypeprefixName[itp_rtti_enum_min_max_rec]+def.unique_id_str,defaultpacking,reqalign,
  656. targetinfos[target_info.system]^.alignment.recordalignmin,
  657. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  658. tcb.emit_ord_const(def.min,s32inttype);
  659. tcb.emit_ord_const(def.max,s32inttype);
  660. tcb.next_field_name:='basetype_array_rec';
  661. { all strings must appear right after each other -> from now on
  662. packrecords 1 (but the start must still be aligned) }
  663. tcb.begin_anonymous_record(internaltypeprefixName[itp_rtti_enum_basetype_array_rec]+def.unique_id_str,1,reqalign,
  664. targetinfos[target_info.system]^.alignment.recordalignmin,
  665. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  666. { write base type }
  667. write_rtti_reference(tcb,def.basedef,rt);
  668. for i:=0 to def.symtable.SymList.Count-1 do
  669. begin
  670. hp:=tenumsym(def.symtable.SymList[i]);
  671. if hp.value<def.minval then
  672. continue
  673. else
  674. if hp.value>def.maxval then
  675. break;
  676. tcb.next_field_name:=hp.name;
  677. tcb.emit_shortstring_const(hp.realname);
  678. end;
  679. { write unit name }
  680. tcb.emit_shortstring_const(current_module.realmodulename^);
  681. { write zero which is required by RTL }
  682. tcb.emit_ord_const(0,u8inttype);
  683. { terminate all records }
  684. tcb.end_anonymous_record;
  685. tcb.end_anonymous_record;
  686. tcb.end_anonymous_record;
  687. end;
  688. procedure orddef_rtti(def:torddef);
  689. procedure doint32_64(typekind: byte;min,max:int64);
  690. const
  691. trans : array[tordtype] of byte =
  692. (otUByte{otNone},
  693. otUByte,otUWord,otULong,otUQWord,otUByte{otNone},
  694. otSByte,otSWord,otSLong,otSQWord,otUByte{otNone},
  695. otUByte,otUWord,otULong,otUQWord,
  696. otSByte,otSWord,otSLong,otSQWord,
  697. otUByte,otUWord,otUByte);
  698. var
  699. elesize: string[1];
  700. begin
  701. write_header(tcb,def,typekind);
  702. case trans[def.ordtype] of
  703. otUQWord,
  704. otSQWord:
  705. elesize:='8'
  706. else
  707. elesize:='4'
  708. end;
  709. tcb.begin_anonymous_record(
  710. internaltypeprefixName[itp_rtti_ord_outer]+elesize,
  711. defaultpacking,reqalign,
  712. targetinfos[target_info.system]^.alignment.recordalignmin,
  713. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  714. tcb.emit_ord_const(byte(trans[def.ordtype]),u8inttype);
  715. tcb.begin_anonymous_record(
  716. internaltypeprefixName[itp_rtti_ord_inner]+elesize,
  717. defaultpacking,reqalign,
  718. targetinfos[target_info.system]^.alignment.recordalignmin,
  719. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  720. {Convert to longint to smuggle values in high(longint)+1..high(cardinal) into asmlist.}
  721. case trans[def.ordtype] of
  722. otUQWord:
  723. begin
  724. tcb.emit_ord_const(min,u64inttype);
  725. tcb.emit_ord_const(max,u64inttype);
  726. end;
  727. otSQWord:
  728. begin
  729. tcb.emit_ord_const(min,s64inttype);
  730. tcb.emit_ord_const(max,s64inttype);
  731. end;
  732. else
  733. begin
  734. tcb.emit_ord_const(longint(min),s32inttype);
  735. tcb.emit_ord_const(longint(max),s32inttype);
  736. end;
  737. end;
  738. tcb.end_anonymous_record;
  739. tcb.end_anonymous_record;
  740. end;
  741. procedure dointeger(typekind:byte);inline;
  742. begin
  743. doint32_64(typekind,int64(def.low.svalue),int64(def.high.svalue));
  744. end;
  745. begin
  746. case def.ordtype of
  747. s64bit :
  748. dointeger(tkInt64);
  749. u64bit :
  750. dointeger(tkQWord);
  751. pasbool8,
  752. pasbool16,
  753. pasbool32,
  754. pasbool64:
  755. dointeger(tkBool);
  756. { use different low/high values to be Delphi compatible }
  757. bool8bit,
  758. bool16bit,
  759. bool32bit:
  760. doint32_64(tkBool,longint(low(longint)),longint(high(longint)));
  761. bool64bit:
  762. doint32_64(tkBool,low(int64),high(int64));
  763. uchar:
  764. dointeger(tkChar);
  765. uwidechar:
  766. dointeger(tkWChar);
  767. scurrency:
  768. begin
  769. write_header(tcb,def,tkFloat);
  770. tcb.begin_anonymous_record(
  771. internaltypeprefixName[itp_1byte],
  772. defaultpacking,reqalign,
  773. targetinfos[target_info.system]^.alignment.recordalignmin,
  774. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  775. tcb.emit_ord_const(ftCurr,u8inttype);
  776. tcb.end_anonymous_record;
  777. end;
  778. else
  779. dointeger(tkInteger);
  780. end;
  781. end;
  782. procedure floatdef_rtti(def:tfloatdef);
  783. const
  784. {tfloattype = (s32real,s64real,s80real,sc80real,s64bit,s128bit);}
  785. translate : array[tfloattype] of byte =
  786. (ftSingle,ftDouble,ftExtended,ftExtended,ftComp,ftCurr,ftFloat128);
  787. begin
  788. write_header(tcb,def,tkFloat);
  789. tcb.begin_anonymous_record(
  790. internaltypeprefixName[itp_1byte],
  791. defaultpacking,reqalign,
  792. targetinfos[target_info.system]^.alignment.recordalignmin,
  793. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  794. tcb.emit_ord_const(translate[def.floattype],u8inttype);
  795. tcb.end_anonymous_record;
  796. end;
  797. procedure setdef_rtti(def:tsetdef);
  798. begin
  799. write_header(tcb,def,tkSet);
  800. tcb.begin_anonymous_record(
  801. internaltypeprefixName[itp_1byte],
  802. defaultpacking,reqalign,
  803. targetinfos[target_info.system]^.alignment.recordalignmin,
  804. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  805. case def.size of
  806. 1:
  807. tcb.emit_ord_const(otUByte,u8inttype);
  808. 2:
  809. tcb.emit_ord_const(otUWord,u8inttype);
  810. 4:
  811. tcb.emit_ord_const(otULong,u8inttype);
  812. else
  813. tcb.emit_ord_const(otUByte,u8inttype);
  814. end;
  815. tcb.end_anonymous_record;
  816. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  817. targetinfos[target_info.system]^.alignment.recordalignmin,
  818. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  819. write_rtti_reference(tcb,def.elementdef,rt);
  820. tcb.end_anonymous_record;
  821. end;
  822. procedure arraydef_rtti(def:tarraydef);
  823. var
  824. i,dimcount: byte;
  825. totalcount: asizeuint;
  826. finaldef: tdef;
  827. curdef:tarraydef;
  828. begin
  829. if ado_IsDynamicArray in def.arrayoptions then
  830. tcb.emit_ord_const(tkDynArray,u8inttype)
  831. else
  832. tcb.emit_ord_const(tkArray,u8inttype);
  833. write_rtti_name(tcb,def);
  834. if not(ado_IsDynamicArray in def.arrayoptions) then
  835. begin
  836. { remember tha last instruction. we will need to insert some
  837. calculated values after it }
  838. finaldef:=def;
  839. totalcount:=1;
  840. dimcount:=0;
  841. repeat
  842. curdef:=tarraydef(finaldef);
  843. finaldef:=curdef.elementdef;
  844. { Dims[i] PTypeInfo }
  845. inc(dimcount);
  846. totalcount:=totalcount*curdef.elecount;
  847. until (finaldef.typ<>arraydef) or
  848. (ado_IsDynamicArray in tarraydef(finaldef).arrayoptions);
  849. tcb.begin_anonymous_record(
  850. internaltypeprefixName[itp_rtti_normal_array]+tostr(dimcount),
  851. defaultpacking,reqalign,
  852. targetinfos[target_info.system]^.alignment.recordalignmin,
  853. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  854. { total size = elecount * elesize of the first arraydef }
  855. tcb.emit_tai(Tai_const.Create_sizeint(def.elecount*def.elesize),sizeuinttype);
  856. { total element count }
  857. tcb.emit_tai(Tai_const.Create_sizeint(asizeint(totalcount)),sizeuinttype);
  858. { last dimension element type }
  859. tcb.emit_tai(Tai_const.Create_sym(ref_rtti(curdef.elementdef,rt,true)),voidpointertype);
  860. { dimension count }
  861. tcb.emit_ord_const(dimcount,u8inttype);
  862. finaldef:=def;
  863. { ranges of the dimensions }
  864. for i:=1 to dimcount do
  865. begin
  866. curdef:=tarraydef(finaldef);
  867. finaldef:=curdef.elementdef;
  868. { Dims[i] PPTypeInfo }
  869. write_rtti_reference(tcb,curdef.rangedef,rt);
  870. end;
  871. end
  872. else
  873. { write a delphi almost compatible dyn. array entry:
  874. there are two types, eltype and eltype2, the latter is nil if the element type needs
  875. no finalization, the former is always valid, delphi has this swapped, but for
  876. compatibility with older fpc versions we do it different, to be delphi compatible,
  877. the names are swapped in typinfo.pp
  878. }
  879. begin
  880. tcb.begin_anonymous_record(
  881. internaltypeprefixName[itp_rtti_dyn_array],
  882. defaultpacking,reqalign,
  883. targetinfos[target_info.system]^.alignment.recordalignmin,
  884. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  885. { size of elements }
  886. tcb.emit_tai(Tai_const.Create_sizeint(def.elesize),sizeuinttype);
  887. { element type }
  888. write_rtti_reference(tcb,def.elementdef,rt);
  889. { variant type }
  890. tcb.emit_ord_const(tstoreddef(def.elementdef).getvardef,s32inttype);
  891. { element type }
  892. if def.elementdef.needs_inittable then
  893. write_rtti_reference(tcb,def.elementdef,rt)
  894. else
  895. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  896. { write unit name }
  897. tcb.emit_shortstring_const(current_module.realmodulename^);
  898. end;
  899. tcb.end_anonymous_record;
  900. end;
  901. procedure classrefdef_rtti(def:tclassrefdef);
  902. begin
  903. write_header(tcb,def,tkClassRef);
  904. tcb.begin_anonymous_record(
  905. '',
  906. defaultpacking,reqalign,
  907. targetinfos[target_info.system]^.alignment.recordalignmin,
  908. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  909. write_rtti_reference(tcb,def.pointeddef,rt);
  910. tcb.end_anonymous_record;
  911. end;
  912. procedure pointerdef_rtti(def:tpointerdef);
  913. begin
  914. write_header(tcb,def,tkPointer);
  915. tcb.begin_anonymous_record(
  916. '',
  917. defaultpacking,reqalign,
  918. targetinfos[target_info.system]^.alignment.recordalignmin,
  919. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  920. write_rtti_reference(tcb,def.pointeddef,rt);
  921. tcb.end_anonymous_record;
  922. end;
  923. procedure recorddef_rtti(def:trecorddef);
  924. begin
  925. write_header(tcb,def,tkRecord);
  926. { need extra reqalign record, because otherwise the u32 int will
  927. only be aligned to 4 even on 64 bit target (while the rtti code
  928. in typinfo expects alignments to sizeof(pointer)) }
  929. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  930. targetinfos[target_info.system]^.alignment.recordalignmin,
  931. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  932. { store special terminator for init table for more optimal rtl operations
  933. strictly related to RecordRTTI procedure in rtti.inc (directly
  934. related to RTTIRecordRttiInfoToInitInfo function) }
  935. if (rt=initrtti) then
  936. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype)
  937. else
  938. begin
  939. { point to more optimal init table }
  940. include(def.defstates,ds_init_table_used);
  941. { we use a direct reference as the init RTTI is always in the same
  942. unit as the full RTTI }
  943. tcb.emit_tai(Tai_const.Create_sym(ref_rtti(def,initrtti,false)),voidpointertype);
  944. end;
  945. tcb.emit_ord_const(def.size,u32inttype);
  946. fields_write_rtti_data(tcb,def,rt);
  947. tcb.end_anonymous_record;
  948. { guarantee initrtti for any record for fpc_initialize, fpc_finalize }
  949. if (rt=fullrtti) and
  950. (ds_init_table_used in def.defstates) and
  951. not (ds_init_table_written in def.defstates)
  952. then
  953. write_rtti(def, initrtti);
  954. end;
  955. procedure procvardef_rtti(def:tprocvardef);
  956. procedure write_para(parasym:tparavarsym);
  957. begin
  958. { only store user visible parameters }
  959. if not(vo_is_hidden_para in parasym.varoptions) then
  960. begin
  961. { write flags for current parameter }
  962. write_param_flag(tcb,parasym);
  963. { write name of current parameter }
  964. tcb.emit_shortstring_const(parasym.realname);
  965. { write name of type of current parameter }
  966. write_rtti_name(tcb,parasym.vardef);
  967. end;
  968. end;
  969. procedure write_procedure_param(parasym:tparavarsym);
  970. begin
  971. { only store user visible parameters }
  972. if not(vo_is_hidden_para in parasym.varoptions) then
  973. begin
  974. { every parameter is expected to start aligned }
  975. tcb.begin_anonymous_record(
  976. internaltypeprefixName[itp_rtti_proc_param]+tostr(length(parasym.realname)),
  977. defaultpacking,min(reqalign,SizeOf(PtrInt)),
  978. targetinfos[target_info.system]^.alignment.recordalignmin,
  979. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  980. { write flags for current parameter }
  981. write_param_flag(tcb,parasym);
  982. { write param type }
  983. write_rtti_reference(tcb,parasym.vardef,fullrtti);
  984. { write name of current parameter }
  985. tcb.emit_shortstring_const(parasym.realname);
  986. tcb.end_anonymous_record;
  987. end;
  988. end;
  989. var
  990. methodkind : byte;
  991. i : integer;
  992. begin
  993. if po_methodpointer in def.procoptions then
  994. begin
  995. { write method id and name }
  996. write_header(tcb,def,tkMethod);
  997. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  998. targetinfos[target_info.system]^.alignment.recordalignmin,
  999. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1000. { write kind of method }
  1001. methodkind:=write_methodkind(tcb,def);
  1002. { write parameter info. The parameters must be written in reverse order
  1003. if this method uses right to left parameter pushing! }
  1004. tcb.emit_ord_const(def.maxparacount,u8inttype);
  1005. for i:=0 to def.paras.count-1 do
  1006. write_para(tparavarsym(def.paras[i]));
  1007. if (methodkind=mkFunction) or (methodkind=mkClassFunction) then
  1008. begin
  1009. { write name of result type }
  1010. write_rtti_name(tcb,def.returndef);
  1011. { enclosing record takes care of alignment }
  1012. { write result typeinfo }
  1013. write_rtti_reference(tcb,def.returndef,fullrtti);
  1014. end;
  1015. { write calling convention }
  1016. write_callconv(tcb,def);
  1017. { enclosing record takes care of alignment }
  1018. { write params typeinfo }
  1019. for i:=0 to def.paras.count-1 do
  1020. if not(vo_is_hidden_para in tparavarsym(def.paras[i]).varoptions) then
  1021. write_rtti_reference(tcb,tparavarsym(def.paras[i]).vardef,fullrtti);
  1022. tcb.end_anonymous_record;
  1023. end
  1024. else
  1025. begin
  1026. write_header(tcb,def,tkProcvar);
  1027. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1028. targetinfos[target_info.system]^.alignment.recordalignmin,
  1029. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1030. { flags }
  1031. tcb.emit_ord_const(0,u8inttype);
  1032. { write calling convention }
  1033. write_callconv(tcb,def);
  1034. { enclosing record takes care of alignment }
  1035. { write result typeinfo }
  1036. write_rtti_reference(tcb,def.returndef,fullrtti);
  1037. { write parameter count }
  1038. tcb.emit_ord_const(def.maxparacount,u8inttype);
  1039. for i:=0 to def.paras.count-1 do
  1040. write_procedure_param(tparavarsym(def.paras[i]));
  1041. tcb.end_anonymous_record;
  1042. end;
  1043. end;
  1044. procedure objectdef_rtti(def: tobjectdef);
  1045. procedure objectdef_rtti_fields(def:tobjectdef);
  1046. begin
  1047. { - for compatiblity with record RTTI we need to write a terminator-
  1048. Nil pointer as well for objects
  1049. - classes are assumed to have the same INIT RTTI as records
  1050. (see TObject.CleanupInstance) }
  1051. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1052. tcb.emit_ord_const(def.size, u32inttype);
  1053. { enclosing record takes care of alignment }
  1054. fields_write_rtti_data(tcb,def,rt);
  1055. end;
  1056. procedure objectdef_rtti_interface_init(def:tobjectdef);
  1057. begin
  1058. tcb.emit_ord_const(def.size, u32inttype);
  1059. end;
  1060. procedure objectdef_rtti_class_full(def:tobjectdef);
  1061. var
  1062. propnamelist : TFPHashObjectList;
  1063. begin
  1064. { Collect unique property names with nameindex }
  1065. propnamelist:=TFPHashObjectList.Create;
  1066. collect_propnamelist(propnamelist,def);
  1067. if not is_objectpascal_helper(def) then
  1068. if (oo_has_vmt in def.objectoptions) then
  1069. tcb.emit_tai(
  1070. Tai_const.Createname(def.vmt_mangledname,AT_DATA_FORCEINDIRECT,0),
  1071. cpointerdef.getreusable(def.vmt_def))
  1072. else
  1073. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1074. { write parent typeinfo }
  1075. write_rtti_reference(tcb,def.childof,fullrtti);
  1076. { write typeinfo of extended type }
  1077. if is_objectpascal_helper(def) then
  1078. if assigned(def.extendeddef) then
  1079. write_rtti_reference(tcb,def.extendeddef,fullrtti)
  1080. else
  1081. InternalError(2011033001);
  1082. { total number of unique properties }
  1083. tcb.emit_ord_const(propnamelist.count,u16inttype);
  1084. { write unit name }
  1085. tcb.emit_shortstring_const(current_module.realmodulename^);
  1086. { write published properties for this object }
  1087. published_properties_write_rtti_data(tcb,propnamelist,def.symtable);
  1088. propnamelist.free;
  1089. end;
  1090. procedure objectdef_rtti_interface_full(def:tobjectdef);
  1091. var
  1092. propnamelist : TFPHashObjectList;
  1093. { if changed to a set, make sure it's still a byte large, and
  1094. swap appropriately when cross-compiling
  1095. }
  1096. IntfFlags: byte;
  1097. begin
  1098. { Collect unique property names with nameindex }
  1099. propnamelist:=TFPHashObjectList.Create;
  1100. collect_propnamelist(propnamelist,def);
  1101. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1102. targetinfos[target_info.system]^.alignment.recordalignmin,
  1103. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1104. { write parent typeinfo }
  1105. write_rtti_reference(tcb,def.childof,fullrtti);
  1106. { interface: write flags, iid and iidstr }
  1107. IntfFlags:=0;
  1108. if assigned(def.iidguid) then
  1109. IntfFlags:=IntfFlags or (1 shl ord(ifHasGuid));
  1110. if (def.objecttype=odt_interfacecorba) and (def.iidstr^<>'') then
  1111. IntfFlags:=IntfFlags or (1 shl ord(ifHasStrGUID));
  1112. if (def.objecttype=odt_dispinterface) then
  1113. IntfFlags:=IntfFlags or (1 shl ord(ifDispInterface));
  1114. if (target_info.endian=endian_big) then
  1115. IntfFlags:=reverse_byte(IntfFlags);
  1116. {
  1117. ifDispatch, }
  1118. tcb.emit_ord_const(IntfFlags,u8inttype);
  1119. { write GUID }
  1120. tcb.emit_guid_const(def.iidguid^);
  1121. { write unit name }
  1122. tcb.emit_shortstring_const(current_module.realmodulename^);
  1123. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1124. targetinfos[target_info.system]^.alignment.recordalignmin,
  1125. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1126. { write iidstr }
  1127. if def.objecttype=odt_interfacecorba then
  1128. begin
  1129. { prepareguid always allocates an empty string }
  1130. if not assigned(def.iidstr) then
  1131. internalerror(2016021901);
  1132. tcb.emit_shortstring_const(def.iidstr^)
  1133. end;
  1134. { write published properties for this object }
  1135. published_properties_write_rtti_data(tcb,propnamelist,def.symtable);
  1136. tcb.end_anonymous_record;
  1137. tcb.end_anonymous_record;
  1138. propnamelist.free;
  1139. end;
  1140. begin
  1141. case def.objecttype of
  1142. odt_class:
  1143. tcb.emit_ord_const(tkclass,u8inttype);
  1144. odt_object:
  1145. tcb.emit_ord_const(tkobject,u8inttype);
  1146. odt_dispinterface,
  1147. odt_interfacecom:
  1148. tcb.emit_ord_const(tkInterface,u8inttype);
  1149. odt_interfacecorba:
  1150. tcb.emit_ord_const(tkinterfaceCorba,u8inttype);
  1151. odt_helper:
  1152. tcb.emit_ord_const(tkhelper,u8inttype);
  1153. else
  1154. internalerror(200611034);
  1155. end;
  1156. { generate the name }
  1157. tcb.emit_shortstring_const(def.objrealname^);
  1158. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1159. targetinfos[target_info.system]^.alignment.recordalignmin,
  1160. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1161. case rt of
  1162. initrtti :
  1163. begin
  1164. if def.objecttype in [odt_class,odt_object,odt_helper] then
  1165. objectdef_rtti_fields(def)
  1166. else
  1167. objectdef_rtti_interface_init(def);
  1168. end;
  1169. fullrtti :
  1170. begin
  1171. case def.objecttype of
  1172. odt_helper,
  1173. odt_class:
  1174. objectdef_rtti_class_full(def);
  1175. odt_object:
  1176. objectdef_rtti_fields(def);
  1177. else
  1178. objectdef_rtti_interface_full(def);
  1179. end;
  1180. end;
  1181. end;
  1182. tcb.end_anonymous_record;
  1183. end;
  1184. begin
  1185. case def.typ of
  1186. variantdef :
  1187. variantdef_rtti(tvariantdef(def));
  1188. stringdef :
  1189. stringdef_rtti(tstringdef(def));
  1190. enumdef :
  1191. enumdef_rtti(tenumdef(def));
  1192. orddef :
  1193. orddef_rtti(torddef(def));
  1194. floatdef :
  1195. floatdef_rtti(tfloatdef(def));
  1196. setdef :
  1197. setdef_rtti(tsetdef(def));
  1198. procvardef :
  1199. procvardef_rtti(tprocvardef(def));
  1200. arraydef :
  1201. begin
  1202. if ado_IsBitPacked in tarraydef(def).arrayoptions then
  1203. unknown_rtti(tstoreddef(def))
  1204. else
  1205. arraydef_rtti(tarraydef(def));
  1206. end;
  1207. recorddef :
  1208. begin
  1209. if trecorddef(def).is_packed then
  1210. unknown_rtti(tstoreddef(def))
  1211. else
  1212. recorddef_rtti(trecorddef(def));
  1213. end;
  1214. objectdef :
  1215. objectdef_rtti(tobjectdef(def));
  1216. classrefdef :
  1217. classrefdef_rtti(tclassrefdef(def));
  1218. pointerdef :
  1219. pointerdef_rtti(tpointerdef(def));
  1220. else
  1221. unknown_rtti(tstoreddef(def));
  1222. end;
  1223. end;
  1224. function enumsym_compare_name(item1, item2: pointer): Integer;
  1225. var
  1226. enum1: tenumsym absolute item1;
  1227. enum2: tenumsym absolute item2;
  1228. begin
  1229. if enum1=enum2 then
  1230. result:=0
  1231. else if enum1.name>enum2.name then
  1232. result:=1
  1233. else
  1234. { there can't be equal names, identifiers are unique }
  1235. result:=-1;
  1236. end;
  1237. function enumsym_compare_value(item1, item2: pointer): Integer;
  1238. var
  1239. enum1: tenumsym absolute item1;
  1240. enum2: tenumsym absolute item2;
  1241. begin
  1242. if enum1.value>enum2.value then
  1243. result:=1
  1244. else if enum1.value<enum2.value then
  1245. result:=-1
  1246. else
  1247. result:=0;
  1248. end;
  1249. procedure TRTTIWriter.write_rtti_extrasyms(def:Tdef;rt:Trttitype;mainrtti:Tasmsymbol);
  1250. type Penumsym = ^Tenumsym;
  1251. { Writes a helper table for accelerated conversion of ordinal enum values to strings.
  1252. If you change something in this method, make sure to adapt the corresponding code
  1253. in sstrings.inc. }
  1254. procedure enumdef_rtti_ord2stringindex(rttidef: trecorddef; const syms: tfplist);
  1255. var rttilab:Tasmsymbol;
  1256. h,i,o,prev_value:longint;
  1257. mode:(lookup,search); {Modify with care, ordinal value of enum is written.}
  1258. r:single; {Must be real type because of integer overflow risk.}
  1259. tcb: ttai_typedconstbuilder;
  1260. sym_count: integer;
  1261. tabledef: tdef;
  1262. begin
  1263. {Decide wether a lookup array is size efficient.}
  1264. mode:=lookup;
  1265. sym_count:=syms.count;
  1266. if sym_count>0 then
  1267. begin
  1268. i:=1;
  1269. r:=0;
  1270. h:=tenumsym(syms[0]).value; {Next expected enum value is min.}
  1271. { set prev_value for the first iteration to a value that is
  1272. different from the first one without risking overflow (it's used
  1273. to detect whether two enum values are the same) }
  1274. if h=0 then
  1275. prev_value:=1
  1276. else
  1277. prev_value:=0;
  1278. while i<sym_count do
  1279. begin
  1280. { if two enum values are the same, we have to create a table }
  1281. if (prev_value=h) then
  1282. begin
  1283. mode:=search;
  1284. break;
  1285. end;
  1286. {Calculate size of hole between values. Avoid integer overflows.}
  1287. r:=r+(single(tenumsym(syms[i]).value)-single(h))-1;
  1288. prev_value:=h;
  1289. h:=tenumsym(syms[i]).value;
  1290. inc(i);
  1291. end;
  1292. if r>sym_count then
  1293. mode:=search; {Don't waste more than 50% space.}
  1294. end;
  1295. { write rtti data; make sure that the alignment matches the corresponding data structure
  1296. in the code that uses it (if alignment is required). }
  1297. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable]);
  1298. { use TConstPtrUInt packrecords to ensure good alignment }
  1299. tcb.begin_anonymous_record('',defaultpacking,reqalign,
  1300. targetinfos[target_info.system]^.alignment.recordalignmin,
  1301. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1302. { now emit the data: first the mode }
  1303. tcb.emit_tai(Tai_const.create_32bit(longint(mode)),u32inttype);
  1304. { align }
  1305. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,sizeof(pointer)),
  1306. targetinfos[target_info.system]^.alignment.recordalignmin,
  1307. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1308. if mode=lookup then
  1309. begin
  1310. o:=tenumsym(syms[0]).value; {Start with min value.}
  1311. for i:=0 to sym_count-1 do
  1312. begin
  1313. while o<tenumsym(syms[i]).value do
  1314. begin
  1315. tcb.emit_tai(Tai_const.create_nil_dataptr,ptruinttype);
  1316. inc(o);
  1317. end;
  1318. inc(o);
  1319. tcb.queue_init(voidpointertype);
  1320. tcb.queue_subscriptn_multiple_by_name(rttidef,
  1321. ['size_start_rec',
  1322. 'min_max_rec',
  1323. 'basetype_array_rec',
  1324. tsym(syms[i]).Name]
  1325. );
  1326. tcb.queue_emit_asmsym(mainrtti,rttidef);
  1327. end;
  1328. end
  1329. else
  1330. begin
  1331. tcb.emit_ord_const(sym_count,u32inttype);
  1332. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,sizeof(pointer)),
  1333. targetinfos[target_info.system]^.alignment.recordalignmin,
  1334. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1335. for i:=0 to sym_count-1 do
  1336. begin
  1337. tcb.emit_ord_const(tenumsym(syms[i]).value,s32inttype);
  1338. tcb.queue_init(voidpointertype);
  1339. tcb.queue_subscriptn_multiple_by_name(rttidef,
  1340. ['size_start_rec',
  1341. 'min_max_rec',
  1342. 'basetype_array_rec',
  1343. tsym(syms[i]).Name]
  1344. );
  1345. tcb.queue_emit_asmsym(mainrtti,rttidef);
  1346. end;
  1347. tcb.end_anonymous_record;
  1348. end;
  1349. tcb.end_anonymous_record;
  1350. tabledef:=tcb.end_anonymous_record;
  1351. rttilab:=current_asmdata.DefineAsmSymbol(Tstoreddef(def).rtti_mangledname(rt)+'_o2s',AB_GLOBAL,AT_DATA_FORCEINDIRECT,tabledef);
  1352. current_asmdata.asmlists[al_rtti].concatlist(tcb.get_final_asmlist(
  1353. rttilab,tabledef,sec_rodata,
  1354. rttilab.name,sizeof(pint)));
  1355. tcb.free;
  1356. current_module.add_public_asmsym(rttilab);
  1357. end;
  1358. { Writes a helper table for accelerated conversion of string to ordinal enum values.
  1359. If you change something in this method, make sure to adapt the corresponding code
  1360. in sstrings.inc. }
  1361. procedure enumdef_rtti_string2ordindex(rttidef: trecorddef; const syms: tfplist);
  1362. var
  1363. tcb: ttai_typedconstbuilder;
  1364. rttilab: Tasmsymbol;
  1365. i:longint;
  1366. tabledef: tdef;
  1367. begin
  1368. { write rtti data }
  1369. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable]);
  1370. { begin of Tstring_to_ord }
  1371. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,sizeof(pointer)),
  1372. targetinfos[target_info.system]^.alignment.recordalignmin,
  1373. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1374. tcb.emit_ord_const(syms.count,s32inttype);
  1375. { begin of "data" array in Tstring_to_ord }
  1376. tcb.begin_anonymous_record('',defaultpacking,min(reqalign,sizeof(pointer)),
  1377. targetinfos[target_info.system]^.alignment.recordalignmin,
  1378. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1379. for i:=0 to syms.count-1 do
  1380. begin
  1381. tcb.emit_ord_const(tenumsym(syms[i]).value,s32inttype);
  1382. { alignment of pointer value handled by enclosing record already }
  1383. tcb.queue_init(voidpointertype);
  1384. tcb.queue_subscriptn_multiple_by_name(rttidef,
  1385. ['size_start_rec',
  1386. 'min_max_rec',
  1387. 'basetype_array_rec',
  1388. tsym(syms[i]).Name]
  1389. );
  1390. tcb.queue_emit_asmsym(mainrtti,rttidef);
  1391. end;
  1392. tcb.end_anonymous_record;
  1393. tabledef:=tcb.end_anonymous_record;
  1394. rttilab:=current_asmdata.DefineAsmSymbol(Tstoreddef(def).rtti_mangledname(rt)+'_s2o',AB_GLOBAL,AT_DATA_FORCEINDIRECT,tabledef);
  1395. current_asmdata.asmlists[al_rtti].concatlist(tcb.get_final_asmlist(
  1396. rttilab,tabledef,sec_rodata,
  1397. rttilab.name,sizeof(pint)));
  1398. tcb.free;
  1399. current_module.add_public_asmsym(rttilab);
  1400. end;
  1401. procedure enumdef_rtti_extrasyms(def:Tenumdef);
  1402. var
  1403. t:Tenumsym;
  1404. syms:tfplist;
  1405. i:longint;
  1406. rttitypesym: ttypesym;
  1407. rttidef: trecorddef;
  1408. begin
  1409. { collect enumsyms belonging to this enum type (could be a subsection
  1410. in case of a subrange type) }
  1411. syms:=tfplist.create;
  1412. for i := 0 to def.symtable.SymList.Count - 1 do
  1413. begin
  1414. t:=tenumsym(def.symtable.SymList[i]);
  1415. if t.value<def.minval then
  1416. continue
  1417. else
  1418. if t.value>def.maxval then
  1419. break;
  1420. syms.add(t);
  1421. end;
  1422. { sort the syms by enum name }
  1423. syms.sort(@enumsym_compare_name);
  1424. rttitypesym:=try_search_current_module_type(internaltypeprefixName[itp_rttidef]+def.rtti_mangledname(fullrtti));
  1425. if not assigned(rttitypesym) or
  1426. (ttypesym(rttitypesym).typedef.typ<>recorddef) then
  1427. internalerror(2015071402);
  1428. rttidef:=trecorddef(ttypesym(rttitypesym).typedef);
  1429. enumdef_rtti_string2ordindex(rttidef,syms);
  1430. { sort the syms by enum value }
  1431. syms.sort(@enumsym_compare_value);
  1432. enumdef_rtti_ord2stringindex(rttidef,syms);
  1433. syms.free;
  1434. end;
  1435. begin
  1436. case def.typ of
  1437. enumdef:
  1438. if rt=fullrtti then
  1439. begin
  1440. enumdef_rtti_extrasyms(Tenumdef(def));
  1441. end;
  1442. end;
  1443. end;
  1444. procedure TRTTIWriter.write_child_rtti_data(def:tdef;rt:trttitype);
  1445. begin
  1446. case def.typ of
  1447. enumdef :
  1448. if assigned(tenumdef(def).basedef) then
  1449. write_rtti(tenumdef(def).basedef,rt);
  1450. setdef :
  1451. write_rtti(tsetdef(def).elementdef,rt);
  1452. arraydef :
  1453. begin
  1454. write_rtti(tarraydef(def).rangedef,rt);
  1455. write_rtti(tarraydef(def).elementdef,rt);
  1456. end;
  1457. recorddef :
  1458. fields_write_rtti(trecorddef(def).symtable,rt);
  1459. objectdef :
  1460. begin
  1461. if assigned(tobjectdef(def).childof) then
  1462. write_rtti(tobjectdef(def).childof,rt);
  1463. if (rt=initrtti) or (tobjectdef(def).objecttype=odt_object) then
  1464. fields_write_rtti(tobjectdef(def).symtable,rt)
  1465. else
  1466. published_write_rtti(tobjectdef(def).symtable,rt);
  1467. end;
  1468. classrefdef,
  1469. pointerdef:
  1470. if not is_objc_class_or_protocol(tabstractpointerdef(def).pointeddef) then
  1471. write_rtti(tabstractpointerdef(def).pointeddef,rt);
  1472. procvardef:
  1473. params_write_rtti(tabstractprocdef(def),rt);
  1474. end;
  1475. end;
  1476. procedure TRTTIWriter.write_rtti_reference(tcb: ttai_typedconstbuilder; def: tdef; rt: trttitype);
  1477. begin
  1478. { we don't care about the real type here, because
  1479. a) we don't index into these elements
  1480. b) we may not have the rtti type available at the point that we
  1481. are emitting this data, because of forward definitions etc
  1482. c) if the rtti is emitted in another unit, we won't have the type
  1483. available at all
  1484. For the cases where the type is emitted in the current unit and hence
  1485. the underlying system will detect and complain about symbol def
  1486. mismatches, type conversions will have to be inserted afterwards (like
  1487. in llvm/llvmtype)
  1488. }
  1489. if not assigned(def) or is_void(def) or ((rt<>initrtti) and is_objc_class_or_protocol(def)) then
  1490. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype)
  1491. else
  1492. tcb.emit_tai(Tai_const.Create_sym(ref_rtti(def,rt,true)),voidpointertype);
  1493. end;
  1494. function TRTTIWriter.ref_rtti(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  1495. var
  1496. s : TSymStr;
  1497. begin
  1498. s:=def.rtti_mangledname(rt);
  1499. result:=current_asmdata.RefAsmSymbol(s,AT_DATA,indirect);
  1500. if (cs_create_pic in current_settings.moduleswitches) and
  1501. assigned(current_procinfo) then
  1502. include(current_procinfo.flags,pi_needs_got);
  1503. if def.owner.moduleid<>current_module.moduleid then
  1504. current_module.add_extern_asmsym(s,AB_EXTERNAL,AT_DATA);
  1505. end;
  1506. procedure TRTTIWriter.write_rtti(def:tdef;rt:trttitype);
  1507. var
  1508. tcb: ttai_typedconstbuilder;
  1509. rttilab: tasmsymbol;
  1510. rttidef: tdef;
  1511. begin
  1512. { only write rtti of definitions from the current module }
  1513. if not findunitsymtable(def.owner).iscurrentunit then
  1514. exit;
  1515. { check if separate initrtti is actually needed }
  1516. if (rt=initrtti) and (not def.needs_separate_initrtti) then
  1517. rt:=fullrtti;
  1518. { prevent recursion }
  1519. if rttidefstate[rt] in def.defstates then
  1520. exit;
  1521. include(def.defstates,rttidefstate[rt]);
  1522. { write first all dependencies }
  1523. write_child_rtti_data(def,rt);
  1524. { write rtti data }
  1525. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable]);
  1526. tcb.begin_anonymous_record(
  1527. internaltypeprefixName[itp_rttidef]+tstoreddef(def).rtti_mangledname(rt),
  1528. defaultpacking,reqalign,
  1529. targetinfos[target_info.system]^.alignment.recordalignmin,
  1530. targetinfos[target_info.system]^.alignment.maxCrecordalign
  1531. );
  1532. write_rtti_data(tcb,def,rt);
  1533. rttidef:=tcb.end_anonymous_record;
  1534. rttilab:=current_asmdata.DefineAsmSymbol(tstoreddef(def).rtti_mangledname(rt),AB_GLOBAL,AT_DATA_FORCEINDIRECT,rttidef);
  1535. current_asmdata.AsmLists[al_rtti].concatList(
  1536. tcb.get_final_asmlist(rttilab,rttidef,sec_rodata,rttilab.name,min(target_info.alignment.maxCrecordalign,SizeOf(QWord))));
  1537. tcb.free;
  1538. current_module.add_public_asmsym(rttilab);
  1539. { write additional data }
  1540. write_rtti_extrasyms(def,rt,rttilab);
  1541. end;
  1542. constructor TRTTIWriter.create;
  1543. begin
  1544. if tf_requires_proper_alignment in target_info.flags then
  1545. begin
  1546. reqalign:=min(sizeof(qword),target_info.alignment.maxCrecordalign);
  1547. defaultpacking:=C_alignment;
  1548. end
  1549. else
  1550. begin
  1551. reqalign:=1;
  1552. defaultpacking:=1;
  1553. end;
  1554. end;
  1555. function TRTTIWriter.get_rtti_label(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  1556. var
  1557. name : tsymstr;
  1558. begin
  1559. name:=def.rtti_mangledname(rt);
  1560. result:=current_asmdata.RefAsmSymbol(name,AT_DATA,indirect);
  1561. if (cs_create_pic in current_settings.moduleswitches) and
  1562. assigned(current_procinfo) then
  1563. include(current_procinfo.flags,pi_needs_got);
  1564. if assigned(current_module) and (findunitsymtable(def.owner).moduleid<>current_module.moduleid) then
  1565. current_module.add_extern_asmsym(name,AB_EXTERNAL,AT_DATA);
  1566. end;
  1567. function TRTTIWriter.get_rtti_label_ord2str(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  1568. var
  1569. name : tsymstr;
  1570. begin
  1571. name:=def.rtti_mangledname(rt)+'_o2s';
  1572. result:=current_asmdata.RefAsmSymbol(name,AT_DATA,indirect);
  1573. if (cs_create_pic in current_settings.moduleswitches) and
  1574. assigned(current_procinfo) then
  1575. include(current_procinfo.flags,pi_needs_got);
  1576. if assigned(current_module) and (findunitsymtable(def.owner).moduleid<>current_module.moduleid) then
  1577. current_module.add_extern_asmsym(name,AB_EXTERNAL,AT_DATA);
  1578. end;
  1579. function TRTTIWriter.get_rtti_label_str2ord(def:tdef;rt:trttitype;indirect:boolean):tasmsymbol;
  1580. var
  1581. name : tsymstr;
  1582. begin
  1583. name:=def.rtti_mangledname(rt)+'_s2o';
  1584. result:=current_asmdata.RefAsmSymbol(name,AT_DATA,indirect);
  1585. if (cs_create_pic in current_settings.moduleswitches) and
  1586. assigned(current_procinfo) then
  1587. include(current_procinfo.flags,pi_needs_got);
  1588. if assigned(current_module) and (findunitsymtable(def.owner).moduleid<>current_module.moduleid) then
  1589. current_module.add_extern_asmsym(name,AB_EXTERNAL,AT_DATA);
  1590. end;
  1591. end.