ncginl.pas 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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. cg64.a_op64_const_loc(current_asmdata.CurrAsmList,addsubop[inlinenumber],def_cgsize(left.resultdef),addvalue,tcallparanode(left).left.location)
  418. else
  419. {$endif not cpu64bitalu and not cpuhighleveltarget}
  420. hlcg.a_op_const_loc(current_asmdata.CurrAsmList,addsubop[inlinenumber],left.resultdef,
  421. {$ifdef cpu64bitalu}
  422. aint(addvalue.svalue),
  423. {$else cpu64bitalu}
  424. longint(addvalue.svalue), // can't use aint, because it breaks 16-bit and 8-bit CPUs
  425. {$endif cpu64bitalu}
  426. tcallparanode(left).left.location);
  427. end
  428. else
  429. begin
  430. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  431. if def_cgsize(left.resultdef) in [OS_64,OS_S64] then
  432. cg64.a_op64_reg_loc(current_asmdata.CurrAsmList,addsubop[inlinenumber],def_cgsize(left.resultdef),
  433. joinreg64(hregister,hregisterhi),tcallparanode(left).left.location)
  434. else
  435. {$endif not cpu64bitalu and not cpuhighleveltarget}
  436. hlcg.a_op_reg_loc(current_asmdata.CurrAsmList,addsubop[inlinenumber],left.resultdef,
  437. hregister,tcallparanode(left).left.location);
  438. end;
  439. { no overflow checking for pointers (see ninl), and range checking }
  440. { is not applicable for them }
  441. if (tcallparanode(left).left.resultdef.typ <> pointerdef) then
  442. begin
  443. { things which can overflow must NOT pass via here, but have to be }
  444. { handled via a regular add node (conversion in tinlinenode.pass_1) }
  445. { Or someone has to rewrite the above to use a_op_const_reg_reg_ov }
  446. { and friends in case of overflow checking, and ask everyone to }
  447. { implement these methods since they don't exist for all cpus (JM) }
  448. { Similarly, range checking also has to be handled separately, }
  449. { see mantis #14841 (JM) }
  450. if ([cs_check_overflow,cs_check_range] * current_settings.localswitches <> []) then
  451. internalerror(2006111010);
  452. // cg.g_overflowcheck(current_asmdata.CurrAsmList,tcallparanode(left).left.location,tcallparanode(left).resultdef);
  453. // cg.g_rangecheck(current_asmdata.CurrAsmList,tcallparanode(left).left.location,tcallparanode(left).left.resultdef,
  454. // tcallparanode(left).left.resultdef);
  455. end;
  456. end;
  457. {*****************************************************************************
  458. AND/OR/XOR/SHIFT/ROTATE ASSIGN GENERIC HANDLING
  459. *****************************************************************************}
  460. procedure tcginlinenode.second_AndOrXorShiftRot_assign;
  461. const
  462. andorxorop:array[in_and_assign_x_y..in_ror_assign_x_y] of TOpCG=
  463. (OP_AND,OP_OR,OP_XOR,OP_SAR,OP_SHL,OP_SHR,OP_ROL,OP_ROR);
  464. var
  465. maskvalue : TConstExprInt;
  466. maskconstant : boolean;
  467. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  468. hregisterhi,
  469. {$endif not cpu64bitalu and not cpuhighleveltarget}
  470. hregister : tregister;
  471. begin
  472. { set defaults }
  473. maskconstant:=true;
  474. hregister:=NR_NO;
  475. maskvalue:=0;
  476. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  477. hregisterhi:=NR_NO;
  478. {$endif not cpu64bitalu and not cpuhighleveltarget}
  479. { first secondpass first argument, because if the second arg }
  480. { is used in that expression then SSL may move it to another }
  481. { register }
  482. secondpass(tcallparanode(left).left);
  483. { load second parameter, must be a reference }
  484. secondpass(tcallparanode(tcallparanode(left).right).left);
  485. { when constant, just get the maskvalue }
  486. if is_constintnode(tcallparanode(left).left) then
  487. maskvalue:=get_ordinal_value(tcallparanode(left).left)
  488. else
  489. begin
  490. { for shift/rotate the shift count can be of different size than the shifted variable }
  491. 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
  492. hlcg.location_force_reg(current_asmdata.CurrAsmList,tcallparanode(left).left.location,tcallparanode(left).left.resultdef,tcallparanode(left).left.resultdef,true)
  493. else
  494. hlcg.location_force_reg(current_asmdata.CurrAsmList,tcallparanode(left).left.location,tcallparanode(left).left.resultdef,tcallparanode(left).right.resultdef,true);
  495. hregister:=tcallparanode(left).left.location.register;
  496. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  497. hregisterhi:=tcallparanode(left).left.location.register64.reghi;
  498. {$endif not cpu64bitalu and not cpuhighleveltarget}
  499. maskconstant:=false;
  500. end;
  501. { write the and/or/xor/sar/shl/shr/rol/ror instruction }
  502. if maskconstant then
  503. begin
  504. 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
  505. if def_cgsize(tcallparanode(left).right.resultdef) in [OS_64,OS_S64] then
  506. maskvalue:=maskvalue and 63
  507. else
  508. maskvalue:=maskvalue and 31;
  509. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  510. if def_cgsize(tcallparanode(left).right.resultdef) in [OS_64,OS_S64] then
  511. cg64.a_op64_const_loc(current_asmdata.CurrAsmList,andorxorop[inlinenumber],def_cgsize(tcallparanode(left).right.resultdef),maskvalue.svalue,tcallparanode(tcallparanode(left).right).left.location)
  512. else
  513. {$endif not cpu64bitalu and not cpuhighleveltarget}
  514. hlcg.a_op_const_loc(current_asmdata.CurrAsmList,andorxorop[inlinenumber],tcallparanode(left).right.resultdef,
  515. {$ifdef cpu64bitalu}
  516. aint(maskvalue.svalue),
  517. {$else cpu64bitalu}
  518. longint(maskvalue.svalue), // can't use aint, because it breaks 16-bit and 8-bit CPUs
  519. {$endif cpu64bitalu}
  520. tcallparanode(tcallparanode(left).right).left.location);
  521. end
  522. else
  523. begin
  524. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  525. if def_cgsize(tcallparanode(left).right.resultdef) in [OS_64,OS_S64] then
  526. cg64.a_op64_reg_loc(current_asmdata.CurrAsmList,andorxorop[inlinenumber],def_cgsize(tcallparanode(left).right.resultdef),
  527. joinreg64(hregister,hregisterhi),tcallparanode(tcallparanode(left).right).left.location)
  528. else
  529. {$endif not cpu64bitalu and not cpuhighleveltarget}
  530. hlcg.a_op_reg_loc(current_asmdata.CurrAsmList,andorxorop[inlinenumber],tcallparanode(left).right.resultdef,
  531. hregister,tcallparanode(tcallparanode(left).right).left.location);
  532. end;
  533. end;
  534. {*****************************************************************************
  535. NEG/NOT ASSIGN GENERIC HANDLING
  536. *****************************************************************************}
  537. procedure tcginlinenode.second_NegNot_assign;
  538. const
  539. negnotop:array[in_neg_assign_x..in_not_assign_x] of TOpCG=(OP_NEG,OP_NOT);
  540. {$ifndef cpu64bitalu}
  541. var
  542. NR_NO64: tregister64=(reglo:NR_NO;reghi:NR_NO);
  543. {$endif not cpu64bitalu}
  544. begin
  545. { load parameter, must be a reference }
  546. secondpass(left);
  547. location_reset(location,LOC_VOID,OS_NO);
  548. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  549. begin
  550. {$ifndef cpu64bitalu}
  551. if def_cgsize(left.resultdef) in [OS_64,OS_S64] then
  552. cg64.a_op64_reg_loc(current_asmdata.CurrAsmList,negnotop[inlinenumber],def_cgsize(left.resultdef),left.location.register64,left.location)
  553. else
  554. {$endif not cpu64bitalu}
  555. hlcg.a_op_reg_loc(current_asmdata.CurrAsmList,negnotop[inlinenumber],left.resultdef,left.location.register,left.location);
  556. end
  557. else if left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  558. begin
  559. {$ifndef cpu64bitalu}
  560. if def_cgsize(left.resultdef) in [OS_64,OS_S64] then
  561. cg64.a_op64_reg_loc(current_asmdata.CurrAsmList,negnotop[inlinenumber],def_cgsize(left.resultdef),NR_NO64,left.location)
  562. else
  563. {$endif not cpu64bitalu}
  564. hlcg.a_op_reg_loc(current_asmdata.CurrAsmList,negnotop[inlinenumber],left.resultdef,NR_NO,left.location);
  565. end
  566. else
  567. internalerror(2017040701);
  568. end;
  569. {*****************************************************************************
  570. TYPEINFO GENERIC HANDLING
  571. *****************************************************************************}
  572. procedure tcginlinenode.second_typeinfo;
  573. begin
  574. internalerror(2013060301);
  575. end;
  576. {*****************************************************************************
  577. INCLUDE/EXCLUDE GENERIC HANDLING
  578. *****************************************************************************}
  579. procedure tcginlinenode.second_IncludeExclude;
  580. var
  581. setpara, elepara: tnode;
  582. begin
  583. { the set }
  584. secondpass(tcallparanode(left).left);
  585. { the element to set }
  586. secondpass(tcallparanode(tcallparanode(left).right).left);
  587. setpara:=tcallparanode(left).left;
  588. elepara:=tcallparanode(tcallparanode(left).right).left;
  589. if elepara.location.loc=LOC_CONSTANT then
  590. begin
  591. hlcg.a_bit_set_const_loc(current_asmdata.CurrAsmList,(inlinenumber=in_include_x_y),
  592. setpara.resultdef,elepara.location.value-tsetdef(setpara.resultdef).setbase,setpara.location);
  593. end
  594. else
  595. begin
  596. hlcg.location_force_reg(current_asmdata.CurrAsmList,elepara.location,elepara.resultdef,u32inttype,true);
  597. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,u32inttype,elepara.location,tsetdef(setpara.resultdef).setbase);
  598. hlcg.a_bit_set_reg_loc(current_asmdata.CurrAsmList,(inlinenumber=in_include_x_y),
  599. u32inttype,setpara.resultdef,elepara.location.register,setpara.location);
  600. end;
  601. end;
  602. {*****************************************************************************
  603. FLOAT GENERIC HANDLING
  604. *****************************************************************************}
  605. {
  606. These routines all call internal RTL routines, so if they are
  607. called here, they give an internal error
  608. }
  609. procedure tcginlinenode.second_pi;
  610. begin
  611. internalerror(20020718);
  612. end;
  613. procedure tcginlinenode.second_arctan_real;
  614. begin
  615. internalerror(20020718);
  616. end;
  617. procedure tcginlinenode.second_abs_real;
  618. begin
  619. internalerror(20020718);
  620. end;
  621. procedure tcginlinenode.second_round_real;
  622. begin
  623. internalerror(20020718);
  624. end;
  625. procedure tcginlinenode.second_trunc_real;
  626. begin
  627. internalerror(20020718);
  628. end;
  629. procedure tcginlinenode.second_int_real;
  630. begin
  631. internalerror(2016112702);
  632. end;
  633. procedure tcginlinenode.second_sqr_real;
  634. begin
  635. internalerror(20020718);
  636. end;
  637. procedure tcginlinenode.second_sqrt_real;
  638. begin
  639. internalerror(20020718);
  640. end;
  641. procedure tcginlinenode.second_ln_real;
  642. begin
  643. internalerror(20020718);
  644. end;
  645. procedure tcginlinenode.second_cos_real;
  646. begin
  647. internalerror(20020718);
  648. end;
  649. procedure tcginlinenode.second_sin_real;
  650. begin
  651. internalerror(20020718);
  652. end;
  653. procedure tcginlinenode.second_prefetch;
  654. begin
  655. end;
  656. procedure tcginlinenode.second_frac_real;
  657. begin
  658. internalerror(2017052104);
  659. end;
  660. procedure tcginlinenode.second_abs_long;
  661. var
  662. tempreg1, tempreg2: tregister;
  663. begin
  664. secondpass(left);
  665. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  666. location:=left.location;
  667. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,left.resultdef);
  668. tempreg1:=hlcg.getintregister(current_asmdata.CurrAsmList,left.resultdef);
  669. tempreg2:=hlcg.getintregister(current_asmdata.CurrAsmList,left.resultdef);
  670. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SAR,left.resultdef,left.resultdef.size*8-1,left.location.register,tempreg1);
  671. hlcg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_XOR,left.resultdef,left.location.register,tempreg1,tempreg2);
  672. hlcg.a_op_reg_reg_reg(current_asmdata.CurrAsmlist,OP_SUB,left.resultdef,tempreg1,tempreg2,location.register);
  673. end;
  674. {*****************************************************************************
  675. ASSIGNED GENERIC HANDLING
  676. *****************************************************************************}
  677. procedure tcginlinenode.second_assigned;
  678. begin
  679. internalerror(2013091602);
  680. end;
  681. procedure Tcginlinenode.second_get_frame;
  682. begin
  683. {$if defined(x86) or defined(arm)}
  684. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  685. begin
  686. location_reset(location,LOC_CONSTANT,OS_ADDR);
  687. location.value:=0;
  688. end
  689. else
  690. {$endif defined(x86) or defined(arm)}
  691. begin
  692. location_reset(location,LOC_CREGISTER,OS_ADDR);
  693. location.register:=current_procinfo.framepointer;
  694. end;
  695. end;
  696. procedure Tcginlinenode.second_get_caller_frame;
  697. var
  698. frame_reg:Tregister;
  699. use_frame_pointer:boolean;
  700. begin
  701. frame_reg:=NR_NO;
  702. if left<>nil then
  703. begin
  704. secondpass(left);
  705. if left.location.loc=LOC_CONSTANT then
  706. use_frame_pointer:=true
  707. else
  708. begin
  709. hlcg.location_force_reg(current_asmdata.currasmlist,left.location,left.resultdef,voidpointertype,false);
  710. frame_reg:=left.location.register;
  711. use_frame_pointer:=false;
  712. end
  713. end
  714. else
  715. begin
  716. use_frame_pointer:=current_procinfo.framepointer=NR_STACK_POINTER_REG;
  717. frame_reg:=current_procinfo.framepointer;
  718. end;
  719. if use_frame_pointer then
  720. begin
  721. location_reset(location,LOC_CREGISTER,OS_ADDR);
  722. location.register:=NR_FRAME_POINTER_REG;
  723. end
  724. else
  725. begin
  726. location_reset_ref(location,LOC_REFERENCE,OS_ADDR,sizeof(pint),[]);
  727. location.reference.base:=frame_reg;
  728. end;
  729. end;
  730. procedure Tcginlinenode.second_get_caller_addr;
  731. var
  732. frame_ref:Treference;
  733. begin
  734. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  735. begin
  736. location_reset(location,LOC_REGISTER,OS_ADDR);
  737. location.register:=cg.getaddressregister(current_asmdata.currasmlist);
  738. reference_reset_base(frame_ref,NR_STACK_POINTER_REG,{current_procinfo.calc_stackframe_size}tg.lasttemp,ctempposinvalid,sizeof(pint),[]);
  739. cg.a_load_ref_reg(current_asmdata.currasmlist,OS_ADDR,OS_ADDR,frame_ref,location.register);
  740. end
  741. else
  742. begin
  743. location_reset(location,LOC_REGISTER,OS_ADDR);
  744. location.register:=cg.getaddressregister(current_asmdata.currasmlist);
  745. reference_reset_base(frame_ref,current_procinfo.framepointer,sizeof(pint),ctempposinvalid,sizeof(pint),[]);
  746. cg.a_load_ref_reg(current_asmdata.currasmlist,OS_ADDR,OS_ADDR,frame_ref,location.register);
  747. end;
  748. end;
  749. procedure tcginlinenode.second_rox_sar;
  750. var
  751. op : topcg;
  752. op1,op2 : tnode;
  753. begin
  754. { one or two parameters? }
  755. if (left.nodetype=callparan) and
  756. assigned(tcallparanode(left).right) then
  757. begin
  758. op1:=tcallparanode(tcallparanode(left).right).left;
  759. op2:=tcallparanode(left).left;
  760. secondpass(op2);
  761. end
  762. else
  763. begin
  764. op1:=left;
  765. op2:=nil;
  766. end;
  767. secondpass(op1);
  768. case inlinenumber of
  769. in_ror_x,
  770. in_ror_x_y:
  771. op:=OP_ROR;
  772. in_rol_x,
  773. in_rol_x_y:
  774. op:=OP_ROL;
  775. in_sar_x,
  776. in_sar_x_y:
  777. op:=OP_SAR;
  778. else
  779. internalerror(2013120110);
  780. end;
  781. hlcg.location_force_reg(current_asmdata.CurrAsmList,op1.location,op1.resultdef,resultdef,true);
  782. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  783. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  784. if def_cgsize(resultdef) in [OS_64,OS_S64] then
  785. begin
  786. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  787. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  788. end
  789. else
  790. {$endif not cpu64bitalu and not cpuhighleveltarget}
  791. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  792. if assigned(op2) then
  793. begin
  794. { rotating by a constant directly coded: }
  795. if op2.nodetype=ordconstn then
  796. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  797. if def_cgsize(resultdef) in [OS_64,OS_S64] then
  798. cg64.a_op64_const_reg_reg(current_asmdata.CurrAsmList,op,def_cgsize(resultdef),
  799. tordconstnode(op2).value.uvalue and (resultdef.size*8-1),
  800. op1.location.register64, location.register64)
  801. else
  802. {$endif not cpu64bitalu and not cpuhighleveltarget}
  803. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,op,resultdef,
  804. tordconstnode(op2).value.uvalue and (resultdef.size*8-1),
  805. op1.location.register, location.register)
  806. else
  807. begin
  808. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  809. if def_cgsize(resultdef) in [OS_64,OS_S64] then
  810. begin
  811. hlcg.location_force_reg(current_asmdata.CurrAsmList,op2.location,
  812. op2.resultdef,alusinttype,true);
  813. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,op,def_cgsize(resultdef),
  814. joinreg64(op2.location.register,NR_NO),op1.location.register64,
  815. location.register64);
  816. end
  817. else
  818. {$endif not cpu64bitalu and not cpuhighleveltarget}
  819. begin
  820. hlcg.location_force_reg(current_asmdata.CurrAsmList,op2.location,
  821. op2.resultdef,resultdef,true);
  822. hlcg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,op,resultdef,
  823. op2.location.register,op1.location.register,
  824. location.register);
  825. end;
  826. end;
  827. end
  828. else
  829. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  830. if def_cgsize(resultdef) in [OS_64,OS_S64] then
  831. cg64.a_op64_const_reg_reg(current_asmdata.CurrAsmList,op,def_cgsize(resultdef),1,
  832. op1.location.register64,location.register64)
  833. else
  834. {$endif not cpu64bitalu and not cpuhighleveltarget}
  835. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,op,resultdef,1,
  836. op1.location.register,location.register);
  837. end;
  838. procedure tcginlinenode.second_BsfBsr;
  839. var
  840. reverse: boolean;
  841. opsize: tcgsize;
  842. begin
  843. reverse:=(inlinenumber = in_bsr_x);
  844. secondpass(left);
  845. opsize:=tcgsize2unsigned[left.location.size];
  846. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  847. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,cgsize_orddef(opsize),true);
  848. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  849. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  850. cg.a_bit_scan_reg_reg(current_asmdata.CurrAsmList,reverse,opsize,location.size,left.location.register,location.register);
  851. end;
  852. procedure tcginlinenode.second_new;
  853. begin
  854. internalerror(2011012202);
  855. end;
  856. procedure tcginlinenode.second_popcnt;
  857. begin
  858. internalerror(2012082601);
  859. end;
  860. procedure tcginlinenode.second_fma;
  861. begin
  862. internalerror(2014032701);
  863. end;
  864. begin
  865. cinlinenode:=tcginlinenode;
  866. end.