cgcpu.pas 86 KB

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