cgx86.pas 65 KB

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