nadd.pas 202 KB

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