cgx86.pas 68 KB

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