cgx86.pas 89 KB

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