cpubase.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Peter Vreman
  3. Contains the base types for the ARM
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {# Base unit for processor information. This unit contains
  18. enumerations of registers, opcodes, sizes, and other
  19. such things which are processor specific.
  20. }
  21. unit cpubase;
  22. {$i fpcdefs.inc}
  23. interface
  24. uses
  25. cutils,cclasses,
  26. globtype,globals,
  27. cpuinfo,
  28. aasmbase,
  29. cgbase
  30. ;
  31. {*****************************************************************************
  32. Assembler Opcodes
  33. *****************************************************************************}
  34. type
  35. TAsmOp=(A_ABS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RT_D,A_SQRT_S,A_SRA,A_SRAV,A_SRL,A_SRLV,A_SSNOP,
  52. A_SUB,A_SUB_D,A_SUB_S,A_SUBU,A_SW,A_SWC1,A_SWC2,A_SWL,
  53. A_SWR,A_SYNC,A_SYSCALL,A_TEQ,A_TEQI,A_TGE,A_TGEI,A_TGEIU,
  54. A_TGEU,A_TLBP,A_TLBR,A_TLBWI,A_TLBWR,A_TLT,A_TLTI,A_TLTIU,
  55. A_TLTU,A_TNE,A_TNEI,A_TRUNC_W_D,A_TRUNC_W_S,A_WAIT,A_XOR,A_XORI
  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 rmipsnor.inc}-1;
  70. const
  71. { Available Superregisters }
  72. {$i rmipssup.inc}
  73. { No Subregisters }
  74. R_SUBWHOLE = R_SUBNONE;
  75. { Available Registers }
  76. {$i rmipscon.inc}
  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_NO;
  85. first_mm_imreg = RS_NO;
  86. {$warning TODO Calculate bsstart}
  87. regnumber_count_bsstart = 64;
  88. regnumber_table : array[tregisterindex] of tregister = (
  89. {$i rmipsnum.inc}
  90. );
  91. regstabs_table : array[tregisterindex] of shortint = (
  92. {$i rmipssta.inc}
  93. );
  94. regdwarf_table : array[tregisterindex] of shortint = (
  95. {$i rmipsdwf.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_NO..RS_NO];
  202. usableregsmm = [RS_NO..RS_NO];
  203. c_countusableregsmm = 0;
  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. maxvarregs = 7;
  222. varregs : Array [1..maxvarregs] of tsuperregister =
  223. (RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,RS_R9,RS_R10);
  224. maxfpuvarregs = 4;
  225. fpuvarregs : Array [1..maxfpuvarregs] of tsuperregister =
  226. (RS_F4,RS_F5,RS_F6,RS_F7);
  227. {*****************************************************************************
  228. Default generic sizes
  229. *****************************************************************************}
  230. { Defines the default address size for a processor, }
  231. OS_ADDR = OS_32;
  232. { the natural int size for a processor, }
  233. OS_INT = OS_32;
  234. { the maximum float size for a processor, }
  235. OS_FLOAT = OS_F64;
  236. { the size of a vector register for a processor }
  237. OS_VECTOR = OS_M32;
  238. {*****************************************************************************
  239. Generic Register names
  240. *****************************************************************************}
  241. { Stack pointer register }
  242. NR_STACK_POINTER_REG = NR_R13;
  243. RS_STACK_POINTER_REG = RS_R13;
  244. { Frame pointer register }
  245. RS_FRAME_POINTER_REG = RS_R11;
  246. NR_FRAME_POINTER_REG = NR_R11;
  247. { Register for addressing absolute data in a position independant way,
  248. such as in PIC code. The exact meaning is ABI specific. For
  249. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  250. }
  251. NR_PIC_OFFSET_REG = NR_R9;
  252. { Results are returned in this register (32-bit values) }
  253. NR_FUNCTION_RETURN_REG = NR_R0;
  254. RS_FUNCTION_RETURN_REG = RS_R0;
  255. { Low part of 64bit return value }
  256. NR_FUNCTION_RETURN64_LOW_REG = NR_R0;
  257. RS_FUNCTION_RETURN64_LOW_REG = RS_R0;
  258. { High part of 64bit return value }
  259. NR_FUNCTION_RETURN64_HIGH_REG = NR_R1;
  260. RS_FUNCTION_RETURN64_HIGH_REG = RS_R1;
  261. { The value returned from a function is available in this register }
  262. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  263. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  264. { The lowh part of 64bit value returned from a function }
  265. NR_FUNCTION_RESULT64_LOW_REG = NR_FUNCTION_RETURN64_LOW_REG;
  266. RS_FUNCTION_RESULT64_LOW_REG = RS_FUNCTION_RETURN64_LOW_REG;
  267. { The high part of 64bit value returned from a function }
  268. NR_FUNCTION_RESULT64_HIGH_REG = NR_FUNCTION_RETURN64_HIGH_REG;
  269. RS_FUNCTION_RESULT64_HIGH_REG = RS_FUNCTION_RETURN64_HIGH_REG;
  270. NR_FPU_RESULT_REG = NR_F0;
  271. NR_MM_RESULT_REG = NR_NO;
  272. NR_RETURN_ADDRESS_REG = NR_FUNCTION_RETURN_REG;
  273. { Offset where the parent framepointer is pushed }
  274. PARENT_FRAMEPOINTER_OFFSET = 0;
  275. {*****************************************************************************
  276. GCC /ABI linking information
  277. *****************************************************************************}
  278. const
  279. { Registers which must be saved when calling a routine declared as
  280. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  281. saved should be the ones as defined in the target ABI and / or GCC.
  282. This value can be deduced from the CALLED_USED_REGISTERS array in the
  283. GCC source.
  284. }
  285. saved_standard_registers : array[0..8] of tsuperregister =
  286. (RS_R16,RS_R17,RS_R18,RS_R19,RS_R20,RS_R21,RS_R22,RS_R23,RS_R30);
  287. { Required parameter alignment when calling a routine declared as
  288. stdcall and cdecl. The alignment value should be the one defined
  289. by GCC or the target ABI.
  290. The value of this constant is equal to the constant
  291. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  292. }
  293. std_param_align = 4;
  294. {*****************************************************************************
  295. Helpers
  296. *****************************************************************************}
  297. { Returns the tcgsize corresponding with the size of reg.}
  298. function reg_cgsize(const reg: tregister) : tcgsize;
  299. function cgsize2subreg(s:Tcgsize):Tsubregister;
  300. function is_calljmp(o:tasmop):boolean;
  301. procedure inverse_flags(var f: TResFlags);
  302. function flags_to_cond(const f: TResFlags) : TAsmCond;
  303. function findreg_by_number(r:Tregister):tregisterindex;
  304. function std_regnum_search(const s:string):Tregister;
  305. function std_regname(r:Tregister):string;
  306. procedure shifterop_reset(var so : tshifterop);
  307. function is_pc(const r : tregister) : boolean;
  308. implementation
  309. uses
  310. rgBase,verbose;
  311. const
  312. std_regname_table : array[tregisterindex] of string[7] = (
  313. {$i rmipsstd.inc}
  314. );
  315. regnumber_index : array[tregisterindex] of tregisterindex = (
  316. {$i rmipsrni.inc}
  317. );
  318. std_regname_index : array[tregisterindex] of tregisterindex = (
  319. {$i rmipssri.inc}
  320. );
  321. function cgsize2subreg(s:Tcgsize):Tsubregister;
  322. begin
  323. cgsize2subreg:=R_SUBWHOLE;
  324. end;
  325. function reg_cgsize(const reg: tregister): tcgsize;
  326. const subreg2cgsize:array[Tsubregister] of Tcgsize =
  327. (OS_NO,OS_8,OS_8,OS_16,OS_32,OS_64,OS_NO,OS_NO,OS_NO);
  328. begin
  329. case getregtype(reg) of
  330. R_INTREGISTER :
  331. reg_cgsize:=OS_32;
  332. R_FPUREGISTER :
  333. reg_cgsize:=OS_F80;
  334. else
  335. internalerror(200303181);
  336. end;
  337. end;
  338. function is_calljmp(o:tasmop):boolean;
  339. begin
  340. { This isn't 100% perfect because the arm allows jumps also by writing to PC=R15.
  341. To overcome this problem we simply forbid that FPC generates jumps by loading R15 }
  342. is_calljmp:= o in [A_J,A_JAL,A_JALR,{ A_JALX, }A_JR,
  343. A_BEQ,A_BNE,A_BGEZ,A_BGEZAL,A_BGTZ,A_BLEZ,A_BLTZ,A_BLTZAL,
  344. A_BEQL,A_BGEZALL,A_BGEZL,A_BGTZL,A_BLEZL,A_BLTZALL,A_BLTZL,A_BNEL];
  345. end;
  346. procedure inverse_flags(var f: TResFlags);
  347. const
  348. inv_flags: array[TResFlags] of TResFlags =
  349. (F_NE,F_EQ,F_CC,F_CS,F_PL,F_MI,F_VC,F_VS,F_LS,F_HI,
  350. F_LT,F_GE,F_LE,F_GT);
  351. begin
  352. f:=inv_flags[f];
  353. end;
  354. function flags_to_cond(const f: TResFlags) : TAsmCond;
  355. const
  356. flag_2_cond: array[F_EQ..F_LE] of TAsmCond =
  357. (C_EQ,C_NE,C_CS,C_CC,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  358. C_GE,C_LT,C_GT,C_LE);
  359. begin
  360. if f>high(flag_2_cond) then
  361. internalerror(200112301);
  362. result:=flag_2_cond[f];
  363. end;
  364. function findreg_by_number(r:Tregister):tregisterindex;
  365. begin
  366. result:=rgBase.findreg_by_number_table(r,regnumber_index);
  367. end;
  368. function std_regnum_search(const s:string):Tregister;
  369. begin
  370. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  371. end;
  372. function std_regname(r:Tregister):string;
  373. var
  374. p : tregisterindex;
  375. begin
  376. p:=findreg_by_number_table(r,regnumber_index);
  377. if p<>0 then
  378. result:=std_regname_table[p]
  379. else
  380. result:=generic_regname(r);
  381. end;
  382. procedure shifterop_reset(var so : tshifterop);
  383. begin
  384. FillChar(so,sizeof(so),0);
  385. end;
  386. function is_pc(const r : tregister) : boolean;
  387. begin
  388. is_pc:=(r=NR_R15);
  389. end;
  390. end.