cgx86.pas 79 KB

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