cpubase.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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. {$ifdef delphi}
  32. ,dmisc
  33. {$endif}
  34. ;
  35. {*****************************************************************************
  36. Assembler Opcodes
  37. *****************************************************************************}
  38. type
  39. TAsmOp=(A_None,A_ADC,A_ADD,A_AND,A_N,A_BIC,A_BKPT,A_B,A_BL,A_BLX,A_BX,
  40. A_CDP,A_CDP2,A_CLZ,A_CMN,A_CMP,A_EOR,A_LDC,_A_LDC2,
  41. A_LDM,A_LDR,A_LDRB,A_LDRD,A_LDRBT,A_LDRH,A_LDRSB,
  42. A_LDRSH,A_LDRT,A_MCR,A_MCR2,A_MCRR,A_MLA,A_MOV,
  43. A_MRC,A_MRC2,A_MRRC,A_RS,A_MSR,A_MUL,A_MVN,
  44. A_ORR,A_PLD,A_QADD,A_QDADD,A_QDSUB,A_QSUB,A_RSB,A_RSC,
  45. A_SBC,A_SMLAL,A_SMULL,A_SMUL,
  46. A_SMULW,A_STC,A_STC2,A_STM,A_STR,A_STRB,A_STRBT,A_STRD,
  47. A_STRH,A_STRT,A_SUB,A_SWI,A_SWP,A_SWPB,A_TEQ,A_TST,
  48. A_UMLAL,A_UMULL,
  49. { FPA coprocessor instructions }
  50. A_LDF,A_STF,A_LFM,A_SFM,A_FLT,A_FIX,A_WFS,A_RFS,A_RFC,
  51. A_ADF,A_DVF,A_FDV,A_FML,A_FRD,A_MUF,A_POL,A_PW,A_RDF,
  52. A_RMF,A_RPW,A_RSF,A_SUF,A_ABS,A_ACS,A_ASN,A_ATN,A_COS,
  53. A_EXP,A_LOG,A_LGN,A_MVF,A_MNF,A_NRM,A_RND,A_SIN,A_SQT,A_TAN,A_URD,
  54. A_CMF,A_CNF
  55. { VPA coprocessor codes }
  56. );
  57. { This should define the array of instructions as string }
  58. op2strtable=array[tasmop] of string[11];
  59. const
  60. { First value of opcode enumeration }
  61. firstop = low(tasmop);
  62. { Last value of opcode enumeration }
  63. lastop = high(tasmop);
  64. {*****************************************************************************
  65. Registers
  66. *****************************************************************************}
  67. type
  68. { Number of registers used for indexing in tables }
  69. tregisterindex=0..{$i rarmnor.inc}-1;
  70. const
  71. { Available Superregisters }
  72. {$i rarmsup.inc}
  73. RS_PC = RS_R15;
  74. { No Subregisters }
  75. R_SUBWHOLE = R_SUBNONE;
  76. { Available Registers }
  77. {$i rarmcon.inc}
  78. { aliases }
  79. NR_PC = NR_R15;
  80. { Integer Super registers first and last }
  81. first_int_supreg = RS_R0;
  82. first_int_imreg = $10;
  83. { Float Super register first and last }
  84. first_fpu_supreg = RS_F0;
  85. first_fpu_imreg = $08;
  86. { MM Super register first and last }
  87. first_mm_supreg = RS_S0;
  88. first_mm_imreg = $20;
  89. {$warning TODO Calculate bsstart}
  90. regnumber_count_bsstart = 64;
  91. regnumber_table : array[tregisterindex] of tregister = (
  92. {$i rarmnum.inc}
  93. );
  94. regstabs_table : array[tregisterindex] of tregister = (
  95. {$i rarmsta.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,FP_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,
  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. inverse_cond : array[TAsmCond] of TAsmCond=(C_None,
  148. C_NE,C_EQ,C_CC,C_CS,C_PL,C_MI,C_VC,C_VS,C_LS,C_HI,
  149. C_LT,C_GE,C_LE,C_GT,C_None,C_None
  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. Reference
  159. *****************************************************************************}
  160. type
  161. trefoptions=(ref_none,ref_parafixup,ref_localfixup,ref_selffixup);
  162. taddressmode = (AM_OFFSET,AM_PREINDEXED,AM_POSTINDEXED);
  163. tshiftmode = (SM_None,SM_LSL,SM_LSR,SM_ASR,SM_ROR,SM_RRX);
  164. { reference record }
  165. preference = ^treference;
  166. treference = packed record
  167. base,
  168. index : tregister;
  169. shiftimm : byte;
  170. signindex : shortint;
  171. offset : longint;
  172. symbol : tasmsymbol;
  173. offsetfixup : longint;
  174. options : trefoptions;
  175. addressmode : taddressmode;
  176. shiftmode : tshiftmode;
  177. end;
  178. { reference record }
  179. pparareference = ^tparareference;
  180. tparareference = packed record
  181. index : tregister;
  182. offset : longint;
  183. end;
  184. {*****************************************************************************
  185. Operands
  186. *****************************************************************************}
  187. tupdatereg = (UR_None,UR_Update);
  188. pshifterop = ^tshifterop;
  189. tshifterop = record
  190. shiftmode : tshiftmode;
  191. rs : tregister;
  192. shiftimm : byte;
  193. end;
  194. {*****************************************************************************
  195. Generic Location
  196. *****************************************************************************}
  197. type
  198. { tparamlocation describes where a parameter for a procedure is stored.
  199. References are given from the caller's point of view. The usual
  200. TLocation isn't used, because contains a lot of unnessary fields.
  201. }
  202. tparalocation = packed record
  203. size : TCGSize;
  204. loc : TCGLoc;
  205. alignment : byte;
  206. case TCGLoc of
  207. LOC_REFERENCE : (reference : tparareference);
  208. { segment in reference at the same place as in loc_register }
  209. LOC_MMREGISTER,LOC_CMMREGISTER,
  210. LOC_FPUREGISTER,LOC_CFPUREGISTER,
  211. LOC_REGISTER,LOC_CREGISTER : (
  212. case longint of
  213. 1 : (register,registerhigh : tregister);
  214. { overlay a registerlow }
  215. 2 : (registerlow : tregister);
  216. { overlay a 64 Bit register type }
  217. 3 : (reg64 : tregister64);
  218. 4 : (register64 : tregister64);
  219. );
  220. end;
  221. tlocation = packed record
  222. loc : TCGLoc;
  223. size : TCGSize;
  224. case TCGLoc of
  225. LOC_FLAGS : (resflags : tresflags);
  226. LOC_CONSTANT : (
  227. case longint of
  228. 1 : (value : AWord);
  229. { can't do this, this layout depends on the host cpu. Use }
  230. { lo(valueqword)/hi(valueqword) instead (JM) }
  231. { 2 : (valuelow, valuehigh:AWord); }
  232. { overlay a complete 64 Bit value }
  233. 3 : (valueqword : qword);
  234. );
  235. LOC_CREFERENCE,
  236. LOC_REFERENCE : (reference : treference);
  237. { segment in reference at the same place as in loc_register }
  238. LOC_REGISTER,LOC_CREGISTER : (
  239. case longint of
  240. 1 : (register,registerhigh,segment : tregister);
  241. { overlay a registerlow }
  242. 2 : (registerlow : tregister);
  243. { overlay a 64 Bit register type }
  244. 3 : (reg64 : tregister64);
  245. 4 : (register64 : tregister64);
  246. );
  247. { it's only for better handling }
  248. LOC_MMXREGISTER,LOC_CMMXREGISTER : (mmxreg : tregister);
  249. end;
  250. {*****************************************************************************
  251. Constants
  252. *****************************************************************************}
  253. const
  254. { declare aliases }
  255. LOC_MMREGISTER = LOC_SSEREGISTER;
  256. LOC_CMMREGISTER = LOC_CSSEREGISTER;
  257. max_operands = 3;
  258. {# Constant defining possibly all registers which might require saving }
  259. ALL_OTHERREGISTERS = [];
  260. general_superregisters = [RS_R0..RS_PC];
  261. {# Table of registers which can be allocated by the code generator
  262. internally, when generating the code.
  263. }
  264. { legend: }
  265. { xxxregs = set of all possibly used registers of that type in the code }
  266. { generator }
  267. { usableregsxxx = set of all 32bit components of registers that can be }
  268. { possible allocated to a regvar or using getregisterxxx (this }
  269. { excludes registers which can be only used for parameter }
  270. { passing on ABI's that define this) }
  271. { c_countusableregsxxx = amount of registers in the usableregsxxx set }
  272. maxintregs = 15;
  273. { to determine how many registers to use for regvars }
  274. maxintscratchregs = 3;
  275. usableregsint = [RS_R4..RS_R10];
  276. c_countusableregsint = 7;
  277. maxfpuregs = 8;
  278. fpuregs = [RS_F0..RS_F7];
  279. usableregsfpu = [RS_F4..RS_F7];
  280. c_countusableregsfpu = 4;
  281. mmregs = [RS_D0..RS_D15];
  282. usableregsmm = [RS_D8..RS_D15];
  283. c_countusableregsmm = 8;
  284. maxaddrregs = 0;
  285. addrregs = [];
  286. usableregsaddr = [];
  287. c_countusableregsaddr = 0;
  288. {*****************************************************************************
  289. Operand Sizes
  290. *****************************************************************************}
  291. type
  292. topsize = (S_NO,
  293. S_B,S_W,S_L,S_BW,S_BL,S_WL,
  294. S_IS,S_IL,S_IQ,
  295. S_FS,S_FL,S_FX,S_D,S_Q,S_FV,S_FXX
  296. );
  297. {*****************************************************************************
  298. Constants
  299. *****************************************************************************}
  300. const
  301. firstsaveintreg = RS_R4;
  302. lastsaveintreg = RS_R10;
  303. firstsavefpureg = RS_F4;
  304. lastsavefpureg = RS_F7;
  305. firstsavemmreg = RS_D8;
  306. lastsavemmreg = RS_D15;
  307. maxvarregs = 7;
  308. varregs : Array [1..maxvarregs] of tsuperregister =
  309. (RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,RS_R9,RS_R10);
  310. maxfpuvarregs = 4;
  311. fpuvarregs : Array [1..maxfpuvarregs] of tsuperregister =
  312. (RS_F4,RS_F5,RS_F6,RS_F7);
  313. {*****************************************************************************
  314. Default generic sizes
  315. *****************************************************************************}
  316. { Defines the default address size for a processor, }
  317. OS_ADDR = OS_32;
  318. { the natural int size for a processor, }
  319. OS_INT = OS_32;
  320. { the maximum float size for a processor, }
  321. OS_FLOAT = OS_F64;
  322. { the size of a vector register for a processor }
  323. OS_VECTOR = OS_M32;
  324. {*****************************************************************************
  325. Generic Register names
  326. *****************************************************************************}
  327. { Stack pointer register }
  328. NR_STACK_POINTER_REG = NR_R13;
  329. RS_STACK_POINTER_REG = RS_R13;
  330. { Frame pointer register }
  331. RS_FRAME_POINTER_REG = RS_R11;
  332. NR_FRAME_POINTER_REG = NR_R11;
  333. { Register for addressing absolute data in a position independant way,
  334. such as in PIC code. The exact meaning is ABI specific. For
  335. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  336. }
  337. NR_PIC_OFFSET_REG = NR_R9;
  338. { Results are returned in this register (32-bit values) }
  339. NR_FUNCTION_RETURN_REG = NR_R0;
  340. RS_FUNCTION_RETURN_REG = RS_R0;
  341. { Low part of 64bit return value }
  342. NR_FUNCTION_RETURN64_LOW_REG = NR_R0;
  343. RS_FUNCTION_RETURN64_LOW_REG = RS_R0;
  344. { High part of 64bit return value }
  345. NR_FUNCTION_RETURN64_HIGH_REG = NR_R1;
  346. RS_FUNCTION_RETURN64_HIGH_REG = RS_R1;
  347. { The value returned from a function is available in this register }
  348. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  349. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  350. { The lowh part of 64bit value returned from a function }
  351. NR_FUNCTION_RESULT64_LOW_REG = NR_FUNCTION_RETURN64_LOW_REG;
  352. RS_FUNCTION_RESULT64_LOW_REG = RS_FUNCTION_RETURN64_LOW_REG;
  353. { The high part of 64bit value returned from a function }
  354. NR_FUNCTION_RESULT64_HIGH_REG = NR_FUNCTION_RETURN64_HIGH_REG;
  355. RS_FUNCTION_RESULT64_HIGH_REG = RS_FUNCTION_RETURN64_HIGH_REG;
  356. NR_FPU_RESULT_REG = NR_F0;
  357. NR_MM_RESULT_REG = NR_NO;
  358. { Offset where the parent framepointer is pushed }
  359. PARENT_FRAMEPOINTER_OFFSET = 0;
  360. {*****************************************************************************
  361. GCC /ABI linking information
  362. *****************************************************************************}
  363. const
  364. { Registers which must be saved when calling a routine declared as
  365. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  366. saved should be the ones as defined in the target ABI and / or GCC.
  367. This value can be deduced from the CALLED_USED_REGISTERS array in the
  368. GCC source.
  369. }
  370. std_saved_registers = [RS_R4..RS_R10];
  371. { Required parameter alignment when calling a routine declared as
  372. stdcall and cdecl. The alignment value should be the one defined
  373. by GCC or the target ABI.
  374. The value of this constant is equal to the constant
  375. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  376. }
  377. std_param_align = 4;
  378. {*****************************************************************************
  379. Helpers
  380. *****************************************************************************}
  381. function cgsize2subreg(s:Tcgsize):Tsubregister;
  382. function is_calljmp(o:tasmop):boolean;
  383. procedure inverse_flags(var f: TResFlags);
  384. function flags_to_cond(const f: TResFlags) : TAsmCond;
  385. function findreg_by_number(r:Tregister):tregisterindex;
  386. function std_regnum_search(const s:string):Tregister;
  387. function std_regname(r:Tregister):string;
  388. procedure shifterop_reset(var so : tshifterop);
  389. function is_pc(const r : tregister) : boolean;
  390. implementation
  391. uses
  392. rgBase,verbose;
  393. const
  394. std_regname_table : array[tregisterindex] of string[7] = (
  395. {$i rarmstd.inc}
  396. );
  397. regnumber_index : array[tregisterindex] of tregisterindex = (
  398. {$i rarmrni.inc}
  399. );
  400. std_regname_index : array[tregisterindex] of tregisterindex = (
  401. {$i rarmsri.inc}
  402. );
  403. function cgsize2subreg(s:Tcgsize):Tsubregister;
  404. begin
  405. cgsize2subreg:=R_SUBWHOLE;
  406. end;
  407. function is_calljmp(o:tasmop):boolean;
  408. begin
  409. { This isn't 100% perfect because the arm allows jumps also by writing to PC=R15.
  410. To overcome this problem we simply forbid that FPC generates jumps by loading R15 }
  411. is_calljmp:= o in [A_B,A_BL,A_BX,A_BLX];
  412. end;
  413. procedure inverse_flags(var f: TResFlags);
  414. const
  415. inv_flags: array[TResFlags] of TResFlags =
  416. (F_NE,F_NE,F_CC,F_CS,F_PL,F_MI,F_VC,F_VS,F_LS,F_HI,
  417. F_LT,F_GE,F_LE,F_GT);
  418. begin
  419. f:=inv_flags[f];
  420. end;
  421. function flags_to_cond(const f: TResFlags) : TAsmCond;
  422. const
  423. flag_2_cond: array[F_EQ..F_LE] of TAsmCond =
  424. (C_EQ,C_NE,C_CS,C_CC,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  425. C_GE,C_LT,C_GT,C_LE);
  426. begin
  427. if f>high(flag_2_cond) then
  428. internalerror(200112301);
  429. result:=flag_2_cond[f];
  430. end;
  431. function findreg_by_number(r:Tregister):tregisterindex;
  432. begin
  433. rgBase.findreg_by_number_table(r,regnumber_index);
  434. end;
  435. function std_regnum_search(const s:string):Tregister;
  436. begin
  437. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  438. end;
  439. function std_regname(r:Tregister):string;
  440. var
  441. p : tregisterindex;
  442. begin
  443. p:=findreg_by_number_table(r,regnumber_index);
  444. if p<>0 then
  445. result:=std_regname_table[p]
  446. else
  447. result:=generic_regname(r);
  448. end;
  449. procedure shifterop_reset(var so : tshifterop);
  450. begin
  451. FillChar(so,sizeof(so),0);
  452. end;
  453. function is_pc(const r : tregister) : boolean;
  454. begin
  455. is_pc:=(r=NR_R15);
  456. end;
  457. end.
  458. {
  459. $Log$
  460. Revision 1.17 2003-11-02 14:30:03 florian
  461. * fixed ARM for new reg. allocation scheme
  462. Revision 1.16 2003/10/31 08:40:51 mazen
  463. * rgHelper renamed to rgBase
  464. * using findreg_by_<name|number>_table directly to decrease heap overheading
  465. Revision 1.15 2003/10/30 15:02:04 mazen
  466. * now uses standard routines in rgBase unit to search registers by number and by name
  467. Revision 1.14 2003/09/05 23:57:01 florian
  468. * arm is working again as before the new register naming scheme was implemented
  469. Revision 1.13 2003/09/04 21:07:03 florian
  470. * ARM compiler compiles again
  471. Revision 1.12 2003/09/04 00:15:29 florian
  472. * first bunch of adaptions of arm compiler for new register type
  473. Revision 1.11 2003/09/03 19:10:30 florian
  474. * initial revision of new register naming
  475. Revision 1.10 2003/09/01 15:11:16 florian
  476. * fixed reference handling
  477. * fixed operand postfix for floating point instructions
  478. * fixed wrong shifter constant handling
  479. Revision 1.9 2003/08/29 21:36:28 florian
  480. * fixed procedure entry/exit code
  481. * started to fix reference handling
  482. Revision 1.8 2003/08/28 00:05:29 florian
  483. * today's arm patches
  484. Revision 1.7 2003/08/25 23:20:38 florian
  485. + started to implement FPU support for the ARM
  486. * fixed a lot of other things
  487. Revision 1.6 2003/08/24 12:27:26 florian
  488. * continued to work on the arm port
  489. Revision 1.5 2003/08/21 03:14:00 florian
  490. * arm compiler can be compiled; far from being working
  491. Revision 1.4 2003/08/20 15:50:13 florian
  492. * more arm stuff
  493. Revision 1.3 2003/08/16 13:23:01 florian
  494. * several arm related stuff fixed
  495. Revision 1.2 2003/07/26 00:55:57 florian
  496. * basic stuff fixed
  497. Revision 1.1 2003/07/21 16:35:30 florian
  498. * very basic stuff for the arm
  499. }