cgx86.pas 141 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756
  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. if not reverse then
  2292. list.concat(taicpu.op_reg_reg(A_BSF,opsize,src,tmpreg))
  2293. else
  2294. list.concat(taicpu.op_reg_reg(A_BSR,opsize,src,tmpreg));
  2295. current_asmdata.getjumplabel(l);
  2296. a_jmp_cond(list,OC_NE,l);
  2297. list.concat(taicpu.op_const_reg(A_MOV,opsize,$ff,tmpreg));
  2298. a_label(list,l);
  2299. if tmpreg<>dst then
  2300. a_load_reg_reg(list,srcsize,dstsize,tmpreg,dst);
  2301. end;
  2302. {*************** compare instructructions ****************}
  2303. procedure tcgx86.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  2304. l : tasmlabel);
  2305. {$ifdef x86_64}
  2306. var
  2307. tmpreg : tregister;
  2308. {$endif x86_64}
  2309. begin
  2310. {$ifdef x86_64}
  2311. { x86_64 only supports signed 32 bits constants directly }
  2312. if (size in [OS_S64,OS_64]) and
  2313. ((a<low(longint)) or (a>high(longint))) then
  2314. begin
  2315. tmpreg:=getintregister(list,size);
  2316. a_load_const_reg(list,size,a,tmpreg);
  2317. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  2318. exit;
  2319. end;
  2320. {$endif x86_64}
  2321. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2322. list.concat(taicpu.op_const_reg(A_CMP,tcgsize2opsize[size],a,reg));
  2323. a_jmp_cond(list,cmp_op,l);
  2324. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2325. end;
  2326. procedure tcgx86.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  2327. l : tasmlabel);
  2328. var
  2329. {$ifdef x86_64}
  2330. tmpreg : tregister;
  2331. {$endif x86_64}
  2332. tmpref : treference;
  2333. begin
  2334. tmpref:=ref;
  2335. make_simple_ref(list,tmpref);
  2336. {$ifdef x86_64}
  2337. { x86_64 only supports signed 32 bits constants directly }
  2338. if (size in [OS_S64,OS_64]) and
  2339. ((a<low(longint)) or (a>high(longint))) then
  2340. begin
  2341. tmpreg:=getintregister(list,size);
  2342. a_load_const_reg(list,size,a,tmpreg);
  2343. a_cmp_reg_ref_label(list,size,cmp_op,tmpreg,tmpref,l);
  2344. exit;
  2345. end;
  2346. {$endif x86_64}
  2347. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2348. list.concat(taicpu.op_const_ref(A_CMP,TCgSize2OpSize[size],a,tmpref));
  2349. a_jmp_cond(list,cmp_op,l);
  2350. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2351. end;
  2352. procedure tcgx86.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;
  2353. reg1,reg2 : tregister;l : tasmlabel);
  2354. begin
  2355. check_register_size(size,reg1);
  2356. check_register_size(size,reg2);
  2357. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2358. list.concat(taicpu.op_reg_reg(A_CMP,TCgSize2OpSize[size],reg1,reg2));
  2359. a_jmp_cond(list,cmp_op,l);
  2360. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2361. end;
  2362. procedure tcgx86.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister;l : tasmlabel);
  2363. var
  2364. tmpref : treference;
  2365. begin
  2366. tmpref:=ref;
  2367. make_simple_ref(list,tmpref);
  2368. check_register_size(size,reg);
  2369. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2370. list.concat(taicpu.op_ref_reg(A_CMP,TCgSize2OpSize[size],tmpref,reg));
  2371. a_jmp_cond(list,cmp_op,l);
  2372. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2373. end;
  2374. procedure tcgx86.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister;const ref: treference; l : tasmlabel);
  2375. var
  2376. tmpref : treference;
  2377. begin
  2378. tmpref:=ref;
  2379. make_simple_ref(list,tmpref);
  2380. check_register_size(size,reg);
  2381. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2382. list.concat(taicpu.op_reg_ref(A_CMP,TCgSize2OpSize[size],reg,tmpref));
  2383. a_jmp_cond(list,cmp_op,l);
  2384. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2385. end;
  2386. procedure tcgx86.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  2387. var
  2388. ai : taicpu;
  2389. begin
  2390. if cond=OC_None then
  2391. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  2392. else
  2393. begin
  2394. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  2395. ai.SetCondition(TOpCmp2AsmCond[cond]);
  2396. end;
  2397. ai.is_jmp:=true;
  2398. list.concat(ai);
  2399. end;
  2400. procedure tcgx86.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  2401. var
  2402. ai : taicpu;
  2403. hl : tasmlabel;
  2404. f2 : tresflags;
  2405. begin
  2406. hl:=nil;
  2407. f2:=f;
  2408. case f of
  2409. F_FNE:
  2410. begin
  2411. ai:=Taicpu.op_sym(A_Jcc,S_NO,l);
  2412. ai.SetCondition(C_P);
  2413. ai.is_jmp:=true;
  2414. list.concat(ai);
  2415. f2:=F_NE;
  2416. end;
  2417. F_FE,F_FA,F_FAE,F_FB,F_FBE:
  2418. begin
  2419. { JP before JA/JAE is redundant, but it must be generated here
  2420. and left for peephole optimizer to remove. }
  2421. current_asmdata.getjumplabel(hl);
  2422. ai:=Taicpu.op_sym(A_Jcc,S_NO,hl);
  2423. ai.SetCondition(C_P);
  2424. ai.is_jmp:=true;
  2425. list.concat(ai);
  2426. f2:=FPUFlags2Flags[f];
  2427. end;
  2428. else
  2429. ;
  2430. end;
  2431. ai := Taicpu.op_sym(A_Jcc,S_NO,l);
  2432. ai.SetCondition(flags_to_cond(f2));
  2433. ai.is_jmp := true;
  2434. list.concat(ai);
  2435. if assigned(hl) then
  2436. a_label(list,hl);
  2437. end;
  2438. procedure tcgx86.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  2439. var
  2440. ai : taicpu;
  2441. f2 : tresflags;
  2442. hreg,hreg2 : tregister;
  2443. op: tasmop;
  2444. begin
  2445. hreg2:=NR_NO;
  2446. op:=A_AND;
  2447. f2:=f;
  2448. case f of
  2449. F_FE,F_FNE,F_FB,F_FBE:
  2450. begin
  2451. hreg2:=getintregister(list,OS_8);
  2452. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg2);
  2453. if (f=F_FNE) then { F_FNE means "PF or (not ZF)" }
  2454. begin
  2455. ai.setcondition(C_P);
  2456. op:=A_OR;
  2457. end
  2458. else
  2459. ai.setcondition(C_NP);
  2460. list.concat(ai);
  2461. f2:=FPUFlags2Flags[f];
  2462. end;
  2463. F_FA,F_FAE: { These do not need PF check }
  2464. f2:=FPUFlags2Flags[f];
  2465. else
  2466. ;
  2467. end;
  2468. hreg:=makeregsize(list,reg,OS_8);
  2469. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg);
  2470. ai.setcondition(flags_to_cond(f2));
  2471. list.concat(ai);
  2472. if (hreg2<>NR_NO) then
  2473. list.concat(taicpu.op_reg_reg(op,S_B,hreg2,hreg));
  2474. if reg<>hreg then
  2475. a_load_reg_reg(list,OS_8,size,hreg,reg);
  2476. end;
  2477. procedure tcgx86.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference);
  2478. var
  2479. ai : taicpu;
  2480. tmpref : treference;
  2481. f2 : tresflags;
  2482. begin
  2483. f2:=f;
  2484. case f of
  2485. F_FE,F_FNE,F_FB,F_FBE:
  2486. begin
  2487. inherited g_flags2ref(list,size,f,ref);
  2488. exit;
  2489. end;
  2490. F_FA,F_FAE:
  2491. f2:=FPUFlags2Flags[f];
  2492. else
  2493. ;
  2494. end;
  2495. tmpref:=ref;
  2496. make_simple_ref(list,tmpref);
  2497. if not(size in [OS_8,OS_S8]) then
  2498. a_load_const_ref(list,size,0,tmpref);
  2499. ai:=Taicpu.op_ref(A_SETcc,S_B,tmpref);
  2500. ai.setcondition(flags_to_cond(f2));
  2501. list.concat(ai);
  2502. {$ifndef cpu64bitalu}
  2503. if size in [OS_S64,OS_64] then
  2504. begin
  2505. inc(tmpref.offset,4);
  2506. a_load_const_ref(list,OS_32,0,tmpref);
  2507. end;
  2508. {$endif cpu64bitalu}
  2509. end;
  2510. class function tcgx86.getcopymode(len: tcgint): tcopymode;
  2511. const
  2512. {$if defined(cpu64bitalu)}
  2513. copy_len_sizes = [1, 2, 4, 8];
  2514. {$elseif defined(cpu32bitalu)}
  2515. copy_len_sizes = [1, 2, 4];
  2516. {$elseif defined(cpu16bitalu)}
  2517. copy_len_sizes = [1, 2, 4]; { 4 is included here, because it's still more
  2518. efficient to use copy_move instead of copy_string for copying 4 bytes }
  2519. {$endif}
  2520. var
  2521. helpsize: tcgint;
  2522. begin
  2523. result:=copy_mov;
  2524. helpsize:=3*sizeof(aword);
  2525. if cs_opt_size in current_settings.optimizerswitches then
  2526. helpsize:=2*sizeof(aword);
  2527. {$ifndef i8086}
  2528. { avx helps only to reduce size, using it in general does at least not help on
  2529. an i7-4770
  2530. but using the xmm registers reduces register pressure (FK) }
  2531. if (FPUX86_HAS_AVXUNIT in fpu_capabilities[current_settings.fputype]) and
  2532. ((len mod 4)=0) and (len<=48) {$ifndef i386}and (len>=16){$endif i386} then
  2533. result:=copy_avx
  2534. else if (FPUX86_HAS_AVX512F in fpu_capabilities[current_settings.fputype]) and
  2535. ((len mod 4)=0) and (len<=128) {$ifndef i386}and (len>=16){$endif i386} then
  2536. result:=copy_avx512
  2537. else
  2538. { I'am not sure what CPUs would benefit from using sse instructions for moves
  2539. but using the xmm registers reduces register pressure (FK) }
  2540. if
  2541. {$ifdef x86_64}
  2542. ((current_settings.fputype>=fpu_sse64)
  2543. {$else x86_64}
  2544. ((current_settings.fputype>=fpu_sse)
  2545. {$endif x86_64}
  2546. or (CPUX86_HAS_SSE2 in cpu_capabilities[current_settings.cputype])) and
  2547. ({$ifdef i386}(len=8) or {$endif i386}(len=16) or (len=24) or (len=32) or (len=40) or (len=48)) then
  2548. result:=copy_mm
  2549. else
  2550. {$endif i8086}
  2551. if (cs_mmx in current_settings.localswitches) and
  2552. not(pi_uses_fpu in current_procinfo.flags) and
  2553. ({$ifdef i386}(len=8) or {$endif i386}(len=16) or (len=24) or (len=32)) then
  2554. result:=copy_mmx
  2555. else
  2556. if len>helpsize then
  2557. result:=copy_string;
  2558. if (result=copy_string) and not(CPUX86_HINT_FAST_SHORT_REP_MOVS in cpu_optimization_hints[current_settings.optimizecputype]) and
  2559. { we can use the move variant only if the subroutine does another call }
  2560. (pi_do_call in current_procinfo.flags) then
  2561. result:=copy_fpc_move;
  2562. if (cs_opt_size in current_settings.optimizerswitches) and
  2563. not((len<=16) and (result in [copy_mmx,copy_mm,copy_avx])) and
  2564. not(len in copy_len_sizes) then
  2565. result:=copy_string;
  2566. end;
  2567. { ************* concatcopy ************ }
  2568. procedure Tcgx86.g_concatcopy(list:TAsmList;const source,dest:Treference;len:tcgint);
  2569. const
  2570. {$if defined(cpu64bitalu)}
  2571. REGCX=NR_RCX;
  2572. REGSI=NR_RSI;
  2573. REGDI=NR_RDI;
  2574. push_segment_size = S_L;
  2575. {$elseif defined(cpu32bitalu)}
  2576. REGCX=NR_ECX;
  2577. REGSI=NR_ESI;
  2578. REGDI=NR_EDI;
  2579. push_segment_size = S_L;
  2580. {$elseif defined(cpu16bitalu)}
  2581. REGCX=NR_CX;
  2582. REGSI=NR_SI;
  2583. REGDI=NR_DI;
  2584. push_segment_size = S_W;
  2585. {$endif}
  2586. var
  2587. srcref,dstref,tmpref:Treference;
  2588. r,r0,r1,r2,r3:Tregister;
  2589. copysize:byte;
  2590. cgsize:Tcgsize;
  2591. cm:tcopymode;
  2592. saved_ds,saved_es: Boolean;
  2593. hlist: TAsmList;
  2594. helpsize: tcgint;
  2595. begin
  2596. srcref:=source;
  2597. dstref:=dest;
  2598. {$ifndef i8086}
  2599. make_simple_ref(list,srcref);
  2600. make_simple_ref(list,dstref);
  2601. {$endif not i8086}
  2602. {$ifdef i386}
  2603. { we could handle "far" pointers here, but reloading es/ds is probably much slower
  2604. than just resolving the tls segment }
  2605. if (srcref.refaddr=addr_ntpoff) and (srcref.segment=NR_GS) then
  2606. begin
  2607. r:=getaddressregister(list);
  2608. a_loadaddr_ref_reg(list,srcref,r);
  2609. reference_reset(srcref,srcref.alignment,srcref.volatility);
  2610. srcref.base:=r;
  2611. end;
  2612. if (dstref.refaddr=addr_ntpoff) and (dstref.segment=NR_GS) then
  2613. begin
  2614. r:=getaddressregister(list);
  2615. a_loadaddr_ref_reg(list,dstref,r);
  2616. reference_reset(dstref,dstref.alignment,dstref.volatility);
  2617. dstref.base:=r;
  2618. end;
  2619. {$endif i386}
  2620. {$ifdef x86_64}
  2621. { we could handle "far" pointers here, but reloading es/ds is probably much slower
  2622. than just resolving the tls segment }
  2623. if (srcref.refaddr=addr_tpoff) and (srcref.segment=NR_FS) then
  2624. begin
  2625. r:=getaddressregister(list);
  2626. a_loadaddr_ref_reg(list,srcref,r);
  2627. reference_reset(srcref,srcref.alignment,srcref.volatility);
  2628. srcref.base:=r;
  2629. end;
  2630. if (dstref.refaddr=addr_tpoff) and (dstref.segment=NR_FS) then
  2631. begin
  2632. r:=getaddressregister(list);
  2633. a_loadaddr_ref_reg(list,dstref,r);
  2634. reference_reset(dstref,dstref.alignment,dstref.volatility);
  2635. dstref.base:=r;
  2636. end;
  2637. {$endif x86_64}
  2638. cm:=getcopymode(len);
  2639. {$ifndef i8086}
  2640. { using %fs and %gs as segment prefixes is perfectly valid }
  2641. if ((srcref.segment<>NR_NO) and (srcref.segment<>NR_FS) and (srcref.segment<>NR_GS)) or
  2642. ((dstref.segment<>NR_NO) and (dstref.segment<>NR_FS) and (dstref.segment<>NR_GS)) then
  2643. cm:=copy_string;
  2644. {$endif not i8086}
  2645. case cm of
  2646. copy_mov:
  2647. begin
  2648. copysize:=sizeof(aint);
  2649. cgsize:=int_cgsize(copysize);
  2650. while len<>0 do
  2651. begin
  2652. if len<2 then
  2653. begin
  2654. copysize:=1;
  2655. cgsize:=OS_8;
  2656. end
  2657. else if len<4 then
  2658. begin
  2659. copysize:=2;
  2660. cgsize:=OS_16;
  2661. end
  2662. {$if defined(cpu32bitalu) or defined(cpu64bitalu)}
  2663. else if len<8 then
  2664. begin
  2665. copysize:=4;
  2666. cgsize:=OS_32;
  2667. end
  2668. {$endif cpu32bitalu or cpu64bitalu}
  2669. {$ifdef cpu64bitalu}
  2670. else if len<16 then
  2671. begin
  2672. copysize:=8;
  2673. cgsize:=OS_64;
  2674. end
  2675. {$endif}
  2676. ;
  2677. dec(len,copysize);
  2678. r:=getintregister(list,cgsize);
  2679. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  2680. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  2681. inc(srcref.offset,copysize);
  2682. inc(dstref.offset,copysize);
  2683. end;
  2684. end;
  2685. copy_mmx:
  2686. begin
  2687. r0:=getmmxregister(list);
  2688. r1:=NR_NO;
  2689. r2:=NR_NO;
  2690. r3:=NR_NO;
  2691. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r0,nil);
  2692. if len>=16 then
  2693. begin
  2694. inc(srcref.offset,8);
  2695. r1:=getmmxregister(list);
  2696. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r1,nil);
  2697. end;
  2698. if len>=24 then
  2699. begin
  2700. inc(srcref.offset,8);
  2701. r2:=getmmxregister(list);
  2702. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r2,nil);
  2703. end;
  2704. if len>=32 then
  2705. begin
  2706. inc(srcref.offset,8);
  2707. r3:=getmmxregister(list);
  2708. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  2709. end;
  2710. a_loadmm_reg_ref(list,OS_M64,OS_M64,r0,dstref,nil);
  2711. if len>=16 then
  2712. begin
  2713. inc(dstref.offset,8);
  2714. a_loadmm_reg_ref(list,OS_M64,OS_M64,r1,dstref,nil);
  2715. end;
  2716. if len>=24 then
  2717. begin
  2718. inc(dstref.offset,8);
  2719. a_loadmm_reg_ref(list,OS_M64,OS_M64,r2,dstref,nil);
  2720. end;
  2721. if len>=32 then
  2722. begin
  2723. inc(dstref.offset,8);
  2724. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  2725. end;
  2726. end;
  2727. copy_mm:
  2728. begin
  2729. r0:=NR_NO;
  2730. r1:=NR_NO;
  2731. r2:=NR_NO;
  2732. r3:=NR_NO;
  2733. if len>=16 then
  2734. begin
  2735. r0:=getmmregister(list,OS_M128);
  2736. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r0,nil);
  2737. inc(srcref.offset,16);
  2738. end;
  2739. if len>=32 then
  2740. begin
  2741. r1:=getmmregister(list,OS_M128);
  2742. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r1,nil);
  2743. inc(srcref.offset,16);
  2744. end;
  2745. if len>=48 then
  2746. begin
  2747. r2:=getmmregister(list,OS_M128);
  2748. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r2,nil);
  2749. inc(srcref.offset,16);
  2750. end;
  2751. if (len=8) or (len=24) or (len=40) then
  2752. begin
  2753. r3:=getmmregister(list,OS_M64);
  2754. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  2755. end;
  2756. if len>=16 then
  2757. begin
  2758. a_loadmm_reg_ref(list,OS_M128,OS_M128,r0,dstref,nil);
  2759. inc(dstref.offset,16);
  2760. end;
  2761. if len>=32 then
  2762. begin
  2763. a_loadmm_reg_ref(list,OS_M128,OS_M128,r1,dstref,nil);
  2764. inc(dstref.offset,16);
  2765. end;
  2766. if len>=48 then
  2767. begin
  2768. a_loadmm_reg_ref(list,OS_M128,OS_M128,r2,dstref,nil);
  2769. inc(dstref.offset,16);
  2770. end;
  2771. if (len=8) or (len=24) or (len=40) then
  2772. begin
  2773. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  2774. end;
  2775. end;
  2776. copy_avx512,
  2777. copy_avx:
  2778. begin
  2779. hlist:=TAsmList.create;
  2780. if cm=copy_avx512 then
  2781. while len>=64 do
  2782. begin
  2783. r0:=getmmregister(list,OS_M512);
  2784. a_loadmm_ref_reg(list,OS_M512,OS_M512,srcref,r0,nil);
  2785. a_loadmm_reg_ref(hlist,OS_M512,OS_M512,r0,dstref,nil);
  2786. inc(srcref.offset,64);
  2787. inc(dstref.offset,64);
  2788. dec(len,64);
  2789. Include(current_procinfo.flags,pi_uses_ymm);
  2790. end;
  2791. while len>=32 do
  2792. begin
  2793. r0:=getmmregister(list,OS_M256);
  2794. a_loadmm_ref_reg(list,OS_M256,OS_M256,srcref,r0,nil);
  2795. a_loadmm_reg_ref(hlist,OS_M256,OS_M256,r0,dstref,nil);
  2796. inc(srcref.offset,32);
  2797. inc(dstref.offset,32);
  2798. dec(len,32);
  2799. Include(current_procinfo.flags,pi_uses_ymm);
  2800. end;
  2801. while len>=16 do
  2802. begin
  2803. r0:=getmmregister(list,OS_M128);
  2804. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r0,nil);
  2805. a_loadmm_reg_ref(hlist,OS_M128,OS_M128,r0,dstref,nil);
  2806. inc(srcref.offset,16);
  2807. inc(dstref.offset,16);
  2808. dec(len,16);
  2809. end;
  2810. if len>=8 then
  2811. begin
  2812. r0:=getmmregister(list,OS_M64);
  2813. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r0,nil);
  2814. a_loadmm_reg_ref(hlist,OS_M64,OS_M64,r0,dstref,nil);
  2815. inc(srcref.offset,8);
  2816. inc(dstref.offset,8);
  2817. dec(len,8);
  2818. end;
  2819. if len>=4 then
  2820. begin
  2821. r0:=getintregister(list,OS_32);
  2822. a_load_ref_reg(list,OS_32,OS_32,srcref,r0);
  2823. a_load_reg_ref(hlist,OS_32,OS_32,r0,dstref);
  2824. inc(srcref.offset,4);
  2825. inc(dstref.offset,4);
  2826. dec(len,4);
  2827. end;
  2828. list.concatList(hlist);
  2829. hlist.free;
  2830. end;
  2831. copy_string:
  2832. begin
  2833. getcpuregister(list,REGDI);
  2834. if (dstref.segment=NR_NO) and
  2835. (segment_regs_equal(NR_SS,NR_DS) or ((dstref.base<>NR_BP) and (dstref.base<>NR_SP))) then
  2836. begin
  2837. a_loadaddr_ref_reg(list,dstref,REGDI);
  2838. saved_es:=false;
  2839. {$ifdef volatile_es}
  2840. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_DS));
  2841. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2842. {$endif volatile_es}
  2843. end
  2844. else
  2845. begin
  2846. { load offset of dest. reference }
  2847. tmpref:=dstref;
  2848. tmpref.segment:=NR_NO;
  2849. a_loadaddr_ref_reg(list,tmpref,REGDI);
  2850. {$ifdef volatile_es}
  2851. saved_es:=false;
  2852. {$else volatile_es}
  2853. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_ES));
  2854. saved_es:=true;
  2855. {$endif volatile_es}
  2856. if dstref.segment<>NR_NO then
  2857. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,dstref.segment))
  2858. else if (dstref.base=NR_BP) or (dstref.base=NR_SP) then
  2859. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_SS))
  2860. else
  2861. internalerror(2014040401);
  2862. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2863. end;
  2864. getcpuregister(list,REGSI);
  2865. {$ifdef i8086}
  2866. { at this point, si and di are allocated, so no register is available as index =>
  2867. compiler will hang/ie during spilling, so avoid that srcref has base and index, see also tests/tbs/tb0637.pp }
  2868. if (srcref.base<>NR_NO) and (srcref.index<>NR_NO) then
  2869. begin
  2870. r:=getaddressregister(list);
  2871. a_op_reg_reg_reg(list,OP_ADD,OS_ADDR,srcref.base,srcref.index,r);
  2872. srcref.base:=r;
  2873. srcref.index:=NR_NO;
  2874. end;
  2875. {$endif i8086}
  2876. if ((srcref.segment=NR_NO) and (segment_regs_equal(NR_SS,NR_DS) or ((srcref.base<>NR_BP) and (srcref.base<>NR_SP)))) or
  2877. (is_segment_reg(srcref.segment) and segment_regs_equal(srcref.segment,NR_DS)) then
  2878. begin
  2879. srcref.segment:=NR_NO;
  2880. a_loadaddr_ref_reg(list,srcref,REGSI);
  2881. saved_ds:=false;
  2882. end
  2883. else
  2884. begin
  2885. { load offset of source reference }
  2886. tmpref:=srcref;
  2887. tmpref.segment:=NR_NO;
  2888. a_loadaddr_ref_reg(list,tmpref,REGSI);
  2889. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_DS));
  2890. saved_ds:=true;
  2891. if srcref.segment<>NR_NO then
  2892. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,srcref.segment))
  2893. else if (srcref.base=NR_BP) or (srcref.base=NR_SP) then
  2894. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_SS))
  2895. else
  2896. internalerror(2014040402);
  2897. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_DS));
  2898. end;
  2899. getcpuregister(list,REGCX);
  2900. if ts_cld in current_settings.targetswitches then
  2901. list.concat(Taicpu.op_none(A_CLD,S_NO));
  2902. if (cs_opt_size in current_settings.optimizerswitches) and
  2903. (len>sizeof(aint)+(sizeof(aint) div 2)) then
  2904. begin
  2905. a_load_const_reg(list,OS_INT,len,REGCX);
  2906. list.concat(Taicpu.op_none(A_REP,S_NO));
  2907. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  2908. end
  2909. else
  2910. begin
  2911. helpsize:=len div sizeof(aint);
  2912. len:=len mod sizeof(aint);
  2913. if helpsize>1 then
  2914. begin
  2915. a_load_const_reg(list,OS_INT,helpsize,REGCX);
  2916. list.concat(Taicpu.op_none(A_REP,S_NO));
  2917. end;
  2918. if helpsize>0 then
  2919. begin
  2920. {$if defined(cpu64bitalu)}
  2921. list.concat(Taicpu.op_none(A_MOVSQ,S_NO))
  2922. {$elseif defined(cpu32bitalu)}
  2923. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  2924. {$elseif defined(cpu16bitalu)}
  2925. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  2926. {$endif}
  2927. end;
  2928. if len>=4 then
  2929. begin
  2930. dec(len,4);
  2931. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  2932. end;
  2933. if len>=2 then
  2934. begin
  2935. dec(len,2);
  2936. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  2937. end;
  2938. if len=1 then
  2939. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  2940. end;
  2941. ungetcpuregister(list,REGCX);
  2942. ungetcpuregister(list,REGSI);
  2943. ungetcpuregister(list,REGDI);
  2944. if saved_ds then
  2945. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_DS));
  2946. if saved_es then
  2947. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2948. end
  2949. else
  2950. { copy by using move, should be a good fallback in all other cases }
  2951. g_concatcopy_move(list,source,dest,len);
  2952. end;
  2953. end;
  2954. {****************************************************************************
  2955. Entry/Exit Code Helpers
  2956. ****************************************************************************}
  2957. procedure tcgx86.g_profilecode(list : TAsmList);
  2958. var
  2959. pl : tasmlabel;
  2960. mcountprefix : String[4];
  2961. begin
  2962. case target_info.system of
  2963. {$ifndef NOTARGETWIN}
  2964. system_i386_win32,
  2965. {$endif}
  2966. system_i386_freebsd,
  2967. system_i386_netbsd,
  2968. system_i386_wdosx :
  2969. begin
  2970. Case target_info.system Of
  2971. system_i386_freebsd : mcountprefix:='.';
  2972. system_i386_netbsd : mcountprefix:='__';
  2973. else
  2974. mcountPrefix:='';
  2975. end;
  2976. current_asmdata.getaddrlabel(pl);
  2977. new_section(list,sec_data,lower(current_procinfo.procdef.mangledname),sizeof(pint));
  2978. list.concat(Tai_label.Create(pl));
  2979. list.concat(Tai_const.Create_32bit(0));
  2980. new_section(list,sec_code,lower(current_procinfo.procdef.mangledname),0);
  2981. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  2982. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,NR_EDX));
  2983. a_call_name(list,target_info.Cprefix+mcountprefix+'mcount',false);
  2984. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDX));
  2985. end;
  2986. system_i386_linux:
  2987. a_call_name(list,target_info.Cprefix+'mcount',false);
  2988. system_i386_go32v2,system_i386_watcom:
  2989. begin
  2990. a_call_name(list,'MCOUNT',false);
  2991. end;
  2992. system_x86_64_linux,
  2993. system_x86_64_darwin,
  2994. system_x86_64_iphonesim:
  2995. begin
  2996. a_call_name(list,'mcount',false);
  2997. end;
  2998. system_i386_openbsd,
  2999. system_x86_64_openbsd:
  3000. begin
  3001. a_call_name(list,'__mcount',false);
  3002. end;
  3003. else
  3004. internalerror(2019050701);
  3005. end;
  3006. end;
  3007. procedure tcgx86.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  3008. procedure decrease_sp(a : tcgint);
  3009. var
  3010. href : treference;
  3011. begin
  3012. {$ifdef x86_64}
  3013. if localsize=8 then
  3014. list.concat(Taicpu.op_reg(A_PUSH,TCGSize2OpSize[OS_ADDR],NR_RAX))
  3015. else
  3016. {$endif x86_64}
  3017. begin
  3018. reference_reset_base(href,NR_STACK_POINTER_REG,-a,ctempposinvalid,0,[]);
  3019. { normally, lea is a better choice than a sub to adjust the stack pointer }
  3020. list.concat(Taicpu.op_ref_reg(A_LEA,TCGSize2OpSize[OS_ADDR],href,NR_STACK_POINTER_REG));
  3021. end;
  3022. end;
  3023. {$ifdef x86}
  3024. {$ifndef NOTARGETWIN}
  3025. {$ifndef i8086}
  3026. var
  3027. href : treference;
  3028. i : integer;
  3029. again : tasmlabel;
  3030. {$endif i8086}
  3031. {$endif NOTARGETWIN}
  3032. {$endif x86}
  3033. begin
  3034. if localsize>0 then
  3035. begin
  3036. {$ifdef i386}
  3037. {$ifndef NOTARGETWIN}
  3038. { windows guards only a few pages for stack growing,
  3039. so we have to access every page first }
  3040. if (target_info.system in [system_i386_win32,system_i386_wince]) and
  3041. (localsize>=winstackpagesize) then
  3042. begin
  3043. if localsize div winstackpagesize<=5 then
  3044. begin
  3045. decrease_sp(localsize-4);
  3046. for i:=1 to localsize div winstackpagesize do
  3047. begin
  3048. reference_reset_base(href,NR_ESP,localsize-i*winstackpagesize,ctempposinvalid,4,[]);
  3049. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  3050. end;
  3051. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  3052. end
  3053. else
  3054. begin
  3055. current_asmdata.getjumplabel(again);
  3056. { Using a_reg_alloc instead of getcpuregister, so this procedure
  3057. does not change "used_in_proc" state of EDI and therefore can be
  3058. called after saving registers with "push" instruction
  3059. without creating an unbalanced "pop edi" in epilogue }
  3060. a_reg_alloc(list,NR_EDI);
  3061. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EDI));
  3062. list.concat(Taicpu.op_const_reg(A_MOV,S_L,localsize div winstackpagesize,NR_EDI));
  3063. a_label(list,again);
  3064. decrease_sp(winstackpagesize-4);
  3065. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  3066. if UseIncDec then
  3067. list.concat(Taicpu.op_reg(A_DEC,S_L,NR_EDI))
  3068. else
  3069. list.concat(Taicpu.op_const_reg(A_SUB,S_L,1,NR_EDI));
  3070. a_jmp_cond(list,OC_NE,again);
  3071. decrease_sp(localsize mod winstackpagesize-4);
  3072. reference_reset_base(href,NR_ESP,localsize-4,ctempposinvalid,4,[]);
  3073. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,href,NR_EDI));
  3074. a_reg_dealloc(list,NR_EDI);
  3075. end
  3076. end
  3077. else
  3078. {$endif NOTARGETWIN}
  3079. {$endif i386}
  3080. {$ifdef x86_64}
  3081. {$ifndef NOTARGETWIN}
  3082. { windows guards only a few pages for stack growing,
  3083. so we have to access every page first }
  3084. if (target_info.system=system_x86_64_win64) and
  3085. (localsize>=winstackpagesize) then
  3086. begin
  3087. if localsize div winstackpagesize<=5 then
  3088. begin
  3089. decrease_sp(localsize);
  3090. for i:=1 to localsize div winstackpagesize do
  3091. begin
  3092. reference_reset_base(href,NR_RSP,localsize-i*winstackpagesize+4,ctempposinvalid,4,[]);
  3093. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  3094. end;
  3095. reference_reset_base(href,NR_RSP,0,ctempposinvalid,4,[]);
  3096. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  3097. end
  3098. else
  3099. begin
  3100. current_asmdata.getjumplabel(again);
  3101. getcpuregister(list,NR_R10);
  3102. list.concat(Taicpu.op_const_reg(A_MOV,S_Q,localsize div winstackpagesize,NR_R10));
  3103. a_label(list,again);
  3104. decrease_sp(winstackpagesize);
  3105. reference_reset_base(href,NR_RSP,0,ctempposinvalid,4,[]);
  3106. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  3107. if UseIncDec then
  3108. list.concat(Taicpu.op_reg(A_DEC,S_Q,NR_R10))
  3109. else
  3110. list.concat(Taicpu.op_const_reg(A_SUB,S_Q,1,NR_R10));
  3111. a_jmp_cond(list,OC_NE,again);
  3112. decrease_sp(localsize mod winstackpagesize);
  3113. ungetcpuregister(list,NR_R10);
  3114. end
  3115. end
  3116. else
  3117. {$endif NOTARGETWIN}
  3118. {$endif x86_64}
  3119. decrease_sp(localsize);
  3120. end;
  3121. end;
  3122. procedure tcgx86.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  3123. var
  3124. stackmisalignment: longint;
  3125. regsize: longint;
  3126. {$ifdef i8086}
  3127. dgroup: treference;
  3128. fardataseg: treference;
  3129. {$endif i8086}
  3130. procedure push_regs;
  3131. var
  3132. r: longint;
  3133. usedregs: tcpuregisterset;
  3134. regs_to_save_int: tcpuregisterarray;
  3135. hreg: TRegister;
  3136. begin
  3137. regsize:=0;
  3138. usedregs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption);
  3139. regs_to_save_int:=paramanager.get_saved_registers_int(current_procinfo.procdef.proccalloption);
  3140. for r := low(regs_to_save_int) to high(regs_to_save_int) do
  3141. if regs_to_save_int[r] in usedregs then
  3142. begin
  3143. inc(regsize,sizeof(aint));
  3144. hreg:=newreg(R_INTREGISTER,regs_to_save_int[r],R_SUBWHOLE);
  3145. list.concat(Taicpu.Op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],hreg));
  3146. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3147. current_asmdata.asmcfi.cfa_offset(list,hreg,-(regsize+sizeof(pint)*2+localsize))
  3148. else
  3149. begin
  3150. current_asmdata.asmcfi.cfa_offset(list,hreg,-(regsize+sizeof(pint)+localsize));
  3151. current_asmdata.asmcfi.cfa_def_cfa_offset(list,regsize+localsize+sizeof(pint));
  3152. end;
  3153. end;
  3154. end;
  3155. begin
  3156. regsize:=0;
  3157. stackmisalignment:=0;
  3158. list.concat(tai_regalloc.alloc(NR_STACK_POINTER_REG,nil));
  3159. {$ifdef i8086}
  3160. { Win16 callback/exported proc prologue support.
  3161. Since callbacks can be called from different modules, DS on entry may be
  3162. initialized with the data segment of a different module, so we need to
  3163. get ours. But we can't do
  3164. push ds
  3165. mov ax, dgroup
  3166. mov ds, ax
  3167. because code segments are shared between different instances of the same
  3168. module (which have different instances of the current program's data segment),
  3169. so the same 'mov ax, dgroup' instruction will be used for all instances
  3170. of the program and it will load the same segment into ax.
  3171. So, the standard win16 prologue looks like this:
  3172. mov ax, ds
  3173. nop
  3174. inc bp
  3175. push bp
  3176. mov bp, sp
  3177. push ds
  3178. mov ds, ax
  3179. By default, this does nothing, except wasting a few extra machine cycles and
  3180. destroying ax in the process. However, Windows checks the first three bytes
  3181. of every exported function and if they are 'mov ax,ds/nop', they are replaced
  3182. with nop/nop/nop. Then the MakeProcInstance api call should be used to create
  3183. a thunk that loads ds for the current program instance in ax before calling
  3184. the routine.
  3185. And now the fun part comes: somebody (Michael Geary) figured out that all this
  3186. crap was unnecessary, because in Win16 exe modules, we always have DS=SS, so we
  3187. can simply initialize DS from SS :) And then calling MakeProcInstance becomes
  3188. unnecessary. This is what "smart callbacks" (cs_win16_smartcallbacks) do. However,
  3189. this only works for exe files, not for dlls, because dlls run with DS<>SS. There's
  3190. another solution for dlls - since win16 dlls only have a single instance of their
  3191. data segment, we can initialize ds from dgroup. However, there's not a single
  3192. solution for both exe and dlls, so we don't know what to use e.g. in a unit. So,
  3193. that's why there's still an option to turn smart callbacks off and go the
  3194. MakeProcInstance way.
  3195. Additional details here: http://www.geary.com/fixds.html }
  3196. if (current_settings.x86memorymodel<>mm_huge) and
  3197. (po_exports in current_procinfo.procdef.procoptions) and
  3198. (target_info.system=system_i8086_win16) then
  3199. begin
  3200. if cs_win16_smartcallbacks in current_settings.moduleswitches then
  3201. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_SS,NR_AX))
  3202. else
  3203. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_DS,NR_AX));
  3204. list.concat(Taicpu.op_none(A_NOP));
  3205. end
  3206. { interrupt support for i8086 }
  3207. else if po_interrupt in current_procinfo.procdef.procoptions then
  3208. begin
  3209. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_AX));
  3210. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_BX));
  3211. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_CX));
  3212. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DX));
  3213. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_SI));
  3214. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DI));
  3215. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  3216. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  3217. if current_settings.x86memorymodel=mm_tiny then
  3218. begin
  3219. { in the tiny memory model, we can't use dgroup, because that
  3220. adds a relocation entry to the .exe and we can't produce a
  3221. .com file (because they don't support relactions), so instead
  3222. we initialize DS from CS. }
  3223. if cs_opt_size in current_settings.optimizerswitches then
  3224. begin
  3225. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_CS));
  3226. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_DS));
  3227. end
  3228. else
  3229. begin
  3230. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_CS,NR_AX));
  3231. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3232. end;
  3233. end
  3234. else if current_settings.x86memorymodel=mm_huge then
  3235. begin
  3236. reference_reset(fardataseg,0,[]);
  3237. fardataseg.refaddr:=addr_fardataseg;
  3238. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,fardataseg,NR_AX));
  3239. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3240. end
  3241. else
  3242. begin
  3243. reference_reset(dgroup,0,[]);
  3244. dgroup.refaddr:=addr_dgroup;
  3245. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,dgroup,NR_AX));
  3246. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3247. end;
  3248. end;
  3249. {$endif i8086}
  3250. {$ifdef i386}
  3251. { interrupt support for i386 }
  3252. if (po_interrupt in current_procinfo.procdef.procoptions) then
  3253. begin
  3254. { .... also the segment registers }
  3255. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_GS));
  3256. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_FS));
  3257. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  3258. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  3259. { save the registers of an interrupt procedure }
  3260. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDI));
  3261. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ESI));
  3262. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  3263. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ECX));
  3264. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EBX));
  3265. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EAX));
  3266. { pushf, push %cs, 4*selector registers, 6*general purpose registers }
  3267. inc(stackmisalignment,4+4+4*2+6*4);
  3268. end;
  3269. {$endif i386}
  3270. { save old framepointer }
  3271. if not nostackframe then
  3272. begin
  3273. { return address }
  3274. inc(stackmisalignment,sizeof(pint));
  3275. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  3276. begin
  3277. {$ifdef i386}
  3278. if (not paramanager.use_fixed_stack) then
  3279. push_regs;
  3280. {$endif i386}
  3281. CGmessage(cg_d_stackframe_omited);
  3282. end
  3283. else
  3284. begin
  3285. list.concat(tai_regalloc.alloc(current_procinfo.framepointer,nil));
  3286. {$ifdef i8086}
  3287. if ((ts_x86_far_procs_push_odd_bp in current_settings.targetswitches) or
  3288. ((po_exports in current_procinfo.procdef.procoptions) and
  3289. (target_info.system=system_i8086_win16))) and
  3290. is_proc_far(current_procinfo.procdef) then
  3291. cg.a_op_const_reg(list,OP_ADD,OS_ADDR,1,current_procinfo.framepointer);
  3292. {$endif i8086}
  3293. { push <frame_pointer> }
  3294. inc(stackmisalignment,sizeof(pint));
  3295. include(rg[R_INTREGISTER].preserved_by_proc,RS_FRAME_POINTER_REG);
  3296. list.concat(Taicpu.op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],NR_FRAME_POINTER_REG));
  3297. { Return address and FP are both on stack }
  3298. current_asmdata.asmcfi.cfa_def_cfa_offset(list,2*sizeof(pint));
  3299. current_asmdata.asmcfi.cfa_offset(list,NR_FRAME_POINTER_REG,-(2*sizeof(pint)));
  3300. if current_procinfo.procdef.proctypeoption<>potype_exceptfilter then
  3301. list.concat(Taicpu.op_reg_reg(A_MOV,tcgsize2opsize[OS_ADDR],NR_STACK_POINTER_REG,NR_FRAME_POINTER_REG))
  3302. else
  3303. begin
  3304. push_regs;
  3305. gen_load_frame_for_exceptfilter(list);
  3306. { Need only as much stack space as necessary to do the calls.
  3307. Exception filters don't have own local vars, and temps are 'mapped'
  3308. to the parent procedure.
  3309. maxpushedparasize is already aligned at least on x86_64. }
  3310. localsize:=current_procinfo.maxpushedparasize;
  3311. end;
  3312. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_FRAME_POINTER_REG);
  3313. end;
  3314. { allocate stackframe space }
  3315. if (localsize<>0) or
  3316. ((target_info.stackalign>sizeof(pint)) and
  3317. (stackmisalignment <> 0) and
  3318. ((pi_do_call in current_procinfo.flags) or
  3319. (po_assembler in current_procinfo.procdef.procoptions))) then
  3320. begin
  3321. if target_info.stackalign>sizeof(pint) then
  3322. localsize := align(localsize+stackmisalignment,target_info.stackalign)-stackmisalignment;
  3323. g_stackpointer_alloc(list,localsize);
  3324. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  3325. current_asmdata.asmcfi.cfa_def_cfa_offset(list,regsize+localsize+sizeof(pint));
  3326. current_procinfo.final_localsize:=localsize;
  3327. end
  3328. {$ifdef i8086}
  3329. else
  3330. { on i8086 we always call g_stackpointer_alloc, even with a zero size,
  3331. because it will generate code for stack checking, if stack checking is on }
  3332. g_stackpointer_alloc(list,0)
  3333. {$endif i8086}
  3334. ;
  3335. {$ifdef i8086}
  3336. { win16 exported proc prologue follow-up (see the huge comment above for details) }
  3337. if (current_settings.x86memorymodel<>mm_huge) and
  3338. (po_exports in current_procinfo.procdef.procoptions) and
  3339. (target_info.system=system_i8086_win16) then
  3340. begin
  3341. list.concat(Taicpu.op_reg(A_PUSH,S_W,NR_DS));
  3342. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3343. end
  3344. else if (current_settings.x86memorymodel=mm_huge) and
  3345. not (po_interrupt in current_procinfo.procdef.procoptions) then
  3346. begin
  3347. list.concat(Taicpu.op_reg(A_PUSH,S_W,NR_DS));
  3348. reference_reset(fardataseg,0,[]);
  3349. fardataseg.refaddr:=addr_fardataseg;
  3350. if current_procinfo.procdef.proccalloption=pocall_register then
  3351. begin
  3352. { Use CX register if using register convention
  3353. as it is not a register used to store parameters }
  3354. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,fardataseg,NR_CX));
  3355. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_CX,NR_DS));
  3356. end
  3357. else
  3358. begin
  3359. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,fardataseg,NR_AX));
  3360. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3361. end;
  3362. end;
  3363. { SI and DI are volatile in the BP7 and FPC's pascal calling convention,
  3364. but must be preserved in Microsoft C's pascal calling convention, and
  3365. since Windows is compiled with Microsoft compilers, these registers
  3366. must be saved for exported procedures (BP7 for Win16 also does this). }
  3367. if (po_exports in current_procinfo.procdef.procoptions) and
  3368. (target_info.system=system_i8086_win16) then
  3369. begin
  3370. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_SI));
  3371. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DI));
  3372. end;
  3373. {$endif i8086}
  3374. {$ifdef i386}
  3375. if (not paramanager.use_fixed_stack) and
  3376. (current_procinfo.framepointer<>NR_STACK_POINTER_REG) and
  3377. (current_procinfo.procdef.proctypeoption<>potype_exceptfilter) then
  3378. begin
  3379. regsize:=0;
  3380. push_regs;
  3381. reference_reset_base(current_procinfo.save_regs_ref,
  3382. current_procinfo.framepointer,
  3383. -(localsize+regsize),ctempposinvalid,sizeof(aint),[]);
  3384. end;
  3385. {$endif i386}
  3386. end;
  3387. end;
  3388. procedure tcgx86.g_save_registers(list: TAsmList);
  3389. begin
  3390. {$ifdef i386}
  3391. if paramanager.use_fixed_stack then
  3392. {$endif i386}
  3393. inherited g_save_registers(list);
  3394. end;
  3395. procedure tcgx86.g_restore_registers(list: TAsmList);
  3396. begin
  3397. {$ifdef i386}
  3398. if paramanager.use_fixed_stack then
  3399. {$endif i386}
  3400. inherited g_restore_registers(list);
  3401. end;
  3402. procedure tcgx86.internal_restore_regs(list: TAsmList; use_pop: boolean);
  3403. var
  3404. r: longint;
  3405. hreg: tregister;
  3406. href: treference;
  3407. usedregs: tcpuregisterset;
  3408. regs_to_save_int: tcpuregisterarray;
  3409. begin
  3410. href:=current_procinfo.save_regs_ref;
  3411. usedregs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption);
  3412. regs_to_save_int:=paramanager.get_saved_registers_int(current_procinfo.procdef.proccalloption);
  3413. for r:=high(regs_to_save_int) downto low(regs_to_save_int) do
  3414. if regs_to_save_int[r] in usedregs then
  3415. begin
  3416. hreg:=newreg(R_INTREGISTER,regs_to_save_int[r],R_SUBWHOLE);
  3417. { Allocate register so the optimizer does not remove the load }
  3418. a_reg_alloc(list,hreg);
  3419. if use_pop then
  3420. list.concat(Taicpu.Op_reg(A_POP,tcgsize2opsize[OS_ADDR],hreg))
  3421. else
  3422. begin
  3423. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  3424. inc(href.offset,sizeof(aint));
  3425. end;
  3426. current_asmdata.asmcfi.cfa_restore(list,hreg);
  3427. end;
  3428. end;
  3429. procedure tcgx86.generate_leave(list: TAsmList);
  3430. begin
  3431. if UseLeave then
  3432. list.concat(taicpu.op_none(A_LEAVE,S_NO))
  3433. else
  3434. begin
  3435. {$if defined(x86_64)}
  3436. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_RSP);
  3437. list.Concat(taicpu.op_reg_reg(A_MOV,S_Q,NR_RBP,NR_RSP));
  3438. current_asmdata.asmcfi.cfa_restore(list,NR_RBP);
  3439. current_asmdata.asmcfi.cfa_def_cfa_offset(list,8);
  3440. list.Concat(taicpu.op_reg(A_POP,S_Q,NR_RBP));
  3441. {$elseif defined(i386)}
  3442. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_ESP);
  3443. list.Concat(taicpu.op_reg_reg(A_MOV,S_L,NR_EBP,NR_ESP));
  3444. current_asmdata.asmcfi.cfa_restore(list,NR_EBP);
  3445. current_asmdata.asmcfi.cfa_def_cfa_offset(list,4);
  3446. list.Concat(taicpu.op_reg(A_POP,S_L,NR_EBP));
  3447. {$elseif defined(i8086)}
  3448. list.Concat(taicpu.op_reg_reg(A_MOV,S_W,NR_BP,NR_SP));
  3449. list.Concat(taicpu.op_reg(A_POP,S_W,NR_BP));
  3450. {$endif}
  3451. end;
  3452. end;
  3453. { produces if necessary overflowcode }
  3454. procedure tcgx86.g_overflowcheck(list: TAsmList; const l:tlocation;def:tdef);
  3455. var
  3456. hl : tasmlabel;
  3457. ai : taicpu;
  3458. cond : TAsmCond;
  3459. begin
  3460. if not(cs_check_overflow in current_settings.localswitches) then
  3461. exit;
  3462. current_asmdata.getjumplabel(hl);
  3463. if not ((def.typ=pointerdef) or
  3464. ((def.typ=orddef) and
  3465. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  3466. pasbool1,pasbool8,pasbool16,pasbool32,pasbool64]))) then
  3467. cond:=C_NO
  3468. else
  3469. cond:=C_NB;
  3470. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl);
  3471. ai.SetCondition(cond);
  3472. ai.is_jmp:=true;
  3473. list.concat(ai);
  3474. a_reg_dealloc(list, NR_DEFAULTFLAGS);
  3475. a_call_name(list,'FPC_OVERFLOW',false);
  3476. a_label(list,hl);
  3477. end;
  3478. end.