cgx86.pas 138 KB

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