aasmcpu.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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,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(taicpu_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. { this is for Jmp instructions }
  49. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  50. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  51. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  52. constructor op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  53. constructor op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  54. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  55. { register spilling code }
  56. function spilling_create_load(const ref:treference;r:tregister): tai;override;
  57. function spilling_create_store(r:tregister; const ref:treference): tai;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 setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  64. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  65. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  66. { inserts pc relative symbols at places where they are reachable }
  67. procedure insertpcrelativedata(list,listtoinsert : taasmoutput);
  68. procedure InitAsm;
  69. procedure DoneAsm;
  70. implementation
  71. uses
  72. cutils,rgobj,itcpugas;
  73. procedure taicpu.loadshifterop(opidx:longint;const so:tshifterop);
  74. begin
  75. allocate_oper(opidx+1);
  76. with oper[opidx]^ do
  77. begin
  78. if typ<>top_shifterop then
  79. begin
  80. clearop(opidx);
  81. new(shifterop);
  82. end;
  83. shifterop^:=so;
  84. typ:=top_shifterop;
  85. if assigned(add_reg_instruction_hook) then
  86. add_reg_instruction_hook(self,shifterop^.rs);
  87. end;
  88. end;
  89. procedure taicpu.loadregset(opidx:longint;const s:tcpuregisterset);
  90. var
  91. i : byte;
  92. begin
  93. allocate_oper(opidx+1);
  94. with oper[opidx]^ do
  95. begin
  96. if typ<>top_regset then
  97. clearop(opidx);
  98. new(regset);
  99. regset^:=s;
  100. typ:=top_regset;
  101. for i:=RS_R0 to RS_R15 do
  102. begin
  103. if assigned(add_reg_instruction_hook) and (i in regset^) then
  104. add_reg_instruction_hook(self,newreg(R_INTREGISTER,i,R_SUBWHOLE));
  105. end;
  106. end;
  107. end;
  108. {*****************************************************************************
  109. taicpu Constructors
  110. *****************************************************************************}
  111. constructor taicpu.op_none(op : tasmop);
  112. begin
  113. inherited create(op);
  114. end;
  115. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  116. begin
  117. inherited create(op);
  118. ops:=1;
  119. loadreg(0,_op1);
  120. end;
  121. constructor taicpu.op_const(op : tasmop;_op1 : longint);
  122. begin
  123. inherited create(op);
  124. ops:=1;
  125. loadconst(0,aword(_op1));
  126. end;
  127. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  128. begin
  129. inherited create(op);
  130. ops:=2;
  131. loadreg(0,_op1);
  132. loadreg(1,_op2);
  133. end;
  134. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: aword);
  135. begin
  136. inherited create(op);
  137. ops:=2;
  138. loadreg(0,_op1);
  139. loadconst(1,aword(_op2));
  140. end;
  141. constructor taicpu.op_ref_regset(op:tasmop; _op1: treference; _op2: tcpuregisterset);
  142. begin
  143. inherited create(op);
  144. ops:=2;
  145. loadref(0,_op1);
  146. loadregset(1,_op2);
  147. end;
  148. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  149. begin
  150. inherited create(op);
  151. ops:=2;
  152. loadreg(0,_op1);
  153. loadref(1,_op2);
  154. end;
  155. constructor taicpu.op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  156. begin
  157. inherited create(op);
  158. ops:=3;
  159. loadreg(0,_op1);
  160. loadreg(1,_op2);
  161. loadreg(2,_op3);
  162. end;
  163. constructor taicpu.op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aword);
  164. begin
  165. inherited create(op);
  166. ops:=3;
  167. loadreg(0,_op1);
  168. loadreg(1,_op2);
  169. loadconst(2,aword(_op3));
  170. end;
  171. constructor taicpu.op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  172. begin
  173. inherited create(op);
  174. ops:=3;
  175. loadreg(0,_op1);
  176. loadreg(1,_op2);
  177. loadsymbol(0,_op3,_op3ofs);
  178. end;
  179. constructor taicpu.op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  180. begin
  181. inherited create(op);
  182. ops:=3;
  183. loadreg(0,_op1);
  184. loadreg(1,_op2);
  185. loadref(2,_op3);
  186. end;
  187. constructor taicpu.op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  188. begin
  189. inherited create(op);
  190. ops:=3;
  191. loadreg(0,_op1);
  192. loadreg(1,_op2);
  193. loadshifterop(2,_op3);
  194. end;
  195. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  196. begin
  197. inherited create(op);
  198. condition:=cond;
  199. ops:=1;
  200. loadsymbol(0,_op1,0);
  201. end;
  202. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  203. begin
  204. inherited create(op);
  205. ops:=1;
  206. loadsymbol(0,_op1,0);
  207. end;
  208. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  209. begin
  210. inherited create(op);
  211. ops:=1;
  212. loadsymbol(0,_op1,_op1ofs);
  213. end;
  214. constructor taicpu.op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  215. begin
  216. inherited create(op);
  217. ops:=2;
  218. loadreg(0,_op1);
  219. loadsymbol(1,_op2,_op2ofs);
  220. end;
  221. constructor taicpu.op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  222. begin
  223. inherited create(op);
  224. ops:=2;
  225. loadsymbol(0,_op1,_op1ofs);
  226. loadref(1,_op2);
  227. end;
  228. { ****************************** newra stuff *************************** }
  229. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  230. begin
  231. { allow the register allocator to remove unnecessary moves }
  232. result:=(((opcode=A_MOV) and (regtype = R_INTREGISTER)) or
  233. ((opcode=A_MVF) and (regtype = R_FPUREGISTER))
  234. ) and
  235. (condition=C_None) and
  236. (ops=2) and
  237. (oper[0]^.typ=top_reg) and
  238. (oper[1]^.typ=top_reg) and
  239. (oper[0]^.reg=oper[1]^.reg);
  240. end;
  241. function taicpu.spilling_create_load(const ref:treference;r:tregister): tai;
  242. begin
  243. internalerror(200401261);
  244. end;
  245. function taicpu.spilling_create_store(r:tregister; const ref:treference): tai;
  246. begin
  247. internalerror(200401262);
  248. end;
  249. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  250. begin
  251. case opcode of
  252. A_ADC,A_ADD,A_AND,
  253. A_EOR,A_CLZ,
  254. A_LDR,A_LDRB,A_LDRD,A_LDRBT,A_LDRH,A_LDRSB,
  255. A_LDRSH,A_LDRT,
  256. A_MOV,A_MVN,A_MLA,A_MUL,
  257. A_ORR,A_RSB,A_RSC,A_SBC,A_SUB,
  258. A_SWP,A_SWPB,
  259. A_LDF,A_FLT,A_FIX,
  260. A_ADF,A_DVF,A_FDV,A_FML,
  261. A_RFS,A_RFC,A_RDF,
  262. A_RMF,A_RPW,A_RSF,A_SUF,A_ABS,A_ACS,A_ASN,A_ATN,A_COS,
  263. A_EXP,A_LOG,A_LGN,A_MVF,A_MNF,A_FRD,A_MUF,A_POL,A_RND,A_SIN,A_SQT,A_TAN:
  264. if opnr=0 then
  265. result:=operand_write
  266. else
  267. result:=operand_read;
  268. A_BIC,A_BKPT,A_B,A_BL,A_BLX,A_BX,
  269. A_CMN,A_CMP,A_TEQ,A_TST,
  270. A_CMF,A_CMFE,A_WFS,A_CNF:
  271. result:=operand_read;
  272. A_SMLAL,A_UMLAL:
  273. if opnr in [0,1] then
  274. result:=operand_readwrite
  275. else
  276. result:=operand_read;
  277. A_SMULL,A_UMULL:
  278. if opnr in [0,1] then
  279. result:=operand_write
  280. else
  281. result:=operand_read;
  282. A_STR,A_STRB,A_STRBT,A_STRD,
  283. A_STRH,A_STRT,A_STF:
  284. { important is what happens with the involved registers }
  285. if opnr=0 then
  286. result := operand_read
  287. else
  288. { check for pre/post indexed }
  289. result := operand_read
  290. else
  291. internalerror(200403151);
  292. end;
  293. end;
  294. procedure InitAsm;
  295. begin
  296. end;
  297. procedure DoneAsm;
  298. begin
  299. end;
  300. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  301. begin
  302. i.oppostfix:=pf;
  303. result:=i;
  304. end;
  305. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  306. begin
  307. i.roundingmode:=rm;
  308. result:=i;
  309. end;
  310. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  311. begin
  312. i.condition:=c;
  313. result:=i;
  314. end;
  315. procedure insertpcrelativedata(list,listtoinsert : taasmoutput);
  316. var
  317. curpos : longint;
  318. lastpos : longint;
  319. curop : longint;
  320. curtai : tai;
  321. curdatatai,hp : tai;
  322. curdata : taasmoutput;
  323. l : tasmlabel;
  324. begin
  325. curdata:=taasmoutput.create;
  326. lastpos:=-1;
  327. curpos:=0;
  328. curtai:=tai(list.first);
  329. while assigned(curtai) do
  330. begin
  331. { instruction? }
  332. if curtai.typ=ait_instruction then
  333. begin
  334. { walk through all operand of the instruction }
  335. for curop:=0 to taicpu(curtai).ops-1 do
  336. begin
  337. { reference? }
  338. if (taicpu(curtai).oper[curop]^.typ=top_ref) then
  339. begin
  340. { pc relative symbol? }
  341. curdatatai:=tai(taicpu(curtai).oper[curop]^.ref^.symboldata);
  342. if assigned(curdatatai) then
  343. begin
  344. { if yes, insert till next symbol }
  345. repeat
  346. hp:=tai(curdatatai.next);
  347. listtoinsert.remove(curdatatai);
  348. curdata.concat(curdatatai);
  349. curdatatai:=hp;
  350. until (curdatatai=nil) or (curdatatai.typ=ait_label);
  351. if lastpos=-1 then
  352. lastpos:=curpos;
  353. end;
  354. end;
  355. end;
  356. inc(curpos);
  357. end;
  358. if (curpos-lastpos)>1020 then
  359. begin
  360. lastpos:=curpos;
  361. hp:=tai(curtai.next);
  362. objectlibrary.getlabel(l);
  363. curdata.insert(taicpu.op_sym(A_B,l));
  364. curdata.concat(tai_label.create(l));
  365. list.insertlistafter(curtai,curdata);
  366. curtai:=hp;
  367. end
  368. else
  369. curtai:=tai(curtai.next);
  370. end;
  371. list.concatlist(curdata);
  372. curdata.free;
  373. end;
  374. end.
  375. {
  376. $Log$
  377. Revision 1.30 2004-03-15 22:20:13 florian
  378. * handling of spilling improved
  379. Revision 1.29 2004/03/14 16:15:39 florian
  380. * spilling problem fixed
  381. * handling of floating point memory references fixed
  382. Revision 1.28 2004/02/09 22:48:45 florian
  383. * several fixes to parameter handling on arm
  384. Revision 1.27 2004/02/08 23:10:21 jonas
  385. * taicpu.is_same_reg_move() now gets a regtype parameter so it only
  386. removes moves of that particular register type. This is necessary so
  387. we don't remove the live_start instruction of a register before it
  388. has been processed
  389. Revision 1.26 2004/02/08 20:15:42 jonas
  390. - removed taicpu.is_reg_move because it's not used anymore
  391. + support tracking fpu register moves by rgobj for the ppc
  392. Revision 1.25 2004/01/26 19:05:56 florian
  393. * fixed several arm issues
  394. Revision 1.24 2004/01/24 20:19:46 florian
  395. * fixed some spilling stuff
  396. + not(<int64>) implemented
  397. + small set comparisations implemented
  398. Revision 1.23 2004/01/23 15:12:49 florian
  399. * fixed generic shl/shr operations
  400. + added register allocation hook calls for arm specific operand types:
  401. register set and shifter op
  402. Revision 1.22 2004/01/21 19:01:03 florian
  403. * fixed handling of max. distance of pc relative symbols
  404. Revision 1.21 2004/01/20 21:02:55 florian
  405. * fixed symbol type writing for arm-linux
  406. * fixed assembler generation for abs
  407. Revision 1.20 2003/12/28 16:20:09 jonas
  408. - removed unused methods from old generic spilling code
  409. Revision 1.19 2003/12/26 14:02:30 peter
  410. * sparc updates
  411. * use registertype in spill_register
  412. Revision 1.18 2003/12/18 17:06:21 florian
  413. * arm compiler compilation fixed
  414. Revision 1.17 2003/12/03 17:39:05 florian
  415. * fixed several arm calling conventions issues
  416. * fixed reference reading in the assembler reader
  417. * fixed a_loadaddr_ref_reg
  418. Revision 1.16 2003/11/30 19:35:29 florian
  419. * fixed several arm related problems
  420. Revision 1.15 2003/11/29 17:36:56 peter
  421. * fixed is_move
  422. Revision 1.14 2003/11/24 15:17:37 florian
  423. * changed some types to prevend range check errors
  424. Revision 1.13 2003/11/02 14:30:03 florian
  425. * fixed ARM for new reg. allocation scheme
  426. Revision 1.12 2003/09/11 11:54:59 florian
  427. * improved arm code generation
  428. * move some protected and private field around
  429. * the temp. register for register parameters/arguments are now released
  430. before the move to the parameter register is done. This improves
  431. the code in a lot of cases.
  432. Revision 1.11 2003/09/06 11:21:49 florian
  433. * fixed stm and ldm to be usable with preindex operand
  434. Revision 1.10 2003/09/04 21:07:03 florian
  435. * ARM compiler compiles again
  436. Revision 1.9 2003/09/04 00:15:29 florian
  437. * first bunch of adaptions of arm compiler for new register type
  438. Revision 1.8 2003/09/03 11:18:37 florian
  439. * fixed arm concatcopy
  440. + arm support in the common compiler sources added
  441. * moved some generic cg code around
  442. + tfputype added
  443. * ...
  444. Revision 1.7 2003/08/29 21:36:28 florian
  445. * fixed procedure entry/exit code
  446. * started to fix reference handling
  447. Revision 1.6 2003/08/28 00:05:29 florian
  448. * today's arm patches
  449. Revision 1.5 2003/08/27 00:27:56 florian
  450. + same procedure as very day: today's work on arm
  451. Revision 1.4 2003/08/25 23:20:38 florian
  452. + started to implement FPU support for the ARM
  453. * fixed a lot of other things
  454. Revision 1.3 2003/08/24 12:27:26 florian
  455. * continued to work on the arm port
  456. Revision 1.2 2003/08/20 15:50:12 florian
  457. * more arm stuff
  458. Revision 1.1 2003/08/16 13:23:01 florian
  459. * several arm related stuff fixed
  460. }