aasmbase.pas 14 KB

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