nadd.pas 194 KB

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