nmat.pas 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  1. {
  2. Copyright (c) 2000-2005 by Florian Klaempfl
  3. Type checking and register allocation for math 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 nmat;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node;
  22. type
  23. tmoddivnode = class(tbinopnode)
  24. function pass_1 : tnode;override;
  25. function pass_typecheck:tnode;override;
  26. function simplify(forinline : boolean) : tnode;override;
  27. protected
  28. { override the following if you want to implement }
  29. { parts explicitely in the code generator (JM) }
  30. function use_moddiv64bitint_helper: boolean; virtual;
  31. function first_moddiv64bitint: tnode; virtual;
  32. function firstoptimize: tnode; virtual;
  33. function first_moddivint: tnode; virtual;
  34. end;
  35. tmoddivnodeclass = class of tmoddivnode;
  36. tshlshrnode = class(tbinopnode)
  37. function pass_1 : tnode;override;
  38. function pass_typecheck:tnode;override;
  39. function simplify(forinline : boolean) : tnode;override;
  40. {$ifndef cpu64bitalu}
  41. { override the following if you want to implement }
  42. { parts explicitely in the code generator (CEC)
  43. Should return nil, if everything will be handled
  44. in the code generator
  45. }
  46. function first_shlshr64bitint: tnode; virtual;
  47. {$endif not cpu64bitalu}
  48. end;
  49. tshlshrnodeclass = class of tshlshrnode;
  50. tunaryminusnode = class(tunarynode)
  51. constructor create(expr : tnode);virtual;
  52. function pass_1 : tnode;override;
  53. function pass_typecheck:tnode;override;
  54. function simplify(forinline : boolean) : tnode;override;
  55. end;
  56. tunaryminusnodeclass = class of tunaryminusnode;
  57. tunaryplusnode = class(tunarynode)
  58. constructor create(expr : tnode);virtual;
  59. function pass_1 : tnode;override;
  60. function pass_typecheck:tnode;override;
  61. end;
  62. tunaryplusnodeclass = class of tunaryplusnode;
  63. tnotnode = class(tunarynode)
  64. constructor create(expr : tnode);virtual;
  65. function pass_1 : tnode;override;
  66. function pass_typecheck:tnode;override;
  67. function simplify(forinline : boolean) : tnode;override;
  68. {$ifdef state_tracking}
  69. function track_state_pass(exec_known:boolean):boolean;override;
  70. {$endif}
  71. end;
  72. tnotnodeclass = class of tnotnode;
  73. var
  74. cmoddivnode : tmoddivnodeclass = tmoddivnode;
  75. cshlshrnode : tshlshrnodeclass = tshlshrnode;
  76. cunaryminusnode : tunaryminusnodeclass = tunaryminusnode;
  77. cunaryplusnode : tunaryplusnodeclass = tunaryplusnode;
  78. cnotnode : tnotnodeclass = tnotnode;
  79. implementation
  80. uses
  81. systems,
  82. verbose,globals,cutils,compinnr,
  83. globtype,constexp,
  84. symconst,symtype,symdef,
  85. defutil,
  86. htypechk,pass_1,
  87. cgbase,
  88. ncon,ncnv,ncal,nadd,nld,nbas,nflw,ninl,
  89. nutils;
  90. {****************************************************************************
  91. TMODDIVNODE
  92. ****************************************************************************}
  93. function tmoddivnode.simplify(forinline : boolean):tnode;
  94. var
  95. rv,lv : tconstexprint;
  96. begin
  97. result:=nil;
  98. if is_constintnode(right) then
  99. begin
  100. rv:=tordconstnode(right).value;
  101. if rv = 1 then
  102. begin
  103. case nodetype of
  104. modn:
  105. result := cordconstnode.create(0,left.resultdef,true);
  106. divn:
  107. result := left.getcopy;
  108. end;
  109. exit;
  110. end;
  111. if rv = 0 then
  112. begin
  113. Message(parser_e_division_by_zero);
  114. { recover }
  115. tordconstnode(right).value := 1;
  116. end;
  117. { the following simplification is also required for correctness
  118. on x86, as its transformation of divisions by constants to
  119. multiplications and shifts does not handle -1 correctly }
  120. if (rv=-1) and
  121. (nodetype=divn) then
  122. begin
  123. result:=cunaryminusnode.create(left);
  124. left:=nil;
  125. exit;
  126. end;
  127. if (nf_isomod in flags) and
  128. (rv<=0) then
  129. begin
  130. Message(cg_e_mod_only_defined_for_pos_quotient);
  131. { recover }
  132. tordconstnode(right).value := 1;
  133. end;
  134. end;
  135. if is_constintnode(right) and is_constintnode(left) then
  136. begin
  137. rv:=tordconstnode(right).value;
  138. lv:=tordconstnode(left).value;
  139. case nodetype of
  140. modn:
  141. if nf_isomod in flags then
  142. begin
  143. if lv>=0 then
  144. result:=create_simplified_ord_const(lv mod rv,resultdef,forinline)
  145. else
  146. if ((-lv) mod rv)=0 then
  147. result:=create_simplified_ord_const((-lv) mod rv,resultdef,forinline)
  148. else
  149. result:=create_simplified_ord_const(rv-((-lv) mod rv),resultdef,forinline);
  150. end
  151. else
  152. result:=create_simplified_ord_const(lv mod rv,resultdef,forinline);
  153. divn:
  154. result:=create_simplified_ord_const(lv div rv,resultdef,forinline);
  155. end;
  156. end;
  157. end;
  158. function tmoddivnode.use_moddiv64bitint_helper: boolean;
  159. begin
  160. { not with an ifdef around the call to this routine, because e.g. the
  161. Java VM has a signed 64 bit division opcode, but not an unsigned
  162. one }
  163. {$ifdef cpu64bitalu}
  164. result:=false;
  165. {$else cpu64bitalu}
  166. result:=
  167. (left.resultdef.typ=orddef) and
  168. (right.resultdef.typ=orddef) and
  169. (is_64bitint(left.resultdef) or is_64bitint(right.resultdef));
  170. {$endif cpu64bitaly}
  171. end;
  172. function tmoddivnode.pass_typecheck:tnode;
  173. var
  174. else_block,
  175. hp,t : tnode;
  176. rd,ld : torddef;
  177. else_statements,
  178. statements : tstatementnode;
  179. result_data : ttempcreatenode;
  180. nd : torddef;
  181. begin
  182. result:=nil;
  183. typecheckpass(left);
  184. typecheckpass(right);
  185. { avoid any problems with type parameters later on }
  186. if is_typeparam(left.resultdef) or is_typeparam(right.resultdef) then
  187. begin
  188. resultdef:=cundefinedtype;
  189. exit;
  190. end;
  191. set_varstate(left,vs_read,[vsf_must_be_valid]);
  192. set_varstate(right,vs_read,[vsf_must_be_valid]);
  193. if codegenerror then
  194. exit;
  195. { tp procvar support }
  196. maybe_call_procvar(left,true);
  197. maybe_call_procvar(right,true);
  198. { allow operator overloading }
  199. t:=self;
  200. if isbinaryoverloaded(t) then
  201. begin
  202. result:=t;
  203. exit;
  204. end;
  205. { we need 2 orddefs always }
  206. if (left.resultdef.typ<>orddef) then
  207. inserttypeconv(left,sinttype);
  208. if (right.resultdef.typ<>orddef) then
  209. inserttypeconv(right,sinttype);
  210. if codegenerror then
  211. exit;
  212. { Try only now to simply constant
  213. as otherwise you might create
  214. tconstnode with return type that are
  215. not compatible with tconst node
  216. as in bug report 21566 PM }
  217. result:=simplify(false);
  218. if assigned(result) then
  219. exit;
  220. rd:=torddef(right.resultdef);
  221. ld:=torddef(left.resultdef);
  222. { if one operand is a cardinal and the other is a positive constant, convert the }
  223. { constant to a cardinal as well so we don't have to do a 64bit division (JM) }
  224. { Do the same for qwords and positive constants as well, otherwise things like }
  225. { "qword mod 10" are evaluated with int64 as result, which is wrong if the }
  226. { "qword" was > high(int64) (JM) }
  227. { Additionally, do the same for cardinal/qwords and other positive types, but }
  228. { always in a way that a smaller type is converted to a bigger type }
  229. { (webtbs/tw8870) }
  230. if (rd.ordtype in [u8bit,u16bit,u32bit,u64bit]) and
  231. ((is_constintnode(left) and
  232. (tordconstnode(left).value >= 0) and
  233. (tordconstnode(left).value <= get_max_value(rd))) or
  234. (not is_signed(ld) and
  235. (rd.size >= ld.size))) then
  236. begin
  237. inserttypeconv(left,right.resultdef);
  238. ld:=torddef(left.resultdef);
  239. end;
  240. if (ld.ordtype in [u8bit,u16bit,u32bit,u64bit]) and
  241. ((is_constintnode(right) and
  242. (tordconstnode(right).value >= 0) and
  243. (tordconstnode(right).value <= get_max_value(ld))) or
  244. (not is_signed(rd) and
  245. (ld.size >= rd.size))) then
  246. begin
  247. inserttypeconv(right,left.resultdef);
  248. rd:=torddef(right.resultdef);
  249. end;
  250. { when there is one currency value, everything is done
  251. using currency }
  252. if (ld.ordtype=scurrency) or
  253. (rd.ordtype=scurrency) then
  254. begin
  255. if (ld.ordtype<>scurrency) then
  256. inserttypeconv(left,s64currencytype);
  257. if (rd.ordtype<>scurrency) then
  258. inserttypeconv(right,s64currencytype);
  259. resultdef:=left.resultdef;
  260. end
  261. else
  262. { when there is one 64bit value, everything is done
  263. in 64bit }
  264. if (is_64bitint(left.resultdef) or
  265. is_64bitint(right.resultdef)) then
  266. begin
  267. if is_signed(rd) or is_signed(ld) then
  268. begin
  269. if (ld.ordtype<>s64bit) then
  270. inserttypeconv(left,s64inttype);
  271. if (rd.ordtype<>s64bit) then
  272. inserttypeconv(right,s64inttype);
  273. end
  274. else
  275. begin
  276. if (ld.ordtype<>u64bit) then
  277. inserttypeconv(left,u64inttype);
  278. if (rd.ordtype<>u64bit) then
  279. inserttypeconv(right,u64inttype);
  280. end;
  281. resultdef:=left.resultdef;
  282. end
  283. else
  284. { is there a larger than the native int? }
  285. if is_oversizedint(ld) or is_oversizedint(rd) then
  286. begin
  287. nd:=get_common_intdef(ld,rd,false);
  288. if (ld.ordtype<>nd.ordtype) then
  289. inserttypeconv(left,nd);
  290. if (rd.ordtype<>nd.ordtype) then
  291. inserttypeconv(right,nd);
  292. resultdef:=left.resultdef;
  293. end
  294. else
  295. { when mixing unsigned and signed native ints, convert everything to a larger signed type (JM) }
  296. if (is_nativeuint(rd) and
  297. is_signed(ld)) or
  298. (is_nativeuint(ld) and
  299. is_signed(rd)) then
  300. begin
  301. CGMessage(type_h_mixed_signed_unsigned);
  302. { get a signed int, larger than the native int }
  303. nd:=get_common_intdef(torddef(sinttype),torddef(uinttype),false);
  304. if (ld.ordtype<>nd.ordtype) then
  305. inserttypeconv(left,nd);
  306. if (rd.ordtype<>nd.ordtype) then
  307. inserttypeconv(right,nd);
  308. resultdef:=left.resultdef;
  309. end
  310. else
  311. begin
  312. { Make everything always default singed int }
  313. if not(rd.ordtype in [torddef(sinttype).ordtype,torddef(uinttype).ordtype]) then
  314. inserttypeconv(right,sinttype);
  315. if not(ld.ordtype in [torddef(sinttype).ordtype,torddef(uinttype).ordtype]) then
  316. inserttypeconv(left,sinttype);
  317. resultdef:=right.resultdef;
  318. end;
  319. { when the result is currency we need some extra code for
  320. division. this should not be done when the divn node is
  321. created internally }
  322. if (nodetype=divn) and
  323. not(nf_is_currency in flags) and
  324. is_currency(resultdef) then
  325. begin
  326. hp:=caddnode.create(muln,getcopy,cordconstnode.create(10000,s64currencytype,false));
  327. include(hp.flags,nf_is_currency);
  328. result:=hp;
  329. end;
  330. if (nodetype=modn) and (nf_isomod in flags) then
  331. begin
  332. result:=internalstatements(statements);
  333. else_block:=internalstatements(else_statements);
  334. result_data:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  335. { right <=0? }
  336. addstatement(statements,cifnode.create_internal(caddnode.create_internal(lten,right.getcopy,cordconstnode.create(0,resultdef,false)),
  337. { then: result:=left mod right }
  338. ccallnode.createintern('fpc_divbyzero',nil),
  339. nil
  340. ));
  341. { prepare else block }
  342. { result:=(-left) mod right }
  343. addstatement(else_statements,cassignmentnode.create(ctemprefnode.create(result_data),cmoddivnode.create(modn,cunaryminusnode.create(left.getcopy),right.getcopy)));
  344. { result<>0? }
  345. addstatement(else_statements,cifnode.create_internal(caddnode.create_internal(unequaln,ctemprefnode.create(result_data),cordconstnode.create(0,resultdef,false)),
  346. { then: result:=right-result }
  347. cassignmentnode.create_internal(ctemprefnode.create(result_data),caddnode.create_internal(subn,right.getcopy,ctemprefnode.create(result_data))),
  348. nil
  349. ));
  350. addstatement(statements,result_data);
  351. { if left>=0 }
  352. addstatement(statements,cifnode.create_internal(caddnode.create_internal(gten,left.getcopy,cordconstnode.create(0,resultdef,false)),
  353. { then: result:=left mod right }
  354. cassignmentnode.create_internal(ctemprefnode.create(result_data),cmoddivnode.create(modn,left.getcopy,right.getcopy)),
  355. { else block }
  356. else_block
  357. ));
  358. addstatement(statements,ctempdeletenode.create_normal_temp(result_data));
  359. addstatement(statements,ctemprefnode.create(result_data));
  360. end;
  361. end;
  362. function tmoddivnode.first_moddivint: tnode;
  363. {$ifdef cpuneedsdivhelper}
  364. var
  365. procname: string[31];
  366. begin
  367. result := nil;
  368. { otherwise create a call to a helper }
  369. if nodetype = divn then
  370. procname := 'fpc_div_'
  371. else
  372. procname := 'fpc_mod_';
  373. { only qword needs the unsigned code, the
  374. signed code is also used for currency }
  375. case torddef(resultdef).ordtype of
  376. u8bit:
  377. procname := procname + 'byte';
  378. s8bit:
  379. procname := procname + 'shortint';
  380. u16bit:
  381. procname := procname + 'word';
  382. s16bit:
  383. procname := procname + 'smallint';
  384. u32bit:
  385. procname := procname + 'dword';
  386. s32bit:
  387. procname := procname + 'longint'
  388. else
  389. internalerror(2015070501);
  390. end;
  391. result := ccallnode.createintern(procname,ccallparanode.create(left,
  392. ccallparanode.create(right,nil)));
  393. left := nil;
  394. right := nil;
  395. firstpass(result);
  396. if result.resultdef.typ<>orddef then
  397. internalerror(2013031701);
  398. if resultdef.typ<>orddef then
  399. internalerror(2013031701);
  400. if torddef(result.resultdef).ordtype <> torddef(resultdef).ordtype then
  401. inserttypeconv(result,resultdef);
  402. end;
  403. {$else cpuneedsdivhelper}
  404. begin
  405. result:=nil;
  406. end;
  407. {$endif cpuneedsdiv32helper}
  408. function tmoddivnode.first_moddiv64bitint: tnode;
  409. var
  410. procname: string[31];
  411. begin
  412. result := nil;
  413. { when currency is used set the result of the
  414. parameters to s64bit, so they are not converted }
  415. if is_currency(resultdef) then
  416. begin
  417. left.resultdef:=s64inttype;
  418. right.resultdef:=s64inttype;
  419. end;
  420. { otherwise create a call to a helper }
  421. if nodetype = divn then
  422. procname := 'fpc_div_'
  423. else
  424. procname := 'fpc_mod_';
  425. { only qword needs the unsigned code, the
  426. signed code is also used for currency }
  427. if is_signed(resultdef) then
  428. procname := procname + 'int64'
  429. else
  430. procname := procname + 'qword';
  431. result := ccallnode.createintern(procname,ccallparanode.create(left,
  432. ccallparanode.create(right,nil)));
  433. left := nil;
  434. right := nil;
  435. firstpass(result);
  436. end;
  437. function tmoddivnode.firstoptimize: tnode;
  438. var
  439. power,shiftval : longint;
  440. statements : tstatementnode;
  441. temp,resulttemp : ttempcreatenode;
  442. masknode : tnode;
  443. begin
  444. result := nil;
  445. { divide/mod a number by a constant which is a power of 2? }
  446. if (right.nodetype = ordconstn) and
  447. ispowerof2(tordconstnode(right).value,power) and
  448. {$ifdef cpu64bitalu}
  449. { for 64 bit, we leave the optimization to the cg }
  450. (not is_signed(resultdef)) then
  451. {$else cpu64bitalu}
  452. (((nodetype=divn) and is_oversizedord(resultdef)) or
  453. (nodetype=modn) or
  454. not is_signed(resultdef)) then
  455. {$endif cpu64bitalu}
  456. begin
  457. if nodetype=divn then
  458. begin
  459. if is_signed(resultdef) then
  460. begin
  461. if is_64bitint(left.resultdef) then
  462. if not (cs_opt_size in current_settings.optimizerswitches) then
  463. shiftval:=63
  464. else
  465. { the shift code is a lot bigger than the call to }
  466. { the divide helper }
  467. exit
  468. else
  469. shiftval:=left.resultdef.size*8-1;
  470. result:=internalstatements(statements);
  471. temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  472. resulttemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  473. addstatement(statements,resulttemp);
  474. addstatement(statements,temp);
  475. addstatement(statements,cassignmentnode.create(ctemprefnode.create(temp),
  476. left));
  477. left:=nil;
  478. { masknode is (sar(temp,shiftval) and ((1 shl power)-1))
  479. for power=1 (i.e. division by 2), masknode is simply (temp shr shiftval)}
  480. if power=1 then
  481. masknode:=
  482. cshlshrnode.create(shrn,
  483. ctemprefnode.create(temp),
  484. cordconstnode.create(shiftval,u8inttype,false)
  485. )
  486. else
  487. masknode:=
  488. caddnode.create(andn,
  489. cinlinenode.create(in_sar_x_y,false,
  490. ccallparanode.create(cordconstnode.create(shiftval,u8inttype,false),
  491. ccallparanode.create(ctemprefnode.create(temp),nil))
  492. ),
  493. cordconstnode.create(tcgint((qword(1) shl power)-1),
  494. right.resultdef,false)
  495. );
  496. addstatement(statements,cassignmentnode.create(ctemprefnode.create(resulttemp),
  497. cinlinenode.create(in_sar_x_y,false,
  498. ccallparanode.create(cordconstnode.create(power,u8inttype,false),
  499. ccallparanode.create(caddnode.create(addn,ctemprefnode.create(temp),
  500. masknode),nil
  501. ))))
  502. );
  503. addstatement(statements,ctempdeletenode.create(temp));
  504. addstatement(statements,ctempdeletenode.create_normal_temp(resulttemp));
  505. addstatement(statements,ctemprefnode.create(resulttemp));
  506. right.Free;
  507. end
  508. else
  509. begin
  510. tordconstnode(right).value:=power;
  511. result:=cshlshrnode.create(shrn,left,right)
  512. end;
  513. end
  514. else if is_signed(resultdef) then { signed modulus }
  515. begin
  516. if (cs_opt_size in current_settings.optimizerswitches) then
  517. exit;
  518. shiftval:=left.resultdef.size*8-1;
  519. dec(tordconstnode(right).value.uvalue);
  520. result:=internalstatements(statements);
  521. temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  522. resulttemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  523. addstatement(statements,resulttemp);
  524. addstatement(statements,temp);
  525. addstatement(statements,cassignmentnode.create(ctemprefnode.create(temp),left));
  526. { mask:=sar(left,sizeof(left)*8-1) and ((1 shl power)-1); }
  527. if power=1 then
  528. masknode:=
  529. cshlshrnode.create(shrn,
  530. ctemprefnode.create(temp),
  531. cordconstnode.create(shiftval,u8inttype,false)
  532. )
  533. else
  534. masknode:=
  535. caddnode.create(andn,
  536. cinlinenode.create(in_sar_x_y,false,
  537. ccallparanode.create(cordconstnode.create(shiftval,u8inttype,false),
  538. ccallparanode.create(ctemprefnode.create(temp),nil))
  539. ),
  540. cordconstnode.create(tcgint((qword(1) shl power)-1),
  541. right.resultdef,false)
  542. );
  543. addstatement(statements,cassignmentnode.create(ctemprefnode.create(resulttemp),masknode));
  544. { result:=((left+mask) and right)-mask; }
  545. addstatement(statements,cassignmentnode.create(ctemprefnode.create(resulttemp),
  546. caddnode.create(subn,
  547. caddnode.create(andn,
  548. right,
  549. caddnode.create(addn,
  550. ctemprefnode.create(temp),
  551. ctemprefnode.create(resulttemp))),
  552. ctemprefnode.create(resulttemp))
  553. ));
  554. addstatement(statements,ctempdeletenode.create(temp));
  555. addstatement(statements,ctempdeletenode.create_normal_temp(resulttemp));
  556. addstatement(statements,ctemprefnode.create(resulttemp));
  557. end
  558. else
  559. begin
  560. dec(tordconstnode(right).value.uvalue);
  561. result := caddnode.create(andn,left,right);
  562. end;
  563. { left and right are reused }
  564. left := nil;
  565. right := nil;
  566. firstpass(result);
  567. exit;
  568. end;
  569. end;
  570. function tmoddivnode.pass_1 : tnode;
  571. begin
  572. result:=nil;
  573. firstpass(left);
  574. firstpass(right);
  575. if codegenerror then
  576. exit;
  577. { Try to optimize mod/div }
  578. result := firstoptimize;
  579. if assigned(result) then
  580. exit;
  581. { 64bit }
  582. if use_moddiv64bitint_helper then
  583. begin
  584. result := first_moddiv64bitint;
  585. if assigned(result) then
  586. exit;
  587. expectloc:=LOC_REGISTER;
  588. end
  589. else
  590. begin
  591. result := first_moddivint;
  592. if assigned(result) then
  593. exit;
  594. end;
  595. expectloc:=LOC_REGISTER;
  596. end;
  597. {****************************************************************************
  598. TSHLSHRNODE
  599. ****************************************************************************}
  600. function tshlshrnode.simplify(forinline : boolean):tnode;
  601. var
  602. lvalue,rvalue : Tconstexprint;
  603. begin
  604. result:=nil;
  605. { constant folding }
  606. if is_constintnode(right) then
  607. begin
  608. if forinline then
  609. begin
  610. { shl/shr are unsigned operations, so cut off upper bits }
  611. case resultdef.size of
  612. 1,2,4:
  613. rvalue:=tordconstnode(right).value and byte($1f);
  614. 8:
  615. rvalue:=tordconstnode(right).value and byte($3f);
  616. else
  617. internalerror(2013122302);
  618. end;
  619. end
  620. else
  621. rvalue:=tordconstnode(right).value;
  622. if is_constintnode(left) then
  623. begin
  624. if forinline then
  625. begin
  626. { shl/shr are unsigned operations, so cut off upper bits }
  627. case resultdef.size of
  628. 1:
  629. lvalue:=tordconstnode(left).value and byte($ff);
  630. 2:
  631. lvalue:=tordconstnode(left).value and word($ffff);
  632. 4:
  633. lvalue:=tordconstnode(left).value and dword($ffffffff);
  634. 8:
  635. lvalue:=tordconstnode(left).value and qword($ffffffffffffffff);
  636. else
  637. internalerror(2013122301);
  638. end;
  639. end
  640. else
  641. lvalue:=tordconstnode(left).value;
  642. case nodetype of
  643. shrn:
  644. result:=create_simplified_ord_const(lvalue shr rvalue,resultdef,forinline);
  645. shln:
  646. result:=create_simplified_ord_const(lvalue shl rvalue,resultdef,forinline);
  647. end;
  648. end
  649. else if rvalue=0 then
  650. begin
  651. result:=left;
  652. left:=nil;
  653. end;
  654. end
  655. else if is_constintnode(left) then
  656. begin
  657. lvalue:=tordconstnode(left).value;
  658. { shl/shr are unsigned operations, so cut off upper bits }
  659. case resultdef.size of
  660. 1:
  661. lvalue:=tordconstnode(left).value and byte($ff);
  662. 2:
  663. lvalue:=tordconstnode(left).value and word($ffff);
  664. 4:
  665. lvalue:=tordconstnode(left).value and dword($ffffffff);
  666. 8:
  667. lvalue:=tordconstnode(left).value and qword($ffffffffffffffff);
  668. else
  669. internalerror(2013122301);
  670. end;
  671. { '0 shl x' and '0 shr x' are 0 }
  672. if (lvalue=0) and
  673. ((cs_opt_level4 in current_settings.optimizerswitches) or
  674. not might_have_sideeffects(right)) then
  675. result:=cordconstnode.create(0,resultdef,true);
  676. end;
  677. end;
  678. function tshlshrnode.pass_typecheck:tnode;
  679. var
  680. t : tnode;
  681. begin
  682. result:=nil;
  683. typecheckpass(left);
  684. typecheckpass(right);
  685. { avoid any problems with type parameters later on }
  686. if is_typeparam(left.resultdef) or is_typeparam(right.resultdef) then
  687. begin
  688. resultdef:=cundefinedtype;
  689. exit;
  690. end;
  691. set_varstate(right,vs_read,[vsf_must_be_valid]);
  692. set_varstate(left,vs_read,[vsf_must_be_valid]);
  693. if codegenerror then
  694. exit;
  695. { tp procvar support }
  696. maybe_call_procvar(left,true);
  697. maybe_call_procvar(right,true);
  698. { allow operator overloading }
  699. t:=self;
  700. if isbinaryoverloaded(t) then
  701. begin
  702. result:=t;
  703. exit;
  704. end;
  705. { calculations for ordinals < 32 bit have to be done in
  706. 32 bit for backwards compatibility. That way 'shl 33' is
  707. the same as 'shl 1'. It's ugly but compatible with delphi/tp/gcc }
  708. if (not is_64bit(left.resultdef)) and
  709. (torddef(left.resultdef).ordtype<>u32bit) then
  710. begin
  711. { keep singness of orignal type }
  712. if is_signed(left.resultdef) then
  713. begin
  714. {$if defined(cpu64bitalu) or defined(cpu32bitalu)}
  715. inserttypeconv(left,s32inttype)
  716. {$elseif defined(cpu16bitalu) or defined(cpu8bitalu)}
  717. inserttypeconv(left,get_common_intdef(torddef(left.resultdef),torddef(sinttype),true));
  718. {$else}
  719. internalerror(2013031301);
  720. {$endif}
  721. end
  722. else
  723. begin
  724. {$if defined(cpu64bitalu) or defined(cpu32bitalu)}
  725. inserttypeconv(left,u32inttype);
  726. {$elseif defined(cpu16bitalu) or defined(cpu8bitalu)}
  727. inserttypeconv(left,get_common_intdef(torddef(left.resultdef),torddef(uinttype),true));
  728. {$else}
  729. internalerror(2013031301);
  730. {$endif}
  731. end
  732. end;
  733. inserttypeconv(right,sinttype);
  734. resultdef:=left.resultdef;
  735. result:=simplify(false);
  736. if assigned(result) then
  737. exit;
  738. end;
  739. {$ifndef cpu64bitalu}
  740. function tshlshrnode.first_shlshr64bitint: tnode;
  741. var
  742. procname: string[31];
  743. begin
  744. result := nil;
  745. { Normally already done below, but called again,
  746. just in case it is called directly }
  747. firstpass(left);
  748. { otherwise create a call to a helper }
  749. if is_signed(left.resultdef) then
  750. procname:='int64'
  751. else
  752. procname:='qword';
  753. if nodetype = shln then
  754. procname := 'fpc_shl_'+procname
  755. else
  756. procname := 'fpc_shr_'+procname;
  757. { this order of parameters works at least for the arm,
  758. however it should work for any calling conventions (FK) }
  759. result := ccallnode.createintern(procname,ccallparanode.create(right,
  760. ccallparanode.create(left,nil)));
  761. left := nil;
  762. right := nil;
  763. firstpass(result);
  764. end;
  765. {$endif not cpu64bitalu}
  766. function tshlshrnode.pass_1 : tnode;
  767. var
  768. regs : longint;
  769. begin
  770. result:=nil;
  771. firstpass(left);
  772. firstpass(right);
  773. if codegenerror then
  774. exit;
  775. {$ifndef cpu64bitalu}
  776. { 64 bit ints have their own shift handling }
  777. if is_64bit(left.resultdef) then
  778. begin
  779. result := first_shlshr64bitint;
  780. if assigned(result) then
  781. exit;
  782. regs:=2;
  783. end
  784. else
  785. {$endif not cpu64bitalu}
  786. begin
  787. regs:=1
  788. end;
  789. if (right.nodetype<>ordconstn) then
  790. inc(regs);
  791. expectloc:=LOC_REGISTER;
  792. end;
  793. {****************************************************************************
  794. TUNARYMINUSNODE
  795. ****************************************************************************}
  796. constructor tunaryminusnode.create(expr : tnode);
  797. begin
  798. inherited create(unaryminusn,expr);
  799. end;
  800. function tunaryminusnode.simplify(forinline : boolean):tnode;
  801. begin
  802. result:=nil;
  803. { constant folding }
  804. if is_constintnode(left) then
  805. begin
  806. result:=create_simplified_ord_const(-tordconstnode(left).value,resultdef,forinline);
  807. exit;
  808. end;
  809. if is_constrealnode(left) then
  810. begin
  811. trealconstnode(left).value_real:=-trealconstnode(left).value_real;
  812. { Avoid integer overflow on x86_64 CPU for currency value }
  813. { i386 uses fildll/fchs/fistll instructions which never seem
  814. to raise any coprocessor flags .. }
  815. {$push}{$Q-}
  816. trealconstnode(left).value_currency:=-trealconstnode(left).value_currency;
  817. result:=left;
  818. {$pop}
  819. left:=nil;
  820. exit;
  821. end;
  822. end;
  823. function tunaryminusnode.pass_typecheck : tnode;
  824. var
  825. t : tnode;
  826. begin
  827. result:=nil;
  828. typecheckpass(left);
  829. { avoid any problems with type parameters later on }
  830. if is_typeparam(left.resultdef) then
  831. begin
  832. resultdef:=cundefinedtype;
  833. exit;
  834. end;
  835. set_varstate(left,vs_read,[vsf_must_be_valid]);
  836. if codegenerror then
  837. exit;
  838. result:=simplify(false);
  839. if assigned(result) then
  840. exit;
  841. resultdef:=left.resultdef;
  842. if (left.resultdef.typ=floatdef) or
  843. is_currency(left.resultdef) then
  844. begin
  845. end
  846. {$ifdef SUPPORT_MMX}
  847. else if (cs_mmx in current_settings.localswitches) and
  848. is_mmx_able_array(left.resultdef) then
  849. begin
  850. { if saturation is on, left.resultdef isn't
  851. "mmx able" (FK)
  852. if (cs_mmx_saturation in current_settings.localswitches^) and
  853. (torddef(tarraydef(resultdef).definition).typ in
  854. [s32bit,u32bit]) then
  855. CGMessage(type_e_mismatch);
  856. }
  857. end
  858. {$endif SUPPORT_MMX}
  859. else if is_oversizedord(left.resultdef) then
  860. begin
  861. if is_64bit(left.resultdef) then
  862. inserttypeconv(left,s64inttype)
  863. else if is_32bit(left.resultdef) then
  864. inserttypeconv(left,s32inttype)
  865. else if is_16bit(left.resultdef) then
  866. inserttypeconv(left,s16inttype)
  867. else
  868. internalerror(2013040701);
  869. resultdef:=left.resultdef;
  870. end
  871. else if (left.resultdef.typ=orddef) then
  872. begin
  873. inserttypeconv(left,sinttype);
  874. resultdef:=left.resultdef
  875. end
  876. else
  877. begin
  878. { allow operator overloading }
  879. t:=self;
  880. if isunaryoverloaded(t) then
  881. begin
  882. result:=t;
  883. exit;
  884. end;
  885. CGMessage(type_e_mismatch);
  886. end;
  887. end;
  888. { generic code }
  889. { overridden by: }
  890. { i386 }
  891. function tunaryminusnode.pass_1 : tnode;
  892. var
  893. procname: string[31];
  894. begin
  895. result:=nil;
  896. firstpass(left);
  897. if codegenerror then
  898. exit;
  899. if (cs_fp_emulation in current_settings.moduleswitches) and (left.resultdef.typ=floatdef) then
  900. begin
  901. if not(target_info.system in systems_wince) then
  902. begin
  903. expectloc:=LOC_REGISTER;
  904. exit;
  905. end
  906. else
  907. begin
  908. case tfloatdef(resultdef).floattype of
  909. s32real:
  910. procname:='negs';
  911. s64real:
  912. procname:='negd';
  913. {!!! not yet implemented
  914. s128real:
  915. }
  916. else
  917. internalerror(2005082802);
  918. end;
  919. result:=ccallnode.createintern(procname,ccallparanode.create(left,nil));
  920. end;
  921. left:=nil;
  922. end
  923. else
  924. begin
  925. if (left.resultdef.typ=floatdef) then
  926. expectloc:=LOC_FPUREGISTER
  927. {$ifdef SUPPORT_MMX}
  928. else if (cs_mmx in current_settings.localswitches) and
  929. is_mmx_able_array(left.resultdef) then
  930. expectloc:=LOC_MMXREGISTER
  931. {$endif SUPPORT_MMX}
  932. else if (left.resultdef.typ=orddef) then
  933. expectloc:=LOC_REGISTER;
  934. end;
  935. end;
  936. {****************************************************************************
  937. TUNARYPLUSNODE
  938. ****************************************************************************}
  939. constructor tunaryplusnode.create(expr: tnode);
  940. begin
  941. inherited create(unaryplusn,expr);
  942. end;
  943. function tunaryplusnode.pass_1: tnode;
  944. begin
  945. result:=nil;
  946. { can never happen because all the conversions happen
  947. in pass_typecheck }
  948. internalerror(201012250);
  949. end;
  950. function tunaryplusnode.pass_typecheck: tnode;
  951. var
  952. t:tnode;
  953. begin
  954. result:=nil;
  955. typecheckpass(left);
  956. { avoid any problems with type parameters later on }
  957. if is_typeparam(left.resultdef) then
  958. begin
  959. resultdef:=cundefinedtype;
  960. exit;
  961. end;
  962. set_varstate(left,vs_read,[vsf_must_be_valid]);
  963. if codegenerror then
  964. exit;
  965. if is_constintnode(left) or
  966. is_constrealnode(left) or
  967. (left.resultdef.typ=floatdef) or
  968. is_currency(left.resultdef)
  969. {$ifdef SUPPORT_MMX}
  970. or ((cs_mmx in current_settings.localswitches) and
  971. is_mmx_able_array(left.resultdef))
  972. {$endif SUPPORT_MMX}
  973. then
  974. begin
  975. result:=left;
  976. left:=nil;
  977. end
  978. else if is_oversizedord(left.resultdef) then
  979. begin
  980. if is_64bit(left.resultdef) then
  981. inserttypeconv(left,s64inttype)
  982. else if is_32bit(left.resultdef) then
  983. inserttypeconv(left,s32inttype)
  984. else if is_16bit(left.resultdef) then
  985. inserttypeconv(left,s16inttype)
  986. else
  987. internalerror(2013040702);
  988. result:=left;
  989. left:=nil;
  990. end
  991. else if (left.resultdef.typ=orddef) then
  992. begin
  993. inserttypeconv(left,sinttype);
  994. result:=left;
  995. left:=nil;
  996. end
  997. else
  998. begin
  999. { allow operator overloading }
  1000. t:=self;
  1001. if isunaryoverloaded(t) then
  1002. begin
  1003. result:=t;
  1004. exit;
  1005. end;
  1006. CGMessage(type_e_mismatch);
  1007. end;
  1008. end;
  1009. {****************************************************************************
  1010. TNOTNODE
  1011. ****************************************************************************}
  1012. const
  1013. boolean_reverse:array[ltn..unequaln] of Tnodetype=(
  1014. gten,gtn,lten,ltn,unequaln,equaln
  1015. );
  1016. constructor tnotnode.create(expr : tnode);
  1017. begin
  1018. inherited create(notn,expr);
  1019. end;
  1020. function tnotnode.simplify(forinline : boolean):tnode;
  1021. var
  1022. v : tconstexprint;
  1023. t : tnode;
  1024. def : tdef;
  1025. begin
  1026. result:=nil;
  1027. { Try optmimizing ourself away }
  1028. if left.nodetype=notn then
  1029. begin
  1030. { Double not. Remove both }
  1031. result:=Tnotnode(left).left;
  1032. tnotnode(left).left:=nil;
  1033. exit;
  1034. end;
  1035. if (left.nodetype in [ltn,lten,equaln,unequaln,gtn,gten]) then
  1036. begin
  1037. { Not of boolean expression. Turn around the operator and remove
  1038. the not. This is not allowed for sets with the gten/lten,
  1039. because there is no ltn/gtn support }
  1040. if (taddnode(left).left.resultdef.typ<>setdef) or
  1041. (left.nodetype in [equaln,unequaln]) then
  1042. begin
  1043. result:=left;
  1044. left.nodetype:=boolean_reverse[left.nodetype];
  1045. left:=nil;
  1046. exit;
  1047. end;
  1048. end;
  1049. { constant folding }
  1050. if (left.nodetype=ordconstn) then
  1051. begin
  1052. v:=tordconstnode(left).value;
  1053. def:=left.resultdef;
  1054. case torddef(left.resultdef).ordtype of
  1055. pasbool8,
  1056. pasbool16,
  1057. pasbool32,
  1058. pasbool64:
  1059. v:=byte(not(boolean(int64(v))));
  1060. bool8bit,
  1061. bool16bit,
  1062. bool32bit,
  1063. bool64bit:
  1064. begin
  1065. if v=0 then
  1066. v:=-1
  1067. else
  1068. v:=0;
  1069. end;
  1070. uchar,
  1071. uwidechar,
  1072. u8bit,
  1073. s8bit,
  1074. u16bit,
  1075. s16bit,
  1076. s32bit,
  1077. u32bit,
  1078. s64bit,
  1079. u64bit:
  1080. begin
  1081. { unsigned, equal or bigger than the native int size? }
  1082. if (torddef(left.resultdef).ordtype in [u64bit,u32bit,u16bit,u8bit,uchar,uwidechar]) and
  1083. (is_nativeord(left.resultdef) or is_oversizedord(left.resultdef)) then
  1084. begin
  1085. { Delphi-compatible: not dword = dword (not word = longint) }
  1086. { Extension: not qword = qword }
  1087. v:=qword(not qword(v));
  1088. { will be truncated by the ordconstnode for u32bit }
  1089. end
  1090. else
  1091. begin
  1092. v:=int64(not int64(v));
  1093. def:=get_common_intdef(torddef(left.resultdef),torddef(sinttype),false);
  1094. end;
  1095. end;
  1096. else
  1097. CGMessage(type_e_mismatch);
  1098. end;
  1099. { not-nodes are not range checked by the code generator -> also
  1100. don't range check while inlining; the resultdef is a bit tricky
  1101. though: the node's resultdef gets changed in most cases compared
  1102. to left, but the not-operation itself is caried out in the code
  1103. generator using the size of left
  1104. }
  1105. if not(forinline) then
  1106. t:=cordconstnode.create(v,def,false)
  1107. else
  1108. begin
  1109. { cut off the value if necessary }
  1110. t:=cordconstnode.create(v,left.resultdef,false);
  1111. { now convert to node's resultdef }
  1112. inserttypeconv_explicit(t,def);
  1113. end;
  1114. result:=t;
  1115. exit;
  1116. end;
  1117. end;
  1118. function tnotnode.pass_typecheck : tnode;
  1119. var
  1120. t : tnode;
  1121. begin
  1122. result:=nil;
  1123. typecheckpass(left);
  1124. { avoid any problems with type parameters later on }
  1125. if is_typeparam(left.resultdef) then
  1126. begin
  1127. resultdef:=cundefinedtype;
  1128. exit;
  1129. end;
  1130. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1131. if codegenerror then
  1132. exit;
  1133. { tp procvar support }
  1134. maybe_call_procvar(left,true);
  1135. resultdef:=left.resultdef;
  1136. result:=simplify(false);
  1137. if assigned(result) then
  1138. exit;
  1139. if is_boolean(resultdef) then
  1140. begin
  1141. end
  1142. else
  1143. {$ifdef SUPPORT_MMX}
  1144. if (cs_mmx in current_settings.localswitches) and
  1145. is_mmx_able_array(left.resultdef) then
  1146. begin
  1147. end
  1148. else
  1149. {$endif SUPPORT_MMX}
  1150. {$ifndef cpu64bitaddr}
  1151. if is_64bitint(left.resultdef) then
  1152. begin
  1153. end
  1154. else
  1155. {$endif not cpu64bitaddr}
  1156. if is_integer(left.resultdef) then
  1157. begin
  1158. end
  1159. else
  1160. begin
  1161. { allow operator overloading }
  1162. t:=self;
  1163. if isunaryoverloaded(t) then
  1164. begin
  1165. result:=t;
  1166. exit;
  1167. end;
  1168. CGMessage(type_e_mismatch);
  1169. end;
  1170. end;
  1171. function tnotnode.pass_1 : tnode;
  1172. begin
  1173. result:=nil;
  1174. firstpass(left);
  1175. if codegenerror then
  1176. exit;
  1177. expectloc:=left.expectloc;
  1178. if is_boolean(resultdef) then
  1179. begin
  1180. if (expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1181. expectloc:=LOC_REGISTER;
  1182. { before loading it into flags we need to load it into
  1183. a register thus 1 register is need PM }
  1184. {$ifdef cpuflags}
  1185. if left.expectloc<>LOC_JUMP then
  1186. expectloc:=LOC_FLAGS;
  1187. {$endif def cpuflags}
  1188. end
  1189. else
  1190. {$ifdef SUPPORT_MMX}
  1191. if (cs_mmx in current_settings.localswitches) and
  1192. is_mmx_able_array(left.resultdef) then
  1193. expectloc:=LOC_MMXREGISTER
  1194. else
  1195. {$endif SUPPORT_MMX}
  1196. {$ifndef cpu64bitalu}
  1197. if is_64bit(left.resultdef) then
  1198. begin
  1199. if (expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1200. expectloc:=LOC_REGISTER;
  1201. end
  1202. else
  1203. {$endif not cpu64bitalu}
  1204. if is_integer(left.resultdef) then
  1205. expectloc:=LOC_REGISTER;
  1206. end;
  1207. {$ifdef state_tracking}
  1208. function Tnotnode.track_state_pass(exec_known:boolean):boolean;
  1209. begin
  1210. track_state_pass:=true;
  1211. if left.track_state_pass(exec_known) then
  1212. begin
  1213. left.resultdef:=nil;
  1214. do_typecheckpass(left);
  1215. end;
  1216. end;
  1217. {$endif}
  1218. end.