cpubase.pas 20 KB

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