cpubase.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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_none,
  32. a_abcd,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. { mc64040 instructions }
  58. a_move16,
  59. { coldfire v4 instructions }
  60. a_mov3q,a_mvz,a_mvs,a_sats,
  61. { fpu processor instructions - directly supported only. }
  62. { ieee aware and misc. condition codes not supported }
  63. a_fabs,a_fadd,
  64. a_fbeq,a_fbne,a_fbngt,a_fbgt,a_fbge,a_fbnge,
  65. a_fblt,a_fbnlt,a_fble,a_fbgl,a_fbngl,a_fbgle,a_fbngle,
  66. a_fdbeq,a_fdbne,a_fdbgt,a_fdbngt,a_fdbge,a_fdbnge,
  67. a_fdblt,a_fdbnlt,a_fdble,a_fdbgl,a_fdbngl,a_fdbgle,a_fdbngle,
  68. a_fseq,a_fsne,a_fsgt,a_fsngt,a_fsge,a_fsnge,
  69. a_fslt,a_fsnlt,a_fsle,a_fsgl,a_fsngl,a_fsgle,a_fsngle,
  70. a_fcmp,a_fdiv,a_fmove,a_fmovem,
  71. a_fmul,a_fneg,a_fnop,a_fsqrt,a_fsub,a_fsgldiv,
  72. a_fsflmul,a_ftst,
  73. a_ftrapeq,a_ftrapne,a_ftrapgt,a_ftrapngt,a_ftrapge,a_ftrapnge,
  74. a_ftraplt,a_ftrapnlt,a_ftraple,a_ftrapgl,a_ftrapngl,a_ftrapgle,a_ftrapngle,
  75. { protected instructions }
  76. a_cprestore,a_cpsave,
  77. { fpu unit protected instructions }
  78. { and 68030/68851 common mmu instructions }
  79. { (this may include 68040 mmu instructions) }
  80. a_frestore,a_fsave,a_pflush,a_pflusha,a_pload,a_pmove,a_ptest,
  81. { useful for assembly language output }
  82. a_label,a_dbxx,a_sxx,a_bxx,a_fbxx);
  83. {# This should define the array of instructions as string }
  84. op2strtable=array[tasmop] of string[11];
  85. Const
  86. {# First value of opcode enumeration }
  87. firstop = low(tasmop);
  88. {# Last value of opcode enumeration }
  89. lastop = high(tasmop);
  90. {*****************************************************************************
  91. Registers
  92. *****************************************************************************}
  93. type
  94. { Number of registers used for indexing in tables }
  95. tregisterindex=0..{$i r68knor.inc}-1;
  96. const
  97. { Available Superregisters }
  98. {$i r68ksup.inc}
  99. { ? whatever... }
  100. R_SUBWHOLE = R_SUBNONE;
  101. { Available Registers }
  102. {$i r68kcon.inc}
  103. { Integer Super registers first and last }
  104. first_int_imreg = RS_D7+1;
  105. { Float Super register first and last }
  106. first_fpu_imreg = RS_FP7+1;
  107. { Integer Super registers first and last }
  108. first_addr_imreg = RS_SP+1;
  109. { MM Super register first and last }
  110. first_mm_supreg = 0;
  111. first_mm_imreg = 0;
  112. maxfpuregs = 8;
  113. { TODO: FIX BSSTART}
  114. regnumber_count_bsstart = 16;
  115. regnumber_table : array[tregisterindex] of tregister = (
  116. {$i r68knum.inc}
  117. );
  118. regstabs_table : array[tregisterindex] of shortint = (
  119. {$i r68ksta.inc}
  120. );
  121. regdwarf_table : array[tregisterindex] of shortint = (
  122. { TODO: reused stabs values!}
  123. {$i r68ksta.inc}
  124. );
  125. { registers which may be destroyed by calls }
  126. VOLATILE_INTREGISTERS = [RS_D0,RS_D1];
  127. VOLATILE_FPUREGISTERS = [];
  128. VOLATILE_ADDRESSREGISTERS = [RS_A0,RS_A1];
  129. type
  130. totherregisterset = set of tregisterindex;
  131. {*****************************************************************************
  132. Conditions
  133. *****************************************************************************}
  134. type
  135. TAsmCond=(C_None,
  136. C_CC,C_LS,C_CS,C_LT,C_EQ,C_MI,C_F,C_NE,
  137. C_GE,C_PL,C_GT,C_T,C_HI,C_VC,C_LE,C_VS
  138. );
  139. const
  140. cond2str:array[TAsmCond] of string[3]=('',
  141. 'cc','ls','cs','lt','eq','mi','f','ne',
  142. 'ge','pl','gt','t','hi','vc','le','vs'
  143. );
  144. {*****************************************************************************
  145. Flags
  146. *****************************************************************************}
  147. type
  148. TResFlags = (
  149. F_E,F_NE,
  150. F_G,F_L,F_GE,F_LE,F_C,F_NC,F_A,F_AE,F_B,F_BE);
  151. {*****************************************************************************
  152. Reference
  153. *****************************************************************************}
  154. type
  155. { direction of address register : }
  156. { (An) (An)+ -(An) }
  157. tdirection = (dir_none,dir_inc,dir_dec);
  158. {*****************************************************************************
  159. Operand Sizes
  160. *****************************************************************************}
  161. { S_NO = No Size of operand }
  162. { S_B = 8-bit size operand }
  163. { S_W = 16-bit size operand }
  164. { S_L = 32-bit size operand }
  165. { Floating point types }
  166. { S_FS = single type (32 bit) }
  167. { S_FD = double/64bit integer }
  168. { S_FX = Extended type }
  169. topsize = (S_NO,S_B,S_W,S_L,S_FS,S_FD,S_FX,S_IQ);
  170. {*****************************************************************************
  171. Constants
  172. *****************************************************************************}
  173. const
  174. {# maximum number of operands in assembler instruction }
  175. max_operands = 4;
  176. {*****************************************************************************
  177. Default generic sizes
  178. *****************************************************************************}
  179. {# Defines the default address size for a processor, }
  180. OS_ADDR = OS_32;
  181. {# the natural int size for a processor,
  182. has to match osuinttype/ossinttype as initialized in psystem }
  183. OS_INT = OS_32;
  184. OS_SINT = OS_S32;
  185. {# the maximum float size for a processor, }
  186. OS_FLOAT = OS_F64;
  187. {# the size of a vector register for a processor }
  188. OS_VECTOR = OS_M128;
  189. {*****************************************************************************
  190. GDB Information
  191. *****************************************************************************}
  192. {# Register indexes for stabs information, when some
  193. parameters or variables are stored in registers.
  194. Taken from m68kelf.h (DBX_REGISTER_NUMBER)
  195. from GCC 3.x source code.
  196. This is not compatible with the m68k-sun
  197. implementation.
  198. }
  199. stab_regindex : array[tregisterindex] of shortint =
  200. (
  201. {$i r68ksta.inc}
  202. );
  203. {*****************************************************************************
  204. Generic Register names
  205. *****************************************************************************}
  206. {# Stack pointer register }
  207. NR_STACK_POINTER_REG = NR_SP;
  208. RS_STACK_POINTER_REG = RS_SP;
  209. {# Frame pointer register }
  210. { Frame pointer register (initialized in tm68kprocinfo.init_framepointer) }
  211. RS_FRAME_POINTER_REG: tsuperregister = RS_NO;
  212. NR_FRAME_POINTER_REG: tregister = NR_NO;
  213. {# Register for addressing absolute data in a position independant way,
  214. such as in PIC code. The exact meaning is ABI specific. For
  215. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  216. }
  217. { TODO: FIX ME!!! pic offset reg conflicts with frame pointer?}
  218. NR_PIC_OFFSET_REG = NR_A5;
  219. { Return address for DWARF }
  220. { TODO: just a guess!}
  221. NR_RETURN_ADDRESS_REG = NR_A0;
  222. { Results are returned in this register (32-bit values) }
  223. NR_FUNCTION_RETURN_REG = NR_D0;
  224. RS_FUNCTION_RETURN_REG = RS_D0;
  225. { Low part of 64bit return value }
  226. NR_FUNCTION_RETURN64_LOW_REG = NR_D0;
  227. RS_FUNCTION_RETURN64_LOW_REG = RS_D0;
  228. { High part of 64bit return value }
  229. NR_FUNCTION_RETURN64_HIGH_REG = NR_D1;
  230. RS_FUNCTION_RETURN64_HIGH_REG = RS_D1;
  231. { The value returned from a function is available in this register }
  232. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  233. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  234. { The lowh part of 64bit value returned from a function }
  235. NR_FUNCTION_RESULT64_LOW_REG = NR_FUNCTION_RETURN64_LOW_REG;
  236. RS_FUNCTION_RESULT64_LOW_REG = RS_FUNCTION_RETURN64_LOW_REG;
  237. { The high part of 64bit value returned from a function }
  238. NR_FUNCTION_RESULT64_HIGH_REG = NR_FUNCTION_RETURN64_HIGH_REG;
  239. RS_FUNCTION_RESULT64_HIGH_REG = RS_FUNCTION_RETURN64_HIGH_REG;
  240. {# Floating point results will be placed into this register }
  241. NR_FPU_RESULT_REG = NR_FP0;
  242. NR_DEFAULTFLAGS = NR_SR;
  243. RS_DEFAULTFLAGS = RS_SR;
  244. {*****************************************************************************
  245. GCC /ABI linking information
  246. *****************************************************************************}
  247. {# Registers which must be saved when calling a routine declared as
  248. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  249. saved should be the ones as defined in the target ABI and / or GCC.
  250. This value can be deduced from CALLED_USED_REGISTERS array in the
  251. GCC source.
  252. }
  253. saved_standard_registers : array[0..5] of tsuperregister = (RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7);
  254. saved_address_registers : array[0..4] of tsuperregister = (RS_A2,RS_A3,RS_A4,RS_A5,RS_A6);
  255. { this is only for the generic code which is not used for this architecture }
  256. saved_mm_registers : array[0..0] of tsuperregister = (RS_INVALID);
  257. {# Required parameter alignment when calling a routine declared as
  258. stdcall and cdecl. The alignment value should be the one defined
  259. by GCC or the target ABI.
  260. The value of this constant is equal to the constant
  261. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  262. }
  263. std_param_align = 4; { for 32-bit version only }
  264. {*****************************************************************************
  265. CPU Dependent Constants
  266. *****************************************************************************}
  267. {*****************************************************************************
  268. Helpers
  269. *****************************************************************************}
  270. const
  271. tcgsize2opsize: Array[tcgsize] of topsize =
  272. (S_NO,S_B,S_W,S_L,S_L,S_NO,S_B,S_W,S_L,S_L,S_NO,
  273. S_FS,S_FD,S_FX,S_NO,S_NO,
  274. S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,
  275. S_NO,S_NO,S_NO,S_NO,S_NO,S_NO);
  276. function is_calljmp(o:tasmop):boolean;
  277. procedure inverse_flags(var r : TResFlags);
  278. function flags_to_cond(const f: TResFlags) : TAsmCond;
  279. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  280. function reg_cgsize(const reg: tregister): tcgsize;
  281. function findreg_by_number(r:Tregister):tregisterindex;
  282. function std_regnum_search(const s:string):Tregister;
  283. function std_regname(r:Tregister):string;
  284. function isaddressregister(reg : tregister) : boolean;
  285. function isintregister(reg : tregister) : boolean;
  286. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  287. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  288. function dwarf_reg(r:tregister):shortint;
  289. implementation
  290. uses
  291. verbose,
  292. rgbase;
  293. const
  294. std_regname_table : TRegNameTable = (
  295. {$i r68kstd.inc}
  296. );
  297. regnumber_index : array[tregisterindex] of tregisterindex = (
  298. {$i r68krni.inc}
  299. );
  300. std_regname_index : array[tregisterindex] of tregisterindex = (
  301. {$i r68ksri.inc}
  302. );
  303. {*****************************************************************************
  304. Helpers
  305. *****************************************************************************}
  306. function is_calljmp(o:tasmop):boolean;
  307. begin
  308. is_calljmp := false;
  309. if o in [A_BXX,A_FBXX,A_DBXX,A_BCC..A_BVS,A_DBCC..A_DBVS,A_FBEQ..A_FSNGLE,
  310. A_JSR,A_BSR,A_JMP] then
  311. is_calljmp := true;
  312. end;
  313. procedure inverse_flags(var r: TResFlags);
  314. const flagsinvers : array[F_E..F_BE] of tresflags =
  315. (F_NE,F_E,
  316. F_LE,F_GE,
  317. F_L,F_G,
  318. F_NC,F_C,
  319. F_BE,F_B,
  320. F_AE,F_A);
  321. begin
  322. r:=flagsinvers[r];
  323. end;
  324. function flags_to_cond(const f: TResFlags) : TAsmCond;
  325. const flags2cond: array[tresflags] of tasmcond = (
  326. C_EQ,{F_E equal}
  327. C_NE,{F_NE not equal}
  328. C_GT,{F_G gt signed}
  329. C_LT,{F_L lt signed}
  330. C_GE,{F_GE ge signed}
  331. C_LE,{F_LE le signed}
  332. C_CS,{F_C carry set}
  333. C_CC,{F_NC carry clear}
  334. C_HI,{F_A gt unsigned}
  335. C_CC,{F_AE ge unsigned}
  336. C_CS,{F_B lt unsigned}
  337. C_LS);{F_BE le unsigned}
  338. begin
  339. flags_to_cond := flags2cond[f];
  340. end;
  341. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  342. var p: pointer;
  343. begin
  344. case s of
  345. OS_NO: begin
  346. { TODO: FIX ME!!! results in bad code generation}
  347. cgsize2subreg:=R_SUBWHOLE;
  348. end;
  349. OS_8,OS_S8:
  350. cgsize2subreg:=R_SUBWHOLE;
  351. OS_16,OS_S16:
  352. cgsize2subreg:=R_SUBWHOLE;
  353. OS_32,OS_S32:
  354. cgsize2subreg:=R_SUBWHOLE;
  355. OS_64,OS_S64:
  356. begin
  357. cgsize2subreg:=R_SUBWHOLE;
  358. end;
  359. OS_F32 :
  360. cgsize2subreg:=R_SUBFS;
  361. OS_F64 :
  362. cgsize2subreg:=R_SUBFD;
  363. {
  364. begin
  365. // is this correct? (KB)
  366. cgsize2subreg:=R_SUBNONE;
  367. end;
  368. }
  369. else begin
  370. // this supposed to be debug
  371. // p:=nil; dword(p^):=0;
  372. // internalerror(200301231);
  373. cgsize2subreg:=R_SUBWHOLE;
  374. end;
  375. end;
  376. end;
  377. function reg_cgsize(const reg: tregister): tcgsize;
  378. begin
  379. case getregtype(reg) of
  380. R_ADDRESSREGISTER,
  381. R_INTREGISTER :
  382. result:=OS_32;
  383. R_FPUREGISTER :
  384. result:=OS_F64;
  385. else
  386. internalerror(200303181);
  387. end;
  388. end;
  389. function findreg_by_number(r:Tregister):tregisterindex;
  390. begin
  391. result:=findreg_by_number_table(r,regnumber_index);
  392. end;
  393. function std_regnum_search(const s:string):Tregister;
  394. begin
  395. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  396. end;
  397. function std_regname(r:Tregister):string;
  398. var
  399. p : tregisterindex;
  400. begin
  401. p:=findreg_by_number_table(r,regnumber_index);
  402. if p<>0 then
  403. result:=std_regname_table[p]
  404. else
  405. result:=generic_regname(r);
  406. end;
  407. function isaddressregister(reg : tregister) : boolean;
  408. begin
  409. result:=getregtype(reg)=R_ADDRESSREGISTER;
  410. end;
  411. function isintregister(reg : tregister) : boolean;
  412. begin
  413. result:=getregtype(reg)=R_INTREGISTER;
  414. end;
  415. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  416. const
  417. inverse:array[TAsmCond] of TAsmCond=(C_None,
  418. { TODO: TODO, this is just a copy!}
  419. C_CC,C_LS,C_CS,C_LT,C_EQ,C_MI,C_F,C_NE,
  420. C_GE,C_PL,C_GT,C_T,C_HI,C_VC,C_LE,C_VS
  421. );
  422. begin
  423. result := inverse[c];
  424. end;
  425. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  426. begin
  427. result := c1 = c2;
  428. end;
  429. function dwarf_reg(r:tregister):shortint;
  430. begin
  431. result:=regdwarf_table[findreg_by_number(r)];
  432. if result=-1 then
  433. internalerror(200603251);
  434. end;
  435. end.