cpubase.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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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  52. A_SLTU,A_SQRT_D,A_SQRT_S,A_SRA,A_SRAV,A_SRL,A_SRLV,A_SSNOP,
  53. A_SUB,A_SUB_D,A_SUB_S,A_SUBU,A_SW,A_SWC1,A_SWC2,A_SWL,
  54. A_SWR,A_SYNC,A_SYSCALL,A_TEQ,A_TEQI,A_TGE,A_TGEI,A_TGEIU,
  55. A_TGEU,A_TLBP,A_TLBR,A_TLBWI,A_TLBWR,A_TLT,A_TLTI,A_TLTIU,
  56. A_TLTU,A_TNE,A_TNEI,A_TRUNC_W_D,A_TRUNC_W_S,A_WAIT,A_XOR,A_XORI
  57. );
  58. { This should define the array of instructions as string }
  59. op2strtable=array[tasmop] of string[11];
  60. const
  61. { First value of opcode enumeration }
  62. firstop = low(tasmop);
  63. { Last value of opcode enumeration }
  64. lastop = high(tasmop);
  65. {*****************************************************************************
  66. Registers
  67. *****************************************************************************}
  68. type
  69. { Number of registers used for indexing in tables }
  70. tregisterindex=0..{$i rmipsnor.inc}-1;
  71. const
  72. { Available Superregisters }
  73. {$i rmipssup.inc}
  74. { No Subregisters }
  75. R_SUBWHOLE = R_SUBNONE;
  76. { Available Registers }
  77. {$i rmipscon.inc}
  78. { Integer Super registers first and last }
  79. first_int_supreg = RS_R0;
  80. first_int_imreg = $10;
  81. { Float Super register first and last }
  82. first_fpu_supreg = RS_F0;
  83. first_fpu_imreg = $08;
  84. { MM Super register first and last }
  85. first_mm_supreg = RS_NO;
  86. first_mm_imreg = RS_NO;
  87. {$warning TODO Calculate bsstart}
  88. regnumber_count_bsstart = 64;
  89. regnumber_table : array[tregisterindex] of tregister = (
  90. {$i rmipsnum.inc}
  91. );
  92. regstabs_table : array[tregisterindex] of shortint = (
  93. {$i rmipssta.inc}
  94. );
  95. regdwarf_table : array[tregisterindex] of shortint = (
  96. {$i rmipsdwf.inc}
  97. );
  98. { registers which may be destroyed by calls }
  99. VOLATILE_INTREGISTERS = [RS_R0..RS_R3,RS_R12..RS_R15];
  100. VOLATILE_FPUREGISTERS = [RS_F0..RS_F3];
  101. type
  102. totherregisterset = set of tregisterindex;
  103. {*****************************************************************************
  104. Instruction post fixes
  105. *****************************************************************************}
  106. type
  107. { ARM instructions load/store and arithmetic instructions
  108. can have several instruction post fixes which are collected
  109. in this enumeration
  110. }
  111. TOpPostfix = (PF_None,
  112. { update condition flags
  113. or floating point single }
  114. PF_S,
  115. { floating point size }
  116. PF_D,PF_E,PF_P,PF_EP,
  117. { load/store }
  118. PF_B,PF_SB,PF_BT,PF_H,PF_SH,PF_T,
  119. { multiple load/store address modes }
  120. PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA
  121. );
  122. TRoundingMode = (RM_None,RM_P,RM_M,RM_Z);
  123. const
  124. cgsize2fpuoppostfix : array[OS_NO..OS_F128] of toppostfix = (
  125. PF_E,
  126. PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,
  127. PF_S,PF_D,PF_E,PF_None,PF_None);
  128. oppostfix2str : array[TOpPostfix] of string[2] = ('',
  129. 's',
  130. 'd','e','p','ep',
  131. 'b','sb','bt','h','sh','t',
  132. 'ia','ib','da','db','fd','fa','ed','ea');
  133. roundingmode2str : array[TRoundingMode] of string[1] = ('',
  134. 'p','m','z');
  135. {*****************************************************************************
  136. Conditions
  137. *****************************************************************************}
  138. type
  139. TAsmCond=(C_None,
  140. C_EQ,C_NE,C_CS,C_CC,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  141. C_GE,C_LT,C_GT,C_LE,C_AL,C_NV
  142. );
  143. const
  144. cond2str : array[TAsmCond] of string[2]=('',
  145. 'eq','ne','cs','cc','mi','pl','vs','vc','hi','ls',
  146. 'ge','lt','gt','le','al','nv'
  147. );
  148. uppercond2str : array[TAsmCond] of string[2]=('',
  149. 'EQ','NE','CS','CC','MI','PL','VS','VC','HI','LS',
  150. 'GE','LT','GT','LE','AL','NV'
  151. );
  152. inverse_cond : array[TAsmCond] of TAsmCond=(C_None,
  153. C_NE,C_EQ,C_CC,C_CS,C_PL,C_MI,C_VC,C_VS,C_LS,C_HI,
  154. C_LT,C_GE,C_LE,C_GT,C_None,C_None
  155. );
  156. {*****************************************************************************
  157. Flags
  158. *****************************************************************************}
  159. type
  160. TResFlags = (F_EQ,F_NE,F_CS,F_CC,F_MI,F_PL,F_VS,F_VC,F_HI,F_LS,
  161. F_GE,F_LT,F_GT,F_LE);
  162. {*****************************************************************************
  163. Operands
  164. *****************************************************************************}
  165. taddressmode = (AM_OFFSET,AM_PREINDEXED,AM_POSTINDEXED);
  166. tshiftmode = (SM_None,SM_LSL,SM_LSR,SM_ASR,SM_ROR,SM_RRX);
  167. tupdatereg = (UR_None,UR_Update);
  168. pshifterop = ^tshifterop;
  169. tshifterop = record
  170. shiftmode : tshiftmode;
  171. rs : tregister;
  172. shiftimm : byte;
  173. end;
  174. {*****************************************************************************
  175. Constants
  176. *****************************************************************************}
  177. const
  178. max_operands = 4;
  179. { Constant defining possibly all registers which might require saving }
  180. ALL_OTHERREGISTERS = [];
  181. general_superregisters = [RS_R0..RS_PC];
  182. { Table of registers which can be allocated by the code generator
  183. internally, when generating the code.
  184. }
  185. { legend: }
  186. { xxxregs = set of all possibly used registers of that type in the code }
  187. { generator }
  188. { usableregsxxx = set of all 32bit components of registers that can be }
  189. { possible allocated to a regvar or using getregisterxxx (this }
  190. { excludes registers which can be only used for parameter }
  191. { passing on ABI's that define this) }
  192. { c_countusableregsxxx = amount of registers in the usableregsxxx set }
  193. maxintregs = 15;
  194. { to determine how many registers to use for regvars }
  195. maxintscratchregs = 3;
  196. usableregsint = [RS_R4..RS_R10];
  197. c_countusableregsint = 7;
  198. maxfpuregs = 8;
  199. fpuregs = [RS_F0..RS_F7];
  200. usableregsfpu = [RS_F4..RS_F7];
  201. c_countusableregsfpu = 4;
  202. mmregs = [RS_NO..RS_NO];
  203. usableregsmm = [RS_NO..RS_NO];
  204. c_countusableregsmm = 0;
  205. maxaddrregs = 0;
  206. addrregs = [];
  207. usableregsaddr = [];
  208. c_countusableregsaddr = 0;
  209. {*****************************************************************************
  210. Operand Sizes
  211. *****************************************************************************}
  212. type
  213. topsize = (S_NO,
  214. S_B,S_W,S_L,S_BW,S_BL,S_WL,
  215. S_IS,S_IL,S_IQ,
  216. S_FS,S_FL,S_FX,S_D,S_Q,S_FV,S_FXX
  217. );
  218. {*****************************************************************************
  219. Constants
  220. *****************************************************************************}
  221. const
  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..8] of tsuperregister =
  287. (RS_R16,RS_R17,RS_R18,RS_R19,RS_R20,RS_R21,RS_R22,RS_R23,RS_R30);
  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. procedure shifterop_reset(var so : tshifterop);
  308. function is_pc(const r : tregister) : boolean;
  309. implementation
  310. uses
  311. rgBase,verbose;
  312. const
  313. std_regname_table : array[tregisterindex] of string[7] = (
  314. {$i rmipsstd.inc}
  315. );
  316. regnumber_index : array[tregisterindex] of tregisterindex = (
  317. {$i rmipsrni.inc}
  318. );
  319. std_regname_index : array[tregisterindex] of tregisterindex = (
  320. {$i rmipssri.inc}
  321. );
  322. function cgsize2subreg(s:Tcgsize):Tsubregister;
  323. begin
  324. cgsize2subreg:=R_SUBWHOLE;
  325. end;
  326. function reg_cgsize(const reg: tregister): tcgsize;
  327. const subreg2cgsize:array[Tsubregister] of Tcgsize =
  328. (OS_NO,OS_8,OS_8,OS_16,OS_32,OS_64,OS_NO,OS_NO,OS_NO);
  329. begin
  330. case getregtype(reg) of
  331. R_INTREGISTER :
  332. reg_cgsize:=OS_32;
  333. R_FPUREGISTER :
  334. reg_cgsize:=OS_F80;
  335. else
  336. internalerror(200303181);
  337. end;
  338. end;
  339. function is_calljmp(o:tasmop):boolean;
  340. begin
  341. { This isn't 100% perfect because the arm allows jumps also by writing to PC=R15.
  342. To overcome this problem we simply forbid that FPC generates jumps by loading R15 }
  343. is_calljmp:= o in [A_J,A_JAL,A_JALR,{ A_JALX, }A_JR,
  344. A_BEQ,A_BNE,A_BGEZ,A_BGEZAL,A_BGTZ,A_BLEZ,A_BLTZ,A_BLTZAL,
  345. A_BEQL,A_BGEZALL,A_BGEZL,A_BGTZL,A_BLEZL,A_BLTZALL,A_BLTZL,A_BNEL];
  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. end.
  392. {
  393. $Log$
  394. Revision 1.1 2005-02-13 18:56:44 florian
  395. + basic mips stuff
  396. }