cgx86.pas 107 KB

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