nadd.pas 172 KB

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