cpubase.pas 14 KB

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