aasmcpu.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. {
  2. $Id$
  3. Copyright (c) 2003 by Florian Klaempfl
  4. Contains the assembler object for the ARM
  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,globtype,globals,verbose,
  24. cpubase,cpuinfo,cgbase;
  25. const
  26. { "mov reg,reg" source operand number }
  27. O_MOV_SOURCE = 1;
  28. { "mov reg,reg" source operand number }
  29. O_MOV_DEST = 0;
  30. type
  31. taicpu = class(tai_cpu_abstract)
  32. oppostfix : TOpPostfix;
  33. roundingmode : troundingmode;
  34. procedure loadshifterop(opidx:longint;const so:tshifterop);
  35. procedure loadregset(opidx:longint;const s:tcpuregisterset);
  36. constructor op_none(op : tasmop);
  37. constructor op_reg(op : tasmop;_op1 : tregister);
  38. constructor op_const(op : tasmop;_op1 : longint);
  39. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  40. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  41. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: aword);
  42. constructor op_ref_regset(op:tasmop; _op1: treference; _op2: tcpuregisterset);
  43. constructor op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  44. constructor op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aword);
  45. constructor op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  46. constructor op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  47. constructor op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  48. { SFM/LFM }
  49. constructor op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aword;_op3 : treference);
  50. { this is for Jmp instructions }
  51. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  52. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  53. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  54. constructor op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  55. constructor op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  56. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  57. function spilling_get_operation_type(opnr: longint): topertype;override;
  58. end;
  59. tai_align = class(tai_align_abstract)
  60. { nothing to add }
  61. end;
  62. function spilling_create_load(const ref:treference;r:tregister): tai;
  63. function spilling_create_store(r:tregister; const ref:treference): tai;
  64. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  65. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  66. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  67. { inserts pc relative symbols at places where they are reachable }
  68. procedure insertpcrelativedata(list,listtoinsert : taasmoutput);
  69. procedure InitAsm;
  70. procedure DoneAsm;
  71. implementation
  72. uses
  73. cutils,rgobj,itcpugas;
  74. procedure taicpu.loadshifterop(opidx:longint;const so:tshifterop);
  75. begin
  76. allocate_oper(opidx+1);
  77. with oper[opidx]^ do
  78. begin
  79. if typ<>top_shifterop then
  80. begin
  81. clearop(opidx);
  82. new(shifterop);
  83. end;
  84. shifterop^:=so;
  85. typ:=top_shifterop;
  86. if assigned(add_reg_instruction_hook) then
  87. add_reg_instruction_hook(self,shifterop^.rs);
  88. end;
  89. end;
  90. procedure taicpu.loadregset(opidx:longint;const s:tcpuregisterset);
  91. var
  92. i : byte;
  93. begin
  94. allocate_oper(opidx+1);
  95. with oper[opidx]^ do
  96. begin
  97. if typ<>top_regset then
  98. clearop(opidx);
  99. new(regset);
  100. regset^:=s;
  101. typ:=top_regset;
  102. for i:=RS_R0 to RS_R15 do
  103. begin
  104. if assigned(add_reg_instruction_hook) and (i in regset^) then
  105. add_reg_instruction_hook(self,newreg(R_INTREGISTER,i,R_SUBWHOLE));
  106. end;
  107. end;
  108. end;
  109. {*****************************************************************************
  110. taicpu Constructors
  111. *****************************************************************************}
  112. constructor taicpu.op_none(op : tasmop);
  113. begin
  114. inherited create(op);
  115. end;
  116. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  117. begin
  118. inherited create(op);
  119. ops:=1;
  120. loadreg(0,_op1);
  121. end;
  122. constructor taicpu.op_const(op : tasmop;_op1 : longint);
  123. begin
  124. inherited create(op);
  125. ops:=1;
  126. loadconst(0,aword(_op1));
  127. end;
  128. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  129. begin
  130. inherited create(op);
  131. ops:=2;
  132. loadreg(0,_op1);
  133. loadreg(1,_op2);
  134. end;
  135. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: aword);
  136. begin
  137. inherited create(op);
  138. ops:=2;
  139. loadreg(0,_op1);
  140. loadconst(1,aword(_op2));
  141. end;
  142. constructor taicpu.op_ref_regset(op:tasmop; _op1: treference; _op2: tcpuregisterset);
  143. begin
  144. inherited create(op);
  145. ops:=2;
  146. loadref(0,_op1);
  147. loadregset(1,_op2);
  148. end;
  149. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  150. begin
  151. inherited create(op);
  152. ops:=2;
  153. loadreg(0,_op1);
  154. loadref(1,_op2);
  155. end;
  156. constructor taicpu.op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  157. begin
  158. inherited create(op);
  159. ops:=3;
  160. loadreg(0,_op1);
  161. loadreg(1,_op2);
  162. loadreg(2,_op3);
  163. end;
  164. constructor taicpu.op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aword);
  165. begin
  166. inherited create(op);
  167. ops:=3;
  168. loadreg(0,_op1);
  169. loadreg(1,_op2);
  170. loadconst(2,aword(_op3));
  171. end;
  172. constructor taicpu.op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aword;_op3 : treference);
  173. begin
  174. inherited create(op);
  175. ops:=3;
  176. loadreg(0,_op1);
  177. loadconst(1,_op2);
  178. loadref(2,_op3);
  179. end;
  180. constructor taicpu.op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  181. begin
  182. inherited create(op);
  183. ops:=3;
  184. loadreg(0,_op1);
  185. loadreg(1,_op2);
  186. loadsymbol(0,_op3,_op3ofs);
  187. end;
  188. constructor taicpu.op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  189. begin
  190. inherited create(op);
  191. ops:=3;
  192. loadreg(0,_op1);
  193. loadreg(1,_op2);
  194. loadref(2,_op3);
  195. end;
  196. constructor taicpu.op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  197. begin
  198. inherited create(op);
  199. ops:=3;
  200. loadreg(0,_op1);
  201. loadreg(1,_op2);
  202. loadshifterop(2,_op3);
  203. end;
  204. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  205. begin
  206. inherited create(op);
  207. condition:=cond;
  208. ops:=1;
  209. loadsymbol(0,_op1,0);
  210. end;
  211. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  212. begin
  213. inherited create(op);
  214. ops:=1;
  215. loadsymbol(0,_op1,0);
  216. end;
  217. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  218. begin
  219. inherited create(op);
  220. ops:=1;
  221. loadsymbol(0,_op1,_op1ofs);
  222. end;
  223. constructor taicpu.op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  224. begin
  225. inherited create(op);
  226. ops:=2;
  227. loadreg(0,_op1);
  228. loadsymbol(1,_op2,_op2ofs);
  229. end;
  230. constructor taicpu.op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  231. begin
  232. inherited create(op);
  233. ops:=2;
  234. loadsymbol(0,_op1,_op1ofs);
  235. loadref(1,_op2);
  236. end;
  237. { ****************************** newra stuff *************************** }
  238. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  239. begin
  240. { allow the register allocator to remove unnecessary moves }
  241. result:=(((opcode=A_MOV) and (regtype = R_INTREGISTER)) or
  242. ((opcode=A_MVF) and (regtype = R_FPUREGISTER))
  243. ) and
  244. (condition=C_None) and
  245. (ops=2) and
  246. (oper[0]^.typ=top_reg) and
  247. (oper[1]^.typ=top_reg) and
  248. (oper[0]^.reg=oper[1]^.reg);
  249. end;
  250. function spilling_create_load(const ref:treference;r:tregister): tai;
  251. begin
  252. case getregtype(r) of
  253. R_INTREGISTER :
  254. result:=taicpu.op_reg_ref(A_LDR,r,ref);
  255. R_FPUREGISTER :
  256. { use lfm because we don't know the current internal format
  257. and avoid exceptions
  258. }
  259. result:=taicpu.op_reg_const_ref(A_LFM,r,1,ref);
  260. else
  261. internalerror(200401041);
  262. end;
  263. end;
  264. function spilling_create_store(r:tregister; const ref:treference): tai;
  265. begin
  266. case getregtype(r) of
  267. R_INTREGISTER :
  268. result:=taicpu.op_reg_ref(A_STR,r,ref);
  269. R_FPUREGISTER :
  270. { use sfm because we don't know the current internal format
  271. and avoid exceptions
  272. }
  273. result:=taicpu.op_reg_const_ref(A_SFM,r,1,ref);
  274. else
  275. internalerror(200401041);
  276. end;
  277. end;
  278. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  279. begin
  280. case opcode of
  281. A_ADC,A_ADD,A_AND,
  282. A_EOR,A_CLZ,
  283. A_LDR,A_LDRB,A_LDRD,A_LDRBT,A_LDRH,A_LDRSB,
  284. A_LDRSH,A_LDRT,
  285. A_MOV,A_MVN,A_MLA,A_MUL,
  286. A_ORR,A_RSB,A_RSC,A_SBC,A_SUB,
  287. A_SWP,A_SWPB,
  288. A_LDF,A_FLT,A_FIX,
  289. A_ADF,A_DVF,A_FDV,A_FML,
  290. A_RFS,A_RFC,A_RDF,
  291. A_RMF,A_RPW,A_RSF,A_SUF,A_ABS,A_ACS,A_ASN,A_ATN,A_COS,
  292. A_EXP,A_LOG,A_LGN,A_MVF,A_MNF,A_FRD,A_MUF,A_POL,A_RND,A_SIN,A_SQT,A_TAN,
  293. A_LFM:
  294. if opnr=0 then
  295. result:=operand_write
  296. else
  297. result:=operand_read;
  298. A_BIC,A_BKPT,A_B,A_BL,A_BLX,A_BX,
  299. A_CMN,A_CMP,A_TEQ,A_TST,
  300. A_CMF,A_CMFE,A_WFS,A_CNF:
  301. result:=operand_read;
  302. A_SMLAL,A_UMLAL:
  303. if opnr in [0,1] then
  304. result:=operand_readwrite
  305. else
  306. result:=operand_read;
  307. A_SMULL,A_UMULL:
  308. if opnr in [0,1] then
  309. result:=operand_write
  310. else
  311. result:=operand_read;
  312. A_STR,A_STRB,A_STRBT,A_STRD,
  313. A_STRH,A_STRT,A_STF,A_SFM:
  314. { important is what happens with the involved registers }
  315. if opnr=0 then
  316. result := operand_read
  317. else
  318. { check for pre/post indexed }
  319. result := operand_read
  320. else
  321. internalerror(200403151);
  322. end;
  323. end;
  324. procedure InitAsm;
  325. begin
  326. end;
  327. procedure DoneAsm;
  328. begin
  329. end;
  330. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  331. begin
  332. i.oppostfix:=pf;
  333. result:=i;
  334. end;
  335. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  336. begin
  337. i.roundingmode:=rm;
  338. result:=i;
  339. end;
  340. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  341. begin
  342. i.condition:=c;
  343. result:=i;
  344. end;
  345. procedure insertpcrelativedata(list,listtoinsert : taasmoutput);
  346. var
  347. curpos : longint;
  348. lastpos : longint;
  349. curop : longint;
  350. curtai : tai;
  351. curdatatai,hp : tai;
  352. curdata : taasmoutput;
  353. l : tasmlabel;
  354. begin
  355. curdata:=taasmoutput.create;
  356. lastpos:=-1;
  357. curpos:=0;
  358. curtai:=tai(list.first);
  359. while assigned(curtai) do
  360. begin
  361. { instruction? }
  362. if curtai.typ=ait_instruction then
  363. begin
  364. { walk through all operand of the instruction }
  365. for curop:=0 to taicpu(curtai).ops-1 do
  366. begin
  367. { reference? }
  368. if (taicpu(curtai).oper[curop]^.typ=top_ref) then
  369. begin
  370. { pc relative symbol? }
  371. curdatatai:=tai(taicpu(curtai).oper[curop]^.ref^.symboldata);
  372. if assigned(curdatatai) then
  373. begin
  374. { if yes, insert till next symbol }
  375. repeat
  376. hp:=tai(curdatatai.next);
  377. listtoinsert.remove(curdatatai);
  378. curdata.concat(curdatatai);
  379. curdatatai:=hp;
  380. until (curdatatai=nil) or (curdatatai.typ=ait_label);
  381. if lastpos=-1 then
  382. lastpos:=curpos;
  383. end;
  384. end;
  385. end;
  386. inc(curpos);
  387. end;
  388. if (curpos-lastpos)>1020 then
  389. begin
  390. lastpos:=curpos;
  391. hp:=tai(curtai.next);
  392. objectlibrary.getlabel(l);
  393. curdata.insert(taicpu.op_sym(A_B,l));
  394. curdata.concat(tai_label.create(l));
  395. list.insertlistafter(curtai,curdata);
  396. curtai:=hp;
  397. end
  398. else
  399. curtai:=tai(curtai.next);
  400. end;
  401. list.concatlist(curdata);
  402. curdata.free;
  403. end;
  404. end.
  405. {
  406. $Log$
  407. Revision 1.34 2004-07-04 15:22:34 florian
  408. * fixed float spilling to use sfm/lfm instead of stf/ldf
  409. Revision 1.33 2004/06/20 08:55:31 florian
  410. * logs truncated
  411. Revision 1.32 2004/06/16 20:07:10 florian
  412. * dwarf branch merged
  413. Revision 1.31.2.2 2004/06/13 10:51:17 florian
  414. * fixed several register allocator problems (sparc/arm)
  415. Revision 1.31.2.1 2004/06/12 17:01:01 florian
  416. * fixed compilation of arm compiler
  417. Revision 1.31 2004/03/29 19:19:35 florian
  418. + arm floating point register saving implemented
  419. * hopefully stabs generation for MacOSX fixed
  420. + some defines for arm added
  421. Revision 1.30 2004/03/15 22:20:13 florian
  422. * handling of spilling improved
  423. }