cgx86.pas 82 KB

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