cgcpu.pas 101 KB

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