aasmcpu.pas 18 KB

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