cpubase.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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 = $30;
  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,RS_S1..RS_S15];
  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. TOpPostfixes = set of TOpPostfix;
  110. TRoundingMode = (RM_None,RM_P,RM_M,RM_Z);
  111. const
  112. cgsize2fpuoppostfix : array[OS_NO..OS_F128] of toppostfix = (
  113. PF_None,
  114. PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,
  115. PF_S,PF_D,PF_E,PF_None,PF_None);
  116. oppostfix2str : array[TOpPostfix] of string[3] = ('',
  117. 's',
  118. 'd','e','p','ep',
  119. 'b','sb','bt','h','sh','t',
  120. 'ia','ib','da','db','fd','fa','ed','ea',
  121. 'iad','dbd','fdd','ead',
  122. 'ias','dbs','fds','eas',
  123. 'iax','dbx','fdx','eax');
  124. roundingmode2str : array[TRoundingMode] of string[1] = ('',
  125. 'p','m','z');
  126. {*****************************************************************************
  127. Conditions
  128. *****************************************************************************}
  129. type
  130. TAsmCond=(C_None,
  131. C_EQ,C_NE,C_CS,C_CC,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  132. C_GE,C_LT,C_GT,C_LE,C_AL,C_NV
  133. );
  134. TAsmConds = set of TAsmCond;
  135. const
  136. cond2str : array[TAsmCond] of string[2]=('',
  137. 'eq','ne','cs','cc','mi','pl','vs','vc','hi','ls',
  138. 'ge','lt','gt','le','al','nv'
  139. );
  140. uppercond2str : array[TAsmCond] of string[2]=('',
  141. 'EQ','NE','CS','CC','MI','PL','VS','VC','HI','LS',
  142. 'GE','LT','GT','LE','AL','NV'
  143. );
  144. {*****************************************************************************
  145. Flags
  146. *****************************************************************************}
  147. type
  148. TResFlags = (F_EQ,F_NE,F_CS,F_CC,F_MI,F_PL,F_VS,F_VC,F_HI,F_LS,
  149. F_GE,F_LT,F_GT,F_LE);
  150. {*****************************************************************************
  151. Operands
  152. *****************************************************************************}
  153. taddressmode = (AM_OFFSET,AM_PREINDEXED,AM_POSTINDEXED);
  154. tshiftmode = (SM_None,SM_LSL,SM_LSR,SM_ASR,SM_ROR,SM_RRX);
  155. tupdatereg = (UR_None,UR_Update);
  156. pshifterop = ^tshifterop;
  157. tshifterop = record
  158. shiftmode : tshiftmode;
  159. rs : tregister;
  160. shiftimm : byte;
  161. end;
  162. tcpumodeflag = (mfA, mfI, mfF);
  163. tcpumodeflags = set of tcpumodeflag;
  164. {*****************************************************************************
  165. Constants
  166. *****************************************************************************}
  167. const
  168. max_operands = 4;
  169. maxintregs = 15;
  170. maxfpuregs = 8;
  171. maxaddrregs = 0;
  172. {*****************************************************************************
  173. Operand Sizes
  174. *****************************************************************************}
  175. type
  176. topsize = (S_NO,
  177. S_B,S_W,S_L,S_BW,S_BL,S_WL,
  178. S_IS,S_IL,S_IQ,
  179. S_FS,S_FL,S_FX,S_D,S_Q,S_FV,S_FXX
  180. );
  181. {*****************************************************************************
  182. Constants
  183. *****************************************************************************}
  184. const
  185. maxvarregs = 7;
  186. varregs : Array [1..maxvarregs] of tsuperregister =
  187. (RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,RS_R9,RS_R10);
  188. maxfpuvarregs = 4;
  189. fpuvarregs : Array [1..maxfpuvarregs] of tsuperregister =
  190. (RS_F4,RS_F5,RS_F6,RS_F7);
  191. {*****************************************************************************
  192. Default generic sizes
  193. *****************************************************************************}
  194. { Defines the default address size for a processor, }
  195. OS_ADDR = OS_32;
  196. { the natural int size for a processor,
  197. has to match osuinttype/ossinttype as initialized in psystem }
  198. OS_INT = OS_32;
  199. OS_SINT = OS_S32;
  200. { the maximum float size for a processor, }
  201. OS_FLOAT = OS_F64;
  202. { the size of a vector register for a processor }
  203. OS_VECTOR = OS_M32;
  204. {*****************************************************************************
  205. Generic Register names
  206. *****************************************************************************}
  207. { Stack pointer register }
  208. NR_STACK_POINTER_REG = NR_R13;
  209. RS_STACK_POINTER_REG = RS_R13;
  210. { Frame pointer register (initialized in tarmprocinfo.init_framepointer) }
  211. RS_FRAME_POINTER_REG: tsuperregister = RS_NO;
  212. NR_FRAME_POINTER_REG: tregister = NR_NO;
  213. { Register for addressing absolute data in a position independant way,
  214. such as in PIC code. The exact meaning is ABI specific. For
  215. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  216. }
  217. NR_PIC_OFFSET_REG = NR_R9;
  218. { Results are returned in this register (32-bit values) }
  219. NR_FUNCTION_RETURN_REG = NR_R0;
  220. RS_FUNCTION_RETURN_REG = RS_R0;
  221. { The value returned from a function is available in this register }
  222. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  223. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  224. NR_FPU_RESULT_REG = NR_F0;
  225. NR_MM_RESULT_REG = NR_D0;
  226. NR_RETURN_ADDRESS_REG = NR_FUNCTION_RETURN_REG;
  227. { Offset where the parent framepointer is pushed }
  228. PARENT_FRAMEPOINTER_OFFSET = 0;
  229. { Low part of 64bit return value }
  230. function NR_FUNCTION_RESULT64_LOW_REG: tregister;
  231. function RS_FUNCTION_RESULT64_LOW_REG: shortint;
  232. { High part of 64bit return value }
  233. function NR_FUNCTION_RESULT64_HIGH_REG: tregister;
  234. function RS_FUNCTION_RESULT64_HIGH_REG: shortint;
  235. {*****************************************************************************
  236. GCC /ABI linking information
  237. *****************************************************************************}
  238. const
  239. { Registers which must be saved when calling a routine declared as
  240. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  241. saved should be the ones as defined in the target ABI and / or GCC.
  242. This value can be deduced from the CALLED_USED_REGISTERS array in the
  243. GCC source.
  244. }
  245. saved_standard_registers : array[0..6] of tsuperregister =
  246. (RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,RS_R9,RS_R10);
  247. { this is only for the generic code which is not used for this architecture }
  248. saved_mm_registers : array[0..0] of tsuperregister = (RS_NO);
  249. { Required parameter alignment when calling a routine declared as
  250. stdcall and cdecl. The alignment value should be the one defined
  251. by GCC or the target ABI.
  252. The value of this constant is equal to the constant
  253. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  254. }
  255. std_param_align = 4;
  256. {*****************************************************************************
  257. Helpers
  258. *****************************************************************************}
  259. { Returns the tcgsize corresponding with the size of reg.}
  260. function reg_cgsize(const reg: tregister) : tcgsize;
  261. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  262. function is_calljmp(o:tasmop):boolean;
  263. procedure inverse_flags(var f: TResFlags);
  264. function flags_to_cond(const f: TResFlags) : TAsmCond;
  265. function findreg_by_number(r:Tregister):tregisterindex;
  266. function std_regnum_search(const s:string):Tregister;
  267. function std_regname(r:Tregister):string;
  268. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  269. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  270. procedure shifterop_reset(var so : tshifterop);
  271. function is_pc(const r : tregister) : boolean;
  272. function is_shifter_const(d : aint;var imm_shift : byte) : boolean;
  273. function dwarf_reg(r:tregister):shortint;
  274. implementation
  275. uses
  276. systems,rgBase,verbose;
  277. const
  278. std_regname_table : array[tregisterindex] of string[7] = (
  279. {$i rarmstd.inc}
  280. );
  281. regnumber_index : array[tregisterindex] of tregisterindex = (
  282. {$i rarmrni.inc}
  283. );
  284. std_regname_index : array[tregisterindex] of tregisterindex = (
  285. {$i rarmsri.inc}
  286. );
  287. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  288. begin
  289. case regtype of
  290. R_MMREGISTER:
  291. begin
  292. case s of
  293. OS_F32:
  294. cgsize2subreg:=R_SUBFS;
  295. OS_F64:
  296. cgsize2subreg:=R_SUBFD;
  297. else
  298. internalerror(2009112701);
  299. end;
  300. end;
  301. else
  302. cgsize2subreg:=R_SUBWHOLE;
  303. end;
  304. end;
  305. function reg_cgsize(const reg: tregister): tcgsize;
  306. begin
  307. case getregtype(reg) of
  308. R_INTREGISTER :
  309. reg_cgsize:=OS_32;
  310. R_FPUREGISTER :
  311. reg_cgsize:=OS_F80;
  312. R_MMREGISTER :
  313. begin
  314. case getsubreg(reg) of
  315. R_SUBFD,
  316. R_SUBWHOLE:
  317. result:=OS_F64;
  318. R_SUBFS:
  319. result:=OS_F32;
  320. else
  321. internalerror(2009112903);
  322. end;
  323. end;
  324. else
  325. internalerror(200303181);
  326. end;
  327. end;
  328. function is_calljmp(o:tasmop):boolean;
  329. begin
  330. { This isn't 100% perfect because the arm allows jumps also by writing to PC=R15.
  331. To overcome this problem we simply forbid that FPC generates jumps by loading R15 }
  332. is_calljmp:= o in [A_B,A_BL,A_BX,A_BLX];
  333. end;
  334. procedure inverse_flags(var f: TResFlags);
  335. const
  336. inv_flags: array[TResFlags] of TResFlags =
  337. (F_NE,F_EQ,F_CC,F_CS,F_PL,F_MI,F_VC,F_VS,F_LS,F_HI,
  338. F_LT,F_GE,F_LE,F_GT);
  339. begin
  340. f:=inv_flags[f];
  341. end;
  342. function flags_to_cond(const f: TResFlags) : TAsmCond;
  343. const
  344. flag_2_cond: array[F_EQ..F_LE] of TAsmCond =
  345. (C_EQ,C_NE,C_CS,C_CC,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  346. C_GE,C_LT,C_GT,C_LE);
  347. begin
  348. if f>high(flag_2_cond) then
  349. internalerror(200112301);
  350. result:=flag_2_cond[f];
  351. end;
  352. function findreg_by_number(r:Tregister):tregisterindex;
  353. begin
  354. result:=rgBase.findreg_by_number_table(r,regnumber_index);
  355. end;
  356. function std_regnum_search(const s:string):Tregister;
  357. begin
  358. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  359. end;
  360. function std_regname(r:Tregister):string;
  361. var
  362. p : tregisterindex;
  363. begin
  364. p:=findreg_by_number_table(r,regnumber_index);
  365. if p<>0 then
  366. result:=std_regname_table[p]
  367. else
  368. result:=generic_regname(r);
  369. end;
  370. procedure shifterop_reset(var so : tshifterop);
  371. begin
  372. FillChar(so,sizeof(so),0);
  373. end;
  374. function is_pc(const r : tregister) : boolean;
  375. begin
  376. is_pc:=(r=NR_R15);
  377. end;
  378. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  379. const
  380. inverse: array[TAsmCond] of TAsmCond=(C_None,
  381. C_NE,C_EQ,C_CC,C_CS,C_PL,C_MI,C_VC,C_VS,C_LS,C_HI,
  382. C_LT,C_GE,C_LE,C_GT,C_None,C_None
  383. );
  384. begin
  385. result := inverse[c];
  386. end;
  387. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  388. begin
  389. result := c1 = c2;
  390. end;
  391. function rotl(d : dword;b : byte) : dword;
  392. begin
  393. result:=(d shr (32-b)) or (d shl b);
  394. end;
  395. function is_shifter_const(d : aint;var imm_shift : byte) : boolean;
  396. var
  397. i : longint;
  398. begin
  399. if current_settings.cputype in cpu_thumb2 then
  400. begin
  401. for i:=0 to 24 do
  402. begin
  403. if (dword(d) and not($ff shl i))=0 then
  404. begin
  405. imm_shift:=i;
  406. result:=true;
  407. exit;
  408. end;
  409. end;
  410. end
  411. else
  412. begin
  413. for i:=0 to 15 do
  414. begin
  415. if (dword(d) and not(rotl($ff,i*2)))=0 then
  416. begin
  417. imm_shift:=i*2;
  418. result:=true;
  419. exit;
  420. end;
  421. end;
  422. end;
  423. result:=false;
  424. end;
  425. function dwarf_reg(r:tregister):shortint;
  426. begin
  427. result:=regdwarf_table[findreg_by_number(r)];
  428. if result=-1 then
  429. internalerror(200603251);
  430. end;
  431. { Low part of 64bit return value }
  432. function NR_FUNCTION_RESULT64_LOW_REG: tregister;
  433. begin
  434. if target_info.endian=endian_little then
  435. result:=NR_R0
  436. else
  437. result:=NR_R1;
  438. end;
  439. function RS_FUNCTION_RESULT64_LOW_REG: shortint;
  440. begin
  441. if target_info.endian=endian_little then
  442. result:=RS_R0
  443. else
  444. result:=RS_R1;
  445. end;
  446. { High part of 64bit return value }
  447. function NR_FUNCTION_RESULT64_HIGH_REG: tregister;
  448. begin
  449. if target_info.endian=endian_little then
  450. result:=NR_R1
  451. else
  452. result:=NR_R0;
  453. end;
  454. function RS_FUNCTION_RESULT64_HIGH_REG: shortint;
  455. begin
  456. if target_info.endian=endian_little then
  457. result:=RS_R1
  458. else
  459. result:=RS_R0;
  460. end;
  461. end.