cgx86.pas 84 KB

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