objcgutl.pas 68 KB

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