nadd.pas 192 KB

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