nadd.pas 172 KB

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