nadd.pas 184 KB

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