aasmbase.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements an abstract asmoutput class for all processor types
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. { @abstract(This unit implements an abstract asm output class for all processor types)
  18. This unit implements an abstract assembler output class for all processors, these
  19. are then overridden for each assembler writer to actually write the data in these
  20. classes to an assembler file.
  21. }
  22. unit aasmbase;
  23. {$i fpcdefs.inc}
  24. interface
  25. uses
  26. cutils,cclasses,
  27. globtype,globals,systems
  28. ;
  29. type
  30. TAsmsymbind=(
  31. AB_NONE,AB_EXTERNAL,AB_COMMON,AB_LOCAL,AB_GLOBAL,AB_WEAK_EXTERNAL,
  32. { global in the current program/library, but not visible outside it }
  33. AB_PRIVATE_EXTERN,AB_LAZY,AB_IMPORT);
  34. TAsmsymtype=(
  35. AT_NONE,AT_FUNCTION,AT_DATA,AT_SECTION,AT_LABEL,
  36. {
  37. the address of this code label is taken somewhere in the code
  38. so it must be taken care of it when creating pic
  39. }
  40. AT_ADDR
  41. );
  42. { is the label only there for getting an DataOffset (e.g. for i/o
  43. checks -> alt_addr) or is it a jump target (alt_jump), for debug
  44. info alt_dbgline and alt_dbgfile, etc. }
  45. TAsmLabelType = (alt_jump,alt_addr,alt_data,alt_dbgline,alt_dbgfile,alt_dbgtype,alt_dbgframe);
  46. const
  47. asmlabeltypeprefix : array[TAsmLabeltype] of char = ('j','a','d','l','f','t','c');
  48. type
  49. TAsmSectiontype=(sec_none,
  50. { this section type allows to define a user named section }
  51. sec_user,
  52. sec_code,
  53. sec_data,
  54. { read-only, but may contain relocations }
  55. sec_rodata,
  56. { read-only and cannot contain relocations }
  57. sec_rodata_norel,
  58. sec_bss,
  59. sec_threadvar,
  60. { used for wince exception handling }
  61. sec_pdata,
  62. { used for darwin import stubs }
  63. sec_stub,
  64. sec_data_nonlazy,
  65. sec_data_lazy,
  66. sec_init_func,
  67. sec_term_func,
  68. { stabs }
  69. sec_stab,sec_stabstr,
  70. { win32 }
  71. sec_idata2,sec_idata4,sec_idata5,sec_idata6,sec_idata7,sec_edata,
  72. { C++ exception handling unwinding (uses dwarf) }
  73. sec_eh_frame,
  74. { dwarf }
  75. sec_debug_frame,
  76. sec_debug_info,
  77. sec_debug_line,
  78. sec_debug_abbrev,
  79. { Yury: "sec_fpc is intended for storing fpc specific data
  80. which must be recognized and processed specially by linker.
  81. Currently fpc version string, dummy links to stab sections
  82. and elf resources are stored in .fpc sections."
  83. "If special .fpc section cannot be used on some target,
  84. .text can be used instead." }
  85. sec_fpc,
  86. sec_rtti,
  87. { Table of contents section }
  88. sec_toc,
  89. sec_init,
  90. sec_fini,
  91. {Objective-C common and fragile ABI }
  92. sec_objc_class,
  93. sec_objc_meta_class,
  94. sec_objc_cat_cls_meth,
  95. sec_objc_cat_inst_meth,
  96. sec_objc_protocol,
  97. sec_objc_string_object,
  98. sec_objc_cls_meth,
  99. sec_objc_inst_meth,
  100. sec_objc_cls_refs,
  101. sec_objc_message_refs,
  102. sec_objc_symbols,
  103. sec_objc_category,
  104. sec_objc_class_vars,
  105. sec_objc_instance_vars,
  106. sec_objc_module_info,
  107. sec_objc_class_names,
  108. sec_objc_meth_var_types,
  109. sec_objc_meth_var_names,
  110. sec_objc_selector_strs,
  111. sec_objc_protocol_ext,
  112. sec_objc_class_ext,
  113. sec_objc_property,
  114. sec_objc_image_info,
  115. sec_objc_cstring_object,
  116. sec_objc_sel_fixup,
  117. { Objective-C non-fragile ABI }
  118. sec_objc_data,
  119. sec_objc_const,
  120. sec_objc_sup_refs,
  121. sec_data_coalesced,
  122. sec_objc_classlist,
  123. sec_objc_nlclasslist,
  124. sec_objc_catlist,
  125. sec_objc_nlcatlist,
  126. sec_objc_protolist
  127. );
  128. TAsmSectionOrder = (secorder_begin,secorder_default,secorder_end);
  129. TAsmSymbol = class(TFPHashObject)
  130. private
  131. { this need to be incremented with every symbol loading into the
  132. TAsmList with loadsym/loadref/const_symbol (PFV) }
  133. refs : longint;
  134. public
  135. { on avr the compiler needs to replace cond. jumps with too large offsets
  136. so we have to store an offset somewhere to calculate jump distances }
  137. {$ifdef AVR}
  138. offset : longint;
  139. {$endif AVR}
  140. bind : TAsmsymbind;
  141. typ : TAsmsymtype;
  142. { Alternate symbol which can be used for 'renaming' needed for
  143. asm inlining. Also used for external and common solving during linking }
  144. altsymbol : TAsmSymbol;
  145. { Cached objsymbol }
  146. cachedobjsymbol : TObject;
  147. constructor Create(AList:TFPHashObjectList;const s:TSymStr;_bind:TAsmsymbind;_typ:Tasmsymtype);
  148. function getaltcopy(AList:TFPHashObjectList;altnr: longint): TAsmSymbol; virtual;
  149. function is_used:boolean;
  150. procedure increfs;
  151. procedure decrefs;
  152. function getrefs: longint;
  153. end;
  154. TAsmSymbolClass = class of TAsmSymbol;
  155. TAsmLabel = class(TAsmSymbol)
  156. protected
  157. function getname:TSymStr;override;
  158. public
  159. labelnr : longint;
  160. labeltype : TAsmLabelType;
  161. is_set : boolean;
  162. constructor Createlocal(AList:TFPHashObjectList;nr:longint;ltyp:TAsmLabelType);
  163. constructor Createglobal(AList:TFPHashObjectList;const modulename:TSymStr;nr:longint;ltyp:TAsmLabelType);
  164. function getaltcopy(AList:TFPHashObjectList;altnr: longint): TAsmSymbol; override;
  165. end;
  166. function create_smartlink_sections:boolean;inline;
  167. function create_smartlink_library:boolean;inline;
  168. function create_smartlink:boolean;inline;
  169. function LengthUleb128(a: qword) : byte;
  170. function LengthSleb128(a: int64) : byte;
  171. function EncodeUleb128(a: qword;out buf) : byte;
  172. function EncodeSleb128(a: int64;out buf) : byte;
  173. function ReplaceForbiddenAsmSymbolChars(const s: string): string;
  174. { dummy default noop callback }
  175. procedure default_global_used;
  176. type
  177. TGlobalUsedProcedure = procedure;
  178. { Procedure variable to allow for special handling of
  179. the occurence of use of a global variable,
  180. used by PIC code generation to request GOT loading }
  181. const
  182. global_used : TGlobalUsedProcedure = @default_global_used;
  183. implementation
  184. uses
  185. SysUtils,
  186. verbose;
  187. function create_smartlink_sections:boolean;inline;
  188. begin
  189. result:=(af_smartlink_sections in target_asm.flags) and
  190. (tf_smartlink_sections in target_info.flags);
  191. end;
  192. function create_smartlink_library:boolean;inline;
  193. begin
  194. result:=(cs_Create_smart in current_settings.moduleswitches) and
  195. (tf_smartlink_library in target_info.flags) and
  196. not create_smartlink_sections;
  197. end;
  198. function create_smartlink:boolean;inline;
  199. begin
  200. result:=(
  201. (af_smartlink_sections in target_asm.flags) and
  202. (tf_smartlink_sections in target_info.flags)
  203. ) or
  204. (
  205. (cs_Create_smart in current_settings.moduleswitches) and
  206. (tf_smartlink_library in target_info.flags)
  207. );
  208. end;
  209. function LengthUleb128(a: qword) : byte;
  210. begin
  211. result:=0;
  212. repeat
  213. a := a shr 7;
  214. inc(result);
  215. if a=0 then
  216. break;
  217. until false;
  218. end;
  219. function LengthSleb128(a: int64) : byte;
  220. var
  221. b, size: byte;
  222. asign : int64;
  223. neg, more: boolean;
  224. begin
  225. more := true;
  226. neg := a < 0;
  227. size := sizeof(a)*8;
  228. result:=0;
  229. repeat
  230. b := a and $7f;
  231. a := a shr 7;
  232. if neg then
  233. begin
  234. { Use a variable to be sure that the correct or mask is generated }
  235. asign:=1;
  236. asign:=asign shl (size - 7);
  237. a := a or -asign;
  238. end;
  239. if (((a = 0) and
  240. (b and $40 = 0)) or
  241. ((a = -1) and
  242. (b and $40 <> 0))) then
  243. more := false;
  244. inc(result);
  245. if not(more) then
  246. break;
  247. until false;
  248. end;
  249. function EncodeUleb128(a: qword;out buf) : byte;
  250. var
  251. b: byte;
  252. pbuf : pbyte;
  253. begin
  254. result:=0;
  255. pbuf:=@buf;
  256. repeat
  257. b := a and $7f;
  258. a := a shr 7;
  259. if a<>0 then
  260. b := b or $80;
  261. pbuf^:=b;
  262. inc(pbuf);
  263. inc(result);
  264. if a=0 then
  265. break;
  266. until false;
  267. end;
  268. function EncodeSleb128(a: int64;out buf) : byte;
  269. var
  270. b, size: byte;
  271. asign : int64;
  272. neg, more: boolean;
  273. pbuf : pbyte;
  274. begin
  275. more := true;
  276. neg := a < 0;
  277. size := sizeof(a)*8;
  278. result:=0;
  279. pbuf:=@buf;
  280. repeat
  281. b := a and $7f;
  282. a := a shr 7;
  283. if neg then
  284. begin
  285. { Use a variable to be sure that the correct or mask is generated }
  286. asign:=1;
  287. asign:=asign shl (size - 7);
  288. a := a or -asign;
  289. end;
  290. if (((a = 0) and
  291. (b and $40 = 0)) or
  292. ((a = -1) and
  293. (b and $40 <> 0))) then
  294. more := false
  295. else
  296. b := b or $80;
  297. pbuf^:=b;
  298. inc(pbuf);
  299. inc(result);
  300. if not(more) then
  301. break;
  302. until false;
  303. end;
  304. function ReplaceForbiddenAsmSymbolChars(const s: string): string;
  305. var
  306. i : longint;
  307. rchar: char;
  308. begin
  309. Result:=s;
  310. rchar:=target_asm.dollarsign;
  311. for i:=1 to Length(Result) do
  312. if Result[i]='$' then
  313. Result[i]:=rchar;
  314. end;
  315. {*****************************************************************************
  316. TAsmSymbol
  317. *****************************************************************************}
  318. constructor TAsmSymbol.Create(AList:TFPHashObjectList;const s:TSymStr;_bind:TAsmsymbind;_typ:Tasmsymtype);
  319. begin;
  320. inherited Create(AList,s);
  321. bind:=_bind;
  322. typ:=_typ;
  323. { used to remove unused labels from the al_procedures }
  324. refs:=0;
  325. end;
  326. function TAsmSymbol.getaltcopy(AList:TFPHashObjectList;altnr: longint): TAsmSymbol;
  327. begin
  328. result := TAsmSymbol(TAsmSymbolClass(classtype).Create(AList,name+'_'+tostr(altnr),bind,typ));
  329. end;
  330. function TAsmSymbol.is_used:boolean;
  331. begin
  332. is_used:=(refs>0);
  333. end;
  334. procedure TAsmSymbol.increfs;
  335. begin
  336. inc(refs);
  337. end;
  338. procedure TAsmSymbol.decrefs;
  339. begin
  340. dec(refs);
  341. if refs<0 then
  342. internalerror(200211121);
  343. end;
  344. function TAsmSymbol.getrefs: longint;
  345. begin
  346. getrefs := refs;
  347. end;
  348. {*****************************************************************************
  349. TAsmLabel
  350. *****************************************************************************}
  351. constructor TAsmLabel.Createlocal(AList:TFPHashObjectList;nr:longint;ltyp:TAsmLabelType);
  352. var
  353. asmtyp: TAsmsymtype;
  354. begin
  355. case ltyp of
  356. alt_addr:
  357. asmtyp:=AT_ADDR;
  358. alt_data:
  359. asmtyp:=AT_DATA;
  360. else
  361. asmtyp:=AT_LABEL;
  362. end;
  363. inherited Create(AList,target_asm.labelprefix+asmlabeltypeprefix[ltyp]+tostr(nr),AB_LOCAL,asmtyp);
  364. labelnr:=nr;
  365. labeltype:=ltyp;
  366. is_set:=false;
  367. end;
  368. constructor TAsmLabel.Createglobal(AList:TFPHashObjectList;const modulename:TSymStr;nr:longint;ltyp:TAsmLabelType);
  369. begin
  370. inherited Create(AList,'_$'+modulename+'$_L'+asmlabeltypeprefix[ltyp]+tostr(nr),AB_GLOBAL,AT_DATA);
  371. labelnr:=nr;
  372. labeltype:=ltyp;
  373. is_set:=false;
  374. { write it always }
  375. increfs;
  376. global_used;
  377. end;
  378. function TAsmLabel.getaltcopy(AList:TFPHashObjectList;altnr: longint): TAsmSymbol;
  379. begin;
  380. result := inherited getaltcopy(AList,altnr);
  381. TAsmLabel(result).labelnr:=labelnr;
  382. TAsmLabel(result).labeltype:=labeltype;
  383. TAsmLabel(result).is_set:=false;
  384. case bind of
  385. AB_GLOBAL,
  386. AB_PRIVATE_EXTERN:
  387. result.increfs;
  388. AB_LOCAL:
  389. ;
  390. else
  391. internalerror(2006053101);
  392. end;
  393. end;
  394. function TAsmLabel.getname:TSymStr;
  395. begin
  396. getname:=inherited getname;
  397. increfs;
  398. end;
  399. procedure default_global_used;
  400. begin
  401. end;
  402. end.