nadd.pas 181 KB

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