nadd.pas 206 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Type checking and simplification for add 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 nadd;
  18. {$i fpcdefs.inc}
  19. {$modeswitch nestedprocvars}
  20. { define addstringopt}
  21. interface
  22. uses
  23. node,symtype;
  24. type
  25. TAddNodeFlag = (
  26. anf_has_pointerdiv,
  27. { the node shall be short boolean evaluated, this flag has priority over localswitches }
  28. anf_short_bool
  29. );
  30. TAddNodeFlags = set of TAddNodeFlag;
  31. taddnode = class(tbinopnode)
  32. private
  33. resultrealdefderef: tderef;
  34. function pass_typecheck_internal:tnode;
  35. public
  36. resultrealdef : tdef;
  37. addnodeflags : TAddNodeFlags;
  38. constructor create(tt : tnodetype;l,r : tnode);override;
  39. constructor create_internal(tt:tnodetype;l,r:tnode);
  40. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  41. procedure ppuwrite(ppufile:tcompilerppufile);override;
  42. procedure buildderefimpl;override;
  43. procedure derefimpl;override;
  44. function pass_1 : tnode;override;
  45. function pass_typecheck:tnode;override;
  46. function simplify(forinline: boolean) : tnode;override;
  47. function dogetcopy : tnode;override;
  48. function docompare(p: tnode): boolean; override;
  49. procedure printnodedata(var t:text);override;
  50. {$ifdef state_tracking}
  51. function track_state_pass(exec_known:boolean):boolean;override;
  52. {$endif}
  53. {$ifdef DEBUG_NODE_XML}
  54. procedure XMLPrintNodeInfo(var T: Text); override;
  55. {$endif DEBUG_NODE_XML}
  56. protected
  57. { override the following if you want to implement }
  58. { parts explicitely in the code generator (JM) }
  59. function first_addstring: tnode; virtual;
  60. function first_addset: tnode; virtual;
  61. function first_adddynarray : tnode; virtual;
  62. { only implements "muln" nodes, the rest always has to be done in }
  63. { the code generator for performance reasons (JM) }
  64. function first_add64bitint: tnode; virtual;
  65. function first_addpointer: tnode; virtual;
  66. function first_cmppointer: tnode; virtual;
  67. { override and return false if you can handle 32x32->64 }
  68. { bit multiplies directly in your code generator. If }
  69. { this function is overridden to return false, you can }
  70. { get multiplies with left/right both s32bit or u32bit, }
  71. { and resultdef of the muln s64bit or u64bit }
  72. function use_generic_mul32to64: boolean; virtual;
  73. { override and return false if code generator can handle }
  74. { full 64 bit multiplies. }
  75. function use_generic_mul64bit: boolean; virtual;
  76. {$ifdef cpuneedsmulhelper}
  77. { override to customize to decide if the code generator }
  78. { can handle a given multiply node directly, or it needs helpers }
  79. function use_mul_helper: boolean; virtual;
  80. {$endif cpuneedsmulhelper}
  81. { shall be overriden if the target cpu supports
  82. an fma instruction
  83. }
  84. function use_fma : boolean; virtual;
  85. { This routine calls internal runtime library helpers
  86. for all floating point arithmetic in the case
  87. where the emulation switches is on. Otherwise
  88. returns nil, and everything must be done in
  89. the code generation phase.
  90. }
  91. function first_addfloat : tnode; virtual;
  92. {
  93. generates softfloat code for the node
  94. }
  95. function first_addfloat_soft: tnode; virtual;
  96. private
  97. { checks whether a muln can be calculated as a 32bit }
  98. { * 32bit -> 64 bit }
  99. function try_make_mul32to64: boolean;
  100. { Match against the ranges, i.e.:
  101. var a:1..10;
  102. begin
  103. if a>0 then
  104. ...
  105. always evaluates to true. (DM)
  106. }
  107. function cmp_of_disjunct_ranges(var res : boolean) : boolean;
  108. { tries to replace the current node by a fma node }
  109. function try_fma(ld,rd : tdef) : tnode;
  110. end;
  111. taddnodeclass = class of taddnode;
  112. var
  113. { caddnode is used to create nodes of the add type }
  114. { the virtual constructor allows to assign }
  115. { another class type to caddnode => processor }
  116. { specific node types can be created }
  117. caddnode : taddnodeclass = taddnode;
  118. implementation
  119. uses
  120. {$IFNDEF USE_FAKE_SYSUTILS}
  121. sysutils,
  122. {$ELSE}
  123. fksysutl,
  124. {$ENDIF}
  125. globtype,systems,constexp,compinnr,
  126. cutils,verbose,globals,widestr,
  127. tokens,
  128. symconst,symdef,symsym,symcpu,symtable,defutil,defcmp,
  129. cgbase,
  130. htypechk,pass_1,
  131. nld,nbas,nmat,ncnv,ncon,nset,nopt,ncal,ninl,nmem,nutils,
  132. {$ifdef state_tracking}
  133. nstate,
  134. {$endif}
  135. cpuinfo,
  136. ppu;
  137. const
  138. swap_relation: array [ltn..unequaln] of Tnodetype=(gtn, gten, ltn, lten, equaln, unequaln);
  139. {*****************************************************************************
  140. TADDNODE
  141. *****************************************************************************}
  142. {$maxfpuregisters 0}
  143. function getbestreal(t1,t2 : tdef) : tdef;
  144. const
  145. floatweight : array[tfloattype] of byte =
  146. (2,3,4,5,0,1,6);
  147. begin
  148. if t1.typ=floatdef then
  149. begin
  150. result:=t1;
  151. if t2.typ=floatdef then
  152. begin
  153. { when a comp or currency is used, use always the
  154. best float type to calculate the result }
  155. if (tfloatdef(t1).floattype in [s64comp,s64currency]) or
  156. (tfloatdef(t2).floattype in [s64comp,s64currency]) or
  157. (cs_excessprecision in current_settings.localswitches) then
  158. result:=pbestrealtype^
  159. else
  160. if floatweight[tfloatdef(t2).floattype]>floatweight[tfloatdef(t1).floattype] then
  161. result:=t2;
  162. end;
  163. end
  164. else if t2.typ=floatdef then
  165. result:=t2
  166. else internalerror(200508061);
  167. end;
  168. constructor taddnode.create(tt : tnodetype;l,r : tnode);
  169. begin
  170. inherited create(tt,l,r);
  171. addnodeflags:=[];
  172. end;
  173. constructor taddnode.create_internal(tt:tnodetype;l,r:tnode);
  174. begin
  175. create(tt,l,r);
  176. include(flags,nf_internal);
  177. end;
  178. constructor taddnode.ppuload(t: tnodetype; ppufile: tcompilerppufile);
  179. begin
  180. inherited ppuload(t, ppufile);
  181. ppufile.getset(tppuset1(addnodeflags));
  182. ppufile.getderef(resultrealdefderef);
  183. end;
  184. procedure taddnode.ppuwrite(ppufile: tcompilerppufile);
  185. begin
  186. inherited ppuwrite(ppufile);
  187. ppufile.putset(tppuset1(addnodeflags));
  188. ppufile.putderef(resultrealdefderef);
  189. end;
  190. procedure taddnode.buildderefimpl;
  191. begin
  192. inherited buildderefimpl;
  193. resultrealdefderef.build(resultrealdef);
  194. end;
  195. procedure taddnode.derefimpl;
  196. begin
  197. inherited derefimpl;
  198. resultrealdef:=tdef(resultrealdefderef.resolve);
  199. end;
  200. function taddnode.cmp_of_disjunct_ranges(var res : boolean) : boolean;
  201. var
  202. hp : tnode;
  203. realdef : tdef;
  204. v : tconstexprint;
  205. begin
  206. result:=false;
  207. { check for comparision with known result because the ranges of the operands don't overlap }
  208. if (is_constintnode(right) and (left.resultdef.typ=orddef) and
  209. { don't ignore type checks }
  210. is_subequal(right.resultdef,left.resultdef)) or
  211. (is_constintnode(left) and (right.resultdef.typ=orddef) and
  212. { don't ignore type checks }
  213. is_subequal(left.resultdef,right.resultdef)) then
  214. begin
  215. if is_constintnode(right) then
  216. begin
  217. hp:=left;
  218. v:=Tordconstnode(right).value;
  219. end
  220. else
  221. begin
  222. hp:=right;
  223. v:=Tordconstnode(left).value;
  224. end;
  225. realdef:=hp.resultdef;
  226. { stop with finding the real def when we either encounter
  227. a) an explicit type conversion (then the value has to be
  228. re-interpreted)
  229. b) an "absolute" type conversion (also requires
  230. re-interpretation)
  231. }
  232. while (hp.nodetype=typeconvn) and
  233. ([nf_internal,nf_explicit,nf_absolute] * hp.flags = []) do
  234. begin
  235. hp:=ttypeconvnode(hp).left;
  236. realdef:=hp.resultdef;
  237. end;
  238. { could become an errordef in case of errors }
  239. if realdef.typ<>orddef then
  240. exit;
  241. if is_constintnode(left) then
  242. with torddef(realdef) do
  243. case nodetype of
  244. ltn:
  245. if v<low then
  246. begin
  247. result:=true;
  248. res:=true;
  249. end
  250. else if v>=high then
  251. begin
  252. result:=true;
  253. res:=false;
  254. end;
  255. lten:
  256. if v<=low then
  257. begin
  258. result:=true;
  259. res:=true;
  260. end
  261. else if v>high then
  262. begin
  263. result:=true;
  264. res:=false;
  265. end;
  266. gtn:
  267. if v<=low then
  268. begin
  269. result:=true;
  270. res:=false;
  271. end
  272. else if v>high then
  273. begin
  274. result:=true;
  275. res:=true;
  276. end;
  277. gten :
  278. if v<low then
  279. begin
  280. result:=true;
  281. res:=false;
  282. end
  283. else if v>=high then
  284. begin
  285. result:=true;
  286. res:=true;
  287. end;
  288. equaln:
  289. if (v<low) or (v>high) then
  290. begin
  291. result:=true;
  292. res:=false;
  293. end;
  294. unequaln:
  295. if (v<low) or (v>high) then
  296. begin
  297. result:=true;
  298. res:=true;
  299. end;
  300. else
  301. ;
  302. end
  303. else
  304. with torddef(realdef) do
  305. case nodetype of
  306. ltn:
  307. if high<v then
  308. begin
  309. result:=true;
  310. res:=true;
  311. end
  312. else if low>=v then
  313. begin
  314. result:=true;
  315. res:=false;
  316. end;
  317. lten:
  318. if high<=v then
  319. begin
  320. result:=true;
  321. res:=true;
  322. end
  323. else if low>v then
  324. begin
  325. result:=true;
  326. res:=false;
  327. end;
  328. gtn:
  329. if high<=v then
  330. begin
  331. result:=true;
  332. res:=false;
  333. end
  334. else if low>v then
  335. begin
  336. result:=true;
  337. res:=true;
  338. end;
  339. gten:
  340. if high<v then
  341. begin
  342. result:=true;
  343. res:=false;
  344. end
  345. else if low>=v then
  346. begin
  347. result:=true;
  348. res:=true;
  349. end;
  350. equaln:
  351. if (v<low) or (v>high) then
  352. begin
  353. result:=true;
  354. res:=false;
  355. end;
  356. unequaln:
  357. if (v<low) or (v>high) then
  358. begin
  359. result:=true;
  360. res:=true;
  361. end;
  362. else
  363. ;
  364. end;
  365. end;
  366. end;
  367. function taddnode.simplify(forinline : boolean) : tnode;
  368. function is_range_test(nodel, noder: taddnode; out value: tnode; var cl,cr: Tconstexprint): boolean;
  369. const
  370. is_upper_test: array[ltn..gten] of boolean = (true,true,false,false);
  371. inclusive_adjust: array[boolean,boolean,ltn..gten] of integer = (((-1,0,1,0),
  372. (1,0,-1,0)),
  373. ((0,-1,0,1),
  374. (0,1,0,-1)));
  375. var
  376. swapl, swapr, inverted_range: Boolean;
  377. valuer: tnode;
  378. t: Tconstexprint;
  379. begin
  380. result:=false;
  381. swapl:=false;
  382. swapr:=false;
  383. if nodel.left.nodetype=ordconstn then
  384. begin
  385. swapl:=true;
  386. cl:=tordconstnode(nodel.left).value;
  387. value:=nodel.right;
  388. end
  389. else if nodel.right.nodetype=ordconstn then
  390. begin
  391. cl:=tordconstnode(nodel.right).value;
  392. value:=nodel.left;
  393. end
  394. else
  395. exit;
  396. if noder.left.nodetype=ordconstn then
  397. begin
  398. swapl:=true;
  399. cr:=tordconstnode(noder.left).value;
  400. valuer:=noder.right;
  401. end
  402. else if noder.right.nodetype=ordconstn then
  403. begin
  404. cr:=tordconstnode(noder.right).value;
  405. valuer:=noder.left;
  406. end
  407. else
  408. exit;
  409. if not value.isequal(valuer) then
  410. exit;
  411. { This is based on De Morgan's theorem, namely that
  412. "A and B" = "not ((not A) or (not B))" }
  413. inverted_range:=(nodetype=orn);
  414. if inverted_range then
  415. begin
  416. swapl:=not swapl;
  417. swapr:=not swapr;
  418. end;
  419. { this could be simplified too, but probably never happens }
  420. if (is_upper_test[nodel.nodetype] xor swapl)=(is_upper_test[noder.nodetype] xor swapr) then
  421. exit;
  422. cl:=cl+inclusive_adjust[inverted_range,swapl,nodel.nodetype];
  423. cr:=cr+inclusive_adjust[inverted_range,swapr,noder.nodetype];
  424. if is_upper_test[nodel.nodetype] xor swapl then
  425. begin
  426. t:=cl;
  427. cl:=cr;
  428. cr:=t;
  429. end;
  430. if cl>cr then
  431. exit;
  432. result:=true;
  433. end;
  434. function TryHandleLengthZero(L,R : tnode; op : tnodetype; var resn : tnode) : boolean;
  435. var
  436. swapn : tnode;
  437. begin
  438. result:=false;
  439. { Attempt to handle Length(S) = 0, <> 0, > 0, < 0, >= 0, <= 0. }
  440. if not (op in [equaln,unequaln,ltn,lten,gtn,gten]) then
  441. exit;
  442. if not is_inlinefunction(L,in_length_x) then
  443. if is_inlinefunction(R,in_length_x) then
  444. begin
  445. op:=swap_relation[op];
  446. swapn:=L;
  447. L:=R;
  448. R:=swapn;
  449. end
  450. else
  451. exit;
  452. if not is_constintvalue(R,0) or is_shortstring(tinlinenode(L).left.resultdef) then
  453. exit;
  454. { Length = 0, <> 0, > 0, <= 0 are reduced to Length = 0. }
  455. if op in [equaln,unequaln,gtn,lten] then
  456. begin
  457. { “pointer(L.left) = nil”. Steal L.left instead of getcopy, zero a bit later. }
  458. resn:=caddnode.create_internal(equaln,ctypeconvnode.create_internal(tinlinenode(L).left,voidpointertype),
  459. cpointerconstnode.create(0,voidpointertype));
  460. { COM widestrings have 32-bit lengths, and can explicitly have 0 while being non-nil. }
  461. if is_widestring(tinlinenode(L).left.resultdef) and (tf_winlikewidestring in target_info.flags) then
  462. begin
  463. { Expand to “(pointer(L.left) = nil) or (PUint32(L.left)[-1] = 0)”. }
  464. resn:=caddnode.create_internal(orn,
  465. resn,
  466. caddnode.create_internal(equaln,
  467. ctypeconvnode.create_internal(
  468. cderefnode.create(
  469. caddnode.create_internal(subn,ctypeconvnode.create_internal(tinlinenode(L).left.getcopy,voidpointertype),
  470. cordconstnode.create(sizeof(uint32),ptruinttype,false))
  471. ),u32inttype
  472. ),
  473. cordconstnode.create(0,u32inttype,false))
  474. );
  475. include(taddnode(resn).addnodeflags,anf_short_bool);
  476. end;
  477. tinlinenode(L).left:=nil; { Was stolen inside resn, and no longer of interest. }
  478. { resn now checks for Length = 0. For Length <> 0, invert. }
  479. if op in [unequaln,gtn] then
  480. resn:=cnotnode.create(resn);
  481. exit(true);
  482. end;
  483. { Warn on Length < 0 and Length >= 0. }
  484. if not (tnf_pass1_done in L.transientflags) then { ...Only once. }
  485. if op=gten then
  486. Message(type_w_comparison_always_true)
  487. else
  488. Message(type_w_comparison_always_false);
  489. { Length < 0 is always false, Length >= 0 is always true. }
  490. if not might_have_sideeffects(tinlinenode(L).left) then { Could somehow remove the check but keep the F() even in Length(F()) >= 0... }
  491. begin
  492. resn:=cordconstnode.create(ord(op=gten),resultdef,true);
  493. exit(true);
  494. end;
  495. end;
  496. function GetCopyAndTypeCheck: tnode;
  497. begin
  498. result:=getcopy;
  499. result.resultdef:=nil;
  500. result:=ctypeconvnode.create_internal(result,resultdef);
  501. do_typecheckpass(result);
  502. end;
  503. function IsAndOrAndNot(n1,n2,n3,n4 : tnode): Boolean;
  504. begin
  505. result:=(n4.nodetype=notn) and
  506. tnotnode(n4).left.isequal(n2);
  507. end;
  508. function TransformAndOrAndNot(n1,n2,n3,n4 : tnode): tnode;
  509. begin
  510. result:=caddnode.create_internal(xorn,n3.getcopy,
  511. caddnode.create_internal(andn,caddnode.create_internal(xorn,n3.getcopy,n1.getcopy),n2.getcopy));
  512. end;
  513. function SwapRightWithLeftRight : tnode;
  514. var
  515. hp : tnode;
  516. begin
  517. hp:=right;
  518. right:=taddnode(left).right;
  519. taddnode(left).right:=hp;
  520. left:=left.simplify(forinline);
  521. if resultdef.typ<>pointerdef then
  522. begin
  523. { ensure that the constant is not expanded to a larger type due to overflow,
  524. but this is only useful if no pointer operation is done }
  525. left:=ctypeconvnode.create_internal(left,resultdef);
  526. do_typecheckpass(left);
  527. end;
  528. result:=GetCopyAndTypeCheck;
  529. end;
  530. function SwapRightWithLeftLeft : tnode;
  531. var
  532. hp,hp2 : tnode;
  533. begin
  534. { keep the order of val+const else pointer and string operations might cause an error }
  535. hp:=taddnode(left).left;
  536. taddnode(left).left:=taddnode(left).right;
  537. taddnode(left).right:=right;
  538. left.resultdef:=nil;
  539. do_typecheckpass(left);
  540. hp2:=left.simplify(forinline);
  541. if assigned(hp2) then
  542. left:=hp2;
  543. if resultdef.typ<>pointerdef then
  544. begin
  545. { ensure that the constant is not expanded to a larger type due to overflow,
  546. but this is only useful if no pointer operation is done }
  547. left:=ctypeconvnode.create_internal(left,resultdef);
  548. do_typecheckpass(left);
  549. end
  550. else if tpointerdef(resultdef).pointeddef.size>1 then
  551. { the constants were already multiplied by the pointer element size }
  552. left:=cmoddivnode.create(divn,left,cordconstnode.create(tpointerdef(resultdef).pointeddef.size,left.resultdef,false));
  553. right:=left;
  554. left:=hp;
  555. result:=GetCopyAndTypeCheck;
  556. end;
  557. function SwapLeftWithRightRight : tnode;
  558. var
  559. hp,hp2 : tnode;
  560. begin
  561. { keep the order of val+const else string operations might cause an error }
  562. hp:=taddnode(right).right;
  563. taddnode(right).right:=taddnode(right).left;
  564. taddnode(right).left:=left;
  565. right.resultdef:=nil;
  566. do_typecheckpass(right);
  567. hp2:=right.simplify(forinline);
  568. if assigned(hp2) then
  569. right:=hp2;
  570. if resultdef.typ<>pointerdef then
  571. begin
  572. { ensure that the constant is not expanded to a larger type due to overflow,
  573. but this is only useful if no pointer operation is done }
  574. right:=ctypeconvnode.create_internal(right,resultdef);
  575. do_typecheckpass(right);
  576. end;
  577. left:=right;
  578. right:=hp;
  579. result:=GetCopyAndTypeCheck;
  580. end;
  581. function SwapLeftWithRightLeft : tnode;
  582. var
  583. hp: tnode;
  584. begin
  585. hp:=left;
  586. left:=taddnode(right).left;
  587. taddnode(right).left:=hp;
  588. right:=right.simplify(false);
  589. result:=GetCopyAndTypeCheck;
  590. end;
  591. var
  592. hp: taddnode;
  593. t,vl,lefttarget,righttarget: tnode;
  594. lt,rt,nt : tnodetype;
  595. hdef,
  596. rd,ld , inttype: tdef;
  597. rv,lv,v : tconstexprint;
  598. rvd,lvd : bestreal;
  599. ws1,ws2 : tcompilerwidestring;
  600. concatstrings : boolean;
  601. c1,c2 : array[0..1] of char;
  602. s1,s2,stmp : pchar;
  603. l1,l2 : longint;
  604. resultset : Tconstset;
  605. res,
  606. b : boolean;
  607. cr, cl : Tconstexprint;
  608. v2p, c2p, c1p, v1p: pnode;
  609. p1,p2: TConstPtrUInt;
  610. begin
  611. result:=nil;
  612. l1:=0;
  613. l2:=0;
  614. s1:=nil;
  615. s2:=nil;
  616. { load easier access variables }
  617. rd:=right.resultdef;
  618. ld:=left.resultdef;
  619. rt:=right.nodetype;
  620. lt:=left.nodetype;
  621. if (nodetype = slashn) and
  622. (((rt = ordconstn) and
  623. (tordconstnode(right).value = 0)) or
  624. ((rt = realconstn) and
  625. (trealconstnode(right).value_real = 0.0))) then
  626. begin
  627. if floating_point_range_check_error then
  628. begin
  629. result:=crealconstnode.create(1,pbestrealtype^);
  630. Message(parser_e_division_by_zero);
  631. exit;
  632. end;
  633. end;
  634. { both are int constants }
  635. if (
  636. is_constintnode(left) and
  637. is_constintnode(right)
  638. ) or
  639. (
  640. is_constboolnode(left) and
  641. is_constboolnode(right) and
  642. (nodetype in [slashn,ltn,lten,gtn,gten,equaln,unequaln,andn,xorn,orn])
  643. ) or
  644. (
  645. is_constenumnode(left) and
  646. is_constenumnode(right) and
  647. (allowenumop(nodetype) or (nf_internal in flags))
  648. ) or
  649. (
  650. (lt in [pointerconstn,niln]) and
  651. is_constintnode(right) and
  652. (nodetype in [addn,subn])
  653. ) or
  654. (
  655. (rt in [pointerconstn,niln]) and
  656. is_constintnode(left) and
  657. (nodetype=addn)
  658. ) or
  659. (
  660. (lt in [pointerconstn,niln]) and
  661. (rt in [pointerconstn,niln]) and
  662. (nodetype in [ltn,lten,gtn,gten,equaln,unequaln,subn])
  663. ) or
  664. (
  665. (lt = ordconstn) and (ld.typ = orddef) and is_currency(ld) and
  666. (rt = ordconstn) and (rd.typ = orddef) and is_currency(rd)
  667. ) then
  668. begin
  669. t:=nil;
  670. { load values }
  671. lv:=get_int_value(left);
  672. rv:=get_int_value(right);
  673. { type checking already took care of multiplying }
  674. { integer constants with pointeddef.size if necessary }
  675. case nodetype of
  676. addn :
  677. begin
  678. v:=lv+rv;
  679. if v.overflow then
  680. begin
  681. Message(parser_e_arithmetic_operation_overflow);
  682. { Recover }
  683. t:=genintconstnode(0)
  684. end
  685. else if is_constpointernode(left) or is_constpointernode(right) then
  686. t := cpointerconstnode.create(qword(v),resultdef)
  687. else
  688. if is_integer(ld) then
  689. t := create_simplified_ord_const(v,resultdef,forinline,cs_check_overflow in localswitches)
  690. else
  691. t := cordconstnode.create(v,resultdef,(ld.typ<>enumdef));
  692. end;
  693. subn :
  694. begin
  695. v:=lv-rv;
  696. if v.overflow then
  697. begin
  698. Message(parser_e_arithmetic_operation_overflow);
  699. { Recover }
  700. t:=genintconstnode(0)
  701. end
  702. else if (lt=pointerconstn) then
  703. { pointer-pointer results in an integer }
  704. if (rt=pointerconstn) then
  705. begin
  706. if not(anf_has_pointerdiv in addnodeflags) then
  707. internalerror(2008030101);
  708. t:=cpointerconstnode.create(qword(v),resultdef)
  709. end
  710. else
  711. t:=cpointerconstnode.create(qword(v),resultdef)
  712. else
  713. if is_integer(ld) then
  714. t:=create_simplified_ord_const(v,resultdef,forinline,cs_check_overflow in localswitches)
  715. else
  716. t:=cordconstnode.create(v,resultdef,(ld.typ<>enumdef));
  717. end;
  718. muln :
  719. begin
  720. v:=lv*rv;
  721. if v.overflow then
  722. begin
  723. message(parser_e_arithmetic_operation_overflow);
  724. { Recover }
  725. t:=genintconstnode(0)
  726. end
  727. else
  728. t := create_simplified_ord_const(v,resultdef,forinline,cs_check_overflow in localswitches)
  729. end;
  730. xorn :
  731. if is_integer(ld) then
  732. t := create_simplified_ord_const(lv xor rv,resultdef,forinline,false)
  733. else
  734. t:=cordconstnode.create(lv xor rv,resultdef,true);
  735. orn :
  736. if is_integer(ld) then
  737. t:=create_simplified_ord_const(lv or rv,resultdef,forinline,false)
  738. else
  739. t:=cordconstnode.create(lv or rv,resultdef,true);
  740. andn :
  741. if is_integer(ld) then
  742. t:=create_simplified_ord_const(lv and rv,resultdef,forinline,false)
  743. else
  744. t:=cordconstnode.create(lv and rv,resultdef,true);
  745. ltn :
  746. t:=cordconstnode.create(ord(lv<rv),pasbool1type,true);
  747. lten :
  748. t:=cordconstnode.create(ord(lv<=rv),pasbool1type,true);
  749. gtn :
  750. t:=cordconstnode.create(ord(lv>rv),pasbool1type,true);
  751. gten :
  752. t:=cordconstnode.create(ord(lv>=rv),pasbool1type,true);
  753. equaln :
  754. t:=cordconstnode.create(ord(lv=rv),pasbool1type,true);
  755. unequaln :
  756. t:=cordconstnode.create(ord(lv<>rv),pasbool1type,true);
  757. slashn :
  758. begin
  759. { int/int becomes a real }
  760. rvd:=rv;
  761. lvd:=lv;
  762. t:=crealconstnode.create(lvd/rvd,resultrealdef);
  763. end;
  764. else
  765. internalerror(2008022101);
  766. end;
  767. result:=t;
  768. exit;
  769. end
  770. else if cmp_of_disjunct_ranges(res) then
  771. begin
  772. if res then
  773. t:=Cordconstnode.create(1,pasbool1type,true)
  774. else
  775. t:=Cordconstnode.create(0,pasbool1type,true);
  776. { don't do this optimization, if the variable expression might
  777. have a side effect }
  778. if (is_constintnode(left) and might_have_sideeffects(right)) or
  779. (is_constintnode(right) and might_have_sideeffects(left)) then
  780. t.free
  781. else
  782. result:=t;
  783. exit;
  784. end;
  785. { For operations that follow the commutative law, put integer constants on the right }
  786. if (nodetype in [addn,orn,xorn,andn,muln]) and
  787. is_constintnode(left) and (is_integer(right.resultdef) or is_pointer(right.resultdef)) then
  788. SwapLeftRight;
  789. { Add,Sub,Mul,Or,Xor,Andn with constant 0, 1 or -1? }
  790. if is_constintnode(right) and (is_integer(ld) or is_pointer(ld)) then
  791. begin
  792. if (tordconstnode(right).value = 0) and (nodetype in [addn,subn,orn,xorn,andn,muln]) then
  793. begin
  794. case nodetype of
  795. addn,subn,orn,xorn:
  796. result := PruneKeepLeft();
  797. andn,muln:
  798. begin
  799. if (cs_opt_level4 in current_settings.optimizerswitches) or
  800. not might_have_sideeffects(left) then
  801. result:=cordconstnode.create(0,resultdef,true);
  802. end
  803. else
  804. ;
  805. end;
  806. end
  807. else if (tordconstnode(right).value = 1) and (nodetype=muln) then
  808. { insert type conversion in case it is a 32*32 to 64 bit multiplication optimization,
  809. the type conversion does not hurt because it is normally removed later on
  810. }
  811. result := ctypeconvnode.create_internal(PruneKeepLeft(),resultdef)
  812. { try to fold
  813. op op
  814. / \ / \
  815. op const1 or op const1
  816. / \ / \
  817. const2 val val const2
  818. }
  819. else if (left.nodetype=nodetype) and
  820. { there might be a mul operation e.g. longint*longint => int64 in this case
  821. we cannot do this optimziation, see e.g. tests/webtbs/tw36587.pp on arm }
  822. (compare_defs(resultdef,ld,nothingn)=te_exact) then
  823. begin
  824. if is_constintnode(taddnode(left).left) then
  825. begin
  826. case left.nodetype of
  827. xorn,
  828. addn,
  829. andn,
  830. orn,
  831. muln:
  832. Result:=SwapRightWithLeftRight;
  833. else
  834. ;
  835. end;
  836. end
  837. else if is_constintnode(taddnode(left).right) then
  838. begin
  839. case left.nodetype of
  840. xorn,
  841. addn,
  842. andn,
  843. orn,
  844. muln:
  845. Result:=SwapRightWithLeftLeft;
  846. else
  847. ;
  848. end;
  849. end
  850. end;
  851. if assigned(result) then
  852. exit;
  853. { multiplication by -1? Convert it into an unary minus if the other conversions before failed, don't do
  854. it before the folding above, see #40448 }
  855. if (tordconstnode(right).value = -1) and (nodetype=muln) then
  856. result := ctypeconvnode.create_internal(cunaryminusnode.create(PruneKeepLeft()),ld);
  857. if assigned(result) then
  858. exit;
  859. end;
  860. { Deal with anti-commutative subtraction }
  861. if (nodetype = subn) then
  862. begin
  863. { transform -1-x into not(x) }
  864. if is_signed(rd) and is_constintnode(left) and (tordconstnode(left).value=-1) then
  865. begin
  866. result:=cnotnode.create(right.getcopy);
  867. exit;
  868. end
  869. { change "0 - val" to "-val" }
  870. else if is_constintnode(left) and (is_integer(right.resultdef) or is_pointer(right.resultdef)) then
  871. begin
  872. if (tordconstnode(left).value = 0) then
  873. result := ctypeconvnode.create_internal(cunaryminusnode.create(right.getcopy),right.resultdef);
  874. end
  875. { convert n - n mod const into n div const*const }
  876. else if (right.nodetype=modn) and is_constintnode(tmoddivnode(right).right) and
  877. (left.isequal(tmoddivnode(right).left)) and not(might_have_sideeffects(left)) { and
  878. not(cs_check_overflow in localswitches) } then
  879. begin
  880. result:=caddnode.create(muln,cmoddivnode.create(divn,left,tmoddivnode(right).right.getcopy),tmoddivnode(right).right);
  881. left:=nil;
  882. tmoddivnode(right).right:=nil;
  883. exit;
  884. end
  885. { transform -x-1 into not(x) }
  886. else if is_signed(ld) and is_constintnode(right) and (tordconstnode(right).value=1) and (left.nodetype=unaryminusn) then
  887. begin
  888. result:=cnotnode.create(tunaryminusnode(left).left.getcopy);
  889. exit;
  890. end;
  891. end;
  892. { both real constants ? }
  893. if (lt=realconstn) and (rt=realconstn) then
  894. begin
  895. lvd:=trealconstnode(left).value_real;
  896. rvd:=trealconstnode(right).value_real;
  897. case nodetype of
  898. addn :
  899. t:=crealconstnode.create(lvd+rvd,resultrealdef);
  900. subn :
  901. t:=crealconstnode.create(lvd-rvd,resultrealdef);
  902. muln :
  903. t:=crealconstnode.create(lvd*rvd,resultrealdef);
  904. starstarn:
  905. begin
  906. if lvd<0 then
  907. begin
  908. Message(parser_e_invalid_float_operation);
  909. t:=crealconstnode.create(0,resultrealdef);
  910. end
  911. else if lvd=0 then
  912. t:=crealconstnode.create(1.0,resultrealdef)
  913. else
  914. t:=crealconstnode.create(exp(ln(lvd)*rvd),resultrealdef);
  915. end;
  916. slashn :
  917. t:=crealconstnode.create(lvd/rvd,resultrealdef);
  918. ltn :
  919. t:=cordconstnode.create(ord(lvd<rvd),pasbool1type,true);
  920. lten :
  921. t:=cordconstnode.create(ord(lvd<=rvd),pasbool1type,true);
  922. gtn :
  923. t:=cordconstnode.create(ord(lvd>rvd),pasbool1type,true);
  924. gten :
  925. t:=cordconstnode.create(ord(lvd>=rvd),pasbool1type,true);
  926. equaln :
  927. t:=cordconstnode.create(ord(lvd=rvd),pasbool1type,true);
  928. unequaln :
  929. t:=cordconstnode.create(ord(lvd<>rvd),pasbool1type,true);
  930. else
  931. internalerror(2008022102);
  932. end;
  933. result:=t;
  934. if nf_is_currency in flags then
  935. include(result.flags,nf_is_currency);
  936. exit;
  937. end;
  938. if is_real(resultdef) then
  939. begin
  940. if (nodetype=addn) then
  941. begin
  942. { -left+right => right-left,
  943. this operation is always valid }
  944. if (left.nodetype=unaryminusn) then
  945. begin
  946. t := tunaryminusnode(left).PruneKeepLeft();
  947. result:=caddnode.create(subn,PruneKeepRight(),t);
  948. exit;
  949. end;
  950. { left+(-right) => left-right,
  951. this operation is always valid }
  952. if (right.nodetype=unaryminusn) then
  953. begin
  954. t := tunaryminusnode(right).PruneKeepLeft();
  955. result:=caddnode.create(subn,PruneKeepLeft(),t);
  956. exit;
  957. end;
  958. end;
  959. { left-(-right) => left+right,
  960. this operation is always valid }
  961. if (nodetype=subn) and (right.nodetype=unaryminusn) then
  962. begin
  963. t := tunaryminusnode(right).PruneKeepLeft();
  964. result:=caddnode.create(addn,PruneKeepLeft(),t);
  965. exit;
  966. end;
  967. { (-left)*(-right) => left*right, and
  968. (-left)/(-right) => left/right,
  969. these operations are always valid }
  970. if (nodetype in [muln,slashn]) and (left.nodetype=unaryminusn) and (right.nodetype=unaryminusn) then
  971. begin
  972. t := tunaryminusnode(right).PruneKeepLeft();
  973. result:=caddnode.create(nodetype,tunaryminusnode(left).PruneKeepLeft(),t);
  974. exit;
  975. end;
  976. { optimize operations with real constants, but only if fast math is switched on as
  977. the operations could change e.g. the sign of 0 so they cannot be optimized always
  978. }
  979. if is_real(resultdef) then
  980. begin
  981. if lt=realconstn then
  982. begin
  983. if (trealconstnode(left).value_real=0) and (nodetype in [addn,muln,subn,slashn]) then
  984. begin
  985. case nodetype of
  986. addn:
  987. begin
  988. { -0.0+(+0.0)=+0.0 so we cannot carry out this optimization if no fastmath is passed }
  989. if not(cs_opt_fastmath in current_settings.optimizerswitches) then
  990. begin
  991. result:=PruneKeepRight();
  992. exit;
  993. end;
  994. end;
  995. slashn,
  996. muln:
  997. if not(might_have_sideeffects(right,[mhs_exceptions])) then
  998. begin
  999. result:=PruneKeepLeft;
  1000. exit;
  1001. end;
  1002. subn:
  1003. begin
  1004. t := PruneKeepRight();
  1005. result:=ctypeconvnode.create_internal(cunaryminusnode.create(t),rd);
  1006. exit;
  1007. end;
  1008. else
  1009. Internalerror(2020060801);
  1010. end;
  1011. end
  1012. else if (trealconstnode(left).value_real=1) and (nodetype=muln) then
  1013. begin
  1014. result:=right.getcopy;
  1015. exit;
  1016. end
  1017. else if (trealconstnode(left).value_real=2) and (nodetype=muln) and not(might_have_sideeffects(right,[mhs_exceptions])) and
  1018. (node_complexity(right)<=1) then
  1019. begin
  1020. result:=caddnode.create_internal(addn,right.getcopy,right.getcopy);
  1021. exit;
  1022. end;
  1023. end
  1024. else if rt=realconstn then
  1025. begin
  1026. if (trealconstnode(right).value_real=0) and (nodetype in [addn,muln,subn]) then
  1027. begin
  1028. case nodetype of
  1029. subn,
  1030. addn:
  1031. begin
  1032. result:=left.getcopy;
  1033. exit;
  1034. end;
  1035. muln:
  1036. if not(might_have_sideeffects(left,[mhs_exceptions])) then
  1037. begin
  1038. result:=right.getcopy;
  1039. exit;
  1040. end;
  1041. else
  1042. Internalerror(2020060802);
  1043. end;
  1044. end
  1045. else if (trealconstnode(right).value_real=1) and (nodetype in [muln,slashn]) then
  1046. begin
  1047. result:=left.getcopy;
  1048. exit;
  1049. end
  1050. else if (trealconstnode(right).value_real=2) and (nodetype=muln) and not(might_have_sideeffects(left,[mhs_exceptions])) and
  1051. (node_complexity(left)<=1) then
  1052. begin
  1053. result:=caddnode.create_internal(addn,left.getcopy,left.getcopy);
  1054. exit;
  1055. end;
  1056. end
  1057. { optimize a/a and a-a }
  1058. else if (((cs_opt_level2 in current_settings.optimizerswitches) and (nodetype=subn)) or
  1059. (([cs_opt_fastmath,cs_opt_level2]*current_settings.optimizerswitches=[cs_opt_fastmath,cs_opt_level2]) and (nodetype=slashn))
  1060. ) and
  1061. left.isequal(right) and not(might_have_sideeffects(left,[mhs_exceptions])) then
  1062. begin
  1063. case nodetype of
  1064. subn:
  1065. result:=crealconstnode.create(0,ld);
  1066. slashn:
  1067. result:=crealconstnode.create(1,ld);
  1068. else
  1069. Internalerror(2020060901);
  1070. end;
  1071. end;
  1072. end;
  1073. end;
  1074. {$if sizeof(bestrealrec) = sizeof(bestreal)}
  1075. { replace .../const by a multiplication, but only if fastmath is enabled or
  1076. the division is done by a power of 2, do not mess with special floating point values like Inf etc.
  1077. do this after constant folding to avoid unnecessary precision loss if
  1078. an slash expresion would be first converted into a multiplication and later
  1079. folded }
  1080. if (nodetype=slashn) and
  1081. { do not mess with currency and comp types }
  1082. (not(is_currency(rd)) and
  1083. not((rd.typ=floatdef) and
  1084. (tfloatdef(rd).floattype=s64comp)
  1085. )
  1086. ) and
  1087. (((cs_opt_fastmath in current_settings.optimizerswitches) and (rt=ordconstn)) or
  1088. ((cs_opt_fastmath in current_settings.optimizerswitches) and (rt=realconstn) and
  1089. (bestrealrec(trealconstnode(right).value_real).SpecialType in [fsPositive,fsNegative])
  1090. ) or
  1091. ((rt=realconstn) and
  1092. (bestrealrec(trealconstnode(right).value_real).SpecialType in [fsPositive,fsNegative]) and
  1093. { mantissa returns the mantissa/fraction without the hidden 1, so power of two means only the hidden
  1094. bit is set => mantissa must be 0 }
  1095. (bestrealrec(trealconstnode(right).value_real).Mantissa=0)
  1096. )
  1097. ) then
  1098. case rt of
  1099. ordconstn:
  1100. begin
  1101. { the normal code handles div/0 }
  1102. if (tordconstnode(right).value<>0) then
  1103. begin
  1104. nodetype:=muln;
  1105. t:=crealconstnode.create(1/tordconstnode(right).value,resultdef);
  1106. right.free;
  1107. right:=t;
  1108. exit;
  1109. end;
  1110. end;
  1111. realconstn:
  1112. begin
  1113. nodetype:=muln;
  1114. trealconstnode(right).value_real:=1.0/trealconstnode(right).value_real;
  1115. exit;
  1116. end;
  1117. else
  1118. ;
  1119. end;
  1120. {$endif sizeof(bestrealrec) = sizeof(bestreal)}
  1121. { first, we handle widestrings, so we can check later for }
  1122. { stringconstn only }
  1123. { widechars are converted above to widestrings too }
  1124. { this isn't ver y efficient, but I don't think }
  1125. { that it does matter that much (FK) }
  1126. if (lt=stringconstn) and (rt=stringconstn) and
  1127. (tstringconstnode(left).cst_type in [cst_widestring,cst_unicodestring]) and
  1128. (tstringconstnode(right).cst_type in [cst_widestring,cst_unicodestring]) then
  1129. begin
  1130. initwidestring(ws1);
  1131. initwidestring(ws2);
  1132. copywidestring(tstringconstnode(left).valuews,ws1);
  1133. copywidestring(tstringconstnode(right).valuews,ws2);
  1134. case nodetype of
  1135. addn :
  1136. begin
  1137. concatwidestrings(ws1,ws2);
  1138. t:=cstringconstnode.createunistr(ws1);
  1139. end;
  1140. ltn :
  1141. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<0),pasbool1type,true);
  1142. lten :
  1143. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<=0),pasbool1type,true);
  1144. gtn :
  1145. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)>0),pasbool1type,true);
  1146. gten :
  1147. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)>=0),pasbool1type,true);
  1148. equaln :
  1149. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)=0),pasbool1type,true);
  1150. unequaln :
  1151. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<>0),pasbool1type,true);
  1152. else
  1153. internalerror(2008022103);
  1154. end;
  1155. donewidestring(ws1);
  1156. donewidestring(ws2);
  1157. result:=t;
  1158. exit;
  1159. end;
  1160. { concating strings ? }
  1161. concatstrings:=false;
  1162. if (lt=ordconstn) and (rt=ordconstn) and
  1163. is_char(ld) and is_char(rd) then
  1164. begin
  1165. c1[0]:=char(int64(tordconstnode(left).value));
  1166. c1[1]:=#0;
  1167. l1:=1;
  1168. c2[0]:=char(int64(tordconstnode(right).value));
  1169. c2[1]:=#0;
  1170. l2:=1;
  1171. s1:=@c1[0];
  1172. s2:=@c2[0];
  1173. concatstrings:=true;
  1174. end
  1175. else if (lt=stringconstn) and (rt=ordconstn) and is_char(rd) then
  1176. begin
  1177. l1:=tstringconstnode(left).len;
  1178. s1:=tstringconstnode(left).asconstpchar;
  1179. c2[0]:=char(int64(tordconstnode(right).value));
  1180. c2[1]:=#0;
  1181. s2:=@c2[0];
  1182. l2:=1;
  1183. concatstrings:=true;
  1184. end
  1185. else if (lt=ordconstn) and (rt=stringconstn) and is_char(ld) then
  1186. begin
  1187. c1[0]:=char(int64(tordconstnode(left).value));
  1188. c1[1]:=#0;
  1189. l1:=1;
  1190. s1:=@c1[0];
  1191. s2:=tstringconstnode(right).asconstpchar;
  1192. l2:=tstringconstnode(right).len;
  1193. concatstrings:=true;
  1194. end
  1195. else if (lt=stringconstn) and (rt=stringconstn) then
  1196. begin
  1197. s1:=tstringconstnode(left).asconstpchar;
  1198. l1:=tstringconstnode(left).len;
  1199. s2:=tstringconstnode(right).asconstpchar;
  1200. l2:=tstringconstnode(right).len;
  1201. concatstrings:=true;
  1202. end;
  1203. if concatstrings then
  1204. begin
  1205. case nodetype of
  1206. addn :
  1207. begin
  1208. stmp:=concatansistrings(s1,s2,l1,l2);
  1209. t:=cstringconstnode.createpchar(stmp,l1+l2,nil);
  1210. Freemem(stmp);
  1211. typecheckpass(t);
  1212. if not is_ansistring(resultdef) or
  1213. (tstringdef(resultdef).encoding<>globals.CP_NONE) then
  1214. tstringconstnode(t).changestringtype(resultdef)
  1215. else
  1216. tstringconstnode(t).changestringtype(getansistringdef)
  1217. end;
  1218. ltn :
  1219. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<0),pasbool1type,true);
  1220. lten :
  1221. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<=0),pasbool1type,true);
  1222. gtn :
  1223. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)>0),pasbool1type,true);
  1224. gten :
  1225. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)>=0),pasbool1type,true);
  1226. equaln :
  1227. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)=0),pasbool1type,true);
  1228. unequaln :
  1229. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<>0),pasbool1type,true);
  1230. else
  1231. internalerror(2008022104);
  1232. end;
  1233. result:=t;
  1234. exit;
  1235. end;
  1236. { set constant evaluation }
  1237. if (right.nodetype=setconstn) and
  1238. not assigned(tsetconstnode(right).left) and
  1239. (left.nodetype=setconstn) and
  1240. not assigned(tsetconstnode(left).left) then
  1241. begin
  1242. case nodetype of
  1243. addn :
  1244. begin
  1245. resultset:=tsetconstnode(right).value_set^ + tsetconstnode(left).value_set^;
  1246. t:=csetconstnode.create(@resultset,resultdef);
  1247. end;
  1248. muln :
  1249. begin
  1250. resultset:=tsetconstnode(right).value_set^ * tsetconstnode(left).value_set^;
  1251. t:=csetconstnode.create(@resultset,resultdef);
  1252. end;
  1253. subn :
  1254. begin
  1255. resultset:=tsetconstnode(left).value_set^ - tsetconstnode(right).value_set^;
  1256. t:=csetconstnode.create(@resultset,resultdef);
  1257. end;
  1258. symdifn :
  1259. begin
  1260. resultset:=tsetconstnode(right).value_set^ >< tsetconstnode(left).value_set^;
  1261. t:=csetconstnode.create(@resultset,resultdef);
  1262. end;
  1263. unequaln :
  1264. begin
  1265. b:=tsetconstnode(right).value_set^ <> tsetconstnode(left).value_set^;
  1266. t:=cordconstnode.create(byte(b),pasbool1type,true);
  1267. end;
  1268. equaln :
  1269. begin
  1270. b:=tsetconstnode(right).value_set^ = tsetconstnode(left).value_set^;
  1271. t:=cordconstnode.create(byte(b),pasbool1type,true);
  1272. end;
  1273. lten :
  1274. begin
  1275. b:=tsetconstnode(left).value_set^ <= tsetconstnode(right).value_set^;
  1276. t:=cordconstnode.create(byte(b),pasbool1type,true);
  1277. end;
  1278. gten :
  1279. begin
  1280. b:=tsetconstnode(left).value_set^ >= tsetconstnode(right).value_set^;
  1281. t:=cordconstnode.create(byte(b),pasbool1type,true);
  1282. end;
  1283. else
  1284. internalerror(2008022105);
  1285. end;
  1286. result:=t;
  1287. exit;
  1288. end;
  1289. { in case of expressions having no side effect, we can simplify boolean expressions
  1290. containing constants }
  1291. if is_boolean(ld) and is_boolean(rd) then
  1292. begin
  1293. if is_constboolnode(left) then
  1294. begin
  1295. if ((nodetype=andn) and (tordconstnode(left).value<>0)) or
  1296. ((nodetype=orn) and (tordconstnode(left).value=0)) or
  1297. ((nodetype=xorn) and (tordconstnode(left).value=0)) then
  1298. begin
  1299. Result := PruneKeepRight();
  1300. exit;
  1301. end
  1302. else if not(might_have_sideeffects(right)) and
  1303. (((nodetype=orn) and (tordconstnode(left).value<>0)) or
  1304. ((nodetype=andn) and (tordconstnode(left).value=0))) then
  1305. begin
  1306. Result := PruneKeepLeft();
  1307. exit;
  1308. end
  1309. else if ((nodetype=xorn) and (tordconstnode(left).value<>0)) then
  1310. begin
  1311. Result := cnotnode.create(PruneKeepRight());
  1312. exit;
  1313. end
  1314. end
  1315. else if is_constboolnode(right) then
  1316. begin
  1317. if ((nodetype=andn) and (tordconstnode(right).value<>0)) or
  1318. ((nodetype=orn) and (tordconstnode(right).value=0)) or
  1319. ((nodetype=xorn) and (tordconstnode(right).value=0)) then
  1320. begin
  1321. result := PruneKeepLeft();
  1322. exit;
  1323. end
  1324. else if not(might_have_sideeffects(left)) and
  1325. (((nodetype=orn) and (tordconstnode(right).value<>0)) or
  1326. ((nodetype=andn) and (tordconstnode(right).value=0))) then
  1327. begin
  1328. result := PruneKeepRight();
  1329. exit;
  1330. end
  1331. else if ((nodetype=xorn) and (tordconstnode(right).value<>0)) then
  1332. begin
  1333. result := cnotnode.create(PruneKeepLeft());
  1334. exit;
  1335. end
  1336. end;
  1337. end;
  1338. { check if
  1339. typeinfo(<type1>)=/<>typeinfo(<type2>)
  1340. can be evaluated at compile time
  1341. }
  1342. lefttarget:=actualtargetnode(@left)^;
  1343. righttarget:=actualtargetnode(@right)^;
  1344. if (nodetype in [equaln,unequaln]) and (lefttarget.nodetype=inlinen) and (righttarget.nodetype=inlinen) and
  1345. (tinlinenode(lefttarget).inlinenumber=in_typeinfo_x) and (tinlinenode(righttarget).inlinenumber=in_typeinfo_x) and
  1346. not (tinlinenode(lefttarget).left.resultdef.typ in [undefineddef,errordef]) and
  1347. not (tinlinenode(righttarget).left.resultdef.typ in [undefineddef,errordef]) then
  1348. begin
  1349. case nodetype of
  1350. equaln:
  1351. result:=cordconstnode.create(ord(tinlinenode(lefttarget).left.resultdef=tinlinenode(righttarget).left.resultdef),bool8type,false);
  1352. unequaln:
  1353. result:=cordconstnode.create(ord(tinlinenode(lefttarget).left.resultdef<>tinlinenode(righttarget).left.resultdef),bool8type,false);
  1354. else
  1355. Internalerror(2020092901);
  1356. end;
  1357. exit;
  1358. end;
  1359. if is_constpointernode(left) and is_constpointernode(right) then
  1360. begin
  1361. p1:=0;
  1362. p2:=0;
  1363. if left.nodetype=pointerconstn then
  1364. p1:=tpointerconstnode(left).value;
  1365. if right.nodetype=pointerconstn then
  1366. p2:=tpointerconstnode(right).value;
  1367. case nodetype of
  1368. equaln:
  1369. result:=cordconstnode.create(ord(p1=p2),bool8type,false);
  1370. unequaln:
  1371. result:=cordconstnode.create(ord(p1<>p2),bool8type,false);
  1372. gtn:
  1373. result:=cordconstnode.create(ord(p1>p2),bool8type,false);
  1374. ltn:
  1375. result:=cordconstnode.create(ord(p1<p2),bool8type,false);
  1376. gten:
  1377. result:=cordconstnode.create(ord(p1>=p2),bool8type,false);
  1378. lten:
  1379. result:=cordconstnode.create(ord(p1<=p2),bool8type,false);
  1380. else
  1381. Internalerror(2020100101);
  1382. end;
  1383. exit;
  1384. end;
  1385. { slow simplifications and/or more sophisticated transformations which might make debugging harder }
  1386. if cs_opt_level2 in current_settings.optimizerswitches then
  1387. begin
  1388. if nodetype in [addn,muln,subn] then
  1389. begin
  1390. { convert a+const1-const2 into a+const1+(-const2) so it is folded later on }
  1391. if (left.nodetype=addn) and
  1392. (nodetype=subn) and
  1393. (cs_opt_fastmath in current_settings.optimizerswitches) and (rt=realconstn) and (taddnode(left).right.nodetype=realconstn) and
  1394. (compare_defs(resultdef,ld,nothingn)=te_exact) then
  1395. begin
  1396. Result:=getcopy;
  1397. Result.nodetype:=addn;
  1398. taddnode(result).right:=cunaryminusnode.create(taddnode(result).right);
  1399. exit;
  1400. end;
  1401. { convert a-const1+const2 into a+(-const1)+const2 so it is folded later on }
  1402. if (left.nodetype=subn) and
  1403. (nodetype=addn) and
  1404. (cs_opt_fastmath in current_settings.optimizerswitches) and (rt=realconstn) and (taddnode(left).right.nodetype=realconstn) and
  1405. (compare_defs(resultdef,ld,nothingn)=te_exact) then
  1406. begin
  1407. Result:=getcopy;
  1408. taddnode(Result).left.nodetype:=addn;
  1409. taddnode(taddnode(Result).left).right:=cunaryminusnode.create(taddnode(taddnode(Result).left).right);
  1410. exit;
  1411. end;
  1412. { try to fold
  1413. op
  1414. / \
  1415. op const1
  1416. / \
  1417. val const2
  1418. while operating on strings or reals
  1419. }
  1420. if (left.nodetype=nodetype) and
  1421. (((nodetype=addn) and ((rt=stringconstn) or is_constcharnode(right)) and ((taddnode(left).right.nodetype=stringconstn) or is_constcharnode(taddnode(left).right))) or
  1422. ((nodetype in [addn,muln,subn]) and (cs_opt_fastmath in current_settings.optimizerswitches) and (rt=realconstn) and (taddnode(left).right.nodetype=realconstn))
  1423. ) and
  1424. (compare_defs(resultdef,ld,nothingn)=te_exact) then
  1425. begin
  1426. { SwapRightWithLeftLeft moves the nodes around in way that we need to insert a minus
  1427. on left.right: a-b-c becomes b-c-a so we
  1428. need
  1429. 1) insert a minus bevor b
  1430. 2) make the current node an add node, see below
  1431. }
  1432. if nodetype=subn then
  1433. begin
  1434. taddnode(left).right:=cunaryminusnode.create(taddnode(left).right);
  1435. do_typecheckpass(taddnode(left).right);
  1436. end;
  1437. Result:=SwapRightWithLeftLeft;
  1438. if nodetype=subn then
  1439. begin
  1440. Result.nodetype:=addn;
  1441. do_typecheckpass(Result);
  1442. end;
  1443. exit;
  1444. end;
  1445. { try to fold
  1446. op
  1447. / \
  1448. const1 op
  1449. / \
  1450. const2 val
  1451. while operating on strings or reals
  1452. }
  1453. if (right.nodetype=nodetype) and
  1454. (((nodetype=addn) and ((lt=stringconstn) or is_constcharnode(left)) and ((taddnode(right).left.nodetype=stringconstn) or is_constcharnode(taddnode(right).left))) or
  1455. ((nodetype in [addn,muln]) and (cs_opt_fastmath in current_settings.optimizerswitches) and (lt=realconstn) and (taddnode(right).left.nodetype=realconstn))
  1456. ) and
  1457. (compare_defs(resultdef,rd,nothingn)=te_exact) then
  1458. begin
  1459. Result:=SwapLeftWithRightRight;
  1460. exit;
  1461. end;
  1462. {
  1463. reorder string expressions with parentheses:
  1464. (s1+(s2+(s3+s4...))) into s1+s2+s3+s4 ...
  1465. so fpc_*_concat_multi can be used efficiently
  1466. }
  1467. hp:=self;
  1468. while (hp.right.nodetype=hp.nodetype) and (hp.resultdef.typ=stringdef) and
  1469. (compare_defs(hp.resultdef,hp.right.resultdef,nothingn)=te_exact) and
  1470. (compare_defs(hp.resultdef,hp.left.resultdef,nothingn)=te_exact) and
  1471. (compare_defs(hp.resultdef,taddnode(hp.right).left.resultdef,nothingn)=te_exact) and
  1472. (compare_defs(hp.resultdef,taddnode(hp.right).right.resultdef,nothingn)=te_exact) do
  1473. begin
  1474. t:=hp.left;
  1475. hp.left:=hp.right;
  1476. hp.right:=taddnode(hp.left).right;
  1477. taddnode(hp.left).right:=taddnode(hp.left).left;
  1478. taddnode(hp.left).left:=t;
  1479. hp:=taddnode(hp.left);
  1480. end;
  1481. end;
  1482. { the comparison is might be expensive and the nodes are usually only
  1483. equal if some previous optimizations were done so don't check
  1484. this simplification always
  1485. }
  1486. if is_boolean(ld) and is_boolean(rd) then
  1487. begin
  1488. { transform unsigned comparisons of (v>=x) and (v<=y)
  1489. into (v-x)<=(y-x)
  1490. }
  1491. if (nodetype in [andn,orn]) and
  1492. (left.nodetype in [ltn,lten,gtn,gten]) and
  1493. (right.nodetype in [ltn,lten,gtn,gten]) and
  1494. (not might_have_sideeffects(left)) and
  1495. (not might_have_sideeffects(right)) and
  1496. is_range_test(taddnode(left),taddnode(right),vl,cl,cr) and
  1497. { avoid optimization being applied to (<string. var > charconst1) and (<string. var < charconst2) }
  1498. (vl.resultdef.typ in [orddef,enumdef]) then
  1499. begin
  1500. hdef:=get_unsigned_inttype(vl.resultdef);
  1501. vl:=ctypeconvnode.create_internal(vl.getcopy,hdef);
  1502. { If the condition is of the inverted form (v<x) or (v>y),
  1503. we have to invert the conditional result as well, since
  1504. the above nodes return True for if v is within the range
  1505. (we're merging "not ((v-x)<=(y-x))" into "(v-x)>(y-x)") }
  1506. if (nodetype=orn) then
  1507. nt:=gtn
  1508. else
  1509. nt:=lten;
  1510. result:=caddnode.create_internal(nt,
  1511. ctypeconvnode.create_internal(caddnode.create_internal(subn,vl,cordconstnode.create(cl,hdef,false)),hdef),
  1512. cordconstnode.create(cr-cl,hdef,false));
  1513. exit;
  1514. end;
  1515. {
  1516. (v1=const1) and (v2=const2)
  1517. can be converted into
  1518. ((v1 xor const1) or (v2 xor const2))=0
  1519. }
  1520. if (nodetype=andn) and
  1521. (left.nodetype=equaln) and
  1522. (right.nodetype=equaln) and
  1523. (not might_have_sideeffects(left)) and
  1524. (not might_have_sideeffects(right,[mhs_exceptions])) and
  1525. (is_constintnode(taddnode(left).left) or is_constintnode(taddnode(left).right) or
  1526. is_constpointernode(taddnode(left).left) or is_constpointernode(taddnode(left).right) or
  1527. is_constcharnode(taddnode(left).left) or is_constcharnode(taddnode(left).right)) and
  1528. (is_constintnode(taddnode(right).left) or is_constintnode(taddnode(right).right) or
  1529. is_constpointernode(taddnode(right).left) or is_constpointernode(taddnode(right).right) or
  1530. is_constcharnode(taddnode(right).left) or is_constcharnode(taddnode(right).right)) then
  1531. begin
  1532. if is_constnode(taddnode(left).left) then
  1533. begin
  1534. v1p:=@taddnode(left).right;
  1535. c1p:=@taddnode(left).left;
  1536. end
  1537. else
  1538. begin
  1539. v1p:=@taddnode(left).left;
  1540. c1p:=@taddnode(left).right;
  1541. end;
  1542. if is_constnode(taddnode(right).left) then
  1543. begin
  1544. v2p:=@taddnode(right).right;
  1545. c2p:=@taddnode(right).left;
  1546. end
  1547. else
  1548. begin
  1549. v2p:=@taddnode(right).left;
  1550. c2p:=@taddnode(right).right;
  1551. end;
  1552. if v1p^.resultdef.size=v2p^.resultdef.size then
  1553. begin
  1554. case v1p^.resultdef.size of
  1555. 1:
  1556. inttype:=u8inttype;
  1557. 2:
  1558. inttype:=u16inttype;
  1559. 4:
  1560. inttype:=u32inttype;
  1561. 8:
  1562. inttype:=u64inttype;
  1563. else
  1564. Internalerror(2020060101);
  1565. end;
  1566. result:=caddnode.create_internal(equaln,
  1567. caddnode.create_internal(orn,
  1568. caddnode.create_internal(xorn,ctypeconvnode.create_internal(v1p^.getcopy,inttype),
  1569. ctypeconvnode.create_internal(c1p^.getcopy,inttype)),
  1570. caddnode.create_internal(xorn,ctypeconvnode.create_internal(v2p^.getcopy,inttype),
  1571. ctypeconvnode.create_internal(c2p^.getcopy,inttype))
  1572. ),
  1573. cordconstnode.create(0,inttype,false));
  1574. end;
  1575. end;
  1576. { even when short circuit boolean evaluation is active, this
  1577. optimization cannot be performed in case the node has
  1578. side effects, because this can change the result (e.g., in an
  1579. or-node that calls the same function twice and first returns
  1580. false and then true because of a global state change }
  1581. if left.isequal(right) and not might_have_sideeffects(left) then
  1582. begin
  1583. case nodetype of
  1584. andn,orn:
  1585. begin
  1586. result:=PruneKeepLeft();
  1587. exit;
  1588. end;
  1589. {
  1590. xorn:
  1591. begin
  1592. result:=cordconstnode.create(0,resultdef,true);
  1593. exit;
  1594. end;
  1595. }
  1596. else
  1597. ;
  1598. end;
  1599. end
  1600. { short to full boolean evalution possible and useful? }
  1601. else if not(might_have_sideeffects(right,[mhs_exceptions])) and doshortbooleval(self) then
  1602. begin
  1603. case nodetype of
  1604. andn,orn:
  1605. begin
  1606. { full boolean evaluation is only useful if the nodes are not too complex and if no jumps must be converted,
  1607. further, we need to know the expectloc }
  1608. if (node_complexity(right)<=2) and
  1609. not(left.expectloc in [LOC_JUMP,LOC_INVALID]) and not(right.expectloc in [LOC_JUMP,LOC_INVALID]) then
  1610. begin
  1611. { we need to copy the whole tree to force another pass_1 }
  1612. include(localswitches,cs_full_boolean_eval);
  1613. exclude(addnodeflags,anf_short_bool);
  1614. result:=getcopy;
  1615. exit;
  1616. end;
  1617. end;
  1618. else
  1619. ;
  1620. end;
  1621. end
  1622. end;
  1623. if is_integer(ld) and is_integer(rd) then
  1624. begin
  1625. if (cs_opt_level3 in current_settings.optimizerswitches) and
  1626. left.isequal(right) and not might_have_sideeffects(left) then
  1627. begin
  1628. case nodetype of
  1629. andn,orn:
  1630. begin
  1631. result:=PruneKeepLeft();
  1632. exit;
  1633. end;
  1634. xorn,
  1635. subn,
  1636. unequaln,
  1637. ltn,
  1638. gtn:
  1639. begin
  1640. result:=cordconstnode.create(0,resultdef,true);
  1641. exit;
  1642. end;
  1643. equaln,
  1644. lten,
  1645. gten:
  1646. begin
  1647. result:=cordconstnode.create(1,resultdef,true);
  1648. exit;
  1649. end;
  1650. else
  1651. ;
  1652. end;
  1653. end
  1654. {$ifndef jvm}
  1655. else if TryHandleLengthZero(left,right,nodetype,Result) then
  1656. exit
  1657. {$endif jvm}
  1658. ;
  1659. end;
  1660. {
  1661. compile x < length(arr) as x <= high(arr)
  1662. compile x >= length(arr) as x > high(arr)
  1663. tested by tests/webtbs/tw40292.pp
  1664. }
  1665. if (nodetype in [ltn,gten]) and
  1666. (right.nodetype=inlinen) and (tinlinenode(right).inlinenumber=in_length_x) and
  1667. ((is_dynamic_array(tinlinenode(right).left.resultdef)) or
  1668. (is_open_array(tinlinenode(right).left.resultdef))
  1669. ) then
  1670. begin
  1671. case nodetype of
  1672. ltn:
  1673. result:=caddnode.create(lten,left,cinlinenode.create(in_high_x,false,tinlinenode(right).left));
  1674. gten:
  1675. result:=caddnode.create(gtn,left,cinlinenode.create(in_high_x,false,tinlinenode(right).left));
  1676. else
  1677. Internalerror(2024041701);
  1678. end;
  1679. left:=nil;
  1680. tinlinenode(right).left:=nil;
  1681. exit;
  1682. end;
  1683. {
  1684. compile length(arr) > x as high(arr) >= x
  1685. compile length(arr) <= x as high(arr) < x
  1686. tested by tests/webtbs/tw40292.pp
  1687. }
  1688. if (nodetype in [lten,gtn]) and
  1689. (left.nodetype=inlinen) and (tinlinenode(left).inlinenumber=in_length_x) and
  1690. ((is_dynamic_array(tinlinenode(left).left.resultdef)) or
  1691. (is_open_array(tinlinenode(left).left.resultdef))
  1692. ) then
  1693. begin
  1694. case nodetype of
  1695. gtn:
  1696. result:=caddnode.create(gten,cinlinenode.create(in_high_x,false,tinlinenode(left).left),right);
  1697. lten:
  1698. result:=caddnode.create(ltn,cinlinenode.create(in_high_x,false,tinlinenode(left).left),right);
  1699. else
  1700. Internalerror(2024041701);
  1701. end;
  1702. right:=nil;
  1703. tinlinenode(left).left:=nil;
  1704. exit;
  1705. end;
  1706. { using sqr(x) for reals instead of x*x might reduces register pressure and/or
  1707. memory accesses while sqr(<real>) has no drawback }
  1708. if
  1709. {$ifdef cpufpemu}
  1710. (current_settings.fputype<>fpu_soft) and
  1711. not(cs_fp_emulation in current_settings.moduleswitches) and
  1712. {$endif cpufpemu}
  1713. {$ifdef xtensa}
  1714. (FPUXTENSA_DOUBLE in fpu_capabilities[current_settings.fputype]) and
  1715. {$endif xtensa}
  1716. (nodetype=muln) and
  1717. is_real(ld) and is_real(rd) and
  1718. left.isequal(right) and
  1719. not(might_have_sideeffects(left)) then
  1720. begin
  1721. result:=cinlinenode.create(in_sqr_real,false,PruneKeepLeft());
  1722. inserttypeconv(result,resultdef);
  1723. exit;
  1724. end;
  1725. {$ifdef cpurox}
  1726. { optimize (i shl x) or (i shr (bitsizeof(i)-x)) into rol(x,i) (and different flavours with shl/shr swapped etc.) }
  1727. if (nodetype=orn)
  1728. {$ifdef m68k}
  1729. and (CPUM68K_HAS_ROLROR in cpu_capabilities[current_settings.cputype])
  1730. {$endif m68k}
  1731. {$ifdef riscv}
  1732. and ([CPURV_HAS_ZBB,CPURV_HAS_ZBKB]*cpu_capabilities[init_settings.cputype]<>[])
  1733. {$endif riscv}
  1734. {$ifndef cpu64bitalu}
  1735. and (ld.typ=orddef) and
  1736. not(torddef(ld).ordtype in [s64bit,u64bit,scurrency])
  1737. {$endif cpu64bitalu}
  1738. then
  1739. begin
  1740. if (left.nodetype=shrn) and (right.nodetype=shln) and
  1741. is_constintnode(tshlshrnode(left).right) and
  1742. is_constintnode(tshlshrnode(right).right) and
  1743. (tordconstnode(tshlshrnode(right).right).value>0) and
  1744. (tordconstnode(tshlshrnode(left).right).value>0) and
  1745. tshlshrnode(left).left.isequal(tshlshrnode(right).left) and
  1746. not(might_have_sideeffects(tshlshrnode(left).left)) then
  1747. begin
  1748. if (tordconstnode(tshlshrnode(left).right).value=
  1749. tshlshrnode(left).left.resultdef.size*8-tordconstnode(tshlshrnode(right).right).value) then
  1750. begin
  1751. result:=cinlinenode.create(in_ror_x_y,false,
  1752. ccallparanode.create(tshlshrnode(left).PruneKeepRight(),
  1753. ccallparanode.create(tshlshrnode(left).PruneKeepLeft(),nil)));
  1754. exit;
  1755. end
  1756. else if (tordconstnode(tshlshrnode(right).right).value=
  1757. tshlshrnode(left).left.resultdef.size*8-tordconstnode(tshlshrnode(left).right).value) then
  1758. begin
  1759. result:=cinlinenode.create(in_rol_x_y,false,
  1760. ccallparanode.create(tshlshrnode(right).PruneKeepRight(),
  1761. ccallparanode.create(tshlshrnode(left).PruneKeepLeft(),nil)));
  1762. exit;
  1763. end;
  1764. end;
  1765. if (left.nodetype=shln) and (right.nodetype=shrn) and
  1766. is_constintnode(tshlshrnode(left).right) and
  1767. is_constintnode(tshlshrnode(right).right) and
  1768. (tordconstnode(tshlshrnode(right).right).value>0) and
  1769. (tordconstnode(tshlshrnode(left).right).value>0) and
  1770. tshlshrnode(left).left.isequal(tshlshrnode(right).left) and
  1771. not(might_have_sideeffects(tshlshrnode(left).left)) then
  1772. begin
  1773. if (tordconstnode(tshlshrnode(left).right).value=
  1774. tshlshrnode(left).left.resultdef.size*8-tordconstnode(tshlshrnode(right).right).value)
  1775. then
  1776. begin
  1777. result:=cinlinenode.create(in_rol_x_y,false,
  1778. ccallparanode.create(tshlshrnode(left).PruneKeepRight(),
  1779. ccallparanode.create(tshlshrnode(left).PruneKeepLeft(),nil)));
  1780. exit;
  1781. end
  1782. else if (tordconstnode(tshlshrnode(right).right).value=
  1783. tshlshrnode(left).left.resultdef.size*8-tordconstnode(tshlshrnode(left).right).value)
  1784. then
  1785. begin
  1786. result:=cinlinenode.create(in_ror_x_y,false,
  1787. ccallparanode.create(tshlshrnode(right).PruneKeepRight(),
  1788. ccallparanode.create(tshlshrnode(left).PruneKeepLeft(),nil)));
  1789. exit;
  1790. end;
  1791. end;
  1792. end;
  1793. {$endif cpurox}
  1794. { optimize
  1795. (a and b) or (c and not(b))
  1796. into
  1797. c xor ((c xor a) and b)
  1798. }
  1799. if (nodetype=orn) and
  1800. (ld.typ=orddef) and
  1801. (left.nodetype=andn) and
  1802. (right.nodetype=andn) and
  1803. (not(is_boolean(resultdef)) or not(might_have_sideeffects(self,[mhs_exceptions])) or not(doshortbooleval(self))) and
  1804. { this test is not needed but it speeds up the test and allows to bail out early }
  1805. ((taddnode(left).left.nodetype=notn) or (taddnode(left).right.nodetype=notn) or
  1806. (taddnode(right).left.nodetype=notn) or (taddnode(right).right.nodetype=notn)
  1807. ) and
  1808. not(might_have_sideeffects(self)) then
  1809. begin
  1810. if MatchAndTransformNodesCommutative(taddnode(left).left,taddnode(left).right,taddnode(right).left,taddnode(right).right,
  1811. @IsAndOrAndNot,@TransformAndOrAndNot,Result) then
  1812. exit;
  1813. end;
  1814. { optimize tests for a single bit:
  1815. (a and one_bit_mask_const) = <> one_bit_mask_const
  1816. into
  1817. (a and one_bit_mask_const) <> = 0
  1818. }
  1819. if (nodetype in [equaln,unequaln]) then
  1820. begin
  1821. if (lt=andn) and (rt=ordconstn) then
  1822. begin
  1823. t:=left;
  1824. cr:=tordconstnode(right).value;
  1825. end
  1826. else
  1827. if (rt=andn) and (lt=ordconstn) then
  1828. begin
  1829. t:=right;
  1830. cr:=tordconstnode(left).value;
  1831. end
  1832. else
  1833. begin
  1834. t:=nil;
  1835. cr:=0;
  1836. end;
  1837. { using cr.uvalue is fine as we are interested only in the bit pattern }
  1838. if (t<>nil) and (PopCnt(cr.uvalue) = 1) then
  1839. begin
  1840. if is_constintnode(taddnode(t).left) then
  1841. vl:=taddnode(t).left
  1842. else
  1843. if is_constintnode(taddnode(t).right) then
  1844. vl:=taddnode(t).right
  1845. else
  1846. vl:=nil;
  1847. if (vl<>nil) and (tordconstnode(vl).value=cr) then
  1848. begin
  1849. if nodetype=equaln then
  1850. nt:=unequaln
  1851. else
  1852. nt:=equaln;
  1853. result:=caddnode.create(nt,t,cordconstnode.create(0,vl.resultdef,false));
  1854. Include(transientflags,tnf_do_not_execute);
  1855. if t=left then
  1856. left:=nil
  1857. else
  1858. right:=nil;
  1859. exit;
  1860. end;
  1861. end;
  1862. end;
  1863. end;
  1864. end;
  1865. function taddnode.dogetcopy: tnode;
  1866. var
  1867. n: taddnode;
  1868. begin
  1869. n:=taddnode(inherited dogetcopy);
  1870. n.addnodeflags:=addnodeflags;
  1871. n.resultrealdef:=resultrealdef;
  1872. result:=n;
  1873. end;
  1874. function taddnode.docompare(p: tnode): boolean;
  1875. begin
  1876. result:=
  1877. inherited docompare(p) and
  1878. equal_defs(taddnode(p).resultrealdef,resultrealdef);
  1879. end;
  1880. procedure taddnode.printnodedata(var t: text);
  1881. var
  1882. i: TAddNodeFlag;
  1883. first: Boolean;
  1884. begin
  1885. write(t,printnodeindention,'addnodeflags = [');
  1886. first:=true;
  1887. for i:=low(TAddNodeFlag) to high(TAddNodeFlag) do
  1888. if i in addnodeflags then
  1889. begin
  1890. if not(first) then
  1891. write(t,',')
  1892. else
  1893. first:=false;
  1894. write(t, i);
  1895. end;
  1896. writeln(t,']');
  1897. inherited printnodedata(t);
  1898. end;
  1899. function taddnode.pass_typecheck:tnode;
  1900. begin
  1901. { This function is small to keep the stack small for recursive of
  1902. large + operations }
  1903. typecheckpass(left);
  1904. typecheckpass(right);
  1905. result:=pass_typecheck_internal;
  1906. end;
  1907. function taddnode.pass_typecheck_internal:tnode;
  1908. var
  1909. hp : tnode;
  1910. rd,ld,nd : tdef;
  1911. hsym : tfieldvarsym;
  1912. llow,lhigh,
  1913. rlow,rhigh : tconstexprint;
  1914. strtype : tstringtype;
  1915. res,
  1916. b : boolean;
  1917. lt,rt : tnodetype;
  1918. ot : tnodetype;
  1919. {$ifdef state_tracking}
  1920. factval : Tnode;
  1921. change : boolean;
  1922. {$endif}
  1923. function maybe_cast_ordconst(var n: tnode; adef: tdef): boolean;
  1924. begin
  1925. result:=(tordconstnode(n).value>=torddef(adef).low) and
  1926. (tordconstnode(n).value<=torddef(adef).high);
  1927. if result then
  1928. inserttypeconv(n,adef);
  1929. end;
  1930. function maybe_convert_to_insert:tnode;
  1931. function element_count(arrconstr: tarrayconstructornode):asizeint;
  1932. begin
  1933. result:=0;
  1934. while assigned(arrconstr) do
  1935. begin
  1936. if arrconstr.nodetype=arrayconstructorrangen then
  1937. internalerror(2018052501);
  1938. inc(result);
  1939. arrconstr:=tarrayconstructornode(tarrayconstructornode(arrconstr).right);
  1940. end;
  1941. end;
  1942. var
  1943. elem : tnode;
  1944. para : tcallparanode;
  1945. isarrconstrl,
  1946. isarrconstrr : boolean;
  1947. index : asizeint;
  1948. begin
  1949. result:=nil;
  1950. isarrconstrl:=left.nodetype=arrayconstructorn;
  1951. isarrconstrr:=right.nodetype=arrayconstructorn;
  1952. if not assigned(aktassignmentnode) or
  1953. (aktassignmentnode.right<>self) or
  1954. not(
  1955. isarrconstrl or
  1956. isarrconstrr
  1957. ) or
  1958. not(
  1959. left.isequal(aktassignmentnode.left) or
  1960. right.isequal(aktassignmentnode.left)
  1961. ) or
  1962. not valid_for_var(aktassignmentnode.left,false) or
  1963. (isarrconstrl and (element_count(tarrayconstructornode(left))>1)) or
  1964. (isarrconstrr and (element_count(tarrayconstructornode(right))>1)) then
  1965. exit;
  1966. if isarrconstrl then
  1967. begin
  1968. index:=0;
  1969. elem:=tarrayconstructornode(left).left;
  1970. tarrayconstructornode(left).left:=nil;
  1971. end
  1972. else
  1973. begin
  1974. index:=high(asizeint);
  1975. elem:=tarrayconstructornode(right).left;
  1976. tarrayconstructornode(right).left:=nil;
  1977. end;
  1978. { we use the fact that insert() caps the index to avoid a copy }
  1979. para:=ccallparanode.create(
  1980. cordconstnode.create(index,sizesinttype,false),
  1981. ccallparanode.create(
  1982. aktassignmentnode.left.getcopy,
  1983. ccallparanode.create(
  1984. elem,nil)));
  1985. result:=cinlinenode.create(in_insert_x_y_z,false,para);
  1986. include(aktassignmentnode.assignmentnodeflags,anf_assign_done_in_right);
  1987. end;
  1988. begin
  1989. result:=nil;
  1990. rlow:=0;
  1991. llow:=0;
  1992. rhigh:=0;
  1993. lhigh:=0;
  1994. { avoid any problems with type parameters later on }
  1995. if is_typeparam(left.resultdef) or is_typeparam(right.resultdef) then
  1996. begin
  1997. resultdef:=cundefinedtype;
  1998. exit;
  1999. end;
  2000. { both left and right need to be valid }
  2001. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2002. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2003. if codegenerror then
  2004. exit;
  2005. { tp procvar support. Omit for converted assigned() nodes }
  2006. if not (nf_load_procvar in flags) then
  2007. begin
  2008. maybe_call_procvar(left,true);
  2009. maybe_call_procvar(right,true);
  2010. end
  2011. else
  2012. if not (nodetype in [equaln,unequaln]) then
  2013. InternalError(2013091601);
  2014. { allow operator overloading }
  2015. hp:=self;
  2016. if is_array_constructor(left.resultdef) or is_array_constructor(right.resultdef) then
  2017. begin
  2018. { check whether there is a suitable operator for the array constructor
  2019. (but only if the "+" array operator isn't used), if not fall back to sets }
  2020. if (
  2021. (nodetype<>addn) or
  2022. not (m_array_operators in current_settings.modeswitches) or
  2023. (is_array_constructor(left.resultdef) and not is_dynamic_array(right.resultdef)) or
  2024. (not is_dynamic_array(left.resultdef) and is_array_constructor(right.resultdef))
  2025. ) and
  2026. not isbinaryoverloaded(hp,[ocf_check_only]) then
  2027. begin
  2028. if is_array_constructor(left.resultdef) then
  2029. begin
  2030. arrayconstructor_to_set(left);
  2031. typecheckpass(left);
  2032. end;
  2033. if is_array_constructor(right.resultdef) then
  2034. begin
  2035. arrayconstructor_to_set(right);
  2036. typecheckpass(right);
  2037. end;
  2038. end;
  2039. end;
  2040. if is_dynamic_array(left.resultdef) and is_dynamic_array(right.resultdef) and
  2041. (nodetype=addn) and
  2042. (m_array_operators in current_settings.modeswitches) and
  2043. isbinaryoverloaded(hp,[ocf_check_non_overloadable,ocf_check_only]) then
  2044. message3(parser_w_operator_overloaded_hidden_3,left.resultdef.typename,arraytokeninfo[_PLUS].str,right.resultdef.typename);
  2045. if isbinaryoverloaded(hp,[]) then
  2046. begin
  2047. result:=hp;
  2048. exit;
  2049. end;
  2050. { Stop checking when an error was found in the operator checking }
  2051. if codegenerror then
  2052. begin
  2053. result:=cerrornode.create;
  2054. exit;
  2055. end;
  2056. { Kylix allows enum+ordconstn in an enum type declaration, we need to do
  2057. the conversion here before the constant folding }
  2058. if (m_delphi in current_settings.modeswitches) and
  2059. (blocktype in [bt_type,bt_const_type,bt_var_type]) then
  2060. begin
  2061. if (left.resultdef.typ=enumdef) and
  2062. (right.resultdef.typ=orddef) then
  2063. begin
  2064. { insert explicit typecast to default signed int }
  2065. left:=ctypeconvnode.create_internal(left,sinttype);
  2066. typecheckpass(left);
  2067. end
  2068. else
  2069. if (left.resultdef.typ=orddef) and
  2070. (right.resultdef.typ=enumdef) then
  2071. begin
  2072. { insert explicit typecast to default signed int }
  2073. right:=ctypeconvnode.create_internal(right,sinttype);
  2074. typecheckpass(right);
  2075. end;
  2076. end;
  2077. { is one a real float, then both need to be floats, this
  2078. need to be done before the constant folding so constant
  2079. operation on a float and int are also handled }
  2080. {$ifdef x86}
  2081. { use extended as default real type only when the x87 fpu is used }
  2082. {$if defined(i386) or defined(i8086)}
  2083. if not(current_settings.fputype=fpu_x87) then
  2084. resultrealdef:=s64floattype
  2085. else
  2086. resultrealdef:=pbestrealtype^;
  2087. {$endif i386 or i8086}
  2088. {$ifdef x86_64}
  2089. { x86-64 has no x87 only mode, so use always double as default }
  2090. resultrealdef:=s64floattype;
  2091. {$endif x86_6}
  2092. {$else not x86}
  2093. resultrealdef:=pbestrealtype^;
  2094. {$endif not x86}
  2095. if (right.resultdef.typ=floatdef) or (left.resultdef.typ=floatdef) then
  2096. begin
  2097. { when both floattypes are already equal then use that
  2098. floattype for results }
  2099. if (right.resultdef.typ=floatdef) and
  2100. (left.resultdef.typ=floatdef) and
  2101. (tfloatdef(left.resultdef).floattype=tfloatdef(right.resultdef).floattype) and
  2102. not(tfloatdef(left.resultdef).floattype in [s64comp,s64currency]) then
  2103. begin
  2104. if cs_excessprecision in current_settings.localswitches then
  2105. begin
  2106. resultrealdef:=pbestrealtype^;
  2107. inserttypeconv(right,resultrealdef);
  2108. inserttypeconv(left,resultrealdef);
  2109. end
  2110. else
  2111. resultrealdef:=left.resultdef
  2112. end
  2113. { when there is a currency type then use currency, but
  2114. only when currency is defined as float }
  2115. else
  2116. if (is_currency(right.resultdef) or
  2117. is_currency(left.resultdef)) and
  2118. ((s64currencytype.typ = floatdef) or
  2119. (nodetype <> slashn)) then
  2120. begin
  2121. resultrealdef:=s64currencytype;
  2122. inserttypeconv(right,resultrealdef);
  2123. inserttypeconv(left,resultrealdef);
  2124. end
  2125. else
  2126. begin
  2127. resultrealdef:=getbestreal(left.resultdef,right.resultdef);
  2128. inserttypeconv(right,resultrealdef);
  2129. inserttypeconv(left,resultrealdef);
  2130. end;
  2131. end;
  2132. { If both operands are constant and there is a unicodestring
  2133. or unicodestring then convert everything to unicodestring }
  2134. if is_constnode(right) and is_constnode(left) and
  2135. (is_unicodestring(right.resultdef) or
  2136. is_unicodestring(left.resultdef)) then
  2137. begin
  2138. inserttypeconv(right,cunicodestringtype);
  2139. inserttypeconv(left,cunicodestringtype);
  2140. end;
  2141. { If both operands are constant and there is a widechar
  2142. or widestring then convert everything to widestring. This
  2143. allows constant folding like char+widechar }
  2144. if is_constnode(right) and is_constnode(left) and
  2145. (is_widestring(right.resultdef) or
  2146. is_widestring(left.resultdef) or
  2147. is_widechar(right.resultdef) or
  2148. is_widechar(left.resultdef)) then
  2149. begin
  2150. inserttypeconv(right,cwidestringtype);
  2151. inserttypeconv(left,cwidestringtype);
  2152. end;
  2153. { load easier access variables }
  2154. rd:=right.resultdef;
  2155. ld:=left.resultdef;
  2156. rt:=right.nodetype;
  2157. lt:=left.nodetype;
  2158. { 4 character constant strings are compatible with orddef }
  2159. { in macpas mode (become cardinals) }
  2160. if (m_mac in current_settings.modeswitches) and
  2161. { only allow for comparisons, additions etc are }
  2162. { normally program errors }
  2163. (nodetype in [ltn,lten,gtn,gten,unequaln,equaln]) and
  2164. (((lt=stringconstn) and
  2165. (tstringconstnode(left).len=4) and
  2166. (rd.typ=orddef)) or
  2167. ((rt=stringconstn) and
  2168. (tstringconstnode(right).len=4) and
  2169. (ld.typ=orddef))) then
  2170. begin
  2171. if (rt=stringconstn) then
  2172. begin
  2173. inserttypeconv(right,u32inttype);
  2174. rt:=right.nodetype;
  2175. rd:=right.resultdef;
  2176. end
  2177. else
  2178. begin
  2179. inserttypeconv(left,u32inttype);
  2180. lt:=left.nodetype;
  2181. ld:=left.resultdef;
  2182. end;
  2183. end;
  2184. { but an int/int gives real/real! }
  2185. if (nodetype=slashn) and not(is_vector(left.resultdef)) and not(is_vector(right.resultdef)) then
  2186. begin
  2187. if is_currency(left.resultdef) and
  2188. is_currency(right.resultdef) then
  2189. { In case of currency, converting to float means dividing by 10000 }
  2190. { However, since this is already a division, both divisions by }
  2191. { 10000 are eliminated when we divide the results -> we can skip }
  2192. { them. }
  2193. if s64currencytype.typ = floatdef then
  2194. begin
  2195. { there's no s64comptype or so, how do we avoid the type conversion?
  2196. left.resultdef := s64comptype;
  2197. right.resultdef := s64comptype; }
  2198. end
  2199. else
  2200. begin
  2201. left.resultdef := s64inttype;
  2202. right.resultdef := s64inttype;
  2203. end;
  2204. if current_settings.fputype=fpu_none then
  2205. begin
  2206. Message(parser_e_unsupported_real);
  2207. result:=cerrornode.create;
  2208. exit;
  2209. end
  2210. else
  2211. begin
  2212. inserttypeconv(right,resultrealdef);
  2213. inserttypeconv(left,resultrealdef);
  2214. end;
  2215. end
  2216. { if both are orddefs then check sub types }
  2217. else if (ld.typ=orddef) and (rd.typ=orddef) then
  2218. begin
  2219. { set for & and | operations in macpas mode: they only work on }
  2220. { booleans, and always short circuit evaluation }
  2221. if (anf_short_bool in addnodeflags) then
  2222. begin
  2223. if not is_boolean(ld) then
  2224. begin
  2225. inserttypeconv(left,pasbool1type);
  2226. ld := left.resultdef;
  2227. end;
  2228. if not is_boolean(rd) then
  2229. begin
  2230. inserttypeconv(right,pasbool1type);
  2231. rd := right.resultdef;
  2232. end;
  2233. end;
  2234. { 2 booleans? }
  2235. if (is_boolean(ld) and is_boolean(rd)) then
  2236. begin
  2237. case nodetype of
  2238. xorn,
  2239. andn,
  2240. orn:
  2241. begin
  2242. { in case of xor or 'and' with cbool: convert both to Pascal bool and then
  2243. perform the xor/and to prevent issues with "longbool(1) and/xor
  2244. longbool(2)" }
  2245. if (is_cbool(ld) or is_cbool(rd)) and
  2246. (nodetype in [xorn,andn]) then
  2247. begin
  2248. resultdef:=nil;
  2249. if is_cbool(ld) then
  2250. begin
  2251. left:=ctypeconvnode.create(left,pasbool8type);
  2252. ttypeconvnode(left).convtype:=tc_bool_2_bool;
  2253. firstpass(left);
  2254. if not is_cbool(rd) or
  2255. (ld.size>=rd.size) then
  2256. resultdef:=ld;
  2257. end;
  2258. if is_cbool(rd) then
  2259. begin
  2260. right:=ctypeconvnode.Create(right,pasbool8type);
  2261. ttypeconvnode(right).convtype:=tc_bool_2_bool;
  2262. firstpass(right);
  2263. if not assigned(resultdef) then
  2264. resultdef:=rd;
  2265. end;
  2266. result:=ctypeconvnode.create_explicit(caddnode.create(nodetype,left,right),resultdef);
  2267. ttypeconvnode(result).convtype:=tc_bool_2_bool;
  2268. left:=nil;
  2269. right:=nil;
  2270. exit;
  2271. end;
  2272. { Make sides equal to the largest boolean }
  2273. if (torddef(left.resultdef).size>torddef(right.resultdef).size) or
  2274. (is_cbool(left.resultdef) and not is_cbool(right.resultdef)) then
  2275. begin
  2276. right:=ctypeconvnode.create_internal(right,left.resultdef);
  2277. ttypeconvnode(right).convtype:=tc_bool_2_bool;
  2278. typecheckpass(right);
  2279. end
  2280. else if (torddef(left.resultdef).size<torddef(right.resultdef).size) or
  2281. (not is_cbool(left.resultdef) and is_cbool(right.resultdef)) then
  2282. begin
  2283. left:=ctypeconvnode.create_internal(left,right.resultdef);
  2284. ttypeconvnode(left).convtype:=tc_bool_2_bool;
  2285. typecheckpass(left);
  2286. end;
  2287. end;
  2288. ltn,
  2289. lten,
  2290. gtn,
  2291. gten:
  2292. begin
  2293. { convert both to pasbool to perform the comparison (so
  2294. that longbool(4) = longbool(2), since both represent
  2295. "true" }
  2296. inserttypeconv(left,pasbool1type);
  2297. inserttypeconv(right,pasbool1type);
  2298. end;
  2299. unequaln,
  2300. equaln:
  2301. begin
  2302. { Remove any compares with constants }
  2303. if (left.nodetype=ordconstn) then
  2304. begin
  2305. hp:=right;
  2306. b:=(tordconstnode(left).value<>0);
  2307. ot:=nodetype;
  2308. right:=nil;
  2309. if (not(b) and (ot=equaln)) or
  2310. (b and (ot=unequaln)) then
  2311. begin
  2312. hp:=cnotnode.create(hp);
  2313. end;
  2314. result:=hp;
  2315. exit;
  2316. end;
  2317. if (right.nodetype=ordconstn) then
  2318. begin
  2319. hp:=left;
  2320. b:=(tordconstnode(right).value<>0);
  2321. ot:=nodetype;
  2322. left:=nil;
  2323. if (not(b) and (ot=equaln)) or
  2324. (b and (ot=unequaln)) then
  2325. begin
  2326. hp:=cnotnode.create(hp);
  2327. end;
  2328. result:=hp;
  2329. exit;
  2330. end;
  2331. { Delphi-compatibility: convert both to pasbool to
  2332. perform the equality comparison }
  2333. inserttypeconv(left,pasbool1type);
  2334. inserttypeconv(right,pasbool1type);
  2335. end;
  2336. else
  2337. begin
  2338. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2339. result:=cnothingnode.create;
  2340. exit;
  2341. end;
  2342. end;
  2343. end
  2344. { Both are chars? }
  2345. else if is_char(rd) and is_char(ld) then
  2346. begin
  2347. if nodetype=addn then
  2348. begin
  2349. resultdef:=cshortstringtype;
  2350. if not(is_constcharnode(left) and is_constcharnode(right)) then
  2351. begin
  2352. inserttypeconv(left,cshortstringtype);
  2353. {$ifdef addstringopt}
  2354. hp := genaddsstringcharoptnode(self);
  2355. result := hp;
  2356. exit;
  2357. {$endif addstringopt}
  2358. end
  2359. end
  2360. else if not(nodetype in [ltn,lten,gtn,gten,unequaln,equaln]) then
  2361. begin
  2362. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2363. result:=cnothingnode.create;
  2364. exit;
  2365. end;
  2366. end
  2367. { There is a widechar? }
  2368. else if is_widechar(rd) or is_widechar(ld) then
  2369. begin
  2370. { widechar+widechar gives unicodestring }
  2371. if nodetype=addn then
  2372. begin
  2373. inserttypeconv(left,cunicodestringtype);
  2374. if (torddef(rd).ordtype<>uwidechar) then
  2375. inserttypeconv(right,cwidechartype);
  2376. resultdef:=cunicodestringtype;
  2377. end
  2378. else
  2379. begin
  2380. if (torddef(ld).ordtype<>uwidechar) then
  2381. inserttypeconv(left,cwidechartype);
  2382. if (torddef(rd).ordtype<>uwidechar) then
  2383. inserttypeconv(right,cwidechartype);
  2384. end;
  2385. end
  2386. { is there a currency type ? }
  2387. else if ((torddef(rd).ordtype=scurrency) or (torddef(ld).ordtype=scurrency)) then
  2388. begin
  2389. if (torddef(ld).ordtype<>scurrency) then
  2390. inserttypeconv(left,s64currencytype);
  2391. if (torddef(rd).ordtype<>scurrency) then
  2392. inserttypeconv(right,s64currencytype);
  2393. end
  2394. { leave some constant integer expressions alone in case the
  2395. resultdef of the integer types doesn't influence the outcome,
  2396. because the forced type conversions below can otherwise result
  2397. in unexpected results (such as high(qword)<high(int64) returning
  2398. true because high(qword) gets converted to int64) }
  2399. else if is_integer(ld) and is_integer(rd) and
  2400. (lt=ordconstn) and (rt=ordconstn) and
  2401. (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) then
  2402. begin
  2403. end
  2404. { "and" does't care about the sign of integers }
  2405. { "xor", "or" and compares don't need extension to native int }
  2406. { size either as long as both values are signed or unsigned }
  2407. { "xor" and "or" also don't care about the sign if the values }
  2408. { occupy an entire register }
  2409. { don't do it if either type is 64 bit (except for "and"), }
  2410. { since in that case we can't safely find a "common" type }
  2411. else if is_integer(ld) and is_integer(rd) and
  2412. ((nodetype=andn) or
  2413. ((nodetype in [orn,xorn,equaln,unequaln,gtn,gten,ltn,lten]) and
  2414. not is_64bitint(ld) and not is_64bitint(rd) and
  2415. (is_signed(ld)=is_signed(rd)))) then
  2416. begin
  2417. { Delphi-compatible: prefer unsigned type for "and", when the
  2418. unsigned type is bigger than the signed one, and also bigger
  2419. than min(native_int, 32-bit) }
  2420. if (is_oversizedint(rd) or is_nativeint(rd) or is_32bitint(rd)) and
  2421. (rd.size>=ld.size) and
  2422. not is_signed(rd) and is_signed(ld) then
  2423. inserttypeconv_internal(left,rd)
  2424. else if (is_oversizedint(ld) or is_nativeint(ld) or is_32bitint(ld)) and
  2425. (ld.size>=rd.size) and
  2426. not is_signed(ld) and is_signed(rd) then
  2427. inserttypeconv_internal(right,ld)
  2428. else
  2429. begin
  2430. { not to left right.resultdef, because that may
  2431. cause a range error if left and right's def don't
  2432. completely overlap }
  2433. nd:=get_common_intdef(torddef(ld),torddef(rd),true);
  2434. inserttypeconv(left,nd);
  2435. inserttypeconv(right,nd);
  2436. end;
  2437. end
  2438. { don't extend (sign-mismatched) comparisons if either side is a constant
  2439. whose value is within range of opposite side }
  2440. else if is_integer(ld) and is_integer(rd) and
  2441. (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  2442. (is_signed(ld)<>is_signed(rd)) and
  2443. (
  2444. ((lt=ordconstn) and maybe_cast_ordconst(left,rd)) or
  2445. ((rt=ordconstn) and maybe_cast_ordconst(right,ld))
  2446. ) then
  2447. begin
  2448. { done here }
  2449. end
  2450. { is there a signed 64 bit type ? }
  2451. else if ((torddef(rd).ordtype=s64bit) or (torddef(ld).ordtype=s64bit)) then
  2452. begin
  2453. if (torddef(ld).ordtype<>s64bit) then
  2454. inserttypeconv(left,s64inttype);
  2455. if (torddef(rd).ordtype<>s64bit) then
  2456. inserttypeconv(right,s64inttype);
  2457. end
  2458. { is there a unsigned 64 bit type ? }
  2459. else if ((torddef(rd).ordtype=u64bit) or (torddef(ld).ordtype=u64bit)) then
  2460. begin
  2461. if (torddef(ld).ordtype<>u64bit) then
  2462. inserttypeconv(left,u64inttype);
  2463. if (torddef(rd).ordtype<>u64bit) then
  2464. inserttypeconv(right,u64inttype);
  2465. end
  2466. { is there a larger int? }
  2467. else if is_oversizedint(rd) or is_oversizedint(ld) then
  2468. begin
  2469. nd:=get_common_intdef(torddef(ld),torddef(rd),false);
  2470. inserttypeconv(right,nd);
  2471. inserttypeconv(left,nd);
  2472. end
  2473. { is there a native unsigned int? }
  2474. else if is_nativeuint(rd) or is_nativeuint(ld) then
  2475. begin
  2476. { convert positive constants to uinttype }
  2477. if (not is_nativeuint(ld)) and
  2478. is_constintnode(left) and
  2479. (tordconstnode(left).value >= 0) then
  2480. inserttypeconv(left,uinttype);
  2481. if (not is_nativeuint(rd)) and
  2482. is_constintnode(right) and
  2483. (tordconstnode(right).value >= 0) then
  2484. inserttypeconv(right,uinttype);
  2485. { when one of the operand is signed or the operation is subn then perform
  2486. the operation in a larger signed type, can't use rd/ld here because there
  2487. could be already typeconvs inserted.
  2488. This is compatible with the code below for other unsigned types (PFV) }
  2489. if is_signed(left.resultdef) or
  2490. is_signed(right.resultdef) or
  2491. ((nodetype=subn)
  2492. {$if defined(cpu8bitalu) or defined(cpu16bitalu)}
  2493. and not (m_tp7 in current_settings.modeswitches)
  2494. {$endif}
  2495. ) then
  2496. begin
  2497. if nodetype<>subn then
  2498. CGMessage(type_h_mixed_signed_unsigned);
  2499. { mark as internal in case added for a subn, so }
  2500. { ttypeconvnode.simplify can remove the larger }
  2501. { typecast again if semantically correct. Even }
  2502. { if we could detect that here already, we }
  2503. { mustn't do it here because that would change }
  2504. { overload choosing behaviour etc. The code in }
  2505. { ncnv.pas is run after that is already decided }
  2506. if (not is_signed(left.resultdef) and
  2507. not is_signed(right.resultdef)) or
  2508. (nodetype in [orn,xorn]) then
  2509. include(flags,nf_internal);
  2510. { get next larger signed int type }
  2511. nd:=get_common_intdef(torddef(sinttype),torddef(uinttype),false);
  2512. inserttypeconv(left,nd);
  2513. inserttypeconv(right,nd);
  2514. end
  2515. else
  2516. begin
  2517. if not is_nativeuint(left.resultdef) then
  2518. inserttypeconv(left,uinttype);
  2519. if not is_nativeuint(right.resultdef) then
  2520. inserttypeconv(right,uinttype);
  2521. end;
  2522. end
  2523. { generic ord conversion is sinttype }
  2524. else
  2525. begin
  2526. { When there is a signed type or there is a minus operation
  2527. or in TP mode for 16-bit CPUs
  2528. we convert to signed int. Otherwise (both are unsigned) we keep
  2529. the result also unsigned. This is compatible with Delphi (PFV) }
  2530. if is_signed(ld) or
  2531. is_signed(rd) or
  2532. (([m_iso,m_extpas]*current_settings.modeswitches)<>[]) or
  2533. {$if defined(cpu16bitalu)}
  2534. (m_tp7 in current_settings.modeswitches) or
  2535. {$endif}
  2536. (nodetype=subn) then
  2537. begin
  2538. inserttypeconv(right,sinttype);
  2539. inserttypeconv(left,sinttype);
  2540. end
  2541. else
  2542. begin
  2543. inserttypeconv(right,uinttype);
  2544. inserttypeconv(left,uinttype);
  2545. end;
  2546. end;
  2547. end
  2548. { if both are floatdefs, conversion is already done before constant folding }
  2549. else if (ld.typ=floatdef) then
  2550. begin
  2551. if not(nodetype in [addn,subn,muln,slashn,equaln,unequaln,ltn,lten,gtn,gten]) then
  2552. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2553. end
  2554. { left side a setdef, must be before string processing,
  2555. else array constructor can be seen as array of char (PFV) }
  2556. else if (ld.typ=setdef) then
  2557. begin
  2558. if not(nodetype in [addn,subn,symdifn,muln,equaln,unequaln,lten,gten]) then
  2559. CGMessage(type_e_set_operation_unknown);
  2560. { right must either be a set or a set element }
  2561. if (rd.typ<>setdef) and
  2562. (rt<>setelementn) then
  2563. CGMessage(type_e_mismatch)
  2564. { Make operands the same setdef. If one's elementtype fits }
  2565. { entirely inside the other's, pick the one with the largest }
  2566. { range. Otherwise create a new setdef with a range which }
  2567. { can contain both. }
  2568. else if not(equal_defs(ld,rd)) then
  2569. begin
  2570. { note: ld cannot be an empty set with elementdef=nil in }
  2571. { case right is not a set, arrayconstructor_to_set takes }
  2572. { care of that }
  2573. { 1: rd is a set with an assigned elementdef, and ld is }
  2574. { either an empty set without elementdef or a set whose }
  2575. { elementdef fits in rd's elementdef -> convert to rd }
  2576. if ((rd.typ=setdef) and
  2577. assigned(tsetdef(rd).elementdef) and
  2578. (not assigned(tsetdef(ld).elementdef) or
  2579. is_in_limit(ld,rd))) then
  2580. inserttypeconv(left,rd)
  2581. { 2: rd is either an empty set without elementdef or a set }
  2582. { whose elementdef fits in ld's elementdef, or a set }
  2583. { element whose def fits in ld's elementdef -> convert }
  2584. { to ld. ld's elementdef can't be nil here, is caught }
  2585. { previous case and "note:" above }
  2586. else if ((rd.typ=setdef) and
  2587. (not assigned(tsetdef(rd).elementdef) or
  2588. is_in_limit(rd,ld))) or
  2589. ((rd.typ<>setdef) and
  2590. is_in_limit(rd,tsetdef(ld).elementdef)) then
  2591. if (rd.typ=setdef) then
  2592. inserttypeconv(right,ld)
  2593. else
  2594. inserttypeconv(right,tsetdef(ld).elementdef)
  2595. { 3: otherwise create setdef which encompasses both, taking }
  2596. { into account empty sets without elementdef }
  2597. else
  2598. begin
  2599. if assigned(tsetdef(ld).elementdef) then
  2600. begin
  2601. llow:=tsetdef(ld).setbase;
  2602. lhigh:=tsetdef(ld).setmax;
  2603. end;
  2604. if (rd.typ=setdef) then
  2605. if assigned(tsetdef(rd).elementdef) then
  2606. begin
  2607. rlow:=tsetdef(rd).setbase;
  2608. rhigh:=tsetdef(rd).setmax;
  2609. end
  2610. else
  2611. begin
  2612. { ld's elementdef must have been valid }
  2613. rlow:=llow;
  2614. rhigh:=lhigh;
  2615. end
  2616. else
  2617. getrange(rd,rlow,rhigh);
  2618. if not assigned(tsetdef(ld).elementdef) then
  2619. begin
  2620. llow:=rlow;
  2621. lhigh:=rhigh;
  2622. end;
  2623. nd:=csetdef.create(tsetdef(ld).elementdef,min(llow,rlow).svalue,max(lhigh,rhigh).svalue,true);
  2624. inserttypeconv(left,nd);
  2625. if (rd.typ=setdef) then
  2626. inserttypeconv(right,nd)
  2627. else
  2628. inserttypeconv(right,tsetdef(nd).elementdef);
  2629. end;
  2630. end;
  2631. end
  2632. { pointer comparision and subtraction }
  2633. else if (
  2634. (rd.typ=pointerdef) and (ld.typ=pointerdef)
  2635. ) or
  2636. { compare/add pchar to variable (not stringconst) char arrays
  2637. by addresses like BP/Delphi }
  2638. (
  2639. (nodetype in [equaln,unequaln,subn,addn]) and
  2640. (
  2641. ((is_pchar(ld) or (lt=niln)) and is_chararray(rd) and (rt<>stringconstn)) or
  2642. ((is_pchar(rd) or (rt=niln)) and is_chararray(ld) and (lt<>stringconstn))
  2643. )
  2644. ) then
  2645. begin
  2646. { convert char array to pointer }
  2647. if is_chararray(rd) then
  2648. begin
  2649. inserttypeconv(right,charpointertype);
  2650. rd:=right.resultdef;
  2651. end
  2652. else if is_chararray(ld) then
  2653. begin
  2654. inserttypeconv(left,charpointertype);
  2655. ld:=left.resultdef;
  2656. end;
  2657. case nodetype of
  2658. equaln,unequaln :
  2659. begin
  2660. if is_voidpointer(right.resultdef) then
  2661. inserttypeconv(right,left.resultdef)
  2662. else if is_voidpointer(left.resultdef) then
  2663. inserttypeconv(left,right.resultdef)
  2664. else if not(equal_defs(ld,rd)) then
  2665. IncompatibleTypes(ld,rd);
  2666. { now that the type checking is done, convert both to charpointer, }
  2667. { because methodpointers are 8 bytes even though only the first 4 }
  2668. { bytes must be compared. This can happen here if we are in }
  2669. { TP/Delphi mode, because there @methodpointer = voidpointer (but }
  2670. { a voidpointer of 8 bytes). A conversion to voidpointer would be }
  2671. { optimized away, since the result already was a voidpointer, so }
  2672. { use a charpointer instead (JM) }
  2673. {$if defined(jvm)}
  2674. inserttypeconv_internal(left,java_jlobject);
  2675. inserttypeconv_internal(right,java_jlobject);
  2676. {$elseif defined(i8086)}
  2677. if is_hugepointer(left.resultdef) then
  2678. inserttypeconv_internal(left,charhugepointertype)
  2679. else if is_farpointer(left.resultdef) then
  2680. inserttypeconv_internal(left,charfarpointertype)
  2681. else
  2682. inserttypeconv_internal(left,charnearpointertype);
  2683. if is_hugepointer(right.resultdef) then
  2684. inserttypeconv_internal(right,charhugepointertype)
  2685. else if is_farpointer(right.resultdef) then
  2686. inserttypeconv_internal(right,charfarpointertype)
  2687. else
  2688. inserttypeconv_internal(right,charnearpointertype);
  2689. {$elseif defined(wasm)}
  2690. if is_wasm_reference_type(left.resultdef) then
  2691. inserttypeconv(right,left.resultdef)
  2692. else if is_wasm_reference_type(right.resultdef) then
  2693. inserttypeconv(left,right.resultdef)
  2694. else
  2695. begin
  2696. inserttypeconv_internal(left,charpointertype);
  2697. inserttypeconv_internal(right,charpointertype);
  2698. end;
  2699. {$else}
  2700. inserttypeconv_internal(left,charpointertype);
  2701. inserttypeconv_internal(right,charpointertype);
  2702. {$endif jvm}
  2703. end;
  2704. ltn,lten,gtn,gten:
  2705. begin
  2706. if (cs_extsyntax in current_settings.moduleswitches) or
  2707. (nf_internal in flags) then
  2708. begin
  2709. if is_voidpointer(right.resultdef) then
  2710. inserttypeconv(right,left.resultdef)
  2711. else if is_voidpointer(left.resultdef) then
  2712. inserttypeconv(left,right.resultdef)
  2713. else if not(equal_defs(ld,rd)) then
  2714. IncompatibleTypes(ld,rd);
  2715. end
  2716. else
  2717. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2718. end;
  2719. subn:
  2720. begin
  2721. if (cs_extsyntax in current_settings.moduleswitches) or
  2722. (nf_internal in flags) then
  2723. begin
  2724. if is_voidpointer(right.resultdef) then
  2725. begin
  2726. if is_big_untyped_addrnode(right) then
  2727. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  2728. inserttypeconv(right,left.resultdef)
  2729. end
  2730. else if is_voidpointer(left.resultdef) then
  2731. inserttypeconv(left,right.resultdef)
  2732. else if not(equal_defs(ld,rd)) then
  2733. IncompatibleTypes(ld,rd);
  2734. end
  2735. else
  2736. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2737. if not(anf_has_pointerdiv in addnodeflags) and
  2738. (tpointerdef(rd).pointeddef.size>1) then
  2739. begin
  2740. hp:=getcopy;
  2741. include(taddnode(hp).addnodeflags, anf_has_pointerdiv);
  2742. result:=cmoddivnode.create(divn,hp,
  2743. cordconstnode.create(tpointerdef(rd).pointeddef.size,tpointerdef(rd).pointer_subtraction_result_type,false));
  2744. end;
  2745. resultdef:=tpointerdef(rd).pointer_subtraction_result_type;
  2746. exit;
  2747. end;
  2748. else
  2749. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2750. end;
  2751. end
  2752. { is one of the operands a string?,
  2753. chararrays are also handled as strings (after conversion), also take
  2754. care of chararray+chararray and chararray+char.
  2755. Note: Must be done after pointerdef+pointerdef has been checked, else
  2756. pchar is converted to string }
  2757. else if (rd.typ=stringdef) or
  2758. (ld.typ=stringdef) or
  2759. { stringconstn's can be arraydefs }
  2760. (lt=stringconstn) or
  2761. (rt=stringconstn) or
  2762. ((is_pchar(rd) or is_chararray(rd) or is_char(rd) or is_open_chararray(rd) or
  2763. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd)) and
  2764. (is_pchar(ld) or is_chararray(ld) or is_char(ld) or is_open_chararray(ld) or
  2765. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld))) then
  2766. begin
  2767. if (nodetype in [addn,equaln,unequaln,lten,gten,ltn,gtn]) then
  2768. begin
  2769. { Is there a unicodestring? }
  2770. if is_unicodestring(rd) or is_unicodestring(ld) or
  2771. ((m_default_unicodestring in current_settings.modeswitches) and
  2772. (cs_refcountedstrings in current_settings.localswitches) and
  2773. (
  2774. is_pwidechar(rd) or is_widechararray(rd) or is_open_widechararray(rd) or (lt = stringconstn) or
  2775. is_pwidechar(ld) or is_widechararray(ld) or is_open_widechararray(ld) or (rt = stringconstn)
  2776. )
  2777. ) then
  2778. strtype:=st_unicodestring
  2779. else
  2780. { Is there a widestring? }
  2781. if is_widestring(rd) or is_widestring(ld) or
  2782. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd) or
  2783. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld) then
  2784. strtype:=st_widestring
  2785. else
  2786. if is_ansistring(rd) or is_ansistring(ld) or
  2787. ((cs_refcountedstrings in current_settings.localswitches) and
  2788. //todo: Move some of this to longstring's then they are implemented?
  2789. (
  2790. is_pchar(rd) or (is_chararray(rd) and (rd.size > 255)) or is_open_chararray(rd) or (lt = stringconstn) or
  2791. is_pchar(ld) or (is_chararray(ld) and (ld.size > 255)) or is_open_chararray(ld) or (rt = stringconstn)
  2792. )
  2793. ) then
  2794. strtype:=st_ansistring
  2795. else
  2796. if is_longstring(rd) or is_longstring(ld) then
  2797. strtype:=st_longstring
  2798. else
  2799. begin
  2800. { TODO: todo: add a warning/hint here if one converting a too large array}
  2801. { nodes is PChar, array [with size > 255] or OpenArrayOfChar.
  2802. Note: Delphi halts with error if "array [0..xx] of char"
  2803. is assigned to ShortString and string length is less
  2804. then array size }
  2805. strtype:= st_shortstring;
  2806. end;
  2807. // Now convert nodes to common string type
  2808. case strtype of
  2809. st_widestring :
  2810. begin
  2811. if not(is_widestring(rd)) then
  2812. inserttypeconv(right,cwidestringtype);
  2813. if not(is_widestring(ld)) then
  2814. inserttypeconv(left,cwidestringtype);
  2815. end;
  2816. st_unicodestring :
  2817. begin
  2818. if not(is_unicodestring(rd)) then
  2819. if not ((ld.size=0) and (nodetype in [equaln,unequaln])) then
  2820. inserttypeconv(right,cunicodestringtype);
  2821. if not(is_unicodestring(ld)) then
  2822. if not ((rd.size=0) and (nodetype in [equaln,unequaln])) then
  2823. inserttypeconv(left,cunicodestringtype);
  2824. end;
  2825. st_ansistring :
  2826. begin
  2827. { use same code page if possible (don't force same code
  2828. page in case both are ansistrings with code page <>
  2829. CP_NONE, since then data loss can occur: the ansistring
  2830. helpers will convert them at run time to an encoding
  2831. that can represent both encodings) }
  2832. if is_ansistring(ld) and
  2833. (tstringdef(ld).encoding<>0) and
  2834. (tstringdef(ld).encoding<>globals.CP_NONE) and
  2835. (not is_ansistring(rd) or
  2836. (tstringdef(rd).encoding=0) or
  2837. (tstringdef(rd).encoding=globals.CP_NONE)) then
  2838. inserttypeconv(right,ld)
  2839. else if is_ansistring(rd) and
  2840. (tstringdef(rd).encoding<>0) and
  2841. (tstringdef(rd).encoding<>globals.CP_NONE) and
  2842. (not is_ansistring(ld) or
  2843. (tstringdef(ld).encoding=0) or
  2844. (tstringdef(ld).encoding=globals.CP_NONE)) then
  2845. inserttypeconv(left,rd)
  2846. else
  2847. begin
  2848. if not is_ansistring(ld) then
  2849. inserttypeconv(left,getansistringdef);
  2850. if not is_ansistring(rd) then
  2851. inserttypeconv(right,getansistringdef);
  2852. end;
  2853. end;
  2854. st_longstring :
  2855. begin
  2856. if not(is_longstring(rd)) then
  2857. inserttypeconv(right,clongstringtype);
  2858. if not(is_longstring(ld)) then
  2859. inserttypeconv(left,clongstringtype);
  2860. end;
  2861. st_shortstring :
  2862. begin
  2863. if not(is_shortstring(ld)) then
  2864. inserttypeconv(left,cshortstringtype);
  2865. { don't convert char, that can be handled by the optimized node }
  2866. if not(is_shortstring(rd) or is_char(rd)) then
  2867. inserttypeconv(right,cshortstringtype);
  2868. end;
  2869. end;
  2870. end
  2871. else
  2872. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2873. end
  2874. { implicit pointer object type comparison }
  2875. else if is_implicit_pointer_object_type(rd) or is_implicit_pointer_object_type(ld) then
  2876. begin
  2877. if (nodetype in [equaln,unequaln]) then
  2878. begin
  2879. if is_implicit_pointer_object_type(rd) and is_implicit_pointer_object_type(ld) then
  2880. begin
  2881. if def_is_related(tobjectdef(rd),tobjectdef(ld)) then
  2882. inserttypeconv(right,left.resultdef)
  2883. else
  2884. inserttypeconv(left,right.resultdef);
  2885. end
  2886. else if is_implicit_pointer_object_type(rd) then
  2887. inserttypeconv(left,right.resultdef)
  2888. else
  2889. inserttypeconv(right,left.resultdef);
  2890. end
  2891. else
  2892. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2893. end
  2894. else if (rd.typ=classrefdef) and (ld.typ=classrefdef) then
  2895. begin
  2896. if (nodetype in [equaln,unequaln]) then
  2897. begin
  2898. if def_is_related(tobjectdef(tclassrefdef(rd).pointeddef),
  2899. tobjectdef(tclassrefdef(ld).pointeddef)) then
  2900. inserttypeconv(right,left.resultdef)
  2901. else
  2902. inserttypeconv(left,right.resultdef);
  2903. end
  2904. else
  2905. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2906. end
  2907. { allow comparison with nil pointer }
  2908. else if is_implicit_pointer_object_type(rd) or (rd.typ=classrefdef) then
  2909. begin
  2910. if (nodetype in [equaln,unequaln]) then
  2911. inserttypeconv(left,right.resultdef)
  2912. else
  2913. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2914. end
  2915. else if is_implicit_pointer_object_type(ld) or (ld.typ=classrefdef) then
  2916. begin
  2917. if (nodetype in [equaln,unequaln]) then
  2918. inserttypeconv(right,left.resultdef)
  2919. else
  2920. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2921. end
  2922. { support procvar=nil,procvar<>nil }
  2923. else if ((ld.typ=procvardef) and (rt=niln)) or
  2924. ((rd.typ=procvardef) and (lt=niln)) then
  2925. begin
  2926. if not(nodetype in [equaln,unequaln]) then
  2927. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2928. { find proc field in methodpointer record }
  2929. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  2930. if not assigned(hsym) then
  2931. internalerror(200412043);
  2932. { For methodpointers compare only tmethodpointer.proc }
  2933. if (rd.typ=procvardef) and
  2934. (not tprocvardef(rd).is_addressonly) then
  2935. begin
  2936. right:=csubscriptnode.create(
  2937. hsym,
  2938. ctypeconvnode.create_internal(right,methodpointertype));
  2939. typecheckpass(right);
  2940. end;
  2941. if (ld.typ=procvardef) and
  2942. (not tprocvardef(ld).is_addressonly) then
  2943. begin
  2944. left:=csubscriptnode.create(
  2945. hsym,
  2946. ctypeconvnode.create_internal(left,methodpointertype));
  2947. typecheckpass(left);
  2948. end;
  2949. if lt=niln then
  2950. inserttypeconv_explicit(left,right.resultdef)
  2951. else
  2952. inserttypeconv_explicit(right,left.resultdef)
  2953. end
  2954. { <dyn. array>+<dyn. array> ? }
  2955. else if (nodetype=addn) and (is_dynamic_array(ld) or is_dynamic_array(rd)) then
  2956. begin
  2957. { empty array to add? }
  2958. if (right.nodetype=arrayconstructorn) and (tarrayconstructornode(right).left=nil) then
  2959. begin
  2960. result:=left;
  2961. left:=nil;
  2962. exit;
  2963. end
  2964. else if (left.nodetype=arrayconstructorn) and (tarrayconstructornode(left).left=nil) then
  2965. begin
  2966. result:=right;
  2967. right:=nil;
  2968. exit;
  2969. end
  2970. else
  2971. begin
  2972. result:=maybe_convert_to_insert;
  2973. if assigned(result) then
  2974. exit;
  2975. if not(is_dynamic_array(ld)) then
  2976. inserttypeconv(left,rd);
  2977. if not(is_dynamic_array(rd)) then
  2978. inserttypeconv(right,ld);
  2979. end;
  2980. end
  2981. { support dynamicarray=nil,dynamicarray<>nil }
  2982. else if (is_dynamic_array(ld) and (rt=niln)) or
  2983. (is_dynamic_array(rd) and (lt=niln)) or
  2984. (is_dynamic_array(ld) and is_dynamic_array(rd)) then
  2985. begin
  2986. if not(nodetype in [equaln,unequaln]) then
  2987. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2988. if lt=niln then
  2989. inserttypeconv_explicit(left,right.resultdef)
  2990. else
  2991. inserttypeconv_explicit(right,left.resultdef)
  2992. end
  2993. {$ifdef SUPPORT_MMX}
  2994. { mmx support, this must be before the zero based array
  2995. check }
  2996. else if (cs_mmx in current_settings.localswitches) and
  2997. is_mmx_able_array(ld) and
  2998. is_mmx_able_array(rd) and
  2999. equal_defs(ld,rd) then
  3000. begin
  3001. case nodetype of
  3002. addn,subn,xorn,orn,andn:
  3003. ;
  3004. { mul is a little bit restricted }
  3005. muln:
  3006. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  3007. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3008. else
  3009. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3010. end;
  3011. end
  3012. {$endif SUPPORT_MMX}
  3013. { vector support, this must be before the zero based array
  3014. check }
  3015. else if (cs_support_vectors in current_settings.globalswitches) and
  3016. fits_in_mm_register(ld) and
  3017. fits_in_mm_register(rd) and
  3018. equal_defs(ld,rd) then
  3019. begin
  3020. if not(nodetype in [addn,subn,xorn,orn,andn,muln,slashn]) then
  3021. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3022. { both defs must be equal, so taking left or right as resultdef doesn't matter }
  3023. resultdef:=to_hwvectordef(left.resultdef,false);
  3024. end
  3025. { this is a little bit dangerous, also the left type }
  3026. { pointer to should be checked! This broke the mmx support }
  3027. else if (rd.typ=pointerdef) or
  3028. (is_zero_based_array(rd) and (rt<>stringconstn)) then
  3029. begin
  3030. if is_zero_based_array(rd) then
  3031. begin
  3032. resultdef:=cpointerdef.getreusable(tarraydef(rd).elementdef);
  3033. inserttypeconv(right,resultdef);
  3034. end
  3035. else
  3036. resultdef:=right.resultdef;
  3037. inserttypeconv(left,tpointerdef(right.resultdef).pointer_arithmetic_int_type);
  3038. if nodetype=addn then
  3039. begin
  3040. if (rt=niln) then
  3041. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,'NIL');
  3042. if (not(cs_extsyntax in current_settings.moduleswitches) and not(nf_internal in flags)) or
  3043. (not (is_pchar(rd) or is_chararray(rd) or is_open_chararray(rd) or is_widechar(rd) or is_widechararray(rd) or is_open_widechararray(rd)) and
  3044. not(cs_pointermath in current_settings.localswitches) and
  3045. not((rd.typ=pointerdef) and tpointerdef(rd).has_pointer_math)) then
  3046. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3047. if (rd.typ=pointerdef) and
  3048. (tpointerdef(rd).pointeddef.size>1) then
  3049. begin
  3050. left:=caddnode.create(muln,left,
  3051. cordconstnode.create(tpointerdef(rd).pointeddef.size,tpointerdef(right.resultdef).pointer_arithmetic_int_type,true));
  3052. typecheckpass(left);
  3053. end;
  3054. end
  3055. else
  3056. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3057. end
  3058. else if (ld.typ=pointerdef) or
  3059. (is_zero_based_array(ld) and (lt<>stringconstn)) then
  3060. begin
  3061. if is_zero_based_array(ld) then
  3062. begin
  3063. resultdef:=cpointerdef.getreusable(tarraydef(ld).elementdef);
  3064. inserttypeconv(left,resultdef);
  3065. end
  3066. else
  3067. resultdef:=left.resultdef;
  3068. inserttypeconv(right,tpointerdef(left.resultdef).pointer_arithmetic_int_type);
  3069. if nodetype in [addn,subn] then
  3070. begin
  3071. if (lt=niln) then
  3072. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),'NIL',rd.typename);
  3073. if (not(cs_extsyntax in current_settings.moduleswitches) and not(nf_internal in flags)) or
  3074. (not (is_pchar(ld) or is_chararray(ld) or is_open_chararray(ld) or is_widechar(ld) or is_widechararray(ld) or is_open_widechararray(ld)) and
  3075. not(cs_pointermath in current_settings.localswitches) and
  3076. not((ld.typ=pointerdef) and tpointerdef(ld).has_pointer_math)) then
  3077. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3078. if (ld.typ=pointerdef) then
  3079. begin
  3080. if is_big_untyped_addrnode(left) then
  3081. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  3082. if (tpointerdef(ld).pointeddef.size>1) then
  3083. begin
  3084. right:=caddnode.create(muln,right,
  3085. cordconstnode.create(tpointerdef(ld).pointeddef.size,tpointerdef(left.resultdef).pointer_arithmetic_int_type,true));
  3086. typecheckpass(right);
  3087. end
  3088. end else
  3089. if is_zero_based_array(ld) and
  3090. (tarraydef(ld).elementdef.size>1) then
  3091. begin
  3092. right:=caddnode.create(muln,right,
  3093. cordconstnode.create(tarraydef(ld).elementdef.size,tpointerdef(left.resultdef).pointer_arithmetic_int_type,true));
  3094. typecheckpass(right);
  3095. end;
  3096. end
  3097. else
  3098. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3099. end
  3100. else if (rd.typ=procvardef) and
  3101. (ld.typ=procvardef) and
  3102. equal_defs(rd,ld) then
  3103. begin
  3104. if (nodetype in [equaln,unequaln]) then
  3105. begin
  3106. if tprocvardef(rd).is_addressonly then
  3107. begin
  3108. inserttypeconv_internal(right,voidcodepointertype);
  3109. inserttypeconv_internal(left,voidcodepointertype);
  3110. end
  3111. else
  3112. begin
  3113. { find proc field in methodpointer record }
  3114. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  3115. if not assigned(hsym) then
  3116. internalerror(2004120405);
  3117. { Compare tmehodpointer(left).proc }
  3118. right:=csubscriptnode.create(
  3119. hsym,
  3120. ctypeconvnode.create_internal(right,methodpointertype));
  3121. typecheckpass(right);
  3122. left:=csubscriptnode.create(
  3123. hsym,
  3124. ctypeconvnode.create_internal(left,methodpointertype));
  3125. typecheckpass(left);
  3126. end;
  3127. end
  3128. else
  3129. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3130. end
  3131. { enums }
  3132. else if (ld.typ=enumdef) and (rd.typ=enumdef) then
  3133. begin
  3134. if allowenumop(nodetype) or (nf_internal in flags) then
  3135. inserttypeconv(right,left.resultdef)
  3136. else
  3137. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3138. end
  3139. { generic conversion, this is for error recovery }
  3140. else
  3141. begin
  3142. inserttypeconv(left,sinttype);
  3143. inserttypeconv(right,sinttype);
  3144. end;
  3145. if cmp_of_disjunct_ranges(res) and not(nf_internal in flags) then
  3146. begin
  3147. if res then
  3148. CGMessage(type_w_comparison_always_true)
  3149. else
  3150. CGMessage(type_w_comparison_always_false);
  3151. end;
  3152. { set resultdef if not already done }
  3153. if not assigned(resultdef) then
  3154. begin
  3155. case nodetype of
  3156. ltn,lten,gtn,gten,equaln,unequaln :
  3157. resultdef:=pasbool1type;
  3158. slashn :
  3159. resultdef:=resultrealdef;
  3160. addn:
  3161. begin
  3162. { for strings, return is always a 255 char string }
  3163. if is_shortstring(left.resultdef) then
  3164. resultdef:=cshortstringtype
  3165. else
  3166. { for ansistrings set resultdef to assignment left node
  3167. if it is an assignment and left node expects ansistring }
  3168. if is_ansistring(left.resultdef) and
  3169. assigned(aktassignmentnode) and
  3170. (aktassignmentnode.right=self) and
  3171. is_ansistring(aktassignmentnode.left.resultdef) then
  3172. resultdef:=aktassignmentnode.left.resultdef
  3173. else
  3174. resultdef:=left.resultdef;
  3175. end;
  3176. else
  3177. resultdef:=left.resultdef;
  3178. end;
  3179. end;
  3180. { when the result is currency we need some extra code for
  3181. multiplication and division. this should not be done when
  3182. the muln or slashn node is created internally }
  3183. if not(nf_is_currency in flags) and
  3184. is_currency(resultdef) then
  3185. begin
  3186. case nodetype of
  3187. slashn :
  3188. begin
  3189. { slashn will only work with floats }
  3190. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  3191. include(hp.flags,nf_is_currency);
  3192. result:=hp;
  3193. end;
  3194. muln :
  3195. begin
  3196. hp:=nil;
  3197. if s64currencytype.typ=floatdef then
  3198. begin
  3199. { if left is a currency integer constant, we can get rid of the factor 10000 }
  3200. { int64(...) causes a cast on currency, so it is the currency value multiplied by 10000 }
  3201. if (left.nodetype=realconstn) and (is_currency(left.resultdef)) and (not(nf_is_currency in left.flags)) and ((trunc(trealconstnode(left).value_real) mod 10000)=0) then
  3202. begin
  3203. { trealconstnode expects that value_real and value_currency contain valid values }
  3204. {$ifdef FPC_CURRENCY_IS_INT64}
  3205. trealconstnode(left).value_currency:=pint64(@(trealconstnode(left).value_currency))^ div 10000;
  3206. {$else}
  3207. trealconstnode(left).value_currency:=trealconstnode(left).value_currency / 10000;
  3208. {$endif}
  3209. trealconstnode(left).value_real:=trealconstnode(left).value_real/10000;
  3210. end
  3211. { or if right is an integer constant, we can get rid of its factor 10000 }
  3212. else if (right.nodetype=realconstn) and (is_currency(right.resultdef)) and (not(nf_is_currency in right.flags)) and ((trunc(trealconstnode(right).value_real) mod 10000)=0) then
  3213. begin
  3214. { trealconstnode expects that value and value_currency contain valid values }
  3215. {$ifdef FPC_CURRENCY_IS_INT64}
  3216. trealconstnode(right).value_currency:=pint64(@(trealconstnode(right).value_currency))^ div 10000;
  3217. {$else}
  3218. trealconstnode(right).value_currency:=trealconstnode(right).value_currency / 10000;
  3219. {$endif}
  3220. trealconstnode(right).value_real:=trealconstnode(right).value_real/10000;
  3221. end
  3222. else
  3223. begin
  3224. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype));
  3225. include(hp.flags,nf_is_currency);
  3226. end;
  3227. end
  3228. else
  3229. begin
  3230. { if left is a currency integer constant, we can get rid of the factor 10000 }
  3231. if (left.nodetype=ordconstn) and (is_currency(left.resultdef)) and ((tordconstnode(left).value mod 10000)=0) then
  3232. tordconstnode(left).value:=tordconstnode(left).value div 10000
  3233. { or if right is an integer constant, we can get rid of its factor 10000 }
  3234. else if (right.nodetype=ordconstn) and (is_currency(right.resultdef)) and ((tordconstnode(right).value mod 10000)=0) then
  3235. tordconstnode(right).value:=tordconstnode(right).value div 10000
  3236. else if (right.nodetype=muln) and is_currency(right.resultdef) and
  3237. { do not test swapped here as the internal conversions are only create as "var."*"10000" }
  3238. is_currency(taddnode(right).right.resultdef) and (taddnode(right).right.nodetype=ordconstn) and (tordconstnode(taddnode(right).right).value=10000) and
  3239. is_currency(taddnode(right).left.resultdef) and (taddnode(right).left.nodetype=typeconvn) then
  3240. begin
  3241. hp:=taddnode(right).left.getcopy;
  3242. include(hp.flags,nf_is_currency);
  3243. right.free;
  3244. right:=hp;
  3245. hp:=nil;
  3246. end
  3247. else if (left.nodetype=muln) and is_currency(left.resultdef) and
  3248. { do not test swapped here as the internal conversions are only create as "var."*"10000" }
  3249. is_currency(taddnode(left).right.resultdef) and (taddnode(left).right.nodetype=ordconstn) and (tordconstnode(taddnode(left).right).value=10000) and
  3250. is_currency(taddnode(left).left.resultdef) and (taddnode(left).left.nodetype=typeconvn) then
  3251. begin
  3252. hp:=taddnode(left).left.getcopy;
  3253. include(hp.flags,nf_is_currency);
  3254. left.free;
  3255. left:=hp;
  3256. hp:=nil;
  3257. end
  3258. else
  3259. begin
  3260. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  3261. include(hp.flags,nf_is_currency);
  3262. end
  3263. end;
  3264. result:=hp
  3265. end;
  3266. else
  3267. ;
  3268. end;
  3269. end;
  3270. if not(codegenerror) and
  3271. not assigned(result) then
  3272. result:=simplify(false);
  3273. end;
  3274. function taddnode.first_addstring: tnode;
  3275. var
  3276. p: tnode;
  3277. newstatement : tstatementnode;
  3278. tempnode : ttempcreatenode;
  3279. cmpfuncname: string;
  3280. para: tcallparanode;
  3281. begin
  3282. result:=nil;
  3283. { when we get here, we are sure that both the left and the right }
  3284. { node are both strings of the same stringtype (JM) }
  3285. case nodetype of
  3286. addn:
  3287. begin
  3288. if (left.nodetype=stringconstn) and (tstringconstnode(left).len=0) then
  3289. begin
  3290. result:=right;
  3291. left.free;
  3292. left:=nil;
  3293. right:=nil;
  3294. exit;
  3295. end;
  3296. if (right.nodetype=stringconstn) and (tstringconstnode(right).len=0) then
  3297. begin
  3298. result:=left;
  3299. left:=nil;
  3300. right.free;
  3301. right:=nil;
  3302. exit;
  3303. end;
  3304. { create the call to the concat routine both strings as arguments }
  3305. if assigned(aktassignmentnode) and
  3306. (aktassignmentnode.right=self) and
  3307. (
  3308. (aktassignmentnode.left.resultdef=resultdef) or
  3309. (
  3310. is_shortstring(aktassignmentnode.left.resultdef) and
  3311. is_shortstring(resultdef)
  3312. )
  3313. ) and
  3314. valid_for_var(aktassignmentnode.left,false) then
  3315. begin
  3316. para:=ccallparanode.create(
  3317. right,
  3318. ccallparanode.create(
  3319. left,
  3320. ccallparanode.create(aktassignmentnode.left.getcopy,nil)
  3321. )
  3322. );
  3323. if is_ansistring(resultdef) then
  3324. para:=ccallparanode.create(
  3325. cordconstnode.create(
  3326. { don't use getparaencoding(), we have to know
  3327. when the result is rawbytestring }
  3328. tstringdef(resultdef).encoding,
  3329. u16inttype,
  3330. true
  3331. ),
  3332. para
  3333. );
  3334. result:=ccallnode.createintern(
  3335. 'fpc_'+tstringdef(resultdef).stringtypname+'_concat',
  3336. para
  3337. );
  3338. include(aktassignmentnode.assignmentnodeflags,anf_assign_done_in_right);
  3339. firstpass(result);
  3340. end
  3341. else
  3342. begin
  3343. result:=internalstatements(newstatement);
  3344. tempnode:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  3345. addstatement(newstatement,tempnode);
  3346. { initialize the temp, since it will be passed to a
  3347. var-parameter (and finalization, which is performed by the
  3348. ttempcreate node and which takes care of the initialization
  3349. on native targets, is a noop on managed VM targets) }
  3350. if (target_info.system in systems_managed_vm) and
  3351. is_managed_type(resultdef) then
  3352. addstatement(newstatement,cinlinenode.create(in_setlength_x,
  3353. false,
  3354. ccallparanode.create(genintconstnode(0),
  3355. ccallparanode.create(ctemprefnode.create(tempnode),nil))));
  3356. para:=ccallparanode.create(
  3357. right,
  3358. ccallparanode.create(
  3359. left,
  3360. ccallparanode.create(ctemprefnode.create(tempnode),nil)
  3361. )
  3362. );
  3363. if is_ansistring(resultdef) then
  3364. para:=ccallparanode.create(
  3365. cordconstnode.create(
  3366. { don't use getparaencoding(), we have to know
  3367. when the result is rawbytestring }
  3368. tstringdef(resultdef).encoding,
  3369. u16inttype,
  3370. true
  3371. ),
  3372. para
  3373. );
  3374. addstatement(
  3375. newstatement,
  3376. ccallnode.createintern(
  3377. 'fpc_'+tstringdef(resultdef).stringtypname+'_concat',
  3378. para
  3379. )
  3380. );
  3381. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  3382. addstatement(newstatement,ctemprefnode.create(tempnode));
  3383. end;
  3384. { we reused the arguments }
  3385. left := nil;
  3386. right := nil;
  3387. end;
  3388. ltn,lten,gtn,gten,equaln,unequaln :
  3389. begin
  3390. { generate better code for comparison with empty string, we
  3391. only need to compare the length with 0 }
  3392. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  3393. (((left.nodetype=stringconstn) and (tstringconstnode(left).len=0)) or
  3394. ((right.nodetype=stringconstn) and (tstringconstnode(right).len=0))) then
  3395. begin
  3396. { switch so that the constant is always on the right }
  3397. if left.nodetype = stringconstn then
  3398. begin
  3399. p := left;
  3400. left := right;
  3401. right := p;
  3402. nodetype:=swap_relation[nodetype];
  3403. end;
  3404. if is_shortstring(left.resultdef) or
  3405. (nodetype in [gtn,gten,ltn,lten]) or
  3406. (target_info.system in systems_managed_vm) then
  3407. { compare the length with 0 }
  3408. result := caddnode.create(nodetype,
  3409. cinlinenode.create(in_length_x,false,left),
  3410. cordconstnode.create(0,s8inttype,false))
  3411. else { nodetype in [equaln,unequaln] }
  3412. begin
  3413. if is_widestring(left.resultdef) and (tf_winlikewidestring in target_info.flags) then
  3414. begin
  3415. { windows like widestrings requires that we also check the length }
  3416. result:=cinlinenode.create(in_length_x,false,left);
  3417. { and compare its result with 0 }
  3418. result:=caddnode.create(equaln,result,cordconstnode.create(0,s8inttype,false));
  3419. if nodetype=unequaln then
  3420. result:=cnotnode.create(result);
  3421. end
  3422. else
  3423. begin
  3424. { compare the pointer with nil (for ansistrings etc), }
  3425. { faster than getting the length (JM) }
  3426. result:= caddnode.create(nodetype,
  3427. ctypeconvnode.create_internal(left,voidpointertype),
  3428. cpointerconstnode.create(0,voidpointertype));
  3429. end;
  3430. end;
  3431. { left is reused }
  3432. left := nil;
  3433. { right isn't }
  3434. right.free;
  3435. right := nil;
  3436. exit;
  3437. end;
  3438. { no string constant -> call compare routine }
  3439. cmpfuncname := 'fpc_'+tstringdef(left.resultdef).stringtypname+'_compare';
  3440. { for equality checks use optimized version }
  3441. if nodetype in [equaln,unequaln] then
  3442. cmpfuncname := cmpfuncname + '_equal';
  3443. result := ccallnode.createintern(cmpfuncname,
  3444. ccallparanode.create(right,ccallparanode.create(left,nil)));
  3445. { and compare its result with 0 according to the original operator }
  3446. result := caddnode.create(nodetype,result,
  3447. cordconstnode.create(0,s8inttype,false));
  3448. left := nil;
  3449. right := nil;
  3450. end;
  3451. else
  3452. internalerror(2019050520);
  3453. end;
  3454. end;
  3455. function taddnode.first_addset : tnode;
  3456. procedure call_varset_helper(const n : string);
  3457. var
  3458. newstatement : tstatementnode;
  3459. temp : ttempcreatenode;
  3460. begin
  3461. { directly load the result set into the assignee if possible }
  3462. if assigned(aktassignmentnode) and
  3463. (aktassignmentnode.right=self) and
  3464. (aktassignmentnode.left.resultdef=resultdef) and
  3465. valid_for_var(aktassignmentnode.left,false) then
  3466. begin
  3467. result:=ccallnode.createintern(n,
  3468. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3469. ccallparanode.create(aktassignmentnode.left.getcopy,
  3470. ccallparanode.create(right,
  3471. ccallparanode.create(left,nil))))
  3472. );
  3473. { remove reused parts from original node }
  3474. left:=nil;
  3475. right:=nil;
  3476. include(aktassignmentnode.assignmentnodeflags,anf_assign_done_in_right);
  3477. firstpass(result);
  3478. end
  3479. else
  3480. begin
  3481. { add two var sets }
  3482. result:=internalstatements(newstatement);
  3483. { create temp for result }
  3484. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  3485. addstatement(newstatement,temp);
  3486. addstatement(newstatement,ccallnode.createintern(n,
  3487. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3488. ccallparanode.create(ctemprefnode.create(temp),
  3489. ccallparanode.create(right,
  3490. ccallparanode.create(left,nil)))))
  3491. );
  3492. { remove reused parts from original node }
  3493. left:=nil;
  3494. right:=nil;
  3495. { the last statement should return the value as
  3496. location and type, this is done be referencing the
  3497. temp and converting it first from a persistent temp to
  3498. normal temp }
  3499. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  3500. addstatement(newstatement,ctemprefnode.create(temp));
  3501. end;
  3502. end;
  3503. var
  3504. procname: string[31];
  3505. tempn: tnode;
  3506. newstatement : tstatementnode;
  3507. temp : ttempcreatenode;
  3508. no_temp: Boolean;
  3509. begin
  3510. result:=nil;
  3511. case nodetype of
  3512. equaln,unequaln,lten,gten:
  3513. begin
  3514. case nodetype of
  3515. equaln,unequaln:
  3516. procname := 'fpc_varset_comp_sets';
  3517. lten,gten:
  3518. begin
  3519. procname := 'fpc_varset_contains_sets';
  3520. { (left >= right) = (right <= left) }
  3521. if nodetype = gten then
  3522. begin
  3523. tempn := left;
  3524. left := right;
  3525. right := tempn;
  3526. end;
  3527. end;
  3528. else
  3529. internalerror(2013112911);
  3530. end;
  3531. result := ccallnode.createinternres(procname,
  3532. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  3533. ccallparanode.create(right,
  3534. ccallparanode.create(left,nil))),resultdef);
  3535. { left and right are reused as parameters }
  3536. left := nil;
  3537. right := nil;
  3538. { for an unequaln, we have to negate the result of comp_sets }
  3539. if nodetype = unequaln then
  3540. result := cnotnode.create(result);
  3541. end;
  3542. addn:
  3543. begin
  3544. { can we directly write into the result? }
  3545. no_temp:=assigned(aktassignmentnode) and
  3546. (aktassignmentnode.right=self) and
  3547. (aktassignmentnode.left.resultdef=self.resultdef) and
  3548. valid_for_var(aktassignmentnode.left,false);
  3549. { optimize first loading of a set }
  3550. if (right.nodetype=setelementn) and
  3551. not(assigned(tsetelementnode(right).right)) and
  3552. is_emptyset(left) then
  3553. begin
  3554. { adjust for set base }
  3555. tsetelementnode(right).left:=caddnode.create(subn,
  3556. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  3557. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  3558. if no_temp then
  3559. begin
  3560. result:=ccallnode.createintern('fpc_varset_create_element',
  3561. ccallparanode.create(aktassignmentnode.left.getcopy,
  3562. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3563. ccallparanode.create(tsetelementnode(right).left,nil))));
  3564. include(aktassignmentnode.assignmentnodeflags,anf_assign_done_in_right);
  3565. end
  3566. else
  3567. begin
  3568. result:=internalstatements(newstatement);
  3569. { create temp for result }
  3570. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  3571. addstatement(newstatement,temp);
  3572. addstatement(newstatement,ccallnode.createintern('fpc_varset_create_element',
  3573. ccallparanode.create(ctemprefnode.create(temp),
  3574. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3575. ccallparanode.create(tsetelementnode(right).left,nil))))
  3576. );
  3577. { the last statement should return the value as
  3578. location and type, this is done be referencing the
  3579. temp and converting it first from a persistent temp to
  3580. normal temp }
  3581. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  3582. addstatement(newstatement,ctemprefnode.create(temp));
  3583. end;
  3584. tsetelementnode(right).left:=nil;
  3585. end
  3586. else
  3587. begin
  3588. if right.nodetype=setelementn then
  3589. begin
  3590. if no_temp then
  3591. begin
  3592. { add a range or a single element? }
  3593. if assigned(tsetelementnode(right).right) then
  3594. begin
  3595. { adjust for set base }
  3596. tsetelementnode(right).left:=caddnode.create(subn,
  3597. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  3598. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  3599. { adjust for set base }
  3600. tsetelementnode(right).right:=caddnode.create(subn,
  3601. ctypeconvnode.create_internal(tsetelementnode(right).right,sinttype),
  3602. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  3603. result:=ccallnode.createintern('fpc_varset_set_range',
  3604. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3605. ccallparanode.create(tsetelementnode(right).right,
  3606. ccallparanode.create(tsetelementnode(right).left,
  3607. ccallparanode.create(aktassignmentnode.left.getcopy,
  3608. ccallparanode.create(left,nil))))));
  3609. end
  3610. else
  3611. begin
  3612. { s:=s+[element]; ? }
  3613. if left.isequal(aktassignmentnode.left) then
  3614. result:=cinlinenode.createintern(in_include_x_y,false,ccallparanode.create(aktassignmentnode.left.getcopy,
  3615. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,tsetdef(resultdef).elementdef),nil)))
  3616. else
  3617. begin
  3618. { adjust for set base }
  3619. tsetelementnode(right).left:=caddnode.create(subn,
  3620. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  3621. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  3622. result:=ccallnode.createintern('fpc_varset_set',
  3623. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3624. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  3625. ccallparanode.create(aktassignmentnode.left.getcopy,
  3626. ccallparanode.create(left,nil)))));
  3627. end;
  3628. end;
  3629. include(aktassignmentnode.assignmentnodeflags,anf_assign_done_in_right);
  3630. end
  3631. else
  3632. begin
  3633. { adjust for set base }
  3634. tsetelementnode(right).left:=caddnode.create(subn,
  3635. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  3636. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  3637. result:=internalstatements(newstatement);
  3638. { create temp for result }
  3639. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  3640. addstatement(newstatement,temp);
  3641. { add a range or a single element? }
  3642. if assigned(tsetelementnode(right).right) then
  3643. begin
  3644. { adjust for set base }
  3645. tsetelementnode(right).right:=caddnode.create(subn,
  3646. ctypeconvnode.create_internal(tsetelementnode(right).right,sinttype),
  3647. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  3648. addstatement(newstatement,ccallnode.createintern('fpc_varset_set_range',
  3649. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3650. ccallparanode.create(tsetelementnode(right).right,
  3651. ccallparanode.create(tsetelementnode(right).left,
  3652. ccallparanode.create(ctemprefnode.create(temp),
  3653. ccallparanode.create(left,nil))))))
  3654. );
  3655. end
  3656. else
  3657. addstatement(newstatement,ccallnode.createintern('fpc_varset_set',
  3658. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3659. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  3660. ccallparanode.create(ctemprefnode.create(temp),
  3661. ccallparanode.create(left,nil)))))
  3662. );
  3663. { the last statement should return the value as
  3664. location and type, this is done be referencing the
  3665. temp and converting it first from a persistent temp to
  3666. normal temp }
  3667. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  3668. addstatement(newstatement,ctemprefnode.create(temp));
  3669. end;
  3670. { remove reused parts from original node }
  3671. tsetelementnode(right).right:=nil;
  3672. tsetelementnode(right).left:=nil;
  3673. left:=nil;
  3674. end
  3675. else
  3676. call_varset_helper('fpc_varset_add_sets');
  3677. end
  3678. end;
  3679. subn:
  3680. call_varset_helper('fpc_varset_sub_sets');
  3681. symdifn:
  3682. call_varset_helper('fpc_varset_symdif_sets');
  3683. muln:
  3684. call_varset_helper('fpc_varset_mul_sets');
  3685. else
  3686. internalerror(200609241);
  3687. end;
  3688. end;
  3689. function taddnode.first_adddynarray : tnode;
  3690. var
  3691. newstatement : tstatementnode;
  3692. tempnode (*,tempnode2*) : ttempcreatenode;
  3693. para: tcallparanode;
  3694. begin
  3695. result:=nil;
  3696. { when we get here, we are sure that both the left and the right }
  3697. { node are both strings of the same stringtype (JM) }
  3698. case nodetype of
  3699. addn:
  3700. begin
  3701. if (left.nodetype=arrayconstructorn) and (tarrayconstructornode(left).isempty) then
  3702. begin
  3703. result:=right;
  3704. left.free;
  3705. left:=nil;
  3706. right:=nil;
  3707. exit;
  3708. end;
  3709. if (right.nodetype=arrayconstructorn) and (tarrayconstructornode(right).isempty) then
  3710. begin
  3711. result:=left;
  3712. left:=nil;
  3713. right.free;
  3714. right:=nil;
  3715. exit;
  3716. end;
  3717. { create the call to the concat routine both strings as arguments }
  3718. if assigned(aktassignmentnode) and
  3719. (aktassignmentnode.right=self) and
  3720. (aktassignmentnode.left.resultdef=resultdef) and
  3721. valid_for_var(aktassignmentnode.left,false) then
  3722. begin
  3723. para:=ccallparanode.create(
  3724. ctypeconvnode.create_internal(right,voidcodepointertype),
  3725. ccallparanode.create(
  3726. ctypeconvnode.create_internal(left,voidcodepointertype),
  3727. ccallparanode.create(
  3728. caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  3729. ccallparanode.create(
  3730. ctypeconvnode.create_internal(aktassignmentnode.left.getcopy,voidcodepointertype),nil)
  3731. )));
  3732. result:=ccallnode.createintern(
  3733. 'fpc_dynarray_concat',
  3734. para
  3735. );
  3736. include(aktassignmentnode.assignmentnodeflags,anf_assign_done_in_right);
  3737. firstpass(result);
  3738. end
  3739. else
  3740. begin
  3741. result:=internalstatements(newstatement);
  3742. tempnode:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  3743. addstatement(newstatement,tempnode);
  3744. { initialize the temp, since it will be passed to a
  3745. var-parameter (and finalization, which is performed by the
  3746. ttempcreate node and which takes care of the initialization
  3747. on native targets, is a noop on managed VM targets) }
  3748. if (target_info.system in systems_managed_vm) and
  3749. is_managed_type(resultdef) then
  3750. addstatement(newstatement,cinlinenode.create(in_setlength_x,
  3751. false,
  3752. ccallparanode.create(genintconstnode(0),
  3753. ccallparanode.create(ctemprefnode.create(tempnode),nil))));
  3754. para:=ccallparanode.create(
  3755. ctypeconvnode.create_internal(right,voidcodepointertype),
  3756. ccallparanode.create(
  3757. ctypeconvnode.create_internal(left,voidcodepointertype),
  3758. ccallparanode.create(
  3759. caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  3760. ccallparanode.create(
  3761. ctypeconvnode.create_internal(ctemprefnode.create(tempnode),voidcodepointertype),nil)
  3762. )));
  3763. addstatement(
  3764. newstatement,
  3765. ccallnode.createintern(
  3766. 'fpc_dynarray_concat',
  3767. para
  3768. )
  3769. );
  3770. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  3771. addstatement(newstatement,ctemprefnode.create(tempnode));
  3772. end;
  3773. { we reused the arguments }
  3774. left := nil;
  3775. right := nil;
  3776. end;
  3777. unequaln,equaln:
  3778. expectloc:=LOC_FLAGS;
  3779. else
  3780. Internalerror(2018030301);
  3781. end;
  3782. end;
  3783. function taddnode.use_generic_mul32to64: boolean;
  3784. begin
  3785. result := true;
  3786. end;
  3787. function taddnode.use_generic_mul64bit: boolean;
  3788. begin
  3789. result := true;
  3790. end;
  3791. function taddnode.try_make_mul32to64: boolean;
  3792. function canbe32bitint(v: tconstexprint; out canbesignedconst, canbeunsignedconst: boolean): boolean;
  3793. begin
  3794. result := ((v >= int64(low(longint))) and (v <= int64(high(longint)))) or
  3795. ((v >= qword(low(cardinal))) and (v <= qword(high(cardinal))));
  3796. canbesignedconst:=v<=int64(high(longint));
  3797. canbeunsignedconst:=v>=0;
  3798. end;
  3799. function is_32bitordconst(n: tnode; out canbesignedconst, canbeunsignedconst: boolean): boolean;
  3800. begin
  3801. canbesignedconst:=false;
  3802. canbeunsignedconst:=false;
  3803. result := (n.nodetype = ordconstn) and
  3804. canbe32bitint(tordconstnode(n).value, canbesignedconst, canbeunsignedconst);
  3805. end;
  3806. function is_32to64typeconv(n: tnode): boolean;
  3807. begin
  3808. result := (n.nodetype = typeconvn) and
  3809. is_integer(ttypeconvnode(n).left.resultdef) and
  3810. not is_64bit(ttypeconvnode(n).left.resultdef);
  3811. end;
  3812. var
  3813. temp: tnode;
  3814. leftoriginallysigned,
  3815. canbesignedconst, canbeunsignedconst, swapped: boolean;
  3816. begin
  3817. result := false;
  3818. swapped := false;
  3819. { make sure that if there is a constant, that it's on the right }
  3820. if left.nodetype = ordconstn then
  3821. begin
  3822. swapleftright;
  3823. swapped := true;
  3824. end;
  3825. if is_32to64typeconv(left) then
  3826. begin
  3827. leftoriginallysigned:=is_signed(ttypeconvnode(left).left.resultdef);
  3828. if ((is_32bitordconst(right,canbesignedconst, canbeunsignedconst) and
  3829. ((leftoriginallysigned and canbesignedconst) or
  3830. (not leftoriginallysigned and canbeunsignedconst))) or
  3831. (is_32to64typeconv(right) and
  3832. ((leftoriginallysigned =
  3833. is_signed(ttypeconvnode(right).left.resultdef)) or
  3834. (leftoriginallysigned and
  3835. (torddef(ttypeconvnode(right).left.resultdef).ordtype in [u8bit,u16bit]))))) then
  3836. begin
  3837. temp := ttypeconvnode(left).left;
  3838. ttypeconvnode(left).left := nil;
  3839. left.free;
  3840. left := temp;
  3841. if (right.nodetype = typeconvn) then
  3842. begin
  3843. temp := ttypeconvnode(right).left;
  3844. ttypeconvnode(right).left := nil;
  3845. right.free;
  3846. right := temp;
  3847. end;
  3848. if (is_signed(left.resultdef)) then
  3849. begin
  3850. inserttypeconv_internal(left,s32inttype);
  3851. inserttypeconv_internal(right,s32inttype);
  3852. end
  3853. else
  3854. begin
  3855. inserttypeconv_internal(left,u32inttype);
  3856. inserttypeconv_internal(right,u32inttype);
  3857. end;
  3858. firstpass(left);
  3859. firstpass(right);
  3860. result := true;
  3861. end;
  3862. end;
  3863. { pass_Typecheck caches left/right type and resultdef, so restore the
  3864. original order }
  3865. if not result and swapped then
  3866. swapleftright;
  3867. end;
  3868. function taddnode.use_fma : boolean;
  3869. begin
  3870. result:=false;
  3871. end;
  3872. function taddnode.try_fma(ld,rd : tdef) : tnode;
  3873. var
  3874. inlinennr : tinlinenumber;
  3875. begin
  3876. result:=nil;
  3877. if (cs_opt_fastmath in current_settings.optimizerswitches) and
  3878. use_fma and
  3879. (nodetype in [addn,subn]) and
  3880. (rd.typ=floatdef) and (ld.typ=floatdef) and
  3881. (is_single(rd) or is_double(rd)) and
  3882. equal_defs(rd,ld) and
  3883. { transforming a*b+c into fma(a,b,c) makes only sense if c can be
  3884. calculated easily. Consider a*b+c*d which results in
  3885. fmul
  3886. fmul
  3887. fadd
  3888. and in
  3889. fmul
  3890. fma
  3891. when using the fma optimization. On a super scalar architecture, the first instruction
  3892. sequence requires clock_cycles(fmul)+clock_cycles(fadd) clock cycles because the fmuls can be executed in parallel.
  3893. The second sequence requires clock_cycles(fmul)+clock_cycles(fma) because the fma has to wait for the
  3894. result of the fmul. Since typically clock_cycles(fma)>clock_cycles(fadd) applies, the first sequence is better.
  3895. }
  3896. (((left.nodetype=muln) and (node_complexity(right)<3)) or
  3897. ((right.nodetype=muln) and (node_complexity(left)<3)) or
  3898. ((left.nodetype=inlinen) and
  3899. (tinlinenode(left).inlinenumber=in_sqr_real) and
  3900. (node_complexity(right)<3)) or
  3901. ((right.nodetype=inlinen) and
  3902. (tinlinenode(right).inlinenumber=in_sqr_real) and
  3903. (node_complexity(left)<3))
  3904. ) then
  3905. begin
  3906. case tfloatdef(ld).floattype of
  3907. s32real:
  3908. inlinennr:=in_fma_single;
  3909. s64real:
  3910. inlinennr:=in_fma_double;
  3911. s80real:
  3912. inlinennr:=in_fma_extended;
  3913. s128real:
  3914. inlinennr:=in_fma_float128;
  3915. else
  3916. internalerror(2014042601);
  3917. end;
  3918. if left.nodetype=muln then
  3919. begin
  3920. if nodetype=subn then
  3921. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(cunaryminusnode.create(right),
  3922. ccallparanode.create(taddnode(left).right,
  3923. ccallparanode.create(taddnode(left).left,nil
  3924. ))))
  3925. else
  3926. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(right,
  3927. ccallparanode.create(taddnode(left).right,
  3928. ccallparanode.create(taddnode(left).left,nil
  3929. ))));
  3930. right:=nil;
  3931. taddnode(left).right:=nil;
  3932. taddnode(left).left:=nil;
  3933. end
  3934. else if right.nodetype=muln then
  3935. begin
  3936. if nodetype=subn then
  3937. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  3938. ccallparanode.create(cunaryminusnode.create(taddnode(right).right),
  3939. ccallparanode.create(taddnode(right).left,nil
  3940. ))))
  3941. else
  3942. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  3943. ccallparanode.create(taddnode(right).right,
  3944. ccallparanode.create(taddnode(right).left,nil
  3945. ))));
  3946. left:=nil;
  3947. taddnode(right).right:=nil;
  3948. taddnode(right).left:=nil;
  3949. end
  3950. else if (left.nodetype=inlinen) and (tinlinenode(left).inlinenumber=in_sqr_real) then
  3951. begin
  3952. if node_complexity(tinlinenode(left).left)=0 then
  3953. begin
  3954. if nodetype=subn then
  3955. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(cunaryminusnode.create(right),
  3956. ccallparanode.create(tinlinenode(left).left.getcopy,
  3957. ccallparanode.create(tinlinenode(left).left.getcopy,nil
  3958. ))))
  3959. else
  3960. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(right,
  3961. ccallparanode.create(tinlinenode(left).left.getcopy,
  3962. ccallparanode.create(tinlinenode(left).left.getcopy,nil
  3963. ))));
  3964. right:=nil;
  3965. end;
  3966. end
  3967. { we get here only if right is a sqr node }
  3968. else if (right.nodetype=inlinen) and (tinlinenode(right).inlinenumber=in_sqr_real) then
  3969. begin
  3970. if node_complexity(tinlinenode(right).left)=0 then
  3971. begin
  3972. if nodetype=subn then
  3973. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  3974. ccallparanode.create(cunaryminusnode.create(tinlinenode(right).left.getcopy),
  3975. ccallparanode.create(tinlinenode(right).left.getcopy,nil
  3976. ))))
  3977. else
  3978. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  3979. ccallparanode.create(tinlinenode(right).left.getcopy,
  3980. ccallparanode.create(tinlinenode(right).left.getcopy,nil
  3981. ))));
  3982. left:=nil;
  3983. end;
  3984. end;
  3985. end;
  3986. end;
  3987. function taddnode.first_add64bitint: tnode;
  3988. var
  3989. procname: string[31];
  3990. power: longint;
  3991. begin
  3992. result := nil;
  3993. { create helper calls mul }
  3994. if nodetype <> muln then
  3995. exit;
  3996. { make sure that if there is a constant, that it's on the right }
  3997. if left.nodetype = ordconstn then
  3998. swapleftright;
  3999. { can we use a shift instead of a mul? }
  4000. if not (cs_check_overflow in current_settings.localswitches) and
  4001. (right.nodetype = ordconstn) and
  4002. ispowerof2(tordconstnode(right).value,power) then
  4003. begin
  4004. tordconstnode(right).value := power;
  4005. result := cshlshrnode.create(shln,left,right);
  4006. { left and right are reused }
  4007. left := nil;
  4008. right := nil;
  4009. { return firstpassed new node }
  4010. exit;
  4011. end;
  4012. if try_make_mul32to64 then
  4013. begin
  4014. { this uses the same criteria for signedness as the 32 to 64-bit mul
  4015. handling in the i386 code generator }
  4016. if is_signed(left.resultdef) and is_signed(right.resultdef) then
  4017. procname := 'fpc_mul_longint_to_int64'
  4018. else
  4019. procname := 'fpc_mul_dword_to_qword';
  4020. right := ccallparanode.create(right,ccallparanode.create(left,nil));
  4021. result := ccallnode.createintern(procname,right);
  4022. left := nil;
  4023. right := nil;
  4024. end
  4025. else
  4026. begin
  4027. { can full 64-bit multiplication be handled inline? }
  4028. if not use_generic_mul64bit then
  4029. begin
  4030. { generic handling replaces this node with call to fpc_mul_int64,
  4031. whose result is int64 }
  4032. if is_currency(resultdef) then
  4033. resultdef:=s64inttype;
  4034. exit;
  4035. end;
  4036. { when currency is used set the result of the
  4037. parameters to s64bit, so they are not converted }
  4038. if is_currency(resultdef) then
  4039. begin
  4040. left.resultdef:=s64inttype;
  4041. right.resultdef:=s64inttype;
  4042. end;
  4043. { otherwise, create the parameters for the helper }
  4044. right := ccallparanode.create(right,ccallparanode.create(left,nil));
  4045. left := nil;
  4046. { only qword needs the unsigned code, the
  4047. signed code is also used for currency }
  4048. if is_signed(resultdef) then
  4049. procname := 'fpc_mul_int64'
  4050. else
  4051. procname := 'fpc_mul_qword';
  4052. if cs_check_overflow in current_settings.localswitches then
  4053. procname := procname + '_checkoverflow';
  4054. result := ccallnode.createintern(procname,right);
  4055. right := nil;
  4056. end;
  4057. end;
  4058. function taddnode.first_addpointer: tnode;
  4059. begin
  4060. result:=nil;
  4061. expectloc:=LOC_REGISTER;
  4062. end;
  4063. function taddnode.first_cmppointer: tnode;
  4064. begin
  4065. result:=nil;
  4066. expectloc:=LOC_FLAGS;
  4067. end;
  4068. function taddnode.first_addfloat_soft : tnode;
  4069. var
  4070. procname: string[31];
  4071. { do we need to reverse the result ? }
  4072. notnode : boolean;
  4073. fdef : tdef;
  4074. begin
  4075. notnode:=false;
  4076. result:=nil;
  4077. fdef:=nil;
  4078. if not(target_info.system in systems_wince) then
  4079. begin
  4080. case tfloatdef(left.resultdef).floattype of
  4081. s32real:
  4082. begin
  4083. fdef:=search_system_type('FLOAT32REC').typedef;
  4084. procname:='float32';
  4085. end;
  4086. s64real:
  4087. begin
  4088. fdef:=search_system_type('FLOAT64').typedef;
  4089. procname:='float64';
  4090. end;
  4091. {!!! not yet implemented
  4092. s128real:
  4093. }
  4094. else
  4095. internalerror(2005082601);
  4096. end;
  4097. case nodetype of
  4098. addn:
  4099. procname:=procname+'_add';
  4100. muln:
  4101. procname:=procname+'_mul';
  4102. subn:
  4103. procname:=procname+'_sub';
  4104. slashn:
  4105. procname:=procname+'_div';
  4106. ltn:
  4107. procname:=procname+'_lt';
  4108. lten:
  4109. procname:=procname+'_le';
  4110. gtn:
  4111. begin
  4112. procname:=procname+'_lt';
  4113. swapleftright;
  4114. end;
  4115. gten:
  4116. begin
  4117. procname:=procname+'_le';
  4118. swapleftright;
  4119. end;
  4120. equaln:
  4121. procname:=procname+'_eq';
  4122. unequaln:
  4123. begin
  4124. procname:=procname+'_eq';
  4125. notnode:=true;
  4126. end;
  4127. else
  4128. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  4129. end;
  4130. end
  4131. else
  4132. begin
  4133. case nodetype of
  4134. addn:
  4135. procname:='add';
  4136. muln:
  4137. procname:='mul';
  4138. subn:
  4139. procname:='sub';
  4140. slashn:
  4141. procname:='div';
  4142. ltn:
  4143. procname:='lt';
  4144. lten:
  4145. procname:='le';
  4146. gtn:
  4147. procname:='gt';
  4148. gten:
  4149. procname:='ge';
  4150. equaln:
  4151. procname:='eq';
  4152. unequaln:
  4153. procname:='ne';
  4154. else
  4155. begin
  4156. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  4157. exit;
  4158. end;
  4159. end;
  4160. case tfloatdef(left.resultdef).floattype of
  4161. s32real:
  4162. begin
  4163. procname:=procname+'s';
  4164. if nodetype in [addn,muln,subn,slashn] then
  4165. procname:=lower(procname);
  4166. end;
  4167. s64real:
  4168. procname:=procname+'d';
  4169. {!!! not yet implemented
  4170. s128real:
  4171. }
  4172. else
  4173. internalerror(2005082602);
  4174. end;
  4175. end;
  4176. { cast softfpu result? }
  4177. if not(target_info.system in systems_wince) then
  4178. begin
  4179. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  4180. resultdef:=pasbool1type;
  4181. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  4182. ctypeconvnode.create_internal(right,fdef),
  4183. ccallparanode.create(
  4184. ctypeconvnode.create_internal(left,fdef),nil))),resultdef);
  4185. end
  4186. else
  4187. result:=ccallnode.createintern(procname,ccallparanode.create(right,
  4188. ccallparanode.create(left,nil)));
  4189. left:=nil;
  4190. right:=nil;
  4191. { do we need to reverse the result }
  4192. if notnode then
  4193. result:=cnotnode.create(result);
  4194. end;
  4195. function taddnode.first_addfloat : tnode;
  4196. begin
  4197. result := nil;
  4198. { In non-emulation mode, real opcodes are
  4199. emitted for floating point values.
  4200. }
  4201. if not ((cs_fp_emulation in current_settings.moduleswitches)
  4202. {$ifdef cpufpemu}
  4203. or (current_settings.fputype=fpu_soft)
  4204. {$endif cpufpemu}
  4205. ) then
  4206. exit;
  4207. result:=first_addfloat_soft
  4208. end;
  4209. {$ifdef cpuneedsmulhelper}
  4210. function taddnode.use_mul_helper: boolean;
  4211. begin
  4212. result:=(nodetype=muln) and
  4213. not(torddef(resultdef).ordtype in [u8bit,s8bit
  4214. {$if defined(cpu16bitalu) or defined(avr)},u16bit,s16bit{$endif}]);
  4215. end;
  4216. {$endif cpuneedsmulhelper}
  4217. function taddnode.pass_1 : tnode;
  4218. function isconstsetfewelements(p : tnode) : boolean;
  4219. begin
  4220. result:=(p.nodetype=setconstn) and (tsetconstnode(p).elements<=4);
  4221. end;
  4222. var
  4223. rd,ld : tdef;
  4224. i,i2 : longint;
  4225. lt,rt : tnodetype;
  4226. {$ifdef cpuneedsmulhelper}
  4227. procname : string[32];
  4228. {$endif cpuneedsmulhelper}
  4229. tempn,varsetnode: tnode;
  4230. mulnode : taddnode;
  4231. constsetnode : tsetconstnode;
  4232. trycreateinnodes : Boolean;
  4233. begin
  4234. result:=nil;
  4235. { Can we optimize multiple string additions into a single call?
  4236. This need to be done on a complete tree to detect the multiple
  4237. add nodes and is therefor done before the subtrees are processed }
  4238. if canbemultistringadd(self) then
  4239. begin
  4240. result:=genmultistringadd(self);
  4241. exit;
  4242. end;
  4243. { Can we optimize multiple dyn. array additions into a single call?
  4244. This need to be done on a complete tree to detect the multiple
  4245. add nodes and is therefor done before the subtrees are processed }
  4246. if (m_array_operators in current_settings.modeswitches) and canbemultidynarrayadd(self) then
  4247. begin
  4248. result:=genmultidynarrayadd(self);
  4249. exit;
  4250. end;
  4251. { typical set tests like (s*[const. set])<>/=[] can be converted into an or'ed chain of in tests
  4252. for var sets if const. set contains only a few elements }
  4253. if (cs_opt_level1 in current_settings.optimizerswitches) and (nodetype in [unequaln,equaln]) and (left.resultdef.typ=setdef) and not(is_smallset(left.resultdef)) then
  4254. begin
  4255. trycreateinnodes:=false;
  4256. mulnode:=nil;
  4257. if (is_emptyset(right) and (left.nodetype=muln) and
  4258. (isconstsetfewelements(taddnode(left).right) or isconstsetfewelements(taddnode(left).left))) then
  4259. begin
  4260. trycreateinnodes:=true;
  4261. mulnode:=taddnode(left);
  4262. end
  4263. else if (is_emptyset(left) and (right.nodetype=muln) and
  4264. (isconstsetfewelements(taddnode(right).right) or isconstsetfewelements(taddnode(right).left))) then
  4265. begin
  4266. trycreateinnodes:=true;
  4267. mulnode:=taddnode(right);
  4268. end;
  4269. if trycreateinnodes then
  4270. begin
  4271. constsetnode:=nil;
  4272. varsetnode:=nil;
  4273. if isconstsetfewelements(mulnode.right) then
  4274. begin
  4275. constsetnode:=tsetconstnode(mulnode.right);
  4276. varsetnode:=mulnode.left;
  4277. end
  4278. else
  4279. begin
  4280. constsetnode:=tsetconstnode(mulnode.left);
  4281. varsetnode:=mulnode.right;
  4282. end;
  4283. { the node is copied so it might have no side effects, if the complexity is too, cse should fix it, so
  4284. do not check complexity }
  4285. if not(might_have_sideeffects(varsetnode)) then
  4286. begin
  4287. result:=nil;
  4288. for i:=low(tconstset) to high(tconstset) do
  4289. if i in constsetnode.value_set^ then
  4290. begin
  4291. tempn:=cinnode.create(cordconstnode.create(i,tsetdef(constsetnode.resultdef).elementdef,false),varsetnode.getcopy);
  4292. if assigned(result) then
  4293. result:=caddnode.create_internal(orn,result,tempn)
  4294. else
  4295. result:=tempn;
  4296. end;
  4297. if nodetype=equaln then
  4298. result:=cnotnode.create(result);
  4299. exit;
  4300. end;
  4301. end;
  4302. end;
  4303. { get rid of adding empty sets generated by set constructors (s+([]+[..]))
  4304. this needs to be done before firstpass, else the set additions get already converted into calls }
  4305. if (resultdef.typ=setdef) and (nodetype=addn) and (right.nodetype=addn) and (is_emptyset(taddnode(right).left)) then
  4306. begin
  4307. result:=caddnode.create(addn,left,taddnode(right).right);
  4308. left:=nil;
  4309. taddnode(right).right:=nil;
  4310. exit;
  4311. end;
  4312. { first do the two subtrees }
  4313. firstpass(left);
  4314. firstpass(right);
  4315. if codegenerror then
  4316. exit;
  4317. { load easier access variables }
  4318. rd:=right.resultdef;
  4319. ld:=left.resultdef;
  4320. rt:=right.nodetype;
  4321. lt:=left.nodetype;
  4322. { int/int gives real/real! }
  4323. if nodetype=slashn then
  4324. begin
  4325. {$ifdef cpufpemu}
  4326. result:=first_addfloat;
  4327. if assigned(result) then
  4328. exit;
  4329. {$endif cpufpemu}
  4330. expectloc:=LOC_FPUREGISTER;
  4331. end
  4332. { if both are orddefs then check sub types }
  4333. else if (ld.typ=orddef) and (rd.typ=orddef) then
  4334. begin
  4335. { optimize multiplacation by a power of 2 }
  4336. if not(cs_check_overflow in current_settings.localswitches) and
  4337. (nodetype = muln) and
  4338. (((left.nodetype = ordconstn) and
  4339. ispowerof2(tordconstnode(left).value,i)) or
  4340. ((right.nodetype = ordconstn) and
  4341. ispowerof2(tordconstnode(right).value,i2))) then
  4342. begin
  4343. { it could be that we are converting a 32x32 -> 64 multiplication:
  4344. in this case, we have to restore the type conversion }
  4345. inserttypeconv_internal(left,resultdef);
  4346. inserttypeconv_internal(right,resultdef);
  4347. if ((left.nodetype = ordconstn) and
  4348. ispowerof2(tordconstnode(left).value,i)) then
  4349. begin
  4350. tordconstnode(left).value := i;
  4351. result := cshlshrnode.create(shln,right,left);
  4352. end
  4353. else
  4354. begin
  4355. tordconstnode(right).value := i2;
  4356. result := cshlshrnode.create(shln,left,right);
  4357. end;
  4358. result.resultdef := resultdef;
  4359. left := nil;
  4360. right := nil;
  4361. exit;
  4362. end;
  4363. { 2 booleans ? }
  4364. if is_boolean(ld) and is_boolean(rd) then
  4365. begin
  4366. if doshortbooleval(self) then
  4367. expectloc:=LOC_JUMP
  4368. else
  4369. begin
  4370. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  4371. expectloc:=LOC_FLAGS
  4372. else
  4373. expectloc:=LOC_REGISTER;
  4374. end;
  4375. end
  4376. else
  4377. { Both are chars? only convert to shortstrings for addn }
  4378. if is_char(ld) then
  4379. begin
  4380. if nodetype=addn then
  4381. internalerror(200103291);
  4382. expectloc:=LOC_FLAGS;
  4383. end
  4384. else if (nodetype=muln) and
  4385. is_64bitint(resultdef) and
  4386. not use_generic_mul32to64 and
  4387. try_make_mul32to64 then
  4388. begin
  4389. { if the code generator can handle 32 to 64-bit muls,
  4390. we're done here }
  4391. expectloc:=LOC_REGISTER;
  4392. end
  4393. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  4394. { is there a 64 bit type ? }
  4395. else if (torddef(ld).ordtype in [s64bit,u64bit,scurrency]) then
  4396. begin
  4397. result := first_add64bitint;
  4398. if assigned(result) then
  4399. exit;
  4400. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  4401. expectloc:=LOC_REGISTER
  4402. else
  4403. expectloc:=LOC_JUMP;
  4404. end
  4405. {$elseif defined(llvm) and defined(cpu32bitalu)}
  4406. { llvm does not support 128 bit math on 32 bit targets, which is
  4407. necessary for overflow checking 64 bit operations }
  4408. else if (torddef(ld).ordtype in [s64bit,u64bit,scurrency]) and
  4409. (cs_check_overflow in current_settings.localswitches) and
  4410. (nodetype in [addn,subn,muln]) then
  4411. begin
  4412. result := first_add64bitint;
  4413. if assigned(result) then
  4414. exit;
  4415. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  4416. expectloc:=LOC_REGISTER
  4417. else
  4418. expectloc:=LOC_JUMP;
  4419. end
  4420. {$endif not(cpu64bitalu) and not(cpuhighleveltarget)}
  4421. { generic 32bit conversion }
  4422. else
  4423. begin
  4424. {$ifdef cpuneedsmulhelper}
  4425. if use_mul_helper then
  4426. begin
  4427. result := nil;
  4428. case torddef(resultdef).ordtype of
  4429. s8bit:
  4430. procname := 'fpc_mul_shortint';
  4431. u8bit:
  4432. procname := 'fpc_mul_byte';
  4433. s16bit:
  4434. procname := 'fpc_mul_integer';
  4435. u16bit:
  4436. procname := 'fpc_mul_word';
  4437. s32bit:
  4438. procname := 'fpc_mul_longint';
  4439. u32bit:
  4440. procname := 'fpc_mul_dword';
  4441. else
  4442. internalerror(2011022301);
  4443. end;
  4444. if cs_check_overflow in current_settings.localswitches then
  4445. procname:=procname+'_checkoverflow';
  4446. result := ccallnode.createintern(procname,
  4447. ccallparanode.create(right,
  4448. ccallparanode.create(left,nil)));
  4449. left := nil;
  4450. right := nil;
  4451. firstpass(result);
  4452. exit;
  4453. end;
  4454. {$endif cpuneedsmulhelper}
  4455. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  4456. expectloc:=LOC_REGISTER
  4457. {$if not defined(cpuhighleveltarget)}
  4458. else if torddef(ld).size>sizeof(aint) then
  4459. expectloc:=LOC_JUMP
  4460. {$endif}
  4461. else
  4462. expectloc:=LOC_FLAGS;
  4463. end;
  4464. end
  4465. { left side a setdef, must be before string processing,
  4466. else array constructor can be seen as array of char (PFV) }
  4467. else if (ld.typ=setdef) then
  4468. begin
  4469. { small sets are handled inline by the compiler.
  4470. small set doesn't have support for adding ranges }
  4471. if is_smallset(ld) and
  4472. not(
  4473. (right.nodetype=setelementn) and
  4474. assigned(tsetelementnode(right).right)
  4475. ) then
  4476. begin
  4477. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  4478. expectloc:=LOC_FLAGS
  4479. else
  4480. expectloc:=LOC_REGISTER;
  4481. end
  4482. else
  4483. begin
  4484. result := first_addset;
  4485. if assigned(result) then
  4486. exit;
  4487. expectloc:=LOC_CREFERENCE;
  4488. end;
  4489. end
  4490. { compare pchar by addresses like BP/Delphi }
  4491. else if is_pchar(ld) then
  4492. begin
  4493. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  4494. result:=first_addpointer
  4495. else
  4496. result:=first_cmppointer;
  4497. end
  4498. { is one of the operands a string }
  4499. else if (ld.typ=stringdef) then
  4500. begin
  4501. if is_widestring(ld) then
  4502. begin
  4503. { this is only for add, the comparison is handled later }
  4504. expectloc:=LOC_REGISTER;
  4505. end
  4506. else if is_unicodestring(ld) then
  4507. begin
  4508. { this is only for add, the comparison is handled later }
  4509. expectloc:=LOC_REGISTER;
  4510. end
  4511. else if is_ansistring(ld) then
  4512. begin
  4513. { this is only for add, the comparison is handled later }
  4514. expectloc:=LOC_REGISTER;
  4515. end
  4516. else if is_longstring(ld) then
  4517. begin
  4518. { this is only for add, the comparison is handled later }
  4519. expectloc:=LOC_REFERENCE;
  4520. end
  4521. else
  4522. begin
  4523. {$ifdef addstringopt}
  4524. { can create a call which isn't handled by callparatemp }
  4525. if canbeaddsstringcharoptnode(self) then
  4526. begin
  4527. hp := genaddsstringcharoptnode(self);
  4528. pass_1 := hp;
  4529. exit;
  4530. end
  4531. else
  4532. {$endif addstringopt}
  4533. begin
  4534. { Fix right to be shortstring }
  4535. if is_char(right.resultdef) then
  4536. begin
  4537. inserttypeconv(right,cshortstringtype);
  4538. firstpass(right);
  4539. end;
  4540. end;
  4541. {$ifdef addstringopt}
  4542. { can create a call which isn't handled by callparatemp }
  4543. if canbeaddsstringcsstringoptnode(self) then
  4544. begin
  4545. hp := genaddsstringcsstringoptnode(self);
  4546. pass_1 := hp;
  4547. exit;
  4548. end;
  4549. {$endif addstringopt}
  4550. end;
  4551. { otherwise, let addstring convert everything }
  4552. result := first_addstring;
  4553. exit;
  4554. end
  4555. { is one a real float ? }
  4556. else if (rd.typ=floatdef) or (ld.typ=floatdef) then
  4557. begin
  4558. {$ifdef cpufpemu}
  4559. result:=first_addfloat;
  4560. if assigned(result) then
  4561. exit;
  4562. {$endif cpufpemu}
  4563. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  4564. expectloc:=LOC_FPUREGISTER
  4565. else
  4566. expectloc:=LOC_FLAGS;
  4567. result:=try_fma(ld,rd);
  4568. if assigned(result) then
  4569. exit;
  4570. end
  4571. { pointer comperation and subtraction }
  4572. else if (ld.typ=pointerdef) then
  4573. begin
  4574. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  4575. result:=first_addpointer
  4576. else
  4577. result:=first_cmppointer;
  4578. end
  4579. else if is_implicit_pointer_object_type(ld) then
  4580. begin
  4581. if ld.size>sizeof(aint) then
  4582. expectloc:=LOC_JUMP
  4583. else
  4584. expectloc:=LOC_FLAGS;
  4585. end
  4586. else if (ld.typ=classrefdef) then
  4587. begin
  4588. if ld.size>sizeof(aint) then
  4589. expectloc:=LOC_JUMP
  4590. else
  4591. expectloc:=LOC_FLAGS;
  4592. end
  4593. { support procvar=nil,procvar<>nil }
  4594. else if ((ld.typ=procvardef) and (rt=niln)) or
  4595. ((rd.typ=procvardef) and (lt=niln)) then
  4596. begin
  4597. if (ld.typ=procvardef) and (tprocvardef(ld).size>sizeof(aint)) or
  4598. (rd.typ=procvardef) and (tprocvardef(rd).size>sizeof(aint)) then
  4599. expectloc:=LOC_JUMP
  4600. else
  4601. expectloc:=LOC_FLAGS;
  4602. end
  4603. {$ifdef SUPPORT_MMX}
  4604. { mmx support, this must be before the zero based array
  4605. check }
  4606. else if (cs_mmx in current_settings.localswitches) and is_mmx_able_array(ld) and
  4607. is_mmx_able_array(rd) then
  4608. begin
  4609. expectloc:=LOC_MMXREGISTER;
  4610. end
  4611. {$endif SUPPORT_MMX}
  4612. else if (rd.typ=pointerdef) or (ld.typ=pointerdef) then
  4613. begin
  4614. result:=first_addpointer;
  4615. end
  4616. else if (rd.typ=procvardef) and
  4617. (ld.typ=procvardef) and
  4618. equal_defs(rd,ld) then
  4619. begin
  4620. if tprocvardef(ld).size>sizeof(aint) then
  4621. expectloc:=LOC_JUMP
  4622. else
  4623. expectloc:=LOC_FLAGS;
  4624. end
  4625. else if (ld.typ=enumdef) then
  4626. begin
  4627. if tenumdef(ld).size>sizeof(aint) then
  4628. expectloc:=LOC_JUMP
  4629. else
  4630. expectloc:=LOC_FLAGS;
  4631. end
  4632. {$ifdef SUPPORT_MMX}
  4633. else if (cs_mmx in current_settings.localswitches) and
  4634. is_mmx_able_array(ld) and
  4635. is_mmx_able_array(rd) then
  4636. begin
  4637. expectloc:=LOC_MMXREGISTER;
  4638. end
  4639. {$endif SUPPORT_MMX}
  4640. else if is_dynamic_array(ld) or is_dynamic_array(rd) then
  4641. begin
  4642. result:=first_adddynarray;
  4643. exit;
  4644. end
  4645. { the general solution is to convert to 32 bit int }
  4646. else
  4647. begin
  4648. expectloc:=LOC_REGISTER;
  4649. end;
  4650. end;
  4651. {$ifdef state_tracking}
  4652. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  4653. var factval:Tnode;
  4654. begin
  4655. track_state_pass:=false;
  4656. if left.track_state_pass(exec_known) then
  4657. begin
  4658. track_state_pass:=true;
  4659. left.resultdef:=nil;
  4660. do_typecheckpass(left);
  4661. end;
  4662. factval:=aktstate.find_fact(left);
  4663. if factval<>nil then
  4664. begin
  4665. track_state_pass:=true;
  4666. left.free;
  4667. left:=factval.getcopy;
  4668. end;
  4669. if right.track_state_pass(exec_known) then
  4670. begin
  4671. track_state_pass:=true;
  4672. right.resultdef:=nil;
  4673. do_typecheckpass(right);
  4674. end;
  4675. factval:=aktstate.find_fact(right);
  4676. if factval<>nil then
  4677. begin
  4678. track_state_pass:=true;
  4679. right.free;
  4680. right:=factval.getcopy;
  4681. end;
  4682. end;
  4683. {$endif}
  4684. {$ifdef DEBUG_NODE_XML}
  4685. procedure TAddNode.XMLPrintNodeInfo(var T: Text);
  4686. var
  4687. i: TAddNodeFlag;
  4688. First: Boolean;
  4689. begin
  4690. inherited XMLPrintNodeInfo(T);
  4691. First := True;
  4692. for i in addnodeflags do
  4693. begin
  4694. if First then
  4695. begin
  4696. Write(T, ' addnodeflags="', i);
  4697. First := False;
  4698. end
  4699. else
  4700. Write(T, ',', i)
  4701. end;
  4702. if not First then
  4703. Write(T, '"');
  4704. end;
  4705. {$endif DEBUG_NODE_XML}
  4706. end.