cgx86.pas 62 KB

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