cgx86.pas 85 KB

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