aasmbase.pas 16 KB

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