cgx86.pas 138 KB

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