cgx86.pas 136 KB

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