cpubase.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Peter Vreman
  3. Contains the base types for the ARM
  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. {$i fpcdefs.inc}
  23. interface
  24. uses
  25. cutils,cclasses,
  26. globtype,globals,
  27. cpuinfo,
  28. aasmbase,
  29. cgbase
  30. ;
  31. {*****************************************************************************
  32. Assembler Opcodes
  33. *****************************************************************************}
  34. type
  35. TAsmOp= {$i armop.inc}
  36. { This should define the array of instructions as string }
  37. op2strtable=array[tasmop] of string[11];
  38. const
  39. { First value of opcode enumeration }
  40. firstop = low(tasmop);
  41. { Last value of opcode enumeration }
  42. lastop = high(tasmop);
  43. {*****************************************************************************
  44. Registers
  45. *****************************************************************************}
  46. type
  47. { Number of registers used for indexing in tables }
  48. tregisterindex=0..{$i rarmnor.inc}-1;
  49. const
  50. { Available Superregisters }
  51. {$i rarmsup.inc}
  52. RS_PC = RS_R15;
  53. { No Subregisters }
  54. R_SUBWHOLE = R_SUBNONE;
  55. { Available Registers }
  56. {$i rarmcon.inc}
  57. { aliases }
  58. NR_PC = NR_R15;
  59. { Integer Super registers first and last }
  60. first_int_supreg = RS_R0;
  61. first_int_imreg = $10;
  62. { Float Super register first and last }
  63. first_fpu_supreg = RS_F0;
  64. first_fpu_imreg = $08;
  65. { MM Super register first and last }
  66. first_mm_supreg = RS_S0;
  67. first_mm_imreg = $20;
  68. { TODO: Calculate bsstart}
  69. regnumber_count_bsstart = 64;
  70. regnumber_table : array[tregisterindex] of tregister = (
  71. {$i rarmnum.inc}
  72. );
  73. regstabs_table : array[tregisterindex] of shortint = (
  74. {$i rarmsta.inc}
  75. );
  76. regdwarf_table : array[tregisterindex] of shortint = (
  77. {$i rarmdwa.inc}
  78. );
  79. { registers which may be destroyed by calls }
  80. VOLATILE_INTREGISTERS = [RS_R0..RS_R3,RS_R12..RS_R14];
  81. VOLATILE_FPUREGISTERS = [RS_F0..RS_F3];
  82. VOLATILE_MMREGISTERS = [RS_D0..RS_D7,RS_D16..RS_D31];
  83. VOLATILE_INTREGISTERS_DARWIN = [RS_R0..RS_R3,RS_R9,RS_R12..RS_R14];
  84. type
  85. totherregisterset = set of tregisterindex;
  86. {*****************************************************************************
  87. Instruction post fixes
  88. *****************************************************************************}
  89. type
  90. { ARM instructions load/store and arithmetic instructions
  91. can have several instruction post fixes which are collected
  92. in this enumeration
  93. }
  94. TOpPostfix = (PF_None,
  95. { update condition flags
  96. or floating point single }
  97. PF_S,
  98. { floating point size }
  99. PF_D,PF_E,PF_P,PF_EP,
  100. { load/store }
  101. PF_B,PF_SB,PF_BT,PF_H,PF_SH,PF_T,
  102. { multiple load/store address modes }
  103. PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA,
  104. { multiple load/store vfp address modes }
  105. PF_IAD,PF_DBD,PF_FDD,PF_EAD,
  106. PF_IAS,PF_DBS,PF_FDS,PF_EAS,
  107. PF_IAX,PF_DBX,PF_FDX,PF_EAX
  108. );
  109. TRoundingMode = (RM_None,RM_P,RM_M,RM_Z);
  110. const
  111. cgsize2fpuoppostfix : array[OS_NO..OS_F128] of toppostfix = (
  112. PF_None,
  113. PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,
  114. PF_S,PF_D,PF_E,PF_None,PF_None);
  115. oppostfix2str : array[TOpPostfix] of string[3] = ('',
  116. 's',
  117. 'd','e','p','ep',
  118. 'b','sb','bt','h','sh','t',
  119. 'ia','ib','da','db','fd','fa','ed','ea',
  120. 'iad','dbd','fdd','ead',
  121. 'ias','dbs','fds','eas',
  122. 'iax','dbx','fdx','eax');
  123. roundingmode2str : array[TRoundingMode] of string[1] = ('',
  124. 'p','m','z');
  125. {*****************************************************************************
  126. Conditions
  127. *****************************************************************************}
  128. type
  129. TAsmCond=(C_None,
  130. C_EQ,C_NE,C_CS,C_CC,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  131. C_GE,C_LT,C_GT,C_LE,C_AL,C_NV
  132. );
  133. const
  134. cond2str : array[TAsmCond] of string[2]=('',
  135. 'eq','ne','cs','cc','mi','pl','vs','vc','hi','ls',
  136. 'ge','lt','gt','le','al','nv'
  137. );
  138. uppercond2str : array[TAsmCond] of string[2]=('',
  139. 'EQ','NE','CS','CC','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. {*****************************************************************************
  149. Operands
  150. *****************************************************************************}
  151. taddressmode = (AM_OFFSET,AM_PREINDEXED,AM_POSTINDEXED);
  152. tshiftmode = (SM_None,SM_LSL,SM_LSR,SM_ASR,SM_ROR,SM_RRX);
  153. tupdatereg = (UR_None,UR_Update);
  154. pshifterop = ^tshifterop;
  155. tshifterop = record
  156. shiftmode : tshiftmode;
  157. rs : tregister;
  158. shiftimm : byte;
  159. end;
  160. tcpumodeflag = (mfA, mfI, mfF);
  161. tcpumodeflags = set of tcpumodeflag;
  162. {*****************************************************************************
  163. Constants
  164. *****************************************************************************}
  165. const
  166. max_operands = 4;
  167. {# Constant defining possibly all registers which might require saving }
  168. ALL_OTHERREGISTERS = [];
  169. general_superregisters = [RS_R0..RS_PC];
  170. {# Table of registers which can be allocated by the code generator
  171. internally, when generating the code.
  172. }
  173. { legend: }
  174. { xxxregs = set of all possibly used registers of that type in the code }
  175. { generator }
  176. { usableregsxxx = set of all 32bit components of registers that can be }
  177. { possible allocated to a regvar or using getregisterxxx (this }
  178. { excludes registers which can be only used for parameter }
  179. { passing on ABI's that define this) }
  180. { c_countusableregsxxx = amount of registers in the usableregsxxx set }
  181. maxintregs = 15;
  182. { to determine how many registers to use for regvars }
  183. maxintscratchregs = 3;
  184. usableregsint = [RS_R4..RS_R10];
  185. c_countusableregsint = 7;
  186. maxfpuregs = 8;
  187. fpuregs = [RS_F0..RS_F7];
  188. usableregsfpu = [RS_F4..RS_F7];
  189. c_countusableregsfpu = 4;
  190. mmregs = [RS_D0..RS_D15];
  191. usableregsmm = [RS_D8..RS_D15];
  192. c_countusableregsmm = 8;
  193. maxaddrregs = 0;
  194. addrregs = [];
  195. usableregsaddr = [];
  196. c_countusableregsaddr = 0;
  197. {*****************************************************************************
  198. Operand Sizes
  199. *****************************************************************************}
  200. type
  201. topsize = (S_NO,
  202. S_B,S_W,S_L,S_BW,S_BL,S_WL,
  203. S_IS,S_IL,S_IQ,
  204. S_FS,S_FL,S_FX,S_D,S_Q,S_FV,S_FXX
  205. );
  206. {*****************************************************************************
  207. Constants
  208. *****************************************************************************}
  209. const
  210. firstsaveintreg = RS_R4;
  211. lastsaveintreg = RS_R10;
  212. firstsavefpureg = RS_F4;
  213. lastsavefpureg = RS_F7;
  214. firstsavemmreg = RS_D8;
  215. lastsavemmreg = RS_D15;
  216. maxvarregs = 7;
  217. varregs : Array [1..maxvarregs] of tsuperregister =
  218. (RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,RS_R9,RS_R10);
  219. maxfpuvarregs = 4;
  220. fpuvarregs : Array [1..maxfpuvarregs] of tsuperregister =
  221. (RS_F4,RS_F5,RS_F6,RS_F7);
  222. {*****************************************************************************
  223. Default generic sizes
  224. *****************************************************************************}
  225. { Defines the default address size for a processor, }
  226. OS_ADDR = OS_32;
  227. { the natural int size for a processor, }
  228. OS_INT = OS_32;
  229. OS_SINT = OS_S32;
  230. { the maximum float size for a processor, }
  231. OS_FLOAT = OS_F64;
  232. { the size of a vector register for a processor }
  233. OS_VECTOR = OS_M32;
  234. {*****************************************************************************
  235. Generic Register names
  236. *****************************************************************************}
  237. { Stack pointer register }
  238. NR_STACK_POINTER_REG = NR_R13;
  239. RS_STACK_POINTER_REG = RS_R13;
  240. { Frame pointer register }
  241. RS_FRAME_POINTER_REG = RS_R11;
  242. NR_FRAME_POINTER_REG = NR_R11;
  243. { Register for addressing absolute data in a position independant way,
  244. such as in PIC code. The exact meaning is ABI specific. For
  245. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  246. }
  247. NR_PIC_OFFSET_REG = NR_R9;
  248. { Results are returned in this register (32-bit values) }
  249. NR_FUNCTION_RETURN_REG = NR_R0;
  250. RS_FUNCTION_RETURN_REG = RS_R0;
  251. { The value returned from a function is available in this register }
  252. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  253. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  254. NR_FPU_RESULT_REG = NR_F0;
  255. NR_MM_RESULT_REG = NR_NO;
  256. NR_RETURN_ADDRESS_REG = NR_FUNCTION_RETURN_REG;
  257. { Offset where the parent framepointer is pushed }
  258. PARENT_FRAMEPOINTER_OFFSET = 0;
  259. { Low part of 64bit return value }
  260. function NR_FUNCTION_RESULT64_LOW_REG: tregister;
  261. function RS_FUNCTION_RESULT64_LOW_REG: shortint;
  262. { High part of 64bit return value }
  263. function NR_FUNCTION_RESULT64_HIGH_REG: tregister;
  264. function RS_FUNCTION_RESULT64_HIGH_REG: shortint;
  265. {*****************************************************************************
  266. GCC /ABI linking information
  267. *****************************************************************************}
  268. const
  269. { Registers which must be saved when calling a routine declared as
  270. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  271. saved should be the ones as defined in the target ABI and / or GCC.
  272. This value can be deduced from the CALLED_USED_REGISTERS array in the
  273. GCC source.
  274. }
  275. saved_standard_registers : array[0..6] of tsuperregister =
  276. (RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,RS_R9,RS_R10);
  277. { this is only for the generic code which is not used for this architecture }
  278. saved_mm_registers : array[0..0] of tsuperregister = (RS_NO);
  279. { Required parameter alignment when calling a routine declared as
  280. stdcall and cdecl. The alignment value should be the one defined
  281. by GCC or the target ABI.
  282. The value of this constant is equal to the constant
  283. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  284. }
  285. std_param_align = 4;
  286. {*****************************************************************************
  287. Helpers
  288. *****************************************************************************}
  289. { Returns the tcgsize corresponding with the size of reg.}
  290. function reg_cgsize(const reg: tregister) : tcgsize;
  291. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  292. function is_calljmp(o:tasmop):boolean;
  293. procedure inverse_flags(var f: TResFlags);
  294. function flags_to_cond(const f: TResFlags) : TAsmCond;
  295. function findreg_by_number(r:Tregister):tregisterindex;
  296. function std_regnum_search(const s:string):Tregister;
  297. function std_regname(r:Tregister):string;
  298. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  299. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  300. procedure shifterop_reset(var so : tshifterop);
  301. function is_pc(const r : tregister) : boolean;
  302. function is_shifter_const(d : aint;var imm_shift : byte) : boolean;
  303. function dwarf_reg(r:tregister):shortint;
  304. implementation
  305. uses
  306. systems,rgBase,verbose;
  307. const
  308. std_regname_table : array[tregisterindex] of string[7] = (
  309. {$i rarmstd.inc}
  310. );
  311. regnumber_index : array[tregisterindex] of tregisterindex = (
  312. {$i rarmrni.inc}
  313. );
  314. std_regname_index : array[tregisterindex] of tregisterindex = (
  315. {$i rarmsri.inc}
  316. );
  317. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  318. begin
  319. case regtype of
  320. R_MMREGISTER:
  321. begin
  322. case s of
  323. OS_F32:
  324. cgsize2subreg:=R_SUBFS;
  325. OS_F64:
  326. cgsize2subreg:=R_SUBFD;
  327. else
  328. internalerror(2009112701);
  329. end;
  330. end;
  331. else
  332. cgsize2subreg:=R_SUBWHOLE;
  333. end;
  334. end;
  335. function reg_cgsize(const reg: tregister): tcgsize;
  336. begin
  337. case getregtype(reg) of
  338. R_INTREGISTER :
  339. reg_cgsize:=OS_32;
  340. R_FPUREGISTER :
  341. reg_cgsize:=OS_F80;
  342. R_MMREGISTER :
  343. begin
  344. case getsubreg(reg) of
  345. R_SUBFD,
  346. R_SUBWHOLE:
  347. result:=OS_F64;
  348. R_SUBFS:
  349. result:=OS_F32;
  350. else
  351. internalerror(2009112903);
  352. end;
  353. end;
  354. else
  355. internalerror(200303181);
  356. end;
  357. end;
  358. function is_calljmp(o:tasmop):boolean;
  359. begin
  360. { This isn't 100% perfect because the arm allows jumps also by writing to PC=R15.
  361. To overcome this problem we simply forbid that FPC generates jumps by loading R15 }
  362. is_calljmp:= o in [A_B,A_BL,A_BX,A_BLX];
  363. end;
  364. procedure inverse_flags(var f: TResFlags);
  365. const
  366. inv_flags: array[TResFlags] of TResFlags =
  367. (F_NE,F_EQ,F_CC,F_CS,F_PL,F_MI,F_VC,F_VS,F_LS,F_HI,
  368. F_LT,F_GE,F_LE,F_GT);
  369. begin
  370. f:=inv_flags[f];
  371. end;
  372. function flags_to_cond(const f: TResFlags) : TAsmCond;
  373. const
  374. flag_2_cond: array[F_EQ..F_LE] of TAsmCond =
  375. (C_EQ,C_NE,C_CS,C_CC,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  376. C_GE,C_LT,C_GT,C_LE);
  377. begin
  378. if f>high(flag_2_cond) then
  379. internalerror(200112301);
  380. result:=flag_2_cond[f];
  381. end;
  382. function findreg_by_number(r:Tregister):tregisterindex;
  383. begin
  384. result:=rgBase.findreg_by_number_table(r,regnumber_index);
  385. end;
  386. function std_regnum_search(const s:string):Tregister;
  387. begin
  388. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  389. end;
  390. function std_regname(r:Tregister):string;
  391. var
  392. p : tregisterindex;
  393. begin
  394. p:=findreg_by_number_table(r,regnumber_index);
  395. if p<>0 then
  396. result:=std_regname_table[p]
  397. else
  398. result:=generic_regname(r);
  399. end;
  400. procedure shifterop_reset(var so : tshifterop);
  401. begin
  402. FillChar(so,sizeof(so),0);
  403. end;
  404. function is_pc(const r : tregister) : boolean;
  405. begin
  406. is_pc:=(r=NR_R15);
  407. end;
  408. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  409. const
  410. inverse: array[TAsmCond] of TAsmCond=(C_None,
  411. C_NE,C_EQ,C_CC,C_CS,C_PL,C_MI,C_VC,C_VS,C_LS,C_HI,
  412. C_LT,C_GE,C_LE,C_GT,C_None,C_None
  413. );
  414. begin
  415. result := inverse[c];
  416. end;
  417. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  418. begin
  419. result := c1 = c2;
  420. end;
  421. function rotl(d : dword;b : byte) : dword;
  422. begin
  423. result:=(d shr (32-b)) or (d shl b);
  424. end;
  425. function is_shifter_const(d : aint;var imm_shift : byte) : boolean;
  426. var
  427. i : longint;
  428. begin
  429. if current_settings.cputype in cpu_thumb2 then
  430. begin
  431. for i:=0 to 24 do
  432. begin
  433. if (dword(d) and not($ff shl i))=0 then
  434. begin
  435. imm_shift:=i;
  436. result:=true;
  437. exit;
  438. end;
  439. end;
  440. end
  441. else
  442. begin
  443. for i:=0 to 15 do
  444. begin
  445. if (dword(d) and not(rotl($ff,i*2)))=0 then
  446. begin
  447. imm_shift:=i*2;
  448. result:=true;
  449. exit;
  450. end;
  451. end;
  452. end;
  453. result:=false;
  454. end;
  455. function dwarf_reg(r:tregister):shortint;
  456. begin
  457. result:=regdwarf_table[findreg_by_number(r)];
  458. if result=-1 then
  459. internalerror(200603251);
  460. end;
  461. { Low part of 64bit return value }
  462. function NR_FUNCTION_RESULT64_LOW_REG: tregister;
  463. begin
  464. if target_info.endian=endian_little then
  465. result:=NR_R0
  466. else
  467. result:=NR_R1;
  468. end;
  469. function RS_FUNCTION_RESULT64_LOW_REG: shortint;
  470. begin
  471. if target_info.endian=endian_little then
  472. result:=RS_R0
  473. else
  474. result:=RS_R1;
  475. end;
  476. { High part of 64bit return value }
  477. function NR_FUNCTION_RESULT64_HIGH_REG: tregister;
  478. begin
  479. if target_info.endian=endian_little then
  480. result:=NR_R1
  481. else
  482. result:=NR_R0;
  483. end;
  484. function RS_FUNCTION_RESULT64_HIGH_REG: shortint;
  485. begin
  486. if target_info.endian=endian_little then
  487. result:=RS_R1
  488. else
  489. result:=RS_R0;
  490. end;
  491. end.