cgx86.pas 137 KB

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