nadd.pas 172 KB

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