cgx86.pas 80 KB

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