objcgutl.pas 17 KB

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