nadd.pas 204 KB

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