nadd.pas 195 KB

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