nadd.pas 184 KB

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