ncginl.pas 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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:=ossinttype;
  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,-ossinttype.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,ossinttype,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. hlcg.location_force_reg(current_asmdata.CurrAsmList,op1.location,op1.resultdef,resultdef,true);
  806. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  807. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  808. if def_cgsize(resultdef) in [OS_64,OS_S64] then
  809. begin
  810. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  811. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  812. end
  813. else
  814. {$endif not cpu64bitalu and not cpuhighleveltarget}
  815. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  816. if assigned(op2) then
  817. begin
  818. { rotating by a constant directly coded: }
  819. if op2.nodetype=ordconstn then
  820. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  821. if def_cgsize(resultdef) in [OS_64,OS_S64] then
  822. cg64.a_op64_const_reg_reg(current_asmdata.CurrAsmList,op,def_cgsize(resultdef),
  823. tordconstnode(op2).value.uvalue and (resultdef.size*8-1),
  824. op1.location.register64, location.register64)
  825. else
  826. {$endif not cpu64bitalu and not cpuhighleveltarget}
  827. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,op,resultdef,
  828. tordconstnode(op2).value.uvalue and (resultdef.size*8-1),
  829. op1.location.register, location.register)
  830. else
  831. begin
  832. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  833. if def_cgsize(resultdef) in [OS_64,OS_S64] then
  834. begin
  835. hlcg.location_force_reg(current_asmdata.CurrAsmList,op2.location,
  836. op2.resultdef,alusinttype,true);
  837. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,op,def_cgsize(resultdef),
  838. joinreg64(op2.location.register,NR_NO),op1.location.register64,
  839. location.register64);
  840. end
  841. else
  842. {$endif not cpu64bitalu and not cpuhighleveltarget}
  843. begin
  844. hlcg.location_force_reg(current_asmdata.CurrAsmList,op2.location,
  845. op2.resultdef,resultdef,true);
  846. hlcg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,op,resultdef,
  847. op2.location.register,op1.location.register,
  848. location.register);
  849. end;
  850. end;
  851. end
  852. else
  853. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  854. if def_cgsize(resultdef) in [OS_64,OS_S64] then
  855. cg64.a_op64_const_reg_reg(current_asmdata.CurrAsmList,op,def_cgsize(resultdef),1,
  856. op1.location.register64,location.register64)
  857. else
  858. {$endif not cpu64bitalu and not cpuhighleveltarget}
  859. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,op,resultdef,1,
  860. op1.location.register,location.register);
  861. end;
  862. procedure tcginlinenode.second_BsfBsr;
  863. var
  864. reverse: boolean;
  865. opsize: tcgsize;
  866. begin
  867. reverse:=(inlinenumber = in_bsr_x);
  868. secondpass(left);
  869. opsize:=tcgsize2unsigned[left.location.size];
  870. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  871. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,cgsize_orddef(opsize),true);
  872. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  873. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  874. cg.a_bit_scan_reg_reg(current_asmdata.CurrAsmList,reverse,opsize,location.size,left.location.register,location.register);
  875. end;
  876. procedure tcginlinenode.second_new;
  877. begin
  878. internalerror(2011012202);
  879. end;
  880. procedure tcginlinenode.second_popcnt;
  881. begin
  882. internalerror(2012082602);
  883. end;
  884. procedure tcginlinenode.second_fma;
  885. begin
  886. internalerror(2014032701);
  887. end;
  888. procedure tcginlinenode.second_minmax;
  889. begin
  890. internalerror(2020120501);
  891. end;
  892. begin
  893. cinlinenode:=tcginlinenode;
  894. end.