cgcpu.pas 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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 : tsuperregisterset);override;
  65. procedure g_restore_standard_registers(list : taasmoutput; usedinproc : tsuperregisterset);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 fixref(list : taasmoutput;var ref : treference);
  70. procedure handle_load_store(list:taasmoutput;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference);
  71. end;
  72. tcg64farm = class(tcg64f32)
  73. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);override;
  74. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);override;
  75. procedure a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);override;
  76. procedure a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);override;
  77. end;
  78. const
  79. OpCmp2AsmCond : Array[topcmp] of TAsmCond = (C_NONE,C_EQ,C_GT,
  80. C_LT,C_GE,C_LE,C_NE,C_LE,C_LT,C_GE,C_GT);
  81. function is_shifter_const(d : dword;var imm_shift : byte) : boolean;
  82. implementation
  83. uses
  84. globtype,globals,verbose,systems,cutils,symconst,symdef,symsym,rgobj,tgobj,cpupi;
  85. procedure tcgarm.a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);
  86. var
  87. ref: treference;
  88. begin
  89. case locpara.loc of
  90. LOC_REGISTER,LOC_CREGISTER:
  91. a_load_const_reg(list,size,a,locpara.register);
  92. LOC_REFERENCE:
  93. begin
  94. reference_reset(ref);
  95. ref.base:=locpara.reference.index;
  96. ref.offset:=locpara.reference.offset;
  97. a_load_const_ref(list,size,a,ref);
  98. end;
  99. else
  100. internalerror(2002081101);
  101. end;
  102. if locpara.sp_fixup<>0 then
  103. internalerror(2002081102);
  104. end;
  105. procedure tcgarm.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);
  106. var
  107. ref: treference;
  108. tmpreg: tregister;
  109. begin
  110. case locpara.loc of
  111. LOC_REGISTER,LOC_CREGISTER:
  112. a_load_ref_reg(list,size,size,r,locpara.register);
  113. LOC_REFERENCE:
  114. begin
  115. reference_reset(ref);
  116. ref.base:=locpara.reference.index;
  117. ref.offset:=locpara.reference.offset;
  118. tmpreg := rg.getregisterint(list,size);
  119. a_load_ref_reg(list,size,size,r,tmpreg);
  120. a_load_reg_ref(list,size,size,tmpreg,ref);
  121. rg.ungetregisterint(list,tmpreg);
  122. end;
  123. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  124. case size of
  125. OS_F32, OS_F64:
  126. a_loadfpu_ref_reg(list,size,r,locpara.register);
  127. else
  128. internalerror(2002072801);
  129. end;
  130. else
  131. internalerror(2002081103);
  132. end;
  133. if locpara.sp_fixup<>0 then
  134. internalerror(2002081104);
  135. end;
  136. procedure tcgarm.a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  137. var
  138. ref: treference;
  139. tmpreg: tregister;
  140. begin
  141. case locpara.loc of
  142. LOC_REGISTER,LOC_CREGISTER:
  143. a_loadaddr_ref_reg(list,r,locpara.register);
  144. LOC_REFERENCE:
  145. begin
  146. reference_reset(ref);
  147. ref.base := locpara.reference.index;
  148. ref.offset := locpara.reference.offset;
  149. tmpreg := rg.getregisterint(list,OS_ADDR);
  150. a_loadaddr_ref_reg(list,r,tmpreg);
  151. a_load_reg_ref(list,OS_ADDR,OS_ADDR,tmpreg,ref);
  152. rg.ungetregisterint(list,tmpreg);
  153. end;
  154. else
  155. internalerror(2002080701);
  156. end;
  157. end;
  158. procedure tcgarm.a_call_name(list : taasmoutput;const s : string);
  159. begin
  160. list.concat(taicpu.op_sym(A_BL,objectlibrary.newasmsymbol(s)));
  161. if not(pi_do_call in current_procinfo.flags) then
  162. internalerror(2003060703);
  163. end;
  164. procedure tcgarm.a_call_reg(list : taasmoutput;reg: tregister);
  165. var
  166. r : tregister;
  167. begin
  168. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,reg));
  169. if not(pi_do_call in current_procinfo.flags) then
  170. internalerror(2003060704);
  171. end;
  172. procedure tcgarm.a_call_ref(list : taasmoutput;const ref : treference);
  173. var
  174. r : tregister;
  175. begin
  176. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,NR_PC);
  177. if not(pi_do_call in current_procinfo.flags) then
  178. internalerror(2003060705);
  179. end;
  180. procedure tcgarm.a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; reg: TRegister);
  181. begin
  182. a_op_const_reg_reg(list,op,size,a,reg,reg);
  183. end;
  184. procedure tcgarm.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  185. begin
  186. case op of
  187. OP_NEG:
  188. list.concat(taicpu.op_reg_reg_const(A_RSB,dst,src,0));
  189. OP_NOT:
  190. list.concat(taicpu.op_reg_reg(A_MVN,dst,src));
  191. else
  192. a_op_reg_reg_reg(list,op,OS_32,src,dst,dst);
  193. end;
  194. end;
  195. const
  196. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  197. (A_NONE,A_ADD,A_AND,A_NONE,A_NONE,A_MUL,A_MUL,A_NONE,A_NONE,A_ORR,
  198. A_NONE,A_NONE,A_NONE,A_SUB,A_EOR);
  199. procedure tcgarm.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  200. size: tcgsize; a: aword; src, dst: tregister);
  201. var
  202. shift : byte;
  203. tmpreg : tregister;
  204. so : tshifterop;
  205. begin
  206. if is_shifter_const(a,shift) and (not(op in [OP_IMUL,OP_MUL])) then
  207. case op of
  208. OP_NEG,OP_NOT,
  209. OP_DIV,OP_IDIV:
  210. internalerror(200308281);
  211. OP_SHL:
  212. begin
  213. if a>32 then
  214. internalerror(200308291);
  215. shifterop_reset(so);
  216. so.shiftmode:=SM_LSL;
  217. so.shiftimm:=a;
  218. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  219. end;
  220. OP_SHR:
  221. begin
  222. if a>32 then
  223. internalerror(200308292);
  224. shifterop_reset(so);
  225. so.shiftmode:=SM_LSR;
  226. so.shiftimm:=a;
  227. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  228. end;
  229. OP_SAR:
  230. begin
  231. if a>32 then
  232. internalerror(200308291);
  233. shifterop_reset(so);
  234. so.shiftmode:=SM_LSL;
  235. so.shiftimm:=a;
  236. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  237. end;
  238. else
  239. list.concat(taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,src,a));
  240. end
  241. else
  242. begin
  243. { there could be added some more sophisticated optimizations }
  244. if (op in [OP_MUL,OP_IMUL]) and (a=1) then
  245. a_load_reg_reg(list,size,size,src,dst)
  246. else if (op in [OP_MUL,OP_IMUL]) and (a=0) then
  247. a_load_const_reg(list,size,0,dst)
  248. else if (op in [OP_IMUL]) and (a=-1) then
  249. a_op_reg_reg(list,OP_NEG,size,src,dst)
  250. else
  251. begin
  252. tmpreg := rg.getregisterint(list,size);
  253. a_load_const_reg(list,size,a,tmpreg);
  254. a_op_reg_reg_reg(list,op,size,tmpreg,src,dst);
  255. rg.ungetregisterint(list,tmpreg);
  256. end;
  257. end;
  258. end;
  259. procedure tcgarm.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  260. size: tcgsize; src1, src2, dst: tregister);
  261. var
  262. so : tshifterop;
  263. tmpreg : tregister;
  264. begin
  265. case op of
  266. OP_NEG,OP_NOT,
  267. OP_DIV,OP_IDIV:
  268. internalerror(200308281);
  269. OP_SHL:
  270. begin
  271. shifterop_reset(so);
  272. so.rs:=src1;
  273. so.shiftmode:=SM_LSL;
  274. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src2,so));
  275. end;
  276. OP_SHR:
  277. begin
  278. shifterop_reset(so);
  279. so.rs:=src1;
  280. so.shiftmode:=SM_LSR;
  281. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src2,so));
  282. end;
  283. OP_SAR:
  284. begin
  285. shifterop_reset(so);
  286. so.rs:=src1;
  287. so.shiftmode:=SM_ASR;
  288. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src2,so));
  289. end;
  290. OP_IMUL,
  291. OP_MUL:
  292. begin
  293. { the arm doesn't allow that rd and rm are the same }
  294. if dst=src2 then
  295. begin
  296. if src1<>src2 then
  297. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src1,src2))
  298. else
  299. begin
  300. writeln('Warning: Fix MUL');
  301. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src2,src1));
  302. end;
  303. end
  304. else
  305. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src2,src1));
  306. end;
  307. else
  308. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  309. end;
  310. end;
  311. function rotl(d : dword;b : byte) : dword;
  312. begin
  313. result:=(d shr (32-b)) or (d shl b);
  314. end;
  315. function is_shifter_const(d : dword;var imm_shift : byte) : boolean;
  316. var
  317. i : longint;
  318. begin
  319. for i:=0 to 15 do
  320. begin
  321. if (d and not(rotl($ff,i*2)))=0 then
  322. begin
  323. imm_shift:=i*2;
  324. result:=true;
  325. exit;
  326. end;
  327. end;
  328. result:=false;
  329. end;
  330. procedure tcgarm.a_load_const_reg(list : taasmoutput; size: tcgsize; a : aword;reg : tregister);
  331. var
  332. imm_shift : byte;
  333. l : tasmlabel;
  334. hr : treference;
  335. begin
  336. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  337. internalerror(2002090902);
  338. if is_shifter_const(a,imm_shift) then
  339. list.concat(taicpu.op_reg_const(A_MOV,reg,a))
  340. else if is_shifter_const(not(a),imm_shift) then
  341. list.concat(taicpu.op_reg_const(A_MVN,reg,not(a)))
  342. else
  343. begin
  344. objectlibrary.getdatalabel(l);
  345. current_procinfo.aktlocaldata.concat(tai_symbol.Create(l,0));
  346. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(a));
  347. reference_reset(hr);
  348. hr.symbol:=l;
  349. list.concat(taicpu.op_reg_ref(A_LDR,reg,hr));
  350. end;
  351. end;
  352. procedure tcgarm.handle_load_store(list:taasmoutput;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference);
  353. var
  354. tmpreg : tregister;
  355. tmpref : treference;
  356. l : tasmlabel;
  357. begin
  358. tmpreg:=NR_NO;
  359. { Be sure to have a base register }
  360. if (ref.base=NR_NO) then
  361. begin
  362. if ref.shiftmode<>SM_None then
  363. internalerror(200308294);
  364. ref.base:=ref.index;
  365. ref.index:=NR_NO;
  366. end;
  367. { absolute symbols can't be handled directly, we've to store the symbol reference
  368. in the text segment and access it pc relative
  369. For now, we assume that references where base or index equals to PC are already
  370. relative, all other references are assumed to be absolute and thus they need
  371. to be handled extra.
  372. A proper solution would be to change refoptions to a set and store the information
  373. if the symbol is absolute or relative there.
  374. }
  375. if (assigned(ref.symbol) and
  376. not(is_pc(ref.base)) and
  377. not(is_pc(ref.index))
  378. ) or
  379. (ref.offset<-4095) or
  380. (ref.offset>4095) then
  381. begin
  382. { check consts distance }
  383. { create consts entry }
  384. objectlibrary.getdatalabel(l);
  385. current_procinfo.aktlocaldata.concat(Tai_symbol.Create(l,0));
  386. if assigned(ref.symbol) then
  387. current_procinfo.aktlocaldata.concat(tai_const_symbol.Create_offset(ref.symbol,ref.offset))
  388. else
  389. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(ref.offset));
  390. { load consts entry }
  391. tmpreg:=rg.getregisterint(list,OS_INT);
  392. reference_reset(tmpref);
  393. tmpref.symbol:=l;
  394. tmpref.base:=NR_R15;
  395. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  396. if (ref.base<>NR_NO) then
  397. begin
  398. if ref.index<>NR_NO then
  399. begin
  400. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  401. rg.ungetregister(list,ref.base);
  402. ref.base:=tmpreg;
  403. end
  404. else
  405. begin
  406. ref.index:=tmpreg;
  407. ref.shiftimm:=0;
  408. ref.signindex:=1;
  409. ref.shiftmode:=SM_None;
  410. end;
  411. end
  412. else
  413. ref.base:=tmpreg;
  414. ref.offset:=0;
  415. ref.symbol:=nil;
  416. end;
  417. list.concat(setoppostfix(taicpu.op_reg_ref(op,reg,ref),oppostfix));
  418. if (tmpreg<>NR_NO) then
  419. rg.ungetregisterint(list,tmpreg);
  420. end;
  421. procedure tcgarm.a_load_reg_ref(list : taasmoutput; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);
  422. var
  423. oppostfix:toppostfix;
  424. begin
  425. case ToSize of
  426. { signed integer registers }
  427. OS_8,
  428. OS_S8:
  429. oppostfix:=PF_B;
  430. OS_16,
  431. OS_S16:
  432. oppostfix:=PF_H;
  433. OS_32,
  434. OS_S32:
  435. oppostfix:=PF_None;
  436. else
  437. InternalError(200308295);
  438. end;
  439. handle_load_store(list,A_STR,oppostfix,reg,ref);
  440. end;
  441. procedure tcgarm.a_load_ref_reg(list : taasmoutput; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  442. var
  443. oppostfix:toppostfix;
  444. begin
  445. case FromSize of
  446. { signed integer registers }
  447. OS_8:
  448. oppostfix:=PF_B;
  449. OS_S8:
  450. oppostfix:=PF_SB;
  451. OS_16:
  452. oppostfix:=PF_H;
  453. OS_S16:
  454. oppostfix:=PF_SH;
  455. OS_32,
  456. OS_S32:
  457. oppostfix:=PF_None;
  458. else
  459. InternalError(200308291);
  460. end;
  461. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  462. end;
  463. procedure tcgarm.a_load_reg_reg(list : taasmoutput; fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  464. var
  465. instr: taicpu;
  466. so : tshifterop;
  467. begin
  468. shifterop_reset(so);
  469. if (reg1<>reg2) or
  470. (tcgsize2size[tosize] < tcgsize2size[fromsize]) or
  471. ((tcgsize2size[tosize] = tcgsize2size[fromsize]) and
  472. (tosize <> fromsize) and
  473. not(fromsize in [OS_32,OS_S32])) then
  474. begin
  475. case tosize of
  476. OS_8:
  477. instr := taicpu.op_reg_reg_const(A_AND,
  478. reg2,reg1,$ff);
  479. OS_S8:
  480. begin
  481. so.shiftmode:=SM_LSL;
  482. so.shiftimm:=24;
  483. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,reg2,reg1,so));
  484. so.shiftmode:=SM_ASR;
  485. so.shiftimm:=24;
  486. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,reg2,reg2,so));
  487. end;
  488. OS_16:
  489. begin
  490. so.shiftmode:=SM_LSL;
  491. so.shiftimm:=16;
  492. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,reg2,reg1,so));
  493. so.shiftmode:=SM_LSR;
  494. so.shiftimm:=16;
  495. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,reg2,reg2,so));
  496. end;
  497. OS_S16:
  498. begin
  499. so.shiftmode:=SM_LSL;
  500. so.shiftimm:=16;
  501. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,reg2,reg1,so));
  502. so.shiftmode:=SM_ASR;
  503. so.shiftimm:=16;
  504. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,reg2,reg2,so));
  505. end;
  506. OS_32,OS_S32:
  507. begin
  508. instr:=taicpu.op_reg_reg(A_MOV,reg2,reg1);
  509. rg.add_move_instruction(instr);
  510. list.concat(instr);
  511. end;
  512. else internalerror(2002090901);
  513. end;
  514. end;
  515. end;
  516. procedure tcgarm.a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister);
  517. begin
  518. list.concat(setoppostfix(taicpu.op_reg_reg(A_MVF,reg2,reg1),cgsize2fpuoppostfix[size]));
  519. end;
  520. procedure tcgarm.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  521. var
  522. oppostfix:toppostfix;
  523. begin
  524. case size of
  525. OS_F32:
  526. oppostfix:=PF_S;
  527. OS_F64:
  528. oppostfix:=PF_D;
  529. OS_F80:
  530. oppostfix:=PF_E;
  531. else
  532. InternalError(200309021);
  533. end;
  534. handle_load_store(list,A_LDF,oppostfix,reg,ref);
  535. end;
  536. procedure tcgarm.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  537. var
  538. oppostfix:toppostfix;
  539. begin
  540. case size of
  541. OS_F32:
  542. oppostfix:=PF_S;
  543. OS_F64:
  544. oppostfix:=PF_D;
  545. OS_F80:
  546. oppostfix:=PF_E;
  547. else
  548. InternalError(200309021);
  549. end;
  550. handle_load_store(list,A_STF,oppostfix,reg,ref);
  551. end;
  552. { comparison operations }
  553. procedure tcgarm.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  554. l : tasmlabel);
  555. var
  556. tmpreg : tregister;
  557. b : byte;
  558. begin
  559. if is_shifter_const(a,b) then
  560. list.concat(taicpu.op_reg_const(A_CMN,reg,a))
  561. { CMN reg,0 and CMN reg,$80000000 are different from CMP reg,$ffffffff
  562. and CMP reg,$7fffffff regarding the flags according to the ARM manual }
  563. else if is_shifter_const(not(a),b) and (a<>$7fffffff) and (a<>$ffffffff) then
  564. list.concat(taicpu.op_reg_const(A_CMN,reg,not(a)))
  565. else
  566. begin
  567. tmpreg:=rg.getregisterint(list,size);
  568. a_load_const_reg(list,size,a,tmpreg);
  569. list.concat(taicpu.op_reg_reg(A_CMP,reg,tmpreg));
  570. rg.ungetregisterint(list,tmpreg);
  571. end;
  572. a_jmp_cond(list,cmp_op,l);
  573. end;
  574. procedure tcgarm.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  575. begin
  576. list.concat(taicpu.op_reg_reg(A_CMP,reg2,reg1));
  577. a_jmp_cond(list,cmp_op,l);
  578. end;
  579. procedure tcgarm.a_jmp_always(list : taasmoutput;l: tasmlabel);
  580. begin
  581. list.concat(taicpu.op_sym(A_B,objectlibrary.newasmsymbol(l.name)));
  582. end;
  583. procedure tcgarm.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  584. var
  585. ai : taicpu;
  586. begin
  587. ai:=setcondition(taicpu.op_sym(A_B,l),flags_to_cond(f));
  588. ai.is_jmp:=true;
  589. list.concat(ai);
  590. end;
  591. procedure tcgarm.g_flags2reg(list: taasmoutput; size: TCgSize; const f: TResFlags; reg: TRegister);
  592. var
  593. ai : taicpu;
  594. begin
  595. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,1),flags_to_cond(f)));
  596. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,0),inverse_cond[flags_to_cond(f)]));
  597. end;
  598. procedure tcgarm.g_copyvaluepara_openarray(list : taasmoutput;const ref, lenref:treference;elesize:integer);
  599. begin
  600. end;
  601. procedure tcgarm.g_stackframe_entry(list : taasmoutput;localsize : longint);
  602. var
  603. ref : treference;
  604. begin
  605. LocalSize:=align(LocalSize,4);
  606. a_reg_alloc(list,NR_STACK_POINTER_REG);
  607. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  608. a_reg_alloc(list,NR_R12);
  609. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_STACK_POINTER_REG));
  610. { save int registers }
  611. reference_reset(ref);
  612. ref.index:=NR_STACK_POINTER_REG;
  613. ref.addressmode:=AM_PREINDEXED;
  614. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,rg.used_in_proc_int-[RS_R0..RS_R3]+[RS_R11,RS_R12,RS_R15]),PF_FD));
  615. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_FRAME_POINTER_REG,NR_R12,4));
  616. a_reg_alloc(list,NR_R12);
  617. { allocate necessary stack size }
  618. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  619. end;
  620. procedure tcgarm.g_return_from_proc(list : taasmoutput;parasize : aword);
  621. var
  622. ref : treference;
  623. begin
  624. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  625. list.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14))
  626. else
  627. begin
  628. { restore int registers and return }
  629. reference_reset(ref);
  630. ref.index:=NR_FRAME_POINTER_REG;
  631. ref.addressmode:=AM_PREINDEXED;
  632. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,rg.used_in_proc_int-[RS_R0..RS_R3]+[RS_R11,RS_R13,RS_R15]),PF_EA));
  633. end;
  634. end;
  635. procedure tcgarm.g_restore_frame_pointer(list : taasmoutput);
  636. begin
  637. { the frame pointer on the ARM is restored while the ret is executed }
  638. end;
  639. procedure tcgarm.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  640. var
  641. b : byte;
  642. tmpref : treference;
  643. begin
  644. tmpref:=ref;
  645. { Be sure to have a base register }
  646. if (tmpref.base=NR_NO) then
  647. begin
  648. if tmpref.shiftmode<>SM_None then
  649. internalerror(200308294);
  650. tmpref.base:=tmpref.index;
  651. tmpref.index:=NR_NO;
  652. end;
  653. if assigned(tmpref.symbol) or
  654. not(is_shifter_const(tmpref.offset,b)) or
  655. ((tmpref.base<>NR_NO) and (tmpref.index<>NR_NO)) then
  656. fixref(list,tmpref);
  657. if ref.index<>NR_NO then
  658. begin
  659. end
  660. { else
  661. list.concat(taicpu.op_reg_reg(A_MOV,r,));
  662. ref.signindex<0 then }
  663. end;
  664. procedure tcgarm.fixref(list : taasmoutput;var ref : treference);
  665. var
  666. tmpreg : tregister;
  667. tmpref : treference;
  668. l : tasmlabel;
  669. begin
  670. { absolute symbols can't be handled directly, we've to store the symbol reference
  671. in the text segment and access it pc relative
  672. For now, we assume that references where base or index equals to PC are already
  673. relative, all other references are assumed to be absolute and thus they need
  674. to be handled extra.
  675. A proper solution would be to change refoptions to a set and store the information
  676. if the symbol is absolute or relative there.
  677. }
  678. { check consts distance }
  679. {!!!!!}
  680. { create consts entry }
  681. objectlibrary.getdatalabel(l);
  682. current_procinfo.aktlocaldata.concat(Tai_symbol.Create(l,0));
  683. if assigned(ref.symbol) then
  684. current_procinfo.aktlocaldata.concat(tai_const_symbol.Create_offset(ref.symbol,ref.offset))
  685. else
  686. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(ref.offset));
  687. { load consts entry }
  688. tmpreg:=rg.getregisterint(list,OS_INT);
  689. reference_reset(tmpref);
  690. tmpref.symbol:=l;
  691. tmpref.base:=NR_PC;
  692. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  693. if (ref.base<>NR_NO) then
  694. begin
  695. if ref.index<>NR_NO then
  696. begin
  697. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  698. rg.ungetregisterint(list,ref.base);
  699. ref.base:=tmpreg;
  700. end
  701. else
  702. begin
  703. ref.index:=tmpreg;
  704. ref.shiftimm:=0;
  705. ref.signindex:=1;
  706. ref.shiftmode:=SM_None;
  707. end;
  708. end
  709. else
  710. ref.base:=tmpreg;
  711. ref.offset:=0;
  712. ref.symbol:=nil;
  713. end;
  714. procedure tcgarm.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  715. var
  716. srcref,dstref:treference;
  717. srcreg,destreg,countreg,r:tregister;
  718. helpsize:aword;
  719. copysize:byte;
  720. cgsize:Tcgsize;
  721. procedure genloop(count : aword;size : byte);
  722. const
  723. size2opsize : array[1..4] of tcgsize = (OS_8,OS_16,OS_NO,OS_32);
  724. var
  725. l : tasmlabel;
  726. begin
  727. objectlibrary.getdatalabel(l);
  728. a_load_const_reg(list,OS_INT,count,countreg);
  729. list.concat(Tai_symbol.Create(l,0));
  730. srcref.addressmode:=AM_POSTINDEXED;
  731. dstref.addressmode:=AM_POSTINDEXED;
  732. srcref.offset:=size;
  733. dstref.offset:=size;
  734. r:=rg.getregisterint(list,size2opsize[size]);
  735. a_load_ref_reg(list,size2opsize[size],size2opsize[size],srcref,r);
  736. a_load_reg_ref(list,size2opsize[size],size2opsize[size],r,dstref);
  737. rg.ungetregisterint(list,r);
  738. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,countreg,countreg,1),PF_S));
  739. list.concat(setcondition(taicpu.op_sym(A_B,l),C_NE));
  740. end;
  741. begin
  742. helpsize:=12;
  743. dstref:=dest;
  744. srcref:=source;
  745. if cs_littlesize in aktglobalswitches then
  746. helpsize:=8;
  747. if not loadref and (len<=helpsize) then
  748. begin
  749. copysize:=4;
  750. cgsize:=OS_32;
  751. while len<>0 do
  752. begin
  753. if len<2 then
  754. begin
  755. copysize:=1;
  756. cgsize:=OS_8;
  757. end
  758. else if len<4 then
  759. begin
  760. copysize:=2;
  761. cgsize:=OS_16;
  762. end;
  763. dec(len,copysize);
  764. r:=rg.getregisterint(list,cgsize);
  765. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  766. if (len=0) and delsource then
  767. reference_release(list,source);
  768. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  769. inc(srcref.offset,copysize);
  770. inc(dstref.offset,copysize);
  771. rg.ungetregisterint(list,r);
  772. end;
  773. end
  774. else
  775. begin
  776. destreg:=rg.getregisterint(list,OS_ADDR);
  777. a_loadaddr_ref_reg(list,dest,destreg);
  778. srcreg:=rg.getregisterint(list,OS_ADDR);
  779. if loadref then
  780. a_load_ref_reg(list,OS_ADDR,OS_ADDR,source,srcreg)
  781. else
  782. begin
  783. a_loadaddr_ref_reg(list,source,srcreg);
  784. if delsource then
  785. begin
  786. srcref:=source;
  787. reference_release(list,srcref);
  788. end;
  789. end;
  790. countreg:=rg.getregisterint(list,OS_32);
  791. // if cs_littlesize in aktglobalswitches then
  792. genloop(len,1);
  793. {
  794. else
  795. begin
  796. helpsize:=len shr 2;
  797. len:=len and 3;
  798. if helpsize>1 then
  799. begin
  800. a_load_const_reg(list,OS_INT,helpsize,countreg);
  801. list.concat(Taicpu.op_none(A_REP,S_NO));
  802. end;
  803. if helpsize>0 then
  804. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  805. if len>1 then
  806. begin
  807. dec(len,2);
  808. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  809. end;
  810. if len=1 then
  811. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  812. end;
  813. }
  814. rg.ungetregisterint(list,countreg);
  815. rg.ungetregisterint(list,srcreg);
  816. rg.ungetregisterint(list,destreg);
  817. end;
  818. if delsource then
  819. tg.ungetiftemp(list,source);
  820. end;
  821. procedure tcgarm.g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef);
  822. begin
  823. end;
  824. procedure tcgarm.g_save_standard_registers(list : taasmoutput; usedinproc : tsuperregisterset);
  825. begin
  826. { we support only ARM standard calling conventions so this procedure has no use on the ARM }
  827. end;
  828. procedure tcgarm.g_restore_standard_registers(list : taasmoutput; usedinproc : tsuperregisterset);
  829. begin
  830. { we support only ARM standard calling conventions so this procedure has no use on the ARM }
  831. end;
  832. procedure tcgarm.g_save_all_registers(list : taasmoutput);
  833. begin
  834. { we support only ARM standard calling conventions so this procedure has no use on the ARM }
  835. end;
  836. procedure tcgarm.g_restore_all_registers(list : taasmoutput;accused,acchiused:boolean);
  837. begin
  838. { we support only ARM standard calling conventions so this procedure has no use on the ARM }
  839. end;
  840. procedure tcgarm.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  841. var
  842. ai : taicpu;
  843. begin
  844. ai:=Taicpu.Op_sym(A_B,l);
  845. ai.SetCondition(OpCmp2AsmCond[cond]);
  846. ai.is_jmp:=true;
  847. list.concat(ai);
  848. end;
  849. procedure tcg64farm.a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);
  850. var
  851. tmpreg : tregister;
  852. begin
  853. case op of
  854. OP_NEG:
  855. begin
  856. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSB,regdst.reglo,regsrc.reglo,0),PF_S));
  857. list.concat(taicpu.op_reg_reg_const(A_RSC,regdst.reghi,regsrc.reghi,0));
  858. end;
  859. else
  860. a_op64_reg_reg_reg(list,op,regsrc,regdst,regdst);
  861. end;
  862. end;
  863. procedure tcg64farm.a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);
  864. begin
  865. a_op64_const_reg_reg(list,op,value,reg,reg);
  866. end;
  867. procedure tcg64farm.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);
  868. var
  869. tmpreg : tregister;
  870. b : byte;
  871. begin
  872. case op of
  873. OP_AND,OP_OR,OP_XOR:
  874. begin
  875. cg.a_op_const_reg_reg(list,op,OS_32,lo(value),regsrc.reglo,regdst.reglo);
  876. cg.a_op_const_reg_reg(list,op,OS_32,hi(value),regsrc.reghi,regdst.reghi);
  877. end;
  878. OP_ADD:
  879. begin
  880. if is_shifter_const(lo(value),b) then
  881. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  882. else
  883. begin
  884. tmpreg:=rg.getregisterint(list,OS_32);
  885. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  886. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  887. rg.ungetregisterint(list,tmpreg);
  888. end;
  889. if is_shifter_const(hi(value),b) then
  890. list.concat(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,hi(value)))
  891. else
  892. begin
  893. tmpreg:=rg.getregisterint(list,OS_32);
  894. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  895. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg));
  896. rg.ungetregisterint(list,tmpreg);
  897. end;
  898. end;
  899. OP_SUB:
  900. begin
  901. if is_shifter_const(lo(value),b) then
  902. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  903. else
  904. begin
  905. tmpreg:=rg.getregisterint(list,OS_32);
  906. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  907. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  908. rg.ungetregisterint(list,tmpreg);
  909. end;
  910. if is_shifter_const(hi(value),b) then
  911. list.concat(taicpu.op_reg_reg_const(A_SBC,regdst.reghi,regsrc.reghi,hi(value)))
  912. else
  913. begin
  914. tmpreg:=rg.getregisterint(list,OS_32);
  915. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  916. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg));
  917. rg.ungetregisterint(list,tmpreg);
  918. end;
  919. end;
  920. else
  921. internalerror(2003083101);
  922. end;
  923. end;
  924. procedure tcg64farm.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  925. begin
  926. case op of
  927. OP_AND,OP_OR,OP_XOR:
  928. begin
  929. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  930. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  931. end;
  932. OP_ADD:
  933. begin
  934. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  935. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  936. end;
  937. OP_SUB:
  938. begin
  939. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  940. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi));
  941. end;
  942. else
  943. internalerror(2003083101);
  944. end;
  945. end;
  946. begin
  947. cg:=tcgarm.create;
  948. cg64:=tcg64farm.create;
  949. end.
  950. {
  951. $Log$
  952. Revision 1.16 2003-09-06 11:21:50 florian
  953. * fixed stm and ldm to be usable with preindex operand
  954. Revision 1.15 2003/09/05 23:57:01 florian
  955. * arm is working again as before the new register naming scheme was implemented
  956. Revision 1.14 2003/09/04 21:07:03 florian
  957. * ARM compiler compiles again
  958. Revision 1.13 2003/09/04 00:15:29 florian
  959. * first bunch of adaptions of arm compiler for new register type
  960. Revision 1.12 2003/09/03 19:10:30 florian
  961. * initial revision of new register naming
  962. Revision 1.11 2003/09/03 11:18:37 florian
  963. * fixed arm concatcopy
  964. + arm support in the common compiler sources added
  965. * moved some generic cg code around
  966. + tfputype added
  967. * ...
  968. Revision 1.10 2003/09/01 15:11:16 florian
  969. * fixed reference handling
  970. * fixed operand postfix for floating point instructions
  971. * fixed wrong shifter constant handling
  972. Revision 1.9 2003/09/01 09:54:57 florian
  973. * results of work on arm port last weekend
  974. Revision 1.8 2003/08/29 21:36:28 florian
  975. * fixed procedure entry/exit code
  976. * started to fix reference handling
  977. Revision 1.7 2003/08/28 13:26:10 florian
  978. * another couple of arm fixes
  979. Revision 1.6 2003/08/28 00:05:29 florian
  980. * today's arm patches
  981. Revision 1.5 2003/08/25 23:20:38 florian
  982. + started to implement FPU support for the ARM
  983. * fixed a lot of other things
  984. Revision 1.4 2003/08/24 12:27:26 florian
  985. * continued to work on the arm port
  986. Revision 1.3 2003/08/21 03:14:00 florian
  987. * arm compiler can be compiled; far from being working
  988. Revision 1.2 2003/08/20 15:50:12 florian
  989. * more arm stuff
  990. Revision 1.1 2003/07/21 16:35:30 florian
  991. * very basic stuff for the arm
  992. }