cgx86.pas 62 KB

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