nadd.pas 197 KB

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