nmat.pas 52 KB

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