cgcpu.pas 101 KB

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