nadd.pas 196 KB

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