cgcpu.pas 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208
  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,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.alignment);
  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.alignment);
  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.alignment);
  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.alignment);
  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,newalignment(8,ref.offset));
  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)))
  522. else
  523. list.concat(taicpu.op_sym(A_BL,current_asmdata.WeakRefAsmSymbol(s)));
  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 (high(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. fromsize:=tosize
  653. { have a 32 bit register but need a 64 bit one? }
  654. else if tosize in [OS_64,OS_S64] then
  655. begin
  656. { sign extend if necessary }
  657. if fromsize in [OS_S8,OS_S16,OS_S32] then
  658. begin
  659. { can't overwrite reg, may be a constant reg }
  660. hreg:=getintregister(list,tosize);
  661. a_load_reg_reg(list,fromsize,tosize,reg,hreg);
  662. reg:=hreg;
  663. end
  664. else
  665. { top 32 bit are zero by default }
  666. reg:=makeregsize(reg,OS_64);
  667. fromsize:=tosize;
  668. end;
  669. if (ref.alignment<>0) and
  670. (ref.alignment<tcgsize2size[tosize]) then
  671. begin
  672. a_load_reg_ref_unaligned(list,fromsize,tosize,reg,ref);
  673. end
  674. else
  675. begin
  676. case tosize of
  677. { signed integer registers }
  678. OS_8,
  679. OS_S8:
  680. oppostfix:=PF_B;
  681. OS_16,
  682. OS_S16:
  683. oppostfix:=PF_H;
  684. OS_32,
  685. OS_S32,
  686. OS_64,
  687. OS_S64:
  688. oppostfix:=PF_None;
  689. else
  690. InternalError(200308299);
  691. end;
  692. handle_load_store(list,A_STR,tosize,oppostfix,reg,ref);
  693. end;
  694. end;
  695. procedure tcgaarch64.a_load_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  696. var
  697. oppostfix:toppostfix;
  698. begin
  699. if tcgsize2Size[fromsize]>=tcgsize2Size[tosize] then
  700. fromsize:=tosize;
  701. { ensure that all bits of the 32/64 register are always correctly set:
  702. * default behaviour is always to zero-extend to the entire (64 bit)
  703. register -> unsigned 8/16/32 bit loads only exist with a 32 bit
  704. target register, as the upper 32 bit will be zeroed implicitly
  705. -> always make target register 32 bit
  706. * signed loads exist both with 32 and 64 bit target registers,
  707. depending on whether the value should be sign extended to 32 or
  708. to 64 bit (if sign extended to 32 bit, the upper 32 bits of the
  709. corresponding 64 bit register are again zeroed) -> no need to
  710. change anything (we only have 32 and 64 bit registers), except that
  711. when loading an OS_S32 to a 32 bit register, we don't need/can't
  712. use sign extension
  713. }
  714. if fromsize in [OS_8,OS_16,OS_32] then
  715. reg:=makeregsize(reg,OS_32);
  716. if (ref.alignment<>0) and
  717. (ref.alignment<tcgsize2size[fromsize]) then
  718. begin
  719. a_load_ref_reg_unaligned(list,fromsize,tosize,ref,reg);
  720. exit;
  721. end;
  722. case fromsize of
  723. { signed integer registers }
  724. OS_8:
  725. oppostfix:=PF_B;
  726. OS_S8:
  727. oppostfix:=PF_SB;
  728. OS_16:
  729. oppostfix:=PF_H;
  730. OS_S16:
  731. oppostfix:=PF_SH;
  732. OS_S32:
  733. if getsubreg(reg)=R_SUBD then
  734. oppostfix:=PF_NONE
  735. else
  736. oppostfix:=PF_SW;
  737. OS_32,
  738. OS_64,
  739. OS_S64:
  740. oppostfix:=PF_None;
  741. else
  742. InternalError(200308297);
  743. end;
  744. handle_load_store(list,A_LDR,fromsize,oppostfix,reg,ref);
  745. { clear upper 16 bits if the value was negative }
  746. if (fromsize=OS_S8) and (tosize=OS_16) then
  747. a_load_reg_reg(list,fromsize,tosize,reg,reg);
  748. end;
  749. procedure tcgaarch64.a_load_ref_reg_unaligned(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; register: tregister);
  750. var
  751. href: treference;
  752. hreg1, hreg2, tmpreg: tregister;
  753. begin
  754. if fromsize in [OS_64,OS_S64] then
  755. begin
  756. { split into two 32 bit loads }
  757. hreg1:=makeregsize(register,OS_32);
  758. hreg2:=getintregister(list,OS_32);
  759. if target_info.endian=endian_big then
  760. begin
  761. tmpreg:=hreg1;
  762. hreg1:=hreg2;
  763. hreg2:=tmpreg;
  764. end;
  765. { can we use LDP? }
  766. if (ref.alignment=4) and
  767. (simple_ref_type(A_LDP,OS_32,PF_None,ref)=sr_simple) then
  768. list.concat(taicpu.op_reg_reg_ref(A_LDP,hreg1,hreg2,ref))
  769. else
  770. begin
  771. a_load_ref_reg(list,OS_32,OS_32,ref,hreg1);
  772. href:=ref;
  773. inc(href.offset,4);
  774. a_load_ref_reg(list,OS_32,OS_32,href,hreg2);
  775. end;
  776. list.concat(taicpu.op_reg_reg_const_const(A_BFI,register,makeregsize(hreg2,OS_64),32,32));
  777. end
  778. else
  779. inherited;
  780. end;
  781. procedure tcgaarch64.a_load_reg_reg(list:TAsmList;fromsize,tosize:tcgsize;reg1,reg2:tregister);
  782. var
  783. instr: taicpu;
  784. begin
  785. { we use both 32 and 64 bit registers -> insert conversion when when
  786. we have to truncate/sign extend inside the (32 or 64 bit) register
  787. holding the value, and when we sign extend from a 32 to a 64 bit
  788. register }
  789. if (tcgsize2size[fromsize]>tcgsize2size[tosize]) or
  790. ((tcgsize2size[fromsize]=tcgsize2size[tosize]) and
  791. (fromsize<>tosize) and
  792. not(fromsize in [OS_32,OS_S32,OS_64,OS_S64])) or
  793. ((fromsize in [OS_S8,OS_S16,OS_S32]) and
  794. (tosize in [OS_64,OS_S64])) or
  795. { needs to mask out the sign in the top 16 bits }
  796. ((fromsize=OS_S8) and
  797. (tosize=OS_16)) then
  798. begin
  799. case tosize of
  800. OS_8:
  801. list.concat(setoppostfix(taicpu.op_reg_reg(A_UXT,reg2,makeregsize(reg1,OS_32)),PF_B));
  802. OS_16:
  803. list.concat(setoppostfix(taicpu.op_reg_reg(A_UXT,reg2,makeregsize(reg1,OS_32)),PF_H));
  804. OS_S8:
  805. list.concat(setoppostfix(taicpu.op_reg_reg(A_SXT,reg2,makeregsize(reg1,OS_32)),PF_B));
  806. OS_S16:
  807. list.concat(setoppostfix(taicpu.op_reg_reg(A_SXT,reg2,makeregsize(reg1,OS_32)),PF_H));
  808. { while "mov wN, wM" automatically inserts a zero-extension and
  809. hence we could encode a 64->32 bit move like that, the problem
  810. is that we then can't distinguish 64->32 from 32->32 moves, and
  811. the 64->32 truncation could be removed altogether... So use a
  812. different instruction }
  813. OS_32,
  814. OS_S32:
  815. { in theory, reg1 should be 64 bit here (since fromsize>tosize),
  816. but because of the way location_force_register() tries to
  817. avoid superfluous zero/sign extensions, it's not always the
  818. case -> also force reg1 to to 64 bit }
  819. list.concat(taicpu.op_reg_reg_const_const(A_UBFIZ,makeregsize(reg2,OS_64),makeregsize(reg1,OS_64),0,32));
  820. OS_64,
  821. OS_S64:
  822. list.concat(setoppostfix(taicpu.op_reg_reg(A_SXT,reg2,makeregsize(reg1,OS_32)),PF_W));
  823. else
  824. internalerror(2002090901);
  825. end;
  826. end
  827. else
  828. begin
  829. { 32 -> 32 bit move implies zero extension (sign extensions have
  830. been handled above) -> also use for 32 <-> 64 bit moves }
  831. if not(fromsize in [OS_64,OS_S64]) or
  832. not(tosize in [OS_64,OS_S64]) then
  833. instr:=taicpu.op_reg_reg(A_MOV,makeregsize(reg2,OS_32),makeregsize(reg1,OS_32))
  834. else
  835. instr:=taicpu.op_reg_reg(A_MOV,reg2,reg1);
  836. list.Concat(instr);
  837. { Notify the register allocator that we have written a move instruction so
  838. it can try to eliminate it. }
  839. add_move_instruction(instr);
  840. end;
  841. end;
  842. procedure tcgaarch64.a_loadaddr_ref_reg(list: TAsmList; const ref: treference; r: tregister);
  843. var
  844. href: treference;
  845. so: tshifterop;
  846. op: tasmop;
  847. begin
  848. op:=A_LDR;
  849. href:=ref;
  850. { simplify as if we're going to perform a regular 64 bit load, using
  851. "r" as the new base register if possible/necessary }
  852. make_simple_ref(list,op,OS_ADDR,PF_None,href,r);
  853. { load literal? }
  854. if assigned(href.symbol) then
  855. begin
  856. if (href.base<>NR_NO) or
  857. (href.index<>NR_NO) or
  858. not assigned(href.symboldata) then
  859. internalerror(2014110912);
  860. list.concat(taicpu.op_reg_sym_ofs(A_ADR,r,href.symbol,href.offset));
  861. end
  862. else
  863. begin
  864. if href.index<>NR_NO then
  865. begin
  866. if href.shiftmode<>SM_None then
  867. begin
  868. { "add" supports a supperset of the shift modes supported by
  869. load/store instructions }
  870. shifterop_reset(so);
  871. so.shiftmode:=href.shiftmode;
  872. so.shiftimm:=href.shiftimm;
  873. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,r,href.base,href.index,so));
  874. end
  875. else
  876. a_op_reg_reg_reg(list,OP_ADD,OS_ADDR,href.index,href.base,r);
  877. end
  878. else if href.offset<>0 then
  879. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,href.offset,href.base,r)
  880. else
  881. a_load_reg_reg(list,OS_ADDR,OS_ADDR,href.base,r);
  882. end;
  883. end;
  884. procedure tcgaarch64.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  885. begin
  886. internalerror(2014122107)
  887. end;
  888. procedure tcgaarch64.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  889. begin
  890. internalerror(2014122108)
  891. end;
  892. procedure tcgaarch64.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference);
  893. begin
  894. internalerror(2014122109)
  895. end;
  896. procedure tcgaarch64.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister; shuffle: pmmshuffle);
  897. var
  898. instr: taicpu;
  899. begin
  900. if assigned(shuffle) and
  901. not shufflescalar(shuffle) then
  902. internalerror(2014122104);
  903. if fromsize=tosize then
  904. begin
  905. instr:=taicpu.op_reg_reg(A_FMOV,reg2,reg1);
  906. { Notify the register allocator that we have written a move
  907. instruction so it can try to eliminate it. }
  908. add_move_instruction(instr);
  909. end
  910. else
  911. begin
  912. if (reg_cgsize(reg1)<>fromsize) or
  913. (reg_cgsize(reg2)<>tosize) then
  914. internalerror(2014110913);
  915. instr:=taicpu.op_reg_reg(A_FCVT,reg2,reg1);
  916. end;
  917. list.Concat(instr);
  918. end;
  919. procedure tcgaarch64.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  920. var
  921. tmpreg: tregister;
  922. begin
  923. if assigned(shuffle) and
  924. not shufflescalar(shuffle) then
  925. internalerror(2014122105);
  926. tmpreg:=NR_NO;
  927. if (fromsize<>tosize) then
  928. begin
  929. tmpreg:=reg;
  930. reg:=getmmregister(list,fromsize);
  931. end;
  932. handle_load_store(list,A_LDR,fromsize,PF_None,reg,ref);
  933. if (fromsize<>tosize) then
  934. a_loadmm_reg_reg(list,fromsize,tosize,reg,tmpreg,nil);
  935. end;
  936. procedure tcgaarch64.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  937. var
  938. tmpreg: tregister;
  939. begin
  940. if assigned(shuffle) and
  941. not shufflescalar(shuffle) then
  942. internalerror(2014122106);
  943. if (fromsize<>tosize) then
  944. begin
  945. tmpreg:=getmmregister(list,tosize);
  946. a_loadmm_reg_reg(list,fromsize,tosize,reg,tmpreg,nil);
  947. reg:=tmpreg;
  948. end;
  949. handle_load_store(list,A_STR,tosize,PF_NONE,reg,ref);
  950. end;
  951. procedure tcgaarch64.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle);
  952. begin
  953. if not shufflescalar(shuffle) then
  954. internalerror(2014122801);
  955. if not(tcgsize2size[fromsize] in [4,8]) or
  956. (tcgsize2size[fromsize]<>tcgsize2size[tosize]) then
  957. internalerror(2014122803);
  958. list.concat(taicpu.op_reg_reg(A_INS,mmreg,intreg));
  959. end;
  960. procedure tcgaarch64.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tcgsize; mmreg, intreg: tregister; shuffle: pmmshuffle);
  961. begin
  962. if not shufflescalar(shuffle) then
  963. internalerror(2014122802);
  964. if not(tcgsize2size[fromsize] in [4,8]) or
  965. (tcgsize2size[fromsize]<>tcgsize2size[tosize]) then
  966. internalerror(2014122804);
  967. list.concat(taicpu.op_reg_reg(A_UMOV,intreg,mmreg));
  968. end;
  969. procedure tcgaarch64.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tcgsize; src, dst: tregister; shuffle: pmmshuffle);
  970. begin
  971. case op of
  972. { "xor Vx,Vx" is used to initialize global regvars to 0 }
  973. OP_XOR:
  974. begin
  975. if (src<>dst) or
  976. (reg_cgsize(src)<>size) or
  977. assigned(shuffle) then
  978. internalerror(2015011401);
  979. case size of
  980. OS_F32,
  981. OS_F64:
  982. list.concat(taicpu.op_reg_const(A_MOVI,makeregsize(dst,OS_F64),0));
  983. else
  984. internalerror(2015011402);
  985. end;
  986. end
  987. else
  988. internalerror(2015011403);
  989. end;
  990. end;
  991. procedure tcgaarch64.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tcgsize; src, dst: TRegister);
  992. var
  993. bitsize,
  994. signbit: longint;
  995. begin
  996. if srcsize in [OS_64,OS_S64] then
  997. begin
  998. bitsize:=64;
  999. signbit:=6;
  1000. end
  1001. else
  1002. begin
  1003. bitsize:=32;
  1004. signbit:=5;
  1005. end;
  1006. { source is 0 -> dst will have to become 255 }
  1007. list.concat(taicpu.op_reg_const(A_CMP,src,0));
  1008. if reverse then
  1009. begin
  1010. list.Concat(taicpu.op_reg_reg(A_CLZ,makeregsize(dst,srcsize),src));
  1011. { xor 31/63 is the same as setting the lower 5/6 bits to
  1012. "31/63-(lower 5/6 bits of dst)" }
  1013. list.Concat(taicpu.op_reg_reg_const(A_EOR,dst,dst,bitsize-1));
  1014. end
  1015. else
  1016. begin
  1017. list.Concat(taicpu.op_reg_reg(A_RBIT,makeregsize(dst,srcsize),src));
  1018. list.Concat(taicpu.op_reg_reg(A_CLZ,dst,dst));
  1019. end;
  1020. { set dst to -1 if src was 0 }
  1021. list.Concat(taicpu.op_reg_reg_reg_cond(A_CSINV,dst,dst,makeregsize(NR_XZR,dstsize),C_NE));
  1022. { mask the -1 to 255 if src was 0 (anyone find a two-instruction
  1023. branch-free version? All of mine are 3...) }
  1024. list.Concat(setoppostfix(taicpu.op_reg_reg(A_UXT,makeregsize(dst,OS_32),makeregsize(dst,OS_32)),PF_B));
  1025. end;
  1026. procedure tcgaarch64.a_load_reg_ref_unaligned(list: TAsmList; fromsize, tosize: tcgsize; register: tregister; const ref: treference);
  1027. var
  1028. href: treference;
  1029. hreg1, hreg2, tmpreg: tregister;
  1030. begin
  1031. if fromsize in [OS_64,OS_S64] then
  1032. begin
  1033. { split into two 32 bit stores }
  1034. hreg1:=makeregsize(register,OS_32);
  1035. hreg2:=getintregister(list,OS_32);
  1036. a_op_const_reg_reg(list,OP_SHR,OS_64,32,register,makeregsize(hreg2,OS_64));
  1037. if target_info.endian=endian_big then
  1038. begin
  1039. tmpreg:=hreg1;
  1040. hreg1:=hreg2;
  1041. hreg2:=tmpreg;
  1042. end;
  1043. { can we use STP? }
  1044. if (ref.alignment=4) and
  1045. (simple_ref_type(A_STP,OS_32,PF_None,ref)=sr_simple) then
  1046. list.concat(taicpu.op_reg_reg_ref(A_STP,hreg1,hreg2,ref))
  1047. else
  1048. begin
  1049. a_load_reg_ref(list,OS_32,OS_32,hreg1,ref);
  1050. href:=ref;
  1051. inc(href.offset,4);
  1052. a_load_reg_ref(list,OS_32,OS_32,hreg2,href);
  1053. end;
  1054. end
  1055. else
  1056. inherited;
  1057. end;
  1058. procedure tcgaarch64.maybeadjustresult(list: TAsmList; op: topcg; size: tcgsize; dst: tregister);
  1059. const
  1060. overflowops = [OP_MUL,OP_IMUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  1061. begin
  1062. if (op in overflowops) and
  1063. (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  1064. a_load_reg_reg(list,OS_32,size,makeregsize(dst,OS_32),makeregsize(dst,OS_32))
  1065. end;
  1066. procedure tcgaarch64.a_op_const_reg(list: TAsmList; op: topcg; size: tcgsize; a: tcgint; reg: tregister);
  1067. begin
  1068. optimize_op_const(size,op,a);
  1069. case op of
  1070. OP_NONE:
  1071. exit;
  1072. OP_MOVE:
  1073. a_load_const_reg(list,size,a,reg);
  1074. OP_NEG,OP_NOT:
  1075. internalerror(200306011);
  1076. else
  1077. a_op_const_reg_reg(list,op,size,a,reg,reg);
  1078. end;
  1079. end;
  1080. procedure tcgaarch64.a_op_reg_reg(list:TAsmList;op:topcg;size:tcgsize;src,dst:tregister);
  1081. begin
  1082. Case op of
  1083. OP_NEG,
  1084. OP_NOT:
  1085. begin
  1086. list.concat(taicpu.op_reg_reg(TOpCG2AsmOpReg[op],dst,src));
  1087. maybeadjustresult(list,op,size,dst);
  1088. end
  1089. else
  1090. a_op_reg_reg_reg(list,op,size,src,dst,dst);
  1091. end;
  1092. end;
  1093. procedure tcgaarch64.a_op_const_reg_reg(list: TAsmList; op: topcg; size: tcgsize; a: tcgint; src, dst: tregister);
  1094. var
  1095. l: tlocation;
  1096. begin
  1097. a_op_const_reg_reg_checkoverflow(list,op,size,a,src,dst,false,l);
  1098. end;
  1099. procedure tcgaarch64.a_op_reg_reg_reg(list: TAsmList; op: topcg; size: tcgsize; src1, src2, dst: tregister);
  1100. var
  1101. hreg: tregister;
  1102. begin
  1103. { no ROLV opcode... }
  1104. if op=OP_ROL then
  1105. begin
  1106. case size of
  1107. OS_32,OS_S32,
  1108. OS_64,OS_S64:
  1109. begin
  1110. hreg:=getintregister(list,size);
  1111. a_load_const_reg(list,size,tcgsize2size[size]*8,hreg);
  1112. a_op_reg_reg(list,OP_SUB,size,src1,hreg);
  1113. a_op_reg_reg_reg(list,OP_ROR,size,hreg,src2,dst);
  1114. exit;
  1115. end;
  1116. else
  1117. internalerror(2014111005);
  1118. end;
  1119. end
  1120. else if (op=OP_ROR) and
  1121. not(size in [OS_32,OS_S32,OS_64,OS_S64]) then
  1122. internalerror(2014111006);
  1123. if TOpCG2AsmOpReg[op]=A_NONE then
  1124. internalerror(2014111007);
  1125. list.concat(taicpu.op_reg_reg_reg(TOpCG2AsmOpReg[op],dst,src2,src1));
  1126. maybeadjustresult(list,op,size,dst);
  1127. end;
  1128. procedure tcgaarch64.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: topcg; size: tcgsize; a: tcgint; src, dst: tregister; setflags : boolean; var ovloc : tlocation);
  1129. var
  1130. shiftcountmask: longint;
  1131. constreg: tregister;
  1132. begin
  1133. { add/sub instructions have only positive immediate operands }
  1134. if (op in [OP_ADD,OP_SUB]) and
  1135. (a<0) then
  1136. begin
  1137. if op=OP_ADD then
  1138. op:=op_SUB
  1139. else
  1140. op:=OP_ADD;
  1141. { avoid range/overflow error in case a = low(tcgint) }
  1142. {$push}{$r-}{$q-}
  1143. a:=-a;
  1144. {$pop}
  1145. end;
  1146. ovloc.loc:=LOC_VOID;
  1147. optimize_op_const(size,op,a);
  1148. case op of
  1149. OP_NONE:
  1150. begin
  1151. a_load_reg_reg(list,size,size,src,dst);
  1152. exit;
  1153. end;
  1154. OP_MOVE:
  1155. begin
  1156. a_load_const_reg(list,size,a,dst);
  1157. exit;
  1158. end;
  1159. end;
  1160. case op of
  1161. OP_ADD,
  1162. OP_SUB:
  1163. begin
  1164. handle_reg_imm12_reg(list,TOpCG2AsmOpImm[op],size,src,a,dst,NR_NO,setflags,true);
  1165. { on a 64 bit target, overflows with smaller data types
  1166. are handled via range errors }
  1167. if setflags and
  1168. (size in [OS_64,OS_S64]) then
  1169. begin
  1170. location_reset(ovloc,LOC_FLAGS,OS_8);
  1171. if size=OS_64 then
  1172. if op=OP_ADD then
  1173. ovloc.resflags:=F_CS
  1174. else
  1175. ovloc.resflags:=F_CC
  1176. else
  1177. ovloc.resflags:=F_VS;
  1178. end;
  1179. end;
  1180. OP_OR,
  1181. OP_AND,
  1182. OP_XOR:
  1183. begin
  1184. if not(size in [OS_64,OS_S64]) then
  1185. a:=cardinal(a);
  1186. if is_shifter_const(a,size) then
  1187. list.concat(taicpu.op_reg_reg_const(TOpCG2AsmOpReg[op],dst,src,a))
  1188. else
  1189. begin
  1190. constreg:=getintregister(list,size);
  1191. a_load_const_reg(list,size,a,constreg);
  1192. a_op_reg_reg_reg(list,op,size,constreg,src,dst);
  1193. end;
  1194. end;
  1195. OP_SHL,
  1196. OP_SHR,
  1197. OP_SAR:
  1198. begin
  1199. if size in [OS_64,OS_S64] then
  1200. shiftcountmask:=63
  1201. else
  1202. shiftcountmask:=31;
  1203. if (a and shiftcountmask)<>0 Then
  1204. list.concat(taicpu.op_reg_reg_const(
  1205. TOpCG2AsmOpImm[Op],dst,src,a and shiftcountmask))
  1206. else
  1207. a_load_reg_reg(list,size,size,src,dst);
  1208. if (a and not(tcgint(shiftcountmask)))<>0 then
  1209. internalError(2014112101);
  1210. end;
  1211. OP_ROL,
  1212. OP_ROR:
  1213. begin
  1214. case size of
  1215. OS_32,OS_S32:
  1216. if (a and not(tcgint(31)))<>0 then
  1217. internalError(2014112102);
  1218. OS_64,OS_S64:
  1219. if (a and not(tcgint(63)))<>0 then
  1220. internalError(2014112103);
  1221. else
  1222. internalError(2014112104);
  1223. end;
  1224. { there's only a ror opcode }
  1225. if op=OP_ROL then
  1226. a:=(tcgsize2size[size]*8)-a;
  1227. list.concat(taicpu.op_reg_reg_const(A_ROR,dst,src,a));
  1228. end;
  1229. OP_MUL,
  1230. OP_IMUL,
  1231. OP_DIV,
  1232. OP_IDIV:
  1233. begin
  1234. constreg:=getintregister(list,size);
  1235. a_load_const_reg(list,size,a,constreg);
  1236. a_op_reg_reg_reg_checkoverflow(list,op,size,constreg,src,dst,setflags,ovloc);
  1237. end;
  1238. else
  1239. internalerror(2014111403);
  1240. end;
  1241. maybeadjustresult(list,op,size,dst);
  1242. end;
  1243. procedure tcgaarch64.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: topcg; size: tcgsize; src1, src2, dst: tregister; setflags : boolean; var ovloc : tlocation);
  1244. var
  1245. tmpreg1: tregister;
  1246. begin
  1247. ovloc.loc:=LOC_VOID;
  1248. { overflow can only occur with 64 bit calculations on 64 bit cpus }
  1249. if setflags and
  1250. (size in [OS_64,OS_S64]) then
  1251. begin
  1252. case op of
  1253. OP_ADD,
  1254. OP_SUB:
  1255. begin
  1256. list.concat(setoppostfix(taicpu.op_reg_reg_reg(TOpCG2AsmOpReg[op],dst,src2,src1),PF_S));
  1257. ovloc.loc:=LOC_FLAGS;
  1258. if size=OS_64 then
  1259. if op=OP_ADD then
  1260. ovloc.resflags:=F_CS
  1261. else
  1262. ovloc.resflags:=F_CC
  1263. else
  1264. ovloc.resflags:=F_VS;
  1265. { finished; since we won't call through to a_op_reg_reg_reg,
  1266. adjust the result here if necessary }
  1267. maybeadjustresult(list,op,size,dst);
  1268. exit;
  1269. end;
  1270. OP_MUL:
  1271. begin
  1272. { check whether the upper 64 bit of the 128 bit product is 0 }
  1273. tmpreg1:=getintregister(list,OS_64);
  1274. list.concat(taicpu.op_reg_reg_reg(A_UMULH,tmpreg1,src2,src1));
  1275. list.concat(taicpu.op_reg_const(A_CMP,tmpreg1,0));
  1276. ovloc.loc:=LOC_FLAGS;
  1277. ovloc.resflags:=F_NE;
  1278. { still have to perform the actual multiplication }
  1279. end;
  1280. OP_IMUL:
  1281. begin
  1282. { check whether the sign bit of the (128 bit) result is the
  1283. same as "sign bit of src1" xor "signbit of src2" (if so, no
  1284. overflow and the xor-product of all sign bits is 0) }
  1285. tmpreg1:=getintregister(list,OS_64);
  1286. list.concat(taicpu.op_reg_reg_reg(A_SMULH,tmpreg1,src2,src1));
  1287. list.concat(taicpu.op_reg_reg_reg(A_EOR,tmpreg1,tmpreg1,src1));
  1288. list.concat(taicpu.op_reg_reg_reg(A_EOR,tmpreg1,tmpreg1,src2));
  1289. list.concat(taicpu.op_reg_const(A_TST,tmpreg1,$80000000));
  1290. ovloc.loc:=LOC_FLAGS;
  1291. ovloc.resflags:=F_NE;
  1292. { still have to perform the actual multiplication }
  1293. end;
  1294. OP_IDIV,
  1295. OP_DIV:
  1296. begin
  1297. { not handled here, needs div-by-zero check (dividing by zero
  1298. just gives a 0 result on aarch64), and low(int64) div -1
  1299. check for overflow) }
  1300. internalerror(2014122101);
  1301. end;
  1302. end;
  1303. end;
  1304. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1305. end;
  1306. {*************** compare instructructions ****************}
  1307. procedure tcgaarch64.a_cmp_const_reg_label(list: TAsmList; size: tcgsize; cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  1308. var
  1309. op: tasmop;
  1310. begin
  1311. if a>=0 then
  1312. op:=A_CMP
  1313. else
  1314. op:=A_CMN;
  1315. { avoid range/overflow error in case a=low(tcgint) }
  1316. {$push}{$r-}{$q-}
  1317. handle_reg_imm12_reg(list,op,size,reg,abs(a),NR_XZR,NR_NO,false,false);
  1318. {$pop}
  1319. a_jmp_cond(list,cmp_op,l);
  1320. end;
  1321. procedure tcgaarch64.a_cmp_reg_reg_label(list: TAsmList; size: tcgsize; cmp_op: topcmp; reg1,reg2: tregister; l: tasmlabel);
  1322. begin
  1323. list.concat(taicpu.op_reg_reg(A_CMP,reg2,reg1));
  1324. a_jmp_cond(list,cmp_op,l);
  1325. end;
  1326. procedure tcgaarch64.a_jmp_always(list: TAsmList; l: TAsmLabel);
  1327. var
  1328. ai: taicpu;
  1329. begin
  1330. ai:=TAiCpu.op_sym(A_B,current_asmdata.RefAsmSymbol(l.name));
  1331. ai.is_jmp:=true;
  1332. list.Concat(ai);
  1333. end;
  1334. procedure tcgaarch64.a_jmp_name(list: TAsmList; const s: string);
  1335. var
  1336. ai: taicpu;
  1337. begin
  1338. ai:=TAiCpu.op_sym(A_B,current_asmdata.RefAsmSymbol(s));
  1339. ai.is_jmp:=true;
  1340. list.Concat(ai);
  1341. end;
  1342. procedure tcgaarch64.a_jmp_cond(list: TAsmList; cond: TOpCmp; l: TAsmLabel);
  1343. var
  1344. ai: taicpu;
  1345. begin
  1346. ai:=TAiCpu.op_sym(A_B,l);
  1347. ai.is_jmp:=true;
  1348. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1349. list.Concat(ai);
  1350. end;
  1351. procedure tcgaarch64.a_jmp_flags(list: TAsmList; const f: tresflags; l: tasmlabel);
  1352. var
  1353. ai : taicpu;
  1354. begin
  1355. ai:=Taicpu.op_sym(A_B,l);
  1356. ai.is_jmp:=true;
  1357. ai.SetCondition(flags_to_cond(f));
  1358. list.Concat(ai);
  1359. end;
  1360. procedure tcgaarch64.g_flags2reg(list: TAsmList; size: tcgsize; const f: tresflags; reg: tregister);
  1361. begin
  1362. list.concat(taicpu.op_reg_cond(A_CSET,reg,flags_to_cond(f)));
  1363. end;
  1364. procedure tcgaarch64.g_overflowcheck(list: TAsmList; const loc: tlocation; def: tdef);
  1365. begin
  1366. { we need an explicit overflow location, because there are many
  1367. possibilities (not just the overflow flag, which is only used for
  1368. signed add/sub) }
  1369. internalerror(2014112303);
  1370. end;
  1371. procedure tcgaarch64.g_overflowcheck_loc(list: TAsmList; const loc: tlocation; def: tdef; ovloc : tlocation);
  1372. var
  1373. hl : tasmlabel;
  1374. hflags : tresflags;
  1375. begin
  1376. if not(cs_check_overflow in current_settings.localswitches) then
  1377. exit;
  1378. current_asmdata.getjumplabel(hl);
  1379. case ovloc.loc of
  1380. LOC_FLAGS:
  1381. begin
  1382. hflags:=ovloc.resflags;
  1383. inverse_flags(hflags);
  1384. cg.a_jmp_flags(list,hflags,hl);
  1385. end;
  1386. else
  1387. internalerror(2014112304);
  1388. end;
  1389. a_call_name(list,'FPC_OVERFLOW',false);
  1390. a_label(list,hl);
  1391. end;
  1392. { *********** entry/exit code and address loading ************ }
  1393. function tcgaarch64.save_regs(list: TAsmList; rt: tregistertype; lowsr, highsr: tsuperregister; sub: tsubregister): longint;
  1394. var
  1395. ref: treference;
  1396. sr: tsuperregister;
  1397. pairreg: tregister;
  1398. begin
  1399. result:=0;
  1400. reference_reset_base(ref,NR_SP,-16,16);
  1401. ref.addressmode:=AM_PREINDEXED;
  1402. pairreg:=NR_NO;
  1403. { store all used registers pairwise }
  1404. for sr:=lowsr to highsr do
  1405. if sr in rg[rt].used_in_proc then
  1406. if pairreg=NR_NO then
  1407. pairreg:=newreg(rt,sr,sub)
  1408. else
  1409. begin
  1410. inc(result,16);
  1411. list.concat(taicpu.op_reg_reg_ref(A_STP,pairreg,newreg(rt,sr,sub),ref));
  1412. pairreg:=NR_NO
  1413. end;
  1414. { one left -> store twice (stack must be 16 bytes aligned) }
  1415. if pairreg<>NR_NO then
  1416. begin
  1417. list.concat(taicpu.op_reg_reg_ref(A_STP,pairreg,pairreg,ref));
  1418. inc(result,16);
  1419. end;
  1420. end;
  1421. procedure FixupOffsets(p:TObject;arg:pointer);
  1422. var
  1423. sym: tabstractnormalvarsym absolute p;
  1424. begin
  1425. if (tsym(p).typ in [paravarsym,localvarsym]) and
  1426. (sym.localloc.loc=LOC_REFERENCE) and
  1427. (sym.localloc.reference.base=NR_STACK_POINTER_REG) then
  1428. begin
  1429. sym.localloc.reference.base:=NR_FRAME_POINTER_REG;
  1430. dec(sym.localloc.reference.offset,PLongint(arg)^);
  1431. end;
  1432. end;
  1433. procedure tcgaarch64.g_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean);
  1434. var
  1435. ref: treference;
  1436. totalstackframesize: longint;
  1437. begin
  1438. if nostackframe then
  1439. exit;
  1440. { stack pointer has to be aligned to 16 bytes at all times }
  1441. localsize:=align(localsize,16);
  1442. { save stack pointer and return address }
  1443. reference_reset_base(ref,NR_SP,-16,16);
  1444. ref.addressmode:=AM_PREINDEXED;
  1445. list.concat(taicpu.op_reg_reg_ref(A_STP,NR_FP,NR_LR,ref));
  1446. { initialise frame pointer }
  1447. a_load_reg_reg(list,OS_ADDR,OS_ADDR,NR_SP,NR_FP);
  1448. totalstackframesize:=localsize;
  1449. { save modified integer registers }
  1450. inc(totalstackframesize,
  1451. save_regs(list,R_INTREGISTER,RS_X19,RS_X28,R_SUBWHOLE));
  1452. { only the lower 64 bits of the modified vector registers need to be
  1453. saved; if the caller needs the upper 64 bits, it has to save them
  1454. itself }
  1455. inc(totalstackframesize,
  1456. save_regs(list,R_MMREGISTER,RS_D8,RS_D15,R_SUBMMD));
  1457. { allocate stack space }
  1458. if localsize<>0 then
  1459. begin
  1460. localsize:=align(localsize,16);
  1461. current_procinfo.final_localsize:=localsize;
  1462. handle_reg_imm12_reg(list,A_SUB,OS_ADDR,NR_SP,localsize,NR_SP,NR_IP0,false,true);
  1463. end;
  1464. { By default, we use the frame pointer to access parameters passed via
  1465. the stack and the stack pointer to address local variables and temps
  1466. because
  1467. a) we can use bigger positive than negative offsets (so accessing
  1468. locals via negative offsets from the frame pointer would be less
  1469. efficient)
  1470. b) we don't know the local size while generating the code, so
  1471. accessing the parameters via the stack pointer is not possible
  1472. without copying them
  1473. The problem with this is the get_frame() intrinsic:
  1474. a) it must return the same value as what we pass as parentfp
  1475. parameter, since that's how it's used in the TP-style objects unit
  1476. b) its return value must usable to access all local data from a
  1477. routine (locals and parameters), since it's all the nested
  1478. routines have access to
  1479. c) its return value must be usable to construct a backtrace, as it's
  1480. also used by the exception handling routines
  1481. The solution we use here, based on something similar that's done in
  1482. the MIPS port, is to generate all accesses to locals in the routine
  1483. itself SP-relative, and then after the code is generated and the local
  1484. size is known (namely, here), we change all SP-relative variables/
  1485. parameters into FP-relative ones. This means that they'll be accessed
  1486. less efficiently from nested routines, but those accesses are indirect
  1487. anyway and at least this way they can be accessed at all
  1488. }
  1489. if current_procinfo.has_nestedprocs then
  1490. begin
  1491. current_procinfo.procdef.localst.SymList.ForEachCall(@FixupOffsets,@totalstackframesize);
  1492. current_procinfo.procdef.parast.SymList.ForEachCall(@FixupOffsets,@totalstackframesize);
  1493. end;
  1494. end;
  1495. procedure tcgaarch64.g_maybe_got_init(list : TAsmList);
  1496. begin
  1497. { nothing to do on Darwin or Linux }
  1498. end;
  1499. procedure tcgaarch64.g_restore_registers(list:TAsmList);
  1500. begin
  1501. { done in g_proc_exit }
  1502. end;
  1503. procedure tcgaarch64.load_regs(list: TAsmList; rt: tregistertype; lowsr, highsr: tsuperregister; sub: tsubregister);
  1504. var
  1505. ref: treference;
  1506. sr, highestsetsr: tsuperregister;
  1507. pairreg: tregister;
  1508. regcount: longint;
  1509. begin
  1510. reference_reset_base(ref,NR_SP,16,16);
  1511. ref.addressmode:=AM_POSTINDEXED;
  1512. { highest reg stored twice? }
  1513. regcount:=0;
  1514. highestsetsr:=RS_NO;
  1515. for sr:=lowsr to highsr do
  1516. if sr in rg[rt].used_in_proc then
  1517. begin
  1518. inc(regcount);
  1519. highestsetsr:=sr;
  1520. end;
  1521. if odd(regcount) then
  1522. begin
  1523. list.concat(taicpu.op_reg_ref(A_LDR,newreg(rt,highestsetsr,sub),ref));
  1524. highestsetsr:=pred(highestsetsr);
  1525. end;
  1526. { load all (other) used registers pairwise }
  1527. pairreg:=NR_NO;
  1528. for sr:=highestsetsr downto lowsr do
  1529. if sr in rg[rt].used_in_proc then
  1530. if pairreg=NR_NO then
  1531. pairreg:=newreg(rt,sr,sub)
  1532. else
  1533. begin
  1534. list.concat(taicpu.op_reg_reg_ref(A_LDP,newreg(rt,sr,sub),pairreg,ref));
  1535. pairreg:=NR_NO
  1536. end;
  1537. { There can't be any register left }
  1538. if pairreg<>NR_NO then
  1539. internalerror(2014112602);
  1540. end;
  1541. procedure tcgaarch64.g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);
  1542. var
  1543. ref: treference;
  1544. regsstored: boolean;
  1545. sr: tsuperregister;
  1546. begin
  1547. if not nostackframe then
  1548. begin
  1549. { if no registers have been stored, we don't have to subtract the
  1550. allocated temp space from the stack pointer }
  1551. regsstored:=false;
  1552. for sr:=RS_X19 to RS_X28 do
  1553. if sr in rg[R_INTREGISTER].used_in_proc then
  1554. begin
  1555. regsstored:=true;
  1556. break;
  1557. end;
  1558. if not regsstored then
  1559. for sr:=RS_D8 to RS_D15 do
  1560. if sr in rg[R_MMREGISTER].used_in_proc then
  1561. begin
  1562. regsstored:=true;
  1563. break;
  1564. end;
  1565. { restore registers (and stack pointer) }
  1566. if regsstored then
  1567. begin
  1568. if current_procinfo.final_localsize<>0 then
  1569. handle_reg_imm12_reg(list,A_ADD,OS_ADDR,NR_SP,current_procinfo.final_localsize,NR_SP,NR_IP0,false,true);
  1570. load_regs(list,R_MMREGISTER,RS_D8,RS_D15,R_SUBMMD);
  1571. load_regs(list,R_INTREGISTER,RS_X19,RS_X28,R_SUBWHOLE);
  1572. end
  1573. else if current_procinfo.final_localsize<>0 then
  1574. { restore stack pointer }
  1575. a_load_reg_reg(list,OS_ADDR,OS_ADDR,NR_FP,NR_SP);
  1576. { restore framepointer and return address }
  1577. reference_reset_base(ref,NR_SP,16,16);
  1578. ref.addressmode:=AM_POSTINDEXED;
  1579. list.concat(taicpu.op_reg_reg_ref(A_LDP,NR_FP,NR_LR,ref));
  1580. end;
  1581. { return }
  1582. list.concat(taicpu.op_none(A_RET));
  1583. end;
  1584. procedure tcgaarch64.g_save_registers(list : TAsmList);
  1585. begin
  1586. { done in g_proc_entry }
  1587. end;
  1588. { ************* concatcopy ************ }
  1589. procedure tcgaarch64.g_concatcopy_move(list : TAsmList;const source,dest : treference;len : tcgint);
  1590. var
  1591. paraloc1,paraloc2,paraloc3 : TCGPara;
  1592. pd : tprocdef;
  1593. begin
  1594. pd:=search_system_proc('MOVE');
  1595. paraloc1.init;
  1596. paraloc2.init;
  1597. paraloc3.init;
  1598. paramanager.getintparaloc(list,pd,1,paraloc1);
  1599. paramanager.getintparaloc(list,pd,2,paraloc2);
  1600. paramanager.getintparaloc(list,pd,3,paraloc3);
  1601. a_load_const_cgpara(list,OS_SINT,len,paraloc3);
  1602. a_loadaddr_ref_cgpara(list,dest,paraloc2);
  1603. a_loadaddr_ref_cgpara(list,source,paraloc1);
  1604. paramanager.freecgpara(list,paraloc3);
  1605. paramanager.freecgpara(list,paraloc2);
  1606. paramanager.freecgpara(list,paraloc1);
  1607. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1608. alloccpuregisters(list,R_MMREGISTER,paramanager.get_volatile_registers_mm(pocall_default));
  1609. a_call_name(list,'FPC_MOVE',false);
  1610. dealloccpuregisters(list,R_MMREGISTER,paramanager.get_volatile_registers_mm(pocall_default));
  1611. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1612. paraloc3.done;
  1613. paraloc2.done;
  1614. paraloc1.done;
  1615. end;
  1616. procedure tcgaarch64.g_concatcopy(list: TAsmList; const source, dest: treference; len: tcgint);
  1617. var
  1618. sourcebasereplaced, destbasereplaced: boolean;
  1619. { get optimal memory operation to use for loading/storing data
  1620. in an unrolled loop }
  1621. procedure getmemop(scaledop, unscaledop: tasmop; const startref, endref: treference; opsize: tcgsize; postfix: toppostfix; out memop: tasmop; out needsimplify: boolean);
  1622. begin
  1623. if (simple_ref_type(scaledop,opsize,postfix,startref)=sr_simple) and
  1624. (simple_ref_type(scaledop,opsize,postfix,endref)=sr_simple) then
  1625. begin
  1626. memop:=unscaledop;
  1627. needsimplify:=true;
  1628. end
  1629. else if (unscaledop<>A_NONE) and
  1630. (simple_ref_type(unscaledop,opsize,postfix,startref)=sr_simple) and
  1631. (simple_ref_type(unscaledop,opsize,postfix,endref)=sr_simple) then
  1632. begin
  1633. memop:=unscaledop;
  1634. needsimplify:=false;
  1635. end
  1636. else
  1637. begin
  1638. memop:=scaledop;
  1639. needsimplify:=true;
  1640. end;
  1641. end;
  1642. { adjust the offset and/or addressing mode after a load/store so it's
  1643. correct for the next one of the same size }
  1644. procedure updaterefafterloadstore(var ref: treference; oplen: longint);
  1645. begin
  1646. case ref.addressmode of
  1647. AM_OFFSET:
  1648. inc(ref.offset,oplen);
  1649. AM_POSTINDEXED:
  1650. { base register updated by instruction, next offset can remain
  1651. the same }
  1652. ;
  1653. AM_PREINDEXED:
  1654. begin
  1655. { base register updated by instruction -> next instruction can
  1656. use post-indexing with offset = sizeof(operation) }
  1657. ref.offset:=0;
  1658. ref.addressmode:=AM_OFFSET;
  1659. end;
  1660. end;
  1661. end;
  1662. { generate a load/store and adjust the reference offset to the next
  1663. memory location if necessary }
  1664. procedure genloadstore(list: TAsmList; op: tasmop; reg: tregister; var ref: treference; postfix: toppostfix; opsize: tcgsize);
  1665. begin
  1666. list.concat(setoppostfix(taicpu.op_reg_ref(op,reg,ref),postfix));
  1667. updaterefafterloadstore(ref,tcgsize2size[opsize]);
  1668. end;
  1669. { generate a dual load/store (ldp/stp) and adjust the reference offset to
  1670. the next memory location if necessary }
  1671. procedure gendualloadstore(list: TAsmList; op: tasmop; reg1, reg2: tregister; var ref: treference; postfix: toppostfix; opsize: tcgsize);
  1672. begin
  1673. list.concat(setoppostfix(taicpu.op_reg_reg_ref(op,reg1,reg2,ref),postfix));
  1674. updaterefafterloadstore(ref,tcgsize2size[opsize]*2);
  1675. end;
  1676. { turn a reference into a pre- or post-indexed reference for use in a
  1677. load/store of a particular size }
  1678. procedure makesimpleforcopy(list: TAsmList; var scaledop: tasmop; opsize: tcgsize; postfix: toppostfix; forcepostindexing: boolean; var ref: treference; var basereplaced: boolean);
  1679. var
  1680. tmpreg: tregister;
  1681. scaledoffset: longint;
  1682. orgaddressmode: taddressmode;
  1683. begin
  1684. scaledoffset:=tcgsize2size[opsize];
  1685. if scaledop in [A_LDP,A_STP] then
  1686. scaledoffset:=scaledoffset*2;
  1687. { can we use the reference as post-indexed without changes? }
  1688. if forcepostindexing then
  1689. begin
  1690. orgaddressmode:=ref.addressmode;
  1691. ref.addressmode:=AM_POSTINDEXED;
  1692. if (orgaddressmode=AM_POSTINDEXED) or
  1693. ((ref.offset=0) and
  1694. (simple_ref_type(scaledop,opsize,postfix,ref)=sr_simple)) then
  1695. begin
  1696. { just change the post-indexed offset to the access size }
  1697. ref.offset:=scaledoffset;
  1698. { and replace the base register if that didn't happen yet
  1699. (could be sp or a regvar) }
  1700. if not basereplaced then
  1701. begin
  1702. tmpreg:=getaddressregister(list);
  1703. a_load_reg_reg(list,OS_ADDR,OS_ADDR,ref.base,tmpreg);
  1704. ref.base:=tmpreg;
  1705. basereplaced:=true;
  1706. end;
  1707. exit;
  1708. end;
  1709. ref.addressmode:=orgaddressmode;
  1710. end;
  1711. {$ifdef dummy}
  1712. This could in theory be useful in case you have a concatcopy from
  1713. e.g. x1+255 to x1+267 *and* the reference is aligned, but this seems
  1714. very unlikely. Disabled because it still needs fixes, as it
  1715. also generates pre-indexed loads right now at the very end for the
  1716. left-over gencopies
  1717. { can we turn it into a pre-indexed reference for free? (after the
  1718. first operation, it will be turned into an offset one) }
  1719. if not forcepostindexing and
  1720. (ref.offset<>0) then
  1721. begin
  1722. orgaddressmode:=ref.addressmode;
  1723. ref.addressmode:=AM_PREINDEXED;
  1724. tmpreg:=ref.base;
  1725. if not basereplaced and
  1726. (ref.base=tmpreg) then
  1727. begin
  1728. tmpreg:=getaddressregister(list);
  1729. a_load_reg_reg(list,OS_ADDR,OS_ADDR,ref.base,tmpreg);
  1730. ref.base:=tmpreg;
  1731. basereplaced:=true;
  1732. end;
  1733. if simple_ref_type(scaledop,opsize,postfix,ref)<>sr_simple then
  1734. make_simple_ref(list,scaledop,opsize,postfix,ref,NR_NO);
  1735. exit;
  1736. end;
  1737. {$endif dummy}
  1738. if not forcepostindexing then
  1739. begin
  1740. ref.addressmode:=AM_OFFSET;
  1741. make_simple_ref(list,scaledop,opsize,postfix,ref,NR_NO);
  1742. { this may still cause problems if the final offset is no longer
  1743. a simple ref; it's a bit complicated to pass all information
  1744. through at all places and check that here, so play safe: we
  1745. currently never generate unrolled copies for more than 64
  1746. bytes (32 with non-double-register copies) }
  1747. if ref.index=NR_NO then
  1748. begin
  1749. if ((scaledop in [A_LDP,A_STP]) and
  1750. (ref.offset<((64-8)*tcgsize2size[opsize]))) or
  1751. ((scaledop in [A_LDUR,A_STUR]) and
  1752. (ref.offset<(255-8*tcgsize2size[opsize]))) or
  1753. ((scaledop in [A_LDR,A_STR]) and
  1754. (ref.offset<((4096-8)*tcgsize2size[opsize]))) then
  1755. exit;
  1756. end;
  1757. end;
  1758. tmpreg:=getaddressregister(list);
  1759. a_loadaddr_ref_reg(list,ref,tmpreg);
  1760. basereplaced:=true;
  1761. if forcepostindexing then
  1762. begin
  1763. reference_reset_base(ref,tmpreg,scaledoffset,ref.alignment);
  1764. ref.addressmode:=AM_POSTINDEXED;
  1765. end
  1766. else
  1767. begin
  1768. reference_reset_base(ref,tmpreg,0,ref.alignment);
  1769. ref.addressmode:=AM_OFFSET;
  1770. end
  1771. end;
  1772. { prepare a reference for use by gencopy. This is done both after the
  1773. unrolled and regular copy loop -> get rid of post-indexing mode, make
  1774. sure ref is valid }
  1775. procedure preparecopy(list: tasmlist; scaledop, unscaledop: tasmop; var ref: treference; opsize: tcgsize; postfix: toppostfix; out op: tasmop; var basereplaced: boolean);
  1776. var
  1777. simplify: boolean;
  1778. begin
  1779. if ref.addressmode=AM_POSTINDEXED then
  1780. ref.offset:=tcgsize2size[opsize];
  1781. getmemop(scaledop,scaledop,ref,ref,opsize,postfix,op,simplify);
  1782. if simplify then
  1783. begin
  1784. makesimpleforcopy(list,scaledop,opsize,postfix,false,ref,basereplaced);
  1785. op:=scaledop;
  1786. end;
  1787. end;
  1788. { generate a copy from source to dest of size opsize/postfix }
  1789. procedure gencopy(list: TAsmList; var source, dest: treference; postfix: toppostfix; opsize: tcgsize);
  1790. var
  1791. reg: tregister;
  1792. loadop, storeop: tasmop;
  1793. begin
  1794. preparecopy(list,A_LDR,A_LDUR,source,opsize,postfix,loadop,sourcebasereplaced);
  1795. preparecopy(list,A_STR,A_STUR,dest,opsize,postfix,storeop,destbasereplaced);
  1796. reg:=getintregister(list,opsize);
  1797. genloadstore(list,loadop,reg,source,postfix,opsize);
  1798. genloadstore(list,storeop,reg,dest,postfix,opsize);
  1799. end;
  1800. { copy the leftovers after an unrolled or regular copy loop }
  1801. procedure gencopyleftovers(list: TAsmList; var source, dest: treference; len: longint);
  1802. begin
  1803. { stop post-indexing if we did so in the loop, since in that case all
  1804. offsets definitely can be represented now }
  1805. if source.addressmode=AM_POSTINDEXED then
  1806. begin
  1807. source.addressmode:=AM_OFFSET;
  1808. source.offset:=0;
  1809. end;
  1810. if dest.addressmode=AM_POSTINDEXED then
  1811. begin
  1812. dest.addressmode:=AM_OFFSET;
  1813. dest.offset:=0;
  1814. end;
  1815. { transfer the leftovers }
  1816. if len>=8 then
  1817. begin
  1818. dec(len,8);
  1819. gencopy(list,source,dest,PF_NONE,OS_64);
  1820. end;
  1821. if len>=4 then
  1822. begin
  1823. dec(len,4);
  1824. gencopy(list,source,dest,PF_NONE,OS_32);
  1825. end;
  1826. if len>=2 then
  1827. begin
  1828. dec(len,2);
  1829. gencopy(list,source,dest,PF_H,OS_16);
  1830. end;
  1831. if len>=1 then
  1832. begin
  1833. dec(len);
  1834. gencopy(list,source,dest,PF_B,OS_8);
  1835. end;
  1836. end;
  1837. const
  1838. { load_length + loop dec + cbnz }
  1839. loopoverhead=12;
  1840. { loop overhead + load + store }
  1841. totallooplen=loopoverhead + 8;
  1842. var
  1843. totalalign: longint;
  1844. maxlenunrolled: tcgint;
  1845. loadop, storeop: tasmop;
  1846. opsize: tcgsize;
  1847. postfix: toppostfix;
  1848. tmpsource, tmpdest: treference;
  1849. scaledstoreop, unscaledstoreop,
  1850. scaledloadop, unscaledloadop: tasmop;
  1851. regs: array[1..8] of tregister;
  1852. countreg: tregister;
  1853. i, regcount: longint;
  1854. hl: tasmlabel;
  1855. simplifysource, simplifydest: boolean;
  1856. begin
  1857. if len=0 then
  1858. exit;
  1859. sourcebasereplaced:=false;
  1860. destbasereplaced:=false;
  1861. { maximum common alignment }
  1862. totalalign:=max(1,newalignment(source.alignment,dest.alignment));
  1863. { use a simple load/store? }
  1864. if (len in [1,2,4,8]) and
  1865. ((totalalign>=(len div 2)) or
  1866. (source.alignment=len) or
  1867. (dest.alignment=len)) then
  1868. begin
  1869. opsize:=int_cgsize(len);
  1870. a_load_ref_ref(list,opsize,opsize,source,dest);
  1871. exit;
  1872. end;
  1873. { alignment > length is not useful, and would break some checks below }
  1874. while totalalign>len do
  1875. totalalign:=totalalign div 2;
  1876. { operation sizes to use based on common alignment }
  1877. case totalalign of
  1878. 1:
  1879. begin
  1880. postfix:=PF_B;
  1881. opsize:=OS_8;
  1882. end;
  1883. 2:
  1884. begin
  1885. postfix:=PF_H;
  1886. opsize:=OS_16;
  1887. end;
  1888. 4:
  1889. begin
  1890. postfix:=PF_None;
  1891. opsize:=OS_32;
  1892. end
  1893. else
  1894. begin
  1895. totalalign:=8;
  1896. postfix:=PF_None;
  1897. opsize:=OS_64;
  1898. end;
  1899. end;
  1900. { maximum length to handled with an unrolled loop (4 loads + 4 stores) }
  1901. maxlenunrolled:=min(totalalign,8)*4;
  1902. { ldp/stp -> 2 registers per instruction }
  1903. if (totalalign>=4) and
  1904. (len>=totalalign*2) then
  1905. begin
  1906. maxlenunrolled:=maxlenunrolled*2;
  1907. scaledstoreop:=A_STP;
  1908. scaledloadop:=A_LDP;
  1909. unscaledstoreop:=A_NONE;
  1910. unscaledloadop:=A_NONE;
  1911. end
  1912. else
  1913. begin
  1914. scaledstoreop:=A_STR;
  1915. scaledloadop:=A_LDR;
  1916. unscaledstoreop:=A_STUR;
  1917. unscaledloadop:=A_LDUR;
  1918. end;
  1919. { we only need 4 instructions extra to call FPC_MOVE }
  1920. if cs_opt_size in current_settings.optimizerswitches then
  1921. maxlenunrolled:=maxlenunrolled div 2;
  1922. if (len>maxlenunrolled) and
  1923. (len>totalalign*8) then
  1924. begin
  1925. g_concatcopy_move(list,source,dest,len);
  1926. exit;
  1927. end;
  1928. simplifysource:=true;
  1929. simplifydest:=true;
  1930. tmpsource:=source;
  1931. tmpdest:=dest;
  1932. { can we directly encode all offsets in an unrolled loop? }
  1933. if len<=maxlenunrolled then
  1934. begin
  1935. {$ifdef extdebug}
  1936. list.concat(tai_comment.Create(strpnew('concatcopy unrolled loop; len/opsize/align: '+tostr(len)+'/'+tostr(tcgsize2size[opsize])+'/'+tostr(totalalign))));
  1937. {$endif extdebug}
  1938. { the leftovers will be handled separately -> -(len mod opsize) }
  1939. inc(tmpsource.offset,len-(len mod tcgsize2size[opsize]));
  1940. { additionally, the last regular load/store will be at
  1941. offset+len-opsize (if len-(len mod opsize)>len) }
  1942. if tmpsource.offset>source.offset then
  1943. dec(tmpsource.offset,tcgsize2size[opsize]);
  1944. getmemop(scaledloadop,unscaledloadop,source,tmpsource,opsize,postfix,loadop,simplifysource);
  1945. inc(tmpdest.offset,len-(len mod tcgsize2size[opsize]));
  1946. if tmpdest.offset>dest.offset then
  1947. dec(tmpdest.offset,tcgsize2size[opsize]);
  1948. getmemop(scaledstoreop,unscaledstoreop,dest,tmpdest,opsize,postfix,storeop,simplifydest);
  1949. tmpsource:=source;
  1950. tmpdest:=dest;
  1951. { if we can't directly encode all offsets, simplify }
  1952. if simplifysource then
  1953. begin
  1954. loadop:=scaledloadop;
  1955. makesimpleforcopy(list,loadop,opsize,postfix,false,tmpsource,sourcebasereplaced);
  1956. end;
  1957. if simplifydest then
  1958. begin
  1959. storeop:=scaledstoreop;
  1960. makesimpleforcopy(list,storeop,opsize,postfix,false,tmpdest,destbasereplaced);
  1961. end;
  1962. regcount:=len div tcgsize2size[opsize];
  1963. { in case we transfer two registers at a time, we copy an even
  1964. number of registers }
  1965. if loadop=A_LDP then
  1966. regcount:=regcount and not(1);
  1967. { initialise for dfa }
  1968. regs[low(regs)]:=NR_NO;
  1969. { max 4 loads/stores -> max 8 registers (in case of ldp/stdp) }
  1970. for i:=1 to regcount do
  1971. regs[i]:=getintregister(list,opsize);
  1972. if loadop=A_LDP then
  1973. begin
  1974. { load registers }
  1975. for i:=1 to (regcount div 2) do
  1976. gendualloadstore(list,loadop,regs[i*2-1],regs[i*2],tmpsource,postfix,opsize);
  1977. { store registers }
  1978. for i:=1 to (regcount div 2) do
  1979. gendualloadstore(list,storeop,regs[i*2-1],regs[i*2],tmpdest,postfix,opsize);
  1980. end
  1981. else
  1982. begin
  1983. for i:=1 to regcount do
  1984. genloadstore(list,loadop,regs[i],tmpsource,postfix,opsize);
  1985. for i:=1 to regcount do
  1986. genloadstore(list,storeop,regs[i],tmpdest,postfix,opsize);
  1987. end;
  1988. { leftover }
  1989. len:=len-regcount*tcgsize2size[opsize];
  1990. {$ifdef extdebug}
  1991. list.concat(tai_comment.Create(strpnew('concatcopy unrolled loop leftover: '+tostr(len))));
  1992. {$endif extdebug}
  1993. end
  1994. else
  1995. begin
  1996. {$ifdef extdebug}
  1997. list.concat(tai_comment.Create(strpnew('concatcopy regular loop; len/align: '+tostr(len)+'/'+tostr(totalalign))));
  1998. {$endif extdebug}
  1999. { regular loop -> definitely use post-indexing }
  2000. loadop:=scaledloadop;
  2001. makesimpleforcopy(list,loadop,opsize,postfix,true,tmpsource,sourcebasereplaced);
  2002. storeop:=scaledstoreop;
  2003. makesimpleforcopy(list,storeop,opsize,postfix,true,tmpdest,destbasereplaced);
  2004. current_asmdata.getjumplabel(hl);
  2005. countreg:=getintregister(list,OS_32);
  2006. if loadop=A_LDP then
  2007. a_load_const_reg(list,OS_32,len div tcgsize2size[opsize]*2,countreg)
  2008. else
  2009. a_load_const_reg(list,OS_32,len div tcgsize2size[opsize],countreg);
  2010. a_label(list,hl);
  2011. a_op_const_reg(list,OP_SUB,OS_32,1,countreg);
  2012. if loadop=A_LDP then
  2013. begin
  2014. regs[1]:=getintregister(list,opsize);
  2015. regs[2]:=getintregister(list,opsize);
  2016. gendualloadstore(list,loadop,regs[1],regs[2],tmpsource,postfix,opsize);
  2017. gendualloadstore(list,storeop,regs[1],regs[2],tmpdest,postfix,opsize);
  2018. end
  2019. else
  2020. begin
  2021. regs[1]:=getintregister(list,opsize);
  2022. genloadstore(list,loadop,regs[1],tmpsource,postfix,opsize);
  2023. genloadstore(list,storeop,regs[1],tmpdest,postfix,opsize);
  2024. end;
  2025. list.concat(taicpu.op_reg_sym_ofs(A_CBNZ,countreg,hl,0));
  2026. len:=len mod tcgsize2size[opsize];
  2027. end;
  2028. gencopyleftovers(list,tmpsource,tmpdest,len);
  2029. end;
  2030. procedure tcgaarch64.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  2031. begin
  2032. { This method is integrated into g_intf_wrapper and shouldn't be called separately }
  2033. InternalError(2013020102);
  2034. end;
  2035. procedure create_codegen;
  2036. begin
  2037. cg:=tcgaarch64.Create;
  2038. cg128:=tcg128.Create;
  2039. end;
  2040. end.