aasmcpu.pas 17 KB

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