cpubase.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Contains the base types for the m68k
  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 m68k
  19. }
  20. unit cpubase;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. globtype,
  25. strings,cutils,cclasses,aasmbase,cpuinfo,cgbase;
  26. {*****************************************************************************
  27. Assembler Opcodes
  28. *****************************************************************************}
  29. type
  30. { warning: CPU32 opcodes are not fully compatible with the MC68020. }
  31. { 68000 only opcodes }
  32. tasmop = (a_abcd,
  33. a_add,a_adda,a_addi,a_addq,a_addx,a_and,a_andi,
  34. a_asl,a_asr,a_bcc,a_bcs,a_beq,a_bge,a_bgt,a_bhi,
  35. a_ble,a_bls,a_blt,a_bmi,a_bne,a_bpl,a_bvc,a_bvs,
  36. a_bchg,a_bclr,a_bra,a_bset,a_bsr,a_btst,a_chk,
  37. a_clr,a_cmp,a_cmpa,a_cmpi,a_cmpm,a_dbcc,a_dbcs,a_dbeq,a_dbge,
  38. a_dbgt,a_dbhi,a_dble,a_dbls,a_dblt,a_dbmi,a_dbne,a_dbra,
  39. a_dbpl,a_dbt,a_dbvc,a_dbvs,a_dbf,a_divs,a_divu,
  40. a_eor,a_eori,a_exg,a_illegal,a_ext,a_jmp,a_jsr,
  41. a_lea,a_link,a_lsl,a_lsr,a_move,a_movea,a_movei,a_moveq,
  42. a_movem,a_movep,a_muls,a_mulu,a_nbcd,a_neg,a_negx,
  43. a_nop,a_not,a_or,a_ori,a_pea,a_rol,a_ror,a_roxl,
  44. a_roxr,a_rtr,a_rts,a_sbcd,a_scc,a_scs,a_seq,a_sge,
  45. a_sgt,a_shi,a_sle,a_sls,a_slt,a_smi,a_sne,
  46. a_spl,a_st,a_svc,a_svs,a_sf,a_sub,a_suba,a_subi,a_subq,
  47. a_subx,a_swap,a_tas,a_trap,a_trapv,a_tst,a_unlk,
  48. a_rte,a_reset,a_stop,
  49. { mc68010 instructions }
  50. a_bkpt,a_movec,a_moves,a_rtd,
  51. { mc68020 instructions }
  52. a_bfchg,a_bfclr,a_bfexts,a_bfextu,a_bfffo,
  53. a_bfins,a_bfset,a_bftst,a_callm,a_cas,a_cas2,
  54. a_chk2,a_cmp2,a_divsl,a_divul,a_extb,a_pack,a_rtm,
  55. a_trapcc,a_tracs,a_trapeq,a_trapf,a_trapge,a_trapgt,
  56. a_traphi,a_traple,a_trapls,a_traplt,a_trapmi,a_trapne,
  57. a_trappl,a_trapt,a_trapvc,a_trapvs,a_unpk,
  58. { fpu processor instructions - directly supported only. }
  59. { ieee aware and misc. condition codes not supported }
  60. a_fabs,a_fadd,
  61. a_fbeq,a_fbne,a_fbngt,a_fbgt,a_fbge,a_fbnge,
  62. a_fblt,a_fbnlt,a_fble,a_fbgl,a_fbngl,a_fbgle,a_fbngle,
  63. a_fdbeq,a_fdbne,a_fdbgt,a_fdbngt,a_fdbge,a_fdbnge,
  64. a_fdblt,a_fdbnlt,a_fdble,a_fdbgl,a_fdbngl,a_fdbgle,a_fdbngle,
  65. a_fseq,a_fsne,a_fsgt,a_fsngt,a_fsge,a_fsnge,
  66. a_fslt,a_fsnlt,a_fsle,a_fsgl,a_fsngl,a_fsgle,a_fsngle,
  67. a_fcmp,a_fdiv,a_fmove,a_fmovem,
  68. a_fmul,a_fneg,a_fnop,a_fsqrt,a_fsub,a_fsgldiv,
  69. a_fsflmul,a_ftst,
  70. a_ftrapeq,a_ftrapne,a_ftrapgt,a_ftrapngt,a_ftrapge,a_ftrapnge,
  71. a_ftraplt,a_ftrapnlt,a_ftraple,a_ftrapgl,a_ftrapngl,a_ftrapgle,a_ftrapngle,
  72. { protected instructions }
  73. a_cprestore,a_cpsave,
  74. { fpu unit protected instructions }
  75. { and 68030/68851 common mmu instructions }
  76. { (this may include 68040 mmu instructions) }
  77. a_frestore,a_fsave,a_pflush,a_pflusha,a_pload,a_pmove,a_ptest,
  78. { useful for assembly language output }
  79. a_label,a_none,a_dbxx,a_sxx,a_bxx,a_fbxx);
  80. {# This should define the array of instructions as string }
  81. op2strtable=array[tasmop] of string[11];
  82. Const
  83. {# First value of opcode enumeration }
  84. firstop = low(tasmop);
  85. {# Last value of opcode enumeration }
  86. lastop = high(tasmop);
  87. {*****************************************************************************
  88. Registers
  89. *****************************************************************************}
  90. type
  91. { Number of registers used for indexing in tables }
  92. tregisterindex=0..{$i r68knor.inc}-1;
  93. const
  94. { Available Superregisters }
  95. {$i r68ksup.inc}
  96. { No Subregisters }
  97. R_SUBWHOLE = R_SUBNONE;
  98. { Available Registers }
  99. {$i r68kcon.inc}
  100. { Integer Super registers first and last }
  101. first_int_imreg = RS_D7+1;
  102. { Float Super register first and last }
  103. first_fpu_imreg = RS_FP7+1;
  104. { Integer Super registers first and last }
  105. first_addr_imreg = RS_SP+1;
  106. { MM Super register first and last }
  107. first_mm_supreg = 0;
  108. first_mm_imreg = 0;
  109. regnumber_count_bsstart = 64;
  110. regnumber_table : array[tregisterindex] of tregister = (
  111. {$i r68knum.inc}
  112. );
  113. regstabs_table : array[tregisterindex] of shortint = (
  114. {$i r68ksta.inc}
  115. );
  116. { registers which may be destroyed by calls }
  117. VOLATILE_INTREGISTERS = [];
  118. VOLATILE_FPUREGISTERS = [];
  119. type
  120. totherregisterset = set of tregisterindex;
  121. {*****************************************************************************
  122. Conditions
  123. *****************************************************************************}
  124. type
  125. TAsmCond=(C_None,
  126. C_CC,C_LS,C_CS,C_LT,C_EQ,C_MI,C_F,C_NE,
  127. C_GE,C_PL,C_GT,C_T,C_HI,C_VC,C_LE,C_VS
  128. );
  129. const
  130. cond2str:array[TAsmCond] of string[3]=('',
  131. 'cc','ls','cs','lt','eq','mi','f','ne',
  132. 'ge','pl','gt','t','hi','vc','le','vs'
  133. );
  134. {*****************************************************************************
  135. Flags
  136. *****************************************************************************}
  137. type
  138. TResFlags = (
  139. F_E,F_NE,
  140. F_G,F_L,F_GE,F_LE,F_C,F_NC,F_A,F_AE,F_B,F_BE);
  141. {*****************************************************************************
  142. Reference
  143. *****************************************************************************}
  144. type
  145. trefoptions=(ref_none,ref_parafixup,ref_localfixup,ref_selffixup);
  146. { direction of address register : }
  147. { (An) (An)+ -(An) }
  148. tdirection = (dir_none,dir_inc,dir_dec);
  149. { reference record }
  150. preference = ^treference;
  151. treference = packed record
  152. base,
  153. index : tregister;
  154. scalefactor : byte;
  155. offset : longint;
  156. symbol : tasmsymbol;
  157. { symbol the symbol of this reference is relative to, nil if none }
  158. relsymbol : tasmsymbol;
  159. { reference type addr or symbol itself }
  160. refaddr : trefaddr;
  161. options : trefoptions;
  162. { indexed increment and decrement mode }
  163. { (An)+ and -(An) }
  164. direction : tdirection;
  165. end;
  166. { reference record }
  167. pparareference = ^tparareference;
  168. tparareference = record
  169. offset : longint;
  170. index : tregister;
  171. end;
  172. {*****************************************************************************
  173. Generic Location
  174. *****************************************************************************}
  175. type
  176. { tparamlocation describes where a parameter for a procedure is stored.
  177. References are given from the caller's point of view. The usual
  178. TLocation isn't used, because contains a lot of unnessary fields.
  179. }
  180. tparalocation = record
  181. size : TCGSize;
  182. loc : TCGLoc;
  183. lochigh : TCGLoc;
  184. alignment : byte;
  185. case TCGLoc of
  186. LOC_REFERENCE : (reference : tparareference);
  187. { segment in reference at the same place as in loc_register }
  188. LOC_REGISTER,LOC_CREGISTER : (
  189. case longint of
  190. 1 : (register,registerhigh : tregister);
  191. { overlay a registerlow }
  192. 2 : (registerlow : tregister);
  193. { overlay a 64 Bit register type }
  194. 3 : (reg64 : tregister64);
  195. 4 : (register64 : tregister64);
  196. );
  197. end;
  198. tlocation = record
  199. loc : TCGLoc;
  200. size : TCGSize;
  201. case TCGLoc of
  202. LOC_FLAGS : (resflags : tresflags);
  203. LOC_CONSTANT : (
  204. case longint of
  205. 1 : (value : aint);
  206. { can't do this, this layout depends on the host cpu. Use }
  207. { lo(valueqword)/hi(valueqword) instead (JM) }
  208. { 2 : (valuelow, valuehigh:AWord); }
  209. { overlay a complete 64 Bit value }
  210. 3 : (value64 : qword);
  211. );
  212. LOC_CREFERENCE,
  213. LOC_REFERENCE : (reference : treference);
  214. { segment in reference at the same place as in loc_register }
  215. LOC_REGISTER,LOC_CREGISTER : (
  216. case longint of
  217. 1 : (register,registerhigh,segment : tregister);
  218. { overlay a registerlow }
  219. 2 : (registerlow : tregister);
  220. { overlay a 64 Bit register type }
  221. 3 : (reg64 : tregister64);
  222. 4 : (register64 : tregister64);
  223. );
  224. end;
  225. {*****************************************************************************
  226. Operand Sizes
  227. *****************************************************************************}
  228. { S_NO = No Size of operand }
  229. { S_B = 8-bit size operand }
  230. { S_W = 16-bit size operand }
  231. { S_L = 32-bit size operand }
  232. { Floating point types }
  233. { S_FS = single type (32 bit) }
  234. { S_FD = double/64bit integer }
  235. { S_FX = Extended type }
  236. topsize = (S_NO,S_B,S_W,S_L,S_FS,S_FD,S_FX,S_IQ);
  237. {*****************************************************************************
  238. Constants
  239. *****************************************************************************}
  240. const
  241. {# maximum number of operands in assembler instruction }
  242. max_operands = 4;
  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_M128;
  254. {*****************************************************************************
  255. GDB Information
  256. *****************************************************************************}
  257. {# Register indexes for stabs information, when some
  258. parameters or variables are stored in registers.
  259. Taken from m68kelf.h (DBX_REGISTER_NUMBER)
  260. from GCC 3.x source code.
  261. This is not compatible with the m68k-sun
  262. implementation.
  263. }
  264. stab_regindex : array[tregisterindex] of shortint =
  265. (
  266. {$i r68ksta.inc}
  267. );
  268. {*****************************************************************************
  269. Generic Register names
  270. *****************************************************************************}
  271. {# Stack pointer register }
  272. NR_STACK_POINTER_REG = NR_SP;
  273. RS_STACK_POINTER_REG = RS_SP;
  274. {# Frame pointer register }
  275. NR_FRAME_POINTER_REG = NR_A6;
  276. RS_FRAME_POINTER_REG = RS_A6;
  277. {# Register for addressing absolute data in a position independant way,
  278. such as in PIC code. The exact meaning is ABI specific. For
  279. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  280. }
  281. NR_PIC_OFFSET_REG = NR_A5;
  282. { Results are returned in this register (32-bit values) }
  283. NR_FUNCTION_RETURN_REG = NR_D0;
  284. RS_FUNCTION_RETURN_REG = NR_D0;
  285. { Low part of 64bit return value }
  286. NR_FUNCTION_RETURN64_LOW_REG = NR_D0;
  287. RS_FUNCTION_RETURN64_LOW_REG = RS_D0;
  288. { High part of 64bit return value }
  289. NR_FUNCTION_RETURN64_HIGH_REG = NR_D1;
  290. RS_FUNCTION_RETURN64_HIGH_REG = RS_D1;
  291. { The value returned from a function is available in this register }
  292. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  293. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  294. { The lowh part of 64bit value returned from a function }
  295. NR_FUNCTION_RESULT64_LOW_REG = NR_FUNCTION_RETURN64_LOW_REG;
  296. RS_FUNCTION_RESULT64_LOW_REG = RS_FUNCTION_RETURN64_LOW_REG;
  297. { The high part of 64bit value returned from a function }
  298. NR_FUNCTION_RESULT64_HIGH_REG = NR_FUNCTION_RETURN64_HIGH_REG;
  299. RS_FUNCTION_RESULT64_HIGH_REG = RS_FUNCTION_RETURN64_HIGH_REG;
  300. {# Floating point results will be placed into this register }
  301. NR_FPU_RESULT_REG = NR_FP0;
  302. {*****************************************************************************
  303. GCC /ABI linking information
  304. *****************************************************************************}
  305. {# Registers which must be saved when calling a routine declared as
  306. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  307. saved should be the ones as defined in the target ABI and / or GCC.
  308. This value can be deduced from CALLED_USED_REGISTERS array in the
  309. GCC source.
  310. }
  311. std_saved_intregisters = [RS_D2..RS_D7];
  312. std_saved_addrregisters = [RS_A2..RS_A5];
  313. {# Required parameter alignment when calling a routine declared as
  314. stdcall and cdecl. The alignment value should be the one defined
  315. by GCC or the target ABI.
  316. The value of this constant is equal to the constant
  317. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  318. }
  319. std_param_align = 4; { for 32-bit version only }
  320. {*****************************************************************************
  321. CPU Dependent Constants
  322. *****************************************************************************}
  323. {*****************************************************************************
  324. Helpers
  325. *****************************************************************************}
  326. function is_calljmp(o:tasmop):boolean;
  327. procedure inverse_flags(var r : TResFlags);
  328. function flags_to_cond(const f: TResFlags) : TAsmCond;
  329. function cgsize2subreg(s:Tcgsize):Tsubregister;
  330. function findreg_by_number(r:Tregister):tregisterindex;
  331. function std_regnum_search(const s:string):Tregister;
  332. function std_regname(r:Tregister):string;
  333. function isaddressregister(reg : tregister) : boolean;
  334. implementation
  335. uses
  336. verbose,
  337. rgbase;
  338. const
  339. std_regname_table : array[tregisterindex] of string[7] = (
  340. {$i r68kstd.inc}
  341. );
  342. regnumber_index : array[tregisterindex] of tregisterindex = (
  343. {$i r68krni.inc}
  344. );
  345. std_regname_index : array[tregisterindex] of tregisterindex = (
  346. {$i r68ksri.inc}
  347. );
  348. {*****************************************************************************
  349. Helpers
  350. *****************************************************************************}
  351. function is_calljmp(o:tasmop):boolean;
  352. begin
  353. is_calljmp := false;
  354. if o in [A_BXX,A_FBXX,A_DBXX,A_BCC..A_BVS,A_DBCC..A_DBVS,A_FBEQ..A_FSNGLE,
  355. A_JSR,A_BSR,A_JMP] then
  356. is_calljmp := true;
  357. end;
  358. procedure inverse_flags(var r: TResFlags);
  359. const flagsinvers : array[F_E..F_BE] of tresflags =
  360. (F_NE,F_E,
  361. F_LE,F_GE,
  362. F_L,F_G,
  363. F_NC,F_C,
  364. F_BE,F_B,
  365. F_AE,F_A);
  366. begin
  367. r:=flagsinvers[r];
  368. end;
  369. function flags_to_cond(const f: TResFlags) : TAsmCond;
  370. const flags2cond: array[tresflags] of tasmcond = (
  371. C_EQ,{F_E equal}
  372. C_NE,{F_NE not equal}
  373. C_GT,{F_G gt signed}
  374. C_LT,{F_L lt signed}
  375. C_GE,{F_GE ge signed}
  376. C_LE,{F_LE le signed}
  377. C_CS,{F_C carry set}
  378. C_CC,{F_NC carry clear}
  379. C_HI,{F_A gt unsigned}
  380. C_CC,{F_AE ge unsigned}
  381. C_CS,{F_B lt unsigned}
  382. C_LS);{F_BE le unsigned}
  383. begin
  384. flags_to_cond := flags2cond[f];
  385. end;
  386. function cgsize2subreg(s:Tcgsize):Tsubregister;
  387. begin
  388. case s of
  389. OS_8,OS_S8:
  390. cgsize2subreg:=R_SUBL;
  391. OS_16,OS_S16:
  392. cgsize2subreg:=R_SUBW;
  393. OS_32,OS_S32:
  394. cgsize2subreg:=R_SUBD;
  395. else
  396. internalerror(200301231);
  397. end;
  398. end;
  399. function findreg_by_number(r:Tregister):tregisterindex;
  400. begin
  401. result:=findreg_by_number_table(r,regnumber_index);
  402. end;
  403. function std_regnum_search(const s:string):Tregister;
  404. begin
  405. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  406. end;
  407. function std_regname(r:Tregister):string;
  408. var
  409. p : tregisterindex;
  410. begin
  411. p:=findreg_by_number_table(r,regnumber_index);
  412. if p<>0 then
  413. result:=std_regname_table[p]
  414. else
  415. result:=generic_regname(r);
  416. end;
  417. function isaddressregister(reg : tregister) : boolean;
  418. begin
  419. result:=getregtype(reg)=R_ADDRESSREGISTER;
  420. end;
  421. end.
  422. {
  423. $Log$
  424. Revision 1.31 2004-06-20 08:55:31 florian
  425. * logs truncated
  426. Revision 1.30 2004/06/20 08:47:33 florian
  427. * spilling of doubles on sparc fixed
  428. Revision 1.29 2004/06/16 20:07:10 florian
  429. * dwarf branch merged
  430. Revision 1.28 2004/05/06 22:01:54 florian
  431. * register numbers for address registers fixed
  432. Revision 1.27 2004/05/06 20:30:51 florian
  433. * m68k compiler compilation fixed
  434. Revision 1.26 2004/04/25 21:26:16 florian
  435. * some m68k stuff fixed
  436. }