nadd.pas 175 KB

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