nadd.pas 172 KB

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