cgx86.pas 84 KB

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