cpubase.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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=(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,A_SQRT_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. { 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. OS_SINT = OS_S32;
  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. { this is only for the generic code which is not used for this architecture }
  289. saved_mm_registers : array[0..0] of tsuperregister = (RS_NO);
  290. { Required parameter alignment when calling a routine declared as
  291. stdcall and cdecl. The alignment value should be the one defined
  292. by GCC or the target ABI.
  293. The value of this constant is equal to the constant
  294. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  295. }
  296. std_param_align = 4;
  297. {*****************************************************************************
  298. Helpers
  299. *****************************************************************************}
  300. { Returns the tcgsize corresponding with the size of reg.}
  301. function reg_cgsize(const reg: tregister) : tcgsize;
  302. function cgsize2subreg(s:Tcgsize):Tsubregister;
  303. function is_calljmp(o:tasmop):boolean;
  304. procedure inverse_flags(var f: TResFlags);
  305. function flags_to_cond(const f: TResFlags) : TAsmCond;
  306. function findreg_by_number(r:Tregister):tregisterindex;
  307. function std_regnum_search(const s:string):Tregister;
  308. function std_regname(r:Tregister):string;
  309. procedure shifterop_reset(var so : tshifterop);
  310. function is_pc(const r : tregister) : boolean;
  311. implementation
  312. uses
  313. rgBase,verbose;
  314. const
  315. std_regname_table : array[tregisterindex] of string[7] = (
  316. {$i rmipsstd.inc}
  317. );
  318. regnumber_index : array[tregisterindex] of tregisterindex = (
  319. {$i rmipsrni.inc}
  320. );
  321. std_regname_index : array[tregisterindex] of tregisterindex = (
  322. {$i rmipssri.inc}
  323. );
  324. function cgsize2subreg(s:Tcgsize):Tsubregister;
  325. begin
  326. cgsize2subreg:=R_SUBWHOLE;
  327. end;
  328. function reg_cgsize(const reg: tregister): tcgsize;
  329. const subreg2cgsize:array[Tsubregister] of Tcgsize =
  330. (OS_NO,OS_8,OS_8,OS_16,OS_32,OS_64,OS_NO,OS_NO,OS_NO);
  331. begin
  332. case getregtype(reg) of
  333. R_INTREGISTER :
  334. reg_cgsize:=OS_32;
  335. R_FPUREGISTER :
  336. reg_cgsize:=OS_F80;
  337. else
  338. internalerror(200303181);
  339. end;
  340. end;
  341. function is_calljmp(o:tasmop):boolean;
  342. begin
  343. { This isn't 100% perfect because the arm allows jumps also by writing to PC=R15.
  344. To overcome this problem we simply forbid that FPC generates jumps by loading R15 }
  345. is_calljmp:= o in [A_J,A_JAL,A_JALR,{ A_JALX, }A_JR,
  346. A_BEQ,A_BNE,A_BGEZ,A_BGEZAL,A_BGTZ,A_BLEZ,A_BLTZ,A_BLTZAL,
  347. A_BEQL,A_BGEZALL,A_BGEZL,A_BGTZL,A_BLEZL,A_BLTZALL,A_BLTZL,A_BNEL];
  348. end;
  349. procedure inverse_flags(var f: TResFlags);
  350. const
  351. inv_flags: array[TResFlags] of TResFlags =
  352. (F_NE,F_EQ,F_CC,F_CS,F_PL,F_MI,F_VC,F_VS,F_LS,F_HI,
  353. F_LT,F_GE,F_LE,F_GT);
  354. begin
  355. f:=inv_flags[f];
  356. end;
  357. function flags_to_cond(const f: TResFlags) : TAsmCond;
  358. const
  359. flag_2_cond: array[F_EQ..F_LE] of TAsmCond =
  360. (C_EQ,C_NE,C_CS,C_CC,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  361. C_GE,C_LT,C_GT,C_LE);
  362. begin
  363. if f>high(flag_2_cond) then
  364. internalerror(200112301);
  365. result:=flag_2_cond[f];
  366. end;
  367. function findreg_by_number(r:Tregister):tregisterindex;
  368. begin
  369. result:=rgBase.findreg_by_number_table(r,regnumber_index);
  370. end;
  371. function std_regnum_search(const s:string):Tregister;
  372. begin
  373. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  374. end;
  375. function std_regname(r:Tregister):string;
  376. var
  377. p : tregisterindex;
  378. begin
  379. p:=findreg_by_number_table(r,regnumber_index);
  380. if p<>0 then
  381. result:=std_regname_table[p]
  382. else
  383. result:=generic_regname(r);
  384. end;
  385. procedure shifterop_reset(var so : tshifterop);
  386. begin
  387. FillChar(so,sizeof(so),0);
  388. end;
  389. function is_pc(const r : tregister) : boolean;
  390. begin
  391. is_pc:=(r=NR_R15);
  392. end;
  393. end.