cgx86.pas 61 KB

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