nadd.pas 195 KB

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