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