nadd.pas 193 KB

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