cgcpu.pas 89 KB

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