cgx86.pas 139 KB

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