cgx86.pas 110 KB

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