cpubase.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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=(A_None,A_ADC,A_ADD,A_AND,A_N,A_BIC,A_BKPT,A_B,A_BL,A_BLX,A_BX,
  36. A_CDP,A_CDP2,A_CLZ,A_CMN,A_CMP,A_EOR,A_LDC,_A_LDC2,
  37. A_LDM,A_LDR,A_LDRB,A_LDRD,A_LDRBT,A_LDRH,A_LDRSB,
  38. A_LDRSH,A_LDRT,A_MCR,A_MCR2,A_MCRR,A_MLA,A_MOV,
  39. A_MRC,A_MRC2,A_MRRC,A_RS,A_MSR,A_MUL,A_MVN,
  40. A_ORR,A_PLD,A_QADD,A_QDADD,A_QDSUB,A_QSUB,A_RSB,A_RSC,
  41. A_SBC,A_SMLAL,A_SMULL,A_SMUL,
  42. A_SMULW,A_STC,A_STC2,A_STM,A_STR,A_STRB,A_STRBT,A_STRD,
  43. A_STRH,A_STRT,A_SUB,A_SWI,A_SWP,A_SWPB,A_TEQ,A_TST,
  44. A_UMLAL,A_UMULL,
  45. { FPA coprocessor instructions }
  46. A_LDF,A_STF,A_LFM,A_SFM,A_FLT,A_FIX,A_WFS,A_RFS,A_RFC,
  47. A_ADF,A_DVF,A_FDV,A_FML,A_FRD,A_MUF,A_POL,A_PW,A_RDF,
  48. A_RMF,A_RPW,A_RSF,A_SUF,A_ABS,A_ACS,A_ASN,A_ATN,A_COS,
  49. A_EXP,A_LOG,A_LGN,A_MVF,A_MNF,A_NRM,A_RND,A_SIN,A_SQT,A_TAN,A_URD,
  50. A_CMF,A_CMFE,A_CNF
  51. { VPA coprocessor codes }
  52. );
  53. { This should define the array of instructions as string }
  54. op2strtable=array[tasmop] of string[11];
  55. const
  56. { First value of opcode enumeration }
  57. firstop = low(tasmop);
  58. { Last value of opcode enumeration }
  59. lastop = high(tasmop);
  60. {*****************************************************************************
  61. Registers
  62. *****************************************************************************}
  63. type
  64. { Number of registers used for indexing in tables }
  65. tregisterindex=0..{$i rarmnor.inc}-1;
  66. const
  67. { Available Superregisters }
  68. {$i rarmsup.inc}
  69. RS_PC = RS_R15;
  70. { No Subregisters }
  71. R_SUBWHOLE = R_SUBNONE;
  72. { Available Registers }
  73. {$i rarmcon.inc}
  74. { aliases }
  75. NR_PC = NR_R15;
  76. { Integer Super registers first and last }
  77. first_int_supreg = RS_R0;
  78. first_int_imreg = $10;
  79. { Float Super register first and last }
  80. first_fpu_supreg = RS_F0;
  81. first_fpu_imreg = $08;
  82. { MM Super register first and last }
  83. first_mm_supreg = RS_S0;
  84. first_mm_imreg = $20;
  85. {$warning TODO Calculate bsstart}
  86. regnumber_count_bsstart = 64;
  87. regnumber_table : array[tregisterindex] of tregister = (
  88. {$i rarmnum.inc}
  89. );
  90. regstabs_table : array[tregisterindex] of shortint = (
  91. {$i rarmsta.inc}
  92. );
  93. regdwarf_table : array[tregisterindex] of shortint = (
  94. {$i rarmdwa.inc}
  95. );
  96. { registers which may be destroyed by calls }
  97. VOLATILE_INTREGISTERS = [RS_R0..RS_R3,RS_R12..RS_R15];
  98. VOLATILE_FPUREGISTERS = [RS_F0..RS_F3];
  99. type
  100. totherregisterset = set of tregisterindex;
  101. {*****************************************************************************
  102. Instruction post fixes
  103. *****************************************************************************}
  104. type
  105. { ARM instructions load/store and arithmetic instructions
  106. can have several instruction post fixes which are collected
  107. in this enumeration
  108. }
  109. TOpPostfix = (PF_None,
  110. { update condition flags
  111. or floating point single }
  112. PF_S,
  113. { floating point size }
  114. PF_D,PF_E,PF_P,PF_EP,
  115. { load/store }
  116. PF_B,PF_SB,PF_BT,PF_H,PF_SH,PF_T,
  117. { multiple load/store address modes }
  118. PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA
  119. );
  120. TRoundingMode = (RM_None,RM_P,RM_M,RM_Z);
  121. const
  122. cgsize2fpuoppostfix : array[OS_NO..OS_F128] of toppostfix = (
  123. PF_E,
  124. PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,
  125. PF_S,PF_D,PF_E,PF_None,PF_None);
  126. oppostfix2str : array[TOpPostfix] of string[2] = ('',
  127. 's',
  128. 'd','e','p','ep',
  129. 'b','sb','bt','h','sh','t',
  130. 'ia','ib','da','db','fd','fa','ed','ea');
  131. roundingmode2str : array[TRoundingMode] of string[1] = ('',
  132. 'p','m','z');
  133. {*****************************************************************************
  134. Conditions
  135. *****************************************************************************}
  136. type
  137. TAsmCond=(C_None,
  138. C_EQ,C_NE,C_CS,C_CC,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  139. C_GE,C_LT,C_GT,C_LE,C_AL,C_NV
  140. );
  141. const
  142. cond2str : array[TAsmCond] of string[2]=('',
  143. 'eq','ne','cs','cc','mi','pl','vs','vc','hi','ls',
  144. 'ge','lt','gt','le','al','nv'
  145. );
  146. uppercond2str : array[TAsmCond] of string[2]=('',
  147. 'EQ','NE','CS','CC','MI','PL','VS','VC','HI','LS',
  148. 'GE','LT','GT','LE','AL','NV'
  149. );
  150. {*****************************************************************************
  151. Flags
  152. *****************************************************************************}
  153. type
  154. TResFlags = (F_EQ,F_NE,F_CS,F_CC,F_MI,F_PL,F_VS,F_VC,F_HI,F_LS,
  155. F_GE,F_LT,F_GT,F_LE);
  156. {*****************************************************************************
  157. Operands
  158. *****************************************************************************}
  159. taddressmode = (AM_OFFSET,AM_PREINDEXED,AM_POSTINDEXED);
  160. tshiftmode = (SM_None,SM_LSL,SM_LSR,SM_ASR,SM_ROR,SM_RRX);
  161. tupdatereg = (UR_None,UR_Update);
  162. pshifterop = ^tshifterop;
  163. tshifterop = record
  164. shiftmode : tshiftmode;
  165. rs : tregister;
  166. shiftimm : byte;
  167. end;
  168. {*****************************************************************************
  169. Constants
  170. *****************************************************************************}
  171. const
  172. max_operands = 4;
  173. {# Constant defining possibly all registers which might require saving }
  174. ALL_OTHERREGISTERS = [];
  175. general_superregisters = [RS_R0..RS_PC];
  176. {# Table of registers which can be allocated by the code generator
  177. internally, when generating the code.
  178. }
  179. { legend: }
  180. { xxxregs = set of all possibly used registers of that type in the code }
  181. { generator }
  182. { usableregsxxx = set of all 32bit components of registers that can be }
  183. { possible allocated to a regvar or using getregisterxxx (this }
  184. { excludes registers which can be only used for parameter }
  185. { passing on ABI's that define this) }
  186. { c_countusableregsxxx = amount of registers in the usableregsxxx set }
  187. maxintregs = 15;
  188. { to determine how many registers to use for regvars }
  189. maxintscratchregs = 3;
  190. usableregsint = [RS_R4..RS_R10];
  191. c_countusableregsint = 7;
  192. maxfpuregs = 8;
  193. fpuregs = [RS_F0..RS_F7];
  194. usableregsfpu = [RS_F4..RS_F7];
  195. c_countusableregsfpu = 4;
  196. mmregs = [RS_D0..RS_D15];
  197. usableregsmm = [RS_D8..RS_D15];
  198. c_countusableregsmm = 8;
  199. maxaddrregs = 0;
  200. addrregs = [];
  201. usableregsaddr = [];
  202. c_countusableregsaddr = 0;
  203. {*****************************************************************************
  204. Operand Sizes
  205. *****************************************************************************}
  206. type
  207. topsize = (S_NO,
  208. S_B,S_W,S_L,S_BW,S_BL,S_WL,
  209. S_IS,S_IL,S_IQ,
  210. S_FS,S_FL,S_FX,S_D,S_Q,S_FV,S_FXX
  211. );
  212. {*****************************************************************************
  213. Constants
  214. *****************************************************************************}
  215. const
  216. firstsaveintreg = RS_R4;
  217. lastsaveintreg = RS_R10;
  218. firstsavefpureg = RS_F4;
  219. lastsavefpureg = RS_F7;
  220. firstsavemmreg = RS_D8;
  221. lastsavemmreg = RS_D15;
  222. maxvarregs = 7;
  223. varregs : Array [1..maxvarregs] of tsuperregister =
  224. (RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,RS_R9,RS_R10);
  225. maxfpuvarregs = 4;
  226. fpuvarregs : Array [1..maxfpuvarregs] of tsuperregister =
  227. (RS_F4,RS_F5,RS_F6,RS_F7);
  228. {*****************************************************************************
  229. Default generic sizes
  230. *****************************************************************************}
  231. { Defines the default address size for a processor, }
  232. OS_ADDR = OS_32;
  233. { the natural int size for a processor, }
  234. OS_INT = OS_32;
  235. OS_SINT = OS_S32;
  236. { the maximum float size for a processor, }
  237. OS_FLOAT = OS_F64;
  238. { the size of a vector register for a processor }
  239. OS_VECTOR = OS_M32;
  240. {*****************************************************************************
  241. Generic Register names
  242. *****************************************************************************}
  243. { Stack pointer register }
  244. NR_STACK_POINTER_REG = NR_R13;
  245. RS_STACK_POINTER_REG = RS_R13;
  246. { Frame pointer register }
  247. RS_FRAME_POINTER_REG = RS_R11;
  248. NR_FRAME_POINTER_REG = NR_R11;
  249. { Register for addressing absolute data in a position independant way,
  250. such as in PIC code. The exact meaning is ABI specific. For
  251. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  252. }
  253. NR_PIC_OFFSET_REG = NR_R9;
  254. { Results are returned in this register (32-bit values) }
  255. NR_FUNCTION_RETURN_REG = NR_R0;
  256. RS_FUNCTION_RETURN_REG = RS_R0;
  257. { Low part of 64bit return value }
  258. NR_FUNCTION_RETURN64_LOW_REG = NR_R0;
  259. RS_FUNCTION_RETURN64_LOW_REG = RS_R0;
  260. { High part of 64bit return value }
  261. NR_FUNCTION_RETURN64_HIGH_REG = NR_R1;
  262. RS_FUNCTION_RETURN64_HIGH_REG = RS_R1;
  263. { The value returned from a function is available in this register }
  264. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  265. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  266. { The lowh part of 64bit value returned from a function }
  267. NR_FUNCTION_RESULT64_LOW_REG = NR_FUNCTION_RETURN64_LOW_REG;
  268. RS_FUNCTION_RESULT64_LOW_REG = RS_FUNCTION_RETURN64_LOW_REG;
  269. { The high part of 64bit value returned from a function }
  270. NR_FUNCTION_RESULT64_HIGH_REG = NR_FUNCTION_RETURN64_HIGH_REG;
  271. RS_FUNCTION_RESULT64_HIGH_REG = RS_FUNCTION_RETURN64_HIGH_REG;
  272. NR_FPU_RESULT_REG = NR_F0;
  273. NR_MM_RESULT_REG = NR_NO;
  274. NR_RETURN_ADDRESS_REG = NR_FUNCTION_RETURN_REG;
  275. { Offset where the parent framepointer is pushed }
  276. PARENT_FRAMEPOINTER_OFFSET = 0;
  277. {*****************************************************************************
  278. GCC /ABI linking information
  279. *****************************************************************************}
  280. const
  281. { Registers which must be saved when calling a routine declared as
  282. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  283. saved should be the ones as defined in the target ABI and / or GCC.
  284. This value can be deduced from the CALLED_USED_REGISTERS array in the
  285. GCC source.
  286. }
  287. saved_standard_registers : array[0..6] of tsuperregister =
  288. (RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,RS_R9,RS_R10);
  289. { Required parameter alignment when calling a routine declared as
  290. stdcall and cdecl. The alignment value should be the one defined
  291. by GCC or the target ABI.
  292. The value of this constant is equal to the constant
  293. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  294. }
  295. std_param_align = 4;
  296. {*****************************************************************************
  297. Helpers
  298. *****************************************************************************}
  299. { Returns the tcgsize corresponding with the size of reg.}
  300. function reg_cgsize(const reg: tregister) : tcgsize;
  301. function cgsize2subreg(s:Tcgsize):Tsubregister;
  302. function is_calljmp(o:tasmop):boolean;
  303. procedure inverse_flags(var f: TResFlags);
  304. function flags_to_cond(const f: TResFlags) : TAsmCond;
  305. function findreg_by_number(r:Tregister):tregisterindex;
  306. function std_regnum_search(const s:string):Tregister;
  307. function std_regname(r:Tregister):string;
  308. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  309. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  310. procedure shifterop_reset(var so : tshifterop);
  311. function is_pc(const r : tregister) : boolean;
  312. implementation
  313. uses
  314. rgBase,verbose;
  315. const
  316. std_regname_table : array[tregisterindex] of string[7] = (
  317. {$i rarmstd.inc}
  318. );
  319. regnumber_index : array[tregisterindex] of tregisterindex = (
  320. {$i rarmrni.inc}
  321. );
  322. std_regname_index : array[tregisterindex] of tregisterindex = (
  323. {$i rarmsri.inc}
  324. );
  325. function cgsize2subreg(s:Tcgsize):Tsubregister;
  326. begin
  327. cgsize2subreg:=R_SUBWHOLE;
  328. end;
  329. function reg_cgsize(const reg: tregister): tcgsize;
  330. const subreg2cgsize:array[Tsubregister] of Tcgsize =
  331. (OS_NO,OS_8,OS_8,OS_16,OS_32,OS_64,OS_NO,OS_NO,OS_NO,OS_NO,OS_NO);
  332. begin
  333. case getregtype(reg) of
  334. R_INTREGISTER :
  335. reg_cgsize:=OS_32;
  336. R_FPUREGISTER :
  337. reg_cgsize:=OS_F80;
  338. else
  339. internalerror(200303181);
  340. end;
  341. end;
  342. function is_calljmp(o:tasmop):boolean;
  343. begin
  344. { This isn't 100% perfect because the arm allows jumps also by writing to PC=R15.
  345. To overcome this problem we simply forbid that FPC generates jumps by loading R15 }
  346. is_calljmp:= o in [A_B,A_BL,A_BX,A_BLX];
  347. end;
  348. procedure inverse_flags(var f: TResFlags);
  349. const
  350. inv_flags: array[TResFlags] of TResFlags =
  351. (F_NE,F_EQ,F_CC,F_CS,F_PL,F_MI,F_VC,F_VS,F_LS,F_HI,
  352. F_LT,F_GE,F_LE,F_GT);
  353. begin
  354. f:=inv_flags[f];
  355. end;
  356. function flags_to_cond(const f: TResFlags) : TAsmCond;
  357. const
  358. flag_2_cond: array[F_EQ..F_LE] of TAsmCond =
  359. (C_EQ,C_NE,C_CS,C_CC,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  360. C_GE,C_LT,C_GT,C_LE);
  361. begin
  362. if f>high(flag_2_cond) then
  363. internalerror(200112301);
  364. result:=flag_2_cond[f];
  365. end;
  366. function findreg_by_number(r:Tregister):tregisterindex;
  367. begin
  368. result:=rgBase.findreg_by_number_table(r,regnumber_index);
  369. end;
  370. function std_regnum_search(const s:string):Tregister;
  371. begin
  372. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  373. end;
  374. function std_regname(r:Tregister):string;
  375. var
  376. p : tregisterindex;
  377. begin
  378. p:=findreg_by_number_table(r,regnumber_index);
  379. if p<>0 then
  380. result:=std_regname_table[p]
  381. else
  382. result:=generic_regname(r);
  383. end;
  384. procedure shifterop_reset(var so : tshifterop);
  385. begin
  386. FillChar(so,sizeof(so),0);
  387. end;
  388. function is_pc(const r : tregister) : boolean;
  389. begin
  390. is_pc:=(r=NR_R15);
  391. end;
  392. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  393. const
  394. inverse: array[TAsmCond] of TAsmCond=(C_None,
  395. C_NE,C_EQ,C_CC,C_CS,C_PL,C_MI,C_VC,C_VS,C_LS,C_HI,
  396. C_LT,C_GE,C_LE,C_GT,C_None,C_None
  397. );
  398. begin
  399. result := inverse[c];
  400. end;
  401. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  402. begin
  403. result := c1 = c2;
  404. end;
  405. end.