cgx86.pas 135 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637
  1. {
  2. Copyright (c) 1998-2005 by Florian Klaempfl
  3. This unit implements the common parts of the code generator for the i386 and the x86-64.
  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. { This unit implements the common parts of the code generator for the i386 and the x86-64.
  18. }
  19. unit cgx86;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. globtype,
  24. cgbase,cgutils,cgobj,
  25. aasmbase,aasmtai,aasmdata,aasmcpu,
  26. cpubase,cpuinfo,rgx86,
  27. symconst,symtype,symdef;
  28. type
  29. { tcgx86 }
  30. tcgx86 = class(tcg)
  31. rgfpu : Trgx86fpu;
  32. procedure done_register_allocators;override;
  33. function getfpuregister(list:TAsmList;size:Tcgsize):Tregister;override;
  34. function getmmxregister(list:TAsmList):Tregister;
  35. function getmmregister(list:TAsmList;size:Tcgsize):Tregister;override;
  36. procedure getcpuregister(list:TAsmList;r:Tregister);override;
  37. procedure ungetcpuregister(list:TAsmList;r:Tregister);override;
  38. procedure alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);override;
  39. procedure dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);override;
  40. function uses_registers(rt:Tregistertype):boolean;override;
  41. procedure add_reg_instruction(instr:Tai;r:tregister);override;
  42. procedure dec_fpu_stack;
  43. procedure inc_fpu_stack;
  44. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);override;
  45. procedure a_call_name_near(list : TAsmList;const s : string; weak: boolean);
  46. procedure a_call_name_static(list : TAsmList;const s : string);override;
  47. procedure a_call_name_static_near(list : TAsmList;const s : string);
  48. procedure a_call_reg(list : TAsmList;reg : tregister);override;
  49. procedure a_call_reg_near(list : TAsmList;reg : tregister);
  50. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister); override;
  51. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); override;
  52. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  53. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); override;
  54. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference); override;
  55. {$ifndef i8086}
  56. procedure a_op_const_reg_reg(list : TAsmList; op : Topcg; size : Tcgsize; a : tcgint; src,dst : Tregister); override;
  57. procedure a_op_reg_reg_reg(list : TAsmList; op : TOpCg; size : tcgsize; src1,src2,dst : tregister); override;
  58. {$endif not i8086}
  59. { move instructions }
  60. procedure a_load_const_reg(list : TAsmList; tosize: tcgsize; a : tcgint;reg : tregister);override;
  61. procedure a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);override;
  62. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize: tcgsize; reg : tregister;const ref : treference);override;
  63. { final as a_load_ref_reg_internal() should be overridden instead }
  64. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize: tcgsize;const ref : treference;reg : tregister);override;final;
  65. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize: tcgsize;reg1,reg2 : tregister);override;
  66. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  67. { bit scan instructions }
  68. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: TCGSize; src, dst: TRegister); override;
  69. { fpu move instructions }
  70. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  71. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); override;
  72. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  73. { vector register move instructions }
  74. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); override;
  75. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  76. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); override;
  77. procedure a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  78. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle);override;
  79. procedure a_opmm_ref_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;const ref : treference;src,dst : tregister;shuffle : pmmshuffle);override;
  80. procedure a_opmm_reg_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;src1,src2,dst : tregister;shuffle : pmmshuffle);override;
  81. { comparison operations }
  82. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  83. l : tasmlabel);override;
  84. procedure a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  85. l : tasmlabel);override;
  86. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  87. procedure a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister; l : tasmlabel); override;
  88. procedure a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); override;
  89. procedure a_jmp_name(list : TAsmList;const s : string);override;
  90. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  91. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  92. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister); override;
  93. procedure g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference); override;
  94. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);override;
  95. { entry/exit code helpers }
  96. procedure g_profilecode(list : TAsmList);override;
  97. procedure g_stackpointer_alloc(list : TAsmList;localsize : longint);override;
  98. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  99. procedure g_save_registers(list: TAsmList); override;
  100. procedure g_restore_registers(list: TAsmList); override;
  101. procedure g_overflowcheck(list: TAsmList; const l:tlocation;def:tdef);override;
  102. procedure make_simple_ref(list:TAsmList;var ref: treference);inline;
  103. procedure make_direct_ref(list:TAsmList;var ref: treference);
  104. function get_darwin_call_stub(const s: string; weak: boolean): tasmsymbol;
  105. procedure generate_leave(list : TAsmList);
  106. protected
  107. procedure a_load_ref_reg_internal(list : TAsmList;fromsize,tosize: tcgsize;const ref : treference;reg : tregister;isdirect:boolean);virtual;
  108. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  109. procedure check_register_size(size:tcgsize;reg:tregister);
  110. procedure opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;dst: tregister; shuffle : pmmshuffle);
  111. procedure opmm_loc_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;loc : tlocation;src,dst : tregister;shuffle : pmmshuffle);
  112. procedure sizes2load(s1,s2 : tcgsize;var op: tasmop; var s3: topsize);
  113. procedure floatload(list: TAsmList; t : tcgsize;const ref : treference);
  114. procedure floatstore(list: TAsmList; t : tcgsize;const ref : treference);
  115. procedure floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  116. procedure floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  117. procedure internal_restore_regs(list: TAsmList; use_pop: boolean);
  118. procedure make_simple_ref(list:TAsmList;var ref: treference;isdirect:boolean);
  119. end;
  120. const
  121. {$if defined(x86_64)}
  122. TCGSize2OpSize: Array[tcgsize] of topsize =
  123. (S_NO,S_B,S_W,S_L,S_Q,S_XMM,S_B,S_W,S_L,S_Q,S_XMM,
  124. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  125. S_NO,S_NO,S_NO,S_MD,S_XMM,S_YMM,S_ZMM,
  126. S_NO,S_NO,S_NO,S_NO,S_XMM,S_YMM,S_ZMM,
  127. S_NO,S_XMM,S_YMM,S_ZMM,
  128. S_NO,S_XMM,S_YMM,S_ZMM);
  129. {$elseif defined(i386)}
  130. TCGSize2OpSize: Array[tcgsize] of topsize =
  131. (S_NO,S_B,S_W,S_L,S_L,S_T,S_B,S_W,S_L,S_L,S_L,
  132. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  133. S_NO,S_NO,S_NO,S_MD,S_XMM,S_YMM,S_ZMM,
  134. S_NO,S_NO,S_NO,S_NO,S_XMM,S_YMM,S_ZMM,
  135. S_NO,S_XMM,S_YMM,S_ZMM,
  136. S_NO,S_XMM,S_YMM,S_ZMM);
  137. {$elseif defined(i8086)}
  138. TCGSize2OpSize: Array[tcgsize] of topsize =
  139. (S_NO,S_B,S_W,S_W,S_W,S_T,S_B,S_W,S_W,S_W,S_W,
  140. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  141. S_NO,S_NO,S_NO,S_MD,S_XMM,S_YMM,S_ZMM,
  142. S_NO,S_NO,S_NO,S_NO,S_XMM,S_YMM,S_ZMM,
  143. S_NO,S_XMM,S_YMM,S_ZMM,
  144. S_NO,S_XMM,S_YMM,S_ZMM);
  145. {$endif}
  146. {$ifndef NOTARGETWIN}
  147. winstackpagesize = 4096;
  148. {$endif NOTARGETWIN}
  149. function UseAVX: boolean;
  150. function UseIncDec: boolean;
  151. { returns true, if the compiler should use leave instead of mov/pop }
  152. function UseLeave: boolean;
  153. { Gets the byte alignment of a reference }
  154. function GetRefAlignment(ref: treference): Byte;
  155. implementation
  156. uses
  157. globals,verbose,systems,cutils,
  158. symcpu,
  159. paramgr,procinfo,
  160. tgobj,ncgutil;
  161. function UseAVX: boolean;
  162. begin
  163. Result:=(current_settings.fputype in fpu_avx_instructionsets) {$ifndef i8086}or (CPUX86_HAS_AVXUNIT in cpu_capabilities[current_settings.cputype]){$endif i8086};
  164. end;
  165. { modern CPUs prefer add/sub over inc/dec because add/sub break instructions dependencies on flags
  166. because they modify all flags }
  167. function UseIncDec: boolean;
  168. begin
  169. {$if defined(x86_64)}
  170. Result:=cs_opt_size in current_settings.optimizerswitches;
  171. {$elseif defined(i386)}
  172. Result:=(cs_opt_size in current_settings.optimizerswitches) or (current_settings.cputype in [cpu_386]);
  173. {$elseif defined(i8086)}
  174. Result:=(cs_opt_size in current_settings.optimizerswitches) or (current_settings.cputype in [cpu_8086..cpu_386]);
  175. {$endif}
  176. end;
  177. function UseLeave: boolean;
  178. begin
  179. {$if defined(x86_64)}
  180. { Modern processors should be happy with mov;pop, maybe except older AMDs }
  181. Result:=cs_opt_size in current_settings.optimizerswitches;
  182. {$elseif defined(i386)}
  183. Result:=(cs_opt_size in current_settings.optimizerswitches) or (current_settings.optimizecputype<cpu_Pentium2);
  184. {$elseif defined(i8086)}
  185. Result:=current_settings.cputype>=cpu_186;
  186. {$endif}
  187. end;
  188. function GetRefAlignment(ref: treference): Byte; {$IFDEF USEINLINE}inline;{$ENDIF}
  189. begin
  190. {$ifdef x86_64}
  191. { The stack pointer and base pointer will be aligned to 16-byte boundaries if the machine code is well-behaved }
  192. if (ref.base = NR_RSP) or (ref.base = NR_RBP) then
  193. begin
  194. if (ref.index = NR_NO) and ((ref.offset mod 16) = 0) then
  195. Result := 16
  196. else
  197. Result := ref.alignment;
  198. end
  199. else
  200. {$endif x86_64}
  201. Result := ref.alignment;
  202. end;
  203. const
  204. TOpCG2AsmOp: Array[topcg] of TAsmOp = (A_NONE,A_MOV,A_ADD,A_AND,A_DIV,
  205. A_IDIV,A_IMUL,A_MUL,A_NEG,A_NOT,A_OR,
  206. A_SAR,A_SHL,A_SHR,A_SUB,A_XOR,A_ROL,A_ROR);
  207. TOpCmp2AsmCond: Array[topcmp] of TAsmCond = (C_NONE,
  208. C_E,C_G,C_L,C_GE,C_LE,C_NE,C_BE,C_B,C_AE,C_A);
  209. procedure Tcgx86.done_register_allocators;
  210. begin
  211. rg[R_INTREGISTER].free;
  212. rg[R_MMREGISTER].free;
  213. rg[R_MMXREGISTER].free;
  214. rgfpu.free;
  215. inherited done_register_allocators;
  216. end;
  217. function Tcgx86.getfpuregister(list:TAsmList;size:Tcgsize):Tregister;
  218. begin
  219. result:=rgfpu.getregisterfpu(list);
  220. end;
  221. function Tcgx86.getmmxregister(list:TAsmList):Tregister;
  222. begin
  223. if not assigned(rg[R_MMXREGISTER]) then
  224. internalerror(2003121214);
  225. result:=rg[R_MMXREGISTER].getregister(list,R_SUBNONE);
  226. end;
  227. function Tcgx86.getmmregister(list:TAsmList;size:Tcgsize):Tregister;
  228. begin
  229. if not assigned(rg[R_MMREGISTER]) then
  230. internalerror(2003121234);
  231. case size of
  232. OS_F64:
  233. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMD);
  234. OS_F32:
  235. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMS);
  236. OS_M64:
  237. result:=rg[R_MMREGISTER].getregister(list,R_SUBQ);
  238. OS_M128,
  239. OS_F128,
  240. OS_MF128,
  241. OS_MD128:
  242. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMX); { R_SUBMMWHOLE seems a bit dangerous and ambiguous, so changed to R_SUBMMX. [Kit] }
  243. OS_M256,
  244. OS_MF256,
  245. OS_MD256:
  246. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMY);
  247. OS_M512,
  248. OS_MF512,
  249. OS_MD512:
  250. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMZ);
  251. else
  252. internalerror(200506041);
  253. end;
  254. end;
  255. procedure Tcgx86.getcpuregister(list:TAsmList;r:Tregister);
  256. begin
  257. if getregtype(r)=R_FPUREGISTER then
  258. internalerror(2003121210)
  259. else
  260. inherited getcpuregister(list,r);
  261. end;
  262. procedure tcgx86.ungetcpuregister(list:TAsmList;r:Tregister);
  263. begin
  264. if getregtype(r)=R_FPUREGISTER then
  265. rgfpu.ungetregisterfpu(list,r)
  266. else
  267. inherited ungetcpuregister(list,r);
  268. end;
  269. procedure Tcgx86.alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  270. begin
  271. if rt<>R_FPUREGISTER then
  272. inherited alloccpuregisters(list,rt,r);
  273. end;
  274. procedure Tcgx86.dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  275. begin
  276. if rt<>R_FPUREGISTER then
  277. inherited dealloccpuregisters(list,rt,r);
  278. end;
  279. function Tcgx86.uses_registers(rt:Tregistertype):boolean;
  280. begin
  281. if rt=R_FPUREGISTER then
  282. result:=false
  283. else
  284. result:=inherited uses_registers(rt);
  285. end;
  286. procedure tcgx86.add_reg_instruction(instr:Tai;r:tregister);
  287. begin
  288. if getregtype(r)<>R_FPUREGISTER then
  289. inherited add_reg_instruction(instr,r);
  290. end;
  291. procedure tcgx86.dec_fpu_stack;
  292. begin
  293. if rgfpu.fpuvaroffset<=0 then
  294. internalerror(200604201);
  295. dec(rgfpu.fpuvaroffset);
  296. end;
  297. procedure tcgx86.inc_fpu_stack;
  298. begin
  299. if rgfpu.fpuvaroffset>=7 then
  300. internalerror(2012062901);
  301. inc(rgfpu.fpuvaroffset);
  302. end;
  303. {****************************************************************************
  304. This is private property, keep out! :)
  305. ****************************************************************************}
  306. procedure tcgx86.sizes2load(s1,s2 : tcgsize; var op: tasmop; var s3: topsize);
  307. begin
  308. { ensure to have always valid sizes }
  309. if s1=OS_NO then
  310. s1:=s2;
  311. if s2=OS_NO then
  312. s2:=s1;
  313. case s2 of
  314. OS_8,OS_S8 :
  315. if S1 in [OS_8,OS_S8] then
  316. s3 := S_B
  317. else
  318. internalerror(200109221);
  319. OS_16,OS_S16:
  320. case s1 of
  321. OS_8,OS_S8:
  322. s3 := S_BW;
  323. OS_16,OS_S16:
  324. s3 := S_W;
  325. else
  326. internalerror(200109222);
  327. end;
  328. OS_32,OS_S32:
  329. case s1 of
  330. OS_8,OS_S8:
  331. s3 := S_BL;
  332. OS_16,OS_S16:
  333. s3 := S_WL;
  334. OS_32,OS_S32:
  335. s3 := S_L;
  336. else
  337. internalerror(200109223);
  338. end;
  339. {$ifdef x86_64}
  340. OS_64,OS_S64:
  341. case s1 of
  342. OS_8:
  343. s3 := S_BL;
  344. OS_S8:
  345. s3 := S_BQ;
  346. OS_16:
  347. s3 := S_WL;
  348. OS_S16:
  349. s3 := S_WQ;
  350. OS_32:
  351. s3 := S_L;
  352. OS_S32:
  353. s3 := S_LQ;
  354. OS_64,OS_S64:
  355. s3 := S_Q;
  356. else
  357. internalerror(200304302);
  358. end;
  359. {$endif x86_64}
  360. else
  361. internalerror(200109227);
  362. end;
  363. if s3 in [S_B,S_W,S_L,S_Q] then
  364. op := A_MOV
  365. else if s1 in [OS_8,OS_16,OS_32,OS_64] then
  366. op := A_MOVZX
  367. else
  368. {$ifdef x86_64}
  369. if s3 in [S_LQ] then
  370. op := A_MOVSXD
  371. else
  372. {$endif x86_64}
  373. op := A_MOVSX;
  374. end;
  375. procedure tcgx86.make_simple_ref(list:TAsmList;var ref: treference);
  376. begin
  377. make_simple_ref(list,ref,false);
  378. end;
  379. procedure tcgx86.make_simple_ref(list:TAsmList;var ref: treference;isdirect:boolean);
  380. var
  381. hreg : tregister;
  382. href : treference;
  383. {$ifndef x86_64}
  384. add_hreg: boolean;
  385. {$endif not x86_64}
  386. begin
  387. hreg:=NR_NO;
  388. { make_simple_ref() may have already been called earlier, and in that
  389. case make sure we don't perform the PIC-simplifications twice }
  390. if (ref.refaddr in [addr_pic,addr_pic_no_got]) then
  391. exit;
  392. { handle indirect symbols first }
  393. if not isdirect then
  394. make_direct_ref(list,ref);
  395. {$if defined(x86_64)}
  396. { Only 32bit is allowed }
  397. { Note that this isn't entirely correct: for RIP-relative targets/memory models,
  398. it is actually (offset+@symbol-RIP) that should fit into 32 bits. Since two last
  399. members aren't known until link time, ABIs place very pessimistic limits
  400. on offset values, e.g. SysV AMD64 allows +/-$1000000 (16 megabytes) }
  401. if ((ref.offset<low(longint)) or (ref.offset>high(longint))) or
  402. { absolute address is not a common thing in x64, but nevertheless a possible one }
  403. ((ref.base=NR_NO) and (ref.index=NR_NO) and (ref.symbol=nil)) then
  404. begin
  405. { Load constant value to register }
  406. hreg:=GetAddressRegister(list);
  407. list.concat(taicpu.op_const_reg(A_MOV,S_Q,ref.offset,hreg));
  408. ref.offset:=0;
  409. {if assigned(ref.symbol) then
  410. begin
  411. list.concat(taicpu.op_sym_ofs_reg(A_ADD,S_Q,ref.symbol,0,hreg));
  412. ref.symbol:=nil;
  413. end;}
  414. { Add register to reference }
  415. if ref.base=NR_NO then
  416. ref.base:=hreg
  417. else if ref.index=NR_NO then
  418. ref.index:=hreg
  419. else
  420. begin
  421. { don't use add, as the flags may contain a value }
  422. reference_reset_base(href,hreg,0,ref.alignment,[]);
  423. href.index:=ref.index;
  424. href.scalefactor:=ref.scalefactor;
  425. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,hreg));
  426. ref.index:=hreg;
  427. ref.scalefactor:=1;
  428. end;
  429. end;
  430. if assigned(ref.symbol) then
  431. begin
  432. if cs_create_pic in current_settings.moduleswitches then
  433. begin
  434. { Local symbols must not be accessed via the GOT }
  435. if (ref.symbol.bind=AB_LOCAL) then
  436. begin
  437. { unfortunately, RIP-based addresses don't support an index }
  438. if (ref.base<>NR_NO) or
  439. (ref.index<>NR_NO) then
  440. begin
  441. reference_reset_symbol(href,ref.symbol,0,ref.alignment,[]);
  442. hreg:=getaddressregister(list);
  443. href.refaddr:=addr_pic_no_got;
  444. href.base:=NR_RIP;
  445. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,hreg));
  446. ref.symbol:=nil;
  447. end
  448. else
  449. begin
  450. ref.refaddr:=addr_pic_no_got;
  451. hreg:=NR_NO;
  452. ref.base:=NR_RIP;
  453. end;
  454. end
  455. else
  456. begin
  457. reference_reset_symbol(href,ref.symbol,0,ref.alignment,[]);
  458. hreg:=getaddressregister(list);
  459. href.refaddr:=addr_pic;
  460. href.base:=NR_RIP;
  461. list.concat(taicpu.op_ref_reg(A_MOV,S_Q,href,hreg));
  462. ref.symbol:=nil;
  463. end;
  464. if ref.base=NR_NO then
  465. ref.base:=hreg
  466. else if ref.index=NR_NO then
  467. begin
  468. ref.index:=hreg;
  469. ref.scalefactor:=1;
  470. end
  471. else
  472. begin
  473. { don't use add, as the flags may contain a value }
  474. reference_reset_base(href,ref.base,0,ref.alignment,[]);
  475. href.index:=hreg;
  476. ref.base:=getaddressregister(list);
  477. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,ref.base));
  478. end;
  479. end
  480. else
  481. { Always use RIP relative symbol addressing for Windows and Darwin targets. }
  482. if (target_info.system in (systems_all_windows+[system_x86_64_darwin,system_x86_64_iphonesim])) and (ref.base<>NR_RIP) then
  483. begin
  484. if (ref.refaddr=addr_no) and (ref.base=NR_NO) and (ref.index=NR_NO) then
  485. begin
  486. { Set RIP relative addressing for simple symbol references }
  487. ref.base:=NR_RIP;
  488. ref.refaddr:=addr_pic_no_got
  489. end
  490. else
  491. begin
  492. { Use temp register to load calculated 64-bit symbol address for complex references }
  493. reference_reset_symbol(href,ref.symbol,0,sizeof(pint),[]);
  494. href.base:=NR_RIP;
  495. href.refaddr:=addr_pic_no_got;
  496. hreg:=GetAddressRegister(list);
  497. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,hreg));
  498. ref.symbol:=nil;
  499. if ref.base=NR_NO then
  500. ref.base:=hreg
  501. else if ref.index=NR_NO then
  502. begin
  503. ref.index:=hreg;
  504. ref.scalefactor:=0;
  505. end
  506. else
  507. begin
  508. { don't use add, as the flags may contain a value }
  509. reference_reset_base(href,ref.base,0,ref.alignment,[]);
  510. href.index:=hreg;
  511. ref.base:=getaddressregister(list);
  512. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,ref.base));
  513. end;
  514. end;
  515. end;
  516. end;
  517. {$elseif defined(i386)}
  518. add_hreg:=false;
  519. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) then
  520. begin
  521. if assigned(ref.symbol) and
  522. not(assigned(ref.relsymbol)) and
  523. ((ref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL,AB_PRIVATE_EXTERN]) or
  524. (cs_create_pic in current_settings.moduleswitches)) then
  525. begin
  526. if ref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL,AB_PRIVATE_EXTERN] then
  527. begin
  528. hreg:=g_indirect_sym_load(list,ref.symbol.name,asmsym2indsymflags(ref.symbol));
  529. ref.symbol:=nil;
  530. end
  531. else
  532. begin
  533. include(current_procinfo.flags,pi_needs_got);
  534. { make a copy of the got register, hreg can get modified }
  535. hreg:=getaddressregister(list);
  536. a_load_reg_reg(list,OS_ADDR,OS_ADDR,current_procinfo.got,hreg);
  537. ref.relsymbol:=current_procinfo.CurrGOTLabel;
  538. end;
  539. add_hreg:=true
  540. end
  541. end
  542. else if (cs_create_pic in current_settings.moduleswitches) and
  543. assigned(ref.symbol) then
  544. begin
  545. reference_reset_symbol(href,ref.symbol,0,sizeof(pint),[]);
  546. href.base:=current_procinfo.got;
  547. href.refaddr:=addr_pic;
  548. include(current_procinfo.flags,pi_needs_got);
  549. hreg:=getaddressregister(list);
  550. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,hreg));
  551. ref.symbol:=nil;
  552. add_hreg:=true;
  553. end;
  554. if add_hreg then
  555. begin
  556. if ref.base=NR_NO then
  557. ref.base:=hreg
  558. else if ref.index=NR_NO then
  559. begin
  560. ref.index:=hreg;
  561. ref.scalefactor:=1;
  562. end
  563. else
  564. begin
  565. { don't use add, as the flags may contain a value }
  566. reference_reset_base(href,ref.base,0,ref.alignment,[]);
  567. href.index:=hreg;
  568. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  569. ref.base:=hreg;
  570. end;
  571. end;
  572. {$elseif defined(i8086)}
  573. { i8086 does not support stack relative addressing }
  574. if ref.base = NR_STACK_POINTER_REG then
  575. begin
  576. href:=ref;
  577. href.base:=getaddressregister(list);
  578. { let the register allocator find a suitable register for the reference }
  579. list.Concat(Taicpu.op_reg_reg(A_MOV, S_W, NR_SP, href.base));
  580. { if DS<>SS in the current memory model, we need to add an SS: segment override as well }
  581. if (ref.segment=NR_NO) and not segment_regs_equal(NR_DS,NR_SS) then
  582. href.segment:=NR_SS;
  583. ref:=href;
  584. end;
  585. { if there is a segment in an int register, move it to ES }
  586. if (ref.segment<>NR_NO) and (not is_segment_reg(ref.segment)) then
  587. begin
  588. list.concat(taicpu.op_reg_reg(A_MOV,S_W,ref.segment,NR_ES));
  589. ref.segment:=NR_ES;
  590. end;
  591. { can the segment override be dropped? }
  592. if ref.segment<>NR_NO then
  593. begin
  594. if (ref.base=NR_BP) and segment_regs_equal(ref.segment,NR_SS) then
  595. ref.segment:=NR_NO;
  596. if (ref.base<>NR_BP) and segment_regs_equal(ref.segment,NR_DS) then
  597. ref.segment:=NR_NO;
  598. end;
  599. {$endif}
  600. end;
  601. procedure tcgx86.make_direct_ref(list:tasmlist;var ref:treference);
  602. var
  603. href : treference;
  604. hreg : tregister;
  605. begin
  606. if assigned(ref.symbol) and (ref.symbol.bind in asmsymbindindirect) then
  607. begin
  608. { load the symbol into a register }
  609. hreg:=getaddressregister(list);
  610. reference_reset_symbol(href,ref.symbol,0,sizeof(pint),[]);
  611. { tell make_simple_ref that we are loading the symbol address via an indirect
  612. symbol and that hence it should not call make_direct_ref() again }
  613. a_load_ref_reg_internal(list,OS_ADDR,OS_ADDR,href,hreg,true);
  614. if ref.base<>NR_NO then
  615. begin
  616. { fold symbol register into base register }
  617. reference_reset_base(href,hreg,0,ref.alignment,[]);
  618. href.index:=ref.base;
  619. hreg:=getaddressregister(list);
  620. a_loadaddr_ref_reg(list,href,hreg);
  621. end;
  622. { we're done }
  623. ref.symbol:=nil;
  624. ref.base:=hreg;
  625. end;
  626. end;
  627. procedure tcgx86.floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  628. begin
  629. case t of
  630. OS_F32 :
  631. begin
  632. op:=A_FLD;
  633. s:=S_FS;
  634. end;
  635. OS_F64 :
  636. begin
  637. op:=A_FLD;
  638. s:=S_FL;
  639. end;
  640. OS_F80 :
  641. begin
  642. op:=A_FLD;
  643. s:=S_FX;
  644. end;
  645. OS_C64 :
  646. begin
  647. op:=A_FILD;
  648. s:=S_IQ;
  649. end;
  650. else
  651. internalerror(200204043);
  652. end;
  653. end;
  654. procedure tcgx86.floatload(list: TAsmList; t : tcgsize;const ref : treference);
  655. var
  656. op : tasmop;
  657. s : topsize;
  658. tmpref : treference;
  659. begin
  660. tmpref:=ref;
  661. make_simple_ref(list,tmpref);
  662. floatloadops(t,op,s);
  663. list.concat(Taicpu.Op_ref(op,s,tmpref));
  664. inc_fpu_stack;
  665. end;
  666. procedure tcgx86.floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  667. begin
  668. case t of
  669. OS_F32 :
  670. begin
  671. op:=A_FSTP;
  672. s:=S_FS;
  673. end;
  674. OS_F64 :
  675. begin
  676. op:=A_FSTP;
  677. s:=S_FL;
  678. end;
  679. OS_F80 :
  680. begin
  681. op:=A_FSTP;
  682. s:=S_FX;
  683. end;
  684. OS_C64 :
  685. begin
  686. op:=A_FISTP;
  687. s:=S_IQ;
  688. end;
  689. else
  690. internalerror(200204042);
  691. end;
  692. end;
  693. procedure tcgx86.floatstore(list: TAsmList; t : tcgsize;const ref : treference);
  694. var
  695. op : tasmop;
  696. s : topsize;
  697. tmpref : treference;
  698. begin
  699. tmpref:=ref;
  700. make_simple_ref(list,tmpref);
  701. floatstoreops(t,op,s);
  702. list.concat(Taicpu.Op_ref(op,s,tmpref));
  703. { storing non extended floats can cause a floating point overflow }
  704. if ((t<>OS_F80) and (cs_fpu_fwait in current_settings.localswitches))
  705. {$ifdef i8086}
  706. { 8087 and 80287 need a FWAIT after a memory store, before it can be
  707. read with the integer unit }
  708. or (current_settings.cputype<=cpu_286)
  709. {$endif i8086}
  710. then
  711. list.concat(Taicpu.Op_none(A_FWAIT,S_NO));
  712. dec_fpu_stack;
  713. end;
  714. procedure tcgx86.check_register_size(size:tcgsize;reg:tregister);
  715. begin
  716. if TCGSize2OpSize[size]<>TCGSize2OpSize[reg_cgsize(reg)] then
  717. internalerror(200306031);
  718. end;
  719. {****************************************************************************
  720. Assembler code
  721. ****************************************************************************}
  722. procedure tcgx86.a_jmp_name(list : TAsmList;const s : string);
  723. var
  724. r: treference;
  725. begin
  726. if (target_info.system <> system_i386_darwin) then
  727. list.concat(taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(s,AT_FUNCTION)))
  728. else
  729. begin
  730. reference_reset_symbol(r,get_darwin_call_stub(s,false),0,sizeof(pint),[]);
  731. r.refaddr:=addr_full;
  732. list.concat(taicpu.op_ref(A_JMP,S_NO,r));
  733. end;
  734. end;
  735. procedure tcgx86.a_jmp_always(list : TAsmList;l: tasmlabel);
  736. begin
  737. a_jmp_cond(list, OC_NONE, l);
  738. end;
  739. function tcgx86.get_darwin_call_stub(const s: string; weak: boolean): tasmsymbol;
  740. var
  741. stubname: string;
  742. begin
  743. stubname := 'L'+s+'$stub';
  744. result := current_asmdata.getasmsymbol(stubname);
  745. if assigned(result) then
  746. exit;
  747. if current_asmdata.asmlists[al_imports]=nil then
  748. current_asmdata.asmlists[al_imports]:=TAsmList.create;
  749. new_section(current_asmdata.asmlists[al_imports],sec_stub,'',0);
  750. result := current_asmdata.DefineAsmSymbol(stubname,AB_LOCAL,AT_FUNCTION,voidcodepointertype);
  751. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Create(result,0));
  752. { register as a weak symbol if necessary }
  753. if weak then
  754. current_asmdata.weakrefasmsymbol(s,AT_FUNCTION);
  755. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_indirect_symbol,s));
  756. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  757. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  758. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  759. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  760. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  761. end;
  762. procedure tcgx86.a_call_name(list : TAsmList;const s : string; weak: boolean);
  763. begin
  764. a_call_name_near(list,s,weak);
  765. end;
  766. procedure tcgx86.a_call_name_near(list : TAsmList;const s : string; weak: boolean);
  767. var
  768. sym : tasmsymbol;
  769. r : treference;
  770. begin
  771. if (target_info.system <> system_i386_darwin) then
  772. begin
  773. if not(weak) then
  774. sym:=current_asmdata.RefAsmSymbol(s,AT_FUNCTION)
  775. else
  776. sym:=current_asmdata.WeakRefAsmSymbol(s,AT_FUNCTION);
  777. reference_reset_symbol(r,sym,0,sizeof(pint),[]);
  778. if (cs_create_pic in current_settings.moduleswitches) and
  779. { darwin's assembler doesn't want @PLT after call symbols }
  780. not(target_info.system in [system_x86_64_darwin,system_i386_iphonesim,system_x86_64_iphonesim]) then
  781. begin
  782. {$ifdef i386}
  783. include(current_procinfo.flags,pi_needs_got);
  784. {$endif i386}
  785. r.refaddr:=addr_pic
  786. end
  787. else
  788. r.refaddr:=addr_full;
  789. end
  790. else
  791. begin
  792. reference_reset_symbol(r,get_darwin_call_stub(s,weak),0,sizeof(pint),[]);
  793. r.refaddr:=addr_full;
  794. end;
  795. list.concat(taicpu.op_ref(A_CALL,S_NO,r));
  796. end;
  797. procedure tcgx86.a_call_name_static(list : TAsmList;const s : string);
  798. begin
  799. a_call_name_static_near(list,s);
  800. end;
  801. procedure tcgx86.a_call_name_static_near(list : TAsmList;const s : string);
  802. var
  803. sym : tasmsymbol;
  804. r : treference;
  805. begin
  806. sym:=current_asmdata.RefAsmSymbol(s,AT_FUNCTION);
  807. reference_reset_symbol(r,sym,0,sizeof(pint),[]);
  808. r.refaddr:=addr_full;
  809. list.concat(taicpu.op_ref(A_CALL,S_NO,r));
  810. end;
  811. procedure tcgx86.a_call_reg(list : TAsmList;reg : tregister);
  812. begin
  813. a_call_reg_near(list,reg);
  814. end;
  815. procedure tcgx86.a_call_reg_near(list: TAsmList; reg: tregister);
  816. begin
  817. list.concat(taicpu.op_reg(A_CALL,S_NO,reg));
  818. end;
  819. {********************** load instructions ********************}
  820. procedure tcgx86.a_load_const_reg(list : TAsmList; tosize: TCGSize; a : tcgint; reg : TRegister);
  821. begin
  822. check_register_size(tosize,reg);
  823. { the optimizer will change it to "xor reg,reg" when loading zero, }
  824. { no need to do it here too (JM) }
  825. list.concat(taicpu.op_const_reg(A_MOV,TCGSize2OpSize[tosize],a,reg))
  826. end;
  827. procedure tcgx86.a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);
  828. var
  829. tmpref : treference;
  830. begin
  831. tmpref:=ref;
  832. make_simple_ref(list,tmpref);
  833. {$ifdef x86_64}
  834. { x86_64 only supports signed 32 bits constants directly }
  835. if (tosize in [OS_S64,OS_64]) and
  836. ((a<low(longint)) or (a>high(longint))) then
  837. begin
  838. a_load_const_ref(list,OS_32,longint(a and $ffffffff),tmpref);
  839. inc(tmpref.offset,4);
  840. a_load_const_ref(list,OS_32,longint(a shr 32),tmpref);
  841. end
  842. else
  843. {$endif x86_64}
  844. list.concat(taicpu.op_const_ref(A_MOV,TCGSize2OpSize[tosize],a,tmpref));
  845. end;
  846. procedure tcgx86.a_load_reg_ref(list : TAsmList; fromsize,tosize: TCGSize; reg : tregister;const ref : treference);
  847. var
  848. op: tasmop;
  849. s: topsize;
  850. tmpsize : tcgsize;
  851. tmpreg : tregister;
  852. tmpref : treference;
  853. begin
  854. tmpref:=ref;
  855. make_simple_ref(list,tmpref);
  856. if TCGSize2Size[fromsize]>TCGSize2Size[tosize] then
  857. begin
  858. fromsize:=tosize;
  859. reg:=makeregsize(list,reg,fromsize);
  860. end;
  861. check_register_size(fromsize,reg);
  862. sizes2load(fromsize,tosize,op,s);
  863. case s of
  864. {$ifdef x86_64}
  865. S_BQ,S_WQ,S_LQ,
  866. {$endif x86_64}
  867. S_BW,S_BL,S_WL :
  868. begin
  869. tmpreg:=getintregister(list,tosize);
  870. {$ifdef x86_64}
  871. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  872. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  873. 64 bit (FK) }
  874. if s in [S_BL,S_WL,S_L] then
  875. begin
  876. tmpreg:=makeregsize(list,tmpreg,OS_32);
  877. tmpsize:=OS_32;
  878. end
  879. else
  880. {$endif x86_64}
  881. tmpsize:=tosize;
  882. list.concat(taicpu.op_reg_reg(op,s,reg,tmpreg));
  883. a_load_reg_ref(list,tmpsize,tosize,tmpreg,tmpref);
  884. end;
  885. else
  886. list.concat(taicpu.op_reg_ref(op,s,reg,tmpref));
  887. end;
  888. end;
  889. procedure tcgx86.a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  890. begin
  891. a_load_ref_reg_internal(list,fromsize,tosize,ref,reg,false);
  892. end;
  893. procedure tcgx86.a_load_ref_reg_internal(list : TAsmList;fromsize,tosize : tcgsize;const ref: treference;reg : tregister;isdirect:boolean);
  894. var
  895. op: tasmop;
  896. s: topsize;
  897. tmpref : treference;
  898. begin
  899. tmpref:=ref;
  900. make_simple_ref(list,tmpref,isdirect);
  901. check_register_size(tosize,reg);
  902. sizes2load(fromsize,tosize,op,s);
  903. {$ifdef x86_64}
  904. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  905. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  906. 64 bit (FK) }
  907. if s in [S_BL,S_WL,S_L] then
  908. reg:=makeregsize(list,reg,OS_32);
  909. {$endif x86_64}
  910. list.concat(taicpu.op_ref_reg(op,s,tmpref,reg));
  911. end;
  912. procedure tcgx86.a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  913. var
  914. op: tasmop;
  915. s: topsize;
  916. instr:Taicpu;
  917. begin
  918. check_register_size(fromsize,reg1);
  919. check_register_size(tosize,reg2);
  920. if tcgsize2size[fromsize]>tcgsize2size[tosize] then
  921. begin
  922. reg1:=makeregsize(list,reg1,tosize);
  923. s:=tcgsize2opsize[tosize];
  924. op:=A_MOV;
  925. end
  926. else
  927. sizes2load(fromsize,tosize,op,s);
  928. {$ifdef x86_64}
  929. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  930. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  931. 64 bit (FK)
  932. }
  933. if s in [S_BL,S_WL,S_L] then
  934. reg2:=makeregsize(list,reg2,OS_32);
  935. {$endif x86_64}
  936. if (reg1<>reg2) then
  937. begin
  938. instr:=taicpu.op_reg_reg(op,s,reg1,reg2);
  939. { Notify the register allocator that we have written a move instruction so
  940. it can try to eliminate it. }
  941. if (reg1<>current_procinfo.framepointer) and (reg1<>NR_STACK_POINTER_REG) then
  942. add_move_instruction(instr);
  943. list.concat(instr);
  944. end;
  945. {$ifdef x86_64}
  946. { avoid merging of registers and killing the zero extensions (FK) }
  947. if (tosize in [OS_64,OS_S64]) and (s=S_L) then
  948. list.concat(taicpu.op_const_reg(A_AND,S_L,$ffffffff,reg2));
  949. {$endif x86_64}
  950. end;
  951. procedure tcgx86.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  952. var
  953. dirref,tmpref : treference;
  954. begin
  955. dirref:=ref;
  956. { this could probably done in a more optimized way, but for now this
  957. is sufficent }
  958. make_direct_ref(list,dirref);
  959. with dirref do
  960. begin
  961. if (base=NR_NO) and (index=NR_NO) then
  962. begin
  963. if assigned(dirref.symbol) then
  964. begin
  965. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) and
  966. ((dirref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]) or
  967. (cs_create_pic in current_settings.moduleswitches)) then
  968. begin
  969. if (dirref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]) or
  970. ((cs_create_pic in current_settings.moduleswitches) and
  971. (dirref.symbol.bind in [AB_COMMON,AB_GLOBAL,AB_PRIVATE_EXTERN])) then
  972. begin
  973. reference_reset_base(tmpref,
  974. g_indirect_sym_load(list,dirref.symbol.name,asmsym2indsymflags(dirref.symbol)),
  975. offset,sizeof(pint),[]);
  976. a_loadaddr_ref_reg(list,tmpref,r);
  977. end
  978. else
  979. begin
  980. include(current_procinfo.flags,pi_needs_got);
  981. reference_reset_base(tmpref,current_procinfo.got,offset,dirref.alignment,[]);
  982. tmpref.symbol:=symbol;
  983. tmpref.relsymbol:=current_procinfo.CurrGOTLabel;
  984. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,r));
  985. end;
  986. end
  987. else if (cs_create_pic in current_settings.moduleswitches)
  988. {$ifdef x86_64}
  989. and not(dirref.symbol.bind=AB_LOCAL)
  990. {$endif x86_64}
  991. then
  992. begin
  993. {$ifdef x86_64}
  994. reference_reset_symbol(tmpref,dirref.symbol,0,sizeof(pint),[]);
  995. tmpref.refaddr:=addr_pic;
  996. tmpref.base:=NR_RIP;
  997. list.concat(taicpu.op_ref_reg(A_MOV,S_Q,tmpref,r));
  998. {$else x86_64}
  999. reference_reset_symbol(tmpref,dirref.symbol,0,sizeof(pint),[]);
  1000. tmpref.refaddr:=addr_pic;
  1001. tmpref.base:=current_procinfo.got;
  1002. include(current_procinfo.flags,pi_needs_got);
  1003. list.concat(taicpu.op_ref_reg(A_MOV,S_L,tmpref,r));
  1004. {$endif x86_64}
  1005. if offset<>0 then
  1006. a_op_const_reg(list,OP_ADD,OS_ADDR,offset,r);
  1007. end
  1008. {$ifdef x86_64}
  1009. else if (target_info.system in (systems_all_windows+[system_x86_64_darwin,system_x86_64_iphonesim]))
  1010. or (cs_create_pic in current_settings.moduleswitches)
  1011. then
  1012. begin
  1013. { Win64 and Darwin/x86_64 always require RIP-relative addressing }
  1014. tmpref:=dirref;
  1015. tmpref.base:=NR_RIP;
  1016. tmpref.refaddr:=addr_pic_no_got;
  1017. list.concat(Taicpu.op_ref_reg(A_LEA,S_Q,tmpref,r));
  1018. end
  1019. {$endif x86_64}
  1020. else
  1021. begin
  1022. tmpref:=dirref;
  1023. tmpref.refaddr:=ADDR_FULL;
  1024. list.concat(Taicpu.op_ref_reg(A_MOV,tcgsize2opsize[OS_ADDR],tmpref,r));
  1025. end
  1026. end
  1027. else
  1028. a_load_const_reg(list,OS_ADDR,offset,r)
  1029. end
  1030. else if (base=NR_NO) and (index<>NR_NO) and
  1031. (offset=0) and (scalefactor=0) and (symbol=nil) then
  1032. a_load_reg_reg(list,OS_ADDR,OS_ADDR,index,r)
  1033. else if (base<>NR_NO) and (index=NR_NO) and
  1034. (offset=0) and (symbol=nil) then
  1035. a_load_reg_reg(list,OS_ADDR,OS_ADDR,base,r)
  1036. else
  1037. begin
  1038. tmpref:=dirref;
  1039. make_simple_ref(list,tmpref);
  1040. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,r));
  1041. end;
  1042. if segment<>NR_NO then
  1043. begin
  1044. {$ifdef i8086}
  1045. if is_segment_reg(segment) then
  1046. list.concat(Taicpu.op_reg_reg(A_MOV,S_W,segment,GetNextReg(r)))
  1047. else
  1048. a_load_reg_reg(list,OS_16,OS_16,segment,GetNextReg(r));
  1049. {$else i8086}
  1050. if (tf_section_threadvars in target_info.flags) then
  1051. begin
  1052. { Convert thread local address to a process global addres
  1053. as we cannot handle far pointers.}
  1054. case target_info.system of
  1055. system_i386_linux,system_i386_android:
  1056. if segment=NR_GS then
  1057. begin
  1058. reference_reset_symbol(tmpref,current_asmdata.RefAsmSymbol('___fpc_threadvar_offset',AT_DATA),0,sizeof(pint),[]);
  1059. tmpref.segment:=NR_GS;
  1060. list.concat(Taicpu.op_ref_reg(A_ADD,tcgsize2opsize[OS_ADDR],tmpref,r));
  1061. end
  1062. else
  1063. cgmessage(cg_e_cant_use_far_pointer_there);
  1064. else
  1065. cgmessage(cg_e_cant_use_far_pointer_there);
  1066. end;
  1067. end
  1068. else
  1069. cgmessage(cg_e_cant_use_far_pointer_there);
  1070. {$endif i8086}
  1071. end;
  1072. end;
  1073. end;
  1074. { all fpu load routines expect that R_ST[0-7] means an fpu regvar and }
  1075. { R_ST means "the current value at the top of the fpu stack" (JM) }
  1076. procedure tcgx86.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  1077. var
  1078. href: treference;
  1079. op: tasmop;
  1080. s: topsize;
  1081. begin
  1082. if (reg1<>NR_ST) then
  1083. begin
  1084. floatloadops(tosize,op,s);
  1085. list.concat(taicpu.op_reg(op,s,rgfpu.correct_fpuregister(reg1,rgfpu.fpuvaroffset)));
  1086. inc_fpu_stack;
  1087. end;
  1088. if (reg2<>NR_ST) then
  1089. begin
  1090. floatstoreops(tosize,op,s);
  1091. list.concat(taicpu.op_reg(op,s,rgfpu.correct_fpuregister(reg2,rgfpu.fpuvaroffset)));
  1092. dec_fpu_stack;
  1093. end;
  1094. { OS_F80 < OS_C64, but OS_C64 fits perfectly in OS_F80 }
  1095. if (reg1=NR_ST) and
  1096. (reg2=NR_ST) and
  1097. (tosize<>OS_F80) and
  1098. (tosize<fromsize) then
  1099. begin
  1100. { can't round down to lower precision in x87 :/ }
  1101. tg.gettemp(list,tcgsize2size[tosize],tcgsize2size[tosize],tt_normal,href);
  1102. a_loadfpu_reg_ref(list,fromsize,tosize,NR_ST,href);
  1103. a_loadfpu_ref_reg(list,tosize,tosize,href,NR_ST);
  1104. tg.ungettemp(list,href);
  1105. end;
  1106. end;
  1107. procedure tcgx86.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  1108. var
  1109. tmpref : treference;
  1110. begin
  1111. tmpref:=ref;
  1112. make_simple_ref(list,tmpref);
  1113. floatload(list,fromsize,tmpref);
  1114. a_loadfpu_reg_reg(list,fromsize,tosize,NR_ST,reg);
  1115. end;
  1116. procedure tcgx86.a_loadfpu_reg_ref(list: TAsmList; fromsize,tosize: tcgsize; reg: tregister; const ref: treference);
  1117. var
  1118. tmpref : treference;
  1119. begin
  1120. tmpref:=ref;
  1121. make_simple_ref(list,tmpref);
  1122. { in case a record returned in a floating point register
  1123. (LOC_FPUREGISTER with OS_F32/OS_F64) is stored in memory
  1124. (LOC_REFERENCE with OS_32/OS_64), we have to adjust the
  1125. tosize }
  1126. if (fromsize in [OS_F32,OS_F64]) and
  1127. (tcgsize2size[fromsize]=tcgsize2size[tosize]) then
  1128. case tosize of
  1129. OS_32:
  1130. tosize:=OS_F32;
  1131. OS_64:
  1132. tosize:=OS_F64;
  1133. end;
  1134. if reg<>NR_ST then
  1135. a_loadfpu_reg_reg(list,fromsize,tosize,reg,NR_ST);
  1136. floatstore(list,tosize,tmpref);
  1137. end;
  1138. function get_scalar_mm_op(fromsize,tosize : tcgsize) : tasmop;
  1139. const
  1140. convertopsse : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  1141. (A_MOVSS,A_CVTSS2SD,A_NONE,A_NONE,A_NONE),
  1142. (A_CVTSD2SS,A_MOVSD,A_NONE,A_NONE,A_NONE),
  1143. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  1144. (A_NONE,A_NONE,A_NONE,A_MOVQ,A_NONE),
  1145. (A_NONE,A_NONE,A_NONE,A_NONE,A_MOVAPS));
  1146. convertopavx : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  1147. (A_VMOVSS,A_VCVTSS2SD,A_NONE,A_NONE,A_NONE),
  1148. (A_VCVTSD2SS,A_VMOVSD,A_NONE,A_NONE,A_NONE),
  1149. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  1150. (A_NONE,A_NONE,A_NONE,A_MOVQ,A_NONE),
  1151. (A_NONE,A_NONE,A_NONE,A_NONE,A_VMOVAPS));
  1152. begin
  1153. { we can have OS_F32/OS_F64 (record in function result/LOC_MMREGISTER) to
  1154. OS_32/OS_64 (record in memory/LOC_REFERENCE) }
  1155. if (fromsize in [OS_F32,OS_F64]) and
  1156. (tcgsize2size[fromsize]=tcgsize2size[tosize]) then
  1157. case tosize of
  1158. OS_32:
  1159. tosize:=OS_F32;
  1160. OS_64:
  1161. tosize:=OS_F64;
  1162. end;
  1163. if (fromsize in [low(convertopsse)..high(convertopsse)]) and
  1164. (tosize in [low(convertopsse)..high(convertopsse)]) then
  1165. begin
  1166. if UseAVX then
  1167. result:=convertopavx[fromsize,tosize]
  1168. else
  1169. result:=convertopsse[fromsize,tosize];
  1170. end
  1171. { we can have OS_M64 (record in function result/LOC_MMREGISTER) to
  1172. OS_64 (record in memory/LOC_REFERENCE) }
  1173. else if (tcgsize2size[fromsize]=tcgsize2size[tosize]) then
  1174. begin
  1175. case fromsize of
  1176. OS_M64:
  1177. { we can have OS_M64 (record in function result/LOC_MMREGISTER) to
  1178. OS_64 (record in memory/LOC_REFERENCE) }
  1179. if UseAVX then
  1180. result:=A_VMOVQ
  1181. else
  1182. result:=A_MOVQ;
  1183. OS_M128:
  1184. { 128-bit aligned vector }
  1185. if UseAVX then
  1186. result:=A_VMOVAPS
  1187. else
  1188. result:=A_MOVAPS;
  1189. OS_M256,
  1190. OS_M512:
  1191. { 256-bit aligned vector }
  1192. if UseAVX then
  1193. result:=A_VMOVAPS
  1194. else
  1195. { SSE does not support 256-bit or 512-bit vectors }
  1196. InternalError(2018012930);
  1197. else
  1198. InternalError(2018012920);
  1199. end;
  1200. end
  1201. else
  1202. internalerror(2010060104);
  1203. if result=A_NONE then
  1204. internalerror(200312205);
  1205. end;
  1206. procedure tcgx86.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle);
  1207. var
  1208. instr : taicpu;
  1209. op : TAsmOp;
  1210. begin
  1211. if shuffle=nil then
  1212. begin
  1213. if fromsize=tosize then
  1214. { needs correct size in case of spilling }
  1215. case fromsize of
  1216. OS_F32,
  1217. OS_MF128:
  1218. if UseAVX then
  1219. instr:=taicpu.op_reg_reg(A_VMOVAPS,S_NO,reg1,reg2)
  1220. else
  1221. instr:=taicpu.op_reg_reg(A_MOVAPS,S_NO,reg1,reg2);
  1222. OS_F64,
  1223. OS_MD128:
  1224. if UseAVX then
  1225. instr:=taicpu.op_reg_reg(A_VMOVAPD,S_NO,reg1,reg2)
  1226. else
  1227. instr:=taicpu.op_reg_reg(A_MOVAPD,S_NO,reg1,reg2);
  1228. OS_M64:
  1229. if UseAVX then
  1230. instr:=taicpu.op_reg_reg(A_VMOVQ,S_NO,reg1,reg2)
  1231. else
  1232. instr:=taicpu.op_reg_reg(A_MOVQ,S_NO,reg1,reg2);
  1233. OS_M128, OS_MS128:
  1234. if UseAVX then
  1235. instr:=taicpu.op_reg_reg(A_VMOVDQA,S_NO,reg1,reg2)
  1236. else
  1237. instr:=taicpu.op_reg_reg(A_MOVDQA,S_NO,reg1,reg2);
  1238. OS_MF256,
  1239. OS_MF512:
  1240. if UseAVX then
  1241. instr:=taicpu.op_reg_reg(A_VMOVAPS,S_NO,reg1,reg2)
  1242. else
  1243. { SSE doesn't support 512-bit vectors }
  1244. InternalError(2018012931);
  1245. OS_MD256,
  1246. OS_MD512:
  1247. if UseAVX then
  1248. instr:=taicpu.op_reg_reg(A_VMOVAPD,S_NO,reg1,reg2)
  1249. else
  1250. { SSE doesn't support 512-bit vectors }
  1251. InternalError(2018012932);
  1252. OS_M256, OS_MS256,
  1253. OS_M512, OS_MS512:
  1254. if UseAVX then
  1255. instr:=taicpu.op_reg_reg(A_VMOVDQA,S_NO,reg1,reg2)
  1256. else
  1257. { SSE doesn't support 512-bit vectors }
  1258. InternalError(2018012933);
  1259. else
  1260. internalerror(2006091201);
  1261. end
  1262. else
  1263. internalerror(200312202);
  1264. add_move_instruction(instr);
  1265. end
  1266. else if shufflescalar(shuffle) then
  1267. begin
  1268. op:=get_scalar_mm_op(fromsize,tosize);
  1269. { MOVAPD/MOVAPS are normally faster }
  1270. if op=A_MOVSD then
  1271. op:=A_MOVAPD
  1272. else if op=A_MOVSS then
  1273. op:=A_MOVAPS
  1274. { VMOVSD/SS is not available with two register operands }
  1275. else if op=A_VMOVSD then
  1276. op:=A_VMOVAPD
  1277. else if op=A_VMOVSS then
  1278. op:=A_VMOVAPS;
  1279. { A_VCVTSD2SS and A_VCVTSS2SD require always three operands }
  1280. if (op=A_VCVTSD2SS) or (op=A_VCVTSS2SD) then
  1281. instr:=taicpu.op_reg_reg_reg(op,S_NO,reg1,reg2,reg2)
  1282. else
  1283. instr:=taicpu.op_reg_reg(op,S_NO,reg1,reg2);
  1284. case op of
  1285. A_VMOVAPD,
  1286. A_VMOVAPS,
  1287. A_VMOVSS,
  1288. A_VMOVSD,
  1289. A_VMOVQ,
  1290. A_MOVAPD,
  1291. A_MOVAPS,
  1292. A_MOVSS,
  1293. A_MOVSD,
  1294. A_MOVQ:
  1295. add_move_instruction(instr);
  1296. end;
  1297. end
  1298. else
  1299. internalerror(200312201);
  1300. list.concat(instr);
  1301. end;
  1302. procedure tcgx86.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1303. var
  1304. tmpref : treference;
  1305. op : tasmop;
  1306. begin
  1307. tmpref:=ref;
  1308. make_simple_ref(list,tmpref);
  1309. if shuffle=nil then
  1310. begin
  1311. case fromsize of
  1312. OS_F32:
  1313. if UseAVX then
  1314. op := A_VMOVSS
  1315. else
  1316. op := A_MOVSS;
  1317. OS_F64:
  1318. if UseAVX then
  1319. op := A_VMOVSD
  1320. else
  1321. op := A_MOVSD;
  1322. OS_M32, OS_32, OS_S32:
  1323. if UseAVX then
  1324. op := A_VMOVD
  1325. else
  1326. op := A_MOVD;
  1327. OS_M64, OS_64, OS_S64:
  1328. if UseAVX then
  1329. op := A_VMOVQ
  1330. else
  1331. op := A_MOVQ;
  1332. OS_MF128:
  1333. { Use XMM transfer of packed singles }
  1334. if UseAVX then
  1335. begin
  1336. if GetRefAlignment(tmpref) = 16 then
  1337. op := A_VMOVAPS
  1338. else
  1339. op := A_VMOVUPS
  1340. end
  1341. else
  1342. begin
  1343. if GetRefAlignment(tmpref) = 16 then
  1344. op := A_MOVAPS
  1345. else
  1346. op := A_MOVUPS
  1347. end;
  1348. OS_MD128:
  1349. { Use XMM transfer of packed doubles }
  1350. if UseAVX then
  1351. begin
  1352. if GetRefAlignment(tmpref) = 16 then
  1353. op := A_VMOVAPD
  1354. else
  1355. op := A_VMOVUPD
  1356. end
  1357. else
  1358. begin
  1359. if GetRefAlignment(tmpref) = 16 then
  1360. op := A_MOVAPD
  1361. else
  1362. op := A_MOVUPD
  1363. end;
  1364. OS_M128, OS_MS128:
  1365. { Use XMM integer transfer }
  1366. if UseAVX then
  1367. begin
  1368. if GetRefAlignment(tmpref) = 16 then
  1369. op := A_VMOVDQA
  1370. else
  1371. op := A_VMOVDQU
  1372. end
  1373. else
  1374. begin
  1375. if GetRefAlignment(tmpref) = 16 then
  1376. op := A_MOVDQA
  1377. else
  1378. op := A_MOVDQU
  1379. end;
  1380. OS_MF256:
  1381. { Use YMM transfer of packed singles }
  1382. if UseAVX then
  1383. begin
  1384. if GetRefAlignment(tmpref) = 32 then
  1385. op := A_VMOVAPS
  1386. else
  1387. op := A_VMOVUPS
  1388. end
  1389. else
  1390. { SSE doesn't support 256-bit vectors }
  1391. InternalError(2018012934);
  1392. OS_MD256:
  1393. { Use YMM transfer of packed doubles }
  1394. if UseAVX then
  1395. begin
  1396. if GetRefAlignment(tmpref) = 32 then
  1397. op := A_VMOVAPD
  1398. else
  1399. op := A_VMOVUPD
  1400. end
  1401. else
  1402. { SSE doesn't support 256-bit vectors }
  1403. InternalError(2018012935);
  1404. OS_M256, OS_MS256:
  1405. { Use YMM integer transfer }
  1406. if UseAVX then
  1407. begin
  1408. if GetRefAlignment(tmpref) = 32 then
  1409. op := A_VMOVDQA
  1410. else
  1411. op := A_VMOVDQU
  1412. end
  1413. else
  1414. { SSE doesn't support 256-bit vectors }
  1415. InternalError(2018012936);
  1416. OS_MF512:
  1417. { Use ZMM transfer of packed singles }
  1418. if UseAVX then
  1419. begin
  1420. if GetRefAlignment(tmpref) = 64 then
  1421. op := A_VMOVAPS
  1422. else
  1423. op := A_VMOVUPS
  1424. end
  1425. else
  1426. { SSE doesn't support 512-bit vectors }
  1427. InternalError(2018012937);
  1428. OS_MD512:
  1429. { Use ZMM transfer of packed doubles }
  1430. if UseAVX then
  1431. begin
  1432. if GetRefAlignment(tmpref) = 64 then
  1433. op := A_VMOVAPD
  1434. else
  1435. op := A_VMOVUPD
  1436. end
  1437. else
  1438. { SSE doesn't support 512-bit vectors }
  1439. InternalError(2018012938);
  1440. OS_M512, OS_MS512:
  1441. { Use ZMM integer transfer }
  1442. if UseAVX then
  1443. begin
  1444. if GetRefAlignment(tmpref) = 64 then
  1445. op := A_VMOVDQA
  1446. else
  1447. op := A_VMOVDQU
  1448. end
  1449. else
  1450. { SSE doesn't support 512-bit vectors }
  1451. InternalError(2018012939);
  1452. else
  1453. { No valid transfer command available }
  1454. internalerror(2017121410);
  1455. end;
  1456. list.concat(taicpu.op_ref_reg(op,S_NO,tmpref,reg));
  1457. end
  1458. else if shufflescalar(shuffle) then
  1459. begin
  1460. op:=get_scalar_mm_op(fromsize,tosize);
  1461. { A_VCVTSD2SS and A_VCVTSS2SD require always three operands }
  1462. if (op=A_VCVTSD2SS) or (op=A_VCVTSS2SD) then
  1463. list.concat(taicpu.op_ref_reg_reg(op,S_NO,tmpref,reg,reg))
  1464. else
  1465. list.concat(taicpu.op_ref_reg(op,S_NO,tmpref,reg))
  1466. end
  1467. else
  1468. internalerror(200312252);
  1469. end;
  1470. procedure tcgx86.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle);
  1471. var
  1472. hreg : tregister;
  1473. tmpref : treference;
  1474. op : tasmop;
  1475. begin
  1476. tmpref:=ref;
  1477. make_simple_ref(list,tmpref);
  1478. if shuffle=nil then
  1479. begin
  1480. case fromsize of
  1481. OS_F32:
  1482. if UseAVX then
  1483. op := A_VMOVSS
  1484. else
  1485. op := A_MOVSS;
  1486. OS_F64:
  1487. if UseAVX then
  1488. op := A_VMOVSD
  1489. else
  1490. op := A_MOVSD;
  1491. OS_M32, OS_32, OS_S32:
  1492. if UseAVX then
  1493. op := A_VMOVD
  1494. else
  1495. op := A_MOVD;
  1496. OS_M64, OS_64, OS_S64:
  1497. if UseAVX then
  1498. op := A_VMOVQ
  1499. else
  1500. op := A_MOVQ;
  1501. OS_MF128:
  1502. { Use XMM transfer of packed singles }
  1503. if UseAVX then
  1504. begin
  1505. if GetRefAlignment(tmpref) = 16 then
  1506. op := A_VMOVAPS
  1507. else
  1508. op := A_VMOVUPS
  1509. end else
  1510. begin
  1511. if GetRefAlignment(tmpref) = 16 then
  1512. op := A_MOVAPS
  1513. else
  1514. op := A_MOVUPS
  1515. end;
  1516. OS_MD128:
  1517. { Use XMM transfer of packed doubles }
  1518. if UseAVX then
  1519. begin
  1520. if GetRefAlignment(tmpref) = 16 then
  1521. op := A_VMOVAPD
  1522. else
  1523. op := A_VMOVUPD
  1524. end else
  1525. begin
  1526. if GetRefAlignment(tmpref) = 16 then
  1527. op := A_MOVAPD
  1528. else
  1529. op := A_MOVUPD
  1530. end;
  1531. OS_M128, OS_MS128:
  1532. { Use XMM integer transfer }
  1533. if UseAVX then
  1534. begin
  1535. if GetRefAlignment(tmpref) = 16 then
  1536. op := A_VMOVDQA
  1537. else
  1538. op := A_VMOVDQU
  1539. end else
  1540. begin
  1541. if GetRefAlignment(tmpref) = 16 then
  1542. op := A_MOVDQA
  1543. else
  1544. op := A_MOVDQU
  1545. end;
  1546. OS_MF256:
  1547. { Use XMM transfer of packed singles }
  1548. if UseAVX then
  1549. begin
  1550. if GetRefAlignment(tmpref) = 32 then
  1551. op := A_VMOVAPS
  1552. else
  1553. op := A_VMOVUPS
  1554. end else
  1555. { SSE doesn't support 256-bit vectors }
  1556. InternalError(2018012940);
  1557. OS_MD256:
  1558. { Use XMM transfer of packed doubles }
  1559. if UseAVX then
  1560. begin
  1561. if GetRefAlignment(tmpref) = 32 then
  1562. op := A_VMOVAPD
  1563. else
  1564. op := A_VMOVUPD
  1565. end else
  1566. { SSE doesn't support 256-bit vectors }
  1567. InternalError(2018012941);
  1568. OS_M256, OS_MS256:
  1569. { Use XMM integer transfer }
  1570. if UseAVX then
  1571. begin
  1572. if GetRefAlignment(tmpref) = 32 then
  1573. op := A_VMOVDQA
  1574. else
  1575. op := A_VMOVDQU
  1576. end else
  1577. { SSE doesn't support 256-bit vectors }
  1578. InternalError(2018012942);
  1579. OS_MF512:
  1580. { Use XMM transfer of packed singles }
  1581. if UseAVX then
  1582. begin
  1583. if GetRefAlignment(tmpref) = 64 then
  1584. op := A_VMOVAPS
  1585. else
  1586. op := A_VMOVUPS
  1587. end else
  1588. { SSE doesn't support 512-bit vectors }
  1589. InternalError(2018012943);
  1590. OS_MD512:
  1591. { Use XMM transfer of packed doubles }
  1592. if UseAVX then
  1593. begin
  1594. if GetRefAlignment(tmpref) = 64 then
  1595. op := A_VMOVAPD
  1596. else
  1597. op := A_VMOVUPD
  1598. end else
  1599. { SSE doesn't support 512-bit vectors }
  1600. InternalError(2018012944);
  1601. OS_M512, OS_MS512:
  1602. { Use XMM integer transfer }
  1603. if UseAVX then
  1604. begin
  1605. if GetRefAlignment(tmpref) = 64 then
  1606. op := A_VMOVDQA
  1607. else
  1608. op := A_VMOVDQU
  1609. end else
  1610. { SSE doesn't support 512-bit vectors }
  1611. InternalError(2018012945);
  1612. else
  1613. { No valid transfer command available }
  1614. internalerror(2017121411);
  1615. end;
  1616. list.concat(taicpu.op_reg_ref(op,S_NO,reg,tmpref));
  1617. end
  1618. else if shufflescalar(shuffle) then
  1619. begin
  1620. if tcgsize2size[tosize]<>tcgsize2size[fromsize] then
  1621. begin
  1622. hreg:=getmmregister(list,tosize);
  1623. op:=get_scalar_mm_op(fromsize,tosize);
  1624. { A_VCVTSD2SS and A_VCVTSS2SD require always three operands }
  1625. if (op=A_VCVTSD2SS) or (op=A_VCVTSS2SD) then
  1626. list.concat(taicpu.op_reg_reg_reg(op,S_NO,reg,hreg,hreg))
  1627. else
  1628. list.concat(taicpu.op_reg_reg(op,S_NO,reg,hreg));
  1629. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(tosize,tosize),S_NO,hreg,tmpref))
  1630. end
  1631. else
  1632. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(fromsize,tosize),S_NO,reg,tmpref));
  1633. end
  1634. else
  1635. internalerror(200312252);
  1636. end;
  1637. procedure tcgx86.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1638. var
  1639. l : tlocation;
  1640. begin
  1641. l.loc:=LOC_REFERENCE;
  1642. l.reference:=ref;
  1643. l.size:=size;
  1644. opmm_loc_reg(list,op,size,l,reg,shuffle);
  1645. end;
  1646. procedure tcgx86.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle);
  1647. var
  1648. l : tlocation;
  1649. begin
  1650. l.loc:=LOC_MMREGISTER;
  1651. l.register:=src;
  1652. l.size:=size;
  1653. opmm_loc_reg(list,op,size,l,dst,shuffle);
  1654. end;
  1655. procedure tcgx86.opmm_loc_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;src,dst: tregister; shuffle : pmmshuffle);
  1656. const
  1657. opmm2asmop : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  1658. ( { scalar }
  1659. ( { OS_F32 }
  1660. A_NOP,A_NOP,A_VADDSS,A_NOP,A_VDIVSS,A_NOP,A_NOP,A_VMULSS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBSS,A_NOP,A_NOP,A_NOP
  1661. ),
  1662. ( { OS_F64 }
  1663. A_NOP,A_NOP,A_VADDSD,A_NOP,A_VDIVSD,A_NOP,A_NOP,A_VMULSD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBSD,A_NOP,A_NOP,A_NOP
  1664. )
  1665. ),
  1666. ( { vectorized/packed }
  1667. { because the logical packed single instructions have shorter op codes, we use always
  1668. these
  1669. }
  1670. ( { OS_F32 }
  1671. A_NOP,A_NOP,A_VADDPS,A_NOP,A_VDIVPS,A_NOP,A_NOP,A_VMULPS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBPS,A_VXORPS,A_NOP,A_NOP
  1672. ),
  1673. ( { OS_F64 }
  1674. A_NOP,A_NOP,A_VADDPD,A_NOP,A_VDIVPD,A_NOP,A_NOP,A_VMULPD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBPD,A_VXORPD,A_NOP,A_NOP
  1675. )
  1676. )
  1677. );
  1678. var
  1679. resultreg : tregister;
  1680. asmop : tasmop;
  1681. begin
  1682. { this is an internally used procedure so the parameters have
  1683. some constrains
  1684. }
  1685. if loc.size<>size then
  1686. internalerror(2013061108);
  1687. resultreg:=dst;
  1688. { deshuffle }
  1689. //!!!
  1690. if (shuffle<>nil) and not(shufflescalar(shuffle)) then
  1691. begin
  1692. internalerror(2013061107);
  1693. end
  1694. else if (shuffle=nil) then
  1695. asmop:=opmm2asmop[1,size,op]
  1696. else if shufflescalar(shuffle) then
  1697. begin
  1698. asmop:=opmm2asmop[0,size,op];
  1699. { no scalar operation available? }
  1700. if asmop=A_NOP then
  1701. begin
  1702. { do vectorized and shuffle finally }
  1703. internalerror(2010060102);
  1704. end;
  1705. end
  1706. else
  1707. internalerror(2013061106);
  1708. if asmop=A_NOP then
  1709. internalerror(2013061105);
  1710. case loc.loc of
  1711. LOC_CREFERENCE,LOC_REFERENCE:
  1712. begin
  1713. make_simple_ref(current_asmdata.CurrAsmList,loc.reference);
  1714. list.concat(taicpu.op_ref_reg_reg(asmop,S_NO,loc.reference,src,resultreg));
  1715. end;
  1716. LOC_CMMREGISTER,LOC_MMREGISTER:
  1717. list.concat(taicpu.op_reg_reg_reg(asmop,S_NO,loc.register,src,resultreg));
  1718. else
  1719. internalerror(2013061104);
  1720. end;
  1721. { shuffle }
  1722. if resultreg<>dst then
  1723. begin
  1724. internalerror(2013061103);
  1725. end;
  1726. end;
  1727. procedure tcgx86.a_opmm_reg_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src1,src2,dst: tregister;shuffle : pmmshuffle);
  1728. var
  1729. l : tlocation;
  1730. begin
  1731. l.loc:=LOC_MMREGISTER;
  1732. l.register:=src1;
  1733. l.size:=size;
  1734. opmm_loc_reg_reg(list,op,size,l,src2,dst,shuffle);
  1735. end;
  1736. procedure tcgx86.a_opmm_ref_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; src,dst: tregister;shuffle : pmmshuffle);
  1737. var
  1738. l : tlocation;
  1739. begin
  1740. l.loc:=LOC_REFERENCE;
  1741. l.reference:=ref;
  1742. l.size:=size;
  1743. opmm_loc_reg_reg(list,op,size,l,src,dst,shuffle);
  1744. end;
  1745. procedure tcgx86.opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;dst: tregister; shuffle : pmmshuffle);
  1746. const
  1747. opmm2asmop : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  1748. ( { scalar }
  1749. ( { OS_F32 }
  1750. A_NOP,A_NOP,A_ADDSS,A_NOP,A_DIVSS,A_NOP,A_NOP,A_MULSS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_SUBSS,A_NOP,A_NOP,A_NOP
  1751. ),
  1752. ( { OS_F64 }
  1753. A_NOP,A_NOP,A_ADDSD,A_NOP,A_DIVSD,A_NOP,A_NOP,A_MULSD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_SUBSD,A_NOP,A_NOP,A_NOP
  1754. )
  1755. ),
  1756. ( { vectorized/packed }
  1757. { because the logical packed single instructions have shorter op codes, we use always
  1758. these
  1759. }
  1760. ( { OS_F32 }
  1761. A_NOP,A_NOP,A_ADDPS,A_NOP,A_DIVPS,A_NOP,A_NOP,A_MULPS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_SUBPS,A_XORPS,A_NOP,A_NOP
  1762. ),
  1763. ( { OS_F64 }
  1764. A_NOP,A_NOP,A_ADDPD,A_NOP,A_DIVPD,A_NOP,A_NOP,A_MULPD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_SUBPD,A_XORPD,A_NOP,A_NOP
  1765. )
  1766. )
  1767. );
  1768. var
  1769. resultreg : tregister;
  1770. asmop : tasmop;
  1771. begin
  1772. { this is an internally used procedure so the parameters have
  1773. some constrains
  1774. }
  1775. if loc.size<>size then
  1776. internalerror(200312213);
  1777. resultreg:=dst;
  1778. { deshuffle }
  1779. //!!!
  1780. if (shuffle<>nil) and not(shufflescalar(shuffle)) then
  1781. begin
  1782. internalerror(2010060101);
  1783. end
  1784. else if (shuffle=nil) then
  1785. asmop:=opmm2asmop[1,size,op]
  1786. else if shufflescalar(shuffle) then
  1787. begin
  1788. asmop:=opmm2asmop[0,size,op];
  1789. { no scalar operation available? }
  1790. if asmop=A_NOP then
  1791. begin
  1792. { do vectorized and shuffle finally }
  1793. internalerror(2010060102);
  1794. end;
  1795. end
  1796. else
  1797. internalerror(200312211);
  1798. if asmop=A_NOP then
  1799. internalerror(200312216);
  1800. case loc.loc of
  1801. LOC_CREFERENCE,LOC_REFERENCE:
  1802. begin
  1803. make_simple_ref(current_asmdata.CurrAsmList,loc.reference);
  1804. list.concat(taicpu.op_ref_reg(asmop,S_NO,loc.reference,resultreg));
  1805. end;
  1806. LOC_CMMREGISTER,LOC_MMREGISTER:
  1807. list.concat(taicpu.op_reg_reg(asmop,S_NO,loc.register,resultreg));
  1808. else
  1809. internalerror(200312214);
  1810. end;
  1811. { shuffle }
  1812. if resultreg<>dst then
  1813. begin
  1814. internalerror(200312212);
  1815. end;
  1816. end;
  1817. {$ifndef i8086}
  1818. procedure tcgx86.a_op_const_reg_reg(list:TAsmList;op:Topcg;size:Tcgsize;
  1819. a:tcgint;src,dst:Tregister);
  1820. var
  1821. power,al : longint;
  1822. href : treference;
  1823. begin
  1824. power:=0;
  1825. optimize_op_const(size,op,a);
  1826. case op of
  1827. OP_NONE:
  1828. begin
  1829. a_load_reg_reg(list,size,size,src,dst);
  1830. exit;
  1831. end;
  1832. OP_MOVE:
  1833. begin
  1834. a_load_const_reg(list,size,a,dst);
  1835. exit;
  1836. end;
  1837. end;
  1838. if (op in [OP_MUL,OP_IMUL]) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1839. not(cs_check_overflow in current_settings.localswitches) and
  1840. (a>1) and ispowerof2(int64(a-1),power) and (power in [1..3]) then
  1841. begin
  1842. reference_reset_base(href,src,0,0,[]);
  1843. href.index:=src;
  1844. href.scalefactor:=a-1;
  1845. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1846. end
  1847. else if (op in [OP_MUL,OP_IMUL]) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1848. not(cs_check_overflow in current_settings.localswitches) and
  1849. (a>1) and ispowerof2(int64(a),power) and (power in [1..3]) then
  1850. begin
  1851. reference_reset_base(href,NR_NO,0,0,[]);
  1852. href.index:=src;
  1853. href.scalefactor:=a;
  1854. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1855. end
  1856. else if (op in [OP_MUL,OP_IMUL]) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1857. (a>1) and (a<=maxLongint) and not ispowerof2(int64(a),power) then
  1858. begin
  1859. { MUL with overflow checking should be handled specifically in the code generator }
  1860. if (op=OP_MUL) and (cs_check_overflow in current_settings.localswitches) then
  1861. internalerror(2014011801);
  1862. list.concat(taicpu.op_const_reg_reg(A_IMUL,TCgSize2OpSize[size],a,src,dst));
  1863. end
  1864. else if (op=OP_ADD) and
  1865. ((size in [OS_32,OS_S32]) or
  1866. { lea supports only 32 bit signed displacments }
  1867. ((size=OS_64) and (a>=0) and (a<=maxLongint)) or
  1868. ((size=OS_S64) and (a>=-maxLongint) and (a<=maxLongint))
  1869. ) and
  1870. not(cs_check_overflow in current_settings.localswitches) then
  1871. begin
  1872. { a might still be in the range 0x80000000 to 0xffffffff
  1873. which might trigger a range check error as
  1874. reference_reset_base expects a longint value. }
  1875. {$push} {$R-}{$Q-}
  1876. al := longint (a);
  1877. {$pop}
  1878. reference_reset_base(href,src,al,0,[]);
  1879. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1880. end
  1881. else if (op=OP_SUB) and
  1882. ((size in [OS_32,OS_S32]) or
  1883. { lea supports only 32 bit signed displacments }
  1884. ((size=OS_64) and (a>=0) and (a<=maxLongint)) or
  1885. ((size=OS_S64) and (a>=-maxLongint) and (a<=maxLongint))
  1886. ) and
  1887. not(cs_check_overflow in current_settings.localswitches) then
  1888. begin
  1889. reference_reset_base(href,src,-a,0,[]);
  1890. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1891. end
  1892. else if (op in [OP_ROR,OP_ROL]) and
  1893. (CPUX86_HAS_BMI2 in cpu_capabilities[current_settings.cputype]) and
  1894. (size in [OS_32,OS_S32
  1895. {$ifdef x86_64}
  1896. ,OS_64,OS_S64
  1897. {$endif x86_64}
  1898. ]) then
  1899. begin
  1900. if op=OP_ROR then
  1901. list.concat(taicpu.op_const_reg_reg(A_RORX,TCgSize2OpSize[size], a,src,dst))
  1902. else
  1903. list.concat(taicpu.op_const_reg_reg(A_RORX,TCgSize2OpSize[size],TCgSize2Size[size]*8-a,src,dst));
  1904. end
  1905. else
  1906. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  1907. end;
  1908. procedure tcgx86.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  1909. size: tcgsize; src1, src2, dst: tregister);
  1910. var
  1911. href : treference;
  1912. begin
  1913. if (op=OP_ADD) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1914. not(cs_check_overflow in current_settings.localswitches) then
  1915. begin
  1916. reference_reset_base(href,src1,0,0,[]);
  1917. href.index:=src2;
  1918. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1919. end
  1920. else if (op in [OP_SHR,OP_SHL]) and
  1921. (CPUX86_HAS_BMI2 in cpu_capabilities[current_settings.cputype]) and
  1922. (size in [OS_32,OS_S32
  1923. {$ifdef x86_64}
  1924. ,OS_64,OS_S64
  1925. {$endif x86_64}
  1926. ]) then
  1927. begin
  1928. if op=OP_SHL then
  1929. list.concat(taicpu.op_reg_reg_reg(A_SHLX,TCgSize2OpSize[size],src1,src2,dst))
  1930. else
  1931. list.concat(taicpu.op_reg_reg_reg(A_SHRX,TCgSize2OpSize[size],src1,src2,dst));
  1932. end
  1933. else
  1934. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1935. end;
  1936. {$endif not i8086}
  1937. procedure tcgx86.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister);
  1938. {$ifdef x86_64}
  1939. var
  1940. tmpreg : tregister;
  1941. {$endif x86_64}
  1942. begin
  1943. optimize_op_const(size, op, a);
  1944. {$ifdef x86_64}
  1945. { x86_64 only supports signed 32 bits constants directly }
  1946. if not(op in [OP_NONE,OP_MOVE]) and
  1947. (size in [OS_S64,OS_64]) and
  1948. ((a<low(longint)) or (a>high(longint))) then
  1949. begin
  1950. tmpreg:=getintregister(list,size);
  1951. a_load_const_reg(list,size,a,tmpreg);
  1952. a_op_reg_reg(list,op,size,tmpreg,reg);
  1953. exit;
  1954. end;
  1955. {$endif x86_64}
  1956. check_register_size(size,reg);
  1957. case op of
  1958. OP_NONE :
  1959. begin
  1960. { Opcode is optimized away }
  1961. end;
  1962. OP_MOVE :
  1963. begin
  1964. { Optimized, replaced with a simple load }
  1965. a_load_const_reg(list,size,a,reg);
  1966. end;
  1967. OP_DIV, OP_IDIV:
  1968. begin
  1969. { should be handled specifically in the code }
  1970. { generator because of the silly register usage restraints }
  1971. internalerror(200109224);
  1972. end;
  1973. OP_MUL,OP_IMUL:
  1974. begin
  1975. if not (cs_check_overflow in current_settings.localswitches) then
  1976. op:=OP_IMUL;
  1977. if op = OP_IMUL then
  1978. list.concat(taicpu.op_const_reg(A_IMUL,TCgSize2OpSize[size],a,reg))
  1979. else
  1980. { OP_MUL should be handled specifically in the code }
  1981. { generator because of the silly register usage restraints }
  1982. internalerror(200109225);
  1983. end;
  1984. OP_ADD, OP_SUB:
  1985. if not(cs_check_overflow in current_settings.localswitches) and
  1986. (a = 1) and
  1987. UseIncDec then
  1988. begin
  1989. if op = OP_ADD then
  1990. list.concat(taicpu.op_reg(A_INC,TCgSize2OpSize[size],reg))
  1991. else
  1992. list.concat(taicpu.op_reg(A_DEC,TCgSize2OpSize[size],reg))
  1993. end
  1994. else
  1995. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],ImmInt(a),reg));
  1996. OP_AND,OP_OR:
  1997. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],ImmInt(a),reg));
  1998. OP_XOR:
  1999. if (aword(a)=high(aword)) then
  2000. list.concat(taicpu.op_reg(A_NOT,TCgSize2OpSize[size],reg))
  2001. else
  2002. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],ImmInt(a),reg));
  2003. OP_SHL,OP_SHR,OP_SAR,OP_ROL,OP_ROR:
  2004. begin
  2005. {$if defined(x86_64)}
  2006. if (a and 63) <> 0 Then
  2007. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 63,reg));
  2008. if (a shr 6) <> 0 Then
  2009. internalerror(200609073);
  2010. {$elseif defined(i386)}
  2011. if (a and 31) <> 0 Then
  2012. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,reg));
  2013. if (a shr 5) <> 0 Then
  2014. internalerror(200609071);
  2015. {$elseif defined(i8086)}
  2016. if (a shr 5) <> 0 Then
  2017. internalerror(2013043002);
  2018. a := a and 31;
  2019. if a <> 0 Then
  2020. begin
  2021. if (current_settings.cputype < cpu_186) and (a <> 1) then
  2022. begin
  2023. getcpuregister(list,NR_CL);
  2024. a_load_const_reg(list,OS_8,a,NR_CL);
  2025. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],NR_CL,reg));
  2026. ungetcpuregister(list,NR_CL);
  2027. end
  2028. else
  2029. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,reg));
  2030. end;
  2031. {$endif}
  2032. end
  2033. else internalerror(200609072);
  2034. end;
  2035. end;
  2036. procedure tcgx86.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  2037. var
  2038. {$ifdef x86_64}
  2039. tmpreg : tregister;
  2040. {$endif x86_64}
  2041. tmpref : treference;
  2042. begin
  2043. optimize_op_const(size, op, a);
  2044. if op in [OP_NONE,OP_MOVE] then
  2045. begin
  2046. if (op=OP_MOVE) then
  2047. a_load_const_ref(list,size,a,ref);
  2048. exit;
  2049. end;
  2050. {$ifdef x86_64}
  2051. { x86_64 only supports signed 32 bits constants directly }
  2052. if (size in [OS_S64,OS_64]) and
  2053. ((a<low(longint)) or (a>high(longint))) then
  2054. begin
  2055. tmpreg:=getintregister(list,size);
  2056. a_load_const_reg(list,size,a,tmpreg);
  2057. a_op_reg_ref(list,op,size,tmpreg,ref);
  2058. exit;
  2059. end;
  2060. {$endif x86_64}
  2061. tmpref:=ref;
  2062. make_simple_ref(list,tmpref);
  2063. Case Op of
  2064. OP_DIV, OP_IDIV:
  2065. Begin
  2066. { should be handled specifically in the code }
  2067. { generator because of the silly register usage restraints }
  2068. internalerror(200109231);
  2069. End;
  2070. OP_MUL,OP_IMUL:
  2071. begin
  2072. if not (cs_check_overflow in current_settings.localswitches) then
  2073. op:=OP_IMUL;
  2074. { can't multiply a memory location directly with a constant }
  2075. if op = OP_IMUL then
  2076. inherited a_op_const_ref(list,op,size,a,tmpref)
  2077. else
  2078. { OP_MUL should be handled specifically in the code }
  2079. { generator because of the silly register usage restraints }
  2080. internalerror(200109232);
  2081. end;
  2082. OP_ADD, OP_SUB:
  2083. if not(cs_check_overflow in current_settings.localswitches) and
  2084. (a = 1) and
  2085. UseIncDec then
  2086. begin
  2087. if op = OP_ADD then
  2088. list.concat(taicpu.op_ref(A_INC,TCgSize2OpSize[size],tmpref))
  2089. else
  2090. list.concat(taicpu.op_ref(A_DEC,TCgSize2OpSize[size],tmpref))
  2091. end
  2092. else
  2093. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  2094. OP_AND,OP_OR:
  2095. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  2096. OP_XOR:
  2097. if (aword(a)=high(aword)) then
  2098. list.concat(taicpu.op_ref(A_NOT,TCgSize2OpSize[size],tmpref))
  2099. else
  2100. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  2101. OP_SHL,OP_SHR,OP_SAR,OP_ROL,OP_ROR:
  2102. begin
  2103. {$if defined(x86_64)}
  2104. if (a and 63) <> 0 Then
  2105. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 63,tmpref));
  2106. if (a shr 6) <> 0 Then
  2107. internalerror(2013111003);
  2108. {$elseif defined(i386)}
  2109. if (a and 31) <> 0 Then
  2110. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,tmpref));
  2111. if (a shr 5) <> 0 Then
  2112. internalerror(2013111002);
  2113. {$elseif defined(i8086)}
  2114. if (a shr 5) <> 0 Then
  2115. internalerror(2013111001);
  2116. a := a and 31;
  2117. if a <> 0 Then
  2118. begin
  2119. if (current_settings.cputype < cpu_186) and (a <> 1) then
  2120. begin
  2121. getcpuregister(list,NR_CL);
  2122. a_load_const_reg(list,OS_8,a,NR_CL);
  2123. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],NR_CL,tmpref));
  2124. ungetcpuregister(list,NR_CL);
  2125. end
  2126. else
  2127. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  2128. end;
  2129. {$endif}
  2130. end
  2131. else internalerror(68992);
  2132. end;
  2133. end;
  2134. procedure tcgx86.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  2135. const
  2136. {$if defined(cpu64bitalu)}
  2137. REGCX=NR_RCX;
  2138. REGCX_Size = OS_64;
  2139. {$elseif defined(cpu32bitalu)}
  2140. REGCX=NR_ECX;
  2141. REGCX_Size = OS_32;
  2142. {$elseif defined(cpu16bitalu)}
  2143. REGCX=NR_CX;
  2144. REGCX_Size = OS_16;
  2145. {$endif}
  2146. var
  2147. dstsize: topsize;
  2148. instr:Taicpu;
  2149. begin
  2150. if not(Op in [OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR]) then
  2151. check_register_size(size,src);
  2152. check_register_size(size,dst);
  2153. dstsize := tcgsize2opsize[size];
  2154. if (op=OP_MUL) and not (cs_check_overflow in current_settings.localswitches) then
  2155. op:=OP_IMUL;
  2156. case op of
  2157. OP_NEG,OP_NOT:
  2158. begin
  2159. if src<>dst then
  2160. a_load_reg_reg(list,size,size,src,dst);
  2161. list.concat(taicpu.op_reg(TOpCG2AsmOp[op],dstsize,dst));
  2162. end;
  2163. OP_MUL,OP_DIV,OP_IDIV:
  2164. { special stuff, needs separate handling inside code }
  2165. { generator }
  2166. internalerror(200109233);
  2167. OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR:
  2168. begin
  2169. { Use ecx to load the value, that allows better coalescing }
  2170. getcpuregister(list,REGCX);
  2171. a_load_reg_reg(list,reg_cgsize(src),REGCX_Size,src,REGCX);
  2172. list.concat(taicpu.op_reg_reg(Topcg2asmop[op],tcgsize2opsize[size],NR_CL,dst));
  2173. ungetcpuregister(list,REGCX);
  2174. end;
  2175. else
  2176. begin
  2177. if reg2opsize(src) <> dstsize then
  2178. internalerror(200109226);
  2179. instr:=taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,src,dst);
  2180. list.concat(instr);
  2181. end;
  2182. end;
  2183. end;
  2184. procedure tcgx86.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  2185. var
  2186. tmpref : treference;
  2187. begin
  2188. tmpref:=ref;
  2189. make_simple_ref(list,tmpref);
  2190. check_register_size(size,reg);
  2191. if (op=OP_MUL) and not (cs_check_overflow in current_settings.localswitches) then
  2192. op:=OP_IMUL;
  2193. case op of
  2194. OP_NEG,OP_NOT,OP_IMUL:
  2195. begin
  2196. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  2197. end;
  2198. OP_MUL,OP_DIV,OP_IDIV:
  2199. { special stuff, needs separate handling inside code }
  2200. { generator }
  2201. internalerror(200109239);
  2202. else
  2203. begin
  2204. reg := makeregsize(list,reg,size);
  2205. list.concat(taicpu.op_ref_reg(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref,reg));
  2206. end;
  2207. end;
  2208. end;
  2209. procedure tcgx86.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  2210. const
  2211. {$if defined(cpu64bitalu)}
  2212. REGCX=NR_RCX;
  2213. REGCX_Size = OS_64;
  2214. {$elseif defined(cpu32bitalu)}
  2215. REGCX=NR_ECX;
  2216. REGCX_Size = OS_32;
  2217. {$elseif defined(cpu16bitalu)}
  2218. REGCX=NR_CX;
  2219. REGCX_Size = OS_16;
  2220. {$endif}
  2221. var
  2222. tmpref : treference;
  2223. begin
  2224. tmpref:=ref;
  2225. make_simple_ref(list,tmpref);
  2226. { we don't check the register size for some operations, for the following reasons:
  2227. NEG,NOT:
  2228. reg isn't used in these operations (they are unary and use only ref)
  2229. SHR,SHL,SAR,ROL,ROR:
  2230. We allow the register size to differ from the destination size.
  2231. This allows generating better code when performing, for example, a
  2232. shift/rotate in place (x:=x shl y) of a byte variable. In this case,
  2233. we allow the shift count (y) to be located in a 32-bit register,
  2234. even though x is a byte. This:
  2235. - reduces register pressure on i386 (because only EAX,EBX,ECX and
  2236. EDX have 8-bit subregisters)
  2237. - avoids partial register writes, which can cause various
  2238. performance issues on modern out-of-order execution x86 CPUs }
  2239. if not (op in [OP_NEG,OP_NOT,OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR]) then
  2240. check_register_size(size,reg);
  2241. if (op=OP_MUL) and not (cs_check_overflow in current_settings.localswitches) then
  2242. op:=OP_IMUL;
  2243. case op of
  2244. OP_NEG,OP_NOT:
  2245. begin
  2246. if reg<>NR_NO then
  2247. internalerror(200109237);
  2248. list.concat(taicpu.op_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref));
  2249. end;
  2250. OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR:
  2251. begin
  2252. { Use ecx to load the value, that allows better coalescing }
  2253. getcpuregister(list,REGCX);
  2254. a_load_reg_reg(list,reg_cgsize(reg),REGCX_Size,reg,REGCX);
  2255. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],NR_CL,tmpref));
  2256. ungetcpuregister(list,REGCX);
  2257. end;
  2258. OP_IMUL:
  2259. begin
  2260. { this one needs a load/imul/store, which is the default }
  2261. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  2262. end;
  2263. OP_MUL,OP_DIV,OP_IDIV:
  2264. { special stuff, needs separate handling inside code }
  2265. { generator }
  2266. internalerror(200109238);
  2267. else
  2268. begin
  2269. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],reg,tmpref));
  2270. end;
  2271. end;
  2272. end;
  2273. procedure tcgx86.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: TCGSize; src, dst: TRegister);
  2274. var
  2275. tmpreg: tregister;
  2276. opsize: topsize;
  2277. l : TAsmLabel;
  2278. begin
  2279. { no bsf/bsr for byte }
  2280. if srcsize in [OS_8,OS_S8] then
  2281. begin
  2282. tmpreg:=getintregister(list,OS_INT);
  2283. a_load_reg_reg(list,srcsize,OS_INT,src,tmpreg);
  2284. src:=tmpreg;
  2285. srcsize:=OS_INT;
  2286. end;
  2287. { source and destination register must have the same size }
  2288. if tcgsize2size[srcsize]<>tcgsize2size[dstsize] then
  2289. tmpreg:=getintregister(list,srcsize)
  2290. else
  2291. tmpreg:=dst;
  2292. opsize:=tcgsize2opsize[srcsize];
  2293. if not reverse then
  2294. list.concat(taicpu.op_reg_reg(A_BSF,opsize,src,tmpreg))
  2295. else
  2296. list.concat(taicpu.op_reg_reg(A_BSR,opsize,src,tmpreg));
  2297. current_asmdata.getjumplabel(l);
  2298. a_jmp_cond(list,OC_NE,l);
  2299. list.concat(taicpu.op_const_reg(A_MOV,opsize,$ff,tmpreg));
  2300. a_label(list,l);
  2301. if tmpreg<>dst then
  2302. a_load_reg_reg(list,srcsize,dstsize,tmpreg,dst);
  2303. end;
  2304. {*************** compare instructructions ****************}
  2305. procedure tcgx86.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  2306. l : tasmlabel);
  2307. {$ifdef x86_64}
  2308. var
  2309. tmpreg : tregister;
  2310. {$endif x86_64}
  2311. begin
  2312. {$ifdef x86_64}
  2313. { x86_64 only supports signed 32 bits constants directly }
  2314. if (size in [OS_S64,OS_64]) and
  2315. ((a<low(longint)) or (a>high(longint))) then
  2316. begin
  2317. tmpreg:=getintregister(list,size);
  2318. a_load_const_reg(list,size,a,tmpreg);
  2319. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  2320. exit;
  2321. end;
  2322. {$endif x86_64}
  2323. if (a = 0) then
  2324. list.concat(taicpu.op_reg_reg(A_TEST,tcgsize2opsize[size],reg,reg))
  2325. else
  2326. list.concat(taicpu.op_const_reg(A_CMP,tcgsize2opsize[size],a,reg));
  2327. a_jmp_cond(list,cmp_op,l);
  2328. end;
  2329. procedure tcgx86.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  2330. l : tasmlabel);
  2331. var
  2332. {$ifdef x86_64}
  2333. tmpreg : tregister;
  2334. {$endif x86_64}
  2335. tmpref : treference;
  2336. begin
  2337. tmpref:=ref;
  2338. make_simple_ref(list,tmpref);
  2339. {$ifdef x86_64}
  2340. { x86_64 only supports signed 32 bits constants directly }
  2341. if (size in [OS_S64,OS_64]) and
  2342. ((a<low(longint)) or (a>high(longint))) then
  2343. begin
  2344. tmpreg:=getintregister(list,size);
  2345. a_load_const_reg(list,size,a,tmpreg);
  2346. a_cmp_reg_ref_label(list,size,cmp_op,tmpreg,tmpref,l);
  2347. exit;
  2348. end;
  2349. {$endif x86_64}
  2350. list.concat(taicpu.op_const_ref(A_CMP,TCgSize2OpSize[size],a,tmpref));
  2351. a_jmp_cond(list,cmp_op,l);
  2352. end;
  2353. procedure tcgx86.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;
  2354. reg1,reg2 : tregister;l : tasmlabel);
  2355. begin
  2356. check_register_size(size,reg1);
  2357. check_register_size(size,reg2);
  2358. list.concat(taicpu.op_reg_reg(A_CMP,TCgSize2OpSize[size],reg1,reg2));
  2359. a_jmp_cond(list,cmp_op,l);
  2360. end;
  2361. procedure tcgx86.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister;l : tasmlabel);
  2362. var
  2363. tmpref : treference;
  2364. begin
  2365. tmpref:=ref;
  2366. make_simple_ref(list,tmpref);
  2367. check_register_size(size,reg);
  2368. list.concat(taicpu.op_ref_reg(A_CMP,TCgSize2OpSize[size],tmpref,reg));
  2369. a_jmp_cond(list,cmp_op,l);
  2370. end;
  2371. procedure tcgx86.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister;const ref: treference; l : tasmlabel);
  2372. var
  2373. tmpref : treference;
  2374. begin
  2375. tmpref:=ref;
  2376. make_simple_ref(list,tmpref);
  2377. check_register_size(size,reg);
  2378. list.concat(taicpu.op_reg_ref(A_CMP,TCgSize2OpSize[size],reg,tmpref));
  2379. a_jmp_cond(list,cmp_op,l);
  2380. end;
  2381. procedure tcgx86.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  2382. var
  2383. ai : taicpu;
  2384. begin
  2385. if cond=OC_None then
  2386. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  2387. else
  2388. begin
  2389. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  2390. ai.SetCondition(TOpCmp2AsmCond[cond]);
  2391. end;
  2392. ai.is_jmp:=true;
  2393. list.concat(ai);
  2394. end;
  2395. procedure tcgx86.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  2396. var
  2397. ai : taicpu;
  2398. hl : tasmlabel;
  2399. f2 : tresflags;
  2400. begin
  2401. hl:=nil;
  2402. f2:=f;
  2403. case f of
  2404. F_FNE:
  2405. begin
  2406. ai:=Taicpu.op_sym(A_Jcc,S_NO,l);
  2407. ai.SetCondition(C_P);
  2408. ai.is_jmp:=true;
  2409. list.concat(ai);
  2410. f2:=F_NE;
  2411. end;
  2412. F_FE,F_FA,F_FAE,F_FB,F_FBE:
  2413. begin
  2414. { JP before JA/JAE is redundant, but it must be generated here
  2415. and left for peephole optimizer to remove. }
  2416. current_asmdata.getjumplabel(hl);
  2417. ai:=Taicpu.op_sym(A_Jcc,S_NO,hl);
  2418. ai.SetCondition(C_P);
  2419. ai.is_jmp:=true;
  2420. list.concat(ai);
  2421. f2:=FPUFlags2Flags[f];
  2422. end;
  2423. end;
  2424. ai := Taicpu.op_sym(A_Jcc,S_NO,l);
  2425. ai.SetCondition(flags_to_cond(f2));
  2426. ai.is_jmp := true;
  2427. list.concat(ai);
  2428. if assigned(hl) then
  2429. a_label(list,hl);
  2430. end;
  2431. procedure tcgx86.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  2432. var
  2433. ai : taicpu;
  2434. f2 : tresflags;
  2435. hreg,hreg2 : tregister;
  2436. op: tasmop;
  2437. begin
  2438. hreg2:=NR_NO;
  2439. op:=A_AND;
  2440. f2:=f;
  2441. case f of
  2442. F_FE,F_FNE,F_FB,F_FBE:
  2443. begin
  2444. hreg2:=getintregister(list,OS_8);
  2445. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg2);
  2446. if (f=F_FNE) then { F_FNE means "PF or (not ZF)" }
  2447. begin
  2448. ai.setcondition(C_P);
  2449. op:=A_OR;
  2450. end
  2451. else
  2452. ai.setcondition(C_NP);
  2453. list.concat(ai);
  2454. f2:=FPUFlags2Flags[f];
  2455. end;
  2456. F_FA,F_FAE: { These do not need PF check }
  2457. f2:=FPUFlags2Flags[f];
  2458. end;
  2459. hreg:=makeregsize(list,reg,OS_8);
  2460. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg);
  2461. ai.setcondition(flags_to_cond(f2));
  2462. list.concat(ai);
  2463. if (hreg2<>NR_NO) then
  2464. list.concat(taicpu.op_reg_reg(op,S_B,hreg2,hreg));
  2465. if reg<>hreg then
  2466. a_load_reg_reg(list,OS_8,size,hreg,reg);
  2467. end;
  2468. procedure tcgx86.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference);
  2469. var
  2470. ai : taicpu;
  2471. tmpref : treference;
  2472. f2 : tresflags;
  2473. begin
  2474. f2:=f;
  2475. case f of
  2476. F_FE,F_FNE,F_FB,F_FBE:
  2477. begin
  2478. inherited g_flags2ref(list,size,f,ref);
  2479. exit;
  2480. end;
  2481. F_FA,F_FAE:
  2482. f2:=FPUFlags2Flags[f];
  2483. end;
  2484. tmpref:=ref;
  2485. make_simple_ref(list,tmpref);
  2486. if not(size in [OS_8,OS_S8]) then
  2487. a_load_const_ref(list,size,0,tmpref);
  2488. ai:=Taicpu.op_ref(A_SETcc,S_B,tmpref);
  2489. ai.setcondition(flags_to_cond(f2));
  2490. list.concat(ai);
  2491. {$ifndef cpu64bitalu}
  2492. if size in [OS_S64,OS_64] then
  2493. begin
  2494. inc(tmpref.offset,4);
  2495. a_load_const_ref(list,OS_32,0,tmpref);
  2496. end;
  2497. {$endif cpu64bitalu}
  2498. end;
  2499. { ************* concatcopy ************ }
  2500. procedure Tcgx86.g_concatcopy(list:TAsmList;const source,dest:Treference;len:tcgint);
  2501. const
  2502. {$if defined(cpu64bitalu)}
  2503. REGCX=NR_RCX;
  2504. REGSI=NR_RSI;
  2505. REGDI=NR_RDI;
  2506. copy_len_sizes = [1, 2, 4, 8];
  2507. push_segment_size = S_L;
  2508. {$elseif defined(cpu32bitalu)}
  2509. REGCX=NR_ECX;
  2510. REGSI=NR_ESI;
  2511. REGDI=NR_EDI;
  2512. copy_len_sizes = [1, 2, 4];
  2513. push_segment_size = S_L;
  2514. {$elseif defined(cpu16bitalu)}
  2515. REGCX=NR_CX;
  2516. REGSI=NR_SI;
  2517. REGDI=NR_DI;
  2518. copy_len_sizes = [1, 2, 4]; { 4 is included here, because it's still more
  2519. efficient to use copy_move instead of copy_string for copying 4 bytes }
  2520. push_segment_size = S_W;
  2521. {$endif}
  2522. type copymode=(copy_move,copy_mmx,copy_string,copy_mm,copy_avx);
  2523. var srcref,dstref:Treference;
  2524. r,r0,r1,r2,r3:Tregister;
  2525. helpsize:tcgint;
  2526. copysize:byte;
  2527. cgsize:Tcgsize;
  2528. cm:copymode;
  2529. saved_ds,saved_es: Boolean;
  2530. begin
  2531. srcref:=source;
  2532. dstref:=dest;
  2533. {$ifndef i8086}
  2534. make_simple_ref(list,srcref);
  2535. make_simple_ref(list,dstref);
  2536. {$endif not i8086}
  2537. cm:=copy_move;
  2538. helpsize:=3*sizeof(aword);
  2539. if cs_opt_size in current_settings.optimizerswitches then
  2540. helpsize:=2*sizeof(aword);
  2541. {$ifndef i8086}
  2542. { avx helps only to reduce size, using it in general does at least not help on
  2543. an i7-4770 (FK) }
  2544. if (CPUX86_HAS_AVXUNIT in cpu_capabilities[current_settings.cputype]) and
  2545. // (cs_opt_size in current_settings.optimizerswitches) and
  2546. ({$ifdef i386}(len=8) or{$endif i386}(len=16) or (len=24) or (len=32) { or (len=40) or (len=48)}) then
  2547. cm:=copy_avx
  2548. else
  2549. {$ifdef dummy}
  2550. { I'am not sure what CPUs would benefit from using sse instructions for moves (FK) }
  2551. if
  2552. {$ifdef x86_64}
  2553. ((current_settings.fputype>=fpu_sse64)
  2554. {$else x86_64}
  2555. ((current_settings.fputype>=fpu_sse)
  2556. {$endif x86_64}
  2557. or (CPUX86_HAS_SSE2 in cpu_capabilities[current_settings.cputype])) and
  2558. ((len=8) or (len=16) or (len=24) or (len=32) or (len=40) or (len=48)) then
  2559. cm:=copy_mm
  2560. else
  2561. {$endif dummy}
  2562. {$endif i8086}
  2563. if (cs_mmx in current_settings.localswitches) and
  2564. not(pi_uses_fpu in current_procinfo.flags) and
  2565. ((len=8) or (len=16) or (len=24) or (len=32)) then
  2566. cm:=copy_mmx;
  2567. if (len>helpsize) then
  2568. cm:=copy_string;
  2569. if (cs_opt_size in current_settings.optimizerswitches) and
  2570. not((len<=16) and (cm in [copy_mmx,copy_mm,copy_avx])) and
  2571. not(len in copy_len_sizes) then
  2572. cm:=copy_string;
  2573. {$ifndef i8086}
  2574. if (srcref.segment<>NR_NO) or
  2575. (dstref.segment<>NR_NO) then
  2576. cm:=copy_string;
  2577. {$endif not i8086}
  2578. case cm of
  2579. copy_move:
  2580. begin
  2581. copysize:=sizeof(aint);
  2582. cgsize:=int_cgsize(copysize);
  2583. while len<>0 do
  2584. begin
  2585. if len<2 then
  2586. begin
  2587. copysize:=1;
  2588. cgsize:=OS_8;
  2589. end
  2590. else if len<4 then
  2591. begin
  2592. copysize:=2;
  2593. cgsize:=OS_16;
  2594. end
  2595. {$if defined(cpu32bitalu) or defined(cpu64bitalu)}
  2596. else if len<8 then
  2597. begin
  2598. copysize:=4;
  2599. cgsize:=OS_32;
  2600. end
  2601. {$endif cpu32bitalu or cpu64bitalu}
  2602. {$ifdef cpu64bitalu}
  2603. else if len<16 then
  2604. begin
  2605. copysize:=8;
  2606. cgsize:=OS_64;
  2607. end
  2608. {$endif}
  2609. ;
  2610. dec(len,copysize);
  2611. r:=getintregister(list,cgsize);
  2612. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  2613. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  2614. inc(srcref.offset,copysize);
  2615. inc(dstref.offset,copysize);
  2616. end;
  2617. end;
  2618. copy_mmx:
  2619. begin
  2620. r0:=getmmxregister(list);
  2621. r1:=NR_NO;
  2622. r2:=NR_NO;
  2623. r3:=NR_NO;
  2624. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r0,nil);
  2625. if len>=16 then
  2626. begin
  2627. inc(srcref.offset,8);
  2628. r1:=getmmxregister(list);
  2629. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r1,nil);
  2630. end;
  2631. if len>=24 then
  2632. begin
  2633. inc(srcref.offset,8);
  2634. r2:=getmmxregister(list);
  2635. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r2,nil);
  2636. end;
  2637. if len>=32 then
  2638. begin
  2639. inc(srcref.offset,8);
  2640. r3:=getmmxregister(list);
  2641. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  2642. end;
  2643. a_loadmm_reg_ref(list,OS_M64,OS_M64,r0,dstref,nil);
  2644. if len>=16 then
  2645. begin
  2646. inc(dstref.offset,8);
  2647. a_loadmm_reg_ref(list,OS_M64,OS_M64,r1,dstref,nil);
  2648. end;
  2649. if len>=24 then
  2650. begin
  2651. inc(dstref.offset,8);
  2652. a_loadmm_reg_ref(list,OS_M64,OS_M64,r2,dstref,nil);
  2653. end;
  2654. if len>=32 then
  2655. begin
  2656. inc(dstref.offset,8);
  2657. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  2658. end;
  2659. end;
  2660. copy_mm:
  2661. begin
  2662. r0:=NR_NO;
  2663. r1:=NR_NO;
  2664. r2:=NR_NO;
  2665. r3:=NR_NO;
  2666. if len>=16 then
  2667. begin
  2668. r0:=getmmregister(list,OS_M128);
  2669. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r0,nil);
  2670. inc(srcref.offset,16);
  2671. end;
  2672. if len>=32 then
  2673. begin
  2674. r1:=getmmregister(list,OS_M128);
  2675. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r1,nil);
  2676. inc(srcref.offset,16);
  2677. end;
  2678. if len>=48 then
  2679. begin
  2680. r2:=getmmregister(list,OS_M128);
  2681. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r2,nil);
  2682. inc(srcref.offset,16);
  2683. end;
  2684. if (len=8) or (len=24) or (len=40) then
  2685. begin
  2686. r3:=getmmregister(list,OS_M64);
  2687. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  2688. end;
  2689. if len>=16 then
  2690. begin
  2691. a_loadmm_reg_ref(list,OS_M128,OS_M128,r0,dstref,nil);
  2692. inc(dstref.offset,16);
  2693. end;
  2694. if len>=32 then
  2695. begin
  2696. a_loadmm_reg_ref(list,OS_M128,OS_M128,r1,dstref,nil);
  2697. inc(dstref.offset,16);
  2698. end;
  2699. if len>=48 then
  2700. begin
  2701. a_loadmm_reg_ref(list,OS_M128,OS_M128,r2,dstref,nil);
  2702. inc(dstref.offset,16);
  2703. end;
  2704. if (len=8) or (len=24) or (len=40) then
  2705. begin
  2706. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  2707. end;
  2708. end;
  2709. copy_avx:
  2710. begin
  2711. r0:=NR_NO;
  2712. r1:=NR_NO;
  2713. r2:=NR_NO;
  2714. r3:=NR_NO;
  2715. if len>=16 then
  2716. begin
  2717. r0:=getmmregister(list,OS_M128);
  2718. { we want to force the use of vmovups, so do not use a_loadmm_ref_reg }
  2719. list.concat(taicpu.op_ref_reg(A_VMOVUPS,S_NO,srcref,r0));
  2720. inc(srcref.offset,16);
  2721. end;
  2722. if len>=32 then
  2723. begin
  2724. r1:=getmmregister(list,OS_M128);
  2725. list.concat(taicpu.op_ref_reg(A_VMOVUPS,S_NO,srcref,r1));
  2726. inc(srcref.offset,16);
  2727. end;
  2728. if len>=48 then
  2729. begin
  2730. r2:=getmmregister(list,OS_M128);
  2731. list.concat(taicpu.op_ref_reg(A_VMOVUPS,S_NO,srcref,r2));
  2732. inc(srcref.offset,16);
  2733. end;
  2734. if (len=8) or (len=24) or (len=40) then
  2735. begin
  2736. r3:=getmmregister(list,OS_M64);
  2737. list.concat(taicpu.op_ref_reg(A_VMOVSD,S_NO,srcref,r3));
  2738. end;
  2739. if len>=16 then
  2740. begin
  2741. list.concat(taicpu.op_reg_ref(A_VMOVUPS,S_NO,r0,dstref));
  2742. inc(dstref.offset,16);
  2743. end;
  2744. if len>=32 then
  2745. begin
  2746. list.concat(taicpu.op_reg_ref(A_VMOVUPS,S_NO,r1,dstref));
  2747. inc(dstref.offset,16);
  2748. end;
  2749. if len>=48 then
  2750. begin
  2751. list.concat(taicpu.op_reg_ref(A_VMOVUPS,S_NO,r2,dstref));
  2752. inc(dstref.offset,16);
  2753. end;
  2754. if (len=8) or (len=24) or (len=40) then
  2755. begin
  2756. list.concat(taicpu.op_reg_ref(A_VMOVSD,S_NO,r3,dstref));
  2757. end;
  2758. end
  2759. else {copy_string, should be a good fallback in case of unhandled}
  2760. begin
  2761. getcpuregister(list,REGDI);
  2762. if (dest.segment=NR_NO) and
  2763. (segment_regs_equal(NR_SS,NR_DS) or ((dest.base<>NR_BP) and (dest.base<>NR_SP))) then
  2764. begin
  2765. a_loadaddr_ref_reg(list,dstref,REGDI);
  2766. saved_es:=false;
  2767. {$ifdef volatile_es}
  2768. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_DS));
  2769. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2770. {$endif volatile_es}
  2771. end
  2772. else
  2773. begin
  2774. dstref.segment:=NR_NO;
  2775. a_loadaddr_ref_reg(list,dstref,REGDI);
  2776. {$ifdef volatile_es}
  2777. saved_es:=false;
  2778. {$else volatile_es}
  2779. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_ES));
  2780. saved_es:=true;
  2781. {$endif volatile_es}
  2782. if dest.segment<>NR_NO then
  2783. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,dest.segment))
  2784. else if (dest.base=NR_BP) or (dest.base=NR_SP) then
  2785. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_SS))
  2786. else
  2787. internalerror(2014040401);
  2788. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2789. end;
  2790. getcpuregister(list,REGSI);
  2791. {$ifdef i8086}
  2792. { at this point, si and di are allocated, so no register is available as index =>
  2793. compiler will hang/ie during spilling, so avoid that srcref has base and index, see also tests/tbs/tb0637.pp }
  2794. if (srcref.base<>NR_NO) and (srcref.index<>NR_NO) then
  2795. begin
  2796. r:=getaddressregister(list);
  2797. a_op_reg_reg_reg(list,OP_ADD,OS_ADDR,srcref.base,srcref.index,r);
  2798. srcref.base:=r;
  2799. srcref.index:=NR_NO;
  2800. end;
  2801. {$endif i8086}
  2802. if ((source.segment=NR_NO) and (segment_regs_equal(NR_SS,NR_DS) or ((source.base<>NR_BP) and (source.base<>NR_SP)))) or
  2803. (is_segment_reg(source.segment) and segment_regs_equal(source.segment,NR_DS)) then
  2804. begin
  2805. srcref.segment:=NR_NO;
  2806. a_loadaddr_ref_reg(list,srcref,REGSI);
  2807. saved_ds:=false;
  2808. end
  2809. else
  2810. begin
  2811. srcref.segment:=NR_NO;
  2812. a_loadaddr_ref_reg(list,srcref,REGSI);
  2813. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_DS));
  2814. saved_ds:=true;
  2815. if source.segment<>NR_NO then
  2816. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,source.segment))
  2817. else if (source.base=NR_BP) or (source.base=NR_SP) then
  2818. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_SS))
  2819. else
  2820. internalerror(2014040402);
  2821. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_DS));
  2822. end;
  2823. getcpuregister(list,REGCX);
  2824. if ts_cld in current_settings.targetswitches then
  2825. list.concat(Taicpu.op_none(A_CLD,S_NO));
  2826. if (cs_opt_size in current_settings.optimizerswitches) and
  2827. (len>sizeof(aint)+(sizeof(aint) div 2)) then
  2828. begin
  2829. a_load_const_reg(list,OS_INT,len,REGCX);
  2830. list.concat(Taicpu.op_none(A_REP,S_NO));
  2831. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  2832. end
  2833. else
  2834. begin
  2835. helpsize:=len div sizeof(aint);
  2836. len:=len mod sizeof(aint);
  2837. if helpsize>1 then
  2838. begin
  2839. a_load_const_reg(list,OS_INT,helpsize,REGCX);
  2840. list.concat(Taicpu.op_none(A_REP,S_NO));
  2841. end;
  2842. if helpsize>0 then
  2843. begin
  2844. {$if defined(cpu64bitalu)}
  2845. list.concat(Taicpu.op_none(A_MOVSQ,S_NO))
  2846. {$elseif defined(cpu32bitalu)}
  2847. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  2848. {$elseif defined(cpu16bitalu)}
  2849. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  2850. {$endif}
  2851. end;
  2852. if len>=4 then
  2853. begin
  2854. dec(len,4);
  2855. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  2856. end;
  2857. if len>=2 then
  2858. begin
  2859. dec(len,2);
  2860. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  2861. end;
  2862. if len=1 then
  2863. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  2864. end;
  2865. ungetcpuregister(list,REGCX);
  2866. ungetcpuregister(list,REGSI);
  2867. ungetcpuregister(list,REGDI);
  2868. if saved_ds then
  2869. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_DS));
  2870. if saved_es then
  2871. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2872. end;
  2873. end;
  2874. end;
  2875. {****************************************************************************
  2876. Entry/Exit Code Helpers
  2877. ****************************************************************************}
  2878. procedure tcgx86.g_profilecode(list : TAsmList);
  2879. var
  2880. pl : tasmlabel;
  2881. mcountprefix : String[4];
  2882. begin
  2883. case target_info.system of
  2884. {$ifndef NOTARGETWIN}
  2885. system_i386_win32,
  2886. {$endif}
  2887. system_i386_freebsd,
  2888. system_i386_netbsd,
  2889. // system_i386_openbsd,
  2890. system_i386_wdosx :
  2891. begin
  2892. Case target_info.system Of
  2893. system_i386_freebsd : mcountprefix:='.';
  2894. system_i386_netbsd : mcountprefix:='__';
  2895. // system_i386_openbsd : mcountprefix:='.';
  2896. else
  2897. mcountPrefix:='';
  2898. end;
  2899. current_asmdata.getaddrlabel(pl);
  2900. new_section(list,sec_data,lower(current_procinfo.procdef.mangledname),sizeof(pint));
  2901. list.concat(Tai_label.Create(pl));
  2902. list.concat(Tai_const.Create_32bit(0));
  2903. new_section(list,sec_code,lower(current_procinfo.procdef.mangledname),0);
  2904. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  2905. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,NR_EDX));
  2906. a_call_name(list,target_info.Cprefix+mcountprefix+'mcount',false);
  2907. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDX));
  2908. end;
  2909. system_i386_linux:
  2910. a_call_name(list,target_info.Cprefix+'mcount',false);
  2911. system_i386_go32v2,system_i386_watcom:
  2912. begin
  2913. a_call_name(list,'MCOUNT',false);
  2914. end;
  2915. system_x86_64_linux,
  2916. system_x86_64_darwin,
  2917. system_x86_64_iphonesim:
  2918. begin
  2919. a_call_name(list,'mcount',false);
  2920. end;
  2921. end;
  2922. end;
  2923. procedure tcgx86.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  2924. procedure decrease_sp(a : tcgint);
  2925. var
  2926. href : treference;
  2927. begin
  2928. reference_reset_base(href,NR_STACK_POINTER_REG,-a,0,[]);
  2929. { normally, lea is a better choice than a sub to adjust the stack pointer }
  2930. list.concat(Taicpu.op_ref_reg(A_LEA,TCGSize2OpSize[OS_ADDR],href,NR_STACK_POINTER_REG));
  2931. end;
  2932. {$ifdef x86}
  2933. {$ifndef NOTARGETWIN}
  2934. var
  2935. href : treference;
  2936. i : integer;
  2937. again : tasmlabel;
  2938. {$endif NOTARGETWIN}
  2939. {$endif x86}
  2940. begin
  2941. if localsize>0 then
  2942. begin
  2943. {$ifdef i386}
  2944. {$ifndef NOTARGETWIN}
  2945. { windows guards only a few pages for stack growing,
  2946. so we have to access every page first }
  2947. if (target_info.system in [system_i386_win32,system_i386_wince]) and
  2948. (localsize>=winstackpagesize) then
  2949. begin
  2950. if localsize div winstackpagesize<=5 then
  2951. begin
  2952. decrease_sp(localsize-4);
  2953. for i:=1 to localsize div winstackpagesize do
  2954. begin
  2955. reference_reset_base(href,NR_ESP,localsize-i*winstackpagesize,4,[]);
  2956. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2957. end;
  2958. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  2959. end
  2960. else
  2961. begin
  2962. current_asmdata.getjumplabel(again);
  2963. { Using a_reg_alloc instead of getcpuregister, so this procedure
  2964. does not change "used_in_proc" state of EDI and therefore can be
  2965. called after saving registers with "push" instruction
  2966. without creating an unbalanced "pop edi" in epilogue }
  2967. a_reg_alloc(list,NR_EDI);
  2968. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EDI));
  2969. list.concat(Taicpu.op_const_reg(A_MOV,S_L,localsize div winstackpagesize,NR_EDI));
  2970. a_label(list,again);
  2971. decrease_sp(winstackpagesize-4);
  2972. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  2973. if UseIncDec then
  2974. list.concat(Taicpu.op_reg(A_DEC,S_L,NR_EDI))
  2975. else
  2976. list.concat(Taicpu.op_const_reg(A_SUB,S_L,1,NR_EDI));
  2977. a_jmp_cond(list,OC_NE,again);
  2978. decrease_sp(localsize mod winstackpagesize-4);
  2979. reference_reset_base(href,NR_ESP,localsize-4,4,[]);
  2980. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,href,NR_EDI));
  2981. a_reg_dealloc(list,NR_EDI);
  2982. end
  2983. end
  2984. else
  2985. {$endif NOTARGETWIN}
  2986. {$endif i386}
  2987. {$ifdef x86_64}
  2988. {$ifndef NOTARGETWIN}
  2989. { windows guards only a few pages for stack growing,
  2990. so we have to access every page first }
  2991. if (target_info.system=system_x86_64_win64) and
  2992. (localsize>=winstackpagesize) then
  2993. begin
  2994. if localsize div winstackpagesize<=5 then
  2995. begin
  2996. decrease_sp(localsize);
  2997. for i:=1 to localsize div winstackpagesize do
  2998. begin
  2999. reference_reset_base(href,NR_RSP,localsize-i*winstackpagesize+4,4,[]);
  3000. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  3001. end;
  3002. reference_reset_base(href,NR_RSP,0,4,[]);
  3003. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  3004. end
  3005. else
  3006. begin
  3007. current_asmdata.getjumplabel(again);
  3008. getcpuregister(list,NR_R10);
  3009. list.concat(Taicpu.op_const_reg(A_MOV,S_Q,localsize div winstackpagesize,NR_R10));
  3010. a_label(list,again);
  3011. decrease_sp(winstackpagesize);
  3012. reference_reset_base(href,NR_RSP,0,4,[]);
  3013. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  3014. if UseIncDec then
  3015. list.concat(Taicpu.op_reg(A_DEC,S_Q,NR_R10))
  3016. else
  3017. list.concat(Taicpu.op_const_reg(A_SUB,S_Q,1,NR_R10));
  3018. a_jmp_cond(list,OC_NE,again);
  3019. decrease_sp(localsize mod winstackpagesize);
  3020. ungetcpuregister(list,NR_R10);
  3021. end
  3022. end
  3023. else
  3024. {$endif NOTARGETWIN}
  3025. {$endif x86_64}
  3026. decrease_sp(localsize);
  3027. end;
  3028. end;
  3029. procedure tcgx86.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  3030. var
  3031. stackmisalignment: longint;
  3032. regsize: longint;
  3033. {$ifdef i8086}
  3034. dgroup: treference;
  3035. fardataseg: treference;
  3036. {$endif i8086}
  3037. procedure push_regs;
  3038. var
  3039. r: longint;
  3040. usedregs: tcpuregisterset;
  3041. begin
  3042. regsize:=0;
  3043. usedregs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption);
  3044. for r := low(saved_standard_registers) to high(saved_standard_registers) do
  3045. if saved_standard_registers[r] in usedregs then
  3046. begin
  3047. inc(regsize,sizeof(aint));
  3048. list.concat(Taicpu.Op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE)));
  3049. end;
  3050. end;
  3051. begin
  3052. {$ifdef i8086}
  3053. { Win16 callback/exported proc prologue support.
  3054. Since callbacks can be called from different modules, DS on entry may be
  3055. initialized with the data segment of a different module, so we need to
  3056. get ours. But we can't do
  3057. push ds
  3058. mov ax, dgroup
  3059. mov ds, ax
  3060. because code segments are shared between different instances of the same
  3061. module (which have different instances of the current program's data segment),
  3062. so the same 'mov ax, dgroup' instruction will be used for all instances
  3063. of the program and it will load the same segment into ax.
  3064. So, the standard win16 prologue looks like this:
  3065. mov ax, ds
  3066. nop
  3067. inc bp
  3068. push bp
  3069. mov bp, sp
  3070. push ds
  3071. mov ds, ax
  3072. By default, this does nothing, except wasting a few extra machine cycles and
  3073. destroying ax in the process. However, Windows checks the first three bytes
  3074. of every exported function and if they are 'mov ax,ds/nop', they are replaced
  3075. with nop/nop/nop. Then the MakeProcInstance api call should be used to create
  3076. a thunk that loads ds for the current program instance in ax before calling
  3077. the routine.
  3078. And now the fun part comes: somebody (Michael Geary) figured out that all this
  3079. crap was unnecessary, because in Win16 exe modules, we always have DS=SS, so we
  3080. can simply initialize DS from SS :) And then calling MakeProcInstance becomes
  3081. unnecessary. This is what "smart callbacks" (cs_win16_smartcallbacks) do. However,
  3082. this only works for exe files, not for dlls, because dlls run with DS<>SS. There's
  3083. another solution for dlls - since win16 dlls only have a single instance of their
  3084. data segment, we can initialize ds from dgroup. However, there's not a single
  3085. solution for both exe and dlls, so we don't know what to use e.g. in a unit. So,
  3086. that's why there's still an option to turn smart callbacks off and go the
  3087. MakeProcInstance way.
  3088. Additional details here: http://www.geary.com/fixds.html }
  3089. if (current_settings.x86memorymodel<>mm_huge) and
  3090. (po_exports in current_procinfo.procdef.procoptions) and
  3091. (target_info.system=system_i8086_win16) then
  3092. begin
  3093. if cs_win16_smartcallbacks in current_settings.moduleswitches then
  3094. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_SS,NR_AX))
  3095. else
  3096. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_DS,NR_AX));
  3097. list.concat(Taicpu.op_none(A_NOP));
  3098. end
  3099. { interrupt support for i8086 }
  3100. else if po_interrupt in current_procinfo.procdef.procoptions then
  3101. begin
  3102. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_AX));
  3103. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_BX));
  3104. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_CX));
  3105. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DX));
  3106. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_SI));
  3107. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DI));
  3108. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  3109. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  3110. if current_settings.x86memorymodel=mm_tiny then
  3111. begin
  3112. { in the tiny memory model, we can't use dgroup, because that
  3113. adds a relocation entry to the .exe and we can't produce a
  3114. .com file (because they don't support relactions), so instead
  3115. we initialize DS from CS. }
  3116. if cs_opt_size in current_settings.optimizerswitches then
  3117. begin
  3118. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_CS));
  3119. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_DS));
  3120. end
  3121. else
  3122. begin
  3123. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_CS,NR_AX));
  3124. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3125. end;
  3126. end
  3127. else if current_settings.x86memorymodel=mm_huge then
  3128. begin
  3129. reference_reset(fardataseg,0,[]);
  3130. fardataseg.refaddr:=addr_fardataseg;
  3131. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,fardataseg,NR_AX));
  3132. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3133. end
  3134. else
  3135. begin
  3136. reference_reset(dgroup,0,[]);
  3137. dgroup.refaddr:=addr_dgroup;
  3138. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,dgroup,NR_AX));
  3139. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3140. end;
  3141. end;
  3142. {$endif i8086}
  3143. {$ifdef i386}
  3144. { interrupt support for i386 }
  3145. if (po_interrupt in current_procinfo.procdef.procoptions) and
  3146. { this messes up stack alignment }
  3147. not(target_info.system in [system_i386_darwin,system_i386_iphonesim,system_i386_android]) then
  3148. begin
  3149. { .... also the segment registers }
  3150. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_GS));
  3151. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_FS));
  3152. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  3153. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  3154. { save the registers of an interrupt procedure }
  3155. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDI));
  3156. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ESI));
  3157. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  3158. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ECX));
  3159. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EBX));
  3160. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EAX));
  3161. end;
  3162. {$endif i386}
  3163. { save old framepointer }
  3164. if not nostackframe then
  3165. begin
  3166. { return address }
  3167. stackmisalignment := sizeof(pint);
  3168. list.concat(tai_regalloc.alloc(current_procinfo.framepointer,nil));
  3169. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  3170. begin
  3171. {$ifdef i386}
  3172. if (not paramanager.use_fixed_stack) then
  3173. push_regs;
  3174. {$endif i386}
  3175. CGmessage(cg_d_stackframe_omited);
  3176. end
  3177. else
  3178. begin
  3179. {$ifdef i8086}
  3180. if ((ts_x86_far_procs_push_odd_bp in current_settings.targetswitches) or
  3181. ((po_exports in current_procinfo.procdef.procoptions) and
  3182. (target_info.system=system_i8086_win16))) and
  3183. is_proc_far(current_procinfo.procdef) then
  3184. cg.a_op_const_reg(list,OP_ADD,OS_ADDR,1,current_procinfo.framepointer);
  3185. {$endif i8086}
  3186. { push <frame_pointer> }
  3187. inc(stackmisalignment,sizeof(pint));
  3188. include(rg[R_INTREGISTER].preserved_by_proc,RS_FRAME_POINTER_REG);
  3189. list.concat(Taicpu.op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],NR_FRAME_POINTER_REG));
  3190. { Return address and FP are both on stack }
  3191. current_asmdata.asmcfi.cfa_def_cfa_offset(list,2*sizeof(pint));
  3192. current_asmdata.asmcfi.cfa_offset(list,NR_FRAME_POINTER_REG,-(2*sizeof(pint)));
  3193. if current_procinfo.procdef.proctypeoption<>potype_exceptfilter then
  3194. list.concat(Taicpu.op_reg_reg(A_MOV,tcgsize2opsize[OS_ADDR],NR_STACK_POINTER_REG,NR_FRAME_POINTER_REG))
  3195. else
  3196. begin
  3197. push_regs;
  3198. gen_load_frame_for_exceptfilter(list);
  3199. { Need only as much stack space as necessary to do the calls.
  3200. Exception filters don't have own local vars, and temps are 'mapped'
  3201. to the parent procedure.
  3202. maxpushedparasize is already aligned at least on x86_64. }
  3203. localsize:=current_procinfo.maxpushedparasize;
  3204. end;
  3205. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_FRAME_POINTER_REG);
  3206. end;
  3207. { allocate stackframe space }
  3208. if (localsize<>0) or
  3209. ((target_info.stackalign>sizeof(pint)) and
  3210. (stackmisalignment <> 0) and
  3211. ((pi_do_call in current_procinfo.flags) or
  3212. (po_assembler in current_procinfo.procdef.procoptions))) then
  3213. begin
  3214. if target_info.stackalign>sizeof(pint) then
  3215. localsize := align(localsize+stackmisalignment,target_info.stackalign)-stackmisalignment;
  3216. g_stackpointer_alloc(list,localsize);
  3217. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  3218. current_asmdata.asmcfi.cfa_def_cfa_offset(list,localsize+sizeof(pint));
  3219. current_procinfo.final_localsize:=localsize;
  3220. end
  3221. {$ifdef i8086}
  3222. else
  3223. { on i8086 we always call g_stackpointer_alloc, even with a zero size,
  3224. because it will generate code for stack checking, if stack checking is on }
  3225. g_stackpointer_alloc(list,0)
  3226. {$endif i8086}
  3227. ;
  3228. {$ifdef i8086}
  3229. { win16 exported proc prologue follow-up (see the huge comment above for details) }
  3230. if (current_settings.x86memorymodel<>mm_huge) and
  3231. (po_exports in current_procinfo.procdef.procoptions) and
  3232. (target_info.system=system_i8086_win16) then
  3233. begin
  3234. list.concat(Taicpu.op_reg(A_PUSH,S_W,NR_DS));
  3235. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3236. end
  3237. else if (current_settings.x86memorymodel=mm_huge) and
  3238. not (po_interrupt in current_procinfo.procdef.procoptions) then
  3239. begin
  3240. list.concat(Taicpu.op_reg(A_PUSH,S_W,NR_DS));
  3241. reference_reset(fardataseg,0,[]);
  3242. fardataseg.refaddr:=addr_fardataseg;
  3243. if current_procinfo.procdef.proccalloption=pocall_register then
  3244. begin
  3245. { Use BX register if using register convention
  3246. as it is not a register used to store parameters }
  3247. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,fardataseg,NR_BX));
  3248. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_BX,NR_DS));
  3249. end
  3250. else
  3251. begin
  3252. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,fardataseg,NR_AX));
  3253. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3254. end;
  3255. end;
  3256. { SI and DI are volatile in the BP7 and FPC's pascal calling convention,
  3257. but must be preserved in Microsoft C's pascal calling convention, and
  3258. since Windows is compiled with Microsoft compilers, these registers
  3259. must be saved for exported procedures (BP7 for Win16 also does this). }
  3260. if (po_exports in current_procinfo.procdef.procoptions) and
  3261. (target_info.system=system_i8086_win16) then
  3262. begin
  3263. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_SI));
  3264. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DI));
  3265. end;
  3266. {$endif i8086}
  3267. {$ifdef i386}
  3268. if (not paramanager.use_fixed_stack) and
  3269. (current_procinfo.framepointer<>NR_STACK_POINTER_REG) and
  3270. (current_procinfo.procdef.proctypeoption<>potype_exceptfilter) then
  3271. begin
  3272. regsize:=0;
  3273. push_regs;
  3274. reference_reset_base(current_procinfo.save_regs_ref,
  3275. current_procinfo.framepointer,
  3276. -(localsize+regsize),sizeof(aint),[]);
  3277. end;
  3278. {$endif i386}
  3279. end;
  3280. end;
  3281. procedure tcgx86.g_save_registers(list: TAsmList);
  3282. begin
  3283. {$ifdef i386}
  3284. if paramanager.use_fixed_stack then
  3285. {$endif i386}
  3286. inherited g_save_registers(list);
  3287. end;
  3288. procedure tcgx86.g_restore_registers(list: TAsmList);
  3289. begin
  3290. {$ifdef i386}
  3291. if paramanager.use_fixed_stack then
  3292. {$endif i386}
  3293. inherited g_restore_registers(list);
  3294. end;
  3295. procedure tcgx86.internal_restore_regs(list: TAsmList; use_pop: boolean);
  3296. var
  3297. r: longint;
  3298. hreg: tregister;
  3299. href: treference;
  3300. usedregs: tcpuregisterset;
  3301. begin
  3302. href:=current_procinfo.save_regs_ref;
  3303. usedregs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption);
  3304. for r:=high(saved_standard_registers) downto low(saved_standard_registers) do
  3305. if saved_standard_registers[r] in usedregs then
  3306. begin
  3307. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  3308. { Allocate register so the optimizer does not remove the load }
  3309. a_reg_alloc(list,hreg);
  3310. if use_pop then
  3311. list.concat(Taicpu.Op_reg(A_POP,tcgsize2opsize[OS_ADDR],hreg))
  3312. else
  3313. begin
  3314. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  3315. inc(href.offset,sizeof(aint));
  3316. end;
  3317. end;
  3318. end;
  3319. procedure tcgx86.generate_leave(list: TAsmList);
  3320. begin
  3321. if UseLeave then
  3322. list.concat(taicpu.op_none(A_LEAVE,S_NO))
  3323. else
  3324. begin
  3325. {$if defined(x86_64)}
  3326. list.Concat(taicpu.op_reg_reg(A_MOV,S_Q,NR_RBP,NR_RSP));
  3327. list.Concat(taicpu.op_reg(A_POP,S_Q,NR_RBP));
  3328. {$elseif defined(i386)}
  3329. list.Concat(taicpu.op_reg_reg(A_MOV,S_L,NR_EBP,NR_ESP));
  3330. list.Concat(taicpu.op_reg(A_POP,S_L,NR_EBP));
  3331. {$elseif defined(i8086)}
  3332. list.Concat(taicpu.op_reg_reg(A_MOV,S_W,NR_BP,NR_SP));
  3333. list.Concat(taicpu.op_reg(A_POP,S_W,NR_BP));
  3334. {$endif}
  3335. end;
  3336. end;
  3337. { produces if necessary overflowcode }
  3338. procedure tcgx86.g_overflowcheck(list: TAsmList; const l:tlocation;def:tdef);
  3339. var
  3340. hl : tasmlabel;
  3341. ai : taicpu;
  3342. cond : TAsmCond;
  3343. begin
  3344. if not(cs_check_overflow in current_settings.localswitches) then
  3345. exit;
  3346. current_asmdata.getjumplabel(hl);
  3347. if not ((def.typ=pointerdef) or
  3348. ((def.typ=orddef) and
  3349. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  3350. pasbool8,pasbool16,pasbool32,pasbool64]))) then
  3351. cond:=C_NO
  3352. else
  3353. cond:=C_NB;
  3354. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl);
  3355. ai.SetCondition(cond);
  3356. ai.is_jmp:=true;
  3357. list.concat(ai);
  3358. a_call_name(list,'FPC_OVERFLOW',false);
  3359. a_label(list,hl);
  3360. end;
  3361. end.