nadd.pas 189 KB

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