nadd.pas 196 KB

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