cgcpu.pas 101 KB

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