nadd.pas 187 KB

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