cgx86.pas 139 KB

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