cgcpu.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. {
  2. $Id$
  3. Copyright (c) 2003 by Florian Klaempfl
  4. Member of the Free Pascal development team
  5. This unit implements the code generator for the ARM
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit cgcpu;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. symtype,
  24. cgbase,cgobj,
  25. aasmbase,aasmcpu,aasmtai,
  26. cpubase,cpuinfo,node,cg64f32,cginfo;
  27. type
  28. tcgarm = class(tcg)
  29. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);override;
  30. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);override;
  31. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);override;
  32. procedure a_call_name(list : taasmoutput;const s : string);override;
  33. procedure a_call_reg(list : taasmoutput;reg: tregister); override;
  34. procedure a_call_ref(list : taasmoutput;const ref : treference);override;
  35. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; reg: TRegister); override;
  36. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  37. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  38. size: tcgsize; a: aword; src, dst: tregister); override;
  39. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  40. size: tcgsize; src1, src2, dst: tregister); override;
  41. { move instructions }
  42. procedure a_load_const_reg(list : taasmoutput; size: tcgsize; a : aword;reg : tregister);override;
  43. procedure a_load_reg_ref(list : taasmoutput; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);override;
  44. procedure a_load_ref_reg(list : taasmoutput; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  45. procedure a_load_reg_reg(list : taasmoutput; fromsize, tosize : tcgsize;reg1,reg2 : tregister);override;
  46. { fpu move instructions }
  47. procedure a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister); override;
  48. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); override;
  49. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); override;
  50. { comparison operations }
  51. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  52. l : tasmlabel);override;
  53. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  54. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); override;
  55. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); override;
  56. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  57. procedure g_copyvaluepara_openarray(list : taasmoutput;const ref, lenref:treference;elesize:integer);override;
  58. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);override;
  59. procedure g_return_from_proc(list : taasmoutput;parasize : aword); override;
  60. procedure g_restore_frame_pointer(list : taasmoutput);override;
  61. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);override;
  62. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);override;
  63. procedure g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef); override;
  64. procedure g_save_standard_registers(list : taasmoutput; usedinproc : Tsupregset);override;
  65. procedure g_restore_standard_registers(list : taasmoutput; usedinproc : Tsupregset);override;
  66. procedure g_save_all_registers(list : taasmoutput);override;
  67. procedure g_restore_all_registers(list : taasmoutput;accused,acchiused:boolean);override;
  68. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  69. procedure a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  70. ref: treference);
  71. { creates the correct branch instruction for a given combination }
  72. { of asmcondflags and destination addressing mode }
  73. procedure a_jmp(list: taasmoutput; op: tasmop;
  74. c: tasmcond; l: tasmlabel);
  75. end;
  76. tcg64farm = class(tcg64f32)
  77. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);override;
  78. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);override;
  79. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);override;
  80. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);override;
  81. end;
  82. {!!!!
  83. const
  84. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_NONE,A_ADDI,A_ANDI_,A_DIVWU,
  85. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  86. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI);
  87. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_NONE,A_ADDIS,A_ANDIS_,
  88. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  89. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS);
  90. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlag = (C_NONE,C_EQ,C_GT,
  91. C_LT,C_GE,C_LE,C_NE,C_LE,C_LT,C_GE,C_GT);
  92. }
  93. function is_shifter_const(d : dword;var imm_shift : byte) : boolean;
  94. implementation
  95. uses
  96. globtype,globals,verbose,systems,cutils,symconst,symdef,symsym,rgobj,tgobj,cpupi;
  97. procedure tcgarm.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  98. var
  99. ref: treference;
  100. begin
  101. case locpara.loc of
  102. LOC_REGISTER,LOC_CREGISTER:
  103. a_load_const_reg(list,size,a,locpara.register);
  104. LOC_REFERENCE:
  105. begin
  106. reference_reset(ref);
  107. ref.base:=locpara.reference.index;
  108. ref.offset:=locpara.reference.offset;
  109. a_load_const_ref(list,size,a,ref);
  110. end;
  111. else
  112. internalerror(2002081101);
  113. end;
  114. if locpara.sp_fixup<>0 then
  115. internalerror(2002081102);
  116. end;
  117. procedure tcgarm.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  118. var
  119. ref: treference;
  120. tmpreg: tregister;
  121. begin
  122. case locpara.loc of
  123. LOC_REGISTER,LOC_CREGISTER:
  124. a_load_ref_reg(list,size,size,r,locpara.register);
  125. LOC_REFERENCE:
  126. begin
  127. reference_reset(ref);
  128. ref.base:=locpara.reference.index;
  129. ref.offset:=locpara.reference.offset;
  130. tmpreg := rg.getregisterint(list,size);
  131. a_load_ref_reg(list,size,size,r,tmpreg);
  132. a_load_reg_ref(list,size,size,tmpreg,ref);
  133. rg.ungetregisterint(list,tmpreg);
  134. end;
  135. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  136. case size of
  137. OS_F32, OS_F64:
  138. a_loadfpu_ref_reg(list,size,r,locpara.register);
  139. else
  140. internalerror(2002072801);
  141. end;
  142. else
  143. internalerror(2002081103);
  144. end;
  145. if locpara.sp_fixup<>0 then
  146. internalerror(2002081104);
  147. end;
  148. procedure tcgarm.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  149. var
  150. ref: treference;
  151. tmpreg: tregister;
  152. begin
  153. case locpara.loc of
  154. LOC_REGISTER,LOC_CREGISTER:
  155. a_loadaddr_ref_reg(list,r,locpara.register);
  156. LOC_REFERENCE:
  157. begin
  158. reference_reset(ref);
  159. ref.base := locpara.reference.index;
  160. ref.offset := locpara.reference.offset;
  161. tmpreg := rg.getregisterint(list,OS_ADDR);
  162. a_loadaddr_ref_reg(list,r,tmpreg);
  163. a_load_reg_ref(list,OS_ADDR,OS_ADDR,tmpreg,ref);
  164. rg.ungetregisterint(list,tmpreg);
  165. end;
  166. else
  167. internalerror(2002080701);
  168. end;
  169. end;
  170. procedure tcgarm.a_call_name(list : taasmoutput;const s : string);
  171. begin
  172. list.concat(taicpu.op_sym(A_BL,objectlibrary.newasmsymbol(s)));
  173. if not(pi_do_call in current_procinfo.flags) then
  174. internalerror(2003060703);
  175. end;
  176. procedure tcgarm.a_call_reg(list : taasmoutput;reg: tregister);
  177. var
  178. r : tregister;
  179. begin
  180. r.enum:=R_INTREGISTER;
  181. r.number:=NR_PC;
  182. list.concat(taicpu.op_reg_reg(A_MOV,r,reg));
  183. if not(pi_do_call in current_procinfo.flags) then
  184. internalerror(2003060704);
  185. end;
  186. procedure tcgarm.a_call_ref(list : taasmoutput;const ref : treference);
  187. var
  188. r : tregister;
  189. begin
  190. r.enum:=R_INTREGISTER;
  191. r.number:=NR_PC;
  192. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,r);
  193. if not(pi_do_call in current_procinfo.flags) then
  194. internalerror(2003060705);
  195. end;
  196. procedure tcgarm.a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; reg: TRegister);
  197. begin
  198. {
  199. shifterop_reset(so);
  200. { determine operator }
  201. if nodetype=shln then
  202. so.shiftertype:=SO_LSL
  203. else
  204. so.shiftertype:=SO_LSR;
  205. { shifting by a constant directly coded: }
  206. if (right.nodetype=ordconstn) then
  207. begin
  208. so.shiftimm:=tordconstnode(right).value and 31;
  209. a_op_reg_reg_shifterop(exprasmlist,op,OS_32,hregister1,resultreg,so)
  210. end
  211. else
  212. begin
  213. { load shift count in a register if necessary }
  214. location_force_reg(exprasmlist,right.location,def_cgsize(right.resulttype.def),true);
  215. hregister2 := right.location.register;
  216. so.rs:=hregister2;
  217. a_op_reg_reg_reg(exprasmlist,op,OS_32,hregister2,hregister1,resultreg);
  218. rg.UnGetRegisterInt(exprasmlist,hregister2);
  219. end;
  220. }
  221. end;
  222. procedure tcgarm.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  223. begin
  224. end;
  225. procedure tcgarm.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  226. size: tcgsize; a: aword; src, dst: tregister);
  227. begin
  228. end;
  229. procedure tcgarm.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  230. size: tcgsize; src1, src2, dst: tregister);
  231. begin
  232. end;
  233. function rotl(d : dword;b : byte) : dword;
  234. begin
  235. result:=(d shr (32-b)) or (d shl b);
  236. end;
  237. function is_shifter_const(d : dword;var imm_shift : byte) : boolean;
  238. var
  239. i : longint;
  240. begin
  241. for i:=0 to 15 do
  242. begin
  243. if (d and not(rotl($ff,i)))=0 then
  244. begin
  245. imm_shift:=i;
  246. result:=true;
  247. exit;
  248. end;
  249. end;
  250. result:=false;
  251. end;
  252. procedure tcgarm.a_load_const_reg(list : taasmoutput; size: tcgsize; a : aword;reg : tregister);
  253. var
  254. imm_shift : byte;
  255. l : tasmlabel;
  256. hr : treference;
  257. begin
  258. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  259. internalerror(2002090902);
  260. if is_shifter_const(a,imm_shift) then
  261. list.concat(taicpu.op_reg_const(A_MOV,reg,a))
  262. else if is_shifter_const(not(a),imm_shift) then
  263. list.concat(taicpu.op_reg_const(A_MVN,reg,not(a)))
  264. else
  265. begin
  266. objectlibrary.getdatalabel(l);
  267. current_procinfo.aktlocaldata.concat(Tai_const_symbol.Create(l));
  268. current_procinfo.aktlocaldata.concat(Tai_const.Create_32bit(a));
  269. reference_reset(hr);
  270. hr.symbol:=l;
  271. list.concat(taicpu.op_reg_ref(A_LDR,reg,hr));
  272. end;
  273. end;
  274. procedure tcgarm.a_load_reg_ref(list : taasmoutput; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);
  275. begin
  276. end;
  277. procedure tcgarm.a_load_ref_reg(list : taasmoutput; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  278. begin
  279. end;
  280. procedure tcgarm.a_load_reg_reg(list : taasmoutput; fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  281. begin
  282. end;
  283. procedure tcgarm.a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister);
  284. begin
  285. end;
  286. procedure tcgarm.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  287. begin
  288. end;
  289. procedure tcgarm.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  290. begin
  291. end;
  292. { comparison operations }
  293. procedure tcgarm.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  294. l : tasmlabel);
  295. begin
  296. end;
  297. procedure tcgarm.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  298. begin
  299. end;
  300. procedure tcgarm.a_jmp_always(list : taasmoutput;l: tasmlabel);
  301. begin
  302. list.concat(taicpu.op_sym(A_B,objectlibrary.newasmsymbol(l.name)));
  303. end;
  304. procedure tcgarm.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  305. begin
  306. end;
  307. procedure tcgarm.g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister);
  308. begin
  309. end;
  310. procedure tcgarm.g_copyvaluepara_openarray(list : taasmoutput;const ref, lenref:treference;elesize:integer);
  311. begin
  312. end;
  313. procedure tcgarm.g_stackframe_entry(list : taasmoutput;localsize : longint);
  314. begin
  315. end;
  316. procedure tcgarm.g_return_from_proc(list : taasmoutput;parasize : aword);
  317. begin
  318. end;
  319. procedure tcgarm.g_restore_frame_pointer(list : taasmoutput);
  320. begin
  321. end;
  322. procedure tcgarm.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  323. begin
  324. end;
  325. procedure tcgarm.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  326. begin
  327. end;
  328. procedure tcgarm.g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef);
  329. begin
  330. end;
  331. procedure tcgarm.g_save_standard_registers(list : taasmoutput; usedinproc : Tsupregset);
  332. begin
  333. end;
  334. procedure tcgarm.g_restore_standard_registers(list : taasmoutput; usedinproc : Tsupregset);
  335. begin
  336. end;
  337. procedure tcgarm.g_save_all_registers(list : taasmoutput);
  338. begin
  339. end;
  340. procedure tcgarm.g_restore_all_registers(list : taasmoutput;accused,acchiused:boolean);
  341. begin
  342. end;
  343. procedure tcgarm.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  344. begin
  345. end;
  346. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  347. procedure tcgarm.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  348. ref: treference);
  349. begin
  350. end;
  351. { creates the correct branch instruction for a given combination }
  352. { of asmcondflags and destination addressing mode }
  353. procedure tcgarm.a_jmp(list: taasmoutput; op: tasmop;
  354. c: tasmcond; l: tasmlabel);
  355. begin
  356. end;
  357. procedure tcg64farm.a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);
  358. begin
  359. end;
  360. procedure tcg64farm.a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);
  361. begin
  362. end;
  363. procedure tcg64farm.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);
  364. begin
  365. end;
  366. procedure tcg64farm.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  367. begin
  368. end;
  369. begin
  370. cg:=tcgarm.create;
  371. cg64:=tcg64farm.create;
  372. end.
  373. {
  374. $Log$
  375. Revision 1.5 2003-08-25 23:20:38 florian
  376. + started to implement FPU support for the ARM
  377. * fixed a lot of other things
  378. Revision 1.4 2003/08/24 12:27:26 florian
  379. * continued to work on the arm port
  380. Revision 1.3 2003/08/21 03:14:00 florian
  381. * arm compiler can be compiled; far from being working
  382. Revision 1.2 2003/08/20 15:50:12 florian
  383. * more arm stuff
  384. Revision 1.1 2003/07/21 16:35:30 florian
  385. * very basic stuff for the arm
  386. }