cpubase.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Peter Vreman
  3. Contains the base types for MIPS
  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=({$i opcode.inc});
  36. { This should define the array of instructions as string }
  37. op2strtable=array[tasmop] of string[11];
  38. const
  39. { First value of opcode enumeration }
  40. firstop = low(tasmop);
  41. { Last value of opcode enumeration }
  42. lastop = high(tasmop);
  43. {*****************************************************************************
  44. Registers
  45. *****************************************************************************}
  46. type
  47. { Number of registers used for indexing in tables }
  48. tregisterindex=0..{$i rmipsnor.inc}-1;
  49. const
  50. { Available Superregisters }
  51. {$i rmipssup.inc}
  52. { No Subregisters }
  53. R_SUBWHOLE = R_SUBNONE;
  54. { Available Registers }
  55. {$i rmipscon.inc}
  56. { Integer Super registers first and last }
  57. first_int_supreg = RS_R0;
  58. first_int_imreg = $10;
  59. { Float Super register first and last }
  60. first_fpu_supreg = RS_F0;
  61. first_fpu_imreg = $08;
  62. { MM Super register first and last }
  63. first_mm_supreg = RS_NO;
  64. first_mm_imreg = RS_NO;
  65. { TODO: Calculate bsstart}
  66. regnumber_count_bsstart = 64;
  67. regnumber_table : array[tregisterindex] of tregister = (
  68. {$i rmipsnum.inc}
  69. );
  70. regstabs_table : array[tregisterindex] of shortint = (
  71. {$i rmipssta.inc}
  72. );
  73. regdwarf_table : array[tregisterindex] of shortint = (
  74. {$i rmipsdwf.inc}
  75. );
  76. { registers which may be destroyed by calls }
  77. VOLATILE_INTREGISTERS = [RS_R0..RS_R3,RS_R12..RS_R15];
  78. VOLATILE_FPUREGISTERS = [RS_F0..RS_F3];
  79. type
  80. totherregisterset = set of tregisterindex;
  81. {*****************************************************************************
  82. Conditions
  83. *****************************************************************************}
  84. type
  85. TAsmCond=(C_None,
  86. C_EQ, C_NE, C_LT, C_LE, C_GT, C_GE, C_LTU, C_LEU, C_GTU, C_GEU,
  87. C_FEQ, {Equal}
  88. C_FNE, {Not Equal}
  89. C_FGT, {Greater}
  90. C_FLT, {Less}
  91. C_FGE, {Greater or Equal}
  92. C_FLE {Less or Equal}
  93. );
  94. const
  95. cond2str : array[TAsmCond] of string[3]=('',
  96. 'eq','ne','lt','le','gt','ge','ltu','leu','gtu','geu',
  97. 'feq','fne','fgt','flt','fge','fle'
  98. );
  99. {*****************************************************************************
  100. Constants
  101. *****************************************************************************}
  102. const
  103. max_operands = 4;
  104. { Constant defining possibly all registers which might require saving }
  105. ALL_OTHERREGISTERS = [];
  106. general_superregisters = [RS_R0..RS_R31];
  107. { Table of registers which can be allocated by the code generator
  108. internally, when generating the code.
  109. }
  110. { legend: }
  111. { xxxregs = set of all possibly used registers of that type in the code }
  112. { generator }
  113. { usableregsxxx = set of all 32bit components of registers that can be }
  114. { possible allocated to a regvar or using getregisterxxx (this }
  115. { excludes registers which can be only used for parameter }
  116. { passing on ABI's that define this) }
  117. { c_countusableregsxxx = amount of registers in the usableregsxxx set }
  118. maxintregs = 31;
  119. { to determine how many registers to use for regvars }
  120. maxintscratchregs = 3;
  121. usableregsint = [RS_R4..RS_R10];
  122. c_countusableregsint = 7;
  123. maxfpuregs = 8;
  124. fpuregs = [RS_F0..RS_F7];
  125. usableregsfpu = [RS_F4..RS_F7];
  126. c_countusableregsfpu = 4;
  127. mmregs = [RS_NO..RS_NO];
  128. usableregsmm = [RS_NO..RS_NO];
  129. c_countusableregsmm = 0;
  130. maxaddrregs = 0;
  131. addrregs = [];
  132. usableregsaddr = [];
  133. c_countusableregsaddr = 0;
  134. {*****************************************************************************
  135. Operand Sizes
  136. *****************************************************************************}
  137. type
  138. topsize = (S_NO,
  139. S_B,S_W,S_L,S_BW,S_BL,S_WL,
  140. S_IS,S_IL,S_IQ,
  141. S_FS,S_FL,S_FX,S_D,S_Q,S_FV,S_FXX
  142. );
  143. {*****************************************************************************
  144. Constants
  145. *****************************************************************************}
  146. const
  147. maxvarregs = 7;
  148. varregs : Array [1..maxvarregs] of tsuperregister =
  149. (RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,RS_R9,RS_R10);
  150. maxfpuvarregs = 4;
  151. fpuvarregs : Array [1..maxfpuvarregs] of tsuperregister =
  152. (RS_F4,RS_F5,RS_F6,RS_F7);
  153. {*****************************************************************************
  154. Default generic sizes
  155. *****************************************************************************}
  156. { Defines the default address size for a processor, }
  157. OS_ADDR = OS_32;
  158. { the natural int size for a processor, }
  159. OS_INT = OS_32;
  160. OS_SINT = OS_S32;
  161. { the maximum float size for a processor, }
  162. OS_FLOAT = OS_F64;
  163. { the size of a vector register for a processor }
  164. OS_VECTOR = OS_M32;
  165. {*****************************************************************************
  166. Generic Register names
  167. *****************************************************************************}
  168. STK2_PTR = NR_R23;
  169. NR_GP = NR_R28;
  170. NR_SP = NR_R29;
  171. NR_S8 = NR_R30;
  172. NR_FP = NR_R30;
  173. NR_RA = NR_R31;
  174. RS_GP = RS_R28;
  175. RS_SP = RS_R29;
  176. RS_S8 = RS_R30;
  177. RS_FP = RS_R30;
  178. RS_RA = RS_R31;
  179. {# Stack pointer register }
  180. NR_STACK_POINTER_REG = NR_SP;
  181. RS_STACK_POINTER_REG = RS_SP;
  182. {# Frame pointer register }
  183. NR_FRAME_POINTER_REG = NR_FP;
  184. RS_FRAME_POINTER_REG = RS_FP;
  185. NR_RETURN_ADDRESS_REG = NR_R7;
  186. { the return_result_reg, is used inside the called function to store its return
  187. value when that is a scalar value otherwise a pointer to the address of the
  188. result is placed inside it }
  189. { Results are returned in this register (32-bit values) }
  190. NR_FUNCTION_RETURN_REG = NR_R2;
  191. RS_FUNCTION_RETURN_REG = RS_R2;
  192. { Low part of 64bit return value }
  193. NR_FUNCTION_RETURN64_LOW_REG = NR_R2;
  194. RS_FUNCTION_RETURN64_LOW_REG = RS_R2;
  195. { High part of 64bit return value }
  196. NR_FUNCTION_RETURN64_HIGH_REG = NR_R3;
  197. RS_FUNCTION_RETURN64_HIGH_REG = RS_R3;
  198. { The value returned from a function is available in this register }
  199. NR_FUNCTION_RESULT_REG = NR_R2;
  200. RS_FUNCTION_RESULT_REG = RS_R2;
  201. { The lowh part of 64bit value returned from a function }
  202. NR_FUNCTION_RESULT64_LOW_REG = NR_R2;
  203. RS_FUNCTION_RESULT64_LOW_REG = RS_R2;
  204. { The high part of 64bit value returned from a function }
  205. NR_FUNCTION_RESULT64_HIGH_REG = NR_R3;
  206. RS_FUNCTION_RESULT64_HIGH_REG = RS_R3;
  207. NR_FPU_RESULT_REG = NR_F0;
  208. NR_MM_RESULT_REG = NR_NO;
  209. NR_TCR0 = NR_R15;
  210. NR_TCR1 = NR_R3;
  211. NR_TCR10 = NR_R20;
  212. NR_TCR11 = NR_R21;
  213. NR_TCR12 = NR_R18;
  214. NR_TCR13 = NR_R19;
  215. {*****************************************************************************
  216. GCC /ABI linking information
  217. *****************************************************************************}
  218. const
  219. { Registers which must be saved when calling a routine declared as
  220. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  221. saved should be the ones as defined in the target ABI and / or GCC.
  222. This value can be deduced from the CALLED_USED_REGISTERS array in the
  223. GCC source.
  224. }
  225. saved_standard_registers : array[0..0] of tsuperregister =
  226. (RS_NO);
  227. { this is only for the generic code which is not used for this architecture }
  228. saved_mm_registers : array[0..0] of tsuperregister = (RS_NO);
  229. { Required parameter alignment when calling a routine declared as
  230. stdcall and cdecl. The alignment value should be the one defined
  231. by GCC or the target ABI.
  232. The value of this constant is equal to the constant
  233. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  234. }
  235. std_param_align = 4;
  236. {*****************************************************************************
  237. Helpers
  238. *****************************************************************************}
  239. { Returns the tcgsize corresponding with the size of reg.}
  240. function reg_cgsize(const reg: tregister) : tcgsize;
  241. function cgsize2subreg(regtype: tregistertype; s:tcgsize):tsubregister;
  242. function is_calljmp(o:tasmop):boolean;
  243. function findreg_by_number(r:Tregister):tregisterindex;
  244. function std_regnum_search(const s:string):Tregister;
  245. function std_regname(r:Tregister):string;
  246. implementation
  247. uses
  248. rgBase,verbose;
  249. const
  250. std_regname_table : array[tregisterindex] of string[7] = (
  251. {$i rmipsstd.inc}
  252. );
  253. regnumber_index : array[tregisterindex] of tregisterindex = (
  254. {$i rmipsrni.inc}
  255. );
  256. std_regname_index : array[tregisterindex] of tregisterindex = (
  257. {$i rmipssri.inc}
  258. );
  259. function cgsize2subreg(regtype: tregistertype; s:tcgsize):tsubregister;
  260. begin
  261. if s in [OS_64,OS_S64] then
  262. cgsize2subreg:=R_SUBQ
  263. else
  264. cgsize2subreg:=R_SUBWHOLE;
  265. end;
  266. function reg_cgsize(const reg: tregister): tcgsize;
  267. begin
  268. case getregtype(reg) of
  269. R_INTREGISTER :
  270. reg_cgsize:=OS_32;
  271. R_FPUREGISTER :
  272. begin
  273. if getsubreg(reg)=R_SUBFD then
  274. result:=OS_F64
  275. else
  276. result:=OS_F32;
  277. end;
  278. else
  279. internalerror(200303181);
  280. end;
  281. end;
  282. function is_calljmp(o:tasmop):boolean;
  283. begin
  284. { This isn't 100% perfect because the arm allows jumps also by writing to PC=R15.
  285. To overcome this problem we simply forbid that FPC generates jumps by loading R15 }
  286. is_calljmp:= o in [A_J,A_JAL,A_JALR,{ A_JALX, }A_JR,
  287. A_BEQ,A_BNE,A_BGEZ,A_BGEZAL,A_BGTZ,A_BLEZ,A_BLTZ,A_BLTZAL,
  288. A_BEQL,A_BGEZALL,A_BGEZL,A_BGTZL,A_BLEZL,A_BLTZALL,A_BLTZL,A_BNEL];
  289. end;
  290. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  291. const
  292. inverse: array[TAsmCond] of TAsmCond=(C_None,
  293. C_EQ, C_NE, C_LT, C_LE, C_GT, C_GE, C_LTU, C_LEU, C_GTU, C_GEU,
  294. C_FEQ, {Equal}
  295. C_FNE, {Not Equal}
  296. C_FGT, {Greater}
  297. C_FLT, {Less}
  298. C_FGE, {Greater or Equal}
  299. C_FLE {Less or Equal}
  300. );
  301. begin
  302. result := inverse[c];
  303. end; function findreg_by_number(r:Tregister):tregisterindex;
  304. begin
  305. result:=rgBase.findreg_by_number_table(r,regnumber_index);
  306. end;
  307. function std_regnum_search(const s:string):Tregister;
  308. begin
  309. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  310. end;
  311. function std_regname(r:Tregister):string;
  312. var
  313. p : tregisterindex;
  314. begin
  315. p:=findreg_by_number_table(r,regnumber_index);
  316. if p<>0 then
  317. result:=std_regname_table[p]
  318. else
  319. result:=generic_regname(r);
  320. end;
  321. end.