cpubase.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. {
  2. Copyright (c) 1998-2012 by Florian Klaempfl and Peter Vreman
  3. Copyright (c) 2014 by Jonas Maebe and Florian Klaempfl
  4. Contains the base types for Aarch64
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. { Base unit for processor information. This unit contains
  19. enumerations of registers, opcodes, sizes, and other
  20. such things which are processor specific.
  21. }
  22. unit cpubase;
  23. {$define USEINLINE}
  24. {$i fpcdefs.inc}
  25. interface
  26. uses
  27. cutils,cclasses,
  28. globtype,globals,
  29. cpuinfo,
  30. aasmbase,
  31. cgbase
  32. ;
  33. {*****************************************************************************
  34. Assembler Opcodes
  35. *****************************************************************************}
  36. type
  37. TAsmOp= {$i a64op.inc}
  38. { This should define the array of instructions as string }
  39. op2strtable=array[tasmop] of string[11];
  40. const
  41. { First value of opcode enumeration }
  42. firstop = low(tasmop);
  43. { Last value of opcode enumeration }
  44. lastop = high(tasmop);
  45. {*****************************************************************************
  46. Registers
  47. *****************************************************************************}
  48. type
  49. { Number of registers used for indexing in tables }
  50. tregisterindex=0..{$i ra64nor.inc}-1;
  51. const
  52. { Available Superregisters }
  53. {$i ra64sup.inc}
  54. RS_IP0 = RS_X16;
  55. RS_IP1 = RS_X17;
  56. R_SUBWHOLE = R_SUBQ;
  57. { Available Registers }
  58. {$i ra64con.inc}
  59. NR_IP0 = NR_X16;
  60. NR_IP1 = NR_X17;
  61. { Integer Super registers first and last }
  62. first_int_supreg = RS_X0;
  63. { xzr and sp take up a separate super register because some instructions
  64. are ambiguous otherwise }
  65. first_int_imreg = $21;
  66. { Integer Super registers first and last }
  67. first_fpu_supreg = RS_S0;
  68. first_fpu_imreg = $20;
  69. { MM Super register first and last }
  70. first_mm_supreg = RS_S0;
  71. first_mm_imreg = $20;
  72. { Required parameter alignment when calling a routine declared as
  73. stdcall and cdecl. The alignment value should be the one defined
  74. by GCC or the target ABI.
  75. The value of this constant is equal to the constant
  76. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  77. }
  78. std_param_align = 8;
  79. { TODO: Calculate bsstart}
  80. regnumber_count_bsstart = 128;
  81. regnumber_table : array[tregisterindex] of tregister = (
  82. {$i ra64num.inc}
  83. );
  84. regstabs_table : array[tregisterindex] of shortint = (
  85. {$i ra64sta.inc}
  86. );
  87. regdwarf_table : array[tregisterindex] of shortint = (
  88. {$i ra64dwa.inc}
  89. );
  90. { registers which may be destroyed by calls }
  91. VOLATILE_INTREGISTERS = [RS_X0..RS_X18,RS_X30];
  92. VOLATILE_MMREGISTERS = [RS_D0..RS_D7,RS_D16..RS_D31];
  93. type
  94. totherregisterset = set of tregisterindex;
  95. {*****************************************************************************
  96. Instruction post fixes
  97. *****************************************************************************}
  98. type
  99. { ARM instructions load/store and arithmetic instructions
  100. can have several instruction post fixes which are collected
  101. in this enumeration
  102. }
  103. TOpPostfix = (PF_None,
  104. { update condition flags }
  105. PF_S,
  106. { load/store sizes }
  107. PF_B,PF_SB,PF_H,PF_SH,PF_W,PF_SW
  108. );
  109. TOpPostfixes = set of TOpPostfix;
  110. const
  111. tcgsizep2size: array[OS_NO..OS_F128] of byte =
  112. {OS_NO }
  113. (0,
  114. {OS_8,OS_16,OS_32,OS_64,OS_128,OS_S8,OS_S16,OS_S32,OS_S64,OS_S128}
  115. 0, 1, 2, 3, 4, 0, 1, 2, 3, 4,
  116. {OS_F32,OS_F64,OS_F80,OS_C64,OS_F128,}
  117. 2, 3, 0, 3, 4);
  118. oppostfix2str: array[TOpPostfix] of string[2] = ('',
  119. 's',
  120. 'b','sb','h','sh','w','sw');
  121. {*****************************************************************************
  122. Conditions
  123. *****************************************************************************}
  124. type
  125. TAsmCond=(C_None,
  126. C_EQ,C_NE,C_HS,C_LO,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  127. C_GE,C_LT,C_GT,C_LE,C_AL,C_NV
  128. );
  129. TAsmConds = set of TAsmCond;
  130. const
  131. C_CS = C_HS;
  132. C_CC = C_LO;
  133. cond2str : array[TAsmCond] of string[2]=('',
  134. 'eq','ne','hs','lo','mi','pl','vs','vc','hi','ls',
  135. 'ge','lt','gt','le','al','nv'
  136. );
  137. uppercond2str : array[TAsmCond] of string[2]=('',
  138. 'EQ','NE','HS','LO','MI','PL','VS','VC','HI','LS',
  139. 'GE','LT','GT','LE','AL','NV'
  140. );
  141. {*****************************************************************************
  142. Flags
  143. *****************************************************************************}
  144. type
  145. TResFlags = (F_EQ,F_NE,F_CS,F_CC,F_MI,F_PL,F_VS,F_VC,F_HI,F_LS,
  146. F_GE,F_LT,F_GT,F_LE);
  147. const
  148. F_HS = F_CS;
  149. F_LO = F_CC;
  150. {*****************************************************************************
  151. Operands
  152. *****************************************************************************}
  153. type
  154. taddressmode = (AM_OFFSET,AM_PREINDEXED,AM_POSTINDEXED);
  155. tshiftmode = (SM_None,
  156. { shifted register instructions. LSL can also be used for
  157. the index register of certain loads/stores }
  158. SM_LSL,SM_LSR,SM_ASR,
  159. { extended register instructions: zero/sign extension +
  160. optional shift (interpreted as LSL after extension)
  161. -- the index register of certain loads/stores can be
  162. extended via (s|u)xtw with a shiftval of either 0 or
  163. log2(transfer size of the load/store)
  164. }
  165. SM_UXTB,SM_UXTH,SM_UXTW,SM_UXTX,SM_SXTB,SM_SXTH,SM_SXTW,SM_SXTX);
  166. tupdatereg = (UR_None,UR_Update);
  167. pshifterop = ^tshifterop;
  168. tshifterop = record
  169. shiftmode : tshiftmode;
  170. shiftimm : byte;
  171. end;
  172. {*****************************************************************************
  173. Constants
  174. *****************************************************************************}
  175. const
  176. max_operands = 6;
  177. maxintregs = 32;
  178. maxfpuregs = 32;
  179. maxaddrregs = 0;
  180. shiftedregmodes = [SM_LSL,SM_UXTB,SM_UXTH,SM_UXTW,SM_UXTX,SM_SXTB,SM_SXTH,SM_SXTW,SM_SXTX];
  181. extendedregmodes = [SM_LSL,SM_LSR,SM_ASR];
  182. {*****************************************************************************
  183. Operand Sizes
  184. *****************************************************************************}
  185. type
  186. topsize = (S_NO,
  187. S_B,S_W,S_L,S_BW,S_BL,S_WL,
  188. S_IS,S_IL,S_IQ,
  189. S_FS,S_FL,S_FX,S_D,S_Q,S_FV,S_FXX
  190. );
  191. {*****************************************************************************
  192. Default generic sizes
  193. *****************************************************************************}
  194. const
  195. { Defines the default address size for a processor, }
  196. OS_ADDR = OS_64;
  197. { the natural int size for a processor,
  198. has to match osuinttype/ossinttype as initialized in psystem }
  199. OS_INT = OS_64;
  200. OS_SINT = OS_S64;
  201. { the maximum float size for a processor, }
  202. OS_FLOAT = OS_F64;
  203. { the size of a vector register for a processor }
  204. OS_VECTOR = OS_M128;
  205. {*****************************************************************************
  206. Generic Register names
  207. *****************************************************************************}
  208. NR_FP = NR_X29;
  209. RS_FP = RS_X29;
  210. NR_WFP = NR_W29;
  211. RS_WFP = RS_W29;
  212. NR_LR = NR_X30;
  213. RS_LR = RS_X30;
  214. NR_WLR = NR_W30;
  215. RS_WLR = RS_W30;
  216. { Stack pointer register }
  217. NR_STACK_POINTER_REG = NR_SP;
  218. RS_STACK_POINTER_REG = RS_SP;
  219. { Frame pointer register }
  220. NR_FRAME_POINTER_REG = NR_X29;
  221. RS_FRAME_POINTER_REG = RS_X29;
  222. { Register for addressing absolute data in a position independant way,
  223. such as in PIC code. The exact meaning is ABI specific. For
  224. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  225. }
  226. NR_PIC_OFFSET_REG = NR_X18;
  227. { Results are returned in this register (32-bit values) }
  228. NR_FUNCTION_RETURN_REG = NR_X0;
  229. RS_FUNCTION_RETURN_REG = RS_X0;
  230. { The value returned from a function is available in this register }
  231. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  232. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  233. NR_FPU_RESULT_REG = NR_NO;
  234. NR_MM_RESULT_REG = NR_D0;
  235. NR_RETURN_ADDRESS_REG = NR_FUNCTION_RETURN_REG;
  236. { Offset where the parent framepointer is pushed }
  237. PARENT_FRAMEPOINTER_OFFSET = 0;
  238. NR_DEFAULTFLAGS = NR_NZCV;
  239. RS_DEFAULTFLAGS = RS_NZCV;
  240. {*****************************************************************************
  241. GCC /ABI linking information
  242. *****************************************************************************}
  243. const
  244. { Registers which must be saved when calling a routine declared as
  245. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  246. saved should be the ones as defined in the target ABI and / or GCC.
  247. This value can be deduced from the CALLED_USED_REGISTERS array in the
  248. GCC source.
  249. }
  250. saved_standard_registers : array[0..9] of tsuperregister =
  251. (RS_X19,RS_X20,RS_X21,RS_X22,RS_X23,RS_X24,RS_X25,RS_X26,RS_X27,RS_X28);
  252. saved_mm_registers : array[0..7] of tsuperregister = (RS_D8,RS_D9,RS_D10,RS_D11,RS_D12,RS_D13,RS_D14,RS_D15);
  253. { this is only for the generic code which is not used for this architecture }
  254. saved_address_registers : array[0..0] of tsuperregister = (RS_INVALID);
  255. {*****************************************************************************
  256. Helpers
  257. *****************************************************************************}
  258. { Returns the tcgsize corresponding with the size of reg.}
  259. function reg_cgsize(const reg: tregister) : tcgsize;
  260. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  261. function is_calljmp(o:tasmop):boolean;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  262. procedure inverse_flags(var f: TResFlags);
  263. function flags_to_cond(const f: TResFlags) : TAsmCond;
  264. function findreg_by_number(r:Tregister):tregisterindex;
  265. function std_regnum_search(const s:string):Tregister;
  266. function std_regname(r:Tregister):string;
  267. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  268. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  269. procedure shifterop_reset(var so : tshifterop); {$ifdef USEINLINE}inline;{$endif USEINLINE}
  270. function dwarf_reg(r:tregister):shortint;
  271. function is_shifter_const(d: aint; size: tcgsize): boolean;
  272. implementation
  273. uses
  274. systems,rgBase,verbose;
  275. const
  276. std_regname_table : TRegNameTable = (
  277. {$i ra64std.inc}
  278. );
  279. regnumber_index : array[tregisterindex] of tregisterindex = (
  280. {$i ra64rni.inc}
  281. );
  282. std_regname_index : array[tregisterindex] of tregisterindex = (
  283. {$i ra64sri.inc}
  284. );
  285. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  286. begin
  287. case regtype of
  288. R_INTREGISTER:
  289. begin
  290. case s of
  291. { there's only Wn and Xn }
  292. OS_64,
  293. OS_S64:
  294. cgsize2subreg:=R_SUBWHOLE;
  295. else
  296. cgsize2subreg:=R_SUBD;
  297. end;
  298. end;
  299. R_MMREGISTER:
  300. begin
  301. case s of
  302. OS_F32:
  303. cgsize2subreg:=R_SUBMMS;
  304. OS_F64:
  305. cgsize2subreg:=R_SUBMMD;
  306. else
  307. internalerror(2009112701);
  308. end;
  309. end;
  310. else
  311. cgsize2subreg:=R_SUBWHOLE;
  312. end;
  313. end;
  314. function reg_cgsize(const reg: tregister): tcgsize;
  315. begin
  316. case getregtype(reg) of
  317. R_INTREGISTER:
  318. case getsubreg(reg) of
  319. R_SUBD:
  320. result:=OS_32
  321. else
  322. result:=OS_64;
  323. end;
  324. R_MMREGISTER :
  325. begin
  326. case getsubreg(reg) of
  327. R_SUBMMD:
  328. result:=OS_F64;
  329. R_SUBMMS:
  330. result:=OS_F32;
  331. R_SUBMMWHOLE:
  332. result:=OS_M128;
  333. else
  334. internalerror(2009112903);
  335. end;
  336. end;
  337. else
  338. internalerror(200303181);
  339. end;
  340. end;
  341. function is_calljmp(o:tasmop):boolean;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  342. begin
  343. is_calljmp:=o in [A_B,A_BL,A_BLR,A_RET,A_CBNZ,A_CBZ];
  344. end;
  345. procedure inverse_flags(var f: TResFlags);
  346. const
  347. inv_flags: array[TResFlags] of TResFlags =
  348. (F_NE,F_EQ,F_CC,F_CS,F_PL,F_MI,F_VC,F_VS,F_LS,F_HI,
  349. F_LT,F_GE,F_LE,F_GT);
  350. begin
  351. f:=inv_flags[f];
  352. end;
  353. function flags_to_cond(const f: TResFlags) : TAsmCond;
  354. const
  355. flag_2_cond: array[TResFlags] of TAsmCond =
  356. (C_EQ,C_NE,C_HS,C_LO,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  357. C_GE,C_LT,C_GT,C_LE);
  358. begin
  359. if f>high(flag_2_cond) then
  360. internalerror(200112301);
  361. result:=flag_2_cond[f];
  362. end;
  363. function findreg_by_number(r:Tregister):tregisterindex;
  364. begin
  365. result:=rgBase.findreg_by_number_table(r,regnumber_index);
  366. end;
  367. function std_regnum_search(const s:string):Tregister;
  368. begin
  369. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  370. end;
  371. function std_regname(r:Tregister):string;
  372. var
  373. p : tregisterindex;
  374. begin
  375. p:=findreg_by_number_table(r,regnumber_index);
  376. if p<>0 then
  377. result:=std_regname_table[p]
  378. else
  379. result:=generic_regname(r);
  380. end;
  381. procedure shifterop_reset(var so : tshifterop);{$ifdef USEINLINE}inline;{$endif USEINLINE}
  382. begin
  383. FillChar(so,sizeof(so),0);
  384. end;
  385. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  386. const
  387. inverse: array[TAsmCond] of TAsmCond=(C_None,
  388. C_NE,C_EQ,C_LO,C_HS,C_PL,C_MI,C_VC,C_VS,C_LS,C_HI,
  389. C_LT,C_GE,C_LE,C_GT,C_None,C_None
  390. );
  391. begin
  392. result := inverse[c];
  393. end;
  394. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  395. begin
  396. result := c1 = c2;
  397. end;
  398. function dwarf_reg(r:tregister):shortint;
  399. begin
  400. result:=regdwarf_table[findreg_by_number(r)];
  401. if result=-1 then
  402. internalerror(200603251);
  403. end;
  404. function is_shifter_const(d: aint; size: tcgsize): boolean;
  405. var
  406. pattern, checkpattern: qword;
  407. patternlen, maxbits, replicatedlen: longint;
  408. rightmostone, rightmostzero, checkbit, secondrightmostbit: longint;
  409. begin
  410. result:=false;
  411. { patterns with all bits 0 or 1 cannot be represented this way }
  412. if (d=0) then
  413. exit;
  414. case size of
  415. OS_64,
  416. OS_S64:
  417. begin
  418. if d=-1 then
  419. exit;
  420. maxbits:=64;
  421. end
  422. else
  423. begin
  424. if longint(d)=-1 then
  425. exit;
  426. { we'll generate a 32 bit pattern -> ignore upper sign bits in
  427. case of negative longint value }
  428. d:=cardinal(d);
  429. maxbits:=32;
  430. end;
  431. end;
  432. { "The Logical (immediate) instructions accept a bitmask immediate value
  433. that is a 32-bit pattern or a 64-bit pattern viewed as a vector of
  434. identical elements of size e = 2, 4, 8, 16, 32 or, 64 bits. Each
  435. element contains the same sub-pattern, that is a single run of
  436. 1 to (e - 1) nonzero bits from bit 0 followed by zero bits, then
  437. rotated by 0 to (e - 1) bits." (ARMv8 ARM)
  438. Rather than generating all possible patterns and checking whether they
  439. match our constant, we check whether the lowest 2/4/8/... bits are
  440. a valid pattern, and if so whether the constant consists of a
  441. replication of this pattern. Such a valid pattern has the form of
  442. either (regexp notation)
  443. * 1+0+1*
  444. * 0+1+0* }
  445. patternlen:=2;
  446. while patternlen<=maxbits do
  447. begin
  448. { try lowest <patternlen> bits of d as pattern }
  449. if patternlen<>64 then
  450. pattern:=qword(d) and ((qword(1) shl patternlen)-1)
  451. else
  452. pattern:=qword(d);
  453. { valid pattern? If it contains too many 1<->0 transitions, larger
  454. parts of d cannot be a valid pattern either }
  455. rightmostone:=BsfQWord(pattern);
  456. rightmostzero:=BsfQWord(not(pattern));
  457. { pattern all ones or zeroes -> not a valid pattern (but larger ones
  458. can still be valid, since we have too few transitions) }
  459. if (rightmostone<patternlen) and
  460. (rightmostzero<patternlen) then
  461. begin
  462. if rightmostone>rightmostzero then
  463. begin
  464. { we have .*1*0* -> check next zero position by shifting
  465. out the existing zeroes (shr rightmostone), inverting and
  466. then again looking for the rightmost one position }
  467. checkpattern:=not(pattern);
  468. checkbit:=rightmostone;
  469. end
  470. else
  471. begin
  472. { same as above, but for .*0*1* }
  473. checkpattern:=pattern;
  474. checkbit:=rightmostzero;
  475. end;
  476. secondrightmostbit:=BsfQWord(checkpattern shr checkbit)+checkbit;
  477. { if this position is >= patternlen -> ok (1 transition),
  478. otherwise we now have 2 transitions and have to check for a
  479. third (if there is one, abort)
  480. bsf returns 255 if no 1 bit is found, so in that case it's
  481. also ok
  482. }
  483. if secondrightmostbit<patternlen then
  484. begin
  485. secondrightmostbit:=BsfQWord(not(checkpattern) shr secondrightmostbit)+secondrightmostbit;
  486. if secondrightmostbit<patternlen then
  487. exit;
  488. end;
  489. { ok, this is a valid pattern, now does d consist of a
  490. repetition of this pattern? }
  491. replicatedlen:=patternlen;
  492. checkpattern:=pattern;
  493. while replicatedlen<maxbits do
  494. begin
  495. { douplicate current pattern }
  496. checkpattern:=checkpattern or (checkpattern shl replicatedlen);
  497. replicatedlen:=replicatedlen*2;
  498. end;
  499. if qword(d)=checkpattern then
  500. begin
  501. { yes! }
  502. result:=true;
  503. exit;
  504. end;
  505. end;
  506. patternlen:=patternlen*2;
  507. end;
  508. end;
  509. end.