cgx86.pas 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217
  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_M64,
  159. OS_M128:
  160. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMWHOLE);
  161. else
  162. internalerror(200506041);
  163. end;
  164. end;
  165. procedure Tcgx86.getcpuregister(list:TAsmList;r:Tregister);
  166. begin
  167. if getregtype(r)=R_FPUREGISTER then
  168. internalerror(2003121210)
  169. else
  170. inherited getcpuregister(list,r);
  171. end;
  172. procedure tcgx86.ungetcpuregister(list:TAsmList;r:Tregister);
  173. begin
  174. if getregtype(r)=R_FPUREGISTER then
  175. rgfpu.ungetregisterfpu(list,r)
  176. else
  177. inherited ungetcpuregister(list,r);
  178. end;
  179. procedure Tcgx86.alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  180. begin
  181. if rt<>R_FPUREGISTER then
  182. inherited alloccpuregisters(list,rt,r);
  183. end;
  184. procedure Tcgx86.dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  185. begin
  186. if rt<>R_FPUREGISTER then
  187. inherited dealloccpuregisters(list,rt,r);
  188. end;
  189. function Tcgx86.uses_registers(rt:Tregistertype):boolean;
  190. begin
  191. if rt=R_FPUREGISTER then
  192. result:=false
  193. else
  194. result:=inherited uses_registers(rt);
  195. end;
  196. procedure tcgx86.add_reg_instruction(instr:Tai;r:tregister);
  197. begin
  198. if getregtype(r)<>R_FPUREGISTER then
  199. inherited add_reg_instruction(instr,r);
  200. end;
  201. procedure tcgx86.dec_fpu_stack;
  202. begin
  203. if rgfpu.fpuvaroffset<=0 then
  204. internalerror(200604201);
  205. dec(rgfpu.fpuvaroffset);
  206. end;
  207. procedure tcgx86.inc_fpu_stack;
  208. begin
  209. inc(rgfpu.fpuvaroffset);
  210. end;
  211. {****************************************************************************
  212. This is private property, keep out! :)
  213. ****************************************************************************}
  214. procedure tcgx86.sizes2load(s1,s2 : tcgsize; var op: tasmop; var s3: topsize);
  215. begin
  216. { ensure to have always valid sizes }
  217. if s1=OS_NO then
  218. s1:=s2;
  219. if s2=OS_NO then
  220. s2:=s1;
  221. case s2 of
  222. OS_8,OS_S8 :
  223. if S1 in [OS_8,OS_S8] then
  224. s3 := S_B
  225. else
  226. internalerror(200109221);
  227. OS_16,OS_S16:
  228. case s1 of
  229. OS_8,OS_S8:
  230. s3 := S_BW;
  231. OS_16,OS_S16:
  232. s3 := S_W;
  233. else
  234. internalerror(200109222);
  235. end;
  236. OS_32,OS_S32:
  237. case s1 of
  238. OS_8,OS_S8:
  239. s3 := S_BL;
  240. OS_16,OS_S16:
  241. s3 := S_WL;
  242. OS_32,OS_S32:
  243. s3 := S_L;
  244. else
  245. internalerror(200109223);
  246. end;
  247. {$ifdef x86_64}
  248. OS_64,OS_S64:
  249. case s1 of
  250. OS_8:
  251. s3 := S_BL;
  252. OS_S8:
  253. s3 := S_BQ;
  254. OS_16:
  255. s3 := S_WL;
  256. OS_S16:
  257. s3 := S_WQ;
  258. OS_32:
  259. s3 := S_L;
  260. OS_S32:
  261. s3 := S_LQ;
  262. OS_64,OS_S64:
  263. s3 := S_Q;
  264. else
  265. internalerror(200304302);
  266. end;
  267. {$endif x86_64}
  268. else
  269. internalerror(200109227);
  270. end;
  271. if s3 in [S_B,S_W,S_L,S_Q] then
  272. op := A_MOV
  273. else if s1 in [OS_8,OS_16,OS_32,OS_64] then
  274. op := A_MOVZX
  275. else
  276. {$ifdef x86_64}
  277. if s3 in [S_LQ] then
  278. op := A_MOVSXD
  279. else
  280. {$endif x86_64}
  281. op := A_MOVSX;
  282. end;
  283. procedure tcgx86.make_simple_ref(list:TAsmList;var ref: treference);
  284. var
  285. hreg : tregister;
  286. href : treference;
  287. {$ifndef x86_64}
  288. add_hreg: boolean;
  289. {$endif not x86_64}
  290. begin
  291. { make_simple_ref() may have already been called earlier, and in that
  292. case make sure we don't perform the PIC-simplifications twice }
  293. if (ref.refaddr in [addr_pic,addr_pic_no_got]) then
  294. exit;
  295. {$ifdef x86_64}
  296. { Only 32bit is allowed }
  297. if ((ref.offset<low(longint)) or (ref.offset>high(longint))) then
  298. begin
  299. { Load constant value to register }
  300. hreg:=GetAddressRegister(list);
  301. list.concat(taicpu.op_const_reg(A_MOV,S_Q,ref.offset,hreg));
  302. ref.offset:=0;
  303. {if assigned(ref.symbol) then
  304. begin
  305. list.concat(taicpu.op_sym_ofs_reg(A_ADD,S_Q,ref.symbol,0,hreg));
  306. ref.symbol:=nil;
  307. end;}
  308. { Add register to reference }
  309. if ref.index=NR_NO then
  310. ref.index:=hreg
  311. else
  312. begin
  313. { don't use add, as the flags may contain a value }
  314. reference_reset_base(href,ref.base,0,8);
  315. href.index:=hreg;
  316. if ref.scalefactor<>0 then
  317. begin
  318. reference_reset_base(href,ref.base,0,8);
  319. href.index:=hreg;
  320. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,hreg));
  321. ref.base:=hreg;
  322. end
  323. else
  324. begin
  325. reference_reset_base(href,ref.index,0,8);
  326. href.index:=hreg;
  327. list.concat(taicpu.op_reg_reg(A_ADD,S_Q,ref.index,hreg));
  328. ref.index:=hreg;
  329. end;
  330. end;
  331. end;
  332. if assigned(ref.symbol) and not((ref.symbol.bind=AB_LOCAL) and (ref.symbol.typ in [AT_LABEL,AT_FUNCTION])) then
  333. begin
  334. if cs_create_pic in current_settings.moduleswitches then
  335. begin
  336. { Local data symbols must not be accessed via the GOT on
  337. darwin/x86_64 under certain circumstances (and do not
  338. have to be in other cases); however, linux/x86_64 does
  339. require it; don't know about others, so do use GOT for
  340. safety reasons
  341. }
  342. if (ref.symbol.bind=AB_LOCAL) and
  343. (ref.symbol.typ=AT_DATA) and
  344. ((target_info.system=system_x86_64_darwin) or
  345. (target_info.system=system_x86_64_solaris)) 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=system_i386_darwin) 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. current_asmdata.asmlists[al_imports].concat(Tai_section.create(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/x86_64's assembler doesn't want @PLT after call symbols }
  630. (target_info.system<>system_x86_64_darwin) 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 : aint; 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 : aint;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=system_i386_darwin) 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) and
  824. ((target_info.system=system_x86_64_darwin) or
  825. (target_info.system=system_x86_64_solaris)))
  826. {$endif x86_64}
  827. then
  828. begin
  829. {$ifdef x86_64}
  830. reference_reset_symbol(tmpref,ref.symbol,0,ref.alignment);
  831. tmpref.refaddr:=addr_pic;
  832. tmpref.base:=NR_RIP;
  833. list.concat(taicpu.op_ref_reg(A_MOV,S_Q,tmpref,r));
  834. {$else x86_64}
  835. reference_reset_symbol(tmpref,ref.symbol,0,ref.alignment);
  836. tmpref.refaddr:=addr_pic;
  837. tmpref.base:=current_procinfo.got;
  838. include(current_procinfo.flags,pi_needs_got);
  839. list.concat(taicpu.op_ref_reg(A_MOV,S_L,tmpref,r));
  840. {$endif x86_64}
  841. if offset<>0 then
  842. a_op_const_reg(list,OP_ADD,OS_ADDR,offset,r);
  843. end
  844. {$ifdef x86_64}
  845. else if (target_info.system in (systems_all_windows+[system_x86_64_darwin]))
  846. or ((target_info.system = system_x86_64_solaris) and
  847. (cs_create_pic in current_settings.moduleswitches))
  848. then
  849. begin
  850. { Win64 and Darwin/x86_64 always require RIP-relative addressing }
  851. tmpref:=ref;
  852. tmpref.base:=NR_RIP;
  853. tmpref.refaddr:=addr_pic_no_got;
  854. list.concat(Taicpu.op_ref_reg(A_LEA,S_Q,tmpref,r));
  855. end
  856. {$endif x86_64}
  857. else
  858. begin
  859. tmpref:=ref;
  860. tmpref.refaddr:=ADDR_FULL;
  861. list.concat(Taicpu.op_ref_reg(A_MOV,tcgsize2opsize[OS_ADDR],tmpref,r));
  862. end
  863. end
  864. else
  865. a_load_const_reg(list,OS_ADDR,offset,r)
  866. end
  867. else if (base=NR_NO) and (index<>NR_NO) and
  868. (offset=0) and (scalefactor=0) and (symbol=nil) then
  869. a_load_reg_reg(list,OS_ADDR,OS_ADDR,index,r)
  870. else if (base<>NR_NO) and (index=NR_NO) and
  871. (offset=0) and (symbol=nil) then
  872. a_load_reg_reg(list,OS_ADDR,OS_ADDR,base,r)
  873. else
  874. begin
  875. tmpref:=ref;
  876. make_simple_ref(list,tmpref);
  877. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,r));
  878. end;
  879. if segment<>NR_NO then
  880. begin
  881. if (tf_section_threadvars in target_info.flags) then
  882. begin
  883. { Convert thread local address to a process global addres
  884. as we cannot handle far pointers.}
  885. case target_info.system of
  886. system_i386_linux:
  887. if segment=NR_GS then
  888. begin
  889. reference_reset_symbol(tmpref,current_asmdata.RefAsmSymbol('___fpc_threadvar_offset'),0,ref.alignment);
  890. tmpref.segment:=NR_GS;
  891. list.concat(Taicpu.op_ref_reg(A_ADD,tcgsize2opsize[OS_ADDR],tmpref,r));
  892. end
  893. else
  894. cgmessage(cg_e_cant_use_far_pointer_there);
  895. system_i386_win32:
  896. if segment=NR_FS then
  897. begin
  898. allocallcpuregisters(list);
  899. a_call_name(list,'GetTls',false);
  900. deallocallcpuregisters(list);
  901. list.concat(Taicpu.op_reg_reg(A_ADD,tcgsize2opsize[OS_ADDR],NR_EAX,r));
  902. end
  903. else
  904. cgmessage(cg_e_cant_use_far_pointer_there);
  905. else
  906. cgmessage(cg_e_cant_use_far_pointer_there);
  907. end;
  908. end
  909. else
  910. cgmessage(cg_e_cant_use_far_pointer_there);
  911. end;
  912. end;
  913. end;
  914. { all fpu load routines expect that R_ST[0-7] means an fpu regvar and }
  915. { R_ST means "the current value at the top of the fpu stack" (JM) }
  916. procedure tcgx86.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  917. var
  918. href: treference;
  919. op: tasmop;
  920. s: topsize;
  921. begin
  922. if (reg1<>NR_ST) then
  923. begin
  924. floatloadops(tosize,op,s);
  925. list.concat(taicpu.op_reg(op,s,rgfpu.correct_fpuregister(reg1,rgfpu.fpuvaroffset)));
  926. inc_fpu_stack;
  927. end;
  928. if (reg2<>NR_ST) then
  929. begin
  930. floatstoreops(tosize,op,s);
  931. list.concat(taicpu.op_reg(op,s,rgfpu.correct_fpuregister(reg2,rgfpu.fpuvaroffset)));
  932. dec_fpu_stack;
  933. end;
  934. { OS_F80 < OS_C64, but OS_C64 fits perfectly in OS_F80 }
  935. if (reg1=NR_ST) and
  936. (reg2=NR_ST) and
  937. (tosize<>OS_F80) and
  938. (tosize<fromsize) then
  939. begin
  940. { can't round down to lower precision in x87 :/ }
  941. tg.gettemp(list,tcgsize2size[tosize],tcgsize2size[tosize],tt_normal,href);
  942. a_loadfpu_reg_ref(list,fromsize,tosize,NR_ST,href);
  943. a_loadfpu_ref_reg(list,tosize,tosize,href,NR_ST);
  944. tg.ungettemp(list,href);
  945. end;
  946. end;
  947. procedure tcgx86.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  948. begin
  949. floatload(list,fromsize,ref);
  950. a_loadfpu_reg_reg(list,fromsize,tosize,NR_ST,reg);
  951. end;
  952. procedure tcgx86.a_loadfpu_reg_ref(list: TAsmList; fromsize,tosize: tcgsize; reg: tregister; const ref: treference);
  953. begin
  954. if reg<>NR_ST then
  955. a_loadfpu_reg_reg(list,fromsize,tosize,reg,NR_ST);
  956. floatstore(list,tosize,ref);
  957. end;
  958. function get_scalar_mm_op(fromsize,tosize : tcgsize) : tasmop;
  959. const
  960. convertop : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  961. (A_MOVSS,A_CVTSS2SD,A_NONE,A_NONE,A_NONE),
  962. (A_CVTSD2SS,A_MOVSD,A_NONE,A_NONE,A_NONE),
  963. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  964. (A_NONE,A_NONE,A_NONE,A_MOVQ,A_NONE),
  965. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE));
  966. begin
  967. if (fromsize in [low(convertop)..high(convertop)]) and
  968. (tosize in [low(convertop)..high(convertop)]) then
  969. result:=convertop[fromsize,tosize]
  970. else if (fromsize=tosize) and
  971. (fromsize=OS_M64) then
  972. result:=A_MOVQ
  973. else
  974. internalerror(2010060104);
  975. if result=A_NONE then
  976. internalerror(200312205);
  977. end;
  978. procedure tcgx86.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle);
  979. var
  980. instr : taicpu;
  981. begin
  982. if shuffle=nil then
  983. begin
  984. if fromsize=tosize then
  985. { needs correct size in case of spilling }
  986. case fromsize of
  987. OS_F32:
  988. instr:=taicpu.op_reg_reg(A_MOVAPS,S_NO,reg1,reg2);
  989. OS_F64:
  990. instr:=taicpu.op_reg_reg(A_MOVAPD,S_NO,reg1,reg2);
  991. OS_M64:
  992. instr:=taicpu.op_reg_reg(A_MOVQ,S_NO,reg1,reg2);
  993. else
  994. internalerror(2006091201);
  995. end
  996. else
  997. internalerror(200312202);
  998. end
  999. else if shufflescalar(shuffle) then
  1000. instr:=taicpu.op_reg_reg(get_scalar_mm_op(fromsize,tosize),S_NO,reg1,reg2)
  1001. else
  1002. internalerror(200312201);
  1003. case get_scalar_mm_op(fromsize,tosize) of
  1004. A_MOVSS,
  1005. A_MOVSD,
  1006. A_MOVQ:
  1007. add_move_instruction(instr);
  1008. end;
  1009. list.concat(instr);
  1010. end;
  1011. procedure tcgx86.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1012. var
  1013. tmpref : treference;
  1014. begin
  1015. tmpref:=ref;
  1016. make_simple_ref(list,tmpref);
  1017. if shuffle=nil then
  1018. begin
  1019. if fromsize=OS_M64 then
  1020. list.concat(taicpu.op_ref_reg(A_MOVQ,S_NO,tmpref,reg))
  1021. else
  1022. {$ifdef x86_64}
  1023. { x86-64 has always properly aligned data }
  1024. list.concat(taicpu.op_ref_reg(A_MOVDQA,S_NO,tmpref,reg));
  1025. {$else x86_64}
  1026. list.concat(taicpu.op_ref_reg(A_MOVDQU,S_NO,tmpref,reg));
  1027. {$endif x86_64}
  1028. end
  1029. else if shufflescalar(shuffle) then
  1030. list.concat(taicpu.op_ref_reg(get_scalar_mm_op(fromsize,tosize),S_NO,tmpref,reg))
  1031. else
  1032. internalerror(200312252);
  1033. end;
  1034. procedure tcgx86.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle);
  1035. var
  1036. hreg : tregister;
  1037. tmpref : treference;
  1038. begin
  1039. tmpref:=ref;
  1040. make_simple_ref(list,tmpref);
  1041. if shuffle=nil then
  1042. begin
  1043. if fromsize=OS_M64 then
  1044. list.concat(taicpu.op_reg_ref(A_MOVQ,S_NO,reg,tmpref))
  1045. else
  1046. {$ifdef x86_64}
  1047. { x86-64 has always properly aligned data }
  1048. list.concat(taicpu.op_reg_ref(A_MOVDQA,S_NO,reg,tmpref))
  1049. {$else x86_64}
  1050. list.concat(taicpu.op_reg_ref(A_MOVDQU,S_NO,reg,tmpref))
  1051. {$endif x86_64}
  1052. end
  1053. else if shufflescalar(shuffle) then
  1054. begin
  1055. if tosize<>fromsize then
  1056. begin
  1057. hreg:=getmmregister(list,tosize);
  1058. list.concat(taicpu.op_reg_reg(get_scalar_mm_op(fromsize,tosize),S_NO,reg,hreg));
  1059. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(tosize,tosize),S_NO,hreg,tmpref));
  1060. end
  1061. else
  1062. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(fromsize,tosize),S_NO,reg,tmpref));
  1063. end
  1064. else
  1065. internalerror(200312252);
  1066. end;
  1067. procedure tcgx86.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1068. var
  1069. l : tlocation;
  1070. begin
  1071. l.loc:=LOC_REFERENCE;
  1072. l.reference:=ref;
  1073. l.size:=size;
  1074. opmm_loc_reg(list,op,size,l,reg,shuffle);
  1075. end;
  1076. procedure tcgx86.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle);
  1077. var
  1078. l : tlocation;
  1079. begin
  1080. l.loc:=LOC_MMREGISTER;
  1081. l.register:=src;
  1082. l.size:=size;
  1083. opmm_loc_reg(list,op,size,l,dst,shuffle);
  1084. end;
  1085. procedure tcgx86.opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;dst: tregister; shuffle : pmmshuffle);
  1086. const
  1087. opmm2asmop : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  1088. ( { scalar }
  1089. ( { OS_F32 }
  1090. 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
  1091. ),
  1092. ( { OS_F64 }
  1093. 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
  1094. )
  1095. ),
  1096. ( { vectorized/packed }
  1097. { because the logical packed single instructions have shorter op codes, we use always
  1098. these
  1099. }
  1100. ( { OS_F32 }
  1101. 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
  1102. ),
  1103. ( { OS_F64 }
  1104. 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
  1105. )
  1106. )
  1107. );
  1108. var
  1109. resultreg : tregister;
  1110. asmop : tasmop;
  1111. begin
  1112. { this is an internally used procedure so the parameters have
  1113. some constrains
  1114. }
  1115. if loc.size<>size then
  1116. internalerror(200312213);
  1117. resultreg:=dst;
  1118. { deshuffle }
  1119. //!!!
  1120. if (shuffle<>nil) and not(shufflescalar(shuffle)) then
  1121. begin
  1122. internalerror(2010060101);
  1123. end
  1124. else if (shuffle=nil) then
  1125. asmop:=opmm2asmop[1,size,op]
  1126. else if shufflescalar(shuffle) then
  1127. begin
  1128. asmop:=opmm2asmop[0,size,op];
  1129. { no scalar operation available? }
  1130. if asmop=A_NOP then
  1131. begin
  1132. { do vectorized and shuffle finally }
  1133. internalerror(2010060102);
  1134. end;
  1135. end
  1136. else
  1137. internalerror(200312211);
  1138. if asmop=A_NOP then
  1139. internalerror(200312216);
  1140. case loc.loc of
  1141. LOC_CREFERENCE,LOC_REFERENCE:
  1142. begin
  1143. make_simple_ref(current_asmdata.CurrAsmList,loc.reference);
  1144. list.concat(taicpu.op_ref_reg(asmop,S_NO,loc.reference,resultreg));
  1145. end;
  1146. LOC_CMMREGISTER,LOC_MMREGISTER:
  1147. list.concat(taicpu.op_reg_reg(asmop,S_NO,loc.register,resultreg));
  1148. else
  1149. internalerror(200312214);
  1150. end;
  1151. { shuffle }
  1152. if resultreg<>dst then
  1153. begin
  1154. internalerror(200312212);
  1155. end;
  1156. end;
  1157. procedure tcgx86.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister);
  1158. var
  1159. opcode : tasmop;
  1160. power : longint;
  1161. {$ifdef x86_64}
  1162. tmpreg : tregister;
  1163. {$endif x86_64}
  1164. begin
  1165. optimize_op_const(op, a);
  1166. {$ifdef x86_64}
  1167. { x86_64 only supports signed 32 bits constants directly }
  1168. if not(op in [OP_NONE,OP_MOVE]) and
  1169. (size in [OS_S64,OS_64]) and
  1170. ((a<low(longint)) or (a>high(longint))) then
  1171. begin
  1172. tmpreg:=getintregister(list,size);
  1173. a_load_const_reg(list,size,a,tmpreg);
  1174. a_op_reg_reg(list,op,size,tmpreg,reg);
  1175. exit;
  1176. end;
  1177. {$endif x86_64}
  1178. check_register_size(size,reg);
  1179. case op of
  1180. OP_NONE :
  1181. begin
  1182. { Opcode is optimized away }
  1183. end;
  1184. OP_MOVE :
  1185. begin
  1186. { Optimized, replaced with a simple load }
  1187. a_load_const_reg(list,size,a,reg);
  1188. end;
  1189. OP_DIV, OP_IDIV:
  1190. begin
  1191. if ispowerof2(int64(a),power) then
  1192. begin
  1193. case op of
  1194. OP_DIV:
  1195. opcode := A_SHR;
  1196. OP_IDIV:
  1197. opcode := A_SAR;
  1198. end;
  1199. list.concat(taicpu.op_const_reg(opcode,TCgSize2OpSize[size],power,reg));
  1200. exit;
  1201. end;
  1202. { the rest should be handled specifically in the code }
  1203. { generator because of the silly register usage restraints }
  1204. internalerror(200109224);
  1205. end;
  1206. OP_MUL,OP_IMUL:
  1207. begin
  1208. if not(cs_check_overflow in current_settings.localswitches) and
  1209. ispowerof2(int64(a),power) then
  1210. begin
  1211. list.concat(taicpu.op_const_reg(A_SHL,TCgSize2OpSize[size],power,reg));
  1212. exit;
  1213. end;
  1214. if op = OP_IMUL then
  1215. list.concat(taicpu.op_const_reg(A_IMUL,TCgSize2OpSize[size],a,reg))
  1216. else
  1217. { OP_MUL should be handled specifically in the code }
  1218. { generator because of the silly register usage restraints }
  1219. internalerror(200109225);
  1220. end;
  1221. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  1222. if not(cs_check_overflow in current_settings.localswitches) and
  1223. (a = 1) and
  1224. (op in [OP_ADD,OP_SUB]) then
  1225. if op = OP_ADD then
  1226. list.concat(taicpu.op_reg(A_INC,TCgSize2OpSize[size],reg))
  1227. else
  1228. list.concat(taicpu.op_reg(A_DEC,TCgSize2OpSize[size],reg))
  1229. else if (a = 0) then
  1230. if (op <> OP_AND) then
  1231. exit
  1232. else
  1233. list.concat(taicpu.op_const_reg(A_MOV,TCgSize2OpSize[size],0,reg))
  1234. else if (aword(a) = high(aword)) and
  1235. (op in [OP_AND,OP_OR,OP_XOR]) then
  1236. begin
  1237. case op of
  1238. OP_AND:
  1239. exit;
  1240. OP_OR:
  1241. list.concat(taicpu.op_const_reg(A_MOV,TCgSize2OpSize[size],aint(high(aword)),reg));
  1242. OP_XOR:
  1243. list.concat(taicpu.op_reg(A_NOT,TCgSize2OpSize[size],reg));
  1244. end
  1245. end
  1246. else
  1247. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,reg));
  1248. OP_SHL,OP_SHR,OP_SAR,OP_ROL,OP_ROR:
  1249. begin
  1250. {$ifdef x86_64}
  1251. if (a and 63) <> 0 Then
  1252. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 63,reg));
  1253. if (a shr 6) <> 0 Then
  1254. internalerror(200609073);
  1255. {$else x86_64}
  1256. if (a and 31) <> 0 Then
  1257. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,reg));
  1258. if (a shr 5) <> 0 Then
  1259. internalerror(200609071);
  1260. {$endif x86_64}
  1261. end
  1262. else internalerror(200609072);
  1263. end;
  1264. end;
  1265. procedure tcgx86.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; const ref: TReference);
  1266. var
  1267. opcode: tasmop;
  1268. power: longint;
  1269. {$ifdef x86_64}
  1270. tmpreg : tregister;
  1271. {$endif x86_64}
  1272. tmpref : treference;
  1273. begin
  1274. optimize_op_const(op, a);
  1275. tmpref:=ref;
  1276. make_simple_ref(list,tmpref);
  1277. {$ifdef x86_64}
  1278. { x86_64 only supports signed 32 bits constants directly }
  1279. if not(op in [OP_NONE,OP_MOVE]) and
  1280. (size in [OS_S64,OS_64]) and
  1281. ((a<low(longint)) or (a>high(longint))) then
  1282. begin
  1283. tmpreg:=getintregister(list,size);
  1284. a_load_const_reg(list,size,a,tmpreg);
  1285. a_op_reg_ref(list,op,size,tmpreg,tmpref);
  1286. exit;
  1287. end;
  1288. {$endif x86_64}
  1289. Case Op of
  1290. OP_NONE :
  1291. begin
  1292. { Opcode is optimized away }
  1293. end;
  1294. OP_MOVE :
  1295. begin
  1296. { Optimized, replaced with a simple load }
  1297. a_load_const_ref(list,size,a,ref);
  1298. end;
  1299. OP_DIV, OP_IDIV:
  1300. Begin
  1301. if ispowerof2(int64(a),power) then
  1302. begin
  1303. case op of
  1304. OP_DIV:
  1305. opcode := A_SHR;
  1306. OP_IDIV:
  1307. opcode := A_SAR;
  1308. end;
  1309. list.concat(taicpu.op_const_ref(opcode,
  1310. TCgSize2OpSize[size],power,tmpref));
  1311. exit;
  1312. end;
  1313. { the rest should be handled specifically in the code }
  1314. { generator because of the silly register usage restraints }
  1315. internalerror(200109231);
  1316. End;
  1317. OP_MUL,OP_IMUL:
  1318. begin
  1319. if not(cs_check_overflow in current_settings.localswitches) and
  1320. ispowerof2(int64(a),power) then
  1321. begin
  1322. list.concat(taicpu.op_const_ref(A_SHL,TCgSize2OpSize[size],
  1323. power,tmpref));
  1324. exit;
  1325. end;
  1326. { can't multiply a memory location directly with a constant }
  1327. if op = OP_IMUL then
  1328. inherited a_op_const_ref(list,op,size,a,tmpref)
  1329. else
  1330. { OP_MUL should be handled specifically in the code }
  1331. { generator because of the silly register usage restraints }
  1332. internalerror(200109232);
  1333. end;
  1334. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  1335. if not(cs_check_overflow in current_settings.localswitches) and
  1336. (a = 1) and
  1337. (op in [OP_ADD,OP_SUB]) then
  1338. if op = OP_ADD then
  1339. list.concat(taicpu.op_ref(A_INC,TCgSize2OpSize[size],tmpref))
  1340. else
  1341. list.concat(taicpu.op_ref(A_DEC,TCgSize2OpSize[size],tmpref))
  1342. else if (a = 0) then
  1343. if (op <> OP_AND) then
  1344. exit
  1345. else
  1346. a_load_const_ref(list,size,0,tmpref)
  1347. else if (aword(a) = high(aword)) and
  1348. (op in [OP_AND,OP_OR,OP_XOR]) then
  1349. begin
  1350. case op of
  1351. OP_AND:
  1352. exit;
  1353. OP_OR:
  1354. list.concat(taicpu.op_const_ref(A_MOV,TCgSize2OpSize[size],aint(high(aword)),tmpref));
  1355. OP_XOR:
  1356. list.concat(taicpu.op_ref(A_NOT,TCgSize2OpSize[size],tmpref));
  1357. end
  1358. end
  1359. else
  1360. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],
  1361. TCgSize2OpSize[size],a,tmpref));
  1362. OP_SHL,OP_SHR,OP_SAR,OP_ROL,OP_ROR:
  1363. begin
  1364. if (a and 31) <> 0 then
  1365. list.concat(taicpu.op_const_ref(
  1366. TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,tmpref));
  1367. if (a shr 5) <> 0 Then
  1368. internalerror(68991);
  1369. end
  1370. else internalerror(68992);
  1371. end;
  1372. end;
  1373. procedure tcgx86.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  1374. var
  1375. dstsize: topsize;
  1376. instr:Taicpu;
  1377. begin
  1378. check_register_size(size,src);
  1379. check_register_size(size,dst);
  1380. dstsize := tcgsize2opsize[size];
  1381. case op of
  1382. OP_NEG,OP_NOT:
  1383. begin
  1384. if src<>dst then
  1385. a_load_reg_reg(list,size,size,src,dst);
  1386. list.concat(taicpu.op_reg(TOpCG2AsmOp[op],dstsize,dst));
  1387. end;
  1388. OP_MUL,OP_DIV,OP_IDIV:
  1389. { special stuff, needs separate handling inside code }
  1390. { generator }
  1391. internalerror(200109233);
  1392. OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR:
  1393. begin
  1394. { Use ecx to load the value, that allows better coalescing }
  1395. getcpuregister(list,NR_ECX);
  1396. a_load_reg_reg(list,size,OS_32,src,NR_ECX);
  1397. list.concat(taicpu.op_reg_reg(Topcg2asmop[op],tcgsize2opsize[size],NR_CL,dst));
  1398. ungetcpuregister(list,NR_ECX);
  1399. end;
  1400. else
  1401. begin
  1402. if reg2opsize(src) <> dstsize then
  1403. internalerror(200109226);
  1404. instr:=taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,src,dst);
  1405. list.concat(instr);
  1406. end;
  1407. end;
  1408. end;
  1409. procedure tcgx86.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1410. var
  1411. tmpref : treference;
  1412. begin
  1413. tmpref:=ref;
  1414. make_simple_ref(list,tmpref);
  1415. check_register_size(size,reg);
  1416. case op of
  1417. OP_NEG,OP_NOT,OP_IMUL:
  1418. begin
  1419. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  1420. end;
  1421. OP_MUL,OP_DIV,OP_IDIV:
  1422. { special stuff, needs separate handling inside code }
  1423. { generator }
  1424. internalerror(200109239);
  1425. else
  1426. begin
  1427. reg := makeregsize(list,reg,size);
  1428. list.concat(taicpu.op_ref_reg(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref,reg));
  1429. end;
  1430. end;
  1431. end;
  1432. procedure tcgx86.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  1433. var
  1434. tmpref : treference;
  1435. begin
  1436. tmpref:=ref;
  1437. make_simple_ref(list,tmpref);
  1438. check_register_size(size,reg);
  1439. case op of
  1440. OP_NEG,OP_NOT:
  1441. begin
  1442. if reg<>NR_NO then
  1443. internalerror(200109237);
  1444. list.concat(taicpu.op_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref));
  1445. end;
  1446. OP_IMUL:
  1447. begin
  1448. { this one needs a load/imul/store, which is the default }
  1449. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  1450. end;
  1451. OP_MUL,OP_DIV,OP_IDIV:
  1452. { special stuff, needs separate handling inside code }
  1453. { generator }
  1454. internalerror(200109238);
  1455. else
  1456. begin
  1457. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],reg,tmpref));
  1458. end;
  1459. end;
  1460. end;
  1461. {*************** compare instructructions ****************}
  1462. procedure tcgx86.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  1463. l : tasmlabel);
  1464. {$ifdef x86_64}
  1465. var
  1466. tmpreg : tregister;
  1467. {$endif x86_64}
  1468. begin
  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_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1477. exit;
  1478. end;
  1479. {$endif x86_64}
  1480. if (a = 0) then
  1481. list.concat(taicpu.op_reg_reg(A_TEST,tcgsize2opsize[size],reg,reg))
  1482. else
  1483. list.concat(taicpu.op_const_reg(A_CMP,tcgsize2opsize[size],a,reg));
  1484. a_jmp_cond(list,cmp_op,l);
  1485. end;
  1486. procedure tcgx86.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;const ref : treference;
  1487. l : tasmlabel);
  1488. var
  1489. {$ifdef x86_64}
  1490. tmpreg : tregister;
  1491. {$endif x86_64}
  1492. tmpref : treference;
  1493. begin
  1494. tmpref:=ref;
  1495. make_simple_ref(list,tmpref);
  1496. {$ifdef x86_64}
  1497. { x86_64 only supports signed 32 bits constants directly }
  1498. if (size in [OS_S64,OS_64]) and
  1499. ((a<low(longint)) or (a>high(longint))) then
  1500. begin
  1501. tmpreg:=getintregister(list,size);
  1502. a_load_const_reg(list,size,a,tmpreg);
  1503. a_cmp_reg_ref_label(list,size,cmp_op,tmpreg,tmpref,l);
  1504. exit;
  1505. end;
  1506. {$endif x86_64}
  1507. list.concat(taicpu.op_const_ref(A_CMP,TCgSize2OpSize[size],a,tmpref));
  1508. a_jmp_cond(list,cmp_op,l);
  1509. end;
  1510. procedure tcgx86.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;
  1511. reg1,reg2 : tregister;l : tasmlabel);
  1512. begin
  1513. check_register_size(size,reg1);
  1514. check_register_size(size,reg2);
  1515. list.concat(taicpu.op_reg_reg(A_CMP,TCgSize2OpSize[size],reg1,reg2));
  1516. a_jmp_cond(list,cmp_op,l);
  1517. end;
  1518. procedure tcgx86.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister;l : tasmlabel);
  1519. var
  1520. tmpref : treference;
  1521. begin
  1522. tmpref:=ref;
  1523. make_simple_ref(list,tmpref);
  1524. check_register_size(size,reg);
  1525. list.concat(taicpu.op_ref_reg(A_CMP,TCgSize2OpSize[size],tmpref,reg));
  1526. a_jmp_cond(list,cmp_op,l);
  1527. end;
  1528. procedure tcgx86.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister;const ref: treference; l : tasmlabel);
  1529. var
  1530. tmpref : treference;
  1531. begin
  1532. tmpref:=ref;
  1533. make_simple_ref(list,tmpref);
  1534. check_register_size(size,reg);
  1535. list.concat(taicpu.op_reg_ref(A_CMP,TCgSize2OpSize[size],reg,tmpref));
  1536. a_jmp_cond(list,cmp_op,l);
  1537. end;
  1538. procedure tcgx86.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1539. var
  1540. ai : taicpu;
  1541. begin
  1542. if cond=OC_None then
  1543. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1544. else
  1545. begin
  1546. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  1547. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1548. end;
  1549. ai.is_jmp:=true;
  1550. list.concat(ai);
  1551. end;
  1552. procedure tcgx86.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1553. var
  1554. ai : taicpu;
  1555. begin
  1556. ai := Taicpu.op_sym(A_Jcc,S_NO,l);
  1557. ai.SetCondition(flags_to_cond(f));
  1558. ai.is_jmp := true;
  1559. list.concat(ai);
  1560. end;
  1561. procedure tcgx86.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  1562. var
  1563. ai : taicpu;
  1564. hreg : tregister;
  1565. begin
  1566. hreg:=makeregsize(list,reg,OS_8);
  1567. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg);
  1568. ai.setcondition(flags_to_cond(f));
  1569. list.concat(ai);
  1570. if (reg<>hreg) then
  1571. a_load_reg_reg(list,OS_8,size,hreg,reg);
  1572. end;
  1573. procedure tcgx86.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference);
  1574. var
  1575. ai : taicpu;
  1576. tmpref : treference;
  1577. begin
  1578. tmpref:=ref;
  1579. make_simple_ref(list,tmpref);
  1580. if not(size in [OS_8,OS_S8]) then
  1581. a_load_const_ref(list,size,0,tmpref);
  1582. ai:=Taicpu.op_ref(A_SETcc,S_B,tmpref);
  1583. ai.setcondition(flags_to_cond(f));
  1584. list.concat(ai);
  1585. end;
  1586. { ************* concatcopy ************ }
  1587. procedure Tcgx86.g_concatcopy(list:TAsmList;const source,dest:Treference;len:aint);
  1588. const
  1589. {$ifdef cpu64bitalu}
  1590. REGCX=NR_RCX;
  1591. REGSI=NR_RSI;
  1592. REGDI=NR_RDI;
  1593. {$else cpu64bitalu}
  1594. REGCX=NR_ECX;
  1595. REGSI=NR_ESI;
  1596. REGDI=NR_EDI;
  1597. {$endif cpu64bitalu}
  1598. type copymode=(copy_move,copy_mmx,copy_string);
  1599. var srcref,dstref:Treference;
  1600. r,r0,r1,r2,r3:Tregister;
  1601. helpsize:aint;
  1602. copysize:byte;
  1603. cgsize:Tcgsize;
  1604. cm:copymode;
  1605. begin
  1606. cm:=copy_move;
  1607. helpsize:=3*sizeof(aword);
  1608. if cs_opt_size in current_settings.optimizerswitches then
  1609. helpsize:=2*sizeof(aword);
  1610. if (cs_mmx in current_settings.localswitches) and
  1611. not(pi_uses_fpu in current_procinfo.flags) and
  1612. ((len=8) or (len=16) or (len=24) or (len=32)) then
  1613. cm:=copy_mmx;
  1614. if (len>helpsize) then
  1615. cm:=copy_string;
  1616. if (cs_opt_size in current_settings.optimizerswitches) and
  1617. not((len<=16) and (cm=copy_mmx)) then
  1618. cm:=copy_string;
  1619. if (source.segment<>NR_NO) or
  1620. (dest.segment<>NR_NO) then
  1621. cm:=copy_string;
  1622. case cm of
  1623. copy_move:
  1624. begin
  1625. dstref:=dest;
  1626. srcref:=source;
  1627. copysize:=sizeof(aint);
  1628. cgsize:=int_cgsize(copysize);
  1629. while len<>0 do
  1630. begin
  1631. if len<2 then
  1632. begin
  1633. copysize:=1;
  1634. cgsize:=OS_8;
  1635. end
  1636. else if len<4 then
  1637. begin
  1638. copysize:=2;
  1639. cgsize:=OS_16;
  1640. end
  1641. else if len<8 then
  1642. begin
  1643. copysize:=4;
  1644. cgsize:=OS_32;
  1645. end
  1646. {$ifdef cpu64bitalu}
  1647. else if len<16 then
  1648. begin
  1649. copysize:=8;
  1650. cgsize:=OS_64;
  1651. end
  1652. {$endif}
  1653. ;
  1654. dec(len,copysize);
  1655. r:=getintregister(list,cgsize);
  1656. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  1657. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  1658. inc(srcref.offset,copysize);
  1659. inc(dstref.offset,copysize);
  1660. end;
  1661. end;
  1662. copy_mmx:
  1663. begin
  1664. dstref:=dest;
  1665. srcref:=source;
  1666. r0:=getmmxregister(list);
  1667. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r0,nil);
  1668. if len>=16 then
  1669. begin
  1670. inc(srcref.offset,8);
  1671. r1:=getmmxregister(list);
  1672. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r1,nil);
  1673. end;
  1674. if len>=24 then
  1675. begin
  1676. inc(srcref.offset,8);
  1677. r2:=getmmxregister(list);
  1678. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r2,nil);
  1679. end;
  1680. if len>=32 then
  1681. begin
  1682. inc(srcref.offset,8);
  1683. r3:=getmmxregister(list);
  1684. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  1685. end;
  1686. a_loadmm_reg_ref(list,OS_M64,OS_M64,r0,dstref,nil);
  1687. if len>=16 then
  1688. begin
  1689. inc(dstref.offset,8);
  1690. a_loadmm_reg_ref(list,OS_M64,OS_M64,r1,dstref,nil);
  1691. end;
  1692. if len>=24 then
  1693. begin
  1694. inc(dstref.offset,8);
  1695. a_loadmm_reg_ref(list,OS_M64,OS_M64,r2,dstref,nil);
  1696. end;
  1697. if len>=32 then
  1698. begin
  1699. inc(dstref.offset,8);
  1700. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  1701. end;
  1702. end
  1703. else {copy_string, should be a good fallback in case of unhandled}
  1704. begin
  1705. getcpuregister(list,REGDI);
  1706. if (dest.segment=NR_NO) then
  1707. a_loadaddr_ref_reg(list,dest,REGDI)
  1708. else
  1709. begin
  1710. dstref:=dest;
  1711. dstref.segment:=NR_NO;
  1712. a_loadaddr_ref_reg(list,dstref,REGDI);
  1713. list.concat(taicpu.op_reg(A_PUSH,S_L,NR_ES));
  1714. list.concat(taicpu.op_reg(A_PUSH,S_L,dest.segment));
  1715. list.concat(taicpu.op_reg(A_POP,S_L,NR_ES));
  1716. end;
  1717. getcpuregister(list,REGSI);
  1718. if (source.segment=NR_NO) then
  1719. a_loadaddr_ref_reg(list,source,REGSI)
  1720. else
  1721. begin
  1722. srcref:=source;
  1723. srcref.segment:=NR_NO;
  1724. a_loadaddr_ref_reg(list,srcref,REGSI);
  1725. list.concat(taicpu.op_reg(A_PUSH,S_L,NR_DS));
  1726. list.concat(taicpu.op_reg(A_PUSH,S_L,source.segment));
  1727. list.concat(taicpu.op_reg(A_POP,S_L,NR_DS));
  1728. end;
  1729. getcpuregister(list,REGCX);
  1730. {$ifdef i386}
  1731. list.concat(Taicpu.op_none(A_CLD,S_NO));
  1732. {$endif i386}
  1733. if (cs_opt_size in current_settings.optimizerswitches) and
  1734. (len>sizeof(aint)+(sizeof(aint) div 2)) then
  1735. begin
  1736. a_load_const_reg(list,OS_INT,len,REGCX);
  1737. list.concat(Taicpu.op_none(A_REP,S_NO));
  1738. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  1739. end
  1740. else
  1741. begin
  1742. helpsize:=len div sizeof(aint);
  1743. len:=len mod sizeof(aint);
  1744. if helpsize>1 then
  1745. begin
  1746. a_load_const_reg(list,OS_INT,helpsize,REGCX);
  1747. list.concat(Taicpu.op_none(A_REP,S_NO));
  1748. end;
  1749. if helpsize>0 then
  1750. begin
  1751. {$ifdef cpu64bitalu}
  1752. list.concat(Taicpu.op_none(A_MOVSQ,S_NO))
  1753. {$else}
  1754. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  1755. {$endif cpu64bitalu}
  1756. end;
  1757. if len>=4 then
  1758. begin
  1759. dec(len,4);
  1760. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  1761. end;
  1762. if len>=2 then
  1763. begin
  1764. dec(len,2);
  1765. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  1766. end;
  1767. if len=1 then
  1768. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  1769. end;
  1770. ungetcpuregister(list,REGCX);
  1771. ungetcpuregister(list,REGSI);
  1772. ungetcpuregister(list,REGDI);
  1773. if (source.segment<>NR_NO) then
  1774. list.concat(taicpu.op_reg(A_POP,S_L,NR_DS));
  1775. if (dest.segment<>NR_NO) then
  1776. list.concat(taicpu.op_reg(A_POP,S_L,NR_ES));
  1777. end;
  1778. end;
  1779. end;
  1780. {****************************************************************************
  1781. Entry/Exit Code Helpers
  1782. ****************************************************************************}
  1783. procedure tcgx86.g_profilecode(list : TAsmList);
  1784. var
  1785. pl : tasmlabel;
  1786. mcountprefix : String[4];
  1787. begin
  1788. case target_info.system of
  1789. {$ifndef NOTARGETWIN}
  1790. system_i386_win32,
  1791. {$endif}
  1792. system_i386_freebsd,
  1793. system_i386_netbsd,
  1794. // system_i386_openbsd,
  1795. system_i386_wdosx :
  1796. begin
  1797. Case target_info.system Of
  1798. system_i386_freebsd : mcountprefix:='.';
  1799. system_i386_netbsd : mcountprefix:='__';
  1800. // system_i386_openbsd : mcountprefix:='.';
  1801. else
  1802. mcountPrefix:='';
  1803. end;
  1804. current_asmdata.getaddrlabel(pl);
  1805. new_section(list,sec_data,lower(current_procinfo.procdef.mangledname),sizeof(pint));
  1806. list.concat(Tai_label.Create(pl));
  1807. list.concat(Tai_const.Create_32bit(0));
  1808. new_section(list,sec_code,lower(current_procinfo.procdef.mangledname),0);
  1809. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  1810. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,NR_EDX));
  1811. a_call_name(list,target_info.Cprefix+mcountprefix+'mcount',false);
  1812. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDX));
  1813. end;
  1814. system_i386_linux:
  1815. a_call_name(list,target_info.Cprefix+'mcount',false);
  1816. system_i386_go32v2,system_i386_watcom:
  1817. begin
  1818. a_call_name(list,'MCOUNT',false);
  1819. end;
  1820. system_x86_64_linux,
  1821. system_x86_64_darwin:
  1822. begin
  1823. a_call_name(list,'mcount',false);
  1824. end;
  1825. end;
  1826. end;
  1827. procedure tcgx86.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  1828. {$ifdef x86}
  1829. {$ifndef NOTARGETWIN}
  1830. var
  1831. href : treference;
  1832. i : integer;
  1833. again : tasmlabel;
  1834. {$endif NOTARGETWIN}
  1835. {$endif x86}
  1836. begin
  1837. if localsize>0 then
  1838. begin
  1839. {$ifdef i386}
  1840. {$ifndef NOTARGETWIN}
  1841. { windows guards only a few pages for stack growing,
  1842. so we have to access every page first }
  1843. if (target_info.system in [system_i386_win32,system_i386_wince]) and
  1844. (localsize>=winstackpagesize) then
  1845. begin
  1846. if localsize div winstackpagesize<=5 then
  1847. begin
  1848. list.concat(Taicpu.Op_const_reg(A_SUB,S_L,localsize-4,NR_ESP));
  1849. for i:=1 to localsize div winstackpagesize do
  1850. begin
  1851. reference_reset_base(href,NR_ESP,localsize-i*winstackpagesize,4);
  1852. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1853. end;
  1854. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  1855. end
  1856. else
  1857. begin
  1858. current_asmdata.getjumplabel(again);
  1859. getcpuregister(list,NR_EDI);
  1860. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EDI));
  1861. list.concat(Taicpu.op_const_reg(A_MOV,S_L,localsize div winstackpagesize,NR_EDI));
  1862. a_label(list,again);
  1863. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,NR_ESP));
  1864. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  1865. list.concat(Taicpu.op_reg(A_DEC,S_L,NR_EDI));
  1866. a_jmp_cond(list,OC_NE,again);
  1867. list.concat(Taicpu.op_const_reg(A_SUB,S_L,localsize mod winstackpagesize - 4,NR_ESP));
  1868. reference_reset_base(href,NR_ESP,localsize-4,4);
  1869. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,href,NR_EDI));
  1870. ungetcpuregister(list,NR_EDI);
  1871. end
  1872. end
  1873. else
  1874. {$endif NOTARGETWIN}
  1875. {$endif i386}
  1876. {$ifdef x86_64}
  1877. {$ifndef NOTARGETWIN}
  1878. { windows guards only a few pages for stack growing,
  1879. so we have to access every page first }
  1880. if (target_info.system=system_x86_64_win64) and
  1881. (localsize>=winstackpagesize) then
  1882. begin
  1883. if localsize div winstackpagesize<=5 then
  1884. begin
  1885. list.concat(Taicpu.Op_const_reg(A_SUB,S_Q,localsize,NR_RSP));
  1886. for i:=1 to localsize div winstackpagesize do
  1887. begin
  1888. reference_reset_base(href,NR_RSP,localsize-i*winstackpagesize+4,4);
  1889. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1890. end;
  1891. reference_reset_base(href,NR_RSP,0,4);
  1892. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1893. end
  1894. else
  1895. begin
  1896. current_asmdata.getjumplabel(again);
  1897. getcpuregister(list,NR_R10);
  1898. list.concat(Taicpu.op_const_reg(A_MOV,S_Q,localsize div winstackpagesize,NR_R10));
  1899. a_label(list,again);
  1900. list.concat(Taicpu.op_const_reg(A_SUB,S_Q,winstackpagesize,NR_RSP));
  1901. reference_reset_base(href,NR_RSP,0,4);
  1902. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1903. list.concat(Taicpu.op_reg(A_DEC,S_Q,NR_R10));
  1904. a_jmp_cond(list,OC_NE,again);
  1905. list.concat(Taicpu.op_const_reg(A_SUB,S_Q,localsize mod winstackpagesize,NR_RSP));
  1906. ungetcpuregister(list,NR_R10);
  1907. end
  1908. end
  1909. else
  1910. {$endif NOTARGETWIN}
  1911. {$endif x86_64}
  1912. list.concat(Taicpu.Op_const_reg(A_SUB,tcgsize2opsize[OS_ADDR],localsize,NR_STACK_POINTER_REG));
  1913. end;
  1914. end;
  1915. procedure tcgx86.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  1916. var
  1917. stackmisalignment: longint;
  1918. begin
  1919. {$ifdef i386}
  1920. { interrupt support for i386 }
  1921. if (po_interrupt in current_procinfo.procdef.procoptions) and
  1922. { this messes up stack alignment }
  1923. (target_info.system <> system_i386_darwin) then
  1924. begin
  1925. { .... also the segment registers }
  1926. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_GS));
  1927. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_FS));
  1928. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  1929. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  1930. { save the registers of an interrupt procedure }
  1931. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDI));
  1932. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ESI));
  1933. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  1934. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ECX));
  1935. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EBX));
  1936. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EAX));
  1937. end;
  1938. {$endif i386}
  1939. { save old framepointer }
  1940. if not nostackframe then
  1941. begin
  1942. { return address }
  1943. stackmisalignment := sizeof(pint);
  1944. list.concat(tai_regalloc.alloc(current_procinfo.framepointer,nil));
  1945. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  1946. CGmessage(cg_d_stackframe_omited)
  1947. else
  1948. begin
  1949. { push <frame_pointer> }
  1950. inc(stackmisalignment,sizeof(pint));
  1951. include(rg[R_INTREGISTER].preserved_by_proc,RS_FRAME_POINTER_REG);
  1952. list.concat(Taicpu.op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],NR_FRAME_POINTER_REG));
  1953. { Return address and FP are both on stack }
  1954. current_asmdata.asmcfi.cfa_def_cfa_offset(list,2*sizeof(pint));
  1955. current_asmdata.asmcfi.cfa_offset(list,NR_FRAME_POINTER_REG,-(2*sizeof(pint)));
  1956. list.concat(Taicpu.op_reg_reg(A_MOV,tcgsize2opsize[OS_ADDR],NR_STACK_POINTER_REG,NR_FRAME_POINTER_REG));
  1957. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_FRAME_POINTER_REG);
  1958. end;
  1959. { allocate stackframe space }
  1960. if (localsize<>0) or
  1961. ((target_info.system in systems_need_16_byte_stack_alignment) and
  1962. (stackmisalignment <> 0) and
  1963. ((pi_do_call in current_procinfo.flags) or
  1964. (po_assembler in current_procinfo.procdef.procoptions))) then
  1965. begin
  1966. if (target_info.system in systems_need_16_byte_stack_alignment) then
  1967. localsize := align(localsize+stackmisalignment,16)-stackmisalignment;
  1968. cg.g_stackpointer_alloc(list,localsize);
  1969. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  1970. current_asmdata.asmcfi.cfa_def_cfa_offset(list,localsize+sizeof(pint));
  1971. end;
  1972. end;
  1973. end;
  1974. { produces if necessary overflowcode }
  1975. procedure tcgx86.g_overflowcheck(list: TAsmList; const l:tlocation;def:tdef);
  1976. var
  1977. hl : tasmlabel;
  1978. ai : taicpu;
  1979. cond : TAsmCond;
  1980. begin
  1981. if not(cs_check_overflow in current_settings.localswitches) then
  1982. exit;
  1983. current_asmdata.getjumplabel(hl);
  1984. if not ((def.typ=pointerdef) or
  1985. ((def.typ=orddef) and
  1986. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,pasbool]))) then
  1987. cond:=C_NO
  1988. else
  1989. cond:=C_NB;
  1990. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl);
  1991. ai.SetCondition(cond);
  1992. ai.is_jmp:=true;
  1993. list.concat(ai);
  1994. a_call_name(list,'FPC_OVERFLOW',false);
  1995. a_label(list,hl);
  1996. end;
  1997. procedure tcgx86.g_external_wrapper(list: TAsmList; procdef: tprocdef; const externalname: string);
  1998. var
  1999. ref : treference;
  2000. sym : tasmsymbol;
  2001. begin
  2002. if (target_info.system=system_i386_darwin) then
  2003. begin
  2004. { a_jmp_name jumps to a stub which is always pic-safe on darwin }
  2005. inherited g_external_wrapper(list,procdef,externalname);
  2006. exit;
  2007. end;
  2008. sym:=current_asmdata.RefAsmSymbol(externalname);
  2009. reference_reset_symbol(ref,sym,0,sizeof(pint));
  2010. { create pic'ed? }
  2011. if (cs_create_pic in current_settings.moduleswitches) and
  2012. { darwin/x86_64's assembler doesn't want @PLT after call symbols }
  2013. (target_info.system<>system_x86_64_darwin) then
  2014. ref.refaddr:=addr_pic
  2015. else
  2016. ref.refaddr:=addr_full;
  2017. list.concat(taicpu.op_ref(A_JMP,S_NO,ref));
  2018. end;
  2019. end.