nadd.pas 173 KB

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