cgcpu.pas 102 KB

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