cgx86.pas 96 KB

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