nmat.pas 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  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,
  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. begin
  443. result := nil;
  444. { divide/mod a number by a constant which is a power of 2? }
  445. if (right.nodetype = ordconstn) and
  446. ispowerof2(tordconstnode(right).value,power) and
  447. {$ifdef cpu64bitalu}
  448. { for 64 bit, we leave the optimization to the cg }
  449. (not is_signed(resultdef)) then
  450. {$else cpu64bitalu}
  451. (((nodetype=divn) and is_64bit(resultdef)) or
  452. (nodetype=modn) or
  453. not is_signed(resultdef)) then
  454. {$endif cpu64bitalu}
  455. begin
  456. if nodetype=divn then
  457. begin
  458. if is_signed(resultdef) then
  459. begin
  460. if is_64bitint(left.resultdef) then
  461. if not (cs_opt_size in current_settings.optimizerswitches) then
  462. shiftval:=63
  463. else
  464. { the shift code is a lot bigger than the call to }
  465. { the divide helper }
  466. exit
  467. else
  468. shiftval:=31;
  469. result:=internalstatements(statements);
  470. temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  471. resulttemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  472. addstatement(statements,resulttemp);
  473. addstatement(statements,temp);
  474. addstatement(statements,cassignmentnode.create(ctemprefnode.create(temp),
  475. left));
  476. left:=nil;
  477. addstatement(statements,cassignmentnode.create(ctemprefnode.create(resulttemp),
  478. cinlinenode.create(in_sar_x_y,false,
  479. ccallparanode.create(cordconstnode.create(power,u8inttype,false),
  480. ccallparanode.create(caddnode.create(addn,ctemprefnode.create(temp),
  481. caddnode.create(andn,
  482. cinlinenode.create(in_sar_x_y,false,
  483. ccallparanode.create(cordconstnode.create(shiftval,u8inttype,false),
  484. ccallparanode.create(ctemprefnode.create(temp),nil))
  485. ),
  486. cordconstnode.create(tordconstnode(right).value-1,
  487. right.resultdef,false)
  488. )),nil
  489. ))))
  490. );
  491. addstatement(statements,ctempdeletenode.create(temp));
  492. addstatement(statements,ctempdeletenode.create_normal_temp(resulttemp));
  493. addstatement(statements,ctemprefnode.create(resulttemp));
  494. right.Free;
  495. end
  496. else
  497. begin
  498. tordconstnode(right).value:=power;
  499. result:=cshlshrnode.create(shrn,left,right)
  500. end;
  501. end
  502. else if is_signed(resultdef) then { signed modulus }
  503. begin
  504. if (cs_opt_size in current_settings.optimizerswitches) then
  505. exit;
  506. shiftval:=left.resultdef.size*8-1;
  507. dec(tordconstnode(right).value.uvalue);
  508. result:=internalstatements(statements);
  509. temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  510. resulttemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  511. addstatement(statements,resulttemp);
  512. addstatement(statements,temp);
  513. addstatement(statements,cassignmentnode.create(ctemprefnode.create(temp),left));
  514. { sign:=sar(left,sizeof(left)*8-1); }
  515. addstatement(statements,cassignmentnode.create(ctemprefnode.create(resulttemp),
  516. cinlinenode.create(in_sar_x_y,false,
  517. ccallparanode.create(cordconstnode.create(shiftval,u8inttype,false),
  518. ccallparanode.create(ctemprefnode.create(temp),nil)
  519. )
  520. )));
  521. { result:=((((left xor sign)-sign) and right) xor sign)-sign; }
  522. addstatement(statements,cassignmentnode.create(ctemprefnode.create(resulttemp),
  523. caddnode.create(subn,
  524. caddnode.create(xorn,
  525. caddnode.create(andn,
  526. right,
  527. caddnode.create(subn,
  528. caddnode.create(xorn,
  529. ctemprefnode.create(resulttemp),
  530. ctemprefnode.create(temp)),
  531. ctemprefnode.create(resulttemp))
  532. ),
  533. ctemprefnode.create(resulttemp)
  534. ),
  535. ctemprefnode.create(resulttemp))
  536. ));
  537. addstatement(statements,ctempdeletenode.create(temp));
  538. addstatement(statements,ctempdeletenode.create_normal_temp(resulttemp));
  539. addstatement(statements,ctemprefnode.create(resulttemp));
  540. end
  541. else
  542. begin
  543. dec(tordconstnode(right).value.uvalue);
  544. result := caddnode.create(andn,left,right);
  545. end;
  546. { left and right are reused }
  547. left := nil;
  548. right := nil;
  549. firstpass(result);
  550. exit;
  551. end;
  552. end;
  553. function tmoddivnode.pass_1 : tnode;
  554. begin
  555. result:=nil;
  556. firstpass(left);
  557. firstpass(right);
  558. if codegenerror then
  559. exit;
  560. { Try to optimize mod/div }
  561. result := firstoptimize;
  562. if assigned(result) then
  563. exit;
  564. { 64bit }
  565. if use_moddiv64bitint_helper then
  566. begin
  567. result := first_moddiv64bitint;
  568. if assigned(result) then
  569. exit;
  570. expectloc:=LOC_REGISTER;
  571. end
  572. else
  573. begin
  574. result := first_moddivint;
  575. if assigned(result) then
  576. exit;
  577. end;
  578. expectloc:=LOC_REGISTER;
  579. end;
  580. {****************************************************************************
  581. TSHLSHRNODE
  582. ****************************************************************************}
  583. function tshlshrnode.simplify(forinline : boolean):tnode;
  584. var
  585. lvalue,rvalue : Tconstexprint;
  586. begin
  587. result:=nil;
  588. { constant folding }
  589. if is_constintnode(right) then
  590. begin
  591. if forinline then
  592. begin
  593. { shl/shr are unsigned operations, so cut off upper bits }
  594. case resultdef.size of
  595. 1,2,4:
  596. rvalue:=tordconstnode(right).value and byte($1f);
  597. 8:
  598. rvalue:=tordconstnode(right).value and byte($3f);
  599. else
  600. internalerror(2013122302);
  601. end;
  602. end
  603. else
  604. rvalue:=tordconstnode(right).value;
  605. if is_constintnode(left) then
  606. begin
  607. if forinline then
  608. begin
  609. { shl/shr are unsigned operations, so cut off upper bits }
  610. case resultdef.size of
  611. 1:
  612. lvalue:=tordconstnode(left).value and byte($ff);
  613. 2:
  614. lvalue:=tordconstnode(left).value and word($ffff);
  615. 4:
  616. lvalue:=tordconstnode(left).value and dword($ffffffff);
  617. 8:
  618. lvalue:=tordconstnode(left).value and qword($ffffffffffffffff);
  619. else
  620. internalerror(2013122301);
  621. end;
  622. end
  623. else
  624. lvalue:=tordconstnode(left).value;
  625. case nodetype of
  626. shrn:
  627. result:=create_simplified_ord_const(lvalue shr rvalue,resultdef,forinline);
  628. shln:
  629. result:=create_simplified_ord_const(lvalue shl rvalue,resultdef,forinline);
  630. end;
  631. end
  632. else if rvalue=0 then
  633. begin
  634. result:=left;
  635. left:=nil;
  636. end;
  637. end
  638. else if is_constintnode(left) then
  639. begin
  640. lvalue:=tordconstnode(left).value;
  641. { shl/shr are unsigned operations, so cut off upper bits }
  642. case resultdef.size of
  643. 1:
  644. lvalue:=tordconstnode(left).value and byte($ff);
  645. 2:
  646. lvalue:=tordconstnode(left).value and word($ffff);
  647. 4:
  648. lvalue:=tordconstnode(left).value and dword($ffffffff);
  649. 8:
  650. lvalue:=tordconstnode(left).value and qword($ffffffffffffffff);
  651. else
  652. internalerror(2013122301);
  653. end;
  654. { '0 shl x' and '0 shr x' are 0 }
  655. if (lvalue=0) and
  656. ((cs_opt_level4 in current_settings.optimizerswitches) or
  657. not might_have_sideeffects(right)) then
  658. result:=cordconstnode.create(0,resultdef,true);
  659. end;
  660. end;
  661. function tshlshrnode.pass_typecheck:tnode;
  662. var
  663. t : tnode;
  664. begin
  665. result:=nil;
  666. typecheckpass(left);
  667. typecheckpass(right);
  668. { avoid any problems with type parameters later on }
  669. if is_typeparam(left.resultdef) or is_typeparam(right.resultdef) then
  670. begin
  671. resultdef:=cundefinedtype;
  672. exit;
  673. end;
  674. set_varstate(right,vs_read,[vsf_must_be_valid]);
  675. set_varstate(left,vs_read,[vsf_must_be_valid]);
  676. if codegenerror then
  677. exit;
  678. { tp procvar support }
  679. maybe_call_procvar(left,true);
  680. maybe_call_procvar(right,true);
  681. { allow operator overloading }
  682. t:=self;
  683. if isbinaryoverloaded(t) then
  684. begin
  685. result:=t;
  686. exit;
  687. end;
  688. { calculations for ordinals < 32 bit have to be done in
  689. 32 bit for backwards compatibility. That way 'shl 33' is
  690. the same as 'shl 1'. It's ugly but compatible with delphi/tp/gcc }
  691. if (not is_64bit(left.resultdef)) and
  692. (torddef(left.resultdef).ordtype<>u32bit) then
  693. begin
  694. { keep singness of orignal type }
  695. if is_signed(left.resultdef) then
  696. begin
  697. {$if defined(cpu64bitalu) or defined(cpu32bitalu)}
  698. inserttypeconv(left,s32inttype)
  699. {$elseif defined(cpu16bitalu) or defined(cpu8bitalu)}
  700. inserttypeconv(left,get_common_intdef(torddef(left.resultdef),torddef(sinttype),true));
  701. {$else}
  702. internalerror(2013031301);
  703. {$endif}
  704. end
  705. else
  706. begin
  707. {$if defined(cpu64bitalu) or defined(cpu32bitalu)}
  708. inserttypeconv(left,u32inttype);
  709. {$elseif defined(cpu16bitalu) or defined(cpu8bitalu)}
  710. inserttypeconv(left,get_common_intdef(torddef(left.resultdef),torddef(uinttype),true));
  711. {$else}
  712. internalerror(2013031301);
  713. {$endif}
  714. end
  715. end;
  716. inserttypeconv(right,sinttype);
  717. resultdef:=left.resultdef;
  718. result:=simplify(false);
  719. if assigned(result) then
  720. exit;
  721. end;
  722. {$ifndef cpu64bitalu}
  723. function tshlshrnode.first_shlshr64bitint: tnode;
  724. var
  725. procname: string[31];
  726. begin
  727. result := nil;
  728. { Normally already done below, but called again,
  729. just in case it is called directly }
  730. firstpass(left);
  731. { otherwise create a call to a helper }
  732. if is_signed(left.resultdef) then
  733. procname:='int64'
  734. else
  735. procname:='qword';
  736. if nodetype = shln then
  737. procname := 'fpc_shl_'+procname
  738. else
  739. procname := 'fpc_shr_'+procname;
  740. { this order of parameters works at least for the arm,
  741. however it should work for any calling conventions (FK) }
  742. result := ccallnode.createintern(procname,ccallparanode.create(right,
  743. ccallparanode.create(left,nil)));
  744. left := nil;
  745. right := nil;
  746. firstpass(result);
  747. end;
  748. {$endif not cpu64bitalu}
  749. function tshlshrnode.pass_1 : tnode;
  750. var
  751. regs : longint;
  752. begin
  753. result:=nil;
  754. firstpass(left);
  755. firstpass(right);
  756. if codegenerror then
  757. exit;
  758. {$ifndef cpu64bitalu}
  759. { 64 bit ints have their own shift handling }
  760. if is_64bit(left.resultdef) then
  761. begin
  762. result := first_shlshr64bitint;
  763. if assigned(result) then
  764. exit;
  765. regs:=2;
  766. end
  767. else
  768. {$endif not cpu64bitalu}
  769. begin
  770. regs:=1
  771. end;
  772. if (right.nodetype<>ordconstn) then
  773. inc(regs);
  774. expectloc:=LOC_REGISTER;
  775. end;
  776. {****************************************************************************
  777. TUNARYMINUSNODE
  778. ****************************************************************************}
  779. constructor tunaryminusnode.create(expr : tnode);
  780. begin
  781. inherited create(unaryminusn,expr);
  782. end;
  783. function tunaryminusnode.simplify(forinline : boolean):tnode;
  784. begin
  785. result:=nil;
  786. { constant folding }
  787. if is_constintnode(left) then
  788. begin
  789. result:=create_simplified_ord_const(-tordconstnode(left).value,resultdef,forinline);
  790. exit;
  791. end;
  792. if is_constrealnode(left) then
  793. begin
  794. trealconstnode(left).value_real:=-trealconstnode(left).value_real;
  795. { Avoid integer overflow on x86_64 CPU for currency value }
  796. { i386 uses fildll/fchs/fistll instructions which never seem
  797. to raise any coprocessor flags .. }
  798. {$push}{$Q-}
  799. trealconstnode(left).value_currency:=-trealconstnode(left).value_currency;
  800. result:=left;
  801. {$pop}
  802. left:=nil;
  803. exit;
  804. end;
  805. end;
  806. function tunaryminusnode.pass_typecheck : tnode;
  807. var
  808. t : tnode;
  809. begin
  810. result:=nil;
  811. typecheckpass(left);
  812. { avoid any problems with type parameters later on }
  813. if is_typeparam(left.resultdef) then
  814. begin
  815. resultdef:=cundefinedtype;
  816. exit;
  817. end;
  818. set_varstate(left,vs_read,[vsf_must_be_valid]);
  819. if codegenerror then
  820. exit;
  821. result:=simplify(false);
  822. if assigned(result) then
  823. exit;
  824. resultdef:=left.resultdef;
  825. if (left.resultdef.typ=floatdef) or
  826. is_currency(left.resultdef) then
  827. begin
  828. end
  829. {$ifdef SUPPORT_MMX}
  830. else if (cs_mmx in current_settings.localswitches) and
  831. is_mmx_able_array(left.resultdef) then
  832. begin
  833. { if saturation is on, left.resultdef isn't
  834. "mmx able" (FK)
  835. if (cs_mmx_saturation in current_settings.localswitches^) and
  836. (torddef(tarraydef(resultdef).definition).typ in
  837. [s32bit,u32bit]) then
  838. CGMessage(type_e_mismatch);
  839. }
  840. end
  841. {$endif SUPPORT_MMX}
  842. else if is_oversizedord(left.resultdef) then
  843. begin
  844. if is_64bit(left.resultdef) then
  845. inserttypeconv(left,s64inttype)
  846. else if is_32bit(left.resultdef) then
  847. inserttypeconv(left,s32inttype)
  848. else if is_16bit(left.resultdef) then
  849. inserttypeconv(left,s16inttype)
  850. else
  851. internalerror(2013040701);
  852. resultdef:=left.resultdef;
  853. end
  854. else if (left.resultdef.typ=orddef) then
  855. begin
  856. inserttypeconv(left,sinttype);
  857. resultdef:=left.resultdef
  858. end
  859. else
  860. begin
  861. { allow operator overloading }
  862. t:=self;
  863. if isunaryoverloaded(t) then
  864. begin
  865. result:=t;
  866. exit;
  867. end;
  868. CGMessage(type_e_mismatch);
  869. end;
  870. end;
  871. { generic code }
  872. { overridden by: }
  873. { i386 }
  874. function tunaryminusnode.pass_1 : tnode;
  875. var
  876. procname: string[31];
  877. begin
  878. result:=nil;
  879. firstpass(left);
  880. if codegenerror then
  881. exit;
  882. if (cs_fp_emulation in current_settings.moduleswitches) and (left.resultdef.typ=floatdef) then
  883. begin
  884. if not(target_info.system in systems_wince) then
  885. begin
  886. expectloc:=LOC_REGISTER;
  887. exit;
  888. end
  889. else
  890. begin
  891. case tfloatdef(resultdef).floattype of
  892. s32real:
  893. procname:='negs';
  894. s64real:
  895. procname:='negd';
  896. {!!! not yet implemented
  897. s128real:
  898. }
  899. else
  900. internalerror(2005082802);
  901. end;
  902. result:=ccallnode.createintern(procname,ccallparanode.create(left,nil));
  903. end;
  904. left:=nil;
  905. end
  906. else
  907. begin
  908. if (left.resultdef.typ=floatdef) then
  909. expectloc:=LOC_FPUREGISTER
  910. {$ifdef SUPPORT_MMX}
  911. else if (cs_mmx in current_settings.localswitches) and
  912. is_mmx_able_array(left.resultdef) then
  913. expectloc:=LOC_MMXREGISTER
  914. {$endif SUPPORT_MMX}
  915. else if (left.resultdef.typ=orddef) then
  916. expectloc:=LOC_REGISTER;
  917. end;
  918. end;
  919. {****************************************************************************
  920. TUNARYPLUSNODE
  921. ****************************************************************************}
  922. constructor tunaryplusnode.create(expr: tnode);
  923. begin
  924. inherited create(unaryplusn,expr);
  925. end;
  926. function tunaryplusnode.pass_1: tnode;
  927. begin
  928. result:=nil;
  929. { can never happen because all the conversions happen
  930. in pass_typecheck }
  931. internalerror(201012250);
  932. end;
  933. function tunaryplusnode.pass_typecheck: tnode;
  934. var
  935. t:tnode;
  936. begin
  937. result:=nil;
  938. typecheckpass(left);
  939. { avoid any problems with type parameters later on }
  940. if is_typeparam(left.resultdef) then
  941. begin
  942. resultdef:=cundefinedtype;
  943. exit;
  944. end;
  945. set_varstate(left,vs_read,[vsf_must_be_valid]);
  946. if codegenerror then
  947. exit;
  948. if is_constintnode(left) or
  949. is_constrealnode(left) or
  950. (left.resultdef.typ=floatdef) or
  951. is_currency(left.resultdef)
  952. {$ifdef SUPPORT_MMX}
  953. or ((cs_mmx in current_settings.localswitches) and
  954. is_mmx_able_array(left.resultdef))
  955. {$endif SUPPORT_MMX}
  956. then
  957. begin
  958. result:=left;
  959. left:=nil;
  960. end
  961. else if is_oversizedord(left.resultdef) then
  962. begin
  963. if is_64bit(left.resultdef) then
  964. inserttypeconv(left,s64inttype)
  965. else if is_32bit(left.resultdef) then
  966. inserttypeconv(left,s32inttype)
  967. else if is_16bit(left.resultdef) then
  968. inserttypeconv(left,s16inttype)
  969. else
  970. internalerror(2013040702);
  971. result:=left;
  972. left:=nil;
  973. end
  974. else if (left.resultdef.typ=orddef) then
  975. begin
  976. inserttypeconv(left,sinttype);
  977. result:=left;
  978. left:=nil;
  979. end
  980. else
  981. begin
  982. { allow operator overloading }
  983. t:=self;
  984. if isunaryoverloaded(t) then
  985. begin
  986. result:=t;
  987. exit;
  988. end;
  989. CGMessage(type_e_mismatch);
  990. end;
  991. end;
  992. {****************************************************************************
  993. TNOTNODE
  994. ****************************************************************************}
  995. const
  996. boolean_reverse:array[ltn..unequaln] of Tnodetype=(
  997. gten,gtn,lten,ltn,unequaln,equaln
  998. );
  999. constructor tnotnode.create(expr : tnode);
  1000. begin
  1001. inherited create(notn,expr);
  1002. end;
  1003. function tnotnode.simplify(forinline : boolean):tnode;
  1004. var
  1005. v : tconstexprint;
  1006. t : tnode;
  1007. def : tdef;
  1008. begin
  1009. result:=nil;
  1010. { Try optmimizing ourself away }
  1011. if left.nodetype=notn then
  1012. begin
  1013. { Double not. Remove both }
  1014. result:=Tnotnode(left).left;
  1015. tnotnode(left).left:=nil;
  1016. exit;
  1017. end;
  1018. if (left.nodetype in [ltn,lten,equaln,unequaln,gtn,gten]) then
  1019. begin
  1020. { Not of boolean expression. Turn around the operator and remove
  1021. the not. This is not allowed for sets with the gten/lten,
  1022. because there is no ltn/gtn support }
  1023. if (taddnode(left).left.resultdef.typ<>setdef) or
  1024. (left.nodetype in [equaln,unequaln]) then
  1025. begin
  1026. result:=left;
  1027. left.nodetype:=boolean_reverse[left.nodetype];
  1028. left:=nil;
  1029. exit;
  1030. end;
  1031. end;
  1032. { constant folding }
  1033. if (left.nodetype=ordconstn) then
  1034. begin
  1035. v:=tordconstnode(left).value;
  1036. def:=left.resultdef;
  1037. case torddef(left.resultdef).ordtype of
  1038. pasbool8,
  1039. pasbool16,
  1040. pasbool32,
  1041. pasbool64:
  1042. v:=byte(not(boolean(int64(v))));
  1043. bool8bit,
  1044. bool16bit,
  1045. bool32bit,
  1046. bool64bit:
  1047. begin
  1048. if v=0 then
  1049. v:=-1
  1050. else
  1051. v:=0;
  1052. end;
  1053. uchar,
  1054. uwidechar,
  1055. u8bit,
  1056. s8bit,
  1057. u16bit,
  1058. s16bit,
  1059. s32bit,
  1060. u32bit,
  1061. s64bit,
  1062. u64bit:
  1063. begin
  1064. { unsigned, equal or bigger than the native int size? }
  1065. if (torddef(left.resultdef).ordtype in [u64bit,u32bit,u16bit,u8bit,uchar,uwidechar]) and
  1066. (is_nativeord(left.resultdef) or is_oversizedord(left.resultdef)) then
  1067. begin
  1068. { Delphi-compatible: not dword = dword (not word = longint) }
  1069. { Extension: not qword = qword }
  1070. v:=qword(not qword(v));
  1071. { will be truncated by the ordconstnode for u32bit }
  1072. end
  1073. else
  1074. begin
  1075. v:=int64(not int64(v));
  1076. def:=get_common_intdef(torddef(left.resultdef),torddef(sinttype),false);
  1077. end;
  1078. end;
  1079. else
  1080. CGMessage(type_e_mismatch);
  1081. end;
  1082. { not-nodes are not range checked by the code generator -> also
  1083. don't range check while inlining; the resultdef is a bit tricky
  1084. though: the node's resultdef gets changed in most cases compared
  1085. to left, but the not-operation itself is caried out in the code
  1086. generator using the size of left
  1087. }
  1088. if not(forinline) then
  1089. t:=cordconstnode.create(v,def,false)
  1090. else
  1091. begin
  1092. { cut off the value if necessary }
  1093. t:=cordconstnode.create(v,left.resultdef,false);
  1094. { now convert to node's resultdef }
  1095. inserttypeconv_explicit(t,def);
  1096. end;
  1097. result:=t;
  1098. exit;
  1099. end;
  1100. end;
  1101. function tnotnode.pass_typecheck : tnode;
  1102. var
  1103. t : tnode;
  1104. begin
  1105. result:=nil;
  1106. typecheckpass(left);
  1107. { avoid any problems with type parameters later on }
  1108. if is_typeparam(left.resultdef) then
  1109. begin
  1110. resultdef:=cundefinedtype;
  1111. exit;
  1112. end;
  1113. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1114. if codegenerror then
  1115. exit;
  1116. { tp procvar support }
  1117. maybe_call_procvar(left,true);
  1118. resultdef:=left.resultdef;
  1119. result:=simplify(false);
  1120. if assigned(result) then
  1121. exit;
  1122. if is_boolean(resultdef) then
  1123. begin
  1124. end
  1125. else
  1126. {$ifdef SUPPORT_MMX}
  1127. if (cs_mmx in current_settings.localswitches) and
  1128. is_mmx_able_array(left.resultdef) then
  1129. begin
  1130. end
  1131. else
  1132. {$endif SUPPORT_MMX}
  1133. {$ifndef cpu64bitaddr}
  1134. if is_64bitint(left.resultdef) then
  1135. begin
  1136. end
  1137. else
  1138. {$endif not cpu64bitaddr}
  1139. if is_integer(left.resultdef) then
  1140. begin
  1141. end
  1142. else
  1143. begin
  1144. { allow operator overloading }
  1145. t:=self;
  1146. if isunaryoverloaded(t) then
  1147. begin
  1148. result:=t;
  1149. exit;
  1150. end;
  1151. CGMessage(type_e_mismatch);
  1152. end;
  1153. end;
  1154. function tnotnode.pass_1 : tnode;
  1155. begin
  1156. result:=nil;
  1157. firstpass(left);
  1158. if codegenerror then
  1159. exit;
  1160. expectloc:=left.expectloc;
  1161. if is_boolean(resultdef) then
  1162. begin
  1163. if (expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1164. expectloc:=LOC_REGISTER;
  1165. { before loading it into flags we need to load it into
  1166. a register thus 1 register is need PM }
  1167. {$ifdef cpuflags}
  1168. if left.expectloc<>LOC_JUMP then
  1169. expectloc:=LOC_FLAGS;
  1170. {$endif def cpuflags}
  1171. end
  1172. else
  1173. {$ifdef SUPPORT_MMX}
  1174. if (cs_mmx in current_settings.localswitches) and
  1175. is_mmx_able_array(left.resultdef) then
  1176. expectloc:=LOC_MMXREGISTER
  1177. else
  1178. {$endif SUPPORT_MMX}
  1179. {$ifndef cpu64bitalu}
  1180. if is_64bit(left.resultdef) then
  1181. begin
  1182. if (expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1183. expectloc:=LOC_REGISTER;
  1184. end
  1185. else
  1186. {$endif not cpu64bitalu}
  1187. if is_integer(left.resultdef) then
  1188. expectloc:=LOC_REGISTER;
  1189. end;
  1190. {$ifdef state_tracking}
  1191. function Tnotnode.track_state_pass(exec_known:boolean):boolean;
  1192. begin
  1193. track_state_pass:=true;
  1194. if left.track_state_pass(exec_known) then
  1195. begin
  1196. left.resultdef:=nil;
  1197. do_typecheckpass(left);
  1198. end;
  1199. end;
  1200. {$endif}
  1201. end.