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