cpubase.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. inverse_cond : array[TAsmCond] of TAsmCond=(C_None,
  152. C_NE,C_EQ,C_CC,C_CS,C_PL,C_MI,C_VC,C_VS,C_LS,C_HI,
  153. C_LT,C_GE,C_LE,C_GT,C_None,C_None
  154. );
  155. {*****************************************************************************
  156. Flags
  157. *****************************************************************************}
  158. type
  159. TResFlags = (F_EQ,F_NE,F_CS,F_CC,F_MI,F_PL,F_VS,F_VC,F_HI,F_LS,
  160. F_GE,F_LT,F_GT,F_LE);
  161. {*****************************************************************************
  162. Operands
  163. *****************************************************************************}
  164. taddressmode = (AM_OFFSET,AM_PREINDEXED,AM_POSTINDEXED);
  165. tshiftmode = (SM_None,SM_LSL,SM_LSR,SM_ASR,SM_ROR,SM_RRX);
  166. tupdatereg = (UR_None,UR_Update);
  167. pshifterop = ^tshifterop;
  168. tshifterop = record
  169. shiftmode : tshiftmode;
  170. rs : tregister;
  171. shiftimm : byte;
  172. end;
  173. {*****************************************************************************
  174. Constants
  175. *****************************************************************************}
  176. const
  177. max_operands = 4;
  178. {# Constant defining possibly all registers which might require saving }
  179. ALL_OTHERREGISTERS = [];
  180. general_superregisters = [RS_R0..RS_PC];
  181. {# Table of registers which can be allocated by the code generator
  182. internally, when generating the code.
  183. }
  184. { legend: }
  185. { xxxregs = set of all possibly used registers of that type in the code }
  186. { generator }
  187. { usableregsxxx = set of all 32bit components of registers that can be }
  188. { possible allocated to a regvar or using getregisterxxx (this }
  189. { excludes registers which can be only used for parameter }
  190. { passing on ABI's that define this) }
  191. { c_countusableregsxxx = amount of registers in the usableregsxxx set }
  192. maxintregs = 15;
  193. { to determine how many registers to use for regvars }
  194. maxintscratchregs = 3;
  195. usableregsint = [RS_R4..RS_R10];
  196. c_countusableregsint = 7;
  197. maxfpuregs = 8;
  198. fpuregs = [RS_F0..RS_F7];
  199. usableregsfpu = [RS_F4..RS_F7];
  200. c_countusableregsfpu = 4;
  201. mmregs = [RS_D0..RS_D15];
  202. usableregsmm = [RS_D8..RS_D15];
  203. c_countusableregsmm = 8;
  204. maxaddrregs = 0;
  205. addrregs = [];
  206. usableregsaddr = [];
  207. c_countusableregsaddr = 0;
  208. {*****************************************************************************
  209. Operand Sizes
  210. *****************************************************************************}
  211. type
  212. topsize = (S_NO,
  213. S_B,S_W,S_L,S_BW,S_BL,S_WL,
  214. S_IS,S_IL,S_IQ,
  215. S_FS,S_FL,S_FX,S_D,S_Q,S_FV,S_FXX
  216. );
  217. {*****************************************************************************
  218. Constants
  219. *****************************************************************************}
  220. const
  221. firstsaveintreg = RS_R4;
  222. lastsaveintreg = RS_R10;
  223. firstsavefpureg = RS_F4;
  224. lastsavefpureg = RS_F7;
  225. firstsavemmreg = RS_D8;
  226. lastsavemmreg = RS_D15;
  227. maxvarregs = 7;
  228. varregs : Array [1..maxvarregs] of tsuperregister =
  229. (RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,RS_R9,RS_R10);
  230. maxfpuvarregs = 4;
  231. fpuvarregs : Array [1..maxfpuvarregs] of tsuperregister =
  232. (RS_F4,RS_F5,RS_F6,RS_F7);
  233. {*****************************************************************************
  234. Default generic sizes
  235. *****************************************************************************}
  236. { Defines the default address size for a processor, }
  237. OS_ADDR = OS_32;
  238. { the natural int size for a processor, }
  239. OS_INT = OS_32;
  240. { the maximum float size for a processor, }
  241. OS_FLOAT = OS_F64;
  242. { the size of a vector register for a processor }
  243. OS_VECTOR = OS_M32;
  244. {*****************************************************************************
  245. Generic Register names
  246. *****************************************************************************}
  247. { Stack pointer register }
  248. NR_STACK_POINTER_REG = NR_R13;
  249. RS_STACK_POINTER_REG = RS_R13;
  250. { Frame pointer register }
  251. RS_FRAME_POINTER_REG = RS_R11;
  252. NR_FRAME_POINTER_REG = NR_R11;
  253. { Register for addressing absolute data in a position independant way,
  254. such as in PIC code. The exact meaning is ABI specific. For
  255. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  256. }
  257. NR_PIC_OFFSET_REG = NR_R9;
  258. { Results are returned in this register (32-bit values) }
  259. NR_FUNCTION_RETURN_REG = NR_R0;
  260. RS_FUNCTION_RETURN_REG = RS_R0;
  261. { Low part of 64bit return value }
  262. NR_FUNCTION_RETURN64_LOW_REG = NR_R0;
  263. RS_FUNCTION_RETURN64_LOW_REG = RS_R0;
  264. { High part of 64bit return value }
  265. NR_FUNCTION_RETURN64_HIGH_REG = NR_R1;
  266. RS_FUNCTION_RETURN64_HIGH_REG = RS_R1;
  267. { The value returned from a function is available in this register }
  268. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  269. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  270. { The lowh part of 64bit value returned from a function }
  271. NR_FUNCTION_RESULT64_LOW_REG = NR_FUNCTION_RETURN64_LOW_REG;
  272. RS_FUNCTION_RESULT64_LOW_REG = RS_FUNCTION_RETURN64_LOW_REG;
  273. { The high part of 64bit value returned from a function }
  274. NR_FUNCTION_RESULT64_HIGH_REG = NR_FUNCTION_RETURN64_HIGH_REG;
  275. RS_FUNCTION_RESULT64_HIGH_REG = RS_FUNCTION_RETURN64_HIGH_REG;
  276. NR_FPU_RESULT_REG = NR_F0;
  277. NR_MM_RESULT_REG = NR_NO;
  278. NR_RETURN_ADDRESS_REG = NR_FUNCTION_RETURN_REG;
  279. { Offset where the parent framepointer is pushed }
  280. PARENT_FRAMEPOINTER_OFFSET = 0;
  281. {*****************************************************************************
  282. GCC /ABI linking information
  283. *****************************************************************************}
  284. const
  285. { Registers which must be saved when calling a routine declared as
  286. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  287. saved should be the ones as defined in the target ABI and / or GCC.
  288. This value can be deduced from the CALLED_USED_REGISTERS array in the
  289. GCC source.
  290. }
  291. saved_standard_registers : array[0..6] of tsuperregister =
  292. (RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,RS_R9,RS_R10);
  293. { Required parameter alignment when calling a routine declared as
  294. stdcall and cdecl. The alignment value should be the one defined
  295. by GCC or the target ABI.
  296. The value of this constant is equal to the constant
  297. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  298. }
  299. std_param_align = 4;
  300. {*****************************************************************************
  301. Helpers
  302. *****************************************************************************}
  303. { Returns the tcgsize corresponding with the size of reg.}
  304. function reg_cgsize(const reg: tregister) : tcgsize;
  305. function cgsize2subreg(s:Tcgsize):Tsubregister;
  306. function is_calljmp(o:tasmop):boolean;
  307. procedure inverse_flags(var f: TResFlags);
  308. function flags_to_cond(const f: TResFlags) : TAsmCond;
  309. function findreg_by_number(r:Tregister):tregisterindex;
  310. function std_regnum_search(const s:string):Tregister;
  311. function std_regname(r:Tregister):string;
  312. procedure shifterop_reset(var so : tshifterop);
  313. function is_pc(const r : tregister) : boolean;
  314. implementation
  315. uses
  316. rgBase,verbose;
  317. const
  318. std_regname_table : array[tregisterindex] of string[7] = (
  319. {$i rarmstd.inc}
  320. );
  321. regnumber_index : array[tregisterindex] of tregisterindex = (
  322. {$i rarmrni.inc}
  323. );
  324. std_regname_index : array[tregisterindex] of tregisterindex = (
  325. {$i rarmsri.inc}
  326. );
  327. function cgsize2subreg(s:Tcgsize):Tsubregister;
  328. begin
  329. cgsize2subreg:=R_SUBWHOLE;
  330. end;
  331. function reg_cgsize(const reg: tregister): tcgsize;
  332. const subreg2cgsize:array[Tsubregister] of Tcgsize =
  333. (OS_NO,OS_8,OS_8,OS_16,OS_32,OS_64,OS_NO,OS_NO,OS_NO);
  334. begin
  335. case getregtype(reg) of
  336. R_INTREGISTER :
  337. reg_cgsize:=OS_32;
  338. R_FPUREGISTER :
  339. reg_cgsize:=OS_F80;
  340. else
  341. internalerror(200303181);
  342. end;
  343. end;
  344. function is_calljmp(o:tasmop):boolean;
  345. begin
  346. { This isn't 100% perfect because the arm allows jumps also by writing to PC=R15.
  347. To overcome this problem we simply forbid that FPC generates jumps by loading R15 }
  348. is_calljmp:= o in [A_B,A_BL,A_BX,A_BLX];
  349. end;
  350. procedure inverse_flags(var f: TResFlags);
  351. const
  352. inv_flags: array[TResFlags] of TResFlags =
  353. (F_NE,F_EQ,F_CC,F_CS,F_PL,F_MI,F_VC,F_VS,F_LS,F_HI,
  354. F_LT,F_GE,F_LE,F_GT);
  355. begin
  356. f:=inv_flags[f];
  357. end;
  358. function flags_to_cond(const f: TResFlags) : TAsmCond;
  359. const
  360. flag_2_cond: array[F_EQ..F_LE] of TAsmCond =
  361. (C_EQ,C_NE,C_CS,C_CC,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  362. C_GE,C_LT,C_GT,C_LE);
  363. begin
  364. if f>high(flag_2_cond) then
  365. internalerror(200112301);
  366. result:=flag_2_cond[f];
  367. end;
  368. function findreg_by_number(r:Tregister):tregisterindex;
  369. begin
  370. result:=rgBase.findreg_by_number_table(r,regnumber_index);
  371. end;
  372. function std_regnum_search(const s:string):Tregister;
  373. begin
  374. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  375. end;
  376. function std_regname(r:Tregister):string;
  377. var
  378. p : tregisterindex;
  379. begin
  380. p:=findreg_by_number_table(r,regnumber_index);
  381. if p<>0 then
  382. result:=std_regname_table[p]
  383. else
  384. result:=generic_regname(r);
  385. end;
  386. procedure shifterop_reset(var so : tshifterop);
  387. begin
  388. FillChar(so,sizeof(so),0);
  389. end;
  390. function is_pc(const r : tregister) : boolean;
  391. begin
  392. is_pc:=(r=NR_R15);
  393. end;
  394. end.
  395. {
  396. $Log$
  397. Revision 1.37 2004-11-01 17:41:28 florian
  398. * fixed arm compilation with cgutils
  399. * ...
  400. Revision 1.36 2004/10/31 21:45:03 peter
  401. * generic tlocation
  402. * move tlocation to cgutils
  403. Revision 1.35 2004/10/31 12:37:11 florian
  404. * another couple of arm fixed
  405. Revision 1.34 2004/10/24 17:32:53 florian
  406. * fixed several arm compiler bugs
  407. Revision 1.33 2004/10/22 16:36:57 florian
  408. * first arm fixes for new paraloc handling
  409. Revision 1.32 2004/10/15 09:15:34 mazen
  410. - remove $IFDEF DELPHI and related code
  411. - remove $IFDEF FPCPROCVAR and related code
  412. Revision 1.31 2004/06/20 08:55:31 florian
  413. * logs truncated
  414. Revision 1.30 2004/06/16 20:07:10 florian
  415. * dwarf branch merged
  416. Revision 1.29.2.3 2004/06/13 10:51:17 florian
  417. * fixed several register allocator problems (sparc/arm)
  418. Revision 1.29.2.2 2004/06/12 17:01:01 florian
  419. * fixed compilation of arm compiler
  420. Revision 1.29.2.1 2004/05/01 11:12:23 florian
  421. * spilling of registers with size<>4 fixed
  422. Revision 1.29 2004/03/23 21:03:50 florian
  423. * arm assembler instructions can have 4 operands
  424. * qword comparisations fixed
  425. }