cpubase.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. {$ModeSwitch advancedrecords}
  20. interface
  21. uses
  22. globtype,strings,cutils,cclasses,aasmbase,cpuinfo,cgbase;
  23. {*****************************************************************************
  24. Assembler Opcodes
  25. *****************************************************************************}
  26. type
  27. { TODO: CPU32 opcodes do not fully include the Ultra SPRAC instruction set.}
  28. { don't change the order of these opcodes! }
  29. TAsmOp=({$i opcode.inc});
  30. {# This should define the array of instructions as string }
  31. op2strtable=array[tasmop] of string[11];
  32. Const
  33. {# First value of opcode enumeration }
  34. firstop = low(tasmop);
  35. {# Last value of opcode enumeration }
  36. lastop = high(tasmop);
  37. std_op2str:op2strtable=({$i strinst.inc});
  38. {*****************************************************************************
  39. Registers
  40. *****************************************************************************}
  41. {$ifdef SPARC}
  42. type
  43. { Number of registers used for indexing in tables }
  44. tregisterindex=0..{$i rspnor.inc}-1;
  45. totherregisterset = set of tregisterindex;
  46. const
  47. { Available Superregisters }
  48. {$i rspsup.inc}
  49. { No Subregisters }
  50. R_SUBWHOLE = R_SUBNONE;
  51. { Available Registers }
  52. {$i rspcon.inc}
  53. first_int_imreg = $20;
  54. first_fpu_imreg = $20;
  55. { MM Super register first and last }
  56. first_mm_supreg = 0;
  57. first_mm_imreg = 1;
  58. { TODO: Calculate bsstart}
  59. regnumber_count_bsstart = 128;
  60. regnumber_table : array[tregisterindex] of tregister = (
  61. {$i rspnum.inc}
  62. );
  63. regstabs_table : array[tregisterindex] of ShortInt = (
  64. {$i rspstab.inc}
  65. );
  66. regdwarf_table : array[tregisterindex] of ShortInt = (
  67. {$i rspdwrf.inc}
  68. );
  69. { Aliases for full register LoadStore instructions }
  70. A_ST_R = A_ST;
  71. A_LD_R = A_LD;
  72. {$endif SPARC}
  73. {$ifdef SPARC64}
  74. type
  75. { Number of registers used for indexing in tables }
  76. tregisterindex=0..{$i rsp64nor.inc}-1;
  77. totherregisterset = set of tregisterindex;
  78. const
  79. { Available Superregisters }
  80. {$i rsp64sup.inc}
  81. { No Subregisters }
  82. R_SUBWHOLE = R_SUBNONE;
  83. { Available Registers }
  84. {$i rsp64con.inc}
  85. first_int_imreg = $20;
  86. first_fpu_imreg = $20;
  87. { MM Super register first and last }
  88. first_mm_supreg = 0;
  89. first_mm_imreg = 1;
  90. { TODO: Calculate bsstart}
  91. regnumber_count_bsstart = 128;
  92. regnumber_table : array[tregisterindex] of tregister = (
  93. {$i rsp64num.inc}
  94. );
  95. regstabs_table : array[tregisterindex] of ShortInt = (
  96. {$i rsp64stab.inc}
  97. );
  98. regdwarf_table : array[tregisterindex] of ShortInt = (
  99. {$i rsp64dwrf.inc}
  100. );
  101. { Aliases for full register LoadStore instructions }
  102. A_ST_R = A_STX;
  103. A_LD_R = A_LDX;
  104. {$endif SPARC64}
  105. {*****************************************************************************
  106. Conditions
  107. *****************************************************************************}
  108. type
  109. TAsmCond=(C_None,
  110. C_A,C_AE,C_B,C_BE,
  111. C_G,C_GE,C_L,C_LE,
  112. C_E,C_NE,
  113. C_POS,C_NEG,C_VC,C_VS,
  114. C_FE,C_FG,C_FL,C_FGE,C_FLE,C_FNE,
  115. C_FU,C_FUG,C_FUL,C_FUGE,C_FULE,C_FO,C_FUE,C_FLG
  116. );
  117. const
  118. firstIntCond=C_A;
  119. lastIntCond=C_VS;
  120. firstFloatCond=C_FE;
  121. lastFloatCond=C_FNE;
  122. floatAsmConds=[C_FE..C_FLG];
  123. cond2str:array[TAsmCond] of string[3]=('',
  124. 'gu','cc','cs','leu',
  125. 'g','ge','l','le',
  126. 'e','ne',
  127. 'pos','neg','vc','vs',
  128. 'e','g','l','ge','le','ne',
  129. 'u','ug','ul','uge','ule','o','ue','lg'
  130. );
  131. {*****************************************************************************
  132. Flags
  133. *****************************************************************************}
  134. type
  135. TSparcFlags = (
  136. { Integer results }
  137. F_E, {Equal}
  138. F_NE, {Not Equal}
  139. F_G, {Greater}
  140. F_L, {Less}
  141. F_GE, {Greater or Equal}
  142. F_LE, {Less or Equal}
  143. F_A, {Above}
  144. F_AE, {Above or Equal, synonym: Carry Clear}
  145. F_B, {Below, synonym: Carry Set}
  146. F_BE, {Below or Equal}
  147. { Floating point results }
  148. F_FE, {Equal}
  149. F_FNE, {Not Equal}
  150. F_FG, {Greater}
  151. F_FL, {Less}
  152. F_FGE, {Greater or Equal}
  153. F_FLE {Less or Equal}
  154. );
  155. TResFlags = record
  156. { either icc or xcc (64 bit }
  157. FlagReg : TRegister;
  158. Flags : TSparcFlags;
  159. procedure Init(r : TRegister;f : TSparcFlags);
  160. end;
  161. {*****************************************************************************
  162. Operand Sizes
  163. *****************************************************************************}
  164. {*****************************************************************************
  165. Constants
  166. *****************************************************************************}
  167. const
  168. max_operands = 3;
  169. maxintregs = 8;
  170. maxfpuregs = 8;
  171. maxaddrregs = 0;
  172. maxvarregs = 8;
  173. varregs : Array [1..maxvarregs] of Tsuperregister =
  174. (RS_L0,RS_L1,RS_L2,RS_L3,RS_L4,RS_L5,RS_L6,RS_L7);
  175. maxfpuvarregs = 1;
  176. fpuvarregs : Array [1..maxfpuvarregs] of TsuperRegister =
  177. (RS_F2);
  178. {*****************************************************************************
  179. Default generic sizes
  180. *****************************************************************************}
  181. {$ifdef SPARC64}
  182. {# Defines the default address size for a processor, }
  183. OS_ADDR = OS_64;
  184. {# the natural int size for a processor,
  185. has to match osuinttype/ossinttype as initialized in psystem }
  186. OS_INT = OS_64;
  187. OS_SINT = OS_S64;
  188. {$else SPARC64}
  189. {# Defines the default address size for a processor, }
  190. OS_ADDR = OS_32;
  191. {# the natural int size for a processor,
  192. has to match osuinttype/ossinttype as initialized in psystem }
  193. OS_INT = OS_32;
  194. OS_SINT = OS_S32;
  195. {$endif SPARC64}
  196. {# the maximum float size for a processor, }
  197. OS_FLOAT = OS_F64;
  198. {# the size of a vector register for a processor }
  199. OS_VECTOR = OS_M64;
  200. {*****************************************************************************
  201. Generic Register names
  202. *****************************************************************************}
  203. {# Stack pointer register }
  204. NR_STACK_POINTER_REG = NR_O6;
  205. RS_STACK_POINTER_REG = RS_O6;
  206. {# Frame pointer register }
  207. NR_FRAME_POINTER_REG = NR_I6;
  208. RS_FRAME_POINTER_REG = RS_I6;
  209. {# Register for addressing absolute data in a position independant way,
  210. such as in PIC code. The exact meaning is ABI specific. For
  211. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  212. Taken from GCC rs6000.h
  213. }
  214. { TODO: As indicated in rs6000.h, but can't find it anywhere else!}
  215. {PIC_OFFSET_REG = R_30;}
  216. { Return address for DWARF }
  217. NR_RETURN_ADDRESS_REG = NR_I7;
  218. { the return_result_reg, is used inside the called function to store its return
  219. value when that is a scalar value otherwise a pointer to the address of the
  220. result is placed inside it }
  221. { Results are returned in this register (32-bit values) }
  222. NR_FUNCTION_RETURN_REG = NR_I0;
  223. RS_FUNCTION_RETURN_REG = RS_I0;
  224. { Low part of 64bit return value }
  225. NR_FUNCTION_RETURN64_LOW_REG = NR_I1;
  226. RS_FUNCTION_RETURN64_LOW_REG = RS_I1;
  227. { High part of 64bit return value }
  228. NR_FUNCTION_RETURN64_HIGH_REG = NR_I0;
  229. RS_FUNCTION_RETURN64_HIGH_REG = RS_I0;
  230. { The value returned from a function is available in this register }
  231. NR_FUNCTION_RESULT_REG = NR_O0;
  232. RS_FUNCTION_RESULT_REG = RS_O0;
  233. { The lowh part of 64bit value returned from a function }
  234. NR_FUNCTION_RESULT64_LOW_REG = NR_O1;
  235. RS_FUNCTION_RESULT64_LOW_REG = RS_O1;
  236. { The high part of 64bit value returned from a function }
  237. NR_FUNCTION_RESULT64_HIGH_REG = NR_O0;
  238. RS_FUNCTION_RESULT64_HIGH_REG = RS_O0;
  239. NR_FPU_RESULT_REG = NR_F0;
  240. NR_MM_RESULT_REG = NR_NO;
  241. PARENT_FRAMEPOINTER_OFFSET = 68; { o0 }
  242. NR_DEFAULTFLAGS = NR_PSR;
  243. RS_DEFAULTFLAGS = RS_PSR;
  244. {*****************************************************************************
  245. GCC /ABI linking information
  246. *****************************************************************************}
  247. {# Registers which must be saved when calling a routine declared as
  248. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  249. saved should be the ones as defined in the target ABI and / or GCC.
  250. This value can be deduced from CALLED_USED_REGISTERS array in the
  251. GCC source.
  252. }
  253. saved_standard_registers : array[0..0] of tsuperregister = (RS_INVALID);
  254. { this is only for the generic code which is not used for this architecture }
  255. saved_address_registers : array[0..0] of tsuperregister = (RS_INVALID);
  256. saved_mm_registers : array[0..0] of tsuperregister = (RS_INVALID);
  257. {# Required parameter alignment when calling a routine declared as
  258. stdcall and cdecl. The alignment value should be the one defined
  259. by GCC or the target ABI.
  260. The value of this constant is equal to the constant
  261. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  262. }
  263. std_param_align = sizeof(AWord);
  264. {$ifdef SPARC64}
  265. STACK_BIAS = 2047;
  266. {$endif SPARC64}
  267. {*****************************************************************************
  268. CPU Dependent Constants
  269. *****************************************************************************}
  270. const
  271. simm13lo=-4096;
  272. simm13hi=4095;
  273. {*****************************************************************************
  274. Helpers
  275. *****************************************************************************}
  276. function is_calljmp(o:tasmop):boolean;
  277. procedure inverse_flags(var f: TResFlags);
  278. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  279. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  280. function flags_to_cond(const f: TResFlags) : TAsmCond;
  281. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  282. function reg_cgsize(const reg: tregister): tcgsize;
  283. function std_regname(r:Tregister):string;
  284. function std_regnum_search(const s:string):Tregister;
  285. function findreg_by_number(r:Tregister):tregisterindex;
  286. function dwarf_reg(r:tregister):shortint;
  287. implementation
  288. uses
  289. rgBase,verbose;
  290. {$ifdef SPARC}
  291. const
  292. std_regname_table : TRegNameTAble = (
  293. {$i rspstd.inc}
  294. );
  295. regnumber_index : TRegisterIndexTable = (
  296. {$i rsprni.inc}
  297. );
  298. std_regname_index : TRegisterIndexTable = (
  299. {$i rspsri.inc}
  300. );
  301. {$endif SPARC}
  302. {$ifdef SPARC64}
  303. const
  304. std_regname_table : TRegNameTAble = (
  305. {$i rsp64std.inc}
  306. );
  307. regnumber_index : TRegisterIndexTable = (
  308. {$i rsp64rni.inc}
  309. );
  310. std_regname_index : TRegisterIndexTable = (
  311. {$i rsp64sri.inc}
  312. );
  313. {$endif SPARC64}
  314. {*****************************************************************************
  315. Helpers
  316. *****************************************************************************}
  317. function is_calljmp(o:tasmop):boolean;
  318. const
  319. CallJmpOp=[A_JMPL..A_CBccc];
  320. begin
  321. is_calljmp:=(o in CallJmpOp);
  322. end;
  323. procedure inverse_flags(var f: TResFlags);
  324. const
  325. inv_flags: array[TSparcFlags] of TSparcFlags =
  326. (F_NE,F_E,F_LE,F_GE,F_L,F_G,F_BE,F_B,F_AE,F_A,
  327. F_FNE,F_FE,F_FLE,F_FGE,F_FL,F_FG);
  328. begin
  329. f.Flags:=inv_flags[f.Flags];
  330. end;
  331. function flags_to_cond(const f:TResFlags):TAsmCond;
  332. const
  333. flags_2_cond:array[TSparcFlags] of TAsmCond=
  334. (C_E,C_NE,C_G,C_L,C_GE,C_LE,C_A,C_AE,C_B,C_BE,
  335. C_FE,C_FNE,C_FG,C_FL,C_FGE,C_FLE);
  336. begin
  337. result:=flags_2_cond[f.Flags];
  338. end;
  339. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  340. begin
  341. case regtype of
  342. R_FPUREGISTER:
  343. case s of
  344. OS_F32:
  345. cgsize2subreg:=R_SUBFS;
  346. OS_F64:
  347. cgsize2subreg:=R_SUBFD;
  348. OS_F128:
  349. cgsize2subreg:=R_SUBFQ;
  350. else
  351. internalerror(2009071903);
  352. end;
  353. else
  354. begin
  355. {$ifdef SPARC32}
  356. if s in [OS_64,OS_S64] then
  357. cgsize2subreg:=R_SUBQ
  358. else
  359. {$endif SPARC32}
  360. cgsize2subreg:=R_SUBWHOLE;
  361. end;
  362. end;
  363. end;
  364. function reg_cgsize(const reg: tregister): tcgsize;
  365. begin
  366. case getregtype(reg) of
  367. R_INTREGISTER :
  368. result:=OS_INT;
  369. R_FPUREGISTER :
  370. begin
  371. if getsubreg(reg)=R_SUBFD then
  372. result:=OS_F64
  373. else
  374. result:=OS_F32;
  375. end;
  376. else
  377. internalerror(200303181);
  378. end;
  379. end;
  380. function findreg_by_number(r:Tregister):tregisterindex;
  381. begin
  382. result:=findreg_by_number_table(r,regnumber_index);
  383. end;
  384. function std_regname(r:Tregister):string;
  385. var
  386. p : tregisterindex;
  387. begin
  388. { For double floats show a pair like %f0:%f1 }
  389. if (getsubreg(r)=R_SUBFD) and
  390. (getsupreg(r)<first_fpu_imreg) then
  391. begin
  392. setsubreg(r,R_SUBFS);
  393. p:=findreg_by_number(r);
  394. if p<>0 then
  395. result:=std_regname_table[p]
  396. else
  397. result:=generic_regname(r);
  398. setsupreg(r,getsupreg(r)+1);
  399. p:=findreg_by_number(r);
  400. if p<>0 then
  401. result:=result+':'+std_regname_table[p]
  402. else
  403. result:=result+':'+generic_regname(r);
  404. end
  405. else
  406. begin
  407. p:=findreg_by_number(r);
  408. if p<>0 then
  409. result:=std_regname_table[p]
  410. else
  411. result:=generic_regname(r);
  412. end;
  413. end;
  414. function std_regnum_search(const s:string):Tregister;
  415. begin
  416. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  417. end;
  418. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  419. const
  420. inverse: array[TAsmCond] of TAsmCond=(C_None,
  421. C_BE,C_B,C_AE,C_A,
  422. C_LE,C_L,C_GE,C_G,
  423. C_NE,C_E,
  424. C_NEG,C_POS,C_VS,C_VC,
  425. C_FNE,C_FULE,C_FUGE,C_FUL,C_FUG,C_FE,
  426. C_FO,C_FLE,C_FGE,C_FL,C_FG,C_FU,C_FLG,C_FUE
  427. );
  428. begin
  429. result := inverse[c];
  430. end;
  431. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  432. begin
  433. result := c1 = c2;
  434. end;
  435. function dwarf_reg(r:tregister):shortint;
  436. begin
  437. result:=regdwarf_table[findreg_by_number(r)];
  438. if result=-1 then
  439. internalerror(200603251);
  440. end;
  441. procedure TResFlags.Init(r : TRegister; f : TSparcFlags);
  442. begin
  443. FlagReg:=r;
  444. Flags:=f;
  445. end;
  446. end.