ncginl.pas 40 KB

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