ncginl.pas 41 KB

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