cgcpu.pas 83 KB

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