nadd.pas 166 KB

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