cgx86.pas 122 KB

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