ncginl.pas 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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(10081);
  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. {$ifndef cpu64bitalu}
  574. var
  575. NR_NO64: tregister64=(reglo:NR_NO;reghi:NR_NO);
  576. {$endif not cpu64bitalu}
  577. begin
  578. { load parameter, must be a reference }
  579. secondpass(left);
  580. location_reset(location,LOC_VOID,OS_NO);
  581. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  582. begin
  583. {$ifndef cpu64bitalu}
  584. if def_cgsize(left.resultdef) in [OS_64,OS_S64] then
  585. cg64.a_op64_reg_loc(current_asmdata.CurrAsmList,negnotop[inlinenumber],def_cgsize(left.resultdef),left.location.register64,left.location)
  586. else
  587. {$endif not cpu64bitalu}
  588. hlcg.a_op_reg_loc(current_asmdata.CurrAsmList,negnotop[inlinenumber],left.resultdef,left.location.register,left.location);
  589. end
  590. else if left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  591. begin
  592. {$ifndef cpu64bitalu}
  593. if def_cgsize(left.resultdef) in [OS_64,OS_S64] then
  594. cg64.a_op64_reg_loc(current_asmdata.CurrAsmList,negnotop[inlinenumber],def_cgsize(left.resultdef),NR_NO64,left.location)
  595. else
  596. {$endif not cpu64bitalu}
  597. hlcg.a_op_reg_loc(current_asmdata.CurrAsmList,negnotop[inlinenumber],left.resultdef,NR_NO,left.location);
  598. end
  599. else
  600. internalerror(2017040701);
  601. end;
  602. {*****************************************************************************
  603. TYPEINFO GENERIC HANDLING
  604. *****************************************************************************}
  605. procedure tcginlinenode.second_typeinfo;
  606. begin
  607. internalerror(2013060301);
  608. end;
  609. {*****************************************************************************
  610. INCLUDE/EXCLUDE GENERIC HANDLING
  611. *****************************************************************************}
  612. procedure tcginlinenode.second_IncludeExclude;
  613. var
  614. setpara, elepara: tnode;
  615. begin
  616. { the set }
  617. secondpass(tcallparanode(left).left);
  618. { the element to set }
  619. secondpass(tcallparanode(tcallparanode(left).right).left);
  620. setpara:=tcallparanode(left).left;
  621. elepara:=tcallparanode(tcallparanode(left).right).left;
  622. if elepara.location.loc=LOC_CONSTANT then
  623. begin
  624. hlcg.a_bit_set_const_loc(current_asmdata.CurrAsmList,(inlinenumber=in_include_x_y),
  625. setpara.resultdef,elepara.location.value-tsetdef(setpara.resultdef).setbase,setpara.location);
  626. end
  627. else
  628. begin
  629. hlcg.location_force_reg(current_asmdata.CurrAsmList,elepara.location,elepara.resultdef,u32inttype,true);
  630. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,u32inttype,elepara.location,tsetdef(setpara.resultdef).setbase);
  631. hlcg.a_bit_set_reg_loc(current_asmdata.CurrAsmList,(inlinenumber=in_include_x_y),
  632. u32inttype,setpara.resultdef,elepara.location.register,setpara.location);
  633. end;
  634. end;
  635. {*****************************************************************************
  636. FLOAT GENERIC HANDLING
  637. *****************************************************************************}
  638. {
  639. These routines all call internal RTL routines, so if they are
  640. called here, they give an internal error
  641. }
  642. procedure tcginlinenode.second_pi;
  643. begin
  644. internalerror(20020718);
  645. end;
  646. procedure tcginlinenode.second_arctan_real;
  647. begin
  648. internalerror(20020718);
  649. end;
  650. procedure tcginlinenode.second_abs_real;
  651. begin
  652. internalerror(20020718);
  653. end;
  654. procedure tcginlinenode.second_round_real;
  655. begin
  656. internalerror(20020718);
  657. end;
  658. procedure tcginlinenode.second_trunc_real;
  659. begin
  660. internalerror(20020718);
  661. end;
  662. procedure tcginlinenode.second_int_real;
  663. begin
  664. internalerror(2016112702);
  665. end;
  666. procedure tcginlinenode.second_sqr_real;
  667. begin
  668. internalerror(20020718);
  669. end;
  670. procedure tcginlinenode.second_sqrt_real;
  671. begin
  672. internalerror(20020718);
  673. end;
  674. procedure tcginlinenode.second_ln_real;
  675. begin
  676. internalerror(20020718);
  677. end;
  678. procedure tcginlinenode.second_cos_real;
  679. begin
  680. internalerror(20020718);
  681. end;
  682. procedure tcginlinenode.second_sin_real;
  683. begin
  684. internalerror(20020718);
  685. end;
  686. procedure tcginlinenode.second_prefetch;
  687. begin
  688. end;
  689. procedure tcginlinenode.second_frac_real;
  690. begin
  691. internalerror(2017052104);
  692. end;
  693. procedure tcginlinenode.second_abs_long;
  694. var
  695. tempreg1, tempreg2: tregister;
  696. begin
  697. secondpass(left);
  698. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  699. location:=left.location;
  700. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,left.resultdef);
  701. tempreg1:=hlcg.getintregister(current_asmdata.CurrAsmList,left.resultdef);
  702. tempreg2:=hlcg.getintregister(current_asmdata.CurrAsmList,left.resultdef);
  703. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SAR,left.resultdef,left.resultdef.size*8-1,left.location.register,tempreg1);
  704. hlcg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_XOR,left.resultdef,left.location.register,tempreg1,tempreg2);
  705. hlcg.a_op_reg_reg_reg(current_asmdata.CurrAsmlist,OP_SUB,left.resultdef,tempreg1,tempreg2,location.register);
  706. end;
  707. {*****************************************************************************
  708. ASSIGNED GENERIC HANDLING
  709. *****************************************************************************}
  710. procedure tcginlinenode.second_assigned;
  711. begin
  712. internalerror(2013091602);
  713. end;
  714. procedure Tcginlinenode.second_get_frame;
  715. begin
  716. {$if defined(x86) or defined(arm)}
  717. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  718. begin
  719. location_reset(location,LOC_CONSTANT,OS_ADDR);
  720. location.value:=0;
  721. end
  722. else
  723. {$endif defined(x86) or defined(arm)}
  724. begin
  725. location_reset(location,LOC_CREGISTER,OS_ADDR);
  726. location.register:=current_procinfo.framepointer;
  727. end;
  728. end;
  729. procedure Tcginlinenode.second_get_caller_frame;
  730. var
  731. frame_reg:Tregister;
  732. use_frame_pointer:boolean;
  733. begin
  734. frame_reg:=NR_NO;
  735. if left<>nil then
  736. begin
  737. secondpass(left);
  738. if left.location.loc=LOC_CONSTANT then
  739. use_frame_pointer:=true
  740. else
  741. begin
  742. hlcg.location_force_reg(current_asmdata.currasmlist,left.location,left.resultdef,voidpointertype,false);
  743. frame_reg:=left.location.register;
  744. use_frame_pointer:=false;
  745. end
  746. end
  747. else
  748. begin
  749. use_frame_pointer:=current_procinfo.framepointer=NR_STACK_POINTER_REG;
  750. frame_reg:=current_procinfo.framepointer;
  751. end;
  752. if use_frame_pointer then
  753. begin
  754. location_reset(location,LOC_CREGISTER,OS_ADDR);
  755. location.register:=NR_FRAME_POINTER_REG;
  756. end
  757. else
  758. begin
  759. location_reset_ref(location,LOC_REFERENCE,OS_ADDR,sizeof(pint),[]);
  760. location.reference.base:=frame_reg;
  761. end;
  762. end;
  763. procedure Tcginlinenode.second_get_caller_addr;
  764. var
  765. frame_ref:Treference;
  766. begin
  767. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  768. begin
  769. location_reset(location,LOC_REGISTER,OS_ADDR);
  770. location.register:=cg.getaddressregister(current_asmdata.currasmlist);
  771. reference_reset_base(frame_ref,NR_STACK_POINTER_REG,{current_procinfo.calc_stackframe_size}tg.lasttemp,ctempposinvalid,sizeof(pint),[]);
  772. cg.a_load_ref_reg(current_asmdata.currasmlist,OS_ADDR,OS_ADDR,frame_ref,location.register);
  773. end
  774. else
  775. begin
  776. location_reset(location,LOC_REGISTER,OS_ADDR);
  777. location.register:=cg.getaddressregister(current_asmdata.currasmlist);
  778. reference_reset_base(frame_ref,current_procinfo.framepointer,sizeof(pint),ctempposinvalid,sizeof(pint),[]);
  779. cg.a_load_ref_reg(current_asmdata.currasmlist,OS_ADDR,OS_ADDR,frame_ref,location.register);
  780. end;
  781. end;
  782. procedure tcginlinenode.second_rox_sar;
  783. var
  784. op : topcg;
  785. op1,op2 : tnode;
  786. begin
  787. { one or two parameters? }
  788. if (left.nodetype=callparan) and
  789. assigned(tcallparanode(left).right) then
  790. begin
  791. op1:=tcallparanode(tcallparanode(left).right).left;
  792. op2:=tcallparanode(left).left;
  793. secondpass(op2);
  794. end
  795. else
  796. begin
  797. op1:=left;
  798. op2:=nil;
  799. end;
  800. secondpass(op1);
  801. case inlinenumber of
  802. in_ror_x,
  803. in_ror_x_y:
  804. op:=OP_ROR;
  805. in_rol_x,
  806. in_rol_x_y:
  807. op:=OP_ROL;
  808. in_sar_x,
  809. in_sar_x_y:
  810. op:=OP_SAR;
  811. else
  812. internalerror(2013120110);
  813. end;
  814. hlcg.location_force_reg(current_asmdata.CurrAsmList,op1.location,op1.resultdef,resultdef,true);
  815. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  816. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  817. if def_cgsize(resultdef) in [OS_64,OS_S64] then
  818. begin
  819. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  820. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  821. end
  822. else
  823. {$endif not cpu64bitalu and not cpuhighleveltarget}
  824. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,resultdef);
  825. if assigned(op2) then
  826. begin
  827. { rotating by a constant directly coded: }
  828. if op2.nodetype=ordconstn then
  829. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  830. if def_cgsize(resultdef) in [OS_64,OS_S64] then
  831. cg64.a_op64_const_reg_reg(current_asmdata.CurrAsmList,op,def_cgsize(resultdef),
  832. tordconstnode(op2).value.uvalue and (resultdef.size*8-1),
  833. op1.location.register64, location.register64)
  834. else
  835. {$endif not cpu64bitalu and not cpuhighleveltarget}
  836. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,op,resultdef,
  837. tordconstnode(op2).value.uvalue and (resultdef.size*8-1),
  838. op1.location.register, location.register)
  839. else
  840. begin
  841. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  842. if def_cgsize(resultdef) in [OS_64,OS_S64] then
  843. begin
  844. hlcg.location_force_reg(current_asmdata.CurrAsmList,op2.location,
  845. op2.resultdef,alusinttype,true);
  846. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,op,def_cgsize(resultdef),
  847. joinreg64(op2.location.register,NR_NO),op1.location.register64,
  848. location.register64);
  849. end
  850. else
  851. {$endif not cpu64bitalu and not cpuhighleveltarget}
  852. begin
  853. hlcg.location_force_reg(current_asmdata.CurrAsmList,op2.location,
  854. op2.resultdef,resultdef,true);
  855. hlcg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,op,resultdef,
  856. op2.location.register,op1.location.register,
  857. location.register);
  858. end;
  859. end;
  860. end
  861. else
  862. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  863. if def_cgsize(resultdef) in [OS_64,OS_S64] then
  864. cg64.a_op64_const_reg_reg(current_asmdata.CurrAsmList,op,def_cgsize(resultdef),1,
  865. op1.location.register64,location.register64)
  866. else
  867. {$endif not cpu64bitalu and not cpuhighleveltarget}
  868. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,op,resultdef,1,
  869. op1.location.register,location.register);
  870. end;
  871. procedure tcginlinenode.second_BsfBsr;
  872. var
  873. reverse: boolean;
  874. opsize: tcgsize;
  875. begin
  876. reverse:=(inlinenumber = in_bsr_x);
  877. secondpass(left);
  878. opsize:=tcgsize2unsigned[left.location.size];
  879. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  880. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,cgsize_orddef(opsize),true);
  881. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  882. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  883. cg.a_bit_scan_reg_reg(current_asmdata.CurrAsmList,reverse,opsize,location.size,left.location.register,location.register);
  884. end;
  885. procedure tcginlinenode.second_new;
  886. begin
  887. internalerror(2011012202);
  888. end;
  889. procedure tcginlinenode.second_popcnt;
  890. begin
  891. internalerror(2012082601);
  892. end;
  893. procedure tcginlinenode.second_fma;
  894. begin
  895. internalerror(2014032701);
  896. end;
  897. begin
  898. cinlinenode:=tcginlinenode;
  899. end.