nadd.pas 194 KB

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