nadd.pas 191 KB

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