cpubase.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Contains the base types for the m68k
  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. { This Unit contains the base types for the m68k
  18. }
  19. unit cpubase;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. globtype,
  24. strings,cutils,cclasses,aasmbase,cpuinfo,cgbase;
  25. {*****************************************************************************
  26. Assembler Opcodes
  27. *****************************************************************************}
  28. type
  29. { warning: CPU32 opcodes are not fully compatible with the MC68020. }
  30. { 68000 only opcodes }
  31. tasmop = (a_abcd,
  32. a_add,a_adda,a_addi,a_addq,a_addx,a_and,a_andi,
  33. a_asl,a_asr,a_bcc,a_bcs,a_beq,a_bge,a_bgt,a_bhi,
  34. a_ble,a_bls,a_blt,a_bmi,a_bne,a_bpl,a_bvc,a_bvs,
  35. a_bchg,a_bclr,a_bra,a_bset,a_bsr,a_btst,a_chk,
  36. a_clr,a_cmp,a_cmpa,a_cmpi,a_cmpm,a_dbcc,a_dbcs,a_dbeq,a_dbge,
  37. a_dbgt,a_dbhi,a_dble,a_dbls,a_dblt,a_dbmi,a_dbne,a_dbra,
  38. a_dbpl,a_dbt,a_dbvc,a_dbvs,a_dbf,a_divs,a_divu,
  39. a_eor,a_eori,a_exg,a_illegal,a_ext,a_jmp,a_jsr,
  40. a_lea,a_link,a_lsl,a_lsr,a_move,a_movea,a_movei,a_moveq,
  41. a_movem,a_movep,a_muls,a_mulu,a_nbcd,a_neg,a_negx,
  42. a_nop,a_not,a_or,a_ori,a_pea,a_rol,a_ror,a_roxl,
  43. a_roxr,a_rtr,a_rts,a_sbcd,a_scc,a_scs,a_seq,a_sge,
  44. a_sgt,a_shi,a_sle,a_sls,a_slt,a_smi,a_sne,
  45. a_spl,a_st,a_svc,a_svs,a_sf,a_sub,a_suba,a_subi,a_subq,
  46. a_subx,a_swap,a_tas,a_trap,a_trapv,a_tst,a_unlk,
  47. a_rte,a_reset,a_stop,
  48. { mc68010 instructions }
  49. a_bkpt,a_movec,a_moves,a_rtd,
  50. { mc68020 instructions }
  51. a_bfchg,a_bfclr,a_bfexts,a_bfextu,a_bfffo,
  52. a_bfins,a_bfset,a_bftst,a_callm,a_cas,a_cas2,
  53. a_chk2,a_cmp2,a_divsl,a_divul,a_extb,a_pack,a_rtm,
  54. a_trapcc,a_tracs,a_trapeq,a_trapf,a_trapge,a_trapgt,
  55. a_traphi,a_traple,a_trapls,a_traplt,a_trapmi,a_trapne,
  56. a_trappl,a_trapt,a_trapvc,a_trapvs,a_unpk,
  57. { fpu processor instructions - directly supported only. }
  58. { ieee aware and misc. condition codes not supported }
  59. a_fabs,a_fadd,
  60. a_fbeq,a_fbne,a_fbngt,a_fbgt,a_fbge,a_fbnge,
  61. a_fblt,a_fbnlt,a_fble,a_fbgl,a_fbngl,a_fbgle,a_fbngle,
  62. a_fdbeq,a_fdbne,a_fdbgt,a_fdbngt,a_fdbge,a_fdbnge,
  63. a_fdblt,a_fdbnlt,a_fdble,a_fdbgl,a_fdbngl,a_fdbgle,a_fdbngle,
  64. a_fseq,a_fsne,a_fsgt,a_fsngt,a_fsge,a_fsnge,
  65. a_fslt,a_fsnlt,a_fsle,a_fsgl,a_fsngl,a_fsgle,a_fsngle,
  66. a_fcmp,a_fdiv,a_fmove,a_fmovem,
  67. a_fmul,a_fneg,a_fnop,a_fsqrt,a_fsub,a_fsgldiv,
  68. a_fsflmul,a_ftst,
  69. a_ftrapeq,a_ftrapne,a_ftrapgt,a_ftrapngt,a_ftrapge,a_ftrapnge,
  70. a_ftraplt,a_ftrapnlt,a_ftraple,a_ftrapgl,a_ftrapngl,a_ftrapgle,a_ftrapngle,
  71. { protected instructions }
  72. a_cprestore,a_cpsave,
  73. { fpu unit protected instructions }
  74. { and 68030/68851 common mmu instructions }
  75. { (this may include 68040 mmu instructions) }
  76. a_frestore,a_fsave,a_pflush,a_pflusha,a_pload,a_pmove,a_ptest,
  77. { useful for assembly language output }
  78. a_label,a_none,a_dbxx,a_sxx,a_bxx,a_fbxx);
  79. {# This should define the array of instructions as string }
  80. op2strtable=array[tasmop] of string[11];
  81. Const
  82. {# First value of opcode enumeration }
  83. firstop = low(tasmop);
  84. {# Last value of opcode enumeration }
  85. lastop = high(tasmop);
  86. {*****************************************************************************
  87. Registers
  88. *****************************************************************************}
  89. type
  90. { Number of registers used for indexing in tables }
  91. tregisterindex=0..{$i r68knor.inc}-1;
  92. const
  93. { Available Superregisters }
  94. {$i r68ksup.inc}
  95. { ? whatever... }
  96. R_SUBWHOLE = R_SUBNONE;
  97. { Available Registers }
  98. {$i r68kcon.inc}
  99. { Integer Super registers first and last }
  100. first_int_imreg = RS_D7+1;
  101. { Float Super register first and last }
  102. first_fpu_imreg = RS_FP7+1;
  103. { Integer Super registers first and last }
  104. first_addr_imreg = RS_SP+1;
  105. { MM Super register first and last }
  106. first_mm_supreg = 0;
  107. first_mm_imreg = 0;
  108. {$WARNING TODO FIX BSSTART}
  109. regnumber_count_bsstart = 16;
  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. regdwarf_table : array[tregisterindex] of shortint = (
  117. {$warning TODO reused stabs values!}
  118. {$i r68ksta.inc}
  119. );
  120. { registers which may be destroyed by calls }
  121. VOLATILE_INTREGISTERS = [];
  122. VOLATILE_FPUREGISTERS = [];
  123. type
  124. totherregisterset = set of tregisterindex;
  125. {*****************************************************************************
  126. Conditions
  127. *****************************************************************************}
  128. type
  129. TAsmCond=(C_None,
  130. C_CC,C_LS,C_CS,C_LT,C_EQ,C_MI,C_F,C_NE,
  131. C_GE,C_PL,C_GT,C_T,C_HI,C_VC,C_LE,C_VS
  132. );
  133. const
  134. cond2str:array[TAsmCond] of string[3]=('',
  135. 'cc','ls','cs','lt','eq','mi','f','ne',
  136. 'ge','pl','gt','t','hi','vc','le','vs'
  137. );
  138. {*****************************************************************************
  139. Flags
  140. *****************************************************************************}
  141. type
  142. TResFlags = (
  143. F_E,F_NE,
  144. F_G,F_L,F_GE,F_LE,F_C,F_NC,F_A,F_AE,F_B,F_BE);
  145. {*****************************************************************************
  146. Reference
  147. *****************************************************************************}
  148. type
  149. { direction of address register : }
  150. { (An) (An)+ -(An) }
  151. tdirection = (dir_none,dir_inc,dir_dec);
  152. {*****************************************************************************
  153. Operand Sizes
  154. *****************************************************************************}
  155. { S_NO = No Size of operand }
  156. { S_B = 8-bit size operand }
  157. { S_W = 16-bit size operand }
  158. { S_L = 32-bit size operand }
  159. { Floating point types }
  160. { S_FS = single type (32 bit) }
  161. { S_FD = double/64bit integer }
  162. { S_FX = Extended type }
  163. topsize = (S_NO,S_B,S_W,S_L,S_FS,S_FD,S_FX,S_IQ);
  164. {*****************************************************************************
  165. Constants
  166. *****************************************************************************}
  167. const
  168. {# maximum number of operands in assembler instruction }
  169. max_operands = 4;
  170. {*****************************************************************************
  171. Default generic sizes
  172. *****************************************************************************}
  173. {# Defines the default address size for a processor, }
  174. OS_ADDR = OS_32;
  175. {# the natural int size for a processor, }
  176. OS_INT = OS_32;
  177. OS_SINT = OS_S32;
  178. {# the maximum float size for a processor, }
  179. OS_FLOAT = OS_F64;
  180. {# the size of a vector register for a processor }
  181. OS_VECTOR = OS_M128;
  182. {*****************************************************************************
  183. GDB Information
  184. *****************************************************************************}
  185. {# Register indexes for stabs information, when some
  186. parameters or variables are stored in registers.
  187. Taken from m68kelf.h (DBX_REGISTER_NUMBER)
  188. from GCC 3.x source code.
  189. This is not compatible with the m68k-sun
  190. implementation.
  191. }
  192. stab_regindex : array[tregisterindex] of shortint =
  193. (
  194. {$i r68ksta.inc}
  195. );
  196. {*****************************************************************************
  197. Generic Register names
  198. *****************************************************************************}
  199. {# Stack pointer register }
  200. NR_STACK_POINTER_REG = NR_SP;
  201. RS_STACK_POINTER_REG = RS_SP;
  202. {# Frame pointer register }
  203. {$warning FIX ME!!! frame pointer is A5 on Amiga, but A6 on unixes?}
  204. NR_FRAME_POINTER_REG = NR_A5;
  205. RS_FRAME_POINTER_REG = RS_A5;
  206. {# Register for addressing absolute data in a position independant way,
  207. such as in PIC code. The exact meaning is ABI specific. For
  208. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  209. }
  210. {$warning FIX ME!!! pic offset reg conflicts with frame pointer?}
  211. NR_PIC_OFFSET_REG = NR_A5;
  212. { Return address for DWARF }
  213. {$warning TODO just a guess!}
  214. NR_RETURN_ADDRESS_REG = NR_A0;
  215. { Results are returned in this register (32-bit values) }
  216. NR_FUNCTION_RETURN_REG = NR_D0;
  217. RS_FUNCTION_RETURN_REG = RS_D0;
  218. { Low part of 64bit return value }
  219. NR_FUNCTION_RETURN64_LOW_REG = NR_D0;
  220. RS_FUNCTION_RETURN64_LOW_REG = RS_D0;
  221. { High part of 64bit return value }
  222. NR_FUNCTION_RETURN64_HIGH_REG = NR_D1;
  223. RS_FUNCTION_RETURN64_HIGH_REG = RS_D1;
  224. { The value returned from a function is available in this register }
  225. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  226. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  227. { The lowh part of 64bit value returned from a function }
  228. NR_FUNCTION_RESULT64_LOW_REG = NR_FUNCTION_RETURN64_LOW_REG;
  229. RS_FUNCTION_RESULT64_LOW_REG = RS_FUNCTION_RETURN64_LOW_REG;
  230. { The high part of 64bit value returned from a function }
  231. NR_FUNCTION_RESULT64_HIGH_REG = NR_FUNCTION_RETURN64_HIGH_REG;
  232. RS_FUNCTION_RESULT64_HIGH_REG = RS_FUNCTION_RETURN64_HIGH_REG;
  233. {# Floating point results will be placed into this register }
  234. NR_FPU_RESULT_REG = NR_FP0;
  235. {*****************************************************************************
  236. GCC /ABI linking information
  237. *****************************************************************************}
  238. {# Registers which must be saved when calling a routine declared as
  239. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  240. saved should be the ones as defined in the target ABI and / or GCC.
  241. This value can be deduced from CALLED_USED_REGISTERS array in the
  242. GCC source.
  243. }
  244. saved_standard_registers : array[0..5] of tsuperregister = (RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7);
  245. saved_standard_address_registers : array[0..3] of tsuperregister = (RS_A2,RS_A3,RS_A4,RS_A5);
  246. {# Required parameter alignment when calling a routine declared as
  247. stdcall and cdecl. The alignment value should be the one defined
  248. by GCC or the target ABI.
  249. The value of this constant is equal to the constant
  250. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  251. }
  252. std_param_align = 4; { for 32-bit version only }
  253. {*****************************************************************************
  254. CPU Dependent Constants
  255. *****************************************************************************}
  256. {*****************************************************************************
  257. Helpers
  258. *****************************************************************************}
  259. function is_calljmp(o:tasmop):boolean;
  260. procedure inverse_flags(var r : TResFlags);
  261. function flags_to_cond(const f: TResFlags) : TAsmCond;
  262. function cgsize2subreg(s:Tcgsize):Tsubregister;
  263. function reg_cgsize(const reg: tregister): tcgsize;
  264. function findreg_by_number(r:Tregister):tregisterindex;
  265. function std_regnum_search(const s:string):Tregister;
  266. function std_regname(r:Tregister):string;
  267. function isaddressregister(reg : tregister) : boolean;
  268. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  269. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  270. implementation
  271. uses
  272. verbose,
  273. rgbase;
  274. const
  275. std_regname_table : array[tregisterindex] of string[7] = (
  276. {$i r68kstd.inc}
  277. );
  278. regnumber_index : array[tregisterindex] of tregisterindex = (
  279. {$i r68krni.inc}
  280. );
  281. std_regname_index : array[tregisterindex] of tregisterindex = (
  282. {$i r68ksri.inc}
  283. );
  284. {*****************************************************************************
  285. Helpers
  286. *****************************************************************************}
  287. function is_calljmp(o:tasmop):boolean;
  288. begin
  289. is_calljmp := false;
  290. if o in [A_BXX,A_FBXX,A_DBXX,A_BCC..A_BVS,A_DBCC..A_DBVS,A_FBEQ..A_FSNGLE,
  291. A_JSR,A_BSR,A_JMP] then
  292. is_calljmp := true;
  293. end;
  294. procedure inverse_flags(var r: TResFlags);
  295. const flagsinvers : array[F_E..F_BE] of tresflags =
  296. (F_NE,F_E,
  297. F_LE,F_GE,
  298. F_L,F_G,
  299. F_NC,F_C,
  300. F_BE,F_B,
  301. F_AE,F_A);
  302. begin
  303. r:=flagsinvers[r];
  304. end;
  305. function flags_to_cond(const f: TResFlags) : TAsmCond;
  306. const flags2cond: array[tresflags] of tasmcond = (
  307. C_EQ,{F_E equal}
  308. C_NE,{F_NE not equal}
  309. C_GT,{F_G gt signed}
  310. C_LT,{F_L lt signed}
  311. C_GE,{F_GE ge signed}
  312. C_LE,{F_LE le signed}
  313. C_CS,{F_C carry set}
  314. C_CC,{F_NC carry clear}
  315. C_HI,{F_A gt unsigned}
  316. C_CC,{F_AE ge unsigned}
  317. C_CS,{F_B lt unsigned}
  318. C_LS);{F_BE le unsigned}
  319. begin
  320. flags_to_cond := flags2cond[f];
  321. end;
  322. function cgsize2subreg(s:Tcgsize):Tsubregister;
  323. begin
  324. case s of
  325. OS_8,OS_S8:
  326. cgsize2subreg:=R_SUBWHOLE;
  327. OS_16,OS_S16:
  328. cgsize2subreg:=R_SUBWHOLE;
  329. OS_32,OS_S32:
  330. cgsize2subreg:=R_SUBWHOLE;
  331. else
  332. internalerror(200301231);
  333. end;
  334. end;
  335. function reg_cgsize(const reg: tregister): tcgsize;
  336. begin
  337. case getregtype(reg) of
  338. R_ADDRESSREGISTER,
  339. R_INTREGISTER :
  340. result:=OS_32;
  341. R_FPUREGISTER :
  342. result:=OS_F32;
  343. else
  344. internalerror(200303181);
  345. end;
  346. end;
  347. function findreg_by_number(r:Tregister):tregisterindex;
  348. begin
  349. result:=findreg_by_number_table(r,regnumber_index);
  350. end;
  351. function std_regnum_search(const s:string):Tregister;
  352. begin
  353. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  354. end;
  355. function std_regname(r:Tregister):string;
  356. var
  357. p : tregisterindex;
  358. begin
  359. p:=findreg_by_number_table(r,regnumber_index);
  360. if p<>0 then
  361. result:=std_regname_table[p]
  362. else
  363. result:=generic_regname(r);
  364. end;
  365. function isaddressregister(reg : tregister) : boolean;
  366. begin
  367. result:=getregtype(reg)=R_ADDRESSREGISTER;
  368. end;
  369. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  370. const
  371. inverse:array[TAsmCond] of TAsmCond=(C_None,
  372. {$warning TODO, this is just a copy!}
  373. C_CC,C_LS,C_CS,C_LT,C_EQ,C_MI,C_F,C_NE,
  374. C_GE,C_PL,C_GT,C_T,C_HI,C_VC,C_LE,C_VS
  375. );
  376. begin
  377. result := inverse[c];
  378. end;
  379. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  380. begin
  381. result := c1 = c2;
  382. end;
  383. end.