objcgutl.pas 69 KB

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