aasmcpu.pas 16 KB

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