cgcpu.pas 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059
  1. {
  2. Copyright (c) 2003 by Florian Klaempfl
  3. Member of the Free Pascal development team
  4. This unit implements the code generator for the ARM
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cgcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype,symtype,symdef,
  23. cgbase,cgutils,cgobj,
  24. aasmbase,aasmcpu,aasmtai,aasmdata,
  25. parabase,
  26. cpubase,cpuinfo,node,cg64f32,rgcpu;
  27. type
  28. tcgarm = class(tcg)
  29. { true, if the next arithmetic operation should modify the flags }
  30. cgsetflags : boolean;
  31. procedure init_register_allocators;override;
  32. procedure done_register_allocators;override;
  33. procedure a_param_const(list : TAsmList;size : tcgsize;a : aint;const paraloc : TCGPara);override;
  34. procedure a_param_ref(list : TAsmList;size : tcgsize;const r : treference;const paraloc : TCGPara);override;
  35. procedure a_paramaddr_ref(list : TAsmList;const r : treference;const paraloc : TCGPara);override;
  36. procedure a_call_name(list : TAsmList;const s : string);override;
  37. procedure a_call_reg(list : TAsmList;reg: tregister);override;
  38. procedure a_call_ref(list : TAsmList;ref: treference);override;
  39. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister); override;
  40. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  41. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  42. size: tcgsize; a: aint; src, dst: tregister); override;
  43. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  44. size: tcgsize; src1, src2, dst: tregister); override;
  45. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  46. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  47. { move instructions }
  48. procedure a_load_const_reg(list : TAsmList; size: tcgsize; a : aint;reg : tregister);override;
  49. procedure a_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);override;
  50. procedure a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  51. procedure a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);override;
  52. function a_internal_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference):treference;
  53. function a_internal_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister):treference;
  54. { fpu move instructions }
  55. procedure a_loadfpu_reg_reg(list: TAsmList; size: tcgsize; reg1, reg2: tregister); override;
  56. procedure a_loadfpu_ref_reg(list: TAsmList; size: tcgsize; const ref: treference; reg: tregister); override;
  57. procedure a_loadfpu_reg_ref(list: TAsmList; size: tcgsize; reg: tregister; const ref: treference); override;
  58. procedure a_paramfpu_ref(list : TAsmList;size : tcgsize;const ref : treference;const paraloc : TCGPara);override;
  59. { comparison operations }
  60. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  61. l : tasmlabel);override;
  62. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  63. procedure a_jmp_name(list : TAsmList;const s : string); override;
  64. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  65. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  66. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  67. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  68. procedure g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean); override;
  69. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  70. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : aint);override;
  71. procedure g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : aint);override;
  72. procedure g_concatcopy_move(list : TAsmList;const source,dest : treference;len : aint);
  73. procedure g_concatcopy_internal(list : TAsmList;const source,dest : treference;len : aint;aligned : boolean);
  74. procedure g_overflowcheck(list: TAsmList; const l: tlocation; def: tdef); override;
  75. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);override;
  76. procedure g_save_standard_registers(list : TAsmList);override;
  77. procedure g_restore_standard_registers(list : TAsmList);override;
  78. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  79. procedure fixref(list : TAsmList;var ref : treference);
  80. function handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference;
  81. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  82. end;
  83. tcg64farm = class(tcg64f32)
  84. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  85. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  86. procedure a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);override;
  87. procedure a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);override;
  88. procedure a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);override;
  89. procedure a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);override;
  90. end;
  91. const
  92. OpCmp2AsmCond : Array[topcmp] of TAsmCond = (C_NONE,C_EQ,C_GT,
  93. C_LT,C_GE,C_LE,C_NE,C_LS,C_CC,C_CS,C_HI);
  94. winstackpagesize = 4096;
  95. function get_fpu_postfix(def : tdef) : toppostfix;
  96. implementation
  97. uses
  98. globals,verbose,systems,cutils,
  99. fmodule,
  100. symconst,symsym,
  101. tgobj,
  102. procinfo,cpupi,
  103. paramgr;
  104. function get_fpu_postfix(def : tdef) : toppostfix;
  105. begin
  106. if def.deftype=floatdef then
  107. begin
  108. case tfloatdef(def).typ of
  109. s32real:
  110. result:=PF_S;
  111. s64real:
  112. result:=PF_D;
  113. s80real:
  114. result:=PF_E;
  115. else
  116. internalerror(200401272);
  117. end;
  118. end
  119. else
  120. internalerror(200401271);
  121. end;
  122. procedure tcgarm.init_register_allocators;
  123. begin
  124. inherited init_register_allocators;
  125. { currently, we save R14 always, so we can use it }
  126. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  127. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  128. RS_R9,RS_R10,RS_R12,RS_R14],first_int_imreg,[]);
  129. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  130. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7],first_fpu_imreg,[]);
  131. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,
  132. [RS_S0,RS_S1,RS_R2,RS_R3,RS_R4,RS_S31],first_mm_imreg,[]);
  133. end;
  134. procedure tcgarm.done_register_allocators;
  135. begin
  136. rg[R_INTREGISTER].free;
  137. rg[R_FPUREGISTER].free;
  138. rg[R_MMREGISTER].free;
  139. inherited done_register_allocators;
  140. end;
  141. procedure tcgarm.a_param_const(list : TAsmList;size : tcgsize;a : aint;const paraloc : TCGPara);
  142. var
  143. ref: treference;
  144. begin
  145. paraloc.check_simple_location;
  146. case paraloc.location^.loc of
  147. LOC_REGISTER,LOC_CREGISTER:
  148. a_load_const_reg(list,size,a,paraloc.location^.register);
  149. LOC_REFERENCE:
  150. begin
  151. reference_reset(ref);
  152. ref.base:=paraloc.location^.reference.index;
  153. ref.offset:=paraloc.location^.reference.offset;
  154. a_load_const_ref(list,size,a,ref);
  155. end;
  156. else
  157. internalerror(2002081101);
  158. end;
  159. end;
  160. procedure tcgarm.a_param_ref(list : TAsmList;size : tcgsize;const r : treference;const paraloc : TCGPara);
  161. var
  162. tmpref, ref: treference;
  163. location: pcgparalocation;
  164. sizeleft: aint;
  165. begin
  166. location := paraloc.location;
  167. tmpref := r;
  168. sizeleft := paraloc.intsize;
  169. while assigned(location) do
  170. begin
  171. case location^.loc of
  172. LOC_REGISTER,LOC_CREGISTER:
  173. a_load_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  174. LOC_REFERENCE:
  175. begin
  176. reference_reset_base(ref,location^.reference.index,location^.reference.offset);
  177. { doubles in softemu mode have a strange order of registers and references }
  178. if location^.size=OS_32 then
  179. g_concatcopy(list,tmpref,ref,4)
  180. else
  181. begin
  182. g_concatcopy(list,tmpref,ref,sizeleft);
  183. if assigned(location^.next) then
  184. internalerror(2005010710);
  185. end;
  186. end;
  187. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  188. case location^.size of
  189. OS_F32, OS_F64:
  190. a_loadfpu_ref_reg(list,location^.size,tmpref,location^.register);
  191. else
  192. internalerror(2002072801);
  193. end;
  194. LOC_VOID:
  195. begin
  196. // nothing to do
  197. end;
  198. else
  199. internalerror(2002081103);
  200. end;
  201. inc(tmpref.offset,tcgsize2size[location^.size]);
  202. dec(sizeleft,tcgsize2size[location^.size]);
  203. location := location^.next;
  204. end;
  205. end;
  206. procedure tcgarm.a_paramaddr_ref(list : TAsmList;const r : treference;const paraloc : TCGPara);
  207. var
  208. ref: treference;
  209. tmpreg: tregister;
  210. begin
  211. paraloc.check_simple_location;
  212. case paraloc.location^.loc of
  213. LOC_REGISTER,LOC_CREGISTER:
  214. a_loadaddr_ref_reg(list,r,paraloc.location^.register);
  215. LOC_REFERENCE:
  216. begin
  217. reference_reset(ref);
  218. ref.base := paraloc.location^.reference.index;
  219. ref.offset := paraloc.location^.reference.offset;
  220. tmpreg := getintregister(list,OS_ADDR);
  221. a_loadaddr_ref_reg(list,r,tmpreg);
  222. a_load_reg_ref(list,OS_ADDR,OS_ADDR,tmpreg,ref);
  223. end;
  224. else
  225. internalerror(2002080701);
  226. end;
  227. end;
  228. procedure tcgarm.a_call_name(list : TAsmList;const s : string);
  229. begin
  230. list.concat(taicpu.op_sym(A_BL,current_asmdata.RefAsmSymbol(s)));
  231. {
  232. the compiler does not properly set this flag anymore in pass 1, and
  233. for now we only need it after pass 2 (I hope) (JM)
  234. if not(pi_do_call in current_procinfo.flags) then
  235. internalerror(2003060703);
  236. }
  237. include(current_procinfo.flags,pi_do_call);
  238. end;
  239. procedure tcgarm.a_call_reg(list : TAsmList;reg: tregister);
  240. var
  241. r : tregister;
  242. begin
  243. list.concat(taicpu.op_reg_reg(A_MOV,NR_R14,NR_PC));
  244. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,reg));
  245. {
  246. the compiler does not properly set this flag anymore in pass 1, and
  247. for now we only need it after pass 2 (I hope) (JM)
  248. if not(pi_do_call in current_procinfo.flags) then
  249. internalerror(2003060703);
  250. }
  251. include(current_procinfo.flags,pi_do_call);
  252. end;
  253. procedure tcgarm.a_call_ref(list : TAsmList;ref: treference);
  254. begin
  255. a_reg_alloc(list,NR_R12);
  256. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,NR_R12);
  257. list.concat(taicpu.op_reg_reg(A_MOV,NR_R14,NR_PC));
  258. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R12));
  259. a_reg_dealloc(list,NR_R12);
  260. include(current_procinfo.flags,pi_do_call);
  261. end;
  262. procedure tcgarm.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister);
  263. begin
  264. a_op_const_reg_reg(list,op,size,a,reg,reg);
  265. end;
  266. procedure tcgarm.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  267. begin
  268. case op of
  269. OP_NEG:
  270. list.concat(taicpu.op_reg_reg_const(A_RSB,dst,src,0));
  271. OP_NOT:
  272. begin
  273. list.concat(taicpu.op_reg_reg(A_MVN,dst,src));
  274. case size of
  275. OS_8 :
  276. a_op_const_reg_reg(list,OP_AND,OS_INT,$ff,dst,dst);
  277. OS_16 :
  278. a_op_const_reg_reg(list,OP_AND,OS_INT,$ffff,dst,dst);
  279. end;
  280. end
  281. else
  282. a_op_reg_reg_reg(list,op,OS_32,src,dst,dst);
  283. end;
  284. end;
  285. const
  286. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  287. (A_NONE,A_MOV,A_ADD,A_AND,A_NONE,A_NONE,A_MUL,A_MUL,A_NONE,A_NONE,A_ORR,
  288. A_NONE,A_NONE,A_NONE,A_SUB,A_EOR);
  289. procedure tcgarm.a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  290. size: tcgsize; a: aint; src, dst: tregister);
  291. var
  292. ovloc : tlocation;
  293. begin
  294. a_op_const_reg_reg_checkoverflow(list,op,size,a,src,dst,false,ovloc);
  295. end;
  296. procedure tcgarm.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  297. size: tcgsize; src1, src2, dst: tregister);
  298. var
  299. ovloc : tlocation;
  300. begin
  301. a_op_reg_reg_reg_checkoverflow(list,op,size,src1,src2,dst,false,ovloc);
  302. end;
  303. procedure tcgarm.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  304. var
  305. shift : byte;
  306. tmpreg : tregister;
  307. so : tshifterop;
  308. l1 : longint;
  309. begin
  310. ovloc.loc:=LOC_VOID;
  311. if is_shifter_const(-a,shift) then
  312. case op of
  313. OP_ADD:
  314. begin
  315. op:=OP_SUB;
  316. a:=dword(-a);
  317. end;
  318. OP_SUB:
  319. begin
  320. op:=OP_ADD;
  321. a:=dword(-a);
  322. end
  323. end;
  324. if is_shifter_const(a,shift) and not(op in [OP_IMUL,OP_MUL]) then
  325. case op of
  326. OP_NEG,OP_NOT,
  327. OP_DIV,OP_IDIV:
  328. internalerror(200308281);
  329. OP_SHL:
  330. begin
  331. if a>32 then
  332. internalerror(200308291);
  333. if a<>0 then
  334. begin
  335. shifterop_reset(so);
  336. so.shiftmode:=SM_LSL;
  337. so.shiftimm:=a;
  338. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  339. end
  340. else
  341. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  342. end;
  343. OP_SHR:
  344. begin
  345. if a>32 then
  346. internalerror(200308292);
  347. shifterop_reset(so);
  348. if a<>0 then
  349. begin
  350. so.shiftmode:=SM_LSR;
  351. so.shiftimm:=a;
  352. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  353. end
  354. else
  355. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  356. end;
  357. OP_SAR:
  358. begin
  359. if a>32 then
  360. internalerror(200308291);
  361. if a<>0 then
  362. begin
  363. shifterop_reset(so);
  364. so.shiftmode:=SM_ASR;
  365. so.shiftimm:=a;
  366. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  367. end
  368. else
  369. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  370. end;
  371. else
  372. list.concat(setoppostfix(
  373. taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,src,a),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))
  374. ));
  375. if (cgsetflags or setflags) and (size in [OS_8,OS_16,OS_32]) then
  376. begin
  377. ovloc.loc:=LOC_FLAGS;
  378. case op of
  379. OP_ADD:
  380. ovloc.resflags:=F_CS;
  381. OP_SUB:
  382. ovloc.resflags:=F_CC;
  383. end;
  384. end;
  385. end
  386. else
  387. begin
  388. { there could be added some more sophisticated optimizations }
  389. if (op in [OP_MUL,OP_IMUL]) and (a=1) then
  390. a_load_reg_reg(list,size,size,src,dst)
  391. else if (op in [OP_MUL,OP_IMUL]) and (a=0) then
  392. a_load_const_reg(list,size,0,dst)
  393. else if (op in [OP_IMUL]) and (a=-1) then
  394. a_op_reg_reg(list,OP_NEG,size,src,dst)
  395. { we do this here instead in the peephole optimizer because
  396. it saves us a register }
  397. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a,l1) and not(cgsetflags or setflags) then
  398. a_op_const_reg_reg(list,OP_SHL,size,l1,src,dst)
  399. { for example : b=a*5 -> b=a*4+a with add instruction and shl }
  400. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a-1,l1) and not(cgsetflags or setflags) then
  401. begin
  402. if l1>32 then{roozbeh does this ever happen?}
  403. internalerror(200308291);
  404. shifterop_reset(so);
  405. so.shiftmode:=SM_LSL;
  406. so.shiftimm:=l1;
  407. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,src,src,so));
  408. end
  409. else
  410. begin
  411. tmpreg:=getintregister(list,size);
  412. a_load_const_reg(list,size,a,tmpreg);
  413. a_op_reg_reg_reg_checkoverflow(list,op,size,tmpreg,src,dst,setflags,ovloc);
  414. end;
  415. end;
  416. end;
  417. procedure tcgarm.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  418. var
  419. so : tshifterop;
  420. tmpreg,overflowreg : tregister;
  421. asmop : tasmop;
  422. begin
  423. ovloc.loc:=LOC_VOID;
  424. case op of
  425. OP_NEG,OP_NOT,
  426. OP_DIV,OP_IDIV:
  427. internalerror(200308281);
  428. OP_SHL:
  429. begin
  430. shifterop_reset(so);
  431. so.rs:=src1;
  432. so.shiftmode:=SM_LSL;
  433. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src2,so));
  434. end;
  435. OP_SHR:
  436. begin
  437. shifterop_reset(so);
  438. so.rs:=src1;
  439. so.shiftmode:=SM_LSR;
  440. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src2,so));
  441. end;
  442. OP_SAR:
  443. begin
  444. shifterop_reset(so);
  445. so.rs:=src1;
  446. so.shiftmode:=SM_ASR;
  447. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src2,so));
  448. end;
  449. OP_IMUL,
  450. OP_MUL:
  451. begin
  452. if cgsetflags or setflags then
  453. begin
  454. overflowreg:=getintregister(list,size);
  455. if op=OP_IMUL then
  456. asmop:=A_SMULL
  457. else
  458. asmop:=A_UMULL;
  459. { the arm doesn't allow that rd and rm are the same }
  460. if dst=src2 then
  461. begin
  462. if dst<>src1 then
  463. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src1,src2))
  464. else
  465. begin
  466. tmpreg:=getintregister(list,size);
  467. a_load_reg_reg(list,size,size,src2,dst);
  468. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,tmpreg,src1));
  469. end;
  470. end
  471. else
  472. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src2,src1));
  473. if op=OP_IMUL then
  474. begin
  475. shifterop_reset(so);
  476. so.shiftmode:=SM_ASR;
  477. so.shiftimm:=31;
  478. list.concat(taicpu.op_reg_reg_shifterop(A_CMP,overflowreg,dst,so));
  479. end
  480. else
  481. list.concat(taicpu.op_reg_const(A_CMP,overflowreg,0));
  482. ovloc.loc:=LOC_FLAGS;
  483. ovloc.resflags:=F_NE;
  484. end
  485. else
  486. begin
  487. { the arm doesn't allow that rd and rm are the same }
  488. if dst=src2 then
  489. begin
  490. if dst<>src1 then
  491. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src1,src2))
  492. else
  493. begin
  494. tmpreg:=getintregister(list,size);
  495. a_load_reg_reg(list,size,size,src2,dst);
  496. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,tmpreg,src1));
  497. end;
  498. end
  499. else
  500. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src2,src1));
  501. end;
  502. end;
  503. else
  504. list.concat(setoppostfix(
  505. taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))
  506. ));
  507. end;
  508. end;
  509. procedure tcgarm.a_load_const_reg(list : TAsmList; size: tcgsize; a : aint;reg : tregister);
  510. var
  511. imm_shift : byte;
  512. l : tasmlabel;
  513. hr : treference;
  514. tmpreg : tregister;
  515. begin
  516. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  517. internalerror(2002090902);
  518. if is_shifter_const(a,imm_shift) then
  519. list.concat(taicpu.op_reg_const(A_MOV,reg,a))
  520. else if is_shifter_const(not(a),imm_shift) then
  521. list.concat(taicpu.op_reg_const(A_MVN,reg,not(a)))
  522. { loading of constants with mov and orr }
  523. {else [if (is_shifter_const(a-byte(a),imm_shift)) then
  524. begin
  525. }{ roozbeh:why using tmpreg later causes error in compiling of system.pp,and also those other similars}
  526. {list.concat(taicpu.op_reg_const(A_MOV,reg,a-byte(a)));
  527. list.concat(taicpu.op_reg_reg_const(A_ORR,reg,reg,byte(a)));
  528. end
  529. else if (is_shifter_const(a-word(a),imm_shift)) and (is_shifter_const(word(a),imm_shift)) then
  530. begin
  531. list.concat(taicpu.op_reg_const(A_MOV,reg,a-word(a)));
  532. list.concat(taicpu.op_reg_reg_const(A_ORR,reg,reg,word(a)));
  533. end
  534. else if (is_shifter_const(a-(longint(a) shl 8) shr 8,imm_shift)) and (is_shifter_const((longint(a) shl 8) shr 8,imm_shift)) then
  535. begin
  536. list.concat(taicpu.op_reg_const(A_MOV,reg,a-(longint(a) shl 8)shr 8));
  537. list.concat(taicpu.op_reg_reg_const(A_ORR,reg,reg,(longint(a) shl 8)shr 8));
  538. end}
  539. else
  540. begin
  541. reference_reset(hr);
  542. current_asmdata.getjumplabel(l);
  543. cg.a_label(current_procinfo.aktlocaldata,l);
  544. hr.symboldata:=current_procinfo.aktlocaldata.last;
  545. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(longint(a)));
  546. hr.symbol:=l;
  547. list.concat(taicpu.op_reg_ref(A_LDR,reg,hr));
  548. end;
  549. end;
  550. function tcgarm.handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference;
  551. var
  552. tmpreg,tmpreg2 : tregister;
  553. tmpref : treference;
  554. l : tasmlabel;
  555. so : tshifterop;
  556. begin
  557. tmpreg:=NR_NO;
  558. { Be sure to have a base register }
  559. if (ref.base=NR_NO) then
  560. begin
  561. if ref.shiftmode<>SM_None then
  562. internalerror(200308294);
  563. ref.base:=ref.index;
  564. ref.index:=NR_NO;
  565. end;
  566. { absolute symbols can't be handled directly, we've to store the symbol reference
  567. in the text segment and access it pc relative
  568. For now, we assume that references where base or index equals to PC are already
  569. relative, all other references are assumed to be absolute and thus they need
  570. to be handled extra.
  571. A proper solution would be to change refoptions to a set and store the information
  572. if the symbol is absolute or relative there.
  573. }
  574. if (assigned(ref.symbol) and
  575. not(is_pc(ref.base)) and
  576. not(is_pc(ref.index))
  577. ) or
  578. { [#xxx] isn't a valid address operand }
  579. ((ref.base=NR_NO) and (ref.index=NR_NO)) or
  580. (ref.offset<-4095) or
  581. (ref.offset>4095) or
  582. ((oppostfix in [PF_SB,PF_H,PF_SH]) and
  583. ((ref.offset<-255) or
  584. (ref.offset>255)
  585. )
  586. ) or
  587. ((op in [A_LDF,A_STF]) and
  588. ((ref.offset<-1020) or
  589. (ref.offset>1020) or
  590. { the usual pc relative symbol handling assumes possible offsets of +/- 4095 }
  591. assigned(ref.symbol)
  592. )
  593. ) then
  594. begin
  595. reference_reset(tmpref);
  596. { load symbol }
  597. tmpreg:=getintregister(list,OS_INT);
  598. if assigned(ref.symbol) then
  599. begin
  600. current_asmdata.getjumplabel(l);
  601. cg.a_label(current_procinfo.aktlocaldata,l);
  602. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  603. current_procinfo.aktlocaldata.concat(tai_const.create_sym_offset(ref.symbol,ref.offset));
  604. { load consts entry }
  605. tmpref.symbol:=l;
  606. tmpref.base:=NR_R15;
  607. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  608. end
  609. else
  610. a_load_const_reg(list,OS_ADDR,ref.offset,tmpreg);
  611. if (ref.base<>NR_NO) then
  612. begin
  613. if ref.index<>NR_NO then
  614. begin
  615. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  616. ref.base:=tmpreg;
  617. end
  618. else
  619. begin
  620. ref.index:=tmpreg;
  621. ref.shiftimm:=0;
  622. ref.signindex:=1;
  623. ref.shiftmode:=SM_None;
  624. end;
  625. end
  626. else
  627. ref.base:=tmpreg;
  628. ref.offset:=0;
  629. ref.symbol:=nil;
  630. end;
  631. if (ref.base<>NR_NO) and (ref.index<>NR_NO) and (ref.offset<>0) then
  632. begin
  633. if tmpreg<>NR_NO then
  634. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,tmpreg,tmpreg)
  635. else
  636. begin
  637. tmpreg:=getintregister(list,OS_ADDR);
  638. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,ref.base,tmpreg);
  639. ref.base:=tmpreg;
  640. end;
  641. ref.offset:=0;
  642. end;
  643. { floating point operations have only limited references
  644. we expect here, that a base is already set }
  645. if (op in [A_LDF,A_STF]) and (ref.index<>NR_NO) then
  646. begin
  647. if ref.shiftmode<>SM_none then
  648. internalerror(200309121);
  649. if tmpreg<>NR_NO then
  650. begin
  651. if ref.base=tmpreg then
  652. begin
  653. if ref.signindex<0 then
  654. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg,tmpreg,ref.index))
  655. else
  656. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,tmpreg,ref.index));
  657. ref.index:=NR_NO;
  658. end
  659. else
  660. begin
  661. if ref.index<>tmpreg then
  662. internalerror(200403161);
  663. if ref.signindex<0 then
  664. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg,ref.base,tmpreg))
  665. else
  666. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  667. ref.base:=tmpreg;
  668. ref.index:=NR_NO;
  669. end;
  670. end
  671. else
  672. begin
  673. tmpreg:=getintregister(list,OS_ADDR);
  674. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,ref.index));
  675. ref.base:=tmpreg;
  676. ref.index:=NR_NO;
  677. end;
  678. end;
  679. list.concat(setoppostfix(taicpu.op_reg_ref(op,reg,ref),oppostfix));
  680. Result := ref;
  681. end;
  682. procedure tcgarm.a_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);
  683. var
  684. oppostfix:toppostfix;
  685. usedtmpref,usedtmpref2: treference;
  686. tmpreg,tmpreg2 : tregister;
  687. so : tshifterop;
  688. begin
  689. case ToSize of
  690. { signed integer registers }
  691. OS_8,
  692. OS_S8:
  693. oppostfix:=PF_B;
  694. OS_16,
  695. OS_S16:
  696. oppostfix:=PF_H;
  697. OS_32,
  698. OS_S32:
  699. oppostfix:=PF_None;
  700. else
  701. InternalError(200308295);
  702. end;
  703. if ref.alignment<>0 then
  704. begin
  705. case FromSize of
  706. OS_16,OS_S16:
  707. begin
  708. shifterop_reset(so);so.shiftmode:=SM_LSR;so.shiftimm:=8;
  709. tmpreg:=getintregister(list,OS_INT);
  710. usedtmpref:=a_internal_load_reg_ref(list,OS_8,OS_8,reg,Ref);
  711. inc(usedtmpref.offset);
  712. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,tmpreg,reg,so));
  713. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  714. end;
  715. OS_32,OS_S32:
  716. begin
  717. shifterop_reset(so);so.shiftmode:=SM_LSR;so.shiftimm:=8;
  718. tmpreg:=getintregister(list,OS_INT);
  719. usedtmpref:=a_internal_load_reg_ref(list,OS_8,OS_8,reg,Ref);
  720. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,tmpreg,reg,so));
  721. inc(usedtmpref.offset);
  722. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  723. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,tmpreg,tmpreg,so));
  724. inc(usedtmpref.offset);
  725. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  726. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,tmpreg,tmpreg,so));
  727. inc(usedtmpref.offset);
  728. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  729. end
  730. else
  731. handle_load_store(list,A_STR,oppostfix,reg,ref);
  732. end;
  733. end
  734. else
  735. handle_load_store(list,A_STR,oppostfix,reg,ref);
  736. end;
  737. procedure tcgarm.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  738. var
  739. oppostfix:toppostfix;
  740. usedtmpref,usedtmpref2: treference;
  741. tmpreg,tmpreg2,tmpreg3 : tregister;
  742. so : tshifterop;
  743. begin
  744. case FromSize of
  745. { signed integer registers }
  746. OS_8:
  747. oppostfix:=PF_B;
  748. OS_S8:
  749. oppostfix:=PF_SB;
  750. OS_16:
  751. oppostfix:=PF_H;
  752. OS_S16:
  753. oppostfix:=PF_SH;
  754. OS_32,
  755. OS_S32:
  756. oppostfix:=PF_None;
  757. else
  758. InternalError(200308291);
  759. end;
  760. if Ref.alignment<>0 then
  761. begin
  762. case FromSize of
  763. OS_16,OS_S16:
  764. begin
  765. a_loadaddr_ref_reg(list,ref,reg);
  766. reference_reset_base(usedtmpref,reg,0);
  767. shifterop_reset(so);so.shiftmode:=SM_LSL;so.shiftimm:=8;
  768. tmpreg:=getintregister(list,OS_INT);
  769. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  770. inc(usedtmpref.offset);
  771. tmpreg2:=getintregister(list,OS_INT);
  772. if FromSize=OS_16 then
  773. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg2)
  774. else
  775. a_internal_load_ref_reg(list,OS_S8,OS_S8,usedtmpref,tmpreg2);
  776. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,tmpreg,tmpreg2,so));
  777. end;
  778. OS_32,OS_S32:
  779. begin
  780. tmpreg:=getintregister(list,OS_INT);
  781. tmpreg2:=getintregister(list,OS_INT);
  782. tmpreg3:=getintregister(list,OS_INT);
  783. shifterop_reset(so);so.shiftmode:=SM_LSL;so.shiftimm:=8;
  784. a_loadaddr_ref_reg(list,ref,tmpreg3);
  785. reference_reset_base(usedtmpref,tmpreg3,0);
  786. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  787. inc(usedtmpref.offset);
  788. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  789. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,tmpreg2,reg,tmpreg,so));
  790. inc(usedtmpref.offset);
  791. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  792. so.shiftimm:=16;
  793. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,tmpreg,tmpreg2,reg,so));
  794. inc(usedtmpref.offset);
  795. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg2);
  796. so.shiftimm:=24;
  797. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,tmpreg,tmpreg2,so));
  798. end
  799. else
  800. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  801. end;
  802. end
  803. else
  804. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  805. end;
  806. function tcgarm.a_internal_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference):treference;
  807. var
  808. oppostfix:toppostfix;
  809. begin
  810. case ToSize of
  811. { signed integer registers }
  812. OS_8,
  813. OS_S8:
  814. oppostfix:=PF_B;
  815. OS_16,
  816. OS_S16:
  817. oppostfix:=PF_H;
  818. OS_32,
  819. OS_S32:
  820. oppostfix:=PF_None;
  821. else
  822. InternalError(2003082910);
  823. end;
  824. result:=handle_load_store(list,A_STR,oppostfix,reg,ref);
  825. end;
  826. function tcgarm.a_internal_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister):treference;
  827. var
  828. oppostfix:toppostfix;
  829. begin
  830. case FromSize of
  831. { signed integer registers }
  832. OS_8:
  833. oppostfix:=PF_B;
  834. OS_S8:
  835. oppostfix:=PF_SB;
  836. OS_16:
  837. oppostfix:=PF_H;
  838. OS_S16:
  839. oppostfix:=PF_SH;
  840. OS_32,
  841. OS_S32:
  842. oppostfix:=PF_None;
  843. else
  844. InternalError(200308291);
  845. end;
  846. result:=handle_load_store(list,A_LDR,oppostfix,reg,ref);
  847. end;
  848. procedure tcgarm.a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  849. var
  850. instr: taicpu;
  851. so : tshifterop;
  852. begin
  853. shifterop_reset(so);
  854. if (tcgsize2size[tosize] < tcgsize2size[fromsize]) or
  855. (
  856. (tcgsize2size[tosize] = tcgsize2size[fromsize]) and
  857. (tosize <> fromsize) and
  858. not(fromsize in [OS_32,OS_S32])
  859. ) then
  860. begin
  861. case tosize of
  862. OS_8:
  863. list.concat(taicpu.op_reg_reg_const(A_AND,
  864. reg2,reg1,$ff));
  865. OS_S8:
  866. begin
  867. so.shiftmode:=SM_LSL;
  868. so.shiftimm:=24;
  869. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,reg2,reg1,so));
  870. so.shiftmode:=SM_ASR;
  871. so.shiftimm:=24;
  872. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,reg2,reg2,so));
  873. end;
  874. OS_16:
  875. begin
  876. so.shiftmode:=SM_LSL;
  877. so.shiftimm:=16;
  878. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,reg2,reg1,so));
  879. so.shiftmode:=SM_LSR;
  880. so.shiftimm:=16;
  881. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,reg2,reg2,so));
  882. end;
  883. OS_S16:
  884. begin
  885. so.shiftmode:=SM_LSL;
  886. so.shiftimm:=16;
  887. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,reg2,reg1,so));
  888. so.shiftmode:=SM_ASR;
  889. so.shiftimm:=16;
  890. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,reg2,reg2,so));
  891. end;
  892. OS_32,OS_S32:
  893. begin
  894. instr:=taicpu.op_reg_reg(A_MOV,reg2,reg1);
  895. list.concat(instr);
  896. add_move_instruction(instr);
  897. end;
  898. else internalerror(2002090901);
  899. end;
  900. end
  901. else
  902. begin
  903. if reg1<>reg2 then
  904. begin
  905. { same size, only a register mov required }
  906. instr:=taicpu.op_reg_reg(A_MOV,reg2,reg1);
  907. list.Concat(instr);
  908. { Notify the register allocator that we have written a move instruction so
  909. it can try to eliminate it. }
  910. add_move_instruction(instr);
  911. end;
  912. end;
  913. end;
  914. procedure tcgarm.a_paramfpu_ref(list : TAsmList;size : tcgsize;const ref : treference;const paraloc : TCGPara);
  915. var
  916. href,href2 : treference;
  917. hloc : pcgparalocation;
  918. begin
  919. href:=ref;
  920. hloc:=paraloc.location;
  921. while assigned(hloc) do
  922. begin
  923. case hloc^.loc of
  924. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  925. a_loadfpu_ref_reg(list,size,ref,hloc^.register);
  926. LOC_REGISTER :
  927. a_load_ref_reg(list,hloc^.size,hloc^.size,href,hloc^.register);
  928. LOC_REFERENCE :
  929. begin
  930. reference_reset_base(href2,hloc^.reference.index,hloc^.reference.offset);
  931. a_load_ref_ref(list,hloc^.size,hloc^.size,href,href2);
  932. end;
  933. else
  934. internalerror(200408241);
  935. end;
  936. inc(href.offset,tcgsize2size[hloc^.size]);
  937. hloc:=hloc^.next;
  938. end;
  939. end;
  940. procedure tcgarm.a_loadfpu_reg_reg(list: TAsmList; size: tcgsize; reg1, reg2: tregister);
  941. begin
  942. list.concat(setoppostfix(taicpu.op_reg_reg(A_MVF,reg2,reg1),cgsize2fpuoppostfix[size]));
  943. end;
  944. procedure tcgarm.a_loadfpu_ref_reg(list: TAsmList; size: tcgsize; const ref: treference; reg: tregister);
  945. var
  946. oppostfix:toppostfix;
  947. begin
  948. case size of
  949. OS_F32:
  950. oppostfix:=PF_S;
  951. OS_F64:
  952. oppostfix:=PF_D;
  953. OS_F80:
  954. oppostfix:=PF_E;
  955. else
  956. InternalError(200309021);
  957. end;
  958. handle_load_store(list,A_LDF,oppostfix,reg,ref);
  959. end;
  960. procedure tcgarm.a_loadfpu_reg_ref(list: TAsmList; size: tcgsize; reg: tregister; const ref: treference);
  961. var
  962. oppostfix:toppostfix;
  963. begin
  964. case size of
  965. OS_F32:
  966. oppostfix:=PF_S;
  967. OS_F64:
  968. oppostfix:=PF_D;
  969. OS_F80:
  970. oppostfix:=PF_E;
  971. else
  972. InternalError(200309022);
  973. end;
  974. handle_load_store(list,A_STF,oppostfix,reg,ref);
  975. end;
  976. { comparison operations }
  977. procedure tcgarm.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  978. l : tasmlabel);
  979. var
  980. tmpreg : tregister;
  981. b : byte;
  982. begin
  983. if is_shifter_const(a,b) then
  984. list.concat(taicpu.op_reg_const(A_CMP,reg,a))
  985. { CMN reg,0 and CMN reg,$80000000 are different from CMP reg,$ffffffff
  986. and CMP reg,$7fffffff regarding the flags according to the ARM manual }
  987. else if (a<>$7fffffff) and (a<>-1) and is_shifter_const(-a,b) then
  988. list.concat(taicpu.op_reg_const(A_CMN,reg,-a))
  989. else
  990. begin
  991. tmpreg:=getintregister(list,size);
  992. a_load_const_reg(list,size,a,tmpreg);
  993. list.concat(taicpu.op_reg_reg(A_CMP,reg,tmpreg));
  994. end;
  995. a_jmp_cond(list,cmp_op,l);
  996. end;
  997. procedure tcgarm.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  998. begin
  999. list.concat(taicpu.op_reg_reg(A_CMP,reg2,reg1));
  1000. a_jmp_cond(list,cmp_op,l);
  1001. end;
  1002. procedure tcgarm.a_jmp_name(list : TAsmList;const s : string);
  1003. begin
  1004. list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(s)));
  1005. end;
  1006. procedure tcgarm.a_jmp_always(list : TAsmList;l: tasmlabel);
  1007. begin
  1008. list.concat(taicpu.op_sym(A_B,l));
  1009. end;
  1010. procedure tcgarm.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1011. var
  1012. ai : taicpu;
  1013. begin
  1014. ai:=setcondition(taicpu.op_sym(A_B,l),flags_to_cond(f));
  1015. ai.is_jmp:=true;
  1016. list.concat(ai);
  1017. end;
  1018. procedure tcgarm.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  1019. var
  1020. ai : taicpu;
  1021. begin
  1022. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,1),flags_to_cond(f)));
  1023. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,0),inverse_cond(flags_to_cond(f))));
  1024. end;
  1025. procedure tcgarm.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  1026. var
  1027. ref,href : treference;
  1028. shift : byte;
  1029. firstfloatreg,lastfloatreg,
  1030. r : byte;
  1031. i : aint;
  1032. again : tasmlabel;
  1033. begin
  1034. LocalSize:=align(LocalSize,4);
  1035. if not(nostackframe) then
  1036. begin
  1037. firstfloatreg:=RS_NO;
  1038. { save floating point registers? }
  1039. for r:=RS_F0 to RS_F7 do
  1040. if r in rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall) then
  1041. begin
  1042. if firstfloatreg=RS_NO then
  1043. firstfloatreg:=r;
  1044. lastfloatreg:=r;
  1045. end;
  1046. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1047. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  1048. a_reg_alloc(list,NR_R12);
  1049. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_STACK_POINTER_REG));
  1050. { save int registers }
  1051. reference_reset(ref);
  1052. ref.index:=NR_STACK_POINTER_REG;
  1053. ref.addressmode:=AM_PREINDEXED;
  1054. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,
  1055. rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall)+[RS_R11,RS_R12,RS_R14,RS_R15]),
  1056. PF_FD));
  1057. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_FRAME_POINTER_REG,NR_R12,4));
  1058. { allocate necessary stack size }
  1059. { don't use a_op_const_reg_reg here because we don't allow register allocations
  1060. in the entry/exit code }
  1061. if (target_info.system in [system_arm_wince]) and
  1062. (localsize>=winstackpagesize) then
  1063. begin
  1064. if localsize div winstackpagesize<=5 then
  1065. begin
  1066. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  1067. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  1068. if is_shifter_const(localsize,shift) then
  1069. list.concat(Taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,localsize))
  1070. else
  1071. begin
  1072. a_load_const_reg(list,OS_ADDR,localsize,NR_R12);
  1073. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  1074. end;
  1075. for i:=1 to localsize div winstackpagesize do
  1076. begin
  1077. if localsize-i*winstackpagesize<4096 then
  1078. reference_reset_base(href,NR_STACK_POINTER_REG,localsize-i*winstackpagesize)
  1079. else
  1080. begin
  1081. a_load_const_reg(list,OS_ADDR,localsize-i*winstackpagesize,NR_R12);
  1082. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1083. href.index:=NR_R12;
  1084. end;
  1085. list.concat(Taicpu.op_reg_ref(A_STR,NR_R0,href));
  1086. end;
  1087. a_reg_dealloc(list,NR_R12);
  1088. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1089. { the data stored doesn't matter }
  1090. list.concat(Taicpu.op_reg_ref(A_STR,NR_R0,href));
  1091. end
  1092. else
  1093. begin
  1094. current_asmdata.getjumplabel(again);
  1095. list.concat(Taicpu.op_reg_const(A_MOV,NR_R12,localsize div winstackpagesize));
  1096. a_label(list,again);
  1097. { always shifterop }
  1098. list.concat(Taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,winstackpagesize));
  1099. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1100. { the data stored doesn't matter }
  1101. list.concat(Taicpu.op_reg_ref(A_STR,NR_R0,href));
  1102. list.concat(Taicpu.op_reg_reg_const(A_SUB,NR_R12,NR_R12,1));
  1103. a_jmp_cond(list,OC_NE,again);
  1104. if is_shifter_const(localsize mod winstackpagesize,shift) then
  1105. list.concat(Taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,localsize mod winstackpagesize))
  1106. else
  1107. begin
  1108. a_load_const_reg(list,OS_ADDR,localsize mod winstackpagesize,NR_R12);
  1109. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  1110. end;
  1111. a_reg_dealloc(list,NR_R12);
  1112. end
  1113. end
  1114. else if not(is_shifter_const(localsize,shift)) then
  1115. begin
  1116. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  1117. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  1118. a_reg_dealloc(list,NR_R12);
  1119. end
  1120. else
  1121. begin
  1122. a_reg_dealloc(list,NR_R12);
  1123. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  1124. end;
  1125. if firstfloatreg<>RS_NO then
  1126. begin
  1127. reference_reset(ref);
  1128. if tarmprocinfo(current_procinfo).floatregstart<=-1023 then
  1129. begin
  1130. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  1131. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,NR_FRAME_POINTER_REG,NR_R12));
  1132. ref.base:=NR_R12;
  1133. end
  1134. else
  1135. begin
  1136. ref.base:=NR_FRAME_POINTER_REG;
  1137. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  1138. end;
  1139. list.concat(taicpu.op_reg_const_ref(A_SFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  1140. lastfloatreg-firstfloatreg+1,ref));
  1141. end;
  1142. end;
  1143. end;
  1144. procedure tcgarm.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  1145. var
  1146. ref : treference;
  1147. firstfloatreg,lastfloatreg,
  1148. r : byte;
  1149. shift : byte;
  1150. begin
  1151. if not(nostackframe) then
  1152. begin
  1153. { restore floating point register }
  1154. firstfloatreg:=RS_NO;
  1155. { save floating point registers? }
  1156. for r:=RS_F0 to RS_F7 do
  1157. if r in rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall) then
  1158. begin
  1159. if firstfloatreg=RS_NO then
  1160. firstfloatreg:=r;
  1161. lastfloatreg:=r;
  1162. end;
  1163. if firstfloatreg<>RS_NO then
  1164. begin
  1165. reference_reset(ref);
  1166. if tarmprocinfo(current_procinfo).floatregstart<=-1023 then
  1167. begin
  1168. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  1169. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,NR_FRAME_POINTER_REG,NR_R12));
  1170. ref.base:=NR_R12;
  1171. end
  1172. else
  1173. begin
  1174. ref.base:=NR_FRAME_POINTER_REG;
  1175. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  1176. end;
  1177. list.concat(taicpu.op_reg_const_ref(A_LFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  1178. lastfloatreg-firstfloatreg+1,ref));
  1179. end;
  1180. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  1181. list.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14))
  1182. else
  1183. begin
  1184. { restore int registers and return }
  1185. reference_reset(ref);
  1186. ref.index:=NR_FRAME_POINTER_REG;
  1187. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall)+[RS_R11,RS_R13,RS_R15]),PF_EA));
  1188. end;
  1189. end
  1190. else
  1191. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14));
  1192. end;
  1193. procedure tcgarm.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  1194. var
  1195. b : byte;
  1196. tmpref : treference;
  1197. instr : taicpu;
  1198. begin
  1199. if ref.addressmode<>AM_OFFSET then
  1200. internalerror(200309071);
  1201. tmpref:=ref;
  1202. { Be sure to have a base register }
  1203. if (tmpref.base=NR_NO) then
  1204. begin
  1205. if tmpref.shiftmode<>SM_None then
  1206. internalerror(200308294);
  1207. if tmpref.signindex<0 then
  1208. internalerror(200312023);
  1209. tmpref.base:=tmpref.index;
  1210. tmpref.index:=NR_NO;
  1211. end;
  1212. if assigned(tmpref.symbol) or
  1213. not((is_shifter_const(tmpref.offset,b)) or
  1214. (is_shifter_const(-tmpref.offset,b))
  1215. ) then
  1216. fixref(list,tmpref);
  1217. { expect a base here if there is an index }
  1218. if (tmpref.base=NR_NO) and (tmpref.index<>NR_NO) then
  1219. internalerror(200312022);
  1220. if tmpref.index<>NR_NO then
  1221. begin
  1222. if tmpref.shiftmode<>SM_None then
  1223. internalerror(200312021);
  1224. if tmpref.signindex<0 then
  1225. a_op_reg_reg_reg(list,OP_SUB,OS_ADDR,tmpref.base,tmpref.index,r)
  1226. else
  1227. a_op_reg_reg_reg(list,OP_ADD,OS_ADDR,tmpref.base,tmpref.index,r);
  1228. if tmpref.offset<>0 then
  1229. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,tmpref.offset,r,r);
  1230. end
  1231. else
  1232. begin
  1233. if tmpref.offset<>0 then
  1234. begin
  1235. if tmpref.base<>NR_NO then
  1236. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,tmpref.offset,tmpref.base,r)
  1237. else
  1238. a_load_const_reg(list,OS_ADDR,tmpref.offset,r);
  1239. end
  1240. else
  1241. begin
  1242. instr:=taicpu.op_reg_reg(A_MOV,r,tmpref.base);
  1243. list.concat(instr);
  1244. add_move_instruction(instr);
  1245. end;
  1246. end;
  1247. end;
  1248. procedure tcgarm.fixref(list : TAsmList;var ref : treference);
  1249. var
  1250. tmpreg : tregister;
  1251. tmpref : treference;
  1252. l : tasmlabel;
  1253. begin
  1254. { absolute symbols can't be handled directly, we've to store the symbol reference
  1255. in the text segment and access it pc relative
  1256. For now, we assume that references where base or index equals to PC are already
  1257. relative, all other references are assumed to be absolute and thus they need
  1258. to be handled extra.
  1259. A proper solution would be to change refoptions to a set and store the information
  1260. if the symbol is absolute or relative there.
  1261. }
  1262. { create consts entry }
  1263. reference_reset(tmpref);
  1264. current_asmdata.getjumplabel(l);
  1265. cg.a_label(current_procinfo.aktlocaldata,l);
  1266. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  1267. if assigned(ref.symbol) then
  1268. current_procinfo.aktlocaldata.concat(tai_const.create_sym_offset(ref.symbol,ref.offset))
  1269. else
  1270. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(ref.offset));
  1271. { load consts entry }
  1272. tmpreg:=getintregister(list,OS_INT);
  1273. tmpref.symbol:=l;
  1274. tmpref.base:=NR_PC;
  1275. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  1276. if (ref.base<>NR_NO) then
  1277. begin
  1278. if ref.index<>NR_NO then
  1279. begin
  1280. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  1281. ref.base:=tmpreg;
  1282. end
  1283. else
  1284. begin
  1285. ref.index:=tmpreg;
  1286. ref.shiftimm:=0;
  1287. ref.signindex:=1;
  1288. ref.shiftmode:=SM_None;
  1289. end;
  1290. end
  1291. else
  1292. ref.base:=tmpreg;
  1293. ref.offset:=0;
  1294. ref.symbol:=nil;
  1295. end;
  1296. procedure tcgarm.g_concatcopy_move(list : TAsmList;const source,dest : treference;len : aint);
  1297. var
  1298. paraloc1,paraloc2,paraloc3 : TCGPara;
  1299. begin
  1300. paraloc1.init;
  1301. paraloc2.init;
  1302. paraloc3.init;
  1303. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1304. paramanager.getintparaloc(pocall_default,2,paraloc2);
  1305. paramanager.getintparaloc(pocall_default,3,paraloc3);
  1306. paramanager.allocparaloc(list,paraloc3);
  1307. a_param_const(list,OS_INT,len,paraloc3);
  1308. paramanager.allocparaloc(list,paraloc2);
  1309. a_paramaddr_ref(list,dest,paraloc2);
  1310. paramanager.allocparaloc(list,paraloc2);
  1311. a_paramaddr_ref(list,source,paraloc1);
  1312. paramanager.freeparaloc(list,paraloc3);
  1313. paramanager.freeparaloc(list,paraloc2);
  1314. paramanager.freeparaloc(list,paraloc1);
  1315. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1316. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1317. a_call_name(list,'FPC_MOVE');
  1318. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1319. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1320. paraloc3.done;
  1321. paraloc2.done;
  1322. paraloc1.done;
  1323. end;
  1324. procedure tcgarm.g_concatcopy_internal(list : TAsmList;const source,dest : treference;len : aint;aligned : boolean);
  1325. const
  1326. maxtmpreg=10;{roozbeh: can be reduced to 8 or lower if might conflick with reserved ones,also +2 is used becouse of regs required for referencing}
  1327. var
  1328. srcref,dstref,usedtmpref,usedtmpref2:treference;
  1329. srcreg,destreg,countreg,r,tmpreg,tmpreg2:tregister;
  1330. helpsize:aword;
  1331. copysize:byte;
  1332. cgsize:Tcgsize;
  1333. so:tshifterop;
  1334. tmpregisters:array[1..maxtmpreg]of tregister;
  1335. tmpregi,tmpregi2:byte;
  1336. { will never be called with count<=4 }
  1337. procedure genloop(count : aword;size : byte);
  1338. const
  1339. size2opsize : array[1..4] of tcgsize = (OS_8,OS_16,OS_NO,OS_32);
  1340. var
  1341. l : tasmlabel;
  1342. begin
  1343. current_asmdata.getjumplabel(l);
  1344. if count<size then size:=1;
  1345. a_load_const_reg(list,OS_INT,count div size,countreg);
  1346. cg.a_label(list,l);
  1347. srcref.addressmode:=AM_POSTINDEXED;
  1348. dstref.addressmode:=AM_POSTINDEXED;
  1349. srcref.offset:=size;
  1350. dstref.offset:=size;
  1351. r:=getintregister(list,size2opsize[size]);
  1352. a_load_ref_reg(list,size2opsize[size],size2opsize[size],srcref,r);
  1353. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,countreg,countreg,1),PF_S));
  1354. a_load_reg_ref(list,size2opsize[size],size2opsize[size],r,dstref);
  1355. list.concat(setcondition(taicpu.op_sym(A_B,l),C_NE));
  1356. srcref.offset:=1;
  1357. dstref.offset:=1;
  1358. case count mod size of
  1359. 1:
  1360. begin
  1361. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1362. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1363. end;
  1364. 2:
  1365. if aligned then
  1366. begin
  1367. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  1368. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  1369. end
  1370. else
  1371. begin
  1372. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1373. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1374. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1375. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1376. end;
  1377. 3:
  1378. if aligned then
  1379. begin
  1380. srcref.offset:=2;
  1381. dstref.offset:=2;
  1382. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  1383. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  1384. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1385. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1386. end
  1387. else
  1388. begin
  1389. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1390. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1391. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1392. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1393. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1394. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1395. end;
  1396. end;
  1397. { keep the registers alive }
  1398. list.concat(taicpu.op_reg_reg(A_MOV,countreg,countreg));
  1399. list.concat(taicpu.op_reg_reg(A_MOV,srcreg,srcreg));
  1400. list.concat(taicpu.op_reg_reg(A_MOV,destreg,destreg));
  1401. end;
  1402. begin
  1403. if len=0 then
  1404. exit;
  1405. helpsize:=12+maxtmpreg*4;//52 with maxtmpreg=10
  1406. dstref:=dest;
  1407. srcref:=source;
  1408. if cs_opt_size in aktoptimizerswitches then
  1409. helpsize:=8;
  1410. if (len<=helpsize) and aligned then
  1411. begin
  1412. tmpregi:=0;
  1413. srcreg:=getintregister(list,OS_ADDR);
  1414. { explicit pc relative addressing, could be
  1415. e.g. a floating point constant }
  1416. if source.base=NR_PC then
  1417. begin
  1418. { ... then we don't need a loadaddr }
  1419. srcref:=source;
  1420. end
  1421. else
  1422. begin
  1423. a_loadaddr_ref_reg(list,source,srcreg);
  1424. reference_reset_base(srcref,srcreg,0);
  1425. end;
  1426. while (len div 4 <> 0) and (tmpregi<=maxtmpreg) do
  1427. begin
  1428. inc(tmpregi);
  1429. tmpregisters[tmpregi]:=getintregister(list,OS_32);
  1430. a_load_ref_reg(list,OS_32,OS_32,srcref,tmpregisters[tmpregi]);
  1431. inc(srcref.offset,4);
  1432. dec(len,4);
  1433. end;
  1434. destreg:=getintregister(list,OS_ADDR);
  1435. a_loadaddr_ref_reg(list,dest,destreg);
  1436. reference_reset_base(dstref,destreg,0);
  1437. tmpregi2:=1;
  1438. while (tmpregi2<=tmpregi) do
  1439. begin
  1440. a_load_reg_ref(list,OS_32,OS_32,tmpregisters[tmpregi2],dstref);
  1441. inc(dstref.offset,4);
  1442. inc(tmpregi2);
  1443. end;
  1444. copysize:=4;
  1445. cgsize:=OS_32;
  1446. while len<>0 do
  1447. begin
  1448. if len<2 then
  1449. begin
  1450. copysize:=1;
  1451. cgsize:=OS_8;
  1452. end
  1453. else if len<4 then
  1454. begin
  1455. copysize:=2;
  1456. cgsize:=OS_16;
  1457. end;
  1458. dec(len,copysize);
  1459. r:=getintregister(list,cgsize);
  1460. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  1461. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  1462. inc(srcref.offset,copysize);
  1463. inc(dstref.offset,copysize);
  1464. end;{end of while}
  1465. end
  1466. else
  1467. begin
  1468. cgsize:=OS_32;
  1469. if (len<=4) then{len<=4 and not aligned}
  1470. begin
  1471. r:=getintregister(list,cgsize);
  1472. usedtmpref:=a_internal_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1473. if Len=1 then
  1474. a_load_reg_ref(list,OS_8,OS_8,r,dstref)
  1475. else
  1476. begin
  1477. tmpreg:=getintregister(list,cgsize);
  1478. usedtmpref2:=a_internal_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1479. inc(usedtmpref.offset,1);
  1480. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  1481. inc(usedtmpref2.offset,1);
  1482. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  1483. if len>2 then
  1484. begin
  1485. inc(usedtmpref.offset,1);
  1486. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  1487. inc(usedtmpref2.offset,1);
  1488. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  1489. if len>3 then
  1490. begin
  1491. inc(usedtmpref.offset,1);
  1492. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  1493. inc(usedtmpref2.offset,1);
  1494. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  1495. end;
  1496. end;
  1497. end;
  1498. end{end of if len<=4}
  1499. else
  1500. begin{unaligned & 4<len<helpsize **or** aligned/unaligned & len>helpsize}
  1501. destreg:=getintregister(list,OS_ADDR);
  1502. a_loadaddr_ref_reg(list,dest,destreg);
  1503. reference_reset_base(dstref,destreg,0);
  1504. srcreg:=getintregister(list,OS_ADDR);
  1505. a_loadaddr_ref_reg(list,source,srcreg);
  1506. reference_reset_base(srcref,srcreg,0);
  1507. countreg:=getintregister(list,OS_32);
  1508. // if cs_opt_size in aktoptimizerswitches then
  1509. { roozbeh : it seems loading 1 byte is faster becouse of caching/fetching(?) }
  1510. {if aligned then
  1511. genloop(len,4)
  1512. else}
  1513. genloop(len,1);
  1514. end;
  1515. end;
  1516. end;
  1517. procedure tcgarm.g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : aint);
  1518. begin
  1519. g_concatcopy_internal(list,source,dest,len,false);
  1520. end;
  1521. procedure tcgarm.g_concatcopy(list : TAsmList;const source,dest : treference;len : aint);
  1522. begin
  1523. if (source.alignment in [1..3]) or
  1524. (dest.alignment in [1..3]) then
  1525. g_concatcopy_internal(list,source,dest,len,false)
  1526. else
  1527. g_concatcopy_internal(list,source,dest,len,true);
  1528. end;
  1529. procedure tcgarm.g_overflowCheck(list : TAsmList;const l : tlocation;def : tdef);
  1530. var
  1531. ovloc : tlocation;
  1532. begin
  1533. ovloc.loc:=LOC_VOID;
  1534. g_overflowCheck_loc(list,l,def,ovloc);
  1535. end;
  1536. procedure tcgarm.g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);
  1537. var
  1538. hl : tasmlabel;
  1539. ai:TAiCpu;
  1540. hflags : tresflags;
  1541. begin
  1542. if not(cs_check_overflow in aktlocalswitches) then
  1543. exit;
  1544. current_asmdata.getjumplabel(hl);
  1545. case ovloc.loc of
  1546. LOC_VOID:
  1547. begin
  1548. ai:=taicpu.op_sym(A_B,hl);
  1549. ai.is_jmp:=true;
  1550. if not((def.deftype=pointerdef) or
  1551. ((def.deftype=orddef) and
  1552. (torddef(def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,bool8bit,bool16bit,bool32bit]))) then
  1553. ai.SetCondition(C_VC)
  1554. else
  1555. if TAiCpu(List.Last).opcode in [A_RSB,A_RSC,A_SBC,A_SUB] then
  1556. ai.SetCondition(C_CS)
  1557. else
  1558. ai.SetCondition(C_CC);
  1559. list.concat(ai);
  1560. end;
  1561. LOC_FLAGS:
  1562. begin
  1563. hflags:=ovloc.resflags;
  1564. inverse_flags(hflags);
  1565. cg.a_jmp_flags(list,hflags,hl);
  1566. end;
  1567. else
  1568. internalerror(200409281);
  1569. end;
  1570. a_call_name(list,'FPC_OVERFLOW');
  1571. a_label(list,hl);
  1572. end;
  1573. procedure tcgarm.g_save_standard_registers(list : TAsmList);
  1574. begin
  1575. { this work is done in g_proc_entry }
  1576. end;
  1577. procedure tcgarm.g_restore_standard_registers(list : TAsmList);
  1578. begin
  1579. { this work is done in g_proc_exit }
  1580. end;
  1581. procedure tcgarm.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1582. var
  1583. ai : taicpu;
  1584. begin
  1585. ai:=Taicpu.Op_sym(A_B,l);
  1586. ai.SetCondition(OpCmp2AsmCond[cond]);
  1587. ai.is_jmp:=true;
  1588. list.concat(ai);
  1589. end;
  1590. procedure tcgarm.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  1591. procedure loadvmttor12;
  1592. var
  1593. href : treference;
  1594. begin
  1595. reference_reset_base(href,NR_R0,0);
  1596. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
  1597. end;
  1598. procedure op_onr12methodaddr;
  1599. var
  1600. href : treference;
  1601. begin
  1602. if (procdef.extnumber=$ffff) then
  1603. Internalerror(200006139);
  1604. { call/jmp vmtoffs(%eax) ; method offs }
  1605. reference_reset_base(href,NR_R12,procdef._class.vmtmethodoffset(procdef.extnumber));
  1606. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
  1607. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R12));
  1608. end;
  1609. var
  1610. lab : tasmsymbol;
  1611. make_global : boolean;
  1612. href : treference;
  1613. begin
  1614. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  1615. Internalerror(200006137);
  1616. if not assigned(procdef._class) or
  1617. (procdef.procoptions*[po_classmethod, po_staticmethod,
  1618. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  1619. Internalerror(200006138);
  1620. if procdef.owner.symtabletype<>objectsymtable then
  1621. Internalerror(200109191);
  1622. make_global:=false;
  1623. if (not current_module.is_unit) or
  1624. (cs_create_smart in aktmoduleswitches) or
  1625. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  1626. make_global:=true;
  1627. if make_global then
  1628. list.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  1629. else
  1630. list.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  1631. { set param1 interface to self }
  1632. g_adjust_self_value(list,procdef,ioffset);
  1633. { case 4 }
  1634. if po_virtualmethod in procdef.procoptions then
  1635. begin
  1636. loadvmttor12;
  1637. op_onr12methodaddr;
  1638. end
  1639. { case 0 }
  1640. else
  1641. list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(procdef.mangledname)));
  1642. list.concat(Tai_symbol_end.Createname(labelname));
  1643. end;
  1644. procedure tcg64farm.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  1645. var
  1646. tmpreg : tregister;
  1647. begin
  1648. case op of
  1649. OP_NEG:
  1650. begin
  1651. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSB,regdst.reglo,regsrc.reglo,0),PF_S));
  1652. list.concat(taicpu.op_reg_reg_const(A_RSC,regdst.reghi,regsrc.reghi,0));
  1653. end;
  1654. OP_NOT:
  1655. begin
  1656. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reglo,regdst.reglo);
  1657. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reghi,regdst.reghi);
  1658. end;
  1659. else
  1660. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  1661. end;
  1662. end;
  1663. procedure tcg64farm.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  1664. begin
  1665. a_op64_const_reg_reg(list,op,size,value,reg,reg);
  1666. end;
  1667. procedure tcg64farm.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);
  1668. var
  1669. ovloc : tlocation;
  1670. begin
  1671. a_op64_const_reg_reg_checkoverflow(list,op,size,value,regsrc,regdst,false,ovloc);
  1672. end;
  1673. procedure tcg64farm.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  1674. var
  1675. ovloc : tlocation;
  1676. begin
  1677. a_op64_reg_reg_reg_checkoverflow(list,op,size,regsrc1,regsrc2,regdst,false,ovloc);
  1678. end;
  1679. procedure tcg64farm.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  1680. var
  1681. tmpreg : tregister;
  1682. b : byte;
  1683. begin
  1684. ovloc.loc:=LOC_VOID;
  1685. case op of
  1686. OP_NEG,
  1687. OP_NOT :
  1688. internalerror(200306017);
  1689. end;
  1690. if (setflags or tcgarm(cg).cgsetflags) and (op in [OP_ADD,OP_SUB]) then
  1691. begin
  1692. case op of
  1693. OP_ADD:
  1694. begin
  1695. if is_shifter_const(lo(value),b) then
  1696. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  1697. else
  1698. begin
  1699. tmpreg:=cg.getintregister(list,OS_32);
  1700. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  1701. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  1702. end;
  1703. if is_shifter_const(hi(value),b) then
  1704. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,hi(value)),PF_S))
  1705. else
  1706. begin
  1707. tmpreg:=cg.getintregister(list,OS_32);
  1708. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  1709. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg),PF_S));
  1710. end;
  1711. end;
  1712. OP_SUB:
  1713. begin
  1714. if is_shifter_const(lo(value),b) then
  1715. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  1716. else
  1717. begin
  1718. tmpreg:=cg.getintregister(list,OS_32);
  1719. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  1720. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  1721. end;
  1722. if is_shifter_const(hi(value),b) then
  1723. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SBC,regdst.reghi,regsrc.reghi,hi(value)),PF_S))
  1724. else
  1725. begin
  1726. tmpreg:=cg.getintregister(list,OS_32);
  1727. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  1728. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg),PF_S));
  1729. end;
  1730. end;
  1731. else
  1732. internalerror(200502131);
  1733. end;
  1734. if size=OS_64 then
  1735. begin
  1736. { the arm has an weired opinion how flags for SUB/ADD are handled }
  1737. ovloc.loc:=LOC_FLAGS;
  1738. case op of
  1739. OP_ADD:
  1740. ovloc.resflags:=F_CS;
  1741. OP_SUB:
  1742. ovloc.resflags:=F_CC;
  1743. end;
  1744. end;
  1745. end
  1746. else
  1747. begin
  1748. case op of
  1749. OP_AND,OP_OR,OP_XOR:
  1750. begin
  1751. cg.a_op_const_reg_reg(list,op,OS_32,lo(value),regsrc.reglo,regdst.reglo);
  1752. cg.a_op_const_reg_reg(list,op,OS_32,hi(value),regsrc.reghi,regdst.reghi);
  1753. end;
  1754. OP_ADD:
  1755. begin
  1756. if is_shifter_const(lo(value),b) then
  1757. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  1758. else
  1759. begin
  1760. tmpreg:=cg.getintregister(list,OS_32);
  1761. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  1762. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  1763. end;
  1764. if is_shifter_const(hi(value),b) then
  1765. list.concat(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,hi(value)))
  1766. else
  1767. begin
  1768. tmpreg:=cg.getintregister(list,OS_32);
  1769. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  1770. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg));
  1771. end;
  1772. end;
  1773. OP_SUB:
  1774. begin
  1775. if is_shifter_const(lo(value),b) then
  1776. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  1777. else
  1778. begin
  1779. tmpreg:=cg.getintregister(list,OS_32);
  1780. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  1781. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  1782. end;
  1783. if is_shifter_const(hi(value),b) then
  1784. list.concat(taicpu.op_reg_reg_const(A_SBC,regdst.reghi,regsrc.reghi,hi(value)))
  1785. else
  1786. begin
  1787. tmpreg:=cg.getintregister(list,OS_32);
  1788. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  1789. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg));
  1790. end;
  1791. end;
  1792. else
  1793. internalerror(2003083101);
  1794. end;
  1795. end;
  1796. end;
  1797. procedure tcg64farm.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  1798. var
  1799. op1,op2:TAsmOp;
  1800. begin
  1801. ovloc.loc:=LOC_VOID;
  1802. case op of
  1803. OP_NEG,
  1804. OP_NOT :
  1805. internalerror(200306017);
  1806. end;
  1807. if (setflags or tcgarm(cg).cgsetflags) and (op in [OP_ADD,OP_SUB]) then
  1808. begin
  1809. case op of
  1810. OP_ADD:
  1811. begin
  1812. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  1813. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi),PF_S));
  1814. end;
  1815. OP_SUB:
  1816. begin
  1817. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  1818. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi),PF_S));
  1819. end;
  1820. else
  1821. internalerror(2003083101);
  1822. end;
  1823. if size=OS_64 then
  1824. begin
  1825. { the arm has an weired opinion how flags for SUB/ADD are handled }
  1826. ovloc.loc:=LOC_FLAGS;
  1827. case op of
  1828. OP_ADD:
  1829. ovloc.resflags:=F_CS;
  1830. OP_SUB:
  1831. ovloc.resflags:=F_CC;
  1832. end;
  1833. end;
  1834. end
  1835. else
  1836. begin
  1837. case op of
  1838. OP_AND,OP_OR,OP_XOR:
  1839. begin
  1840. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  1841. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  1842. end;
  1843. OP_ADD:
  1844. begin
  1845. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  1846. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1847. end;
  1848. OP_SUB:
  1849. begin
  1850. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  1851. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi));
  1852. end;
  1853. else
  1854. internalerror(2003083101);
  1855. end;
  1856. end;
  1857. end;
  1858. begin
  1859. cg:=tcgarm.create;
  1860. cg64:=tcg64farm.create;
  1861. end.