cgx86.pas 142 KB

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