objcgutl.pas 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932
  1. {
  2. Copyright (c) 2009 by Jonas Maebe
  3. This unit implements some Objective-C helper routines at the code generator
  4. level.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License,or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {$i fpcdefs.inc}
  19. unit objcgutl;
  20. interface
  21. uses
  22. cclasses,
  23. aasmbase,aasmdata,
  24. symbase,symdef;
  25. procedure objcfinishstringrefpoolentry(entry: phashsetitem; stringpool: tconstpooltype; refsec, stringsec: tasmsectiontype);
  26. procedure objcfinishclassrefnfpoolentry(entry: phashsetitem; classdef: tobjectdef);
  27. procedure MaybeGenerateObjectiveCImageInfo(globalst, localst: tsymtable);
  28. implementation
  29. uses
  30. globtype,globals,fmodule,cutils,
  31. systems,
  32. aasmtai,
  33. cgbase,
  34. objcdef,objcutil,
  35. aasmcnst,
  36. symconst,symtype,symsym,symtable,
  37. ngenutil,
  38. verbose;
  39. type
  40. tobjcabi = (oa_fragile, oa_nonfragile);
  41. (* tivarlayouttype = (il_weak,il_strong); *)
  42. tobjcrttiwriter = class
  43. protected
  44. fabi: tobjcabi;
  45. classdefs,
  46. catdefs: tfpobjectlist;
  47. classrttidefs,
  48. catrttidefs: tfpobjectlist;
  49. classsyms,
  50. catsyms: tfpobjectlist;
  51. procedure gen_objc_methods(list: tasmlist; objccls: tobjectdef; out methodslabel: tasmsymbol; classmethods, iscategory: Boolean);
  52. procedure gen_objc_protocol_elements(list: tasmlist; protocol: tobjectdef; out reqinstsym, optinstsym, reqclssym, optclssym: TAsmLabel);
  53. procedure gen_objc_protocol_list(list:TAsmList; protolist: TFPObjectList; out protolistsym: TAsmLabel);
  54. procedure gen_objc_cat_methods(list:TAsmList; items: TFPObjectList; section: tasmsectiontype;const sectname: string; out listsym: TAsmLabel);
  55. procedure gen_objc_protocol(list:TAsmList; protocol: tobjectdef; out protocollabel: TAsmSymbol);virtual;abstract;
  56. procedure gen_objc_category_sections(list:TAsmList; objccat: tobjectdef; out catlabel: TAsmSymbol; out catlabeldef: tdef);virtual;abstract;
  57. procedure gen_objc_classes_sections(list:TAsmList; objclss: tobjectdef; out classlabel: TAsmSymbol; out classlabeldef: tdef);virtual;abstract;
  58. procedure gen_objc_info_sections(list: tasmlist);virtual;abstract;
  59. public
  60. constructor create(_abi: tobjcabi);
  61. destructor destroy;override;
  62. procedure gen_objc_rtti_sections(list:TAsmList; st:TSymtable);
  63. property abi: tobjcabi read fabi;
  64. end;
  65. { Used by by PowerPC/32 and i386 }
  66. tobjcrttiwriter_fragile = class(tobjcrttiwriter)
  67. protected
  68. function gen_objc_protocol_ext(list: TAsmList; optinstsym, optclssym: TAsmLabel): TAsmLabel;
  69. procedure gen_objc_ivars(list: TAsmList; objccls: tobjectdef; out ivarslabel: TAsmLabel);
  70. procedure gen_objc_protocol(list:TAsmList; protocol: tobjectdef; out protocollabel: TAsmSymbol);override;
  71. procedure gen_objc_category_sections(list:TAsmList; objccat: tobjectdef; out catlabel: TAsmSymbol; out catlabeldef: tdef);override;
  72. procedure gen_objc_classes_sections(list:TAsmList; objclss: tobjectdef; out classlabel: TAsmSymbol; out classlabeldef: tdef);override;
  73. procedure gen_objc_info_sections(list: tasmlist);override;
  74. public
  75. constructor create;
  76. end;
  77. { Used by PowerPC/64, ARM, x86_64 and AArch64 }
  78. tobjcrttiwriter_nonfragile = class(tobjcrttiwriter)
  79. protected
  80. ObjCEmptyCacheVar,
  81. ObjCEmptyVtableVar: TAsmSymbol;
  82. procedure gen_objc_class_ro_part(list: TAsmList; objclss: tobjectdef; protolistsym: TAsmSymbol; out classrolabel: TAsmSymbol; metaclass: boolean);
  83. procedure addclasslist(list: tasmlist; section: tasmsectiontype; const symname: string; classes: tfpobjectlist);
  84. procedure gen_objc_ivars(list: TAsmList; objccls: tobjectdef; out ivarslabel: TAsmLabel);
  85. procedure gen_objc_protocol(list:TAsmList; protocol: tobjectdef; out protocollabel: TAsmSymbol);override;
  86. procedure gen_objc_category_sections(list:TAsmList; objccat: tobjectdef; out catlabel: TAsmSymbol; out catlabeldef: tdef);override;
  87. procedure gen_objc_classes_sections(list:TAsmList; objclss: tobjectdef; out classlabel: TAsmSymbol; out classlabeldef: tdef);override;
  88. procedure gen_objc_info_sections(list: tasmlist);override;
  89. public
  90. constructor create;
  91. end;
  92. {******************************************************************
  93. Protocol declaration helpers
  94. *******************************************************************}
  95. function objcfindprotocolentry(const p: shortstring): TAsmSymbol;
  96. var
  97. item : PHashSetItem;
  98. begin
  99. result:=nil;
  100. if not assigned(current_asmdata.ConstPools[sp_objcprotocolrefs]) then
  101. exit;
  102. item:=current_asmdata.constpools[sp_objcprotocolrefs].Find(@p[1], length(p));
  103. if not assigned(item) then
  104. exit;
  105. result:=TAsmSymbol(item^.Data);
  106. end;
  107. function objcaddprotocolentry(const p: shortstring; ref: TAsmSymbol): Boolean;
  108. var
  109. item : PHashSetItem;
  110. begin
  111. item:=current_asmdata.constpools[sp_objcprotocolrefs].FindOrAdd(@p[1], length(p));
  112. Result:=(item^.Data=nil);
  113. if Result then
  114. item^.Data:=ref;
  115. end;
  116. {******************************************************************
  117. Pool section helpers
  118. *******************************************************************}
  119. procedure objcreatestringpoolentryintern(p: pchar; len: longint; pooltype: tconstpooltype; stringsec: tasmsectiontype; out sym: TAsmLabel; out def: tdef);
  120. var
  121. entry : PHashSetItem;
  122. strlab : tasmlabel;
  123. pc : pchar;
  124. pool : THashSet;
  125. tcb : ttai_typedconstbuilder;
  126. begin
  127. pool := current_asmdata.constpools[pooltype];
  128. entry:=pool.FindOrAdd(p,len);
  129. if not assigned(entry^.data) then
  130. begin
  131. { create new entry }
  132. current_asmdata.getlabel(strlab,alt_data);
  133. entry^.Data:=strlab;
  134. { Make sure strlab has a reference }
  135. strlab.increfs;
  136. getmem(pc,entry^.keylength+1);
  137. move(entry^.key^,pc^,entry^.keylength);
  138. pc[entry^.keylength]:=#0;
  139. { add the string to the approriate section }
  140. tcb:=ctai_typedconstbuilder.create([tcalo_is_lab,tcalo_new_section]);
  141. def:=tcb.emit_pchar_const(pc,entry^.keylength,false);
  142. current_asmdata.asmlists[al_objc_pools].concatList(
  143. tcb.get_final_asmlist(strlab,def,stringsec,strlab.name,1)
  144. );
  145. tcb.free;
  146. def:=cpointerdef.getreusable(def);
  147. end
  148. else
  149. def:=cpointerdef.getreusable(carraydef.getreusable(cansichartype,len+1));
  150. sym:=TAsmLabel(Entry^.Data);
  151. end;
  152. procedure objcfinishstringrefpoolentry(entry: phashsetitem; stringpool: tconstpooltype; refsec, stringsec: tasmsectiontype);
  153. var
  154. reflab,
  155. strlab : tasmlabel;
  156. classname: string;
  157. tcb: ttai_typedconstbuilder;
  158. strdef: tdef;
  159. begin
  160. { have we already generated a reference for this string entry? }
  161. if not assigned(entry^.Data) then
  162. begin
  163. { no, add the string to the associated strings section }
  164. objcreatestringpoolentryintern(pchar(entry^.key),entry^.keylength,stringpool,stringsec,strlab,strdef);
  165. { and now finish the reference }
  166. current_asmdata.getlabel(reflab,alt_data);
  167. entry^.Data:=reflab;
  168. { add a pointer to the string in the string references section }
  169. tcb:=ctai_typedconstbuilder.create([tcalo_is_lab,tcalo_new_section,tcalo_no_dead_strip]);
  170. tcb.emit_tai(Tai_const.Create_sym(strlab),strdef);
  171. current_asmdata.asmlists[al_objc_pools].concatList(
  172. tcb.get_final_asmlist(reflab,strdef,refsec,reflab.name,sizeof(pint))
  173. );
  174. tcb.free;
  175. { in case of a class reference, also add a lazy symbol reference for
  176. the class (the linker requires this for the fragile ABI). }
  177. if (refsec=sec_objc_cls_refs) and
  178. not(target_info.system in systems_objc_nfabi) then
  179. begin
  180. classname:='';
  181. setlength(classname,entry^.keylength);
  182. move(entry^.key^,classname[1],entry^.keylength);
  183. { no way to express this in LLVM either, they also just emit
  184. module level assembly for it }
  185. current_asmdata.asmlists[al_pure_assembler].concat(tai_directive.Create(asd_lazy_reference,'.objc_class_name_'+classname));
  186. end;
  187. end;
  188. end;
  189. procedure objcreatestringpoolentry(const s: string; pooltype: tconstpooltype; stringsec: tasmsectiontype; out sym: TAsmLabel; out def: tdef);
  190. begin
  191. objcreatestringpoolentryintern(@s[1],length(s),pooltype,stringsec,sym,def);
  192. end;
  193. procedure objcfinishclassrefnfpoolentry(entry: phashsetitem; classdef: tobjectdef);
  194. var
  195. reflab: TAsmLabel;
  196. classym: TasmSymbol;
  197. tcb: ttai_typedconstbuilder;
  198. begin
  199. { have we already generated a reference for this class ref entry? }
  200. if not assigned(entry^.Data) then
  201. begin
  202. { no, add the classref to the sec_objc_cls_refs section }
  203. current_asmdata.getlabel(reflab,alt_data);
  204. entry^.Data:=reflab;
  205. { add a pointer to the class }
  206. classym:=current_asmdata.RefAsmSymbol(classdef.rtti_mangledname(objcclassrtti),AT_DATA);
  207. tcb:=ctai_typedconstbuilder.create([tcalo_is_lab,tcalo_new_section]);
  208. tcb.emit_tai(Tai_const.Create_sym(classym),voidpointertype);
  209. current_asmdata.asmlists[al_objc_pools].concatList(
  210. tcb.get_final_asmlist(reflab,voidpointertype,sec_objc_cls_refs,reflab.name,sizeof(pint))
  211. );
  212. tcb.free;
  213. end;
  214. end;
  215. {******************************************************************
  216. RTTI generation -- Helpers
  217. *******************************************************************}
  218. procedure ConcatSymOrNil(tcb: ttai_typedconstbuilder; sym: TAsmSymbol; def: tdef); inline;
  219. begin
  220. if Assigned(sym) then
  221. tcb.emit_tai(tai_const.Create_sym(sym),def)
  222. else
  223. tcb.emit_tai(tai_const.Create_nil_dataptr,def);
  224. end;
  225. {******************************************************************
  226. RTTI generation -- Common
  227. *******************************************************************}
  228. { generate a method list, either of class methods or of instance methods,
  229. and both for obj-c classes and categories. }
  230. procedure tobjcrttiwriter.gen_objc_methods(list: tasmlist; objccls: tobjectdef; out methodslabel: tasmsymbol; classmethods, iscategory: Boolean);
  231. const
  232. {clas/cat inst/cls}
  233. SectType : array [Boolean, Boolean] of tasmsectiontype =
  234. ((sec_objc_inst_meth, sec_objc_cls_meth),
  235. (sec_objc_cat_inst_meth, sec_objc_cat_cls_meth));
  236. {clas/cat inst/cls}
  237. SectName : array [Boolean, Boolean] of string[20] =
  238. (('_OBJC_INST_METH','_OBJC_CLS_METH'),
  239. ('_OBJC_CAT_INST_METH','_OBJC_CAT_CLS_METH'));
  240. {frag/non-frag ABI}
  241. SectFlags : array [Boolean] of ttcasmlistoptions =
  242. ([tcalo_new_section],[tcalo_new_section,tcalo_no_dead_strip]);
  243. {inst/cls}
  244. instclsName : array [Boolean] of string = ('INSTANCE','CLASS');
  245. type
  246. method_data = record
  247. def : tprocdef;
  248. selsym : TAsmLabel;
  249. seldef : tdef;
  250. encsym : TAsmLabel;
  251. encdef : tdef;
  252. end;
  253. var
  254. i : Integer;
  255. def : tprocdef;
  256. defs : array of method_data;
  257. mcnt : integer;
  258. mtype : tdef;
  259. tcb : ttai_typedconstbuilder;
  260. mdef : tdef;
  261. begin
  262. methodslabel:=nil;
  263. mcnt:=0;
  264. defs:=nil;
  265. { collect all instance/class methods }
  266. SetLength(defs,objccls.vmtentries.count);
  267. for i:=0 to objccls.vmtentries.count-1 do
  268. begin
  269. def:=pvmtentry(objccls.vmtentries[i])^.procdef;
  270. if (def.owner.defowner=objccls) and
  271. (classmethods = (po_classmethod in def.procoptions)) then
  272. begin
  273. defs[mcnt].def:=def;
  274. objcreatestringpoolentry(def.messageinf.str^,sp_objcvarnames,sec_objc_meth_var_names,defs[mcnt].selsym,defs[mcnt].seldef);
  275. objcreatestringpoolentry(objcencodemethod(def),sp_objcvartypes,sec_objc_meth_var_types,defs[mcnt].encsym,defs[mcnt].encdef);
  276. inc(mcnt);
  277. end;
  278. end;
  279. if mcnt=0 then
  280. exit;
  281. tcb:=ctai_typedconstbuilder.create(SectFlags[target_info.system in systems_objc_nfabi]);
  282. tcb.begin_anonymous_record(internaltypeprefixName[itp_objc_method_list]+tostr(mcnt),
  283. C_alignment,1,
  284. targetinfos[target_info.system]^.alignment.recordalignmin);
  285. if (abi=oa_fragile) then
  286. { not used, always zero }
  287. tcb.emit_ord_const(0,u32inttype)
  288. else
  289. begin
  290. { size of each entry -- always 32 bit value }
  291. mtype:=search_named_unit_globaltype('OBJC','OBJC_METHOD',true).typedef;
  292. tcb.emit_ord_const(mtype.size,u32inttype);
  293. end;
  294. { number of objc_method entries in the method_list array -- always 32 bit}
  295. tcb.emit_ord_const(mcnt,u32inttype);
  296. for i:=0 to mcnt-1 do
  297. begin
  298. { reference to the selector name }
  299. tcb.queue_init(charpointertype);
  300. tcb.queue_emit_asmsym(defs[i].selsym,defs[i].seldef);
  301. { reference to the obj-c encoded function parameters (signature) }
  302. tcb.queue_init(charpointertype);
  303. tcb.queue_emit_asmsym(defs[i].encsym,defs[i].encdef);
  304. { mangled name of the method }
  305. tcb.queue_init(voidcodepointertype);
  306. tcb.queue_emit_proc(defs[i].def);
  307. end;
  308. mdef:=tcb.end_anonymous_record;
  309. if iscategory then
  310. begin
  311. methodslabel:=current_asmdata.DefineAsmSymbol('l_OBJC_$_CATEGORY_'+instclsName[classmethods]+'_METHODS_'+objccls.objextname^+'_$_'+objccls.childof.objextname^,AB_LOCAL,AT_DATA,mdef);
  312. end
  313. else
  314. begin
  315. methodslabel:=current_asmdata.DefineAsmSymbol('l_OBJC_$_'+instclsName[classmethods]+'_METHODS_'+objccls.objextname^,AB_LOCAL,AT_DATA,mdef);
  316. end;
  317. list.concatList(
  318. tcb.get_final_asmlist(methodslabel,mdef,
  319. SectType[iscategory,classmethods],
  320. SectName[iscategory,classmethods],sizeof(ptrint)
  321. )
  322. );
  323. tcb.free;
  324. end;
  325. { generate method (and in the future also property) info for protocols }
  326. procedure tobjcrttiwriter.gen_objc_protocol_elements(list: tasmlist; protocol: tobjectdef; out reqinstsym, optinstsym, reqclssym, optclssym: TAsmLabel);
  327. var
  328. proc : tprocdef;
  329. reqinstmlist,
  330. reqclsmlist,
  331. optinstmlist,
  332. optclsmlist : TFPObjectList;
  333. i : ptrint;
  334. begin
  335. reqinstmlist:=TFPObjectList.Create(false);
  336. reqclsmlist:=TFPObjectList.Create(false);
  337. optinstmlist:=TFPObjectList.Create(false);
  338. optclsmlist:=TFPObjectList.Create(false);
  339. for i:=0 to protocol.vmtentries.Count-1 do
  340. begin
  341. proc:=pvmtentry(protocol.vmtentries[i])^.procdef;
  342. if (po_classmethod in proc.procoptions) then
  343. if not(po_optional in proc.procoptions) then
  344. reqclsmlist.Add(proc)
  345. else
  346. optclsmlist.Add(proc)
  347. else if not(po_optional in proc.procoptions) then
  348. reqinstmlist.Add(proc)
  349. else
  350. optinstmlist.Add(proc);
  351. end;
  352. if reqinstmlist.Count > 0 then
  353. gen_objc_cat_methods(list,reqinstmlist,sec_objc_cat_inst_meth,'_OBJC_CAT_INST_METH',reqinstsym)
  354. else
  355. reqinstsym:=nil;
  356. if optinstmlist.Count > 0 then
  357. gen_objc_cat_methods(list,optinstmlist,sec_objc_cat_inst_meth,'_OBJC_CAT_INST_METH',optinstsym)
  358. else
  359. optinstsym:=nil;
  360. if reqclsmlist.Count>0 then
  361. gen_objc_cat_methods(list,reqclsmlist,sec_objc_cat_cls_meth,'_OBJC_CAT_CLS_METH',reqclssym)
  362. else
  363. reqclssym:=nil;
  364. if optclsmlist.Count>0 then
  365. gen_objc_cat_methods(list,optclsmlist,sec_objc_cat_cls_meth,'_OBJC_CAT_CLS_METH',optclssym)
  366. else
  367. optclssym:=nil;
  368. reqinstmlist.Free;
  369. reqclsmlist.Free;
  370. optinstmlist.Free;
  371. optclsmlist.Free;
  372. end;
  373. (*
  374. From CLang:
  375. struct objc_protocol_list
  376. {
  377. #ifdef FRAGILE_ABI
  378. struct objc_protocol_list *next;
  379. int count;
  380. #else
  381. long count;
  382. #endif
  383. Protocol *list[1];
  384. };
  385. *)
  386. procedure tobjcrttiwriter.gen_objc_protocol_list(list: tasmlist; protolist: tfpobjectlist; out protolistsym: tasmlabel);
  387. var
  388. i : Integer;
  389. protosym : TAsmSymbol;
  390. protodef : tobjectdef;
  391. tcb : ttai_typedconstbuilder;
  392. begin
  393. if not Assigned(protolist) or
  394. (protolist.Count=0) then
  395. begin
  396. protolistsym:=nil;
  397. Exit;
  398. end;
  399. for i:=0 to protolist.Count-1 do
  400. begin
  401. protodef:=TImplementedInterface(protolist[i]).IntfDef;
  402. protosym:=objcfindprotocolentry(protodef.objextname^);
  403. if not assigned(protosym) then
  404. begin
  405. gen_objc_protocol(list,protodef,protosym);
  406. objcaddprotocolentry(protodef.objextname^,protosym);
  407. end;
  408. end;
  409. tcb:=ctai_typedconstbuilder.create([tcalo_is_lab,tcalo_new_section]);
  410. tcb.begin_anonymous_record(internaltypeprefixName[itp_objc_proto_list]+tostr(protolist.Count),
  411. C_alignment,1,
  412. targetinfos[target_info.system]^.alignment.recordalignmin);
  413. { protocol lists are stored in .objc_cat_cls_meth section }
  414. current_asmdata.getlabel(protolistsym, alt_data);
  415. if (abi=oa_fragile) then
  416. { From Clang: next, always nil}
  417. tcb.emit_tai(tai_const.Create_nil_dataptr,ptruinttype);
  418. { From Clang: protocols count}
  419. tcb.emit_tai(Tai_const.Create_int_dataptr(protolist.Count),ptruinttype);
  420. for i:=0 to protolist.Count-1 do
  421. begin
  422. protodef:=(protolist[i] as TImplementedInterface).IntfDef;
  423. protosym:=objcfindprotocolentry(protodef.objextname^);
  424. if not Assigned(protosym) then
  425. begin
  426. { For some reason protosym is not declared, though must be!
  427. Probably gen_obcj1_protocol returned wrong protosym
  428. }
  429. InternalError(2009091602);
  430. end;
  431. tcb.emit_tai(tai_const.Create_sym(protosym),voidpointertype);
  432. end;
  433. list.concatList(
  434. tcb.get_final_asmlist(
  435. protolistsym,tcb.end_anonymous_record,
  436. sec_objc_cat_cls_meth,'_OBJC_PROTOCOLLIST',sizeof(pint)
  437. )
  438. );
  439. tcb.free;
  440. { the symbol will point to a record }
  441. end;
  442. { Generate rtti for an Objective-C methods (methods without implementation) }
  443. { items : TFPObjectList of Tprocdef }
  444. procedure tobjcrttiwriter.gen_objc_cat_methods(list:TAsmList; items: TFPObjectList; section: tasmsectiontype;
  445. const sectname: string; out listsym: TAsmLabel);
  446. var
  447. i : integer;
  448. m : tprocdef;
  449. lab : tasmlabel;
  450. ldef : tdef;
  451. mtype : tdef;
  452. tcb : ttai_typedconstbuilder;
  453. begin
  454. if not assigned(items) or
  455. (items.count=0) then
  456. exit;
  457. tcb:=ctai_typedconstbuilder.create([tcalo_is_lab,tcalo_new_section]);
  458. current_asmdata.getlabel(listsym,alt_data);
  459. tcb.begin_anonymous_record(
  460. internaltypeprefixName[itp_objc_cat_methods]+tostr(items.count),
  461. C_alignment,1,
  462. targetinfos[target_info.system]^.alignment.recordalignmin);
  463. if (abi=oa_nonfragile) then
  464. begin
  465. { size of each entry -- always 32 bit value }
  466. mtype:=search_named_unit_globaltype('OBJC','OBJC_METHOD',true).typedef;
  467. tcb.emit_ord_const(mtype.size,u32inttype);
  468. end;
  469. tcb.emit_ord_const(items.count,u32inttype);
  470. for i:=0 to items.Count-1 do
  471. begin
  472. m:=tprocdef(items[i]);
  473. objcreatestringpoolentry(m.messageinf.str^,sp_objcvarnames,sec_objc_meth_var_names,lab,ldef);
  474. tcb.emit_tai(Tai_const.Create_sym(lab),ldef);
  475. objcreatestringpoolentry(objcencodemethod(m),sp_objcvartypes,sec_objc_meth_var_types,lab,ldef);
  476. tcb.emit_tai(Tai_const.Create_sym(lab),ldef);
  477. { placeholder for address of implementation? }
  478. if (abi=oa_nonfragile) then
  479. tcb.emit_tai(Tai_const.Create_nil_codeptr,codeptruinttype);
  480. end;
  481. list.concatList(
  482. tcb.get_final_asmlist(
  483. listsym,tcb.end_anonymous_record,section,sectname,sizeof(pint))
  484. );
  485. tcb.free;
  486. end;
  487. { Generate the rtti sections for all obj-c classes defined in st, and return
  488. these classes in the classes list. }
  489. procedure tobjcrttiwriter.gen_objc_rtti_sections(list:TAsmList; st:TSymtable);
  490. var
  491. i: longint;
  492. def,
  493. rttidef: tdef;
  494. sym: TAsmSymbol;
  495. begin
  496. if not Assigned(st) then
  497. exit;
  498. for i:=0 to st.DefList.Count-1 do
  499. begin
  500. def:=tdef(st.DefList[i]);
  501. { check whether all types used in Objective-C class/protocol/category
  502. declarations can be used with the Objective-C run time (can only be
  503. done now, because at parse-time some of these types can still be
  504. forwarddefs) }
  505. if is_objc_class_or_protocol(def) then
  506. if not tobjectdef(def).check_objc_types then
  507. continue;
  508. if is_objcclass(def) and
  509. not(oo_is_external in tobjectdef(def).objectoptions) then
  510. begin
  511. if not(oo_is_classhelper in tobjectdef(def).objectoptions) then
  512. begin
  513. gen_objc_classes_sections(list,tobjectdef(def),sym,rttidef);
  514. classsyms.add(sym);
  515. classrttidefs.add(rttidef);
  516. classdefs.add(def);
  517. end
  518. else
  519. begin
  520. gen_objc_category_sections(list,tobjectdef(def),sym,rttidef);
  521. catsyms.add(sym);
  522. catrttidefs.add(rttidef);
  523. catdefs.add(def);
  524. end
  525. end;
  526. end;
  527. end;
  528. constructor tobjcrttiwriter.create(_abi: tobjcabi);
  529. begin
  530. fabi:=_abi;
  531. classdefs:=tfpobjectlist.create(false);
  532. classsyms:=tfpobjectlist.create(false);
  533. classrttidefs:=tfpobjectlist.create(false);
  534. catrttidefs:=tfpobjectlist.create(false);
  535. catdefs:=tfpobjectlist.create(false);
  536. catsyms:=tfpobjectlist.create(false);
  537. end;
  538. destructor tobjcrttiwriter.destroy;
  539. begin
  540. classdefs.free;
  541. classsyms.free;
  542. classrttidefs.free;
  543. catrttidefs.free;
  544. catdefs.free;
  545. catsyms.free;
  546. inherited destroy;
  547. end;
  548. {******************************************************************
  549. RTTI generation -- Fragile ABI
  550. *******************************************************************}
  551. { generate an instance variables list for an obj-c class. }
  552. procedure tobjcrttiwriter_fragile.gen_objc_ivars(list: TAsmList; objccls: tobjectdef; out ivarslabel: TAsmLabel);
  553. type
  554. ivar_data = record
  555. vf : tfieldvarsym;
  556. namesym : TAsmLabel;
  557. namedef : tdef;
  558. typesym : TAsmLabel;
  559. typedef : tdef;
  560. end;
  561. var
  562. i : integer;
  563. vf : tfieldvarsym;
  564. vars : array of ivar_data;
  565. vcnt : Integer;
  566. enctype : ansistring;
  567. encerr : tdef;
  568. begin
  569. ivarslabel:=nil;
  570. vcnt:=0;
  571. vars:=nil;
  572. setLength(vars,objccls.symtable.SymList.Count);
  573. for i:=0 to objccls.symtable.SymList.Count-1 do
  574. if tsym(objccls.symtable.SymList[i]).typ=fieldvarsym then
  575. begin
  576. vf:=tfieldvarsym(objccls.symtable.SymList[i]);
  577. if objctryencodetype(vf.vardef,enctype,encerr) then
  578. begin
  579. vars[vcnt].vf:=vf;
  580. objcreatestringpoolentry(vf.RealName,sp_objcvarnames,sec_objc_meth_var_names,vars[vcnt].namesym,vars[vcnt].namedef);
  581. objcreatestringpoolentry(enctype,sp_objcvartypes,sec_objc_meth_var_types,vars[vcnt].typesym,vars[vcnt].typedef);
  582. inc(vcnt);
  583. end
  584. else
  585. { Should be caught during parsing }
  586. internalerror(2009090601);
  587. end;
  588. if vcnt=0 then
  589. exit;
  590. new_section(list,sec_objc_instance_vars,'_OBJC_INSTANCE_VARS',sizeof(pint));
  591. current_asmdata.getlabel(ivarslabel,alt_data);
  592. list.Concat(tai_label.Create(ivarslabel));
  593. { objc_ivar_list: first the number of elements }
  594. list.Concat(tai_const.Create_32bit(vcnt));
  595. for i:=0 to vcnt-1 do
  596. begin
  597. { reference to the instance variable name }
  598. list.Concat(tai_const.Create_sym(vars[i].namesym));
  599. { reference to the encoded type }
  600. list.Concat(tai_const.Create_sym(vars[i].typesym));
  601. { and the offset of the field }
  602. list.Concat(tai_const.Create_32bit(vars[i].vf.fieldoffset));
  603. end;
  604. end;
  605. (* From GCC:
  606. struct _objc_protocol_extension
  607. {
  608. uint32_t size; // sizeof (struct _objc_protocol_extension)
  609. struct objc_method_list *optional_instance_methods;
  610. struct objc_method_list *optional_class_methods;
  611. struct objc_prop_list *instance_properties;
  612. }
  613. *)
  614. function tobjcrttiwriter_fragile.gen_objc_protocol_ext(list: TAsmList; optinstsym, optclssym: TAsmLabel): TAsmLabel;
  615. var
  616. tcb: ttai_typedconstbuilder;
  617. begin
  618. if assigned(optinstsym) or
  619. assigned(optclssym) then
  620. begin
  621. current_asmdata.getlabel(Result,alt_data);
  622. tcb:=ctai_typedconstbuilder.create([tcalo_new_section,tcalo_no_dead_strip]);
  623. tcb.begin_anonymous_record(
  624. internaltypeprefixName[itb_objc_fr_protocol_ext],
  625. C_alignment,1,
  626. targetinfos[target_info.system]^.alignment.recordalignmin);
  627. { size of this structure }
  628. tcb.emit_ord_const(16,u32inttype);
  629. { optional instance methods }
  630. ConcatSymOrNil(tcb,optinstsym,voidpointertype);
  631. { optional class methods }
  632. ConcatSymOrNil(tcb,optclssym,voidpointertype);
  633. { optional properties (todo) }
  634. ConcatSymOrNil(tcb,nil,voidpointertype);
  635. list.concatList(
  636. tcb.get_final_asmlist(
  637. result,tcb.end_anonymous_record,
  638. sec_objc_protocol_ext,'_OBJC_PROTOCOLEXT',sizeof(pint)
  639. )
  640. );
  641. tcb.free;
  642. end
  643. else
  644. Result:=nil;
  645. end;
  646. { Generate rtti for an Objective-C protocol }
  647. procedure tobjcrttiwriter_fragile.gen_objc_protocol(list:TAsmList; protocol: tobjectdef; out protocollabel: TAsmSymbol);
  648. var
  649. namesym : TAsmLabel;
  650. namedef : tdef;
  651. protolist : TAsmLabel;
  652. reqinstsym,
  653. optinstsym,
  654. reqclssym,
  655. optclssym,
  656. protoext,
  657. lbl : TAsmLabel;
  658. tcb : ttai_typedconstbuilder;
  659. begin
  660. gen_objc_protocol_list(list,protocol.ImplementedInterfaces,protolist);
  661. gen_objc_protocol_elements(list,protocol,reqinstsym,optinstsym,reqclssym,optclssym);
  662. protoext:=gen_objc_protocol_ext(list,optinstsym,optclssym);
  663. current_asmdata.getlabel(lbl,alt_data);
  664. protocollabel:=lbl;
  665. tcb:=ctai_typedconstbuilder.create([tcalo_new_section,tcalo_no_dead_strip]);
  666. tcb.begin_anonymous_record(
  667. internaltypeprefixName[itb_objc_fr_protocol],
  668. C_alignment,1,
  669. targetinfos[target_info.system]^.alignment.recordalignmin);
  670. { protocol's isa - points to information about optional methods/properties }
  671. ConcatSymOrNil(tcb,protoext,voidpointertype);
  672. { name }
  673. objcreatestringpoolentry(protocol.objextname^,sp_objcclassnames,sec_objc_class_names,namesym,namedef);
  674. tcb.queue_init(voidpointertype);
  675. tcb.queue_emit_asmsym(namesym,namedef);
  676. { protocol's list }
  677. ConcatSymOrNil(tcb,protolist,voidpointertype);
  678. { instance methods, in __cat_inst_meth }
  679. ConcatSymOrNil(tcb,reqinstsym,voidpointertype);
  680. { class methods, in __cat_cls_meth }
  681. ConcatSymOrNil(tcb,reqclssym,voidpointertype);
  682. list.concatList(
  683. tcb.get_final_asmlist(
  684. lbl,tcb.end_anonymous_record,
  685. sec_objc_protocol,'_OBJC_PROTOCOL',sizeof(pint)
  686. )
  687. );
  688. tcb.free;
  689. end;
  690. (*
  691. From Clang:
  692. struct _objc_category {
  693. char *category_name;
  694. char *class_name;
  695. struct _objc_method_list *instance_methods;
  696. struct _objc_method_list *class_methods;
  697. struct _objc_protocol_list *protocols;
  698. uint32_t size; // <rdar://4585769>
  699. struct _objc_property_list *instance_properties;
  700. };
  701. *)
  702. { Generate rtti for an Objective-C class and its meta-class. }
  703. procedure tobjcrttiwriter_fragile.gen_objc_category_sections(list:TAsmList; objccat: tobjectdef; out catlabel: TAsmSymbol; out catlabeldef: tdef);
  704. var
  705. catstrsym,
  706. clsstrsym,
  707. protolistsym : TAsmLabel;
  708. instmthdlist,
  709. clsmthdlist,
  710. catsym : TAsmSymbol;
  711. catstrdef,
  712. clsstrdef,
  713. catdef : tdef;
  714. tcb : ttai_typedconstbuilder;
  715. begin
  716. { the category name }
  717. objcreatestringpoolentry(objccat.objextname^,sp_objcclassnames,sec_objc_class_names,catstrsym,catstrdef);
  718. { the name of the class it extends }
  719. objcreatestringpoolentry(objccat.childof.objextname^,sp_objcclassnames,sec_objc_class_names,clsstrsym,clsstrdef);
  720. { generate the methods lists }
  721. gen_objc_methods(list,objccat,instmthdlist,false,true);
  722. gen_objc_methods(list,objccat,clsmthdlist,true,true);
  723. { generate implemented protocols list }
  724. gen_objc_protocol_list(list,objccat.ImplementedInterfaces,protolistsym);
  725. { category declaration section }
  726. tcb:=ctai_typedconstbuilder.create([tcalo_new_section,tcalo_no_dead_strip]);
  727. tcb.begin_anonymous_record(
  728. internaltypeprefixName[itb_objc_fr_category],
  729. C_alignment,1,
  730. targetinfos[target_info.system]^.alignment.recordalignmin);
  731. tcb.queue_init(voidpointertype);
  732. tcb.queue_emit_asmsym(catstrsym,catstrdef);
  733. tcb.queue_init(voidpointertype);
  734. tcb.queue_emit_asmsym(clsstrsym,clsstrdef);
  735. ConcatSymOrNil(tcb,instmthdlist,voidpointertype);
  736. ConcatSymOrNil(tcb,clsmthdlist,voidpointertype);
  737. ConcatSymOrNil(tcb,protolistsym,voidpointertype);
  738. { size of this structure }
  739. tcb.emit_ord_const(28,u32inttype);
  740. { properties, not yet supported }
  741. tcb.emit_ord_const(0,u32inttype);
  742. catdef:=tcb.end_anonymous_record;
  743. catsym:=current_asmdata.DefineAsmSymbol(objccat.rtti_mangledname(objcclassrtti),AB_LOCAL,AT_DATA,catdef);
  744. list.concatList(
  745. tcb.get_final_asmlist(
  746. catsym,catdef,
  747. sec_objc_category,'_OBJC_CATEGORY',sizeof(pint)
  748. )
  749. );
  750. tcb.free;
  751. catlabel:=catsym;
  752. catlabeldef:=catdef;
  753. end;
  754. (*
  755. From Clang:
  756. struct _objc_class {
  757. Class isa;
  758. Class super_class;
  759. const char *name;
  760. long version;
  761. long info;
  762. long instance_size;
  763. struct _objc_ivar_list *ivars;
  764. struct _objc_method_list *methods;
  765. struct _objc_cache *cache;
  766. struct _objc_protocol_list *protocols;
  767. // Objective-C 1.0 extensions (<rdr://4585769>) -- for garbage collection
  768. const char *ivar_layout;
  769. struct _objc_class_ext *ext;
  770. };
  771. *)
  772. { Generate rtti for an Objective-C class and its meta-class. }
  773. procedure tobjcrttiwriter_fragile.gen_objc_classes_sections(list:TAsmList; objclss: tobjectdef; out classlabel: TAsmSymbol; out classlabeldef: tdef);
  774. const
  775. CLS_CLASS = 1;
  776. CLS_META = 2;
  777. CLS_HIDDEN = $20000;
  778. META_INST_SIZE = 40+8; // sizeof(objc_class) + 8
  779. var
  780. root : tobjectdef;
  781. metasym,
  782. mthdlist,
  783. clssym : TAsmSymbol;
  784. superStrDef,
  785. classStrDef,
  786. metaisaStrDef,
  787. metaDef,
  788. clsDef : tdef;
  789. superStrSym,
  790. classStrSym,
  791. metaisaStrSym,
  792. ivarslist,
  793. protolistsym : TAsmLabel;
  794. hiddenflag : cardinal;
  795. tcb : ttai_typedconstbuilder;
  796. begin
  797. { generate the class methods list }
  798. gen_objc_methods(list,objclss,mthdlist,true,false);
  799. { generate implemented protocols list }
  800. gen_objc_protocol_list(list,objclss.ImplementedInterfaces,protolistsym);
  801. { register necessary names }
  802. { 1) the superclass }
  803. if assigned(objclss.childof) then
  804. objcreatestringpoolentry(objclss.childof.objextname^,sp_objcclassnames,sec_objc_class_names,superStrSym,superStrDef)
  805. else
  806. begin
  807. { not empty string, but nil! }
  808. superStrSym:=nil;
  809. superStrDef:=voidpointertype;
  810. end;
  811. { 2) the current class }
  812. objcreatestringpoolentry(objclss.objextname^,sp_objcclassnames,sec_objc_class_names,classStrSym,classStrDef);
  813. { 3) the isa }
  814. { From Clang: The isa for the meta-class is the root of the hierarchy. }
  815. root:=objclss;
  816. while assigned(root.childof) do
  817. root:=root.childof;
  818. objcreatestringpoolentry(root.objextname^,sp_objcclassnames,sec_objc_class_names,metaisaStrSym,metaisaStrDef);
  819. { 4) the flags }
  820. { consider every class declared in the implementation section of a unit
  821. as "hidden"
  822. }
  823. hiddenflag:=0;
  824. if (objclss.owner.symtabletype=staticsymtable) and
  825. current_module.is_unit then
  826. hiddenflag:=CLS_HIDDEN;
  827. { class declaration section }
  828. { 1) meta-class declaration }
  829. tcb:=ctai_typedconstbuilder.create([tcalo_new_section,tcalo_no_dead_strip]);
  830. tcb.begin_anonymous_record(internaltypeprefixName[itb_objc_fr_meta_class],
  831. C_alignment,1,
  832. targetinfos[target_info.system]^.alignment.recordalignmin);
  833. tcb.queue_init(voidpointertype);
  834. tcb.queue_emit_asmsym(metaisaStrSym,metaisaStrDef);
  835. { pointer to the superclass name if any, otherwise nil }
  836. if assigned(superstrsym) then
  837. begin
  838. tcb.queue_init(voidpointertype);
  839. tcb.queue_emit_asmsym(superStrSym,superStrDef);
  840. end
  841. else
  842. tcb.emit_tai(tai_const.Create_nil_dataptr,voidpointertype);
  843. { pointer to the class name }
  844. tcb.queue_init(voidpointertype);
  845. tcb.queue_emit_asmsym(classStrSym,classStrDef);
  846. { version is always 0 currently }
  847. tcb.emit_ord_const(0,u32inttype);
  848. { CLS_META for meta-classes }
  849. tcb.emit_ord_const(hiddenflag or CLS_META,u32inttype);
  850. { size of the meta-class instance: sizeof(objc_class) + 8 bytes }
  851. tcb.emit_ord_const(META_INST_SIZE,u32inttype);
  852. { meta-classes don't have ivars list (=0) }
  853. tcb.emit_ord_const(0,u32inttype);
  854. { class methods list (stored in "__cls_meth" section) }
  855. ConcatSymOrNil(tcb,mthdlist,voidpointertype);
  856. { From Clang: cache is always nil }
  857. tcb.emit_ord_const(0,u32inttype);
  858. { protocols }
  859. ConcatSymOrNil(tcb,protolistsym,voidpointertype);
  860. { From Clang: ivar_layout for meta-class is always NULL. }
  861. tcb.emit_ord_const(0,u32inttype);
  862. { From Clang: The class extension is always unused for meta-classes. }
  863. tcb.emit_ord_const(0,u32inttype);
  864. metaDef:=tcb.end_anonymous_record;
  865. metasym:=current_asmdata.DefineAsmSymbol(objclss.rtti_mangledname(objcmetartti),AB_LOCAL,AT_DATA,metadef);
  866. list.concatList(
  867. tcb.get_final_asmlist(
  868. metasym,metaDef,
  869. sec_objc_meta_class,'_OBJC_META_CLASS',sizeof(pint)
  870. )
  871. );
  872. tcb.free;
  873. { 2) regular class declaration }
  874. { generate the instance methods list }
  875. gen_objc_methods(list,objclss,mthdlist,false,false);
  876. { generate the instance variables list }
  877. gen_objc_ivars(list,objclss,ivarslist);
  878. tcb:=ctai_typedconstbuilder.create([tcalo_new_section,tcalo_no_dead_strip]);
  879. tcb.begin_anonymous_record(internaltypeprefixName[itb_objc_fr_class],
  880. C_alignment,1,
  881. targetinfos[target_info.system]^.alignment.recordalignmin);
  882. { for class declaration: the isa points to the meta-class declaration }
  883. tcb.emit_tai(Tai_const.Create_sym(metasym),cpointerdef.getreusable(metaDef));
  884. { pointer to the super_class name if any, nil otherwise }
  885. if assigned(superStrSym) then
  886. begin
  887. tcb.queue_init(voidcodepointertype);
  888. tcb.queue_emit_asmsym(superStrSym,superStrDef)
  889. end
  890. else
  891. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidcodepointertype);
  892. { pointer to the class name }
  893. tcb.queue_init(voidcodepointertype);
  894. tcb.queue_emit_asmsym(classStrSym,classStrDef);
  895. { version is always 0 currently }
  896. tcb.emit_ord_const(0,u32inttype);
  897. { CLS_CLASS for classes }
  898. tcb.emit_ord_const(hiddenflag or CLS_CLASS,u32inttype);
  899. { size of instance: total size of instance variables }
  900. tcb.emit_ord_const(tobjectsymtable(objclss.symtable).datasize,u32inttype);
  901. { objc_ivar_list (stored in "__instance_vars" section) }
  902. ConcatSymOrNil(tcb,ivarslist,voidpointertype);
  903. { instance methods list (stored in "__inst_meth" section) }
  904. ConcatSymOrNil(tcb,mthdlist,voidpointertype);
  905. { From Clang: cache is always NULL }
  906. tcb.emit_tai(tai_const.Create_nil_dataptr,voidpointertype);
  907. { protocols, protolistsym has been created for meta-class, no need to create another one}
  908. ConcatSymOrNil(tcb, protolistsym,voidpointertype);
  909. { From Clang: strong ivar_layout, necessary for garbage collection support }
  910. tcb.emit_tai(tai_const.Create_nil_dataptr,voidpointertype);
  911. { TODO: From Clang: weak ivar_layout, necessary for garbage collection support }
  912. tcb.emit_tai(tai_const.Create_nil_dataptr,voidpointertype);
  913. clsDef:=tcb.end_anonymous_record;
  914. clssym:=current_asmdata.DefineAsmSymbol(objclss.rtti_mangledname(objcclassrtti),AB_LOCAL,AT_DATA,clsDef);
  915. list.concatList(
  916. tcb.get_final_asmlist(
  917. clssym,clsDef,
  918. sec_objc_class,'_OBJC_CLASS',sizeof(pint)
  919. )
  920. );
  921. tcb.free;
  922. classlabel:=clssym;
  923. classlabeldef:=clsDef;
  924. end;
  925. { Generate the global information sections (objc_symbols and objc_module_info)
  926. for this module. }
  927. procedure tobjcrttiwriter_fragile.gen_objc_info_sections(list: tasmlist);
  928. var
  929. i: longint;
  930. sym : TAsmSymbol;
  931. lab : TAsmLabel;
  932. symsdef,
  933. def : tdef;
  934. parent: tobjectdef;
  935. superclasses: tfpobjectlist;
  936. tcb: ttai_typedconstbuilder;
  937. begin
  938. if (classsyms.count<>0) or
  939. (catsyms.count<>0) then
  940. begin
  941. tcb:=ctai_typedconstbuilder.create([tcalo_new_section,tcalo_no_dead_strip]);
  942. tcb.begin_anonymous_record('',
  943. C_alignment,1,
  944. targetinfos[target_info.system]^.alignment.recordalignmin);
  945. { ??? (always 0 in Clang) }
  946. tcb.emit_tai(tai_const.Create_nil_dataptr,voidpointertype);
  947. { ??? (From Clang: always 0, pointer to some selector) }
  948. tcb.emit_tai(tai_const.Create_nil_dataptr,voidpointertype);
  949. { From Clang: number of defined classes }
  950. tcb.emit_ord_const(classsyms.count,u16inttype);
  951. { From Clang: number of defined categories }
  952. tcb.emit_ord_const(catsyms.count,u16inttype);
  953. { first all classes }
  954. for i:=0 to classsyms.count-1 do
  955. tcb.emit_tai(Tai_const.Create_sym(tasmsymbol(classsyms[i])),tdef(classrttidefs[i]));
  956. { then all categories }
  957. for i:=0 to catsyms.count-1 do
  958. tcb.emit_tai(Tai_const.Create_sym(tasmsymbol(catsyms[i])),tdef(catrttidefs[i]));
  959. symsdef:=tcb.end_anonymous_record;
  960. sym := current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'_OBJC_SYMBOLS_$',AB_LOCAL,AT_DATA,symsdef);
  961. list.concatList(tcb.get_final_asmlist(sym,
  962. symsdef,
  963. sec_objc_symbols,'_OBJC_SYMBOLS',
  964. sizeof(pint)));
  965. end
  966. else
  967. begin
  968. sym:=nil;
  969. symsdef:=nil;
  970. end;
  971. tcb:=ctai_typedconstbuilder.create([tcalo_new_section,tcalo_no_dead_strip]);
  972. tcb.begin_anonymous_record('',
  973. C_alignment,1,
  974. targetinfos[target_info.system]^.alignment.recordalignmin);
  975. { version number = 7 (always, both for gcc and clang) }
  976. tcb.emit_ord_const(7,ptruinttype);
  977. { sizeof(objc_module): 4 pointer-size entities }
  978. tcb.emit_ord_const(sizeof(pint)*4,ptruinttype);
  979. { used to be file name, now unused (points to empty string) }
  980. objcreatestringpoolentry('',sp_objcclassnames,sec_objc_class_names,lab,def);
  981. tcb.emit_tai(Tai_const.Create_sym(lab),def);
  982. { pointer to classes/categories list declared in this module }
  983. if assigned(sym) then
  984. tcb.emit_tai(tai_const.Create_sym(sym),symsdef)
  985. else
  986. tcb.emit_tai(tai_const.Create_nil_dataptr,voidpointertype);
  987. current_asmdata.getlabel(lab,alt_data);
  988. list.concatList(tcb.get_final_asmlist(lab,
  989. tcb.end_anonymous_record,sec_objc_module_info,'_OBJC_MODULE_INFO',4));
  990. { Add lazy references to parent classes of all classes defined in this unit }
  991. superclasses:=tfpobjectlist.create(false);
  992. for i:=0 to classdefs.count-1 do
  993. begin
  994. parent:=tobjectdef(classdefs[i]).childof;
  995. { warning: linear search, performance hazard if large number of subclasses }
  996. if assigned(parent) and
  997. (superclasses.indexof(parent)=-1) then
  998. begin
  999. list.concat(tai_directive.create(asd_lazy_reference,'.objc_class_name_'+parent.objextname^));
  1000. superclasses.add(parent);
  1001. end;
  1002. end;
  1003. for i:=0 to catdefs.count-1 do
  1004. begin
  1005. parent:=tobjectdef(catdefs[i]).childof;
  1006. { warning: linear search, performance hazard if large number of subclasses }
  1007. if assigned(parent) and
  1008. (superclasses.indexof(parent)=-1) then
  1009. begin
  1010. list.concat(tai_directive.create(asd_lazy_reference,'.objc_class_name_'+parent.objextname^));
  1011. superclasses.add(parent);
  1012. end;
  1013. end;
  1014. superclasses.free;
  1015. { reference symbols for all classes and categories defined in this unit }
  1016. for i:=0 to classdefs.count-1 do
  1017. list.concat(tai_symbol.Createname_global_value('.objc_class_name_'+tobjectdef(classdefs[i]).objextname^,AT_DATA,0,0,voidpointertype));
  1018. for i:=0 to catdefs.count-1 do
  1019. list.concat(tai_symbol.Createname_global_value('.objc_category_name_'+
  1020. tobjectdef(catdefs[i]).childof.objextname^+'_'+
  1021. tobjectdef(catdefs[i]).objextname^,AT_DATA,0,0,voidpointertype));
  1022. end;
  1023. constructor tobjcrttiwriter_fragile.create;
  1024. begin
  1025. inherited create(oa_fragile);
  1026. end;
  1027. {******************************************************************
  1028. RTTI generation -- Non-Fragile ABI
  1029. *******************************************************************}
  1030. (*
  1031. From Clang:
  1032. /// EmitIvarList - Emit the ivar list for the given
  1033. /// implementation. The return value has type
  1034. /// IvarListnfABIPtrTy.
  1035. /// struct _ivar_t {
  1036. /// unsigned long int *offset; // pointer to ivar offset location
  1037. /// char *name;
  1038. /// char *type;
  1039. /// uint32_t alignment;
  1040. /// uint32_t size;
  1041. /// }
  1042. /// struct _ivar_list_t {
  1043. /// uint32 entsize; // sizeof(struct _ivar_t)
  1044. /// uint32 count;
  1045. /// struct _iver_t list[count];
  1046. /// }
  1047. ///
  1048. *)
  1049. procedure tobjcrttiwriter_nonfragile.gen_objc_ivars(list: tasmlist; objccls: tobjectdef; out ivarslabel: tasmlabel);
  1050. type
  1051. ivar_data = record
  1052. vf : tfieldvarsym;
  1053. namesym : TAsmLabel;
  1054. namedef : tdef;
  1055. typesym : TAsmLabel;
  1056. typedef : tdef;
  1057. offssym : TAsmSymbol;
  1058. end;
  1059. var
  1060. ivtype: tdef;
  1061. vf : tfieldvarsym;
  1062. vars : array of ivar_data;
  1063. i : integer;
  1064. vcnt : integer;
  1065. enctype : ansistring;
  1066. encerr : tdef;
  1067. prefix : shortstring;
  1068. vis : TAsmsymbind;
  1069. tcb : ttai_typedconstbuilder;
  1070. pptruinttype : tdef;
  1071. begin
  1072. ivarslabel:=nil;
  1073. prefix:='';
  1074. vcnt:=0;
  1075. vars:=nil;
  1076. setLength(vars,objccls.symtable.SymList.Count);
  1077. tcb:=nil;
  1078. prefix:='';
  1079. for i:=0 to objccls.symtable.SymList.Count-1 do
  1080. if tsym(objccls.symtable.SymList[i]).typ=fieldvarsym then
  1081. begin
  1082. vf:=tfieldvarsym(objccls.symtable.SymList[i]);
  1083. if objctryencodetype(vf.vardef,enctype,encerr) then
  1084. begin
  1085. vars[vcnt].vf:=vf;
  1086. objcreatestringpoolentry(vf.RealName,sp_objcvarnames,sec_objc_meth_var_names,vars[vcnt].namesym,vars[vcnt].namedef);
  1087. objcreatestringpoolentry(enctype,sp_objcvartypes,sec_objc_meth_var_types,vars[vcnt].typesym,vars[vcnt].typedef);
  1088. if (vcnt=0) then
  1089. begin
  1090. tcb:=ctai_typedconstbuilder.create([tcalo_new_section]);
  1091. prefix:=target_info.cprefix+'OBJC_IVAR_$_'+objccls.objextname^+'.';
  1092. end
  1093. else
  1094. tcb:=ctai_typedconstbuilder.create([tcalo_new_section]);
  1095. { This matches gcc/Clang, but is strange: I would expect private
  1096. fields to be local symbols rather than private_extern (which
  1097. is "package-global") (JM)
  1098. }
  1099. if not(vf.visibility in [vis_public,vis_protected,vis_strictprotected]) then
  1100. vis:=AB_PRIVATE_EXTERN
  1101. else
  1102. vis:=AB_GLOBAL;
  1103. vars[vcnt].offssym:=current_asmdata.DefineAsmSymbol(prefix+vf.RealName,vis,AT_DATA,ptruinttype);
  1104. tcb.emit_tai(tai_const.Create_int_dataptr(vf.fieldoffset),ptruinttype);
  1105. list.concatList(
  1106. tcb.get_final_asmlist(
  1107. vars[vcnt].offssym,ptruinttype,
  1108. sec_objc_const,'_OBJC_IVAR_OFFSETS',sizeof(pint)
  1109. )
  1110. );
  1111. tcb.free;
  1112. inc(vcnt);
  1113. end
  1114. else
  1115. { must be caught during parsing }
  1116. internalerror(2009092301);
  1117. end;
  1118. if vcnt=0 then
  1119. exit;
  1120. tcb:=ctai_typedconstbuilder.create([tcalo_is_lab,tcalo_new_section,tcalo_no_dead_strip]);
  1121. current_asmdata.getlabel(ivarslabel,alt_data);
  1122. tcb.begin_anonymous_record(
  1123. internaltypeprefixName[itb_objc_nf_ivars]+tostr(vcnt),
  1124. C_alignment,1,
  1125. targetinfos[target_info.system]^.alignment.recordalignmin);
  1126. { size of each entry -- always 32 bit value }
  1127. ivtype:=search_named_unit_globaltype('OBJC','OBJC_IVAR',true).typedef;
  1128. tcb.emit_ord_const(ivtype.size,u32inttype);
  1129. { number of entries -- always 32 bit value }
  1130. tcb.emit_ord_const(vcnt,u32inttype);
  1131. { we use voidpointertype for all elements so that we can reuse the
  1132. recorddef for all ivar tables with the same number of elements }
  1133. pptruinttype:=cpointerdef.getreusable(ptruinttype);
  1134. for i:=0 to vcnt-1 do
  1135. begin
  1136. { reference to the offset }
  1137. tcb.emit_tai(tai_const.Create_sym(vars[i].offssym),pptruinttype);
  1138. { reference to the instance variable name (}
  1139. tcb.queue_init(voidpointertype);
  1140. tcb.queue_emit_asmsym(vars[i].namesym,vars[i].namedef);
  1141. { reference to the encoded type }
  1142. tcb.queue_init(voidpointertype);
  1143. tcb.queue_emit_asmsym(vars[i].typesym,vars[i].typedef);
  1144. { alignment -- always 32 bit value }
  1145. tcb.emit_ord_const(vars[i].vf.vardef.alignment,u32inttype);
  1146. { size -- always 32 bit value }
  1147. tcb.emit_ord_const(vars[i].vf.vardef.size,u32inttype);
  1148. end;
  1149. list.concatList(
  1150. tcb.get_final_asmlist(
  1151. ivarslabel,tcb.end_anonymous_record,
  1152. sec_objc_instance_vars,'_OBJC_INSTANCE_VARS',sizeof(pint)
  1153. )
  1154. );
  1155. tcb.free;
  1156. end;
  1157. (*
  1158. From Clang:
  1159. /// GetOrEmitProtocol - Generate the protocol meta-data:
  1160. /// @code
  1161. /// struct _protocol_t {
  1162. /// id isa; // NULL
  1163. /// const char * const protocol_name;
  1164. /// const struct _protocol_list_t * protocol_list; // super protocols
  1165. /// const struct method_list_t * const instance_methods;
  1166. /// const struct method_list_t * const class_methods;
  1167. /// const struct method_list_t *optionalInstanceMethods;
  1168. /// const struct method_list_t *optionalClassMethods;
  1169. /// const struct _prop_list_t * properties;
  1170. /// const uint32_t size; // sizeof(struct _protocol_t)
  1171. /// const uint32_t flags; // = 0
  1172. /// }
  1173. /// @endcode
  1174. *)
  1175. procedure tobjcrttiwriter_nonfragile.gen_objc_protocol(list: tasmlist; protocol: tobjectdef; out protocollabel: tasmsymbol);
  1176. var
  1177. lbl,
  1178. listsym : TAsmSymbol;
  1179. namedef : tdef;
  1180. namesym,
  1181. protolist : TAsmLabel;
  1182. reqinstsym,
  1183. reqclssym,
  1184. optinstsym,
  1185. optclssym : TAsmLabel;
  1186. prottype : tdef;
  1187. tcb : ttai_typedconstbuilder;
  1188. sec : TAsmSectiontype;
  1189. begin
  1190. gen_objc_protocol_list(list,protocol.ImplementedInterfaces,protolist);
  1191. gen_objc_protocol_elements(list,protocol,reqinstsym,optinstsym,reqclssym,optclssym);
  1192. { label for the protocol needs to be
  1193. a) in a coalesced section (so multiple definitions of the same protocol
  1194. can be merged by the linker)
  1195. b) private_extern (should only be merged within the same module)
  1196. c) weakly defined (so multiple definitions don't cause errors)
  1197. }
  1198. prottype:=search_named_unit_globaltype('OBJC','OBJC_PROTOCOL',true).typedef;
  1199. lbl:=current_asmdata.DefineAsmSymbol(protocol.rtti_mangledname(objcclassrtti),AB_PRIVATE_EXTERN,AT_DATA,prottype);
  1200. protocollabel:=lbl;
  1201. tcb:=ctai_typedconstbuilder.create([tcalo_new_section,tcalo_weak]);
  1202. tcb.maybe_begin_aggregate(prottype);
  1203. { protocol's isa - always nil }
  1204. tcb.emit_tai(Tai_const.Create_nil_dataptr,objc_idtype);
  1205. { name }
  1206. objcreatestringpoolentry(protocol.objextname^,sp_objcclassnames,sec_objc_class_names,namesym,namedef);
  1207. tcb.queue_init(charpointertype);
  1208. tcb.queue_emit_asmsym(namesym,namedef);
  1209. { parent protocols list }
  1210. ConcatSymOrNil(tcb,protolist,voidpointertype);
  1211. { required instance methods }
  1212. ConcatSymOrNil(tcb,reqinstsym,voidpointertype);
  1213. { required class methods }
  1214. ConcatSymOrNil(tcb,reqclssym,voidpointertype);
  1215. { optional instance methods }
  1216. ConcatSymOrNil(tcb,optinstsym,voidpointertype);
  1217. { optional class methods }
  1218. ConcatSymOrNil(tcb,optclssym,voidpointertype);
  1219. { TODO: properties }
  1220. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1221. { size of this type }
  1222. tcb.emit_ord_const(prottype.size,u32inttype);
  1223. { flags }
  1224. tcb.emit_ord_const(0,u32inttype);
  1225. tcb.maybe_end_aggregate(prottype);
  1226. { coalesced sections are only required for ld-classic, because it does not
  1227. support "weak" bits on all symbols. They are deprecated in ld64 starting
  1228. from 2015: https://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20151012/305992.html
  1229. While targeting 10.6 or higher does not guarantee we are using ld64,
  1230. we don't have a better heuristic available.
  1231. }
  1232. if not MacOSXVersionMin.isvalid or
  1233. (MacOSXVersionMin.relationto(10,6,0)>=0) then
  1234. sec:=sec_data
  1235. else
  1236. sec:=sec_data_coalesced;
  1237. list.concatList(
  1238. tcb.get_final_asmlist(
  1239. lbl,prottype,
  1240. sec,'_OBJC_PROTOCOL',sizeof(pint)
  1241. )
  1242. );
  1243. tcb.free;
  1244. { also add an entry to the __DATA, __objc_protolist section, required to
  1245. register the protocol with the runtime }
  1246. listsym:=current_asmdata.DefineAsmSymbol(protocol.rtti_mangledname(objcmetartti),AB_PRIVATE_EXTERN,AT_DATA,cpointerdef.getreusable(prottype));
  1247. tcb:=ctai_typedconstbuilder.create([tcalo_new_section,tcalo_weak,tcalo_no_dead_strip]);
  1248. tcb.emit_tai(tai_const.Create_sym(lbl),cpointerdef.getreusable(prottype));
  1249. list.concatList(
  1250. tcb.get_final_asmlist(
  1251. listsym,cpointerdef.getreusable(prottype),
  1252. sec_objc_protolist,'_OBJC_PROTOLIST',sizeof(pint)
  1253. )
  1254. );
  1255. tcb.free;
  1256. end;
  1257. (*
  1258. From Clang:
  1259. /// struct _category_t {
  1260. /// const char * const name;
  1261. /// struct _class_t *const cls;
  1262. /// const struct _method_list_t * const instance_methods;
  1263. /// const struct _method_list_t * const class_methods;
  1264. /// const struct _protocol_list_t * const protocols;
  1265. /// const struct _prop_list_t * const properties;
  1266. /// }
  1267. *)
  1268. procedure tobjcrttiwriter_nonfragile.gen_objc_category_sections(list:TAsmList; objccat: tobjectdef; out catlabel: TAsmSymbol; out catlabeldef: tdef);
  1269. var
  1270. catstrsym,
  1271. protolistsym : TAsmLabel;
  1272. instmthdlist,
  1273. clsmthdlist,
  1274. clssym,
  1275. catsym : TAsmSymbol;
  1276. catstrdef,
  1277. catdef : tdef;
  1278. tcb : ttai_typedconstbuilder;
  1279. begin
  1280. { the category name }
  1281. objcreatestringpoolentry(objccat.objextname^,sp_objcclassnames,sec_objc_class_names,catstrsym,catstrdef);
  1282. { the class it extends }
  1283. clssym:=current_asmdata.RefAsmSymbol(objccat.childof.rtti_mangledname(objcclassrtti),AT_DATA);
  1284. { generate the methods lists }
  1285. gen_objc_methods(list,objccat,instmthdlist,false,true);
  1286. gen_objc_methods(list,objccat,clsmthdlist,true,true);
  1287. { generate implemented protocols list }
  1288. gen_objc_protocol_list(list,objccat.ImplementedInterfaces,protolistsym);
  1289. { category declaration section }
  1290. tcb:=ctai_typedconstbuilder.create([tcalo_new_section]);
  1291. tcb.begin_anonymous_record(internaltypeprefixName[itb_objc_nf_category],
  1292. C_alignment,1,
  1293. targetinfos[target_info.system]^.alignment.recordalignmin);
  1294. tcb.queue_init(voidpointertype);
  1295. tcb.queue_emit_asmsym(catstrsym,catstrdef);
  1296. tcb.emit_tai(Tai_const.Create_sym(clssym),voidpointertype);
  1297. ConcatSymOrNil(tcb,instmthdlist,voidpointertype);
  1298. ConcatSymOrNil(tcb,clsmthdlist,voidpointertype);
  1299. ConcatSymOrNil(tcb,protolistsym,voidpointertype);
  1300. { properties, not yet supported }
  1301. tcb.emit_tai(Tai_const.Create_nil_dataptr,voidpointertype);
  1302. catdef:=tcb.end_anonymous_record;
  1303. catsym:=current_asmdata.DefineAsmSymbol(objccat.rtti_mangledname(objcclassrtti),AB_LOCAL,AT_DATA,catdef);
  1304. list.concatList(
  1305. tcb.get_final_asmlist(
  1306. catsym,catdef,
  1307. sec_objc_const,'_OBJC_CATEGORY',sizeof(pint)
  1308. )
  1309. );
  1310. tcb.free;
  1311. catlabel:=catsym;
  1312. catlabeldef:=catdef;
  1313. end;
  1314. (*
  1315. From Clang:
  1316. /// BuildIvarLayout - Builds ivar layout bitmap for the class
  1317. /// implementation for the __strong or __weak case.
  1318. /// The layout map displays which words in ivar list must be skipped
  1319. /// and which must be scanned by GC (see below). String is built of bytes.
  1320. /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
  1321. /// of words to skip and right nibble is count of words to scan. So, each
  1322. /// nibble represents up to 15 workds to skip or scan. Skipping the rest is
  1323. /// represented by a 0x00 byte which also ends the string.
  1324. /// 1. when ForStrongLayout is true, following ivars are scanned:
  1325. /// - id, Class
  1326. /// - object * // note: this "object" means "Objective-C object" (JM)
  1327. /// - __strong anything
  1328. ///
  1329. /// 2. When ForStrongLayout is false, following ivars are scanned:
  1330. /// - __weak anything
  1331. *)
  1332. (*
  1333. Only required when supporting garbage collection
  1334. procedure tobjcrttiwriter_nonfragile.gen_objc_ivargc_recursive(st: tabstractrecordsymtable; ptrbset: tbitset; startoffset: puint; il: tivarlayouttype);
  1335. var
  1336. i: longint;
  1337. fs: tfieldvarsym;
  1338. includelen: longint;
  1339. begin
  1340. for i:=0 to st.SymList.count-1 do
  1341. if (tsym(st.symlist[i]).typ=fieldvarsym) then
  1342. begin
  1343. fs:=tfieldvarsym(st.symlist[i]);
  1344. includelen:=0;
  1345. case fs.vardef.typ of
  1346. pointerdef,
  1347. classrefdef:
  1348. if (fs.vardef=objc_idtype) or
  1349. (fs.vardef=objc_metaclasstype) then
  1350. includelen:=1;
  1351. recorddef:
  1352. TODO: bitpacking -> offset differences
  1353. gen_objc_ivargc_recursive(tabstractrecordsymtable(trecorddef(fs.vardef).symtable),ptrbset,startoffset+fs.fieldoffset,il);
  1354. arraydef:
  1355. begin
  1356. if not is_special_
  1357. end;
  1358. objectdef :
  1359. begin
  1360. case tobjectdef(fs.vardef).objecttype of
  1361. odt_objcclass,
  1362. odt_objcprotocol:
  1363. includelen:=1;
  1364. odt_object:
  1365. gen_objc_ivargc_recursive(tabstractrecordsymtable(tobjectdef(fs.vardef).symtable),ptrbset,startoffset+fs.fieldoffset,il);
  1366. end;
  1367. end;
  1368. end;
  1369. end;
  1370. end;
  1371. function tobjcrttiwriter_nonfragile.gen_objc_ivargcstring(objclss: tobjectdef; il: tivarlayouttype): ansistring;
  1372. var
  1373. ptrbset: tbitset;
  1374. parent: tobjectdef;
  1375. size,
  1376. startoffset: puint;
  1377. i: longint;
  1378. begin
  1379. size:=tObjectSymtable(objclss.symtable).datasize;
  1380. if assigned(objclss.childof) then
  1381. startoffset:=tObjectSymtable(objclss.childof.symtable).datasize
  1382. else
  1383. startoffset:=0;
  1384. size:=size-startoffset;
  1385. ptrbset:=tbitset.create_bytesize((size+sizeof(ptruint)-1) div sizeof(ptruint));
  1386. { has to include info for this class' fields and those of all parent
  1387. classes as well
  1388. }
  1389. parent:=obclss;
  1390. repeat
  1391. gen_objc_ivargc_recursive(parent.symtable,ptrbset,0,il);
  1392. parent:=parent.childof;
  1393. until not assigned(parent);
  1394. { convert bits set to encoded string }
  1395. end;
  1396. *)
  1397. (*
  1398. From Clang:
  1399. /// struct _class_ro_t {
  1400. /// uint32_t const flags;
  1401. /// uint32_t const instanceStart;
  1402. /// uint32_t const instanceSize;
  1403. /// uint32_t const reserved; // only when building for 64bit targets
  1404. /// const uint8_t * const ivarLayout;
  1405. /// const char *const name;
  1406. /// const struct _method_list_t * const baseMethods;
  1407. /// const struct _protocol_list_t *const baseProtocols;
  1408. /// const struct _ivar_list_t *const ivars;
  1409. /// const uint8_t * const weakIvarLayout;
  1410. /// const struct _prop_list_t * const properties;
  1411. /// }
  1412. *)
  1413. procedure tobjcrttiwriter_nonfragile.gen_objc_class_ro_part(list: tasmlist; objclss: tobjectdef; protolistsym: TAsmSymbol; out classrolabel: tasmsymbol; metaclass: boolean);
  1414. const
  1415. CLS_CLASS = 0;
  1416. CLS_META = 1;
  1417. CLS_ROOT = 2;
  1418. OBJC2_CLS_HIDDEN = $10;
  1419. CLS_EXCEPTION = $20;
  1420. var
  1421. methodssym,
  1422. rosym : TAsmSymbol;
  1423. classStrDef : tdef;
  1424. classStrSym,
  1425. ivarslab : TAsmLabel;
  1426. rodef,
  1427. class_type : tdef;
  1428. start,
  1429. size,
  1430. flags : cardinal;
  1431. rttitype : trttitype;
  1432. firstfield : tfieldvarsym;
  1433. i : longint;
  1434. tcb : ttai_typedconstbuilder;
  1435. begin
  1436. { consider every class declared in the implementation section of a unit
  1437. as "hidden"
  1438. }
  1439. flags:=0;
  1440. if (objclss.owner.symtabletype=staticsymtable) and
  1441. current_module.is_unit then
  1442. flags:=OBJC2_CLS_HIDDEN;
  1443. if metaclass then
  1444. begin
  1445. flags:=flags or CLS_META;
  1446. rttitype:=objcmetarortti;
  1447. { metaclass size/start: always size of objc_object }
  1448. class_type:=search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef;
  1449. start:=class_type.size;
  1450. size:=start;
  1451. end
  1452. else
  1453. begin
  1454. flags:=flags or CLS_CLASS;
  1455. rttitype:=objcclassrortti;
  1456. size:=tObjectSymtable(objclss.symtable).datasize;
  1457. { can't simply use childof's datasize, because alignment may cause the
  1458. first field to skip a couple of bytes after the previous end }
  1459. firstfield:=nil;
  1460. for i:=0 to objclss.symtable.SymList.Count-1 do
  1461. if (tsym(objclss.symtable.SymList[i]).typ=fieldvarsym) then
  1462. begin
  1463. firstfield:=tfieldvarsym(objclss.symtable.SymList[i]);
  1464. break;
  1465. end;
  1466. if assigned(firstfield) then
  1467. start:=firstfield.fieldoffset
  1468. else
  1469. { no extra fields -> start = size }
  1470. start:=size;
  1471. end;
  1472. if not assigned(objclss.childof) then
  1473. flags:=flags or CLS_ROOT;
  1474. objcreatestringpoolentry(objclss.objextname^,sp_objcclassnames,sec_objc_class_names,classStrSym,classStrDef);
  1475. { generate methods list }
  1476. gen_objc_methods(list,objclss,methodssym,metaclass,false);
  1477. { generate ivars (nil for metaclass) }
  1478. if metaclass then
  1479. ivarslab:=nil
  1480. else
  1481. gen_objc_ivars(list,objclss,ivarslab);
  1482. { class declaration section }
  1483. tcb:=ctai_typedconstbuilder.create([tcalo_new_section]);
  1484. tcb.begin_anonymous_record(
  1485. internaltypeprefixName[itb_objc_nf_class_ro_part],
  1486. C_alignment,1,
  1487. targetinfos[target_info.system]^.alignment.recordalignmin);
  1488. tcb.emit_ord_const(flags,u32inttype);
  1489. tcb.emit_ord_const(start,u32inttype);
  1490. tcb.emit_ord_const(size,u32inttype);
  1491. { strong ivar layout for garbage collection (deprecated) }
  1492. tcb.emit_tai(tai_const.Create_nil_dataptr,voidpointertype);
  1493. tcb.queue_init(voidpointertype);
  1494. tcb.queue_emit_asmsym(classStrSym,classStrDef);
  1495. ConcatSymOrNil(tcb,methodssym,voidpointertype);
  1496. ConcatSymOrNil(tcb,protolistsym,voidpointertype);
  1497. ConcatSymOrNil(tcb,ivarslab,voidpointertype);
  1498. { weak ivar layout for garbage collection (deprecated) }
  1499. tcb.emit_tai(tai_const.Create_nil_dataptr,voidpointertype);
  1500. { TODO: properties }
  1501. tcb.emit_tai(tai_const.Create_nil_dataptr,voidpointertype);
  1502. rodef:=tcb.end_anonymous_record;
  1503. rosym:=current_asmdata.DefineAsmSymbol(objclss.rtti_mangledname(rttitype),AB_LOCAL,AT_DATA,rodef);
  1504. list.concatList(
  1505. tcb.get_final_asmlist(
  1506. rosym,rodef,
  1507. sec_objc_const,'_OBJC_META_CLASS',sizeof(pint)
  1508. )
  1509. );
  1510. tcb.free;
  1511. classrolabel:=rosym;
  1512. end;
  1513. (*
  1514. From Clang:
  1515. /// struct _class_t {
  1516. /// struct _class_t *isa;
  1517. /// struct _class_t * const superclass;
  1518. /// void *cache;
  1519. /// IMP *vtable;
  1520. /// struct class_ro_t *ro;
  1521. /// }
  1522. ///
  1523. *)
  1524. { Generate rtti for an Objective-C class and its meta-class. }
  1525. procedure tobjcrttiwriter_nonfragile.gen_objc_classes_sections(list:TAsmList; objclss: tobjectdef; out classlabel: TAsmSymbol; out classlabeldef: tdef);
  1526. var
  1527. root : tobjectdef;
  1528. superSym,
  1529. superMetaSym,
  1530. metaisaSym,
  1531. metasym,
  1532. clssym,
  1533. metarosym,
  1534. rosym : TAsmSymbol;
  1535. protolistsym : TAsmLabel;
  1536. vis : TAsmsymbind;
  1537. isatcb,
  1538. metatcb : ttai_typedconstbuilder;
  1539. metadef,
  1540. classdef : tdef;
  1541. begin
  1542. { A) Register necessary names }
  1543. { 1) the current class and metaclass }
  1544. if (objclss.owner.symtabletype=globalsymtable) then
  1545. vis:=AB_GLOBAL
  1546. else
  1547. vis:=AB_PRIVATE_EXTERN;
  1548. { create the typed const builders so we can get the (provisional) types
  1549. for the class and metaclass symbols }
  1550. isatcb:=ctai_typedconstbuilder.create([]);
  1551. classdef:=isatcb.begin_anonymous_record(
  1552. internaltypeprefixName[itb_objc_nf_class],
  1553. C_alignment,1,
  1554. targetinfos[target_info.system]^.alignment.recordalignmin);
  1555. metatcb:=ctai_typedconstbuilder.create([tcalo_new_section]);
  1556. metadef:=metatcb.begin_anonymous_record(
  1557. internaltypeprefixName[itb_objc_nf_meta_class],
  1558. C_alignment,1,
  1559. targetinfos[target_info.system]^.alignment.recordalignmin);
  1560. clssym:=current_asmdata.DefineAsmSymbol(objclss.rtti_mangledname(objcclassrtti),vis,AT_DATA,classdef);
  1561. metasym:=current_asmdata.DefineAsmSymbol(objclss.rtti_mangledname(objcmetartti),vis,AT_DATA,metadef);
  1562. { 2) the superclass and meta superclass }
  1563. if assigned(objclss.childof) then
  1564. begin
  1565. superSym:=current_asmdata.RefAsmSymbol(objclss.childof.rtti_mangledname(objcclassrtti),AT_DATA);
  1566. superMetaSym:=current_asmdata.RefAsmSymbol(objclss.childof.rtti_mangledname(objcmetartti),AT_DATA);
  1567. end
  1568. else
  1569. begin
  1570. superSym:=nil;
  1571. { the class itself }
  1572. superMetaSym:=clssym;
  1573. end;
  1574. { 3) the isa }
  1575. { From Clang: The isa for the meta-class is the root of the hierarchy. }
  1576. root:=objclss;
  1577. while assigned(root.childof) do
  1578. root:=root.childof;
  1579. metaisaSym:=current_asmdata.RefAsmSymbol(root.rtti_mangledname(objcmetartti),AT_DATA);
  1580. { 4) the implemented protocols (same for metaclass and regular class) }
  1581. gen_objc_protocol_list(list,objclss.ImplementedInterfaces,protolistsym);
  1582. { 5) the read-only parts of the class definitions }
  1583. gen_objc_class_ro_part(list,objclss,protolistsym,metarosym,true);
  1584. gen_objc_class_ro_part(list,objclss,protolistsym,rosym,false);
  1585. { B) Class declaration section }
  1586. { both class and metaclass are in the objc_data section for obj-c 2 }
  1587. { 1) meta-class declaration }
  1588. { the isa }
  1589. metatcb.emit_tai(Tai_const.Create_sym(metaisaSym),voidpointertype);
  1590. { the superclass }
  1591. metatcb.emit_tai(Tai_const.Create_sym(superMetaSym),voidpointertype);
  1592. { pointer to cache }
  1593. if not assigned(ObjCEmptyCacheVar) then
  1594. ObjCEmptyCacheVar:=current_asmdata.RefAsmSymbol(target_info.Cprefix+'_objc_empty_cache',AT_DATA);
  1595. metatcb.emit_tai(Tai_const.Create_sym(ObjCEmptyCacheVar),voidpointertype);
  1596. { pointer to vtable }
  1597. if not assigned(ObjCEmptyVtableVar) and
  1598. not(target_info.system in [system_arm_ios,system_aarch64_ios,system_aarch64_darwin,system_i386_iphonesim,system_x86_64_iphonesim,system_aarch64_iphonesim]) then
  1599. ObjCEmptyVtableVar:=current_asmdata.RefAsmSymbol(target_info.Cprefix+'_objc_empty_vtable',AT_DATA);
  1600. ConcatSymOrNil(metatcb,ObjCEmptyVtableVar,voidpointertype);
  1601. { the read-only part }
  1602. metatcb.emit_tai(Tai_const.Create_sym(metarosym),voidpointertype);
  1603. metatcb.end_anonymous_record;
  1604. list.concatList(
  1605. metatcb.get_final_asmlist(
  1606. metasym,metadef,
  1607. sec_objc_data,'_OBJC_CLASS',sizeof(pint)
  1608. )
  1609. );
  1610. metatcb.free;
  1611. { 2) regular class declaration }
  1612. { the isa }
  1613. isatcb.emit_tai(Tai_const.Create_sym(metasym),cpointerdef.getreusable(metadef));
  1614. { the superclass }
  1615. ConcatSymOrNil(isatcb,supersym,voidpointertype);
  1616. { pointer to cache }
  1617. isatcb.emit_tai(Tai_const.Create_sym(ObjCEmptyCacheVar),voidpointertype);
  1618. { pointer to vtable }
  1619. ConcatSymOrNil(isatcb,ObjCEmptyVtableVar,voidpointertype);
  1620. { the read-only part }
  1621. isatcb.emit_tai(Tai_const.Create_sym(rosym),voidpointertype);
  1622. isatcb.end_anonymous_record;
  1623. list.concatList(
  1624. isatcb.get_final_asmlist(
  1625. clssym,classdef,
  1626. sec_objc_data,'_OBJC_CLASS',sizeof(pint)
  1627. )
  1628. );
  1629. isatcb.free;
  1630. classlabel:=clssym;
  1631. classlabeldef:=classdef;
  1632. end;
  1633. procedure tobjcrttiwriter_nonfragile.addclasslist(list: tasmlist; section: tasmsectiontype; const symname: string; classes: tfpobjectlist);
  1634. var
  1635. i: longint;
  1636. sym: TAsmSymbol;
  1637. tcb: ttai_typedconstbuilder;
  1638. arrdef: tdef;
  1639. begin
  1640. if classes.count=0 then
  1641. exit;
  1642. tcb:=ctai_typedconstbuilder.create([tcalo_new_section,tcalo_no_dead_strip]);
  1643. arrdef:=carraydef.getreusable(voidpointertype,classes.count);
  1644. sym:=current_asmdata.DefineAsmSymbol(symname,AB_LOCAL,AT_DATA,arrdef);
  1645. tcb.maybe_begin_aggregate(arrdef);
  1646. for i:=0 to classes.count-1 do
  1647. tcb.emit_tai(
  1648. tai_const.Create_sym(current_asmdata.RefAsmSymbol(tobjectdef(classes[i]).rtti_mangledname(objcclassrtti),AT_DATA)),
  1649. voidpointertype
  1650. );
  1651. tcb.maybe_end_aggregate(arrdef);
  1652. list.concatList(
  1653. tcb.get_final_asmlist(
  1654. sym,arrdef,
  1655. section,symname,sizeof(pint)
  1656. )
  1657. );
  1658. end;
  1659. procedure tobjcrttiwriter_nonfragile.gen_objc_info_sections(list: tasmlist);
  1660. function collectnonlazyclasses(classes: tfpobjectlist): tfpobjectlist;
  1661. var
  1662. symentry : tsym;
  1663. procdef : tprocdef;
  1664. i,j : longint;
  1665. begin
  1666. { non-lazy classes are all classes that define a class method with the
  1667. selector called "load" (simply inheriting this class method is not enough,
  1668. they have to implement it themselves)
  1669. -- TODO: this currently only works if the Pascal identifier is also 'load'! }
  1670. result:=tfpobjectlist.create(false);
  1671. for i:=0 to classes.count-1 do
  1672. begin
  1673. symentry:=tsym(tobjectsymtable(tobjectdef(classes[i]).symtable).find('LOAD'));
  1674. if assigned(symentry) and
  1675. (symentry.typ=procsym) then
  1676. begin
  1677. for j:=0 to tprocsym(symentry).ProcdefList.count do
  1678. begin
  1679. procdef:=tprocdef(tprocsym(symentry).ProcdefList[0]);
  1680. if ((po_classmethod in procdef.procoptions) and
  1681. (procdef.messageinf.str^='load')) then
  1682. begin
  1683. result.add(classes[i]);
  1684. break;
  1685. end;
  1686. end;
  1687. end;
  1688. end;
  1689. end;
  1690. var
  1691. nonlazyclasses,
  1692. nonlazycategories : tfpobjectlist;
  1693. begin
  1694. if (classdefs.count=0) and
  1695. (catdefs.count=0) then
  1696. exit;
  1697. nonlazyclasses:=collectnonlazyclasses(classdefs);
  1698. nonlazycategories:=collectnonlazyclasses(catdefs);
  1699. { this list has to include all classes, also the non-lazy ones }
  1700. addclasslist(list,sec_objc_classlist,target_asm.labelprefix+'_OBJC_LABEL_CLASS_$',classdefs);
  1701. addclasslist(list,sec_objc_nlclasslist,target_asm.labelprefix+'_OBJC_LABEL_NONLAZY_CLASS_$',nonlazyclasses);
  1702. { category and non-lazy category lists }
  1703. addclasslist(list,sec_objc_catlist,target_asm.labelprefix+'_OBJC_LABEL_CATEGORY_$',catdefs);
  1704. addclasslist(list,sec_objc_nlcatlist,target_asm.labelprefix+'_OBJC_LABEL_NONLAZY_CATEGORY_$',nonlazycategories);
  1705. nonlazyclasses.free;
  1706. nonlazycategories.free;
  1707. { the non-fragile abi doesn't have any module info, nor lazy references
  1708. to used classes or to parent classes }
  1709. end;
  1710. constructor tobjcrttiwriter_nonfragile.create;
  1711. begin
  1712. inherited create(oa_nonfragile);
  1713. end;
  1714. {******************************************************************
  1715. RTTI generation -- Main function
  1716. *******************************************************************}
  1717. procedure MaybeGenerateObjectiveCImageInfo(globalst, localst: tsymtable);
  1718. var
  1719. objcrttiwriter: tobjcrttiwriter;
  1720. begin
  1721. if (m_objectivec1 in current_settings.modeswitches) then
  1722. begin
  1723. { generate rtti for all obj-c classes, protocols and categories
  1724. defined in this module. }
  1725. if not(target_info.system in systems_objc_nfabi) then
  1726. objcrttiwriter:=tobjcrttiwriter_fragile.create
  1727. else
  1728. objcrttiwriter:=tobjcrttiwriter_nonfragile.create;
  1729. objcrttiwriter.gen_objc_rtti_sections(current_asmdata.asmlists[al_objc_data],globalst);
  1730. objcrttiwriter.gen_objc_rtti_sections(current_asmdata.asmlists[al_objc_data],localst);
  1731. objcrttiwriter.gen_objc_info_sections(current_asmdata.asmlists[al_objc_data]);
  1732. objcrttiwriter.free;
  1733. end;
  1734. end;
  1735. end.