ncginl.pas 41 KB

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