cgx86.pas 122 KB

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