nadd.pas 206 KB

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