nadd.pas 206 KB

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