cgx86.pas 64 KB

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