cpubase.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Contains the base types for the SPARC
  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. unit cpubase;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,strings,cutils,cclasses,aasmbase,cpuinfo,cgbase;
  22. {*****************************************************************************
  23. Assembler Opcodes
  24. *****************************************************************************}
  25. type
  26. { TODO: CPU32 opcodes do not fully include the Ultra SPRAC instruction set.}
  27. { don't change the order of these opcodes! }
  28. TAsmOp=({$i opcode.inc});
  29. {# This should define the array of instructions as string }
  30. op2strtable=array[tasmop] of string[11];
  31. Const
  32. {# First value of opcode enumeration }
  33. firstop = low(tasmop);
  34. {# Last value of opcode enumeration }
  35. lastop = high(tasmop);
  36. std_op2str:op2strtable=({$i strinst.inc});
  37. {*****************************************************************************
  38. Registers
  39. *****************************************************************************}
  40. type
  41. { Number of registers used for indexing in tables }
  42. tregisterindex=0..{$i rspnor.inc}-1;
  43. totherregisterset = set of tregisterindex;
  44. const
  45. { Available Superregisters }
  46. {$i rspsup.inc}
  47. { No Subregisters }
  48. R_SUBWHOLE = R_SUBD;
  49. { Available Registers }
  50. {$i rspcon.inc}
  51. first_int_imreg = $20;
  52. first_fpu_imreg = $20;
  53. { MM Super register first and last }
  54. first_mm_supreg = 0;
  55. first_mm_imreg = 1;
  56. { TODO: Calculate bsstart}
  57. regnumber_count_bsstart = 128;
  58. regnumber_table : array[tregisterindex] of tregister = (
  59. {$i rspnum.inc}
  60. );
  61. regstabs_table : array[tregisterindex] of ShortInt = (
  62. {$i rspstab.inc}
  63. );
  64. regdwarf_table : array[tregisterindex] of ShortInt = (
  65. {$i rspdwrf.inc}
  66. );
  67. {*****************************************************************************
  68. Conditions
  69. *****************************************************************************}
  70. type
  71. TAsmCond=(C_None,
  72. C_A,C_AE,C_B,C_BE,C_C,C_E,C_G,C_GE,C_L,C_LE,C_NA,C_NAE,
  73. C_NB,C_NBE,C_NC,C_NE,C_NG,C_NGE,C_NL,C_NLE,C_NO,C_NP,
  74. C_NS,C_NZ,C_O,C_P,C_PE,C_PO,C_S,C_Z,
  75. C_FE,C_FG,C_FL,C_FGE,C_FLE,C_FNE
  76. );
  77. const
  78. cond2str:array[TAsmCond] of string[3]=('',
  79. 'gu','cc','cs','leu','cs','e','g','ge','l','le','leu','cs',
  80. 'cc','gu','cc','ne','le','l','ge','g','vc','XX',
  81. 'pos','ne','vs','XX','XX','XX','vs','e',
  82. 'e','g','l','ge','le','ne'
  83. );
  84. const
  85. CondAsmOps=2;
  86. CondAsmOp:array[0..CondAsmOps-1] of TAsmOp=(
  87. A_Bxx,A_FBxx
  88. );
  89. CondAsmOpStr:array[0..CondAsmOps-1] of string[7]=(
  90. 'B','FB'
  91. );
  92. {*****************************************************************************
  93. Flags
  94. *****************************************************************************}
  95. type
  96. TResFlags=(
  97. { Integer results }
  98. F_E, {Equal}
  99. F_NE, {Not Equal}
  100. F_G, {Greater}
  101. F_L, {Less}
  102. F_GE, {Greater or Equal}
  103. F_LE, {Less or Equal}
  104. F_A, {Above}
  105. F_AE, {Above or Equal}
  106. F_B, {Below}
  107. F_BE, {Below or Equal}
  108. F_C, {Carry}
  109. F_NC, {Not Carry}
  110. { Floating point results }
  111. F_FE, {Equal}
  112. F_FNE, {Not Equal}
  113. F_FG, {Greater}
  114. F_FL, {Less}
  115. F_FGE, {Greater or Equal}
  116. F_FLE {Less or Equal}
  117. );
  118. {*****************************************************************************
  119. Operand Sizes
  120. *****************************************************************************}
  121. {*****************************************************************************
  122. Constants
  123. *****************************************************************************}
  124. const
  125. max_operands = 3;
  126. {# Constant defining possibly all registers which might require saving }
  127. ALL_OTHERREGISTERS = [];
  128. general_superregisters = [RS_O0..RS_I7];
  129. {# Table of registers which can be allocated by the code generator
  130. internally, when generating the code.
  131. }
  132. { legend: }
  133. { xxxregs = set of all possibly used registers of that type in the code }
  134. { generator }
  135. { usableregsxxx = set of all 32bit components of registers that can be }
  136. { possible allocated to a regvar or using getregisterxxx (this }
  137. { excludes registers which can be only used for parameter }
  138. { passing on ABI's that define this) }
  139. { c_countusableregsxxx = amount of registers in the usableregsxxx set }
  140. maxintregs = 8;
  141. { to determine how many registers to use for regvars }
  142. maxintscratchregs = 3;
  143. usableregsint = [RS_L0..RS_L7];
  144. c_countusableregsint = 8;
  145. maxfpuregs = 8;
  146. usableregsfpu=[RS_F0..RS_F31];
  147. c_countusableregsfpu=32;
  148. mmregs = [];
  149. usableregsmm = [];
  150. c_countusableregsmm = 0;
  151. { no distinction on this platform }
  152. maxaddrregs = 0;
  153. addrregs = [];
  154. usableregsaddr = [];
  155. c_countusableregsaddr = 0;
  156. { TODO: firstsaveintreg shall be RS_NO}
  157. firstsaveintreg = RS_L0; { Temporary, having RS_NO is broken }
  158. lastsaveintreg = RS_L0; { L0..L7 are already saved, I0..O7 are parameter }
  159. firstsavefpureg = RS_F2; { F0..F1 is used for return value }
  160. lastsavefpureg = RS_F31;
  161. firstsavemmreg = RS_INVALID;
  162. lastsavemmreg = RS_INVALID;
  163. maxvarregs = 8;
  164. varregs : Array [1..maxvarregs] of Tsuperregister =
  165. (RS_L0,RS_L1,RS_L2,RS_L3,RS_L4,RS_L5,RS_L6,RS_L7);
  166. maxfpuvarregs = 1;
  167. fpuvarregs : Array [1..maxfpuvarregs] of TsuperRegister =
  168. (RS_F2);
  169. {
  170. max_param_regs_int = 6;
  171. param_regs_int: Array[1..max_param_regs_int] of TCpuRegister =
  172. (R_3,R_4,R_5,R_6,R_7,R_8,R_9,R_10);
  173. max_param_regs_fpu = 13;
  174. param_regs_fpu: Array[1..max_param_regs_fpu] of TCpuRegister =
  175. (R_F1,R_F2,R_F3,R_F4,R_F5,R_F6,R_F7,R_F8,R_F9,R_F10,R_F11,R_F12,R_F13);
  176. max_param_regs_mm = 13;
  177. param_regs_mm: Array[1..max_param_regs_mm] of TCpuRegister =
  178. (R_M1,R_M2,R_M3,R_M4,R_M5,R_M6,R_M7,R_M8,R_M9,R_M10,R_M11,R_M12,R_M13);
  179. }
  180. {*****************************************************************************
  181. Default generic sizes
  182. *****************************************************************************}
  183. {# Defines the default address size for a processor, }
  184. OS_ADDR = OS_32;
  185. {# the natural int size for a processor, }
  186. OS_INT = OS_32;
  187. OS_SINT = OS_S32;
  188. {# the maximum float size for a processor, }
  189. OS_FLOAT = OS_F64;
  190. {# the size of a vector register for a processor }
  191. OS_VECTOR = OS_M64;
  192. {*****************************************************************************
  193. Generic Register names
  194. *****************************************************************************}
  195. {# Stack pointer register }
  196. NR_STACK_POINTER_REG = NR_O6;
  197. RS_STACK_POINTER_REG = RS_O6;
  198. {# Frame pointer register }
  199. NR_FRAME_POINTER_REG = NR_I6;
  200. RS_FRAME_POINTER_REG = RS_I6;
  201. {# Register for addressing absolute data in a position independant way,
  202. such as in PIC code. The exact meaning is ABI specific. For
  203. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  204. Taken from GCC rs6000.h
  205. }
  206. { TODO: As indicated in rs6000.h, but can't find it anywhere else!}
  207. {PIC_OFFSET_REG = R_30;}
  208. { Return address for DWARF }
  209. NR_RETURN_ADDRESS_REG = NR_I7;
  210. { the return_result_reg, is used inside the called function to store its return
  211. value when that is a scalar value otherwise a pointer to the address of the
  212. result is placed inside it }
  213. { Results are returned in this register (32-bit values) }
  214. NR_FUNCTION_RETURN_REG = NR_I0;
  215. RS_FUNCTION_RETURN_REG = RS_I0;
  216. { Low part of 64bit return value }
  217. NR_FUNCTION_RETURN64_LOW_REG = NR_I1;
  218. RS_FUNCTION_RETURN64_LOW_REG = RS_I1;
  219. { High part of 64bit return value }
  220. NR_FUNCTION_RETURN64_HIGH_REG = NR_I0;
  221. RS_FUNCTION_RETURN64_HIGH_REG = RS_I0;
  222. { The value returned from a function is available in this register }
  223. NR_FUNCTION_RESULT_REG = NR_O0;
  224. RS_FUNCTION_RESULT_REG = RS_O0;
  225. { The lowh part of 64bit value returned from a function }
  226. NR_FUNCTION_RESULT64_LOW_REG = NR_O1;
  227. RS_FUNCTION_RESULT64_LOW_REG = RS_O1;
  228. { The high part of 64bit value returned from a function }
  229. NR_FUNCTION_RESULT64_HIGH_REG = NR_O0;
  230. RS_FUNCTION_RESULT64_HIGH_REG = RS_O0;
  231. NR_FPU_RESULT_REG = NR_F0;
  232. NR_MM_RESULT_REG = NR_NO;
  233. PARENT_FRAMEPOINTER_OFFSET = 68; { o0 }
  234. {*****************************************************************************
  235. GCC /ABI linking information
  236. *****************************************************************************}
  237. {# Registers which must be saved when calling a routine declared as
  238. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  239. saved should be the ones as defined in the target ABI and / or GCC.
  240. This value can be deduced from CALLED_USED_REGISTERS array in the
  241. GCC source.
  242. }
  243. saved_standard_registers : array[0..0] of tsuperregister = (RS_NO);
  244. { this is only for the generic code which is not used for this architecture }
  245. saved_mm_registers : array[0..0] of tsuperregister = (RS_NO);
  246. {# Required parameter alignment when calling a routine declared as
  247. stdcall and cdecl. The alignment value should be the one defined
  248. by GCC or the target ABI.
  249. The value of this constant is equal to the constant
  250. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  251. }
  252. std_param_align = 4; { for 32-bit version only }
  253. {*****************************************************************************
  254. CPU Dependent Constants
  255. *****************************************************************************}
  256. const
  257. simm13lo=-4096;
  258. simm13hi=4095;
  259. {*****************************************************************************
  260. Helpers
  261. *****************************************************************************}
  262. function is_calljmp(o:tasmop):boolean;
  263. procedure inverse_flags(var f: TResFlags);
  264. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  265. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  266. function flags_to_cond(const f: TResFlags) : TAsmCond;
  267. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  268. function reg_cgsize(const reg: tregister): tcgsize;
  269. function std_regname(r:Tregister):string;
  270. function std_regnum_search(const s:string):Tregister;
  271. function findreg_by_number(r:Tregister):tregisterindex;
  272. function dwarf_reg(r:tregister):shortint;
  273. implementation
  274. uses
  275. rgBase,verbose;
  276. const
  277. std_regname_table : TRegNameTAble = (
  278. {$i rspstd.inc}
  279. );
  280. regnumber_index : TRegisterIndexTable = (
  281. {$i rsprni.inc}
  282. );
  283. std_regname_index : TRegisterIndexTable = (
  284. {$i rspsri.inc}
  285. );
  286. {*****************************************************************************
  287. Helpers
  288. *****************************************************************************}
  289. function is_calljmp(o:tasmop):boolean;
  290. const
  291. CallJmpOp=[A_JMPL..A_CBccc];
  292. begin
  293. is_calljmp:=(o in CallJmpOp);
  294. end;
  295. procedure inverse_flags(var f: TResFlags);
  296. const
  297. inv_flags: array[TResFlags] of TResFlags =
  298. (F_NE,F_E,F_LE,F_GE,F_L,F_G,F_BE,F_B,F_AE,F_A,F_NC,F_C,
  299. F_FNE,F_FE,F_FLE,F_FGE,F_FL,F_FG);
  300. begin
  301. f:=inv_flags[f];
  302. end;
  303. function flags_to_cond(const f:TResFlags):TAsmCond;
  304. const
  305. flags_2_cond:array[TResFlags] of TAsmCond=
  306. (C_E,C_NE,C_G,C_L,C_GE,C_LE,C_A,C_AE,C_B,C_BE,C_C,C_NC,
  307. C_FE,C_FNE,C_FG,C_FL,C_FGE,C_FLE);
  308. begin
  309. result:=flags_2_cond[f];
  310. end;
  311. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  312. begin
  313. case regtype of
  314. R_FPUREGISTER:
  315. case s of
  316. OS_F32:
  317. cgsize2subreg:=R_SUBFS;
  318. OS_F64:
  319. cgsize2subreg:=R_SUBFD;
  320. OS_F128:
  321. cgsize2subreg:=R_SUBFQ;
  322. else
  323. internalerror(2009071903);
  324. end;
  325. else
  326. begin
  327. if s in [OS_64,OS_S64] then
  328. cgsize2subreg:=R_SUBQ
  329. else
  330. cgsize2subreg:=R_SUBWHOLE;
  331. end;
  332. end;
  333. end;
  334. function reg_cgsize(const reg: tregister): tcgsize;
  335. begin
  336. case getregtype(reg) of
  337. R_INTREGISTER :
  338. result:=OS_32;
  339. R_FPUREGISTER :
  340. begin
  341. if getsubreg(reg)=R_SUBFD then
  342. result:=OS_F64
  343. else
  344. result:=OS_F32;
  345. end;
  346. else
  347. internalerror(200303181);
  348. end;
  349. end;
  350. function findreg_by_number(r:Tregister):tregisterindex;
  351. begin
  352. result:=findreg_by_number_table(r,regnumber_index);
  353. end;
  354. function std_regname(r:Tregister):string;
  355. var
  356. p : tregisterindex;
  357. begin
  358. { For double floats show a pair like %f0:%f1 }
  359. if (getsubreg(r)=R_SUBFD) and
  360. (getsupreg(r)<first_fpu_imreg) then
  361. begin
  362. setsubreg(r,R_SUBFS);
  363. p:=findreg_by_number(r);
  364. if p<>0 then
  365. result:=std_regname_table[p]
  366. else
  367. result:=generic_regname(r);
  368. setsupreg(r,getsupreg(r)+1);
  369. p:=findreg_by_number(r);
  370. if p<>0 then
  371. result:=result+':'+std_regname_table[p]
  372. else
  373. result:=result+':'+generic_regname(r);
  374. end
  375. else
  376. begin
  377. p:=findreg_by_number(r);
  378. if p<>0 then
  379. result:=std_regname_table[p]
  380. else
  381. result:=generic_regname(r);
  382. end;
  383. end;
  384. function std_regnum_search(const s:string):Tregister;
  385. begin
  386. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  387. end;
  388. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  389. const
  390. inverse: array[TAsmCond] of TAsmCond=(C_None,
  391. C_NA,C_NAE,C_NB,C_NBE,C_NC,C_NE,C_NG,C_NGE,C_NL,C_NLE,C_A,C_AE,
  392. C_B,C_BE,C_C,C_E,C_G,C_GE,C_L,C_LE,C_O,C_P,
  393. C_S,C_Z,C_NO,C_NP,C_NP,C_P,C_NS,C_NZ,
  394. C_FNE,C_FLE,C_FGE,C_FL,C_FG,C_FE
  395. );
  396. begin
  397. result := inverse[c];
  398. end;
  399. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  400. begin
  401. result := c1 = c2;
  402. end;
  403. function dwarf_reg(r:tregister):shortint;
  404. begin
  405. result:=regdwarf_table[findreg_by_number(r)];
  406. if result=-1 then
  407. internalerror(200603251);
  408. end;
  409. end.