cgx86.pas 140 KB

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