cgx86.pas 85 KB

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