cgx86.pas 80 KB

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