aasmbase.pas 15 KB

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