cgx86.pas 89 KB

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