nadd.pas 164 KB

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