aasmcpu.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. {
  2. Copyright (c) 1998-2001 by Florian Klaempfl and Pierre Muller
  3. m68k family assembler instructions
  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 aasmcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,aasmtai,aasmdata,aasmsym,
  22. aasmbase,globals,verbose,symtype,
  23. cpubase,cpuinfo,cgbase,cgutils;
  24. const
  25. { "mov reg,reg" source operand number }
  26. O_MOV_SOURCE = 0;
  27. { "mov reg,reg" source operand number }
  28. O_MOV_DEST = 1;
  29. type
  30. taicpu = class(tai_cpu_abstract_sym)
  31. opsize : topsize;
  32. procedure loadregset(opidx:longint; const dataregs,addrregs:tcpuregisterset);
  33. constructor op_none(op : tasmop);
  34. constructor op_none(op : tasmop;_size : topsize);
  35. constructor op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  36. constructor op_const(op : tasmop;_size : topsize;_op1 : longint);
  37. constructor op_ref(op : tasmop;_size : topsize;_op1 : treference);
  38. constructor op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  39. constructor op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;_op2 : treference);
  40. constructor op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: longint);
  41. constructor op_const_reg(op : tasmop;_size : topsize;_op1 : longint;_op2 : tregister);
  42. constructor op_const_const(op : tasmop;_size : topsize;_op1,_op2 : longint);
  43. constructor op_const_ref(op : tasmop;_size : topsize;_op1 : longint;_op2 : treference);
  44. constructor op_ref_reg(op : tasmop;_size : topsize;_op1 : treference;_op2 : tregister);
  45. { this is only allowed if _op1 is an int value (_op1^.isintvalue=true) }
  46. constructor op_ref_ref(op : tasmop;_size : topsize;_op1,_op2 : treference);
  47. constructor op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  48. constructor op_const_reg_reg(op : tasmop;_size : topsize;_op1 : longint;_op2 : tregister;_op3 : tregister);
  49. constructor op_const_ref_reg(op : tasmop;_size : topsize;_op1 : longint;_op2 : treference;_op3 : tregister);
  50. constructor op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister; _op3 : treference);
  51. constructor op_const_reg_ref(op : tasmop;_size : topsize;_op1 : longint;_op2 : tregister;_op3 : treference);
  52. constructor op_reg_regset(op: tasmop; _size : topsize; _op1: tregister;const _op2data,_op2addr: tcpuregisterset);
  53. constructor op_regset_reg(op: tasmop; _size : topsize;const _op1data,_op1addr: tcpuregisterset; _op2: tregister);
  54. constructor op_ref_regset(op: tasmop; _size : topsize; _op1: treference;const _op2data,_op2addr: tcpuregisterset);
  55. constructor op_regset_ref(op: tasmop; _size : topsize;const _op1data,_op1addr: tcpuregisterset; _op2: treference);
  56. { this is for Jmp instructions }
  57. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  58. constructor op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  59. { for DBxx opcodes }
  60. constructor op_reg_sym(op: tasmop; _size : topsize; _op1: tregister; _op2 :tasmsymbol);
  61. constructor op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  62. constructor op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  63. constructor op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  64. function is_same_reg_move(regtype: Tregistertype):boolean;override;
  65. function spilling_get_operation_type(opnr: longint): topertype;override;
  66. private
  67. procedure init(_size : topsize); { this need to be called by all constructor }
  68. end;
  69. tai_align = class(tai_align_abstract)
  70. { nothing to add }
  71. end;
  72. procedure InitAsm;
  73. procedure DoneAsm;
  74. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  75. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  76. implementation
  77. uses
  78. globtype;
  79. { TODO: FIX ME!! useful for debug, remove it, same table as in ag68kgas }
  80. const
  81. gas_op2str:op2strtable=
  82. { warning: CPU32 opcodes are not fully compatible with the MC68020. }
  83. { 68000 only opcodes }
  84. ( '',
  85. 'abcd','add','adda','addi','addq','addx','and','andi',
  86. 'asl','asr','bcc','bcs','beq','bge','bgt','bhi',
  87. 'ble','bls','blt','bmi','bne','bpl','bvc','bvs',
  88. 'bchg','bclr','bra','bset','bsr','btst','chk',
  89. 'clr','cmp','cmpa','cmpi','cmpm','dbcc','dbcs','dbeq','dbge',
  90. 'dbgt','dbhi','dble','dbls','dblt','dbmi','dbne','dbra',
  91. 'dbpl','dbt','dbvc','dbvs','dbf','divs','divu',
  92. 'eor','eori','exg','illegal','ext','jmp','jsr',
  93. 'lea','link','lsl','lsr','move','movea','movei','moveq',
  94. 'movem','movep','muls','mulu','nbcd','neg','negx',
  95. 'nop','not','or','ori','pea','rol','ror','roxl',
  96. 'roxr','rtr','rts','sbcd','scc','scs','seq','sge',
  97. 'sgt','shi','sle','sls','slt','smi','sne',
  98. 'spl','st','svc','svs','sf','sub','suba','subi','subq',
  99. 'subx','swap','tas','trap','trapv','tst','unlk',
  100. 'rte','reset','stop',
  101. { mc68010 instructions }
  102. 'bkpt','movec','moves','rtd',
  103. { mc68020 instructions }
  104. 'bfchg','bfclr','bfexts','bfextu','bfffo',
  105. 'bfins','bfset','bftst','callm','cas','cas2',
  106. 'chk2','cmp2','divsl','divul','extb','pack','rtm',
  107. 'trapcc','tracs','trapeq','trapf','trapge','trapgt',
  108. 'traphi','traple','trapls','traplt','trapmi','trapne',
  109. 'trappl','trapt','trapvc','trapvs','unpk',
  110. { fpu processor instructions - directly supported only. }
  111. { ieee aware and misc. condition codes not supported }
  112. 'fabs','fadd',
  113. 'fbeq','fbne','fbngt','fbgt','fbge','fbnge',
  114. 'fblt','fbnlt','fble','fbgl','fbngl','fbgle','fbngle',
  115. 'fdbeq','fdbne','fdbgt','fdbngt','fdbge','fdbnge',
  116. 'fdblt','fdbnlt','fdble','fdbgl','fdbngl','fdbgle','fdbngle',
  117. 'fseq','fsne','fsgt','fsngt','fsge','fsnge',
  118. 'fslt','fsnlt','fsle','fsgl','fsngl','fsgle','fsngle',
  119. 'fcmp','fdiv','fmove','fmovem',
  120. 'fmul','fneg','fnop','fsqrt','fsub','fsgldiv',
  121. 'fsflmul','ftst',
  122. 'ftrapeq','ftrapne','ftrapgt','ftrapngt','ftrapge','ftrapnge',
  123. 'ftraplt','ftrapnlt','ftraple','ftrapgl','ftrapngl','ftrapgle','ftrapngle',
  124. { protected instructions }
  125. 'cprestore','cpsave',
  126. { fpu unit protected instructions }
  127. { and 68030/68851 common mmu instructions }
  128. { (this may include 68040 mmu instructions) }
  129. 'frestore','fsave','pflush','pflusha','pload','pmove','ptest',
  130. { useful for assembly language output }
  131. 'label','db','s','b','fb');
  132. {*****************************************************************************
  133. Taicpu Constructors
  134. *****************************************************************************}
  135. procedure taicpu.loadregset(opidx:longint; const dataregs,addrregs:tcpuregisterset);
  136. var
  137. i : byte;
  138. begin
  139. allocate_oper(opidx+1);
  140. with oper[opidx]^ do
  141. begin
  142. if typ<>top_regset then
  143. clearop(opidx);
  144. new(dataregset);
  145. new(addrregset);
  146. dataregset^:=dataregs;
  147. addrregset^:=addrregs;
  148. typ:=top_regset;
  149. for i:=RS_D0 to RS_D7 do
  150. begin
  151. if assigned(add_reg_instruction_hook) and (i in dataregset^) then
  152. add_reg_instruction_hook(self,newreg(R_INTREGISTER,i,R_SUBWHOLE));
  153. end;
  154. for i:=RS_A0 to RS_SP do
  155. begin
  156. if assigned(add_reg_instruction_hook) and (i in addrregset^) then
  157. add_reg_instruction_hook(self,newreg(R_ADDRESSREGISTER,i,R_SUBWHOLE));
  158. end;
  159. end;
  160. end;
  161. procedure taicpu.init(_size : topsize);
  162. begin
  163. typ:=ait_instruction;
  164. is_jmp:=false;
  165. opsize:=_size;
  166. ops:=0;
  167. end;
  168. constructor taicpu.op_none(op : tasmop);
  169. begin
  170. inherited create(op);
  171. init(S_NO);
  172. end;
  173. constructor taicpu.op_none(op : tasmop;_size : topsize);
  174. begin
  175. inherited create(op);
  176. init(_size);
  177. end;
  178. constructor taicpu.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  179. begin
  180. inherited create(op);
  181. init(_size);
  182. ops:=1;
  183. loadreg(0,_op1);
  184. end;
  185. constructor taicpu.op_const(op : tasmop;_size : topsize;_op1 : longint);
  186. begin
  187. inherited create(op);
  188. init(_size);
  189. ops:=1;
  190. loadconst(0,aword(_op1));
  191. end;
  192. constructor taicpu.op_ref(op : tasmop;_size : topsize;_op1 : treference);
  193. begin
  194. inherited create(op);
  195. init(_size);
  196. ops:=1;
  197. loadref(0,_op1);
  198. end;
  199. constructor taicpu.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  200. begin
  201. inherited create(op);
  202. init(_size);
  203. ops:=2;
  204. loadreg(0,_op1);
  205. loadreg(1,_op2);
  206. end;
  207. constructor taicpu.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: longint);
  208. begin
  209. inherited create(op);
  210. init(_size);
  211. ops:=2;
  212. loadreg(0,_op1);
  213. loadconst(1,aword(_op2));
  214. end;
  215. constructor taicpu.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;_op2 : treference);
  216. begin
  217. inherited create(op);
  218. init(_size);
  219. ops:=2;
  220. loadreg(0,_op1);
  221. loadref(1,_op2);
  222. end;
  223. constructor taicpu.op_const_reg(op : tasmop;_size : topsize;_op1 : longint;_op2 : tregister);
  224. begin
  225. inherited create(op);
  226. init(_size);
  227. ops:=2;
  228. loadconst(0,aword(_op1));
  229. loadreg(1,_op2);
  230. end;
  231. constructor taicpu.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : longint);
  232. begin
  233. inherited create(op);
  234. init(_size);
  235. ops:=2;
  236. loadconst(0,aword(_op1));
  237. loadconst(1,aword(_op2));
  238. end;
  239. constructor taicpu.op_const_ref(op : tasmop;_size : topsize;_op1 : longint;_op2 : treference);
  240. begin
  241. inherited create(op);
  242. init(_size);
  243. ops:=2;
  244. loadconst(0,aword(_op1));
  245. loadref(1,_op2);
  246. end;
  247. constructor taicpu.op_ref_reg(op : tasmop;_size : topsize;_op1 : treference;_op2 : tregister);
  248. begin
  249. inherited create(op);
  250. init(_size);
  251. ops:=2;
  252. loadref(0,_op1);
  253. loadreg(1,_op2);
  254. end;
  255. constructor taicpu.op_ref_ref(op : tasmop;_size : topsize;_op1,_op2 : treference);
  256. begin
  257. inherited create(op);
  258. init(_size);
  259. ops:=2;
  260. loadref(0,_op1);
  261. loadref(1,_op2);
  262. end;
  263. constructor taicpu.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  264. begin
  265. inherited create(op);
  266. init(_size);
  267. ops:=3;
  268. loadreg(0,_op1);
  269. loadreg(1,_op2);
  270. loadreg(2,_op3);
  271. end;
  272. constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : longint;_op2 : tregister;_op3 : tregister);
  273. begin
  274. inherited create(op);
  275. init(_size);
  276. ops:=3;
  277. loadconst(0,aword(_op1));
  278. loadreg(1,_op2);
  279. loadreg(2,_op3);
  280. end;
  281. constructor taicpu.op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister;_op3 : treference);
  282. begin
  283. inherited create(op);
  284. init(_size);
  285. ops:=3;
  286. loadreg(0,_op1);
  287. loadreg(1,_op2);
  288. loadref(2,_op3);
  289. end;
  290. constructor taicpu.op_const_ref_reg(op : tasmop;_size : topsize;_op1 : longint;_op2 : treference;_op3 : tregister);
  291. begin
  292. inherited create(op);
  293. init(_size);
  294. ops:=3;
  295. loadconst(0,aword(_op1));
  296. loadref(1,_op2);
  297. loadreg(2,_op3);
  298. end;
  299. constructor taicpu.op_const_reg_ref(op : tasmop;_size : topsize;_op1 : longint;_op2 : tregister;_op3 : treference);
  300. begin
  301. inherited create(op);
  302. init(_size);
  303. ops:=3;
  304. loadconst(0,aword(_op1));
  305. loadreg(1,_op2);
  306. loadref(2,_op3);
  307. end;
  308. constructor taicpu.op_ref_regset(op: tasmop; _size : topsize; _op1: treference;const _op2data,_op2addr: tcpuregisterset);
  309. Begin
  310. inherited create(op);
  311. init(_size);
  312. ops:=2;
  313. loadref(0,_op1);
  314. loadregset(1,_op2data,_op2addr);
  315. end;
  316. constructor taicpu.op_regset_ref(op: tasmop; _size : topsize;const _op1data,_op1addr: tcpuregisterset; _op2: treference);
  317. Begin
  318. inherited create(op);
  319. init(_size);
  320. ops:=2;
  321. loadregset(0,_op1data,_op1addr);
  322. loadref(1,_op2);
  323. End;
  324. constructor taicpu.op_reg_regset(op: tasmop; _size : topsize; _op1: tregister;const _op2data,_op2addr: tcpuregisterset);
  325. Begin
  326. inherited create(op);
  327. init(_size);
  328. ops:=2;
  329. loadreg(0,_op1);
  330. loadregset(1,_op2data,_op2addr);
  331. end;
  332. constructor taicpu.op_regset_reg(op: tasmop; _size : topsize;const _op1data,_op1addr: tcpuregisterset; _op2: tregister);
  333. Begin
  334. inherited create(op);
  335. init(_size);
  336. ops:=2;
  337. loadregset(0,_op1data,_op1addr);
  338. loadreg(1,_op2);
  339. End;
  340. constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  341. begin
  342. inherited create(op);
  343. init(_size);
  344. ops:=1;
  345. loadsymbol(0,_op1,0);
  346. end;
  347. constructor taicpu.op_reg_sym(op: tasmop; _size : topsize; _op1: tregister; _op2 :tasmsymbol);
  348. begin
  349. inherited create(op);
  350. init(_size);
  351. ops:=2;
  352. loadreg(0,_op1);
  353. loadsymbol(1,_op2,0);
  354. end;
  355. constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  356. begin
  357. inherited create(op);
  358. init(_size);
  359. ops:=2;
  360. loadsymbol(0,_op1,_op1ofs);
  361. loadref(1,_op2);
  362. end;
  363. constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  364. begin
  365. inherited create(op);
  366. init(_size);
  367. ops:=1;
  368. loadsymbol(0,_op1,_op1ofs);
  369. end;
  370. constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  371. begin
  372. inherited create(op);
  373. init(_size);
  374. ops:=2;
  375. if ((op >= A_DBCC) and (op <= A_DBF))
  376. or ((op >= A_FDBEQ) and (op <= A_FDBNGLE)) then
  377. begin
  378. loadreg(0,_op2);
  379. loadsymbol(1,_op1,_op1ofs);
  380. end
  381. else
  382. begin
  383. loadsymbol(0,_op1,_op1ofs);
  384. loadreg(1,_op2);
  385. end;
  386. end;
  387. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  388. begin
  389. inherited create(op);
  390. init(_size);
  391. condition:=cond;
  392. ops:=1;
  393. loadsymbol(0,_op1,0);
  394. end;
  395. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  396. begin
  397. result:=(((opcode=A_MOVE) or (opcode=A_EXG)) and
  398. (regtype = R_INTREGISTER) and
  399. (ops=2) and
  400. (oper[0]^.typ=top_reg) and
  401. (oper[1]^.typ=top_reg) and
  402. (oper[0]^.reg=oper[1]^.reg)
  403. ) or
  404. (((opcode=A_MOVE) or (opcode=A_EXG) or (opcode=A_MOVEA)) and
  405. (regtype = R_ADDRESSREGISTER) and
  406. (ops=2) and
  407. (oper[0]^.typ=top_reg) and
  408. (oper[1]^.typ=top_reg) and
  409. (oper[0]^.reg=oper[1]^.reg)
  410. ) or
  411. ((opcode=A_FMOVE) and
  412. (regtype = R_FPUREGISTER) and
  413. (ops=2) and
  414. (oper[0]^.typ=top_reg) and
  415. (oper[1]^.typ=top_reg) and
  416. (oper[0]^.reg=oper[1]^.reg)
  417. );
  418. end;
  419. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  420. begin
  421. result:=operand_read;
  422. case opcode of
  423. A_MOVE, A_MOVEQ, A_MOVEA:
  424. if opnr=1 then
  425. result:=operand_write;
  426. A_ADD, A_ADDQ, A_ADDX, A_SUB, A_SUBQ, A_SUBX,
  427. A_AND, A_LSR, A_LSL, A_ASR, A_ASL, A_EOR, A_EORI, A_OR,
  428. A_MULS, A_MULU, A_DIVS, A_DIVU, A_DIVSL, A_DIVUL:
  429. if opnr=1 then
  430. result:=operand_readwrite;
  431. A_DBRA:
  432. if opnr=0 then
  433. result:=operand_readwrite;
  434. A_CLR, A_SXX, A_SEQ, A_SNE, A_SLT, A_SLE, A_SGT, A_SGE, A_SCS, A_SCC,
  435. A_SMI, A_SPL, A_SF, A_ST, A_SVS, A_SVC, A_SHI, A_SLS:
  436. result:=operand_write;
  437. A_NEG, A_NEGX, A_EXT, A_EXTB, A_NOT:
  438. result:=operand_readwrite;
  439. A_TST,A_CMP,A_CMPI:
  440. begin end; { Do nothing, default operand_read is fine here. }
  441. else begin
  442. { TODO: FIX ME!!! remove ugly debug code ... }
  443. writeln('M68K: unknown opcode when spilling: ',gas_op2str[opcode]);
  444. internalerror(2004040903);
  445. end;
  446. end;
  447. end;
  448. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  449. begin
  450. case getregtype(r) of
  451. R_INTREGISTER :
  452. result:=taicpu.op_ref_reg(A_MOVE,S_L,ref,r);
  453. R_ADDRESSREGISTER :
  454. result:=taicpu.op_ref_reg(A_MOVE,S_L,ref,r);
  455. R_FPUREGISTER :
  456. // no need to handle sizes here
  457. result:=taicpu.op_ref_reg(A_FMOVE,S_FS,ref,r);
  458. else
  459. internalerror(200602011);
  460. end;
  461. end;
  462. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  463. begin
  464. case getregtype(r) of
  465. R_INTREGISTER :
  466. result:=taicpu.op_reg_ref(A_MOVE,S_L,r,ref);
  467. R_ADDRESSREGISTER :
  468. result:=taicpu.op_reg_ref(A_MOVE,S_L,r,ref);
  469. R_FPUREGISTER :
  470. // no need to handle sizes here
  471. result:=taicpu.op_reg_ref(A_FMOVE,S_FS,r,ref);
  472. else
  473. internalerror(200602012);
  474. end;
  475. end;
  476. procedure InitAsm;
  477. begin
  478. end;
  479. procedure DoneAsm;
  480. begin
  481. end;
  482. begin
  483. cai_align:=tai_align;
  484. cai_cpu:=taicpu;
  485. end.