aasmcpu.pas 15 KB

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