cgx86.pas 118 KB

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