nx86inl.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate x86 inline nodes
  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. unit nx86inl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ninl,ncginl;
  22. type
  23. tx86inlinenode = class(tcginlinenode)
  24. { first pass override
  25. so that the code generator will actually generate
  26. these nodes.
  27. }
  28. function first_pi: tnode ; override;
  29. function first_arctan_real: tnode; override;
  30. function first_abs_real: tnode; override;
  31. function first_sqr_real: tnode; override;
  32. function first_sqrt_real: tnode; override;
  33. function first_ln_real: tnode; override;
  34. function first_cos_real: tnode; override;
  35. function first_sin_real: tnode; override;
  36. function first_round_real: tnode; override;
  37. function first_trunc_real: tnode; override;
  38. function first_popcnt: tnode; override;
  39. { second pass override to generate these nodes }
  40. procedure second_IncludeExclude;override;
  41. procedure second_pi; override;
  42. procedure second_arctan_real; override;
  43. procedure second_abs_real; override;
  44. procedure second_round_real; override;
  45. procedure second_sqr_real; override;
  46. procedure second_sqrt_real; override;
  47. procedure second_ln_real; override;
  48. procedure second_cos_real; override;
  49. procedure second_sin_real; override;
  50. procedure second_trunc_real; override;
  51. procedure second_prefetch;override;
  52. {$ifndef i8086}
  53. procedure second_abs_long;override;
  54. {$endif not i8086}
  55. procedure second_popcnt;override;
  56. private
  57. procedure load_fpu_location(lnode: tnode);
  58. end;
  59. implementation
  60. uses
  61. systems,
  62. globtype,globals,
  63. cutils,verbose,
  64. symconst,
  65. defutil,
  66. aasmbase,aasmtai,aasmdata,aasmcpu,
  67. symtype,symdef,
  68. cgbase,pass_2,
  69. cpuinfo,cpubase,paramgr,
  70. nbas,ncon,ncal,ncnv,nld,ncgutil,
  71. tgobj,
  72. cga,cgutils,cgx86,cgobj,hlcgobj;
  73. {*****************************************************************************
  74. TX86INLINENODE
  75. *****************************************************************************}
  76. function tx86inlinenode.first_pi : tnode;
  77. begin
  78. if (tfloatdef(pbestrealtype^).floattype=s80real) then
  79. begin
  80. expectloc:=LOC_FPUREGISTER;
  81. first_pi := nil;
  82. end
  83. else
  84. result:=inherited;
  85. end;
  86. function tx86inlinenode.first_arctan_real : tnode;
  87. begin
  88. if (tfloatdef(pbestrealtype^).floattype=s80real) then
  89. begin
  90. expectloc:=LOC_FPUREGISTER;
  91. first_arctan_real := nil;
  92. end
  93. else
  94. result:=inherited;
  95. end;
  96. function tx86inlinenode.first_abs_real : tnode;
  97. begin
  98. if use_vectorfpu(resultdef) then
  99. expectloc:=LOC_MMREGISTER
  100. else
  101. expectloc:=LOC_FPUREGISTER;
  102. first_abs_real := nil;
  103. end;
  104. function tx86inlinenode.first_sqr_real : tnode;
  105. begin
  106. if use_vectorfpu(resultdef) then
  107. expectloc:=LOC_MMREGISTER
  108. else
  109. expectloc:=LOC_FPUREGISTER;
  110. first_sqr_real := nil;
  111. end;
  112. function tx86inlinenode.first_sqrt_real : tnode;
  113. begin
  114. if use_vectorfpu(resultdef) then
  115. expectloc:=LOC_MMREGISTER
  116. else
  117. expectloc:=LOC_FPUREGISTER;
  118. first_sqrt_real := nil;
  119. end;
  120. function tx86inlinenode.first_ln_real : tnode;
  121. begin
  122. if (tfloatdef(pbestrealtype^).floattype=s80real) then
  123. begin
  124. expectloc:=LOC_FPUREGISTER;
  125. first_ln_real := nil;
  126. end
  127. else
  128. result:=inherited;
  129. end;
  130. function tx86inlinenode.first_cos_real : tnode;
  131. begin
  132. {$ifdef i8086}
  133. { FCOS is 387+ }
  134. if current_settings.cputype < cpu_386 then
  135. begin
  136. result := inherited;
  137. exit;
  138. end;
  139. {$endif i8086}
  140. if (tfloatdef(pbestrealtype^).floattype=s80real) then
  141. begin
  142. expectloc:=LOC_FPUREGISTER;
  143. result:=nil;
  144. end
  145. else
  146. result:=inherited;
  147. end;
  148. function tx86inlinenode.first_sin_real : tnode;
  149. begin
  150. {$ifdef i8086}
  151. { FSIN is 387+ }
  152. if current_settings.cputype < cpu_386 then
  153. begin
  154. result := inherited;
  155. exit;
  156. end;
  157. {$endif i8086}
  158. if (tfloatdef(pbestrealtype^).floattype=s80real) then
  159. begin
  160. expectloc:=LOC_FPUREGISTER;
  161. result:=nil;
  162. end
  163. else
  164. result:=inherited;
  165. end;
  166. function tx86inlinenode.first_round_real : tnode;
  167. begin
  168. {$ifdef x86_64}
  169. if use_vectorfpu(left.resultdef) then
  170. expectloc:=LOC_REGISTER
  171. else
  172. {$endif x86_64}
  173. expectloc:=LOC_REFERENCE;
  174. result:=nil;
  175. end;
  176. function tx86inlinenode.first_trunc_real: tnode;
  177. begin
  178. if (cs_opt_size in current_settings.optimizerswitches)
  179. {$ifdef x86_64}
  180. and not(use_vectorfpu(left.resultdef))
  181. {$endif x86_64}
  182. then
  183. result:=inherited
  184. else
  185. begin
  186. {$ifdef x86_64}
  187. if use_vectorfpu(left.resultdef) then
  188. expectloc:=LOC_REGISTER
  189. else
  190. {$endif x86_64}
  191. expectloc:=LOC_REFERENCE;
  192. result:=nil;
  193. end;
  194. end;
  195. function tx86inlinenode.first_popcnt: tnode;
  196. begin
  197. Result:=nil;
  198. if (current_settings.fputype<fpu_sse42)
  199. {$ifdef i386}
  200. or is_64bit(left.resultdef)
  201. {$endif i386}
  202. then
  203. Result:=inherited first_popcnt
  204. else
  205. expectloc:=LOC_REGISTER;
  206. end;
  207. procedure tx86inlinenode.second_Pi;
  208. begin
  209. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  210. emit_none(A_FLDPI,S_NO);
  211. tcgx86(cg).inc_fpu_stack;
  212. location.register:=NR_FPU_RESULT_REG;
  213. end;
  214. { load the FPU into the an fpu register }
  215. procedure tx86inlinenode.load_fpu_location(lnode: tnode);
  216. begin
  217. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  218. location.register:=NR_FPU_RESULT_REG;
  219. secondpass(lnode);
  220. case lnode.location.loc of
  221. LOC_FPUREGISTER:
  222. ;
  223. LOC_CFPUREGISTER:
  224. begin
  225. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,lnode.location.size,
  226. lnode.location.size,lnode.location.register,location.register);
  227. end;
  228. LOC_REFERENCE,LOC_CREFERENCE:
  229. begin
  230. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,
  231. lnode.location.size,lnode.location.size,
  232. lnode.location.reference,location.register);
  233. end;
  234. LOC_MMREGISTER,LOC_CMMREGISTER:
  235. begin
  236. location:=lnode.location;
  237. location_force_fpureg(current_asmdata.CurrAsmList,location,false);
  238. end;
  239. else
  240. internalerror(309991);
  241. end;
  242. end;
  243. procedure tx86inlinenode.second_arctan_real;
  244. begin
  245. load_fpu_location(left);
  246. emit_none(A_FLD1,S_NO);
  247. emit_none(A_FPATAN,S_NO);
  248. end;
  249. procedure tx86inlinenode.second_abs_real;
  250. var
  251. href : treference;
  252. begin
  253. if use_vectorfpu(resultdef) then
  254. begin
  255. secondpass(left);
  256. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  257. location:=left.location;
  258. case tfloatdef(resultdef).floattype of
  259. s32real:
  260. reference_reset_symbol(href,current_asmdata.RefAsmSymbol('FPC_ABSMASK_SINGLE'),0,4);
  261. s64real:
  262. reference_reset_symbol(href,current_asmdata.RefAsmSymbol('FPC_ABSMASK_DOUBLE'),0,4);
  263. else
  264. internalerror(200506081);
  265. end;
  266. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList, href);
  267. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_ANDPS,S_XMM,href,location.register))
  268. end
  269. else
  270. begin
  271. load_fpu_location(left);
  272. emit_none(A_FABS,S_NO);
  273. end;
  274. end;
  275. procedure tx86inlinenode.second_round_real;
  276. begin
  277. {$ifdef x86_64}
  278. if use_vectorfpu(left.resultdef) then
  279. begin
  280. secondpass(left);
  281. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  282. location_reset(location,LOC_REGISTER,OS_S64);
  283. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_S64);
  284. if UseAVX then
  285. case left.location.size of
  286. OS_F32:
  287. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_VCVTSS2SI,S_Q,left.location.register,location.register));
  288. OS_F64:
  289. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_VCVTSD2SI,S_Q,left.location.register,location.register));
  290. else
  291. internalerror(2007031402);
  292. end
  293. else
  294. case left.location.size of
  295. OS_F32:
  296. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVTSS2SI,S_Q,left.location.register,location.register));
  297. OS_F64:
  298. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVTSD2SI,S_Q,left.location.register,location.register));
  299. else
  300. internalerror(2007031402);
  301. end;
  302. end
  303. else
  304. {$endif x86_64}
  305. begin
  306. load_fpu_location(left);
  307. location_reset_ref(location,LOC_REFERENCE,OS_S64,0);
  308. tg.GetTemp(current_asmdata.CurrAsmList,resultdef.size,resultdef.alignment,tt_normal,location.reference);
  309. emit_ref(A_FISTP,S_IQ,location.reference);
  310. tcgx86(cg).dec_fpu_stack;
  311. emit_none(A_FWAIT,S_NO);
  312. end;
  313. end;
  314. procedure tx86inlinenode.second_trunc_real;
  315. var
  316. oldcw,newcw : treference;
  317. begin
  318. {$ifdef x86_64}
  319. if use_vectorfpu(left.resultdef) and
  320. not((left.location.loc=LOC_FPUREGISTER) and (current_settings.fputype>=fpu_sse3)) then
  321. begin
  322. secondpass(left);
  323. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  324. location_reset(location,LOC_REGISTER,OS_S64);
  325. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_S64);
  326. if UseAVX then
  327. case left.location.size of
  328. OS_F32:
  329. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_VCVTTSS2SI,S_Q,left.location.register,location.register));
  330. OS_F64:
  331. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_VCVTTSD2SI,S_Q,left.location.register,location.register));
  332. else
  333. internalerror(2007031401);
  334. end
  335. else
  336. case left.location.size of
  337. OS_F32:
  338. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVTTSS2SI,S_Q,left.location.register,location.register));
  339. OS_F64:
  340. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVTTSD2SI,S_Q,left.location.register,location.register));
  341. else
  342. internalerror(2007031401);
  343. end;
  344. end
  345. else
  346. {$endif x86_64}
  347. begin
  348. if (current_settings.fputype>=fpu_sse3) then
  349. begin
  350. load_fpu_location(left);
  351. location_reset_ref(location,LOC_REFERENCE,OS_S64,0);
  352. tg.GetTemp(current_asmdata.CurrAsmList,resultdef.size,resultdef.alignment,tt_normal,location.reference);
  353. emit_ref(A_FISTTP,S_IQ,location.reference);
  354. tcgx86(cg).dec_fpu_stack;
  355. end
  356. else
  357. begin
  358. tg.GetTemp(current_asmdata.CurrAsmList,2,2,tt_normal,oldcw);
  359. tg.GetTemp(current_asmdata.CurrAsmList,2,2,tt_normal,newcw);
  360. emit_ref(A_FNSTCW,S_NO,newcw);
  361. emit_ref(A_FNSTCW,S_NO,oldcw);
  362. emit_const_ref(A_OR,S_W,$0f00,newcw);
  363. load_fpu_location(left);
  364. emit_ref(A_FLDCW,S_NO,newcw);
  365. location_reset_ref(location,LOC_REFERENCE,OS_S64,0);
  366. tg.GetTemp(current_asmdata.CurrAsmList,resultdef.size,resultdef.alignment,tt_normal,location.reference);
  367. emit_ref(A_FISTP,S_IQ,location.reference);
  368. tcgx86(cg).dec_fpu_stack;
  369. emit_ref(A_FLDCW,S_NO,oldcw);
  370. emit_none(A_FWAIT,S_NO);
  371. tg.UnGetTemp(current_asmdata.CurrAsmList,oldcw);
  372. tg.UnGetTemp(current_asmdata.CurrAsmList,newcw);
  373. end;
  374. end;
  375. end;
  376. procedure tx86inlinenode.second_sqr_real;
  377. begin
  378. if use_vectorfpu(resultdef) then
  379. begin
  380. secondpass(left);
  381. location_reset(location,LOC_MMREGISTER,left.location.size);
  382. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  383. if UseAVX then
  384. begin
  385. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  386. cg.a_opmm_reg_reg_reg(current_asmdata.CurrAsmList,OP_MUL,left.location.size,left.location.register,left.location.register,location.register,mms_movescalar);
  387. end
  388. else
  389. begin
  390. if left.location.loc in [LOC_CFPUREGISTER,LOC_FPUREGISTER] then
  391. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  392. cg.a_loadmm_loc_reg(current_asmdata.CurrAsmList,location.size,left.location,location.register,mms_movescalar);
  393. cg.a_opmm_reg_reg(current_asmdata.CurrAsmList,OP_MUL,left.location.size,location.register,location.register,mms_movescalar);
  394. end;
  395. end
  396. else
  397. begin
  398. load_fpu_location(left);
  399. emit_reg_reg(A_FMUL,S_NO,NR_ST0,NR_ST0);
  400. end;
  401. end;
  402. procedure tx86inlinenode.second_sqrt_real;
  403. begin
  404. if use_vectorfpu(resultdef) then
  405. begin
  406. secondpass(left);
  407. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  408. location_reset(location,LOC_MMREGISTER,left.location.size);
  409. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  410. if UseAVX then
  411. case tfloatdef(resultdef).floattype of
  412. s32real:
  413. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_VSQRTSS,S_XMM,left.location.register,location.register,location.register));
  414. s64real:
  415. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_VSQRTSD,S_XMM,left.location.register,location.register,location.register));
  416. else
  417. internalerror(200510031);
  418. end
  419. else
  420. case tfloatdef(resultdef).floattype of
  421. s32real:
  422. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_SQRTSS,S_XMM,left.location.register,location.register));
  423. s64real:
  424. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_SQRTSD,S_XMM,left.location.register,location.register));
  425. else
  426. internalerror(200510031);
  427. end;
  428. end
  429. else
  430. begin
  431. load_fpu_location(left);
  432. emit_none(A_FSQRT,S_NO);
  433. end;
  434. end;
  435. procedure tx86inlinenode.second_ln_real;
  436. begin
  437. load_fpu_location(left);
  438. emit_none(A_FLDLN2,S_NO);
  439. emit_none(A_FXCH,S_NO);
  440. emit_none(A_FYL2X,S_NO);
  441. end;
  442. procedure tx86inlinenode.second_cos_real;
  443. begin
  444. {$ifdef i8086}
  445. { FCOS is 387+ }
  446. if current_settings.cputype < cpu_386 then
  447. begin
  448. inherited;
  449. exit;
  450. end;
  451. {$endif i8086}
  452. load_fpu_location(left);
  453. emit_none(A_FCOS,S_NO);
  454. end;
  455. procedure tx86inlinenode.second_sin_real;
  456. begin
  457. {$ifdef i8086}
  458. { FSIN is 387+ }
  459. if current_settings.cputype < cpu_386 then
  460. begin
  461. inherited;
  462. exit;
  463. end;
  464. {$endif i8086}
  465. load_fpu_location(left);
  466. emit_none(A_FSIN,S_NO)
  467. end;
  468. procedure tx86inlinenode.second_prefetch;
  469. var
  470. ref : treference;
  471. r : tregister;
  472. begin
  473. {$if defined(i386) or defined(i8086)}
  474. if current_settings.cputype>=cpu_Pentium3 then
  475. {$endif i386 or i8086}
  476. begin
  477. secondpass(left);
  478. case left.location.loc of
  479. LOC_CREFERENCE,
  480. LOC_REFERENCE:
  481. begin
  482. r:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  483. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,r);
  484. reference_reset_base(ref,r,0,left.location.reference.alignment);
  485. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_PREFETCHNTA,S_NO,ref));
  486. end;
  487. else
  488. internalerror(200402021);
  489. end;
  490. end;
  491. end;
  492. {$ifndef i8086}
  493. procedure tx86inlinenode.second_abs_long;
  494. var
  495. hregister : tregister;
  496. opsize : tcgsize;
  497. hp : taicpu;
  498. begin
  499. {$ifdef i386}
  500. if current_settings.cputype<cpu_Pentium2 then
  501. begin
  502. opsize:=def_cgsize(left.resultdef);
  503. secondpass(left);
  504. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  505. location:=left.location;
  506. location.register:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  507. emit_reg_reg(A_MOV,S_L,left.location.register,location.register);
  508. emit_const_reg(A_SAR,tcgsize2opsize[opsize],31,left.location.register);
  509. emit_reg_reg(A_XOR,S_L,left.location.register,location.register);
  510. emit_reg_reg(A_SUB,S_L,left.location.register,location.register);
  511. end
  512. else
  513. {$endif i386}
  514. begin
  515. opsize:=def_cgsize(left.resultdef);
  516. secondpass(left);
  517. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  518. hregister:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  519. location:=left.location;
  520. location.register:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  521. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,opsize,left.location.register,hregister);
  522. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,opsize,left.location.register,location.register);
  523. emit_reg(A_NEG,tcgsize2opsize[opsize],hregister);
  524. hp:=taicpu.op_reg_reg(A_CMOVcc,tcgsize2opsize[opsize],hregister,location.register);
  525. hp.condition:=C_NS;
  526. current_asmdata.CurrAsmList.concat(hp);
  527. end;
  528. end;
  529. {$endif not i8086}
  530. {*****************************************************************************
  531. INCLUDE/EXCLUDE GENERIC HANDLING
  532. *****************************************************************************}
  533. procedure tx86inlinenode.second_IncludeExclude;
  534. var
  535. hregister,
  536. hregister2: tregister;
  537. setbase : aint;
  538. bitsperop,l : longint;
  539. cgop : topcg;
  540. asmop : tasmop;
  541. opdef : tdef;
  542. opsize,
  543. orgsize: tcgsize;
  544. begin
  545. {$ifdef i8086}
  546. { BTS and BTR are 386+ }
  547. if current_settings.cputype < cpu_386 then
  548. begin
  549. inherited;
  550. exit;
  551. end;
  552. {$endif i8086}
  553. if is_smallset(tcallparanode(left).resultdef) then
  554. begin
  555. opdef:=tcallparanode(left).resultdef;
  556. opsize:=int_cgsize(opdef.size)
  557. end
  558. else
  559. begin
  560. opdef:=u32inttype;
  561. opsize:=OS_32;
  562. end;
  563. bitsperop:=(8*tcgsize2size[opsize]);
  564. secondpass(tcallparanode(left).left);
  565. secondpass(tcallparanode(tcallparanode(left).right).left);
  566. setbase:=tsetdef(tcallparanode(left).left.resultdef).setbase;
  567. if tcallparanode(tcallparanode(left).right).left.location.loc=LOC_CONSTANT then
  568. begin
  569. { calculate bit position }
  570. l:=1 shl ((tcallparanode(tcallparanode(left).right).left.location.value-setbase) mod bitsperop);
  571. { determine operator }
  572. if inlinenumber=in_include_x_y then
  573. cgop:=OP_OR
  574. else
  575. begin
  576. cgop:=OP_AND;
  577. l:=not(l);
  578. end;
  579. case tcallparanode(left).left.location.loc of
  580. LOC_REFERENCE :
  581. begin
  582. inc(tcallparanode(left).left.location.reference.offset,
  583. ((tcallparanode(tcallparanode(left).right).left.location.value-setbase) div bitsperop)*tcgsize2size[opsize]);
  584. cg.a_op_const_ref(current_asmdata.CurrAsmList,cgop,opsize,l,tcallparanode(left).left.location.reference);
  585. end;
  586. LOC_CREGISTER :
  587. cg.a_op_const_reg(current_asmdata.CurrAsmList,cgop,tcallparanode(left).left.location.size,l,tcallparanode(left).left.location.register);
  588. else
  589. internalerror(200405022);
  590. end;
  591. end
  592. else
  593. begin
  594. orgsize:=opsize;
  595. if opsize in [OS_8,OS_S8] then
  596. begin
  597. opdef:=u32inttype;
  598. opsize:=OS_32;
  599. end;
  600. { determine asm operator }
  601. if inlinenumber=in_include_x_y then
  602. asmop:=A_BTS
  603. else
  604. asmop:=A_BTR;
  605. hlcg.location_force_reg(current_asmdata.CurrAsmList,tcallparanode(tcallparanode(left).right).left.location,tcallparanode(tcallparanode(left).right).left.resultdef,opdef,true);
  606. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,tcallparanode(tcallparanode(left).right).left.location,setbase);
  607. hregister:=tcallparanode(tcallparanode(left).right).left.location.register;
  608. if (tcallparanode(left).left.location.loc=LOC_REFERENCE) then
  609. emit_reg_ref(asmop,tcgsize2opsize[opsize],hregister,tcallparanode(left).left.location.reference)
  610. else
  611. begin
  612. { second argument can't be an 8 bit register either }
  613. hregister2:=tcallparanode(left).left.location.register;
  614. if (orgsize in [OS_8,OS_S8]) then
  615. hregister2:=cg.makeregsize(current_asmdata.CurrAsmList,hregister2,opsize);
  616. emit_reg_reg(asmop,tcgsize2opsize[opsize],hregister,hregister2);
  617. end;
  618. end;
  619. end;
  620. procedure tx86inlinenode.second_popcnt;
  621. var
  622. opsize: tcgsize;
  623. begin
  624. secondpass(left);
  625. opsize:=tcgsize2unsigned[left.location.size];
  626. { no 8 Bit popcont }
  627. if opsize=OS_8 then
  628. opsize:=OS_16;
  629. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE]) or
  630. (left.location.size<>opsize) then
  631. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,cgsize_orddef(opsize),true);
  632. location_reset(location,LOC_REGISTER,opsize);
  633. location.register:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  634. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  635. emit_reg_reg(A_POPCNT,TCGSize2OpSize[opsize],left.location.register,location.register)
  636. else
  637. emit_ref_reg(A_POPCNT,TCGSize2OpSize[opsize],left.location.reference,location.register);
  638. end;
  639. end.