nadd.pas 206 KB

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