objcgutl.pas 67 KB

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