cgx86.pas 82 KB

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