cgx86.pas 99 KB

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