cgcpu.pas 18 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. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  65. { that's the case, we can use rlwinm to do an AND operation }
  66. function get_rlwi_const(a: aword; var l1, l2: longint): boolean;
  67. procedure g_save_standard_registers(list : taasmoutput; usedinproc : Tsupregset);override;
  68. procedure g_restore_standard_registers(list : taasmoutput; usedinproc : Tsupregset);override;
  69. procedure g_save_all_registers(list : taasmoutput);override;
  70. procedure g_restore_all_registers(list : taasmoutput;accused,acchiused:boolean);override;
  71. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  72. private
  73. procedure g_stackframe_entry_sysv(list : taasmoutput;localsize : longint);
  74. procedure g_return_from_proc_sysv(list : taasmoutput;parasize : aword);
  75. procedure g_stackframe_entry_aix(list : taasmoutput;localsize : longint);
  76. procedure g_return_from_proc_aix(list : taasmoutput;parasize : aword);
  77. procedure g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  78. procedure g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  79. { Make sure ref is a valid reference for the PowerPC and sets the }
  80. { base to the value of the index if (base = R_NO). }
  81. { Returns true if the reference contained a base, index and an }
  82. { offset or symbol, in which case the base will have been changed }
  83. { to a tempreg (which has to be freed by the caller) containing }
  84. { the sum of part of the original reference }
  85. function fixref(list: taasmoutput; var ref: treference): boolean;
  86. { returns whether a reference can be used immediately in a powerpc }
  87. { instruction }
  88. function issimpleref(const ref: treference): boolean;
  89. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  90. procedure a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  91. ref: treference);
  92. { creates the correct branch instruction for a given combination }
  93. { of asmcondflags and destination addressing mode }
  94. procedure a_jmp(list: taasmoutput; op: tasmop;
  95. c: tasmcond; l: tasmlabel);
  96. end;
  97. tcg64farm = class(tcg64f32)
  98. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);override;
  99. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);override;
  100. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);override;
  101. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);override;
  102. end;
  103. {!!!!
  104. const
  105. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_NONE,A_ADDI,A_ANDI_,A_DIVWU,
  106. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  107. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI);
  108. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_NONE,A_ADDIS,A_ANDIS_,
  109. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  110. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS);
  111. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlag = (C_NONE,C_EQ,C_GT,
  112. C_LT,C_GE,C_LE,C_NE,C_LE,C_LT,C_GE,C_GT);
  113. }
  114. implementation
  115. uses
  116. globtype,globals,verbose,systems,cutils,symconst,symdef,symsym,rgobj,tgobj,cpupi;
  117. procedure tcgarm.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  118. var
  119. ref: treference;
  120. begin
  121. case locpara.loc of
  122. LOC_REGISTER,LOC_CREGISTER:
  123. a_load_const_reg(list,size,a,locpara.register);
  124. LOC_REFERENCE:
  125. begin
  126. reference_reset(ref);
  127. ref.base:=locpara.reference.index;
  128. ref.offset:=locpara.reference.offset;
  129. a_load_const_ref(list,size,a,ref);
  130. end;
  131. else
  132. internalerror(2002081101);
  133. end;
  134. if locpara.sp_fixup<>0 then
  135. internalerror(2002081102);
  136. end;
  137. procedure tcgarm.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  138. var
  139. ref: treference;
  140. tmpreg: tregister;
  141. begin
  142. case locpara.loc of
  143. LOC_REGISTER,LOC_CREGISTER:
  144. a_load_ref_reg(list,size,size,r,locpara.register);
  145. LOC_REFERENCE:
  146. begin
  147. reference_reset(ref);
  148. ref.base:=locpara.reference.index;
  149. ref.offset:=locpara.reference.offset;
  150. tmpreg := rg.getregisterint(list,size);
  151. a_load_ref_reg(list,size,size,r,tmpreg);
  152. a_load_reg_ref(list,size,size,tmpreg,ref);
  153. rg.ungetregisterint(list,tmpreg);
  154. end;
  155. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  156. case size of
  157. OS_F32, OS_F64:
  158. a_loadfpu_ref_reg(list,size,r,locpara.register);
  159. else
  160. internalerror(2002072801);
  161. end;
  162. else
  163. internalerror(2002081103);
  164. end;
  165. if locpara.sp_fixup<>0 then
  166. internalerror(2002081104);
  167. end;
  168. procedure tcgarm.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  169. var
  170. ref: treference;
  171. tmpreg: tregister;
  172. begin
  173. case locpara.loc of
  174. LOC_REGISTER,LOC_CREGISTER:
  175. a_loadaddr_ref_reg(list,r,locpara.register);
  176. LOC_REFERENCE:
  177. begin
  178. reference_reset(ref);
  179. ref.base := locpara.reference.index;
  180. ref.offset := locpara.reference.offset;
  181. tmpreg := rg.getregisterint(list,OS_ADDR);
  182. a_loadaddr_ref_reg(list,r,tmpreg);
  183. a_load_reg_ref(list,OS_ADDR,OS_ADDR,tmpreg,ref);
  184. rg.ungetregisterint(list,tmpreg);
  185. end;
  186. else
  187. internalerror(2002080701);
  188. end;
  189. end;
  190. procedure tcgarm.a_call_name(list : taasmoutput;const s : string);
  191. begin
  192. list.concat(taicpu.op_sym(A_BL,objectlibrary.newasmsymbol(s)));
  193. if not(pi_do_call in current_procinfo.flags) then
  194. internalerror(2003060703);
  195. end;
  196. procedure tcgarm.a_call_reg(list : taasmoutput;reg: tregister);
  197. var
  198. r : tregister;
  199. begin
  200. r.enum:=R_INTREGISTER;
  201. r.number:=NR_PC;
  202. list.concat(taicpu.op_reg_reg(A_MOV,r,reg));
  203. if not(pi_do_call in current_procinfo.flags) then
  204. internalerror(2003060704);
  205. end;
  206. procedure tcgarm.a_call_ref(list : taasmoutput;const ref : treference);
  207. var
  208. r : tregister;
  209. begin
  210. r.enum:=R_INTREGISTER;
  211. r.number:=NR_PC;
  212. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,r);
  213. if not(pi_do_call in current_procinfo.flags) then
  214. internalerror(2003060705);
  215. end;
  216. procedure tcgarm.a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; reg: TRegister);
  217. begin
  218. end;
  219. procedure tcgarm.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  220. begin
  221. end;
  222. procedure tcgarm.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  223. size: tcgsize; a: aword; src, dst: tregister);
  224. begin
  225. end;
  226. procedure tcgarm.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  227. size: tcgsize; src1, src2, dst: tregister);
  228. begin
  229. end;
  230. function rotl(d : dword;b : byte) : dword;
  231. begin
  232. result=(d shr (32-b)) or (d shl b);
  233. end;
  234. function is_shifter_const(d : dword;var imm_shift : byte) : boolean;
  235. var
  236. i : longint;
  237. begin
  238. for i:=0 to 15 do
  239. begin
  240. if (d and not(rotl($ff,i)))=0 then
  241. begin
  242. imm_shift:=i;
  243. result:=true;
  244. exit;
  245. end;
  246. end;
  247. result:=false;
  248. end;
  249. procedure a_load_const_reg(list : taasmoutput; size: tcgsize; a : aword;reg : tregister);override;
  250. var
  251. imm_shift : byte;
  252. l : tasmlabel;
  253. hr : treference;
  254. begin
  255. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  256. internalerror(2002090902);
  257. if is_shifter_const(a,imm_shift) then
  258. list.concat(taicpu.op_reg_const(A_MOV,reg,a))
  259. else if is_shifter_const(not(a),imm_shift) then
  260. list.concat(taicpu.op_reg_const(A_MVN,reg,not(a)))
  261. else
  262. begin
  263. objectlibrary.getdatalabel(l);
  264. aktlocaldata.concat(Tai_const_symbol.Create(l));
  265. aktlocaldata.concat(Tai_const.Create_32bit(a));
  266. reference_reset(hr);
  267. hr.symbol:=l;
  268. list.concat(taicpu.op_reg_ref(A_LDR,reg,hr));
  269. end;
  270. end;
  271. procedure tcgarm.a_load_reg_ref(list : taasmoutput; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);
  272. begin
  273. end;
  274. procedure tcgarm.a_load_ref_reg(list : taasmoutput; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  275. begin
  276. end;
  277. procedure tcgarm.a_load_reg_reg(list : taasmoutput; fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  278. begin
  279. end;
  280. procedure tcgarm.a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister);
  281. begin
  282. end;
  283. procedure tcgarm.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  284. begin
  285. end;
  286. procedure tcgarm.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  287. begin
  288. end;
  289. { comparison operations }
  290. procedure tcgarm.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  291. l : tasmlabel);
  292. begin
  293. end;
  294. procedure tcgarm.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  295. begin
  296. end;
  297. procedure tcgarm.a_jmp_always(list : taasmoutput;l: tasmlabel);
  298. begin
  299. end;
  300. procedure tcgarm.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  301. begin
  302. end;
  303. procedure tcgarm.g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister);
  304. begin
  305. end;
  306. procedure tcgarm.g_copyvaluepara_openarray(list : taasmoutput;const ref, lenref:treference;elesize:integer);
  307. begin
  308. end;
  309. procedure tcgarm.g_stackframe_entry(list : taasmoutput;localsize : longint);
  310. begin
  311. end;
  312. procedure tcgarm.g_return_from_proc(list : taasmoutput;parasize : aword);
  313. begin
  314. end;
  315. procedure tcgarm.g_restore_frame_pointer(list : taasmoutput);
  316. begin
  317. end;
  318. procedure tcgarm.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  319. begin
  320. end;
  321. procedure tcgarm.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  322. begin
  323. end;
  324. procedure tcgarm.g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef);
  325. begin
  326. end;
  327. procedure tcgarm.g_save_standard_registers(list : taasmoutput; usedinproc : Tsupregset);
  328. begin
  329. end;
  330. procedure tcgarm.g_restore_standard_registers(list : taasmoutput; usedinproc : Tsupregset);
  331. begin
  332. end;
  333. procedure tcgarm.g_save_all_registers(list : taasmoutput);
  334. begin
  335. end;
  336. procedure tcgarm.g_restore_all_registers(list : taasmoutput;accused,acchiused:boolean);
  337. begin
  338. end;
  339. procedure tcgarm.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  340. begin
  341. end;
  342. procedure tcgarm.g_stackframe_entry_sysv(list : taasmoutput;localsize : longint);
  343. begin
  344. end;
  345. procedure tcgarm.g_return_from_proc_sysv(list : taasmoutput;parasize : aword);
  346. begin
  347. end;
  348. procedure tcgarm.g_stackframe_entry_aix(list : taasmoutput;localsize : longint);
  349. begin
  350. end;
  351. procedure tcgarm.g_return_from_proc_aix(list : taasmoutput;parasize : aword);
  352. begin
  353. end;
  354. procedure tcgarm.g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  355. begin
  356. end;
  357. procedure tcgarm.g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  358. begin
  359. end;
  360. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  361. procedure tcgarm.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  362. ref: treference);
  363. begin
  364. end;
  365. { creates the correct branch instruction for a given combination }
  366. { of asmcondflags and destination addressing mode }
  367. procedure tcgarm.a_jmp(list: taasmoutput; op: tasmop;
  368. c: tasmcond; l: tasmlabel);
  369. begin
  370. end;
  371. begin
  372. cg := tcgarm.create;
  373. cg64 :=tcg64farm.create;
  374. end.
  375. {
  376. $Log$
  377. Revision 1.2 2003-08-20 15:50:12 florian
  378. * more arm stuff
  379. Revision 1.1 2003/07/21 16:35:30 florian
  380. * very basic stuff for the arm
  381. }