ncginl.pas 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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. nadd,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_int64,
  209. in_min_qword,
  210. in_max_int64,
  211. in_max_qword,
  212. in_min_single,
  213. in_min_double,
  214. in_max_single,
  215. in_max_double:
  216. second_minmax;
  217. in_and_assign_x_y,
  218. in_or_assign_x_y,
  219. in_xor_assign_x_y,
  220. in_sar_assign_x_y,
  221. in_shl_assign_x_y,
  222. in_shr_assign_x_y,
  223. in_rol_assign_x_y,
  224. in_ror_assign_x_y:
  225. second_AndOrXorShiftRot_assign;
  226. in_neg_assign_x,
  227. in_not_assign_x:
  228. second_NegNot_assign;
  229. in_high_x:
  230. second_high;
  231. else
  232. pass_generate_code_cpu;
  233. end;
  234. end;
  235. procedure tcginlinenode.pass_generate_code_cpu;
  236. begin
  237. Internalerror(2017110103);
  238. end;
  239. {*****************************************************************************
  240. SIZEOF / TYPEOF GENERIC HANDLING
  241. *****************************************************************************}
  242. { second_handle_ the sizeof and typeof routines }
  243. procedure tcginlinenode.second_SizeOfTypeOf;
  244. begin
  245. { handled in pass 1 }
  246. internalerror(2015122701);
  247. end;
  248. {*****************************************************************************
  249. LENGTH GENERIC HANDLING
  250. *****************************************************************************}
  251. procedure tcginlinenode.second_Length;
  252. var
  253. lengthlab : tasmlabel;
  254. hregister : tregister;
  255. lendef : tdef;
  256. href : treference;
  257. begin
  258. secondpass(left);
  259. if is_shortstring(left.resultdef) then
  260. begin
  261. location_copy(location,left.location);
  262. location.size:=OS_8;
  263. end
  264. else
  265. begin
  266. { length in ansi/wide strings and high in dynamic arrays is at offset -sizeof(pint) }
  267. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  268. current_asmdata.getjumplabel(lengthlab);
  269. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,left.resultdef,OC_EQ,0,left.location.register,lengthlab);
  270. { the length of a widestring is a 32 bit unsigned int. Since every
  271. character occupies 2 bytes, on a 32 bit platform you can express
  272. the maximum length using 31 bits. On a 64 bit platform, it may be
  273. 32 bits. This means that regardless of the platform, a location
  274. with size OS_SINT/ossinttype can hold the length without
  275. overflowing (this code returns an ossinttype value) }
  276. if is_widestring(left.resultdef) then
  277. lendef:=u32inttype
  278. else
  279. lendef:=sizesinttype;
  280. { volatility of the ansistring/widestring refers to the volatility of the
  281. string pointer, not of the string data }
  282. hlcg.reference_reset_base(href,left.resultdef,left.location.register,-lendef.size,ctempposinvalid,lendef.alignment,[]);
  283. { if the string pointer is nil, the length is 0 -> reuse the register
  284. that originally held the string pointer for the length, so that we
  285. can keep the original nil/0 as length in that case }
  286. hregister:=cg.makeregsize(current_asmdata.CurrAsmList,left.location.register,def_cgsize(resultdef));
  287. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,lendef,resultdef,href,hregister);
  288. if is_widestring(left.resultdef) then
  289. hlcg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHR,resultdef,1,hregister);
  290. { Dynamic arrays do not have their length attached but their maximum index }
  291. if is_dynamic_array(left.resultdef) then
  292. hlcg.a_op_const_reg(current_asmdata.CurrAsmList,OP_ADD,resultdef,1,hregister);
  293. cg.a_label(current_asmdata.CurrAsmList,lengthlab);
  294. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  295. location.register:=hregister;
  296. end;
  297. end;
  298. {*****************************************************************************
  299. HIGH(<dyn. array>) GENERIC HANDLING
  300. *****************************************************************************}
  301. procedure tcginlinenode.second_high;
  302. var
  303. loadlab, nillab, donelab: tasmlabel;
  304. hregister : tregister;
  305. href : treference;
  306. begin
  307. secondpass(left);
  308. if not(is_dynamic_array(left.resultdef)) then
  309. Internalerror(2019122801);
  310. { length in dynamic arrays is at offset -sizeof(pint) }
  311. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  312. current_asmdata.getjumplabel(loadlab);
  313. current_asmdata.getjumplabel(nillab);
  314. current_asmdata.getjumplabel(donelab);
  315. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,left.resultdef,OC_EQ,0,left.location.register,nillab);
  316. { volatility of the dyn. array refers to the volatility of the
  317. string pointer, not of the string data }
  318. hlcg.reference_reset_base(href,left.resultdef,left.location.register,-sizesinttype.size,ctempposinvalid,ossinttype.alignment,[]);
  319. { if the string pointer is nil, the length is 0 -> reuse the register
  320. that originally held the string pointer for the length, so that we
  321. can keep the original nil/0 as length in that case }
  322. hregister:=cg.makeregsize(current_asmdata.CurrAsmList,left.location.register,def_cgsize(resultdef));
  323. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,sizesinttype,resultdef,href,hregister);
  324. hlcg.a_jmp_always(current_asmdata.CurrAsmList,donelab);
  325. cg.a_label(current_asmdata.CurrAsmList,nillab);
  326. hlcg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,resultdef,1,hregister);
  327. cg.a_label(current_asmdata.CurrAsmList,donelab);
  328. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  329. location.register:=hregister;
  330. end;
  331. {*****************************************************************************
  332. PRED/SUCC GENERIC HANDLING
  333. *****************************************************************************}
  334. procedure tcginlinenode.second_PredSucc;
  335. var
  336. cgop : topcg;
  337. begin
  338. secondpass(left);
  339. if inlinenumber=in_pred_x then
  340. cgop:=OP_SUB
  341. else
  342. cgop:=OP_ADD;
  343. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  344. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  345. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  346. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  347. if def_cgsize(resultdef) in [OS_64,OS_S64] then
  348. begin
  349. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  350. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  351. cg64.a_op64_const_reg_reg(current_asmdata.CurrAsmList,cgop,def_cgsize(resultdef),1,left.location.register64,location.register64);
  352. end
  353. else
  354. {$endif not cpu64bitalu and not cpuhighleveltarget}
  355. begin
  356. location.register:=hlcg.getregisterfordef(current_asmdata.CurrAsmList,resultdef);
  357. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,cgop,resultdef,1,left.location.register,location.register);
  358. end;
  359. end;
  360. {*****************************************************************************
  361. INC/DEC GENERIC HANDLING
  362. *****************************************************************************}
  363. function tcginlinenode.second_incdec_tempregdef: tdef;
  364. begin
  365. second_incdec_tempregdef:=left.resultdef;
  366. end;
  367. procedure tcginlinenode.second_IncDec;
  368. const
  369. addsubop:array[in_inc_x..in_dec_x] of TOpCG=(OP_ADD,OP_SUB);
  370. var
  371. addvalue : TConstExprInt;
  372. addconstant : boolean;
  373. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  374. hregisterhi,
  375. {$endif not cpu64bitalu and not cpuhighleveltarget}
  376. hregister : tregister;
  377. begin
  378. { set defaults }
  379. addconstant:=true;
  380. hregister:=NR_NO;
  381. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  382. hregisterhi:=NR_NO;
  383. {$endif not cpu64bitalu and not cpuhighleveltarget}
  384. { first secondpass second argument, because if the first arg }
  385. { is used in that expression then SSL may move it to another }
  386. { register }
  387. if assigned(tcallparanode(left).right) then
  388. secondpass(tcallparanode(tcallparanode(left).right).left);
  389. { load first parameter, must be a reference }
  390. secondpass(tcallparanode(left).left);
  391. { get addvalue }
  392. case tcallparanode(left).left.resultdef.typ of
  393. orddef,
  394. enumdef :
  395. addvalue:=1;
  396. pointerdef :
  397. begin
  398. if is_void(tpointerdef(tcallparanode(left).left.resultdef).pointeddef) then
  399. addvalue:=1
  400. else
  401. addvalue:=tpointerdef(tcallparanode(left).left.resultdef).pointeddef.size;
  402. end;
  403. else
  404. internalerror(2020100814);
  405. end;
  406. { second_ argument specified?, must be a s32bit in register }
  407. if assigned(tcallparanode(left).right) then
  408. begin
  409. { when constant, just multiply the addvalue }
  410. if is_constintnode(tcallparanode(tcallparanode(left).right).left) then
  411. addvalue:=addvalue*get_ordinal_value(tcallparanode(tcallparanode(left).right).left)
  412. else if is_constpointernode(tcallparanode(tcallparanode(left).right).left) then
  413. addvalue:=addvalue*tpointerconstnode(tcallparanode(tcallparanode(left).right).left).value
  414. else
  415. begin
  416. if not(tcallparanode(tcallparanode(left).right).left.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE]) or (addvalue>1) or
  417. not(equal_defs(left.resultdef,tcallparanode(tcallparanode(left).right).left.resultdef)) then
  418. begin
  419. hlcg.location_force_reg(current_asmdata.CurrAsmList,tcallparanode(tcallparanode(left).right).left.location,tcallparanode(tcallparanode(left).right).left.resultdef,second_incdec_tempregdef,addvalue<=1);
  420. hregister:=tcallparanode(tcallparanode(left).right).left.location.register;
  421. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  422. hregisterhi:=tcallparanode(tcallparanode(left).right).left.location.register64.reghi;
  423. {$endif not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  424. { insert multiply with addvalue if its >1 }
  425. if addvalue>1 then
  426. hlcg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,left.resultdef,addvalue.svalue,hregister);
  427. end
  428. else if tcallparanode(tcallparanode(left).right).left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  429. begin
  430. hregister:=tcallparanode(tcallparanode(left).right).left.location.register;
  431. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  432. hregisterhi:=tcallparanode(tcallparanode(left).right).left.location.register64.reghi;
  433. {$endif not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  434. end;
  435. addconstant:=false;
  436. end;
  437. end;
  438. { write the add instruction }
  439. if addconstant then
  440. begin
  441. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  442. if def_cgsize(left.resultdef) in [OS_64,OS_S64] then
  443. { use addvalue.svalue here to avoid an internal error if addvalue is unsigned and overflows int64, see #35298,
  444. we are only interested in the bit pattern here }
  445. cg64.a_op64_const_loc(current_asmdata.CurrAsmList,addsubop[inlinenumber],def_cgsize(left.resultdef),addvalue.svalue,tcallparanode(left).left.location)
  446. else
  447. {$endif not cpu64bitalu and not cpuhighleveltarget}
  448. hlcg.a_op_const_loc(current_asmdata.CurrAsmList,addsubop[inlinenumber],left.resultdef,
  449. {$ifdef cpu64bitalu}
  450. aint(addvalue.svalue),
  451. {$else cpu64bitalu}
  452. longint(addvalue.svalue), // can't use aint, because it breaks 16-bit and 8-bit CPUs
  453. {$endif cpu64bitalu}
  454. tcallparanode(left).left.location);
  455. end
  456. else
  457. begin
  458. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  459. if def_cgsize(left.resultdef) in [OS_64,OS_S64] then
  460. case tcallparanode(tcallparanode(left).right).left.location.loc of
  461. LOC_REFERENCE,LOC_CREFERENCE:
  462. cg64.a_op64_ref_loc(current_asmdata.CurrAsmList,addsubop[inlinenumber],def_cgsize(left.resultdef),
  463. tcallparanode(tcallparanode(left).right).left.location.reference,tcallparanode(left).left.location);
  464. LOC_REGISTER,LOC_CREGISTER:
  465. cg64.a_op64_reg_loc(current_asmdata.CurrAsmList,addsubop[inlinenumber],def_cgsize(left.resultdef),
  466. joinreg64(hregister,hregisterhi),tcallparanode(left).left.location);
  467. else
  468. Internalerror(2020042801);
  469. end
  470. else
  471. {$endif not cpu64bitalu and not cpuhighleveltarget}
  472. case tcallparanode(tcallparanode(left).right).left.location.loc of
  473. LOC_REFERENCE,LOC_CREFERENCE:
  474. hlcg.a_op_ref_loc(current_asmdata.CurrAsmList,addsubop[inlinenumber],left.resultdef,
  475. tcallparanode(tcallparanode(left).right).left.location.reference,tcallparanode(left).left.location);
  476. LOC_REGISTER,LOC_CREGISTER:
  477. hlcg.a_op_reg_loc(current_asmdata.CurrAsmList,addsubop[inlinenumber],left.resultdef,
  478. hregister,tcallparanode(left).left.location);
  479. else
  480. Internalerror(2020042802);
  481. end;
  482. end;
  483. { no overflow checking for pointers (see ninl), and range checking }
  484. { is not applicable for them }
  485. if (tcallparanode(left).left.resultdef.typ <> pointerdef) then
  486. begin
  487. { things which can overflow must NOT pass via here, but have to be }
  488. { handled via a regular add node (conversion in tinlinenode.pass_1) }
  489. { Or someone has to rewrite the above to use a_op_const_reg_reg_ov }
  490. { and friends in case of overflow checking, and ask everyone to }
  491. { implement these methods since they don't exist for all cpus (JM) }
  492. { Similarly, range checking also has to be handled separately, }
  493. { see mantis #14841 (JM) }
  494. if ([cs_check_overflow,cs_check_range] * current_settings.localswitches <> []) then
  495. internalerror(2006111010);
  496. // cg.g_overflowcheck(current_asmdata.CurrAsmList,tcallparanode(left).left.location,tcallparanode(left).resultdef);
  497. // cg.g_rangecheck(current_asmdata.CurrAsmList,tcallparanode(left).left.location,tcallparanode(left).left.resultdef,
  498. // tcallparanode(left).left.resultdef);
  499. end;
  500. end;
  501. {*****************************************************************************
  502. AND/OR/XOR/SHIFT/ROTATE ASSIGN GENERIC HANDLING
  503. *****************************************************************************}
  504. procedure tcginlinenode.second_AndOrXorShiftRot_assign;
  505. const
  506. andorxorop:array[in_and_assign_x_y..in_ror_assign_x_y] of TOpCG=
  507. (OP_AND,OP_OR,OP_XOR,OP_SAR,OP_SHL,OP_SHR,OP_ROL,OP_ROR);
  508. var
  509. maskvalue : TConstExprInt;
  510. maskconstant : boolean;
  511. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  512. hregisterhi,
  513. {$endif not cpu64bitalu and not cpuhighleveltarget}
  514. hregister : tregister;
  515. begin
  516. { set defaults }
  517. maskconstant:=true;
  518. hregister:=NR_NO;
  519. maskvalue:=0;
  520. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  521. hregisterhi:=NR_NO;
  522. {$endif not cpu64bitalu and not cpuhighleveltarget}
  523. { first secondpass first argument, because if the second arg }
  524. { is used in that expression then SSL may move it to another }
  525. { register }
  526. secondpass(tcallparanode(left).left);
  527. { load second parameter, must be a reference }
  528. secondpass(tcallparanode(tcallparanode(left).right).left);
  529. { when constant, just get the maskvalue }
  530. if is_constintnode(tcallparanode(left).left) then
  531. maskvalue:=get_ordinal_value(tcallparanode(left).left)
  532. else
  533. begin
  534. { for shift/rotate the shift count can be of different size than the shifted variable }
  535. 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
  536. hlcg.location_force_reg(current_asmdata.CurrAsmList,tcallparanode(left).left.location,tcallparanode(left).left.resultdef,tcallparanode(left).left.resultdef,true)
  537. else
  538. hlcg.location_force_reg(current_asmdata.CurrAsmList,tcallparanode(left).left.location,tcallparanode(left).left.resultdef,tcallparanode(left).right.resultdef,true);
  539. hregister:=tcallparanode(left).left.location.register;
  540. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  541. hregisterhi:=tcallparanode(left).left.location.register64.reghi;
  542. {$endif not cpu64bitalu and not cpuhighleveltarget}
  543. maskconstant:=false;
  544. end;
  545. { write the and/or/xor/sar/shl/shr/rol/ror instruction }
  546. if maskconstant then
  547. begin
  548. 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
  549. if def_cgsize(tcallparanode(left).right.resultdef) in [OS_64,OS_S64] then
  550. maskvalue:=maskvalue and 63
  551. else
  552. maskvalue:=maskvalue and 31;
  553. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  554. if (def_cgsize(tcallparanode(left).right.resultdef) in [OS_64,OS_S64]) and
  555. (tcallparanode(tcallparanode(left).right).left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_REGISTER,LOC_CREGISTER]) then
  556. cg64.a_op64_const_loc(current_asmdata.CurrAsmList,andorxorop[inlinenumber],def_cgsize(tcallparanode(left).right.resultdef),maskvalue.svalue,tcallparanode(tcallparanode(left).right).left.location)
  557. else
  558. {$endif not cpu64bitalu and not cpuhighleveltarget}
  559. hlcg.a_op_const_loc(current_asmdata.CurrAsmList,andorxorop[inlinenumber],tcallparanode(left).right.resultdef,
  560. {$ifdef cpu64bitalu}
  561. aint(maskvalue.svalue),
  562. {$else cpu64bitalu}
  563. longint(maskvalue.svalue), // can't use aint, because it breaks 16-bit and 8-bit CPUs
  564. {$endif cpu64bitalu}
  565. tcallparanode(tcallparanode(left).right).left.location);
  566. end
  567. else
  568. begin
  569. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  570. if (def_cgsize(tcallparanode(left).right.resultdef) in [OS_64,OS_S64]) and
  571. (tcallparanode(tcallparanode(left).right).left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_REGISTER,LOC_CREGISTER]) then
  572. cg64.a_op64_reg_loc(current_asmdata.CurrAsmList,andorxorop[inlinenumber],def_cgsize(tcallparanode(left).right.resultdef),
  573. joinreg64(hregister,hregisterhi),tcallparanode(tcallparanode(left).right).left.location)
  574. else
  575. {$endif not cpu64bitalu and not cpuhighleveltarget}
  576. hlcg.a_op_reg_loc(current_asmdata.CurrAsmList,andorxorop[inlinenumber],tcallparanode(left).right.resultdef,
  577. hregister,tcallparanode(tcallparanode(left).right).left.location);
  578. end;
  579. end;
  580. {*****************************************************************************
  581. NEG/NOT ASSIGN GENERIC HANDLING
  582. *****************************************************************************}
  583. procedure tcginlinenode.second_NegNot_assign;
  584. const
  585. negnotop:array[in_neg_assign_x..in_not_assign_x] of TOpCG=(OP_NEG,OP_NOT);
  586. begin
  587. { load parameter, must be a reference }
  588. secondpass(left);
  589. location_reset(location,LOC_VOID,OS_NO);
  590. {$if not defined(cpu64bitalu) } // and not defined(cpuhighleveltarget)}
  591. if (def_cgsize(left.resultdef) in [OS_64,OS_S64]) and (left.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE]) then
  592. cg64.a_op64_loc(current_asmdata.CurrAsmList,negnotop[inlinenumber],def_cgsize(left.resultdef),left.location)
  593. else
  594. {$endif not cpu64bitalu}
  595. hlcg.a_op_loc(current_asmdata.CurrAsmList,negnotop[inlinenumber],left.resultdef,left.location);
  596. end;
  597. {*****************************************************************************
  598. TYPEINFO GENERIC HANDLING
  599. *****************************************************************************}
  600. procedure tcginlinenode.second_typeinfo;
  601. begin
  602. internalerror(2013060301);
  603. end;
  604. {*****************************************************************************
  605. INCLUDE/EXCLUDE GENERIC HANDLING
  606. *****************************************************************************}
  607. procedure tcginlinenode.second_IncludeExclude;
  608. var
  609. setpara, elepara: tnode;
  610. begin
  611. { the set }
  612. secondpass(tcallparanode(left).left);
  613. { the element to set }
  614. secondpass(tcallparanode(tcallparanode(left).right).left);
  615. setpara:=tcallparanode(left).left;
  616. elepara:=tcallparanode(tcallparanode(left).right).left;
  617. if elepara.location.loc=LOC_CONSTANT then
  618. begin
  619. hlcg.a_bit_set_const_loc(current_asmdata.CurrAsmList,(inlinenumber=in_include_x_y),
  620. setpara.resultdef,elepara.location.value-tsetdef(setpara.resultdef).setbase,setpara.location);
  621. end
  622. else
  623. begin
  624. hlcg.location_force_reg(current_asmdata.CurrAsmList,elepara.location,elepara.resultdef,u32inttype,true);
  625. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,u32inttype,elepara.location,tsetdef(setpara.resultdef).setbase);
  626. hlcg.a_bit_set_reg_loc(current_asmdata.CurrAsmList,(inlinenumber=in_include_x_y),
  627. u32inttype,setpara.resultdef,elepara.location.register,setpara.location);
  628. end;
  629. end;
  630. {*****************************************************************************
  631. FLOAT GENERIC HANDLING
  632. *****************************************************************************}
  633. {
  634. These routines all call internal RTL routines, so if they are
  635. called here, they give an internal error
  636. }
  637. procedure tcginlinenode.second_pi;
  638. begin
  639. internalerror(2002071801);
  640. end;
  641. procedure tcginlinenode.second_arctan_real;
  642. begin
  643. internalerror(2002071802);
  644. end;
  645. procedure tcginlinenode.second_abs_real;
  646. begin
  647. internalerror(2002071803);
  648. end;
  649. procedure tcginlinenode.second_round_real;
  650. begin
  651. internalerror(2002071804);
  652. end;
  653. procedure tcginlinenode.second_trunc_real;
  654. begin
  655. internalerror(2002071805);
  656. end;
  657. procedure tcginlinenode.second_int_real;
  658. begin
  659. internalerror(2016112702);
  660. end;
  661. procedure tcginlinenode.second_sqr_real;
  662. begin
  663. internalerror(2002071806);
  664. end;
  665. procedure tcginlinenode.second_sqrt_real;
  666. begin
  667. internalerror(2002071807);
  668. end;
  669. procedure tcginlinenode.second_ln_real;
  670. begin
  671. internalerror(2002071808);
  672. end;
  673. procedure tcginlinenode.second_cos_real;
  674. begin
  675. internalerror(2002071809);
  676. end;
  677. procedure tcginlinenode.second_sin_real;
  678. begin
  679. internalerror(2002071810);
  680. end;
  681. procedure tcginlinenode.second_prefetch;
  682. begin
  683. end;
  684. procedure tcginlinenode.second_frac_real;
  685. begin
  686. internalerror(2017052104);
  687. end;
  688. procedure tcginlinenode.second_abs_long;
  689. var
  690. tempreg1, tempreg2: tregister;
  691. {$if not(defined(cpu64bitalu)) and not defined(cpuhighleveltarget)}
  692. tempreg64: tregister64;
  693. {$endif not(defined(cpu64bitalu)) and not defined(cpuhighleveltarget)}
  694. ovloc: tlocation;
  695. begin
  696. secondpass(left);
  697. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  698. location:=left.location;
  699. {$if not(defined(cpu64bitalu)) and not defined(cpuhighleveltarget)}
  700. if is_64bitint(left.resultdef) then
  701. begin
  702. location:=left.location;
  703. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  704. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  705. cg64.a_load64_reg_reg(current_asmdata.CurrAsmList,left.location.register64,location.register64);
  706. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SAR,OS_32,31,left.location.register64.reghi);
  707. tempreg64.reghi:=left.location.register64.reghi;
  708. tempreg64.reglo:=left.location.register64.reghi;
  709. cg64.a_op64_reg_reg(current_asmdata.CurrAsmList,OP_XOR,def_cgsize(resultdef),tempreg64,location.register64);
  710. if cs_check_overflow in current_settings.localswitches then
  711. begin
  712. cg64.a_op64_reg_reg_reg_checkoverflow(current_asmdata.CurrAsmList,OP_SUB,def_cgsize(resultdef),tempreg64,location.register64,location.register64,true,ovloc);
  713. hlcg.g_overflowcheck_loc(current_asmdata.CurrAsmList,Location,resultdef,ovloc);
  714. end
  715. else
  716. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,OP_SUB,def_cgsize(resultdef),tempreg64,location.register64,location.register64);
  717. end
  718. else
  719. {$endif not(defined(cpu64bitalu)) and not defined(cpuhighleveltarget)}
  720. begin
  721. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,left.resultdef);
  722. tempreg1:=hlcg.getintregister(current_asmdata.CurrAsmList,left.resultdef);
  723. tempreg2:=hlcg.getintregister(current_asmdata.CurrAsmList,left.resultdef);
  724. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SAR,left.resultdef,left.resultdef.size*8-1,left.location.register,tempreg1);
  725. hlcg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_XOR,left.resultdef,left.location.register,tempreg1,tempreg2);
  726. if cs_check_overflow in current_settings.localswitches then
  727. begin
  728. hlcg.a_op_reg_reg_reg_checkoverflow(current_asmdata.CurrAsmlist,OP_SUB,resultdef,tempreg1,tempreg2,location.register,true,ovloc);
  729. hlcg.g_overflowcheck_loc(current_asmdata.CurrAsmList,Location,resultdef,ovloc);
  730. end
  731. else
  732. hlcg.a_op_reg_reg_reg(current_asmdata.CurrAsmlist,OP_SUB,resultdef,tempreg1,tempreg2,location.register);
  733. end;
  734. end;
  735. {*****************************************************************************
  736. ASSIGNED GENERIC HANDLING
  737. *****************************************************************************}
  738. procedure tcginlinenode.second_assigned;
  739. begin
  740. internalerror(2013091602);
  741. end;
  742. procedure Tcginlinenode.second_get_frame;
  743. begin
  744. {$if defined(x86) or defined(arm)}
  745. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  746. begin
  747. location_reset(location,LOC_CONSTANT,OS_ADDR);
  748. location.value:=0;
  749. end
  750. else
  751. {$endif defined(x86) or defined(arm)}
  752. begin
  753. location_reset(location,LOC_CREGISTER,OS_ADDR);
  754. location.register:=current_procinfo.framepointer;
  755. end;
  756. end;
  757. procedure Tcginlinenode.second_get_caller_frame;
  758. var
  759. frame_reg:Tregister;
  760. use_frame_pointer:boolean;
  761. begin
  762. frame_reg:=NR_NO;
  763. if left<>nil then
  764. begin
  765. secondpass(left);
  766. if left.location.loc=LOC_CONSTANT then
  767. use_frame_pointer:=true
  768. else
  769. begin
  770. hlcg.location_force_reg(current_asmdata.currasmlist,left.location,left.resultdef,voidpointertype,false);
  771. frame_reg:=left.location.register;
  772. use_frame_pointer:=false;
  773. end
  774. end
  775. else
  776. begin
  777. use_frame_pointer:=current_procinfo.framepointer=NR_STACK_POINTER_REG;
  778. frame_reg:=current_procinfo.framepointer;
  779. end;
  780. if use_frame_pointer then
  781. begin
  782. location_reset(location,LOC_CREGISTER,OS_ADDR);
  783. location.register:=NR_FRAME_POINTER_REG;
  784. end
  785. else
  786. begin
  787. location_reset_ref(location,LOC_REFERENCE,OS_ADDR,sizeof(pint),[]);
  788. location.reference.base:=frame_reg;
  789. end;
  790. end;
  791. procedure Tcginlinenode.second_get_caller_addr;
  792. var
  793. frame_ref:Treference;
  794. begin
  795. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  796. begin
  797. location_reset(location,LOC_REGISTER,OS_ADDR);
  798. location.register:=cg.getaddressregister(current_asmdata.currasmlist);
  799. reference_reset_base(frame_ref,NR_STACK_POINTER_REG,{current_procinfo.calc_stackframe_size}tg.lasttemp,ctempposinvalid,sizeof(pint),[]);
  800. cg.a_load_ref_reg(current_asmdata.currasmlist,OS_ADDR,OS_ADDR,frame_ref,location.register);
  801. end
  802. else
  803. begin
  804. location_reset(location,LOC_REGISTER,OS_ADDR);
  805. location.register:=cg.getaddressregister(current_asmdata.currasmlist);
  806. reference_reset_base(frame_ref,current_procinfo.framepointer,sizeof(pint),ctempposinvalid,sizeof(pint),[]);
  807. cg.a_load_ref_reg(current_asmdata.currasmlist,OS_ADDR,OS_ADDR,frame_ref,location.register);
  808. end;
  809. end;
  810. procedure tcginlinenode.second_rox_sar;
  811. var
  812. op : topcg;
  813. op1,op2 : tnode;
  814. begin
  815. { one or two parameters? }
  816. if (left.nodetype=callparan) and
  817. assigned(tcallparanode(left).right) then
  818. begin
  819. op1:=tcallparanode(tcallparanode(left).right).left;
  820. op2:=tcallparanode(left).left;
  821. secondpass(op2);
  822. end
  823. else
  824. begin
  825. op1:=left;
  826. op2:=nil;
  827. end;
  828. secondpass(op1);
  829. case inlinenumber of
  830. in_ror_x,
  831. in_ror_x_y:
  832. op:=OP_ROR;
  833. in_rol_x,
  834. in_rol_x_y:
  835. op:=OP_ROL;
  836. in_sar_x,
  837. in_sar_x_y:
  838. op:=OP_SAR;
  839. else
  840. internalerror(2013120110);
  841. end;
  842. if not(op1.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  843. hlcg.location_force_reg(current_asmdata.CurrAsmList,op1.location,op1.resultdef,resultdef,true);
  844. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  845. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  846. if def_cgsize(resultdef) in [OS_64,OS_S64] then
  847. begin
  848. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  849. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  850. end
  851. else
  852. {$endif not cpu64bitalu and not cpuhighleveltarget}
  853. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  854. if assigned(op2) then
  855. begin
  856. { rotating by a constant directly coded: }
  857. if op2.nodetype=ordconstn then
  858. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  859. if def_cgsize(resultdef) in [OS_64,OS_S64] then
  860. cg64.a_op64_const_reg_reg(current_asmdata.CurrAsmList,op,def_cgsize(resultdef),
  861. tordconstnode(op2).value.uvalue and (resultdef.size*8-1),
  862. op1.location.register64, location.register64)
  863. else
  864. {$endif not cpu64bitalu and not cpuhighleveltarget}
  865. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,op,resultdef,
  866. tordconstnode(op2).value.uvalue and (resultdef.size*8-1),
  867. op1.location.register, location.register)
  868. else
  869. begin
  870. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  871. if def_cgsize(resultdef) in [OS_64,OS_S64] then
  872. begin
  873. if not(op2.location.loc in [LOC_REGISTER,LOC_CREGISTER]) or
  874. not(equal_defs(op2.resultdef,alusinttype)) then
  875. hlcg.location_force_reg(current_asmdata.CurrAsmList,op2.location,
  876. op2.resultdef,alusinttype,true);
  877. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,op,def_cgsize(resultdef),
  878. joinreg64(op2.location.register,NR_NO),op1.location.register64,
  879. location.register64);
  880. end
  881. else
  882. {$endif not cpu64bitalu and not cpuhighleveltarget}
  883. begin
  884. if not(op2.location.loc in [LOC_REGISTER,LOC_CREGISTER]) or
  885. not(equal_defs(op2.resultdef,resultdef)) then
  886. hlcg.location_force_reg(current_asmdata.CurrAsmList,op2.location,
  887. op2.resultdef,resultdef,true);
  888. hlcg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,op,resultdef,
  889. op2.location.register,op1.location.register,
  890. location.register);
  891. end;
  892. end;
  893. end
  894. else
  895. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  896. if def_cgsize(resultdef) in [OS_64,OS_S64] then
  897. cg64.a_op64_const_reg_reg(current_asmdata.CurrAsmList,op,def_cgsize(resultdef),1,
  898. op1.location.register64,location.register64)
  899. else
  900. {$endif not cpu64bitalu and not cpuhighleveltarget}
  901. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,op,resultdef,1,
  902. op1.location.register,location.register);
  903. end;
  904. procedure tcginlinenode.second_BsfBsr;
  905. var
  906. not_zero,
  907. reverse: boolean;
  908. opsize: tcgsize;
  909. begin
  910. reverse:=(inlinenumber = in_bsr_x);
  911. not_zero:=(left.nodetype=orn) and (((is_constintnode(taddnode(left).left) and (tordconstnode(taddnode(left).left).value<>0))) or
  912. ((is_constintnode(taddnode(left).right) and (tordconstnode(taddnode(left).right).value<>0))));
  913. secondpass(left);
  914. opsize:=tcgsize2unsigned[left.location.size];
  915. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  916. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,cgsize_orddef(opsize),true);
  917. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  918. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  919. cg.a_bit_scan_reg_reg(current_asmdata.CurrAsmList,reverse,not_zero,opsize,location.size,left.location.register,location.register);
  920. end;
  921. procedure tcginlinenode.second_new;
  922. begin
  923. internalerror(2011012202);
  924. end;
  925. procedure tcginlinenode.second_popcnt;
  926. begin
  927. internalerror(2012082602);
  928. end;
  929. procedure tcginlinenode.second_fma;
  930. begin
  931. internalerror(2014032701);
  932. end;
  933. procedure tcginlinenode.second_minmax;
  934. begin
  935. internalerror(2020120510);
  936. end;
  937. begin
  938. cinlinenode:=tcginlinenode;
  939. end.