cpubase.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. {
  2. Copyright (c) 1998-2012 by Florian Klaempfl and Peter Vreman
  3. Contains the base types for ARM64
  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. { Base unit for processor information. This unit contains
  18. enumerations of registers, opcodes, sizes, and other
  19. such things which are processor specific.
  20. }
  21. unit cpubase;
  22. {$define USEINLINE}
  23. {$i fpcdefs.inc}
  24. interface
  25. uses
  26. cutils,cclasses,
  27. globtype,globals,
  28. cpuinfo,
  29. aasmbase,
  30. cgbase
  31. ;
  32. {*****************************************************************************
  33. Assembler Opcodes
  34. *****************************************************************************}
  35. type
  36. TAsmOp= {$i a64op.inc}
  37. { This should define the array of instructions as string }
  38. op2strtable=array[tasmop] of string[11];
  39. const
  40. { First value of opcode enumeration }
  41. firstop = low(tasmop);
  42. { Last value of opcode enumeration }
  43. lastop = high(tasmop);
  44. {*****************************************************************************
  45. Registers
  46. *****************************************************************************}
  47. type
  48. { Number of registers used for indexing in tables }
  49. tregisterindex=0..{$i ra64nor.inc}-1;
  50. const
  51. { Available Superregisters }
  52. {$i ra64sup.inc}
  53. R_SUBWHOLE = R_SUBQ;
  54. { Available Registers }
  55. {$i ra64con.inc}
  56. { Integer Super registers first and last }
  57. first_int_supreg = RS_X0;
  58. first_int_imreg = $20;
  59. { Integer Super registers first and last }
  60. first_fpu_supreg = RS_S0;
  61. first_fpu_imreg = $20;
  62. { MM Super register first and last }
  63. first_mm_supreg = RS_S0;
  64. first_mm_imreg = $20;
  65. { Required parameter alignment when calling a routine declared as
  66. stdcall and cdecl. The alignment value should be the one defined
  67. by GCC or the target ABI.
  68. The value of this constant is equal to the constant
  69. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  70. }
  71. std_param_align = 4;
  72. { TODO: Calculate bsstart}
  73. regnumber_count_bsstart = 128;
  74. regnumber_table : array[tregisterindex] of tregister = (
  75. {$i ra64num.inc}
  76. );
  77. regstabs_table : array[tregisterindex] of shortint = (
  78. {$i ra64sta.inc}
  79. );
  80. regdwarf_table : array[tregisterindex] of shortint = (
  81. {$i ra64dwa.inc}
  82. );
  83. { registers which may be destroyed by calls }
  84. VOLATILE_INTREGISTERS = [RS_X0..RS_X18,RS_X29..RS_X30];
  85. VOLATILE_MMREGISTERS = [RS_D0..RS_D7,RS_D16..RS_D31];
  86. type
  87. totherregisterset = set of tregisterindex;
  88. {*****************************************************************************
  89. Instruction post fixes
  90. *****************************************************************************}
  91. type
  92. { ARM instructions load/store and arithmetic instructions
  93. can have several instruction post fixes which are collected
  94. in this enumeration
  95. }
  96. TOpPostfix = (PF_None,
  97. { update condition flags }
  98. PF_S,
  99. { load/store }
  100. PF_B,PF_SB,PF_H,PF_SH
  101. );
  102. TOpPostfixes = set of TOpPostfix;
  103. const
  104. oppostfix2str : array[TOpPostfix] of string[2] = ('',
  105. 's',
  106. 'b','sb','h','sh');
  107. {*****************************************************************************
  108. Conditions
  109. *****************************************************************************}
  110. type
  111. TAsmCond=(C_None,
  112. C_EQ,C_NE,C_HS,C_LO,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  113. C_GE,C_LT,C_GT,C_LE,C_AL,C_NV
  114. );
  115. TAsmConds = set of TAsmCond;
  116. const
  117. cond2str : array[TAsmCond] of string[2]=('',
  118. 'eq','ne','hs','lo','mi','pl','vs','vc','hi','ls',
  119. 'ge','lt','gt','le','al','nv'
  120. );
  121. uppercond2str : array[TAsmCond] of string[2]=('',
  122. 'EQ','NE','hs','LO','MI','PL','VS','VC','HI','LS',
  123. 'GE','LT','GT','LE','AL','NV'
  124. );
  125. {*****************************************************************************
  126. Flags
  127. *****************************************************************************}
  128. type
  129. TResFlags = (F_EQ,F_NE,F_CS,F_CC,F_MI,F_PL,F_VS,F_VC,F_HI,F_LS,
  130. F_GE,F_LT,F_GT,F_LE);
  131. {*****************************************************************************
  132. Operands
  133. *****************************************************************************}
  134. taddressmode = (AM_OFFSET,AM_PREINDEXED,AM_POSTINDEXED);
  135. tshiftmode = (SM_None,SM_LSL,SM_LSR,SM_ASR,SM_ROR);
  136. tupdatereg = (UR_None,UR_Update);
  137. pshifterop = ^tshifterop;
  138. tshifterop = record
  139. shiftmode : tshiftmode;
  140. shiftimm : byte;
  141. end;
  142. tcpumodeflag = (mfA, mfI, mfF);
  143. tcpumodeflags = set of tcpumodeflag;
  144. tspecialregflag = (srC, srX, srS, srF);
  145. tspecialregflags = set of tspecialregflag;
  146. {*****************************************************************************
  147. Constants
  148. *****************************************************************************}
  149. const
  150. max_operands = 6;
  151. maxintregs = 32;
  152. maxfpuregs = 32;
  153. maxaddrregs = 0;
  154. {*****************************************************************************
  155. Operand Sizes
  156. *****************************************************************************}
  157. type
  158. topsize = (S_NO,
  159. S_B,S_W,S_L,S_BW,S_BL,S_WL,
  160. S_IS,S_IL,S_IQ,
  161. S_FS,S_FL,S_FX,S_D,S_Q,S_FV,S_FXX
  162. );
  163. {*****************************************************************************
  164. Default generic sizes
  165. *****************************************************************************}
  166. const
  167. { Defines the default address size for a processor, }
  168. OS_ADDR = OS_64;
  169. { the natural int size for a processor,
  170. has to match osuinttype/ossinttype as initialized in psystem }
  171. OS_INT = OS_64;
  172. OS_SINT = OS_S64;
  173. { the maximum float size for a processor, }
  174. OS_FLOAT = OS_F64;
  175. { the size of a vector register for a processor }
  176. OS_VECTOR = OS_M128;
  177. {*****************************************************************************
  178. Generic Register names
  179. *****************************************************************************}
  180. NR_SP = NR_XZR;
  181. RS_SP = RS_XZR;
  182. NR_WSP = NR_WZR;
  183. RS_WSP = RS_WZR;
  184. { Stack pointer register }
  185. NR_STACK_POINTER_REG = NR_SP;
  186. RS_STACK_POINTER_REG = RS_SP;
  187. { Frame pointer register (initialized in tarmprocinfo.init_framepointer) }
  188. RS_FRAME_POINTER_REG: tsuperregister = RS_X29;
  189. NR_FRAME_POINTER_REG: tregister = NR_X29;
  190. { Register for addressing absolute data in a position independant way,
  191. such as in PIC code. The exact meaning is ABI specific. For
  192. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  193. }
  194. NR_PIC_OFFSET_REG = NR_X18;
  195. { Results are returned in this register (32-bit values) }
  196. NR_FUNCTION_RETURN_REG = NR_X0;
  197. RS_FUNCTION_RETURN_REG = RS_X0;
  198. { The value returned from a function is available in this register }
  199. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  200. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  201. NR_FPU_RESULT_REG = NR_NO;
  202. NR_MM_RESULT_REG = NR_D0;
  203. NR_RETURN_ADDRESS_REG = NR_FUNCTION_RETURN_REG;
  204. { Offset where the parent framepointer is pushed }
  205. PARENT_FRAMEPOINTER_OFFSET = 0;
  206. NR_DEFAULTFLAGS = NR_NZCV;
  207. RS_DEFAULTFLAGS = RS_NZCV;
  208. {*****************************************************************************
  209. GCC /ABI linking information
  210. *****************************************************************************}
  211. const
  212. { Registers which must be saved when calling a routine declared as
  213. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  214. saved should be the ones as defined in the target ABI and / or GCC.
  215. This value can be deduced from the CALLED_USED_REGISTERS array in the
  216. GCC source.
  217. }
  218. saved_standard_registers : array[0..9] of tsuperregister =
  219. (RS_X19,RS_X20,RS_X21,RS_X22,RS_X23,RS_X24,RS_X25,RS_X26,RS_X27,RS_X28);
  220. { this is only for the generic code which is not used for this architecture }
  221. saved_mm_registers : array[0..7] of tsuperregister = (RS_D8,RS_D9,RS_D10,RS_D11,RS_D12,RS_D13,RS_D14,RS_D15);
  222. {*****************************************************************************
  223. Helpers
  224. *****************************************************************************}
  225. { Returns the tcgsize corresponding with the size of reg.}
  226. function reg_cgsize(const reg: tregister) : tcgsize;
  227. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  228. function is_calljmp(o:tasmop):boolean;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  229. procedure inverse_flags(var f: TResFlags);
  230. function flags_to_cond(const f: TResFlags) : TAsmCond;
  231. function findreg_by_number(r:Tregister):tregisterindex;
  232. function std_regnum_search(const s:string):Tregister;
  233. function std_regname(r:Tregister):string;
  234. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  235. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  236. procedure shifterop_reset(var so : tshifterop); {$ifdef USEINLINE}inline;{$endif USEINLINE}
  237. function dwarf_reg(r:tregister):shortint;
  238. implementation
  239. uses
  240. systems,rgBase,verbose;
  241. const
  242. std_regname_table : TRegNameTable = (
  243. {$i ra64std.inc}
  244. );
  245. regnumber_index : array[tregisterindex] of tregisterindex = (
  246. {$i ra64rni.inc}
  247. );
  248. std_regname_index : array[tregisterindex] of tregisterindex = (
  249. {$i ra64sri.inc}
  250. );
  251. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  252. begin
  253. case regtype of
  254. R_MMREGISTER:
  255. begin
  256. case s of
  257. OS_F32:
  258. cgsize2subreg:=R_SUBFS;
  259. OS_F64:
  260. cgsize2subreg:=R_SUBFD;
  261. else
  262. internalerror(2009112701);
  263. end;
  264. end;
  265. else
  266. cgsize2subreg:=R_SUBWHOLE;
  267. end;
  268. end;
  269. function reg_cgsize(const reg: tregister): tcgsize;
  270. begin
  271. case getregtype(reg) of
  272. R_INTREGISTER :
  273. reg_cgsize:=OS_32;
  274. R_FPUREGISTER :
  275. reg_cgsize:=OS_F80;
  276. R_MMREGISTER :
  277. begin
  278. case getsubreg(reg) of
  279. R_SUBFD,
  280. R_SUBWHOLE:
  281. result:=OS_F64;
  282. R_SUBFS:
  283. result:=OS_F32;
  284. else
  285. internalerror(2009112903);
  286. end;
  287. end;
  288. else
  289. internalerror(200303181);
  290. end;
  291. end;
  292. function is_calljmp(o:tasmop):boolean;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  293. begin
  294. { This isn't 100% perfect because the arm allows jumps also by writing to PC=R15.
  295. To overcome this problem we simply forbid that FPC generates jumps by loading R15 }
  296. is_calljmp:= o in [A_B,A_BLR,A_RET];
  297. end;
  298. procedure inverse_flags(var f: TResFlags);
  299. const
  300. inv_flags: array[TResFlags] of TResFlags =
  301. (F_NE,F_EQ,F_CC,F_CS,F_PL,F_MI,F_VC,F_VS,F_LS,F_HI,
  302. F_LT,F_GE,F_LE,F_GT);
  303. begin
  304. f:=inv_flags[f];
  305. end;
  306. function flags_to_cond(const f: TResFlags) : TAsmCond;
  307. const
  308. flag_2_cond: array[F_EQ..F_LE] of TAsmCond =
  309. (C_EQ,C_NE,C_HI,C_LO,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  310. C_GE,C_LT,C_GT,C_LE);
  311. begin
  312. if f>high(flag_2_cond) then
  313. internalerror(200112301);
  314. result:=flag_2_cond[f];
  315. end;
  316. function findreg_by_number(r:Tregister):tregisterindex;
  317. begin
  318. result:=rgBase.findreg_by_number_table(r,regnumber_index);
  319. end;
  320. function std_regnum_search(const s:string):Tregister;
  321. begin
  322. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  323. end;
  324. function std_regname(r:Tregister):string;
  325. var
  326. p : tregisterindex;
  327. begin
  328. p:=findreg_by_number_table(r,regnumber_index);
  329. if p<>0 then
  330. result:=std_regname_table[p]
  331. else
  332. result:=generic_regname(r);
  333. end;
  334. procedure shifterop_reset(var so : tshifterop);{$ifdef USEINLINE}inline;{$endif USEINLINE}
  335. begin
  336. FillChar(so,sizeof(so),0);
  337. end;
  338. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  339. const
  340. inverse: array[TAsmCond] of TAsmCond=(C_None,
  341. C_NE,C_EQ,C_LO,C_HI,C_PL,C_MI,C_VC,C_VS,C_LS,C_HI,
  342. C_LT,C_GE,C_LE,C_GT,C_None,C_None
  343. );
  344. begin
  345. result := inverse[c];
  346. end;
  347. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  348. begin
  349. result := c1 = c2;
  350. end;
  351. function dwarf_reg(r:tregister):shortint;
  352. begin
  353. result:=regdwarf_table[findreg_by_number(r)];
  354. if result=-1 then
  355. internalerror(200603251);
  356. end;
  357. end.