nadd.pas 206 KB

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