nadd.pas 196 KB

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