nadd.pas 206 KB

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