nadd.pas 189 KB

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