cgx86.pas 101 KB

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