nadd.pas 178 KB

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