cgx86.pas 139 KB

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