nadd.pas 198 KB

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