aasmcpu.pas 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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. ogbase;
  25. const
  26. { "mov reg,reg" source operand number }
  27. O_MOV_SOURCE = 0;
  28. { "mov reg,reg" source operand number }
  29. O_MOV_DEST = 1;
  30. instabentries = {$i m68knop.inc}
  31. type
  32. TOperandType = (
  33. OT_DATA,
  34. OT_ADDR,
  35. OT_ADDR_INDIR,
  36. OT_ADDR_INDIR_POSTINC,
  37. OT_ADDR_INDIR_PREDEC,
  38. OT_ADDR_DISP16,
  39. OT_ADDR_IDX_DISP8,
  40. OT_ABS_SHORT,
  41. OT_ABS_LONG,
  42. OT_PC_DISP16,
  43. OT_PC_IDX_DISP8,
  44. OT_IMMEDIATE,
  45. OT_REG_LIST,
  46. OT_FPUREG_LIST,
  47. OT_FPUREG,
  48. OT_SPECIALREG
  49. );
  50. TOperandFlags = (
  51. OF_IMM_QUICK,
  52. OF_IMM_FLOAT,
  53. OF_IMM_64BIT,
  54. OF_SPECREG,
  55. OF_SPECREG_CCR,
  56. OF_SPECREG_SR,
  57. OF_SPECREG_USP,
  58. OF_SPECREG_FPIAR,
  59. OF_SPECREG_FPU,
  60. OF_BITFIELD,
  61. OF_BRANCH,
  62. OF_DOUBLE_REG,
  63. OF_KFACTOR,
  64. OF_NOSIZE
  65. );
  66. TOpSizeFlag = (
  67. OPS_UNSIZED,
  68. OPS_SHORT,
  69. OPS_BYTE,
  70. OPS_WORD,
  71. OPS_LONG,
  72. OPS_QUAD,
  73. OPS_SINGLE,
  74. OPS_DOUBLE,
  75. OPS_EXTENDED,
  76. OPS_PACKED,
  77. OPS_COLDFIRE
  78. );
  79. TOpSupported = (
  80. OS_M68000,
  81. OS_M68000UP,
  82. OS_M68010UP,
  83. OS_M68020,
  84. OS_M68020UP,
  85. OS_M68030,
  86. OS_M68040,
  87. OS_M68040UP,
  88. OS_M68060,
  89. OS_M68881,
  90. OS_M68851,
  91. OS_CPU32,
  92. OS_CF,
  93. OS_CF_ISA_A,
  94. OS_CF_ISA_APL,
  95. OS_CF_ISA_B,
  96. OS_CF_ISA_C,
  97. OS_CF_HWDIV,
  98. OS_CF_FPU,
  99. OS_CF_USP,
  100. OS_GNU_AS
  101. );
  102. const
  103. AM_Dn = 0;
  104. AM_An = 1;
  105. AM_An_Indir = 2;
  106. AM_An_PostInc = 3;
  107. AM_An_PreDec = 4;
  108. AM_An_Disp16 = 5;
  109. AM_An_Format8 = 6;
  110. AM_Extended = 7;
  111. AM_FPn = 8;
  112. AM_SpecReg = 9;
  113. REG_AbsShort = 0;
  114. REG_AbsLong = 1;
  115. REG_PC_Disp16 = 2;
  116. REG_PC_Format8 = 3;
  117. REG_Immediate = 4;
  118. REG_RegList = 5;
  119. REG_FPURegList = 6;
  120. type
  121. toperandtypeset = set of toperandtype;
  122. toperandflagset = set of toperandflags;
  123. topsupportedset = set of topsupported;
  124. topsizeflagset = set of topsizeflag;
  125. type
  126. tinsentry = record
  127. opcode : tasmop;
  128. ops : byte;
  129. optypes : array[0..max_operands-1] of toperandtypeset;
  130. opflags : array[0..max_operands-1] of toperandflagset;
  131. codelen : byte;
  132. code : array[0..1] of word;
  133. support : topsupportedset;
  134. sizes : topsizeflagset;
  135. end;
  136. pinsentry = ^tinsentry;
  137. type
  138. TInsTabCache=array[TasmOp] of longint;
  139. PInsTabCache=^TInsTabCache;
  140. var
  141. InsTabCache: PInsTabCache;
  142. const
  143. InsTab:array[0..instabentries-1] of TInsEntry = {$i m68ktab.inc}
  144. type
  145. taicpu = class(tai_cpu_abstract_sym)
  146. private
  147. { next fields are filled in pass1, so pass2 is faster }
  148. insentry : PInsEntry;
  149. inssize : shortint;
  150. insoffset : longint;
  151. LastInsOffset : longint;
  152. function CalcSize(p: PInsEntry):shortint;
  153. function Matches(p: PInsEntry; objdata: TObjData):boolean;
  154. function FindInsEntry(objdata: TObjData):boolean;
  155. procedure GenCode(objdata: TObjData);
  156. procedure init(_size : topsize); { this need to be called by all constructor }
  157. public
  158. opsize : topsize;
  159. procedure loadregset(opidx:longint; const dataregs,addrregs,fpuregs:tcpuregisterset);
  160. procedure loadregpair(opidx:longint; const _reghi,_reglo: tregister);
  161. procedure loadrealconst(opidx:longint; const value_real: bestreal);
  162. constructor op_none(op : tasmop);
  163. constructor op_none(op : tasmop;_size : topsize);
  164. constructor op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  165. constructor op_const(op : tasmop;_size : topsize;_op1 : longint);
  166. constructor op_ref(op : tasmop;_size : topsize;_op1 : treference);
  167. constructor op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  168. constructor op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;_op2 : treference);
  169. constructor op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: longint);
  170. constructor op_const_reg(op : tasmop;_size : topsize;_op1 : longint;_op2 : tregister);
  171. constructor op_const_const(op : tasmop;_size : topsize;_op1,_op2 : longint);
  172. constructor op_const_ref(op : tasmop;_size : topsize;_op1 : longint;_op2 : treference);
  173. constructor op_realconst_reg(op : tasmop;_size : topsize;_op1: bestreal;_op2: tregister);
  174. constructor op_ref_reg(op : tasmop;_size : topsize;_op1 : treference;_op2 : tregister);
  175. { this is only allowed if _op1 is an int value (_op1^.isintvalue=true) }
  176. constructor op_ref_ref(op : tasmop;_size : topsize;_op1,_op2 : treference);
  177. { this is used for mulx/divx/remx regpair generation }
  178. constructor op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  179. constructor op_const_reg_reg(op : tasmop;_size : topsize;_op1 : longint; _op2,_op3 : tregister);
  180. constructor op_ref_reg_reg(op : tasmop;_size : topsize;_op1 : treference; _op2,_op3 : tregister);
  181. constructor op_reg_regset(op: tasmop; _size : topsize; _op1: tregister;const _op2data,_op2addr,_op2fpu: tcpuregisterset);
  182. constructor op_regset_reg(op: tasmop; _size : topsize;const _op1data,_op1addr,_op1fpu: tcpuregisterset; _op2: tregister);
  183. constructor op_ref_regset(op: tasmop; _size : topsize; _op1: treference;const _op2data,_op2addr,_op2fpu: tcpuregisterset);
  184. constructor op_regset_ref(op: tasmop; _size : topsize;const _op1data,_op1addr,_op1fpu: tcpuregisterset; _op2: treference);
  185. { this is for Jmp instructions }
  186. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  187. constructor op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  188. { for DBxx opcodes }
  189. constructor op_reg_sym(op: tasmop; _size : topsize; _op1: tregister; _op2 :tasmsymbol);
  190. constructor op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  191. constructor op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  192. constructor op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  193. function is_same_reg_move(regtype: Tregistertype):boolean;override;
  194. function spilling_get_operation_type(opnr: longint): topertype;override;
  195. function spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;override;
  196. procedure ResetPass1;override;
  197. procedure ResetPass2;override;
  198. function Pass1(objdata:TObjData):longint;override;
  199. procedure Pass2(objdata:TObjData);override;
  200. end;
  201. tai_align = class(tai_align_abstract)
  202. { nothing to add }
  203. end;
  204. procedure InitAsm;
  205. procedure DoneAsm;
  206. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  207. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  208. implementation
  209. uses
  210. globtype, itcpugas;
  211. {*****************************************************************************
  212. Taicpu Constructors
  213. *****************************************************************************}
  214. procedure taicpu.loadregset(opidx:longint; const dataregs,addrregs,fpuregs:tcpuregisterset);
  215. var
  216. i : byte;
  217. begin
  218. allocate_oper(opidx+1);
  219. with oper[opidx]^ do
  220. begin
  221. if typ<>top_regset then
  222. clearop(opidx);
  223. dataregset:=dataregs;
  224. addrregset:=addrregs;
  225. fpuregset:=fpuregs;
  226. typ:=top_regset;
  227. for i:=RS_D0 to RS_D7 do
  228. begin
  229. if assigned(add_reg_instruction_hook) and (i in dataregset) then
  230. add_reg_instruction_hook(self,newreg(R_INTREGISTER,i,R_SUBWHOLE));
  231. end;
  232. for i:=RS_A0 to RS_SP do
  233. begin
  234. if assigned(add_reg_instruction_hook) and (i in addrregset) then
  235. add_reg_instruction_hook(self,newreg(R_ADDRESSREGISTER,i,R_SUBWHOLE));
  236. end;
  237. for i:=RS_FP0 to RS_FP7 do
  238. begin
  239. if assigned(add_reg_instruction_hook) and (i in fpuregset) then
  240. add_reg_instruction_hook(self,newreg(R_FPUREGISTER,i,R_SUBWHOLE));
  241. end;
  242. end;
  243. end;
  244. procedure taicpu.loadregpair(opidx:longint; const _reghi,_reglo: tregister);
  245. begin
  246. allocate_oper(opidx+1);
  247. with oper[opidx]^ do
  248. begin
  249. if typ<>top_regpair then
  250. clearop(opidx);
  251. typ:=top_regpair;
  252. reghi:=_reghi;
  253. reglo:=_reglo;
  254. end;
  255. end;
  256. procedure taicpu.loadrealconst(opidx:longint; const value_real: bestreal);
  257. begin
  258. allocate_oper(opidx+1);
  259. with oper[opidx]^ do
  260. begin
  261. if typ<>top_realconst then
  262. clearop(opidx);
  263. val_real:=value_real;
  264. typ:=top_realconst;
  265. end;
  266. end;
  267. procedure taicpu.init(_size : topsize);
  268. begin
  269. typ:=ait_instruction;
  270. is_jmp:=false;
  271. opsize:=_size;
  272. ops:=0;
  273. end;
  274. constructor taicpu.op_none(op : tasmop);
  275. begin
  276. inherited create(op);
  277. init(S_NO);
  278. end;
  279. constructor taicpu.op_none(op : tasmop;_size : topsize);
  280. begin
  281. inherited create(op);
  282. init(_size);
  283. end;
  284. constructor taicpu.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  285. begin
  286. inherited create(op);
  287. init(_size);
  288. ops:=1;
  289. loadreg(0,_op1);
  290. end;
  291. constructor taicpu.op_const(op : tasmop;_size : topsize;_op1 : longint);
  292. begin
  293. inherited create(op);
  294. init(_size);
  295. ops:=1;
  296. loadconst(0,aword(_op1));
  297. end;
  298. constructor taicpu.op_ref(op : tasmop;_size : topsize;_op1 : treference);
  299. begin
  300. inherited create(op);
  301. init(_size);
  302. ops:=1;
  303. loadref(0,_op1);
  304. end;
  305. constructor taicpu.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  306. begin
  307. inherited create(op);
  308. init(_size);
  309. ops:=2;
  310. loadreg(0,_op1);
  311. loadreg(1,_op2);
  312. end;
  313. constructor taicpu.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: longint);
  314. begin
  315. inherited create(op);
  316. init(_size);
  317. ops:=2;
  318. loadreg(0,_op1);
  319. loadconst(1,aword(_op2));
  320. end;
  321. constructor taicpu.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;_op2 : treference);
  322. begin
  323. inherited create(op);
  324. init(_size);
  325. ops:=2;
  326. loadreg(0,_op1);
  327. loadref(1,_op2);
  328. end;
  329. constructor taicpu.op_const_reg(op : tasmop;_size : topsize;_op1 : longint;_op2 : tregister);
  330. begin
  331. inherited create(op);
  332. init(_size);
  333. ops:=2;
  334. loadconst(0,aword(_op1));
  335. loadreg(1,_op2);
  336. end;
  337. constructor taicpu.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : longint);
  338. begin
  339. inherited create(op);
  340. init(_size);
  341. ops:=2;
  342. loadconst(0,aword(_op1));
  343. loadconst(1,aword(_op2));
  344. end;
  345. constructor taicpu.op_const_ref(op : tasmop;_size : topsize;_op1 : longint;_op2 : treference);
  346. begin
  347. inherited create(op);
  348. init(_size);
  349. ops:=2;
  350. loadconst(0,aword(_op1));
  351. loadref(1,_op2);
  352. end;
  353. constructor taicpu.op_realconst_reg(op : tasmop;_size : topsize;_op1 : bestreal;_op2 : tregister);
  354. begin
  355. inherited create(op);
  356. init(_size);
  357. ops:=2;
  358. loadrealconst(0,_op1);
  359. loadreg(1,_op2);
  360. end;
  361. constructor taicpu.op_ref_reg(op : tasmop;_size : topsize;_op1 : treference;_op2 : tregister);
  362. begin
  363. inherited create(op);
  364. init(_size);
  365. ops:=2;
  366. loadref(0,_op1);
  367. loadreg(1,_op2);
  368. end;
  369. constructor taicpu.op_ref_ref(op : tasmop;_size : topsize;_op1,_op2 : treference);
  370. begin
  371. inherited create(op);
  372. init(_size);
  373. ops:=2;
  374. loadref(0,_op1);
  375. loadref(1,_op2);
  376. end;
  377. constructor taicpu.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  378. begin
  379. inherited create(op);
  380. init(_size);
  381. ops:=3;
  382. loadreg(0,_op1);
  383. loadreg(1,_op2);
  384. loadreg(2,_op3);
  385. end;
  386. constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : longint; _op2,_op3 : tregister);
  387. begin
  388. inherited create(op);
  389. init(_size);
  390. ops:=3;
  391. loadconst(0,aword(_op1));
  392. loadreg(1,_op2);
  393. loadreg(2,_op3);
  394. end;
  395. constructor taicpu.op_ref_reg_reg(op : tasmop;_size : topsize;_op1 : treference; _op2,_op3 : tregister);
  396. begin
  397. inherited create(op);
  398. init(_size);
  399. ops:=3;
  400. loadref(0,_op1);
  401. loadreg(1,_op2);
  402. loadreg(2,_op3);
  403. end;
  404. constructor taicpu.op_ref_regset(op: tasmop; _size : topsize; _op1: treference;const _op2data,_op2addr,_op2fpu: tcpuregisterset);
  405. Begin
  406. inherited create(op);
  407. init(_size);
  408. ops:=2;
  409. loadref(0,_op1);
  410. loadregset(1,_op2data,_op2addr,_op2fpu);
  411. end;
  412. constructor taicpu.op_regset_ref(op: tasmop; _size : topsize;const _op1data,_op1addr,_op1fpu: tcpuregisterset; _op2: treference);
  413. Begin
  414. inherited create(op);
  415. init(_size);
  416. ops:=2;
  417. loadregset(0,_op1data,_op1addr,_op1fpu);
  418. loadref(1,_op2);
  419. End;
  420. constructor taicpu.op_reg_regset(op: tasmop; _size : topsize; _op1: tregister;const _op2data,_op2addr,_op2fpu: tcpuregisterset);
  421. Begin
  422. inherited create(op);
  423. init(_size);
  424. ops:=2;
  425. loadreg(0,_op1);
  426. loadregset(1,_op2data,_op2addr,_op2fpu);
  427. end;
  428. constructor taicpu.op_regset_reg(op: tasmop; _size : topsize;const _op1data,_op1addr,_op1fpu: tcpuregisterset; _op2: tregister);
  429. Begin
  430. inherited create(op);
  431. init(_size);
  432. ops:=2;
  433. loadregset(0,_op1data,_op1addr,_op1fpu);
  434. loadreg(1,_op2);
  435. End;
  436. constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  437. begin
  438. inherited create(op);
  439. init(_size);
  440. ops:=1;
  441. loadsymbol(0,_op1,0);
  442. end;
  443. constructor taicpu.op_reg_sym(op: tasmop; _size : topsize; _op1: tregister; _op2 :tasmsymbol);
  444. begin
  445. inherited create(op);
  446. init(_size);
  447. ops:=2;
  448. loadreg(0,_op1);
  449. loadsymbol(1,_op2,0);
  450. end;
  451. constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  452. begin
  453. inherited create(op);
  454. init(_size);
  455. ops:=2;
  456. loadsymbol(0,_op1,_op1ofs);
  457. loadref(1,_op2);
  458. end;
  459. constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  460. begin
  461. inherited create(op);
  462. init(_size);
  463. ops:=1;
  464. loadsymbol(0,_op1,_op1ofs);
  465. end;
  466. constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  467. begin
  468. inherited create(op);
  469. init(_size);
  470. ops:=2;
  471. if ((op >= A_DBCC) and (op <= A_DBF))
  472. or ((op >= A_FDBEQ) and (op <= A_FDBNGLE)) then
  473. begin
  474. loadreg(0,_op2);
  475. loadsymbol(1,_op1,_op1ofs);
  476. end
  477. else
  478. begin
  479. loadsymbol(0,_op1,_op1ofs);
  480. loadreg(1,_op2);
  481. end;
  482. end;
  483. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  484. begin
  485. inherited create(op);
  486. init(_size);
  487. condition:=cond;
  488. ops:=1;
  489. loadsymbol(0,_op1,0);
  490. end;
  491. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  492. begin
  493. result:=(((opcode=A_MOVE) or (opcode=A_EXG)) and
  494. (regtype = R_INTREGISTER) and
  495. (ops=2) and
  496. (oper[0]^.typ=top_reg) and
  497. (oper[1]^.typ=top_reg) and
  498. (isregoverlap(oper[0]^.reg,oper[1]^.reg))
  499. ) or
  500. (((opcode=A_MOVE) or (opcode=A_EXG) or (opcode=A_MOVEA)) and
  501. (regtype = R_ADDRESSREGISTER) and
  502. (ops=2) and
  503. (oper[0]^.typ=top_reg) and
  504. (oper[1]^.typ=top_reg) and
  505. (isregoverlap(oper[0]^.reg,oper[1]^.reg))
  506. ) or
  507. ((opcode=A_FMOVE) and
  508. (regtype = R_FPUREGISTER) and
  509. (ops=2) and
  510. (oper[0]^.typ=top_reg) and
  511. (oper[1]^.typ=top_reg) and
  512. (oper[0]^.reg=oper[1]^.reg)
  513. );
  514. end;
  515. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  516. begin
  517. result:=operand_read;
  518. case opcode of
  519. // CPU opcodes
  520. A_MOVE, A_MOVEQ, A_MOVEA, A_MVZ, A_MVS, A_MOV3Q, A_LEA:
  521. if opnr=1 then
  522. result:=operand_write;
  523. A_ADD, A_ADDQ, A_ADDX, A_SUB, A_SUBQ, A_SUBX,
  524. A_AND, A_LSR, A_LSL, A_ASR, A_ASL, A_EOR, A_EORI, A_OR,
  525. A_ROL, A_ROR, A_ROXL, A_ROXR,
  526. A_BSET, A_BCLR:
  527. if opnr=1 then
  528. result:=operand_readwrite;
  529. A_MULS, A_MULU, A_DIVS, A_DIVU, A_DIVSL, A_DIVUL, A_REMS, A_REMU:
  530. { FIXME: actually, one of the operand of the 3 op DIV/MUL is write only,
  531. but we can't handle it easily... }
  532. if opnr>0 then
  533. result:=operand_readwrite;
  534. A_DBRA:
  535. if opnr=0 then
  536. result:=operand_readwrite;
  537. A_CLR, A_SXX, A_SEQ, A_SNE, A_SLT, A_SLE, A_SGT, A_SGE, A_SCS, A_SCC,
  538. A_SMI, A_SPL, A_SF, A_ST, A_SVS, A_SVC, A_SHI, A_SLS:
  539. result:=operand_write;
  540. A_NEG, A_NEGX, A_EXT, A_EXTB, A_NOT, A_SWAP:
  541. result:=operand_readwrite;
  542. A_TST, A_CMP, A_CMPI, A_BTST:
  543. begin end; { Do nothing, default operand_read is fine here. }
  544. // FPU opcodes
  545. A_FSXX, A_FSEQ, A_FSNE, A_FSLT, A_FSLE, A_FSGT, A_FSGE:
  546. result:=operand_write;
  547. A_FABS, A_FSABS, A_FDABS,
  548. A_FSQRT, A_FSSQRT, A_FDSQRT,
  549. A_FNEG, A_FSNEG, A_FDNEG,
  550. A_FSIN, A_FCOS,
  551. A_FINT, A_FINTRZ:
  552. if ops = 1 then
  553. begin
  554. if opnr = 0 then
  555. result:=operand_readwrite;
  556. end
  557. else
  558. if opnr = 1 then
  559. result:=operand_write;
  560. A_FMOVE, A_FSMOVE, A_FDMOVE:
  561. if opnr=1 then
  562. result:=operand_write;
  563. A_FADD, A_FSADD, A_FDADD,
  564. A_FSUB, A_FSSUB, A_FDSUB,
  565. A_FMUL, A_FSMUL, A_FDMUL, A_FSGLMUL,
  566. A_FDIV, A_FSDIV, A_FDDIV, A_FSGLDIV:
  567. if opnr=1 then
  568. result:=operand_readwrite;
  569. A_FCMP, A_FTST:
  570. begin end; { operand_read }
  571. else begin
  572. internalerror(2004040903);
  573. end;
  574. end;
  575. end;
  576. function taicpu.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  577. begin
  578. result := operand_read;
  579. if (oper[opnr]^.ref^.base = reg) and
  580. (oper[opnr]^.ref^.direction <> dir_none) then
  581. result := operand_readwrite;
  582. end;
  583. function taicpu.CalcSize(p: PInsEntry): shortint;
  584. begin
  585. result:=p^.codelen * 2;
  586. end;
  587. function taicpu.Matches(p: PInsEntry; objdata:TObjData): boolean;
  588. function TargetMatch: boolean;
  589. const
  590. CPUTypeToOpSupported: array[TCPUtype] of topsupportedset = (
  591. {* cpu_none *} [],
  592. {* cpu_MC68000 *} [OS_M68000,OS_M68000UP],
  593. {* cpu_MC68020 *} [OS_M68020,OS_M68000UP,OS_M68010UP,OS_M68020UP,OS_M68851],
  594. {* cpu_MC68040 *} [OS_M68040,OS_M68000UP,OS_M68010UP,OS_M68020UP,OS_M68040UP],
  595. {* cpu_MC68060 *} [OS_M68060,OS_M68000UP,OS_M68010UP,OS_M68020UP,OS_M68040UP],
  596. {* cpu_isa_a *} [OS_CF,OS_CF_ISA_A],
  597. {* cpu_isa_a_p *} [OS_CF,OS_CF_ISA_APL],
  598. {* cpu_isa_b *} [OS_CF,OS_CF_ISA_B],
  599. {* cpu_isa_c *} [OS_CF,OS_CF_ISA_C],
  600. {* cpu_cfv4e *} [OS_CF,OS_CF_ISA_B]
  601. );
  602. FPUTypeToOpSupported: array[TFPUtype] of topsupportedset = (
  603. {* fpu_none *} [],
  604. {* fpu_soft *} [],
  605. {* fpu_libgcc *} [],
  606. {* fpu_68881 *} [OS_M68881],
  607. {* fpu_68040 *} [OS_M68881,OS_M68040,OS_M68040UP],
  608. {* fpu_68060 *} [OS_M68881,OS_M68040,OS_M68040UP,OS_M68060],
  609. {* fpu_coldfire *} [OS_CF_FPU]
  610. );
  611. begin
  612. result:=((CPUTypeToOpSupported[current_settings.cputype] * p^.support) <> []) or
  613. ((FPUTypeToOpSupported[current_settings.fputype] * p^.support) <> []);
  614. end;
  615. function OpsizeMatch: boolean;
  616. const
  617. TOpSizeToOpSizeFlag: array[TOpSize] of TOpSizeFlagSet = (
  618. { S_NO } [ OPS_UNSIZED],
  619. { S_B } [ OPS_SHORT, OPS_BYTE ],
  620. { S_W } [ OPS_WORD ],
  621. { S_L } [ OPS_LONG ],
  622. { S_FS } [ OPS_SINGLE ],
  623. { S_FD } [ OPS_DOUBLE ],
  624. { S_FX } [ OPS_EXTENDED ]
  625. );
  626. begin
  627. result:=(TOpSizeToOpSizeFlag[opsize] * p^.sizes) <> [];
  628. { Special handling for instructions where the size can be
  629. implicitly determined, because only one size is possible. }
  630. if not result and (opsize in [S_NO]) then
  631. begin
  632. result:=(p^.sizes <> []) and (
  633. { if OPS_SHORT is in sizes, it means we have a branch
  634. instruction, so let unsized pass. }
  635. (OPS_SHORT in p^.sizes) or
  636. { Or only one size is possible. }
  637. ((p^.sizes - [ OPS_BYTE ]) = []) or
  638. ((p^.sizes - [ OPS_WORD ]) = []) or
  639. ((p^.sizes - [ OPS_LONG ]) = []));
  640. end;
  641. end;
  642. function OperandsMatch(const oper: toper; const ots: toperandtypeset): boolean;
  643. var
  644. ot: toperandtype;
  645. begin
  646. // fix me: this function could use some improvements, in particular checking
  647. // agains for example CF or 68000 limitations, etc
  648. result:=false;
  649. for ot in ots do
  650. begin
  651. case ot of
  652. OT_DATA:
  653. result:=(oper.typ=top_reg) and isintregister(oper.reg);
  654. OT_ADDR:
  655. result:=(oper.typ=top_reg) and isaddressregister(oper.reg);
  656. OT_ADDR_INDIR:
  657. result:=(oper.typ=top_ref) and isaddressregister(oper.ref^.base)
  658. and (oper.ref^.direction=dir_none) and (oper.ref^.index=NR_NO)
  659. and (oper.ref^.offset=0) and (oper.ref^.symbol=nil);
  660. OT_ADDR_INDIR_POSTINC:
  661. result:=(oper.typ=top_ref) and isaddressregister(oper.ref^.base)
  662. and (oper.ref^.direction=dir_inc) and (oper.ref^.index=NR_NO)
  663. and (oper.ref^.offset=0) and (oper.ref^.symbol=nil);
  664. OT_ADDR_INDIR_PREDEC:
  665. result:=(oper.typ=top_ref) and isaddressregister(oper.ref^.base)
  666. and (oper.ref^.direction=dir_dec) and (oper.ref^.index=NR_NO)
  667. and (oper.ref^.offset=0) and (oper.ref^.symbol=nil);
  668. OT_ADDR_DISP16:
  669. // fix me: also needs checking offset sizes, incl. 020+ base displacements!
  670. result:=(oper.typ=top_ref) and isaddressregister(oper.ref^.base)
  671. and (oper.ref^.direction=dir_none) and (oper.ref^.index=NR_NO)
  672. and (oper.ref^.symbol=nil);
  673. OT_ADDR_IDX_DISP8:
  674. // fix me: also needs checking offset sizes, incl. 020+ base displacements!
  675. result:=(oper.typ=top_ref) and isaddressregister(oper.ref^.base)
  676. and (isaddressregister(oper.ref^.index) or isintregister(oper.ref^.index))
  677. and (oper.ref^.direction=dir_none)
  678. and (oper.ref^.symbol=nil);
  679. OT_ABS_SHORT,
  680. // fix me: also needs checking sizes!
  681. OT_ABS_LONG:
  682. result:=((oper.typ=top_ref) and assigned(oper.ref^.symbol)
  683. and (oper.ref^.base=NR_NO) and (oper.ref^.index=NR_NO)
  684. and (oper.ref^.direction=dir_none)) or
  685. (oper.typ=top_const);
  686. OT_PC_DISP16:
  687. // fix me: also needs checking offset sizes, incl. 020+ base displacements!
  688. result:=(oper.typ=top_ref) and (oper.ref^.base=NR_PC)
  689. and (oper.ref^.direction=dir_none) and (oper.ref^.index=NR_NO)
  690. and (oper.ref^.symbol=nil);
  691. OT_PC_IDX_DISP8:
  692. // fix me: also needs checking offset sizes, incl. 020+ base displacements!
  693. result:=(oper.typ=top_ref) and (oper.ref^.base=NR_PC)
  694. and (isaddressregister(oper.ref^.index) or isintregister(oper.ref^.index))
  695. and (oper.ref^.direction=dir_none)
  696. and (oper.ref^.symbol=nil);
  697. OT_IMMEDIATE:
  698. // fix me: needs checking against OF_IMM_QUICK and others
  699. result:=(oper.typ=top_const);
  700. OT_REG_LIST:
  701. result:=(oper.typ=top_regset) and (oper.fpuregset=[]) and
  702. ((oper.dataregset<>[]) or (oper.addrregset<>[]));
  703. OT_FPUREG_LIST:
  704. result:=(oper.typ=top_regset) and (oper.fpuregset<>[]) and
  705. ((oper.dataregset=[]) or (oper.addrregset=[]));
  706. OT_FPUREG:
  707. result:=(oper.typ=top_reg) and isfpuregister(oper.reg);
  708. {OT_SPECIALREG}
  709. else
  710. internalerror(2023010101);
  711. end;
  712. if result then
  713. break;
  714. end;
  715. end;
  716. var
  717. i: Integer;
  718. begin
  719. result:=false;
  720. { Check the opcode and number of operands }
  721. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  722. exit;
  723. { Check if opcode is valid for this target }
  724. if not TargetMatch then
  725. exit;
  726. { Check if opcode size is valid }
  727. if not OpsizeMatch then
  728. exit;
  729. { Check the operands }
  730. for i:=0 to p^.ops-1 do
  731. if not OperandsMatch(oper[i]^,p^.optypes[i]) then
  732. exit;
  733. result:=true;
  734. end;
  735. function taicpu.FindInsEntry(objdata: TObjData): boolean;
  736. var
  737. i : longint;
  738. begin
  739. result:=false;
  740. { Things which may only be done once, not when a second pass is done to
  741. optimize }
  742. if (InsEntry=nil) then
  743. begin
  744. { set the file postion }
  745. current_filepos:=fileinfo;
  746. end
  747. else
  748. begin
  749. { we've already an insentry so it's valid }
  750. result:=true;
  751. exit;
  752. end;
  753. { Lookup opcode in the table }
  754. InsSize:=-1;
  755. i:=InsTabCache^[opcode];
  756. if i=-1 then
  757. begin
  758. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  759. exit;
  760. end;
  761. InsEntry:=@instab[i];
  762. while (InsEntry^.opcode=opcode) do
  763. begin
  764. if Matches(insentry,objdata) then
  765. begin
  766. result:=true;
  767. exit;
  768. end;
  769. inc(insentry);
  770. end;
  771. Message1(asmw_e_invalid_opcode_and_operands,gas_op2str[opcode]{,GetString});
  772. { No instruction found, set insentry to nil and inssize to -1 }
  773. InsEntry:=nil;
  774. InsSize:=-1;
  775. end;
  776. procedure taicpu.GenCode(objdata: TObjData);
  777. procedure WriteWord(w: word);
  778. var
  779. bytes: array [0..1] of Byte;
  780. begin
  781. Word(bytes):=NToBE(w);
  782. objdata.writebytes(bytes,2);
  783. end;
  784. procedure OpcodeSetReg(opcode: word; regnum: byte);
  785. begin
  786. opcode:=(opcode and $fff8) or (regnum and $7);
  787. end;
  788. procedure OpcodeSetMode(opcode: word; mode: byte);
  789. begin
  790. opcode:=(opcode and $ffc7) or ((mode and $7) shl 3);
  791. end;
  792. procedure OpcodeSetEA(opcode: word; mode: byte; regnum: byte);
  793. begin
  794. opcode:=(opcode and $ffc0) or ((mode and $7) shl 3) or (regnum and $7);
  795. end;
  796. var
  797. i: longint;
  798. begin
  799. // writeln('GenCode: ',insentry^.opcode);
  800. for i:=0 to insentry^.codelen do
  801. WriteWord(insentry^.code[i]);
  802. end;
  803. procedure taicpu.ResetPass1;
  804. begin
  805. { we need to reset everything here, because the choosen insentry
  806. can be invalid for a new situation where the previously optimized
  807. insentry is not correct }
  808. InsEntry:=nil;
  809. InsSize:=0;
  810. LastInsOffset:=-1;
  811. end;
  812. procedure taicpu.ResetPass2;
  813. begin
  814. { we are here in a second pass, check if the instruction can be optimized }
  815. if assigned(InsEntry) then
  816. begin
  817. InsEntry:=nil;
  818. InsSize:=0;
  819. end;
  820. LastInsOffset:=-1;
  821. end;
  822. function taicpu.Pass1(objdata:TObjData):longint;
  823. begin
  824. Pass1:=0;
  825. { Save the old offset and set the new offset }
  826. InsOffset:=ObjData.CurrObjSec.Size;
  827. { Error? }
  828. if (InsEntry=nil) and (InsSize=-1) then
  829. exit;
  830. { set the file postion }
  831. current_filepos:=fileinfo;
  832. { Get InsEntry }
  833. if FindInsEntry(ObjData) then
  834. begin
  835. { Calculate instruction size }
  836. InsSize:=CalcSize(InsEntry);
  837. LastInsOffset:=InsOffset;
  838. Pass1:=InsSize;
  839. exit;
  840. end;
  841. LastInsOffset:=-1;
  842. end;
  843. procedure taicpu.Pass2(objdata: TObjData);
  844. begin
  845. { error in pass1 ? }
  846. if InsEntry=nil then
  847. exit;
  848. current_filepos:=fileinfo;
  849. { Generate the instruction }
  850. GenCode(ObjData);
  851. end;
  852. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  853. begin
  854. case getregtype(r) of
  855. R_INTREGISTER :
  856. result:=taicpu.op_ref_reg(A_MOVE,S_L,ref,r);
  857. R_ADDRESSREGISTER :
  858. result:=taicpu.op_ref_reg(A_MOVE,S_L,ref,r);
  859. R_FPUREGISTER :
  860. result:=taicpu.op_ref_reg(A_FMOVE,fpuregopsize,ref,r);
  861. else
  862. internalerror(200602011);
  863. end;
  864. end;
  865. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  866. begin
  867. case getregtype(r) of
  868. R_INTREGISTER :
  869. result:=taicpu.op_reg_ref(A_MOVE,S_L,r,ref);
  870. R_ADDRESSREGISTER :
  871. result:=taicpu.op_reg_ref(A_MOVE,S_L,r,ref);
  872. R_FPUREGISTER :
  873. result:=taicpu.op_reg_ref(A_FMOVE,fpuregopsize,r,ref);
  874. else
  875. internalerror(200602012);
  876. end;
  877. end;
  878. {****************************************************************************
  879. Instruction table
  880. *****************************************************************************}
  881. procedure BuildInsTabCache;
  882. var
  883. i : longint;
  884. begin
  885. new(InsTabCache);
  886. FillChar(InsTabCache^,sizeof(TInsTabCache),$ff);
  887. i:=0;
  888. while (i<InsTabEntries) do
  889. begin
  890. if InsTabCache^[InsTab[i].OPcode]=-1 then
  891. InsTabCache^[InsTab[i].OPcode]:=i;
  892. inc(i);
  893. end;
  894. end;
  895. procedure InitAsm;
  896. begin
  897. if not assigned(InsTabCache) then
  898. BuildInsTabCache;
  899. end;
  900. procedure DoneAsm;
  901. begin
  902. if assigned(InsTabCache) then
  903. begin
  904. dispose(InsTabCache);
  905. InsTabCache:=nil;
  906. end;
  907. end;
  908. begin
  909. cai_align:=tai_align;
  910. cai_cpu:=taicpu;
  911. end.