objcgutl.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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;
  25. procedure objcfinishstringrefpoolentry(entry: phashsetitem; stringpool: tconstpooltype; refsec, stringsec: tasmsectiontype);
  26. procedure MaybeGenerateObjectiveCImageInfo(globalst, localst: tsymtable);
  27. implementation
  28. uses
  29. globtype,globals,
  30. systems,
  31. aasmtai,
  32. cgbase,cgutils,
  33. objcutil,
  34. symconst,symtype,symsym,symdef,symtable,
  35. verbose;
  36. {******************************************************************
  37. String section helpers
  38. *******************************************************************}
  39. function objcreatestringpoolentryintern(p: pchar; len: longint; pooltype: tconstpooltype; stringsec: tasmsectiontype): TAsmSymbol;
  40. var
  41. entry : PHashSetItem;
  42. strlab : tasmlabel;
  43. pc : pchar;
  44. pool : THashSet;
  45. begin
  46. if current_asmdata.ConstPools[pooltype]=nil then
  47. current_asmdata.ConstPools[pooltype]:=THashSet.Create(64, True, False);
  48. pool := current_asmdata.constpools[pooltype];
  49. entry:=pool.FindOrAdd(p,len);
  50. if not assigned(entry^.data) then
  51. begin
  52. { create new entry }
  53. current_asmdata.getlabel(strlab,alt_data);
  54. entry^.Data:=strlab;
  55. getmem(pc,entry^.keylength+1);
  56. move(entry^.key^,pc^,entry^.keylength);
  57. pc[entry^.keylength]:=#0;
  58. { add the string to the approriate section }
  59. new_section(current_asmdata.asmlists[al_objc_pools],stringsec,strlab.name,sizeof(pint));
  60. current_asmdata.asmlists[al_objc_pools].concat(Tai_label.Create(strlab));
  61. current_asmdata.asmlists[al_objc_pools].concat(Tai_string.Create_pchar(pc,entry^.keylength+1));
  62. Result := strlab;
  63. end
  64. else
  65. Result := TAsmLabel(Entry^.Data);
  66. end;
  67. procedure objcfinishstringrefpoolentry(entry: phashsetitem; stringpool: tconstpooltype; refsec, stringsec: tasmsectiontype);
  68. var
  69. reflab : tasmlabel;
  70. strlab : tasmsymbol;
  71. classname: string;
  72. begin
  73. { have we already generated a reference for this string entry? }
  74. if not assigned(entry^.Data) then
  75. begin
  76. { no, add the string to the associated strings section }
  77. strlab:=objcreatestringpoolentryintern(pchar(entry^.key),entry^.keylength,stringpool,stringsec);
  78. { and now finish the reference }
  79. current_asmdata.getlabel(reflab,alt_data);
  80. entry^.Data:=reflab;
  81. { add a pointer to the string in the string references section }
  82. new_section(current_asmdata.asmlists[al_objc_pools],refsec,reflab.name,sizeof(pint));
  83. current_asmdata.asmlists[al_objc_pools].concat(Tai_label.Create(reflab));
  84. current_asmdata.asmlists[al_objc_pools].concat(Tai_const.Create_sym(strlab));
  85. { in case of a class reference, also add a lazy symbol reference for
  86. the class (the linker requires this). }
  87. if (refsec=sec_objc_cls_refs) then
  88. begin
  89. setlength(classname,entry^.keylength);
  90. move(entry^.key^,classname[1],entry^.keylength);
  91. current_asmdata.asmlists[al_objc_pools].concat(tai_directive.Create(asd_lazy_reference,'.objc_class_name_'+classname));
  92. end;
  93. end;
  94. end;
  95. function objcreatestringpoolentry(const s: string; pooltype: tconstpooltype; stringsec: tasmsectiontype): TAsmSymbol;
  96. begin
  97. result:=objcreatestringpoolentryintern(@s[1],length(s),pooltype,stringsec);
  98. end;
  99. {******************************************************************
  100. RTTI generation
  101. *******************************************************************}
  102. { generate a method list, either of class methods or of instance methods,
  103. and both for obj-c classes and categories. }
  104. procedure gen_objc1_methods(list: tasmlist; objccls: tobjectdef; out methodslabel: tasmlabel; classmethods, iscategory: Boolean);
  105. const
  106. clsSectType : array [Boolean] of tasmsectiontype = (sec_objc_inst_meth, sec_objc_cls_meth);
  107. clsSectName : array [Boolean] of string = ('_OBJC_INST_METH','_OBJC_CLS_METH');
  108. catSectType : array [Boolean] of tasmsectiontype = (sec_objc_cat_inst_meth, sec_objc_cat_cls_meth);
  109. catSectName : array [Boolean] of string = ('_OBJC_CAT_INST_METH','_OBJC_CAT_CLS_METH');
  110. type
  111. method_data = record
  112. def : tprocdef;
  113. selsym : TAsmSymbol;
  114. encsym : TAsmSymbol;
  115. end;
  116. var
  117. i : Integer;
  118. def : tprocdef;
  119. defs : array of method_data;
  120. mcnt : integer;
  121. begin
  122. methodslabel:=nil;
  123. mcnt:=0;
  124. { collect all instance/class methods }
  125. SetLength(defs,objccls.vmtentries.count);
  126. for i:=0 to objccls.vmtentries.count-1 do
  127. begin
  128. def:=pvmtentry(objccls.vmtentries[i])^.procdef;
  129. if Assigned(def.procstarttai) and
  130. (classmethods = (po_classmethod in def.procoptions)) then
  131. begin
  132. defs[mcnt].def:=def;
  133. defs[mcnt].selsym:=objcreatestringpoolentry(def.messageinf.str^,sp_objcvarnames,sec_objc_meth_var_names);
  134. defs[mcnt].encsym:=objcreatestringpoolentry(objcencodemethod(def),sp_objcvartypes,sec_objc_meth_var_types);
  135. inc(mcnt);
  136. end;
  137. end;
  138. if mcnt=0 then
  139. exit;
  140. if iscategory then
  141. new_section(list,clsSectType[classmethods],clsSectName[classmethods],4)
  142. else
  143. new_section(list,catSectType[classmethods],catSectName[classmethods],4);
  144. current_asmdata.getlabel(methodslabel,alt_data);
  145. list.Concat(tai_label.Create(methodslabel));
  146. { not used, always zero }
  147. list.Concat(tai_const.Create_32bit(0));
  148. { number of objc_method entries in the method_list array }
  149. list.Concat(tai_const.Create_32bit(mcnt));
  150. for i := 0 to mcnt - 1 do
  151. begin
  152. { reference to the selector name }
  153. list.Concat(tai_const.Create_sym(defs[i].selsym));
  154. { reference to the obj-c encoded function parameters (signature) }
  155. list.Concat(tai_const.Create_sym(defs[i].encsym));
  156. { mangled name of the method }
  157. list.Concat(tai_const.Create_sym(
  158. current_asmdata.GetAsmSymbol(defs[i].def.objcmangledname)));
  159. end;
  160. end;
  161. { generate an instance variables list for an obj-c class. }
  162. procedure gen_objc1_ivars(list: TAsmList; objccls: tobjectdef; out ivarslabel: TAsmLabel);
  163. type
  164. ivar_data = record
  165. vf : tfieldvarsym;
  166. namesym : TAsmSymbol;
  167. typesym : TAsmSymbol;
  168. end;
  169. var
  170. i : integer;
  171. vf : tfieldvarsym;
  172. vars : array of ivar_data;
  173. vcnt : Integer;
  174. enctype : ansistring;
  175. encerr : tdef;
  176. begin
  177. ivarslabel:=nil;
  178. vcnt:=0;
  179. setLength(vars,objccls.symtable.SymList.Count);
  180. for i:=0 to objccls.symtable.SymList.Count-1 do
  181. if tsym(objccls.symtable.SymList[i]).typ=fieldvarsym then
  182. begin
  183. vf:=tfieldvarsym(objccls.symtable.SymList[i]);
  184. if objctryencodetype(vf.vardef,enctype,encerr) then
  185. begin
  186. vars[vcnt].vf:=vf;
  187. vars[vcnt].namesym:=objcreatestringpoolentry(vf.RealName,sp_objcvarnames,sec_objc_meth_var_names);
  188. vars[vcnt].typesym:=objcreatestringpoolentry(enctype,sp_objcvartypes,sec_objc_meth_var_types);
  189. inc(vcnt);
  190. end
  191. else
  192. { must be caught during parsing }
  193. internalerror(2009090601);
  194. end;
  195. if vcnt=0 then
  196. exit;
  197. new_section(list,sec_objc_instance_vars,'_OBJC_INSTANCE_VARS',sizeof(pint));
  198. current_asmdata.getlabel(ivarslabel,alt_data);
  199. list.Concat(tai_label.Create(ivarslabel));
  200. { objc_ivar_list: first the number of elements }
  201. list.Concat(tai_const.Create_32bit(vcnt));
  202. for i:=0 to vcnt-1 do
  203. begin
  204. { reference to the instance variable name }
  205. list.Concat(tai_const.Create_sym(vars[i].namesym));
  206. { reference to the encoded type }
  207. list.Concat(tai_const.Create_sym(vars[i].typesym));
  208. { and the offset of the field }
  209. list.Concat(tai_const.Create_32bit(vars[i].vf.fieldoffset));
  210. end;
  211. end;
  212. (*
  213. From Clang:
  214. struct _objc_class {
  215. Class isa;
  216. Class super_class;
  217. const char *name;
  218. long version;
  219. long info;
  220. long instance_size;
  221. struct _objc_ivar_list *ivars;
  222. struct _objc_method_list *methods;
  223. struct _objc_cache *cache;
  224. struct _objc_protocol_list *protocols;
  225. // Objective-C 1.0 extensions (<rdr://4585769>) -- for garbage collection
  226. const char *ivar_layout;
  227. struct _objc_class_ext *ext;
  228. };
  229. *)
  230. { Generate rtti for an Objective-C class and its meta-class. }
  231. procedure gen_objc1_classes_sections(list:TAsmList; objclss: tobjectdef; out classlabel: TAsmSymbol);
  232. const
  233. CLS_CLASS = 1;
  234. CLS_META = 2;
  235. META_INST_SIZE = 40+8; // sizeof(objc_class) + 8
  236. var
  237. root : tobjectdef;
  238. superStrSym,
  239. classStrSym,
  240. metaisaStrSym,
  241. metasym,
  242. clssym : TAsmSymbol;
  243. mthdlist,
  244. ivarslist : TAsmLabel;
  245. begin
  246. { generate the class methods list }
  247. gen_objc1_methods(list,objclss,mthdlist,true,false);
  248. { register necessary names }
  249. { 1) the superclass }
  250. if assigned(objclss.childof) then
  251. superStrSym:=objcreatestringpoolentry(objclss.childof.objextname^,sp_objcclassnames,sec_objc_class_names)
  252. else
  253. { not empty string, but nil! }
  254. superStrSym:=nil;
  255. { 2) the current class }
  256. classStrSym:=objcreatestringpoolentry(objclss.objextname^,sp_objcclassnames,sec_objc_class_names);
  257. { 3) the isa }
  258. { From Clang: The isa for the meta-class is the root of the hierarchy. }
  259. root:=objclss;
  260. while assigned(root.childof) do
  261. root:=root.childof;
  262. metaisaStrSym:=objcreatestringpoolentry(root.objextname^,sp_objcclassnames,sec_objc_class_names);
  263. { class declaration section }
  264. new_section(list,sec_objc_meta_class,'_OBJC_META_CLASS',sizeof(pint));
  265. { 1) meta-class declaration (warning: if name changed, also change tclassrefdef.rtti_mangledname) }
  266. metasym:=current_asmdata.DefineAsmSymbol(target_asm.labelprefix+'_OBJC_METACLASS_'+objclss.objextname^,AB_LOCAL,AT_DATA);
  267. list.Concat(tai_symbol.Create(metasym,0));
  268. list.Concat(Tai_const.Create_sym(metaisaStrSym));
  269. { pointer to the superclass name if any, otherwise nil }
  270. if assigned(superstrsym) then
  271. list.Concat(Tai_const.Create_sym(superStrSym))
  272. else
  273. list.concat(tai_const.create_32bit(0));
  274. { pointer to the class name }
  275. list.Concat(Tai_const.Create_sym(classStrSym));
  276. { version is always 0 currently }
  277. list.Concat(Tai_const.Create_32bit(0));
  278. { CLS_META for meta-classes }
  279. list.Concat(Tai_const.Create_32bit(CLS_META));
  280. { size of the meta-class instance: sizeof(objc_class) + 8 bytes }
  281. list.Concat(Tai_const.Create_32bit(META_INST_SIZE) );
  282. { meta-classes don't have ivars list (=0) }
  283. list.Concat(Tai_const.Create_32bit(0));
  284. { class methods list (stored in "__cls_meth" section) }
  285. if Assigned(mthdlist) then
  286. list.Concat(Tai_const.Create_sym(mthdlist))
  287. else
  288. list.Concat(Tai_const.Create_32bit(0));
  289. { From Clang: cache is always nil }
  290. list.Concat(Tai_const.Create_32bit(0));
  291. { TODO: protocols }
  292. list.Concat(Tai_const.Create_32bit(0));
  293. { From Clang: ivar_layout for meta-class is always NULL. }
  294. list.Concat(Tai_const.Create_32bit(0));
  295. { From Clang: The class extension is always unused for meta-classes. }
  296. list.Concat(Tai_const.Create_32bit(0));
  297. { 2) regular class declaration }
  298. { generate the instance methods list }
  299. gen_objc1_methods(list,objclss,mthdlist,false,false);
  300. { generate the instance variables list }
  301. gen_objc1_ivars(list,objclss,ivarslist);
  302. new_section(list,sec_objc_class,'_OBJC_CLASS',sizeof(pint));
  303. clssym:=current_asmdata.DefineAsmSymbol(objclss.rtti_mangledname(fullrtti),AB_LOCAL,AT_DATA);
  304. list.Concat(tai_symbol.Create(clssym,0));
  305. { for class declaration: the is points to the meta-class declaration }
  306. list.Concat(Tai_const.Create_sym(metasym));
  307. { pointer to the super_class name if any, nil otherwise }
  308. if assigned(superStrSym) then
  309. list.Concat(Tai_const.Create_sym(superStrSym))
  310. else
  311. list.Concat(Tai_const.Create_32bit(0));
  312. { pointer to the class name }
  313. list.Concat(Tai_const.Create_sym(classStrSym));
  314. { version is always 0 currently }
  315. list.Concat(Tai_const.Create_32bit(0));
  316. { CLS_CLASS for classes }
  317. list.Concat(Tai_const.Create_32bit(CLS_CLASS));
  318. { size of instance: total size of instance variables }
  319. list.Concat(Tai_const.Create_32bit(tobjectsymtable(objclss.symtable).datasize));
  320. { objc_ivar_list (stored in "__instance_vars" section) }
  321. if assigned(ivarslist) then
  322. list.Concat(Tai_const.Create_sym(ivarslist))
  323. else
  324. list.Concat(tai_const.create_32bit(0));
  325. { instance methods list (stored in "__inst_meth" section) }
  326. if Assigned(mthdlist) then
  327. list.Concat(Tai_const.Create_sym(mthdlist))
  328. else
  329. list.Concat(Tai_const.Create_32bit(0));
  330. { From Clang: cache is always NULL }
  331. list.Concat(Tai_const.Create_32bit(0));
  332. { TODO: protocols }
  333. list.Concat(Tai_const.Create_32bit(0));
  334. { TODO: From Clang: strong ivar_layout, necessary for garbage collection support }
  335. list.Concat(Tai_const.Create_32bit(0));
  336. { TODO: From Clang: weak ivar_layout, necessary for garbage collection support }
  337. list.Concat(Tai_const.Create_32bit(0));
  338. classlabel:=clssym;
  339. end;
  340. { Generate the rtti sections for all obj-c classes defined in st, and return
  341. these classes in the classes list. }
  342. procedure gen_objc1_rtti_sections(list:TAsmList; st:TSymtable; var classsyms, classdefs: tfpobjectlist);
  343. var
  344. i: longint;
  345. def: tdef;
  346. sym : TAsmSymbol;
  347. begin
  348. if not Assigned(st) then
  349. exit;
  350. for i:=0 to st.DefList.Count-1 do
  351. begin
  352. def:=tdef(st.DefList[i]);
  353. if is_objcclass(def) and
  354. not(oo_is_external in tobjectdef(def).objectoptions) then
  355. begin
  356. gen_objc1_classes_sections(list,tobjectdef(def),sym);
  357. classsyms.add(sym);
  358. classdefs.add(def);
  359. end;
  360. end;
  361. end;
  362. { Generate the global information sections (objc_symbols and objc_module_info)
  363. for this module. }
  364. procedure gen_objc1_info_sections(list: tasmlist; classsyms,classdefs: tfpobjectlist);
  365. var
  366. i: longint;
  367. sym : TAsmSymbol;
  368. parent: tobjectdef;
  369. superclasses: tfpobjectlist;
  370. begin
  371. if (classsyms.count<>0) then
  372. begin
  373. new_section(list,sec_objc_symbols,'_OBJC_SYMBOLS',sizeof(pint));
  374. sym := current_asmdata.RefAsmSymbol(target_asm.labelprefix+'_OBJC_SYMBOLS');
  375. { symbol to refer to this information }
  376. list.Concat(tai_symbol.Create(sym,0));
  377. { ??? (always 0 in Clang) }
  378. list.Concat(Tai_const.Create_pint(0));
  379. { ??? (From Clang: always 0, pointer to some selector) }
  380. list.Concat(Tai_const.Create_pint(0));
  381. { From Clang: number of defined classes }
  382. list.Concat(Tai_const.Create_16bit(classsyms.count));
  383. { From Clang: number of defined categories }
  384. list.Concat(Tai_const.Create_16bit(0));
  385. { first all classes }
  386. for i:=0 to classsyms.count-1 do
  387. list.Concat(Tai_const.Create_sym(tasmsymbol(classsyms[i])));
  388. { then all categories }
  389. end
  390. else
  391. sym:=nil;
  392. new_section(list,sec_objc_module_info,'_OBJC_MODULE_INFO',4);
  393. { version number = 7 (always, both for gcc and clang, regardless of objc-1 or 2 }
  394. list.Concat(Tai_const.Create_pint(7));
  395. { sizeof(objc_module): 4 pointer-size entities }
  396. list.Concat(Tai_const.Create_pint(sizeof(pint)*4));
  397. { used to be file name, now unused (points to empty string) }
  398. list.Concat(Tai_const.Create_sym(objcreatestringpoolentry('',sp_objcclassnames,sec_objc_class_names)));
  399. { pointer to classes/categories list declared in this module }
  400. if assigned(sym) then
  401. list.Concat(Tai_const.Create_sym(sym))
  402. else
  403. list.concat(tai_const.create_pint(0));
  404. { Add lazy references to parent classes of all classes defined in this unit }
  405. superclasses:=tfpobjectlist.create(false);
  406. for i:=0 to classdefs.count-1 do
  407. begin
  408. parent:=tobjectdef(classdefs[i]).childof;
  409. if assigned(parent) and
  410. (superclasses.indexof(parent)=-1) then
  411. begin
  412. list.concat(tai_directive.create(asd_lazy_reference,'.objc_class_name_'+parent.objextname^));
  413. superclasses.add(parent);
  414. end;
  415. end;
  416. superclasses.free;
  417. { reference symbols for all classes declaredin this unit }
  418. for i:=0 to classdefs.count-1 do
  419. list.concat(tai_symbol.Createname_global_value('.objc_class_name_'+tobjectdef(classdefs[i]).objextname^,AT_DATA,0,0));
  420. end;
  421. procedure MaybeGenerateObjectiveCImageInfo(globalst, localst: tsymtable);
  422. var
  423. classsyms,
  424. classdefs: tfpobjectlist;
  425. begin
  426. if (m_objectivec1 in current_settings.modeswitches) then
  427. begin
  428. { first 4 bytes contain version information about this section (currently version 0),
  429. next 4 bytes contain flags (currently only regarding whether the code in the object
  430. file supports or requires garbage collection)
  431. }
  432. new_section(current_asmdata.asmlists[al_objc_data],sec_objc_image_info,'_OBJC_IMAGE_INFO',sizeof(pint));
  433. current_asmdata.asmlists[al_objc_data].concat(Tai_symbol.Createname(target_asm.labelprefix+'_OBJC_IMAGE_INFO',AT_LABEL,sizeof(pint)));
  434. current_asmdata.asmlists[al_objc_data].concat(Tai_const.Create_64bit(0));
  435. { generate rtti for all obj-c classes, protocols (todo) and categories (todo)
  436. defined in this module. }
  437. classsyms:=tfpobjectlist.create(false);
  438. classdefs:=tfpobjectlist.create(false);
  439. gen_objc1_rtti_sections(current_asmdata.asmlists[al_objc_data],globalst,classsyms,classdefs);
  440. gen_objc1_rtti_sections(current_asmdata.asmlists[al_objc_data],localst,classsyms,classdefs);
  441. gen_objc1_info_sections(current_asmdata.asmlists[al_objc_data],classsyms,classdefs);
  442. classsyms.free;
  443. classdefs.free;
  444. end;
  445. end;
  446. end.