nadd.pas 193 KB

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