nadd.pas 196 KB

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