nadd.pas 183 KB

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