nadd.pas 205 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003
  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(equal_defs(ld,rd)) then
  2698. IncompatibleTypes(ld,rd);
  2699. end
  2700. else
  2701. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2702. end;
  2703. subn:
  2704. begin
  2705. if (cs_extsyntax in current_settings.moduleswitches) or
  2706. (nf_internal in flags) then
  2707. begin
  2708. if is_voidpointer(right.resultdef) then
  2709. begin
  2710. if is_big_untyped_addrnode(right) then
  2711. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  2712. inserttypeconv(right,left.resultdef)
  2713. end
  2714. else if is_voidpointer(left.resultdef) then
  2715. inserttypeconv(left,right.resultdef)
  2716. else if not(equal_defs(ld,rd)) then
  2717. IncompatibleTypes(ld,rd);
  2718. end
  2719. else
  2720. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2721. if not(anf_has_pointerdiv in addnodeflags) and
  2722. (tpointerdef(rd).pointeddef.size>1) then
  2723. begin
  2724. hp:=getcopy;
  2725. include(taddnode(hp).addnodeflags, anf_has_pointerdiv);
  2726. result:=cmoddivnode.create(divn,hp,
  2727. cordconstnode.create(tpointerdef(rd).pointeddef.size,tpointerdef(rd).pointer_subtraction_result_type,false));
  2728. end;
  2729. resultdef:=tpointerdef(rd).pointer_subtraction_result_type;
  2730. exit;
  2731. end;
  2732. else
  2733. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2734. end;
  2735. end
  2736. { is one of the operands a string?,
  2737. chararrays are also handled as strings (after conversion), also take
  2738. care of chararray+chararray and chararray+char.
  2739. Note: Must be done after pointerdef+pointerdef has been checked, else
  2740. pchar is converted to string }
  2741. else if (rd.typ=stringdef) or
  2742. (ld.typ=stringdef) or
  2743. { stringconstn's can be arraydefs }
  2744. (lt=stringconstn) or
  2745. (rt=stringconstn) or
  2746. ((is_pchar(rd) or is_chararray(rd) or is_char(rd) or is_open_chararray(rd) or
  2747. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd)) and
  2748. (is_pchar(ld) or is_chararray(ld) or is_char(ld) or is_open_chararray(ld) or
  2749. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld))) then
  2750. begin
  2751. if (nodetype in [addn,equaln,unequaln,lten,gten,ltn,gtn]) then
  2752. begin
  2753. { Is there a unicodestring? }
  2754. if is_unicodestring(rd) or is_unicodestring(ld) or
  2755. ((m_default_unicodestring in current_settings.modeswitches) and
  2756. (cs_refcountedstrings in current_settings.localswitches) and
  2757. (
  2758. is_pwidechar(rd) or is_widechararray(rd) or is_open_widechararray(rd) or (lt = stringconstn) or
  2759. is_pwidechar(ld) or is_widechararray(ld) or is_open_widechararray(ld) or (rt = stringconstn)
  2760. )
  2761. ) then
  2762. strtype:=st_unicodestring
  2763. else
  2764. { Is there a widestring? }
  2765. if is_widestring(rd) or is_widestring(ld) or
  2766. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd) or
  2767. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld) then
  2768. strtype:=st_widestring
  2769. else
  2770. if is_ansistring(rd) or is_ansistring(ld) or
  2771. ((cs_refcountedstrings in current_settings.localswitches) and
  2772. //todo: Move some of this to longstring's then they are implemented?
  2773. (
  2774. is_pchar(rd) or (is_chararray(rd) and (rd.size > 255)) or is_open_chararray(rd) or (lt = stringconstn) or
  2775. is_pchar(ld) or (is_chararray(ld) and (ld.size > 255)) or is_open_chararray(ld) or (rt = stringconstn)
  2776. )
  2777. ) then
  2778. strtype:=st_ansistring
  2779. else
  2780. if is_longstring(rd) or is_longstring(ld) then
  2781. strtype:=st_longstring
  2782. else
  2783. begin
  2784. { TODO: todo: add a warning/hint here if one converting a too large array}
  2785. { nodes is PChar, array [with size > 255] or OpenArrayOfChar.
  2786. Note: Delphi halts with error if "array [0..xx] of char"
  2787. is assigned to ShortString and string length is less
  2788. then array size }
  2789. strtype:= st_shortstring;
  2790. end;
  2791. // Now convert nodes to common string type
  2792. case strtype of
  2793. st_widestring :
  2794. begin
  2795. if not(is_widestring(rd)) then
  2796. inserttypeconv(right,cwidestringtype);
  2797. if not(is_widestring(ld)) then
  2798. inserttypeconv(left,cwidestringtype);
  2799. end;
  2800. st_unicodestring :
  2801. begin
  2802. if not(is_unicodestring(rd)) then
  2803. if not ((ld.size=0) and (nodetype in [equaln,unequaln])) then
  2804. inserttypeconv(right,cunicodestringtype);
  2805. if not(is_unicodestring(ld)) then
  2806. if not ((rd.size=0) and (nodetype in [equaln,unequaln])) then
  2807. inserttypeconv(left,cunicodestringtype);
  2808. end;
  2809. st_ansistring :
  2810. begin
  2811. { use same code page if possible (don't force same code
  2812. page in case both are ansistrings with code page <>
  2813. CP_NONE, since then data loss can occur: the ansistring
  2814. helpers will convert them at run time to an encoding
  2815. that can represent both encodings) }
  2816. if is_ansistring(ld) and
  2817. (tstringdef(ld).encoding<>0) and
  2818. (tstringdef(ld).encoding<>globals.CP_NONE) and
  2819. (not is_ansistring(rd) or
  2820. (tstringdef(rd).encoding=0) or
  2821. (tstringdef(rd).encoding=globals.CP_NONE)) then
  2822. inserttypeconv(right,ld)
  2823. else if is_ansistring(rd) and
  2824. (tstringdef(rd).encoding<>0) and
  2825. (tstringdef(rd).encoding<>globals.CP_NONE) and
  2826. (not is_ansistring(ld) or
  2827. (tstringdef(ld).encoding=0) or
  2828. (tstringdef(ld).encoding=globals.CP_NONE)) then
  2829. inserttypeconv(left,rd)
  2830. else
  2831. begin
  2832. if not is_ansistring(ld) then
  2833. inserttypeconv(left,getansistringdef);
  2834. if not is_ansistring(rd) then
  2835. inserttypeconv(right,getansistringdef);
  2836. end;
  2837. end;
  2838. st_longstring :
  2839. begin
  2840. if not(is_longstring(rd)) then
  2841. inserttypeconv(right,clongstringtype);
  2842. if not(is_longstring(ld)) then
  2843. inserttypeconv(left,clongstringtype);
  2844. end;
  2845. st_shortstring :
  2846. begin
  2847. if not(is_shortstring(ld)) then
  2848. inserttypeconv(left,cshortstringtype);
  2849. { don't convert char, that can be handled by the optimized node }
  2850. if not(is_shortstring(rd) or is_char(rd)) then
  2851. inserttypeconv(right,cshortstringtype);
  2852. end;
  2853. end;
  2854. end
  2855. else
  2856. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2857. end
  2858. { implicit pointer object type comparison }
  2859. else if is_implicit_pointer_object_type(rd) or is_implicit_pointer_object_type(ld) then
  2860. begin
  2861. if (nodetype in [equaln,unequaln]) then
  2862. begin
  2863. if is_implicit_pointer_object_type(rd) and is_implicit_pointer_object_type(ld) then
  2864. begin
  2865. if def_is_related(tobjectdef(rd),tobjectdef(ld)) then
  2866. inserttypeconv(right,left.resultdef)
  2867. else
  2868. inserttypeconv(left,right.resultdef);
  2869. end
  2870. else if is_implicit_pointer_object_type(rd) then
  2871. inserttypeconv(left,right.resultdef)
  2872. else
  2873. inserttypeconv(right,left.resultdef);
  2874. end
  2875. else
  2876. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2877. end
  2878. else if (rd.typ=classrefdef) and (ld.typ=classrefdef) then
  2879. begin
  2880. if (nodetype in [equaln,unequaln]) then
  2881. begin
  2882. if def_is_related(tobjectdef(tclassrefdef(rd).pointeddef),
  2883. tobjectdef(tclassrefdef(ld).pointeddef)) then
  2884. inserttypeconv(right,left.resultdef)
  2885. else
  2886. inserttypeconv(left,right.resultdef);
  2887. end
  2888. else
  2889. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2890. end
  2891. { allow comparison with nil pointer }
  2892. else if is_implicit_pointer_object_type(rd) or (rd.typ=classrefdef) then
  2893. begin
  2894. if (nodetype in [equaln,unequaln]) then
  2895. inserttypeconv(left,right.resultdef)
  2896. else
  2897. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2898. end
  2899. else if is_implicit_pointer_object_type(ld) or (ld.typ=classrefdef) then
  2900. begin
  2901. if (nodetype in [equaln,unequaln]) then
  2902. inserttypeconv(right,left.resultdef)
  2903. else
  2904. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2905. end
  2906. { support procvar=nil,procvar<>nil }
  2907. else if ((ld.typ=procvardef) and (rt=niln)) or
  2908. ((rd.typ=procvardef) and (lt=niln)) then
  2909. begin
  2910. if not(nodetype in [equaln,unequaln]) then
  2911. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2912. { find proc field in methodpointer record }
  2913. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  2914. if not assigned(hsym) then
  2915. internalerror(200412043);
  2916. { For methodpointers compare only tmethodpointer.proc }
  2917. if (rd.typ=procvardef) and
  2918. (not tprocvardef(rd).is_addressonly) then
  2919. begin
  2920. right:=csubscriptnode.create(
  2921. hsym,
  2922. ctypeconvnode.create_internal(right,methodpointertype));
  2923. typecheckpass(right);
  2924. end;
  2925. if (ld.typ=procvardef) and
  2926. (not tprocvardef(ld).is_addressonly) then
  2927. begin
  2928. left:=csubscriptnode.create(
  2929. hsym,
  2930. ctypeconvnode.create_internal(left,methodpointertype));
  2931. typecheckpass(left);
  2932. end;
  2933. if lt=niln then
  2934. inserttypeconv_explicit(left,right.resultdef)
  2935. else
  2936. inserttypeconv_explicit(right,left.resultdef)
  2937. end
  2938. { <dyn. array>+<dyn. array> ? }
  2939. else if (nodetype=addn) and (is_dynamic_array(ld) or is_dynamic_array(rd)) then
  2940. begin
  2941. { empty array to add? }
  2942. if (right.nodetype=arrayconstructorn) and (tarrayconstructornode(right).left=nil) then
  2943. begin
  2944. result:=left;
  2945. left:=nil;
  2946. exit;
  2947. end
  2948. else if (left.nodetype=arrayconstructorn) and (tarrayconstructornode(left).left=nil) then
  2949. begin
  2950. result:=right;
  2951. right:=nil;
  2952. exit;
  2953. end
  2954. else
  2955. begin
  2956. result:=maybe_convert_to_insert;
  2957. if assigned(result) then
  2958. exit;
  2959. if not(is_dynamic_array(ld)) then
  2960. inserttypeconv(left,rd);
  2961. if not(is_dynamic_array(rd)) then
  2962. inserttypeconv(right,ld);
  2963. end;
  2964. end
  2965. { support dynamicarray=nil,dynamicarray<>nil }
  2966. else if (is_dynamic_array(ld) and (rt=niln)) or
  2967. (is_dynamic_array(rd) and (lt=niln)) or
  2968. (is_dynamic_array(ld) and is_dynamic_array(rd)) then
  2969. begin
  2970. if not(nodetype in [equaln,unequaln]) then
  2971. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2972. if lt=niln then
  2973. inserttypeconv_explicit(left,right.resultdef)
  2974. else
  2975. inserttypeconv_explicit(right,left.resultdef)
  2976. end
  2977. {$ifdef SUPPORT_MMX}
  2978. { mmx support, this must be before the zero based array
  2979. check }
  2980. else if (cs_mmx in current_settings.localswitches) and
  2981. is_mmx_able_array(ld) and
  2982. is_mmx_able_array(rd) and
  2983. equal_defs(ld,rd) then
  2984. begin
  2985. case nodetype of
  2986. addn,subn,xorn,orn,andn:
  2987. ;
  2988. { mul is a little bit restricted }
  2989. muln:
  2990. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  2991. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2992. else
  2993. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2994. end;
  2995. end
  2996. {$endif SUPPORT_MMX}
  2997. { vector support, this must be before the zero based array
  2998. check }
  2999. else if (cs_support_vectors in current_settings.globalswitches) and
  3000. fits_in_mm_register(ld) and
  3001. fits_in_mm_register(rd) and
  3002. equal_defs(ld,rd) then
  3003. begin
  3004. if not(nodetype in [addn,subn,xorn,orn,andn,muln,slashn]) then
  3005. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3006. { both defs must be equal, so taking left or right as resultdef doesn't matter }
  3007. resultdef:=to_hwvectordef(left.resultdef,false);
  3008. end
  3009. { this is a little bit dangerous, also the left type }
  3010. { pointer to should be checked! This broke the mmx support }
  3011. else if (rd.typ=pointerdef) or
  3012. (is_zero_based_array(rd) and (rt<>stringconstn)) then
  3013. begin
  3014. if is_zero_based_array(rd) then
  3015. begin
  3016. resultdef:=cpointerdef.getreusable(tarraydef(rd).elementdef);
  3017. inserttypeconv(right,resultdef);
  3018. end
  3019. else
  3020. resultdef:=right.resultdef;
  3021. inserttypeconv(left,tpointerdef(right.resultdef).pointer_arithmetic_int_type);
  3022. if nodetype=addn then
  3023. begin
  3024. if (rt=niln) then
  3025. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,'NIL');
  3026. if (not(cs_extsyntax in current_settings.moduleswitches) and not(nf_internal in flags)) or
  3027. (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
  3028. not(cs_pointermath in current_settings.localswitches) and
  3029. not((rd.typ=pointerdef) and tpointerdef(rd).has_pointer_math)) then
  3030. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3031. if (rd.typ=pointerdef) and
  3032. (tpointerdef(rd).pointeddef.size>1) then
  3033. begin
  3034. left:=caddnode.create(muln,left,
  3035. cordconstnode.create(tpointerdef(rd).pointeddef.size,tpointerdef(right.resultdef).pointer_arithmetic_int_type,true));
  3036. typecheckpass(left);
  3037. end;
  3038. end
  3039. else
  3040. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3041. end
  3042. else if (ld.typ=pointerdef) or
  3043. (is_zero_based_array(ld) and (lt<>stringconstn)) then
  3044. begin
  3045. if is_zero_based_array(ld) then
  3046. begin
  3047. resultdef:=cpointerdef.getreusable(tarraydef(ld).elementdef);
  3048. inserttypeconv(left,resultdef);
  3049. end
  3050. else
  3051. resultdef:=left.resultdef;
  3052. inserttypeconv(right,tpointerdef(left.resultdef).pointer_arithmetic_int_type);
  3053. if nodetype in [addn,subn] then
  3054. begin
  3055. if (lt=niln) then
  3056. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),'NIL',rd.typename);
  3057. if (not(cs_extsyntax in current_settings.moduleswitches) and not(nf_internal in flags)) or
  3058. (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
  3059. not(cs_pointermath in current_settings.localswitches) and
  3060. not((ld.typ=pointerdef) and tpointerdef(ld).has_pointer_math)) then
  3061. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3062. if (ld.typ=pointerdef) then
  3063. begin
  3064. if is_big_untyped_addrnode(left) then
  3065. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  3066. if (tpointerdef(ld).pointeddef.size>1) then
  3067. begin
  3068. right:=caddnode.create(muln,right,
  3069. cordconstnode.create(tpointerdef(ld).pointeddef.size,tpointerdef(left.resultdef).pointer_arithmetic_int_type,true));
  3070. typecheckpass(right);
  3071. end
  3072. end else
  3073. if is_zero_based_array(ld) and
  3074. (tarraydef(ld).elementdef.size>1) then
  3075. begin
  3076. right:=caddnode.create(muln,right,
  3077. cordconstnode.create(tarraydef(ld).elementdef.size,tpointerdef(left.resultdef).pointer_arithmetic_int_type,true));
  3078. typecheckpass(right);
  3079. end;
  3080. end
  3081. else
  3082. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3083. end
  3084. else if (rd.typ=procvardef) and
  3085. (ld.typ=procvardef) and
  3086. equal_defs(rd,ld) then
  3087. begin
  3088. if (nodetype in [equaln,unequaln]) then
  3089. begin
  3090. if tprocvardef(rd).is_addressonly then
  3091. begin
  3092. inserttypeconv_internal(right,voidcodepointertype);
  3093. inserttypeconv_internal(left,voidcodepointertype);
  3094. end
  3095. else
  3096. begin
  3097. { find proc field in methodpointer record }
  3098. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  3099. if not assigned(hsym) then
  3100. internalerror(2004120405);
  3101. { Compare tmehodpointer(left).proc }
  3102. right:=csubscriptnode.create(
  3103. hsym,
  3104. ctypeconvnode.create_internal(right,methodpointertype));
  3105. typecheckpass(right);
  3106. left:=csubscriptnode.create(
  3107. hsym,
  3108. ctypeconvnode.create_internal(left,methodpointertype));
  3109. typecheckpass(left);
  3110. end;
  3111. end
  3112. else
  3113. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3114. end
  3115. { enums }
  3116. else if (ld.typ=enumdef) and (rd.typ=enumdef) then
  3117. begin
  3118. if allowenumop(nodetype) or (nf_internal in flags) then
  3119. inserttypeconv(right,left.resultdef)
  3120. else
  3121. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  3122. end
  3123. { generic conversion, this is for error recovery }
  3124. else
  3125. begin
  3126. inserttypeconv(left,sinttype);
  3127. inserttypeconv(right,sinttype);
  3128. end;
  3129. if cmp_of_disjunct_ranges(res) and not(nf_internal in flags) then
  3130. begin
  3131. if res then
  3132. CGMessage(type_w_comparison_always_true)
  3133. else
  3134. CGMessage(type_w_comparison_always_false);
  3135. end;
  3136. { set resultdef if not already done }
  3137. if not assigned(resultdef) then
  3138. begin
  3139. case nodetype of
  3140. ltn,lten,gtn,gten,equaln,unequaln :
  3141. resultdef:=pasbool1type;
  3142. slashn :
  3143. resultdef:=resultrealdef;
  3144. addn:
  3145. begin
  3146. { for strings, return is always a 255 char string }
  3147. if is_shortstring(left.resultdef) then
  3148. resultdef:=cshortstringtype
  3149. else
  3150. { for ansistrings set resultdef to assignment left node
  3151. if it is an assignment and left node expects ansistring }
  3152. if is_ansistring(left.resultdef) and
  3153. assigned(aktassignmentnode) and
  3154. (aktassignmentnode.right=self) and
  3155. is_ansistring(aktassignmentnode.left.resultdef) then
  3156. resultdef:=aktassignmentnode.left.resultdef
  3157. else
  3158. resultdef:=left.resultdef;
  3159. end;
  3160. else
  3161. resultdef:=left.resultdef;
  3162. end;
  3163. end;
  3164. { when the result is currency we need some extra code for
  3165. multiplication and division. this should not be done when
  3166. the muln or slashn node is created internally }
  3167. if not(nf_is_currency in flags) and
  3168. is_currency(resultdef) then
  3169. begin
  3170. case nodetype of
  3171. slashn :
  3172. begin
  3173. { slashn will only work with floats }
  3174. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  3175. include(hp.flags,nf_is_currency);
  3176. result:=hp;
  3177. end;
  3178. muln :
  3179. begin
  3180. hp:=nil;
  3181. if s64currencytype.typ=floatdef then
  3182. begin
  3183. { if left is a currency integer constant, we can get rid of the factor 10000 }
  3184. { int64(...) causes a cast on currency, so it is the currency value multiplied by 10000 }
  3185. 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
  3186. begin
  3187. { trealconstnode expects that value_real and value_currency contain valid values }
  3188. {$ifdef FPC_CURRENCY_IS_INT64}
  3189. trealconstnode(left).value_currency:=pint64(@(trealconstnode(left).value_currency))^ div 10000;
  3190. {$else}
  3191. trealconstnode(left).value_currency:=trealconstnode(left).value_currency / 10000;
  3192. {$endif}
  3193. trealconstnode(left).value_real:=trealconstnode(left).value_real/10000;
  3194. end
  3195. { or if right is an integer constant, we can get rid of its factor 10000 }
  3196. 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
  3197. begin
  3198. { trealconstnode expects that value and value_currency contain valid values }
  3199. {$ifdef FPC_CURRENCY_IS_INT64}
  3200. trealconstnode(right).value_currency:=pint64(@(trealconstnode(right).value_currency))^ div 10000;
  3201. {$else}
  3202. trealconstnode(right).value_currency:=trealconstnode(right).value_currency / 10000;
  3203. {$endif}
  3204. trealconstnode(right).value_real:=trealconstnode(right).value_real/10000;
  3205. end
  3206. else
  3207. begin
  3208. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype));
  3209. include(hp.flags,nf_is_currency);
  3210. end;
  3211. end
  3212. else
  3213. begin
  3214. { if left is a currency integer constant, we can get rid of the factor 10000 }
  3215. if (left.nodetype=ordconstn) and (is_currency(left.resultdef)) and ((tordconstnode(left).value mod 10000)=0) then
  3216. tordconstnode(left).value:=tordconstnode(left).value div 10000
  3217. { or if right is an integer constant, we can get rid of its factor 10000 }
  3218. else if (right.nodetype=ordconstn) and (is_currency(right.resultdef)) and ((tordconstnode(right).value mod 10000)=0) then
  3219. tordconstnode(right).value:=tordconstnode(right).value div 10000
  3220. else if (right.nodetype=muln) and is_currency(right.resultdef) and
  3221. { do not test swapped here as the internal conversions are only create as "var."*"10000" }
  3222. is_currency(taddnode(right).right.resultdef) and (taddnode(right).right.nodetype=ordconstn) and (tordconstnode(taddnode(right).right).value=10000) and
  3223. is_currency(taddnode(right).left.resultdef) and (taddnode(right).left.nodetype=typeconvn) then
  3224. begin
  3225. hp:=taddnode(right).left.getcopy;
  3226. include(hp.flags,nf_is_currency);
  3227. right.free;
  3228. right:=hp;
  3229. hp:=nil;
  3230. end
  3231. else if (left.nodetype=muln) and is_currency(left.resultdef) and
  3232. { do not test swapped here as the internal conversions are only create as "var."*"10000" }
  3233. is_currency(taddnode(left).right.resultdef) and (taddnode(left).right.nodetype=ordconstn) and (tordconstnode(taddnode(left).right).value=10000) and
  3234. is_currency(taddnode(left).left.resultdef) and (taddnode(left).left.nodetype=typeconvn) then
  3235. begin
  3236. hp:=taddnode(left).left.getcopy;
  3237. include(hp.flags,nf_is_currency);
  3238. left.free;
  3239. left:=hp;
  3240. hp:=nil;
  3241. end
  3242. else
  3243. begin
  3244. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  3245. include(hp.flags,nf_is_currency);
  3246. end
  3247. end;
  3248. result:=hp
  3249. end;
  3250. else
  3251. ;
  3252. end;
  3253. end;
  3254. if not(codegenerror) and
  3255. not assigned(result) then
  3256. result:=simplify(false);
  3257. end;
  3258. function taddnode.first_addstring: tnode;
  3259. var
  3260. p: tnode;
  3261. newstatement : tstatementnode;
  3262. tempnode : ttempcreatenode;
  3263. cmpfuncname: string;
  3264. para: tcallparanode;
  3265. begin
  3266. result:=nil;
  3267. { when we get here, we are sure that both the left and the right }
  3268. { node are both strings of the same stringtype (JM) }
  3269. case nodetype of
  3270. addn:
  3271. begin
  3272. if (left.nodetype=stringconstn) and (tstringconstnode(left).len=0) then
  3273. begin
  3274. result:=right;
  3275. left.free;
  3276. left:=nil;
  3277. right:=nil;
  3278. exit;
  3279. end;
  3280. if (right.nodetype=stringconstn) and (tstringconstnode(right).len=0) then
  3281. begin
  3282. result:=left;
  3283. left:=nil;
  3284. right.free;
  3285. right:=nil;
  3286. exit;
  3287. end;
  3288. { create the call to the concat routine both strings as arguments }
  3289. if assigned(aktassignmentnode) and
  3290. (aktassignmentnode.right=self) and
  3291. (
  3292. (aktassignmentnode.left.resultdef=resultdef) or
  3293. (
  3294. is_shortstring(aktassignmentnode.left.resultdef) and
  3295. is_shortstring(resultdef)
  3296. )
  3297. ) and
  3298. valid_for_var(aktassignmentnode.left,false) then
  3299. begin
  3300. para:=ccallparanode.create(
  3301. right,
  3302. ccallparanode.create(
  3303. left,
  3304. ccallparanode.create(aktassignmentnode.left.getcopy,nil)
  3305. )
  3306. );
  3307. if is_ansistring(resultdef) then
  3308. para:=ccallparanode.create(
  3309. cordconstnode.create(
  3310. { don't use getparaencoding(), we have to know
  3311. when the result is rawbytestring }
  3312. tstringdef(resultdef).encoding,
  3313. u16inttype,
  3314. true
  3315. ),
  3316. para
  3317. );
  3318. result:=ccallnode.createintern(
  3319. 'fpc_'+tstringdef(resultdef).stringtypname+'_concat',
  3320. para
  3321. );
  3322. include(aktassignmentnode.assignmentnodeflags,anf_assign_done_in_right);
  3323. firstpass(result);
  3324. end
  3325. else
  3326. begin
  3327. result:=internalstatements(newstatement);
  3328. tempnode:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  3329. addstatement(newstatement,tempnode);
  3330. { initialize the temp, since it will be passed to a
  3331. var-parameter (and finalization, which is performed by the
  3332. ttempcreate node and which takes care of the initialization
  3333. on native targets, is a noop on managed VM targets) }
  3334. if (target_info.system in systems_managed_vm) and
  3335. is_managed_type(resultdef) then
  3336. addstatement(newstatement,cinlinenode.create(in_setlength_x,
  3337. false,
  3338. ccallparanode.create(genintconstnode(0),
  3339. ccallparanode.create(ctemprefnode.create(tempnode),nil))));
  3340. para:=ccallparanode.create(
  3341. right,
  3342. ccallparanode.create(
  3343. left,
  3344. ccallparanode.create(ctemprefnode.create(tempnode),nil)
  3345. )
  3346. );
  3347. if is_ansistring(resultdef) then
  3348. para:=ccallparanode.create(
  3349. cordconstnode.create(
  3350. { don't use getparaencoding(), we have to know
  3351. when the result is rawbytestring }
  3352. tstringdef(resultdef).encoding,
  3353. u16inttype,
  3354. true
  3355. ),
  3356. para
  3357. );
  3358. addstatement(
  3359. newstatement,
  3360. ccallnode.createintern(
  3361. 'fpc_'+tstringdef(resultdef).stringtypname+'_concat',
  3362. para
  3363. )
  3364. );
  3365. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  3366. addstatement(newstatement,ctemprefnode.create(tempnode));
  3367. end;
  3368. { we reused the arguments }
  3369. left := nil;
  3370. right := nil;
  3371. end;
  3372. ltn,lten,gtn,gten,equaln,unequaln :
  3373. begin
  3374. { generate better code for comparison with empty string, we
  3375. only need to compare the length with 0 }
  3376. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  3377. (((left.nodetype=stringconstn) and (tstringconstnode(left).len=0)) or
  3378. ((right.nodetype=stringconstn) and (tstringconstnode(right).len=0))) then
  3379. begin
  3380. { switch so that the constant is always on the right }
  3381. if left.nodetype = stringconstn then
  3382. begin
  3383. p := left;
  3384. left := right;
  3385. right := p;
  3386. nodetype:=swap_relation[nodetype];
  3387. end;
  3388. if is_shortstring(left.resultdef) or
  3389. (nodetype in [gtn,gten,ltn,lten]) or
  3390. (target_info.system in systems_managed_vm) then
  3391. { compare the length with 0 }
  3392. result := caddnode.create(nodetype,
  3393. cinlinenode.create(in_length_x,false,left),
  3394. cordconstnode.create(0,s8inttype,false))
  3395. else { nodetype in [equaln,unequaln] }
  3396. begin
  3397. if is_widestring(left.resultdef) and (tf_winlikewidestring in target_info.flags) then
  3398. begin
  3399. { windows like widestrings requires that we also check the length }
  3400. result:=cinlinenode.create(in_length_x,false,left);
  3401. { and compare its result with 0 }
  3402. result:=caddnode.create(equaln,result,cordconstnode.create(0,s8inttype,false));
  3403. if nodetype=unequaln then
  3404. result:=cnotnode.create(result);
  3405. end
  3406. else
  3407. begin
  3408. { compare the pointer with nil (for ansistrings etc), }
  3409. { faster than getting the length (JM) }
  3410. result:= caddnode.create(nodetype,
  3411. ctypeconvnode.create_internal(left,voidpointertype),
  3412. cpointerconstnode.create(0,voidpointertype));
  3413. end;
  3414. end;
  3415. { left is reused }
  3416. left := nil;
  3417. { right isn't }
  3418. right.free;
  3419. right := nil;
  3420. exit;
  3421. end;
  3422. { no string constant -> call compare routine }
  3423. cmpfuncname := 'fpc_'+tstringdef(left.resultdef).stringtypname+'_compare';
  3424. { for equality checks use optimized version }
  3425. if nodetype in [equaln,unequaln] then
  3426. cmpfuncname := cmpfuncname + '_equal';
  3427. result := ccallnode.createintern(cmpfuncname,
  3428. ccallparanode.create(right,ccallparanode.create(left,nil)));
  3429. { and compare its result with 0 according to the original operator }
  3430. result := caddnode.create(nodetype,result,
  3431. cordconstnode.create(0,s8inttype,false));
  3432. left := nil;
  3433. right := nil;
  3434. end;
  3435. else
  3436. internalerror(2019050520);
  3437. end;
  3438. end;
  3439. function taddnode.first_addset : tnode;
  3440. procedure call_varset_helper(const n : string);
  3441. var
  3442. newstatement : tstatementnode;
  3443. temp : ttempcreatenode;
  3444. begin
  3445. { directly load the result set into the assignee if possible }
  3446. if assigned(aktassignmentnode) and
  3447. (aktassignmentnode.right=self) and
  3448. (aktassignmentnode.left.resultdef=resultdef) and
  3449. valid_for_var(aktassignmentnode.left,false) then
  3450. begin
  3451. result:=ccallnode.createintern(n,
  3452. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3453. ccallparanode.create(aktassignmentnode.left.getcopy,
  3454. ccallparanode.create(right,
  3455. ccallparanode.create(left,nil))))
  3456. );
  3457. { remove reused parts from original node }
  3458. left:=nil;
  3459. right:=nil;
  3460. include(aktassignmentnode.assignmentnodeflags,anf_assign_done_in_right);
  3461. firstpass(result);
  3462. end
  3463. else
  3464. begin
  3465. { add two var sets }
  3466. result:=internalstatements(newstatement);
  3467. { create temp for result }
  3468. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  3469. addstatement(newstatement,temp);
  3470. addstatement(newstatement,ccallnode.createintern(n,
  3471. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3472. ccallparanode.create(ctemprefnode.create(temp),
  3473. ccallparanode.create(right,
  3474. ccallparanode.create(left,nil)))))
  3475. );
  3476. { remove reused parts from original node }
  3477. left:=nil;
  3478. right:=nil;
  3479. { the last statement should return the value as
  3480. location and type, this is done be referencing the
  3481. temp and converting it first from a persistent temp to
  3482. normal temp }
  3483. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  3484. addstatement(newstatement,ctemprefnode.create(temp));
  3485. end;
  3486. end;
  3487. var
  3488. procname: string[31];
  3489. tempn: tnode;
  3490. newstatement : tstatementnode;
  3491. temp : ttempcreatenode;
  3492. no_temp: Boolean;
  3493. begin
  3494. result:=nil;
  3495. case nodetype of
  3496. equaln,unequaln,lten,gten:
  3497. begin
  3498. case nodetype of
  3499. equaln,unequaln:
  3500. procname := 'fpc_varset_comp_sets';
  3501. lten,gten:
  3502. begin
  3503. procname := 'fpc_varset_contains_sets';
  3504. { (left >= right) = (right <= left) }
  3505. if nodetype = gten then
  3506. begin
  3507. tempn := left;
  3508. left := right;
  3509. right := tempn;
  3510. end;
  3511. end;
  3512. else
  3513. internalerror(2013112911);
  3514. end;
  3515. result := ccallnode.createinternres(procname,
  3516. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  3517. ccallparanode.create(right,
  3518. ccallparanode.create(left,nil))),resultdef);
  3519. { left and right are reused as parameters }
  3520. left := nil;
  3521. right := nil;
  3522. { for an unequaln, we have to negate the result of comp_sets }
  3523. if nodetype = unequaln then
  3524. result := cnotnode.create(result);
  3525. end;
  3526. addn:
  3527. begin
  3528. { can we directly write into the result? }
  3529. no_temp:=assigned(aktassignmentnode) and
  3530. (aktassignmentnode.right=self) and
  3531. (aktassignmentnode.left.resultdef=self.resultdef) and
  3532. valid_for_var(aktassignmentnode.left,false);
  3533. { optimize first loading of a set }
  3534. if (right.nodetype=setelementn) and
  3535. not(assigned(tsetelementnode(right).right)) and
  3536. is_emptyset(left) then
  3537. begin
  3538. { adjust for set base }
  3539. tsetelementnode(right).left:=caddnode.create(subn,
  3540. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  3541. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  3542. if no_temp then
  3543. begin
  3544. result:=ccallnode.createintern('fpc_varset_create_element',
  3545. ccallparanode.create(aktassignmentnode.left.getcopy,
  3546. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3547. ccallparanode.create(tsetelementnode(right).left,nil))));
  3548. include(aktassignmentnode.assignmentnodeflags,anf_assign_done_in_right);
  3549. end
  3550. else
  3551. begin
  3552. result:=internalstatements(newstatement);
  3553. { create temp for result }
  3554. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  3555. addstatement(newstatement,temp);
  3556. addstatement(newstatement,ccallnode.createintern('fpc_varset_create_element',
  3557. ccallparanode.create(ctemprefnode.create(temp),
  3558. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3559. ccallparanode.create(tsetelementnode(right).left,nil))))
  3560. );
  3561. { the last statement should return the value as
  3562. location and type, this is done be referencing the
  3563. temp and converting it first from a persistent temp to
  3564. normal temp }
  3565. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  3566. addstatement(newstatement,ctemprefnode.create(temp));
  3567. end;
  3568. tsetelementnode(right).left:=nil;
  3569. end
  3570. else
  3571. begin
  3572. if right.nodetype=setelementn then
  3573. begin
  3574. if no_temp then
  3575. begin
  3576. { add a range or a single element? }
  3577. if assigned(tsetelementnode(right).right) then
  3578. begin
  3579. { adjust for set base }
  3580. tsetelementnode(right).left:=caddnode.create(subn,
  3581. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  3582. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  3583. { adjust for set base }
  3584. tsetelementnode(right).right:=caddnode.create(subn,
  3585. ctypeconvnode.create_internal(tsetelementnode(right).right,sinttype),
  3586. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  3587. result:=ccallnode.createintern('fpc_varset_set_range',
  3588. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3589. ccallparanode.create(tsetelementnode(right).right,
  3590. ccallparanode.create(tsetelementnode(right).left,
  3591. ccallparanode.create(aktassignmentnode.left.getcopy,
  3592. ccallparanode.create(left,nil))))));
  3593. end
  3594. else
  3595. begin
  3596. { s:=s+[element]; ? }
  3597. if left.isequal(aktassignmentnode.left) then
  3598. result:=cinlinenode.createintern(in_include_x_y,false,ccallparanode.create(aktassignmentnode.left.getcopy,
  3599. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,tsetdef(resultdef).elementdef),nil)))
  3600. else
  3601. begin
  3602. { adjust for set base }
  3603. tsetelementnode(right).left:=caddnode.create(subn,
  3604. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  3605. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  3606. result:=ccallnode.createintern('fpc_varset_set',
  3607. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3608. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  3609. ccallparanode.create(aktassignmentnode.left.getcopy,
  3610. ccallparanode.create(left,nil)))));
  3611. end;
  3612. end;
  3613. include(aktassignmentnode.assignmentnodeflags,anf_assign_done_in_right);
  3614. end
  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:=internalstatements(newstatement);
  3622. { create temp for result }
  3623. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  3624. addstatement(newstatement,temp);
  3625. { add a range or a single element? }
  3626. if assigned(tsetelementnode(right).right) then
  3627. begin
  3628. { adjust for set base }
  3629. tsetelementnode(right).right:=caddnode.create(subn,
  3630. ctypeconvnode.create_internal(tsetelementnode(right).right,sinttype),
  3631. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  3632. addstatement(newstatement,ccallnode.createintern('fpc_varset_set_range',
  3633. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3634. ccallparanode.create(tsetelementnode(right).right,
  3635. ccallparanode.create(tsetelementnode(right).left,
  3636. ccallparanode.create(ctemprefnode.create(temp),
  3637. ccallparanode.create(left,nil))))))
  3638. );
  3639. end
  3640. else
  3641. addstatement(newstatement,ccallnode.createintern('fpc_varset_set',
  3642. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  3643. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  3644. ccallparanode.create(ctemprefnode.create(temp),
  3645. ccallparanode.create(left,nil)))))
  3646. );
  3647. { the last statement should return the value as
  3648. location and type, this is done be referencing the
  3649. temp and converting it first from a persistent temp to
  3650. normal temp }
  3651. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  3652. addstatement(newstatement,ctemprefnode.create(temp));
  3653. end;
  3654. { remove reused parts from original node }
  3655. tsetelementnode(right).right:=nil;
  3656. tsetelementnode(right).left:=nil;
  3657. left:=nil;
  3658. end
  3659. else
  3660. call_varset_helper('fpc_varset_add_sets');
  3661. end
  3662. end;
  3663. subn:
  3664. call_varset_helper('fpc_varset_sub_sets');
  3665. symdifn:
  3666. call_varset_helper('fpc_varset_symdif_sets');
  3667. muln:
  3668. call_varset_helper('fpc_varset_mul_sets');
  3669. else
  3670. internalerror(200609241);
  3671. end;
  3672. end;
  3673. function taddnode.first_adddynarray : tnode;
  3674. var
  3675. newstatement : tstatementnode;
  3676. tempnode (*,tempnode2*) : ttempcreatenode;
  3677. para: tcallparanode;
  3678. begin
  3679. result:=nil;
  3680. { when we get here, we are sure that both the left and the right }
  3681. { node are both strings of the same stringtype (JM) }
  3682. case nodetype of
  3683. addn:
  3684. begin
  3685. if (left.nodetype=arrayconstructorn) and (tarrayconstructornode(left).isempty) then
  3686. begin
  3687. result:=right;
  3688. left.free;
  3689. left:=nil;
  3690. right:=nil;
  3691. exit;
  3692. end;
  3693. if (right.nodetype=arrayconstructorn) and (tarrayconstructornode(right).isempty) then
  3694. begin
  3695. result:=left;
  3696. left:=nil;
  3697. right.free;
  3698. right:=nil;
  3699. exit;
  3700. end;
  3701. { create the call to the concat routine both strings as arguments }
  3702. if assigned(aktassignmentnode) and
  3703. (aktassignmentnode.right=self) and
  3704. (aktassignmentnode.left.resultdef=resultdef) and
  3705. valid_for_var(aktassignmentnode.left,false) then
  3706. begin
  3707. para:=ccallparanode.create(
  3708. ctypeconvnode.create_internal(right,voidcodepointertype),
  3709. ccallparanode.create(
  3710. ctypeconvnode.create_internal(left,voidcodepointertype),
  3711. ccallparanode.create(
  3712. caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  3713. ccallparanode.create(
  3714. ctypeconvnode.create_internal(aktassignmentnode.left.getcopy,voidcodepointertype),nil)
  3715. )));
  3716. result:=ccallnode.createintern(
  3717. 'fpc_dynarray_concat',
  3718. para
  3719. );
  3720. include(aktassignmentnode.assignmentnodeflags,anf_assign_done_in_right);
  3721. firstpass(result);
  3722. end
  3723. else
  3724. begin
  3725. result:=internalstatements(newstatement);
  3726. tempnode:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  3727. addstatement(newstatement,tempnode);
  3728. { initialize the temp, since it will be passed to a
  3729. var-parameter (and finalization, which is performed by the
  3730. ttempcreate node and which takes care of the initialization
  3731. on native targets, is a noop on managed VM targets) }
  3732. if (target_info.system in systems_managed_vm) and
  3733. is_managed_type(resultdef) then
  3734. addstatement(newstatement,cinlinenode.create(in_setlength_x,
  3735. false,
  3736. ccallparanode.create(genintconstnode(0),
  3737. ccallparanode.create(ctemprefnode.create(tempnode),nil))));
  3738. para:=ccallparanode.create(
  3739. ctypeconvnode.create_internal(right,voidcodepointertype),
  3740. ccallparanode.create(
  3741. ctypeconvnode.create_internal(left,voidcodepointertype),
  3742. ccallparanode.create(
  3743. caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  3744. ccallparanode.create(
  3745. ctypeconvnode.create_internal(ctemprefnode.create(tempnode),voidcodepointertype),nil)
  3746. )));
  3747. addstatement(
  3748. newstatement,
  3749. ccallnode.createintern(
  3750. 'fpc_dynarray_concat',
  3751. para
  3752. )
  3753. );
  3754. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  3755. addstatement(newstatement,ctemprefnode.create(tempnode));
  3756. end;
  3757. { we reused the arguments }
  3758. left := nil;
  3759. right := nil;
  3760. end;
  3761. unequaln,equaln:
  3762. expectloc:=LOC_FLAGS;
  3763. else
  3764. Internalerror(2018030301);
  3765. end;
  3766. end;
  3767. function taddnode.use_generic_mul32to64: boolean;
  3768. begin
  3769. result := true;
  3770. end;
  3771. function taddnode.use_generic_mul64bit: boolean;
  3772. begin
  3773. result := true;
  3774. end;
  3775. function taddnode.try_make_mul32to64: boolean;
  3776. function canbe32bitint(v: tconstexprint; out canbesignedconst, canbeunsignedconst: boolean): boolean;
  3777. begin
  3778. result := ((v >= int64(low(longint))) and (v <= int64(high(longint)))) or
  3779. ((v >= qword(low(cardinal))) and (v <= qword(high(cardinal))));
  3780. canbesignedconst:=v<=int64(high(longint));
  3781. canbeunsignedconst:=v>=0;
  3782. end;
  3783. function is_32bitordconst(n: tnode; out canbesignedconst, canbeunsignedconst: boolean): boolean;
  3784. begin
  3785. canbesignedconst:=false;
  3786. canbeunsignedconst:=false;
  3787. result := (n.nodetype = ordconstn) and
  3788. canbe32bitint(tordconstnode(n).value, canbesignedconst, canbeunsignedconst);
  3789. end;
  3790. function is_32to64typeconv(n: tnode): boolean;
  3791. begin
  3792. result := (n.nodetype = typeconvn) and
  3793. is_integer(ttypeconvnode(n).left.resultdef) and
  3794. not is_64bit(ttypeconvnode(n).left.resultdef);
  3795. end;
  3796. var
  3797. temp: tnode;
  3798. leftoriginallysigned,
  3799. canbesignedconst, canbeunsignedconst, swapped: boolean;
  3800. begin
  3801. result := false;
  3802. swapped := false;
  3803. { make sure that if there is a constant, that it's on the right }
  3804. if left.nodetype = ordconstn then
  3805. begin
  3806. swapleftright;
  3807. swapped := true;
  3808. end;
  3809. if is_32to64typeconv(left) then
  3810. begin
  3811. leftoriginallysigned:=is_signed(ttypeconvnode(left).left.resultdef);
  3812. if ((is_32bitordconst(right,canbesignedconst, canbeunsignedconst) and
  3813. ((leftoriginallysigned and canbesignedconst) or
  3814. (not leftoriginallysigned and canbeunsignedconst))) or
  3815. (is_32to64typeconv(right) and
  3816. ((leftoriginallysigned =
  3817. is_signed(ttypeconvnode(right).left.resultdef)) or
  3818. (leftoriginallysigned and
  3819. (torddef(ttypeconvnode(right).left.resultdef).ordtype in [u8bit,u16bit]))))) then
  3820. begin
  3821. temp := ttypeconvnode(left).left;
  3822. ttypeconvnode(left).left := nil;
  3823. left.free;
  3824. left := temp;
  3825. if (right.nodetype = typeconvn) then
  3826. begin
  3827. temp := ttypeconvnode(right).left;
  3828. ttypeconvnode(right).left := nil;
  3829. right.free;
  3830. right := temp;
  3831. end;
  3832. if (is_signed(left.resultdef)) then
  3833. begin
  3834. inserttypeconv_internal(left,s32inttype);
  3835. inserttypeconv_internal(right,s32inttype);
  3836. end
  3837. else
  3838. begin
  3839. inserttypeconv_internal(left,u32inttype);
  3840. inserttypeconv_internal(right,u32inttype);
  3841. end;
  3842. firstpass(left);
  3843. firstpass(right);
  3844. result := true;
  3845. end;
  3846. end;
  3847. { pass_Typecheck caches left/right type and resultdef, so restore the
  3848. original order }
  3849. if not result and swapped then
  3850. swapleftright;
  3851. end;
  3852. function taddnode.use_fma : boolean;
  3853. begin
  3854. result:=false;
  3855. end;
  3856. function taddnode.try_fma(ld,rd : tdef) : tnode;
  3857. var
  3858. inlinennr : tinlinenumber;
  3859. begin
  3860. result:=nil;
  3861. if (cs_opt_fastmath in current_settings.optimizerswitches) and
  3862. use_fma and
  3863. (nodetype in [addn,subn]) and
  3864. (rd.typ=floatdef) and (ld.typ=floatdef) and
  3865. (is_single(rd) or is_double(rd)) and
  3866. equal_defs(rd,ld) and
  3867. { transforming a*b+c into fma(a,b,c) makes only sense if c can be
  3868. calculated easily. Consider a*b+c*d which results in
  3869. fmul
  3870. fmul
  3871. fadd
  3872. and in
  3873. fmul
  3874. fma
  3875. when using the fma optimization. On a super scalar architecture, the first instruction
  3876. sequence requires clock_cycles(fmul)+clock_cycles(fadd) clock cycles because the fmuls can be executed in parallel.
  3877. The second sequence requires clock_cycles(fmul)+clock_cycles(fma) because the fma has to wait for the
  3878. result of the fmul. Since typically clock_cycles(fma)>clock_cycles(fadd) applies, the first sequence is better.
  3879. }
  3880. (((left.nodetype=muln) and (node_complexity(right)<3)) or
  3881. ((right.nodetype=muln) and (node_complexity(left)<3)) or
  3882. ((left.nodetype=inlinen) and
  3883. (tinlinenode(left).inlinenumber=in_sqr_real) and
  3884. (node_complexity(right)<3)) or
  3885. ((right.nodetype=inlinen) and
  3886. (tinlinenode(right).inlinenumber=in_sqr_real) and
  3887. (node_complexity(left)<3))
  3888. ) then
  3889. begin
  3890. case tfloatdef(ld).floattype of
  3891. s32real:
  3892. inlinennr:=in_fma_single;
  3893. s64real:
  3894. inlinennr:=in_fma_double;
  3895. s80real:
  3896. inlinennr:=in_fma_extended;
  3897. s128real:
  3898. inlinennr:=in_fma_float128;
  3899. else
  3900. internalerror(2014042601);
  3901. end;
  3902. if left.nodetype=muln then
  3903. begin
  3904. if nodetype=subn then
  3905. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(cunaryminusnode.create(right),
  3906. ccallparanode.create(taddnode(left).right,
  3907. ccallparanode.create(taddnode(left).left,nil
  3908. ))))
  3909. else
  3910. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(right,
  3911. ccallparanode.create(taddnode(left).right,
  3912. ccallparanode.create(taddnode(left).left,nil
  3913. ))));
  3914. right:=nil;
  3915. taddnode(left).right:=nil;
  3916. taddnode(left).left:=nil;
  3917. end
  3918. else if right.nodetype=muln then
  3919. begin
  3920. if nodetype=subn then
  3921. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  3922. ccallparanode.create(cunaryminusnode.create(taddnode(right).right),
  3923. ccallparanode.create(taddnode(right).left,nil
  3924. ))))
  3925. else
  3926. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  3927. ccallparanode.create(taddnode(right).right,
  3928. ccallparanode.create(taddnode(right).left,nil
  3929. ))));
  3930. left:=nil;
  3931. taddnode(right).right:=nil;
  3932. taddnode(right).left:=nil;
  3933. end
  3934. else if (left.nodetype=inlinen) and (tinlinenode(left).inlinenumber=in_sqr_real) then
  3935. begin
  3936. if node_complexity(tinlinenode(left).left)=0 then
  3937. begin
  3938. if nodetype=subn then
  3939. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(cunaryminusnode.create(right),
  3940. ccallparanode.create(tinlinenode(left).left.getcopy,
  3941. ccallparanode.create(tinlinenode(left).left.getcopy,nil
  3942. ))))
  3943. else
  3944. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(right,
  3945. ccallparanode.create(tinlinenode(left).left.getcopy,
  3946. ccallparanode.create(tinlinenode(left).left.getcopy,nil
  3947. ))));
  3948. right:=nil;
  3949. end;
  3950. end
  3951. { we get here only if right is a sqr node }
  3952. else if (right.nodetype=inlinen) and (tinlinenode(right).inlinenumber=in_sqr_real) then
  3953. begin
  3954. if node_complexity(tinlinenode(right).left)=0 then
  3955. begin
  3956. if nodetype=subn then
  3957. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  3958. ccallparanode.create(cunaryminusnode.create(tinlinenode(right).left.getcopy),
  3959. ccallparanode.create(tinlinenode(right).left.getcopy,nil
  3960. ))))
  3961. else
  3962. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  3963. ccallparanode.create(tinlinenode(right).left.getcopy,
  3964. ccallparanode.create(tinlinenode(right).left.getcopy,nil
  3965. ))));
  3966. left:=nil;
  3967. end;
  3968. end;
  3969. end;
  3970. end;
  3971. function taddnode.first_add64bitint: tnode;
  3972. var
  3973. procname: string[31];
  3974. power: longint;
  3975. begin
  3976. result := nil;
  3977. { create helper calls mul }
  3978. if nodetype <> muln then
  3979. exit;
  3980. { make sure that if there is a constant, that it's on the right }
  3981. if left.nodetype = ordconstn then
  3982. swapleftright;
  3983. { can we use a shift instead of a mul? }
  3984. if not (cs_check_overflow in current_settings.localswitches) and
  3985. (right.nodetype = ordconstn) and
  3986. ispowerof2(tordconstnode(right).value,power) then
  3987. begin
  3988. tordconstnode(right).value := power;
  3989. result := cshlshrnode.create(shln,left,right);
  3990. { left and right are reused }
  3991. left := nil;
  3992. right := nil;
  3993. { return firstpassed new node }
  3994. exit;
  3995. end;
  3996. if try_make_mul32to64 then
  3997. begin
  3998. { this uses the same criteria for signedness as the 32 to 64-bit mul
  3999. handling in the i386 code generator }
  4000. if is_signed(left.resultdef) and is_signed(right.resultdef) then
  4001. procname := 'fpc_mul_longint_to_int64'
  4002. else
  4003. procname := 'fpc_mul_dword_to_qword';
  4004. right := ccallparanode.create(right,ccallparanode.create(left,nil));
  4005. result := ccallnode.createintern(procname,right);
  4006. left := nil;
  4007. right := nil;
  4008. end
  4009. else
  4010. begin
  4011. { can full 64-bit multiplication be handled inline? }
  4012. if not use_generic_mul64bit then
  4013. begin
  4014. { generic handling replaces this node with call to fpc_mul_int64,
  4015. whose result is int64 }
  4016. if is_currency(resultdef) then
  4017. resultdef:=s64inttype;
  4018. exit;
  4019. end;
  4020. { when currency is used set the result of the
  4021. parameters to s64bit, so they are not converted }
  4022. if is_currency(resultdef) then
  4023. begin
  4024. left.resultdef:=s64inttype;
  4025. right.resultdef:=s64inttype;
  4026. end;
  4027. { otherwise, create the parameters for the helper }
  4028. right := ccallparanode.create(right,ccallparanode.create(left,nil));
  4029. left := nil;
  4030. { only qword needs the unsigned code, the
  4031. signed code is also used for currency }
  4032. if is_signed(resultdef) then
  4033. procname := 'fpc_mul_int64'
  4034. else
  4035. procname := 'fpc_mul_qword';
  4036. if cs_check_overflow in current_settings.localswitches then
  4037. procname := procname + '_checkoverflow';
  4038. result := ccallnode.createintern(procname,right);
  4039. right := nil;
  4040. end;
  4041. end;
  4042. function taddnode.first_addpointer: tnode;
  4043. begin
  4044. result:=nil;
  4045. expectloc:=LOC_REGISTER;
  4046. end;
  4047. function taddnode.first_cmppointer: tnode;
  4048. begin
  4049. result:=nil;
  4050. expectloc:=LOC_FLAGS;
  4051. end;
  4052. function taddnode.first_addfloat_soft : tnode;
  4053. var
  4054. procname: string[31];
  4055. { do we need to reverse the result ? }
  4056. notnode : boolean;
  4057. fdef : tdef;
  4058. begin
  4059. notnode:=false;
  4060. result:=nil;
  4061. fdef:=nil;
  4062. if not(target_info.system in systems_wince) then
  4063. begin
  4064. case tfloatdef(left.resultdef).floattype of
  4065. s32real:
  4066. begin
  4067. fdef:=search_system_type('FLOAT32REC').typedef;
  4068. procname:='float32';
  4069. end;
  4070. s64real:
  4071. begin
  4072. fdef:=search_system_type('FLOAT64').typedef;
  4073. procname:='float64';
  4074. end;
  4075. {!!! not yet implemented
  4076. s128real:
  4077. }
  4078. else
  4079. internalerror(2005082601);
  4080. end;
  4081. case nodetype of
  4082. addn:
  4083. procname:=procname+'_add';
  4084. muln:
  4085. procname:=procname+'_mul';
  4086. subn:
  4087. procname:=procname+'_sub';
  4088. slashn:
  4089. procname:=procname+'_div';
  4090. ltn:
  4091. procname:=procname+'_lt';
  4092. lten:
  4093. procname:=procname+'_le';
  4094. gtn:
  4095. begin
  4096. procname:=procname+'_lt';
  4097. swapleftright;
  4098. end;
  4099. gten:
  4100. begin
  4101. procname:=procname+'_le';
  4102. swapleftright;
  4103. end;
  4104. equaln:
  4105. procname:=procname+'_eq';
  4106. unequaln:
  4107. begin
  4108. procname:=procname+'_eq';
  4109. notnode:=true;
  4110. end;
  4111. else
  4112. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  4113. end;
  4114. end
  4115. else
  4116. begin
  4117. case nodetype of
  4118. addn:
  4119. procname:='add';
  4120. muln:
  4121. procname:='mul';
  4122. subn:
  4123. procname:='sub';
  4124. slashn:
  4125. procname:='div';
  4126. ltn:
  4127. procname:='lt';
  4128. lten:
  4129. procname:='le';
  4130. gtn:
  4131. procname:='gt';
  4132. gten:
  4133. procname:='ge';
  4134. equaln:
  4135. procname:='eq';
  4136. unequaln:
  4137. procname:='ne';
  4138. else
  4139. begin
  4140. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  4141. exit;
  4142. end;
  4143. end;
  4144. case tfloatdef(left.resultdef).floattype of
  4145. s32real:
  4146. begin
  4147. procname:=procname+'s';
  4148. if nodetype in [addn,muln,subn,slashn] then
  4149. procname:=lower(procname);
  4150. end;
  4151. s64real:
  4152. procname:=procname+'d';
  4153. {!!! not yet implemented
  4154. s128real:
  4155. }
  4156. else
  4157. internalerror(2005082602);
  4158. end;
  4159. end;
  4160. { cast softfpu result? }
  4161. if not(target_info.system in systems_wince) then
  4162. begin
  4163. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  4164. resultdef:=pasbool1type;
  4165. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  4166. ctypeconvnode.create_internal(right,fdef),
  4167. ccallparanode.create(
  4168. ctypeconvnode.create_internal(left,fdef),nil))),resultdef);
  4169. end
  4170. else
  4171. result:=ccallnode.createintern(procname,ccallparanode.create(right,
  4172. ccallparanode.create(left,nil)));
  4173. left:=nil;
  4174. right:=nil;
  4175. { do we need to reverse the result }
  4176. if notnode then
  4177. result:=cnotnode.create(result);
  4178. end;
  4179. function taddnode.first_addfloat : tnode;
  4180. begin
  4181. result := nil;
  4182. { In non-emulation mode, real opcodes are
  4183. emitted for floating point values.
  4184. }
  4185. if not ((cs_fp_emulation in current_settings.moduleswitches)
  4186. {$ifdef cpufpemu}
  4187. or (current_settings.fputype=fpu_soft)
  4188. {$endif cpufpemu}
  4189. ) then
  4190. exit;
  4191. result:=first_addfloat_soft
  4192. end;
  4193. {$ifdef cpuneedsmulhelper}
  4194. function taddnode.use_mul_helper: boolean;
  4195. begin
  4196. result:=(nodetype=muln) and
  4197. not(torddef(resultdef).ordtype in [u8bit,s8bit
  4198. {$if defined(cpu16bitalu) or defined(avr)},u16bit,s16bit{$endif}]);
  4199. end;
  4200. {$endif cpuneedsmulhelper}
  4201. function taddnode.pass_1 : tnode;
  4202. function isconstsetfewelements(p : tnode) : boolean;
  4203. begin
  4204. result:=(p.nodetype=setconstn) and (tsetconstnode(p).elements<=4);
  4205. end;
  4206. var
  4207. rd,ld : tdef;
  4208. i,i2 : longint;
  4209. lt,rt : tnodetype;
  4210. {$ifdef cpuneedsmulhelper}
  4211. procname : string[32];
  4212. {$endif cpuneedsmulhelper}
  4213. tempn,varsetnode: tnode;
  4214. mulnode : taddnode;
  4215. constsetnode : tsetconstnode;
  4216. trycreateinnodes : Boolean;
  4217. begin
  4218. result:=nil;
  4219. { Can we optimize multiple string additions into a single call?
  4220. This need to be done on a complete tree to detect the multiple
  4221. add nodes and is therefor done before the subtrees are processed }
  4222. if canbemultistringadd(self) then
  4223. begin
  4224. result:=genmultistringadd(self);
  4225. exit;
  4226. end;
  4227. { Can we optimize multiple dyn. array additions into a single call?
  4228. This need to be done on a complete tree to detect the multiple
  4229. add nodes and is therefor done before the subtrees are processed }
  4230. if (m_array_operators in current_settings.modeswitches) and canbemultidynarrayadd(self) then
  4231. begin
  4232. result:=genmultidynarrayadd(self);
  4233. exit;
  4234. end;
  4235. { typical set tests like (s*[const. set])<>/=[] can be converted into an or'ed chain of in tests
  4236. for var sets if const. set contains only a few elements }
  4237. 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
  4238. begin
  4239. trycreateinnodes:=false;
  4240. mulnode:=nil;
  4241. if (is_emptyset(right) and (left.nodetype=muln) and
  4242. (isconstsetfewelements(taddnode(left).right) or isconstsetfewelements(taddnode(left).left))) then
  4243. begin
  4244. trycreateinnodes:=true;
  4245. mulnode:=taddnode(left);
  4246. end
  4247. else if (is_emptyset(left) and (right.nodetype=muln) and
  4248. (isconstsetfewelements(taddnode(right).right) or isconstsetfewelements(taddnode(right).left))) then
  4249. begin
  4250. trycreateinnodes:=true;
  4251. mulnode:=taddnode(right);
  4252. end;
  4253. if trycreateinnodes then
  4254. begin
  4255. constsetnode:=nil;
  4256. varsetnode:=nil;
  4257. if isconstsetfewelements(mulnode.right) then
  4258. begin
  4259. constsetnode:=tsetconstnode(mulnode.right);
  4260. varsetnode:=mulnode.left;
  4261. end
  4262. else
  4263. begin
  4264. constsetnode:=tsetconstnode(mulnode.left);
  4265. varsetnode:=mulnode.right;
  4266. end;
  4267. { the node is copied so it might have no side effects, if the complexity is too, cse should fix it, so
  4268. do not check complexity }
  4269. if not(might_have_sideeffects(varsetnode)) then
  4270. begin
  4271. result:=nil;
  4272. for i:=low(tconstset) to high(tconstset) do
  4273. if i in constsetnode.value_set^ then
  4274. begin
  4275. tempn:=cinnode.create(cordconstnode.create(i,tsetdef(constsetnode.resultdef).elementdef,false),varsetnode.getcopy);
  4276. if assigned(result) then
  4277. result:=caddnode.create_internal(orn,result,tempn)
  4278. else
  4279. result:=tempn;
  4280. end;
  4281. if nodetype=equaln then
  4282. result:=cnotnode.create(result);
  4283. exit;
  4284. end;
  4285. end;
  4286. end;
  4287. { get rid of adding empty sets generated by set constructors (s+([]+[..]))
  4288. this needs to be done before firstpass, else the set additions get already converted into calls }
  4289. if (resultdef.typ=setdef) and (nodetype=addn) and (right.nodetype=addn) and (is_emptyset(taddnode(right).left)) then
  4290. begin
  4291. result:=caddnode.create(addn,left,taddnode(right).right);
  4292. left:=nil;
  4293. taddnode(right).right:=nil;
  4294. exit;
  4295. end;
  4296. { first do the two subtrees }
  4297. firstpass(left);
  4298. firstpass(right);
  4299. if codegenerror then
  4300. exit;
  4301. { load easier access variables }
  4302. rd:=right.resultdef;
  4303. ld:=left.resultdef;
  4304. rt:=right.nodetype;
  4305. lt:=left.nodetype;
  4306. { int/int gives real/real! }
  4307. if nodetype=slashn then
  4308. begin
  4309. {$ifdef cpufpemu}
  4310. result:=first_addfloat;
  4311. if assigned(result) then
  4312. exit;
  4313. {$endif cpufpemu}
  4314. expectloc:=LOC_FPUREGISTER;
  4315. end
  4316. { if both are orddefs then check sub types }
  4317. else if (ld.typ=orddef) and (rd.typ=orddef) then
  4318. begin
  4319. { optimize multiplacation by a power of 2 }
  4320. if not(cs_check_overflow in current_settings.localswitches) and
  4321. (nodetype = muln) and
  4322. (((left.nodetype = ordconstn) and
  4323. ispowerof2(tordconstnode(left).value,i)) or
  4324. ((right.nodetype = ordconstn) and
  4325. ispowerof2(tordconstnode(right).value,i2))) then
  4326. begin
  4327. { it could be that we are converting a 32x32 -> 64 multiplication:
  4328. in this case, we have to restore the type conversion }
  4329. inserttypeconv_internal(left,resultdef);
  4330. inserttypeconv_internal(right,resultdef);
  4331. if ((left.nodetype = ordconstn) and
  4332. ispowerof2(tordconstnode(left).value,i)) then
  4333. begin
  4334. tordconstnode(left).value := i;
  4335. result := cshlshrnode.create(shln,right,left);
  4336. end
  4337. else
  4338. begin
  4339. tordconstnode(right).value := i2;
  4340. result := cshlshrnode.create(shln,left,right);
  4341. end;
  4342. result.resultdef := resultdef;
  4343. left := nil;
  4344. right := nil;
  4345. exit;
  4346. end;
  4347. { 2 booleans ? }
  4348. if is_boolean(ld) and is_boolean(rd) then
  4349. begin
  4350. if doshortbooleval(self) then
  4351. expectloc:=LOC_JUMP
  4352. else
  4353. begin
  4354. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  4355. expectloc:=LOC_FLAGS
  4356. else
  4357. expectloc:=LOC_REGISTER;
  4358. end;
  4359. end
  4360. else
  4361. { Both are chars? only convert to shortstrings for addn }
  4362. if is_char(ld) then
  4363. begin
  4364. if nodetype=addn then
  4365. internalerror(200103291);
  4366. expectloc:=LOC_FLAGS;
  4367. end
  4368. else if (nodetype=muln) and
  4369. is_64bitint(resultdef) and
  4370. not use_generic_mul32to64 and
  4371. try_make_mul32to64 then
  4372. begin
  4373. { if the code generator can handle 32 to 64-bit muls,
  4374. we're done here }
  4375. expectloc:=LOC_REGISTER;
  4376. end
  4377. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  4378. { is there a 64 bit type ? }
  4379. else if (torddef(ld).ordtype in [s64bit,u64bit,scurrency]) then
  4380. begin
  4381. result := first_add64bitint;
  4382. if assigned(result) then
  4383. exit;
  4384. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  4385. expectloc:=LOC_REGISTER
  4386. else
  4387. expectloc:=LOC_JUMP;
  4388. end
  4389. {$elseif defined(llvm) and defined(cpu32bitalu)}
  4390. { llvm does not support 128 bit math on 32 bit targets, which is
  4391. necessary for overflow checking 64 bit operations }
  4392. else if (torddef(ld).ordtype in [s64bit,u64bit,scurrency]) and
  4393. (cs_check_overflow in current_settings.localswitches) and
  4394. (nodetype in [addn,subn,muln]) 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(wasm)}
  4405. { WebAssembly does not support overflow checking for 64-bit multiplication }
  4406. else if (torddef(ld).ordtype in [s64bit,u64bit,scurrency]) and
  4407. (cs_check_overflow in current_settings.localswitches) and
  4408. (nodetype = muln) then
  4409. begin
  4410. result := first_add64bitint;
  4411. if assigned(result) then
  4412. exit;
  4413. expectloc:=LOC_REGISTER
  4414. end
  4415. {$endif not(cpu64bitalu) and not(cpuhighleveltarget)}
  4416. { generic 32bit conversion }
  4417. else
  4418. begin
  4419. {$ifdef cpuneedsmulhelper}
  4420. if use_mul_helper then
  4421. begin
  4422. result := nil;
  4423. case torddef(resultdef).ordtype of
  4424. s8bit:
  4425. procname := 'fpc_mul_shortint';
  4426. u8bit:
  4427. procname := 'fpc_mul_byte';
  4428. s16bit:
  4429. procname := 'fpc_mul_integer';
  4430. u16bit:
  4431. procname := 'fpc_mul_word';
  4432. s32bit:
  4433. procname := 'fpc_mul_longint';
  4434. u32bit:
  4435. procname := 'fpc_mul_dword';
  4436. else
  4437. internalerror(2011022301);
  4438. end;
  4439. if cs_check_overflow in current_settings.localswitches then
  4440. procname:=procname+'_checkoverflow';
  4441. result := ccallnode.createintern(procname,
  4442. ccallparanode.create(right,
  4443. ccallparanode.create(left,nil)));
  4444. left := nil;
  4445. right := nil;
  4446. firstpass(result);
  4447. exit;
  4448. end;
  4449. {$endif cpuneedsmulhelper}
  4450. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  4451. expectloc:=LOC_REGISTER
  4452. {$if not defined(cpuhighleveltarget)}
  4453. else if torddef(ld).size>sizeof(aint) then
  4454. expectloc:=LOC_JUMP
  4455. {$endif}
  4456. else
  4457. expectloc:=LOC_FLAGS;
  4458. end;
  4459. end
  4460. { left side a setdef, must be before string processing,
  4461. else array constructor can be seen as array of char (PFV) }
  4462. else if (ld.typ=setdef) then
  4463. begin
  4464. { small sets are handled inline by the compiler.
  4465. small set doesn't have support for adding ranges }
  4466. if is_smallset(ld) and
  4467. not(
  4468. (right.nodetype=setelementn) and
  4469. assigned(tsetelementnode(right).right)
  4470. ) then
  4471. begin
  4472. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  4473. expectloc:=LOC_FLAGS
  4474. else
  4475. expectloc:=LOC_REGISTER;
  4476. end
  4477. else
  4478. begin
  4479. result := first_addset;
  4480. if assigned(result) then
  4481. exit;
  4482. expectloc:=LOC_CREFERENCE;
  4483. end;
  4484. end
  4485. { compare pchar by addresses like BP/Delphi }
  4486. else if is_pchar(ld) then
  4487. begin
  4488. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  4489. result:=first_addpointer
  4490. else
  4491. result:=first_cmppointer;
  4492. end
  4493. { is one of the operands a string }
  4494. else if (ld.typ=stringdef) then
  4495. begin
  4496. if is_widestring(ld) then
  4497. begin
  4498. { this is only for add, the comparison is handled later }
  4499. expectloc:=LOC_REGISTER;
  4500. end
  4501. else if is_unicodestring(ld) then
  4502. begin
  4503. { this is only for add, the comparison is handled later }
  4504. expectloc:=LOC_REGISTER;
  4505. end
  4506. else if is_ansistring(ld) then
  4507. begin
  4508. { this is only for add, the comparison is handled later }
  4509. expectloc:=LOC_REGISTER;
  4510. end
  4511. else if is_longstring(ld) then
  4512. begin
  4513. { this is only for add, the comparison is handled later }
  4514. expectloc:=LOC_REFERENCE;
  4515. end
  4516. else
  4517. begin
  4518. {$ifdef addstringopt}
  4519. { can create a call which isn't handled by callparatemp }
  4520. if canbeaddsstringcharoptnode(self) then
  4521. begin
  4522. hp := genaddsstringcharoptnode(self);
  4523. pass_1 := hp;
  4524. exit;
  4525. end
  4526. else
  4527. {$endif addstringopt}
  4528. begin
  4529. { Fix right to be shortstring }
  4530. if is_char(right.resultdef) then
  4531. begin
  4532. inserttypeconv(right,cshortstringtype);
  4533. firstpass(right);
  4534. end;
  4535. end;
  4536. {$ifdef addstringopt}
  4537. { can create a call which isn't handled by callparatemp }
  4538. if canbeaddsstringcsstringoptnode(self) then
  4539. begin
  4540. hp := genaddsstringcsstringoptnode(self);
  4541. pass_1 := hp;
  4542. exit;
  4543. end;
  4544. {$endif addstringopt}
  4545. end;
  4546. { otherwise, let addstring convert everything }
  4547. result := first_addstring;
  4548. exit;
  4549. end
  4550. { is one a real float ? }
  4551. else if (rd.typ=floatdef) or (ld.typ=floatdef) then
  4552. begin
  4553. {$ifdef cpufpemu}
  4554. result:=first_addfloat;
  4555. if assigned(result) then
  4556. exit;
  4557. {$endif cpufpemu}
  4558. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  4559. expectloc:=LOC_FPUREGISTER
  4560. else
  4561. expectloc:=LOC_FLAGS;
  4562. result:=try_fma(ld,rd);
  4563. if assigned(result) then
  4564. exit;
  4565. end
  4566. { pointer comperation and subtraction }
  4567. else if (ld.typ=pointerdef) then
  4568. begin
  4569. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  4570. result:=first_addpointer
  4571. else
  4572. result:=first_cmppointer;
  4573. end
  4574. else if is_implicit_pointer_object_type(ld) then
  4575. begin
  4576. if ld.size>sizeof(aint) then
  4577. expectloc:=LOC_JUMP
  4578. else
  4579. expectloc:=LOC_FLAGS;
  4580. end
  4581. else if (ld.typ=classrefdef) then
  4582. begin
  4583. if ld.size>sizeof(aint) then
  4584. expectloc:=LOC_JUMP
  4585. else
  4586. expectloc:=LOC_FLAGS;
  4587. end
  4588. { support procvar=nil,procvar<>nil }
  4589. else if ((ld.typ=procvardef) and (rt=niln)) or
  4590. ((rd.typ=procvardef) and (lt=niln)) then
  4591. begin
  4592. if (ld.typ=procvardef) and (tprocvardef(ld).size>sizeof(aint)) or
  4593. (rd.typ=procvardef) and (tprocvardef(rd).size>sizeof(aint)) then
  4594. expectloc:=LOC_JUMP
  4595. else
  4596. expectloc:=LOC_FLAGS;
  4597. end
  4598. {$ifdef SUPPORT_MMX}
  4599. { mmx support, this must be before the zero based array
  4600. check }
  4601. else if (cs_mmx in current_settings.localswitches) and is_mmx_able_array(ld) and
  4602. is_mmx_able_array(rd) then
  4603. begin
  4604. expectloc:=LOC_MMXREGISTER;
  4605. end
  4606. {$endif SUPPORT_MMX}
  4607. else if (rd.typ=pointerdef) or (ld.typ=pointerdef) then
  4608. begin
  4609. result:=first_addpointer;
  4610. end
  4611. else if (rd.typ=procvardef) and
  4612. (ld.typ=procvardef) and
  4613. equal_defs(rd,ld) then
  4614. begin
  4615. if tprocvardef(ld).size>sizeof(aint) then
  4616. expectloc:=LOC_JUMP
  4617. else
  4618. expectloc:=LOC_FLAGS;
  4619. end
  4620. else if (ld.typ=enumdef) then
  4621. begin
  4622. if tenumdef(ld).size>sizeof(aint) then
  4623. expectloc:=LOC_JUMP
  4624. else
  4625. expectloc:=LOC_FLAGS;
  4626. end
  4627. {$ifdef SUPPORT_MMX}
  4628. else if (cs_mmx in current_settings.localswitches) and
  4629. is_mmx_able_array(ld) and
  4630. is_mmx_able_array(rd) then
  4631. begin
  4632. expectloc:=LOC_MMXREGISTER;
  4633. end
  4634. {$endif SUPPORT_MMX}
  4635. else if is_dynamic_array(ld) or is_dynamic_array(rd) then
  4636. begin
  4637. result:=first_adddynarray;
  4638. exit;
  4639. end
  4640. { the general solution is to convert to 32 bit int }
  4641. else
  4642. begin
  4643. expectloc:=LOC_REGISTER;
  4644. end;
  4645. end;
  4646. {$ifdef state_tracking}
  4647. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  4648. var factval:Tnode;
  4649. begin
  4650. track_state_pass:=false;
  4651. if left.track_state_pass(exec_known) then
  4652. begin
  4653. track_state_pass:=true;
  4654. left.resultdef:=nil;
  4655. do_typecheckpass(left);
  4656. end;
  4657. factval:=aktstate.find_fact(left);
  4658. if factval<>nil then
  4659. begin
  4660. track_state_pass:=true;
  4661. left.free;
  4662. left:=factval.getcopy;
  4663. end;
  4664. if right.track_state_pass(exec_known) then
  4665. begin
  4666. track_state_pass:=true;
  4667. right.resultdef:=nil;
  4668. do_typecheckpass(right);
  4669. end;
  4670. factval:=aktstate.find_fact(right);
  4671. if factval<>nil then
  4672. begin
  4673. track_state_pass:=true;
  4674. right.free;
  4675. right:=factval.getcopy;
  4676. end;
  4677. end;
  4678. {$endif}
  4679. {$ifdef DEBUG_NODE_XML}
  4680. procedure TAddNode.XMLPrintNodeInfo(var T: Text);
  4681. var
  4682. i: TAddNodeFlag;
  4683. First: Boolean;
  4684. begin
  4685. inherited XMLPrintNodeInfo(T);
  4686. First := True;
  4687. for i in addnodeflags do
  4688. begin
  4689. if First then
  4690. begin
  4691. Write(T, ' addnodeflags="', i);
  4692. First := False;
  4693. end
  4694. else
  4695. Write(T, ',', i)
  4696. end;
  4697. if not First then
  4698. Write(T, '"');
  4699. end;
  4700. {$endif DEBUG_NODE_XML}
  4701. end.