cpubase.pas 17 KB

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