nadd.pas 186 KB

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