cpubase.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl and Peter Vreman
  4. Contains the base types for the ARM
  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. {$i fpcdefs.inc}
  24. interface
  25. uses
  26. cutils,cclasses,
  27. globtype,globals,
  28. cpuinfo,
  29. aasmbase,
  30. cgbase
  31. ;
  32. {*****************************************************************************
  33. Assembler Opcodes
  34. *****************************************************************************}
  35. type
  36. TAsmOp=(A_None,A_ADC,A_ADD,A_AND,A_N,A_BIC,A_BKPT,A_B,A_BL,A_BLX,A_BX,
  37. A_CDP,A_CDP2,A_CLZ,A_CMN,A_CMP,A_EOR,A_LDC,_A_LDC2,
  38. A_LDM,A_LDR,A_LDRB,A_LDRD,A_LDRBT,A_LDRH,A_LDRSB,
  39. A_LDRSH,A_LDRT,A_MCR,A_MCR2,A_MCRR,A_MLA,A_MOV,
  40. A_MRC,A_MRC2,A_MRRC,A_RS,A_MSR,A_MUL,A_MVN,
  41. A_ORR,A_PLD,A_QADD,A_QDADD,A_QDSUB,A_QSUB,A_RSB,A_RSC,
  42. A_SBC,A_SMLAL,A_SMULL,A_SMUL,
  43. A_SMULW,A_STC,A_STC2,A_STM,A_STR,A_STRB,A_STRBT,A_STRD,
  44. A_STRH,A_STRT,A_SUB,A_SWI,A_SWP,A_SWPB,A_TEQ,A_TST,
  45. A_UMLAL,A_UMULL,
  46. { FPA coprocessor instructions }
  47. A_LDF,A_STF,A_LFM,A_SFM,A_FLT,A_FIX,A_WFS,A_RFS,A_RFC,
  48. A_ADF,A_DVF,A_FDV,A_FML,A_FRD,A_MUF,A_POL,A_PW,A_RDF,
  49. A_RMF,A_RPW,A_RSF,A_SUF,A_ABS,A_ACS,A_ASN,A_ATN,A_COS,
  50. A_EXP,A_LOG,A_LGN,A_MVF,A_MNF,A_NRM,A_RND,A_SIN,A_SQT,A_TAN,A_URD,
  51. A_CMF,A_CMFE,A_CNF
  52. { VPA coprocessor codes }
  53. );
  54. { This should define the array of instructions as string }
  55. op2strtable=array[tasmop] of string[11];
  56. const
  57. { First value of opcode enumeration }
  58. firstop = low(tasmop);
  59. { Last value of opcode enumeration }
  60. lastop = high(tasmop);
  61. {*****************************************************************************
  62. Registers
  63. *****************************************************************************}
  64. type
  65. { Number of registers used for indexing in tables }
  66. tregisterindex=0..{$i rarmnor.inc}-1;
  67. const
  68. { Available Superregisters }
  69. {$i rarmsup.inc}
  70. RS_PC = RS_R15;
  71. { No Subregisters }
  72. R_SUBWHOLE = R_SUBNONE;
  73. { Available Registers }
  74. {$i rarmcon.inc}
  75. { aliases }
  76. NR_PC = NR_R15;
  77. { Integer Super registers first and last }
  78. first_int_supreg = RS_R0;
  79. first_int_imreg = $10;
  80. { Float Super register first and last }
  81. first_fpu_supreg = RS_F0;
  82. first_fpu_imreg = $08;
  83. { MM Super register first and last }
  84. first_mm_supreg = RS_S0;
  85. first_mm_imreg = $20;
  86. {$warning TODO Calculate bsstart}
  87. regnumber_count_bsstart = 64;
  88. regnumber_table : array[tregisterindex] of tregister = (
  89. {$i rarmnum.inc}
  90. );
  91. regstabs_table : array[tregisterindex] of shortint = (
  92. {$i rarmsta.inc}
  93. );
  94. regdwarf_table : array[tregisterindex] of shortint = (
  95. {$i rarmdwa.inc}
  96. );
  97. { registers which may be destroyed by calls }
  98. VOLATILE_INTREGISTERS = [RS_R0..RS_R3,RS_R12..RS_R15];
  99. VOLATILE_FPUREGISTERS = [RS_F0..RS_F3];
  100. type
  101. totherregisterset = set of tregisterindex;
  102. {*****************************************************************************
  103. Instruction post fixes
  104. *****************************************************************************}
  105. type
  106. { ARM instructions load/store and arithmetic instructions
  107. can have several instruction post fixes which are collected
  108. in this enumeration
  109. }
  110. TOpPostfix = (PF_None,
  111. { update condition flags
  112. or floating point single }
  113. PF_S,
  114. { floating point size }
  115. PF_D,PF_E,PF_P,PF_EP,
  116. { load/store }
  117. PF_B,PF_SB,PF_BT,PF_H,PF_SH,PF_T,
  118. { multiple load/store address modes }
  119. PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA
  120. );
  121. TRoundingMode = (RM_None,RM_P,RM_M,RM_Z);
  122. const
  123. cgsize2fpuoppostfix : array[OS_NO..OS_F128] of toppostfix = (
  124. PF_E,
  125. PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,
  126. PF_S,PF_D,PF_E,PF_None,PF_None);
  127. oppostfix2str : array[TOpPostfix] of string[2] = ('',
  128. 's',
  129. 'd','e','p','ep',
  130. 'b','sb','bt','h','sh','t',
  131. 'ia','ib','da','db','fd','fa','ed','ea');
  132. roundingmode2str : array[TRoundingMode] of string[1] = ('',
  133. 'p','m','z');
  134. {*****************************************************************************
  135. Conditions
  136. *****************************************************************************}
  137. type
  138. TAsmCond=(C_None,
  139. C_EQ,C_NE,C_CS,C_CC,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  140. C_GE,C_LT,C_GT,C_LE,C_AL,C_NV
  141. );
  142. const
  143. cond2str : array[TAsmCond] of string[2]=('',
  144. 'eq','ne','cs','cc','mi','pl','vs','vc','hi','ls',
  145. 'ge','lt','gt','le','al','nv'
  146. );
  147. uppercond2str : array[TAsmCond] of string[2]=('',
  148. 'EQ','NE','CS','CC','MI','PL','VS','VC','HI','LS',
  149. 'GE','LT','GT','LE','AL','NV'
  150. );
  151. {*****************************************************************************
  152. Flags
  153. *****************************************************************************}
  154. type
  155. TResFlags = (F_EQ,F_NE,F_CS,F_CC,F_MI,F_PL,F_VS,F_VC,F_HI,F_LS,
  156. F_GE,F_LT,F_GT,F_LE);
  157. {*****************************************************************************
  158. Operands
  159. *****************************************************************************}
  160. taddressmode = (AM_OFFSET,AM_PREINDEXED,AM_POSTINDEXED);
  161. tshiftmode = (SM_None,SM_LSL,SM_LSR,SM_ASR,SM_ROR,SM_RRX);
  162. tupdatereg = (UR_None,UR_Update);
  163. pshifterop = ^tshifterop;
  164. tshifterop = record
  165. shiftmode : tshiftmode;
  166. rs : tregister;
  167. shiftimm : byte;
  168. end;
  169. {*****************************************************************************
  170. Constants
  171. *****************************************************************************}
  172. const
  173. max_operands = 4;
  174. {# Constant defining possibly all registers which might require saving }
  175. ALL_OTHERREGISTERS = [];
  176. general_superregisters = [RS_R0..RS_PC];
  177. {# Table of registers which can be allocated by the code generator
  178. internally, when generating the code.
  179. }
  180. { legend: }
  181. { xxxregs = set of all possibly used registers of that type in the code }
  182. { generator }
  183. { usableregsxxx = set of all 32bit components of registers that can be }
  184. { possible allocated to a regvar or using getregisterxxx (this }
  185. { excludes registers which can be only used for parameter }
  186. { passing on ABI's that define this) }
  187. { c_countusableregsxxx = amount of registers in the usableregsxxx set }
  188. maxintregs = 15;
  189. { to determine how many registers to use for regvars }
  190. maxintscratchregs = 3;
  191. usableregsint = [RS_R4..RS_R10];
  192. c_countusableregsint = 7;
  193. maxfpuregs = 8;
  194. fpuregs = [RS_F0..RS_F7];
  195. usableregsfpu = [RS_F4..RS_F7];
  196. c_countusableregsfpu = 4;
  197. mmregs = [RS_D0..RS_D15];
  198. usableregsmm = [RS_D8..RS_D15];
  199. c_countusableregsmm = 8;
  200. maxaddrregs = 0;
  201. addrregs = [];
  202. usableregsaddr = [];
  203. c_countusableregsaddr = 0;
  204. {*****************************************************************************
  205. Operand Sizes
  206. *****************************************************************************}
  207. type
  208. topsize = (S_NO,
  209. S_B,S_W,S_L,S_BW,S_BL,S_WL,
  210. S_IS,S_IL,S_IQ,
  211. S_FS,S_FL,S_FX,S_D,S_Q,S_FV,S_FXX
  212. );
  213. {*****************************************************************************
  214. Constants
  215. *****************************************************************************}
  216. const
  217. firstsaveintreg = RS_R4;
  218. lastsaveintreg = RS_R10;
  219. firstsavefpureg = RS_F4;
  220. lastsavefpureg = RS_F7;
  221. firstsavemmreg = RS_D8;
  222. lastsavemmreg = RS_D15;
  223. maxvarregs = 7;
  224. varregs : Array [1..maxvarregs] of tsuperregister =
  225. (RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,RS_R9,RS_R10);
  226. maxfpuvarregs = 4;
  227. fpuvarregs : Array [1..maxfpuvarregs] of tsuperregister =
  228. (RS_F4,RS_F5,RS_F6,RS_F7);
  229. {*****************************************************************************
  230. Default generic sizes
  231. *****************************************************************************}
  232. { Defines the default address size for a processor, }
  233. OS_ADDR = OS_32;
  234. { the natural int size for a processor, }
  235. OS_INT = OS_32;
  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);
  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.
  406. {
  407. $Log$
  408. Revision 1.39 2005-02-26 01:26:59 jonas
  409. * fixed generic jumps optimizer and enabled it for ppc (the label table
  410. was not being initialised -> getfinaldestination always failed, which
  411. caused wrong optimizations in some cases)
  412. * changed the inverse_cond into a function, because tasmcond is a record
  413. on ppc
  414. + added a compare_conditions() function for the same reason
  415. Revision 1.38 2005/02/14 17:13:09 peter
  416. * truncate log
  417. }