cgcpu.pas 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321
  1. {
  2. Copyright (c) 2014 by Jonas Maebe
  3. This unit implements the code generator for AArch64
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cgcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,parabase,
  22. cgbase,cgutils,cgobj,
  23. aasmbase,aasmtai,aasmdata,aasmcpu,
  24. cpubase,cpuinfo,
  25. node,symconst,SymType,symdef,
  26. rgcpu;
  27. type
  28. tcgaarch64=class(tcg)
  29. protected
  30. { changes register size without adding register allocation info }
  31. function makeregsize(reg: tregister; size: tcgsize): tregister; overload;
  32. public
  33. { simplifies "ref" so it can be used with "op". If "ref" can be used
  34. with a different load/Store operation that has the same meaning as the
  35. original one, "op" will be replaced with the alternative }
  36. procedure make_simple_ref(list:TAsmList; var op: tasmop; size: tcgsize; oppostfix: toppostfix; var ref: treference; preferred_newbasereg: tregister);
  37. function getfpuregister(list: TAsmList; size: Tcgsize): Tregister; override;
  38. procedure handle_reg_imm12_reg(list: TAsmList; op: Tasmop; size: tcgsize; src: tregister; a: tcgint; dst: tregister; tmpreg: tregister; setflags, usedest: boolean);
  39. procedure init_register_allocators;override;
  40. procedure done_register_allocators;override;
  41. function getmmregister(list:TAsmList;size:tcgsize):tregister;override;
  42. function handle_load_store(list:TAsmList; op: tasmop; size: tcgsize; oppostfix: toppostfix; reg: tregister; ref: treference):treference;
  43. procedure a_call_name(list:TAsmList;const s:string; weak: boolean);override;
  44. procedure a_call_reg(list:TAsmList;Reg:tregister);override;
  45. { General purpose instructions }
  46. procedure maybeadjustresult(list: TAsmList; op: topcg; size: tcgsize; dst: tregister);
  47. procedure a_op_const_reg(list: TAsmList; op: topcg; size: tcgsize; a: tcgint; reg: tregister);override;
  48. procedure a_op_reg_reg(list: TAsmList; op: topcg; size: tcgsize; src, dst: tregister);override;
  49. procedure a_op_const_reg_reg(list: TAsmList; op: topcg; size: tcgsize; a: tcgint; src, dst: tregister);override;
  50. procedure a_op_reg_reg_reg(list: TAsmList; op: topcg; size: tcgsize; src1, src2, dst: tregister);override;
  51. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: topcg; size: tcgsize; a: tcgint; src, dst: tregister; setflags : boolean; var ovloc : tlocation);override;
  52. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: topcg; size: tcgsize; src1, src2, dst: tregister; setflags : boolean; var ovloc : tlocation);override;
  53. { move instructions }
  54. procedure a_load_const_reg(list: TAsmList; size: tcgsize; a: tcgint; reg: tregister);override;
  55. procedure a_load_const_ref(list: TAsmList; size: tcgsize; a: tcgint; const ref: treference); override;
  56. procedure a_load_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister;const ref: TReference);override;
  57. procedure a_load_reg_ref_unaligned(list: TAsmList; fromsize, tosize: tcgsize; register: tregister; const ref: treference); override;
  58. procedure a_load_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: TReference; reg: tregister);override;
  59. procedure a_load_ref_reg_unaligned(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; register: tregister); override;
  60. procedure a_load_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);override;
  61. procedure a_loadaddr_ref_reg(list: TAsmList; const ref: TReference; r: tregister);override;
  62. { fpu move instructions (not used, all floating point is vector unit-based) }
  63. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  64. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); override;
  65. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  66. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister;shuffle : pmmshuffle);override;
  67. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: TReference; reg: tregister; shuffle: pmmshuffle);override;
  68. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: TReference; shuffle: pmmshuffle);override;
  69. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle); override;
  70. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tcgsize; mmreg, intreg: tregister; shuffle: pmmshuffle); override;
  71. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tcgsize; src, dst: tregister; shuffle: pmmshuffle); override;
  72. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tcgsize; src, dst: TRegister); override;
  73. { comparison operations }
  74. procedure a_cmp_const_reg_label(list: TAsmList; size: tcgsize; cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);override;
  75. procedure a_cmp_reg_reg_label(list: TAsmList; size: tcgsize; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);override;
  76. procedure a_jmp_always(list: TAsmList; l: TAsmLabel);override;
  77. procedure a_jmp_name(list: TAsmList; const s: string);override;
  78. procedure a_jmp_cond(list: TAsmList; cond: TOpCmp; l: tasmlabel);{ override;}
  79. procedure a_jmp_flags(list: TAsmList; const f: tresflags; l: tasmlabel);override;
  80. procedure g_flags2reg(list: TAsmList; size: tcgsize; const f:tresflags; reg: tregister);override;
  81. procedure g_overflowcheck(list: TAsmList; const loc: tlocation; def: tdef);override;
  82. procedure g_overflowcheck_loc(list: TAsmList; const loc: tlocation; def: tdef; ovloc: tlocation);override;
  83. procedure g_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean);override;
  84. procedure g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean);override;
  85. procedure g_maybe_got_init(list: TAsmList); override;
  86. procedure g_restore_registers(list: TAsmList);override;
  87. procedure g_save_registers(list: TAsmList);override;
  88. procedure g_concatcopy_move(list: TAsmList; const source, dest: treference; len: tcgint);
  89. procedure g_concatcopy(list: TAsmList; const source, dest: treference; len: tcgint);override;
  90. procedure g_adjust_self_value(list: TAsmList; procdef: tprocdef; ioffset: tcgint);override;
  91. procedure g_check_for_fpu_exception(list: TAsmList; force, clear: boolean);override;
  92. private
  93. function save_regs(list: TAsmList; rt: tregistertype; lowsr, highsr: tsuperregister; sub: tsubregister): longint;
  94. procedure load_regs(list: TAsmList; rt: tregistertype; lowsr, highsr: tsuperregister; sub: tsubregister);
  95. end;
  96. procedure create_codegen;
  97. const
  98. TOpCG2AsmOpReg: array[topcg] of TAsmOp = (
  99. A_NONE,A_MOV,A_ADD,A_AND,A_UDIV,A_SDIV,A_MUL,A_MUL,A_NEG,A_MVN,A_ORR,A_ASRV,A_LSLV,A_LSRV,A_SUB,A_EOR,A_NONE,A_RORV
  100. );
  101. TOpCG2AsmOpImm: array[topcg] of TAsmOp = (
  102. A_NONE,A_MOV,A_ADD,A_AND,A_UDIV,A_SDIV,A_MUL,A_MUL,A_NEG,A_MVN,A_ORR,A_ASR,A_LSL,A_LSR,A_SUB,A_EOR,A_NONE,A_ROR
  103. );
  104. TOpCmp2AsmCond: array[topcmp] of TAsmCond = (C_NONE,C_EQ,C_GT,
  105. C_LT,C_GE,C_LE,C_NE,C_LS,C_CC,C_CS,C_HI
  106. );
  107. implementation
  108. uses
  109. globals,verbose,systems,cutils,
  110. paramgr,fmodule,
  111. symtable,symsym,
  112. tgobj,
  113. procinfo,cpupi;
  114. procedure tcgaarch64.make_simple_ref(list:TAsmList; var op: tasmop; size: tcgsize; oppostfix: toppostfix; var ref: treference; preferred_newbasereg: tregister);
  115. var
  116. href: treference;
  117. so: tshifterop;
  118. accesssize: longint;
  119. begin
  120. if (ref.base=NR_NO) then
  121. begin
  122. if ref.shiftmode<>SM_None then
  123. internalerror(2014110701);
  124. ref.base:=ref.index;
  125. ref.index:=NR_NO;
  126. end;
  127. { no abitrary scale factor support (the generic code doesn't set it,
  128. AArch-specific code shouldn't either) }
  129. if not(ref.scalefactor in [0,1]) then
  130. internalerror(2014111002);
  131. case simple_ref_type(op,size,oppostfix,ref) of
  132. sr_simple:
  133. exit;
  134. sr_internal_illegal:
  135. internalerror(2014121702);
  136. sr_complex:
  137. { continue } ;
  138. end;
  139. if assigned(ref.symbol) then
  140. begin
  141. { internal "load symbol" instructions should already be valid }
  142. if assigned(ref.symboldata) or
  143. (ref.refaddr in [addr_pic,addr_gotpage,addr_gotpageoffset,addr_page,addr_pageoffset]) then
  144. internalerror(2014110802);
  145. { no relative symbol support (needed) yet }
  146. if assigned(ref.relsymbol) then
  147. internalerror(2014111001);
  148. { loading a symbol address (whether it's in the GOT or not) consists
  149. of two parts: first load the page on which it is located, then
  150. either the offset in the page or load the value at that offset in
  151. the page. This final GOT-load can be relaxed by the linker in case
  152. the variable itself can be stored directly in the GOT }
  153. if (preferred_newbasereg=NR_NO) or
  154. (ref.base=preferred_newbasereg) or
  155. (ref.index=preferred_newbasereg) then
  156. preferred_newbasereg:=getaddressregister(list);
  157. { load the (GOT) page }
  158. reference_reset_symbol(href,ref.symbol,0,8,[]);
  159. if ((ref.symbol.typ in [AT_FUNCTION,AT_LABEL]) and
  160. (ref.symbol.bind in [AB_LOCAL,AB_GLOBAL])) or
  161. ((ref.symbol.typ=AT_DATA) and
  162. (ref.symbol.bind=AB_LOCAL)) then
  163. href.refaddr:=addr_page
  164. else
  165. href.refaddr:=addr_gotpage;
  166. list.concat(taicpu.op_reg_ref(A_ADRP,preferred_newbasereg,href));
  167. { load the GOT entry (= address of the variable) }
  168. reference_reset_base(href,preferred_newbasereg,0,ctempposinvalid,sizeof(pint),[]);
  169. href.symbol:=ref.symbol;
  170. { code symbols defined in the current compilation unit do not
  171. have to be accessed via the GOT }
  172. if ((ref.symbol.typ in [AT_FUNCTION,AT_LABEL]) and
  173. (ref.symbol.bind in [AB_LOCAL,AB_GLOBAL])) or
  174. ((ref.symbol.typ=AT_DATA) and
  175. (ref.symbol.bind=AB_LOCAL)) then
  176. begin
  177. href.base:=NR_NO;
  178. href.refaddr:=addr_pageoffset;
  179. list.concat(taicpu.op_reg_reg_ref(A_ADD,preferred_newbasereg,preferred_newbasereg,href));
  180. end
  181. else
  182. begin
  183. href.refaddr:=addr_gotpageoffset;
  184. { use a_load_ref_reg() rather than directly encoding the LDR,
  185. so that we'll check the validity of the reference }
  186. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,preferred_newbasereg);
  187. end;
  188. { set as new base register }
  189. if ref.base=NR_NO then
  190. ref.base:=preferred_newbasereg
  191. else if ref.index=NR_NO then
  192. ref.index:=preferred_newbasereg
  193. else
  194. begin
  195. { make sure it's valid in case ref.base is SP -> make it
  196. the second operand}
  197. a_op_reg_reg_reg(list,OP_ADD,OS_ADDR,preferred_newbasereg,ref.base,preferred_newbasereg);
  198. ref.base:=preferred_newbasereg
  199. end;
  200. ref.symbol:=nil;
  201. end;
  202. { base & index }
  203. if (ref.base<>NR_NO) and
  204. (ref.index<>NR_NO) then
  205. begin
  206. case op of
  207. A_LDR, A_STR:
  208. begin
  209. if (ref.shiftmode=SM_None) and
  210. (ref.shiftimm<>0) then
  211. internalerror(2014110805);
  212. { wrong shift? (possible in case of something like
  213. array_of_2byte_rec[x].bytefield -> shift will be set 1, but
  214. the final load is a 1 byte -> can't use shift after all }
  215. if (ref.shiftmode in [SM_LSL,SM_UXTW,SM_SXTW]) and
  216. ((ref.shiftimm<>BsfDWord(tcgsizep2size[size])) or
  217. (ref.offset<>0)) then
  218. begin
  219. if preferred_newbasereg=NR_NO then
  220. preferred_newbasereg:=getaddressregister(list);
  221. { "add" supports a superset of the shift modes supported by
  222. load/store instructions }
  223. shifterop_reset(so);
  224. so.shiftmode:=ref.shiftmode;
  225. so.shiftimm:=ref.shiftimm;
  226. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,preferred_newbasereg,ref.base,ref.index,so));
  227. reference_reset_base(ref,preferred_newbasereg,ref.offset,ref.temppos,ref.alignment,ref.volatility);
  228. { possibly still an invalid offset -> fall through }
  229. end
  230. else if ref.offset<>0 then
  231. begin
  232. if (preferred_newbasereg=NR_NO) or
  233. { we keep ref.index, so it must not be overwritten }
  234. (ref.index=preferred_newbasereg) then
  235. preferred_newbasereg:=getaddressregister(list);
  236. { add to the base and not to the index, because the index
  237. may be scaled; this works even if the base is SP }
  238. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,ref.base,preferred_newbasereg);
  239. ref.offset:=0;
  240. ref.base:=preferred_newbasereg;
  241. { finished }
  242. exit;
  243. end
  244. else
  245. { valid -> exit }
  246. exit;
  247. end;
  248. { todo }
  249. A_LD1,A_LD2,A_LD3,A_LD4,
  250. A_ST1,A_ST2,A_ST3,A_ST4:
  251. internalerror(2014110704);
  252. { these don't support base+index }
  253. A_LDUR,A_STUR,
  254. A_LDP,A_STP:
  255. begin
  256. { these either don't support pre-/post-indexing, or don't
  257. support it with base+index }
  258. if ref.addressmode<>AM_OFFSET then
  259. internalerror(2014110911);
  260. if preferred_newbasereg=NR_NO then
  261. preferred_newbasereg:=getaddressregister(list);
  262. if ref.shiftmode<>SM_None then
  263. begin
  264. { "add" supports a superset of the shift modes supported by
  265. load/store instructions }
  266. shifterop_reset(so);
  267. so.shiftmode:=ref.shiftmode;
  268. so.shiftimm:=ref.shiftimm;
  269. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,preferred_newbasereg,ref.base,ref.index,so));
  270. end
  271. else
  272. a_op_reg_reg_reg(list,OP_ADD,OS_ADDR,ref.index,ref.base,preferred_newbasereg);
  273. reference_reset_base(ref,preferred_newbasereg,ref.offset,ref.temppos,ref.alignment,ref.volatility);
  274. { fall through to the handling of base + offset, since the
  275. offset may still be too big }
  276. end;
  277. else
  278. internalerror(2014110901);
  279. end;
  280. end;
  281. { base + offset }
  282. if ref.base<>NR_NO then
  283. begin
  284. { valid offset for LDUR/STUR -> use that }
  285. if (ref.addressmode=AM_OFFSET) and
  286. (op in [A_LDR,A_STR]) and
  287. (ref.offset>=-256) and
  288. (ref.offset<=255) then
  289. begin
  290. if op=A_LDR then
  291. op:=A_LDUR
  292. else
  293. op:=A_STUR
  294. end
  295. { if it's not a valid LDUR/STUR, use LDR/STR }
  296. else if (op in [A_LDUR,A_STUR]) and
  297. ((ref.offset<-256) or
  298. (ref.offset>255) or
  299. (ref.addressmode<>AM_OFFSET)) then
  300. begin
  301. if op=A_LDUR then
  302. op:=A_LDR
  303. else
  304. op:=A_STR
  305. end;
  306. case op of
  307. A_LDR,A_STR:
  308. begin
  309. case ref.addressmode of
  310. AM_PREINDEXED:
  311. begin
  312. { since the loaded/stored register cannot be the same
  313. as the base register, we can safely add the
  314. offset to the base if it doesn't fit}
  315. if (ref.offset<-256) or
  316. (ref.offset>255) then
  317. begin
  318. a_op_const_reg(list,OP_ADD,OS_ADDR,ref.offset,ref.base);
  319. ref.offset:=0;
  320. end;
  321. end;
  322. AM_POSTINDEXED:
  323. begin
  324. { cannot emulate post-indexing if we have to fold the
  325. offset into the base register }
  326. if (ref.offset<-256) or
  327. (ref.offset>255) then
  328. internalerror(2014110909);
  329. { ok }
  330. end;
  331. AM_OFFSET:
  332. begin
  333. { unsupported offset -> fold into base register }
  334. accesssize:=1 shl tcgsizep2size[size];
  335. if (ref.offset<0) or
  336. (ref.offset>(((1 shl 12)-1)*accesssize)) or
  337. ((ref.offset mod accesssize)<>0) then
  338. begin
  339. if preferred_newbasereg=NR_NO then
  340. preferred_newbasereg:=getaddressregister(list);
  341. { can we split the offset beween an
  342. "add/sub (imm12 shl 12)" and the load (also an
  343. imm12)?
  344. -- the offset from the load will always be added,
  345. that's why the lower bound has a smaller range
  346. than the upper bound; it must also be a multiple
  347. of the access size }
  348. if (ref.offset>=-(((1 shl 12)-1) shl 12)) and
  349. (ref.offset<=((1 shl 12)-1) shl 12 + ((1 shl 12)-1)) and
  350. ((ref.offset mod accesssize)=0) then
  351. begin
  352. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,(ref.offset shr 12) shl 12,ref.base,preferred_newbasereg);
  353. ref.offset:=ref.offset-(ref.offset shr 12) shl 12;
  354. end
  355. else
  356. begin
  357. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,ref.base,preferred_newbasereg);
  358. ref.offset:=0;
  359. end;
  360. reference_reset_base(ref,preferred_newbasereg,ref.offset,ref.temppos,ref.alignment,ref.volatility);
  361. end;
  362. end
  363. else
  364. internalerror(2014110904);
  365. end;
  366. end;
  367. A_LDP,A_STP:
  368. begin
  369. { unsupported offset -> fold into base register (these
  370. instructions support all addressmodes) }
  371. if (ref.offset<-(1 shl (6+tcgsizep2size[size]))) or
  372. (ref.offset>(1 shl (6+tcgsizep2size[size]))-1) then
  373. begin
  374. case ref.addressmode of
  375. AM_POSTINDEXED:
  376. { don't emulate post-indexing if we have to fold the
  377. offset into the base register }
  378. internalerror(2014110910);
  379. AM_PREINDEXED:
  380. { this means the offset must be added to the current
  381. base register }
  382. preferred_newbasereg:=ref.base;
  383. AM_OFFSET:
  384. if preferred_newbasereg=NR_NO then
  385. preferred_newbasereg:=getaddressregister(list);
  386. end;
  387. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,ref.base,preferred_newbasereg);
  388. reference_reset_base(ref,preferred_newbasereg,0,ref.temppos,ref.alignment,ref.volatility);
  389. end
  390. end;
  391. A_LDUR,A_STUR:
  392. begin
  393. { valid, checked above }
  394. end;
  395. { todo }
  396. A_LD1,A_LD2,A_LD3,A_LD4,
  397. A_ST1,A_ST2,A_ST3,A_ST4:
  398. internalerror(2014110908);
  399. else
  400. internalerror(2014110708);
  401. end;
  402. { done }
  403. exit;
  404. end;
  405. { only an offset -> change to base (+ offset 0) }
  406. if preferred_newbasereg=NR_NO then
  407. preferred_newbasereg:=getaddressregister(list);
  408. a_load_const_reg(list,OS_ADDR,ref.offset,preferred_newbasereg);
  409. reference_reset_base(ref,preferred_newbasereg,0,ref.temppos,newalignment(8,ref.offset),ref.volatility);
  410. end;
  411. function tcgaarch64.makeregsize(reg: tregister; size: tcgsize): tregister;
  412. var
  413. subreg:Tsubregister;
  414. begin
  415. subreg:=cgsize2subreg(getregtype(reg),size);
  416. result:=reg;
  417. setsubreg(result,subreg);
  418. end;
  419. function tcgaarch64.getfpuregister(list: TAsmList; size: Tcgsize): Tregister;
  420. begin
  421. internalerror(2014122110);
  422. { squash warning }
  423. result:=NR_NO;
  424. end;
  425. function tcgaarch64.handle_load_store(list: TAsmList; op: tasmop; size: tcgsize; oppostfix: toppostfix; reg: tregister; ref: treference):treference;
  426. begin
  427. make_simple_ref(list,op,size,oppostfix,ref,NR_NO);
  428. list.concat(setoppostfix(taicpu.op_reg_ref(op,reg,ref),oppostfix));
  429. result:=ref;
  430. end;
  431. procedure tcgaarch64.handle_reg_imm12_reg(list: TAsmList; op: Tasmop; size: tcgsize; src: tregister; a: tcgint; dst: tregister; tmpreg: tregister; setflags, usedest: boolean);
  432. var
  433. instr: taicpu;
  434. so: tshifterop;
  435. hadtmpreg: boolean;
  436. begin
  437. { imm12 }
  438. if (a>=0) and
  439. (a<=((1 shl 12)-1)) then
  440. if usedest then
  441. instr:=taicpu.op_reg_reg_const(op,dst,src,a)
  442. else
  443. instr:=taicpu.op_reg_const(op,src,a)
  444. { imm12 lsl 12 }
  445. else if (a and not(((tcgint(1) shl 12)-1) shl 12))=0 then
  446. begin
  447. so.shiftmode:=SM_LSL;
  448. so.shiftimm:=12;
  449. if usedest then
  450. instr:=taicpu.op_reg_reg_const_shifterop(op,dst,src,a shr 12,so)
  451. else
  452. instr:=taicpu.op_reg_const_shifterop(op,src,a shr 12,so)
  453. end
  454. else
  455. begin
  456. { todo: other possible optimizations (e.g. load 16 bit constant in
  457. register and then add/sub/cmp/cmn shifted the rest) }
  458. if tmpreg=NR_NO then
  459. begin
  460. hadtmpreg:=false;
  461. tmpreg:=getintregister(list,size);
  462. end
  463. else
  464. begin
  465. hadtmpreg:=true;
  466. getcpuregister(list,tmpreg);
  467. end;
  468. a_load_const_reg(list,size,a,tmpreg);
  469. if usedest then
  470. instr:=taicpu.op_reg_reg_reg(op,dst,src,tmpreg)
  471. else
  472. instr:=taicpu.op_reg_reg(op,src,tmpreg);
  473. if hadtmpreg then
  474. ungetcpuregister(list,tmpreg);
  475. end;
  476. if setflags then
  477. setoppostfix(instr,PF_S);
  478. list.concat(instr);
  479. end;
  480. {****************************************************************************
  481. Assembler code
  482. ****************************************************************************}
  483. procedure tcgaarch64.init_register_allocators;
  484. begin
  485. inherited init_register_allocators;
  486. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  487. [RS_X0,RS_X1,RS_X2,RS_X3,RS_X4,RS_X5,RS_X6,RS_X7,RS_X8,
  488. RS_X9,RS_X10,RS_X11,RS_X12,RS_X13,RS_X14,RS_X15,RS_X16,RS_X17,
  489. RS_X19,RS_X20,RS_X21,RS_X22,RS_X23,RS_X24,RS_X25,RS_X26,RS_X27,RS_X28
  490. { maybe we can enable this in the future for leaf functions (it's
  491. the frame pointer)
  492. ,RS_X29 }],
  493. first_int_imreg,[]);
  494. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBMMD,
  495. [RS_Q0,RS_Q1,RS_Q2,RS_Q3,RS_Q4,RS_Q5,RS_Q6,RS_Q7,
  496. RS_Q8,RS_Q9,RS_Q10,RS_Q11,RS_Q12,RS_Q13,RS_Q14,RS_Q15,
  497. RS_Q16,RS_Q17,RS_Q18,RS_Q19,RS_Q20,RS_Q21,RS_Q22,RS_Q23,
  498. RS_Q24,RS_Q25,RS_Q26,RS_Q27,RS_Q28,RS_Q29,RS_Q30,RS_Q31],
  499. first_mm_imreg,[]);
  500. end;
  501. procedure tcgaarch64.done_register_allocators;
  502. begin
  503. rg[R_INTREGISTER].free;
  504. rg[R_FPUREGISTER].free;
  505. rg[R_MMREGISTER].free;
  506. inherited done_register_allocators;
  507. end;
  508. function tcgaarch64.getmmregister(list: TAsmList; size: tcgsize):tregister;
  509. begin
  510. case size of
  511. OS_F32:
  512. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMS);
  513. OS_F64:
  514. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMD)
  515. else
  516. internalerror(2014102701);
  517. end;
  518. end;
  519. procedure tcgaarch64.a_call_name(list: TAsmList; const s: string; weak: boolean);
  520. begin
  521. if not weak then
  522. list.concat(taicpu.op_sym(A_BL,current_asmdata.RefAsmSymbol(s,AT_FUNCTION)))
  523. else
  524. list.concat(taicpu.op_sym(A_BL,current_asmdata.WeakRefAsmSymbol(s,AT_FUNCTION)));
  525. end;
  526. procedure tcgaarch64.a_call_reg(list:TAsmList;Reg:tregister);
  527. begin
  528. list.concat(taicpu.op_reg(A_BLR,reg));
  529. end;
  530. {********************** load instructions ********************}
  531. procedure tcgaarch64.a_load_const_reg(list: TAsmList; size: tcgsize; a: tcgint; reg : tregister);
  532. var
  533. opc: tasmop;
  534. shift: byte;
  535. so: tshifterop;
  536. reginited,doinverted: boolean;
  537. manipulated_a: tcgint;
  538. leftover_a: word;
  539. begin
  540. case a of
  541. { Small positive number }
  542. $0..$FFFF:
  543. begin
  544. list.concat(taicpu.op_reg_const(A_MOVZ, reg, a));
  545. Exit;
  546. end;
  547. { Small negative number }
  548. -65536..-1:
  549. begin
  550. list.concat(taicpu.op_reg_const(A_MOVN, reg, Word(not a)));
  551. Exit;
  552. end;
  553. { Can be represented as a negative number more compactly }
  554. $FFFF0000..$FFFFFFFF:
  555. begin
  556. { if we load a value into a 32 bit register, it is automatically
  557. zero-extended to 64 bit }
  558. list.concat(taicpu.op_reg_const(A_MOVN, makeregsize(reg,OS_32), Word(not a)));
  559. Exit;
  560. end;
  561. else
  562. begin
  563. if size in [OS_64,OS_S64] then
  564. begin
  565. { Check to see if a is a valid shifter constant that can be encoded in ORR as is }
  566. if is_shifter_const(a,size) then
  567. begin
  568. list.concat(taicpu.op_reg_reg_const(A_ORR,reg,makeregsize(NR_XZR,size),a));
  569. Exit;
  570. end;
  571. { This determines whether this write can be peformed with an ORR followed by MOVK
  572. by copying the 2nd word to the 4th word for the ORR constant, then overwriting
  573. the 4th word (unless the word is. The alternative would require 3 instructions }
  574. leftover_a := word(a shr 48);
  575. manipulated_a := (a and $0000FFFFFFFFFFFF);
  576. if manipulated_a = $0000FFFFFFFFFFFF then
  577. begin
  578. { This is even better, as we can just use a single MOVN on the last word }
  579. shifterop_reset(so);
  580. so.shiftmode := SM_LSL;
  581. so.shiftimm := 48;
  582. list.concat(taicpu.op_reg_const_shifterop(A_MOVN, reg, word(not leftover_a), so));
  583. Exit;
  584. end;
  585. manipulated_a := manipulated_a or (((a shr 16) and $FFFF) shl 48);
  586. { if manipulated_a = a, don't check, because is_shifter_const was already
  587. called for a and it returned False. Reduces processing time. [Kit] }
  588. if (manipulated_a <> a) and is_shifter_const(manipulated_a, size) then
  589. begin
  590. list.concat(taicpu.op_reg_reg_const(A_ORR, reg, makeregsize(NR_XZR, size), manipulated_a));
  591. if (leftover_a <> 0) then
  592. begin
  593. shifterop_reset(so);
  594. so.shiftmode := SM_LSL;
  595. so.shiftimm := 48;
  596. list.concat(taicpu.op_reg_const_shifterop(A_MOVK, reg, leftover_a, so));
  597. end;
  598. Exit;
  599. end;
  600. case a of
  601. { If a is in the given negative range, it can be stored
  602. more efficiently if it is inverted. }
  603. TCgInt($FFFF000000000000)..-65537:
  604. begin
  605. { NOTE: This excluded range can be more efficiently
  606. stored as the first 16 bits followed by a shifter constant }
  607. case a of
  608. TCgInt($FFFF0000FFFF0000)..TCgInt($FFFF0000FFFFFFFF):
  609. doinverted := False
  610. else
  611. begin
  612. doinverted := True;
  613. a := not a;
  614. end;
  615. end;
  616. end;
  617. else
  618. doinverted := False;
  619. end;
  620. end
  621. else
  622. begin
  623. a:=cardinal(a);
  624. doinverted:=False;
  625. end;
  626. end;
  627. end;
  628. reginited:=false;
  629. shift:=0;
  630. if doinverted then
  631. opc:=A_MOVN
  632. else
  633. opc:=A_MOVZ;
  634. repeat
  635. { leftover is shifterconst? (don't check if we can represent it just
  636. as effectively with movz/movk, as this check is expensive) }
  637. if (word(a)<>0) then
  638. begin
  639. if not doinverted and
  640. ((shift<tcgsize2size[size]*(8 div 2)) and
  641. ((a shr 16)<>0)) and
  642. is_shifter_const(a shl shift,size) then
  643. begin
  644. if reginited then
  645. list.concat(taicpu.op_reg_reg_const(A_ORR,reg,reg,a shl shift))
  646. else
  647. list.concat(taicpu.op_reg_reg_const(A_ORR,reg,makeregsize(NR_XZR,size),a shl shift));
  648. exit;
  649. end;
  650. { set all 16 bit parts <> 0 }
  651. if shift=0 then
  652. begin
  653. list.concat(taicpu.op_reg_const(opc,reg,word(a)));
  654. reginited:=true;
  655. end
  656. else
  657. begin
  658. shifterop_reset(so);
  659. so.shiftmode:=SM_LSL;
  660. so.shiftimm:=shift;
  661. if not reginited then
  662. begin
  663. list.concat(taicpu.op_reg_const_shifterop(opc,reg,word(a),so));
  664. reginited:=true;
  665. end
  666. else
  667. begin
  668. if doinverted then
  669. list.concat(taicpu.op_reg_const_shifterop(A_MOVK,reg,word(not a),so))
  670. else
  671. list.concat(taicpu.op_reg_const_shifterop(A_MOVK,reg,word(a),so));
  672. end;
  673. end;
  674. end;
  675. a:=a shr 16;
  676. inc(shift,16);
  677. until a = 0;
  678. if not reginited then
  679. internalerror(2014102702);
  680. end;
  681. procedure tcgaarch64.a_load_const_ref(list: TAsmList; size: tcgsize; a: tcgint; const ref: treference);
  682. var
  683. reg: tregister;
  684. begin
  685. { use the zero register if possible }
  686. if a=0 then
  687. begin
  688. if size in [OS_64,OS_S64] then
  689. reg:=NR_XZR
  690. else
  691. reg:=NR_WZR;
  692. a_load_reg_ref(list,size,size,reg,ref);
  693. end
  694. else
  695. inherited;
  696. end;
  697. procedure tcgaarch64.a_load_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference);
  698. var
  699. oppostfix:toppostfix;
  700. hreg: tregister;
  701. begin
  702. if tcgsize2Size[fromsize]>=tcgsize2Size[tosize] then
  703. begin
  704. fromsize:=tosize;
  705. reg:=makeregsize(list,reg,fromsize);
  706. end
  707. { have a 32 bit register but need a 64 bit one? }
  708. else if tosize in [OS_64,OS_S64] then
  709. begin
  710. { sign extend if necessary }
  711. if fromsize in [OS_S8,OS_S16,OS_S32] then
  712. begin
  713. { can't overwrite reg, may be a constant reg }
  714. hreg:=getintregister(list,tosize);
  715. a_load_reg_reg(list,fromsize,tosize,reg,hreg);
  716. reg:=hreg;
  717. end
  718. else
  719. { top 32 bit are zero by default }
  720. reg:=makeregsize(reg,OS_64);
  721. fromsize:=tosize;
  722. end;
  723. if (ref.alignment<>0) and
  724. (ref.alignment<tcgsize2size[tosize]) then
  725. begin
  726. a_load_reg_ref_unaligned(list,fromsize,tosize,reg,ref);
  727. end
  728. else
  729. begin
  730. case tosize of
  731. { signed integer registers }
  732. OS_8,
  733. OS_S8:
  734. oppostfix:=PF_B;
  735. OS_16,
  736. OS_S16:
  737. oppostfix:=PF_H;
  738. OS_32,
  739. OS_S32,
  740. OS_64,
  741. OS_S64:
  742. oppostfix:=PF_None;
  743. else
  744. InternalError(200308299);
  745. end;
  746. handle_load_store(list,A_STR,tosize,oppostfix,reg,ref);
  747. end;
  748. end;
  749. procedure tcgaarch64.a_load_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  750. var
  751. oppostfix:toppostfix;
  752. begin
  753. if tcgsize2Size[fromsize]>=tcgsize2Size[tosize] then
  754. fromsize:=tosize;
  755. { ensure that all bits of the 32/64 register are always correctly set:
  756. * default behaviour is always to zero-extend to the entire (64 bit)
  757. register -> unsigned 8/16/32 bit loads only exist with a 32 bit
  758. target register, as the upper 32 bit will be zeroed implicitly
  759. -> always make target register 32 bit
  760. * signed loads exist both with 32 and 64 bit target registers,
  761. depending on whether the value should be sign extended to 32 or
  762. to 64 bit (if sign extended to 32 bit, the upper 32 bits of the
  763. corresponding 64 bit register are again zeroed) -> no need to
  764. change anything (we only have 32 and 64 bit registers), except that
  765. when loading an OS_S32 to a 32 bit register, we don't need/can't
  766. use sign extension
  767. }
  768. if fromsize in [OS_8,OS_16,OS_32] then
  769. reg:=makeregsize(reg,OS_32);
  770. if (ref.alignment<>0) and
  771. (ref.alignment<tcgsize2size[fromsize]) then
  772. begin
  773. a_load_ref_reg_unaligned(list,fromsize,tosize,ref,reg);
  774. exit;
  775. end;
  776. case fromsize of
  777. { signed integer registers }
  778. OS_8:
  779. oppostfix:=PF_B;
  780. OS_S8:
  781. oppostfix:=PF_SB;
  782. OS_16:
  783. oppostfix:=PF_H;
  784. OS_S16:
  785. oppostfix:=PF_SH;
  786. OS_S32:
  787. if getsubreg(reg)=R_SUBD then
  788. oppostfix:=PF_NONE
  789. else
  790. oppostfix:=PF_SW;
  791. OS_32,
  792. OS_64,
  793. OS_S64:
  794. oppostfix:=PF_None;
  795. else
  796. InternalError(200308297);
  797. end;
  798. handle_load_store(list,A_LDR,fromsize,oppostfix,reg,ref);
  799. { clear upper 16 bits if the value was negative }
  800. if (fromsize=OS_S8) and (tosize=OS_16) then
  801. a_load_reg_reg(list,fromsize,tosize,reg,reg);
  802. end;
  803. procedure tcgaarch64.a_load_ref_reg_unaligned(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; register: tregister);
  804. var
  805. href: treference;
  806. hreg1, hreg2, tmpreg: tregister;
  807. begin
  808. if fromsize in [OS_64,OS_S64] then
  809. begin
  810. { split into two 32 bit loads }
  811. hreg1:=getintregister(list,OS_32);
  812. hreg2:=getintregister(list,OS_32);
  813. if target_info.endian=endian_big then
  814. begin
  815. tmpreg:=hreg1;
  816. hreg1:=hreg2;
  817. hreg2:=tmpreg;
  818. end;
  819. { can we use LDP? }
  820. if (ref.alignment=4) and
  821. (simple_ref_type(A_LDP,OS_32,PF_None,ref)=sr_simple) then
  822. list.concat(taicpu.op_reg_reg_ref(A_LDP,hreg1,hreg2,ref))
  823. else
  824. begin
  825. a_load_ref_reg(list,OS_32,OS_32,ref,hreg1);
  826. href:=ref;
  827. inc(href.offset,4);
  828. a_load_ref_reg(list,OS_32,OS_32,href,hreg2);
  829. end;
  830. a_load_reg_reg(list,OS_32,OS_64,hreg1,register);
  831. list.concat(taicpu.op_reg_reg_const_const(A_BFI,register,makeregsize(hreg2,OS_64),32,32));
  832. end
  833. else
  834. inherited;
  835. end;
  836. procedure tcgaarch64.a_load_reg_reg(list:TAsmList;fromsize,tosize:tcgsize;reg1,reg2:tregister);
  837. var
  838. instr: taicpu;
  839. begin
  840. { we use both 32 and 64 bit registers -> insert conversion when when
  841. we have to truncate/sign extend inside the (32 or 64 bit) register
  842. holding the value, and when we sign extend from a 32 to a 64 bit
  843. register }
  844. if (tcgsize2size[fromsize]>tcgsize2size[tosize]) or
  845. ((tcgsize2size[fromsize]=tcgsize2size[tosize]) and
  846. (fromsize<>tosize) and
  847. not(fromsize in [OS_32,OS_S32,OS_64,OS_S64])) or
  848. ((fromsize in [OS_S8,OS_S16,OS_S32]) and
  849. (tosize in [OS_64,OS_S64])) or
  850. { needs to mask out the sign in the top 16 bits }
  851. ((fromsize=OS_S8) and
  852. (tosize=OS_16)) then
  853. begin
  854. case tosize of
  855. OS_8:
  856. list.concat(setoppostfix(taicpu.op_reg_reg(A_UXT,reg2,makeregsize(reg1,OS_32)),PF_B));
  857. OS_16:
  858. list.concat(setoppostfix(taicpu.op_reg_reg(A_UXT,reg2,makeregsize(reg1,OS_32)),PF_H));
  859. OS_S8:
  860. list.concat(setoppostfix(taicpu.op_reg_reg(A_SXT,reg2,makeregsize(reg1,OS_32)),PF_B));
  861. OS_S16:
  862. list.concat(setoppostfix(taicpu.op_reg_reg(A_SXT,reg2,makeregsize(reg1,OS_32)),PF_H));
  863. { while "mov wN, wM" automatically inserts a zero-extension and
  864. hence we could encode a 64->32 bit move like that, the problem
  865. is that we then can't distinguish 64->32 from 32->32 moves, and
  866. the 64->32 truncation could be removed altogether... So use a
  867. different instruction }
  868. OS_32,
  869. OS_S32:
  870. { in theory, reg1 should be 64 bit here (since fromsize>tosize),
  871. but because of the way location_force_register() tries to
  872. avoid superfluous zero/sign extensions, it's not always the
  873. case -> also force reg1 to to 64 bit }
  874. list.concat(taicpu.op_reg_reg_const_const(A_UBFIZ,makeregsize(reg2,OS_64),makeregsize(reg1,OS_64),0,32));
  875. OS_64,
  876. OS_S64:
  877. list.concat(setoppostfix(taicpu.op_reg_reg(A_SXT,reg2,makeregsize(reg1,OS_32)),PF_W));
  878. else
  879. internalerror(2002090901);
  880. end;
  881. end
  882. else
  883. begin
  884. { 32 -> 32 bit move implies zero extension (sign extensions have
  885. been handled above) -> also use for 32 <-> 64 bit moves }
  886. if not(fromsize in [OS_64,OS_S64]) or
  887. not(tosize in [OS_64,OS_S64]) then
  888. instr:=taicpu.op_reg_reg(A_MOV,makeregsize(reg2,OS_32),makeregsize(reg1,OS_32))
  889. else
  890. instr:=taicpu.op_reg_reg(A_MOV,reg2,reg1);
  891. list.Concat(instr);
  892. { Notify the register allocator that we have written a move instruction so
  893. it can try to eliminate it. }
  894. add_move_instruction(instr);
  895. end;
  896. end;
  897. procedure tcgaarch64.a_loadaddr_ref_reg(list: TAsmList; const ref: treference; r: tregister);
  898. var
  899. href: treference;
  900. so: tshifterop;
  901. op: tasmop;
  902. begin
  903. op:=A_LDR;
  904. href:=ref;
  905. { simplify as if we're going to perform a regular 64 bit load, using
  906. "r" as the new base register if possible/necessary }
  907. make_simple_ref(list,op,OS_ADDR,PF_None,href,r);
  908. { load literal? }
  909. if assigned(href.symbol) then
  910. begin
  911. if (href.base<>NR_NO) or
  912. (href.index<>NR_NO) or
  913. not assigned(href.symboldata) then
  914. internalerror(2014110912);
  915. list.concat(taicpu.op_reg_sym_ofs(A_ADR,r,href.symbol,href.offset));
  916. end
  917. else
  918. begin
  919. if href.index<>NR_NO then
  920. begin
  921. if href.shiftmode<>SM_None then
  922. begin
  923. { "add" supports a supperset of the shift modes supported by
  924. load/store instructions }
  925. shifterop_reset(so);
  926. so.shiftmode:=href.shiftmode;
  927. so.shiftimm:=href.shiftimm;
  928. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,r,href.base,href.index,so));
  929. end
  930. else
  931. a_op_reg_reg_reg(list,OP_ADD,OS_ADDR,href.index,href.base,r);
  932. end
  933. else if href.offset<>0 then
  934. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,href.offset,href.base,r)
  935. else
  936. a_load_reg_reg(list,OS_ADDR,OS_ADDR,href.base,r);
  937. end;
  938. end;
  939. procedure tcgaarch64.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  940. begin
  941. internalerror(2014122107)
  942. end;
  943. procedure tcgaarch64.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  944. begin
  945. internalerror(2014122108)
  946. end;
  947. procedure tcgaarch64.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference);
  948. begin
  949. internalerror(2014122109)
  950. end;
  951. procedure tcgaarch64.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister; shuffle: pmmshuffle);
  952. var
  953. instr: taicpu;
  954. begin
  955. if assigned(shuffle) and
  956. not shufflescalar(shuffle) then
  957. internalerror(2014122104);
  958. if fromsize=tosize then
  959. begin
  960. instr:=taicpu.op_reg_reg(A_FMOV,reg2,reg1);
  961. { Notify the register allocator that we have written a move
  962. instruction so it can try to eliminate it. }
  963. add_move_instruction(instr);
  964. end
  965. else
  966. begin
  967. if (reg_cgsize(reg1)<>fromsize) or
  968. (reg_cgsize(reg2)<>tosize) then
  969. internalerror(2014110913);
  970. instr:=taicpu.op_reg_reg(A_FCVT,reg2,reg1);
  971. end;
  972. list.Concat(instr);
  973. maybe_check_for_fpu_exception(list);
  974. end;
  975. procedure tcgaarch64.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  976. var
  977. tmpreg: tregister;
  978. begin
  979. if assigned(shuffle) and
  980. not shufflescalar(shuffle) then
  981. internalerror(2014122105);
  982. tmpreg:=NR_NO;
  983. if (fromsize<>tosize) then
  984. begin
  985. tmpreg:=reg;
  986. reg:=getmmregister(list,fromsize);
  987. end;
  988. handle_load_store(list,A_LDR,fromsize,PF_None,reg,ref);
  989. if (fromsize<>tosize) then
  990. a_loadmm_reg_reg(list,fromsize,tosize,reg,tmpreg,nil);
  991. end;
  992. procedure tcgaarch64.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  993. var
  994. tmpreg: tregister;
  995. begin
  996. if assigned(shuffle) and
  997. not shufflescalar(shuffle) then
  998. internalerror(2014122106);
  999. if (fromsize<>tosize) then
  1000. begin
  1001. tmpreg:=getmmregister(list,tosize);
  1002. a_loadmm_reg_reg(list,fromsize,tosize,reg,tmpreg,nil);
  1003. reg:=tmpreg;
  1004. end;
  1005. handle_load_store(list,A_STR,tosize,PF_NONE,reg,ref);
  1006. end;
  1007. procedure tcgaarch64.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle);
  1008. begin
  1009. if not shufflescalar(shuffle) then
  1010. internalerror(2014122801);
  1011. if not(tcgsize2size[fromsize] in [4,8]) or
  1012. (tcgsize2size[fromsize]<>tcgsize2size[tosize]) then
  1013. internalerror(2014122803);
  1014. list.concat(taicpu.op_reg_reg(A_INS,mmreg,intreg));
  1015. end;
  1016. procedure tcgaarch64.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tcgsize; mmreg, intreg: tregister; shuffle: pmmshuffle);
  1017. var
  1018. r : tregister;
  1019. begin
  1020. if not shufflescalar(shuffle) then
  1021. internalerror(2014122802);
  1022. if not(tcgsize2size[fromsize] in [4,8]) or
  1023. (tcgsize2size[fromsize]>tcgsize2size[tosize]) then
  1024. internalerror(2014122804);
  1025. if tcgsize2size[fromsize]<tcgsize2size[tosize] then
  1026. r:=makeregsize(intreg,fromsize)
  1027. else
  1028. r:=intreg;
  1029. list.concat(taicpu.op_reg_reg(A_UMOV,r,mmreg));
  1030. end;
  1031. procedure tcgaarch64.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tcgsize; src, dst: tregister; shuffle: pmmshuffle);
  1032. begin
  1033. case op of
  1034. { "xor Vx,Vx" is used to initialize global regvars to 0 }
  1035. OP_XOR:
  1036. begin
  1037. if (src<>dst) or
  1038. (reg_cgsize(src)<>size) or
  1039. assigned(shuffle) then
  1040. internalerror(2015011401);
  1041. case size of
  1042. OS_F32,
  1043. OS_F64:
  1044. list.concat(taicpu.op_reg_const(A_MOVI,makeregsize(dst,OS_F64),0));
  1045. else
  1046. internalerror(2015011402);
  1047. end;
  1048. end
  1049. else
  1050. internalerror(2015011403);
  1051. end;
  1052. end;
  1053. procedure tcgaarch64.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tcgsize; src, dst: TRegister);
  1054. var
  1055. bitsize,
  1056. signbit: longint;
  1057. begin
  1058. if srcsize in [OS_64,OS_S64] then
  1059. begin
  1060. bitsize:=64;
  1061. signbit:=6;
  1062. end
  1063. else
  1064. begin
  1065. bitsize:=32;
  1066. signbit:=5;
  1067. end;
  1068. { source is 0 -> dst will have to become 255 }
  1069. list.concat(taicpu.op_reg_const(A_CMP,src,0));
  1070. if reverse then
  1071. begin
  1072. list.Concat(taicpu.op_reg_reg(A_CLZ,makeregsize(dst,srcsize),src));
  1073. { xor 31/63 is the same as setting the lower 5/6 bits to
  1074. "31/63-(lower 5/6 bits of dst)" }
  1075. list.Concat(taicpu.op_reg_reg_const(A_EOR,dst,dst,bitsize-1));
  1076. end
  1077. else
  1078. begin
  1079. list.Concat(taicpu.op_reg_reg(A_RBIT,makeregsize(dst,srcsize),src));
  1080. list.Concat(taicpu.op_reg_reg(A_CLZ,dst,dst));
  1081. end;
  1082. { set dst to -1 if src was 0 }
  1083. list.Concat(taicpu.op_reg_reg_reg_cond(A_CSINV,dst,dst,makeregsize(NR_XZR,dstsize),C_NE));
  1084. { mask the -1 to 255 if src was 0 (anyone find a two-instruction
  1085. branch-free version? All of mine are 3...) }
  1086. list.Concat(setoppostfix(taicpu.op_reg_reg(A_UXT,makeregsize(dst,OS_32),makeregsize(dst,OS_32)),PF_B));
  1087. end;
  1088. procedure tcgaarch64.a_load_reg_ref_unaligned(list: TAsmList; fromsize, tosize: tcgsize; register: tregister; const ref: treference);
  1089. var
  1090. href: treference;
  1091. hreg1, hreg2, tmpreg: tregister;
  1092. begin
  1093. if fromsize in [OS_64,OS_S64] then
  1094. begin
  1095. { split into two 32 bit stores }
  1096. hreg1:=getintregister(list,OS_32);
  1097. hreg2:=getintregister(list,OS_32);
  1098. a_load_reg_reg(list,OS_32,OS_32,makeregsize(register,OS_32),hreg1);
  1099. a_op_const_reg_reg(list,OP_SHR,OS_64,32,register,makeregsize(hreg2,OS_64));
  1100. if target_info.endian=endian_big then
  1101. begin
  1102. tmpreg:=hreg1;
  1103. hreg1:=hreg2;
  1104. hreg2:=tmpreg;
  1105. end;
  1106. { can we use STP? }
  1107. if (ref.alignment=4) and
  1108. (simple_ref_type(A_STP,OS_32,PF_None,ref)=sr_simple) then
  1109. list.concat(taicpu.op_reg_reg_ref(A_STP,hreg1,hreg2,ref))
  1110. else
  1111. begin
  1112. a_load_reg_ref(list,OS_32,OS_32,hreg1,ref);
  1113. href:=ref;
  1114. inc(href.offset,4);
  1115. a_load_reg_ref(list,OS_32,OS_32,hreg2,href);
  1116. end;
  1117. end
  1118. else
  1119. inherited;
  1120. end;
  1121. procedure tcgaarch64.maybeadjustresult(list: TAsmList; op: topcg; size: tcgsize; dst: tregister);
  1122. const
  1123. overflowops = [OP_MUL,OP_IMUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  1124. begin
  1125. if (op in overflowops) and
  1126. (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  1127. a_load_reg_reg(list,OS_32,size,makeregsize(dst,OS_32),makeregsize(dst,OS_32))
  1128. end;
  1129. procedure tcgaarch64.a_op_const_reg(list: TAsmList; op: topcg; size: tcgsize; a: tcgint; reg: tregister);
  1130. begin
  1131. optimize_op_const(size,op,a);
  1132. case op of
  1133. OP_NONE:
  1134. exit;
  1135. OP_MOVE:
  1136. a_load_const_reg(list,size,a,reg);
  1137. OP_NEG,OP_NOT:
  1138. internalerror(200306011);
  1139. else
  1140. a_op_const_reg_reg(list,op,size,a,reg,reg);
  1141. end;
  1142. end;
  1143. procedure tcgaarch64.a_op_reg_reg(list:TAsmList;op:topcg;size:tcgsize;src,dst:tregister);
  1144. begin
  1145. Case op of
  1146. OP_NEG,
  1147. OP_NOT:
  1148. begin
  1149. list.concat(taicpu.op_reg_reg(TOpCG2AsmOpReg[op],dst,src));
  1150. maybeadjustresult(list,op,size,dst);
  1151. end
  1152. else
  1153. a_op_reg_reg_reg(list,op,size,src,dst,dst);
  1154. end;
  1155. end;
  1156. procedure tcgaarch64.a_op_const_reg_reg(list: TAsmList; op: topcg; size: tcgsize; a: tcgint; src, dst: tregister);
  1157. var
  1158. l: tlocation;
  1159. begin
  1160. a_op_const_reg_reg_checkoverflow(list,op,size,a,src,dst,false,l);
  1161. end;
  1162. procedure tcgaarch64.a_op_reg_reg_reg(list: TAsmList; op: topcg; size: tcgsize; src1, src2, dst: tregister);
  1163. var
  1164. hreg: tregister;
  1165. begin
  1166. { no ROLV opcode... }
  1167. if op=OP_ROL then
  1168. begin
  1169. case size of
  1170. OS_32,OS_S32,
  1171. OS_64,OS_S64:
  1172. begin
  1173. hreg:=getintregister(list,size);
  1174. a_load_const_reg(list,size,tcgsize2size[size]*8,hreg);
  1175. a_op_reg_reg(list,OP_SUB,size,src1,hreg);
  1176. a_op_reg_reg_reg(list,OP_ROR,size,hreg,src2,dst);
  1177. exit;
  1178. end;
  1179. else
  1180. internalerror(2014111005);
  1181. end;
  1182. end
  1183. else if (op=OP_ROR) and
  1184. not(size in [OS_32,OS_S32,OS_64,OS_S64]) then
  1185. internalerror(2014111006);
  1186. if TOpCG2AsmOpReg[op]=A_NONE then
  1187. internalerror(2014111007);
  1188. list.concat(taicpu.op_reg_reg_reg(TOpCG2AsmOpReg[op],dst,src2,src1));
  1189. maybeadjustresult(list,op,size,dst);
  1190. end;
  1191. procedure tcgaarch64.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: topcg; size: tcgsize; a: tcgint; src, dst: tregister; setflags : boolean; var ovloc : tlocation);
  1192. var
  1193. shiftcountmask: longint;
  1194. constreg: tregister;
  1195. begin
  1196. { add/sub instructions have only positive immediate operands }
  1197. if (op in [OP_ADD,OP_SUB]) and
  1198. (a<0) then
  1199. begin
  1200. if op=OP_ADD then
  1201. op:=op_SUB
  1202. else
  1203. op:=OP_ADD;
  1204. { avoid range/overflow error in case a = low(tcgint) }
  1205. {$push}{$r-}{$q-}
  1206. a:=-a;
  1207. {$pop}
  1208. end;
  1209. ovloc.loc:=LOC_VOID;
  1210. optimize_op_const(size,op,a);
  1211. case op of
  1212. OP_NONE:
  1213. begin
  1214. a_load_reg_reg(list,size,size,src,dst);
  1215. exit;
  1216. end;
  1217. OP_MOVE:
  1218. begin
  1219. a_load_const_reg(list,size,a,dst);
  1220. exit;
  1221. end;
  1222. end;
  1223. case op of
  1224. OP_ADD,
  1225. OP_SUB:
  1226. begin
  1227. handle_reg_imm12_reg(list,TOpCG2AsmOpImm[op],size,src,a,dst,NR_NO,setflags,true);
  1228. { on a 64 bit target, overflows with smaller data types
  1229. are handled via range errors }
  1230. if setflags and
  1231. (size in [OS_64,OS_S64]) then
  1232. begin
  1233. location_reset(ovloc,LOC_FLAGS,OS_8);
  1234. if size=OS_64 then
  1235. if op=OP_ADD then
  1236. ovloc.resflags:=F_CS
  1237. else
  1238. ovloc.resflags:=F_CC
  1239. else
  1240. ovloc.resflags:=F_VS;
  1241. end;
  1242. end;
  1243. OP_OR,
  1244. OP_AND,
  1245. OP_XOR:
  1246. begin
  1247. if not(size in [OS_64,OS_S64]) then
  1248. a:=cardinal(a);
  1249. if is_shifter_const(a,size) then
  1250. list.concat(taicpu.op_reg_reg_const(TOpCG2AsmOpReg[op],dst,src,a))
  1251. else
  1252. begin
  1253. constreg:=getintregister(list,size);
  1254. a_load_const_reg(list,size,a,constreg);
  1255. a_op_reg_reg_reg(list,op,size,constreg,src,dst);
  1256. end;
  1257. end;
  1258. OP_SHL,
  1259. OP_SHR,
  1260. OP_SAR:
  1261. begin
  1262. if size in [OS_64,OS_S64] then
  1263. shiftcountmask:=63
  1264. else
  1265. shiftcountmask:=31;
  1266. if (a and shiftcountmask)<>0 Then
  1267. list.concat(taicpu.op_reg_reg_const(
  1268. TOpCG2AsmOpImm[Op],dst,src,a and shiftcountmask))
  1269. else
  1270. a_load_reg_reg(list,size,size,src,dst);
  1271. if (a and not(tcgint(shiftcountmask)))<>0 then
  1272. internalError(2014112101);
  1273. end;
  1274. OP_ROL,
  1275. OP_ROR:
  1276. begin
  1277. case size of
  1278. OS_32,OS_S32:
  1279. if (a and not(tcgint(31)))<>0 then
  1280. internalError(2014112102);
  1281. OS_64,OS_S64:
  1282. if (a and not(tcgint(63)))<>0 then
  1283. internalError(2014112103);
  1284. else
  1285. internalError(2014112104);
  1286. end;
  1287. { there's only a ror opcode }
  1288. if op=OP_ROL then
  1289. a:=(tcgsize2size[size]*8)-a;
  1290. list.concat(taicpu.op_reg_reg_const(A_ROR,dst,src,a));
  1291. end;
  1292. OP_MUL,
  1293. OP_IMUL,
  1294. OP_DIV,
  1295. OP_IDIV:
  1296. begin
  1297. constreg:=getintregister(list,size);
  1298. a_load_const_reg(list,size,a,constreg);
  1299. a_op_reg_reg_reg_checkoverflow(list,op,size,constreg,src,dst,setflags,ovloc);
  1300. end;
  1301. else
  1302. internalerror(2014111403);
  1303. end;
  1304. maybeadjustresult(list,op,size,dst);
  1305. end;
  1306. procedure tcgaarch64.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: topcg; size: tcgsize; src1, src2, dst: tregister; setflags : boolean; var ovloc : tlocation);
  1307. var
  1308. tmpreg1, tmpreg2: tregister;
  1309. begin
  1310. ovloc.loc:=LOC_VOID;
  1311. { overflow can only occur with 64 bit calculations on 64 bit cpus }
  1312. if setflags and
  1313. (size in [OS_64,OS_S64]) then
  1314. begin
  1315. case op of
  1316. OP_ADD,
  1317. OP_SUB:
  1318. begin
  1319. list.concat(setoppostfix(taicpu.op_reg_reg_reg(TOpCG2AsmOpReg[op],dst,src2,src1),PF_S));
  1320. ovloc.loc:=LOC_FLAGS;
  1321. if size=OS_64 then
  1322. if op=OP_ADD then
  1323. ovloc.resflags:=F_CS
  1324. else
  1325. ovloc.resflags:=F_CC
  1326. else
  1327. ovloc.resflags:=F_VS;
  1328. { finished }
  1329. exit;
  1330. end;
  1331. OP_MUL:
  1332. begin
  1333. { check whether the upper 64 bit of the 128 bit product is 0 }
  1334. tmpreg1:=getintregister(list,OS_64);
  1335. list.concat(taicpu.op_reg_reg_reg(A_UMULH,tmpreg1,src2,src1));
  1336. list.concat(taicpu.op_reg_const(A_CMP,tmpreg1,0));
  1337. ovloc.loc:=LOC_FLAGS;
  1338. ovloc.resflags:=F_NE;
  1339. { still have to perform the actual multiplication }
  1340. end;
  1341. OP_IMUL:
  1342. begin
  1343. { check whether the upper 64 bits of the 128 bit multiplication
  1344. result have the same value as the replicated sign bit of the
  1345. lower 64 bits }
  1346. tmpreg1:=getintregister(list,OS_64);
  1347. list.concat(taicpu.op_reg_reg_reg(A_SMULH,tmpreg1,src2,src1));
  1348. { calculate lower 64 bits (afterwards, because dst may be
  1349. equal to src1 or src2) }
  1350. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1351. { replicate sign bit }
  1352. tmpreg2:=getintregister(list,OS_64);
  1353. a_op_const_reg_reg(list,OP_SAR,OS_S64,63,dst,tmpreg2);
  1354. list.concat(taicpu.op_reg_reg(A_CMP,tmpreg1,tmpreg2));
  1355. ovloc.loc:=LOC_FLAGS;
  1356. ovloc.resflags:=F_NE;
  1357. { finished }
  1358. exit;
  1359. end;
  1360. OP_IDIV,
  1361. OP_DIV:
  1362. begin
  1363. { not handled here, needs div-by-zero check (dividing by zero
  1364. just gives a 0 result on aarch64), and low(int64) div -1
  1365. check for overflow) }
  1366. internalerror(2014122101);
  1367. end;
  1368. end;
  1369. end;
  1370. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1371. end;
  1372. {*************** compare instructructions ****************}
  1373. procedure tcgaarch64.a_cmp_const_reg_label(list: TAsmList; size: tcgsize; cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  1374. var
  1375. op: tasmop;
  1376. begin
  1377. if a>=0 then
  1378. op:=A_CMP
  1379. else
  1380. op:=A_CMN;
  1381. { avoid range/overflow error in case a=low(tcgint) }
  1382. {$push}{$r-}{$q-}
  1383. handle_reg_imm12_reg(list,op,size,reg,abs(a),NR_XZR,NR_NO,false,false);
  1384. {$pop}
  1385. a_jmp_cond(list,cmp_op,l);
  1386. end;
  1387. procedure tcgaarch64.a_cmp_reg_reg_label(list: TAsmList; size: tcgsize; cmp_op: topcmp; reg1,reg2: tregister; l: tasmlabel);
  1388. begin
  1389. list.concat(taicpu.op_reg_reg(A_CMP,reg2,reg1));
  1390. a_jmp_cond(list,cmp_op,l);
  1391. end;
  1392. procedure tcgaarch64.a_jmp_always(list: TAsmList; l: TAsmLabel);
  1393. var
  1394. ai: taicpu;
  1395. begin
  1396. ai:=TAiCpu.op_sym(A_B,current_asmdata.RefAsmSymbol(l.name,AT_FUNCTION));
  1397. ai.is_jmp:=true;
  1398. list.Concat(ai);
  1399. end;
  1400. procedure tcgaarch64.a_jmp_name(list: TAsmList; const s: string);
  1401. var
  1402. ai: taicpu;
  1403. begin
  1404. ai:=TAiCpu.op_sym(A_B,current_asmdata.RefAsmSymbol(s,AT_FUNCTION));
  1405. ai.is_jmp:=true;
  1406. list.Concat(ai);
  1407. end;
  1408. procedure tcgaarch64.a_jmp_cond(list: TAsmList; cond: TOpCmp; l: TAsmLabel);
  1409. var
  1410. ai: taicpu;
  1411. begin
  1412. ai:=TAiCpu.op_sym(A_B,l);
  1413. ai.is_jmp:=true;
  1414. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1415. list.Concat(ai);
  1416. end;
  1417. procedure tcgaarch64.a_jmp_flags(list: TAsmList; const f: tresflags; l: tasmlabel);
  1418. var
  1419. ai : taicpu;
  1420. begin
  1421. ai:=Taicpu.op_sym(A_B,l);
  1422. ai.is_jmp:=true;
  1423. ai.SetCondition(flags_to_cond(f));
  1424. list.Concat(ai);
  1425. end;
  1426. procedure tcgaarch64.g_flags2reg(list: TAsmList; size: tcgsize; const f: tresflags; reg: tregister);
  1427. begin
  1428. list.concat(taicpu.op_reg_cond(A_CSET,reg,flags_to_cond(f)));
  1429. end;
  1430. procedure tcgaarch64.g_overflowcheck(list: TAsmList; const loc: tlocation; def: tdef);
  1431. begin
  1432. { we need an explicit overflow location, because there are many
  1433. possibilities (not just the overflow flag, which is only used for
  1434. signed add/sub) }
  1435. internalerror(2014112303);
  1436. end;
  1437. procedure tcgaarch64.g_overflowcheck_loc(list: TAsmList; const loc: tlocation; def: tdef; ovloc : tlocation);
  1438. var
  1439. hl : tasmlabel;
  1440. hflags : tresflags;
  1441. begin
  1442. if not(cs_check_overflow in current_settings.localswitches) then
  1443. exit;
  1444. current_asmdata.getjumplabel(hl);
  1445. case ovloc.loc of
  1446. LOC_FLAGS:
  1447. begin
  1448. hflags:=ovloc.resflags;
  1449. inverse_flags(hflags);
  1450. cg.a_jmp_flags(list,hflags,hl);
  1451. end;
  1452. else
  1453. internalerror(2014112304);
  1454. end;
  1455. a_call_name(list,'FPC_OVERFLOW',false);
  1456. a_label(list,hl);
  1457. end;
  1458. { *********** entry/exit code and address loading ************ }
  1459. function tcgaarch64.save_regs(list: TAsmList; rt: tregistertype; lowsr, highsr: tsuperregister; sub: tsubregister): longint;
  1460. var
  1461. ref: treference;
  1462. sr: tsuperregister;
  1463. pairreg: tregister;
  1464. begin
  1465. result:=0;
  1466. reference_reset_base(ref,NR_SP,-16,ctempposinvalid,16,[]);
  1467. ref.addressmode:=AM_PREINDEXED;
  1468. pairreg:=NR_NO;
  1469. { store all used registers pairwise }
  1470. for sr:=lowsr to highsr do
  1471. if sr in rg[rt].used_in_proc then
  1472. if pairreg=NR_NO then
  1473. pairreg:=newreg(rt,sr,sub)
  1474. else
  1475. begin
  1476. inc(result,16);
  1477. list.concat(taicpu.op_reg_reg_ref(A_STP,pairreg,newreg(rt,sr,sub),ref));
  1478. pairreg:=NR_NO
  1479. end;
  1480. { one left -> store twice (stack must be 16 bytes aligned) }
  1481. if pairreg<>NR_NO then
  1482. begin
  1483. list.concat(taicpu.op_reg_reg_ref(A_STP,pairreg,pairreg,ref));
  1484. inc(result,16);
  1485. end;
  1486. end;
  1487. procedure FixupOffsets(p:TObject;arg:pointer);
  1488. var
  1489. sym: tabstractnormalvarsym absolute p;
  1490. begin
  1491. if (tsym(p).typ in [paravarsym,localvarsym]) and
  1492. (sym.localloc.loc=LOC_REFERENCE) and
  1493. (sym.localloc.reference.base=NR_STACK_POINTER_REG) then
  1494. begin
  1495. sym.localloc.reference.base:=NR_FRAME_POINTER_REG;
  1496. dec(sym.localloc.reference.offset,PLongint(arg)^);
  1497. end;
  1498. end;
  1499. procedure tcgaarch64.g_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean);
  1500. var
  1501. ref: treference;
  1502. totalstackframesize: longint;
  1503. begin
  1504. if nostackframe then
  1505. exit;
  1506. { stack pointer has to be aligned to 16 bytes at all times }
  1507. localsize:=align(localsize,16);
  1508. { save stack pointer and return address }
  1509. reference_reset_base(ref,NR_SP,-16,ctempposinvalid,16,[]);
  1510. ref.addressmode:=AM_PREINDEXED;
  1511. list.concat(taicpu.op_reg_reg_ref(A_STP,NR_FP,NR_LR,ref));
  1512. { initialise frame pointer }
  1513. a_load_reg_reg(list,OS_ADDR,OS_ADDR,NR_SP,NR_FP);
  1514. totalstackframesize:=localsize;
  1515. { save modified integer registers }
  1516. inc(totalstackframesize,
  1517. save_regs(list,R_INTREGISTER,RS_X19,RS_X28,R_SUBWHOLE));
  1518. { only the lower 64 bits of the modified vector registers need to be
  1519. saved; if the caller needs the upper 64 bits, it has to save them
  1520. itself }
  1521. inc(totalstackframesize,
  1522. save_regs(list,R_MMREGISTER,RS_D8,RS_D15,R_SUBMMD));
  1523. { allocate stack space }
  1524. if localsize<>0 then
  1525. begin
  1526. localsize:=align(localsize,16);
  1527. current_procinfo.final_localsize:=localsize;
  1528. handle_reg_imm12_reg(list,A_SUB,OS_ADDR,NR_SP,localsize,NR_SP,NR_IP0,false,true);
  1529. end;
  1530. { By default, we use the frame pointer to access parameters passed via
  1531. the stack and the stack pointer to address local variables and temps
  1532. because
  1533. a) we can use bigger positive than negative offsets (so accessing
  1534. locals via negative offsets from the frame pointer would be less
  1535. efficient)
  1536. b) we don't know the local size while generating the code, so
  1537. accessing the parameters via the stack pointer is not possible
  1538. without copying them
  1539. The problem with this is the get_frame() intrinsic:
  1540. a) it must return the same value as what we pass as parentfp
  1541. parameter, since that's how it's used in the TP-style objects unit
  1542. b) its return value must usable to access all local data from a
  1543. routine (locals and parameters), since it's all the nested
  1544. routines have access to
  1545. c) its return value must be usable to construct a backtrace, as it's
  1546. also used by the exception handling routines
  1547. The solution we use here, based on something similar that's done in
  1548. the MIPS port, is to generate all accesses to locals in the routine
  1549. itself SP-relative, and then after the code is generated and the local
  1550. size is known (namely, here), we change all SP-relative variables/
  1551. parameters into FP-relative ones. This means that they'll be accessed
  1552. less efficiently from nested routines, but those accesses are indirect
  1553. anyway and at least this way they can be accessed at all
  1554. }
  1555. if current_procinfo.has_nestedprocs then
  1556. begin
  1557. current_procinfo.procdef.localst.SymList.ForEachCall(@FixupOffsets,@totalstackframesize);
  1558. current_procinfo.procdef.parast.SymList.ForEachCall(@FixupOffsets,@totalstackframesize);
  1559. end;
  1560. end;
  1561. procedure tcgaarch64.g_maybe_got_init(list : TAsmList);
  1562. begin
  1563. { nothing to do on Darwin or Linux }
  1564. end;
  1565. procedure tcgaarch64.g_restore_registers(list:TAsmList);
  1566. begin
  1567. { done in g_proc_exit }
  1568. end;
  1569. procedure tcgaarch64.load_regs(list: TAsmList; rt: tregistertype; lowsr, highsr: tsuperregister; sub: tsubregister);
  1570. var
  1571. ref: treference;
  1572. sr, highestsetsr: tsuperregister;
  1573. pairreg: tregister;
  1574. regcount: longint;
  1575. begin
  1576. reference_reset_base(ref,NR_SP,16,ctempposinvalid,16,[]);
  1577. ref.addressmode:=AM_POSTINDEXED;
  1578. { highest reg stored twice? }
  1579. regcount:=0;
  1580. highestsetsr:=RS_NO;
  1581. for sr:=lowsr to highsr do
  1582. if sr in rg[rt].used_in_proc then
  1583. begin
  1584. inc(regcount);
  1585. highestsetsr:=sr;
  1586. end;
  1587. if odd(regcount) then
  1588. begin
  1589. list.concat(taicpu.op_reg_ref(A_LDR,newreg(rt,highestsetsr,sub),ref));
  1590. highestsetsr:=pred(highestsetsr);
  1591. end;
  1592. { load all (other) used registers pairwise }
  1593. pairreg:=NR_NO;
  1594. for sr:=highestsetsr downto lowsr do
  1595. if sr in rg[rt].used_in_proc then
  1596. if pairreg=NR_NO then
  1597. pairreg:=newreg(rt,sr,sub)
  1598. else
  1599. begin
  1600. list.concat(taicpu.op_reg_reg_ref(A_LDP,newreg(rt,sr,sub),pairreg,ref));
  1601. pairreg:=NR_NO
  1602. end;
  1603. { There can't be any register left }
  1604. if pairreg<>NR_NO then
  1605. internalerror(2014112602);
  1606. end;
  1607. procedure tcgaarch64.g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);
  1608. var
  1609. ref: treference;
  1610. regsstored: boolean;
  1611. sr: tsuperregister;
  1612. begin
  1613. if not nostackframe then
  1614. begin
  1615. { if no registers have been stored, we don't have to subtract the
  1616. allocated temp space from the stack pointer }
  1617. regsstored:=false;
  1618. for sr:=RS_X19 to RS_X28 do
  1619. if sr in rg[R_INTREGISTER].used_in_proc then
  1620. begin
  1621. regsstored:=true;
  1622. break;
  1623. end;
  1624. if not regsstored then
  1625. for sr:=RS_D8 to RS_D15 do
  1626. if sr in rg[R_MMREGISTER].used_in_proc then
  1627. begin
  1628. regsstored:=true;
  1629. break;
  1630. end;
  1631. { restore registers (and stack pointer) }
  1632. if regsstored then
  1633. begin
  1634. if current_procinfo.final_localsize<>0 then
  1635. handle_reg_imm12_reg(list,A_ADD,OS_ADDR,NR_SP,current_procinfo.final_localsize,NR_SP,NR_IP0,false,true);
  1636. load_regs(list,R_MMREGISTER,RS_D8,RS_D15,R_SUBMMD);
  1637. load_regs(list,R_INTREGISTER,RS_X19,RS_X28,R_SUBWHOLE);
  1638. end
  1639. else if current_procinfo.final_localsize<>0 then
  1640. { restore stack pointer }
  1641. a_load_reg_reg(list,OS_ADDR,OS_ADDR,NR_FP,NR_SP);
  1642. { restore framepointer and return address }
  1643. reference_reset_base(ref,NR_SP,16,ctempposinvalid,16,[]);
  1644. ref.addressmode:=AM_POSTINDEXED;
  1645. list.concat(taicpu.op_reg_reg_ref(A_LDP,NR_FP,NR_LR,ref));
  1646. end;
  1647. { return }
  1648. list.concat(taicpu.op_none(A_RET));
  1649. end;
  1650. procedure tcgaarch64.g_save_registers(list : TAsmList);
  1651. begin
  1652. { done in g_proc_entry }
  1653. end;
  1654. { ************* concatcopy ************ }
  1655. procedure tcgaarch64.g_concatcopy_move(list : TAsmList;const source,dest : treference;len : tcgint);
  1656. var
  1657. paraloc1,paraloc2,paraloc3 : TCGPara;
  1658. pd : tprocdef;
  1659. begin
  1660. pd:=search_system_proc('MOVE');
  1661. paraloc1.init;
  1662. paraloc2.init;
  1663. paraloc3.init;
  1664. paramanager.getintparaloc(list,pd,1,paraloc1);
  1665. paramanager.getintparaloc(list,pd,2,paraloc2);
  1666. paramanager.getintparaloc(list,pd,3,paraloc3);
  1667. a_load_const_cgpara(list,OS_SINT,len,paraloc3);
  1668. a_loadaddr_ref_cgpara(list,dest,paraloc2);
  1669. a_loadaddr_ref_cgpara(list,source,paraloc1);
  1670. paramanager.freecgpara(list,paraloc3);
  1671. paramanager.freecgpara(list,paraloc2);
  1672. paramanager.freecgpara(list,paraloc1);
  1673. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1674. alloccpuregisters(list,R_MMREGISTER,paramanager.get_volatile_registers_mm(pocall_default));
  1675. a_call_name(list,'FPC_MOVE',false);
  1676. dealloccpuregisters(list,R_MMREGISTER,paramanager.get_volatile_registers_mm(pocall_default));
  1677. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1678. paraloc3.done;
  1679. paraloc2.done;
  1680. paraloc1.done;
  1681. end;
  1682. procedure tcgaarch64.g_concatcopy(list: TAsmList; const source, dest: treference; len: tcgint);
  1683. var
  1684. sourcebasereplaced, destbasereplaced: boolean;
  1685. { get optimal memory operation to use for loading/storing data
  1686. in an unrolled loop }
  1687. procedure getmemop(scaledop, unscaledop: tasmop; const startref, endref: treference; opsize: tcgsize; postfix: toppostfix; out memop: tasmop; out needsimplify: boolean);
  1688. begin
  1689. if (simple_ref_type(scaledop,opsize,postfix,startref)=sr_simple) and
  1690. (simple_ref_type(scaledop,opsize,postfix,endref)=sr_simple) then
  1691. begin
  1692. memop:=unscaledop;
  1693. needsimplify:=true;
  1694. end
  1695. else if (unscaledop<>A_NONE) and
  1696. (simple_ref_type(unscaledop,opsize,postfix,startref)=sr_simple) and
  1697. (simple_ref_type(unscaledop,opsize,postfix,endref)=sr_simple) then
  1698. begin
  1699. memop:=unscaledop;
  1700. needsimplify:=false;
  1701. end
  1702. else
  1703. begin
  1704. memop:=scaledop;
  1705. needsimplify:=true;
  1706. end;
  1707. end;
  1708. { adjust the offset and/or addressing mode after a load/store so it's
  1709. correct for the next one of the same size }
  1710. procedure updaterefafterloadstore(var ref: treference; oplen: longint);
  1711. begin
  1712. case ref.addressmode of
  1713. AM_OFFSET:
  1714. inc(ref.offset,oplen);
  1715. AM_POSTINDEXED:
  1716. { base register updated by instruction, next offset can remain
  1717. the same }
  1718. ;
  1719. AM_PREINDEXED:
  1720. begin
  1721. { base register updated by instruction -> next instruction can
  1722. use post-indexing with offset = sizeof(operation) }
  1723. ref.offset:=0;
  1724. ref.addressmode:=AM_OFFSET;
  1725. end;
  1726. end;
  1727. end;
  1728. { generate a load/store and adjust the reference offset to the next
  1729. memory location if necessary }
  1730. procedure genloadstore(list: TAsmList; op: tasmop; reg: tregister; var ref: treference; postfix: toppostfix; opsize: tcgsize);
  1731. begin
  1732. list.concat(setoppostfix(taicpu.op_reg_ref(op,reg,ref),postfix));
  1733. updaterefafterloadstore(ref,tcgsize2size[opsize]);
  1734. end;
  1735. { generate a dual load/store (ldp/stp) and adjust the reference offset to
  1736. the next memory location if necessary }
  1737. procedure gendualloadstore(list: TAsmList; op: tasmop; reg1, reg2: tregister; var ref: treference; postfix: toppostfix; opsize: tcgsize);
  1738. begin
  1739. list.concat(setoppostfix(taicpu.op_reg_reg_ref(op,reg1,reg2,ref),postfix));
  1740. updaterefafterloadstore(ref,tcgsize2size[opsize]*2);
  1741. end;
  1742. { turn a reference into a pre- or post-indexed reference for use in a
  1743. load/store of a particular size }
  1744. procedure makesimpleforcopy(list: TAsmList; var scaledop: tasmop; opsize: tcgsize; postfix: toppostfix; forcepostindexing: boolean; var ref: treference; var basereplaced: boolean);
  1745. var
  1746. tmpreg: tregister;
  1747. scaledoffset: longint;
  1748. orgaddressmode: taddressmode;
  1749. begin
  1750. scaledoffset:=tcgsize2size[opsize];
  1751. if scaledop in [A_LDP,A_STP] then
  1752. scaledoffset:=scaledoffset*2;
  1753. { can we use the reference as post-indexed without changes? }
  1754. if forcepostindexing then
  1755. begin
  1756. orgaddressmode:=ref.addressmode;
  1757. ref.addressmode:=AM_POSTINDEXED;
  1758. if (orgaddressmode=AM_POSTINDEXED) or
  1759. ((ref.offset=0) and
  1760. (simple_ref_type(scaledop,opsize,postfix,ref)=sr_simple)) then
  1761. begin
  1762. { just change the post-indexed offset to the access size }
  1763. ref.offset:=scaledoffset;
  1764. { and replace the base register if that didn't happen yet
  1765. (could be sp or a regvar) }
  1766. if not basereplaced then
  1767. begin
  1768. tmpreg:=getaddressregister(list);
  1769. a_load_reg_reg(list,OS_ADDR,OS_ADDR,ref.base,tmpreg);
  1770. ref.base:=tmpreg;
  1771. basereplaced:=true;
  1772. end;
  1773. exit;
  1774. end;
  1775. ref.addressmode:=orgaddressmode;
  1776. end;
  1777. {$ifdef dummy}
  1778. This could in theory be useful in case you have a concatcopy from
  1779. e.g. x1+255 to x1+267 *and* the reference is aligned, but this seems
  1780. very unlikely. Disabled because it still needs fixes, as it
  1781. also generates pre-indexed loads right now at the very end for the
  1782. left-over gencopies
  1783. { can we turn it into a pre-indexed reference for free? (after the
  1784. first operation, it will be turned into an offset one) }
  1785. if not forcepostindexing and
  1786. (ref.offset<>0) then
  1787. begin
  1788. orgaddressmode:=ref.addressmode;
  1789. ref.addressmode:=AM_PREINDEXED;
  1790. tmpreg:=ref.base;
  1791. if not basereplaced and
  1792. (ref.base=tmpreg) then
  1793. begin
  1794. tmpreg:=getaddressregister(list);
  1795. a_load_reg_reg(list,OS_ADDR,OS_ADDR,ref.base,tmpreg);
  1796. ref.base:=tmpreg;
  1797. basereplaced:=true;
  1798. end;
  1799. if simple_ref_type(scaledop,opsize,postfix,ref)<>sr_simple then
  1800. make_simple_ref(list,scaledop,opsize,postfix,ref,NR_NO);
  1801. exit;
  1802. end;
  1803. {$endif dummy}
  1804. if not forcepostindexing then
  1805. begin
  1806. ref.addressmode:=AM_OFFSET;
  1807. make_simple_ref(list,scaledop,opsize,postfix,ref,NR_NO);
  1808. { this may still cause problems if the final offset is no longer
  1809. a simple ref; it's a bit complicated to pass all information
  1810. through at all places and check that here, so play safe: we
  1811. currently never generate unrolled copies for more than 64
  1812. bytes (32 with non-double-register copies) }
  1813. if ref.index=NR_NO then
  1814. begin
  1815. if ((scaledop in [A_LDP,A_STP]) and
  1816. (ref.offset<((64-8)*tcgsize2size[opsize]))) or
  1817. ((scaledop in [A_LDUR,A_STUR]) and
  1818. (ref.offset<(255-8*tcgsize2size[opsize]))) or
  1819. ((scaledop in [A_LDR,A_STR]) and
  1820. (ref.offset<((4096-8)*tcgsize2size[opsize]))) then
  1821. exit;
  1822. end;
  1823. end;
  1824. tmpreg:=getaddressregister(list);
  1825. a_loadaddr_ref_reg(list,ref,tmpreg);
  1826. basereplaced:=true;
  1827. if forcepostindexing then
  1828. begin
  1829. reference_reset_base(ref,tmpreg,scaledoffset,ref.temppos,ref.alignment,ref.volatility);
  1830. ref.addressmode:=AM_POSTINDEXED;
  1831. end
  1832. else
  1833. begin
  1834. reference_reset_base(ref,tmpreg,0,ref.temppos,ref.alignment,ref.volatility);
  1835. ref.addressmode:=AM_OFFSET;
  1836. end
  1837. end;
  1838. { prepare a reference for use by gencopy. This is done both after the
  1839. unrolled and regular copy loop -> get rid of post-indexing mode, make
  1840. sure ref is valid }
  1841. procedure preparecopy(list: tasmlist; scaledop, unscaledop: tasmop; var ref: treference; opsize: tcgsize; postfix: toppostfix; out op: tasmop; var basereplaced: boolean);
  1842. var
  1843. simplify: boolean;
  1844. begin
  1845. if ref.addressmode=AM_POSTINDEXED then
  1846. ref.offset:=tcgsize2size[opsize];
  1847. getmemop(scaledop,scaledop,ref,ref,opsize,postfix,op,simplify);
  1848. if simplify then
  1849. begin
  1850. makesimpleforcopy(list,scaledop,opsize,postfix,false,ref,basereplaced);
  1851. op:=scaledop;
  1852. end;
  1853. end;
  1854. { generate a copy from source to dest of size opsize/postfix }
  1855. procedure gencopy(list: TAsmList; var source, dest: treference; postfix: toppostfix; opsize: tcgsize);
  1856. var
  1857. reg: tregister;
  1858. loadop, storeop: tasmop;
  1859. begin
  1860. preparecopy(list,A_LDR,A_LDUR,source,opsize,postfix,loadop,sourcebasereplaced);
  1861. preparecopy(list,A_STR,A_STUR,dest,opsize,postfix,storeop,destbasereplaced);
  1862. reg:=getintregister(list,opsize);
  1863. genloadstore(list,loadop,reg,source,postfix,opsize);
  1864. genloadstore(list,storeop,reg,dest,postfix,opsize);
  1865. end;
  1866. { copy the leftovers after an unrolled or regular copy loop }
  1867. procedure gencopyleftovers(list: TAsmList; var source, dest: treference; len: longint);
  1868. begin
  1869. { stop post-indexing if we did so in the loop, since in that case all
  1870. offsets definitely can be represented now }
  1871. if source.addressmode=AM_POSTINDEXED then
  1872. begin
  1873. source.addressmode:=AM_OFFSET;
  1874. source.offset:=0;
  1875. end;
  1876. if dest.addressmode=AM_POSTINDEXED then
  1877. begin
  1878. dest.addressmode:=AM_OFFSET;
  1879. dest.offset:=0;
  1880. end;
  1881. { transfer the leftovers }
  1882. if len>=8 then
  1883. begin
  1884. dec(len,8);
  1885. gencopy(list,source,dest,PF_NONE,OS_64);
  1886. end;
  1887. if len>=4 then
  1888. begin
  1889. dec(len,4);
  1890. gencopy(list,source,dest,PF_NONE,OS_32);
  1891. end;
  1892. if len>=2 then
  1893. begin
  1894. dec(len,2);
  1895. gencopy(list,source,dest,PF_H,OS_16);
  1896. end;
  1897. if len>=1 then
  1898. begin
  1899. dec(len);
  1900. gencopy(list,source,dest,PF_B,OS_8);
  1901. end;
  1902. end;
  1903. const
  1904. { load_length + loop dec + cbnz }
  1905. loopoverhead=12;
  1906. { loop overhead + load + store }
  1907. totallooplen=loopoverhead + 8;
  1908. var
  1909. totalalign: longint;
  1910. maxlenunrolled: tcgint;
  1911. loadop, storeop: tasmop;
  1912. opsize: tcgsize;
  1913. postfix: toppostfix;
  1914. tmpsource, tmpdest: treference;
  1915. scaledstoreop, unscaledstoreop,
  1916. scaledloadop, unscaledloadop: tasmop;
  1917. regs: array[1..8] of tregister;
  1918. countreg: tregister;
  1919. i, regcount: longint;
  1920. hl: tasmlabel;
  1921. simplifysource, simplifydest: boolean;
  1922. begin
  1923. if len=0 then
  1924. exit;
  1925. sourcebasereplaced:=false;
  1926. destbasereplaced:=false;
  1927. { maximum common alignment }
  1928. totalalign:=max(1,newalignment(source.alignment,dest.alignment));
  1929. { use a simple load/store? }
  1930. if (len in [1,2,4,8]) and
  1931. ((totalalign>=(len div 2)) or
  1932. (source.alignment=len) or
  1933. (dest.alignment=len)) then
  1934. begin
  1935. opsize:=int_cgsize(len);
  1936. a_load_ref_ref(list,opsize,opsize,source,dest);
  1937. exit;
  1938. end;
  1939. { alignment > length is not useful, and would break some checks below }
  1940. while totalalign>len do
  1941. totalalign:=totalalign div 2;
  1942. { operation sizes to use based on common alignment }
  1943. case totalalign of
  1944. 1:
  1945. begin
  1946. postfix:=PF_B;
  1947. opsize:=OS_8;
  1948. end;
  1949. 2:
  1950. begin
  1951. postfix:=PF_H;
  1952. opsize:=OS_16;
  1953. end;
  1954. 4:
  1955. begin
  1956. postfix:=PF_None;
  1957. opsize:=OS_32;
  1958. end
  1959. else
  1960. begin
  1961. totalalign:=8;
  1962. postfix:=PF_None;
  1963. opsize:=OS_64;
  1964. end;
  1965. end;
  1966. { maximum length to handled with an unrolled loop (4 loads + 4 stores) }
  1967. maxlenunrolled:=min(totalalign,8)*4;
  1968. { ldp/stp -> 2 registers per instruction }
  1969. if (totalalign>=4) and
  1970. (len>=totalalign*2) then
  1971. begin
  1972. maxlenunrolled:=maxlenunrolled*2;
  1973. scaledstoreop:=A_STP;
  1974. scaledloadop:=A_LDP;
  1975. unscaledstoreop:=A_NONE;
  1976. unscaledloadop:=A_NONE;
  1977. end
  1978. else
  1979. begin
  1980. scaledstoreop:=A_STR;
  1981. scaledloadop:=A_LDR;
  1982. unscaledstoreop:=A_STUR;
  1983. unscaledloadop:=A_LDUR;
  1984. end;
  1985. { we only need 4 instructions extra to call FPC_MOVE }
  1986. if cs_opt_size in current_settings.optimizerswitches then
  1987. maxlenunrolled:=maxlenunrolled div 2;
  1988. if (len>maxlenunrolled) and
  1989. (len>totalalign*8) then
  1990. begin
  1991. g_concatcopy_move(list,source,dest,len);
  1992. exit;
  1993. end;
  1994. simplifysource:=true;
  1995. simplifydest:=true;
  1996. tmpsource:=source;
  1997. tmpdest:=dest;
  1998. { can we directly encode all offsets in an unrolled loop? }
  1999. if len<=maxlenunrolled then
  2000. begin
  2001. {$ifdef extdebug}
  2002. list.concat(tai_comment.Create(strpnew('concatcopy unrolled loop; len/opsize/align: '+tostr(len)+'/'+tostr(tcgsize2size[opsize])+'/'+tostr(totalalign))));
  2003. {$endif extdebug}
  2004. { the leftovers will be handled separately -> -(len mod opsize) }
  2005. inc(tmpsource.offset,len-(len mod tcgsize2size[opsize]));
  2006. { additionally, the last regular load/store will be at
  2007. offset+len-opsize (if len-(len mod opsize)>len) }
  2008. if tmpsource.offset>source.offset then
  2009. dec(tmpsource.offset,tcgsize2size[opsize]);
  2010. getmemop(scaledloadop,unscaledloadop,source,tmpsource,opsize,postfix,loadop,simplifysource);
  2011. inc(tmpdest.offset,len-(len mod tcgsize2size[opsize]));
  2012. if tmpdest.offset>dest.offset then
  2013. dec(tmpdest.offset,tcgsize2size[opsize]);
  2014. getmemop(scaledstoreop,unscaledstoreop,dest,tmpdest,opsize,postfix,storeop,simplifydest);
  2015. tmpsource:=source;
  2016. tmpdest:=dest;
  2017. { if we can't directly encode all offsets, simplify }
  2018. if simplifysource then
  2019. begin
  2020. loadop:=scaledloadop;
  2021. makesimpleforcopy(list,loadop,opsize,postfix,false,tmpsource,sourcebasereplaced);
  2022. end;
  2023. if simplifydest then
  2024. begin
  2025. storeop:=scaledstoreop;
  2026. makesimpleforcopy(list,storeop,opsize,postfix,false,tmpdest,destbasereplaced);
  2027. end;
  2028. regcount:=len div tcgsize2size[opsize];
  2029. { in case we transfer two registers at a time, we copy an even
  2030. number of registers }
  2031. if loadop=A_LDP then
  2032. regcount:=regcount and not(1);
  2033. { initialise for dfa }
  2034. regs[low(regs)]:=NR_NO;
  2035. { max 4 loads/stores -> max 8 registers (in case of ldp/stdp) }
  2036. for i:=1 to regcount do
  2037. regs[i]:=getintregister(list,opsize);
  2038. if loadop=A_LDP then
  2039. begin
  2040. { load registers }
  2041. for i:=1 to (regcount div 2) do
  2042. gendualloadstore(list,loadop,regs[i*2-1],regs[i*2],tmpsource,postfix,opsize);
  2043. { store registers }
  2044. for i:=1 to (regcount div 2) do
  2045. gendualloadstore(list,storeop,regs[i*2-1],regs[i*2],tmpdest,postfix,opsize);
  2046. end
  2047. else
  2048. begin
  2049. for i:=1 to regcount do
  2050. genloadstore(list,loadop,regs[i],tmpsource,postfix,opsize);
  2051. for i:=1 to regcount do
  2052. genloadstore(list,storeop,regs[i],tmpdest,postfix,opsize);
  2053. end;
  2054. { leftover }
  2055. len:=len-regcount*tcgsize2size[opsize];
  2056. {$ifdef extdebug}
  2057. list.concat(tai_comment.Create(strpnew('concatcopy unrolled loop leftover: '+tostr(len))));
  2058. {$endif extdebug}
  2059. end
  2060. else
  2061. begin
  2062. {$ifdef extdebug}
  2063. list.concat(tai_comment.Create(strpnew('concatcopy regular loop; len/align: '+tostr(len)+'/'+tostr(totalalign))));
  2064. {$endif extdebug}
  2065. { regular loop -> definitely use post-indexing }
  2066. loadop:=scaledloadop;
  2067. makesimpleforcopy(list,loadop,opsize,postfix,true,tmpsource,sourcebasereplaced);
  2068. storeop:=scaledstoreop;
  2069. makesimpleforcopy(list,storeop,opsize,postfix,true,tmpdest,destbasereplaced);
  2070. current_asmdata.getjumplabel(hl);
  2071. countreg:=getintregister(list,OS_32);
  2072. if loadop=A_LDP then
  2073. a_load_const_reg(list,OS_32,len div tcgsize2size[opsize]*2,countreg)
  2074. else
  2075. a_load_const_reg(list,OS_32,len div tcgsize2size[opsize],countreg);
  2076. a_label(list,hl);
  2077. a_op_const_reg(list,OP_SUB,OS_32,1,countreg);
  2078. if loadop=A_LDP then
  2079. begin
  2080. regs[1]:=getintregister(list,opsize);
  2081. regs[2]:=getintregister(list,opsize);
  2082. gendualloadstore(list,loadop,regs[1],regs[2],tmpsource,postfix,opsize);
  2083. gendualloadstore(list,storeop,regs[1],regs[2],tmpdest,postfix,opsize);
  2084. end
  2085. else
  2086. begin
  2087. regs[1]:=getintregister(list,opsize);
  2088. genloadstore(list,loadop,regs[1],tmpsource,postfix,opsize);
  2089. genloadstore(list,storeop,regs[1],tmpdest,postfix,opsize);
  2090. end;
  2091. list.concat(taicpu.op_reg_sym_ofs(A_CBNZ,countreg,hl,0));
  2092. len:=len mod tcgsize2size[opsize];
  2093. end;
  2094. gencopyleftovers(list,tmpsource,tmpdest,len);
  2095. end;
  2096. procedure tcgaarch64.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  2097. begin
  2098. { This method is integrated into g_intf_wrapper and shouldn't be called separately }
  2099. InternalError(2013020102);
  2100. end;
  2101. procedure tcgaarch64.g_check_for_fpu_exception(list: TAsmList;force,clear : boolean);
  2102. var
  2103. r : TRegister;
  2104. ai: taicpu;
  2105. l1,l2: TAsmLabel;
  2106. begin
  2107. { so far, we assume all flavours of AArch64 need explicit floating point exception checking }
  2108. if ((cs_check_fpu_exceptions in current_settings.localswitches) and
  2109. (force or current_procinfo.FPUExceptionCheckNeeded)) then
  2110. begin
  2111. r:=getintregister(list,OS_INT);
  2112. list.concat(taicpu.op_reg_reg(A_MRS,r,NR_FPSR));
  2113. list.concat(taicpu.op_reg_const(A_TST,r,$1f));
  2114. current_asmdata.getjumplabel(l1);
  2115. current_asmdata.getjumplabel(l2);
  2116. ai:=taicpu.op_sym(A_B,l1);
  2117. ai.is_jmp:=true;
  2118. ai.condition:=C_NE;
  2119. list.concat(ai);
  2120. list.concat(taicpu.op_reg_const(A_TST,r,$80));
  2121. ai:=taicpu.op_sym(A_B,l2);
  2122. ai.is_jmp:=true;
  2123. ai.condition:=C_EQ;
  2124. list.concat(ai);
  2125. a_label(list,l1);
  2126. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  2127. cg.a_call_name(list,'FPC_THROWFPUEXCEPTION',false);
  2128. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  2129. a_label(list,l2);
  2130. if clear then
  2131. current_procinfo.FPUExceptionCheckNeeded:=false;
  2132. end;
  2133. end;
  2134. procedure create_codegen;
  2135. begin
  2136. cg:=tcgaarch64.Create;
  2137. cg128:=tcg128.Create;
  2138. end;
  2139. end.