nadd.pas 197 KB

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