cpubase.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. { the maximum float size for a processor, }
  236. OS_FLOAT = OS_F64;
  237. { the size of a vector register for a processor }
  238. OS_VECTOR = OS_M32;
  239. {*****************************************************************************
  240. Generic Register names
  241. *****************************************************************************}
  242. { Stack pointer register }
  243. NR_STACK_POINTER_REG = NR_R13;
  244. RS_STACK_POINTER_REG = RS_R13;
  245. { Frame pointer register }
  246. RS_FRAME_POINTER_REG = RS_R11;
  247. NR_FRAME_POINTER_REG = NR_R11;
  248. { Register for addressing absolute data in a position independant way,
  249. such as in PIC code. The exact meaning is ABI specific. For
  250. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  251. }
  252. NR_PIC_OFFSET_REG = NR_R9;
  253. { Results are returned in this register (32-bit values) }
  254. NR_FUNCTION_RETURN_REG = NR_R0;
  255. RS_FUNCTION_RETURN_REG = RS_R0;
  256. { Low part of 64bit return value }
  257. NR_FUNCTION_RETURN64_LOW_REG = NR_R0;
  258. RS_FUNCTION_RETURN64_LOW_REG = RS_R0;
  259. { High part of 64bit return value }
  260. NR_FUNCTION_RETURN64_HIGH_REG = NR_R1;
  261. RS_FUNCTION_RETURN64_HIGH_REG = RS_R1;
  262. { The value returned from a function is available in this register }
  263. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  264. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  265. { The lowh part of 64bit value returned from a function }
  266. NR_FUNCTION_RESULT64_LOW_REG = NR_FUNCTION_RETURN64_LOW_REG;
  267. RS_FUNCTION_RESULT64_LOW_REG = RS_FUNCTION_RETURN64_LOW_REG;
  268. { The high part of 64bit value returned from a function }
  269. NR_FUNCTION_RESULT64_HIGH_REG = NR_FUNCTION_RETURN64_HIGH_REG;
  270. RS_FUNCTION_RESULT64_HIGH_REG = RS_FUNCTION_RETURN64_HIGH_REG;
  271. NR_FPU_RESULT_REG = NR_F0;
  272. NR_MM_RESULT_REG = NR_NO;
  273. NR_RETURN_ADDRESS_REG = NR_FUNCTION_RETURN_REG;
  274. { Offset where the parent framepointer is pushed }
  275. PARENT_FRAMEPOINTER_OFFSET = 0;
  276. {*****************************************************************************
  277. GCC /ABI linking information
  278. *****************************************************************************}
  279. const
  280. { Registers which must be saved when calling a routine declared as
  281. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  282. saved should be the ones as defined in the target ABI and / or GCC.
  283. This value can be deduced from the CALLED_USED_REGISTERS array in the
  284. GCC source.
  285. }
  286. saved_standard_registers : array[0..6] of tsuperregister =
  287. (RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,RS_R9,RS_R10);
  288. { Required parameter alignment when calling a routine declared as
  289. stdcall and cdecl. The alignment value should be the one defined
  290. by GCC or the target ABI.
  291. The value of this constant is equal to the constant
  292. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  293. }
  294. std_param_align = 4;
  295. {*****************************************************************************
  296. Helpers
  297. *****************************************************************************}
  298. { Returns the tcgsize corresponding with the size of reg.}
  299. function reg_cgsize(const reg: tregister) : tcgsize;
  300. function cgsize2subreg(s:Tcgsize):Tsubregister;
  301. function is_calljmp(o:tasmop):boolean;
  302. procedure inverse_flags(var f: TResFlags);
  303. function flags_to_cond(const f: TResFlags) : TAsmCond;
  304. function findreg_by_number(r:Tregister):tregisterindex;
  305. function std_regnum_search(const s:string):Tregister;
  306. function std_regname(r:Tregister):string;
  307. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  308. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  309. procedure shifterop_reset(var so : tshifterop);
  310. function is_pc(const r : tregister) : boolean;
  311. implementation
  312. uses
  313. rgBase,verbose;
  314. const
  315. std_regname_table : array[tregisterindex] of string[7] = (
  316. {$i rarmstd.inc}
  317. );
  318. regnumber_index : array[tregisterindex] of tregisterindex = (
  319. {$i rarmrni.inc}
  320. );
  321. std_regname_index : array[tregisterindex] of tregisterindex = (
  322. {$i rarmsri.inc}
  323. );
  324. function cgsize2subreg(s:Tcgsize):Tsubregister;
  325. begin
  326. cgsize2subreg:=R_SUBWHOLE;
  327. end;
  328. function reg_cgsize(const reg: tregister): tcgsize;
  329. const subreg2cgsize:array[Tsubregister] of Tcgsize =
  330. (OS_NO,OS_8,OS_8,OS_16,OS_32,OS_64,OS_NO,OS_NO,OS_NO);
  331. begin
  332. case getregtype(reg) of
  333. R_INTREGISTER :
  334. reg_cgsize:=OS_32;
  335. R_FPUREGISTER :
  336. reg_cgsize:=OS_F80;
  337. else
  338. internalerror(200303181);
  339. end;
  340. end;
  341. function is_calljmp(o:tasmop):boolean;
  342. begin
  343. { This isn't 100% perfect because the arm allows jumps also by writing to PC=R15.
  344. To overcome this problem we simply forbid that FPC generates jumps by loading R15 }
  345. is_calljmp:= o in [A_B,A_BL,A_BX,A_BLX];
  346. end;
  347. procedure inverse_flags(var f: TResFlags);
  348. const
  349. inv_flags: array[TResFlags] of TResFlags =
  350. (F_NE,F_EQ,F_CC,F_CS,F_PL,F_MI,F_VC,F_VS,F_LS,F_HI,
  351. F_LT,F_GE,F_LE,F_GT);
  352. begin
  353. f:=inv_flags[f];
  354. end;
  355. function flags_to_cond(const f: TResFlags) : TAsmCond;
  356. const
  357. flag_2_cond: array[F_EQ..F_LE] of TAsmCond =
  358. (C_EQ,C_NE,C_CS,C_CC,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  359. C_GE,C_LT,C_GT,C_LE);
  360. begin
  361. if f>high(flag_2_cond) then
  362. internalerror(200112301);
  363. result:=flag_2_cond[f];
  364. end;
  365. function findreg_by_number(r:Tregister):tregisterindex;
  366. begin
  367. result:=rgBase.findreg_by_number_table(r,regnumber_index);
  368. end;
  369. function std_regnum_search(const s:string):Tregister;
  370. begin
  371. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  372. end;
  373. function std_regname(r:Tregister):string;
  374. var
  375. p : tregisterindex;
  376. begin
  377. p:=findreg_by_number_table(r,regnumber_index);
  378. if p<>0 then
  379. result:=std_regname_table[p]
  380. else
  381. result:=generic_regname(r);
  382. end;
  383. procedure shifterop_reset(var so : tshifterop);
  384. begin
  385. FillChar(so,sizeof(so),0);
  386. end;
  387. function is_pc(const r : tregister) : boolean;
  388. begin
  389. is_pc:=(r=NR_R15);
  390. end;
  391. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  392. const
  393. inverse: array[TAsmCond] of TAsmCond=(C_None,
  394. C_NE,C_EQ,C_CC,C_CS,C_PL,C_MI,C_VC,C_VS,C_LS,C_HI,
  395. C_LT,C_GE,C_LE,C_GT,C_None,C_None
  396. );
  397. begin
  398. result := inverse[c];
  399. end;
  400. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  401. begin
  402. result := c1 = c2;
  403. end;
  404. end.