cgcpu.pas 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074
  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(200308294);
  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(200308295);
  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(200308296);
  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(200308297);
  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. case hloc^.size of
  928. OS_F32:
  929. a_load_ref_reg(list,OS_32,OS_32,href,hloc^.register);
  930. OS_64,
  931. OS_F64:
  932. cg64.a_param64_ref(list,href,paraloc);
  933. else
  934. a_load_ref_reg(list,hloc^.size,hloc^.size,href,hloc^.register);
  935. end;
  936. LOC_REFERENCE :
  937. begin
  938. reference_reset_base(href2,hloc^.reference.index,hloc^.reference.offset);
  939. a_load_ref_ref(list,hloc^.size,hloc^.size,href,href2);
  940. end;
  941. else
  942. internalerror(200408241);
  943. end;
  944. inc(href.offset,tcgsize2size[hloc^.size]);
  945. hloc:=hloc^.next;
  946. end;
  947. end;
  948. procedure tcgarm.a_loadfpu_reg_reg(list: TAsmList; size: tcgsize; reg1, reg2: tregister);
  949. begin
  950. list.concat(setoppostfix(taicpu.op_reg_reg(A_MVF,reg2,reg1),cgsize2fpuoppostfix[size]));
  951. end;
  952. procedure tcgarm.a_loadfpu_ref_reg(list: TAsmList; size: tcgsize; const ref: treference; reg: tregister);
  953. var
  954. oppostfix:toppostfix;
  955. begin
  956. case size of
  957. OS_32,
  958. OS_F32:
  959. oppostfix:=PF_S;
  960. OS_64,
  961. OS_F64:
  962. oppostfix:=PF_D;
  963. OS_F80:
  964. oppostfix:=PF_E;
  965. else
  966. InternalError(200309021);
  967. end;
  968. handle_load_store(list,A_LDF,oppostfix,reg,ref);
  969. end;
  970. procedure tcgarm.a_loadfpu_reg_ref(list: TAsmList; size: tcgsize; reg: tregister; const ref: treference);
  971. var
  972. oppostfix:toppostfix;
  973. begin
  974. case size of
  975. OS_F32:
  976. oppostfix:=PF_S;
  977. OS_F64:
  978. oppostfix:=PF_D;
  979. OS_F80:
  980. oppostfix:=PF_E;
  981. else
  982. InternalError(200309022);
  983. end;
  984. handle_load_store(list,A_STF,oppostfix,reg,ref);
  985. end;
  986. { comparison operations }
  987. procedure tcgarm.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  988. l : tasmlabel);
  989. var
  990. tmpreg : tregister;
  991. b : byte;
  992. begin
  993. if is_shifter_const(a,b) then
  994. list.concat(taicpu.op_reg_const(A_CMP,reg,a))
  995. { CMN reg,0 and CMN reg,$80000000 are different from CMP reg,$ffffffff
  996. and CMP reg,$7fffffff regarding the flags according to the ARM manual }
  997. else if (a<>$7fffffff) and (a<>-1) and is_shifter_const(-a,b) then
  998. list.concat(taicpu.op_reg_const(A_CMN,reg,-a))
  999. else
  1000. begin
  1001. tmpreg:=getintregister(list,size);
  1002. a_load_const_reg(list,size,a,tmpreg);
  1003. list.concat(taicpu.op_reg_reg(A_CMP,reg,tmpreg));
  1004. end;
  1005. a_jmp_cond(list,cmp_op,l);
  1006. end;
  1007. procedure tcgarm.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1008. begin
  1009. list.concat(taicpu.op_reg_reg(A_CMP,reg2,reg1));
  1010. a_jmp_cond(list,cmp_op,l);
  1011. end;
  1012. procedure tcgarm.a_jmp_name(list : TAsmList;const s : string);
  1013. begin
  1014. list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(s)));
  1015. end;
  1016. procedure tcgarm.a_jmp_always(list : TAsmList;l: tasmlabel);
  1017. begin
  1018. list.concat(taicpu.op_sym(A_B,l));
  1019. end;
  1020. procedure tcgarm.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1021. var
  1022. ai : taicpu;
  1023. begin
  1024. ai:=setcondition(taicpu.op_sym(A_B,l),flags_to_cond(f));
  1025. ai.is_jmp:=true;
  1026. list.concat(ai);
  1027. end;
  1028. procedure tcgarm.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  1029. var
  1030. ai : taicpu;
  1031. begin
  1032. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,1),flags_to_cond(f)));
  1033. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,0),inverse_cond(flags_to_cond(f))));
  1034. end;
  1035. procedure tcgarm.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  1036. var
  1037. ref,href : treference;
  1038. shift : byte;
  1039. firstfloatreg,lastfloatreg,
  1040. r : byte;
  1041. i : aint;
  1042. again : tasmlabel;
  1043. begin
  1044. LocalSize:=align(LocalSize,4);
  1045. if not(nostackframe) then
  1046. begin
  1047. firstfloatreg:=RS_NO;
  1048. { save floating point registers? }
  1049. for r:=RS_F0 to RS_F7 do
  1050. if r in rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall) then
  1051. begin
  1052. if firstfloatreg=RS_NO then
  1053. firstfloatreg:=r;
  1054. lastfloatreg:=r;
  1055. end;
  1056. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1057. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  1058. a_reg_alloc(list,NR_R12);
  1059. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_STACK_POINTER_REG));
  1060. { save int registers }
  1061. reference_reset(ref);
  1062. ref.index:=NR_STACK_POINTER_REG;
  1063. ref.addressmode:=AM_PREINDEXED;
  1064. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,
  1065. rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall)+[RS_R11,RS_R12,RS_R14,RS_R15]),
  1066. PF_FD));
  1067. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_FRAME_POINTER_REG,NR_R12,4));
  1068. { allocate necessary stack size
  1069. not necessary according to Yury Sidorov
  1070. { don't use a_op_const_reg_reg here because we don't allow register allocations
  1071. in the entry/exit code }
  1072. if (target_info.system in [system_arm_wince]) and
  1073. (localsize>=winstackpagesize) then
  1074. begin
  1075. if localsize div winstackpagesize<=5 then
  1076. begin
  1077. if is_shifter_const(localsize,shift) then
  1078. list.concat(Taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,localsize))
  1079. else
  1080. begin
  1081. a_load_const_reg(list,OS_ADDR,localsize,NR_R12);
  1082. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  1083. end;
  1084. for i:=1 to localsize div winstackpagesize do
  1085. begin
  1086. if localsize-i*winstackpagesize<4096 then
  1087. reference_reset_base(href,NR_STACK_POINTER_REG,-(localsize-i*winstackpagesize))
  1088. else
  1089. begin
  1090. a_load_const_reg(list,OS_ADDR,-(localsize-i*winstackpagesize),NR_R12);
  1091. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1092. href.index:=NR_R12;
  1093. end;
  1094. { the data stored doesn't matter }
  1095. list.concat(Taicpu.op_reg_ref(A_STR,NR_R0,href));
  1096. end;
  1097. a_reg_dealloc(list,NR_R12);
  1098. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1099. { the data stored doesn't matter }
  1100. list.concat(Taicpu.op_reg_ref(A_STR,NR_R0,href));
  1101. end
  1102. else
  1103. begin
  1104. current_asmdata.getjumplabel(again);
  1105. list.concat(Taicpu.op_reg_const(A_MOV,NR_R12,localsize div winstackpagesize));
  1106. a_label(list,again);
  1107. { always shifterop }
  1108. list.concat(Taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,winstackpagesize));
  1109. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1110. { the data stored doesn't matter }
  1111. list.concat(Taicpu.op_reg_ref(A_STR,NR_R0,href));
  1112. list.concat(Taicpu.op_reg_reg_const(A_SUB,NR_R12,NR_R12,1));
  1113. a_jmp_cond(list,OC_NE,again);
  1114. if is_shifter_const(localsize mod winstackpagesize,shift) then
  1115. list.concat(Taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,localsize mod winstackpagesize))
  1116. else
  1117. begin
  1118. a_load_const_reg(list,OS_ADDR,localsize mod winstackpagesize,NR_R12);
  1119. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  1120. end;
  1121. a_reg_dealloc(list,NR_R12);
  1122. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1123. { the data stored doesn't matter }
  1124. list.concat(Taicpu.op_reg_ref(A_STR,NR_R0,href));
  1125. end
  1126. end
  1127. else
  1128. }
  1129. if not(is_shifter_const(localsize,shift)) then
  1130. begin
  1131. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  1132. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  1133. a_reg_dealloc(list,NR_R12);
  1134. end
  1135. else
  1136. begin
  1137. a_reg_dealloc(list,NR_R12);
  1138. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  1139. end;
  1140. if firstfloatreg<>RS_NO then
  1141. begin
  1142. reference_reset(ref);
  1143. if tarmprocinfo(current_procinfo).floatregstart<=-1023 then
  1144. begin
  1145. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  1146. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,NR_FRAME_POINTER_REG,NR_R12));
  1147. ref.base:=NR_R12;
  1148. end
  1149. else
  1150. begin
  1151. ref.base:=NR_FRAME_POINTER_REG;
  1152. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  1153. end;
  1154. list.concat(taicpu.op_reg_const_ref(A_SFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  1155. lastfloatreg-firstfloatreg+1,ref));
  1156. end;
  1157. end;
  1158. end;
  1159. procedure tcgarm.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  1160. var
  1161. ref : treference;
  1162. firstfloatreg,lastfloatreg,
  1163. r : byte;
  1164. shift : byte;
  1165. begin
  1166. if not(nostackframe) then
  1167. begin
  1168. { restore floating point register }
  1169. firstfloatreg:=RS_NO;
  1170. { save floating point registers? }
  1171. for r:=RS_F0 to RS_F7 do
  1172. if r in rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall) then
  1173. begin
  1174. if firstfloatreg=RS_NO then
  1175. firstfloatreg:=r;
  1176. lastfloatreg:=r;
  1177. end;
  1178. if firstfloatreg<>RS_NO then
  1179. begin
  1180. reference_reset(ref);
  1181. if tarmprocinfo(current_procinfo).floatregstart<=-1023 then
  1182. begin
  1183. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  1184. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,NR_FRAME_POINTER_REG,NR_R12));
  1185. ref.base:=NR_R12;
  1186. end
  1187. else
  1188. begin
  1189. ref.base:=NR_FRAME_POINTER_REG;
  1190. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  1191. end;
  1192. list.concat(taicpu.op_reg_const_ref(A_LFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  1193. lastfloatreg-firstfloatreg+1,ref));
  1194. end;
  1195. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  1196. list.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14))
  1197. else
  1198. begin
  1199. { restore int registers and return }
  1200. reference_reset(ref);
  1201. ref.index:=NR_FRAME_POINTER_REG;
  1202. 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));
  1203. end;
  1204. end
  1205. else
  1206. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14));
  1207. end;
  1208. procedure tcgarm.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  1209. var
  1210. b : byte;
  1211. tmpref : treference;
  1212. instr : taicpu;
  1213. begin
  1214. if ref.addressmode<>AM_OFFSET then
  1215. internalerror(200309071);
  1216. tmpref:=ref;
  1217. { Be sure to have a base register }
  1218. if (tmpref.base=NR_NO) then
  1219. begin
  1220. if tmpref.shiftmode<>SM_None then
  1221. internalerror(200308294);
  1222. if tmpref.signindex<0 then
  1223. internalerror(200312023);
  1224. tmpref.base:=tmpref.index;
  1225. tmpref.index:=NR_NO;
  1226. end;
  1227. if assigned(tmpref.symbol) or
  1228. not((is_shifter_const(tmpref.offset,b)) or
  1229. (is_shifter_const(-tmpref.offset,b))
  1230. ) then
  1231. fixref(list,tmpref);
  1232. { expect a base here if there is an index }
  1233. if (tmpref.base=NR_NO) and (tmpref.index<>NR_NO) then
  1234. internalerror(200312022);
  1235. if tmpref.index<>NR_NO then
  1236. begin
  1237. if tmpref.shiftmode<>SM_None then
  1238. internalerror(200312021);
  1239. if tmpref.signindex<0 then
  1240. a_op_reg_reg_reg(list,OP_SUB,OS_ADDR,tmpref.base,tmpref.index,r)
  1241. else
  1242. a_op_reg_reg_reg(list,OP_ADD,OS_ADDR,tmpref.base,tmpref.index,r);
  1243. if tmpref.offset<>0 then
  1244. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,tmpref.offset,r,r);
  1245. end
  1246. else
  1247. begin
  1248. if tmpref.offset<>0 then
  1249. begin
  1250. if tmpref.base<>NR_NO then
  1251. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,tmpref.offset,tmpref.base,r)
  1252. else
  1253. a_load_const_reg(list,OS_ADDR,tmpref.offset,r);
  1254. end
  1255. else
  1256. begin
  1257. instr:=taicpu.op_reg_reg(A_MOV,r,tmpref.base);
  1258. list.concat(instr);
  1259. add_move_instruction(instr);
  1260. end;
  1261. end;
  1262. end;
  1263. procedure tcgarm.fixref(list : TAsmList;var ref : treference);
  1264. var
  1265. tmpreg : tregister;
  1266. tmpref : treference;
  1267. l : tasmlabel;
  1268. begin
  1269. { absolute symbols can't be handled directly, we've to store the symbol reference
  1270. in the text segment and access it pc relative
  1271. For now, we assume that references where base or index equals to PC are already
  1272. relative, all other references are assumed to be absolute and thus they need
  1273. to be handled extra.
  1274. A proper solution would be to change refoptions to a set and store the information
  1275. if the symbol is absolute or relative there.
  1276. }
  1277. { create consts entry }
  1278. reference_reset(tmpref);
  1279. current_asmdata.getjumplabel(l);
  1280. cg.a_label(current_procinfo.aktlocaldata,l);
  1281. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  1282. if assigned(ref.symbol) then
  1283. current_procinfo.aktlocaldata.concat(tai_const.create_sym_offset(ref.symbol,ref.offset))
  1284. else
  1285. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(ref.offset));
  1286. { load consts entry }
  1287. tmpreg:=getintregister(list,OS_INT);
  1288. tmpref.symbol:=l;
  1289. tmpref.base:=NR_PC;
  1290. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  1291. if (ref.base<>NR_NO) then
  1292. begin
  1293. if ref.index<>NR_NO then
  1294. begin
  1295. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  1296. ref.base:=tmpreg;
  1297. end
  1298. else
  1299. begin
  1300. ref.index:=tmpreg;
  1301. ref.shiftimm:=0;
  1302. ref.signindex:=1;
  1303. ref.shiftmode:=SM_None;
  1304. end;
  1305. end
  1306. else
  1307. ref.base:=tmpreg;
  1308. ref.offset:=0;
  1309. ref.symbol:=nil;
  1310. end;
  1311. procedure tcgarm.g_concatcopy_move(list : TAsmList;const source,dest : treference;len : aint);
  1312. var
  1313. paraloc1,paraloc2,paraloc3 : TCGPara;
  1314. begin
  1315. paraloc1.init;
  1316. paraloc2.init;
  1317. paraloc3.init;
  1318. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1319. paramanager.getintparaloc(pocall_default,2,paraloc2);
  1320. paramanager.getintparaloc(pocall_default,3,paraloc3);
  1321. paramanager.allocparaloc(list,paraloc3);
  1322. a_param_const(list,OS_INT,len,paraloc3);
  1323. paramanager.allocparaloc(list,paraloc2);
  1324. a_paramaddr_ref(list,dest,paraloc2);
  1325. paramanager.allocparaloc(list,paraloc2);
  1326. a_paramaddr_ref(list,source,paraloc1);
  1327. paramanager.freeparaloc(list,paraloc3);
  1328. paramanager.freeparaloc(list,paraloc2);
  1329. paramanager.freeparaloc(list,paraloc1);
  1330. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1331. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1332. a_call_name(list,'FPC_MOVE');
  1333. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1334. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1335. paraloc3.done;
  1336. paraloc2.done;
  1337. paraloc1.done;
  1338. end;
  1339. procedure tcgarm.g_concatcopy_internal(list : TAsmList;const source,dest : treference;len : aint;aligned : boolean);
  1340. const
  1341. 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}
  1342. var
  1343. srcref,dstref,usedtmpref,usedtmpref2:treference;
  1344. srcreg,destreg,countreg,r,tmpreg,tmpreg2:tregister;
  1345. helpsize:aword;
  1346. copysize:byte;
  1347. cgsize:Tcgsize;
  1348. so:tshifterop;
  1349. tmpregisters:array[1..maxtmpreg]of tregister;
  1350. tmpregi,tmpregi2:byte;
  1351. { will never be called with count<=4 }
  1352. procedure genloop(count : aword;size : byte);
  1353. const
  1354. size2opsize : array[1..4] of tcgsize = (OS_8,OS_16,OS_NO,OS_32);
  1355. var
  1356. l : tasmlabel;
  1357. begin
  1358. current_asmdata.getjumplabel(l);
  1359. if count<size then size:=1;
  1360. a_load_const_reg(list,OS_INT,count div size,countreg);
  1361. cg.a_label(list,l);
  1362. srcref.addressmode:=AM_POSTINDEXED;
  1363. dstref.addressmode:=AM_POSTINDEXED;
  1364. srcref.offset:=size;
  1365. dstref.offset:=size;
  1366. r:=getintregister(list,size2opsize[size]);
  1367. a_load_ref_reg(list,size2opsize[size],size2opsize[size],srcref,r);
  1368. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,countreg,countreg,1),PF_S));
  1369. a_load_reg_ref(list,size2opsize[size],size2opsize[size],r,dstref);
  1370. list.concat(setcondition(taicpu.op_sym(A_B,l),C_NE));
  1371. srcref.offset:=1;
  1372. dstref.offset:=1;
  1373. case count mod size of
  1374. 1:
  1375. begin
  1376. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1377. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1378. end;
  1379. 2:
  1380. if aligned then
  1381. begin
  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. end
  1385. else
  1386. begin
  1387. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1388. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  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. end;
  1392. 3:
  1393. if aligned then
  1394. begin
  1395. srcref.offset:=2;
  1396. dstref.offset:=2;
  1397. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  1398. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  1399. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1400. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1401. end
  1402. else
  1403. begin
  1404. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1405. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1406. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1407. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1408. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1409. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1410. end;
  1411. end;
  1412. { keep the registers alive }
  1413. list.concat(taicpu.op_reg_reg(A_MOV,countreg,countreg));
  1414. list.concat(taicpu.op_reg_reg(A_MOV,srcreg,srcreg));
  1415. list.concat(taicpu.op_reg_reg(A_MOV,destreg,destreg));
  1416. end;
  1417. begin
  1418. if len=0 then
  1419. exit;
  1420. helpsize:=12+maxtmpreg*4;//52 with maxtmpreg=10
  1421. dstref:=dest;
  1422. srcref:=source;
  1423. if cs_opt_size in aktoptimizerswitches then
  1424. helpsize:=8;
  1425. if (len<=helpsize) and aligned then
  1426. begin
  1427. tmpregi:=0;
  1428. srcreg:=getintregister(list,OS_ADDR);
  1429. { explicit pc relative addressing, could be
  1430. e.g. a floating point constant }
  1431. if source.base=NR_PC then
  1432. begin
  1433. { ... then we don't need a loadaddr }
  1434. srcref:=source;
  1435. end
  1436. else
  1437. begin
  1438. a_loadaddr_ref_reg(list,source,srcreg);
  1439. reference_reset_base(srcref,srcreg,0);
  1440. end;
  1441. while (len div 4 <> 0) and (tmpregi<=maxtmpreg) do
  1442. begin
  1443. inc(tmpregi);
  1444. tmpregisters[tmpregi]:=getintregister(list,OS_32);
  1445. a_load_ref_reg(list,OS_32,OS_32,srcref,tmpregisters[tmpregi]);
  1446. inc(srcref.offset,4);
  1447. dec(len,4);
  1448. end;
  1449. destreg:=getintregister(list,OS_ADDR);
  1450. a_loadaddr_ref_reg(list,dest,destreg);
  1451. reference_reset_base(dstref,destreg,0);
  1452. tmpregi2:=1;
  1453. while (tmpregi2<=tmpregi) do
  1454. begin
  1455. a_load_reg_ref(list,OS_32,OS_32,tmpregisters[tmpregi2],dstref);
  1456. inc(dstref.offset,4);
  1457. inc(tmpregi2);
  1458. end;
  1459. copysize:=4;
  1460. cgsize:=OS_32;
  1461. while len<>0 do
  1462. begin
  1463. if len<2 then
  1464. begin
  1465. copysize:=1;
  1466. cgsize:=OS_8;
  1467. end
  1468. else if len<4 then
  1469. begin
  1470. copysize:=2;
  1471. cgsize:=OS_16;
  1472. end;
  1473. dec(len,copysize);
  1474. r:=getintregister(list,cgsize);
  1475. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  1476. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  1477. inc(srcref.offset,copysize);
  1478. inc(dstref.offset,copysize);
  1479. end;{end of while}
  1480. end
  1481. else
  1482. begin
  1483. cgsize:=OS_32;
  1484. if (len<=4) then{len<=4 and not aligned}
  1485. begin
  1486. r:=getintregister(list,cgsize);
  1487. usedtmpref:=a_internal_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1488. if Len=1 then
  1489. a_load_reg_ref(list,OS_8,OS_8,r,dstref)
  1490. else
  1491. begin
  1492. tmpreg:=getintregister(list,cgsize);
  1493. usedtmpref2:=a_internal_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1494. inc(usedtmpref.offset,1);
  1495. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  1496. inc(usedtmpref2.offset,1);
  1497. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  1498. if len>2 then
  1499. begin
  1500. inc(usedtmpref.offset,1);
  1501. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  1502. inc(usedtmpref2.offset,1);
  1503. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  1504. if len>3 then
  1505. begin
  1506. inc(usedtmpref.offset,1);
  1507. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  1508. inc(usedtmpref2.offset,1);
  1509. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  1510. end;
  1511. end;
  1512. end;
  1513. end{end of if len<=4}
  1514. else
  1515. begin{unaligned & 4<len<helpsize **or** aligned/unaligned & len>helpsize}
  1516. destreg:=getintregister(list,OS_ADDR);
  1517. a_loadaddr_ref_reg(list,dest,destreg);
  1518. reference_reset_base(dstref,destreg,0);
  1519. srcreg:=getintregister(list,OS_ADDR);
  1520. a_loadaddr_ref_reg(list,source,srcreg);
  1521. reference_reset_base(srcref,srcreg,0);
  1522. countreg:=getintregister(list,OS_32);
  1523. // if cs_opt_size in aktoptimizerswitches then
  1524. { roozbeh : it seems loading 1 byte is faster becouse of caching/fetching(?) }
  1525. {if aligned then
  1526. genloop(len,4)
  1527. else}
  1528. genloop(len,1);
  1529. end;
  1530. end;
  1531. end;
  1532. procedure tcgarm.g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : aint);
  1533. begin
  1534. g_concatcopy_internal(list,source,dest,len,false);
  1535. end;
  1536. procedure tcgarm.g_concatcopy(list : TAsmList;const source,dest : treference;len : aint);
  1537. begin
  1538. if (source.alignment in [1..3]) or
  1539. (dest.alignment in [1..3]) then
  1540. g_concatcopy_internal(list,source,dest,len,false)
  1541. else
  1542. g_concatcopy_internal(list,source,dest,len,true);
  1543. end;
  1544. procedure tcgarm.g_overflowCheck(list : TAsmList;const l : tlocation;def : tdef);
  1545. var
  1546. ovloc : tlocation;
  1547. begin
  1548. ovloc.loc:=LOC_VOID;
  1549. g_overflowCheck_loc(list,l,def,ovloc);
  1550. end;
  1551. procedure tcgarm.g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);
  1552. var
  1553. hl : tasmlabel;
  1554. ai:TAiCpu;
  1555. hflags : tresflags;
  1556. begin
  1557. if not(cs_check_overflow in aktlocalswitches) then
  1558. exit;
  1559. current_asmdata.getjumplabel(hl);
  1560. case ovloc.loc of
  1561. LOC_VOID:
  1562. begin
  1563. ai:=taicpu.op_sym(A_B,hl);
  1564. ai.is_jmp:=true;
  1565. if not((def.deftype=pointerdef) or
  1566. ((def.deftype=orddef) and
  1567. (torddef(def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,bool8bit,bool16bit,bool32bit]))) then
  1568. ai.SetCondition(C_VC)
  1569. else
  1570. if TAiCpu(List.Last).opcode in [A_RSB,A_RSC,A_SBC,A_SUB] then
  1571. ai.SetCondition(C_CS)
  1572. else
  1573. ai.SetCondition(C_CC);
  1574. list.concat(ai);
  1575. end;
  1576. LOC_FLAGS:
  1577. begin
  1578. hflags:=ovloc.resflags;
  1579. inverse_flags(hflags);
  1580. cg.a_jmp_flags(list,hflags,hl);
  1581. end;
  1582. else
  1583. internalerror(200409281);
  1584. end;
  1585. a_call_name(list,'FPC_OVERFLOW');
  1586. a_label(list,hl);
  1587. end;
  1588. procedure tcgarm.g_save_standard_registers(list : TAsmList);
  1589. begin
  1590. { this work is done in g_proc_entry }
  1591. end;
  1592. procedure tcgarm.g_restore_standard_registers(list : TAsmList);
  1593. begin
  1594. { this work is done in g_proc_exit }
  1595. end;
  1596. procedure tcgarm.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1597. var
  1598. ai : taicpu;
  1599. begin
  1600. ai:=Taicpu.Op_sym(A_B,l);
  1601. ai.SetCondition(OpCmp2AsmCond[cond]);
  1602. ai.is_jmp:=true;
  1603. list.concat(ai);
  1604. end;
  1605. procedure tcgarm.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  1606. procedure loadvmttor12;
  1607. var
  1608. href : treference;
  1609. begin
  1610. reference_reset_base(href,NR_R0,0);
  1611. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
  1612. end;
  1613. procedure op_onr12methodaddr;
  1614. var
  1615. href : treference;
  1616. begin
  1617. if (procdef.extnumber=$ffff) then
  1618. Internalerror(200006139);
  1619. { call/jmp vmtoffs(%eax) ; method offs }
  1620. reference_reset_base(href,NR_R12,procdef._class.vmtmethodoffset(procdef.extnumber));
  1621. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
  1622. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R12));
  1623. end;
  1624. var
  1625. lab : tasmsymbol;
  1626. make_global : boolean;
  1627. href : treference;
  1628. begin
  1629. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  1630. Internalerror(200006137);
  1631. if not assigned(procdef._class) or
  1632. (procdef.procoptions*[po_classmethod, po_staticmethod,
  1633. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  1634. Internalerror(200006138);
  1635. if procdef.owner.symtabletype<>objectsymtable then
  1636. Internalerror(200109191);
  1637. make_global:=false;
  1638. if (not current_module.is_unit) or
  1639. (cs_create_smart in aktmoduleswitches) or
  1640. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  1641. make_global:=true;
  1642. if make_global then
  1643. list.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  1644. else
  1645. list.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  1646. { set param1 interface to self }
  1647. g_adjust_self_value(list,procdef,ioffset);
  1648. { case 4 }
  1649. if po_virtualmethod in procdef.procoptions then
  1650. begin
  1651. loadvmttor12;
  1652. op_onr12methodaddr;
  1653. end
  1654. { case 0 }
  1655. else
  1656. list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(procdef.mangledname)));
  1657. list.concat(Tai_symbol_end.Createname(labelname));
  1658. end;
  1659. procedure tcg64farm.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  1660. var
  1661. tmpreg : tregister;
  1662. begin
  1663. case op of
  1664. OP_NEG:
  1665. begin
  1666. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSB,regdst.reglo,regsrc.reglo,0),PF_S));
  1667. list.concat(taicpu.op_reg_reg_const(A_RSC,regdst.reghi,regsrc.reghi,0));
  1668. end;
  1669. OP_NOT:
  1670. begin
  1671. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reglo,regdst.reglo);
  1672. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reghi,regdst.reghi);
  1673. end;
  1674. else
  1675. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  1676. end;
  1677. end;
  1678. procedure tcg64farm.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  1679. begin
  1680. a_op64_const_reg_reg(list,op,size,value,reg,reg);
  1681. end;
  1682. procedure tcg64farm.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);
  1683. var
  1684. ovloc : tlocation;
  1685. begin
  1686. a_op64_const_reg_reg_checkoverflow(list,op,size,value,regsrc,regdst,false,ovloc);
  1687. end;
  1688. procedure tcg64farm.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  1689. var
  1690. ovloc : tlocation;
  1691. begin
  1692. a_op64_reg_reg_reg_checkoverflow(list,op,size,regsrc1,regsrc2,regdst,false,ovloc);
  1693. end;
  1694. procedure tcg64farm.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  1695. var
  1696. tmpreg : tregister;
  1697. b : byte;
  1698. begin
  1699. ovloc.loc:=LOC_VOID;
  1700. case op of
  1701. OP_NEG,
  1702. OP_NOT :
  1703. internalerror(200306017);
  1704. end;
  1705. if (setflags or tcgarm(cg).cgsetflags) and (op in [OP_ADD,OP_SUB]) then
  1706. begin
  1707. case op of
  1708. OP_ADD:
  1709. begin
  1710. if is_shifter_const(lo(value),b) then
  1711. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  1712. else
  1713. begin
  1714. tmpreg:=cg.getintregister(list,OS_32);
  1715. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  1716. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  1717. end;
  1718. if is_shifter_const(hi(value),b) then
  1719. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,hi(value)),PF_S))
  1720. else
  1721. begin
  1722. tmpreg:=cg.getintregister(list,OS_32);
  1723. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  1724. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg),PF_S));
  1725. end;
  1726. end;
  1727. OP_SUB:
  1728. begin
  1729. if is_shifter_const(lo(value),b) then
  1730. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  1731. else
  1732. begin
  1733. tmpreg:=cg.getintregister(list,OS_32);
  1734. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  1735. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  1736. end;
  1737. if is_shifter_const(hi(value),b) then
  1738. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SBC,regdst.reghi,regsrc.reghi,hi(value)),PF_S))
  1739. else
  1740. begin
  1741. tmpreg:=cg.getintregister(list,OS_32);
  1742. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  1743. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg),PF_S));
  1744. end;
  1745. end;
  1746. else
  1747. internalerror(200502131);
  1748. end;
  1749. if size=OS_64 then
  1750. begin
  1751. { the arm has an weired opinion how flags for SUB/ADD are handled }
  1752. ovloc.loc:=LOC_FLAGS;
  1753. case op of
  1754. OP_ADD:
  1755. ovloc.resflags:=F_CS;
  1756. OP_SUB:
  1757. ovloc.resflags:=F_CC;
  1758. end;
  1759. end;
  1760. end
  1761. else
  1762. begin
  1763. case op of
  1764. OP_AND,OP_OR,OP_XOR:
  1765. begin
  1766. cg.a_op_const_reg_reg(list,op,OS_32,lo(value),regsrc.reglo,regdst.reglo);
  1767. cg.a_op_const_reg_reg(list,op,OS_32,hi(value),regsrc.reghi,regdst.reghi);
  1768. end;
  1769. OP_ADD:
  1770. begin
  1771. if is_shifter_const(lo(value),b) then
  1772. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  1773. else
  1774. begin
  1775. tmpreg:=cg.getintregister(list,OS_32);
  1776. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  1777. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  1778. end;
  1779. if is_shifter_const(hi(value),b) then
  1780. list.concat(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,hi(value)))
  1781. else
  1782. begin
  1783. tmpreg:=cg.getintregister(list,OS_32);
  1784. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  1785. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg));
  1786. end;
  1787. end;
  1788. OP_SUB:
  1789. begin
  1790. if is_shifter_const(lo(value),b) then
  1791. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  1792. else
  1793. begin
  1794. tmpreg:=cg.getintregister(list,OS_32);
  1795. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  1796. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  1797. end;
  1798. if is_shifter_const(hi(value),b) then
  1799. list.concat(taicpu.op_reg_reg_const(A_SBC,regdst.reghi,regsrc.reghi,hi(value)))
  1800. else
  1801. begin
  1802. tmpreg:=cg.getintregister(list,OS_32);
  1803. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  1804. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg));
  1805. end;
  1806. end;
  1807. else
  1808. internalerror(2003083101);
  1809. end;
  1810. end;
  1811. end;
  1812. procedure tcg64farm.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  1813. var
  1814. op1,op2:TAsmOp;
  1815. begin
  1816. ovloc.loc:=LOC_VOID;
  1817. case op of
  1818. OP_NEG,
  1819. OP_NOT :
  1820. internalerror(200306017);
  1821. end;
  1822. if (setflags or tcgarm(cg).cgsetflags) and (op in [OP_ADD,OP_SUB]) then
  1823. begin
  1824. case op of
  1825. OP_ADD:
  1826. begin
  1827. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  1828. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi),PF_S));
  1829. end;
  1830. OP_SUB:
  1831. begin
  1832. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  1833. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi),PF_S));
  1834. end;
  1835. else
  1836. internalerror(2003083101);
  1837. end;
  1838. if size=OS_64 then
  1839. begin
  1840. { the arm has an weired opinion how flags for SUB/ADD are handled }
  1841. ovloc.loc:=LOC_FLAGS;
  1842. case op of
  1843. OP_ADD:
  1844. ovloc.resflags:=F_CS;
  1845. OP_SUB:
  1846. ovloc.resflags:=F_CC;
  1847. end;
  1848. end;
  1849. end
  1850. else
  1851. begin
  1852. case op of
  1853. OP_AND,OP_OR,OP_XOR:
  1854. begin
  1855. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  1856. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  1857. end;
  1858. OP_ADD:
  1859. begin
  1860. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  1861. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1862. end;
  1863. OP_SUB:
  1864. begin
  1865. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  1866. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi));
  1867. end;
  1868. else
  1869. internalerror(2003083101);
  1870. end;
  1871. end;
  1872. end;
  1873. begin
  1874. cg:=tcgarm.create;
  1875. cg64:=tcg64farm.create;
  1876. end.