nadd.pas 187 KB

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