ncginl.pas 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Carl Eric Codere
  3. Generate generic 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 ncginl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ninl,symtype;
  22. type
  23. tcginlinenode = class(tinlinenode)
  24. procedure pass_generate_code;override;
  25. procedure pass_generate_code_cpu;virtual;
  26. procedure second_sizeoftypeof;virtual;
  27. procedure second_length;virtual;
  28. procedure second_predsucc;virtual;
  29. procedure second_incdec;virtual;
  30. procedure second_AndOrXorShiftRot_assign;virtual;
  31. procedure second_NegNot_assign;virtual;
  32. procedure second_typeinfo;virtual;
  33. procedure second_includeexclude;virtual;
  34. procedure second_pi; virtual;
  35. procedure second_arctan_real; virtual;
  36. procedure second_abs_real; virtual;
  37. procedure second_sqr_real; virtual;
  38. procedure second_sqrt_real; virtual;
  39. procedure second_ln_real; virtual;
  40. procedure second_cos_real; virtual;
  41. procedure second_sin_real; virtual;
  42. procedure second_assigned; virtual;
  43. procedure second_get_frame;virtual;
  44. procedure second_get_caller_frame;virtual;
  45. procedure second_get_caller_addr;virtual;
  46. procedure second_prefetch; virtual;
  47. procedure second_round_real; virtual;
  48. procedure second_trunc_real; virtual;
  49. procedure second_int_real; virtual;
  50. procedure second_abs_long; virtual;
  51. procedure second_rox_sar; virtual;
  52. procedure second_bsfbsr; virtual;
  53. procedure second_new; virtual;
  54. procedure second_setlength; virtual; abstract;
  55. procedure second_box; virtual; abstract;
  56. procedure second_popcnt; virtual;
  57. procedure second_seg; virtual; abstract;
  58. procedure second_fma; virtual;
  59. procedure second_frac_real; virtual;
  60. procedure second_high; virtual;
  61. procedure second_minmax; virtual;
  62. protected
  63. function second_incdec_tempregdef: tdef;virtual;
  64. end;
  65. implementation
  66. uses
  67. globtype,constexp,
  68. verbose,globals,compinnr,
  69. symconst,symdef,defutil,
  70. aasmbase,aasmdata,
  71. cgbase,pass_2,
  72. cpubase,procinfo,
  73. ncon,ncal,
  74. tgobj,ncgutil,
  75. cgutils,cgobj,hlcgobj,
  76. defcmp
  77. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  78. ,cg64f32
  79. {$endif not cpu64bitalu and not cpuhighleveltarget}
  80. ;
  81. {*****************************************************************************
  82. TCGINLINENODE
  83. *****************************************************************************}
  84. procedure tcginlinenode.pass_generate_code;
  85. begin
  86. location_reset(location,LOC_VOID,OS_NO);
  87. case inlinenumber of
  88. in_sizeof_x,
  89. in_typeof_x :
  90. second_SizeofTypeOf;
  91. in_length_x :
  92. second_Length;
  93. in_pred_x,
  94. in_succ_x:
  95. second_PredSucc;
  96. in_dec_x,
  97. in_inc_x :
  98. second_IncDec;
  99. in_typeinfo_x:
  100. second_TypeInfo;
  101. in_include_x_y,
  102. in_exclude_x_y:
  103. second_IncludeExclude;
  104. in_pi_real:
  105. second_pi;
  106. in_sin_real:
  107. second_sin_real;
  108. in_arctan_real:
  109. second_arctan_real;
  110. in_abs_real:
  111. second_abs_real;
  112. in_abs_long:
  113. second_abs_long;
  114. in_round_real:
  115. second_round_real;
  116. in_trunc_real:
  117. second_trunc_real;
  118. in_int_real:
  119. second_int_real;
  120. in_sqr_real:
  121. second_sqr_real;
  122. in_sqrt_real:
  123. second_sqrt_real;
  124. in_ln_real:
  125. second_ln_real;
  126. in_cos_real:
  127. second_cos_real;
  128. in_frac_real:
  129. second_frac_real;
  130. in_prefetch_var:
  131. second_prefetch;
  132. in_assigned_x:
  133. second_assigned;
  134. in_get_frame:
  135. second_get_frame;
  136. in_get_caller_frame:
  137. second_get_caller_frame;
  138. in_get_caller_addr:
  139. second_get_caller_addr;
  140. in_unaligned_x:
  141. begin
  142. secondpass(tcallparanode(left).left);
  143. location:=tcallparanode(left).left.location;
  144. if location.loc in [LOC_CREFERENCE,LOC_REFERENCE] then
  145. location.reference.alignment:=1;
  146. end;
  147. in_aligned_x:
  148. begin
  149. secondpass(tcallparanode(left).left);
  150. location:=tcallparanode(left).left.location;
  151. if location.loc in [LOC_CREFERENCE,LOC_REFERENCE] then
  152. location.reference.alignment:=resultdef.alignment;
  153. end;
  154. in_volatile_x:
  155. begin
  156. secondpass(tcallparanode(left).left);
  157. location:=tcallparanode(left).left.location;
  158. if location.loc in [LOC_CREFERENCE,LOC_REFERENCE,LOC_SUBSETREF,LOC_CSUBSETREF] then
  159. location.reference.volatility:=[vol_read,vol_write];
  160. end;
  161. {$ifdef SUPPORT_MMX}
  162. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  163. begin
  164. location_reset(location,LOC_MMXREGISTER,OS_NO);
  165. if left.location.loc=LOC_REGISTER then
  166. begin
  167. {!!!!!!!}
  168. end
  169. else if tcallparanode(left).left.location.loc=LOC_REGISTER then
  170. begin
  171. {!!!!!!!}
  172. end
  173. else
  174. begin
  175. {!!!!!!!}
  176. end;
  177. end;
  178. {$endif SUPPORT_MMX}
  179. in_rol_x,
  180. in_rol_x_y,
  181. in_ror_x,
  182. in_ror_x_y,
  183. in_sar_x,
  184. in_sar_x_y:
  185. second_rox_sar;
  186. in_bsf_x,
  187. in_bsr_x:
  188. second_BsfBsr;
  189. in_new_x:
  190. second_new;
  191. in_setlength_x:
  192. second_setlength;
  193. in_box_x:
  194. second_box;
  195. in_popcnt_x:
  196. second_popcnt;
  197. in_seg_x:
  198. second_seg;
  199. in_fma_single,
  200. in_fma_double,
  201. in_fma_extended,
  202. in_fma_float128:
  203. second_fma;
  204. in_max_longint,
  205. in_max_dword,
  206. in_min_longint,
  207. in_min_dword,
  208. in_min_single,
  209. in_min_double,
  210. in_max_single,
  211. in_max_double:
  212. second_minmax;
  213. in_and_assign_x_y,
  214. in_or_assign_x_y,
  215. in_xor_assign_x_y,
  216. in_sar_assign_x_y,
  217. in_shl_assign_x_y,
  218. in_shr_assign_x_y,
  219. in_rol_assign_x_y,
  220. in_ror_assign_x_y:
  221. second_AndOrXorShiftRot_assign;
  222. in_neg_assign_x,
  223. in_not_assign_x:
  224. second_NegNot_assign;
  225. in_high_x:
  226. second_high;
  227. else
  228. pass_generate_code_cpu;
  229. end;
  230. end;
  231. procedure tcginlinenode.pass_generate_code_cpu;
  232. begin
  233. Internalerror(2017110103);
  234. end;
  235. {*****************************************************************************
  236. SIZEOF / TYPEOF GENERIC HANDLING
  237. *****************************************************************************}
  238. { second_handle_ the sizeof and typeof routines }
  239. procedure tcginlinenode.second_SizeOfTypeOf;
  240. begin
  241. { handled in pass 1 }
  242. internalerror(2015122701);
  243. end;
  244. {*****************************************************************************
  245. LENGTH GENERIC HANDLING
  246. *****************************************************************************}
  247. procedure tcginlinenode.second_Length;
  248. var
  249. lengthlab : tasmlabel;
  250. hregister : tregister;
  251. lendef : tdef;
  252. href : treference;
  253. begin
  254. secondpass(left);
  255. if is_shortstring(left.resultdef) then
  256. begin
  257. location_copy(location,left.location);
  258. location.size:=OS_8;
  259. end
  260. else
  261. begin
  262. { length in ansi/wide strings and high in dynamic arrays is at offset -sizeof(pint) }
  263. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  264. current_asmdata.getjumplabel(lengthlab);
  265. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,left.resultdef,OC_EQ,0,left.location.register,lengthlab);
  266. { the length of a widestring is a 32 bit unsigned int. Since every
  267. character occupies 2 bytes, on a 32 bit platform you can express
  268. the maximum length using 31 bits. On a 64 bit platform, it may be
  269. 32 bits. This means that regardless of the platform, a location
  270. with size OS_SINT/ossinttype can hold the length without
  271. overflowing (this code returns an ossinttype value) }
  272. if is_widestring(left.resultdef) then
  273. lendef:=u32inttype
  274. else
  275. lendef:=sizesinttype;
  276. { volatility of the ansistring/widestring refers to the volatility of the
  277. string pointer, not of the string data }
  278. hlcg.reference_reset_base(href,left.resultdef,left.location.register,-lendef.size,ctempposinvalid,lendef.alignment,[]);
  279. { if the string pointer is nil, the length is 0 -> reuse the register
  280. that originally held the string pointer for the length, so that we
  281. can keep the original nil/0 as length in that case }
  282. hregister:=cg.makeregsize(current_asmdata.CurrAsmList,left.location.register,def_cgsize(resultdef));
  283. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,lendef,resultdef,href,hregister);
  284. if is_widestring(left.resultdef) then
  285. hlcg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHR,resultdef,1,hregister);
  286. { Dynamic arrays do not have their length attached but their maximum index }
  287. if is_dynamic_array(left.resultdef) then
  288. hlcg.a_op_const_reg(current_asmdata.CurrAsmList,OP_ADD,resultdef,1,hregister);
  289. cg.a_label(current_asmdata.CurrAsmList,lengthlab);
  290. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  291. location.register:=hregister;
  292. end;
  293. end;
  294. {*****************************************************************************
  295. HIGH(<dyn. array>) GENERIC HANDLING
  296. *****************************************************************************}
  297. procedure tcginlinenode.second_high;
  298. var
  299. loadlab, nillab, donelab: tasmlabel;
  300. hregister : tregister;
  301. href : treference;
  302. begin
  303. secondpass(left);
  304. if not(is_dynamic_array(left.resultdef)) then
  305. Internalerror(2019122801);
  306. { length in dynamic arrays is at offset -sizeof(pint) }
  307. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  308. current_asmdata.getjumplabel(loadlab);
  309. current_asmdata.getjumplabel(nillab);
  310. current_asmdata.getjumplabel(donelab);
  311. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,left.resultdef,OC_EQ,0,left.location.register,nillab);
  312. { volatility of the dyn. array refers to the volatility of the
  313. string pointer, not of the string data }
  314. hlcg.reference_reset_base(href,left.resultdef,left.location.register,-sizesinttype.size,ctempposinvalid,ossinttype.alignment,[]);
  315. { if the string pointer is nil, the length is 0 -> reuse the register
  316. that originally held the string pointer for the length, so that we
  317. can keep the original nil/0 as length in that case }
  318. hregister:=cg.makeregsize(current_asmdata.CurrAsmList,left.location.register,def_cgsize(resultdef));
  319. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,sizesinttype,resultdef,href,hregister);
  320. hlcg.a_jmp_always(current_asmdata.CurrAsmList,donelab);
  321. cg.a_label(current_asmdata.CurrAsmList,nillab);
  322. hlcg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,resultdef,1,hregister);
  323. cg.a_label(current_asmdata.CurrAsmList,donelab);
  324. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  325. location.register:=hregister;
  326. end;
  327. {*****************************************************************************
  328. PRED/SUCC GENERIC HANDLING
  329. *****************************************************************************}
  330. procedure tcginlinenode.second_PredSucc;
  331. var
  332. cgop : topcg;
  333. begin
  334. secondpass(left);
  335. if inlinenumber=in_pred_x then
  336. cgop:=OP_SUB
  337. else
  338. cgop:=OP_ADD;
  339. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  340. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  341. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  342. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  343. if def_cgsize(resultdef) in [OS_64,OS_S64] then
  344. begin
  345. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  346. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  347. cg64.a_op64_const_reg_reg(current_asmdata.CurrAsmList,cgop,def_cgsize(resultdef),1,left.location.register64,location.register64);
  348. end
  349. else
  350. {$endif not cpu64bitalu and not cpuhighleveltarget}
  351. begin
  352. location.register:=hlcg.getregisterfordef(current_asmdata.CurrAsmList,resultdef);
  353. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,cgop,resultdef,1,left.location.register,location.register);
  354. end;
  355. end;
  356. {*****************************************************************************
  357. INC/DEC GENERIC HANDLING
  358. *****************************************************************************}
  359. function tcginlinenode.second_incdec_tempregdef: tdef;
  360. begin
  361. second_incdec_tempregdef:=left.resultdef;
  362. end;
  363. procedure tcginlinenode.second_IncDec;
  364. const
  365. addsubop:array[in_inc_x..in_dec_x] of TOpCG=(OP_ADD,OP_SUB);
  366. var
  367. addvalue : TConstExprInt;
  368. addconstant : boolean;
  369. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  370. hregisterhi,
  371. {$endif not cpu64bitalu and not cpuhighleveltarget}
  372. hregister : tregister;
  373. begin
  374. { set defaults }
  375. addconstant:=true;
  376. hregister:=NR_NO;
  377. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  378. hregisterhi:=NR_NO;
  379. {$endif not cpu64bitalu and not cpuhighleveltarget}
  380. { first secondpass second argument, because if the first arg }
  381. { is used in that expression then SSL may move it to another }
  382. { register }
  383. if assigned(tcallparanode(left).right) then
  384. secondpass(tcallparanode(tcallparanode(left).right).left);
  385. { load first parameter, must be a reference }
  386. secondpass(tcallparanode(left).left);
  387. { get addvalue }
  388. case tcallparanode(left).left.resultdef.typ of
  389. orddef,
  390. enumdef :
  391. addvalue:=1;
  392. pointerdef :
  393. begin
  394. if is_void(tpointerdef(tcallparanode(left).left.resultdef).pointeddef) then
  395. addvalue:=1
  396. else
  397. addvalue:=tpointerdef(tcallparanode(left).left.resultdef).pointeddef.size;
  398. end;
  399. else
  400. internalerror(2020100814);
  401. end;
  402. { second_ argument specified?, must be a s32bit in register }
  403. if assigned(tcallparanode(left).right) then
  404. begin
  405. { when constant, just multiply the addvalue }
  406. if is_constintnode(tcallparanode(tcallparanode(left).right).left) then
  407. addvalue:=addvalue*get_ordinal_value(tcallparanode(tcallparanode(left).right).left)
  408. else if is_constpointernode(tcallparanode(tcallparanode(left).right).left) then
  409. addvalue:=addvalue*tpointerconstnode(tcallparanode(tcallparanode(left).right).left).value
  410. else
  411. begin
  412. if not(tcallparanode(tcallparanode(left).right).left.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE]) or (addvalue>1) or
  413. not(equal_defs(left.resultdef,tcallparanode(tcallparanode(left).right).left.resultdef)) then
  414. begin
  415. hlcg.location_force_reg(current_asmdata.CurrAsmList,tcallparanode(tcallparanode(left).right).left.location,tcallparanode(tcallparanode(left).right).left.resultdef,second_incdec_tempregdef,addvalue<=1);
  416. hregister:=tcallparanode(tcallparanode(left).right).left.location.register;
  417. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  418. hregisterhi:=tcallparanode(tcallparanode(left).right).left.location.register64.reghi;
  419. {$endif not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  420. { insert multiply with addvalue if its >1 }
  421. if addvalue>1 then
  422. hlcg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,left.resultdef,addvalue.svalue,hregister);
  423. end
  424. else if tcallparanode(tcallparanode(left).right).left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  425. begin
  426. hregister:=tcallparanode(tcallparanode(left).right).left.location.register;
  427. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  428. hregisterhi:=tcallparanode(tcallparanode(left).right).left.location.register64.reghi;
  429. {$endif not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  430. end;
  431. addconstant:=false;
  432. end;
  433. end;
  434. { write the add instruction }
  435. if addconstant then
  436. begin
  437. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  438. if def_cgsize(left.resultdef) in [OS_64,OS_S64] then
  439. { use addvalue.svalue here to avoid an internal error if addvalue is unsigned and overflows int64, see #35298,
  440. we are only interested in the bit pattern here }
  441. cg64.a_op64_const_loc(current_asmdata.CurrAsmList,addsubop[inlinenumber],def_cgsize(left.resultdef),addvalue.svalue,tcallparanode(left).left.location)
  442. else
  443. {$endif not cpu64bitalu and not cpuhighleveltarget}
  444. hlcg.a_op_const_loc(current_asmdata.CurrAsmList,addsubop[inlinenumber],left.resultdef,
  445. {$ifdef cpu64bitalu}
  446. aint(addvalue.svalue),
  447. {$else cpu64bitalu}
  448. longint(addvalue.svalue), // can't use aint, because it breaks 16-bit and 8-bit CPUs
  449. {$endif cpu64bitalu}
  450. tcallparanode(left).left.location);
  451. end
  452. else
  453. begin
  454. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  455. if def_cgsize(left.resultdef) in [OS_64,OS_S64] then
  456. case tcallparanode(tcallparanode(left).right).left.location.loc of
  457. LOC_REFERENCE,LOC_CREFERENCE:
  458. cg64.a_op64_ref_loc(current_asmdata.CurrAsmList,addsubop[inlinenumber],def_cgsize(left.resultdef),
  459. tcallparanode(tcallparanode(left).right).left.location.reference,tcallparanode(left).left.location);
  460. LOC_REGISTER,LOC_CREGISTER:
  461. cg64.a_op64_reg_loc(current_asmdata.CurrAsmList,addsubop[inlinenumber],def_cgsize(left.resultdef),
  462. joinreg64(hregister,hregisterhi),tcallparanode(left).left.location);
  463. else
  464. Internalerror(2020042801);
  465. end
  466. else
  467. {$endif not cpu64bitalu and not cpuhighleveltarget}
  468. case tcallparanode(tcallparanode(left).right).left.location.loc of
  469. LOC_REFERENCE,LOC_CREFERENCE:
  470. hlcg.a_op_ref_loc(current_asmdata.CurrAsmList,addsubop[inlinenumber],left.resultdef,
  471. tcallparanode(tcallparanode(left).right).left.location.reference,tcallparanode(left).left.location);
  472. LOC_REGISTER,LOC_CREGISTER:
  473. hlcg.a_op_reg_loc(current_asmdata.CurrAsmList,addsubop[inlinenumber],left.resultdef,
  474. hregister,tcallparanode(left).left.location);
  475. else
  476. Internalerror(2020042802);
  477. end;
  478. end;
  479. { no overflow checking for pointers (see ninl), and range checking }
  480. { is not applicable for them }
  481. if (tcallparanode(left).left.resultdef.typ <> pointerdef) then
  482. begin
  483. { things which can overflow must NOT pass via here, but have to be }
  484. { handled via a regular add node (conversion in tinlinenode.pass_1) }
  485. { Or someone has to rewrite the above to use a_op_const_reg_reg_ov }
  486. { and friends in case of overflow checking, and ask everyone to }
  487. { implement these methods since they don't exist for all cpus (JM) }
  488. { Similarly, range checking also has to be handled separately, }
  489. { see mantis #14841 (JM) }
  490. if ([cs_check_overflow,cs_check_range] * current_settings.localswitches <> []) then
  491. internalerror(2006111010);
  492. // cg.g_overflowcheck(current_asmdata.CurrAsmList,tcallparanode(left).left.location,tcallparanode(left).resultdef);
  493. // cg.g_rangecheck(current_asmdata.CurrAsmList,tcallparanode(left).left.location,tcallparanode(left).left.resultdef,
  494. // tcallparanode(left).left.resultdef);
  495. end;
  496. end;
  497. {*****************************************************************************
  498. AND/OR/XOR/SHIFT/ROTATE ASSIGN GENERIC HANDLING
  499. *****************************************************************************}
  500. procedure tcginlinenode.second_AndOrXorShiftRot_assign;
  501. const
  502. andorxorop:array[in_and_assign_x_y..in_ror_assign_x_y] of TOpCG=
  503. (OP_AND,OP_OR,OP_XOR,OP_SAR,OP_SHL,OP_SHR,OP_ROL,OP_ROR);
  504. var
  505. maskvalue : TConstExprInt;
  506. maskconstant : boolean;
  507. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  508. hregisterhi,
  509. {$endif not cpu64bitalu and not cpuhighleveltarget}
  510. hregister : tregister;
  511. begin
  512. { set defaults }
  513. maskconstant:=true;
  514. hregister:=NR_NO;
  515. maskvalue:=0;
  516. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  517. hregisterhi:=NR_NO;
  518. {$endif not cpu64bitalu and not cpuhighleveltarget}
  519. { first secondpass first argument, because if the second arg }
  520. { is used in that expression then SSL may move it to another }
  521. { register }
  522. secondpass(tcallparanode(left).left);
  523. { load second parameter, must be a reference }
  524. secondpass(tcallparanode(tcallparanode(left).right).left);
  525. { when constant, just get the maskvalue }
  526. if is_constintnode(tcallparanode(left).left) then
  527. maskvalue:=get_ordinal_value(tcallparanode(left).left)
  528. else
  529. begin
  530. { for shift/rotate the shift count can be of different size than the shifted variable }
  531. if inlinenumber in [in_sar_assign_x_y,in_shl_assign_x_y,in_shr_assign_x_y,in_rol_assign_x_y,in_ror_assign_x_y] then
  532. hlcg.location_force_reg(current_asmdata.CurrAsmList,tcallparanode(left).left.location,tcallparanode(left).left.resultdef,tcallparanode(left).left.resultdef,true)
  533. else
  534. hlcg.location_force_reg(current_asmdata.CurrAsmList,tcallparanode(left).left.location,tcallparanode(left).left.resultdef,tcallparanode(left).right.resultdef,true);
  535. hregister:=tcallparanode(left).left.location.register;
  536. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  537. hregisterhi:=tcallparanode(left).left.location.register64.reghi;
  538. {$endif not cpu64bitalu and not cpuhighleveltarget}
  539. maskconstant:=false;
  540. end;
  541. { write the and/or/xor/sar/shl/shr/rol/ror instruction }
  542. if maskconstant then
  543. begin
  544. if inlinenumber in [in_sar_assign_x_y,in_shl_assign_x_y,in_shr_assign_x_y,in_rol_assign_x_y,in_ror_assign_x_y] then
  545. if def_cgsize(tcallparanode(left).right.resultdef) in [OS_64,OS_S64] then
  546. maskvalue:=maskvalue and 63
  547. else
  548. maskvalue:=maskvalue and 31;
  549. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  550. if (def_cgsize(tcallparanode(left).right.resultdef) in [OS_64,OS_S64]) and
  551. (tcallparanode(tcallparanode(left).right).left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_REGISTER,LOC_CREGISTER]) then
  552. cg64.a_op64_const_loc(current_asmdata.CurrAsmList,andorxorop[inlinenumber],def_cgsize(tcallparanode(left).right.resultdef),maskvalue.svalue,tcallparanode(tcallparanode(left).right).left.location)
  553. else
  554. {$endif not cpu64bitalu and not cpuhighleveltarget}
  555. hlcg.a_op_const_loc(current_asmdata.CurrAsmList,andorxorop[inlinenumber],tcallparanode(left).right.resultdef,
  556. {$ifdef cpu64bitalu}
  557. aint(maskvalue.svalue),
  558. {$else cpu64bitalu}
  559. longint(maskvalue.svalue), // can't use aint, because it breaks 16-bit and 8-bit CPUs
  560. {$endif cpu64bitalu}
  561. tcallparanode(tcallparanode(left).right).left.location);
  562. end
  563. else
  564. begin
  565. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  566. if (def_cgsize(tcallparanode(left).right.resultdef) in [OS_64,OS_S64]) and
  567. (tcallparanode(tcallparanode(left).right).left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_REGISTER,LOC_CREGISTER]) then
  568. cg64.a_op64_reg_loc(current_asmdata.CurrAsmList,andorxorop[inlinenumber],def_cgsize(tcallparanode(left).right.resultdef),
  569. joinreg64(hregister,hregisterhi),tcallparanode(tcallparanode(left).right).left.location)
  570. else
  571. {$endif not cpu64bitalu and not cpuhighleveltarget}
  572. hlcg.a_op_reg_loc(current_asmdata.CurrAsmList,andorxorop[inlinenumber],tcallparanode(left).right.resultdef,
  573. hregister,tcallparanode(tcallparanode(left).right).left.location);
  574. end;
  575. end;
  576. {*****************************************************************************
  577. NEG/NOT ASSIGN GENERIC HANDLING
  578. *****************************************************************************}
  579. procedure tcginlinenode.second_NegNot_assign;
  580. const
  581. negnotop:array[in_neg_assign_x..in_not_assign_x] of TOpCG=(OP_NEG,OP_NOT);
  582. begin
  583. { load parameter, must be a reference }
  584. secondpass(left);
  585. location_reset(location,LOC_VOID,OS_NO);
  586. {$ifndef cpu64bitalu}
  587. if (def_cgsize(left.resultdef) in [OS_64,OS_S64]) and (left.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE]) then
  588. cg64.a_op64_loc(current_asmdata.CurrAsmList,negnotop[inlinenumber],def_cgsize(left.resultdef),left.location)
  589. else
  590. {$endif not cpu64bitalu}
  591. hlcg.a_op_loc(current_asmdata.CurrAsmList,negnotop[inlinenumber],left.resultdef,left.location);
  592. end;
  593. {*****************************************************************************
  594. TYPEINFO GENERIC HANDLING
  595. *****************************************************************************}
  596. procedure tcginlinenode.second_typeinfo;
  597. begin
  598. internalerror(2013060301);
  599. end;
  600. {*****************************************************************************
  601. INCLUDE/EXCLUDE GENERIC HANDLING
  602. *****************************************************************************}
  603. procedure tcginlinenode.second_IncludeExclude;
  604. var
  605. setpara, elepara: tnode;
  606. begin
  607. { the set }
  608. secondpass(tcallparanode(left).left);
  609. { the element to set }
  610. secondpass(tcallparanode(tcallparanode(left).right).left);
  611. setpara:=tcallparanode(left).left;
  612. elepara:=tcallparanode(tcallparanode(left).right).left;
  613. if elepara.location.loc=LOC_CONSTANT then
  614. begin
  615. hlcg.a_bit_set_const_loc(current_asmdata.CurrAsmList,(inlinenumber=in_include_x_y),
  616. setpara.resultdef,elepara.location.value-tsetdef(setpara.resultdef).setbase,setpara.location);
  617. end
  618. else
  619. begin
  620. hlcg.location_force_reg(current_asmdata.CurrAsmList,elepara.location,elepara.resultdef,u32inttype,true);
  621. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,u32inttype,elepara.location,tsetdef(setpara.resultdef).setbase);
  622. hlcg.a_bit_set_reg_loc(current_asmdata.CurrAsmList,(inlinenumber=in_include_x_y),
  623. u32inttype,setpara.resultdef,elepara.location.register,setpara.location);
  624. end;
  625. end;
  626. {*****************************************************************************
  627. FLOAT GENERIC HANDLING
  628. *****************************************************************************}
  629. {
  630. These routines all call internal RTL routines, so if they are
  631. called here, they give an internal error
  632. }
  633. procedure tcginlinenode.second_pi;
  634. begin
  635. internalerror(2002071801);
  636. end;
  637. procedure tcginlinenode.second_arctan_real;
  638. begin
  639. internalerror(2002071802);
  640. end;
  641. procedure tcginlinenode.second_abs_real;
  642. begin
  643. internalerror(2002071803);
  644. end;
  645. procedure tcginlinenode.second_round_real;
  646. begin
  647. internalerror(2002071804);
  648. end;
  649. procedure tcginlinenode.second_trunc_real;
  650. begin
  651. internalerror(2002071805);
  652. end;
  653. procedure tcginlinenode.second_int_real;
  654. begin
  655. internalerror(2016112702);
  656. end;
  657. procedure tcginlinenode.second_sqr_real;
  658. begin
  659. internalerror(2002071806);
  660. end;
  661. procedure tcginlinenode.second_sqrt_real;
  662. begin
  663. internalerror(2002071807);
  664. end;
  665. procedure tcginlinenode.second_ln_real;
  666. begin
  667. internalerror(2002071808);
  668. end;
  669. procedure tcginlinenode.second_cos_real;
  670. begin
  671. internalerror(2002071809);
  672. end;
  673. procedure tcginlinenode.second_sin_real;
  674. begin
  675. internalerror(2002071810);
  676. end;
  677. procedure tcginlinenode.second_prefetch;
  678. begin
  679. end;
  680. procedure tcginlinenode.second_frac_real;
  681. begin
  682. internalerror(2017052104);
  683. end;
  684. procedure tcginlinenode.second_abs_long;
  685. var
  686. tempreg1, tempreg2: tregister;
  687. begin
  688. secondpass(left);
  689. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  690. location:=left.location;
  691. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,left.resultdef);
  692. tempreg1:=hlcg.getintregister(current_asmdata.CurrAsmList,left.resultdef);
  693. tempreg2:=hlcg.getintregister(current_asmdata.CurrAsmList,left.resultdef);
  694. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SAR,left.resultdef,left.resultdef.size*8-1,left.location.register,tempreg1);
  695. hlcg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_XOR,left.resultdef,left.location.register,tempreg1,tempreg2);
  696. hlcg.a_op_reg_reg_reg(current_asmdata.CurrAsmlist,OP_SUB,left.resultdef,tempreg1,tempreg2,location.register);
  697. end;
  698. {*****************************************************************************
  699. ASSIGNED GENERIC HANDLING
  700. *****************************************************************************}
  701. procedure tcginlinenode.second_assigned;
  702. begin
  703. internalerror(2013091602);
  704. end;
  705. procedure Tcginlinenode.second_get_frame;
  706. begin
  707. {$if defined(x86) or defined(arm)}
  708. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  709. begin
  710. location_reset(location,LOC_CONSTANT,OS_ADDR);
  711. location.value:=0;
  712. end
  713. else
  714. {$endif defined(x86) or defined(arm)}
  715. begin
  716. location_reset(location,LOC_CREGISTER,OS_ADDR);
  717. location.register:=current_procinfo.framepointer;
  718. end;
  719. end;
  720. procedure Tcginlinenode.second_get_caller_frame;
  721. var
  722. frame_reg:Tregister;
  723. use_frame_pointer:boolean;
  724. begin
  725. frame_reg:=NR_NO;
  726. if left<>nil then
  727. begin
  728. secondpass(left);
  729. if left.location.loc=LOC_CONSTANT then
  730. use_frame_pointer:=true
  731. else
  732. begin
  733. hlcg.location_force_reg(current_asmdata.currasmlist,left.location,left.resultdef,voidpointertype,false);
  734. frame_reg:=left.location.register;
  735. use_frame_pointer:=false;
  736. end
  737. end
  738. else
  739. begin
  740. use_frame_pointer:=current_procinfo.framepointer=NR_STACK_POINTER_REG;
  741. frame_reg:=current_procinfo.framepointer;
  742. end;
  743. if use_frame_pointer then
  744. begin
  745. location_reset(location,LOC_CREGISTER,OS_ADDR);
  746. location.register:=NR_FRAME_POINTER_REG;
  747. end
  748. else
  749. begin
  750. location_reset_ref(location,LOC_REFERENCE,OS_ADDR,sizeof(pint),[]);
  751. location.reference.base:=frame_reg;
  752. end;
  753. end;
  754. procedure Tcginlinenode.second_get_caller_addr;
  755. var
  756. frame_ref:Treference;
  757. begin
  758. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  759. begin
  760. location_reset(location,LOC_REGISTER,OS_ADDR);
  761. location.register:=cg.getaddressregister(current_asmdata.currasmlist);
  762. reference_reset_base(frame_ref,NR_STACK_POINTER_REG,{current_procinfo.calc_stackframe_size}tg.lasttemp,ctempposinvalid,sizeof(pint),[]);
  763. cg.a_load_ref_reg(current_asmdata.currasmlist,OS_ADDR,OS_ADDR,frame_ref,location.register);
  764. end
  765. else
  766. begin
  767. location_reset(location,LOC_REGISTER,OS_ADDR);
  768. location.register:=cg.getaddressregister(current_asmdata.currasmlist);
  769. reference_reset_base(frame_ref,current_procinfo.framepointer,sizeof(pint),ctempposinvalid,sizeof(pint),[]);
  770. cg.a_load_ref_reg(current_asmdata.currasmlist,OS_ADDR,OS_ADDR,frame_ref,location.register);
  771. end;
  772. end;
  773. procedure tcginlinenode.second_rox_sar;
  774. var
  775. op : topcg;
  776. op1,op2 : tnode;
  777. begin
  778. { one or two parameters? }
  779. if (left.nodetype=callparan) and
  780. assigned(tcallparanode(left).right) then
  781. begin
  782. op1:=tcallparanode(tcallparanode(left).right).left;
  783. op2:=tcallparanode(left).left;
  784. secondpass(op2);
  785. end
  786. else
  787. begin
  788. op1:=left;
  789. op2:=nil;
  790. end;
  791. secondpass(op1);
  792. case inlinenumber of
  793. in_ror_x,
  794. in_ror_x_y:
  795. op:=OP_ROR;
  796. in_rol_x,
  797. in_rol_x_y:
  798. op:=OP_ROL;
  799. in_sar_x,
  800. in_sar_x_y:
  801. op:=OP_SAR;
  802. else
  803. internalerror(2013120110);
  804. end;
  805. if not(op1.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  806. hlcg.location_force_reg(current_asmdata.CurrAsmList,op1.location,op1.resultdef,resultdef,true);
  807. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  808. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  809. if def_cgsize(resultdef) in [OS_64,OS_S64] then
  810. begin
  811. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  812. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  813. end
  814. else
  815. {$endif not cpu64bitalu and not cpuhighleveltarget}
  816. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  817. if assigned(op2) then
  818. begin
  819. { rotating by a constant directly coded: }
  820. if op2.nodetype=ordconstn then
  821. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  822. if def_cgsize(resultdef) in [OS_64,OS_S64] then
  823. cg64.a_op64_const_reg_reg(current_asmdata.CurrAsmList,op,def_cgsize(resultdef),
  824. tordconstnode(op2).value.uvalue and (resultdef.size*8-1),
  825. op1.location.register64, location.register64)
  826. else
  827. {$endif not cpu64bitalu and not cpuhighleveltarget}
  828. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,op,resultdef,
  829. tordconstnode(op2).value.uvalue and (resultdef.size*8-1),
  830. op1.location.register, location.register)
  831. else
  832. begin
  833. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  834. if def_cgsize(resultdef) in [OS_64,OS_S64] then
  835. begin
  836. if not(op2.location.loc in [LOC_REGISTER,LOC_CREGISTER]) or
  837. not(equal_defs(op2.resultdef,alusinttype)) then
  838. hlcg.location_force_reg(current_asmdata.CurrAsmList,op2.location,
  839. op2.resultdef,alusinttype,true);
  840. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,op,def_cgsize(resultdef),
  841. joinreg64(op2.location.register,NR_NO),op1.location.register64,
  842. location.register64);
  843. end
  844. else
  845. {$endif not cpu64bitalu and not cpuhighleveltarget}
  846. begin
  847. if not(op2.location.loc in [LOC_REGISTER,LOC_CREGISTER]) or
  848. not(equal_defs(op2.resultdef,resultdef)) then
  849. hlcg.location_force_reg(current_asmdata.CurrAsmList,op2.location,
  850. op2.resultdef,resultdef,true);
  851. hlcg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,op,resultdef,
  852. op2.location.register,op1.location.register,
  853. location.register);
  854. end;
  855. end;
  856. end
  857. else
  858. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  859. if def_cgsize(resultdef) in [OS_64,OS_S64] then
  860. cg64.a_op64_const_reg_reg(current_asmdata.CurrAsmList,op,def_cgsize(resultdef),1,
  861. op1.location.register64,location.register64)
  862. else
  863. {$endif not cpu64bitalu and not cpuhighleveltarget}
  864. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,op,resultdef,1,
  865. op1.location.register,location.register);
  866. end;
  867. procedure tcginlinenode.second_BsfBsr;
  868. var
  869. reverse: boolean;
  870. opsize: tcgsize;
  871. begin
  872. reverse:=(inlinenumber = in_bsr_x);
  873. secondpass(left);
  874. opsize:=tcgsize2unsigned[left.location.size];
  875. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  876. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,cgsize_orddef(opsize),true);
  877. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  878. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  879. cg.a_bit_scan_reg_reg(current_asmdata.CurrAsmList,reverse,opsize,location.size,left.location.register,location.register);
  880. end;
  881. procedure tcginlinenode.second_new;
  882. begin
  883. internalerror(2011012202);
  884. end;
  885. procedure tcginlinenode.second_popcnt;
  886. begin
  887. internalerror(2012082602);
  888. end;
  889. procedure tcginlinenode.second_fma;
  890. begin
  891. internalerror(2014032701);
  892. end;
  893. procedure tcginlinenode.second_minmax;
  894. begin
  895. internalerror(2020120510);
  896. end;
  897. begin
  898. cinlinenode:=tcginlinenode;
  899. end.