cgx86.pas 141 KB

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