aasmcpu.pas 18 KB

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