cgx86.pas 60 KB

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