ncginl.pas 39 KB

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