cgx86.pas 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273
  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;
  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. end;
  1620. { ************* concatcopy ************ }
  1621. procedure Tcgx86.g_concatcopy(list:TAsmList;const source,dest:Treference;len:tcgint);
  1622. const
  1623. {$ifdef cpu64bitalu}
  1624. REGCX=NR_RCX;
  1625. REGSI=NR_RSI;
  1626. REGDI=NR_RDI;
  1627. {$else cpu64bitalu}
  1628. REGCX=NR_ECX;
  1629. REGSI=NR_ESI;
  1630. REGDI=NR_EDI;
  1631. {$endif cpu64bitalu}
  1632. type copymode=(copy_move,copy_mmx,copy_string);
  1633. var srcref,dstref:Treference;
  1634. r,r0,r1,r2,r3:Tregister;
  1635. helpsize:tcgint;
  1636. copysize:byte;
  1637. cgsize:Tcgsize;
  1638. cm:copymode;
  1639. begin
  1640. cm:=copy_move;
  1641. helpsize:=3*sizeof(aword);
  1642. if cs_opt_size in current_settings.optimizerswitches then
  1643. helpsize:=2*sizeof(aword);
  1644. if (cs_mmx in current_settings.localswitches) and
  1645. not(pi_uses_fpu in current_procinfo.flags) and
  1646. ((len=8) or (len=16) or (len=24) or (len=32)) then
  1647. cm:=copy_mmx;
  1648. if (len>helpsize) then
  1649. cm:=copy_string;
  1650. if (cs_opt_size in current_settings.optimizerswitches) and
  1651. not((len<=16) and (cm=copy_mmx)) and
  1652. not(len in [1,2,4{$ifdef x86_64},8{$endif x86_64}]) then
  1653. cm:=copy_string;
  1654. if (source.segment<>NR_NO) or
  1655. (dest.segment<>NR_NO) then
  1656. cm:=copy_string;
  1657. case cm of
  1658. copy_move:
  1659. begin
  1660. dstref:=dest;
  1661. srcref:=source;
  1662. copysize:=sizeof(aint);
  1663. cgsize:=int_cgsize(copysize);
  1664. while len<>0 do
  1665. begin
  1666. if len<2 then
  1667. begin
  1668. copysize:=1;
  1669. cgsize:=OS_8;
  1670. end
  1671. else if len<4 then
  1672. begin
  1673. copysize:=2;
  1674. cgsize:=OS_16;
  1675. end
  1676. else if len<8 then
  1677. begin
  1678. copysize:=4;
  1679. cgsize:=OS_32;
  1680. end
  1681. {$ifdef cpu64bitalu}
  1682. else if len<16 then
  1683. begin
  1684. copysize:=8;
  1685. cgsize:=OS_64;
  1686. end
  1687. {$endif}
  1688. ;
  1689. dec(len,copysize);
  1690. r:=getintregister(list,cgsize);
  1691. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  1692. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  1693. inc(srcref.offset,copysize);
  1694. inc(dstref.offset,copysize);
  1695. end;
  1696. end;
  1697. copy_mmx:
  1698. begin
  1699. dstref:=dest;
  1700. srcref:=source;
  1701. r0:=getmmxregister(list);
  1702. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r0,nil);
  1703. if len>=16 then
  1704. begin
  1705. inc(srcref.offset,8);
  1706. r1:=getmmxregister(list);
  1707. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r1,nil);
  1708. end;
  1709. if len>=24 then
  1710. begin
  1711. inc(srcref.offset,8);
  1712. r2:=getmmxregister(list);
  1713. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r2,nil);
  1714. end;
  1715. if len>=32 then
  1716. begin
  1717. inc(srcref.offset,8);
  1718. r3:=getmmxregister(list);
  1719. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  1720. end;
  1721. a_loadmm_reg_ref(list,OS_M64,OS_M64,r0,dstref,nil);
  1722. if len>=16 then
  1723. begin
  1724. inc(dstref.offset,8);
  1725. a_loadmm_reg_ref(list,OS_M64,OS_M64,r1,dstref,nil);
  1726. end;
  1727. if len>=24 then
  1728. begin
  1729. inc(dstref.offset,8);
  1730. a_loadmm_reg_ref(list,OS_M64,OS_M64,r2,dstref,nil);
  1731. end;
  1732. if len>=32 then
  1733. begin
  1734. inc(dstref.offset,8);
  1735. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  1736. end;
  1737. end
  1738. else {copy_string, should be a good fallback in case of unhandled}
  1739. begin
  1740. getcpuregister(list,REGDI);
  1741. if (dest.segment=NR_NO) then
  1742. a_loadaddr_ref_reg(list,dest,REGDI)
  1743. else
  1744. begin
  1745. dstref:=dest;
  1746. dstref.segment:=NR_NO;
  1747. a_loadaddr_ref_reg(list,dstref,REGDI);
  1748. list.concat(taicpu.op_reg(A_PUSH,S_L,NR_ES));
  1749. list.concat(taicpu.op_reg(A_PUSH,S_L,dest.segment));
  1750. list.concat(taicpu.op_reg(A_POP,S_L,NR_ES));
  1751. end;
  1752. getcpuregister(list,REGSI);
  1753. if (source.segment=NR_NO) then
  1754. a_loadaddr_ref_reg(list,source,REGSI)
  1755. else
  1756. begin
  1757. srcref:=source;
  1758. srcref.segment:=NR_NO;
  1759. a_loadaddr_ref_reg(list,srcref,REGSI);
  1760. list.concat(taicpu.op_reg(A_PUSH,S_L,NR_DS));
  1761. list.concat(taicpu.op_reg(A_PUSH,S_L,source.segment));
  1762. list.concat(taicpu.op_reg(A_POP,S_L,NR_DS));
  1763. end;
  1764. getcpuregister(list,REGCX);
  1765. {$ifdef i386}
  1766. list.concat(Taicpu.op_none(A_CLD,S_NO));
  1767. {$endif i386}
  1768. if (cs_opt_size in current_settings.optimizerswitches) and
  1769. (len>sizeof(aint)+(sizeof(aint) div 2)) then
  1770. begin
  1771. a_load_const_reg(list,OS_INT,len,REGCX);
  1772. list.concat(Taicpu.op_none(A_REP,S_NO));
  1773. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  1774. end
  1775. else
  1776. begin
  1777. helpsize:=len div sizeof(aint);
  1778. len:=len mod sizeof(aint);
  1779. if helpsize>1 then
  1780. begin
  1781. a_load_const_reg(list,OS_INT,helpsize,REGCX);
  1782. list.concat(Taicpu.op_none(A_REP,S_NO));
  1783. end;
  1784. if helpsize>0 then
  1785. begin
  1786. {$ifdef cpu64bitalu}
  1787. list.concat(Taicpu.op_none(A_MOVSQ,S_NO))
  1788. {$else}
  1789. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  1790. {$endif cpu64bitalu}
  1791. end;
  1792. if len>=4 then
  1793. begin
  1794. dec(len,4);
  1795. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  1796. end;
  1797. if len>=2 then
  1798. begin
  1799. dec(len,2);
  1800. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  1801. end;
  1802. if len=1 then
  1803. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  1804. end;
  1805. ungetcpuregister(list,REGCX);
  1806. ungetcpuregister(list,REGSI);
  1807. ungetcpuregister(list,REGDI);
  1808. if (source.segment<>NR_NO) then
  1809. list.concat(taicpu.op_reg(A_POP,S_L,NR_DS));
  1810. if (dest.segment<>NR_NO) then
  1811. list.concat(taicpu.op_reg(A_POP,S_L,NR_ES));
  1812. end;
  1813. end;
  1814. end;
  1815. {****************************************************************************
  1816. Entry/Exit Code Helpers
  1817. ****************************************************************************}
  1818. procedure tcgx86.g_profilecode(list : TAsmList);
  1819. var
  1820. pl : tasmlabel;
  1821. mcountprefix : String[4];
  1822. begin
  1823. case target_info.system of
  1824. {$ifndef NOTARGETWIN}
  1825. system_i386_win32,
  1826. {$endif}
  1827. system_i386_freebsd,
  1828. system_i386_netbsd,
  1829. // system_i386_openbsd,
  1830. system_i386_wdosx :
  1831. begin
  1832. Case target_info.system Of
  1833. system_i386_freebsd : mcountprefix:='.';
  1834. system_i386_netbsd : mcountprefix:='__';
  1835. // system_i386_openbsd : mcountprefix:='.';
  1836. else
  1837. mcountPrefix:='';
  1838. end;
  1839. current_asmdata.getaddrlabel(pl);
  1840. new_section(list,sec_data,lower(current_procinfo.procdef.mangledname),sizeof(pint));
  1841. list.concat(Tai_label.Create(pl));
  1842. list.concat(Tai_const.Create_32bit(0));
  1843. new_section(list,sec_code,lower(current_procinfo.procdef.mangledname),0);
  1844. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  1845. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,NR_EDX));
  1846. a_call_name(list,target_info.Cprefix+mcountprefix+'mcount',false);
  1847. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDX));
  1848. end;
  1849. system_i386_linux:
  1850. a_call_name(list,target_info.Cprefix+'mcount',false);
  1851. system_i386_go32v2,system_i386_watcom:
  1852. begin
  1853. a_call_name(list,'MCOUNT',false);
  1854. end;
  1855. system_x86_64_linux,
  1856. system_x86_64_darwin:
  1857. begin
  1858. a_call_name(list,'mcount',false);
  1859. end;
  1860. end;
  1861. end;
  1862. procedure tcgx86.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  1863. {$ifdef x86}
  1864. {$ifndef NOTARGETWIN}
  1865. var
  1866. href : treference;
  1867. i : integer;
  1868. again : tasmlabel;
  1869. {$endif NOTARGETWIN}
  1870. {$endif x86}
  1871. begin
  1872. if localsize>0 then
  1873. begin
  1874. {$ifdef i386}
  1875. {$ifndef NOTARGETWIN}
  1876. { windows guards only a few pages for stack growing,
  1877. so we have to access every page first }
  1878. if (target_info.system in [system_i386_win32,system_i386_wince]) and
  1879. (localsize>=winstackpagesize) then
  1880. begin
  1881. if localsize div winstackpagesize<=5 then
  1882. begin
  1883. list.concat(Taicpu.Op_const_reg(A_SUB,S_L,localsize-4,NR_ESP));
  1884. for i:=1 to localsize div winstackpagesize do
  1885. begin
  1886. reference_reset_base(href,NR_ESP,localsize-i*winstackpagesize,4);
  1887. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1888. end;
  1889. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  1890. end
  1891. else
  1892. begin
  1893. current_asmdata.getjumplabel(again);
  1894. getcpuregister(list,NR_EDI);
  1895. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EDI));
  1896. list.concat(Taicpu.op_const_reg(A_MOV,S_L,localsize div winstackpagesize,NR_EDI));
  1897. a_label(list,again);
  1898. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,NR_ESP));
  1899. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  1900. list.concat(Taicpu.op_reg(A_DEC,S_L,NR_EDI));
  1901. a_jmp_cond(list,OC_NE,again);
  1902. list.concat(Taicpu.op_const_reg(A_SUB,S_L,localsize mod winstackpagesize - 4,NR_ESP));
  1903. reference_reset_base(href,NR_ESP,localsize-4,4);
  1904. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,href,NR_EDI));
  1905. ungetcpuregister(list,NR_EDI);
  1906. end
  1907. end
  1908. else
  1909. {$endif NOTARGETWIN}
  1910. {$endif i386}
  1911. {$ifdef x86_64}
  1912. {$ifndef NOTARGETWIN}
  1913. { windows guards only a few pages for stack growing,
  1914. so we have to access every page first }
  1915. if (target_info.system=system_x86_64_win64) and
  1916. (localsize>=winstackpagesize) then
  1917. begin
  1918. if localsize div winstackpagesize<=5 then
  1919. begin
  1920. list.concat(Taicpu.Op_const_reg(A_SUB,S_Q,localsize,NR_RSP));
  1921. for i:=1 to localsize div winstackpagesize do
  1922. begin
  1923. reference_reset_base(href,NR_RSP,localsize-i*winstackpagesize+4,4);
  1924. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1925. end;
  1926. reference_reset_base(href,NR_RSP,0,4);
  1927. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1928. end
  1929. else
  1930. begin
  1931. current_asmdata.getjumplabel(again);
  1932. getcpuregister(list,NR_R10);
  1933. list.concat(Taicpu.op_const_reg(A_MOV,S_Q,localsize div winstackpagesize,NR_R10));
  1934. a_label(list,again);
  1935. list.concat(Taicpu.op_const_reg(A_SUB,S_Q,winstackpagesize,NR_RSP));
  1936. reference_reset_base(href,NR_RSP,0,4);
  1937. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1938. list.concat(Taicpu.op_reg(A_DEC,S_Q,NR_R10));
  1939. a_jmp_cond(list,OC_NE,again);
  1940. list.concat(Taicpu.op_const_reg(A_SUB,S_Q,localsize mod winstackpagesize,NR_RSP));
  1941. ungetcpuregister(list,NR_R10);
  1942. end
  1943. end
  1944. else
  1945. {$endif NOTARGETWIN}
  1946. {$endif x86_64}
  1947. list.concat(Taicpu.Op_const_reg(A_SUB,tcgsize2opsize[OS_ADDR],localsize,NR_STACK_POINTER_REG));
  1948. end;
  1949. end;
  1950. procedure tcgx86.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  1951. var
  1952. stackmisalignment: longint;
  1953. begin
  1954. {$ifdef i386}
  1955. { interrupt support for i386 }
  1956. if (po_interrupt in current_procinfo.procdef.procoptions) and
  1957. { this messes up stack alignment }
  1958. not(target_info.system in [system_i386_darwin,system_i386_iphonesim]) then
  1959. begin
  1960. { .... also the segment registers }
  1961. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_GS));
  1962. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_FS));
  1963. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  1964. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  1965. { save the registers of an interrupt procedure }
  1966. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDI));
  1967. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ESI));
  1968. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  1969. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ECX));
  1970. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EBX));
  1971. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EAX));
  1972. end;
  1973. {$endif i386}
  1974. { save old framepointer }
  1975. if not nostackframe then
  1976. begin
  1977. { return address }
  1978. stackmisalignment := sizeof(pint);
  1979. list.concat(tai_regalloc.alloc(current_procinfo.framepointer,nil));
  1980. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  1981. CGmessage(cg_d_stackframe_omited)
  1982. else
  1983. begin
  1984. { push <frame_pointer> }
  1985. inc(stackmisalignment,sizeof(pint));
  1986. include(rg[R_INTREGISTER].preserved_by_proc,RS_FRAME_POINTER_REG);
  1987. list.concat(Taicpu.op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],NR_FRAME_POINTER_REG));
  1988. if (target_info.system=system_x86_64_win64) then
  1989. begin
  1990. list.concat(cai_seh_directive.create_reg(ash_pushreg,NR_FRAME_POINTER_REG));
  1991. include(current_procinfo.flags,pi_has_unwind_info);
  1992. end;
  1993. { Return address and FP are both on stack }
  1994. current_asmdata.asmcfi.cfa_def_cfa_offset(list,2*sizeof(pint));
  1995. current_asmdata.asmcfi.cfa_offset(list,NR_FRAME_POINTER_REG,-(2*sizeof(pint)));
  1996. list.concat(Taicpu.op_reg_reg(A_MOV,tcgsize2opsize[OS_ADDR],NR_STACK_POINTER_REG,NR_FRAME_POINTER_REG));
  1997. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_FRAME_POINTER_REG);
  1998. {
  1999. TODO: current framepointer handling is not compatible with Win64 at all:
  2000. Win64 expects FP to point to the top or into the middle of local area.
  2001. In FPC it points to the bottom, making it impossible to generate
  2002. UWOP_SET_FPREG unwind code if local area is > 240 bytes.
  2003. So for now pretend we never have a framepointer.
  2004. }
  2005. end;
  2006. { allocate stackframe space }
  2007. if (localsize<>0) or
  2008. ((target_info.system in systems_need_16_byte_stack_alignment) and
  2009. (stackmisalignment <> 0) and
  2010. ((pi_do_call in current_procinfo.flags) or
  2011. (po_assembler in current_procinfo.procdef.procoptions))) then
  2012. begin
  2013. if (target_info.system in systems_need_16_byte_stack_alignment) then
  2014. localsize := align(localsize+stackmisalignment,16)-stackmisalignment;
  2015. cg.g_stackpointer_alloc(list,localsize);
  2016. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  2017. current_asmdata.asmcfi.cfa_def_cfa_offset(list,localsize+sizeof(pint));
  2018. current_procinfo.final_localsize:=localsize;
  2019. if (target_info.system=system_x86_64_win64) then
  2020. begin
  2021. if localsize<>0 then
  2022. list.concat(cai_seh_directive.create_offset(ash_stackalloc,localsize));
  2023. include(current_procinfo.flags,pi_has_unwind_info);
  2024. end;
  2025. end;
  2026. end;
  2027. end;
  2028. { produces if necessary overflowcode }
  2029. procedure tcgx86.g_overflowcheck(list: TAsmList; const l:tlocation;def:tdef);
  2030. var
  2031. hl : tasmlabel;
  2032. ai : taicpu;
  2033. cond : TAsmCond;
  2034. begin
  2035. if not(cs_check_overflow in current_settings.localswitches) then
  2036. exit;
  2037. current_asmdata.getjumplabel(hl);
  2038. if not ((def.typ=pointerdef) or
  2039. ((def.typ=orddef) and
  2040. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  2041. pasbool8,pasbool16,pasbool32,pasbool64]))) then
  2042. cond:=C_NO
  2043. else
  2044. cond:=C_NB;
  2045. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl);
  2046. ai.SetCondition(cond);
  2047. ai.is_jmp:=true;
  2048. list.concat(ai);
  2049. a_call_name(list,'FPC_OVERFLOW',false);
  2050. a_label(list,hl);
  2051. end;
  2052. procedure tcgx86.g_external_wrapper(list: TAsmList; procdef: tprocdef; const externalname: string);
  2053. var
  2054. ref : treference;
  2055. sym : tasmsymbol;
  2056. begin
  2057. if (target_info.system = system_i386_darwin) then
  2058. begin
  2059. { a_jmp_name jumps to a stub which is always pic-safe on darwin }
  2060. inherited g_external_wrapper(list,procdef,externalname);
  2061. exit;
  2062. end;
  2063. sym:=current_asmdata.RefAsmSymbol(externalname);
  2064. reference_reset_symbol(ref,sym,0,sizeof(pint));
  2065. { create pic'ed? }
  2066. if (cs_create_pic in current_settings.moduleswitches) and
  2067. { darwin/x86_64's assembler doesn't want @PLT after call symbols }
  2068. not(target_info.system in [system_x86_64_darwin,system_i386_iphonesim]) then
  2069. ref.refaddr:=addr_pic
  2070. else
  2071. ref.refaddr:=addr_full;
  2072. list.concat(taicpu.op_ref(A_JMP,S_NO,ref));
  2073. end;
  2074. end.