cpubase.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. 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. { No Subregisters }
  96. R_SUBWHOLE = R_SUBNONE;
  97. { Available Registers }
  98. {$i r68kcon.inc}
  99. { Integer Super registers first and last }
  100. first_int_supreg = RS_SP;
  101. first_int_imreg = RS_SP+1;
  102. { Float Super register first and last }
  103. first_fpu_supreg = RS_FP7;
  104. first_fpu_imreg = RS_FP7+1;
  105. { MM Super register first and last }
  106. first_mm_supreg = 0;
  107. first_mm_imreg = 0;
  108. regnumber_count_bsstart = 64;
  109. regnumber_table : array[tregisterindex] of tregister = (
  110. {$i r68knum.inc}
  111. );
  112. regstabs_table : array[tregisterindex] of shortint = (
  113. {$i r68ksta.inc}
  114. );
  115. { registers which may be destroyed by calls }
  116. VOLATILE_INTREGISTERS = [];
  117. VOLATILE_FPUREGISTERS = [];
  118. type
  119. totherregisterset = set of tregisterindex;
  120. {*****************************************************************************
  121. Conditions
  122. *****************************************************************************}
  123. type
  124. TAsmCond=(C_None,
  125. C_CC,C_LS,C_CS,C_LT,C_EQ,C_MI,C_F,C_NE,
  126. C_GE,C_PL,C_GT,C_T,C_HI,C_VC,C_LE,C_VS
  127. );
  128. const
  129. cond2str:array[TAsmCond] of string[3]=('',
  130. 'cc','ls','cs','lt','eq','mi','f','ne',
  131. 'ge','pl','gt','t','hi','vc','le','vs'
  132. );
  133. {*****************************************************************************
  134. Flags
  135. *****************************************************************************}
  136. type
  137. TResFlags = (
  138. F_E,F_NE,
  139. F_G,F_L,F_GE,F_LE,F_C,F_NC,F_A,F_AE,F_B,F_BE);
  140. {*****************************************************************************
  141. Reference
  142. *****************************************************************************}
  143. type
  144. trefoptions=(ref_none,ref_parafixup,ref_localfixup,ref_selffixup);
  145. { direction of address register : }
  146. { (An) (An)+ -(An) }
  147. tdirection = (dir_none,dir_inc,dir_dec);
  148. { reference record }
  149. preference = ^treference;
  150. treference = packed record
  151. base,
  152. index : tregister;
  153. scalefactor : byte;
  154. offset : longint;
  155. symbol : tasmsymbol;
  156. { symbol the symbol of this reference is relative to, nil if none }
  157. relsymbol : tasmsymbol;
  158. { reference type addr or symbol itself }
  159. refaddr : trefaddr;
  160. options : trefoptions;
  161. { indexed increment and decrement mode }
  162. { (An)+ and -(An) }
  163. direction : tdirection;
  164. end;
  165. { reference record }
  166. pparareference = ^tparareference;
  167. tparareference = record
  168. offset : longint;
  169. index : tregister;
  170. end;
  171. {*****************************************************************************
  172. Generic Location
  173. *****************************************************************************}
  174. type
  175. { tparamlocation describes where a parameter for a procedure is stored.
  176. References are given from the caller's point of view. The usual
  177. TLocation isn't used, because contains a lot of unnessary fields.
  178. }
  179. tparalocation = record
  180. size : TCGSize;
  181. loc : TCGLoc;
  182. lochigh : TCGLoc;
  183. alignment : byte;
  184. case TCGLoc of
  185. LOC_REFERENCE : (reference : tparareference);
  186. { segment in reference at the same place as in loc_register }
  187. LOC_REGISTER,LOC_CREGISTER : (
  188. case longint of
  189. 1 : (register,registerhigh : tregister);
  190. { overlay a registerlow }
  191. 2 : (registerlow : tregister);
  192. { overlay a 64 Bit register type }
  193. 3 : (reg64 : tregister64);
  194. 4 : (register64 : tregister64);
  195. );
  196. end;
  197. tlocation = record
  198. loc : TCGLoc;
  199. size : TCGSize;
  200. case TCGLoc of
  201. LOC_FLAGS : (resflags : tresflags);
  202. LOC_CONSTANT : (
  203. case longint of
  204. 1 : (value : AWord);
  205. { can't do this, this layout depends on the host cpu. Use }
  206. { lo(valueqword)/hi(valueqword) instead (JM) }
  207. { 2 : (valuelow, valuehigh:AWord); }
  208. { overlay a complete 64 Bit value }
  209. 3 : (valueqword : qword);
  210. );
  211. LOC_CREFERENCE,
  212. LOC_REFERENCE : (reference : treference);
  213. { segment in reference at the same place as in loc_register }
  214. LOC_REGISTER,LOC_CREGISTER : (
  215. case longint of
  216. 1 : (register,registerhigh,segment : tregister);
  217. { overlay a registerlow }
  218. 2 : (registerlow : tregister);
  219. { overlay a 64 Bit register type }
  220. 3 : (reg64 : tregister64);
  221. 4 : (register64 : tregister64);
  222. );
  223. end;
  224. {*****************************************************************************
  225. Operand Sizes
  226. *****************************************************************************}
  227. { S_NO = No Size of operand }
  228. { S_B = 8-bit size operand }
  229. { S_W = 16-bit size operand }
  230. { S_L = 32-bit size operand }
  231. { Floating point types }
  232. { S_FS = single type (32 bit) }
  233. { S_FD = double/64bit integer }
  234. { S_FX = Extended type }
  235. topsize = (S_NO,S_B,S_W,S_L,S_FS,S_FD,S_FX,S_IQ);
  236. {*****************************************************************************
  237. Constants
  238. *****************************************************************************}
  239. const
  240. {# maximum number of operands in assembler instruction }
  241. max_operands = 4;
  242. {*****************************************************************************
  243. Default generic sizes
  244. *****************************************************************************}
  245. {# Defines the default address size for a processor, }
  246. OS_ADDR = OS_32;
  247. {# the natural int size for a processor, }
  248. OS_INT = OS_32;
  249. {# the maximum float size for a processor, }
  250. OS_FLOAT = OS_F64;
  251. {# the size of a vector register for a processor }
  252. OS_VECTOR = OS_M128;
  253. {*****************************************************************************
  254. GDB Information
  255. *****************************************************************************}
  256. {# Register indexes for stabs information, when some
  257. parameters or variables are stored in registers.
  258. Taken from m68kelf.h (DBX_REGISTER_NUMBER)
  259. from GCC 3.x source code.
  260. This is not compatible with the m68k-sun
  261. implementation.
  262. }
  263. stab_regindex : array[tregisterindex] of shortint =
  264. (
  265. {$i r68ksta.inc}
  266. );
  267. {*****************************************************************************
  268. Generic Register names
  269. *****************************************************************************}
  270. {# Stack pointer register }
  271. NR_STACK_POINTER_REG = NR_SP;
  272. RS_STACK_POINTER_REG = RS_SP;
  273. {# Frame pointer register }
  274. NR_FRAME_POINTER_REG = NR_A6;
  275. RS_FRAME_POINTER_REG = RS_A6;
  276. {# Register for addressing absolute data in a position independant way,
  277. such as in PIC code. The exact meaning is ABI specific. For
  278. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  279. }
  280. NR_PIC_OFFSET_REG = NR_A5;
  281. { Results are returned in this register (32-bit values) }
  282. NR_FUNCTION_RETURN_REG = NR_D0;
  283. RS_FUNCTION_RETURN_REG = NR_D0;
  284. { Low part of 64bit return value }
  285. NR_FUNCTION_RETURN64_LOW_REG = NR_D0;
  286. RS_FUNCTION_RETURN64_LOW_REG = RS_D0;
  287. { High part of 64bit return value }
  288. NR_FUNCTION_RETURN64_HIGH_REG = NR_D1;
  289. RS_FUNCTION_RETURN64_HIGH_REG = RS_D1;
  290. { The value returned from a function is available in this register }
  291. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  292. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  293. { The lowh part of 64bit value returned from a function }
  294. NR_FUNCTION_RESULT64_LOW_REG = NR_FUNCTION_RETURN64_LOW_REG;
  295. RS_FUNCTION_RESULT64_LOW_REG = RS_FUNCTION_RETURN64_LOW_REG;
  296. { The high part of 64bit value returned from a function }
  297. NR_FUNCTION_RESULT64_HIGH_REG = NR_FUNCTION_RETURN64_HIGH_REG;
  298. RS_FUNCTION_RESULT64_HIGH_REG = RS_FUNCTION_RETURN64_HIGH_REG;
  299. {# Floating point results will be placed into this register }
  300. NR_FPU_RESULT_REG = NR_FP0;
  301. {*****************************************************************************
  302. GCC /ABI linking information
  303. *****************************************************************************}
  304. {# Registers which must be saved when calling a routine declared as
  305. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  306. saved should be the ones as defined in the target ABI and / or GCC.
  307. This value can be deduced from CALLED_USED_REGISTERS array in the
  308. GCC source.
  309. }
  310. std_saved_registers = [RS_D2..RS_D7,RS_A2..RS_A5];
  311. {# Required parameter alignment when calling a routine declared as
  312. stdcall and cdecl. The alignment value should be the one defined
  313. by GCC or the target ABI.
  314. The value of this constant is equal to the constant
  315. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  316. }
  317. std_param_align = 4; { for 32-bit version only }
  318. {*****************************************************************************
  319. CPU Dependent Constants
  320. *****************************************************************************}
  321. {*****************************************************************************
  322. Helpers
  323. *****************************************************************************}
  324. function is_calljmp(o:tasmop):boolean;
  325. procedure inverse_flags(var r : TResFlags);
  326. function flags_to_cond(const f: TResFlags) : TAsmCond;
  327. function cgsize2subreg(s:Tcgsize):Tsubregister;
  328. function findreg_by_number(r:Tregister):tregisterindex;
  329. function std_regnum_search(const s:string):Tregister;
  330. function std_regname(r:Tregister):string;
  331. function isaddressregister(reg : tregister) : boolean;
  332. implementation
  333. uses
  334. verbose,
  335. rgbase;
  336. const
  337. std_regname_table : array[tregisterindex] of string[7] = (
  338. {$i r68kstd.inc}
  339. );
  340. regnumber_index : array[tregisterindex] of tregisterindex = (
  341. {$i r68krni.inc}
  342. );
  343. std_regname_index : array[tregisterindex] of tregisterindex = (
  344. {$i r68ksri.inc}
  345. );
  346. {*****************************************************************************
  347. Helpers
  348. *****************************************************************************}
  349. function is_calljmp(o:tasmop):boolean;
  350. begin
  351. is_calljmp := false;
  352. if o in [A_BXX,A_FBXX,A_DBXX,A_BCC..A_BVS,A_DBCC..A_DBVS,A_FBEQ..A_FSNGLE,
  353. A_JSR,A_BSR,A_JMP] then
  354. is_calljmp := true;
  355. end;
  356. procedure inverse_flags(var r: TResFlags);
  357. const flagsinvers : array[F_E..F_BE] of tresflags =
  358. (F_NE,F_E,
  359. F_LE,F_GE,
  360. F_L,F_G,
  361. F_NC,F_C,
  362. F_BE,F_B,
  363. F_AE,F_A);
  364. begin
  365. r:=flagsinvers[r];
  366. end;
  367. function flags_to_cond(const f: TResFlags) : TAsmCond;
  368. const flags2cond: array[tresflags] of tasmcond = (
  369. C_EQ,{F_E equal}
  370. C_NE,{F_NE not equal}
  371. C_GT,{F_G gt signed}
  372. C_LT,{F_L lt signed}
  373. C_GE,{F_GE ge signed}
  374. C_LE,{F_LE le signed}
  375. C_CS,{F_C carry set}
  376. C_CC,{F_NC carry clear}
  377. C_HI,{F_A gt unsigned}
  378. C_CC,{F_AE ge unsigned}
  379. C_CS,{F_B lt unsigned}
  380. C_LS);{F_BE le unsigned}
  381. begin
  382. flags_to_cond := flags2cond[f];
  383. end;
  384. function cgsize2subreg(s:Tcgsize):Tsubregister;
  385. begin
  386. case s of
  387. OS_8,OS_S8:
  388. cgsize2subreg:=R_SUBL;
  389. OS_16,OS_S16:
  390. cgsize2subreg:=R_SUBW;
  391. OS_32,OS_S32:
  392. cgsize2subreg:=R_SUBD;
  393. else
  394. internalerror(200301231);
  395. end;
  396. end;
  397. function findreg_by_number(r:Tregister):tregisterindex;
  398. begin
  399. result:=findreg_by_number_table(r,regnumber_index);
  400. end;
  401. function std_regnum_search(const s:string):Tregister;
  402. begin
  403. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  404. end;
  405. function std_regname(r:Tregister):string;
  406. var
  407. p : tregisterindex;
  408. begin
  409. p:=findreg_by_number_table(r,regnumber_index);
  410. if p<>0 then
  411. result:=std_regname_table[p]
  412. else
  413. result:=generic_regname(r);
  414. end;
  415. function isaddressregister(reg : tregister) : boolean;
  416. begin
  417. result:=getregtype(reg)=R_ADDRESSREGISTER;
  418. end;
  419. end.
  420. {
  421. $Log$
  422. Revision 1.25 2004-04-18 21:13:59 florian
  423. * more adaptions for m68k
  424. Revision 1.24 2004/01/30 12:17:18 florian
  425. * fixed some m68k compilation problems
  426. Revision 1.23 2003/08/17 16:59:20 jonas
  427. * fixed regvars so they work with newra (at least for ppc)
  428. * fixed some volatile register bugs
  429. + -dnotranslation option for -dnewra, which causes the registers not to
  430. be translated from virtual to normal registers. Requires support in
  431. the assembler writer as well, which is only implemented in aggas/
  432. agppcgas currently
  433. Revision 1.22 2003/06/17 16:34:44 jonas
  434. * lots of newra fixes (need getfuncretparaloc implementation for i386)!
  435. * renamed all_intregisters to volatile_intregisters and made it
  436. processor dependent
  437. Revision 1.21 2003/06/03 13:01:59 daniel
  438. * Register allocator finished
  439. Revision 1.20 2003/04/23 13:40:33 peter
  440. * fix m68k compile
  441. Revision 1.19 2003/04/23 12:35:35 florian
  442. * fixed several issues with powerpc
  443. + applied a patch from Jonas for nested function calls (PowerPC only)
  444. * ...
  445. Revision 1.18 2003/02/19 22:00:16 daniel
  446. * Code generator converted to new register notation
  447. - Horribily outdated todo.txt removed
  448. Revision 1.17 2003/02/02 19:25:54 carl
  449. * Several bugfixes for m68k target (register alloc., opcode emission)
  450. + VIS target
  451. + Generic add more complete (still not verified)
  452. Revision 1.16 2003/01/09 15:49:56 daniel
  453. * Added register conversion
  454. Revision 1.15 2003/01/08 18:43:57 daniel
  455. * Tregister changed into a record
  456. Revision 1.14 2002/11/30 23:33:03 carl
  457. * merges from Pierre's fixes in m68k fixes branch
  458. Revision 1.13 2002/11/17 18:26:16 mazen
  459. * fixed a compilation bug accmulator-->accumulator, in definition of return_result_reg
  460. Revision 1.12 2002/11/17 17:49:09 mazen
  461. + return_result_reg and function_result_reg are now used, in all plateforms, to pass functions result between called function and its caller. See the explanation of each one
  462. Revision 1.11 2002/10/14 16:32:36 carl
  463. + flag_2_cond implemented
  464. Revision 1.10 2002/08/18 09:02:12 florian
  465. * fixed compilation problems
  466. Revision 1.9 2002/08/15 08:13:54 carl
  467. - a_load_sym_ofs_reg removed
  468. * loadvmt now calls loadaddr_ref_reg instead
  469. Revision 1.8 2002/08/14 18:41:47 jonas
  470. - remove valuelow/valuehigh fields from tlocation, because they depend
  471. on the endianess of the host operating system -> difficult to get
  472. right. Use lo/hi(location.valueqword) instead (remember to use
  473. valueqword and not value!!)
  474. Revision 1.7 2002/08/13 21:40:58 florian
  475. * more fixes for ppc calling conventions
  476. Revision 1.6 2002/08/13 18:58:54 carl
  477. + m68k problems with cvs fixed?()!
  478. Revision 1.4 2002/08/12 15:08:44 carl
  479. + stab register indexes for powerpc (moved from gdb to cpubase)
  480. + tprocessor enumeration moved to cpuinfo
  481. + linker in target_info is now a class
  482. * many many updates for m68k (will soon start to compile)
  483. - removed some ifdef or correct them for correct cpu
  484. Revision 1.3 2002/07/29 17:51:32 carl
  485. + restart m68k support
  486. }