2
0

nadd.pas 197 KB

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