nadd.pas 195 KB

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