cgx86.pas 107 KB

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