cpubase.pas 20 KB

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