cpubase.pas 19 KB

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