aasmcpu.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. {
  2. $Id$
  3. Copyright (c) 1999-2002 by Mazen Neifer
  4. Contains the assembler object for the SPARC
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit aasmcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,aasmtai,
  23. aasmbase,globals,verbose,
  24. cpubase,cpuinfo;
  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. type
  31. taicpu = class(taicpu_abstract)
  32. constructor op_none(op : tasmop);
  33. constructor op_reg(op : tasmop;_op1 : tregister);
  34. constructor op_ref(op : tasmop;const _op1 : treference);
  35. constructor op_const(op : tasmop;_op1 : aword);
  36. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  37. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  38. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: aword);
  39. constructor op_const_reg(op:tasmop; _op1: aword; _op2: tregister);
  40. constructor op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  41. constructor op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  42. constructor op_reg_ref_reg(op:tasmop;_op1:TRegister;_op2:TReference;_op3:tregister);
  43. constructor op_reg_const_reg(op:tasmop;_op1:TRegister;_op2:aword;_op3:tregister);
  44. { this is for Jmp instructions }
  45. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  46. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  47. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  48. { register allocation }
  49. function is_nop:boolean;override;
  50. function is_move:boolean;override;
  51. function spill_registers(list:Taasmoutput;
  52. rgget:Trggetproc;
  53. rgunget:Trgungetproc;
  54. r:Tsupregset;
  55. var unusedregsint:Tsupregset;
  56. const spilltemplist:Tspill_temp_list):boolean; override;
  57. end;
  58. tai_align = class(tai_align_abstract)
  59. { nothing to add }
  60. end;
  61. procedure InitAsm;
  62. procedure DoneAsm;
  63. implementation
  64. {*****************************************************************************
  65. taicpu Constructors
  66. *****************************************************************************}
  67. constructor taicpu.op_none(op : tasmop);
  68. begin
  69. inherited create(op);
  70. end;
  71. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  72. begin
  73. inherited create(op);
  74. if (_op1.enum = R_INTREGISTER) and (_op1.number = NR_NO) then
  75. internalerror(2003031207);
  76. ops:=1;
  77. loadreg(0,_op1);
  78. end;
  79. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  80. begin
  81. inherited create(op);
  82. ops:=1;
  83. loadref(0,_op1);
  84. end;
  85. constructor taicpu.op_const(op : tasmop;_op1 : aword);
  86. begin
  87. inherited create(op);
  88. ops:=1;
  89. loadconst(0,_op1);
  90. end;
  91. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  92. begin
  93. inherited create(op);
  94. if (_op1.enum = R_INTREGISTER) and (_op1.number = NR_NO) then
  95. internalerror(2003031205);
  96. if (_op2.enum = R_INTREGISTER) and (_op2.number = NR_NO) then
  97. internalerror(2003031206);
  98. ops:=2;
  99. loadreg(0,_op1);
  100. loadreg(1,_op2);
  101. end;
  102. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: aword);
  103. begin
  104. inherited create(op);
  105. if (_op1.enum = R_INTREGISTER) and (_op1.number = NR_NO) then
  106. internalerror(2003031208);
  107. ops:=2;
  108. loadreg(0,_op1);
  109. loadconst(1,_op2);
  110. end;
  111. constructor taicpu.op_const_reg(op:tasmop; _op1: aword; _op2: tregister);
  112. begin
  113. inherited create(op);
  114. if (_op2.enum = R_INTREGISTER) and (_op2.number = NR_NO) then
  115. internalerror(2003031209);
  116. ops:=2;
  117. loadconst(0,_op1);
  118. loadreg(1,_op2);
  119. end;
  120. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  121. begin
  122. inherited create(op);
  123. if (_op1.enum = R_INTREGISTER) and (_op1.number = NR_NO) then
  124. internalerror(2003031210);
  125. ops:=2;
  126. loadreg(0,_op1);
  127. loadref(1,_op2);
  128. end;
  129. constructor taicpu.op_ref_reg(op : tasmop;const _op1 : treference;_op2 : tregister);
  130. begin
  131. inherited create(op);
  132. if (_op2.enum = R_INTREGISTER) and (_op2.number = NR_NO) then
  133. internalerror(2003031210);
  134. ops:=2;
  135. loadref(0,_op1);
  136. loadreg(1,_op2);
  137. end;
  138. constructor taicpu.op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  139. begin
  140. inherited create(op);
  141. if (_op1.enum = R_INTREGISTER) and (_op1.number = NR_NO) then
  142. internalerror(2003031211);
  143. if (_op2.enum = R_INTREGISTER) and (_op2.number = NR_NO) then
  144. internalerror(2003031212);
  145. if (_op3.enum = R_INTREGISTER) and (_op3.number = NR_NO) then
  146. internalerror(2003031213);
  147. ops:=3;
  148. loadreg(0,_op1);
  149. loadreg(1,_op2);
  150. loadreg(2,_op3);
  151. end;
  152. constructor taicpu.op_reg_ref_reg(op:tasmop;_op1:TRegister;_op2:TReference;_op3:tregister);
  153. begin
  154. inherited create(op);
  155. if (_op1.enum = R_INTREGISTER) and (_op1.number = NR_NO) then
  156. internalerror(2003031214);
  157. if (_op3.enum = R_INTREGISTER) and (_op3.number = NR_NO) then
  158. internalerror(2003031215);
  159. { only allowed to load the address }
  160. if not(_op2.symaddr in [refs_lo,refs_hi]) then
  161. internalerror(200305311);
  162. ops:=3;
  163. loadreg(0,_op1);
  164. loadref(1,_op2);
  165. loadreg(2,_op3);
  166. end;
  167. constructor taicpu.op_reg_const_reg(op:tasmop;_op1:TRegister;_op2:aword;_op3:tregister);
  168. begin
  169. inherited create(op);
  170. if (_op1.enum = R_INTREGISTER) and (_op1.number = NR_NO) then
  171. internalerror(2003031216);
  172. if (_op3.enum = R_INTREGISTER) and (_op3.number = NR_NO) then
  173. internalerror(2003031217);
  174. ops:=3;
  175. loadreg(0,_op1);
  176. loadconst(1,_op2);
  177. loadreg(2,_op3);
  178. end;
  179. constructor taicpu.op_cond_sym(op:tasmop;cond:TAsmCond;_op1:tasmsymbol);
  180. begin
  181. inherited create(op);
  182. condition:=cond;
  183. ops:=1;
  184. loadsymbol(0,_op1,0);
  185. end;
  186. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  187. begin
  188. inherited create(op);
  189. ops:=1;
  190. loadsymbol(0,_op1,0);
  191. end;
  192. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  193. begin
  194. inherited create(op);
  195. ops:=1;
  196. loadsymbol(0,_op1,_op1ofs);
  197. end;
  198. function taicpu.is_nop:boolean;
  199. begin
  200. result:=(opcode=A_NOP);
  201. end;
  202. function taicpu.is_move:boolean;
  203. begin
  204. result:=(opcode=A_MOV) and
  205. (oper[0].typ=top_reg) and
  206. (oper[1].typ=top_reg);
  207. end;
  208. function taicpu.spill_registers(list:Taasmoutput;
  209. rgget:Trggetproc;
  210. rgunget:Trgungetproc;
  211. r:Tsupregset;
  212. var unusedregsint:Tsupregset;
  213. const spilltemplist:Tspill_temp_list): boolean;
  214. function decode_loadstore(op: tasmop; var counterpart: tasmop; var wasload: boolean): boolean;
  215. begin
  216. result := true;
  217. wasload := true;
  218. case op of
  219. A_LDSB,
  220. A_LDUB :
  221. begin
  222. counterpart := A_STB;
  223. end;
  224. A_LDSH,
  225. A_LDUH:
  226. begin
  227. counterpart := A_STH;
  228. end;
  229. A_LD :
  230. begin
  231. counterpart := A_ST;
  232. wasload := false;
  233. end;
  234. A_LDD:
  235. begin
  236. counterpart := A_STD;
  237. wasload := false;
  238. end;
  239. else
  240. result := false;
  241. end;
  242. end;
  243. var i:byte;
  244. supreg, reg1, reg2, reg3: Tsuperregister;
  245. helpreg:Tregister;
  246. helpins:Taicpu;
  247. op:Tasmop;
  248. pos:Tai;
  249. wasload: boolean;
  250. begin
  251. spill_registers:=false;
  252. if (ops = 2) and
  253. (oper[1].typ=top_ref) and
  254. { oper[1] can also be ref in case of "lis r3,symbol@ha" or so }
  255. decode_loadstore(opcode,op,wasload) then
  256. begin
  257. { the register that's being stored/loaded }
  258. supreg:=oper[0].reg.number shr 8;
  259. if supreg in r then
  260. begin
  261. // Example:
  262. // l?? r20d, 8(r1) ; r20d must be spilled into -60(r1)
  263. //
  264. // Change into:
  265. //
  266. // l?? r21d, 8(r1)
  267. // st? r21d, -60(r1)
  268. //
  269. // And:
  270. //
  271. // st? r20d, 8(r1) ; r20d must be spilled into -60(r1)
  272. //
  273. // Change into:
  274. //
  275. // l?? r21d, -60(r1)
  276. // st? r21d, 8(r1)
  277. pos := get_insert_pos(Tai(previous),oper[0].reg.number shr 8,
  278. oper[1].ref^.base.number shr 8,
  279. oper[1].ref^.index.number shr 8,
  280. unusedregsint);
  281. rgget(list,pos,0,helpreg);
  282. spill_registers := true;
  283. if wasload then
  284. begin
  285. helpins := taicpu.op_reg_ref(opcode,helpreg,oper[1].ref^);
  286. loadref(1,spilltemplist[supreg]);
  287. opcode := op;
  288. end
  289. else
  290. helpins := taicpu.op_reg_ref(op,helpreg,spilltemplist[supreg]);
  291. if pos=nil then
  292. list.insertafter(helpins,list.first)
  293. else
  294. list.insertafter(helpins,pos.next);
  295. loadreg(0,helpreg);
  296. rgunget(list,helpins,helpreg);
  297. forward_allocation(tai(helpins.next),unusedregsint);
  298. {
  299. writeln('spilling!');
  300. list.insertafter(tai_comment.Create(strpnew('Spilling!')),helpins);
  301. }
  302. end;
  303. { now the registers used in the reference }
  304. { a) base }
  305. supreg := oper[1].ref^.base.number shr 8;
  306. if supreg in r then
  307. begin
  308. if wasload then
  309. pos:=get_insert_pos(Tai(previous),oper[1].ref^.index.number shr 8,oper[0].reg.number shr 8,0,unusedregsint)
  310. else
  311. pos:=get_insert_pos(Tai(previous),oper[1].ref^.index.number shr 8,0,0,unusedregsint);
  312. rgget(list,pos,0,helpreg);
  313. spill_registers:=true;
  314. helpins:=Taicpu.op_reg_ref(A_LD,helpreg,spilltemplist[supreg]);
  315. if pos=nil then
  316. list.insertafter(helpins,list.first)
  317. else
  318. list.insertafter(helpins,pos.next);
  319. oper[1].ref^.base:=helpreg;
  320. rgunget(list,helpins,helpreg);
  321. forward_allocation(Tai(helpins.next),unusedregsint);
  322. {
  323. writeln('spilling!');
  324. list.insertafter(tai_comment.Create(strpnew('Spilling!')),helpins);
  325. }
  326. end;
  327. { b) index }
  328. supreg := oper[1].ref^.index.number shr 8;
  329. if supreg in r then
  330. begin
  331. if wasload then
  332. pos:=get_insert_pos(Tai(previous),oper[1].ref^.base.number shr 8,oper[0].reg.number shr 8,0,unusedregsint)
  333. else
  334. pos:=get_insert_pos(Tai(previous),oper[1].ref^.base.number shr 8,0,0,unusedregsint);
  335. rgget(list,pos,0,helpreg);
  336. spill_registers:=true;
  337. helpins:=Taicpu.op_reg_ref(A_LD,helpreg,spilltemplist[supreg]);
  338. if pos=nil then
  339. list.insertafter(helpins,list.first)
  340. else
  341. list.insertafter(helpins,pos.next);
  342. oper[1].ref^.index:=helpreg;
  343. rgunget(list,helpins,helpreg);
  344. forward_allocation(Tai(helpins.next),unusedregsint);
  345. {
  346. writeln('spilling!');
  347. list.insertafter(tai_comment.Create(strpnew('Spilling!')),helpins);
  348. }
  349. end;
  350. { load/store is done }
  351. exit;
  352. end;
  353. { all other instructions the compiler generates are the same (I hope): }
  354. { operand 0 is a register and is the destination, the others are sources }
  355. { and can be either registers or constants }
  356. { exception: branches (is_jmp isn't always set for them) }
  357. if oper[0].typ <> top_reg then
  358. exit;
  359. reg1 := oper[0].reg.number shr 8;
  360. if oper[1].typ = top_reg then
  361. reg2 := oper[1].reg.number shr 8
  362. else
  363. reg2 := 0;
  364. if (ops >= 3) and
  365. (oper[2].typ = top_reg) then
  366. reg3 := oper[2].reg.number shr 8
  367. else
  368. reg3 := 0;
  369. supreg:=reg1;
  370. if supreg in r then
  371. begin
  372. // Example:
  373. // add r20d, r21d, r22d ; r20d must be spilled into -60(r1)
  374. //
  375. // Change into:
  376. //
  377. // lwz r23d, -60(r1)
  378. // add r23d, r21d, r22d
  379. // stw r23d, -60(r1)
  380. pos := get_insert_pos(Tai(previous),reg1,reg2,reg3,unusedregsint);
  381. rgget(list,pos,0,helpreg);
  382. spill_registers := true;
  383. helpins := taicpu.op_reg_ref(A_ST,helpreg,spilltemplist[supreg]);
  384. list.insertafter(helpins,self);
  385. helpins := taicpu.op_reg_ref(A_LD,helpreg,spilltemplist[supreg]);
  386. if pos=nil then
  387. list.insertafter(helpins,list.first)
  388. else
  389. list.insertafter(helpins,pos.next);
  390. loadreg(0,helpreg);
  391. rgunget(list,helpins,helpreg);
  392. forward_allocation(tai(helpins.next),unusedregsint);
  393. {
  394. writeln('spilling!');
  395. list.insertafter(tai_comment.Create(strpnew('Spilling!')),helpins);
  396. }
  397. end;
  398. for i := 1 to 2 do
  399. if (oper[i].typ = top_reg) then
  400. begin
  401. supreg:=oper[i].reg.number shr 8;
  402. if supreg in r then
  403. begin
  404. // Example:
  405. // add r20d, r21d, r22d ; r20d must be spilled into -60(r1)
  406. //
  407. // Change into:
  408. //
  409. // lwz r23d, -60(r1)
  410. // add r23d, r21d, r22d
  411. // stw r23d, -60(r1)
  412. pos := get_insert_pos(Tai(previous),reg1,reg2,reg3,unusedregsint);
  413. rgget(list,pos,0,helpreg);
  414. spill_registers := true;
  415. helpins := taicpu.op_reg_ref(A_LD,helpreg,spilltemplist[supreg]);
  416. if pos=nil then
  417. list.insertafter(helpins,list.first)
  418. else
  419. list.insertafter(helpins,pos.next);
  420. loadreg(i,helpreg);
  421. rgunget(list,helpins,helpreg);
  422. forward_allocation(tai(helpins.next),unusedregsint);
  423. {
  424. writeln('spilling!');
  425. list.insertafter(tai_comment.Create(strpnew('Spilling!')),helpins);
  426. }
  427. end;
  428. end;
  429. end;
  430. procedure InitAsm;
  431. begin
  432. end;
  433. procedure DoneAsm;
  434. begin
  435. end;
  436. end.
  437. {
  438. $Log$
  439. Revision 1.31 2003-08-11 21:18:20 peter
  440. * start of sparc support for newra
  441. Revision 1.30 2003/06/14 14:53:50 jonas
  442. * fixed newra cycle for x86
  443. * added constants for indicating source and destination operands of the
  444. "move reg,reg" instruction to aasmcpu (and use those in rgobj)
  445. Revision 1.29 2003/06/12 16:43:07 peter
  446. * newra compiles for sparc
  447. Revision 1.28 2003/06/01 01:03:41 peter
  448. * remove unsupported combinations
  449. * reg_ref_reg only allowed for refs_lo,refs_hi
  450. Revision 1.27 2003/05/30 23:57:08 peter
  451. * more sparc cleanup
  452. * accumulator removed, splitted in function_return_reg (called) and
  453. function_result_reg (caller)
  454. }