nadd.pas 206 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998
  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. {$ifndef cpu64bitalu}
  1732. and (ld.typ=orddef) and
  1733. not(torddef(ld).ordtype in [s64bit,u64bit,scurrency])
  1734. {$endif cpu64bitalu}
  1735. then
  1736. begin
  1737. if (left.nodetype=shrn) and (right.nodetype=shln) and
  1738. is_constintnode(tshlshrnode(left).right) and
  1739. is_constintnode(tshlshrnode(right).right) and
  1740. (tordconstnode(tshlshrnode(right).right).value>0) and
  1741. (tordconstnode(tshlshrnode(left).right).value>0) and
  1742. tshlshrnode(left).left.isequal(tshlshrnode(right).left) and
  1743. not(might_have_sideeffects(tshlshrnode(left).left)) then
  1744. begin
  1745. if (tordconstnode(tshlshrnode(left).right).value=
  1746. tshlshrnode(left).left.resultdef.size*8-tordconstnode(tshlshrnode(right).right).value) then
  1747. begin
  1748. result:=cinlinenode.create(in_ror_x_y,false,
  1749. ccallparanode.create(tshlshrnode(left).PruneKeepRight(),
  1750. ccallparanode.create(tshlshrnode(left).PruneKeepLeft(),nil)));
  1751. exit;
  1752. end
  1753. else if (tordconstnode(tshlshrnode(right).right).value=
  1754. tshlshrnode(left).left.resultdef.size*8-tordconstnode(tshlshrnode(left).right).value) then
  1755. begin
  1756. result:=cinlinenode.create(in_rol_x_y,false,
  1757. ccallparanode.create(tshlshrnode(right).PruneKeepRight(),
  1758. ccallparanode.create(tshlshrnode(left).PruneKeepLeft(),nil)));
  1759. exit;
  1760. end;
  1761. end;
  1762. if (left.nodetype=shln) and (right.nodetype=shrn) and
  1763. is_constintnode(tshlshrnode(left).right) and
  1764. is_constintnode(tshlshrnode(right).right) and
  1765. (tordconstnode(tshlshrnode(right).right).value>0) and
  1766. (tordconstnode(tshlshrnode(left).right).value>0) and
  1767. tshlshrnode(left).left.isequal(tshlshrnode(right).left) and
  1768. not(might_have_sideeffects(tshlshrnode(left).left)) then
  1769. begin
  1770. if (tordconstnode(tshlshrnode(left).right).value=
  1771. tshlshrnode(left).left.resultdef.size*8-tordconstnode(tshlshrnode(right).right).value)
  1772. then
  1773. begin
  1774. result:=cinlinenode.create(in_rol_x_y,false,
  1775. ccallparanode.create(tshlshrnode(left).PruneKeepRight(),
  1776. ccallparanode.create(tshlshrnode(left).PruneKeepLeft(),nil)));
  1777. exit;
  1778. end
  1779. else if (tordconstnode(tshlshrnode(right).right).value=
  1780. tshlshrnode(left).left.resultdef.size*8-tordconstnode(tshlshrnode(left).right).value)
  1781. then
  1782. begin
  1783. result:=cinlinenode.create(in_ror_x_y,false,
  1784. ccallparanode.create(tshlshrnode(right).PruneKeepRight(),
  1785. ccallparanode.create(tshlshrnode(left).PruneKeepLeft(),nil)));
  1786. exit;
  1787. end;
  1788. end;
  1789. end;
  1790. {$endif cpurox}
  1791. { optimize
  1792. (a and b) or (c and not(b))
  1793. into
  1794. c xor ((c xor a) and b)
  1795. }
  1796. if (nodetype=orn) and
  1797. (ld.typ=orddef) and
  1798. (left.nodetype=andn) and
  1799. (right.nodetype=andn) and
  1800. (not(is_boolean(resultdef)) or not(might_have_sideeffects(self,[mhs_exceptions])) or not(doshortbooleval(self))) and
  1801. { this test is not needed but it speeds up the test and allows to bail out early }
  1802. ((taddnode(left).left.nodetype=notn) or (taddnode(left).right.nodetype=notn) or
  1803. (taddnode(right).left.nodetype=notn) or (taddnode(right).right.nodetype=notn)
  1804. ) and
  1805. not(might_have_sideeffects(self)) then
  1806. begin
  1807. if MatchAndTransformNodesCommutative(taddnode(left).left,taddnode(left).right,taddnode(right).left,taddnode(right).right,
  1808. @IsAndOrAndNot,@TransformAndOrAndNot,Result) then
  1809. exit;
  1810. end;
  1811. { optimize tests for a single bit:
  1812. (a and one_bit_mask_const) = <> one_bit_mask_const
  1813. into
  1814. (a and one_bit_mask_const) <> = 0
  1815. }
  1816. if (nodetype in [equaln,unequaln]) then
  1817. begin
  1818. if (lt=andn) and (rt=ordconstn) then
  1819. begin
  1820. t:=left;
  1821. cr:=tordconstnode(right).value;
  1822. end
  1823. else
  1824. if (rt=andn) and (lt=ordconstn) then
  1825. begin
  1826. t:=right;
  1827. cr:=tordconstnode(left).value;
  1828. end
  1829. else
  1830. begin
  1831. t:=nil;
  1832. cr:=0;
  1833. end;
  1834. { using cr.uvalue is fine as we are interested only in the bit pattern }
  1835. if (t<>nil) and (PopCnt(cr.uvalue) = 1) then
  1836. begin
  1837. if is_constintnode(taddnode(t).left) then
  1838. vl:=taddnode(t).left
  1839. else
  1840. if is_constintnode(taddnode(t).right) then
  1841. vl:=taddnode(t).right
  1842. else
  1843. vl:=nil;
  1844. if (vl<>nil) and (tordconstnode(vl).value=cr) then
  1845. begin
  1846. if nodetype=equaln then
  1847. nt:=unequaln
  1848. else
  1849. nt:=equaln;
  1850. result:=caddnode.create(nt,t,cordconstnode.create(0,vl.resultdef,false));
  1851. Include(transientflags,tnf_do_not_execute);
  1852. if t=left then
  1853. left:=nil
  1854. else
  1855. right:=nil;
  1856. exit;
  1857. end;
  1858. end;
  1859. end;
  1860. end;
  1861. end;
  1862. function taddnode.dogetcopy: tnode;
  1863. var
  1864. n: taddnode;
  1865. begin
  1866. n:=taddnode(inherited dogetcopy);
  1867. n.addnodeflags:=addnodeflags;
  1868. n.resultrealdef:=resultrealdef;
  1869. result:=n;
  1870. end;
  1871. function taddnode.docompare(p: tnode): boolean;
  1872. begin
  1873. result:=
  1874. inherited docompare(p) and
  1875. equal_defs(taddnode(p).resultrealdef,resultrealdef);
  1876. end;
  1877. procedure taddnode.printnodedata(var t: text);
  1878. var
  1879. i: TAddNodeFlag;
  1880. first: Boolean;
  1881. begin
  1882. write(t,printnodeindention,'addnodeflags = [');
  1883. first:=true;
  1884. for i:=low(TAddNodeFlag) to high(TAddNodeFlag) do
  1885. if i in addnodeflags then
  1886. begin
  1887. if not(first) then
  1888. write(t,',')
  1889. else
  1890. first:=false;
  1891. write(t, i);
  1892. end;
  1893. writeln(t,']');
  1894. inherited printnodedata(t);
  1895. end;
  1896. function taddnode.pass_typecheck:tnode;
  1897. begin
  1898. { This function is small to keep the stack small for recursive of
  1899. large + operations }
  1900. typecheckpass(left);
  1901. typecheckpass(right);
  1902. result:=pass_typecheck_internal;
  1903. end;
  1904. function taddnode.pass_typecheck_internal:tnode;
  1905. var
  1906. hp : tnode;
  1907. rd,ld,nd : tdef;
  1908. hsym : tfieldvarsym;
  1909. llow,lhigh,
  1910. rlow,rhigh : tconstexprint;
  1911. strtype : tstringtype;
  1912. res,
  1913. b : boolean;
  1914. lt,rt : tnodetype;
  1915. ot : tnodetype;
  1916. {$ifdef state_tracking}
  1917. factval : Tnode;
  1918. change : boolean;
  1919. {$endif}
  1920. function maybe_cast_ordconst(var n: tnode; adef: tdef): boolean;
  1921. begin
  1922. result:=(tordconstnode(n).value>=torddef(adef).low) and
  1923. (tordconstnode(n).value<=torddef(adef).high);
  1924. if result then
  1925. inserttypeconv(n,adef);
  1926. end;
  1927. function maybe_convert_to_insert:tnode;
  1928. function element_count(arrconstr: tarrayconstructornode):asizeint;
  1929. begin
  1930. result:=0;
  1931. while assigned(arrconstr) do
  1932. begin
  1933. if arrconstr.nodetype=arrayconstructorrangen then
  1934. internalerror(2018052501);
  1935. inc(result);
  1936. arrconstr:=tarrayconstructornode(tarrayconstructornode(arrconstr).right);
  1937. end;
  1938. end;
  1939. var
  1940. elem : tnode;
  1941. para : tcallparanode;
  1942. isarrconstrl,
  1943. isarrconstrr : boolean;
  1944. index : asizeint;
  1945. begin
  1946. result:=nil;
  1947. isarrconstrl:=left.nodetype=arrayconstructorn;
  1948. isarrconstrr:=right.nodetype=arrayconstructorn;
  1949. if not assigned(aktassignmentnode) or
  1950. (aktassignmentnode.right<>self) or
  1951. not(
  1952. isarrconstrl or
  1953. isarrconstrr
  1954. ) or
  1955. not(
  1956. left.isequal(aktassignmentnode.left) or
  1957. right.isequal(aktassignmentnode.left)
  1958. ) or
  1959. not valid_for_var(aktassignmentnode.left,false) or
  1960. (isarrconstrl and (element_count(tarrayconstructornode(left))>1)) or
  1961. (isarrconstrr and (element_count(tarrayconstructornode(right))>1)) then
  1962. exit;
  1963. if isarrconstrl then
  1964. begin
  1965. index:=0;
  1966. elem:=tarrayconstructornode(left).left;
  1967. tarrayconstructornode(left).left:=nil;
  1968. end
  1969. else
  1970. begin
  1971. index:=high(asizeint);
  1972. elem:=tarrayconstructornode(right).left;
  1973. tarrayconstructornode(right).left:=nil;
  1974. end;
  1975. { we use the fact that insert() caps the index to avoid a copy }
  1976. para:=ccallparanode.create(
  1977. cordconstnode.create(index,sizesinttype,false),
  1978. ccallparanode.create(
  1979. aktassignmentnode.left.getcopy,
  1980. ccallparanode.create(
  1981. elem,nil)));
  1982. result:=cinlinenode.create(in_insert_x_y_z,false,para);
  1983. include(aktassignmentnode.assignmentnodeflags,anf_assign_done_in_right);
  1984. end;
  1985. begin
  1986. result:=nil;
  1987. rlow:=0;
  1988. llow:=0;
  1989. rhigh:=0;
  1990. lhigh:=0;
  1991. { avoid any problems with type parameters later on }
  1992. if is_typeparam(left.resultdef) or is_typeparam(right.resultdef) then
  1993. begin
  1994. resultdef:=cundefinedtype;
  1995. exit;
  1996. end;
  1997. { both left and right need to be valid }
  1998. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1999. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2000. if codegenerror then
  2001. exit;
  2002. { tp procvar support. Omit for converted assigned() nodes }
  2003. if not (nf_load_procvar in flags) then
  2004. begin
  2005. maybe_call_procvar(left,true);
  2006. maybe_call_procvar(right,true);
  2007. end
  2008. else
  2009. if not (nodetype in [equaln,unequaln]) then
  2010. InternalError(2013091601);
  2011. { allow operator overloading }
  2012. hp:=self;
  2013. if is_array_constructor(left.resultdef) or is_array_constructor(right.resultdef) then
  2014. begin
  2015. { check whether there is a suitable operator for the array constructor
  2016. (but only if the "+" array operator isn't used), if not fall back to sets }
  2017. if (
  2018. (nodetype<>addn) or
  2019. not (m_array_operators in current_settings.modeswitches) or
  2020. (is_array_constructor(left.resultdef) and not is_dynamic_array(right.resultdef)) or
  2021. (not is_dynamic_array(left.resultdef) and is_array_constructor(right.resultdef))
  2022. ) and
  2023. not isbinaryoverloaded(hp,[ocf_check_only]) then
  2024. begin
  2025. if is_array_constructor(left.resultdef) then
  2026. begin
  2027. arrayconstructor_to_set(left);
  2028. typecheckpass(left);
  2029. end;
  2030. if is_array_constructor(right.resultdef) then
  2031. begin
  2032. arrayconstructor_to_set(right);
  2033. typecheckpass(right);
  2034. end;
  2035. end;
  2036. end;
  2037. if is_dynamic_array(left.resultdef) and is_dynamic_array(right.resultdef) and
  2038. (nodetype=addn) and
  2039. (m_array_operators in current_settings.modeswitches) and
  2040. isbinaryoverloaded(hp,[ocf_check_non_overloadable,ocf_check_only]) then
  2041. message3(parser_w_operator_overloaded_hidden_3,left.resultdef.typename,arraytokeninfo[_PLUS].str,right.resultdef.typename);
  2042. if isbinaryoverloaded(hp,[]) then
  2043. begin
  2044. result:=hp;
  2045. exit;
  2046. end;
  2047. { Stop checking when an error was found in the operator checking }
  2048. if codegenerror then
  2049. begin
  2050. result:=cerrornode.create;
  2051. exit;
  2052. end;
  2053. { Kylix allows enum+ordconstn in an enum type declaration, we need to do
  2054. the conversion here before the constant folding }
  2055. if (m_delphi in current_settings.modeswitches) and
  2056. (blocktype in [bt_type,bt_const_type,bt_var_type]) then
  2057. begin
  2058. if (left.resultdef.typ=enumdef) and
  2059. (right.resultdef.typ=orddef) then
  2060. begin
  2061. { insert explicit typecast to default signed int }
  2062. left:=ctypeconvnode.create_internal(left,sinttype);
  2063. typecheckpass(left);
  2064. end
  2065. else
  2066. if (left.resultdef.typ=orddef) and
  2067. (right.resultdef.typ=enumdef) then
  2068. begin
  2069. { insert explicit typecast to default signed int }
  2070. right:=ctypeconvnode.create_internal(right,sinttype);
  2071. typecheckpass(right);
  2072. end;
  2073. end;
  2074. { is one a real float, then both need to be floats, this
  2075. need to be done before the constant folding so constant
  2076. operation on a float and int are also handled }
  2077. {$ifdef x86}
  2078. { use extended as default real type only when the x87 fpu is used }
  2079. {$if defined(i386) or defined(i8086)}
  2080. if not(current_settings.fputype=fpu_x87) then
  2081. resultrealdef:=s64floattype
  2082. else
  2083. resultrealdef:=pbestrealtype^;
  2084. {$endif i386 or i8086}
  2085. {$ifdef x86_64}
  2086. { x86-64 has no x87 only mode, so use always double as default }
  2087. resultrealdef:=s64floattype;
  2088. {$endif x86_6}
  2089. {$else not x86}
  2090. resultrealdef:=pbestrealtype^;
  2091. {$endif not x86}
  2092. if (right.resultdef.typ=floatdef) or (left.resultdef.typ=floatdef) then
  2093. begin
  2094. { when both floattypes are already equal then use that
  2095. floattype for results }
  2096. if (right.resultdef.typ=floatdef) and
  2097. (left.resultdef.typ=floatdef) and
  2098. (tfloatdef(left.resultdef).floattype=tfloatdef(right.resultdef).floattype) and
  2099. not(tfloatdef(left.resultdef).floattype in [s64comp,s64currency]) then
  2100. begin
  2101. if cs_excessprecision in current_settings.localswitches then
  2102. begin
  2103. resultrealdef:=pbestrealtype^;
  2104. inserttypeconv(right,resultrealdef);
  2105. inserttypeconv(left,resultrealdef);
  2106. end
  2107. else
  2108. resultrealdef:=left.resultdef
  2109. end
  2110. { when there is a currency type then use currency, but
  2111. only when currency is defined as float }
  2112. else
  2113. if (is_currency(right.resultdef) or
  2114. is_currency(left.resultdef)) and
  2115. ((s64currencytype.typ = floatdef) or
  2116. (nodetype <> slashn)) then
  2117. begin
  2118. resultrealdef:=s64currencytype;
  2119. inserttypeconv(right,resultrealdef);
  2120. inserttypeconv(left,resultrealdef);
  2121. end
  2122. else
  2123. begin
  2124. resultrealdef:=getbestreal(left.resultdef,right.resultdef);
  2125. inserttypeconv(right,resultrealdef);
  2126. inserttypeconv(left,resultrealdef);
  2127. end;
  2128. end;
  2129. { If both operands are constant and there is a unicodestring
  2130. or unicodestring then convert everything to unicodestring }
  2131. if is_constnode(right) and is_constnode(left) and
  2132. (is_unicodestring(right.resultdef) or
  2133. is_unicodestring(left.resultdef)) then
  2134. begin
  2135. inserttypeconv(right,cunicodestringtype);
  2136. inserttypeconv(left,cunicodestringtype);
  2137. end;
  2138. { If both operands are constant and there is a widechar
  2139. or widestring then convert everything to widestring. This
  2140. allows constant folding like char+widechar }
  2141. if is_constnode(right) and is_constnode(left) and
  2142. (is_widestring(right.resultdef) or
  2143. is_widestring(left.resultdef) or
  2144. is_widechar(right.resultdef) or
  2145. is_widechar(left.resultdef)) then
  2146. begin
  2147. inserttypeconv(right,cwidestringtype);
  2148. inserttypeconv(left,cwidestringtype);
  2149. end;
  2150. { load easier access variables }
  2151. rd:=right.resultdef;
  2152. ld:=left.resultdef;
  2153. rt:=right.nodetype;
  2154. lt:=left.nodetype;
  2155. { 4 character constant strings are compatible with orddef }
  2156. { in macpas mode (become cardinals) }
  2157. if (m_mac in current_settings.modeswitches) and
  2158. { only allow for comparisons, additions etc are }
  2159. { normally program errors }
  2160. (nodetype in [ltn,lten,gtn,gten,unequaln,equaln]) and
  2161. (((lt=stringconstn) and
  2162. (tstringconstnode(left).len=4) and
  2163. (rd.typ=orddef)) or
  2164. ((rt=stringconstn) and
  2165. (tstringconstnode(right).len=4) and
  2166. (ld.typ=orddef))) then
  2167. begin
  2168. if (rt=stringconstn) then
  2169. begin
  2170. inserttypeconv(right,u32inttype);
  2171. rt:=right.nodetype;
  2172. rd:=right.resultdef;
  2173. end
  2174. else
  2175. begin
  2176. inserttypeconv(left,u32inttype);
  2177. lt:=left.nodetype;
  2178. ld:=left.resultdef;
  2179. end;
  2180. end;
  2181. { but an int/int gives real/real! }
  2182. if (nodetype=slashn) and not(is_vector(left.resultdef)) and not(is_vector(right.resultdef)) then
  2183. begin
  2184. if is_currency(left.resultdef) and
  2185. is_currency(right.resultdef) then
  2186. { In case of currency, converting to float means dividing by 10000 }
  2187. { However, since this is already a division, both divisions by }
  2188. { 10000 are eliminated when we divide the results -> we can skip }
  2189. { them. }
  2190. if s64currencytype.typ = floatdef then
  2191. begin
  2192. { there's no s64comptype or so, how do we avoid the type conversion?
  2193. left.resultdef := s64comptype;
  2194. right.resultdef := s64comptype; }
  2195. end
  2196. else
  2197. begin
  2198. left.resultdef := s64inttype;
  2199. right.resultdef := s64inttype;
  2200. end;
  2201. if current_settings.fputype=fpu_none then
  2202. begin
  2203. Message(parser_e_unsupported_real);
  2204. result:=cerrornode.create;
  2205. exit;
  2206. end
  2207. else
  2208. begin
  2209. inserttypeconv(right,resultrealdef);
  2210. inserttypeconv(left,resultrealdef);
  2211. end;
  2212. end
  2213. { if both are orddefs then check sub types }
  2214. else if (ld.typ=orddef) and (rd.typ=orddef) then
  2215. begin
  2216. { set for & and | operations in macpas mode: they only work on }
  2217. { booleans, and always short circuit evaluation }
  2218. if (anf_short_bool in addnodeflags) then
  2219. begin
  2220. if not is_boolean(ld) then
  2221. begin
  2222. inserttypeconv(left,pasbool1type);
  2223. ld := left.resultdef;
  2224. end;
  2225. if not is_boolean(rd) then
  2226. begin
  2227. inserttypeconv(right,pasbool1type);
  2228. rd := right.resultdef;
  2229. end;
  2230. end;
  2231. { 2 booleans? }
  2232. if (is_boolean(ld) and is_boolean(rd)) then
  2233. begin
  2234. case nodetype of
  2235. xorn,
  2236. andn,
  2237. orn:
  2238. begin
  2239. { in case of xor or 'and' with cbool: convert both to Pascal bool and then
  2240. perform the xor/and to prevent issues with "longbool(1) and/xor
  2241. longbool(2)" }
  2242. if (is_cbool(ld) or is_cbool(rd)) and
  2243. (nodetype in [xorn,andn]) then
  2244. begin
  2245. resultdef:=nil;
  2246. if is_cbool(ld) then
  2247. begin
  2248. left:=ctypeconvnode.create(left,pasbool8type);
  2249. ttypeconvnode(left).convtype:=tc_bool_2_bool;
  2250. firstpass(left);
  2251. if not is_cbool(rd) or
  2252. (ld.size>=rd.size) then
  2253. resultdef:=ld;
  2254. end;
  2255. if is_cbool(rd) then
  2256. begin
  2257. right:=ctypeconvnode.Create(right,pasbool8type);
  2258. ttypeconvnode(right).convtype:=tc_bool_2_bool;
  2259. firstpass(right);
  2260. if not assigned(resultdef) then
  2261. resultdef:=rd;
  2262. end;
  2263. result:=ctypeconvnode.create_explicit(caddnode.create(nodetype,left,right),resultdef);
  2264. ttypeconvnode(result).convtype:=tc_bool_2_bool;
  2265. left:=nil;
  2266. right:=nil;
  2267. exit;
  2268. end;
  2269. { Make sides equal to the largest boolean }
  2270. if (torddef(left.resultdef).size>torddef(right.resultdef).size) or
  2271. (is_cbool(left.resultdef) and not is_cbool(right.resultdef)) then
  2272. begin
  2273. right:=ctypeconvnode.create_internal(right,left.resultdef);
  2274. ttypeconvnode(right).convtype:=tc_bool_2_bool;
  2275. typecheckpass(right);
  2276. end
  2277. else if (torddef(left.resultdef).size<torddef(right.resultdef).size) or
  2278. (not is_cbool(left.resultdef) and is_cbool(right.resultdef)) then
  2279. begin
  2280. left:=ctypeconvnode.create_internal(left,right.resultdef);
  2281. ttypeconvnode(left).convtype:=tc_bool_2_bool;
  2282. typecheckpass(left);
  2283. end;
  2284. end;
  2285. ltn,
  2286. lten,
  2287. gtn,
  2288. gten:
  2289. begin
  2290. { convert both to pasbool to perform the comparison (so
  2291. that longbool(4) = longbool(2), since both represent
  2292. "true" }
  2293. inserttypeconv(left,pasbool1type);
  2294. inserttypeconv(right,pasbool1type);
  2295. end;
  2296. unequaln,
  2297. equaln:
  2298. begin
  2299. { Remove any compares with constants }
  2300. if (left.nodetype=ordconstn) then
  2301. begin
  2302. hp:=right;
  2303. b:=(tordconstnode(left).value<>0);
  2304. ot:=nodetype;
  2305. right:=nil;
  2306. if (not(b) and (ot=equaln)) or
  2307. (b and (ot=unequaln)) then
  2308. begin
  2309. hp:=cnotnode.create(hp);
  2310. end;
  2311. result:=hp;
  2312. exit;
  2313. end;
  2314. if (right.nodetype=ordconstn) then
  2315. begin
  2316. hp:=left;
  2317. b:=(tordconstnode(right).value<>0);
  2318. ot:=nodetype;
  2319. left:=nil;
  2320. if (not(b) and (ot=equaln)) or
  2321. (b and (ot=unequaln)) then
  2322. begin
  2323. hp:=cnotnode.create(hp);
  2324. end;
  2325. result:=hp;
  2326. exit;
  2327. end;
  2328. { Delphi-compatibility: convert both to pasbool to
  2329. perform the equality comparison }
  2330. inserttypeconv(left,pasbool1type);
  2331. inserttypeconv(right,pasbool1type);
  2332. end;
  2333. else
  2334. begin
  2335. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2336. result:=cnothingnode.create;
  2337. exit;
  2338. end;
  2339. end;
  2340. end
  2341. { Both are chars? }
  2342. else if is_char(rd) and is_char(ld) then
  2343. begin
  2344. if nodetype=addn then
  2345. begin
  2346. resultdef:=cshortstringtype;
  2347. if not(is_constcharnode(left) and is_constcharnode(right)) then
  2348. begin
  2349. inserttypeconv(left,cshortstringtype);
  2350. {$ifdef addstringopt}
  2351. hp := genaddsstringcharoptnode(self);
  2352. result := hp;
  2353. exit;
  2354. {$endif addstringopt}
  2355. end
  2356. end
  2357. else if not(nodetype in [ltn,lten,gtn,gten,unequaln,equaln]) then
  2358. begin
  2359. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2360. result:=cnothingnode.create;
  2361. exit;
  2362. end;
  2363. end
  2364. { There is a widechar? }
  2365. else if is_widechar(rd) or is_widechar(ld) then
  2366. begin
  2367. { widechar+widechar gives unicodestring }
  2368. if nodetype=addn then
  2369. begin
  2370. inserttypeconv(left,cunicodestringtype);
  2371. if (torddef(rd).ordtype<>uwidechar) then
  2372. inserttypeconv(right,cwidechartype);
  2373. resultdef:=cunicodestringtype;
  2374. end
  2375. else
  2376. begin
  2377. if (torddef(ld).ordtype<>uwidechar) then
  2378. inserttypeconv(left,cwidechartype);
  2379. if (torddef(rd).ordtype<>uwidechar) then
  2380. inserttypeconv(right,cwidechartype);
  2381. end;
  2382. end
  2383. { is there a currency type ? }
  2384. else if ((torddef(rd).ordtype=scurrency) or (torddef(ld).ordtype=scurrency)) then
  2385. begin
  2386. if (torddef(ld).ordtype<>scurrency) then
  2387. inserttypeconv(left,s64currencytype);
  2388. if (torddef(rd).ordtype<>scurrency) then
  2389. inserttypeconv(right,s64currencytype);
  2390. end
  2391. { leave some constant integer expressions alone in case the
  2392. resultdef of the integer types doesn't influence the outcome,
  2393. because the forced type conversions below can otherwise result
  2394. in unexpected results (such as high(qword)<high(int64) returning
  2395. true because high(qword) gets converted to int64) }
  2396. else if is_integer(ld) and is_integer(rd) and
  2397. (lt=ordconstn) and (rt=ordconstn) and
  2398. (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) then
  2399. begin
  2400. end
  2401. { "and" does't care about the sign of integers }
  2402. { "xor", "or" and compares don't need extension to native int }
  2403. { size either as long as both values are signed or unsigned }
  2404. { "xor" and "or" also don't care about the sign if the values }
  2405. { occupy an entire register }
  2406. { don't do it if either type is 64 bit (except for "and"), }
  2407. { since in that case we can't safely find a "common" type }
  2408. else if is_integer(ld) and is_integer(rd) and
  2409. ((nodetype=andn) or
  2410. ((nodetype in [orn,xorn,equaln,unequaln,gtn,gten,ltn,lten]) and
  2411. not is_64bitint(ld) and not is_64bitint(rd) and
  2412. (is_signed(ld)=is_signed(rd)))) then
  2413. begin
  2414. { Delphi-compatible: prefer unsigned type for "and", when the
  2415. unsigned type is bigger than the signed one, and also bigger
  2416. than min(native_int, 32-bit) }
  2417. if (is_oversizedint(rd) or is_nativeint(rd) or is_32bitint(rd)) and
  2418. (rd.size>=ld.size) and
  2419. not is_signed(rd) and is_signed(ld) then
  2420. inserttypeconv_internal(left,rd)
  2421. else if (is_oversizedint(ld) or is_nativeint(ld) or is_32bitint(ld)) and
  2422. (ld.size>=rd.size) and
  2423. not is_signed(ld) and is_signed(rd) then
  2424. inserttypeconv_internal(right,ld)
  2425. else
  2426. begin
  2427. { not to left right.resultdef, because that may
  2428. cause a range error if left and right's def don't
  2429. completely overlap }
  2430. nd:=get_common_intdef(torddef(ld),torddef(rd),true);
  2431. inserttypeconv(left,nd);
  2432. inserttypeconv(right,nd);
  2433. end;
  2434. end
  2435. { don't extend (sign-mismatched) comparisons if either side is a constant
  2436. whose value is within range of opposite side }
  2437. else if is_integer(ld) and is_integer(rd) and
  2438. (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  2439. (is_signed(ld)<>is_signed(rd)) and
  2440. (
  2441. ((lt=ordconstn) and maybe_cast_ordconst(left,rd)) or
  2442. ((rt=ordconstn) and maybe_cast_ordconst(right,ld))
  2443. ) then
  2444. begin
  2445. { done here }
  2446. end
  2447. { is there a signed 64 bit type ? }
  2448. else if ((torddef(rd).ordtype=s64bit) or (torddef(ld).ordtype=s64bit)) then
  2449. begin
  2450. if (torddef(ld).ordtype<>s64bit) then
  2451. inserttypeconv(left,s64inttype);
  2452. if (torddef(rd).ordtype<>s64bit) then
  2453. inserttypeconv(right,s64inttype);
  2454. end
  2455. { is there a unsigned 64 bit type ? }
  2456. else if ((torddef(rd).ordtype=u64bit) or (torddef(ld).ordtype=u64bit)) then
  2457. begin
  2458. if (torddef(ld).ordtype<>u64bit) then
  2459. inserttypeconv(left,u64inttype);
  2460. if (torddef(rd).ordtype<>u64bit) then
  2461. inserttypeconv(right,u64inttype);
  2462. end
  2463. { is there a larger int? }
  2464. else if is_oversizedint(rd) or is_oversizedint(ld) then
  2465. begin
  2466. nd:=get_common_intdef(torddef(ld),torddef(rd),false);
  2467. inserttypeconv(right,nd);
  2468. inserttypeconv(left,nd);
  2469. end
  2470. { is there a native unsigned int? }
  2471. else if is_nativeuint(rd) or is_nativeuint(ld) then
  2472. begin
  2473. { convert positive constants to uinttype }
  2474. if (not is_nativeuint(ld)) and
  2475. is_constintnode(left) and
  2476. (tordconstnode(left).value >= 0) then
  2477. inserttypeconv(left,uinttype);
  2478. if (not is_nativeuint(rd)) and
  2479. is_constintnode(right) and
  2480. (tordconstnode(right).value >= 0) then
  2481. inserttypeconv(right,uinttype);
  2482. { when one of the operand is signed or the operation is subn then perform
  2483. the operation in a larger signed type, can't use rd/ld here because there
  2484. could be already typeconvs inserted.
  2485. This is compatible with the code below for other unsigned types (PFV) }
  2486. if is_signed(left.resultdef) or
  2487. is_signed(right.resultdef) or
  2488. ((nodetype=subn)
  2489. {$if defined(cpu8bitalu) or defined(cpu16bitalu)}
  2490. and not (m_tp7 in current_settings.modeswitches)
  2491. {$endif}
  2492. ) then
  2493. begin
  2494. if nodetype<>subn then
  2495. CGMessage(type_h_mixed_signed_unsigned);
  2496. { mark as internal in case added for a subn, so }
  2497. { ttypeconvnode.simplify can remove the larger }
  2498. { typecast again if semantically correct. Even }
  2499. { if we could detect that here already, we }
  2500. { mustn't do it here because that would change }
  2501. { overload choosing behaviour etc. The code in }
  2502. { ncnv.pas is run after that is already decided }
  2503. if (not is_signed(left.resultdef) and
  2504. not is_signed(right.resultdef)) or
  2505. (nodetype in [orn,xorn]) then
  2506. include(flags,nf_internal);
  2507. { get next larger signed int type }
  2508. nd:=get_common_intdef(torddef(sinttype),torddef(uinttype),false);
  2509. inserttypeconv(left,nd);
  2510. inserttypeconv(right,nd);
  2511. end
  2512. else
  2513. begin
  2514. if not is_nativeuint(left.resultdef) then
  2515. inserttypeconv(left,uinttype);
  2516. if not is_nativeuint(right.resultdef) then
  2517. inserttypeconv(right,uinttype);
  2518. end;
  2519. end
  2520. { generic ord conversion is sinttype }
  2521. else
  2522. begin
  2523. { When there is a signed type or there is a minus operation
  2524. or in TP mode for 16-bit CPUs
  2525. we convert to signed int. Otherwise (both are unsigned) we keep
  2526. the result also unsigned. This is compatible with Delphi (PFV) }
  2527. if is_signed(ld) or
  2528. is_signed(rd) or
  2529. (([m_iso,m_extpas]*current_settings.modeswitches)<>[]) or
  2530. {$if defined(cpu16bitalu)}
  2531. (m_tp7 in current_settings.modeswitches) or
  2532. {$endif}
  2533. (nodetype=subn) then
  2534. begin
  2535. inserttypeconv(right,sinttype);
  2536. inserttypeconv(left,sinttype);
  2537. end
  2538. else
  2539. begin
  2540. inserttypeconv(right,uinttype);
  2541. inserttypeconv(left,uinttype);
  2542. end;
  2543. end;
  2544. end
  2545. { if both are floatdefs, conversion is already done before constant folding }
  2546. else if (ld.typ=floatdef) then
  2547. begin
  2548. if not(nodetype in [addn,subn,muln,slashn,equaln,unequaln,ltn,lten,gtn,gten]) then
  2549. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2550. end
  2551. { left side a setdef, must be before string processing,
  2552. else array constructor can be seen as array of char (PFV) }
  2553. else if (ld.typ=setdef) then
  2554. begin
  2555. if not(nodetype in [addn,subn,symdifn,muln,equaln,unequaln,lten,gten]) then
  2556. CGMessage(type_e_set_operation_unknown);
  2557. { right must either be a set or a set element }
  2558. if (rd.typ<>setdef) and
  2559. (rt<>setelementn) then
  2560. CGMessage(type_e_mismatch)
  2561. { Make operands the same setdef. If one's elementtype fits }
  2562. { entirely inside the other's, pick the one with the largest }
  2563. { range. Otherwise create a new setdef with a range which }
  2564. { can contain both. }
  2565. else if not(equal_defs(ld,rd)) then
  2566. begin
  2567. { note: ld cannot be an empty set with elementdef=nil in }
  2568. { case right is not a set, arrayconstructor_to_set takes }
  2569. { care of that }
  2570. { 1: rd is a set with an assigned elementdef, and ld is }
  2571. { either an empty set without elementdef or a set whose }
  2572. { elementdef fits in rd's elementdef -> convert to rd }
  2573. if ((rd.typ=setdef) and
  2574. assigned(tsetdef(rd).elementdef) and
  2575. (not assigned(tsetdef(ld).elementdef) or
  2576. is_in_limit(ld,rd))) then
  2577. inserttypeconv(left,rd)
  2578. { 2: rd is either an empty set without elementdef or a set }
  2579. { whose elementdef fits in ld's elementdef, or a set }
  2580. { element whose def fits in ld's elementdef -> convert }
  2581. { to ld. ld's elementdef can't be nil here, is caught }
  2582. { previous case and "note:" above }
  2583. else if ((rd.typ=setdef) and
  2584. (not assigned(tsetdef(rd).elementdef) or
  2585. is_in_limit(rd,ld))) or
  2586. ((rd.typ<>setdef) and
  2587. is_in_limit(rd,tsetdef(ld).elementdef)) then
  2588. if (rd.typ=setdef) then
  2589. inserttypeconv(right,ld)
  2590. else
  2591. inserttypeconv(right,tsetdef(ld).elementdef)
  2592. { 3: otherwise create setdef which encompasses both, taking }
  2593. { into account empty sets without elementdef }
  2594. else
  2595. begin
  2596. if assigned(tsetdef(ld).elementdef) then
  2597. begin
  2598. llow:=tsetdef(ld).setbase;
  2599. lhigh:=tsetdef(ld).setmax;
  2600. end;
  2601. if (rd.typ=setdef) then
  2602. if assigned(tsetdef(rd).elementdef) then
  2603. begin
  2604. rlow:=tsetdef(rd).setbase;
  2605. rhigh:=tsetdef(rd).setmax;
  2606. end
  2607. else
  2608. begin
  2609. { ld's elementdef must have been valid }
  2610. rlow:=llow;
  2611. rhigh:=lhigh;
  2612. end
  2613. else
  2614. getrange(rd,rlow,rhigh);
  2615. if not assigned(tsetdef(ld).elementdef) then
  2616. begin
  2617. llow:=rlow;
  2618. lhigh:=rhigh;
  2619. end;
  2620. nd:=csetdef.create(tsetdef(ld).elementdef,min(llow,rlow).svalue,max(lhigh,rhigh).svalue,true);
  2621. inserttypeconv(left,nd);
  2622. if (rd.typ=setdef) then
  2623. inserttypeconv(right,nd)
  2624. else
  2625. inserttypeconv(right,tsetdef(nd).elementdef);
  2626. end;
  2627. end;
  2628. end
  2629. { pointer comparision and subtraction }
  2630. else if (
  2631. (rd.typ=pointerdef) and (ld.typ=pointerdef)
  2632. ) or
  2633. { compare/add pchar to variable (not stringconst) char arrays
  2634. by addresses like BP/Delphi }
  2635. (
  2636. (nodetype in [equaln,unequaln,subn,addn]) and
  2637. (
  2638. ((is_pchar(ld) or (lt=niln)) and is_chararray(rd) and (rt<>stringconstn)) or
  2639. ((is_pchar(rd) or (rt=niln)) and is_chararray(ld) and (lt<>stringconstn))
  2640. )
  2641. ) then
  2642. begin
  2643. { convert char array to pointer }
  2644. if is_chararray(rd) then
  2645. begin
  2646. inserttypeconv(right,charpointertype);
  2647. rd:=right.resultdef;
  2648. end
  2649. else if is_chararray(ld) then
  2650. begin
  2651. inserttypeconv(left,charpointertype);
  2652. ld:=left.resultdef;
  2653. end;
  2654. case nodetype of
  2655. equaln,unequaln :
  2656. begin
  2657. if is_voidpointer(right.resultdef) then
  2658. inserttypeconv(right,left.resultdef)
  2659. else if is_voidpointer(left.resultdef) then
  2660. inserttypeconv(left,right.resultdef)
  2661. else if not(equal_defs(ld,rd)) then
  2662. IncompatibleTypes(ld,rd);
  2663. { now that the type checking is done, convert both to charpointer, }
  2664. { because methodpointers are 8 bytes even though only the first 4 }
  2665. { bytes must be compared. This can happen here if we are in }
  2666. { TP/Delphi mode, because there @methodpointer = voidpointer (but }
  2667. { a voidpointer of 8 bytes). A conversion to voidpointer would be }
  2668. { optimized away, since the result already was a voidpointer, so }
  2669. { use a charpointer instead (JM) }
  2670. {$if defined(jvm)}
  2671. inserttypeconv_internal(left,java_jlobject);
  2672. inserttypeconv_internal(right,java_jlobject);
  2673. {$elseif defined(i8086)}
  2674. if is_hugepointer(left.resultdef) then
  2675. inserttypeconv_internal(left,charhugepointertype)
  2676. else if is_farpointer(left.resultdef) then
  2677. inserttypeconv_internal(left,charfarpointertype)
  2678. else
  2679. inserttypeconv_internal(left,charnearpointertype);
  2680. if is_hugepointer(right.resultdef) then
  2681. inserttypeconv_internal(right,charhugepointertype)
  2682. else if is_farpointer(right.resultdef) then
  2683. inserttypeconv_internal(right,charfarpointertype)
  2684. else
  2685. inserttypeconv_internal(right,charnearpointertype);
  2686. {$elseif defined(wasm)}
  2687. if is_wasm_reference_type(left.resultdef) then
  2688. inserttypeconv(right,left.resultdef)
  2689. else if is_wasm_reference_type(right.resultdef) then
  2690. inserttypeconv(left,right.resultdef)
  2691. else
  2692. begin
  2693. inserttypeconv_internal(left,charpointertype);
  2694. inserttypeconv_internal(right,charpointertype);
  2695. end;
  2696. {$else}
  2697. inserttypeconv_internal(left,charpointertype);
  2698. inserttypeconv_internal(right,charpointertype);
  2699. {$endif jvm}
  2700. end;
  2701. ltn,lten,gtn,gten:
  2702. begin
  2703. if (cs_extsyntax in current_settings.moduleswitches) or
  2704. (nf_internal in flags) then
  2705. begin
  2706. if is_voidpointer(right.resultdef) then
  2707. inserttypeconv(right,left.resultdef)
  2708. else if is_voidpointer(left.resultdef) then
  2709. inserttypeconv(left,right.resultdef)
  2710. else if not(equal_defs(ld,rd)) then
  2711. IncompatibleTypes(ld,rd);
  2712. end
  2713. else
  2714. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2715. end;
  2716. subn:
  2717. begin
  2718. if (cs_extsyntax in current_settings.moduleswitches) or
  2719. (nf_internal in flags) then
  2720. begin
  2721. if is_voidpointer(right.resultdef) then
  2722. begin
  2723. if is_big_untyped_addrnode(right) then
  2724. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  2725. inserttypeconv(right,left.resultdef)
  2726. end
  2727. else if is_voidpointer(left.resultdef) then
  2728. inserttypeconv(left,right.resultdef)
  2729. else if not(equal_defs(ld,rd)) then
  2730. IncompatibleTypes(ld,rd);
  2731. end
  2732. else
  2733. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2734. if not(anf_has_pointerdiv in addnodeflags) and
  2735. (tpointerdef(rd).pointeddef.size>1) then
  2736. begin
  2737. hp:=getcopy;
  2738. include(taddnode(hp).addnodeflags, anf_has_pointerdiv);
  2739. result:=cmoddivnode.create(divn,hp,
  2740. cordconstnode.create(tpointerdef(rd).pointeddef.size,tpointerdef(rd).pointer_subtraction_result_type,false));
  2741. end;
  2742. resultdef:=tpointerdef(rd).pointer_subtraction_result_type;
  2743. exit;
  2744. end;
  2745. else
  2746. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2747. end;
  2748. end
  2749. { is one of the operands a string?,
  2750. chararrays are also handled as strings (after conversion), also take
  2751. care of chararray+chararray and chararray+char.
  2752. Note: Must be done after pointerdef+pointerdef has been checked, else
  2753. pchar is converted to string }
  2754. else if (rd.typ=stringdef) or
  2755. (ld.typ=stringdef) or
  2756. { stringconstn's can be arraydefs }
  2757. (lt=stringconstn) or
  2758. (rt=stringconstn) or
  2759. ((is_pchar(rd) or is_chararray(rd) or is_char(rd) or is_open_chararray(rd) or
  2760. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd)) and
  2761. (is_pchar(ld) or is_chararray(ld) or is_char(ld) or is_open_chararray(ld) or
  2762. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld))) then
  2763. begin
  2764. if (nodetype in [addn,equaln,unequaln,lten,gten,ltn,gtn]) then
  2765. begin
  2766. { Is there a unicodestring? }
  2767. if is_unicodestring(rd) or is_unicodestring(ld) or
  2768. ((m_default_unicodestring in current_settings.modeswitches) and
  2769. (cs_refcountedstrings in current_settings.localswitches) and
  2770. (
  2771. is_pwidechar(rd) or is_widechararray(rd) or is_open_widechararray(rd) or (lt = stringconstn) or
  2772. is_pwidechar(ld) or is_widechararray(ld) or is_open_widechararray(ld) or (rt = stringconstn)
  2773. )
  2774. ) then
  2775. strtype:=st_unicodestring
  2776. else
  2777. { Is there a widestring? }
  2778. if is_widestring(rd) or is_widestring(ld) or
  2779. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd) or
  2780. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld) then
  2781. strtype:=st_widestring
  2782. else
  2783. if is_ansistring(rd) or is_ansistring(ld) or
  2784. ((cs_refcountedstrings in current_settings.localswitches) and
  2785. //todo: Move some of this to longstring's then they are implemented?
  2786. (
  2787. is_pchar(rd) or (is_chararray(rd) and (rd.size > 255)) or is_open_chararray(rd) or (lt = stringconstn) or
  2788. is_pchar(ld) or (is_chararray(ld) and (ld.size > 255)) or is_open_chararray(ld) or (rt = stringconstn)
  2789. )
  2790. ) then
  2791. strtype:=st_ansistring
  2792. else
  2793. if is_longstring(rd) or is_longstring(ld) then
  2794. strtype:=st_longstring
  2795. else
  2796. begin
  2797. { TODO: todo: add a warning/hint here if one converting a too large array}
  2798. { nodes is PChar, array [with size > 255] or OpenArrayOfChar.
  2799. Note: Delphi halts with error if "array [0..xx] of char"
  2800. is assigned to ShortString and string length is less
  2801. then array size }
  2802. strtype:= st_shortstring;
  2803. end;
  2804. // Now convert nodes to common string type
  2805. case strtype of
  2806. st_widestring :
  2807. begin
  2808. if not(is_widestring(rd)) then
  2809. inserttypeconv(right,cwidestringtype);
  2810. if not(is_widestring(ld)) then
  2811. inserttypeconv(left,cwidestringtype);
  2812. end;
  2813. st_unicodestring :
  2814. begin
  2815. if not(is_unicodestring(rd)) then
  2816. if not ((ld.size=0) and (nodetype in [equaln,unequaln])) then
  2817. inserttypeconv(right,cunicodestringtype);
  2818. if not(is_unicodestring(ld)) then
  2819. if not ((rd.size=0) and (nodetype in [equaln,unequaln])) then
  2820. inserttypeconv(left,cunicodestringtype);
  2821. end;
  2822. st_ansistring :
  2823. begin
  2824. { use same code page if possible (don't force same code
  2825. page in case both are ansistrings with code page <>
  2826. CP_NONE, since then data loss can occur: the ansistring
  2827. helpers will convert them at run time to an encoding
  2828. that can represent both encodings) }
  2829. if is_ansistring(ld) and
  2830. (tstringdef(ld).encoding<>0) and
  2831. (tstringdef(ld).encoding<>globals.CP_NONE) and
  2832. (not is_ansistring(rd) or
  2833. (tstringdef(rd).encoding=0) or
  2834. (tstringdef(rd).encoding=globals.CP_NONE)) then
  2835. inserttypeconv(right,ld)
  2836. else if is_ansistring(rd) and
  2837. (tstringdef(rd).encoding<>0) and
  2838. (tstringdef(rd).encoding<>globals.CP_NONE) and
  2839. (not is_ansistring(ld) or
  2840. (tstringdef(ld).encoding=0) or
  2841. (tstringdef(ld).encoding=globals.CP_NONE)) then
  2842. inserttypeconv(left,rd)
  2843. else
  2844. begin
  2845. if not is_ansistring(ld) then
  2846. inserttypeconv(left,getansistringdef);
  2847. if not is_ansistring(rd) then
  2848. inserttypeconv(right,getansistringdef);
  2849. end;
  2850. end;
  2851. st_longstring :
  2852. begin
  2853. if not(is_longstring(rd)) then
  2854. inserttypeconv(right,clongstringtype);
  2855. if not(is_longstring(ld)) then
  2856. inserttypeconv(left,clongstringtype);
  2857. end;
  2858. st_shortstring :
  2859. begin
  2860. if not(is_shortstring(ld)) then
  2861. inserttypeconv(left,cshortstringtype);
  2862. { don't convert char, that can be handled by the optimized node }
  2863. if not(is_shortstring(rd) or is_char(rd)) then
  2864. inserttypeconv(right,cshortstringtype);
  2865. end;
  2866. end;
  2867. end
  2868. else
  2869. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2870. end
  2871. { implicit pointer object type comparison }
  2872. else if is_implicit_pointer_object_type(rd) or is_implicit_pointer_object_type(ld) then
  2873. begin
  2874. if (nodetype in [equaln,unequaln]) then
  2875. begin
  2876. if is_implicit_pointer_object_type(rd) and is_implicit_pointer_object_type(ld) then
  2877. begin
  2878. if def_is_related(tobjectdef(rd),tobjectdef(ld)) then
  2879. inserttypeconv(right,left.resultdef)
  2880. else
  2881. inserttypeconv(left,right.resultdef);
  2882. end
  2883. else if is_implicit_pointer_object_type(rd) then
  2884. inserttypeconv(left,right.resultdef)
  2885. else
  2886. inserttypeconv(right,left.resultdef);
  2887. end
  2888. else
  2889. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2890. end
  2891. else if (rd.typ=classrefdef) and (ld.typ=classrefdef) then
  2892. begin
  2893. if (nodetype in [equaln,unequaln]) then
  2894. begin
  2895. if def_is_related(tobjectdef(tclassrefdef(rd).pointeddef),
  2896. tobjectdef(tclassrefdef(ld).pointeddef)) then
  2897. inserttypeconv(right,left.resultdef)
  2898. else
  2899. inserttypeconv(left,right.resultdef);
  2900. end
  2901. else
  2902. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2903. end
  2904. { allow comparison with nil pointer }
  2905. else if is_implicit_pointer_object_type(rd) or (rd.typ=classrefdef) then
  2906. begin
  2907. if (nodetype in [equaln,unequaln]) then
  2908. inserttypeconv(left,right.resultdef)
  2909. else
  2910. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2911. end
  2912. else if is_implicit_pointer_object_type(ld) or (ld.typ=classrefdef) then
  2913. begin
  2914. if (nodetype in [equaln,unequaln]) then
  2915. inserttypeconv(right,left.resultdef)
  2916. else
  2917. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2918. end
  2919. { support procvar=nil,procvar<>nil }
  2920. else if ((ld.typ=procvardef) and (rt=niln)) or
  2921. ((rd.typ=procvardef) and (lt=niln)) then
  2922. begin
  2923. if not(nodetype in [equaln,unequaln]) then
  2924. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2925. { find proc field in methodpointer record }
  2926. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  2927. if not assigned(hsym) then
  2928. internalerror(200412043);
  2929. { For methodpointers compare only tmethodpointer.proc }
  2930. if (rd.typ=procvardef) and
  2931. (not tprocvardef(rd).is_addressonly) then
  2932. begin
  2933. right:=csubscriptnode.create(
  2934. hsym,
  2935. ctypeconvnode.create_internal(right,methodpointertype));
  2936. typecheckpass(right);
  2937. end;
  2938. if (ld.typ=procvardef) and
  2939. (not tprocvardef(ld).is_addressonly) then
  2940. begin
  2941. left:=csubscriptnode.create(
  2942. hsym,
  2943. ctypeconvnode.create_internal(left,methodpointertype));
  2944. typecheckpass(left);
  2945. end;
  2946. if lt=niln then
  2947. inserttypeconv_explicit(left,right.resultdef)
  2948. else
  2949. inserttypeconv_explicit(right,left.resultdef)
  2950. end
  2951. { <dyn. array>+<dyn. array> ? }
  2952. else if (nodetype=addn) and (is_dynamic_array(ld) or is_dynamic_array(rd)) then
  2953. begin
  2954. { empty array to add? }
  2955. if (right.nodetype=arrayconstructorn) and (tarrayconstructornode(right).left=nil) then
  2956. begin
  2957. result:=left;
  2958. left:=nil;
  2959. exit;
  2960. end
  2961. else if (left.nodetype=arrayconstructorn) and (tarrayconstructornode(left).left=nil) then
  2962. begin
  2963. result:=right;
  2964. right:=nil;
  2965. exit;
  2966. end
  2967. else
  2968. begin
  2969. result:=maybe_convert_to_insert;
  2970. if assigned(result) then
  2971. exit;
  2972. if not(is_dynamic_array(ld)) then
  2973. inserttypeconv(left,rd);
  2974. if not(is_dynamic_array(rd)) then
  2975. inserttypeconv(right,ld);
  2976. end;
  2977. end
  2978. { support dynamicarray=nil,dynamicarray<>nil }
  2979. else if (is_dynamic_array(ld) and (rt=niln)) or
  2980. (is_dynamic_array(rd) and (lt=niln)) or
  2981. (is_dynamic_array(ld) and is_dynamic_array(rd)) then
  2982. begin
  2983. if not(nodetype in [equaln,unequaln]) then
  2984. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2985. if lt=niln then
  2986. inserttypeconv_explicit(left,right.resultdef)
  2987. else
  2988. inserttypeconv_explicit(right,left.resultdef)
  2989. end
  2990. {$ifdef SUPPORT_MMX}
  2991. { mmx support, this must be before the zero based array
  2992. check }
  2993. else if (cs_mmx in current_settings.localswitches) and
  2994. is_mmx_able_array(ld) and
  2995. is_mmx_able_array(rd) and
  2996. equal_defs(ld,rd) then
  2997. begin
  2998. case nodetype of
  2999. addn,subn,xorn,orn,andn:
  3000. ;
  3001. { mul is a little bit restricted }
  3002. muln:
  3003. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  3004. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3005. else
  3006. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3007. end;
  3008. end
  3009. {$endif SUPPORT_MMX}
  3010. { vector support, this must be before the zero based array
  3011. check }
  3012. else if (cs_support_vectors in current_settings.globalswitches) and
  3013. fits_in_mm_register(ld) and
  3014. fits_in_mm_register(rd) and
  3015. equal_defs(ld,rd) then
  3016. begin
  3017. if not(nodetype in [addn,subn,xorn,orn,andn,muln,slashn]) then
  3018. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3019. { both defs must be equal, so taking left or right as resultdef doesn't matter }
  3020. resultdef:=to_hwvectordef(left.resultdef,false);
  3021. end
  3022. { this is a little bit dangerous, also the left type }
  3023. { pointer to should be checked! This broke the mmx support }
  3024. else if (rd.typ=pointerdef) or
  3025. (is_zero_based_array(rd) and (rt<>stringconstn)) then
  3026. begin
  3027. if is_zero_based_array(rd) then
  3028. begin
  3029. resultdef:=cpointerdef.getreusable(tarraydef(rd).elementdef);
  3030. inserttypeconv(right,resultdef);
  3031. end
  3032. else
  3033. resultdef:=right.resultdef;
  3034. inserttypeconv(left,tpointerdef(right.resultdef).pointer_arithmetic_int_type);
  3035. if nodetype=addn then
  3036. begin
  3037. if (rt=niln) then
  3038. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,'NIL');
  3039. if (not(cs_extsyntax in current_settings.moduleswitches) and not(nf_internal in flags)) or
  3040. (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
  3041. not(cs_pointermath in current_settings.localswitches) and
  3042. not((rd.typ=pointerdef) and tpointerdef(rd).has_pointer_math)) then
  3043. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3044. if (rd.typ=pointerdef) and
  3045. (tpointerdef(rd).pointeddef.size>1) then
  3046. begin
  3047. left:=caddnode.create(muln,left,
  3048. cordconstnode.create(tpointerdef(rd).pointeddef.size,tpointerdef(right.resultdef).pointer_arithmetic_int_type,true));
  3049. typecheckpass(left);
  3050. end;
  3051. end
  3052. else
  3053. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3054. end
  3055. else if (ld.typ=pointerdef) or
  3056. (is_zero_based_array(ld) and (lt<>stringconstn)) then
  3057. begin
  3058. if is_zero_based_array(ld) then
  3059. begin
  3060. resultdef:=cpointerdef.getreusable(tarraydef(ld).elementdef);
  3061. inserttypeconv(left,resultdef);
  3062. end
  3063. else
  3064. resultdef:=left.resultdef;
  3065. inserttypeconv(right,tpointerdef(left.resultdef).pointer_arithmetic_int_type);
  3066. if nodetype in [addn,subn] then
  3067. begin
  3068. if (lt=niln) then
  3069. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),'NIL',rd.typename);
  3070. if (not(cs_extsyntax in current_settings.moduleswitches) and not(nf_internal in flags)) or
  3071. (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
  3072. not(cs_pointermath in current_settings.localswitches) and
  3073. not((ld.typ=pointerdef) and tpointerdef(ld).has_pointer_math)) then
  3074. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3075. if (ld.typ=pointerdef) then
  3076. begin
  3077. if is_big_untyped_addrnode(left) then
  3078. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  3079. if (tpointerdef(ld).pointeddef.size>1) then
  3080. begin
  3081. right:=caddnode.create(muln,right,
  3082. cordconstnode.create(tpointerdef(ld).pointeddef.size,tpointerdef(left.resultdef).pointer_arithmetic_int_type,true));
  3083. typecheckpass(right);
  3084. end
  3085. end else
  3086. if is_zero_based_array(ld) and
  3087. (tarraydef(ld).elementdef.size>1) then
  3088. begin
  3089. right:=caddnode.create(muln,right,
  3090. cordconstnode.create(tarraydef(ld).elementdef.size,tpointerdef(left.resultdef).pointer_arithmetic_int_type,true));
  3091. typecheckpass(right);
  3092. end;
  3093. end
  3094. else
  3095. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3096. end
  3097. else if (rd.typ=procvardef) and
  3098. (ld.typ=procvardef) and
  3099. equal_defs(rd,ld) then
  3100. begin
  3101. if (nodetype in [equaln,unequaln]) then
  3102. begin
  3103. if tprocvardef(rd).is_addressonly then
  3104. begin
  3105. inserttypeconv_internal(right,voidcodepointertype);
  3106. inserttypeconv_internal(left,voidcodepointertype);
  3107. end
  3108. else
  3109. begin
  3110. { find proc field in methodpointer record }
  3111. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  3112. if not assigned(hsym) then
  3113. internalerror(2004120405);
  3114. { Compare tmehodpointer(left).proc }
  3115. right:=csubscriptnode.create(
  3116. hsym,
  3117. ctypeconvnode.create_internal(right,methodpointertype));
  3118. typecheckpass(right);
  3119. left:=csubscriptnode.create(
  3120. hsym,
  3121. ctypeconvnode.create_internal(left,methodpointertype));
  3122. typecheckpass(left);
  3123. end;
  3124. end
  3125. else
  3126. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3127. end
  3128. { enums }
  3129. else if (ld.typ=enumdef) and (rd.typ=enumdef) then
  3130. begin
  3131. if allowenumop(nodetype) or (nf_internal in flags) then
  3132. inserttypeconv(right,left.resultdef)
  3133. else
  3134. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3135. end
  3136. { generic conversion, this is for error recovery }
  3137. else
  3138. begin
  3139. inserttypeconv(left,sinttype);
  3140. inserttypeconv(right,sinttype);
  3141. end;
  3142. if cmp_of_disjunct_ranges(res) and not(nf_internal in flags) then
  3143. begin
  3144. if res then
  3145. CGMessage(type_w_comparison_always_true)
  3146. else
  3147. CGMessage(type_w_comparison_always_false);
  3148. end;
  3149. { set resultdef if not already done }
  3150. if not assigned(resultdef) then
  3151. begin
  3152. case nodetype of
  3153. ltn,lten,gtn,gten,equaln,unequaln :
  3154. resultdef:=pasbool1type;
  3155. slashn :
  3156. resultdef:=resultrealdef;
  3157. addn:
  3158. begin
  3159. { for strings, return is always a 255 char string }
  3160. if is_shortstring(left.resultdef) then
  3161. resultdef:=cshortstringtype
  3162. else
  3163. { for ansistrings set resultdef to assignment left node
  3164. if it is an assignment and left node expects ansistring }
  3165. if is_ansistring(left.resultdef) and
  3166. assigned(aktassignmentnode) and
  3167. (aktassignmentnode.right=self) and
  3168. is_ansistring(aktassignmentnode.left.resultdef) then
  3169. resultdef:=aktassignmentnode.left.resultdef
  3170. else
  3171. resultdef:=left.resultdef;
  3172. end;
  3173. else
  3174. resultdef:=left.resultdef;
  3175. end;
  3176. end;
  3177. { when the result is currency we need some extra code for
  3178. multiplication and division. this should not be done when
  3179. the muln or slashn node is created internally }
  3180. if not(nf_is_currency in flags) and
  3181. is_currency(resultdef) then
  3182. begin
  3183. case nodetype of
  3184. slashn :
  3185. begin
  3186. { slashn will only work with floats }
  3187. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  3188. include(hp.flags,nf_is_currency);
  3189. result:=hp;
  3190. end;
  3191. muln :
  3192. begin
  3193. hp:=nil;
  3194. if s64currencytype.typ=floatdef then
  3195. begin
  3196. { if left is a currency integer constant, we can get rid of the factor 10000 }
  3197. { int64(...) causes a cast on currency, so it is the currency value multiplied by 10000 }
  3198. 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
  3199. begin
  3200. { trealconstnode expects that value_real and value_currency contain valid values }
  3201. {$ifdef FPC_CURRENCY_IS_INT64}
  3202. trealconstnode(left).value_currency:=pint64(@(trealconstnode(left).value_currency))^ div 10000;
  3203. {$else}
  3204. trealconstnode(left).value_currency:=trealconstnode(left).value_currency / 10000;
  3205. {$endif}
  3206. trealconstnode(left).value_real:=trealconstnode(left).value_real/10000;
  3207. end
  3208. { or if right is an integer constant, we can get rid of its factor 10000 }
  3209. 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
  3210. begin
  3211. { trealconstnode expects that value and value_currency contain valid values }
  3212. {$ifdef FPC_CURRENCY_IS_INT64}
  3213. trealconstnode(right).value_currency:=pint64(@(trealconstnode(right).value_currency))^ div 10000;
  3214. {$else}
  3215. trealconstnode(right).value_currency:=trealconstnode(right).value_currency / 10000;
  3216. {$endif}
  3217. trealconstnode(right).value_real:=trealconstnode(right).value_real/10000;
  3218. end
  3219. else
  3220. begin
  3221. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype));
  3222. include(hp.flags,nf_is_currency);
  3223. end;
  3224. end
  3225. else
  3226. begin
  3227. { if left is a currency integer constant, we can get rid of the factor 10000 }
  3228. if (left.nodetype=ordconstn) and (is_currency(left.resultdef)) and ((tordconstnode(left).value mod 10000)=0) then
  3229. tordconstnode(left).value:=tordconstnode(left).value div 10000
  3230. { or if right is an integer constant, we can get rid of its factor 10000 }
  3231. else if (right.nodetype=ordconstn) and (is_currency(right.resultdef)) and ((tordconstnode(right).value mod 10000)=0) then
  3232. tordconstnode(right).value:=tordconstnode(right).value div 10000
  3233. else if (right.nodetype=muln) and is_currency(right.resultdef) and
  3234. { do not test swapped here as the internal conversions are only create as "var."*"10000" }
  3235. is_currency(taddnode(right).right.resultdef) and (taddnode(right).right.nodetype=ordconstn) and (tordconstnode(taddnode(right).right).value=10000) and
  3236. is_currency(taddnode(right).left.resultdef) and (taddnode(right).left.nodetype=typeconvn) then
  3237. begin
  3238. hp:=taddnode(right).left.getcopy;
  3239. include(hp.flags,nf_is_currency);
  3240. right.free;
  3241. right:=hp;
  3242. hp:=nil;
  3243. end
  3244. else if (left.nodetype=muln) and is_currency(left.resultdef) and
  3245. { do not test swapped here as the internal conversions are only create as "var."*"10000" }
  3246. is_currency(taddnode(left).right.resultdef) and (taddnode(left).right.nodetype=ordconstn) and (tordconstnode(taddnode(left).right).value=10000) and
  3247. is_currency(taddnode(left).left.resultdef) and (taddnode(left).left.nodetype=typeconvn) then
  3248. begin
  3249. hp:=taddnode(left).left.getcopy;
  3250. include(hp.flags,nf_is_currency);
  3251. left.free;
  3252. left:=hp;
  3253. hp:=nil;
  3254. end
  3255. else
  3256. begin
  3257. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  3258. include(hp.flags,nf_is_currency);
  3259. end
  3260. end;
  3261. result:=hp
  3262. end;
  3263. else
  3264. ;
  3265. end;
  3266. end;
  3267. if not(codegenerror) and
  3268. not assigned(result) then
  3269. result:=simplify(false);
  3270. end;
  3271. function taddnode.first_addstring: tnode;
  3272. var
  3273. p: tnode;
  3274. newstatement : tstatementnode;
  3275. tempnode : ttempcreatenode;
  3276. cmpfuncname: string;
  3277. para: tcallparanode;
  3278. begin
  3279. result:=nil;
  3280. { when we get here, we are sure that both the left and the right }
  3281. { node are both strings of the same stringtype (JM) }
  3282. case nodetype of
  3283. addn:
  3284. begin
  3285. if (left.nodetype=stringconstn) and (tstringconstnode(left).len=0) then
  3286. begin
  3287. result:=right;
  3288. left.free;
  3289. left:=nil;
  3290. right:=nil;
  3291. exit;
  3292. end;
  3293. if (right.nodetype=stringconstn) and (tstringconstnode(right).len=0) then
  3294. begin
  3295. result:=left;
  3296. left:=nil;
  3297. right.free;
  3298. right:=nil;
  3299. exit;
  3300. end;
  3301. { create the call to the concat routine both strings as arguments }
  3302. if assigned(aktassignmentnode) and
  3303. (aktassignmentnode.right=self) and
  3304. (
  3305. (aktassignmentnode.left.resultdef=resultdef) or
  3306. (
  3307. is_shortstring(aktassignmentnode.left.resultdef) and
  3308. is_shortstring(resultdef)
  3309. )
  3310. ) and
  3311. valid_for_var(aktassignmentnode.left,false) then
  3312. begin
  3313. para:=ccallparanode.create(
  3314. right,
  3315. ccallparanode.create(
  3316. left,
  3317. ccallparanode.create(aktassignmentnode.left.getcopy,nil)
  3318. )
  3319. );
  3320. if is_ansistring(resultdef) then
  3321. para:=ccallparanode.create(
  3322. cordconstnode.create(
  3323. { don't use getparaencoding(), we have to know
  3324. when the result is rawbytestring }
  3325. tstringdef(resultdef).encoding,
  3326. u16inttype,
  3327. true
  3328. ),
  3329. para
  3330. );
  3331. result:=ccallnode.createintern(
  3332. 'fpc_'+tstringdef(resultdef).stringtypname+'_concat',
  3333. para
  3334. );
  3335. include(aktassignmentnode.assignmentnodeflags,anf_assign_done_in_right);
  3336. firstpass(result);
  3337. end
  3338. else
  3339. begin
  3340. result:=internalstatements(newstatement);
  3341. tempnode:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  3342. addstatement(newstatement,tempnode);
  3343. { initialize the temp, since it will be passed to a
  3344. var-parameter (and finalization, which is performed by the
  3345. ttempcreate node and which takes care of the initialization
  3346. on native targets, is a noop on managed VM targets) }
  3347. if (target_info.system in systems_managed_vm) and
  3348. is_managed_type(resultdef) then
  3349. addstatement(newstatement,cinlinenode.create(in_setlength_x,
  3350. false,
  3351. ccallparanode.create(genintconstnode(0),
  3352. ccallparanode.create(ctemprefnode.create(tempnode),nil))));
  3353. para:=ccallparanode.create(
  3354. right,
  3355. ccallparanode.create(
  3356. left,
  3357. ccallparanode.create(ctemprefnode.create(tempnode),nil)
  3358. )
  3359. );
  3360. if is_ansistring(resultdef) then
  3361. para:=ccallparanode.create(
  3362. cordconstnode.create(
  3363. { don't use getparaencoding(), we have to know
  3364. when the result is rawbytestring }
  3365. tstringdef(resultdef).encoding,
  3366. u16inttype,
  3367. true
  3368. ),
  3369. para
  3370. );
  3371. addstatement(
  3372. newstatement,
  3373. ccallnode.createintern(
  3374. 'fpc_'+tstringdef(resultdef).stringtypname+'_concat',
  3375. para
  3376. )
  3377. );
  3378. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  3379. addstatement(newstatement,ctemprefnode.create(tempnode));
  3380. end;
  3381. { we reused the arguments }
  3382. left := nil;
  3383. right := nil;
  3384. end;
  3385. ltn,lten,gtn,gten,equaln,unequaln :
  3386. begin
  3387. { generate better code for comparison with empty string, we
  3388. only need to compare the length with 0 }
  3389. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  3390. (((left.nodetype=stringconstn) and (tstringconstnode(left).len=0)) or
  3391. ((right.nodetype=stringconstn) and (tstringconstnode(right).len=0))) then
  3392. begin
  3393. { switch so that the constant is always on the right }
  3394. if left.nodetype = stringconstn then
  3395. begin
  3396. p := left;
  3397. left := right;
  3398. right := p;
  3399. nodetype:=swap_relation[nodetype];
  3400. end;
  3401. if is_shortstring(left.resultdef) or
  3402. (nodetype in [gtn,gten,ltn,lten]) or
  3403. (target_info.system in systems_managed_vm) then
  3404. { compare the length with 0 }
  3405. result := caddnode.create(nodetype,
  3406. cinlinenode.create(in_length_x,false,left),
  3407. cordconstnode.create(0,s8inttype,false))
  3408. else { nodetype in [equaln,unequaln] }
  3409. begin
  3410. if is_widestring(left.resultdef) and (tf_winlikewidestring in target_info.flags) then
  3411. begin
  3412. { windows like widestrings requires that we also check the length }
  3413. result:=cinlinenode.create(in_length_x,false,left);
  3414. { and compare its result with 0 }
  3415. result:=caddnode.create(equaln,result,cordconstnode.create(0,s8inttype,false));
  3416. if nodetype=unequaln then
  3417. result:=cnotnode.create(result);
  3418. end
  3419. else
  3420. begin
  3421. { compare the pointer with nil (for ansistrings etc), }
  3422. { faster than getting the length (JM) }
  3423. result:= caddnode.create(nodetype,
  3424. ctypeconvnode.create_internal(left,voidpointertype),
  3425. cpointerconstnode.create(0,voidpointertype));
  3426. end;
  3427. end;
  3428. { left is reused }
  3429. left := nil;
  3430. { right isn't }
  3431. right.free;
  3432. right := nil;
  3433. exit;
  3434. end;
  3435. { no string constant -> call compare routine }
  3436. cmpfuncname := 'fpc_'+tstringdef(left.resultdef).stringtypname+'_compare';
  3437. { for equality checks use optimized version }
  3438. if nodetype in [equaln,unequaln] then
  3439. cmpfuncname := cmpfuncname + '_equal';
  3440. result := ccallnode.createintern(cmpfuncname,
  3441. ccallparanode.create(right,ccallparanode.create(left,nil)));
  3442. { and compare its result with 0 according to the original operator }
  3443. result := caddnode.create(nodetype,result,
  3444. cordconstnode.create(0,s8inttype,false));
  3445. left := nil;
  3446. right := nil;
  3447. end;
  3448. else
  3449. internalerror(2019050520);
  3450. end;
  3451. end;
  3452. function taddnode.first_addset : tnode;
  3453. procedure call_varset_helper(const n : string);
  3454. var
  3455. newstatement : tstatementnode;
  3456. temp : ttempcreatenode;
  3457. begin
  3458. { directly load the result set into the assignee if possible }
  3459. if assigned(aktassignmentnode) and
  3460. (aktassignmentnode.right=self) and
  3461. (aktassignmentnode.left.resultdef=resultdef) and
  3462. valid_for_var(aktassignmentnode.left,false) then
  3463. begin
  3464. result:=ccallnode.createintern(n,
  3465. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3466. ccallparanode.create(aktassignmentnode.left.getcopy,
  3467. ccallparanode.create(right,
  3468. ccallparanode.create(left,nil))))
  3469. );
  3470. { remove reused parts from original node }
  3471. left:=nil;
  3472. right:=nil;
  3473. include(aktassignmentnode.assignmentnodeflags,anf_assign_done_in_right);
  3474. firstpass(result);
  3475. end
  3476. else
  3477. begin
  3478. { add two var sets }
  3479. result:=internalstatements(newstatement);
  3480. { create temp for result }
  3481. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  3482. addstatement(newstatement,temp);
  3483. addstatement(newstatement,ccallnode.createintern(n,
  3484. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3485. ccallparanode.create(ctemprefnode.create(temp),
  3486. ccallparanode.create(right,
  3487. ccallparanode.create(left,nil)))))
  3488. );
  3489. { remove reused parts from original node }
  3490. left:=nil;
  3491. right:=nil;
  3492. { the last statement should return the value as
  3493. location and type, this is done be referencing the
  3494. temp and converting it first from a persistent temp to
  3495. normal temp }
  3496. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  3497. addstatement(newstatement,ctemprefnode.create(temp));
  3498. end;
  3499. end;
  3500. var
  3501. procname: string[31];
  3502. tempn: tnode;
  3503. newstatement : tstatementnode;
  3504. temp : ttempcreatenode;
  3505. no_temp: Boolean;
  3506. begin
  3507. result:=nil;
  3508. case nodetype of
  3509. equaln,unequaln,lten,gten:
  3510. begin
  3511. case nodetype of
  3512. equaln,unequaln:
  3513. procname := 'fpc_varset_comp_sets';
  3514. lten,gten:
  3515. begin
  3516. procname := 'fpc_varset_contains_sets';
  3517. { (left >= right) = (right <= left) }
  3518. if nodetype = gten then
  3519. begin
  3520. tempn := left;
  3521. left := right;
  3522. right := tempn;
  3523. end;
  3524. end;
  3525. else
  3526. internalerror(2013112911);
  3527. end;
  3528. result := ccallnode.createinternres(procname,
  3529. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  3530. ccallparanode.create(right,
  3531. ccallparanode.create(left,nil))),resultdef);
  3532. { left and right are reused as parameters }
  3533. left := nil;
  3534. right := nil;
  3535. { for an unequaln, we have to negate the result of comp_sets }
  3536. if nodetype = unequaln then
  3537. result := cnotnode.create(result);
  3538. end;
  3539. addn:
  3540. begin
  3541. { can we directly write into the result? }
  3542. no_temp:=assigned(aktassignmentnode) and
  3543. (aktassignmentnode.right=self) and
  3544. (aktassignmentnode.left.resultdef=self.resultdef) and
  3545. valid_for_var(aktassignmentnode.left,false);
  3546. { optimize first loading of a set }
  3547. if (right.nodetype=setelementn) and
  3548. not(assigned(tsetelementnode(right).right)) and
  3549. is_emptyset(left) then
  3550. begin
  3551. { adjust for set base }
  3552. tsetelementnode(right).left:=caddnode.create(subn,
  3553. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  3554. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  3555. if no_temp then
  3556. begin
  3557. result:=ccallnode.createintern('fpc_varset_create_element',
  3558. ccallparanode.create(aktassignmentnode.left.getcopy,
  3559. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3560. ccallparanode.create(tsetelementnode(right).left,nil))));
  3561. include(aktassignmentnode.assignmentnodeflags,anf_assign_done_in_right);
  3562. end
  3563. else
  3564. begin
  3565. result:=internalstatements(newstatement);
  3566. { create temp for result }
  3567. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  3568. addstatement(newstatement,temp);
  3569. addstatement(newstatement,ccallnode.createintern('fpc_varset_create_element',
  3570. ccallparanode.create(ctemprefnode.create(temp),
  3571. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3572. ccallparanode.create(tsetelementnode(right).left,nil))))
  3573. );
  3574. { the last statement should return the value as
  3575. location and type, this is done be referencing the
  3576. temp and converting it first from a persistent temp to
  3577. normal temp }
  3578. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  3579. addstatement(newstatement,ctemprefnode.create(temp));
  3580. end;
  3581. tsetelementnode(right).left:=nil;
  3582. end
  3583. else
  3584. begin
  3585. if right.nodetype=setelementn then
  3586. begin
  3587. if no_temp then
  3588. begin
  3589. { add a range or a single element? }
  3590. if assigned(tsetelementnode(right).right) then
  3591. begin
  3592. { adjust for set base }
  3593. tsetelementnode(right).left:=caddnode.create(subn,
  3594. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  3595. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  3596. { adjust for set base }
  3597. tsetelementnode(right).right:=caddnode.create(subn,
  3598. ctypeconvnode.create_internal(tsetelementnode(right).right,sinttype),
  3599. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  3600. result:=ccallnode.createintern('fpc_varset_set_range',
  3601. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3602. ccallparanode.create(tsetelementnode(right).right,
  3603. ccallparanode.create(tsetelementnode(right).left,
  3604. ccallparanode.create(aktassignmentnode.left.getcopy,
  3605. ccallparanode.create(left,nil))))));
  3606. end
  3607. else
  3608. begin
  3609. { s:=s+[element]; ? }
  3610. if left.isequal(aktassignmentnode.left) then
  3611. result:=cinlinenode.createintern(in_include_x_y,false,ccallparanode.create(aktassignmentnode.left.getcopy,
  3612. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,tsetdef(resultdef).elementdef),nil)))
  3613. else
  3614. begin
  3615. { adjust for set base }
  3616. tsetelementnode(right).left:=caddnode.create(subn,
  3617. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  3618. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  3619. result:=ccallnode.createintern('fpc_varset_set',
  3620. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3621. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  3622. ccallparanode.create(aktassignmentnode.left.getcopy,
  3623. ccallparanode.create(left,nil)))));
  3624. end;
  3625. end;
  3626. include(aktassignmentnode.assignmentnodeflags,anf_assign_done_in_right);
  3627. end
  3628. else
  3629. begin
  3630. { adjust for set base }
  3631. tsetelementnode(right).left:=caddnode.create(subn,
  3632. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  3633. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  3634. result:=internalstatements(newstatement);
  3635. { create temp for result }
  3636. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  3637. addstatement(newstatement,temp);
  3638. { add a range or a single element? }
  3639. if assigned(tsetelementnode(right).right) then
  3640. begin
  3641. { adjust for set base }
  3642. tsetelementnode(right).right:=caddnode.create(subn,
  3643. ctypeconvnode.create_internal(tsetelementnode(right).right,sinttype),
  3644. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  3645. addstatement(newstatement,ccallnode.createintern('fpc_varset_set_range',
  3646. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3647. ccallparanode.create(tsetelementnode(right).right,
  3648. ccallparanode.create(tsetelementnode(right).left,
  3649. ccallparanode.create(ctemprefnode.create(temp),
  3650. ccallparanode.create(left,nil))))))
  3651. );
  3652. end
  3653. else
  3654. addstatement(newstatement,ccallnode.createintern('fpc_varset_set',
  3655. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3656. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  3657. ccallparanode.create(ctemprefnode.create(temp),
  3658. ccallparanode.create(left,nil)))))
  3659. );
  3660. { the last statement should return the value as
  3661. location and type, this is done be referencing the
  3662. temp and converting it first from a persistent temp to
  3663. normal temp }
  3664. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  3665. addstatement(newstatement,ctemprefnode.create(temp));
  3666. end;
  3667. { remove reused parts from original node }
  3668. tsetelementnode(right).right:=nil;
  3669. tsetelementnode(right).left:=nil;
  3670. left:=nil;
  3671. end
  3672. else
  3673. call_varset_helper('fpc_varset_add_sets');
  3674. end
  3675. end;
  3676. subn:
  3677. call_varset_helper('fpc_varset_sub_sets');
  3678. symdifn:
  3679. call_varset_helper('fpc_varset_symdif_sets');
  3680. muln:
  3681. call_varset_helper('fpc_varset_mul_sets');
  3682. else
  3683. internalerror(200609241);
  3684. end;
  3685. end;
  3686. function taddnode.first_adddynarray : tnode;
  3687. var
  3688. newstatement : tstatementnode;
  3689. tempnode (*,tempnode2*) : ttempcreatenode;
  3690. para: tcallparanode;
  3691. begin
  3692. result:=nil;
  3693. { when we get here, we are sure that both the left and the right }
  3694. { node are both strings of the same stringtype (JM) }
  3695. case nodetype of
  3696. addn:
  3697. begin
  3698. if (left.nodetype=arrayconstructorn) and (tarrayconstructornode(left).isempty) then
  3699. begin
  3700. result:=right;
  3701. left.free;
  3702. left:=nil;
  3703. right:=nil;
  3704. exit;
  3705. end;
  3706. if (right.nodetype=arrayconstructorn) and (tarrayconstructornode(right).isempty) then
  3707. begin
  3708. result:=left;
  3709. left:=nil;
  3710. right.free;
  3711. right:=nil;
  3712. exit;
  3713. end;
  3714. { create the call to the concat routine both strings as arguments }
  3715. if assigned(aktassignmentnode) and
  3716. (aktassignmentnode.right=self) and
  3717. (aktassignmentnode.left.resultdef=resultdef) and
  3718. valid_for_var(aktassignmentnode.left,false) then
  3719. begin
  3720. para:=ccallparanode.create(
  3721. ctypeconvnode.create_internal(right,voidcodepointertype),
  3722. ccallparanode.create(
  3723. ctypeconvnode.create_internal(left,voidcodepointertype),
  3724. ccallparanode.create(
  3725. caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  3726. ccallparanode.create(
  3727. ctypeconvnode.create_internal(aktassignmentnode.left.getcopy,voidcodepointertype),nil)
  3728. )));
  3729. result:=ccallnode.createintern(
  3730. 'fpc_dynarray_concat',
  3731. para
  3732. );
  3733. include(aktassignmentnode.assignmentnodeflags,anf_assign_done_in_right);
  3734. firstpass(result);
  3735. end
  3736. else
  3737. begin
  3738. result:=internalstatements(newstatement);
  3739. tempnode:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  3740. addstatement(newstatement,tempnode);
  3741. { initialize the temp, since it will be passed to a
  3742. var-parameter (and finalization, which is performed by the
  3743. ttempcreate node and which takes care of the initialization
  3744. on native targets, is a noop on managed VM targets) }
  3745. if (target_info.system in systems_managed_vm) and
  3746. is_managed_type(resultdef) then
  3747. addstatement(newstatement,cinlinenode.create(in_setlength_x,
  3748. false,
  3749. ccallparanode.create(genintconstnode(0),
  3750. ccallparanode.create(ctemprefnode.create(tempnode),nil))));
  3751. para:=ccallparanode.create(
  3752. ctypeconvnode.create_internal(right,voidcodepointertype),
  3753. ccallparanode.create(
  3754. ctypeconvnode.create_internal(left,voidcodepointertype),
  3755. ccallparanode.create(
  3756. caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  3757. ccallparanode.create(
  3758. ctypeconvnode.create_internal(ctemprefnode.create(tempnode),voidcodepointertype),nil)
  3759. )));
  3760. addstatement(
  3761. newstatement,
  3762. ccallnode.createintern(
  3763. 'fpc_dynarray_concat',
  3764. para
  3765. )
  3766. );
  3767. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  3768. addstatement(newstatement,ctemprefnode.create(tempnode));
  3769. end;
  3770. { we reused the arguments }
  3771. left := nil;
  3772. right := nil;
  3773. end;
  3774. unequaln,equaln:
  3775. expectloc:=LOC_FLAGS;
  3776. else
  3777. Internalerror(2018030301);
  3778. end;
  3779. end;
  3780. function taddnode.use_generic_mul32to64: boolean;
  3781. begin
  3782. result := true;
  3783. end;
  3784. function taddnode.use_generic_mul64bit: boolean;
  3785. begin
  3786. result := true;
  3787. end;
  3788. function taddnode.try_make_mul32to64: boolean;
  3789. function canbe32bitint(v: tconstexprint; out canbesignedconst, canbeunsignedconst: boolean): boolean;
  3790. begin
  3791. result := ((v >= int64(low(longint))) and (v <= int64(high(longint)))) or
  3792. ((v >= qword(low(cardinal))) and (v <= qword(high(cardinal))));
  3793. canbesignedconst:=v<=int64(high(longint));
  3794. canbeunsignedconst:=v>=0;
  3795. end;
  3796. function is_32bitordconst(n: tnode; out canbesignedconst, canbeunsignedconst: boolean): boolean;
  3797. begin
  3798. canbesignedconst:=false;
  3799. canbeunsignedconst:=false;
  3800. result := (n.nodetype = ordconstn) and
  3801. canbe32bitint(tordconstnode(n).value, canbesignedconst, canbeunsignedconst);
  3802. end;
  3803. function is_32to64typeconv(n: tnode): boolean;
  3804. begin
  3805. result := (n.nodetype = typeconvn) and
  3806. is_integer(ttypeconvnode(n).left.resultdef) and
  3807. not is_64bit(ttypeconvnode(n).left.resultdef);
  3808. end;
  3809. var
  3810. temp: tnode;
  3811. leftoriginallysigned,
  3812. canbesignedconst, canbeunsignedconst, swapped: boolean;
  3813. begin
  3814. result := false;
  3815. swapped := false;
  3816. { make sure that if there is a constant, that it's on the right }
  3817. if left.nodetype = ordconstn then
  3818. begin
  3819. swapleftright;
  3820. swapped := true;
  3821. end;
  3822. if is_32to64typeconv(left) then
  3823. begin
  3824. leftoriginallysigned:=is_signed(ttypeconvnode(left).left.resultdef);
  3825. if ((is_32bitordconst(right,canbesignedconst, canbeunsignedconst) and
  3826. ((leftoriginallysigned and canbesignedconst) or
  3827. (not leftoriginallysigned and canbeunsignedconst))) or
  3828. (is_32to64typeconv(right) and
  3829. ((leftoriginallysigned =
  3830. is_signed(ttypeconvnode(right).left.resultdef)) or
  3831. (leftoriginallysigned and
  3832. (torddef(ttypeconvnode(right).left.resultdef).ordtype in [u8bit,u16bit]))))) then
  3833. begin
  3834. temp := ttypeconvnode(left).left;
  3835. ttypeconvnode(left).left := nil;
  3836. left.free;
  3837. left := temp;
  3838. if (right.nodetype = typeconvn) then
  3839. begin
  3840. temp := ttypeconvnode(right).left;
  3841. ttypeconvnode(right).left := nil;
  3842. right.free;
  3843. right := temp;
  3844. end;
  3845. if (is_signed(left.resultdef)) then
  3846. begin
  3847. inserttypeconv_internal(left,s32inttype);
  3848. inserttypeconv_internal(right,s32inttype);
  3849. end
  3850. else
  3851. begin
  3852. inserttypeconv_internal(left,u32inttype);
  3853. inserttypeconv_internal(right,u32inttype);
  3854. end;
  3855. firstpass(left);
  3856. firstpass(right);
  3857. result := true;
  3858. end;
  3859. end;
  3860. { pass_Typecheck caches left/right type and resultdef, so restore the
  3861. original order }
  3862. if not result and swapped then
  3863. swapleftright;
  3864. end;
  3865. function taddnode.use_fma : boolean;
  3866. begin
  3867. result:=false;
  3868. end;
  3869. function taddnode.try_fma(ld,rd : tdef) : tnode;
  3870. var
  3871. inlinennr : tinlinenumber;
  3872. begin
  3873. result:=nil;
  3874. if (cs_opt_fastmath in current_settings.optimizerswitches) and
  3875. use_fma and
  3876. (nodetype in [addn,subn]) and
  3877. (rd.typ=floatdef) and (ld.typ=floatdef) and
  3878. (is_single(rd) or is_double(rd)) and
  3879. equal_defs(rd,ld) and
  3880. { transforming a*b+c into fma(a,b,c) makes only sense if c can be
  3881. calculated easily. Consider a*b+c*d which results in
  3882. fmul
  3883. fmul
  3884. fadd
  3885. and in
  3886. fmul
  3887. fma
  3888. when using the fma optimization. On a super scalar architecture, the first instruction
  3889. sequence requires clock_cycles(fmul)+clock_cycles(fadd) clock cycles because the fmuls can be executed in parallel.
  3890. The second sequence requires clock_cycles(fmul)+clock_cycles(fma) because the fma has to wait for the
  3891. result of the fmul. Since typically clock_cycles(fma)>clock_cycles(fadd) applies, the first sequence is better.
  3892. }
  3893. (((left.nodetype=muln) and (node_complexity(right)<3)) or
  3894. ((right.nodetype=muln) and (node_complexity(left)<3)) or
  3895. ((left.nodetype=inlinen) and
  3896. (tinlinenode(left).inlinenumber=in_sqr_real) and
  3897. (node_complexity(right)<3)) or
  3898. ((right.nodetype=inlinen) and
  3899. (tinlinenode(right).inlinenumber=in_sqr_real) and
  3900. (node_complexity(left)<3))
  3901. ) then
  3902. begin
  3903. case tfloatdef(ld).floattype of
  3904. s32real:
  3905. inlinennr:=in_fma_single;
  3906. s64real:
  3907. inlinennr:=in_fma_double;
  3908. s80real:
  3909. inlinennr:=in_fma_extended;
  3910. s128real:
  3911. inlinennr:=in_fma_float128;
  3912. else
  3913. internalerror(2014042601);
  3914. end;
  3915. if left.nodetype=muln then
  3916. begin
  3917. if nodetype=subn then
  3918. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(cunaryminusnode.create(right),
  3919. ccallparanode.create(taddnode(left).right,
  3920. ccallparanode.create(taddnode(left).left,nil
  3921. ))))
  3922. else
  3923. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(right,
  3924. ccallparanode.create(taddnode(left).right,
  3925. ccallparanode.create(taddnode(left).left,nil
  3926. ))));
  3927. right:=nil;
  3928. taddnode(left).right:=nil;
  3929. taddnode(left).left:=nil;
  3930. end
  3931. else if right.nodetype=muln then
  3932. begin
  3933. if nodetype=subn then
  3934. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  3935. ccallparanode.create(cunaryminusnode.create(taddnode(right).right),
  3936. ccallparanode.create(taddnode(right).left,nil
  3937. ))))
  3938. else
  3939. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  3940. ccallparanode.create(taddnode(right).right,
  3941. ccallparanode.create(taddnode(right).left,nil
  3942. ))));
  3943. left:=nil;
  3944. taddnode(right).right:=nil;
  3945. taddnode(right).left:=nil;
  3946. end
  3947. else if (left.nodetype=inlinen) and (tinlinenode(left).inlinenumber=in_sqr_real) then
  3948. begin
  3949. if node_complexity(tinlinenode(left).left)=0 then
  3950. begin
  3951. if nodetype=subn then
  3952. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(cunaryminusnode.create(right),
  3953. ccallparanode.create(tinlinenode(left).left.getcopy,
  3954. ccallparanode.create(tinlinenode(left).left.getcopy,nil
  3955. ))))
  3956. else
  3957. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(right,
  3958. ccallparanode.create(tinlinenode(left).left.getcopy,
  3959. ccallparanode.create(tinlinenode(left).left.getcopy,nil
  3960. ))));
  3961. right:=nil;
  3962. end;
  3963. end
  3964. { we get here only if right is a sqr node }
  3965. else if (right.nodetype=inlinen) and (tinlinenode(right).inlinenumber=in_sqr_real) then
  3966. begin
  3967. if node_complexity(tinlinenode(right).left)=0 then
  3968. begin
  3969. if nodetype=subn then
  3970. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  3971. ccallparanode.create(cunaryminusnode.create(tinlinenode(right).left.getcopy),
  3972. ccallparanode.create(tinlinenode(right).left.getcopy,nil
  3973. ))))
  3974. else
  3975. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  3976. ccallparanode.create(tinlinenode(right).left.getcopy,
  3977. ccallparanode.create(tinlinenode(right).left.getcopy,nil
  3978. ))));
  3979. left:=nil;
  3980. end;
  3981. end;
  3982. end;
  3983. end;
  3984. function taddnode.first_add64bitint: tnode;
  3985. var
  3986. procname: string[31];
  3987. power: longint;
  3988. begin
  3989. result := nil;
  3990. { create helper calls mul }
  3991. if nodetype <> muln then
  3992. exit;
  3993. { make sure that if there is a constant, that it's on the right }
  3994. if left.nodetype = ordconstn then
  3995. swapleftright;
  3996. { can we use a shift instead of a mul? }
  3997. if not (cs_check_overflow in current_settings.localswitches) and
  3998. (right.nodetype = ordconstn) and
  3999. ispowerof2(tordconstnode(right).value,power) then
  4000. begin
  4001. tordconstnode(right).value := power;
  4002. result := cshlshrnode.create(shln,left,right);
  4003. { left and right are reused }
  4004. left := nil;
  4005. right := nil;
  4006. { return firstpassed new node }
  4007. exit;
  4008. end;
  4009. if try_make_mul32to64 then
  4010. begin
  4011. { this uses the same criteria for signedness as the 32 to 64-bit mul
  4012. handling in the i386 code generator }
  4013. if is_signed(left.resultdef) and is_signed(right.resultdef) then
  4014. procname := 'fpc_mul_longint_to_int64'
  4015. else
  4016. procname := 'fpc_mul_dword_to_qword';
  4017. right := ccallparanode.create(right,ccallparanode.create(left,nil));
  4018. result := ccallnode.createintern(procname,right);
  4019. left := nil;
  4020. right := nil;
  4021. end
  4022. else
  4023. begin
  4024. { can full 64-bit multiplication be handled inline? }
  4025. if not use_generic_mul64bit then
  4026. begin
  4027. { generic handling replaces this node with call to fpc_mul_int64,
  4028. whose result is int64 }
  4029. if is_currency(resultdef) then
  4030. resultdef:=s64inttype;
  4031. exit;
  4032. end;
  4033. { when currency is used set the result of the
  4034. parameters to s64bit, so they are not converted }
  4035. if is_currency(resultdef) then
  4036. begin
  4037. left.resultdef:=s64inttype;
  4038. right.resultdef:=s64inttype;
  4039. end;
  4040. { otherwise, create the parameters for the helper }
  4041. right := ccallparanode.create(right,ccallparanode.create(left,nil));
  4042. left := nil;
  4043. { only qword needs the unsigned code, the
  4044. signed code is also used for currency }
  4045. if is_signed(resultdef) then
  4046. procname := 'fpc_mul_int64'
  4047. else
  4048. procname := 'fpc_mul_qword';
  4049. if cs_check_overflow in current_settings.localswitches then
  4050. procname := procname + '_checkoverflow';
  4051. result := ccallnode.createintern(procname,right);
  4052. right := nil;
  4053. end;
  4054. end;
  4055. function taddnode.first_addpointer: tnode;
  4056. begin
  4057. result:=nil;
  4058. expectloc:=LOC_REGISTER;
  4059. end;
  4060. function taddnode.first_cmppointer: tnode;
  4061. begin
  4062. result:=nil;
  4063. expectloc:=LOC_FLAGS;
  4064. end;
  4065. function taddnode.first_addfloat_soft : tnode;
  4066. var
  4067. procname: string[31];
  4068. { do we need to reverse the result ? }
  4069. notnode : boolean;
  4070. fdef : tdef;
  4071. begin
  4072. notnode:=false;
  4073. result:=nil;
  4074. fdef:=nil;
  4075. if not(target_info.system in systems_wince) then
  4076. begin
  4077. case tfloatdef(left.resultdef).floattype of
  4078. s32real:
  4079. begin
  4080. fdef:=search_system_type('FLOAT32REC').typedef;
  4081. procname:='float32';
  4082. end;
  4083. s64real:
  4084. begin
  4085. fdef:=search_system_type('FLOAT64').typedef;
  4086. procname:='float64';
  4087. end;
  4088. {!!! not yet implemented
  4089. s128real:
  4090. }
  4091. else
  4092. internalerror(2005082601);
  4093. end;
  4094. case nodetype of
  4095. addn:
  4096. procname:=procname+'_add';
  4097. muln:
  4098. procname:=procname+'_mul';
  4099. subn:
  4100. procname:=procname+'_sub';
  4101. slashn:
  4102. procname:=procname+'_div';
  4103. ltn:
  4104. procname:=procname+'_lt';
  4105. lten:
  4106. procname:=procname+'_le';
  4107. gtn:
  4108. begin
  4109. procname:=procname+'_lt';
  4110. swapleftright;
  4111. end;
  4112. gten:
  4113. begin
  4114. procname:=procname+'_le';
  4115. swapleftright;
  4116. end;
  4117. equaln:
  4118. procname:=procname+'_eq';
  4119. unequaln:
  4120. begin
  4121. procname:=procname+'_eq';
  4122. notnode:=true;
  4123. end;
  4124. else
  4125. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  4126. end;
  4127. end
  4128. else
  4129. begin
  4130. case nodetype of
  4131. addn:
  4132. procname:='add';
  4133. muln:
  4134. procname:='mul';
  4135. subn:
  4136. procname:='sub';
  4137. slashn:
  4138. procname:='div';
  4139. ltn:
  4140. procname:='lt';
  4141. lten:
  4142. procname:='le';
  4143. gtn:
  4144. procname:='gt';
  4145. gten:
  4146. procname:='ge';
  4147. equaln:
  4148. procname:='eq';
  4149. unequaln:
  4150. procname:='ne';
  4151. else
  4152. begin
  4153. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  4154. exit;
  4155. end;
  4156. end;
  4157. case tfloatdef(left.resultdef).floattype of
  4158. s32real:
  4159. begin
  4160. procname:=procname+'s';
  4161. if nodetype in [addn,muln,subn,slashn] then
  4162. procname:=lower(procname);
  4163. end;
  4164. s64real:
  4165. procname:=procname+'d';
  4166. {!!! not yet implemented
  4167. s128real:
  4168. }
  4169. else
  4170. internalerror(2005082602);
  4171. end;
  4172. end;
  4173. { cast softfpu result? }
  4174. if not(target_info.system in systems_wince) then
  4175. begin
  4176. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  4177. resultdef:=pasbool1type;
  4178. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  4179. ctypeconvnode.create_internal(right,fdef),
  4180. ccallparanode.create(
  4181. ctypeconvnode.create_internal(left,fdef),nil))),resultdef);
  4182. end
  4183. else
  4184. result:=ccallnode.createintern(procname,ccallparanode.create(right,
  4185. ccallparanode.create(left,nil)));
  4186. left:=nil;
  4187. right:=nil;
  4188. { do we need to reverse the result }
  4189. if notnode then
  4190. result:=cnotnode.create(result);
  4191. end;
  4192. function taddnode.first_addfloat : tnode;
  4193. begin
  4194. result := nil;
  4195. { In non-emulation mode, real opcodes are
  4196. emitted for floating point values.
  4197. }
  4198. if not ((cs_fp_emulation in current_settings.moduleswitches)
  4199. {$ifdef cpufpemu}
  4200. or (current_settings.fputype=fpu_soft)
  4201. {$endif cpufpemu}
  4202. ) then
  4203. exit;
  4204. result:=first_addfloat_soft
  4205. end;
  4206. {$ifdef cpuneedsmulhelper}
  4207. function taddnode.use_mul_helper: boolean;
  4208. begin
  4209. result:=(nodetype=muln) and
  4210. not(torddef(resultdef).ordtype in [u8bit,s8bit
  4211. {$if defined(cpu16bitalu) or defined(avr)},u16bit,s16bit{$endif}]);
  4212. end;
  4213. {$endif cpuneedsmulhelper}
  4214. function taddnode.pass_1 : tnode;
  4215. function isconstsetfewelements(p : tnode) : boolean;
  4216. begin
  4217. result:=(p.nodetype=setconstn) and (tsetconstnode(p).elements<=4);
  4218. end;
  4219. var
  4220. rd,ld : tdef;
  4221. i,i2 : longint;
  4222. lt,rt : tnodetype;
  4223. {$ifdef cpuneedsmulhelper}
  4224. procname : string[32];
  4225. {$endif cpuneedsmulhelper}
  4226. tempn,varsetnode: tnode;
  4227. mulnode : taddnode;
  4228. constsetnode : tsetconstnode;
  4229. trycreateinnodes : Boolean;
  4230. begin
  4231. result:=nil;
  4232. { Can we optimize multiple string additions into a single call?
  4233. This need to be done on a complete tree to detect the multiple
  4234. add nodes and is therefor done before the subtrees are processed }
  4235. if canbemultistringadd(self) then
  4236. begin
  4237. result:=genmultistringadd(self);
  4238. exit;
  4239. end;
  4240. { Can we optimize multiple dyn. array additions into a single call?
  4241. This need to be done on a complete tree to detect the multiple
  4242. add nodes and is therefor done before the subtrees are processed }
  4243. if (m_array_operators in current_settings.modeswitches) and canbemultidynarrayadd(self) then
  4244. begin
  4245. result:=genmultidynarrayadd(self);
  4246. exit;
  4247. end;
  4248. { typical set tests like (s*[const. set])<>/=[] can be converted into an or'ed chain of in tests
  4249. for var sets if const. set contains only a few elements }
  4250. 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
  4251. begin
  4252. trycreateinnodes:=false;
  4253. mulnode:=nil;
  4254. if (is_emptyset(right) and (left.nodetype=muln) and
  4255. (isconstsetfewelements(taddnode(left).right) or isconstsetfewelements(taddnode(left).left))) then
  4256. begin
  4257. trycreateinnodes:=true;
  4258. mulnode:=taddnode(left);
  4259. end
  4260. else if (is_emptyset(left) and (right.nodetype=muln) and
  4261. (isconstsetfewelements(taddnode(right).right) or isconstsetfewelements(taddnode(right).left))) then
  4262. begin
  4263. trycreateinnodes:=true;
  4264. mulnode:=taddnode(right);
  4265. end;
  4266. if trycreateinnodes then
  4267. begin
  4268. constsetnode:=nil;
  4269. varsetnode:=nil;
  4270. if isconstsetfewelements(mulnode.right) then
  4271. begin
  4272. constsetnode:=tsetconstnode(mulnode.right);
  4273. varsetnode:=mulnode.left;
  4274. end
  4275. else
  4276. begin
  4277. constsetnode:=tsetconstnode(mulnode.left);
  4278. varsetnode:=mulnode.right;
  4279. end;
  4280. { the node is copied so it might have no side effects, if the complexity is too, cse should fix it, so
  4281. do not check complexity }
  4282. if not(might_have_sideeffects(varsetnode)) then
  4283. begin
  4284. result:=nil;
  4285. for i:=low(tconstset) to high(tconstset) do
  4286. if i in constsetnode.value_set^ then
  4287. begin
  4288. tempn:=cinnode.create(cordconstnode.create(i,tsetdef(constsetnode.resultdef).elementdef,false),varsetnode.getcopy);
  4289. if assigned(result) then
  4290. result:=caddnode.create_internal(orn,result,tempn)
  4291. else
  4292. result:=tempn;
  4293. end;
  4294. if nodetype=equaln then
  4295. result:=cnotnode.create(result);
  4296. exit;
  4297. end;
  4298. end;
  4299. end;
  4300. { get rid of adding empty sets generated by set constructors (s+([]+[..]))
  4301. this needs to be done before firstpass, else the set additions get already converted into calls }
  4302. if (resultdef.typ=setdef) and (nodetype=addn) and (right.nodetype=addn) and (is_emptyset(taddnode(right).left)) then
  4303. begin
  4304. result:=caddnode.create(addn,left,taddnode(right).right);
  4305. left:=nil;
  4306. taddnode(right).right:=nil;
  4307. exit;
  4308. end;
  4309. { first do the two subtrees }
  4310. firstpass(left);
  4311. firstpass(right);
  4312. if codegenerror then
  4313. exit;
  4314. { load easier access variables }
  4315. rd:=right.resultdef;
  4316. ld:=left.resultdef;
  4317. rt:=right.nodetype;
  4318. lt:=left.nodetype;
  4319. { int/int gives real/real! }
  4320. if nodetype=slashn then
  4321. begin
  4322. {$ifdef cpufpemu}
  4323. result:=first_addfloat;
  4324. if assigned(result) then
  4325. exit;
  4326. {$endif cpufpemu}
  4327. expectloc:=LOC_FPUREGISTER;
  4328. end
  4329. { if both are orddefs then check sub types }
  4330. else if (ld.typ=orddef) and (rd.typ=orddef) then
  4331. begin
  4332. { optimize multiplacation by a power of 2 }
  4333. if not(cs_check_overflow in current_settings.localswitches) and
  4334. (nodetype = muln) and
  4335. (((left.nodetype = ordconstn) and
  4336. ispowerof2(tordconstnode(left).value,i)) or
  4337. ((right.nodetype = ordconstn) and
  4338. ispowerof2(tordconstnode(right).value,i2))) then
  4339. begin
  4340. { it could be that we are converting a 32x32 -> 64 multiplication:
  4341. in this case, we have to restore the type conversion }
  4342. inserttypeconv_internal(left,resultdef);
  4343. inserttypeconv_internal(right,resultdef);
  4344. if ((left.nodetype = ordconstn) and
  4345. ispowerof2(tordconstnode(left).value,i)) then
  4346. begin
  4347. tordconstnode(left).value := i;
  4348. result := cshlshrnode.create(shln,right,left);
  4349. end
  4350. else
  4351. begin
  4352. tordconstnode(right).value := i2;
  4353. result := cshlshrnode.create(shln,left,right);
  4354. end;
  4355. result.resultdef := resultdef;
  4356. left := nil;
  4357. right := nil;
  4358. exit;
  4359. end;
  4360. { 2 booleans ? }
  4361. if is_boolean(ld) and is_boolean(rd) then
  4362. begin
  4363. if doshortbooleval(self) then
  4364. expectloc:=LOC_JUMP
  4365. else
  4366. begin
  4367. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  4368. expectloc:=LOC_FLAGS
  4369. else
  4370. expectloc:=LOC_REGISTER;
  4371. end;
  4372. end
  4373. else
  4374. { Both are chars? only convert to shortstrings for addn }
  4375. if is_char(ld) then
  4376. begin
  4377. if nodetype=addn then
  4378. internalerror(200103291);
  4379. expectloc:=LOC_FLAGS;
  4380. end
  4381. else if (nodetype=muln) and
  4382. is_64bitint(resultdef) and
  4383. not use_generic_mul32to64 and
  4384. try_make_mul32to64 then
  4385. begin
  4386. { if the code generator can handle 32 to 64-bit muls,
  4387. we're done here }
  4388. expectloc:=LOC_REGISTER;
  4389. end
  4390. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  4391. { is there a 64 bit type ? }
  4392. else if (torddef(ld).ordtype in [s64bit,u64bit,scurrency]) then
  4393. begin
  4394. result := first_add64bitint;
  4395. if assigned(result) then
  4396. exit;
  4397. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  4398. expectloc:=LOC_REGISTER
  4399. else
  4400. expectloc:=LOC_JUMP;
  4401. end
  4402. {$elseif defined(llvm) and defined(cpu32bitalu)}
  4403. { llvm does not support 128 bit math on 32 bit targets, which is
  4404. necessary for overflow checking 64 bit operations }
  4405. else if (torddef(ld).ordtype in [s64bit,u64bit,scurrency]) and
  4406. (cs_check_overflow in current_settings.localswitches) and
  4407. (nodetype in [addn,subn,muln]) then
  4408. begin
  4409. result := first_add64bitint;
  4410. if assigned(result) then
  4411. exit;
  4412. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  4413. expectloc:=LOC_REGISTER
  4414. else
  4415. expectloc:=LOC_JUMP;
  4416. end
  4417. {$endif not(cpu64bitalu) and not(cpuhighleveltarget)}
  4418. { generic 32bit conversion }
  4419. else
  4420. begin
  4421. {$ifdef cpuneedsmulhelper}
  4422. if use_mul_helper then
  4423. begin
  4424. result := nil;
  4425. case torddef(resultdef).ordtype of
  4426. s8bit:
  4427. procname := 'fpc_mul_shortint';
  4428. u8bit:
  4429. procname := 'fpc_mul_byte';
  4430. s16bit:
  4431. procname := 'fpc_mul_integer';
  4432. u16bit:
  4433. procname := 'fpc_mul_word';
  4434. s32bit:
  4435. procname := 'fpc_mul_longint';
  4436. u32bit:
  4437. procname := 'fpc_mul_dword';
  4438. else
  4439. internalerror(2011022301);
  4440. end;
  4441. if cs_check_overflow in current_settings.localswitches then
  4442. procname:=procname+'_checkoverflow';
  4443. result := ccallnode.createintern(procname,
  4444. ccallparanode.create(right,
  4445. ccallparanode.create(left,nil)));
  4446. left := nil;
  4447. right := nil;
  4448. firstpass(result);
  4449. exit;
  4450. end;
  4451. {$endif cpuneedsmulhelper}
  4452. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  4453. expectloc:=LOC_REGISTER
  4454. {$if not defined(cpuhighleveltarget)}
  4455. else if torddef(ld).size>sizeof(aint) then
  4456. expectloc:=LOC_JUMP
  4457. {$endif}
  4458. else
  4459. expectloc:=LOC_FLAGS;
  4460. end;
  4461. end
  4462. { left side a setdef, must be before string processing,
  4463. else array constructor can be seen as array of char (PFV) }
  4464. else if (ld.typ=setdef) then
  4465. begin
  4466. { small sets are handled inline by the compiler.
  4467. small set doesn't have support for adding ranges }
  4468. if is_smallset(ld) and
  4469. not(
  4470. (right.nodetype=setelementn) and
  4471. assigned(tsetelementnode(right).right)
  4472. ) then
  4473. begin
  4474. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  4475. expectloc:=LOC_FLAGS
  4476. else
  4477. expectloc:=LOC_REGISTER;
  4478. end
  4479. else
  4480. begin
  4481. result := first_addset;
  4482. if assigned(result) then
  4483. exit;
  4484. expectloc:=LOC_CREFERENCE;
  4485. end;
  4486. end
  4487. { compare pchar by addresses like BP/Delphi }
  4488. else if is_pchar(ld) then
  4489. begin
  4490. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  4491. result:=first_addpointer
  4492. else
  4493. result:=first_cmppointer;
  4494. end
  4495. { is one of the operands a string }
  4496. else if (ld.typ=stringdef) then
  4497. begin
  4498. if is_widestring(ld) then
  4499. begin
  4500. { this is only for add, the comparison is handled later }
  4501. expectloc:=LOC_REGISTER;
  4502. end
  4503. else if is_unicodestring(ld) then
  4504. begin
  4505. { this is only for add, the comparison is handled later }
  4506. expectloc:=LOC_REGISTER;
  4507. end
  4508. else if is_ansistring(ld) then
  4509. begin
  4510. { this is only for add, the comparison is handled later }
  4511. expectloc:=LOC_REGISTER;
  4512. end
  4513. else if is_longstring(ld) then
  4514. begin
  4515. { this is only for add, the comparison is handled later }
  4516. expectloc:=LOC_REFERENCE;
  4517. end
  4518. else
  4519. begin
  4520. {$ifdef addstringopt}
  4521. { can create a call which isn't handled by callparatemp }
  4522. if canbeaddsstringcharoptnode(self) then
  4523. begin
  4524. hp := genaddsstringcharoptnode(self);
  4525. pass_1 := hp;
  4526. exit;
  4527. end
  4528. else
  4529. {$endif addstringopt}
  4530. begin
  4531. { Fix right to be shortstring }
  4532. if is_char(right.resultdef) then
  4533. begin
  4534. inserttypeconv(right,cshortstringtype);
  4535. firstpass(right);
  4536. end;
  4537. end;
  4538. {$ifdef addstringopt}
  4539. { can create a call which isn't handled by callparatemp }
  4540. if canbeaddsstringcsstringoptnode(self) then
  4541. begin
  4542. hp := genaddsstringcsstringoptnode(self);
  4543. pass_1 := hp;
  4544. exit;
  4545. end;
  4546. {$endif addstringopt}
  4547. end;
  4548. { otherwise, let addstring convert everything }
  4549. result := first_addstring;
  4550. exit;
  4551. end
  4552. { is one a real float ? }
  4553. else if (rd.typ=floatdef) or (ld.typ=floatdef) then
  4554. begin
  4555. {$ifdef cpufpemu}
  4556. result:=first_addfloat;
  4557. if assigned(result) then
  4558. exit;
  4559. {$endif cpufpemu}
  4560. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  4561. expectloc:=LOC_FPUREGISTER
  4562. else
  4563. expectloc:=LOC_FLAGS;
  4564. result:=try_fma(ld,rd);
  4565. if assigned(result) then
  4566. exit;
  4567. end
  4568. { pointer comperation and subtraction }
  4569. else if (ld.typ=pointerdef) then
  4570. begin
  4571. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  4572. result:=first_addpointer
  4573. else
  4574. result:=first_cmppointer;
  4575. end
  4576. else if is_implicit_pointer_object_type(ld) then
  4577. begin
  4578. if ld.size>sizeof(aint) then
  4579. expectloc:=LOC_JUMP
  4580. else
  4581. expectloc:=LOC_FLAGS;
  4582. end
  4583. else if (ld.typ=classrefdef) then
  4584. begin
  4585. if ld.size>sizeof(aint) then
  4586. expectloc:=LOC_JUMP
  4587. else
  4588. expectloc:=LOC_FLAGS;
  4589. end
  4590. { support procvar=nil,procvar<>nil }
  4591. else if ((ld.typ=procvardef) and (rt=niln)) or
  4592. ((rd.typ=procvardef) and (lt=niln)) then
  4593. begin
  4594. if (ld.typ=procvardef) and (tprocvardef(ld).size>sizeof(aint)) or
  4595. (rd.typ=procvardef) and (tprocvardef(rd).size>sizeof(aint)) then
  4596. expectloc:=LOC_JUMP
  4597. else
  4598. expectloc:=LOC_FLAGS;
  4599. end
  4600. {$ifdef SUPPORT_MMX}
  4601. { mmx support, this must be before the zero based array
  4602. check }
  4603. else if (cs_mmx in current_settings.localswitches) and is_mmx_able_array(ld) and
  4604. is_mmx_able_array(rd) then
  4605. begin
  4606. expectloc:=LOC_MMXREGISTER;
  4607. end
  4608. {$endif SUPPORT_MMX}
  4609. else if (rd.typ=pointerdef) or (ld.typ=pointerdef) then
  4610. begin
  4611. result:=first_addpointer;
  4612. end
  4613. else if (rd.typ=procvardef) and
  4614. (ld.typ=procvardef) and
  4615. equal_defs(rd,ld) then
  4616. begin
  4617. if tprocvardef(ld).size>sizeof(aint) then
  4618. expectloc:=LOC_JUMP
  4619. else
  4620. expectloc:=LOC_FLAGS;
  4621. end
  4622. else if (ld.typ=enumdef) then
  4623. begin
  4624. if tenumdef(ld).size>sizeof(aint) then
  4625. expectloc:=LOC_JUMP
  4626. else
  4627. expectloc:=LOC_FLAGS;
  4628. end
  4629. {$ifdef SUPPORT_MMX}
  4630. else if (cs_mmx in current_settings.localswitches) and
  4631. is_mmx_able_array(ld) and
  4632. is_mmx_able_array(rd) then
  4633. begin
  4634. expectloc:=LOC_MMXREGISTER;
  4635. end
  4636. {$endif SUPPORT_MMX}
  4637. else if is_dynamic_array(ld) or is_dynamic_array(rd) then
  4638. begin
  4639. result:=first_adddynarray;
  4640. exit;
  4641. end
  4642. { the general solution is to convert to 32 bit int }
  4643. else
  4644. begin
  4645. expectloc:=LOC_REGISTER;
  4646. end;
  4647. end;
  4648. {$ifdef state_tracking}
  4649. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  4650. var factval:Tnode;
  4651. begin
  4652. track_state_pass:=false;
  4653. if left.track_state_pass(exec_known) then
  4654. begin
  4655. track_state_pass:=true;
  4656. left.resultdef:=nil;
  4657. do_typecheckpass(left);
  4658. end;
  4659. factval:=aktstate.find_fact(left);
  4660. if factval<>nil then
  4661. begin
  4662. track_state_pass:=true;
  4663. left.destroy;
  4664. left:=factval.getcopy;
  4665. end;
  4666. if right.track_state_pass(exec_known) then
  4667. begin
  4668. track_state_pass:=true;
  4669. right.resultdef:=nil;
  4670. do_typecheckpass(right);
  4671. end;
  4672. factval:=aktstate.find_fact(right);
  4673. if factval<>nil then
  4674. begin
  4675. track_state_pass:=true;
  4676. right.destroy;
  4677. right:=factval.getcopy;
  4678. end;
  4679. end;
  4680. {$endif}
  4681. {$ifdef DEBUG_NODE_XML}
  4682. procedure TAddNode.XMLPrintNodeInfo(var T: Text);
  4683. var
  4684. i: TAddNodeFlag;
  4685. First: Boolean;
  4686. begin
  4687. inherited XMLPrintNodeInfo(T);
  4688. First := True;
  4689. for i in addnodeflags do
  4690. begin
  4691. if First then
  4692. begin
  4693. Write(T, ' addnodeflags="', i);
  4694. First := False;
  4695. end
  4696. else
  4697. Write(T, ',', i)
  4698. end;
  4699. if not First then
  4700. Write(T, '"');
  4701. end;
  4702. {$endif DEBUG_NODE_XML}
  4703. end.