cgcpu.pas 91 KB

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