cgcpu.pas 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213
  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. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  697. FromSize := ToSize;
  698. case ToSize of
  699. { signed integer registers }
  700. OS_8,
  701. OS_S8:
  702. oppostfix:=PF_B;
  703. OS_16,
  704. OS_S16:
  705. oppostfix:=PF_H;
  706. OS_32,
  707. OS_S32:
  708. oppostfix:=PF_None;
  709. else
  710. InternalError(200308295);
  711. end;
  712. if (ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[tosize]) then
  713. begin
  714. if target_info.endian=endian_big then
  715. dir:=-1
  716. else
  717. dir:=1;
  718. case FromSize of
  719. OS_16,OS_S16:
  720. begin
  721. shifterop_reset(so);so.shiftmode:=SM_LSR;so.shiftimm:=8;
  722. tmpreg:=getintregister(list,OS_INT);
  723. usedtmpref:=ref;
  724. if target_info.endian=endian_big then
  725. inc(usedtmpref.offset,1);
  726. usedtmpref:=a_internal_load_reg_ref(list,OS_8,OS_8,reg,usedtmpref);
  727. inc(usedtmpref.offset,dir);
  728. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,tmpreg,reg,so));
  729. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  730. end;
  731. OS_32,OS_S32:
  732. begin
  733. tmpreg:=getintregister(list,OS_INT);
  734. usedtmpref:=ref;
  735. shifterop_reset(so);so.shiftmode:=SM_LSR;
  736. if ref.alignment=2 then
  737. begin
  738. so.shiftimm:=16;
  739. if target_info.endian=endian_big then
  740. inc(usedtmpref.offset,2);
  741. usedtmpref:=a_internal_load_reg_ref(list,OS_16,OS_16,reg,usedtmpref);
  742. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,tmpreg,reg,so));
  743. inc(usedtmpref.offset,dir*2);
  744. a_internal_load_reg_ref(list,OS_16,OS_16,tmpreg,usedtmpref);
  745. end
  746. else
  747. begin
  748. so.shiftimm:=8;
  749. if target_info.endian=endian_big then
  750. inc(usedtmpref.offset,3);
  751. usedtmpref:=a_internal_load_reg_ref(list,OS_8,OS_8,reg,usedtmpref);
  752. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,tmpreg,reg,so));
  753. inc(usedtmpref.offset,dir);
  754. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  755. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,tmpreg,tmpreg,so));
  756. inc(usedtmpref.offset,dir);
  757. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  758. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,tmpreg,tmpreg,so));
  759. inc(usedtmpref.offset,dir);
  760. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  761. end;
  762. end
  763. else
  764. handle_load_store(list,A_STR,oppostfix,reg,ref);
  765. end;
  766. end
  767. else
  768. handle_load_store(list,A_STR,oppostfix,reg,ref);
  769. end;
  770. procedure tcgarm.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  771. var
  772. oppostfix:toppostfix;
  773. usedtmpref: treference;
  774. tmpreg,tmpreg2 : tregister;
  775. so : tshifterop;
  776. dir : integer;
  777. begin
  778. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  779. FromSize := ToSize;
  780. case FromSize of
  781. { signed integer registers }
  782. OS_8:
  783. oppostfix:=PF_B;
  784. OS_S8:
  785. oppostfix:=PF_SB;
  786. OS_16:
  787. oppostfix:=PF_H;
  788. OS_S16:
  789. oppostfix:=PF_SH;
  790. OS_32,
  791. OS_S32:
  792. oppostfix:=PF_None;
  793. else
  794. InternalError(200308297);
  795. end;
  796. if (ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[fromsize]) then
  797. begin
  798. if target_info.endian=endian_big then
  799. dir:=-1
  800. else
  801. dir:=1;
  802. case FromSize of
  803. OS_16,OS_S16:
  804. begin
  805. { only complicated references need an extra loadaddr }
  806. if assigned(ref.symbol) or
  807. (ref.index<>NR_NO) or
  808. (ref.offset<-4095) or
  809. (ref.offset>4094) or
  810. { sometimes the compiler reused registers }
  811. (reg=ref.index) or
  812. (reg=ref.base) then
  813. begin
  814. tmpreg2:=getintregister(list,OS_INT);
  815. a_loadaddr_ref_reg(list,ref,tmpreg2);
  816. reference_reset_base(usedtmpref,tmpreg2,0);
  817. end
  818. else
  819. usedtmpref:=ref;
  820. if target_info.endian=endian_big then
  821. inc(usedtmpref.offset,1);
  822. shifterop_reset(so);so.shiftmode:=SM_LSL;so.shiftimm:=8;
  823. tmpreg:=getintregister(list,OS_INT);
  824. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  825. inc(usedtmpref.offset,dir);
  826. if FromSize=OS_16 then
  827. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg)
  828. else
  829. a_internal_load_ref_reg(list,OS_S8,OS_S8,usedtmpref,tmpreg);
  830. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  831. end;
  832. OS_32,OS_S32:
  833. begin
  834. tmpreg:=getintregister(list,OS_INT);
  835. { only complicated references need an extra loadaddr }
  836. if assigned(ref.symbol) or
  837. (ref.index<>NR_NO) or
  838. (ref.offset<-4095) or
  839. (ref.offset>4092) or
  840. { sometimes the compiler reused registers }
  841. (reg=ref.index) or
  842. (reg=ref.base) then
  843. begin
  844. tmpreg2:=getintregister(list,OS_INT);
  845. a_loadaddr_ref_reg(list,ref,tmpreg2);
  846. reference_reset_base(usedtmpref,tmpreg2,0);
  847. end
  848. else
  849. usedtmpref:=ref;
  850. shifterop_reset(so);so.shiftmode:=SM_LSL;
  851. if ref.alignment=2 then
  852. begin
  853. if target_info.endian=endian_big then
  854. inc(usedtmpref.offset,2);
  855. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,reg);
  856. inc(usedtmpref.offset,dir*2);
  857. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,tmpreg);
  858. so.shiftimm:=16;
  859. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  860. end
  861. else
  862. begin
  863. if target_info.endian=endian_big then
  864. inc(usedtmpref.offset,3);
  865. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  866. inc(usedtmpref.offset,dir);
  867. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  868. so.shiftimm:=8;
  869. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  870. inc(usedtmpref.offset,dir);
  871. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  872. so.shiftimm:=16;
  873. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  874. inc(usedtmpref.offset,dir);
  875. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  876. so.shiftimm:=24;
  877. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  878. end;
  879. end
  880. else
  881. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  882. end;
  883. end
  884. else
  885. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  886. if (fromsize=OS_S8) and (tosize = OS_16) then
  887. a_load_reg_reg(list,OS_16,OS_32,reg,reg);
  888. end;
  889. function tcgarm.a_internal_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference):treference;
  890. var
  891. oppostfix:toppostfix;
  892. begin
  893. case ToSize of
  894. { signed integer registers }
  895. OS_8,
  896. OS_S8:
  897. oppostfix:=PF_B;
  898. OS_16,
  899. OS_S16:
  900. oppostfix:=PF_H;
  901. OS_32,
  902. OS_S32:
  903. oppostfix:=PF_None;
  904. else
  905. InternalError(2003082910);
  906. end;
  907. result:=handle_load_store(list,A_STR,oppostfix,reg,ref);
  908. end;
  909. function tcgarm.a_internal_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister):treference;
  910. var
  911. oppostfix:toppostfix;
  912. begin
  913. case FromSize of
  914. { signed integer registers }
  915. OS_8:
  916. oppostfix:=PF_B;
  917. OS_S8:
  918. oppostfix:=PF_SB;
  919. OS_16:
  920. oppostfix:=PF_H;
  921. OS_S16:
  922. oppostfix:=PF_SH;
  923. OS_32,
  924. OS_S32:
  925. oppostfix:=PF_None;
  926. else
  927. InternalError(200308291);
  928. end;
  929. result:=handle_load_store(list,A_LDR,oppostfix,reg,ref);
  930. end;
  931. procedure tcgarm.a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  932. var
  933. so : tshifterop;
  934. procedure do_shift(shiftmode : tshiftmode; shiftimm : byte; reg : tregister);
  935. begin
  936. so.shiftmode:=shiftmode;
  937. so.shiftimm:=shiftimm;
  938. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,reg2,reg,so));
  939. end;
  940. var
  941. instr: taicpu;
  942. conv_done: boolean;
  943. begin
  944. if (tcgsize2size[fromsize]>32) or (tcgsize2size[tosize]>32) or (fromsize=OS_NO) or (tosize=OS_NO) then
  945. internalerror(2002090901);
  946. conv_done:=false;
  947. if tosize<>fromsize then
  948. begin
  949. shifterop_reset(so);
  950. conv_done:=true;
  951. if tcgsize2size[tosize]<=tcgsize2size[fromsize] then
  952. fromsize:=tosize;
  953. case fromsize of
  954. OS_8:
  955. list.concat(taicpu.op_reg_reg_const(A_AND,reg2,reg1,$ff));
  956. OS_S8:
  957. begin
  958. do_shift(SM_LSL,24,reg1);
  959. if tosize=OS_16 then
  960. begin
  961. do_shift(SM_ASR,8,reg2);
  962. do_shift(SM_LSR,16,reg2);
  963. end
  964. else
  965. do_shift(SM_ASR,24,reg2);
  966. end;
  967. OS_16:
  968. begin
  969. do_shift(SM_LSL,16,reg1);
  970. do_shift(SM_LSR,16,reg2);
  971. end;
  972. OS_S16:
  973. begin
  974. do_shift(SM_LSL,16,reg1);
  975. do_shift(SM_ASR,16,reg2)
  976. end;
  977. else
  978. conv_done:=false;
  979. end;
  980. end;
  981. if not conv_done and (reg1<>reg2) then
  982. begin
  983. { same size, only a register mov required }
  984. instr:=taicpu.op_reg_reg(A_MOV,reg2,reg1);
  985. list.Concat(instr);
  986. { Notify the register allocator that we have written a move instruction so
  987. it can try to eliminate it. }
  988. add_move_instruction(instr);
  989. end;
  990. end;
  991. procedure tcgarm.a_paramfpu_ref(list : TAsmList;size : tcgsize;const ref : treference;const paraloc : TCGPara);
  992. var
  993. href,href2 : treference;
  994. hloc : pcgparalocation;
  995. begin
  996. href:=ref;
  997. hloc:=paraloc.location;
  998. while assigned(hloc) do
  999. begin
  1000. case hloc^.loc of
  1001. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1002. a_loadfpu_ref_reg(list,size,size,ref,hloc^.register);
  1003. LOC_REGISTER :
  1004. case hloc^.size of
  1005. OS_F32:
  1006. a_load_ref_reg(list,OS_32,OS_32,href,hloc^.register);
  1007. OS_64,
  1008. OS_F64:
  1009. cg64.a_param64_ref(list,href,paraloc);
  1010. else
  1011. a_load_ref_reg(list,hloc^.size,hloc^.size,href,hloc^.register);
  1012. end;
  1013. LOC_REFERENCE :
  1014. begin
  1015. reference_reset_base(href2,hloc^.reference.index,hloc^.reference.offset);
  1016. { concatcopy should choose the best way to copy the data }
  1017. g_concatcopy(list,href,href2,tcgsize2size[size]);
  1018. end;
  1019. else
  1020. internalerror(200408241);
  1021. end;
  1022. inc(href.offset,tcgsize2size[hloc^.size]);
  1023. hloc:=hloc^.next;
  1024. end;
  1025. end;
  1026. procedure tcgarm.a_loadfpu_reg_reg(list: TAsmList; fromsize,tosize: tcgsize; reg1, reg2: tregister);
  1027. begin
  1028. list.concat(setoppostfix(taicpu.op_reg_reg(A_MVF,reg2,reg1),cgsize2fpuoppostfix[tosize]));
  1029. end;
  1030. procedure tcgarm.a_loadfpu_ref_reg(list: TAsmList; fromsize,tosize: tcgsize; const ref: treference; reg: tregister);
  1031. var
  1032. oppostfix:toppostfix;
  1033. begin
  1034. case tosize of
  1035. OS_32,
  1036. OS_F32:
  1037. oppostfix:=PF_S;
  1038. OS_64,
  1039. OS_F64:
  1040. oppostfix:=PF_D;
  1041. OS_F80:
  1042. oppostfix:=PF_E;
  1043. else
  1044. InternalError(200309021);
  1045. end;
  1046. handle_load_store(list,A_LDF,oppostfix,reg,ref);
  1047. end;
  1048. procedure tcgarm.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference);
  1049. var
  1050. oppostfix:toppostfix;
  1051. begin
  1052. case tosize of
  1053. OS_F32:
  1054. oppostfix:=PF_S;
  1055. OS_F64:
  1056. oppostfix:=PF_D;
  1057. OS_F80:
  1058. oppostfix:=PF_E;
  1059. else
  1060. InternalError(200309022);
  1061. end;
  1062. handle_load_store(list,A_STF,oppostfix,reg,ref);
  1063. end;
  1064. { comparison operations }
  1065. procedure tcgarm.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  1066. l : tasmlabel);
  1067. var
  1068. tmpreg : tregister;
  1069. b : byte;
  1070. begin
  1071. if is_shifter_const(a,b) then
  1072. list.concat(taicpu.op_reg_const(A_CMP,reg,a))
  1073. { CMN reg,0 and CMN reg,$80000000 are different from CMP reg,$ffffffff
  1074. and CMP reg,$7fffffff regarding the flags according to the ARM manual }
  1075. else if (a<>$7fffffff) and (a<>-1) and is_shifter_const(-a,b) then
  1076. list.concat(taicpu.op_reg_const(A_CMN,reg,-a))
  1077. else
  1078. begin
  1079. tmpreg:=getintregister(list,size);
  1080. a_load_const_reg(list,size,a,tmpreg);
  1081. list.concat(taicpu.op_reg_reg(A_CMP,reg,tmpreg));
  1082. end;
  1083. a_jmp_cond(list,cmp_op,l);
  1084. end;
  1085. procedure tcgarm.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1086. begin
  1087. list.concat(taicpu.op_reg_reg(A_CMP,reg2,reg1));
  1088. a_jmp_cond(list,cmp_op,l);
  1089. end;
  1090. procedure tcgarm.a_jmp_name(list : TAsmList;const s : string);
  1091. var
  1092. ai : taicpu;
  1093. begin
  1094. ai:=taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(s));
  1095. ai.is_jmp:=true;
  1096. list.concat(ai);
  1097. end;
  1098. procedure tcgarm.a_jmp_always(list : TAsmList;l: tasmlabel);
  1099. var
  1100. ai : taicpu;
  1101. begin
  1102. ai:=taicpu.op_sym(A_B,l);
  1103. ai.is_jmp:=true;
  1104. list.concat(ai);
  1105. end;
  1106. procedure tcgarm.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1107. var
  1108. ai : taicpu;
  1109. begin
  1110. ai:=setcondition(taicpu.op_sym(A_B,l),flags_to_cond(f));
  1111. ai.is_jmp:=true;
  1112. list.concat(ai);
  1113. end;
  1114. procedure tcgarm.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  1115. begin
  1116. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,1),flags_to_cond(f)));
  1117. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,0),inverse_cond(flags_to_cond(f))));
  1118. end;
  1119. procedure tcgarm.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  1120. var
  1121. ref : treference;
  1122. shift : byte;
  1123. firstfloatreg,lastfloatreg,
  1124. r : byte;
  1125. regs : tcpuregisterset;
  1126. begin
  1127. LocalSize:=align(LocalSize,4);
  1128. if not(nostackframe) then
  1129. begin
  1130. firstfloatreg:=RS_NO;
  1131. { save floating point registers? }
  1132. for r:=RS_F0 to RS_F7 do
  1133. if r in rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall) then
  1134. begin
  1135. if firstfloatreg=RS_NO then
  1136. firstfloatreg:=r;
  1137. lastfloatreg:=r;
  1138. end;
  1139. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1140. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1141. begin
  1142. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  1143. a_reg_alloc(list,NR_R12);
  1144. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_STACK_POINTER_REG));
  1145. end;
  1146. { save int registers }
  1147. reference_reset(ref);
  1148. ref.index:=NR_STACK_POINTER_REG;
  1149. ref.addressmode:=AM_PREINDEXED;
  1150. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  1151. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1152. regs:=regs+[RS_R11,RS_R12,RS_R14,RS_R15]
  1153. else
  1154. if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  1155. include(regs,RS_R14);
  1156. if regs<>[] then
  1157. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,regs),PF_FD));
  1158. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1159. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_FRAME_POINTER_REG,NR_R12,4));
  1160. { allocate necessary stack size
  1161. not necessary according to Yury Sidorov
  1162. { don't use a_op_const_reg_reg here because we don't allow register allocations
  1163. in the entry/exit code }
  1164. if (target_info.system in [system_arm_wince]) and
  1165. (localsize>=winstackpagesize) then
  1166. begin
  1167. if localsize div winstackpagesize<=5 then
  1168. begin
  1169. if is_shifter_const(localsize,shift) then
  1170. list.concat(Taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,localsize))
  1171. else
  1172. begin
  1173. a_load_const_reg(list,OS_ADDR,localsize,NR_R12);
  1174. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  1175. end;
  1176. for i:=1 to localsize div winstackpagesize do
  1177. begin
  1178. if localsize-i*winstackpagesize<4096 then
  1179. reference_reset_base(href,NR_STACK_POINTER_REG,-(localsize-i*winstackpagesize))
  1180. else
  1181. begin
  1182. a_load_const_reg(list,OS_ADDR,-(localsize-i*winstackpagesize),NR_R12);
  1183. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1184. href.index:=NR_R12;
  1185. end;
  1186. { the data stored doesn't matter }
  1187. list.concat(Taicpu.op_reg_ref(A_STR,NR_R0,href));
  1188. end;
  1189. a_reg_dealloc(list,NR_R12);
  1190. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1191. { the data stored doesn't matter }
  1192. list.concat(Taicpu.op_reg_ref(A_STR,NR_R0,href));
  1193. end
  1194. else
  1195. begin
  1196. current_asmdata.getjumplabel(again);
  1197. list.concat(Taicpu.op_reg_const(A_MOV,NR_R12,localsize div winstackpagesize));
  1198. a_label(list,again);
  1199. { always shifterop }
  1200. list.concat(Taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,winstackpagesize));
  1201. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1202. { the data stored doesn't matter }
  1203. list.concat(Taicpu.op_reg_ref(A_STR,NR_R0,href));
  1204. list.concat(Taicpu.op_reg_reg_const(A_SUB,NR_R12,NR_R12,1));
  1205. a_jmp_cond(list,OC_NE,again);
  1206. if is_shifter_const(localsize mod winstackpagesize,shift) then
  1207. list.concat(Taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,localsize mod winstackpagesize))
  1208. else
  1209. begin
  1210. a_load_const_reg(list,OS_ADDR,localsize mod winstackpagesize,NR_R12);
  1211. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  1212. end;
  1213. a_reg_dealloc(list,NR_R12);
  1214. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  1215. { the data stored doesn't matter }
  1216. list.concat(Taicpu.op_reg_ref(A_STR,NR_R0,href));
  1217. end
  1218. end
  1219. else
  1220. }
  1221. if LocalSize<>0 then
  1222. if not(is_shifter_const(localsize,shift)) then
  1223. begin
  1224. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  1225. a_reg_alloc(list,NR_R12);
  1226. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  1227. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  1228. a_reg_dealloc(list,NR_R12);
  1229. end
  1230. else
  1231. begin
  1232. a_reg_dealloc(list,NR_R12);
  1233. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  1234. end;
  1235. if firstfloatreg<>RS_NO then
  1236. begin
  1237. reference_reset(ref);
  1238. if tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023 then
  1239. begin
  1240. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  1241. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  1242. ref.base:=NR_R12;
  1243. end
  1244. else
  1245. begin
  1246. ref.base:=current_procinfo.framepointer;
  1247. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  1248. end;
  1249. list.concat(taicpu.op_reg_const_ref(A_SFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  1250. lastfloatreg-firstfloatreg+1,ref));
  1251. end;
  1252. end;
  1253. end;
  1254. procedure tcgarm.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  1255. var
  1256. ref : treference;
  1257. firstfloatreg,lastfloatreg,
  1258. r : byte;
  1259. shift : byte;
  1260. regs : tcpuregisterset;
  1261. LocalSize : longint;
  1262. begin
  1263. if not(nostackframe) then
  1264. begin
  1265. { restore floating point register }
  1266. firstfloatreg:=RS_NO;
  1267. { save floating point registers? }
  1268. for r:=RS_F0 to RS_F7 do
  1269. if r in rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall) then
  1270. begin
  1271. if firstfloatreg=RS_NO then
  1272. firstfloatreg:=r;
  1273. lastfloatreg:=r;
  1274. end;
  1275. if firstfloatreg<>RS_NO then
  1276. begin
  1277. reference_reset(ref);
  1278. if tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023 then
  1279. begin
  1280. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  1281. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  1282. ref.base:=NR_R12;
  1283. end
  1284. else
  1285. begin
  1286. ref.base:=current_procinfo.framepointer;
  1287. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  1288. end;
  1289. list.concat(taicpu.op_reg_const_ref(A_LFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  1290. lastfloatreg-firstfloatreg+1,ref));
  1291. end;
  1292. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  1293. begin
  1294. LocalSize:=current_procinfo.calc_stackframe_size;
  1295. if LocalSize<>0 then
  1296. if not(is_shifter_const(LocalSize,shift)) then
  1297. begin
  1298. a_reg_alloc(list,NR_R12);
  1299. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  1300. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  1301. a_reg_dealloc(list,NR_R12);
  1302. end
  1303. else
  1304. begin
  1305. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  1306. end;
  1307. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  1308. if (pi_do_call in current_procinfo.flags) or (regs<>[]) then
  1309. begin
  1310. exclude(regs,RS_R14);
  1311. include(regs,RS_R15);
  1312. end;
  1313. if regs=[] then
  1314. list.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14))
  1315. else
  1316. begin
  1317. reference_reset(ref);
  1318. ref.index:=NR_STACK_POINTER_REG;
  1319. ref.addressmode:=AM_PREINDEXED;
  1320. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,regs),PF_FD));
  1321. end;
  1322. end
  1323. else
  1324. begin
  1325. { restore int registers and return }
  1326. reference_reset(ref);
  1327. ref.index:=NR_FRAME_POINTER_REG;
  1328. 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));
  1329. end;
  1330. end
  1331. else
  1332. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14));
  1333. end;
  1334. procedure tcgarm.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  1335. var
  1336. b : byte;
  1337. tmpref : treference;
  1338. instr : taicpu;
  1339. begin
  1340. if ref.addressmode<>AM_OFFSET then
  1341. internalerror(200309071);
  1342. tmpref:=ref;
  1343. { Be sure to have a base register }
  1344. if (tmpref.base=NR_NO) then
  1345. begin
  1346. if tmpref.shiftmode<>SM_None then
  1347. internalerror(200308294);
  1348. if tmpref.signindex<0 then
  1349. internalerror(200312023);
  1350. tmpref.base:=tmpref.index;
  1351. tmpref.index:=NR_NO;
  1352. end;
  1353. if assigned(tmpref.symbol) or
  1354. not((is_shifter_const(tmpref.offset,b)) or
  1355. (is_shifter_const(-tmpref.offset,b))
  1356. ) then
  1357. fixref(list,tmpref);
  1358. { expect a base here if there is an index }
  1359. if (tmpref.base=NR_NO) and (tmpref.index<>NR_NO) then
  1360. internalerror(200312022);
  1361. if tmpref.index<>NR_NO then
  1362. begin
  1363. if tmpref.shiftmode<>SM_None then
  1364. internalerror(200312021);
  1365. if tmpref.signindex<0 then
  1366. a_op_reg_reg_reg(list,OP_SUB,OS_ADDR,tmpref.base,tmpref.index,r)
  1367. else
  1368. a_op_reg_reg_reg(list,OP_ADD,OS_ADDR,tmpref.base,tmpref.index,r);
  1369. if tmpref.offset<>0 then
  1370. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,tmpref.offset,r,r);
  1371. end
  1372. else
  1373. begin
  1374. if tmpref.base=NR_NO then
  1375. a_load_const_reg(list,OS_ADDR,tmpref.offset,r)
  1376. else
  1377. if tmpref.offset<>0 then
  1378. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,tmpref.offset,tmpref.base,r)
  1379. else
  1380. begin
  1381. instr:=taicpu.op_reg_reg(A_MOV,r,tmpref.base);
  1382. list.concat(instr);
  1383. add_move_instruction(instr);
  1384. end;
  1385. end;
  1386. end;
  1387. procedure tcgarm.fixref(list : TAsmList;var ref : treference);
  1388. var
  1389. tmpreg : tregister;
  1390. tmpref : treference;
  1391. l : tasmlabel;
  1392. begin
  1393. { absolute symbols can't be handled directly, we've to store the symbol reference
  1394. in the text segment and access it pc relative
  1395. For now, we assume that references where base or index equals to PC are already
  1396. relative, all other references are assumed to be absolute and thus they need
  1397. to be handled extra.
  1398. A proper solution would be to change refoptions to a set and store the information
  1399. if the symbol is absolute or relative there.
  1400. }
  1401. { create consts entry }
  1402. reference_reset(tmpref);
  1403. current_asmdata.getjumplabel(l);
  1404. cg.a_label(current_procinfo.aktlocaldata,l);
  1405. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  1406. if assigned(ref.symbol) then
  1407. current_procinfo.aktlocaldata.concat(tai_const.create_sym_offset(ref.symbol,ref.offset))
  1408. else
  1409. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(ref.offset));
  1410. { load consts entry }
  1411. tmpreg:=getintregister(list,OS_INT);
  1412. tmpref.symbol:=l;
  1413. tmpref.base:=NR_PC;
  1414. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  1415. if (ref.base<>NR_NO) then
  1416. begin
  1417. if ref.index<>NR_NO then
  1418. begin
  1419. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  1420. ref.base:=tmpreg;
  1421. end
  1422. else
  1423. if ref.base<>NR_PC then
  1424. begin
  1425. ref.index:=tmpreg;
  1426. ref.shiftimm:=0;
  1427. ref.signindex:=1;
  1428. ref.shiftmode:=SM_None;
  1429. end
  1430. else
  1431. ref.base:=tmpreg;
  1432. end
  1433. else
  1434. ref.base:=tmpreg;
  1435. ref.offset:=0;
  1436. ref.symbol:=nil;
  1437. end;
  1438. procedure tcgarm.g_concatcopy_move(list : TAsmList;const source,dest : treference;len : aint);
  1439. var
  1440. paraloc1,paraloc2,paraloc3 : TCGPara;
  1441. begin
  1442. paraloc1.init;
  1443. paraloc2.init;
  1444. paraloc3.init;
  1445. paramanager.getintparaloc(pocall_default,1,paraloc1);
  1446. paramanager.getintparaloc(pocall_default,2,paraloc2);
  1447. paramanager.getintparaloc(pocall_default,3,paraloc3);
  1448. paramanager.allocparaloc(list,paraloc3);
  1449. a_param_const(list,OS_INT,len,paraloc3);
  1450. paramanager.allocparaloc(list,paraloc2);
  1451. a_paramaddr_ref(list,dest,paraloc2);
  1452. paramanager.allocparaloc(list,paraloc2);
  1453. a_paramaddr_ref(list,source,paraloc1);
  1454. paramanager.freeparaloc(list,paraloc3);
  1455. paramanager.freeparaloc(list,paraloc2);
  1456. paramanager.freeparaloc(list,paraloc1);
  1457. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1458. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1459. a_call_name(list,'FPC_MOVE');
  1460. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1461. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1462. paraloc3.done;
  1463. paraloc2.done;
  1464. paraloc1.done;
  1465. end;
  1466. procedure tcgarm.g_concatcopy_internal(list : TAsmList;const source,dest : treference;len : aint;aligned : boolean);
  1467. const
  1468. 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}
  1469. var
  1470. srcref,dstref,usedtmpref,usedtmpref2:treference;
  1471. srcreg,destreg,countreg,r,tmpreg:tregister;
  1472. helpsize:aint;
  1473. copysize:byte;
  1474. cgsize:Tcgsize;
  1475. tmpregisters:array[1..maxtmpreg] of tregister;
  1476. tmpregi,tmpregi2:byte;
  1477. { will never be called with count<=4 }
  1478. procedure genloop(count : aword;size : byte);
  1479. const
  1480. size2opsize : array[1..4] of tcgsize = (OS_8,OS_16,OS_NO,OS_32);
  1481. var
  1482. l : tasmlabel;
  1483. begin
  1484. current_asmdata.getjumplabel(l);
  1485. if count<size then size:=1;
  1486. a_load_const_reg(list,OS_INT,count div size,countreg);
  1487. cg.a_label(list,l);
  1488. srcref.addressmode:=AM_POSTINDEXED;
  1489. dstref.addressmode:=AM_POSTINDEXED;
  1490. srcref.offset:=size;
  1491. dstref.offset:=size;
  1492. r:=getintregister(list,size2opsize[size]);
  1493. a_load_ref_reg(list,size2opsize[size],size2opsize[size],srcref,r);
  1494. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,countreg,countreg,1),PF_S));
  1495. a_load_reg_ref(list,size2opsize[size],size2opsize[size],r,dstref);
  1496. a_jmp_flags(list,F_NE,l);
  1497. srcref.offset:=1;
  1498. dstref.offset:=1;
  1499. case count mod size of
  1500. 1:
  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. end;
  1505. 2:
  1506. if aligned then
  1507. begin
  1508. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  1509. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  1510. end
  1511. else
  1512. begin
  1513. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1514. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1515. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1516. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1517. end;
  1518. 3:
  1519. if aligned then
  1520. begin
  1521. srcref.offset:=2;
  1522. dstref.offset:=2;
  1523. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  1524. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  1525. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1526. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1527. end
  1528. else
  1529. begin
  1530. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1531. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1532. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1533. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1534. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1535. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1536. end;
  1537. end;
  1538. { keep the registers alive }
  1539. list.concat(taicpu.op_reg_reg(A_MOV,countreg,countreg));
  1540. list.concat(taicpu.op_reg_reg(A_MOV,srcreg,srcreg));
  1541. list.concat(taicpu.op_reg_reg(A_MOV,destreg,destreg));
  1542. end;
  1543. begin
  1544. if len=0 then
  1545. exit;
  1546. helpsize:=12+maxtmpreg*4;//52 with maxtmpreg=10
  1547. dstref:=dest;
  1548. srcref:=source;
  1549. if cs_opt_size in current_settings.optimizerswitches then
  1550. helpsize:=8;
  1551. if (len<=helpsize) and aligned then
  1552. begin
  1553. tmpregi:=0;
  1554. srcreg:=getintregister(list,OS_ADDR);
  1555. { explicit pc relative addressing, could be
  1556. e.g. a floating point constant }
  1557. if source.base=NR_PC then
  1558. begin
  1559. { ... then we don't need a loadaddr }
  1560. srcref:=source;
  1561. end
  1562. else
  1563. begin
  1564. a_loadaddr_ref_reg(list,source,srcreg);
  1565. reference_reset_base(srcref,srcreg,0);
  1566. end;
  1567. while (len div 4 <> 0) and (tmpregi<maxtmpreg) do
  1568. begin
  1569. inc(tmpregi);
  1570. tmpregisters[tmpregi]:=getintregister(list,OS_32);
  1571. a_load_ref_reg(list,OS_32,OS_32,srcref,tmpregisters[tmpregi]);
  1572. inc(srcref.offset,4);
  1573. dec(len,4);
  1574. end;
  1575. destreg:=getintregister(list,OS_ADDR);
  1576. a_loadaddr_ref_reg(list,dest,destreg);
  1577. reference_reset_base(dstref,destreg,0);
  1578. tmpregi2:=1;
  1579. while (tmpregi2<=tmpregi) do
  1580. begin
  1581. a_load_reg_ref(list,OS_32,OS_32,tmpregisters[tmpregi2],dstref);
  1582. inc(dstref.offset,4);
  1583. inc(tmpregi2);
  1584. end;
  1585. copysize:=4;
  1586. cgsize:=OS_32;
  1587. while len<>0 do
  1588. begin
  1589. if len<2 then
  1590. begin
  1591. copysize:=1;
  1592. cgsize:=OS_8;
  1593. end
  1594. else if len<4 then
  1595. begin
  1596. copysize:=2;
  1597. cgsize:=OS_16;
  1598. end;
  1599. dec(len,copysize);
  1600. r:=getintregister(list,cgsize);
  1601. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  1602. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  1603. inc(srcref.offset,copysize);
  1604. inc(dstref.offset,copysize);
  1605. end;{end of while}
  1606. end
  1607. else
  1608. begin
  1609. cgsize:=OS_32;
  1610. if (len<=4) then{len<=4 and not aligned}
  1611. begin
  1612. r:=getintregister(list,cgsize);
  1613. usedtmpref:=a_internal_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1614. if Len=1 then
  1615. a_load_reg_ref(list,OS_8,OS_8,r,dstref)
  1616. else
  1617. begin
  1618. tmpreg:=getintregister(list,cgsize);
  1619. usedtmpref2:=a_internal_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1620. inc(usedtmpref.offset,1);
  1621. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  1622. inc(usedtmpref2.offset,1);
  1623. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  1624. if len>2 then
  1625. begin
  1626. inc(usedtmpref.offset,1);
  1627. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  1628. inc(usedtmpref2.offset,1);
  1629. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  1630. if len>3 then
  1631. begin
  1632. inc(usedtmpref.offset,1);
  1633. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  1634. inc(usedtmpref2.offset,1);
  1635. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  1636. end;
  1637. end;
  1638. end;
  1639. end{end of if len<=4}
  1640. else
  1641. begin{unaligned & 4<len<helpsize **or** aligned/unaligned & len>helpsize}
  1642. destreg:=getintregister(list,OS_ADDR);
  1643. a_loadaddr_ref_reg(list,dest,destreg);
  1644. reference_reset_base(dstref,destreg,0);
  1645. srcreg:=getintregister(list,OS_ADDR);
  1646. a_loadaddr_ref_reg(list,source,srcreg);
  1647. reference_reset_base(srcref,srcreg,0);
  1648. countreg:=getintregister(list,OS_32);
  1649. // if cs_opt_size in current_settings.optimizerswitches then
  1650. { roozbeh : it seems loading 1 byte is faster becouse of caching/fetching(?) }
  1651. {if aligned then
  1652. genloop(len,4)
  1653. else}
  1654. genloop(len,1);
  1655. end;
  1656. end;
  1657. end;
  1658. procedure tcgarm.g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : aint);
  1659. begin
  1660. g_concatcopy_internal(list,source,dest,len,false);
  1661. end;
  1662. procedure tcgarm.g_concatcopy(list : TAsmList;const source,dest : treference;len : aint);
  1663. begin
  1664. if (source.alignment in [1..3]) or
  1665. (dest.alignment in [1..3]) then
  1666. g_concatcopy_internal(list,source,dest,len,false)
  1667. else
  1668. g_concatcopy_internal(list,source,dest,len,true);
  1669. end;
  1670. procedure tcgarm.g_overflowCheck(list : TAsmList;const l : tlocation;def : tdef);
  1671. var
  1672. ovloc : tlocation;
  1673. begin
  1674. ovloc.loc:=LOC_VOID;
  1675. g_overflowCheck_loc(list,l,def,ovloc);
  1676. end;
  1677. procedure tcgarm.g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);
  1678. var
  1679. hl : tasmlabel;
  1680. ai:TAiCpu;
  1681. hflags : tresflags;
  1682. begin
  1683. if not(cs_check_overflow in current_settings.localswitches) then
  1684. exit;
  1685. current_asmdata.getjumplabel(hl);
  1686. case ovloc.loc of
  1687. LOC_VOID:
  1688. begin
  1689. ai:=taicpu.op_sym(A_B,hl);
  1690. ai.is_jmp:=true;
  1691. if not((def.typ=pointerdef) or
  1692. ((def.typ=orddef) and
  1693. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,pasbool]))) then
  1694. ai.SetCondition(C_VC)
  1695. else
  1696. if TAiCpu(List.Last).opcode in [A_RSB,A_RSC,A_SBC,A_SUB] then
  1697. ai.SetCondition(C_CS)
  1698. else
  1699. ai.SetCondition(C_CC);
  1700. list.concat(ai);
  1701. end;
  1702. LOC_FLAGS:
  1703. begin
  1704. hflags:=ovloc.resflags;
  1705. inverse_flags(hflags);
  1706. cg.a_jmp_flags(list,hflags,hl);
  1707. end;
  1708. else
  1709. internalerror(200409281);
  1710. end;
  1711. a_call_name(list,'FPC_OVERFLOW');
  1712. a_label(list,hl);
  1713. end;
  1714. procedure tcgarm.g_save_registers(list : TAsmList);
  1715. begin
  1716. { this work is done in g_proc_entry }
  1717. end;
  1718. procedure tcgarm.g_restore_registers(list : TAsmList);
  1719. begin
  1720. { this work is done in g_proc_exit }
  1721. end;
  1722. procedure tcgarm.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1723. var
  1724. ai : taicpu;
  1725. begin
  1726. ai:=Taicpu.Op_sym(A_B,l);
  1727. ai.SetCondition(OpCmp2AsmCond[cond]);
  1728. ai.is_jmp:=true;
  1729. list.concat(ai);
  1730. end;
  1731. procedure tcgarm.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  1732. procedure loadvmttor12;
  1733. var
  1734. href : treference;
  1735. begin
  1736. reference_reset_base(href,NR_R0,0);
  1737. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
  1738. end;
  1739. procedure op_onr12methodaddr;
  1740. var
  1741. href : treference;
  1742. begin
  1743. if (procdef.extnumber=$ffff) then
  1744. Internalerror(200006139);
  1745. { call/jmp vmtoffs(%eax) ; method offs }
  1746. reference_reset_base(href,NR_R12,procdef._class.vmtmethodoffset(procdef.extnumber));
  1747. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
  1748. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R12));
  1749. end;
  1750. var
  1751. make_global : boolean;
  1752. begin
  1753. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  1754. Internalerror(200006137);
  1755. if not assigned(procdef._class) or
  1756. (procdef.procoptions*[po_classmethod, po_staticmethod,
  1757. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  1758. Internalerror(200006138);
  1759. if procdef.owner.symtabletype<>ObjectSymtable then
  1760. Internalerror(200109191);
  1761. make_global:=false;
  1762. if (not current_module.is_unit) or
  1763. create_smartlink or
  1764. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  1765. make_global:=true;
  1766. if make_global then
  1767. list.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  1768. else
  1769. list.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  1770. { set param1 interface to self }
  1771. g_adjust_self_value(list,procdef,ioffset);
  1772. { case 4 }
  1773. if po_virtualmethod in procdef.procoptions then
  1774. begin
  1775. loadvmttor12;
  1776. op_onr12methodaddr;
  1777. end
  1778. { case 0 }
  1779. else
  1780. list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(procdef.mangledname)));
  1781. list.concat(Tai_symbol_end.Createname(labelname));
  1782. end;
  1783. procedure tcgarm.maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  1784. const
  1785. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  1786. begin
  1787. if (op in overflowops) and
  1788. (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  1789. a_load_reg_reg(list,OS_32,size,dst,dst);
  1790. end;
  1791. procedure tcg64farm.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  1792. begin
  1793. case op of
  1794. OP_NEG:
  1795. begin
  1796. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSB,regdst.reglo,regsrc.reglo,0),PF_S));
  1797. list.concat(taicpu.op_reg_reg_const(A_RSC,regdst.reghi,regsrc.reghi,0));
  1798. end;
  1799. OP_NOT:
  1800. begin
  1801. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reglo,regdst.reglo);
  1802. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reghi,regdst.reghi);
  1803. end;
  1804. else
  1805. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  1806. end;
  1807. end;
  1808. procedure tcg64farm.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  1809. begin
  1810. a_op64_const_reg_reg(list,op,size,value,reg,reg);
  1811. end;
  1812. procedure tcg64farm.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);
  1813. var
  1814. ovloc : tlocation;
  1815. begin
  1816. a_op64_const_reg_reg_checkoverflow(list,op,size,value,regsrc,regdst,false,ovloc);
  1817. end;
  1818. procedure tcg64farm.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  1819. var
  1820. ovloc : tlocation;
  1821. begin
  1822. a_op64_reg_reg_reg_checkoverflow(list,op,size,regsrc1,regsrc2,regdst,false,ovloc);
  1823. end;
  1824. procedure tcg64farm.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  1825. var
  1826. tmpreg : tregister;
  1827. b : byte;
  1828. begin
  1829. ovloc.loc:=LOC_VOID;
  1830. case op of
  1831. OP_NEG,
  1832. OP_NOT :
  1833. internalerror(200306017);
  1834. end;
  1835. if (setflags or tcgarm(cg).cgsetflags) and (op in [OP_ADD,OP_SUB]) then
  1836. begin
  1837. case op of
  1838. OP_ADD:
  1839. begin
  1840. if is_shifter_const(lo(value),b) then
  1841. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  1842. else
  1843. begin
  1844. tmpreg:=cg.getintregister(list,OS_32);
  1845. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  1846. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  1847. end;
  1848. if is_shifter_const(hi(value),b) then
  1849. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,hi(value)),PF_S))
  1850. else
  1851. begin
  1852. tmpreg:=cg.getintregister(list,OS_32);
  1853. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  1854. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg),PF_S));
  1855. end;
  1856. end;
  1857. OP_SUB:
  1858. begin
  1859. if is_shifter_const(lo(value),b) then
  1860. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  1861. else
  1862. begin
  1863. tmpreg:=cg.getintregister(list,OS_32);
  1864. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  1865. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  1866. end;
  1867. if is_shifter_const(hi(value),b) then
  1868. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SBC,regdst.reghi,regsrc.reghi,aint(hi(value))),PF_S))
  1869. else
  1870. begin
  1871. tmpreg:=cg.getintregister(list,OS_32);
  1872. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  1873. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg),PF_S));
  1874. end;
  1875. end;
  1876. else
  1877. internalerror(200502131);
  1878. end;
  1879. if size=OS_64 then
  1880. begin
  1881. { the arm has an weired opinion how flags for SUB/ADD are handled }
  1882. ovloc.loc:=LOC_FLAGS;
  1883. case op of
  1884. OP_ADD:
  1885. ovloc.resflags:=F_CS;
  1886. OP_SUB:
  1887. ovloc.resflags:=F_CC;
  1888. end;
  1889. end;
  1890. end
  1891. else
  1892. begin
  1893. case op of
  1894. OP_AND,OP_OR,OP_XOR:
  1895. begin
  1896. cg.a_op_const_reg_reg(list,op,OS_32,aint(lo(value)),regsrc.reglo,regdst.reglo);
  1897. cg.a_op_const_reg_reg(list,op,OS_32,aint(hi(value)),regsrc.reghi,regdst.reghi);
  1898. end;
  1899. OP_ADD:
  1900. begin
  1901. if is_shifter_const(aint(lo(value)),b) then
  1902. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,aint(lo(value))),PF_S))
  1903. else
  1904. begin
  1905. tmpreg:=cg.getintregister(list,OS_32);
  1906. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  1907. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  1908. end;
  1909. if is_shifter_const(aint(hi(value)),b) then
  1910. list.concat(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,aint(hi(value))))
  1911. else
  1912. begin
  1913. tmpreg:=cg.getintregister(list,OS_32);
  1914. cg.a_load_const_reg(list,OS_32,aint(hi(value)),tmpreg);
  1915. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg));
  1916. end;
  1917. end;
  1918. OP_SUB:
  1919. begin
  1920. if is_shifter_const(aint(lo(value)),b) then
  1921. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,regdst.reglo,regsrc.reglo,aint(lo(value))),PF_S))
  1922. else
  1923. begin
  1924. tmpreg:=cg.getintregister(list,OS_32);
  1925. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  1926. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  1927. end;
  1928. if is_shifter_const(aint(hi(value)),b) then
  1929. list.concat(taicpu.op_reg_reg_const(A_SBC,regdst.reghi,regsrc.reghi,aint(hi(value))))
  1930. else
  1931. begin
  1932. tmpreg:=cg.getintregister(list,OS_32);
  1933. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  1934. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg));
  1935. end;
  1936. end;
  1937. else
  1938. internalerror(2003083101);
  1939. end;
  1940. end;
  1941. end;
  1942. procedure tcg64farm.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  1943. begin
  1944. ovloc.loc:=LOC_VOID;
  1945. case op of
  1946. OP_NEG,
  1947. OP_NOT :
  1948. internalerror(200306017);
  1949. end;
  1950. if (setflags or tcgarm(cg).cgsetflags) and (op in [OP_ADD,OP_SUB]) then
  1951. begin
  1952. case op of
  1953. OP_ADD:
  1954. begin
  1955. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  1956. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi),PF_S));
  1957. end;
  1958. OP_SUB:
  1959. begin
  1960. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  1961. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi),PF_S));
  1962. end;
  1963. else
  1964. internalerror(2003083101);
  1965. end;
  1966. if size=OS_64 then
  1967. begin
  1968. { the arm has an weired opinion how flags for SUB/ADD are handled }
  1969. ovloc.loc:=LOC_FLAGS;
  1970. case op of
  1971. OP_ADD:
  1972. ovloc.resflags:=F_CS;
  1973. OP_SUB:
  1974. ovloc.resflags:=F_CC;
  1975. end;
  1976. end;
  1977. end
  1978. else
  1979. begin
  1980. case op of
  1981. OP_AND,OP_OR,OP_XOR:
  1982. begin
  1983. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  1984. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  1985. end;
  1986. OP_ADD:
  1987. begin
  1988. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  1989. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  1990. end;
  1991. OP_SUB:
  1992. begin
  1993. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  1994. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi));
  1995. end;
  1996. else
  1997. internalerror(2003083101);
  1998. end;
  1999. end;
  2000. end;
  2001. begin
  2002. cg:=tcgarm.create;
  2003. cg64:=tcg64farm.create;
  2004. end.