nadd.pas 192 KB

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