aasmcpu.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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,fpuregs:tcpuregisterset);
  33. procedure loadregpair(opidx:longint; const _reghi,_reglo: tregister);
  34. procedure loadrealconst(opidx:longint; const value_real: bestreal);
  35. constructor op_none(op : tasmop);
  36. constructor op_none(op : tasmop;_size : topsize);
  37. constructor op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  38. constructor op_const(op : tasmop;_size : topsize;_op1 : longint);
  39. constructor op_ref(op : tasmop;_size : topsize;_op1 : treference);
  40. constructor op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  41. constructor op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;_op2 : treference);
  42. constructor op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: longint);
  43. constructor op_const_reg(op : tasmop;_size : topsize;_op1 : longint;_op2 : tregister);
  44. constructor op_const_const(op : tasmop;_size : topsize;_op1,_op2 : longint);
  45. constructor op_const_ref(op : tasmop;_size : topsize;_op1 : longint;_op2 : treference);
  46. constructor op_realconst_reg(op : tasmop;_size : topsize;_op1: bestreal;_op2: tregister);
  47. constructor op_ref_reg(op : tasmop;_size : topsize;_op1 : treference;_op2 : tregister);
  48. { this is only allowed if _op1 is an int value (_op1^.isintvalue=true) }
  49. constructor op_ref_ref(op : tasmop;_size : topsize;_op1,_op2 : treference);
  50. { this is used for mulx/divx/remx regpair generation }
  51. constructor op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  52. constructor op_const_reg_reg(op : tasmop;_size : topsize;_op1 : longint; _op2,_op3 : tregister);
  53. constructor op_ref_reg_reg(op : tasmop;_size : topsize;_op1 : treference; _op2,_op3 : tregister);
  54. constructor op_reg_regset(op: tasmop; _size : topsize; _op1: tregister;const _op2data,_op2addr,_op2fpu: tcpuregisterset);
  55. constructor op_regset_reg(op: tasmop; _size : topsize;const _op1data,_op1addr,_op1fpu: tcpuregisterset; _op2: tregister);
  56. constructor op_ref_regset(op: tasmop; _size : topsize; _op1: treference;const _op2data,_op2addr,_op2fpu: tcpuregisterset);
  57. constructor op_regset_ref(op: tasmop; _size : topsize;const _op1data,_op1addr,_op1fpu: tcpuregisterset; _op2: treference);
  58. { this is for Jmp instructions }
  59. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  60. constructor op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  61. { for DBxx opcodes }
  62. constructor op_reg_sym(op: tasmop; _size : topsize; _op1: tregister; _op2 :tasmsymbol);
  63. constructor op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  64. constructor op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  65. constructor op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  66. function is_same_reg_move(regtype: Tregistertype):boolean;override;
  67. function spilling_get_operation_type(opnr: longint): topertype;override;
  68. function spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;override;
  69. private
  70. procedure init(_size : topsize); { this need to be called by all constructor }
  71. end;
  72. tai_align = class(tai_align_abstract)
  73. { nothing to add }
  74. end;
  75. procedure InitAsm;
  76. procedure DoneAsm;
  77. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  78. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  79. implementation
  80. uses
  81. globtype;
  82. {*****************************************************************************
  83. Taicpu Constructors
  84. *****************************************************************************}
  85. procedure taicpu.loadregset(opidx:longint; const dataregs,addrregs,fpuregs:tcpuregisterset);
  86. var
  87. i : byte;
  88. begin
  89. allocate_oper(opidx+1);
  90. with oper[opidx]^ do
  91. begin
  92. if typ<>top_regset then
  93. clearop(opidx);
  94. dataregset:=dataregs;
  95. addrregset:=addrregs;
  96. fpuregset:=fpuregs;
  97. typ:=top_regset;
  98. for i:=RS_D0 to RS_D7 do
  99. begin
  100. if assigned(add_reg_instruction_hook) and (i in dataregset) then
  101. add_reg_instruction_hook(self,newreg(R_INTREGISTER,i,R_SUBWHOLE));
  102. end;
  103. for i:=RS_A0 to RS_SP do
  104. begin
  105. if assigned(add_reg_instruction_hook) and (i in addrregset) then
  106. add_reg_instruction_hook(self,newreg(R_ADDRESSREGISTER,i,R_SUBWHOLE));
  107. end;
  108. for i:=RS_FP0 to RS_FP7 do
  109. begin
  110. if assigned(add_reg_instruction_hook) and (i in fpuregset) then
  111. add_reg_instruction_hook(self,newreg(R_FPUREGISTER,i,R_SUBWHOLE));
  112. end;
  113. end;
  114. end;
  115. procedure taicpu.loadregpair(opidx:longint; const _reghi,_reglo: tregister);
  116. begin
  117. allocate_oper(opidx+1);
  118. with oper[opidx]^ do
  119. begin
  120. if typ<>top_regpair then
  121. clearop(opidx);
  122. typ:=top_regpair;
  123. reghi:=_reghi;
  124. reglo:=_reglo;
  125. end;
  126. end;
  127. procedure taicpu.loadrealconst(opidx:longint; const value_real: bestreal);
  128. begin
  129. allocate_oper(opidx+1);
  130. with oper[opidx]^ do
  131. begin
  132. if typ<>top_realconst then
  133. clearop(opidx);
  134. val_real:=value_real;
  135. typ:=top_realconst;
  136. end;
  137. end;
  138. procedure taicpu.init(_size : topsize);
  139. begin
  140. typ:=ait_instruction;
  141. is_jmp:=false;
  142. opsize:=_size;
  143. ops:=0;
  144. end;
  145. constructor taicpu.op_none(op : tasmop);
  146. begin
  147. inherited create(op);
  148. init(S_NO);
  149. end;
  150. constructor taicpu.op_none(op : tasmop;_size : topsize);
  151. begin
  152. inherited create(op);
  153. init(_size);
  154. end;
  155. constructor taicpu.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  156. begin
  157. inherited create(op);
  158. init(_size);
  159. ops:=1;
  160. loadreg(0,_op1);
  161. end;
  162. constructor taicpu.op_const(op : tasmop;_size : topsize;_op1 : longint);
  163. begin
  164. inherited create(op);
  165. init(_size);
  166. ops:=1;
  167. loadconst(0,aword(_op1));
  168. end;
  169. constructor taicpu.op_ref(op : tasmop;_size : topsize;_op1 : treference);
  170. begin
  171. inherited create(op);
  172. init(_size);
  173. ops:=1;
  174. loadref(0,_op1);
  175. end;
  176. constructor taicpu.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  177. begin
  178. inherited create(op);
  179. init(_size);
  180. ops:=2;
  181. loadreg(0,_op1);
  182. loadreg(1,_op2);
  183. end;
  184. constructor taicpu.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: longint);
  185. begin
  186. inherited create(op);
  187. init(_size);
  188. ops:=2;
  189. loadreg(0,_op1);
  190. loadconst(1,aword(_op2));
  191. end;
  192. constructor taicpu.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;_op2 : treference);
  193. begin
  194. inherited create(op);
  195. init(_size);
  196. ops:=2;
  197. loadreg(0,_op1);
  198. loadref(1,_op2);
  199. end;
  200. constructor taicpu.op_const_reg(op : tasmop;_size : topsize;_op1 : longint;_op2 : tregister);
  201. begin
  202. inherited create(op);
  203. init(_size);
  204. ops:=2;
  205. loadconst(0,aword(_op1));
  206. loadreg(1,_op2);
  207. end;
  208. constructor taicpu.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : longint);
  209. begin
  210. inherited create(op);
  211. init(_size);
  212. ops:=2;
  213. loadconst(0,aword(_op1));
  214. loadconst(1,aword(_op2));
  215. end;
  216. constructor taicpu.op_const_ref(op : tasmop;_size : topsize;_op1 : longint;_op2 : treference);
  217. begin
  218. inherited create(op);
  219. init(_size);
  220. ops:=2;
  221. loadconst(0,aword(_op1));
  222. loadref(1,_op2);
  223. end;
  224. constructor taicpu.op_realconst_reg(op : tasmop;_size : topsize;_op1 : bestreal;_op2 : tregister);
  225. begin
  226. inherited create(op);
  227. init(_size);
  228. ops:=2;
  229. loadrealconst(0,_op1);
  230. loadreg(1,_op2);
  231. end;
  232. constructor taicpu.op_ref_reg(op : tasmop;_size : topsize;_op1 : treference;_op2 : tregister);
  233. begin
  234. inherited create(op);
  235. init(_size);
  236. ops:=2;
  237. loadref(0,_op1);
  238. loadreg(1,_op2);
  239. end;
  240. constructor taicpu.op_ref_ref(op : tasmop;_size : topsize;_op1,_op2 : treference);
  241. begin
  242. inherited create(op);
  243. init(_size);
  244. ops:=2;
  245. loadref(0,_op1);
  246. loadref(1,_op2);
  247. end;
  248. constructor taicpu.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  249. begin
  250. inherited create(op);
  251. init(_size);
  252. ops:=3;
  253. loadreg(0,_op1);
  254. loadreg(1,_op2);
  255. loadreg(2,_op3);
  256. end;
  257. constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : longint; _op2,_op3 : tregister);
  258. begin
  259. inherited create(op);
  260. init(_size);
  261. ops:=3;
  262. loadconst(0,aword(_op1));
  263. loadreg(1,_op2);
  264. loadreg(2,_op3);
  265. end;
  266. constructor taicpu.op_ref_reg_reg(op : tasmop;_size : topsize;_op1 : treference; _op2,_op3 : tregister);
  267. begin
  268. inherited create(op);
  269. init(_size);
  270. ops:=3;
  271. loadref(0,_op1);
  272. loadreg(1,_op2);
  273. loadreg(2,_op3);
  274. end;
  275. constructor taicpu.op_ref_regset(op: tasmop; _size : topsize; _op1: treference;const _op2data,_op2addr,_op2fpu: tcpuregisterset);
  276. Begin
  277. inherited create(op);
  278. init(_size);
  279. ops:=2;
  280. loadref(0,_op1);
  281. loadregset(1,_op2data,_op2addr,_op2fpu);
  282. end;
  283. constructor taicpu.op_regset_ref(op: tasmop; _size : topsize;const _op1data,_op1addr,_op1fpu: tcpuregisterset; _op2: treference);
  284. Begin
  285. inherited create(op);
  286. init(_size);
  287. ops:=2;
  288. loadregset(0,_op1data,_op1addr,_op1fpu);
  289. loadref(1,_op2);
  290. End;
  291. constructor taicpu.op_reg_regset(op: tasmop; _size : topsize; _op1: tregister;const _op2data,_op2addr,_op2fpu: tcpuregisterset);
  292. Begin
  293. inherited create(op);
  294. init(_size);
  295. ops:=2;
  296. loadreg(0,_op1);
  297. loadregset(1,_op2data,_op2addr,_op2fpu);
  298. end;
  299. constructor taicpu.op_regset_reg(op: tasmop; _size : topsize;const _op1data,_op1addr,_op1fpu: tcpuregisterset; _op2: tregister);
  300. Begin
  301. inherited create(op);
  302. init(_size);
  303. ops:=2;
  304. loadregset(0,_op1data,_op1addr,_op1fpu);
  305. loadreg(1,_op2);
  306. End;
  307. constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  308. begin
  309. inherited create(op);
  310. init(_size);
  311. ops:=1;
  312. loadsymbol(0,_op1,0);
  313. end;
  314. constructor taicpu.op_reg_sym(op: tasmop; _size : topsize; _op1: tregister; _op2 :tasmsymbol);
  315. begin
  316. inherited create(op);
  317. init(_size);
  318. ops:=2;
  319. loadreg(0,_op1);
  320. loadsymbol(1,_op2,0);
  321. end;
  322. constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  323. begin
  324. inherited create(op);
  325. init(_size);
  326. ops:=2;
  327. loadsymbol(0,_op1,_op1ofs);
  328. loadref(1,_op2);
  329. end;
  330. constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  331. begin
  332. inherited create(op);
  333. init(_size);
  334. ops:=1;
  335. loadsymbol(0,_op1,_op1ofs);
  336. end;
  337. constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  338. begin
  339. inherited create(op);
  340. init(_size);
  341. ops:=2;
  342. if ((op >= A_DBCC) and (op <= A_DBF))
  343. or ((op >= A_FDBEQ) and (op <= A_FDBNGLE)) then
  344. begin
  345. loadreg(0,_op2);
  346. loadsymbol(1,_op1,_op1ofs);
  347. end
  348. else
  349. begin
  350. loadsymbol(0,_op1,_op1ofs);
  351. loadreg(1,_op2);
  352. end;
  353. end;
  354. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  355. begin
  356. inherited create(op);
  357. init(_size);
  358. condition:=cond;
  359. ops:=1;
  360. loadsymbol(0,_op1,0);
  361. end;
  362. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  363. begin
  364. result:=(((opcode=A_MOVE) or (opcode=A_EXG)) and
  365. (regtype = R_INTREGISTER) and
  366. (ops=2) and
  367. (oper[0]^.typ=top_reg) and
  368. (oper[1]^.typ=top_reg) and
  369. (isregoverlap(oper[0]^.reg,oper[1]^.reg))
  370. ) or
  371. (((opcode=A_MOVE) or (opcode=A_EXG) or (opcode=A_MOVEA)) and
  372. (regtype = R_ADDRESSREGISTER) and
  373. (ops=2) and
  374. (oper[0]^.typ=top_reg) and
  375. (oper[1]^.typ=top_reg) and
  376. (isregoverlap(oper[0]^.reg,oper[1]^.reg))
  377. ) or
  378. ((opcode=A_FMOVE) and
  379. (regtype = R_FPUREGISTER) and
  380. (ops=2) and
  381. (oper[0]^.typ=top_reg) and
  382. (oper[1]^.typ=top_reg) and
  383. (oper[0]^.reg=oper[1]^.reg)
  384. );
  385. end;
  386. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  387. begin
  388. result:=operand_read;
  389. case opcode of
  390. // CPU opcodes
  391. A_MOVE, A_MOVEQ, A_MOVEA, A_MVZ, A_MVS, A_MOV3Q, A_LEA:
  392. if opnr=1 then
  393. result:=operand_write;
  394. A_ADD, A_ADDQ, A_ADDX, A_SUB, A_SUBQ, A_SUBX,
  395. A_AND, A_LSR, A_LSL, A_ASR, A_ASL, A_EOR, A_EORI, A_OR,
  396. A_ROL, A_ROR, A_ROXL, A_ROXR,
  397. A_BSET, A_BCLR:
  398. if opnr=1 then
  399. result:=operand_readwrite;
  400. A_MULS, A_MULU, A_DIVS, A_DIVU, A_DIVSL, A_DIVUL, A_REMS, A_REMU:
  401. { FIXME: actually, one of the operand of the 3 op DIV/MUL is write only,
  402. but we can't handle it easily... }
  403. if opnr>0 then
  404. result:=operand_readwrite;
  405. A_DBRA:
  406. if opnr=0 then
  407. result:=operand_readwrite;
  408. A_CLR, A_SXX, A_SEQ, A_SNE, A_SLT, A_SLE, A_SGT, A_SGE, A_SCS, A_SCC,
  409. A_SMI, A_SPL, A_SF, A_ST, A_SVS, A_SVC, A_SHI, A_SLS:
  410. result:=operand_write;
  411. A_NEG, A_NEGX, A_EXT, A_EXTB, A_NOT, A_SWAP:
  412. result:=operand_readwrite;
  413. A_TST,A_CMP,A_CMPI,A_BTST:
  414. begin end; { Do nothing, default operand_read is fine here. }
  415. // FPU opcodes
  416. A_FSXX, A_FSEQ, A_FSNE, A_FSLT, A_FSLE, A_FSGT, A_FSGE:
  417. result:=operand_write;
  418. A_FABS,A_FSQRT,A_FNEG,A_FSIN,A_FCOS:
  419. if ops = 1 then
  420. begin
  421. if opnr = 0 then
  422. result:=operand_readwrite;
  423. end
  424. else
  425. if opnr = 1 then
  426. result:=operand_write;
  427. A_FMOVE:
  428. if opnr=1 then
  429. result:=operand_write;
  430. A_FADD, A_FSUB, A_FMUL, A_FDIV:
  431. if opnr=1 then
  432. result:=operand_readwrite;
  433. A_FCMP, A_FTST:
  434. begin end; { operand_read }
  435. else begin
  436. internalerror(2004040903);
  437. end;
  438. end;
  439. end;
  440. function taicpu.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  441. begin
  442. result := operand_read;
  443. if (oper[opnr]^.ref^.base = reg) and
  444. (oper[opnr]^.ref^.direction <> dir_none) then
  445. result := operand_readwrite;
  446. end;
  447. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  448. begin
  449. case getregtype(r) of
  450. R_INTREGISTER :
  451. result:=taicpu.op_ref_reg(A_MOVE,S_L,ref,r);
  452. R_ADDRESSREGISTER :
  453. result:=taicpu.op_ref_reg(A_MOVE,S_L,ref,r);
  454. R_FPUREGISTER :
  455. // no need to handle sizes here
  456. result:=taicpu.op_ref_reg(A_FMOVE,S_FS,ref,r);
  457. else
  458. internalerror(200602011);
  459. end;
  460. end;
  461. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  462. begin
  463. case getregtype(r) of
  464. R_INTREGISTER :
  465. result:=taicpu.op_reg_ref(A_MOVE,S_L,r,ref);
  466. R_ADDRESSREGISTER :
  467. result:=taicpu.op_reg_ref(A_MOVE,S_L,r,ref);
  468. R_FPUREGISTER :
  469. // no need to handle sizes here
  470. result:=taicpu.op_reg_ref(A_FMOVE,S_FS,r,ref);
  471. else
  472. internalerror(200602012);
  473. end;
  474. end;
  475. procedure InitAsm;
  476. begin
  477. end;
  478. procedure DoneAsm;
  479. begin
  480. end;
  481. begin
  482. cai_align:=tai_align;
  483. cai_cpu:=taicpu;
  484. end.