nadd.pas 197 KB

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