cgcpu.pas 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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. rip,rsp,rfp : tregister;
  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. { restore int registers and return }
  611. list.concat(setoppostfix(taicpu.op_reg_regset(A_STM,rsp,rg.used_in_proc_int-[RS_R0..RS_R3]+[RS_R11,RS_R12,RS_R15]),PF_FD));
  612. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_FRAME_POINTER_REG,NR_R12,4));
  613. a_reg_alloc(list,NR_R12);
  614. { allocate necessary stack size }
  615. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  616. end;
  617. procedure tcgarm.g_return_from_proc(list : taasmoutput;parasize : aword);
  618. begin
  619. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  620. list.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14))
  621. else
  622. { restore int registers and return }
  623. list.concat(setoppostfix(taicpu.op_reg_regset(A_LDM,NR_R11,rg.used_in_proc_int-[RS_R0..RS_R3]+[RS_R11,RS_R13,RS_R15]),PF_EA));
  624. end;
  625. procedure tcgarm.g_restore_frame_pointer(list : taasmoutput);
  626. begin
  627. { the frame pointer on the ARM is restored while the ret is executed }
  628. end;
  629. procedure tcgarm.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  630. var
  631. b : byte;
  632. tmpref : treference;
  633. begin
  634. tmpref:=ref;
  635. { Be sure to have a base register }
  636. if (tmpref.base=NR_NO) then
  637. begin
  638. if tmpref.shiftmode<>SM_None then
  639. internalerror(200308294);
  640. tmpref.base:=tmpref.index;
  641. tmpref.index:=NR_NO;
  642. end;
  643. if assigned(tmpref.symbol) or
  644. not(is_shifter_const(tmpref.offset,b)) or
  645. ((tmpref.base<>NR_NO) and (tmpref.index<>NR_NO)) then
  646. fixref(list,tmpref);
  647. if ref.index<>NR_NO then
  648. begin
  649. end
  650. { else
  651. list.concat(taicpu.op_reg_reg(A_MOV,r,));
  652. ref.signindex<0 then }
  653. end;
  654. procedure tcgarm.fixref(list : taasmoutput;var ref : treference);
  655. var
  656. tmpreg : tregister;
  657. tmpref : treference;
  658. l : tasmlabel;
  659. begin
  660. { absolute symbols can't be handled directly, we've to store the symbol reference
  661. in the text segment and access it pc relative
  662. For now, we assume that references where base or index equals to PC are already
  663. relative, all other references are assumed to be absolute and thus they need
  664. to be handled extra.
  665. A proper solution would be to change refoptions to a set and store the information
  666. if the symbol is absolute or relative there.
  667. }
  668. { check consts distance }
  669. {!!!!!}
  670. { create consts entry }
  671. objectlibrary.getdatalabel(l);
  672. current_procinfo.aktlocaldata.concat(Tai_symbol.Create(l,0));
  673. if assigned(ref.symbol) then
  674. current_procinfo.aktlocaldata.concat(tai_const_symbol.Create_offset(ref.symbol,ref.offset))
  675. else
  676. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(ref.offset));
  677. { load consts entry }
  678. tmpreg:=rg.getregisterint(list,OS_INT);
  679. reference_reset(tmpref);
  680. tmpref.symbol:=l;
  681. tmpref.base:=NR_PC;
  682. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  683. if (ref.base<>NR_NO) then
  684. begin
  685. if ref.index<>NR_NO then
  686. begin
  687. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  688. rg.ungetregister(list,ref.base);
  689. ref.base:=tmpreg;
  690. end
  691. else
  692. begin
  693. ref.index:=tmpreg;
  694. ref.shiftimm:=0;
  695. ref.signindex:=1;
  696. ref.shiftmode:=SM_None;
  697. end;
  698. end
  699. else
  700. ref.base:=tmpreg;
  701. ref.offset:=0;
  702. ref.symbol:=nil;
  703. end;
  704. procedure tcgarm.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  705. var
  706. srcref,dstref:treference;
  707. srcreg,destreg,countreg,r:tregister;
  708. helpsize:aword;
  709. copysize:byte;
  710. cgsize:Tcgsize;
  711. procedure genloop(count : aword;size : byte);
  712. const
  713. size2opsize : array[1..4] of tcgsize = (OS_8,OS_16,OS_NO,OS_32);
  714. var
  715. l : tasmlabel;
  716. begin
  717. objectlibrary.getdatalabel(l);
  718. a_load_const_reg(list,OS_INT,count,countreg);
  719. list.concat(Tai_symbol.Create(l,0));
  720. srcref.addressmode:=AM_POSTINDEXED;
  721. dstref.addressmode:=AM_POSTINDEXED;
  722. srcref.offset:=size;
  723. dstref.offset:=size;
  724. r:=rg.getregisterint(list,size2opsize[size]);
  725. a_load_ref_reg(list,size2opsize[size],size2opsize[size],srcref,r);
  726. a_load_reg_ref(list,size2opsize[size],size2opsize[size],r,dstref);
  727. rg.ungetregisterint(list,r);
  728. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,countreg,countreg,1),PF_S));
  729. list.concat(setcondition(taicpu.op_sym(A_B,l),C_NE));
  730. end;
  731. begin
  732. helpsize:=12;
  733. dstref:=dest;
  734. srcref:=source;
  735. if cs_littlesize in aktglobalswitches then
  736. helpsize:=8;
  737. if not loadref and (len<=helpsize) then
  738. begin
  739. copysize:=4;
  740. cgsize:=OS_32;
  741. while len<>0 do
  742. begin
  743. if len<2 then
  744. begin
  745. copysize:=1;
  746. cgsize:=OS_8;
  747. end
  748. else if len<4 then
  749. begin
  750. copysize:=2;
  751. cgsize:=OS_16;
  752. end;
  753. dec(len,copysize);
  754. r:=rg.getregisterint(list,cgsize);
  755. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  756. if (len=0) and delsource then
  757. reference_release(list,source);
  758. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  759. inc(srcref.offset,copysize);
  760. inc(dstref.offset,copysize);
  761. rg.ungetregisterint(list,r);
  762. end;
  763. end
  764. else
  765. begin
  766. destreg:=rg.getregisterint(list,OS_ADDR);
  767. a_loadaddr_ref_reg(list,dest,destreg);
  768. srcreg:=rg.getregisterint(list,OS_ADDR);
  769. if loadref then
  770. a_load_ref_reg(list,OS_ADDR,OS_ADDR,source,srcreg)
  771. else
  772. begin
  773. a_loadaddr_ref_reg(list,source,srcreg);
  774. if delsource then
  775. begin
  776. srcref:=source;
  777. reference_release(list,srcref);
  778. end;
  779. end;
  780. countreg:=rg.getregisterint(list,OS_32);
  781. // if cs_littlesize in aktglobalswitches then
  782. genloop(len,1);
  783. {
  784. else
  785. begin
  786. helpsize:=len shr 2;
  787. len:=len and 3;
  788. if helpsize>1 then
  789. begin
  790. a_load_const_reg(list,OS_INT,helpsize,countreg);
  791. list.concat(Taicpu.op_none(A_REP,S_NO));
  792. end;
  793. if helpsize>0 then
  794. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  795. if len>1 then
  796. begin
  797. dec(len,2);
  798. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  799. end;
  800. if len=1 then
  801. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  802. end;
  803. }
  804. rg.ungetregisterint(list,countreg);
  805. rg.ungetregisterint(list,srcreg);
  806. rg.ungetregisterint(list,destreg);
  807. end;
  808. if delsource then
  809. tg.ungetiftemp(list,source);
  810. end;
  811. procedure tcgarm.g_overflowcheck(list: taasmoutput; const l: tlocation; def: tdef);
  812. begin
  813. end;
  814. procedure tcgarm.g_save_standard_registers(list : taasmoutput; usedinproc : tsuperregisterset);
  815. begin
  816. { we support only ARM standard calling conventions so this procedure has no use on the ARM }
  817. end;
  818. procedure tcgarm.g_restore_standard_registers(list : taasmoutput; usedinproc : tsuperregisterset);
  819. begin
  820. { we support only ARM standard calling conventions so this procedure has no use on the ARM }
  821. end;
  822. procedure tcgarm.g_save_all_registers(list : taasmoutput);
  823. begin
  824. { we support only ARM standard calling conventions so this procedure has no use on the ARM }
  825. end;
  826. procedure tcgarm.g_restore_all_registers(list : taasmoutput;accused,acchiused:boolean);
  827. begin
  828. { we support only ARM standard calling conventions so this procedure has no use on the ARM }
  829. end;
  830. procedure tcgarm.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  831. var
  832. ai : taicpu;
  833. begin
  834. ai:=Taicpu.Op_sym(A_B,l);
  835. ai.SetCondition(OpCmp2AsmCond[cond]);
  836. ai.is_jmp:=true;
  837. list.concat(ai);
  838. end;
  839. procedure tcg64farm.a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);
  840. var
  841. tmpreg : tregister;
  842. begin
  843. case op of
  844. OP_NEG:
  845. begin
  846. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSB,regdst.reglo,regsrc.reglo,0),PF_S));
  847. list.concat(taicpu.op_reg_reg_const(A_RSC,regdst.reghi,regsrc.reghi,0));
  848. end;
  849. else
  850. a_op64_reg_reg_reg(list,op,regsrc,regdst,regdst);
  851. end;
  852. end;
  853. procedure tcg64farm.a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);
  854. begin
  855. a_op64_const_reg_reg(list,op,value,reg,reg);
  856. end;
  857. procedure tcg64farm.a_op64_const_reg_reg(list: taasmoutput;op:TOpCG;value : qword;regsrc,regdst : tregister64);
  858. var
  859. tmpreg : tregister;
  860. b : byte;
  861. begin
  862. case op of
  863. OP_AND,OP_OR,OP_XOR:
  864. begin
  865. cg.a_op_const_reg_reg(list,op,OS_32,lo(value),regsrc.reglo,regdst.reglo);
  866. cg.a_op_const_reg_reg(list,op,OS_32,hi(value),regsrc.reghi,regdst.reghi);
  867. end;
  868. OP_ADD:
  869. begin
  870. if is_shifter_const(lo(value),b) then
  871. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  872. else
  873. begin
  874. tmpreg:=rg.getregisterint(list,OS_32);
  875. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  876. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  877. rg.ungetregisterint(list,tmpreg);
  878. end;
  879. if is_shifter_const(hi(value),b) then
  880. list.concat(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,hi(value)))
  881. else
  882. begin
  883. tmpreg:=rg.getregisterint(list,OS_32);
  884. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  885. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg));
  886. rg.ungetregisterint(list,tmpreg);
  887. end;
  888. end;
  889. OP_SUB:
  890. begin
  891. if is_shifter_const(lo(value),b) then
  892. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  893. else
  894. begin
  895. tmpreg:=rg.getregisterint(list,OS_32);
  896. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  897. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  898. rg.ungetregisterint(list,tmpreg);
  899. end;
  900. if is_shifter_const(hi(value),b) then
  901. list.concat(taicpu.op_reg_reg_const(A_SBC,regdst.reghi,regsrc.reghi,hi(value)))
  902. else
  903. begin
  904. tmpreg:=rg.getregisterint(list,OS_32);
  905. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  906. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg));
  907. rg.ungetregisterint(list,tmpreg);
  908. end;
  909. end;
  910. else
  911. internalerror(2003083101);
  912. end;
  913. end;
  914. procedure tcg64farm.a_op64_reg_reg_reg(list: taasmoutput;op:TOpCG;regsrc1,regsrc2,regdst : tregister64);
  915. begin
  916. case op of
  917. OP_AND,OP_OR,OP_XOR:
  918. begin
  919. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  920. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  921. end;
  922. OP_ADD:
  923. begin
  924. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  925. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  926. end;
  927. OP_SUB:
  928. begin
  929. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  930. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi));
  931. end;
  932. else
  933. internalerror(2003083101);
  934. end;
  935. end;
  936. begin
  937. cg:=tcgarm.create;
  938. cg64:=tcg64farm.create;
  939. end.
  940. {
  941. $Log$
  942. Revision 1.14 2003-09-04 21:07:03 florian
  943. * ARM compiler compiles again
  944. Revision 1.13 2003/09/04 00:15:29 florian
  945. * first bunch of adaptions of arm compiler for new register type
  946. Revision 1.12 2003/09/03 19:10:30 florian
  947. * initial revision of new register naming
  948. Revision 1.11 2003/09/03 11:18:37 florian
  949. * fixed arm concatcopy
  950. + arm support in the common compiler sources added
  951. * moved some generic cg code around
  952. + tfputype added
  953. * ...
  954. Revision 1.10 2003/09/01 15:11:16 florian
  955. * fixed reference handling
  956. * fixed operand postfix for floating point instructions
  957. * fixed wrong shifter constant handling
  958. Revision 1.9 2003/09/01 09:54:57 florian
  959. * results of work on arm port last weekend
  960. Revision 1.8 2003/08/29 21:36:28 florian
  961. * fixed procedure entry/exit code
  962. * started to fix reference handling
  963. Revision 1.7 2003/08/28 13:26:10 florian
  964. * another couple of arm fixes
  965. Revision 1.6 2003/08/28 00:05:29 florian
  966. * today's arm patches
  967. Revision 1.5 2003/08/25 23:20:38 florian
  968. + started to implement FPU support for the ARM
  969. * fixed a lot of other things
  970. Revision 1.4 2003/08/24 12:27:26 florian
  971. * continued to work on the arm port
  972. Revision 1.3 2003/08/21 03:14:00 florian
  973. * arm compiler can be compiled; far from being working
  974. Revision 1.2 2003/08/20 15:50:12 florian
  975. * more arm stuff
  976. Revision 1.1 2003/07/21 16:35:30 florian
  977. * very basic stuff for the arm
  978. }