nadd.pas 184 KB

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