nadd.pas 205 KB

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