cgx86.pas 71 KB

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