cpubase.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. saved_mm_registers : array[0..7] of tsuperregister = (RS_D8,RS_D9,RS_D10,RS_D11,RS_D12,RS_D13,RS_D14,RS_D15);
  221. { this is only for the generic code which is not used for this architecture }
  222. saved_address_registers : array[0..0] of tsuperregister = (RS_INVALID);
  223. {*****************************************************************************
  224. Helpers
  225. *****************************************************************************}
  226. { Returns the tcgsize corresponding with the size of reg.}
  227. function reg_cgsize(const reg: tregister) : tcgsize;
  228. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  229. function is_calljmp(o:tasmop):boolean;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  230. procedure inverse_flags(var f: TResFlags);
  231. function flags_to_cond(const f: TResFlags) : TAsmCond;
  232. function findreg_by_number(r:Tregister):tregisterindex;
  233. function std_regnum_search(const s:string):Tregister;
  234. function std_regname(r:Tregister):string;
  235. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  236. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  237. procedure shifterop_reset(var so : tshifterop); {$ifdef USEINLINE}inline;{$endif USEINLINE}
  238. function dwarf_reg(r:tregister):shortint;
  239. implementation
  240. uses
  241. systems,rgBase,verbose;
  242. const
  243. std_regname_table : TRegNameTable = (
  244. {$i ra64std.inc}
  245. );
  246. regnumber_index : array[tregisterindex] of tregisterindex = (
  247. {$i ra64rni.inc}
  248. );
  249. std_regname_index : array[tregisterindex] of tregisterindex = (
  250. {$i ra64sri.inc}
  251. );
  252. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  253. begin
  254. case regtype of
  255. R_MMREGISTER:
  256. begin
  257. case s of
  258. OS_F32:
  259. cgsize2subreg:=R_SUBFS;
  260. OS_F64:
  261. cgsize2subreg:=R_SUBFD;
  262. else
  263. internalerror(2009112701);
  264. end;
  265. end;
  266. else
  267. cgsize2subreg:=R_SUBWHOLE;
  268. end;
  269. end;
  270. function reg_cgsize(const reg: tregister): tcgsize;
  271. begin
  272. case getregtype(reg) of
  273. R_INTREGISTER :
  274. reg_cgsize:=OS_32;
  275. R_FPUREGISTER :
  276. reg_cgsize:=OS_F80;
  277. R_MMREGISTER :
  278. begin
  279. case getsubreg(reg) of
  280. R_SUBFD,
  281. R_SUBWHOLE:
  282. result:=OS_F64;
  283. R_SUBFS:
  284. result:=OS_F32;
  285. else
  286. internalerror(2009112903);
  287. end;
  288. end;
  289. else
  290. internalerror(200303181);
  291. end;
  292. end;
  293. function is_calljmp(o:tasmop):boolean;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  294. begin
  295. { This isn't 100% perfect because the arm allows jumps also by writing to PC=R15.
  296. To overcome this problem we simply forbid that FPC generates jumps by loading R15 }
  297. is_calljmp:= o in [A_B,A_BLR,A_RET];
  298. end;
  299. procedure inverse_flags(var f: TResFlags);
  300. const
  301. inv_flags: array[TResFlags] of TResFlags =
  302. (F_NE,F_EQ,F_CC,F_CS,F_PL,F_MI,F_VC,F_VS,F_LS,F_HI,
  303. F_LT,F_GE,F_LE,F_GT);
  304. begin
  305. f:=inv_flags[f];
  306. end;
  307. function flags_to_cond(const f: TResFlags) : TAsmCond;
  308. const
  309. flag_2_cond: array[F_EQ..F_LE] of TAsmCond =
  310. (C_EQ,C_NE,C_HI,C_LO,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  311. C_GE,C_LT,C_GT,C_LE);
  312. begin
  313. if f>high(flag_2_cond) then
  314. internalerror(200112301);
  315. result:=flag_2_cond[f];
  316. end;
  317. function findreg_by_number(r:Tregister):tregisterindex;
  318. begin
  319. result:=rgBase.findreg_by_number_table(r,regnumber_index);
  320. end;
  321. function std_regnum_search(const s:string):Tregister;
  322. begin
  323. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  324. end;
  325. function std_regname(r:Tregister):string;
  326. var
  327. p : tregisterindex;
  328. begin
  329. p:=findreg_by_number_table(r,regnumber_index);
  330. if p<>0 then
  331. result:=std_regname_table[p]
  332. else
  333. result:=generic_regname(r);
  334. end;
  335. procedure shifterop_reset(var so : tshifterop);{$ifdef USEINLINE}inline;{$endif USEINLINE}
  336. begin
  337. FillChar(so,sizeof(so),0);
  338. end;
  339. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  340. const
  341. inverse: array[TAsmCond] of TAsmCond=(C_None,
  342. C_NE,C_EQ,C_LO,C_HI,C_PL,C_MI,C_VC,C_VS,C_LS,C_HI,
  343. C_LT,C_GE,C_LE,C_GT,C_None,C_None
  344. );
  345. begin
  346. result := inverse[c];
  347. end;
  348. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  349. begin
  350. result := c1 = c2;
  351. end;
  352. function dwarf_reg(r:tregister):shortint;
  353. begin
  354. result:=regdwarf_table[findreg_by_number(r)];
  355. if result=-1 then
  356. internalerror(200603251);
  357. end;
  358. end.