cgx86.pas 120 KB

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