cpubase.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. firstIntCond=C_A;
  79. lastIntCond=C_Z;
  80. firstFloatCond=C_FE;
  81. lastFloatCond=C_FNE;
  82. floatAsmConds=[C_FE..C_FNE];
  83. cond2str:array[TAsmCond] of string[3]=('',
  84. 'gu','cc','cs','leu','cs','e','g','ge','l','le','leu','cs',
  85. 'cc','gu','cc','ne','le','l','ge','g','vc','XX',
  86. 'pos','ne','vs','XX','XX','XX','vs','e',
  87. 'e','g','l','ge','le','ne'
  88. );
  89. {*****************************************************************************
  90. Flags
  91. *****************************************************************************}
  92. type
  93. TResFlags=(
  94. { Integer results }
  95. F_E, {Equal}
  96. F_NE, {Not Equal}
  97. F_G, {Greater}
  98. F_L, {Less}
  99. F_GE, {Greater or Equal}
  100. F_LE, {Less or Equal}
  101. F_A, {Above}
  102. F_AE, {Above or Equal}
  103. F_B, {Below}
  104. F_BE, {Below or Equal}
  105. F_C, {Carry}
  106. F_NC, {Not Carry}
  107. { Floating point results }
  108. F_FE, {Equal}
  109. F_FNE, {Not Equal}
  110. F_FG, {Greater}
  111. F_FL, {Less}
  112. F_FGE, {Greater or Equal}
  113. F_FLE {Less or Equal}
  114. );
  115. {*****************************************************************************
  116. Operand Sizes
  117. *****************************************************************************}
  118. {*****************************************************************************
  119. Constants
  120. *****************************************************************************}
  121. const
  122. max_operands = 3;
  123. maxintregs = 8;
  124. maxfpuregs = 8;
  125. maxaddrregs = 0;
  126. maxvarregs = 8;
  127. varregs : Array [1..maxvarregs] of Tsuperregister =
  128. (RS_L0,RS_L1,RS_L2,RS_L3,RS_L4,RS_L5,RS_L6,RS_L7);
  129. maxfpuvarregs = 1;
  130. fpuvarregs : Array [1..maxfpuvarregs] of TsuperRegister =
  131. (RS_F2);
  132. {*****************************************************************************
  133. Default generic sizes
  134. *****************************************************************************}
  135. {# Defines the default address size for a processor, }
  136. OS_ADDR = OS_32;
  137. {# the natural int size for a processor,
  138. has to match osuinttype/ossinttype as initialized in psystem }
  139. OS_INT = OS_32;
  140. OS_SINT = OS_S32;
  141. {# the maximum float size for a processor, }
  142. OS_FLOAT = OS_F64;
  143. {# the size of a vector register for a processor }
  144. OS_VECTOR = OS_M64;
  145. {*****************************************************************************
  146. Generic Register names
  147. *****************************************************************************}
  148. {# Stack pointer register }
  149. NR_STACK_POINTER_REG = NR_O6;
  150. RS_STACK_POINTER_REG = RS_O6;
  151. {# Frame pointer register }
  152. NR_FRAME_POINTER_REG = NR_I6;
  153. RS_FRAME_POINTER_REG = RS_I6;
  154. {# Register for addressing absolute data in a position independant way,
  155. such as in PIC code. The exact meaning is ABI specific. For
  156. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  157. Taken from GCC rs6000.h
  158. }
  159. { TODO: As indicated in rs6000.h, but can't find it anywhere else!}
  160. {PIC_OFFSET_REG = R_30;}
  161. { Return address for DWARF }
  162. NR_RETURN_ADDRESS_REG = NR_I7;
  163. { the return_result_reg, is used inside the called function to store its return
  164. value when that is a scalar value otherwise a pointer to the address of the
  165. result is placed inside it }
  166. { Results are returned in this register (32-bit values) }
  167. NR_FUNCTION_RETURN_REG = NR_I0;
  168. RS_FUNCTION_RETURN_REG = RS_I0;
  169. { Low part of 64bit return value }
  170. NR_FUNCTION_RETURN64_LOW_REG = NR_I1;
  171. RS_FUNCTION_RETURN64_LOW_REG = RS_I1;
  172. { High part of 64bit return value }
  173. NR_FUNCTION_RETURN64_HIGH_REG = NR_I0;
  174. RS_FUNCTION_RETURN64_HIGH_REG = RS_I0;
  175. { The value returned from a function is available in this register }
  176. NR_FUNCTION_RESULT_REG = NR_O0;
  177. RS_FUNCTION_RESULT_REG = RS_O0;
  178. { The lowh part of 64bit value returned from a function }
  179. NR_FUNCTION_RESULT64_LOW_REG = NR_O1;
  180. RS_FUNCTION_RESULT64_LOW_REG = RS_O1;
  181. { The high part of 64bit value returned from a function }
  182. NR_FUNCTION_RESULT64_HIGH_REG = NR_O0;
  183. RS_FUNCTION_RESULT64_HIGH_REG = RS_O0;
  184. NR_FPU_RESULT_REG = NR_F0;
  185. NR_MM_RESULT_REG = NR_NO;
  186. PARENT_FRAMEPOINTER_OFFSET = 68; { o0 }
  187. NR_DEFAULTFLAGS = NR_PSR;
  188. RS_DEFAULTFLAGS = RS_PSR;
  189. {*****************************************************************************
  190. GCC /ABI linking information
  191. *****************************************************************************}
  192. {# Registers which must be saved when calling a routine declared as
  193. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  194. saved should be the ones as defined in the target ABI and / or GCC.
  195. This value can be deduced from CALLED_USED_REGISTERS array in the
  196. GCC source.
  197. }
  198. saved_standard_registers : array[0..0] of tsuperregister = (RS_INVALID);
  199. { this is only for the generic code which is not used for this architecture }
  200. saved_address_registers : array[0..0] of tsuperregister = (RS_INVALID);
  201. saved_mm_registers : array[0..0] of tsuperregister = (RS_INVALID);
  202. {# Required parameter alignment when calling a routine declared as
  203. stdcall and cdecl. The alignment value should be the one defined
  204. by GCC or the target ABI.
  205. The value of this constant is equal to the constant
  206. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  207. }
  208. std_param_align = 4; { for 32-bit version only }
  209. {*****************************************************************************
  210. CPU Dependent Constants
  211. *****************************************************************************}
  212. const
  213. simm13lo=-4096;
  214. simm13hi=4095;
  215. {*****************************************************************************
  216. Helpers
  217. *****************************************************************************}
  218. function is_calljmp(o:tasmop):boolean;
  219. procedure inverse_flags(var f: TResFlags);
  220. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  221. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  222. function flags_to_cond(const f: TResFlags) : TAsmCond;
  223. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  224. function reg_cgsize(const reg: tregister): tcgsize;
  225. function std_regname(r:Tregister):string;
  226. function std_regnum_search(const s:string):Tregister;
  227. function findreg_by_number(r:Tregister):tregisterindex;
  228. function dwarf_reg(r:tregister):shortint;
  229. implementation
  230. uses
  231. rgBase,verbose;
  232. const
  233. std_regname_table : TRegNameTAble = (
  234. {$i rspstd.inc}
  235. );
  236. regnumber_index : TRegisterIndexTable = (
  237. {$i rsprni.inc}
  238. );
  239. std_regname_index : TRegisterIndexTable = (
  240. {$i rspsri.inc}
  241. );
  242. {*****************************************************************************
  243. Helpers
  244. *****************************************************************************}
  245. function is_calljmp(o:tasmop):boolean;
  246. const
  247. CallJmpOp=[A_JMPL..A_CBccc];
  248. begin
  249. is_calljmp:=(o in CallJmpOp);
  250. end;
  251. procedure inverse_flags(var f: TResFlags);
  252. const
  253. inv_flags: array[TResFlags] of TResFlags =
  254. (F_NE,F_E,F_LE,F_GE,F_L,F_G,F_BE,F_B,F_AE,F_A,F_NC,F_C,
  255. F_FNE,F_FE,F_FLE,F_FGE,F_FL,F_FG);
  256. begin
  257. f:=inv_flags[f];
  258. end;
  259. function flags_to_cond(const f:TResFlags):TAsmCond;
  260. const
  261. flags_2_cond:array[TResFlags] of TAsmCond=
  262. (C_E,C_NE,C_G,C_L,C_GE,C_LE,C_A,C_AE,C_B,C_BE,C_C,C_NC,
  263. C_FE,C_FNE,C_FG,C_FL,C_FGE,C_FLE);
  264. begin
  265. result:=flags_2_cond[f];
  266. end;
  267. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  268. begin
  269. case regtype of
  270. R_FPUREGISTER:
  271. case s of
  272. OS_F32:
  273. cgsize2subreg:=R_SUBFS;
  274. OS_F64:
  275. cgsize2subreg:=R_SUBFD;
  276. OS_F128:
  277. cgsize2subreg:=R_SUBFQ;
  278. else
  279. internalerror(2009071903);
  280. end;
  281. else
  282. begin
  283. if s in [OS_64,OS_S64] then
  284. cgsize2subreg:=R_SUBQ
  285. else
  286. cgsize2subreg:=R_SUBWHOLE;
  287. end;
  288. end;
  289. end;
  290. function reg_cgsize(const reg: tregister): tcgsize;
  291. begin
  292. case getregtype(reg) of
  293. R_INTREGISTER :
  294. result:=OS_32;
  295. R_FPUREGISTER :
  296. begin
  297. if getsubreg(reg)=R_SUBFD then
  298. result:=OS_F64
  299. else
  300. result:=OS_F32;
  301. end;
  302. else
  303. internalerror(200303181);
  304. end;
  305. end;
  306. function findreg_by_number(r:Tregister):tregisterindex;
  307. begin
  308. result:=findreg_by_number_table(r,regnumber_index);
  309. end;
  310. function std_regname(r:Tregister):string;
  311. var
  312. p : tregisterindex;
  313. begin
  314. { For double floats show a pair like %f0:%f1 }
  315. if (getsubreg(r)=R_SUBFD) and
  316. (getsupreg(r)<first_fpu_imreg) then
  317. begin
  318. setsubreg(r,R_SUBFS);
  319. p:=findreg_by_number(r);
  320. if p<>0 then
  321. result:=std_regname_table[p]
  322. else
  323. result:=generic_regname(r);
  324. setsupreg(r,getsupreg(r)+1);
  325. p:=findreg_by_number(r);
  326. if p<>0 then
  327. result:=result+':'+std_regname_table[p]
  328. else
  329. result:=result+':'+generic_regname(r);
  330. end
  331. else
  332. begin
  333. p:=findreg_by_number(r);
  334. if p<>0 then
  335. result:=std_regname_table[p]
  336. else
  337. result:=generic_regname(r);
  338. end;
  339. end;
  340. function std_regnum_search(const s:string):Tregister;
  341. begin
  342. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  343. end;
  344. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  345. const
  346. inverse: array[TAsmCond] of TAsmCond=(C_None,
  347. C_NA,C_NAE,C_NB,C_NBE,C_NC,C_NE,C_NG,C_NGE,C_NL,C_NLE,C_A,C_AE,
  348. C_B,C_BE,C_C,C_E,C_G,C_GE,C_L,C_LE,C_O,C_P,
  349. C_S,C_Z,C_NO,C_NP,C_NP,C_P,C_NS,C_NZ,
  350. C_FNE,C_FLE,C_FGE,C_FL,C_FG,C_FE
  351. );
  352. begin
  353. result := inverse[c];
  354. end;
  355. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  356. begin
  357. result := c1 = c2;
  358. end;
  359. function dwarf_reg(r:tregister):shortint;
  360. begin
  361. result:=regdwarf_table[findreg_by_number(r)];
  362. if result=-1 then
  363. internalerror(200603251);
  364. end;
  365. end.