cpubase.pas 15 KB

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