cgcpu.pas 107 KB

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