cgx86.pas 63 KB

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