aasmbase.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. {$push}{$warnings off}
  174. { new visibility section to let "warnings off" take effect }
  175. protected
  176. { this constructor is only supposed to be used internally by
  177. createstatoc/createlocal -> disable warning that constructors should
  178. be public }
  179. constructor create_non_global(AList: TFPHashObjectList; nr: longint; ltyp: TAsmLabelType; const prefix: TSymStr);
  180. public
  181. {$pop}
  182. labelnr : longint;
  183. labeltype : TAsmLabelType;
  184. is_set : boolean;
  185. constructor Createlocal(AList: TFPHashObjectList; nr: longint; ltyp: TAsmLabelType);
  186. constructor Createstatic(AList: TFPHashObjectList; nr: longint; ltyp: TAsmLabelType);
  187. constructor Createglobal(AList: TFPHashObjectList; const modulename: TSymStr; nr: longint; ltyp: TAsmLabelType);
  188. function getaltcopy(AList:TFPHashObjectList;altnr: longint): TAsmSymbol; override;
  189. end;
  190. function create_smartlink_sections:boolean;inline;
  191. function create_smartlink_library:boolean;inline;
  192. function create_smartlink:boolean;inline;
  193. function LengthUleb128(a: qword) : byte;
  194. function LengthSleb128(a: int64) : byte;
  195. function EncodeUleb128(a: qword;out buf) : byte;
  196. function EncodeSleb128(a: int64;out buf) : byte;
  197. function ReplaceForbiddenAsmSymbolChars(const s: ansistring): ansistring;
  198. { dummy default noop callback }
  199. procedure default_global_used;
  200. type
  201. { Procedure variable to allow for special handling of
  202. the occurence of use of a global variable,
  203. used by PIC code generation to request GOT loading }
  204. TGlobalUsedProcedure = procedure;
  205. const
  206. global_used : TGlobalUsedProcedure = @default_global_used;
  207. implementation
  208. uses
  209. verbose;
  210. function create_smartlink_sections:boolean;inline;
  211. begin
  212. result:=(af_smartlink_sections in target_asm.flags) and
  213. (tf_smartlink_sections in target_info.flags);
  214. end;
  215. function create_smartlink_library:boolean;inline;
  216. begin
  217. result:=(cs_Create_smart in current_settings.moduleswitches) and
  218. (tf_smartlink_library in target_info.flags) and
  219. not create_smartlink_sections;
  220. end;
  221. function create_smartlink:boolean;inline;
  222. begin
  223. result:=(
  224. (af_smartlink_sections in target_asm.flags) and
  225. (tf_smartlink_sections in target_info.flags)
  226. ) or
  227. (
  228. (cs_Create_smart in current_settings.moduleswitches) and
  229. (tf_smartlink_library in target_info.flags)
  230. );
  231. end;
  232. function LengthUleb128(a: qword) : byte;
  233. begin
  234. result:=0;
  235. repeat
  236. a := a shr 7;
  237. inc(result);
  238. if a=0 then
  239. break;
  240. until false;
  241. end;
  242. function LengthSleb128(a: int64) : byte;
  243. var
  244. b, size: byte;
  245. asign : int64;
  246. neg, more: boolean;
  247. begin
  248. more := true;
  249. neg := a < 0;
  250. size := sizeof(a)*8;
  251. result:=0;
  252. repeat
  253. b := a and $7f;
  254. a := a shr 7;
  255. if neg then
  256. begin
  257. { Use a variable to be sure that the correct or mask is generated }
  258. asign:=1;
  259. asign:=asign shl (size - 7);
  260. a := a or -asign;
  261. end;
  262. if (((a = 0) and
  263. (b and $40 = 0)) or
  264. ((a = -1) and
  265. (b and $40 <> 0))) then
  266. more := false;
  267. inc(result);
  268. if not(more) then
  269. break;
  270. until false;
  271. end;
  272. function EncodeUleb128(a: qword;out buf) : byte;
  273. var
  274. b: byte;
  275. pbuf : pbyte;
  276. begin
  277. result:=0;
  278. pbuf:=@buf;
  279. repeat
  280. b := a and $7f;
  281. a := a shr 7;
  282. if a<>0 then
  283. b := b or $80;
  284. pbuf^:=b;
  285. inc(pbuf);
  286. inc(result);
  287. if a=0 then
  288. break;
  289. until false;
  290. end;
  291. function EncodeSleb128(a: int64;out buf) : byte;
  292. var
  293. b, size: byte;
  294. asign : int64;
  295. neg, more: boolean;
  296. pbuf : pbyte;
  297. begin
  298. more := true;
  299. neg := a < 0;
  300. size := sizeof(a)*8;
  301. result:=0;
  302. pbuf:=@buf;
  303. repeat
  304. b := a and $7f;
  305. a := a shr 7;
  306. if neg then
  307. begin
  308. { Use a variable to be sure that the correct or mask is generated }
  309. asign:=1;
  310. asign:=asign shl (size - 7);
  311. a := a or -asign;
  312. end;
  313. if (((a = 0) and
  314. (b and $40 = 0)) or
  315. ((a = -1) and
  316. (b and $40 <> 0))) then
  317. more := false
  318. else
  319. b := b or $80;
  320. pbuf^:=b;
  321. inc(pbuf);
  322. inc(result);
  323. if not(more) then
  324. break;
  325. until false;
  326. end;
  327. function ReplaceForbiddenAsmSymbolChars(const s: ansistring): ansistring;
  328. var
  329. i : longint;
  330. rchar: char;
  331. begin
  332. Result:=s;
  333. rchar:=target_asm.dollarsign;
  334. for i:=1 to Length(Result) do
  335. if Result[i]='$' then
  336. Result[i]:=rchar;
  337. end;
  338. {*****************************************************************************
  339. TAsmSymbol
  340. *****************************************************************************}
  341. constructor TAsmSymbol.Create(AList:TFPHashObjectList;const s:TSymStr;_bind:TAsmsymbind;_typ:Tasmsymtype);
  342. begin;
  343. inherited Create(AList,s);
  344. bind:=_bind;
  345. typ:=_typ;
  346. { used to remove unused labels from the al_procedures }
  347. refs:=0;
  348. end;
  349. function TAsmSymbol.getaltcopy(AList:TFPHashObjectList;altnr: longint): TAsmSymbol;
  350. begin
  351. result := TAsmSymbol(TAsmSymbolClass(classtype).Create(AList,name+'_'+tostr(altnr),bind,typ));
  352. end;
  353. function TAsmSymbol.is_used:boolean;
  354. begin
  355. is_used:=(refs>0);
  356. end;
  357. procedure TAsmSymbol.increfs;
  358. begin
  359. inc(refs);
  360. end;
  361. procedure TAsmSymbol.decrefs;
  362. begin
  363. dec(refs);
  364. if refs<0 then
  365. internalerror(200211121);
  366. end;
  367. function TAsmSymbol.getrefs: longint;
  368. begin
  369. getrefs := refs;
  370. end;
  371. {*****************************************************************************
  372. TAsmLabel
  373. *****************************************************************************}
  374. constructor TAsmLabel.Createlocal(AList: TFPHashObjectList; nr: longint; ltyp: TAsmLabelType);
  375. begin
  376. create_non_global(AList,nr,ltyp,target_asm.labelprefix);
  377. end;
  378. constructor TAsmLabel.Createstatic(AList:TFPHashObjectList;nr:longint;ltyp:TAsmLabelType);
  379. begin
  380. create_non_global(AList,nr,ltyp,'_$$fpclocal$_l');
  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. constructor TAsmLabel.create_non_global(AList: TFPHashObjectList; nr: longint; ltyp: TAsmLabelType; const prefix: TSymStr);
  414. var
  415. asmtyp: TAsmsymtype;
  416. begin
  417. case ltyp of
  418. alt_addr:
  419. asmtyp:=AT_ADDR;
  420. alt_data:
  421. asmtyp:=AT_DATA;
  422. else
  423. asmtyp:=AT_LABEL;
  424. end;
  425. inherited Create(AList,prefix+asmlabeltypeprefix[ltyp]+tostr(nr),AB_LOCAL,asmtyp);
  426. labelnr:=nr;
  427. labeltype:=ltyp;
  428. is_set:=false;
  429. end;
  430. procedure default_global_used;
  431. begin
  432. end;
  433. end.