cgx86.pas 136 KB

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