cgx86.pas 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937
  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;
  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);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; size: tcgsize; reg1, reg2: tregister); override;
  61. procedure a_loadfpu_ref_reg(list: TAsmList; size: tcgsize; const ref: treference; reg: tregister); override;
  62. procedure a_loadfpu_reg_ref(list: TAsmList; size: 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_releasevaluepara_openarray(list : TAsmList;const l:tlocation);override;
  85. procedure g_profilecode(list : TAsmList);override;
  86. procedure g_stackpointer_alloc(list : TAsmList;localsize : longint);override;
  87. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  88. procedure g_overflowcheck(list: TAsmList; const l:tlocation;def:tdef);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): 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. symdef,defutil,paramgr,procinfo,
  123. fmodule;
  124. const
  125. TOpCG2AsmOp: Array[topcg] of TAsmOp = (A_NONE,A_MOV,A_ADD,A_AND,A_DIV,
  126. A_IDIV,A_IMUL,A_MUL,A_NEG,A_NOT,A_OR,
  127. A_SAR,A_SHL,A_SHR,A_SUB,A_XOR);
  128. TOpCmp2AsmCond: Array[topcmp] of TAsmCond = (C_NONE,
  129. C_E,C_G,C_L,C_GE,C_LE,C_NE,C_BE,C_B,C_AE,C_A);
  130. procedure Tcgx86.done_register_allocators;
  131. begin
  132. rg[R_INTREGISTER].free;
  133. rg[R_MMREGISTER].free;
  134. rg[R_MMXREGISTER].free;
  135. rgfpu.free;
  136. inherited done_register_allocators;
  137. end;
  138. function Tcgx86.getfpuregister(list:TAsmList;size:Tcgsize):Tregister;
  139. begin
  140. result:=rgfpu.getregisterfpu(list);
  141. end;
  142. function Tcgx86.getmmxregister(list:TAsmList):Tregister;
  143. begin
  144. if not assigned(rg[R_MMXREGISTER]) then
  145. internalerror(2003121214);
  146. result:=rg[R_MMXREGISTER].getregister(list,R_SUBNONE);
  147. end;
  148. function Tcgx86.getmmregister(list:TAsmList;size:Tcgsize):Tregister;
  149. begin
  150. if not assigned(rg[R_MMREGISTER]) then
  151. internalerror(2003121234);
  152. case size of
  153. OS_F64:
  154. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMD);
  155. OS_F32:
  156. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMS);
  157. else
  158. internalerror(200506041);
  159. end;
  160. end;
  161. procedure Tcgx86.getcpuregister(list:TAsmList;r:Tregister);
  162. begin
  163. if getregtype(r)=R_FPUREGISTER then
  164. internalerror(2003121210)
  165. else
  166. inherited getcpuregister(list,r);
  167. end;
  168. procedure tcgx86.ungetcpuregister(list:TAsmList;r:Tregister);
  169. begin
  170. if getregtype(r)=R_FPUREGISTER then
  171. rgfpu.ungetregisterfpu(list,r)
  172. else
  173. inherited ungetcpuregister(list,r);
  174. end;
  175. procedure Tcgx86.alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  176. begin
  177. if rt<>R_FPUREGISTER then
  178. inherited alloccpuregisters(list,rt,r);
  179. end;
  180. procedure Tcgx86.dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  181. begin
  182. if rt<>R_FPUREGISTER then
  183. inherited dealloccpuregisters(list,rt,r);
  184. end;
  185. function Tcgx86.uses_registers(rt:Tregistertype):boolean;
  186. begin
  187. if rt=R_FPUREGISTER then
  188. result:=false
  189. else
  190. result:=inherited uses_registers(rt);
  191. end;
  192. procedure tcgx86.add_reg_instruction(instr:Tai;r:tregister);
  193. begin
  194. if getregtype(r)<>R_FPUREGISTER then
  195. inherited add_reg_instruction(instr,r);
  196. end;
  197. procedure tcgx86.dec_fpu_stack;
  198. begin
  199. if rgfpu.fpuvaroffset<=0 then
  200. internalerror(200604201);
  201. dec(rgfpu.fpuvaroffset);
  202. end;
  203. procedure tcgx86.inc_fpu_stack;
  204. begin
  205. inc(rgfpu.fpuvaroffset);
  206. end;
  207. {****************************************************************************
  208. This is private property, keep out! :)
  209. ****************************************************************************}
  210. procedure tcgx86.sizes2load(s1,s2 : tcgsize; var op: tasmop; var s3: topsize);
  211. begin
  212. { ensure to have always valid sizes }
  213. if s1=OS_NO then
  214. s1:=s2;
  215. if s2=OS_NO then
  216. s2:=s1;
  217. case s2 of
  218. OS_8,OS_S8 :
  219. if S1 in [OS_8,OS_S8] then
  220. s3 := S_B
  221. else
  222. internalerror(200109221);
  223. OS_16,OS_S16:
  224. case s1 of
  225. OS_8,OS_S8:
  226. s3 := S_BW;
  227. OS_16,OS_S16:
  228. s3 := S_W;
  229. else
  230. internalerror(200109222);
  231. end;
  232. OS_32,OS_S32:
  233. case s1 of
  234. OS_8,OS_S8:
  235. s3 := S_BL;
  236. OS_16,OS_S16:
  237. s3 := S_WL;
  238. OS_32,OS_S32:
  239. s3 := S_L;
  240. else
  241. internalerror(200109223);
  242. end;
  243. {$ifdef x86_64}
  244. OS_64,OS_S64:
  245. case s1 of
  246. OS_8:
  247. s3 := S_BL;
  248. OS_S8:
  249. s3 := S_BQ;
  250. OS_16:
  251. s3 := S_WL;
  252. OS_S16:
  253. s3 := S_WQ;
  254. OS_32:
  255. s3 := S_L;
  256. OS_S32:
  257. s3 := S_LQ;
  258. OS_64,OS_S64:
  259. s3 := S_Q;
  260. else
  261. internalerror(200304302);
  262. end;
  263. {$endif x86_64}
  264. else
  265. internalerror(200109227);
  266. end;
  267. if s3 in [S_B,S_W,S_L,S_Q] then
  268. op := A_MOV
  269. else if s1 in [OS_8,OS_16,OS_32,OS_64] then
  270. op := A_MOVZX
  271. else
  272. {$ifdef x86_64}
  273. if s3 in [S_LQ] then
  274. op := A_MOVSXD
  275. else
  276. {$endif x86_64}
  277. op := A_MOVSX;
  278. end;
  279. procedure tcgx86.make_simple_ref(list:TAsmList;var ref: treference);
  280. var
  281. hreg : tregister;
  282. href : treference;
  283. begin
  284. {$ifdef x86_64}
  285. { Only 32bit is allowed }
  286. if ((ref.offset<low(longint)) or (ref.offset>high(longint))) then
  287. begin
  288. { Load constant value to register }
  289. hreg:=GetAddressRegister(list);
  290. list.concat(taicpu.op_const_reg(A_MOV,S_Q,ref.offset,hreg));
  291. ref.offset:=0;
  292. {if assigned(ref.symbol) then
  293. begin
  294. list.concat(taicpu.op_sym_ofs_reg(A_ADD,S_Q,ref.symbol,0,hreg));
  295. ref.symbol:=nil;
  296. end;}
  297. { Add register to reference }
  298. if ref.index=NR_NO then
  299. ref.index:=hreg
  300. else
  301. begin
  302. if ref.scalefactor<>0 then
  303. begin
  304. list.concat(taicpu.op_reg_reg(A_ADD,S_Q,ref.base,hreg));
  305. ref.base:=hreg;
  306. end
  307. else
  308. begin
  309. list.concat(taicpu.op_reg_reg(A_ADD,S_Q,ref.index,hreg));
  310. ref.index:=hreg;
  311. end;
  312. end;
  313. end;
  314. if (cs_create_pic in aktmoduleswitches) and
  315. assigned(ref.symbol) then
  316. begin
  317. reference_reset_symbol(href,ref.symbol,0);
  318. hreg:=getaddressregister(list);
  319. href.refaddr:=addr_pic;
  320. href.base:=NR_RIP;
  321. list.concat(taicpu.op_ref_reg(A_MOV,S_Q,href,hreg));
  322. ref.symbol:=nil;
  323. if ref.base=NR_NO then
  324. ref.base:=hreg
  325. else if ref.index=NR_NO then
  326. begin
  327. ref.index:=hreg;
  328. ref.scalefactor:=1;
  329. end
  330. else
  331. begin
  332. list.concat(taicpu.op_reg_reg(A_ADD,S_Q,ref.base,hreg));
  333. ref.base:=hreg;
  334. end;
  335. end;
  336. {$else x86_64}
  337. if (cs_create_pic in aktmoduleswitches) and
  338. assigned(ref.symbol) then
  339. begin
  340. reference_reset_symbol(href,ref.symbol,0);
  341. hreg:=getaddressregister(list);
  342. href.refaddr:=addr_pic;
  343. href.base:=current_procinfo.got;
  344. include(current_procinfo.flags,pi_needs_got);
  345. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,hreg));
  346. ref.symbol:=nil;
  347. if ref.base=NR_NO then
  348. ref.base:=hreg
  349. else if ref.index=NR_NO then
  350. begin
  351. ref.index:=hreg;
  352. ref.scalefactor:=1;
  353. end
  354. else
  355. begin
  356. list.concat(taicpu.op_reg_reg(A_ADD,S_L,ref.base,hreg));
  357. ref.base:=hreg;
  358. end;
  359. end;
  360. {$endif x86_64}
  361. end;
  362. procedure tcgx86.floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  363. begin
  364. case t of
  365. OS_F32 :
  366. begin
  367. op:=A_FLD;
  368. s:=S_FS;
  369. end;
  370. OS_F64 :
  371. begin
  372. op:=A_FLD;
  373. s:=S_FL;
  374. end;
  375. OS_F80 :
  376. begin
  377. op:=A_FLD;
  378. s:=S_FX;
  379. end;
  380. OS_C64 :
  381. begin
  382. op:=A_FILD;
  383. s:=S_IQ;
  384. end;
  385. else
  386. internalerror(200204041);
  387. end;
  388. end;
  389. procedure tcgx86.floatload(list: TAsmList; t : tcgsize;const ref : treference);
  390. var
  391. op : tasmop;
  392. s : topsize;
  393. tmpref : treference;
  394. begin
  395. tmpref:=ref;
  396. make_simple_ref(list,tmpref);
  397. floatloadops(t,op,s);
  398. list.concat(Taicpu.Op_ref(op,s,tmpref));
  399. inc_fpu_stack;
  400. end;
  401. procedure tcgx86.floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  402. begin
  403. case t of
  404. OS_F32 :
  405. begin
  406. op:=A_FSTP;
  407. s:=S_FS;
  408. end;
  409. OS_F64 :
  410. begin
  411. op:=A_FSTP;
  412. s:=S_FL;
  413. end;
  414. OS_F80 :
  415. begin
  416. op:=A_FSTP;
  417. s:=S_FX;
  418. end;
  419. OS_C64 :
  420. begin
  421. op:=A_FISTP;
  422. s:=S_IQ;
  423. end;
  424. else
  425. internalerror(200204042);
  426. end;
  427. end;
  428. procedure tcgx86.floatstore(list: TAsmList; t : tcgsize;const ref : treference);
  429. var
  430. op : tasmop;
  431. s : topsize;
  432. tmpref : treference;
  433. begin
  434. tmpref:=ref;
  435. make_simple_ref(list,tmpref);
  436. floatstoreops(t,op,s);
  437. list.concat(Taicpu.Op_ref(op,s,tmpref));
  438. { storing non extended floats can cause a floating point overflow }
  439. if (t<>OS_F80) and
  440. (cs_fpu_fwait in aktlocalswitches) then
  441. list.concat(Taicpu.Op_none(A_FWAIT,S_NO));
  442. dec_fpu_stack;
  443. end;
  444. procedure tcgx86.check_register_size(size:tcgsize;reg:tregister);
  445. begin
  446. if TCGSize2OpSize[size]<>TCGSize2OpSize[reg_cgsize(reg)] then
  447. internalerror(200306031);
  448. end;
  449. {****************************************************************************
  450. Assembler code
  451. ****************************************************************************}
  452. procedure tcgx86.a_jmp_name(list : TAsmList;const s : string);
  453. begin
  454. list.concat(taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(s)));
  455. end;
  456. procedure tcgx86.a_jmp_always(list : TAsmList;l: tasmlabel);
  457. begin
  458. a_jmp_cond(list, OC_NONE, l);
  459. end;
  460. function tcgx86.get_darwin_call_stub(const s: string): tasmsymbol;
  461. var
  462. stubname: string;
  463. begin
  464. stubname := 'L'+s+'$stub';
  465. result := current_asmdata.getasmsymbol(stubname);
  466. if assigned(result) then
  467. exit;
  468. if current_asmdata.asmlists[al_imports]=nil then
  469. current_asmdata.asmlists[al_imports]:=TAsmList.create;
  470. current_asmdata.asmlists[al_imports].concat(Tai_section.create(sec_stub,'',0));
  471. result := current_asmdata.RefAsmSymbol(stubname);
  472. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Create(result,0));
  473. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_indirect_symbol,s));
  474. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  475. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  476. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  477. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  478. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  479. end;
  480. procedure tcgx86.a_call_name(list : TAsmList;const s : string);
  481. var
  482. sym : tasmsymbol;
  483. r : treference;
  484. begin
  485. if (target_info.system <> system_i386_darwin) then
  486. begin
  487. sym:=current_asmdata.RefAsmSymbol(s);
  488. reference_reset_symbol(r,sym,0);
  489. if cs_create_pic in aktmoduleswitches then
  490. begin
  491. {$ifdef i386}
  492. include(current_procinfo.flags,pi_needs_got);
  493. {$endif i386}
  494. r.refaddr:=addr_pic
  495. end
  496. else
  497. r.refaddr:=addr_full;
  498. end
  499. else
  500. begin
  501. reference_reset_symbol(r,get_darwin_call_stub(s),0);
  502. r.refaddr:=addr_full;
  503. end;
  504. list.concat(taicpu.op_ref(A_CALL,S_NO,r));
  505. end;
  506. procedure tcgx86.a_call_name_static(list : TAsmList;const s : string);
  507. var
  508. sym : tasmsymbol;
  509. r : treference;
  510. begin
  511. sym:=current_asmdata.RefAsmSymbol(s);
  512. reference_reset_symbol(r,sym,0);
  513. r.refaddr:=addr_full;
  514. list.concat(taicpu.op_ref(A_CALL,S_NO,r));
  515. end;
  516. procedure tcgx86.a_call_reg(list : TAsmList;reg : tregister);
  517. begin
  518. list.concat(taicpu.op_reg(A_CALL,S_NO,reg));
  519. end;
  520. procedure tcgx86.a_call_ref(list : TAsmList;ref : treference);
  521. begin
  522. list.concat(taicpu.op_ref(A_CALL,S_NO,ref));
  523. end;
  524. {********************** load instructions ********************}
  525. procedure tcgx86.a_load_const_reg(list : TAsmList; tosize: TCGSize; a : aint; reg : TRegister);
  526. begin
  527. check_register_size(tosize,reg);
  528. { the optimizer will change it to "xor reg,reg" when loading zero, }
  529. { no need to do it here too (JM) }
  530. list.concat(taicpu.op_const_reg(A_MOV,TCGSize2OpSize[tosize],a,reg))
  531. end;
  532. procedure tcgx86.a_load_const_ref(list : TAsmList; tosize: tcgsize; a : aint;const ref : treference);
  533. var
  534. tmpref : treference;
  535. begin
  536. tmpref:=ref;
  537. make_simple_ref(list,tmpref);
  538. {$ifdef x86_64}
  539. { x86_64 only supports signed 32 bits constants directly }
  540. if (tosize in [OS_S64,OS_64]) and
  541. ((a<low(longint)) or (a>high(longint))) then
  542. begin
  543. a_load_const_ref(list,OS_32,longint(a and $ffffffff),tmpref);
  544. inc(tmpref.offset,4);
  545. a_load_const_ref(list,OS_32,longint(a shr 32),tmpref);
  546. end
  547. else
  548. {$endif x86_64}
  549. list.concat(taicpu.op_const_ref(A_MOV,TCGSize2OpSize[tosize],a,tmpref));
  550. end;
  551. procedure tcgx86.a_load_reg_ref(list : TAsmList; fromsize,tosize: TCGSize; reg : tregister;const ref : treference);
  552. var
  553. op: tasmop;
  554. s: topsize;
  555. tmpsize : tcgsize;
  556. tmpreg : tregister;
  557. tmpref : treference;
  558. begin
  559. tmpref:=ref;
  560. make_simple_ref(list,tmpref);
  561. check_register_size(fromsize,reg);
  562. sizes2load(fromsize,tosize,op,s);
  563. case s of
  564. {$ifdef x86_64}
  565. S_BQ,S_WQ,S_LQ,
  566. {$endif x86_64}
  567. S_BW,S_BL,S_WL :
  568. begin
  569. tmpreg:=getintregister(list,tosize);
  570. {$ifdef x86_64}
  571. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  572. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  573. 64 bit (FK) }
  574. if s in [S_BL,S_WL,S_L] then
  575. begin
  576. tmpreg:=makeregsize(list,tmpreg,OS_32);
  577. tmpsize:=OS_32;
  578. end
  579. else
  580. {$endif x86_64}
  581. tmpsize:=tosize;
  582. list.concat(taicpu.op_reg_reg(op,s,reg,tmpreg));
  583. a_load_reg_ref(list,tmpsize,tosize,tmpreg,tmpref);
  584. end;
  585. else
  586. list.concat(taicpu.op_reg_ref(op,s,reg,tmpref));
  587. end;
  588. end;
  589. procedure tcgx86.a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  590. var
  591. op: tasmop;
  592. s: topsize;
  593. tmpref : treference;
  594. begin
  595. tmpref:=ref;
  596. make_simple_ref(list,tmpref);
  597. check_register_size(tosize,reg);
  598. sizes2load(fromsize,tosize,op,s);
  599. {$ifdef x86_64}
  600. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  601. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  602. 64 bit (FK) }
  603. if s in [S_BL,S_WL,S_L] then
  604. reg:=makeregsize(list,reg,OS_32);
  605. {$endif x86_64}
  606. list.concat(taicpu.op_ref_reg(op,s,tmpref,reg));
  607. end;
  608. procedure tcgx86.a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  609. var
  610. op: tasmop;
  611. s: topsize;
  612. instr:Taicpu;
  613. begin
  614. check_register_size(fromsize,reg1);
  615. check_register_size(tosize,reg2);
  616. if tcgsize2size[fromsize]>tcgsize2size[tosize] then
  617. begin
  618. reg1:=makeregsize(list,reg1,tosize);
  619. s:=tcgsize2opsize[tosize];
  620. op:=A_MOV;
  621. end
  622. else
  623. sizes2load(fromsize,tosize,op,s);
  624. {$ifdef x86_64}
  625. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  626. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  627. 64 bit (FK)
  628. }
  629. if s in [S_BL,S_WL,S_L] then
  630. reg2:=makeregsize(list,reg2,OS_32);
  631. {$endif x86_64}
  632. if (reg1<>reg2) then
  633. begin
  634. instr:=taicpu.op_reg_reg(op,s,reg1,reg2);
  635. { Notify the register allocator that we have written a move instruction so
  636. it can try to eliminate it. }
  637. if (reg1<>NR_ESP) and (reg1<>NR_EBP) then
  638. add_move_instruction(instr);
  639. list.concat(instr);
  640. end;
  641. {$ifdef x86_64}
  642. { avoid merging of registers and killing the zero extensions (FK) }
  643. if (tosize in [OS_64,OS_S64]) and (s=S_L) then
  644. list.concat(taicpu.op_const_reg(A_AND,S_L,$ffffffff,reg2));
  645. {$endif x86_64}
  646. end;
  647. procedure tcgx86.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  648. var
  649. tmpref : treference;
  650. begin
  651. with ref do
  652. begin
  653. if (base=NR_NO) and (index=NR_NO) then
  654. begin
  655. if assigned(ref.symbol) then
  656. begin
  657. if (cs_create_pic in aktmoduleswitches) then
  658. begin
  659. {$ifdef x86_64}
  660. reference_reset_symbol(tmpref,ref.symbol,0);
  661. tmpref.refaddr:=addr_pic;
  662. tmpref.base:=NR_RIP;
  663. list.concat(taicpu.op_ref_reg(A_MOV,S_Q,tmpref,r));
  664. {$else x86_64}
  665. reference_reset_symbol(tmpref,ref.symbol,0);
  666. tmpref.refaddr:=addr_pic;
  667. tmpref.base:=current_procinfo.got;
  668. include(current_procinfo.flags,pi_needs_got);
  669. list.concat(taicpu.op_ref_reg(A_MOV,S_L,tmpref,r));
  670. {$endif x86_64}
  671. if offset<>0 then
  672. a_op_const_reg(list,OP_ADD,OS_ADDR,offset,r);
  673. end
  674. else
  675. begin
  676. tmpref:=ref;
  677. tmpref.refaddr:=ADDR_FULL;
  678. list.concat(Taicpu.op_ref_reg(A_MOV,tcgsize2opsize[OS_ADDR],tmpref,r));
  679. end
  680. end
  681. else
  682. a_load_const_reg(list,OS_ADDR,offset,r)
  683. end
  684. else if (base=NR_NO) and (index<>NR_NO) and
  685. (offset=0) and (scalefactor=0) and (symbol=nil) then
  686. a_load_reg_reg(list,OS_ADDR,OS_ADDR,index,r)
  687. else if (base<>NR_NO) and (index=NR_NO) and
  688. (offset=0) and (symbol=nil) then
  689. a_load_reg_reg(list,OS_ADDR,OS_ADDR,base,r)
  690. else
  691. begin
  692. tmpref:=ref;
  693. make_simple_ref(list,tmpref);
  694. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,r));
  695. end;
  696. if segment<>NR_NO then
  697. begin
  698. if (tf_section_threadvars in target_info.flags) then
  699. begin
  700. { Convert thread local address to a process global addres
  701. as we cannot handle far pointers.}
  702. case target_info.system of
  703. system_i386_linux:
  704. if segment=NR_GS then
  705. begin
  706. reference_reset_symbol(tmpref,current_asmdata.RefAsmSymbol('___fpc_threadvar_offset'),0);
  707. tmpref.segment:=NR_GS;
  708. list.concat(Taicpu.op_ref_reg(A_ADD,tcgsize2opsize[OS_ADDR],tmpref,r));
  709. end
  710. else
  711. cgmessage(cg_e_cant_use_far_pointer_there);
  712. system_i386_win32:
  713. if segment=NR_FS then
  714. begin
  715. allocallcpuregisters(list);
  716. a_call_name(list,'GetTls');
  717. deallocallcpuregisters(list);
  718. list.concat(Taicpu.op_reg_reg(A_ADD,tcgsize2opsize[OS_ADDR],NR_EAX,r));
  719. end
  720. else
  721. cgmessage(cg_e_cant_use_far_pointer_there);
  722. else
  723. cgmessage(cg_e_cant_use_far_pointer_there);
  724. end;
  725. end
  726. else
  727. cgmessage(cg_e_cant_use_far_pointer_there);
  728. end;
  729. end;
  730. end;
  731. { all fpu load routines expect that R_ST[0-7] means an fpu regvar and }
  732. { R_ST means "the current value at the top of the fpu stack" (JM) }
  733. procedure tcgx86.a_loadfpu_reg_reg(list: TAsmList; size: tcgsize; reg1, reg2: tregister);
  734. begin
  735. if (reg1<>NR_ST) then
  736. begin
  737. list.concat(taicpu.op_reg(A_FLD,S_NO,rgfpu.correct_fpuregister(reg1,rgfpu.fpuvaroffset)));
  738. inc_fpu_stack;
  739. end;
  740. if (reg2<>NR_ST) then
  741. begin
  742. list.concat(taicpu.op_reg(A_FSTP,S_NO,rgfpu.correct_fpuregister(reg2,rgfpu.fpuvaroffset)));
  743. dec_fpu_stack;
  744. end;
  745. end;
  746. procedure tcgx86.a_loadfpu_ref_reg(list: TAsmList; size: tcgsize; const ref: treference; reg: tregister);
  747. begin
  748. floatload(list,size,ref);
  749. if (reg<>NR_ST) then
  750. a_loadfpu_reg_reg(list,size,NR_ST,reg);
  751. end;
  752. procedure tcgx86.a_loadfpu_reg_ref(list: TAsmList; size: tcgsize; reg: tregister; const ref: treference);
  753. begin
  754. if reg<>NR_ST then
  755. a_loadfpu_reg_reg(list,size,reg,NR_ST);
  756. floatstore(list,size,ref);
  757. end;
  758. function get_scalar_mm_op(fromsize,tosize : tcgsize) : tasmop;
  759. const
  760. convertop : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  761. (A_MOVSS,A_CVTSS2SD,A_NONE,A_NONE,A_NONE),
  762. (A_CVTSD2SS,A_MOVSD,A_NONE,A_NONE,A_NONE),
  763. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  764. (A_NONE,A_NONE,A_NONE,A_MOVQ,A_NONE),
  765. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE));
  766. begin
  767. result:=convertop[fromsize,tosize];
  768. if result=A_NONE then
  769. internalerror(200312205);
  770. end;
  771. procedure tcgx86.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle);
  772. var
  773. instr : taicpu;
  774. begin
  775. if shuffle=nil then
  776. begin
  777. if fromsize=tosize then
  778. { needs correct size in case of spilling }
  779. case fromsize of
  780. OS_F32:
  781. instr:=taicpu.op_reg_reg(A_MOVAPS,S_NO,reg1,reg2);
  782. OS_F64:
  783. instr:=taicpu.op_reg_reg(A_MOVAPD,S_NO,reg1,reg2);
  784. else
  785. internalerror(2006091201);
  786. end
  787. else
  788. internalerror(200312202);
  789. end
  790. else if shufflescalar(shuffle) then
  791. instr:=taicpu.op_reg_reg(get_scalar_mm_op(fromsize,tosize),S_NO,reg1,reg2)
  792. else
  793. internalerror(200312201);
  794. case get_scalar_mm_op(fromsize,tosize) of
  795. A_MOVSS,
  796. A_MOVSD,
  797. A_MOVQ:
  798. add_move_instruction(instr);
  799. end;
  800. list.concat(instr);
  801. end;
  802. procedure tcgx86.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  803. var
  804. tmpref : treference;
  805. begin
  806. tmpref:=ref;
  807. make_simple_ref(list,tmpref);
  808. if shuffle=nil then
  809. list.concat(taicpu.op_ref_reg(A_MOVQ,S_NO,tmpref,reg))
  810. else if shufflescalar(shuffle) then
  811. list.concat(taicpu.op_ref_reg(get_scalar_mm_op(fromsize,tosize),S_NO,tmpref,reg))
  812. else
  813. internalerror(200312252);
  814. end;
  815. procedure tcgx86.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle);
  816. var
  817. hreg : tregister;
  818. tmpref : treference;
  819. begin
  820. tmpref:=ref;
  821. make_simple_ref(list,tmpref);
  822. if shuffle=nil then
  823. list.concat(taicpu.op_reg_ref(A_MOVQ,S_NO,reg,tmpref))
  824. else if shufflescalar(shuffle) then
  825. begin
  826. if tosize<>fromsize then
  827. begin
  828. hreg:=getmmregister(list,tosize);
  829. list.concat(taicpu.op_reg_reg(get_scalar_mm_op(fromsize,tosize),S_NO,reg,hreg));
  830. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(tosize,tosize),S_NO,hreg,tmpref));
  831. end
  832. else
  833. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(fromsize,tosize),S_NO,reg,tmpref));
  834. end
  835. else
  836. internalerror(200312252);
  837. end;
  838. procedure tcgx86.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  839. var
  840. l : tlocation;
  841. begin
  842. l.loc:=LOC_REFERENCE;
  843. l.reference:=ref;
  844. l.size:=size;
  845. opmm_loc_reg(list,op,size,l,reg,shuffle);
  846. end;
  847. procedure tcgx86.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle);
  848. var
  849. l : tlocation;
  850. begin
  851. l.loc:=LOC_MMREGISTER;
  852. l.register:=src;
  853. l.size:=size;
  854. opmm_loc_reg(list,op,size,l,dst,shuffle);
  855. end;
  856. procedure tcgx86.opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;dst: tregister; shuffle : pmmshuffle);
  857. const
  858. opmm2asmop : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  859. ( { scalar }
  860. ( { OS_F32 }
  861. 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
  862. ),
  863. ( { OS_F64 }
  864. 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
  865. )
  866. ),
  867. ( { vectorized/packed }
  868. { because the logical packed single instructions have shorter op codes, we use always
  869. these
  870. }
  871. ( { OS_F32 }
  872. A_NOP,A_NOP,A_ADDPS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_XORPS
  873. ),
  874. ( { OS_F64 }
  875. A_NOP,A_NOP,A_ADDPD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_XORPD
  876. )
  877. )
  878. );
  879. var
  880. resultreg : tregister;
  881. asmop : tasmop;
  882. begin
  883. { this is an internally used procedure so the parameters have
  884. some constrains
  885. }
  886. if loc.size<>size then
  887. internalerror(200312213);
  888. resultreg:=dst;
  889. { deshuffle }
  890. //!!!
  891. if (shuffle<>nil) and not(shufflescalar(shuffle)) then
  892. begin
  893. end
  894. else if (shuffle=nil) then
  895. asmop:=opmm2asmop[1,size,op]
  896. else if shufflescalar(shuffle) then
  897. begin
  898. asmop:=opmm2asmop[0,size,op];
  899. { no scalar operation available? }
  900. if asmop=A_NOP then
  901. begin
  902. { do vectorized and shuffle finally }
  903. //!!!
  904. end;
  905. end
  906. else
  907. internalerror(200312211);
  908. if asmop=A_NOP then
  909. internalerror(200312215);
  910. case loc.loc of
  911. LOC_CREFERENCE,LOC_REFERENCE:
  912. begin
  913. make_simple_ref(current_asmdata.CurrAsmList,loc.reference);
  914. list.concat(taicpu.op_ref_reg(asmop,S_NO,loc.reference,resultreg));
  915. end;
  916. LOC_CMMREGISTER,LOC_MMREGISTER:
  917. list.concat(taicpu.op_reg_reg(asmop,S_NO,loc.register,resultreg));
  918. else
  919. internalerror(200312214);
  920. end;
  921. { shuffle }
  922. if resultreg<>dst then
  923. begin
  924. internalerror(200312212);
  925. end;
  926. end;
  927. procedure tcgx86.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister);
  928. var
  929. opcode : tasmop;
  930. power : longint;
  931. {$ifdef x86_64}
  932. tmpreg : tregister;
  933. {$endif x86_64}
  934. begin
  935. optimize_op_const(op, a);
  936. {$ifdef x86_64}
  937. { x86_64 only supports signed 32 bits constants directly }
  938. if not(op in [OP_NONE,OP_MOVE]) and
  939. (size in [OS_S64,OS_64]) and
  940. ((a<low(longint)) or (a>high(longint))) then
  941. begin
  942. tmpreg:=getintregister(list,size);
  943. a_load_const_reg(list,size,a,tmpreg);
  944. a_op_reg_reg(list,op,size,tmpreg,reg);
  945. exit;
  946. end;
  947. {$endif x86_64}
  948. check_register_size(size,reg);
  949. case op of
  950. OP_NONE :
  951. begin
  952. { Opcode is optimized away }
  953. end;
  954. OP_MOVE :
  955. begin
  956. { Optimized, replaced with a simple load }
  957. a_load_const_reg(list,size,a,reg);
  958. end;
  959. OP_DIV, OP_IDIV:
  960. begin
  961. if ispowerof2(int64(a),power) then
  962. begin
  963. case op of
  964. OP_DIV:
  965. opcode := A_SHR;
  966. OP_IDIV:
  967. opcode := A_SAR;
  968. end;
  969. list.concat(taicpu.op_const_reg(opcode,TCgSize2OpSize[size],power,reg));
  970. exit;
  971. end;
  972. { the rest should be handled specifically in the code }
  973. { generator because of the silly register usage restraints }
  974. internalerror(200109224);
  975. end;
  976. OP_MUL,OP_IMUL:
  977. begin
  978. if not(cs_check_overflow in aktlocalswitches) and
  979. ispowerof2(int64(a),power) then
  980. begin
  981. list.concat(taicpu.op_const_reg(A_SHL,TCgSize2OpSize[size],power,reg));
  982. exit;
  983. end;
  984. if op = OP_IMUL then
  985. list.concat(taicpu.op_const_reg(A_IMUL,TCgSize2OpSize[size],a,reg))
  986. else
  987. { OP_MUL should be handled specifically in the code }
  988. { generator because of the silly register usage restraints }
  989. internalerror(200109225);
  990. end;
  991. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  992. if not(cs_check_overflow in aktlocalswitches) and
  993. (a = 1) and
  994. (op in [OP_ADD,OP_SUB]) then
  995. if op = OP_ADD then
  996. list.concat(taicpu.op_reg(A_INC,TCgSize2OpSize[size],reg))
  997. else
  998. list.concat(taicpu.op_reg(A_DEC,TCgSize2OpSize[size],reg))
  999. else if (a = 0) then
  1000. if (op <> OP_AND) then
  1001. exit
  1002. else
  1003. list.concat(taicpu.op_const_reg(A_MOV,TCgSize2OpSize[size],0,reg))
  1004. else if (aword(a) = high(aword)) and
  1005. (op in [OP_AND,OP_OR,OP_XOR]) then
  1006. begin
  1007. case op of
  1008. OP_AND:
  1009. exit;
  1010. OP_OR:
  1011. list.concat(taicpu.op_const_reg(A_MOV,TCgSize2OpSize[size],aint(high(aword)),reg));
  1012. OP_XOR:
  1013. list.concat(taicpu.op_reg(A_NOT,TCgSize2OpSize[size],reg));
  1014. end
  1015. end
  1016. else
  1017. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,reg));
  1018. OP_SHL,OP_SHR,OP_SAR:
  1019. begin
  1020. {$ifdef x86_64}
  1021. if (a and 63) <> 0 Then
  1022. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,reg));
  1023. if (a shr 6) <> 0 Then
  1024. internalerror(200609073);
  1025. {$else x86_64}
  1026. if (a and 31) <> 0 Then
  1027. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,reg));
  1028. if (a shr 5) <> 0 Then
  1029. internalerror(200609071);
  1030. {$endif x86_64}
  1031. end
  1032. else internalerror(200609072);
  1033. end;
  1034. end;
  1035. procedure tcgx86.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; const ref: TReference);
  1036. var
  1037. opcode: tasmop;
  1038. power: longint;
  1039. {$ifdef x86_64}
  1040. tmpreg : tregister;
  1041. {$endif x86_64}
  1042. tmpref : treference;
  1043. begin
  1044. optimize_op_const(op, a);
  1045. tmpref:=ref;
  1046. make_simple_ref(list,tmpref);
  1047. {$ifdef x86_64}
  1048. { x86_64 only supports signed 32 bits constants directly }
  1049. if not(op in [OP_NONE,OP_MOVE]) and
  1050. (size in [OS_S64,OS_64]) and
  1051. ((a<low(longint)) or (a>high(longint))) then
  1052. begin
  1053. tmpreg:=getintregister(list,size);
  1054. a_load_const_reg(list,size,a,tmpreg);
  1055. a_op_reg_ref(list,op,size,tmpreg,tmpref);
  1056. exit;
  1057. end;
  1058. {$endif x86_64}
  1059. Case Op of
  1060. OP_NONE :
  1061. begin
  1062. { Opcode is optimized away }
  1063. end;
  1064. OP_MOVE :
  1065. begin
  1066. { Optimized, replaced with a simple load }
  1067. a_load_const_ref(list,size,a,ref);
  1068. end;
  1069. OP_DIV, OP_IDIV:
  1070. Begin
  1071. if ispowerof2(int64(a),power) then
  1072. begin
  1073. case op of
  1074. OP_DIV:
  1075. opcode := A_SHR;
  1076. OP_IDIV:
  1077. opcode := A_SAR;
  1078. end;
  1079. list.concat(taicpu.op_const_ref(opcode,
  1080. TCgSize2OpSize[size],power,tmpref));
  1081. exit;
  1082. end;
  1083. { the rest should be handled specifically in the code }
  1084. { generator because of the silly register usage restraints }
  1085. internalerror(200109231);
  1086. End;
  1087. OP_MUL,OP_IMUL:
  1088. begin
  1089. if not(cs_check_overflow in aktlocalswitches) and
  1090. ispowerof2(int64(a),power) then
  1091. begin
  1092. list.concat(taicpu.op_const_ref(A_SHL,TCgSize2OpSize[size],
  1093. power,tmpref));
  1094. exit;
  1095. end;
  1096. { can't multiply a memory location directly with a constant }
  1097. if op = OP_IMUL then
  1098. inherited a_op_const_ref(list,op,size,a,tmpref)
  1099. else
  1100. { OP_MUL should be handled specifically in the code }
  1101. { generator because of the silly register usage restraints }
  1102. internalerror(200109232);
  1103. end;
  1104. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  1105. if not(cs_check_overflow in aktlocalswitches) and
  1106. (a = 1) and
  1107. (op in [OP_ADD,OP_SUB]) then
  1108. if op = OP_ADD then
  1109. list.concat(taicpu.op_ref(A_INC,TCgSize2OpSize[size],tmpref))
  1110. else
  1111. list.concat(taicpu.op_ref(A_DEC,TCgSize2OpSize[size],tmpref))
  1112. else if (a = 0) then
  1113. if (op <> OP_AND) then
  1114. exit
  1115. else
  1116. a_load_const_ref(list,size,0,tmpref)
  1117. else if (aword(a) = high(aword)) and
  1118. (op in [OP_AND,OP_OR,OP_XOR]) then
  1119. begin
  1120. case op of
  1121. OP_AND:
  1122. exit;
  1123. OP_OR:
  1124. list.concat(taicpu.op_const_ref(A_MOV,TCgSize2OpSize[size],aint(high(aword)),tmpref));
  1125. OP_XOR:
  1126. list.concat(taicpu.op_ref(A_NOT,TCgSize2OpSize[size],tmpref));
  1127. end
  1128. end
  1129. else
  1130. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],
  1131. TCgSize2OpSize[size],a,tmpref));
  1132. OP_SHL,OP_SHR,OP_SAR:
  1133. begin
  1134. if (a and 31) <> 0 then
  1135. list.concat(taicpu.op_const_ref(
  1136. TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,tmpref));
  1137. if (a shr 5) <> 0 Then
  1138. internalerror(68991);
  1139. end
  1140. else internalerror(68992);
  1141. end;
  1142. end;
  1143. procedure tcgx86.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  1144. var
  1145. dstsize: topsize;
  1146. instr:Taicpu;
  1147. begin
  1148. check_register_size(size,src);
  1149. check_register_size(size,dst);
  1150. dstsize := tcgsize2opsize[size];
  1151. case op of
  1152. OP_NEG,OP_NOT:
  1153. begin
  1154. if src<>dst then
  1155. a_load_reg_reg(list,size,size,src,dst);
  1156. list.concat(taicpu.op_reg(TOpCG2AsmOp[op],dstsize,dst));
  1157. end;
  1158. OP_MUL,OP_DIV,OP_IDIV:
  1159. { special stuff, needs separate handling inside code }
  1160. { generator }
  1161. internalerror(200109233);
  1162. OP_SHR,OP_SHL,OP_SAR:
  1163. begin
  1164. { Use ecx to load the value, that allows beter coalescing }
  1165. getcpuregister(list,NR_ECX);
  1166. a_load_reg_reg(list,size,OS_32,src,NR_ECX);
  1167. list.concat(taicpu.op_reg_reg(Topcg2asmop[op],tcgsize2opsize[size],NR_CL,dst));
  1168. ungetcpuregister(list,NR_ECX);
  1169. end;
  1170. else
  1171. begin
  1172. if reg2opsize(src) <> dstsize then
  1173. internalerror(200109226);
  1174. instr:=taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,src,dst);
  1175. list.concat(instr);
  1176. end;
  1177. end;
  1178. end;
  1179. procedure tcgx86.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1180. var
  1181. tmpref : treference;
  1182. begin
  1183. tmpref:=ref;
  1184. make_simple_ref(list,tmpref);
  1185. check_register_size(size,reg);
  1186. case op of
  1187. OP_NEG,OP_NOT,OP_IMUL:
  1188. begin
  1189. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  1190. end;
  1191. OP_MUL,OP_DIV,OP_IDIV:
  1192. { special stuff, needs separate handling inside code }
  1193. { generator }
  1194. internalerror(200109239);
  1195. else
  1196. begin
  1197. reg := makeregsize(list,reg,size);
  1198. list.concat(taicpu.op_ref_reg(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref,reg));
  1199. end;
  1200. end;
  1201. end;
  1202. procedure tcgx86.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  1203. var
  1204. tmpref : treference;
  1205. begin
  1206. tmpref:=ref;
  1207. make_simple_ref(list,tmpref);
  1208. check_register_size(size,reg);
  1209. case op of
  1210. OP_NEG,OP_NOT:
  1211. begin
  1212. if reg<>NR_NO then
  1213. internalerror(200109237);
  1214. list.concat(taicpu.op_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref));
  1215. end;
  1216. OP_IMUL:
  1217. begin
  1218. { this one needs a load/imul/store, which is the default }
  1219. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  1220. end;
  1221. OP_MUL,OP_DIV,OP_IDIV:
  1222. { special stuff, needs separate handling inside code }
  1223. { generator }
  1224. internalerror(200109238);
  1225. else
  1226. begin
  1227. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],reg,tmpref));
  1228. end;
  1229. end;
  1230. end;
  1231. {*************** compare instructructions ****************}
  1232. procedure tcgx86.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  1233. l : tasmlabel);
  1234. {$ifdef x86_64}
  1235. var
  1236. tmpreg : tregister;
  1237. {$endif x86_64}
  1238. begin
  1239. {$ifdef x86_64}
  1240. { x86_64 only supports signed 32 bits constants directly }
  1241. if (size in [OS_S64,OS_64]) and
  1242. ((a<low(longint)) or (a>high(longint))) then
  1243. begin
  1244. tmpreg:=getintregister(list,size);
  1245. a_load_const_reg(list,size,a,tmpreg);
  1246. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1247. exit;
  1248. end;
  1249. {$endif x86_64}
  1250. if (a = 0) then
  1251. list.concat(taicpu.op_reg_reg(A_TEST,tcgsize2opsize[size],reg,reg))
  1252. else
  1253. list.concat(taicpu.op_const_reg(A_CMP,tcgsize2opsize[size],a,reg));
  1254. a_jmp_cond(list,cmp_op,l);
  1255. end;
  1256. procedure tcgx86.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;const ref : treference;
  1257. l : tasmlabel);
  1258. var
  1259. {$ifdef x86_64}
  1260. tmpreg : tregister;
  1261. {$endif x86_64}
  1262. tmpref : treference;
  1263. begin
  1264. tmpref:=ref;
  1265. make_simple_ref(list,tmpref);
  1266. {$ifdef x86_64}
  1267. { x86_64 only supports signed 32 bits constants directly }
  1268. if (size in [OS_S64,OS_64]) and
  1269. ((a<low(longint)) or (a>high(longint))) then
  1270. begin
  1271. tmpreg:=getintregister(list,size);
  1272. a_load_const_reg(list,size,a,tmpreg);
  1273. a_cmp_reg_ref_label(list,size,cmp_op,tmpreg,tmpref,l);
  1274. exit;
  1275. end;
  1276. {$endif x86_64}
  1277. list.concat(taicpu.op_const_ref(A_CMP,TCgSize2OpSize[size],a,tmpref));
  1278. a_jmp_cond(list,cmp_op,l);
  1279. end;
  1280. procedure tcgx86.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;
  1281. reg1,reg2 : tregister;l : tasmlabel);
  1282. begin
  1283. check_register_size(size,reg1);
  1284. check_register_size(size,reg2);
  1285. list.concat(taicpu.op_reg_reg(A_CMP,TCgSize2OpSize[size],reg1,reg2));
  1286. a_jmp_cond(list,cmp_op,l);
  1287. end;
  1288. procedure tcgx86.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister;l : tasmlabel);
  1289. var
  1290. tmpref : treference;
  1291. begin
  1292. tmpref:=ref;
  1293. make_simple_ref(list,tmpref);
  1294. check_register_size(size,reg);
  1295. list.concat(taicpu.op_ref_reg(A_CMP,TCgSize2OpSize[size],tmpref,reg));
  1296. a_jmp_cond(list,cmp_op,l);
  1297. end;
  1298. procedure tcgx86.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister;const ref: treference; l : tasmlabel);
  1299. var
  1300. tmpref : treference;
  1301. begin
  1302. tmpref:=ref;
  1303. make_simple_ref(list,tmpref);
  1304. check_register_size(size,reg);
  1305. list.concat(taicpu.op_reg_ref(A_CMP,TCgSize2OpSize[size],reg,tmpref));
  1306. a_jmp_cond(list,cmp_op,l);
  1307. end;
  1308. procedure tcgx86.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1309. var
  1310. ai : taicpu;
  1311. begin
  1312. if cond=OC_None then
  1313. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1314. else
  1315. begin
  1316. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  1317. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1318. end;
  1319. ai.is_jmp:=true;
  1320. list.concat(ai);
  1321. end;
  1322. procedure tcgx86.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1323. var
  1324. ai : taicpu;
  1325. begin
  1326. ai := Taicpu.op_sym(A_Jcc,S_NO,l);
  1327. ai.SetCondition(flags_to_cond(f));
  1328. ai.is_jmp := true;
  1329. list.concat(ai);
  1330. end;
  1331. procedure tcgx86.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  1332. var
  1333. ai : taicpu;
  1334. hreg : tregister;
  1335. begin
  1336. hreg:=makeregsize(list,reg,OS_8);
  1337. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg);
  1338. ai.setcondition(flags_to_cond(f));
  1339. list.concat(ai);
  1340. if (reg<>hreg) then
  1341. a_load_reg_reg(list,OS_8,size,hreg,reg);
  1342. end;
  1343. procedure tcgx86.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference);
  1344. var
  1345. ai : taicpu;
  1346. tmpref : treference;
  1347. begin
  1348. tmpref:=ref;
  1349. make_simple_ref(list,tmpref);
  1350. if not(size in [OS_8,OS_S8]) then
  1351. a_load_const_ref(list,size,0,tmpref);
  1352. ai:=Taicpu.op_ref(A_SETcc,S_B,tmpref);
  1353. ai.setcondition(flags_to_cond(f));
  1354. list.concat(ai);
  1355. end;
  1356. { ************* concatcopy ************ }
  1357. procedure Tcgx86.g_concatcopy(list:TAsmList;const source,dest:Treference;len:aint);
  1358. const
  1359. {$ifdef cpu64bit}
  1360. REGCX=NR_RCX;
  1361. REGSI=NR_RSI;
  1362. REGDI=NR_RDI;
  1363. {$else cpu64bit}
  1364. REGCX=NR_ECX;
  1365. REGSI=NR_ESI;
  1366. REGDI=NR_EDI;
  1367. {$endif cpu64bit}
  1368. type copymode=(copy_move,copy_mmx,copy_string);
  1369. var srcref,dstref:Treference;
  1370. r,r0,r1,r2,r3:Tregister;
  1371. helpsize:aint;
  1372. copysize:byte;
  1373. cgsize:Tcgsize;
  1374. cm:copymode;
  1375. begin
  1376. cm:=copy_move;
  1377. helpsize:=12;
  1378. if cs_opt_size in aktoptimizerswitches then
  1379. helpsize:=8;
  1380. if (cs_mmx in aktlocalswitches) and
  1381. not(pi_uses_fpu in current_procinfo.flags) and
  1382. ((len=8) or (len=16) or (len=24) or (len=32)) then
  1383. cm:=copy_mmx;
  1384. if (len>helpsize) then
  1385. cm:=copy_string;
  1386. if (cs_opt_size in aktoptimizerswitches) and
  1387. not((len<=16) and (cm=copy_mmx)) then
  1388. cm:=copy_string;
  1389. case cm of
  1390. copy_move:
  1391. begin
  1392. dstref:=dest;
  1393. srcref:=source;
  1394. copysize:=sizeof(aint);
  1395. cgsize:=int_cgsize(copysize);
  1396. while len<>0 do
  1397. begin
  1398. if len<2 then
  1399. begin
  1400. copysize:=1;
  1401. cgsize:=OS_8;
  1402. end
  1403. else if len<4 then
  1404. begin
  1405. copysize:=2;
  1406. cgsize:=OS_16;
  1407. end
  1408. else if len<8 then
  1409. begin
  1410. copysize:=4;
  1411. cgsize:=OS_32;
  1412. end;
  1413. dec(len,copysize);
  1414. r:=getintregister(list,cgsize);
  1415. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  1416. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  1417. inc(srcref.offset,copysize);
  1418. inc(dstref.offset,copysize);
  1419. end;
  1420. end;
  1421. copy_mmx:
  1422. begin
  1423. dstref:=dest;
  1424. srcref:=source;
  1425. r0:=getmmxregister(list);
  1426. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r0,nil);
  1427. if len>=16 then
  1428. begin
  1429. inc(srcref.offset,8);
  1430. r1:=getmmxregister(list);
  1431. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r1,nil);
  1432. end;
  1433. if len>=24 then
  1434. begin
  1435. inc(srcref.offset,8);
  1436. r2:=getmmxregister(list);
  1437. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r2,nil);
  1438. end;
  1439. if len>=32 then
  1440. begin
  1441. inc(srcref.offset,8);
  1442. r3:=getmmxregister(list);
  1443. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  1444. end;
  1445. a_loadmm_reg_ref(list,OS_M64,OS_M64,r0,dstref,nil);
  1446. if len>=16 then
  1447. begin
  1448. inc(dstref.offset,8);
  1449. a_loadmm_reg_ref(list,OS_M64,OS_M64,r1,dstref,nil);
  1450. end;
  1451. if len>=24 then
  1452. begin
  1453. inc(dstref.offset,8);
  1454. a_loadmm_reg_ref(list,OS_M64,OS_M64,r2,dstref,nil);
  1455. end;
  1456. if len>=32 then
  1457. begin
  1458. inc(dstref.offset,8);
  1459. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  1460. end;
  1461. end
  1462. else {copy_string, should be a good fallback in case of unhandled}
  1463. begin
  1464. getcpuregister(list,REGDI);
  1465. a_loadaddr_ref_reg(list,dest,REGDI);
  1466. getcpuregister(list,REGSI);
  1467. a_loadaddr_ref_reg(list,source,REGSI);
  1468. getcpuregister(list,REGCX);
  1469. {$ifdef i386}
  1470. list.concat(Taicpu.op_none(A_CLD,S_NO));
  1471. {$endif i386}
  1472. if cs_opt_size in aktoptimizerswitches then
  1473. begin
  1474. a_load_const_reg(list,OS_INT,len,REGCX);
  1475. list.concat(Taicpu.op_none(A_REP,S_NO));
  1476. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  1477. end
  1478. else
  1479. begin
  1480. helpsize:=len div sizeof(aint);
  1481. len:=len mod sizeof(aint);
  1482. if helpsize>1 then
  1483. begin
  1484. a_load_const_reg(list,OS_INT,helpsize,REGCX);
  1485. list.concat(Taicpu.op_none(A_REP,S_NO));
  1486. end;
  1487. if helpsize>0 then
  1488. begin
  1489. {$ifdef cpu64bit}
  1490. if sizeof(aint)=8 then
  1491. list.concat(Taicpu.op_none(A_MOVSQ,S_NO))
  1492. else
  1493. {$endif cpu64bit}
  1494. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  1495. end;
  1496. if len>=4 then
  1497. begin
  1498. dec(len,4);
  1499. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  1500. end;
  1501. if len>=2 then
  1502. begin
  1503. dec(len,2);
  1504. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  1505. end;
  1506. if len=1 then
  1507. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  1508. end;
  1509. ungetcpuregister(list,REGCX);
  1510. ungetcpuregister(list,REGSI);
  1511. ungetcpuregister(list,REGDI);
  1512. end;
  1513. end;
  1514. end;
  1515. {****************************************************************************
  1516. Entry/Exit Code Helpers
  1517. ****************************************************************************}
  1518. procedure tcgx86.g_releasevaluepara_openarray(list : TAsmList;const l:tlocation);
  1519. begin
  1520. if (use_fixed_stack) then
  1521. begin
  1522. inherited g_releasevaluepara_openarray(list,l);
  1523. exit;
  1524. end;
  1525. { Nothing to release }
  1526. end;
  1527. procedure tcgx86.g_profilecode(list : TAsmList);
  1528. var
  1529. pl : tasmlabel;
  1530. mcountprefix : String[4];
  1531. begin
  1532. case target_info.system of
  1533. {$ifndef NOTARGETWIN}
  1534. system_i386_win32,
  1535. {$endif}
  1536. system_i386_freebsd,
  1537. system_i386_netbsd,
  1538. // system_i386_openbsd,
  1539. system_i386_wdosx :
  1540. begin
  1541. Case target_info.system Of
  1542. system_i386_freebsd : mcountprefix:='.';
  1543. system_i386_netbsd : mcountprefix:='__';
  1544. // system_i386_openbsd : mcountprefix:='.';
  1545. else
  1546. mcountPrefix:='';
  1547. end;
  1548. current_asmdata.getaddrlabel(pl);
  1549. new_section(list,sec_data,lower(current_procinfo.procdef.mangledname),sizeof(aint));
  1550. list.concat(Tai_label.Create(pl));
  1551. list.concat(Tai_const.Create_32bit(0));
  1552. new_section(list,sec_code,lower(current_procinfo.procdef.mangledname),0);
  1553. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  1554. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,NR_EDX));
  1555. a_call_name(list,target_info.Cprefix+mcountprefix+'mcount');
  1556. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDX));
  1557. end;
  1558. system_i386_linux:
  1559. a_call_name(list,target_info.Cprefix+'mcount');
  1560. system_i386_go32v2,system_i386_watcom:
  1561. begin
  1562. a_call_name(list,'MCOUNT');
  1563. end;
  1564. system_x86_64_linux:
  1565. begin
  1566. a_call_name(list,'mcount');
  1567. end;
  1568. end;
  1569. end;
  1570. procedure tcgx86.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  1571. {$ifdef x86}
  1572. {$ifndef NOTARGETWIN}
  1573. var
  1574. href : treference;
  1575. i : integer;
  1576. again : tasmlabel;
  1577. {$endif NOTARGETWIN}
  1578. {$endif x86}
  1579. begin
  1580. if localsize>0 then
  1581. begin
  1582. {$ifdef i386}
  1583. {$ifndef NOTARGETWIN}
  1584. { windows guards only a few pages for stack growing,
  1585. so we have to access every page first }
  1586. if (target_info.system in [system_i386_win32,system_i386_wince]) and
  1587. (localsize>=winstackpagesize) then
  1588. begin
  1589. if localsize div winstackpagesize<=5 then
  1590. begin
  1591. list.concat(Taicpu.Op_const_reg(A_SUB,S_L,localsize-4,NR_ESP));
  1592. for i:=1 to localsize div winstackpagesize do
  1593. begin
  1594. reference_reset_base(href,NR_ESP,localsize-i*winstackpagesize);
  1595. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1596. end;
  1597. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  1598. end
  1599. else
  1600. begin
  1601. current_asmdata.getjumplabel(again);
  1602. getcpuregister(list,NR_EDI);
  1603. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EDI));
  1604. list.concat(Taicpu.op_const_reg(A_MOV,S_L,localsize div winstackpagesize,NR_EDI));
  1605. a_label(list,again);
  1606. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,NR_ESP));
  1607. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  1608. list.concat(Taicpu.op_reg(A_DEC,S_L,NR_EDI));
  1609. a_jmp_cond(list,OC_NE,again);
  1610. list.concat(Taicpu.op_const_reg(A_SUB,S_L,localsize mod winstackpagesize - 4,NR_ESP));
  1611. reference_reset_base(href,NR_ESP,localsize-4);
  1612. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,href,NR_EDI));
  1613. ungetcpuregister(list,NR_EDI);
  1614. end
  1615. end
  1616. else
  1617. {$endif NOTARGETWIN}
  1618. {$endif i386}
  1619. {$ifdef x86_64}
  1620. {$ifndef NOTARGETWIN}
  1621. { windows guards only a few pages for stack growing,
  1622. so we have to access every page first }
  1623. if (target_info.system=system_x86_64_win64) and
  1624. (localsize>=winstackpagesize) then
  1625. begin
  1626. if localsize div winstackpagesize<=5 then
  1627. begin
  1628. list.concat(Taicpu.Op_const_reg(A_SUB,S_Q,localsize,NR_RSP));
  1629. for i:=1 to localsize div winstackpagesize do
  1630. begin
  1631. reference_reset_base(href,NR_RSP,localsize-i*winstackpagesize+4);
  1632. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1633. end;
  1634. reference_reset_base(href,NR_RSP,0);
  1635. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1636. end
  1637. else
  1638. begin
  1639. current_asmdata.getjumplabel(again);
  1640. getcpuregister(list,NR_R10);
  1641. list.concat(Taicpu.op_const_reg(A_MOV,S_Q,localsize div winstackpagesize,NR_R10));
  1642. a_label(list,again);
  1643. list.concat(Taicpu.op_const_reg(A_SUB,S_Q,winstackpagesize,NR_RSP));
  1644. reference_reset_base(href,NR_RSP,0);
  1645. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1646. list.concat(Taicpu.op_reg(A_DEC,S_Q,NR_R10));
  1647. a_jmp_cond(list,OC_NE,again);
  1648. list.concat(Taicpu.op_const_reg(A_SUB,S_Q,localsize mod winstackpagesize,NR_RSP));
  1649. ungetcpuregister(list,NR_R10);
  1650. end
  1651. end
  1652. else
  1653. {$endif NOTARGETWIN}
  1654. {$endif x86_64}
  1655. list.concat(Taicpu.Op_const_reg(A_SUB,tcgsize2opsize[OS_ADDR],localsize,NR_STACK_POINTER_REG));
  1656. end;
  1657. end;
  1658. procedure tcgx86.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  1659. var
  1660. stackmisalignment: longint;
  1661. begin
  1662. {$ifdef i386}
  1663. { interrupt support for i386 }
  1664. if (po_interrupt in current_procinfo.procdef.procoptions) and
  1665. { this messes up stack alignment }
  1666. (target_info.system <> system_i386_darwin) then
  1667. begin
  1668. { .... also the segment registers }
  1669. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_GS));
  1670. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_FS));
  1671. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  1672. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  1673. { save the registers of an interrupt procedure }
  1674. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDI));
  1675. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ESI));
  1676. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  1677. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ECX));
  1678. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EBX));
  1679. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EAX));
  1680. end;
  1681. {$endif i386}
  1682. { save old framepointer }
  1683. if not nostackframe then
  1684. begin
  1685. { return address }
  1686. stackmisalignment := sizeof(aint);
  1687. list.concat(tai_regalloc.alloc(current_procinfo.framepointer,nil));
  1688. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  1689. CGmessage(cg_d_stackframe_omited)
  1690. else
  1691. begin
  1692. { push <frame_pointer> }
  1693. inc(stackmisalignment,sizeof(aint));
  1694. include(rg[R_INTREGISTER].preserved_by_proc,RS_FRAME_POINTER_REG);
  1695. list.concat(Taicpu.op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],NR_FRAME_POINTER_REG));
  1696. { Return address and FP are both on stack }
  1697. current_asmdata.asmcfi.cfa_def_cfa_offset(list,2*sizeof(aint));
  1698. current_asmdata.asmcfi.cfa_offset(list,NR_FRAME_POINTER_REG,-(2*sizeof(aint)));
  1699. list.concat(Taicpu.op_reg_reg(A_MOV,tcgsize2opsize[OS_ADDR],NR_STACK_POINTER_REG,NR_FRAME_POINTER_REG));
  1700. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_FRAME_POINTER_REG);
  1701. end;
  1702. { allocate stackframe space }
  1703. if (localsize<>0) or
  1704. ((target_info.system in [system_i386_darwin,system_x86_64_win64]) and
  1705. (stackmisalignment <> 0) and
  1706. ((pi_do_call in current_procinfo.flags) or
  1707. (po_assembler in current_procinfo.procdef.procoptions))) then
  1708. begin
  1709. if (target_info.system in [system_i386_darwin,system_x86_64_win64]) then
  1710. localsize := align(localsize+stackmisalignment,16)-stackmisalignment;
  1711. cg.g_stackpointer_alloc(list,localsize);
  1712. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  1713. current_asmdata.asmcfi.cfa_def_cfa_offset(list,localsize+sizeof(aint));
  1714. end;
  1715. end;
  1716. end;
  1717. { produces if necessary overflowcode }
  1718. procedure tcgx86.g_overflowcheck(list: TAsmList; const l:tlocation;def:tdef);
  1719. var
  1720. hl : tasmlabel;
  1721. ai : taicpu;
  1722. cond : TAsmCond;
  1723. begin
  1724. if not(cs_check_overflow in aktlocalswitches) then
  1725. exit;
  1726. current_asmdata.getjumplabel(hl);
  1727. if not ((def.deftype=pointerdef) or
  1728. ((def.deftype=orddef) and
  1729. (torddef(def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1730. bool8bit,bool16bit,bool32bit,bool64bit]))) then
  1731. cond:=C_NO
  1732. else
  1733. cond:=C_NB;
  1734. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl);
  1735. ai.SetCondition(cond);
  1736. ai.is_jmp:=true;
  1737. list.concat(ai);
  1738. a_call_name(list,'FPC_OVERFLOW');
  1739. a_label(list,hl);
  1740. end;
  1741. end.