aasmcpu.pas 19 KB

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