cgx86.pas 143 KB

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