nadd.pas 182 KB

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