cgx86.pas 63 KB

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