nadd.pas 205 KB

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