cgcpu.pas 98 KB

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