nadd.pas 179 KB

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