nadd.pas 192 KB

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