cgx86.pas 67 KB

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