cpubase.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Contains the base types for the SPARC
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. { This Unit contains the base types for the PowerPC
  19. }
  20. unit cpubase;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. globtype,strings,cutils,cclasses,aasmbase,cpuinfo,cgbase;
  25. {*****************************************************************************
  26. Assembler Opcodes
  27. *****************************************************************************}
  28. type
  29. {$WARNING CPU32 opcodes do not fully include the Ultra SPRAC instruction set.}
  30. { don't change the order of these opcodes! }
  31. TAsmOp=({$i opcode.inc});
  32. {# This should define the array of instructions as string }
  33. op2strtable=array[tasmop] of string[11];
  34. Const
  35. {# First value of opcode enumeration }
  36. firstop = low(tasmop);
  37. {# Last value of opcode enumeration }
  38. lastop = high(tasmop);
  39. std_op2str:op2strtable=({$i strinst.inc});
  40. {*****************************************************************************
  41. Registers
  42. *****************************************************************************}
  43. type
  44. { Number of registers used for indexing in tables }
  45. tregisterindex=0..{$i rspnor.inc}-1;
  46. totherregisterset = set of tregisterindex;
  47. const
  48. { Available Superregisters }
  49. {$i rspsup.inc}
  50. { No Subregisters }
  51. R_SUBWHOLE=R_SUBNONE;
  52. { Available Registers }
  53. {$i rspcon.inc}
  54. first_int_imreg = $20;
  55. first_fpu_imreg = $20;
  56. {$warning 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. inverse_cond:array[TAsmCond] of TAsmCond=(C_None,
  85. C_NA,C_NAE,C_NB,C_NBE,C_NC,C_NE,C_NG,C_NGE,C_NL,C_NLE,C_A,C_AE,
  86. C_B,C_BE,C_C,C_E,C_G,C_GE,C_L,C_LE,C_O,C_P,
  87. C_S,C_Z,C_NO,C_NP,C_NP,C_P,C_NS,C_NZ,
  88. C_FNE,C_FLE,C_FGE,C_FL,C_FG,C_FE
  89. );
  90. const
  91. CondAsmOps=2;
  92. CondAsmOp:array[0..CondAsmOps-1] of TAsmOp=(
  93. A_Bxx,A_FBxx
  94. );
  95. CondAsmOpStr:array[0..CondAsmOps-1] of string[7]=(
  96. 'B','FB'
  97. );
  98. {*****************************************************************************
  99. Flags
  100. *****************************************************************************}
  101. type
  102. TResFlags=(
  103. { Integer results }
  104. F_E, {Equal}
  105. F_NE, {Not Equal}
  106. F_G, {Greater}
  107. F_L, {Less}
  108. F_GE, {Greater or Equal}
  109. F_LE, {Less or Equal}
  110. F_A, {Above}
  111. F_AE, {Above or Equal}
  112. F_B, {Below}
  113. F_BE, {Below or Equal}
  114. F_C, {Carry}
  115. F_NC, {Not Carry}
  116. { Floating point results }
  117. F_FE, {Equal}
  118. F_FNE, {Not Equal}
  119. F_FG, {Greater}
  120. F_FL, {Less}
  121. F_FGE, {Greater or Equal}
  122. F_FLE {Less or Equal}
  123. );
  124. {*****************************************************************************
  125. Reference
  126. *****************************************************************************}
  127. type
  128. TRefOptions=(ref_none,ref_parafixup,ref_localfixup,ref_selffixup);
  129. { reference record }
  130. preference = ^treference;
  131. treference = packed record
  132. { base register, R_NO if none }
  133. base,
  134. { index register, R_NO if none }
  135. index : tregister;
  136. { offset, 0 if none }
  137. offset : longint;
  138. { symbol this reference refers to, nil if none }
  139. symbol : tasmsymbol;
  140. { symbol the symbol of this reference is relative to, nil if none }
  141. relsymbol : tasmsymbol;
  142. { reference type addr or symbol itself }
  143. refaddr : trefaddr;
  144. { used in conjunction with the previous field }
  145. options : trefoptions;
  146. { alignment this reference is guaranteed to have }
  147. alignment : byte;
  148. end;
  149. { reference record }
  150. pparareference = ^tparareference;
  151. tparareference = packed record
  152. index : tregister;
  153. offset : longint;
  154. end;
  155. {*****************************************************************************
  156. Operand Sizes
  157. *****************************************************************************}
  158. {$ifdef dummy}
  159. {*****************************************************************************
  160. Argument Classification
  161. *****************************************************************************}
  162. type
  163. TArgClass = (
  164. { the following classes should be defined by all processor implemnations }
  165. AC_NOCLASS,
  166. AC_MEMORY,
  167. AC_INTEGER,
  168. AC_FPU,
  169. { the following argument classes are i386 specific }
  170. AC_FPUUP,
  171. AC_SSE,
  172. AC_SSEUP);
  173. {$endif dummy}
  174. {*****************************************************************************
  175. Generic Location
  176. *****************************************************************************}
  177. type
  178. { tparamlocation describes where a parameter for a procedure is stored.
  179. References are given from the caller's point of view. The usual
  180. TLocation isn't used, because contains a lot of unnessary fields.
  181. }
  182. tparalocation = packed record
  183. Size : TCGSize;
  184. { The location type where the parameter is passed, usually
  185. LOC_REFERENCE,LOC_REGISTER or LOC_FPUREGISTER
  186. }
  187. Loc : TCGLoc;
  188. LocHigh : TCGLoc;
  189. {Word alignment on stack 4 --> 32 bit}
  190. Alignment:Byte;
  191. case TCGLoc of
  192. LOC_REFERENCE : (reference : tparareference; low_in_reg: boolean; lowreg : tregister);
  193. LOC_FPUREGISTER, LOC_CFPUREGISTER, LOC_MMREGISTER, LOC_CMMREGISTER,
  194. LOC_REGISTER,LOC_CREGISTER : (
  195. case longint of
  196. 1 : (register,registerhigh : tregister);
  197. { overlay a registerlow }
  198. 2 : (registerlow : tregister);
  199. { overlay a 64 Bit register type }
  200. 3 : (reg64 : tregister64);
  201. 4 : (register64 : tregister64);
  202. );
  203. end;
  204. treglocation = packed record
  205. case longint of
  206. 1 : (register,registerhigh : tregister);
  207. { overlay a registerlow }
  208. 2 : (registerlow : tregister);
  209. { overlay a 64 Bit register type }
  210. 3 : (reg64 : tregister64);
  211. 4 : (register64 : tregister64);
  212. end;
  213. tlocation = packed record
  214. size : TCGSize;
  215. loc : tcgloc;
  216. case tcgloc of
  217. LOC_CREFERENCE,LOC_REFERENCE : (reference : treference);
  218. LOC_CONSTANT : (
  219. case longint of
  220. {$ifdef FPC_BIG_ENDIAN}
  221. 1 : (_valuedummy,value : Aint);
  222. {$else FPC_BIG_ENDIAN}
  223. 1 : (value : Aint);
  224. {$endif FPC_BIG_ENDIAN}
  225. 2 : (value64 : int64);
  226. );
  227. LOC_FPUREGISTER, LOC_CFPUREGISTER, LOC_MMREGISTER, LOC_CMMREGISTER,
  228. LOC_REGISTER,LOC_CREGISTER : (
  229. case longint of
  230. 1 : (registerlow,registerhigh : tregister);
  231. 2 : (register : tregister);
  232. { overlay a 64 Bit register type }
  233. 3 : (reg64 : tregister64);
  234. 4 : (register64 : tregister64);
  235. );
  236. LOC_FLAGS : (resflags : tresflags);
  237. end;
  238. {*****************************************************************************
  239. Constants
  240. *****************************************************************************}
  241. const
  242. max_operands = 3;
  243. {# Constant defining possibly all registers which might require saving }
  244. ALL_OTHERREGISTERS = [];
  245. general_superregisters = [RS_O0..RS_I7];
  246. {# Table of registers which can be allocated by the code generator
  247. internally, when generating the code.
  248. }
  249. { legend: }
  250. { xxxregs = set of all possibly used registers of that type in the code }
  251. { generator }
  252. { usableregsxxx = set of all 32bit components of registers that can be }
  253. { possible allocated to a regvar or using getregisterxxx (this }
  254. { excludes registers which can be only used for parameter }
  255. { passing on ABI's that define this) }
  256. { c_countusableregsxxx = amount of registers in the usableregsxxx set }
  257. maxintregs = 8;
  258. { to determine how many registers to use for regvars }
  259. maxintscratchregs = 3;
  260. usableregsint = [RS_L0..RS_L7];
  261. c_countusableregsint = 8;
  262. maxfpuregs = 8;
  263. usableregsfpu=[RS_F0..RS_F31];
  264. c_countusableregsfpu=32;
  265. mmregs = [];
  266. usableregsmm = [];
  267. c_countusableregsmm = 0;
  268. { no distinction on this platform }
  269. maxaddrregs = 0;
  270. addrregs = [];
  271. usableregsaddr = [];
  272. c_countusableregsaddr = 0;
  273. {$warning firstsaveintreg shall be RS_NO}
  274. firstsaveintreg = RS_L0; { Temporary, having RS_NO is broken }
  275. lastsaveintreg = RS_L0; { L0..L7 are already saved, I0..O7 are parameter }
  276. firstsavefpureg = RS_F2; { F0..F1 is used for return value }
  277. lastsavefpureg = RS_F31;
  278. firstsavemmreg = RS_INVALID;
  279. lastsavemmreg = RS_INVALID;
  280. maxvarregs = 8;
  281. varregs : Array [1..maxvarregs] of Tsuperregister =
  282. (RS_L0,RS_L1,RS_L2,RS_L3,RS_L4,RS_L5,RS_L6,RS_L7);
  283. maxfpuvarregs = 1;
  284. fpuvarregs : Array [1..maxfpuvarregs] of TsuperRegister =
  285. (RS_F2);
  286. {
  287. max_param_regs_int = 6;
  288. param_regs_int: Array[1..max_param_regs_int] of TCpuRegister =
  289. (R_3,R_4,R_5,R_6,R_7,R_8,R_9,R_10);
  290. max_param_regs_fpu = 13;
  291. param_regs_fpu: Array[1..max_param_regs_fpu] of TCpuRegister =
  292. (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);
  293. max_param_regs_mm = 13;
  294. param_regs_mm: Array[1..max_param_regs_mm] of TCpuRegister =
  295. (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);
  296. }
  297. {*****************************************************************************
  298. Default generic sizes
  299. *****************************************************************************}
  300. {# Defines the default address size for a processor, }
  301. OS_ADDR = OS_32;
  302. {# the natural int size for a processor, }
  303. OS_INT = OS_32;
  304. {# the maximum float size for a processor, }
  305. OS_FLOAT = OS_F64;
  306. {# the size of a vector register for a processor }
  307. OS_VECTOR = OS_M64;
  308. {*****************************************************************************
  309. Generic Register names
  310. *****************************************************************************}
  311. {# Stack pointer register }
  312. NR_STACK_POINTER_REG = NR_O6;
  313. RS_STACK_POINTER_REG = RS_O6;
  314. {# Frame pointer register }
  315. NR_FRAME_POINTER_REG = NR_I6;
  316. RS_FRAME_POINTER_REG = RS_I6;
  317. {# Register for addressing absolute data in a position independant way,
  318. such as in PIC code. The exact meaning is ABI specific. For
  319. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  320. Taken from GCC rs6000.h
  321. }
  322. {$warning As indicated in rs6000.h, but can't find it anywhere else!}
  323. {PIC_OFFSET_REG = R_30;}
  324. { Return address for DWARF }
  325. NR_RETURN_ADDRESS_REG = NR_I7;
  326. { the return_result_reg, is used inside the called function to store its return
  327. value when that is a scalar value otherwise a pointer to the address of the
  328. result is placed inside it }
  329. { Results are returned in this register (32-bit values) }
  330. NR_FUNCTION_RETURN_REG = NR_I0;
  331. RS_FUNCTION_RETURN_REG = RS_I0;
  332. { Low part of 64bit return value }
  333. NR_FUNCTION_RETURN64_LOW_REG = NR_I1;
  334. RS_FUNCTION_RETURN64_LOW_REG = RS_I1;
  335. { High part of 64bit return value }
  336. NR_FUNCTION_RETURN64_HIGH_REG = NR_I0;
  337. RS_FUNCTION_RETURN64_HIGH_REG = RS_I0;
  338. { The value returned from a function is available in this register }
  339. NR_FUNCTION_RESULT_REG = NR_O0;
  340. RS_FUNCTION_RESULT_REG = RS_O0;
  341. { The lowh part of 64bit value returned from a function }
  342. NR_FUNCTION_RESULT64_LOW_REG = NR_O1;
  343. RS_FUNCTION_RESULT64_LOW_REG = RS_O1;
  344. { The high part of 64bit value returned from a function }
  345. NR_FUNCTION_RESULT64_HIGH_REG = NR_O0;
  346. RS_FUNCTION_RESULT64_HIGH_REG = RS_O0;
  347. NR_FPU_RESULT_REG = NR_F0;
  348. NR_MM_RESULT_REG = NR_NO;
  349. PARENT_FRAMEPOINTER_OFFSET = 68; { o0 }
  350. {*****************************************************************************
  351. GCC /ABI linking information
  352. *****************************************************************************}
  353. {# Registers which must be saved when calling a routine declared as
  354. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  355. saved should be the ones as defined in the target ABI and / or GCC.
  356. This value can be deduced from CALLED_USED_REGISTERS array in the
  357. GCC source.
  358. }
  359. std_saved_registers = [];
  360. {# Required parameter alignment when calling a routine declared as
  361. stdcall and cdecl. The alignment value should be the one defined
  362. by GCC or the target ABI.
  363. The value of this constant is equal to the constant
  364. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  365. }
  366. std_param_align = 4; { for 32-bit version only }
  367. {*****************************************************************************
  368. CPU Dependent Constants
  369. *****************************************************************************}
  370. const
  371. simm13lo=-4096;
  372. simm13hi=4095;
  373. {*****************************************************************************
  374. Helpers
  375. *****************************************************************************}
  376. function is_calljmp(o:tasmop):boolean;
  377. procedure inverse_flags(var f: TResFlags);
  378. function flags_to_cond(const f: TResFlags) : TAsmCond;
  379. function cgsize2subreg(s:Tcgsize):Tsubregister;
  380. function reg_cgsize(const reg: tregister): tcgsize;
  381. function std_regname(r:Tregister):string;
  382. function std_regnum_search(const s:string):Tregister;
  383. function findreg_by_number(r:Tregister):tregisterindex;
  384. implementation
  385. uses
  386. rgBase,verbose;
  387. const
  388. std_regname_table : TRegNameTAble = (
  389. {$i rspstd.inc}
  390. );
  391. regnumber_index : TRegisterIndexTable = (
  392. {$i rsprni.inc}
  393. );
  394. std_regname_index : TRegisterIndexTable = (
  395. {$i rspsri.inc}
  396. );
  397. {*****************************************************************************
  398. Helpers
  399. *****************************************************************************}
  400. function is_calljmp(o:tasmop):boolean;
  401. const
  402. CallJmpOp=[A_JMPL..A_CBccc];
  403. begin
  404. is_calljmp:=(o in CallJmpOp);
  405. end;
  406. procedure inverse_flags(var f: TResFlags);
  407. const
  408. inv_flags: array[TResFlags] of TResFlags =
  409. (F_NE,F_E,F_LE,F_GE,F_L,F_G,F_BE,F_B,F_AE,F_A,F_NC,F_C,
  410. F_FNE,F_FE,F_FLE,F_FGE,F_FL,F_FG);
  411. begin
  412. f:=inv_flags[f];
  413. end;
  414. function flags_to_cond(const f:TResFlags):TAsmCond;
  415. const
  416. flags_2_cond:array[TResFlags] of TAsmCond=
  417. (C_E,C_NE,C_G,C_L,C_GE,C_LE,C_A,C_AE,C_B,C_BE,C_C,C_NC,
  418. C_FE,C_FNE,C_FG,C_FL,C_FGE,C_FLE);
  419. begin
  420. result:=flags_2_cond[f];
  421. end;
  422. function cgsize2subreg(s:Tcgsize):Tsubregister;
  423. begin
  424. cgsize2subreg:=R_SUBWHOLE;
  425. end;
  426. function reg_cgsize(const reg: tregister): tcgsize;
  427. begin
  428. case getregtype(reg) of
  429. R_INTREGISTER :
  430. result:=OS_32;
  431. R_FPUREGISTER :
  432. begin
  433. if getsubreg(reg)=R_SUBFD then
  434. result:=OS_F64
  435. else
  436. result:=OS_F32;
  437. end;
  438. else
  439. internalerror(200303181);
  440. end;
  441. end;
  442. function std_regname(r:Tregister):string;
  443. var
  444. p : tregisterindex;
  445. begin
  446. p:=findreg_by_number_table(r,regnumber_index);
  447. if p<>0 then
  448. result:=std_regname_table[p]
  449. else
  450. result:=generic_regname(r);
  451. end;
  452. function std_regnum_search(const s:string):Tregister;
  453. begin
  454. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  455. end;
  456. function findreg_by_number(r:Tregister):tregisterindex;
  457. begin
  458. result:=findreg_by_number_table(r,regnumber_index);
  459. end;
  460. end.
  461. {
  462. $Log$
  463. Revision 1.67 2004-06-20 08:55:32 florian
  464. * logs truncated
  465. Revision 1.66 2004/06/16 20:07:10 florian
  466. * dwarf branch merged
  467. Revision 1.65.2.5 2004/06/13 20:38:38 florian
  468. * fixed floating point register spilling on sparc
  469. Revision 1.65.2.4 2004/05/28 22:21:48 peter
  470. * fixed sparc compile
  471. Revision 1.65.2.3 2004/05/28 20:29:50 florian
  472. * fixed currency trouble on x86-64
  473. Revision 1.65.2.2 2004/05/13 20:58:47 florian
  474. * fixed register addressed jumps in interface wrappers
  475. }