nadd.pas 195 KB

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