cgx86.pas 64 KB

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