nadd.pas 172 KB

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