nadd.pas 192 KB

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