aasmbase.pas 14 KB

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