2
0

nadd.pas 166 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094
  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(0,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. else if (nodetype=equaln) and MatchAndTransformNodesCommutative(left,right,@IsLengthZero,@TransformLengthZero,Result) then
  1135. exit;
  1136. end;
  1137. { using sqr(x) for reals instead of x*x might reduces register pressure and/or
  1138. memory accesses while sqr(<real>) has no drawback }
  1139. if
  1140. {$ifdef cpufpemu}
  1141. (current_settings.fputype<>fpu_soft) and
  1142. not(cs_fp_emulation in current_settings.moduleswitches) and
  1143. {$endif cpufpemu}
  1144. (nodetype=muln) and
  1145. is_real(left.resultdef) and is_real(right.resultdef) and
  1146. left.isequal(right) and
  1147. not(might_have_sideeffects(left)) then
  1148. begin
  1149. result:=cinlinenode.create(in_sqr_real,false,left);
  1150. left:=nil;
  1151. exit;
  1152. end;
  1153. {$ifdef cpurox}
  1154. { optimize (i shl x) or (i shr (bitsizeof(i)-x)) into rol(x,i) (and different flavours with shl/shr swapped etc.) }
  1155. if (nodetype=orn)
  1156. {$ifdef m68k}
  1157. and (CPUM68K_HAS_ROLROR in cpu_capabilities[current_settings.cputype])
  1158. {$endif m68k}
  1159. {$ifndef cpu64bitalu}
  1160. and (left.resultdef.typ=orddef) and
  1161. not(torddef(left.resultdef).ordtype in [s64bit,u64bit,scurrency])
  1162. {$endif cpu64bitalu}
  1163. then
  1164. begin
  1165. if (left.nodetype=shrn) and (right.nodetype=shln) and
  1166. is_constintnode(tshlshrnode(left).right) and
  1167. is_constintnode(tshlshrnode(right).right) and
  1168. (tordconstnode(tshlshrnode(right).right).value>0) and
  1169. (tordconstnode(tshlshrnode(left).right).value>0) and
  1170. tshlshrnode(left).left.isequal(tshlshrnode(right).left) and
  1171. not(might_have_sideeffects(tshlshrnode(left).left)) then
  1172. begin
  1173. if (tordconstnode(tshlshrnode(left).right).value=
  1174. tshlshrnode(left).left.resultdef.size*8-tordconstnode(tshlshrnode(right).right).value)
  1175. then
  1176. begin
  1177. result:=cinlinenode.create(in_ror_x_y,false,
  1178. ccallparanode.create(tshlshrnode(left).right,
  1179. ccallparanode.create(tshlshrnode(left).left,nil)));
  1180. tshlshrnode(left).left:=nil;
  1181. tshlshrnode(left).right:=nil;
  1182. exit;
  1183. end
  1184. else if (tordconstnode(tshlshrnode(right).right).value=
  1185. tshlshrnode(left).left.resultdef.size*8-tordconstnode(tshlshrnode(left).right).value)
  1186. then
  1187. begin
  1188. result:=cinlinenode.create(in_rol_x_y,false,
  1189. ccallparanode.create(tshlshrnode(right).right,
  1190. ccallparanode.create(tshlshrnode(left).left,nil)));
  1191. tshlshrnode(left).left:=nil;
  1192. tshlshrnode(right).right:=nil;
  1193. exit;
  1194. end;
  1195. end;
  1196. if (left.nodetype=shln) and (right.nodetype=shrn) and
  1197. is_constintnode(tshlshrnode(left).right) and
  1198. is_constintnode(tshlshrnode(right).right) and
  1199. (tordconstnode(tshlshrnode(right).right).value>0) and
  1200. (tordconstnode(tshlshrnode(left).right).value>0) and
  1201. tshlshrnode(left).left.isequal(tshlshrnode(right).left) and
  1202. not(might_have_sideeffects(tshlshrnode(left).left)) then
  1203. begin
  1204. if (tordconstnode(tshlshrnode(left).right).value=
  1205. tshlshrnode(left).left.resultdef.size*8-tordconstnode(tshlshrnode(right).right).value)
  1206. then
  1207. begin
  1208. result:=cinlinenode.create(in_rol_x_y,false,
  1209. ccallparanode.create(tshlshrnode(left).right,
  1210. ccallparanode.create(tshlshrnode(left).left,nil)));
  1211. tshlshrnode(left).left:=nil;
  1212. tshlshrnode(left).right:=nil;
  1213. exit;
  1214. end
  1215. else if (tordconstnode(tshlshrnode(right).right).value=
  1216. tshlshrnode(left).left.resultdef.size*8-tordconstnode(tshlshrnode(left).right).value)
  1217. then
  1218. begin
  1219. result:=cinlinenode.create(in_ror_x_y,false,
  1220. ccallparanode.create(tshlshrnode(right).right,
  1221. ccallparanode.create(tshlshrnode(left).left,nil)));
  1222. tshlshrnode(left).left:=nil;
  1223. tshlshrnode(right).right:=nil;
  1224. exit;
  1225. end;
  1226. end;
  1227. end;
  1228. {$endif cpurox}
  1229. end;
  1230. end;
  1231. function taddnode.dogetcopy: tnode;
  1232. var
  1233. n: taddnode;
  1234. begin
  1235. n:=taddnode(inherited dogetcopy);
  1236. n.resultrealdef:=resultrealdef;
  1237. result:=n;
  1238. end;
  1239. function taddnode.docompare(p: tnode): boolean;
  1240. begin
  1241. result:=
  1242. inherited docompare(p) and
  1243. equal_defs(taddnode(p).resultrealdef,resultrealdef);
  1244. end;
  1245. function taddnode.pass_typecheck:tnode;
  1246. begin
  1247. { This function is small to keep the stack small for recursive of
  1248. large + operations }
  1249. typecheckpass(left);
  1250. typecheckpass(right);
  1251. result:=pass_typecheck_internal;
  1252. end;
  1253. function taddnode.pass_typecheck_internal:tnode;
  1254. var
  1255. hp : tnode;
  1256. rd,ld,nd : tdef;
  1257. hsym : tfieldvarsym;
  1258. llow,lhigh,
  1259. rlow,rhigh : tconstexprint;
  1260. strtype : tstringtype;
  1261. res,
  1262. b : boolean;
  1263. lt,rt : tnodetype;
  1264. ot : tnodetype;
  1265. {$ifdef state_tracking}
  1266. factval : Tnode;
  1267. change : boolean;
  1268. {$endif}
  1269. function maybe_cast_ordconst(var n: tnode; adef: tdef): boolean;
  1270. begin
  1271. result:=(tordconstnode(n).value>=torddef(adef).low) and
  1272. (tordconstnode(n).value<=torddef(adef).high);
  1273. if result then
  1274. inserttypeconv(n,adef);
  1275. end;
  1276. function maybe_convert_to_insert:tnode;
  1277. function element_count(arrconstr: tarrayconstructornode):asizeint;
  1278. begin
  1279. result:=0;
  1280. while assigned(arrconstr) do
  1281. begin
  1282. if arrconstr.nodetype=arrayconstructorrangen then
  1283. internalerror(2018052501);
  1284. inc(result);
  1285. arrconstr:=tarrayconstructornode(tarrayconstructornode(arrconstr).right);
  1286. end;
  1287. end;
  1288. var
  1289. elem : tnode;
  1290. para : tcallparanode;
  1291. isarrconstrl,
  1292. isarrconstrr : boolean;
  1293. index : asizeint;
  1294. begin
  1295. result:=nil;
  1296. isarrconstrl:=left.nodetype=arrayconstructorn;
  1297. isarrconstrr:=right.nodetype=arrayconstructorn;
  1298. if not assigned(aktassignmentnode) or
  1299. (aktassignmentnode.right<>self) or
  1300. not(
  1301. isarrconstrl or
  1302. isarrconstrr
  1303. ) or
  1304. not(
  1305. left.isequal(aktassignmentnode.left) or
  1306. right.isequal(aktassignmentnode.left)
  1307. ) or
  1308. not valid_for_var(aktassignmentnode.left,false) or
  1309. (isarrconstrl and (element_count(tarrayconstructornode(left))>1)) or
  1310. (isarrconstrr and (element_count(tarrayconstructornode(right))>1)) then
  1311. exit;
  1312. if isarrconstrl then
  1313. begin
  1314. index:=0;
  1315. elem:=tarrayconstructornode(left).left;
  1316. tarrayconstructornode(left).left:=nil;
  1317. end
  1318. else
  1319. begin
  1320. index:=high(asizeint);
  1321. elem:=tarrayconstructornode(right).left;
  1322. tarrayconstructornode(right).left:=nil;
  1323. end;
  1324. { we use the fact that insert() caps the index to avoid a copy }
  1325. para:=ccallparanode.create(
  1326. cordconstnode.create(index,sizesinttype,false),
  1327. ccallparanode.create(
  1328. aktassignmentnode.left.getcopy,
  1329. ccallparanode.create(
  1330. elem,nil)));
  1331. result:=cinlinenode.create(in_insert_x_y_z,false,para);
  1332. include(aktassignmentnode.flags,nf_assign_done_in_right);
  1333. end;
  1334. begin
  1335. result:=nil;
  1336. rlow:=0;
  1337. llow:=0;
  1338. rhigh:=0;
  1339. lhigh:=0;
  1340. { avoid any problems with type parameters later on }
  1341. if is_typeparam(left.resultdef) or is_typeparam(right.resultdef) then
  1342. begin
  1343. resultdef:=cundefinedtype;
  1344. exit;
  1345. end;
  1346. { both left and right need to be valid }
  1347. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1348. set_varstate(right,vs_read,[vsf_must_be_valid]);
  1349. if codegenerror then
  1350. exit;
  1351. { tp procvar support. Omit for converted assigned() nodes }
  1352. if not (nf_load_procvar in flags) then
  1353. begin
  1354. maybe_call_procvar(left,true);
  1355. maybe_call_procvar(right,true);
  1356. end
  1357. else
  1358. if not (nodetype in [equaln,unequaln]) then
  1359. InternalError(2013091601);
  1360. { allow operator overloading }
  1361. hp:=self;
  1362. if is_array_constructor(left.resultdef) or is_array_constructor(right.resultdef) then
  1363. begin
  1364. { check whether there is a suitable operator for the array constructor
  1365. (but only if the "+" array operator isn't used), if not fall back to sets }
  1366. if (
  1367. (nodetype<>addn) or
  1368. not (m_array_operators in current_settings.modeswitches) or
  1369. (is_array_constructor(left.resultdef) and not is_dynamic_array(right.resultdef)) or
  1370. (not is_dynamic_array(left.resultdef) and is_array_constructor(right.resultdef))
  1371. ) and
  1372. not isbinaryoverloaded(hp,[ocf_check_only]) then
  1373. begin
  1374. if is_array_constructor(left.resultdef) then
  1375. begin
  1376. arrayconstructor_to_set(left);
  1377. typecheckpass(left);
  1378. end;
  1379. if is_array_constructor(right.resultdef) then
  1380. begin
  1381. arrayconstructor_to_set(right);
  1382. typecheckpass(right);
  1383. end;
  1384. end;
  1385. end;
  1386. if is_dynamic_array(left.resultdef) and is_dynamic_array(right.resultdef) and
  1387. (nodetype=addn) and
  1388. (m_array_operators in current_settings.modeswitches) and
  1389. isbinaryoverloaded(hp,[ocf_check_non_overloadable,ocf_check_only]) then
  1390. message3(parser_w_operator_overloaded_hidden_3,left.resultdef.typename,arraytokeninfo[_PLUS].str,right.resultdef.typename);
  1391. if isbinaryoverloaded(hp,[]) then
  1392. begin
  1393. result:=hp;
  1394. exit;
  1395. end;
  1396. { Stop checking when an error was found in the operator checking }
  1397. if codegenerror then
  1398. begin
  1399. result:=cerrornode.create;
  1400. exit;
  1401. end;
  1402. { Kylix allows enum+ordconstn in an enum type declaration, we need to do
  1403. the conversion here before the constant folding }
  1404. if (m_delphi in current_settings.modeswitches) and
  1405. (blocktype in [bt_type,bt_const_type,bt_var_type]) then
  1406. begin
  1407. if (left.resultdef.typ=enumdef) and
  1408. (right.resultdef.typ=orddef) then
  1409. begin
  1410. { insert explicit typecast to default signed int }
  1411. left:=ctypeconvnode.create_internal(left,sinttype);
  1412. typecheckpass(left);
  1413. end
  1414. else
  1415. if (left.resultdef.typ=orddef) and
  1416. (right.resultdef.typ=enumdef) then
  1417. begin
  1418. { insert explicit typecast to default signed int }
  1419. right:=ctypeconvnode.create_internal(right,sinttype);
  1420. typecheckpass(right);
  1421. end;
  1422. end;
  1423. { is one a real float, then both need to be floats, this
  1424. need to be done before the constant folding so constant
  1425. operation on a float and int are also handled }
  1426. {$ifdef x86}
  1427. { use extended as default real type only when the x87 fpu is used }
  1428. {$if defined(i386) or defined(i8086)}
  1429. if not(current_settings.fputype=fpu_x87) then
  1430. resultrealdef:=s64floattype
  1431. else
  1432. resultrealdef:=pbestrealtype^;
  1433. {$endif i386 or i8086}
  1434. {$ifdef x86_64}
  1435. { x86-64 has no x87 only mode, so use always double as default }
  1436. resultrealdef:=s64floattype;
  1437. {$endif x86_6}
  1438. {$else not x86}
  1439. resultrealdef:=pbestrealtype^;
  1440. {$endif not x86}
  1441. if (right.resultdef.typ=floatdef) or (left.resultdef.typ=floatdef) then
  1442. begin
  1443. { when both floattypes are already equal then use that
  1444. floattype for results }
  1445. if (right.resultdef.typ=floatdef) and
  1446. (left.resultdef.typ=floatdef) and
  1447. (tfloatdef(left.resultdef).floattype=tfloatdef(right.resultdef).floattype) then
  1448. begin
  1449. if cs_excessprecision in current_settings.localswitches then
  1450. resultrealdef:=pbestrealtype^
  1451. else
  1452. resultrealdef:=left.resultdef
  1453. end
  1454. { when there is a currency type then use currency, but
  1455. only when currency is defined as float }
  1456. else
  1457. if (is_currency(right.resultdef) or
  1458. is_currency(left.resultdef)) and
  1459. ((s64currencytype.typ = floatdef) or
  1460. (nodetype <> slashn)) then
  1461. begin
  1462. resultrealdef:=s64currencytype;
  1463. inserttypeconv(right,resultrealdef);
  1464. inserttypeconv(left,resultrealdef);
  1465. end
  1466. else
  1467. begin
  1468. resultrealdef:=getbestreal(left.resultdef,right.resultdef);
  1469. inserttypeconv(right,resultrealdef);
  1470. inserttypeconv(left,resultrealdef);
  1471. end;
  1472. end;
  1473. { If both operands are constant and there is a unicodestring
  1474. or unicodestring then convert everything to unicodestring }
  1475. if is_constnode(right) and is_constnode(left) and
  1476. (is_unicodestring(right.resultdef) or
  1477. is_unicodestring(left.resultdef)) then
  1478. begin
  1479. inserttypeconv(right,cunicodestringtype);
  1480. inserttypeconv(left,cunicodestringtype);
  1481. end;
  1482. { If both operands are constant and there is a widechar
  1483. or widestring then convert everything to widestring. This
  1484. allows constant folding like char+widechar }
  1485. if is_constnode(right) and is_constnode(left) and
  1486. (is_widestring(right.resultdef) or
  1487. is_widestring(left.resultdef) or
  1488. is_widechar(right.resultdef) or
  1489. is_widechar(left.resultdef)) then
  1490. begin
  1491. inserttypeconv(right,cwidestringtype);
  1492. inserttypeconv(left,cwidestringtype);
  1493. end;
  1494. { load easier access variables }
  1495. rd:=right.resultdef;
  1496. ld:=left.resultdef;
  1497. rt:=right.nodetype;
  1498. lt:=left.nodetype;
  1499. { 4 character constant strings are compatible with orddef }
  1500. { in macpas mode (become cardinals) }
  1501. if (m_mac in current_settings.modeswitches) and
  1502. { only allow for comparisons, additions etc are }
  1503. { normally program errors }
  1504. (nodetype in [ltn,lten,gtn,gten,unequaln,equaln]) and
  1505. (((lt=stringconstn) and
  1506. (tstringconstnode(left).len=4) and
  1507. (rd.typ=orddef)) or
  1508. ((rt=stringconstn) and
  1509. (tstringconstnode(right).len=4) and
  1510. (ld.typ=orddef))) then
  1511. begin
  1512. if (rt=stringconstn) then
  1513. begin
  1514. inserttypeconv(right,u32inttype);
  1515. rt:=right.nodetype;
  1516. rd:=right.resultdef;
  1517. end
  1518. else
  1519. begin
  1520. inserttypeconv(left,u32inttype);
  1521. lt:=left.nodetype;
  1522. ld:=left.resultdef;
  1523. end;
  1524. end;
  1525. { but an int/int gives real/real! }
  1526. if (nodetype=slashn) and not(is_vector(left.resultdef)) and not(is_vector(right.resultdef)) then
  1527. begin
  1528. if is_currency(left.resultdef) and
  1529. is_currency(right.resultdef) then
  1530. { In case of currency, converting to float means dividing by 10000 }
  1531. { However, since this is already a division, both divisions by }
  1532. { 10000 are eliminated when we divide the results -> we can skip }
  1533. { them. }
  1534. if s64currencytype.typ = floatdef then
  1535. begin
  1536. { there's no s64comptype or so, how do we avoid the type conversion?
  1537. left.resultdef := s64comptype;
  1538. right.resultdef := s64comptype; }
  1539. end
  1540. else
  1541. begin
  1542. left.resultdef := s64inttype;
  1543. right.resultdef := s64inttype;
  1544. end;
  1545. inserttypeconv(right,resultrealdef);
  1546. inserttypeconv(left,resultrealdef);
  1547. end
  1548. { if both are orddefs then check sub types }
  1549. else if (ld.typ=orddef) and (rd.typ=orddef) then
  1550. begin
  1551. { set for & and | operations in macpas mode: they only work on }
  1552. { booleans, and always short circuit evaluation }
  1553. if (nf_short_bool in flags) then
  1554. begin
  1555. if not is_boolean(ld) then
  1556. begin
  1557. inserttypeconv(left,pasbool1type);
  1558. ld := left.resultdef;
  1559. end;
  1560. if not is_boolean(rd) then
  1561. begin
  1562. inserttypeconv(right,pasbool1type);
  1563. rd := right.resultdef;
  1564. end;
  1565. end;
  1566. { 2 booleans? }
  1567. if (is_boolean(ld) and is_boolean(rd)) then
  1568. begin
  1569. case nodetype of
  1570. xorn,
  1571. andn,
  1572. orn:
  1573. begin
  1574. { in case of xor, or 'and' with full and cbool: convert both to Pascal bool and then
  1575. perform the xor/and to prevent issues with "longbool(1) and/xor
  1576. longbool(2)" }
  1577. if (is_cbool(ld) or is_cbool(rd)) and
  1578. ((nodetype=xorn) or
  1579. ((nodetype=andn) and
  1580. ((cs_full_boolean_eval in current_settings.localswitches) or
  1581. not(nf_short_bool in flags)
  1582. )
  1583. )
  1584. ) then
  1585. begin
  1586. resultdef:=nil;
  1587. if is_cbool(ld) then
  1588. begin
  1589. inserttypeconv(left,pasbool8type);
  1590. { inserttypeconv might already simplify
  1591. the typeconvnode after insertion,
  1592. thus we need to check if it still
  1593. really is a typeconv node }
  1594. if left is ttypeconvnode then
  1595. ttypeconvnode(left).convtype:=tc_bool_2_bool;
  1596. if not is_cbool(rd) or
  1597. (ld.size>=rd.size) then
  1598. resultdef:=ld;
  1599. end;
  1600. if is_cbool(rd) then
  1601. begin
  1602. inserttypeconv(right,pasbool8type);
  1603. { inserttypeconv might already simplify
  1604. the typeconvnode after insertion,
  1605. thus we need to check if it still
  1606. really is a typeconv node }
  1607. if right is ttypeconvnode then
  1608. ttypeconvnode(right).convtype:=tc_bool_2_bool;
  1609. if not assigned(resultdef) then
  1610. resultdef:=rd;
  1611. end;
  1612. result:=ctypeconvnode.create_explicit(caddnode.create(nodetype,left,right),resultdef);
  1613. ttypeconvnode(result).convtype:=tc_bool_2_bool;
  1614. left:=nil;
  1615. right:=nil;
  1616. exit;
  1617. end;
  1618. { Make sides equal to the largest boolean }
  1619. if (torddef(left.resultdef).size>torddef(right.resultdef).size) or
  1620. (is_cbool(left.resultdef) and not is_cbool(right.resultdef)) then
  1621. begin
  1622. right:=ctypeconvnode.create_internal(right,left.resultdef);
  1623. ttypeconvnode(right).convtype:=tc_bool_2_bool;
  1624. typecheckpass(right);
  1625. end
  1626. else if (torddef(left.resultdef).size<torddef(right.resultdef).size) or
  1627. (not is_cbool(left.resultdef) and is_cbool(right.resultdef)) then
  1628. begin
  1629. left:=ctypeconvnode.create_internal(left,right.resultdef);
  1630. ttypeconvnode(left).convtype:=tc_bool_2_bool;
  1631. typecheckpass(left);
  1632. end;
  1633. end;
  1634. ltn,
  1635. lten,
  1636. gtn,
  1637. gten:
  1638. begin
  1639. { convert both to pasbool to perform the comparison (so
  1640. that longbool(4) = longbool(2), since both represent
  1641. "true" }
  1642. inserttypeconv(left,pasbool1type);
  1643. inserttypeconv(right,pasbool1type);
  1644. end;
  1645. unequaln,
  1646. equaln:
  1647. begin
  1648. if not(cs_full_boolean_eval in current_settings.localswitches) or
  1649. (nf_short_bool in flags) then
  1650. begin
  1651. { Remove any compares with constants }
  1652. if (left.nodetype=ordconstn) then
  1653. begin
  1654. hp:=right;
  1655. b:=(tordconstnode(left).value<>0);
  1656. ot:=nodetype;
  1657. left.free;
  1658. left:=nil;
  1659. right:=nil;
  1660. if (not(b) and (ot=equaln)) or
  1661. (b and (ot=unequaln)) then
  1662. begin
  1663. hp:=cnotnode.create(hp);
  1664. end;
  1665. result:=hp;
  1666. exit;
  1667. end;
  1668. if (right.nodetype=ordconstn) then
  1669. begin
  1670. hp:=left;
  1671. b:=(tordconstnode(right).value<>0);
  1672. ot:=nodetype;
  1673. right.free;
  1674. right:=nil;
  1675. left:=nil;
  1676. if (not(b) and (ot=equaln)) or
  1677. (b and (ot=unequaln)) then
  1678. begin
  1679. hp:=cnotnode.create(hp);
  1680. end;
  1681. result:=hp;
  1682. exit;
  1683. end;
  1684. end;
  1685. { Delphi-compatibility: convert both to pasbool to
  1686. perform the equality comparison }
  1687. inserttypeconv(left,pasbool1type);
  1688. inserttypeconv(right,pasbool1type);
  1689. end;
  1690. else
  1691. begin
  1692. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1693. result:=cnothingnode.create;
  1694. exit;
  1695. end;
  1696. end;
  1697. end
  1698. { Both are chars? }
  1699. else if is_char(rd) and is_char(ld) then
  1700. begin
  1701. if nodetype=addn then
  1702. begin
  1703. resultdef:=cshortstringtype;
  1704. if not(is_constcharnode(left) and is_constcharnode(right)) then
  1705. begin
  1706. inserttypeconv(left,cshortstringtype);
  1707. {$ifdef addstringopt}
  1708. hp := genaddsstringcharoptnode(self);
  1709. result := hp;
  1710. exit;
  1711. {$endif addstringopt}
  1712. end
  1713. end
  1714. else if not(nodetype in [ltn,lten,gtn,gten,unequaln,equaln]) then
  1715. begin
  1716. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1717. result:=cnothingnode.create;
  1718. exit;
  1719. end;
  1720. end
  1721. { There is a widechar? }
  1722. else if is_widechar(rd) or is_widechar(ld) then
  1723. begin
  1724. { widechar+widechar gives unicodestring }
  1725. if nodetype=addn then
  1726. begin
  1727. inserttypeconv(left,cunicodestringtype);
  1728. if (torddef(rd).ordtype<>uwidechar) then
  1729. inserttypeconv(right,cwidechartype);
  1730. resultdef:=cunicodestringtype;
  1731. end
  1732. else
  1733. begin
  1734. if (torddef(ld).ordtype<>uwidechar) then
  1735. inserttypeconv(left,cwidechartype);
  1736. if (torddef(rd).ordtype<>uwidechar) then
  1737. inserttypeconv(right,cwidechartype);
  1738. end;
  1739. end
  1740. { is there a currency type ? }
  1741. else if ((torddef(rd).ordtype=scurrency) or (torddef(ld).ordtype=scurrency)) then
  1742. begin
  1743. if (torddef(ld).ordtype<>scurrency) then
  1744. inserttypeconv(left,s64currencytype);
  1745. if (torddef(rd).ordtype<>scurrency) then
  1746. inserttypeconv(right,s64currencytype);
  1747. end
  1748. { leave some constant integer expressions alone in case the
  1749. resultdef of the integer types doesn't influence the outcome,
  1750. because the forced type conversions below can otherwise result
  1751. in unexpected results (such as high(qword)<high(int64) returning
  1752. true because high(qword) gets converted to int64) }
  1753. else if is_integer(ld) and is_integer(rd) and
  1754. (lt=ordconstn) and (rt=ordconstn) and
  1755. (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) then
  1756. begin
  1757. end
  1758. { "and" does't care about the sign of integers }
  1759. { "xor", "or" and compares don't need extension to native int }
  1760. { size either as long as both values are signed or unsigned }
  1761. { "xor" and "or" also don't care about the sign if the values }
  1762. { occupy an entire register }
  1763. { don't do it if either type is 64 bit (except for "and"), }
  1764. { since in that case we can't safely find a "common" type }
  1765. else if is_integer(ld) and is_integer(rd) and
  1766. ((nodetype=andn) or
  1767. ((nodetype in [orn,xorn,equaln,unequaln,gtn,gten,ltn,lten]) and
  1768. not is_64bitint(ld) and not is_64bitint(rd) and
  1769. (is_signed(ld)=is_signed(rd)))) then
  1770. begin
  1771. { Delphi-compatible: prefer unsigned type for "and", when the
  1772. unsigned type is bigger than the signed one, and also bigger
  1773. than min(native_int, 32-bit) }
  1774. if (is_oversizedint(rd) or is_nativeint(rd) or is_32bitint(rd)) and
  1775. (rd.size>=ld.size) and
  1776. not is_signed(rd) and is_signed(ld) then
  1777. inserttypeconv_internal(left,rd)
  1778. else if (is_oversizedint(ld) or is_nativeint(ld) or is_32bitint(ld)) and
  1779. (ld.size>=rd.size) and
  1780. not is_signed(ld) and is_signed(rd) then
  1781. inserttypeconv_internal(right,ld)
  1782. else
  1783. begin
  1784. { not to left right.resultdef, because that may
  1785. cause a range error if left and right's def don't
  1786. completely overlap }
  1787. nd:=get_common_intdef(torddef(ld),torddef(rd),true);
  1788. inserttypeconv(left,nd);
  1789. inserttypeconv(right,nd);
  1790. end;
  1791. end
  1792. { don't extend (sign-mismatched) comparisons if either side is a constant
  1793. whose value is within range of opposite side }
  1794. else if is_integer(ld) and is_integer(rd) and
  1795. (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  1796. (is_signed(ld)<>is_signed(rd)) and
  1797. (
  1798. ((lt=ordconstn) and maybe_cast_ordconst(left,rd)) or
  1799. ((rt=ordconstn) and maybe_cast_ordconst(right,ld))
  1800. ) then
  1801. begin
  1802. { done here }
  1803. end
  1804. { is there a signed 64 bit type ? }
  1805. else if ((torddef(rd).ordtype=s64bit) or (torddef(ld).ordtype=s64bit)) then
  1806. begin
  1807. if (torddef(ld).ordtype<>s64bit) then
  1808. inserttypeconv(left,s64inttype);
  1809. if (torddef(rd).ordtype<>s64bit) then
  1810. inserttypeconv(right,s64inttype);
  1811. end
  1812. { is there a unsigned 64 bit type ? }
  1813. else if ((torddef(rd).ordtype=u64bit) or (torddef(ld).ordtype=u64bit)) then
  1814. begin
  1815. if (torddef(ld).ordtype<>u64bit) then
  1816. inserttypeconv(left,u64inttype);
  1817. if (torddef(rd).ordtype<>u64bit) then
  1818. inserttypeconv(right,u64inttype);
  1819. end
  1820. { is there a larger int? }
  1821. else if is_oversizedint(rd) or is_oversizedint(ld) then
  1822. begin
  1823. nd:=get_common_intdef(torddef(ld),torddef(rd),false);
  1824. inserttypeconv(right,nd);
  1825. inserttypeconv(left,nd);
  1826. end
  1827. { is there a native unsigned int? }
  1828. else if is_nativeuint(rd) or is_nativeuint(ld) then
  1829. begin
  1830. { convert positive constants to uinttype }
  1831. if (not is_nativeuint(ld)) and
  1832. is_constintnode(left) and
  1833. (tordconstnode(left).value >= 0) then
  1834. inserttypeconv(left,uinttype);
  1835. if (not is_nativeuint(rd)) and
  1836. is_constintnode(right) and
  1837. (tordconstnode(right).value >= 0) then
  1838. inserttypeconv(right,uinttype);
  1839. { when one of the operand is signed or the operation is subn then perform
  1840. the operation in a larger signed type, can't use rd/ld here because there
  1841. could be already typeconvs inserted.
  1842. This is compatible with the code below for other unsigned types (PFV) }
  1843. if is_signed(left.resultdef) or
  1844. is_signed(right.resultdef) or
  1845. (nodetype=subn) then
  1846. begin
  1847. if nodetype<>subn then
  1848. CGMessage(type_h_mixed_signed_unsigned);
  1849. { mark as internal in case added for a subn, so }
  1850. { ttypeconvnode.simplify can remove the larger }
  1851. { typecast again if semantically correct. Even }
  1852. { if we could detect that here already, we }
  1853. { mustn't do it here because that would change }
  1854. { overload choosing behaviour etc. The code in }
  1855. { ncnv.pas is run after that is already decided }
  1856. if (not is_signed(left.resultdef) and
  1857. not is_signed(right.resultdef)) or
  1858. (nodetype in [orn,xorn]) then
  1859. include(flags,nf_internal);
  1860. { get next larger signed int type }
  1861. nd:=get_common_intdef(torddef(sinttype),torddef(uinttype),false);
  1862. inserttypeconv(left,nd);
  1863. inserttypeconv(right,nd);
  1864. end
  1865. else
  1866. begin
  1867. if not is_nativeuint(left.resultdef) then
  1868. inserttypeconv(left,uinttype);
  1869. if not is_nativeuint(right.resultdef) then
  1870. inserttypeconv(right,uinttype);
  1871. end;
  1872. end
  1873. { generic ord conversion is sinttype }
  1874. else
  1875. begin
  1876. { When there is a signed type or there is a minus operation
  1877. we convert to signed int. Otherwise (both are unsigned) we keep
  1878. the result also unsigned. This is compatible with Delphi (PFV) }
  1879. if is_signed(ld) or
  1880. is_signed(rd) or
  1881. (nodetype=subn) then
  1882. begin
  1883. inserttypeconv(right,sinttype);
  1884. inserttypeconv(left,sinttype);
  1885. end
  1886. else
  1887. begin
  1888. inserttypeconv(right,uinttype);
  1889. inserttypeconv(left,uinttype);
  1890. end;
  1891. end;
  1892. end
  1893. { if both are floatdefs, conversion is already done before constant folding }
  1894. else if (ld.typ=floatdef) then
  1895. begin
  1896. if not(nodetype in [addn,subn,muln,slashn,equaln,unequaln,ltn,lten,gtn,gten]) then
  1897. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1898. end
  1899. { left side a setdef, must be before string processing,
  1900. else array constructor can be seen as array of char (PFV) }
  1901. else if (ld.typ=setdef) then
  1902. begin
  1903. if not(nodetype in [addn,subn,symdifn,muln,equaln,unequaln,lten,gten]) then
  1904. CGMessage(type_e_set_operation_unknown);
  1905. { right must either be a set or a set element }
  1906. if (rd.typ<>setdef) and
  1907. (rt<>setelementn) then
  1908. CGMessage(type_e_mismatch)
  1909. { Make operands the same setdef. If one's elementtype fits }
  1910. { entirely inside the other's, pick the one with the largest }
  1911. { range. Otherwise create a new setdef with a range which }
  1912. { can contain both. }
  1913. else if not(equal_defs(ld,rd)) then
  1914. begin
  1915. { note: ld cannot be an empty set with elementdef=nil in }
  1916. { case right is not a set, arrayconstructor_to_set takes }
  1917. { care of that }
  1918. { 1: rd is a set with an assigned elementdef, and ld is }
  1919. { either an empty set without elementdef or a set whose }
  1920. { elementdef fits in rd's elementdef -> convert to rd }
  1921. if ((rd.typ=setdef) and
  1922. assigned(tsetdef(rd).elementdef) and
  1923. (not assigned(tsetdef(ld).elementdef) or
  1924. is_in_limit(ld,rd))) then
  1925. inserttypeconv(left,rd)
  1926. { 2: rd is either an empty set without elementdef or a set }
  1927. { whose elementdef fits in ld's elementdef, or a set }
  1928. { element whose def fits in ld's elementdef -> convert }
  1929. { to ld. ld's elementdef can't be nil here, is caught }
  1930. { previous case and "note:" above }
  1931. else if ((rd.typ=setdef) and
  1932. (not assigned(tsetdef(rd).elementdef) or
  1933. is_in_limit(rd,ld))) or
  1934. ((rd.typ<>setdef) and
  1935. is_in_limit(rd,tsetdef(ld).elementdef)) then
  1936. if (rd.typ=setdef) then
  1937. inserttypeconv(right,ld)
  1938. else
  1939. inserttypeconv(right,tsetdef(ld).elementdef)
  1940. { 3: otherwise create setdef which encompasses both, taking }
  1941. { into account empty sets without elementdef }
  1942. else
  1943. begin
  1944. if assigned(tsetdef(ld).elementdef) then
  1945. begin
  1946. llow:=tsetdef(ld).setbase;
  1947. lhigh:=tsetdef(ld).setmax;
  1948. end;
  1949. if (rd.typ=setdef) then
  1950. if assigned(tsetdef(rd).elementdef) then
  1951. begin
  1952. rlow:=tsetdef(rd).setbase;
  1953. rhigh:=tsetdef(rd).setmax;
  1954. end
  1955. else
  1956. begin
  1957. { ld's elementdef must have been valid }
  1958. rlow:=llow;
  1959. rhigh:=lhigh;
  1960. end
  1961. else
  1962. getrange(rd,rlow,rhigh);
  1963. if not assigned(tsetdef(ld).elementdef) then
  1964. begin
  1965. llow:=rlow;
  1966. lhigh:=rhigh;
  1967. end;
  1968. nd:=csetdef.create(tsetdef(ld).elementdef,min(llow,rlow).svalue,max(lhigh,rhigh).svalue,true);
  1969. inserttypeconv(left,nd);
  1970. if (rd.typ=setdef) then
  1971. inserttypeconv(right,nd)
  1972. else
  1973. inserttypeconv(right,tsetdef(nd).elementdef);
  1974. end;
  1975. end;
  1976. end
  1977. { pointer comparision and subtraction }
  1978. else if (
  1979. (rd.typ=pointerdef) and (ld.typ=pointerdef)
  1980. ) or
  1981. { compare/add pchar to variable (not stringconst) char arrays
  1982. by addresses like BP/Delphi }
  1983. (
  1984. (nodetype in [equaln,unequaln,subn,addn]) and
  1985. (
  1986. ((is_pchar(ld) or (lt=niln)) and is_chararray(rd) and (rt<>stringconstn)) or
  1987. ((is_pchar(rd) or (rt=niln)) and is_chararray(ld) and (lt<>stringconstn))
  1988. )
  1989. ) then
  1990. begin
  1991. { convert char array to pointer }
  1992. if is_chararray(rd) then
  1993. begin
  1994. inserttypeconv(right,charpointertype);
  1995. rd:=right.resultdef;
  1996. end
  1997. else if is_chararray(ld) then
  1998. begin
  1999. inserttypeconv(left,charpointertype);
  2000. ld:=left.resultdef;
  2001. end;
  2002. case nodetype of
  2003. equaln,unequaln :
  2004. begin
  2005. if is_voidpointer(right.resultdef) then
  2006. inserttypeconv(right,left.resultdef)
  2007. else if is_voidpointer(left.resultdef) then
  2008. inserttypeconv(left,right.resultdef)
  2009. else if not(equal_defs(ld,rd)) then
  2010. IncompatibleTypes(ld,rd);
  2011. { now that the type checking is done, convert both to charpointer, }
  2012. { because methodpointers are 8 bytes even though only the first 4 }
  2013. { bytes must be compared. This can happen here if we are in }
  2014. { TP/Delphi mode, because there @methodpointer = voidpointer (but }
  2015. { a voidpointer of 8 bytes). A conversion to voidpointer would be }
  2016. { optimized away, since the result already was a voidpointer, so }
  2017. { use a charpointer instead (JM) }
  2018. {$if defined(jvm)}
  2019. inserttypeconv_internal(left,java_jlobject);
  2020. inserttypeconv_internal(right,java_jlobject);
  2021. {$elseif defined(i8086)}
  2022. if is_hugepointer(left.resultdef) then
  2023. inserttypeconv_internal(left,charhugepointertype)
  2024. else if is_farpointer(left.resultdef) then
  2025. inserttypeconv_internal(left,charfarpointertype)
  2026. else
  2027. inserttypeconv_internal(left,charnearpointertype);
  2028. if is_hugepointer(right.resultdef) then
  2029. inserttypeconv_internal(right,charhugepointertype)
  2030. else if is_farpointer(right.resultdef) then
  2031. inserttypeconv_internal(right,charfarpointertype)
  2032. else
  2033. inserttypeconv_internal(right,charnearpointertype);
  2034. {$else}
  2035. inserttypeconv_internal(left,charpointertype);
  2036. inserttypeconv_internal(right,charpointertype);
  2037. {$endif jvm}
  2038. end;
  2039. ltn,lten,gtn,gten:
  2040. begin
  2041. if (cs_extsyntax in current_settings.moduleswitches) or
  2042. (nf_internal in flags) then
  2043. begin
  2044. if is_voidpointer(right.resultdef) then
  2045. inserttypeconv(right,left.resultdef)
  2046. else if is_voidpointer(left.resultdef) then
  2047. inserttypeconv(left,right.resultdef)
  2048. else if not(equal_defs(ld,rd)) then
  2049. IncompatibleTypes(ld,rd);
  2050. end
  2051. else
  2052. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2053. end;
  2054. subn:
  2055. begin
  2056. if (cs_extsyntax in current_settings.moduleswitches) or
  2057. (nf_internal in flags) then
  2058. begin
  2059. if is_voidpointer(right.resultdef) then
  2060. begin
  2061. if is_big_untyped_addrnode(right) then
  2062. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  2063. inserttypeconv(right,left.resultdef)
  2064. end
  2065. else if is_voidpointer(left.resultdef) then
  2066. inserttypeconv(left,right.resultdef)
  2067. else if not(equal_defs(ld,rd)) then
  2068. IncompatibleTypes(ld,rd);
  2069. end
  2070. else
  2071. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2072. if not(nf_has_pointerdiv in flags) and
  2073. (tpointerdef(rd).pointeddef.size>1) then
  2074. begin
  2075. hp:=getcopy;
  2076. include(hp.flags,nf_has_pointerdiv);
  2077. result:=cmoddivnode.create(divn,hp,
  2078. cordconstnode.create(tpointerdef(rd).pointeddef.size,tpointerdef(rd).pointer_subtraction_result_type,false));
  2079. end;
  2080. resultdef:=tpointerdef(rd).pointer_subtraction_result_type;
  2081. exit;
  2082. end;
  2083. else
  2084. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2085. end;
  2086. end
  2087. { is one of the operands a string?,
  2088. chararrays are also handled as strings (after conversion), also take
  2089. care of chararray+chararray and chararray+char.
  2090. Note: Must be done after pointerdef+pointerdef has been checked, else
  2091. pchar is converted to string }
  2092. else if (rd.typ=stringdef) or
  2093. (ld.typ=stringdef) or
  2094. { stringconstn's can be arraydefs }
  2095. (lt=stringconstn) or
  2096. (rt=stringconstn) or
  2097. ((is_pchar(rd) or is_chararray(rd) or is_char(rd) or is_open_chararray(rd) or
  2098. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd)) and
  2099. (is_pchar(ld) or is_chararray(ld) or is_char(ld) or is_open_chararray(ld) or
  2100. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld))) then
  2101. begin
  2102. if (nodetype in [addn,equaln,unequaln,lten,gten,ltn,gtn]) then
  2103. begin
  2104. { Is there a unicodestring? }
  2105. if is_unicodestring(rd) or is_unicodestring(ld) or
  2106. ((m_default_unicodestring in current_settings.modeswitches) and
  2107. (cs_refcountedstrings in current_settings.localswitches) and
  2108. (
  2109. is_pwidechar(rd) or is_widechararray(rd) or is_open_widechararray(rd) or (lt = stringconstn) or
  2110. is_pwidechar(ld) or is_widechararray(ld) or is_open_widechararray(ld) or (rt = stringconstn)
  2111. )
  2112. ) then
  2113. strtype:=st_unicodestring
  2114. else
  2115. { Is there a widestring? }
  2116. if is_widestring(rd) or is_widestring(ld) or
  2117. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd) or
  2118. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld) then
  2119. strtype:=st_widestring
  2120. else
  2121. if is_ansistring(rd) or is_ansistring(ld) or
  2122. ((cs_refcountedstrings in current_settings.localswitches) and
  2123. //todo: Move some of this to longstring's then they are implemented?
  2124. (
  2125. is_pchar(rd) or (is_chararray(rd) and (rd.size > 255)) or is_open_chararray(rd) or (lt = stringconstn) or
  2126. is_pchar(ld) or (is_chararray(ld) and (ld.size > 255)) or is_open_chararray(ld) or (rt = stringconstn)
  2127. )
  2128. ) then
  2129. strtype:=st_ansistring
  2130. else
  2131. if is_longstring(rd) or is_longstring(ld) then
  2132. strtype:=st_longstring
  2133. else
  2134. begin
  2135. { TODO: todo: add a warning/hint here if one converting a too large array}
  2136. { nodes is PChar, array [with size > 255] or OpenArrayOfChar.
  2137. Note: Delphi halts with error if "array [0..xx] of char"
  2138. is assigned to ShortString and string length is less
  2139. then array size }
  2140. strtype:= st_shortstring;
  2141. end;
  2142. // Now convert nodes to common string type
  2143. case strtype of
  2144. st_widestring :
  2145. begin
  2146. if not(is_widestring(rd)) then
  2147. inserttypeconv(right,cwidestringtype);
  2148. if not(is_widestring(ld)) then
  2149. inserttypeconv(left,cwidestringtype);
  2150. end;
  2151. st_unicodestring :
  2152. begin
  2153. if not(is_unicodestring(rd)) then
  2154. inserttypeconv(right,cunicodestringtype);
  2155. if not(is_unicodestring(ld)) then
  2156. inserttypeconv(left,cunicodestringtype);
  2157. end;
  2158. st_ansistring :
  2159. begin
  2160. { use same code page if possible (don't force same code
  2161. page in case both are ansistrings with code page <>
  2162. CP_NONE, since then data loss can occur: the ansistring
  2163. helpers will convert them at run time to an encoding
  2164. that can represent both encodings) }
  2165. if is_ansistring(ld) and
  2166. (tstringdef(ld).encoding<>0) and
  2167. (tstringdef(ld).encoding<>globals.CP_NONE) and
  2168. (not is_ansistring(rd) or
  2169. (tstringdef(rd).encoding=0) or
  2170. (tstringdef(rd).encoding=globals.CP_NONE)) then
  2171. inserttypeconv(right,ld)
  2172. else if is_ansistring(rd) and
  2173. (tstringdef(rd).encoding<>0) and
  2174. (tstringdef(rd).encoding<>globals.CP_NONE) and
  2175. (not is_ansistring(ld) or
  2176. (tstringdef(ld).encoding=0) or
  2177. (tstringdef(ld).encoding=globals.CP_NONE)) then
  2178. inserttypeconv(left,rd)
  2179. else
  2180. begin
  2181. if not is_ansistring(ld) then
  2182. inserttypeconv(left,getansistringdef);
  2183. if not is_ansistring(rd) then
  2184. inserttypeconv(right,getansistringdef);
  2185. end;
  2186. end;
  2187. st_longstring :
  2188. begin
  2189. if not(is_longstring(rd)) then
  2190. inserttypeconv(right,clongstringtype);
  2191. if not(is_longstring(ld)) then
  2192. inserttypeconv(left,clongstringtype);
  2193. end;
  2194. st_shortstring :
  2195. begin
  2196. if not(is_shortstring(ld)) then
  2197. inserttypeconv(left,cshortstringtype);
  2198. { don't convert char, that can be handled by the optimized node }
  2199. if not(is_shortstring(rd) or is_char(rd)) then
  2200. inserttypeconv(right,cshortstringtype);
  2201. end;
  2202. end;
  2203. end
  2204. else
  2205. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2206. end
  2207. { implicit pointer object type comparison }
  2208. else if is_implicit_pointer_object_type(rd) or is_implicit_pointer_object_type(ld) then
  2209. begin
  2210. if (nodetype in [equaln,unequaln]) then
  2211. begin
  2212. if is_implicit_pointer_object_type(rd) and is_implicit_pointer_object_type(ld) then
  2213. begin
  2214. if def_is_related(tobjectdef(rd),tobjectdef(ld)) then
  2215. inserttypeconv(right,left.resultdef)
  2216. else
  2217. inserttypeconv(left,right.resultdef);
  2218. end
  2219. else if is_implicit_pointer_object_type(rd) then
  2220. inserttypeconv(left,right.resultdef)
  2221. else
  2222. inserttypeconv(right,left.resultdef);
  2223. end
  2224. else
  2225. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2226. end
  2227. else if (rd.typ=classrefdef) and (ld.typ=classrefdef) then
  2228. begin
  2229. if (nodetype in [equaln,unequaln]) then
  2230. begin
  2231. if def_is_related(tobjectdef(tclassrefdef(rd).pointeddef),
  2232. tobjectdef(tclassrefdef(ld).pointeddef)) then
  2233. inserttypeconv(right,left.resultdef)
  2234. else
  2235. inserttypeconv(left,right.resultdef);
  2236. end
  2237. else
  2238. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2239. end
  2240. { allow comparison with nil pointer }
  2241. else if is_implicit_pointer_object_type(rd) or (rd.typ=classrefdef) then
  2242. begin
  2243. if (nodetype in [equaln,unequaln]) then
  2244. inserttypeconv(left,right.resultdef)
  2245. else
  2246. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2247. end
  2248. else if is_implicit_pointer_object_type(ld) or (ld.typ=classrefdef) then
  2249. begin
  2250. if (nodetype in [equaln,unequaln]) then
  2251. inserttypeconv(right,left.resultdef)
  2252. else
  2253. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2254. end
  2255. { support procvar=nil,procvar<>nil }
  2256. else if ((ld.typ=procvardef) and (rt=niln)) or
  2257. ((rd.typ=procvardef) and (lt=niln)) then
  2258. begin
  2259. if not(nodetype in [equaln,unequaln]) then
  2260. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2261. { find proc field in methodpointer record }
  2262. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  2263. if not assigned(hsym) then
  2264. internalerror(200412043);
  2265. { For methodpointers compare only tmethodpointer.proc }
  2266. if (rd.typ=procvardef) and
  2267. (not tprocvardef(rd).is_addressonly) then
  2268. begin
  2269. right:=csubscriptnode.create(
  2270. hsym,
  2271. ctypeconvnode.create_internal(right,methodpointertype));
  2272. typecheckpass(right);
  2273. end;
  2274. if (ld.typ=procvardef) and
  2275. (not tprocvardef(ld).is_addressonly) then
  2276. begin
  2277. left:=csubscriptnode.create(
  2278. hsym,
  2279. ctypeconvnode.create_internal(left,methodpointertype));
  2280. typecheckpass(left);
  2281. end;
  2282. if lt=niln then
  2283. inserttypeconv_explicit(left,right.resultdef)
  2284. else
  2285. inserttypeconv_explicit(right,left.resultdef)
  2286. end
  2287. { <dyn. array>+<dyn. array> ? }
  2288. else if (nodetype=addn) and (is_dynamic_array(ld) or is_dynamic_array(rd)) then
  2289. begin
  2290. result:=maybe_convert_to_insert;
  2291. if assigned(result) then
  2292. exit;
  2293. if not(is_dynamic_array(ld)) then
  2294. inserttypeconv(left,rd);
  2295. if not(is_dynamic_array(rd)) then
  2296. inserttypeconv(right,ld);
  2297. end
  2298. { support dynamicarray=nil,dynamicarray<>nil }
  2299. else if (is_dynamic_array(ld) and (rt=niln)) or
  2300. (is_dynamic_array(rd) and (lt=niln)) or
  2301. (is_dynamic_array(ld) and is_dynamic_array(rd)) then
  2302. begin
  2303. if not(nodetype in [equaln,unequaln]) then
  2304. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2305. if lt=niln then
  2306. inserttypeconv_explicit(left,right.resultdef)
  2307. else
  2308. inserttypeconv_explicit(right,left.resultdef)
  2309. end
  2310. {$ifdef SUPPORT_MMX}
  2311. { mmx support, this must be before the zero based array
  2312. check }
  2313. else if (cs_mmx in current_settings.localswitches) and
  2314. is_mmx_able_array(ld) and
  2315. is_mmx_able_array(rd) and
  2316. equal_defs(ld,rd) then
  2317. begin
  2318. case nodetype of
  2319. addn,subn,xorn,orn,andn:
  2320. ;
  2321. { mul is a little bit restricted }
  2322. muln:
  2323. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  2324. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2325. else
  2326. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2327. end;
  2328. end
  2329. {$endif SUPPORT_MMX}
  2330. { vector support, this must be before the zero based array
  2331. check }
  2332. else if (cs_support_vectors in current_settings.globalswitches) and
  2333. is_vector(ld) and
  2334. is_vector(rd) and
  2335. equal_defs(ld,rd) then
  2336. begin
  2337. if not(nodetype in [addn,subn,xorn,orn,andn,muln,slashn]) then
  2338. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2339. { both defs must be equal, so taking left or right as resultdef doesn't matter }
  2340. resultdef:=left.resultdef;
  2341. end
  2342. { this is a little bit dangerous, also the left type }
  2343. { pointer to should be checked! This broke the mmx support }
  2344. else if (rd.typ=pointerdef) or
  2345. (is_zero_based_array(rd) and (rt<>stringconstn)) then
  2346. begin
  2347. if is_zero_based_array(rd) then
  2348. begin
  2349. resultdef:=cpointerdef.getreusable(tarraydef(rd).elementdef);
  2350. inserttypeconv(right,resultdef);
  2351. end
  2352. else
  2353. resultdef:=right.resultdef;
  2354. inserttypeconv(left,tpointerdef(right.resultdef).pointer_arithmetic_int_type);
  2355. if nodetype=addn then
  2356. begin
  2357. if (rt=niln) then
  2358. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,'NIL');
  2359. if (not(cs_extsyntax in current_settings.moduleswitches) and not(nf_internal in flags)) or
  2360. (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
  2361. not(cs_pointermath in current_settings.localswitches) and
  2362. not((ld.typ=pointerdef) and tpointerdef(ld).has_pointer_math)) then
  2363. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2364. if (rd.typ=pointerdef) and
  2365. (tpointerdef(rd).pointeddef.size>1) then
  2366. begin
  2367. left:=caddnode.create(muln,left,
  2368. cordconstnode.create(tpointerdef(rd).pointeddef.size,tpointerdef(right.resultdef).pointer_arithmetic_int_type,true));
  2369. typecheckpass(left);
  2370. end;
  2371. end
  2372. else
  2373. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2374. end
  2375. else if (ld.typ=pointerdef) or
  2376. (is_zero_based_array(ld) and (lt<>stringconstn)) then
  2377. begin
  2378. if is_zero_based_array(ld) then
  2379. begin
  2380. resultdef:=cpointerdef.getreusable(tarraydef(ld).elementdef);
  2381. inserttypeconv(left,resultdef);
  2382. end
  2383. else
  2384. resultdef:=left.resultdef;
  2385. inserttypeconv(right,tpointerdef(left.resultdef).pointer_arithmetic_int_type);
  2386. if nodetype in [addn,subn] then
  2387. begin
  2388. if (lt=niln) then
  2389. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),'NIL',rd.typename);
  2390. if (not(cs_extsyntax in current_settings.moduleswitches) and not(nf_internal in flags)) or
  2391. (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
  2392. not(cs_pointermath in current_settings.localswitches) and
  2393. not((ld.typ=pointerdef) and tpointerdef(ld).has_pointer_math)) then
  2394. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2395. if (ld.typ=pointerdef) then
  2396. begin
  2397. if is_big_untyped_addrnode(left) then
  2398. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  2399. if (tpointerdef(ld).pointeddef.size>1) then
  2400. begin
  2401. right:=caddnode.create(muln,right,
  2402. cordconstnode.create(tpointerdef(ld).pointeddef.size,tpointerdef(left.resultdef).pointer_arithmetic_int_type,true));
  2403. typecheckpass(right);
  2404. end
  2405. end else
  2406. if is_zero_based_array(ld) and
  2407. (tarraydef(ld).elementdef.size>1) then
  2408. begin
  2409. right:=caddnode.create(muln,right,
  2410. cordconstnode.create(tarraydef(ld).elementdef.size,tpointerdef(left.resultdef).pointer_arithmetic_int_type,true));
  2411. typecheckpass(right);
  2412. end;
  2413. end
  2414. else
  2415. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2416. end
  2417. else if (rd.typ=procvardef) and
  2418. (ld.typ=procvardef) and
  2419. equal_defs(rd,ld) then
  2420. begin
  2421. if (nodetype in [equaln,unequaln]) then
  2422. begin
  2423. if tprocvardef(rd).is_addressonly then
  2424. begin
  2425. inserttypeconv_internal(right,voidcodepointertype);
  2426. inserttypeconv_internal(left,voidcodepointertype);
  2427. end
  2428. else
  2429. begin
  2430. { find proc field in methodpointer record }
  2431. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  2432. if not assigned(hsym) then
  2433. internalerror(200412043);
  2434. { Compare tmehodpointer(left).proc }
  2435. right:=csubscriptnode.create(
  2436. hsym,
  2437. ctypeconvnode.create_internal(right,methodpointertype));
  2438. typecheckpass(right);
  2439. left:=csubscriptnode.create(
  2440. hsym,
  2441. ctypeconvnode.create_internal(left,methodpointertype));
  2442. typecheckpass(left);
  2443. end;
  2444. end
  2445. else
  2446. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2447. end
  2448. { enums }
  2449. else if (ld.typ=enumdef) and (rd.typ=enumdef) then
  2450. begin
  2451. if allowenumop(nodetype) or (nf_internal in flags) then
  2452. inserttypeconv(right,left.resultdef)
  2453. else
  2454. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2455. end
  2456. { generic conversion, this is for error recovery }
  2457. else
  2458. begin
  2459. inserttypeconv(left,sinttype);
  2460. inserttypeconv(right,sinttype);
  2461. end;
  2462. if cmp_of_disjunct_ranges(res) and not(nf_internal in flags) then
  2463. begin
  2464. if res then
  2465. CGMessage(type_w_comparison_always_true)
  2466. else
  2467. CGMessage(type_w_comparison_always_false);
  2468. end;
  2469. { set resultdef if not already done }
  2470. if not assigned(resultdef) then
  2471. begin
  2472. case nodetype of
  2473. ltn,lten,gtn,gten,equaln,unequaln :
  2474. resultdef:=pasbool1type;
  2475. slashn :
  2476. resultdef:=resultrealdef;
  2477. addn:
  2478. begin
  2479. { for strings, return is always a 255 char string }
  2480. if is_shortstring(left.resultdef) then
  2481. resultdef:=cshortstringtype
  2482. else
  2483. { for ansistrings set resultdef to assignment left node
  2484. if it is an assignment and left node expects ansistring }
  2485. if is_ansistring(left.resultdef) and
  2486. assigned(aktassignmentnode) and
  2487. (aktassignmentnode.right=self) and
  2488. is_ansistring(aktassignmentnode.left.resultdef) then
  2489. resultdef:=aktassignmentnode.left.resultdef
  2490. else
  2491. resultdef:=left.resultdef;
  2492. end;
  2493. else
  2494. resultdef:=left.resultdef;
  2495. end;
  2496. end;
  2497. { when the result is currency we need some extra code for
  2498. multiplication and division. this should not be done when
  2499. the muln or slashn node is created internally }
  2500. if not(nf_is_currency in flags) and
  2501. is_currency(resultdef) then
  2502. begin
  2503. case nodetype of
  2504. slashn :
  2505. begin
  2506. { slashn will only work with floats }
  2507. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  2508. include(hp.flags,nf_is_currency);
  2509. result:=hp;
  2510. end;
  2511. muln :
  2512. begin
  2513. hp:=nil;
  2514. if s64currencytype.typ=floatdef then
  2515. begin
  2516. {$ifndef VER3_0}
  2517. { if left is a currency integer constant, we can get rid of the factor 10000 }
  2518. { int64(...) causes a cast on currency, so it is the currency value multiplied by 10000 }
  2519. if (left.nodetype=realconstn) and (is_currency(left.resultdef)) and ((int64(trealconstnode(left).value_currency) mod 10000)=0) then
  2520. begin
  2521. { trealconstnode expects that value_real and value_currency contain valid values }
  2522. trealconstnode(left).value_currency:=trealconstnode(left).value_currency {$ifdef FPC_CURRENCY_IS_INT64}div{$else}/{$endif} 10000;
  2523. trealconstnode(left).value_real:=trealconstnode(left).value_real/10000;
  2524. end
  2525. { or if right is an integer constant, we can get rid of its factor 10000 }
  2526. else if (right.nodetype=realconstn) and (is_currency(right.resultdef)) and ((int64(trealconstnode(right).value_currency) mod 10000)=0) then
  2527. begin
  2528. { trealconstnode expects that value and value_currency contain valid values }
  2529. trealconstnode(right).value_currency:=trealconstnode(right).value_currency {$ifdef FPC_CURRENCY_IS_INT64}div{$else}/{$endif} 10000;
  2530. trealconstnode(right).value_real:=trealconstnode(right).value_real/10000;
  2531. end
  2532. else
  2533. {$endif VER3_0}
  2534. begin
  2535. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype));
  2536. include(hp.flags,nf_is_currency);
  2537. end;
  2538. end
  2539. else
  2540. begin
  2541. {$ifndef VER3_0}
  2542. { if left is a currency integer constant, we can get rid of the factor 10000 }
  2543. if (left.nodetype=ordconstn) and (is_currency(left.resultdef)) and ((tordconstnode(left).value mod 10000)=0) then
  2544. tordconstnode(left).value:=tordconstnode(left).value div 10000
  2545. { or if right is an integer constant, we can get rid of its factor 10000 }
  2546. else if (right.nodetype=ordconstn) and (is_currency(right.resultdef)) and ((tordconstnode(right).value mod 10000)=0) then
  2547. tordconstnode(right).value:=tordconstnode(right).value div 10000
  2548. else
  2549. {$endif VER3_0}
  2550. begin
  2551. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  2552. include(hp.flags,nf_is_currency);
  2553. end
  2554. end;
  2555. result:=hp
  2556. end;
  2557. else
  2558. ;
  2559. end;
  2560. end;
  2561. if not codegenerror and
  2562. not assigned(result) then
  2563. result:=simplify(false);
  2564. end;
  2565. function taddnode.first_addstring: tnode;
  2566. const
  2567. swap_relation: array [ltn..unequaln] of Tnodetype=(gtn, gten, ltn, lten, equaln, unequaln);
  2568. var
  2569. p: tnode;
  2570. newstatement : tstatementnode;
  2571. tempnode (*,tempnode2*) : ttempcreatenode;
  2572. cmpfuncname: string;
  2573. para: tcallparanode;
  2574. begin
  2575. result:=nil;
  2576. { when we get here, we are sure that both the left and the right }
  2577. { node are both strings of the same stringtype (JM) }
  2578. case nodetype of
  2579. addn:
  2580. begin
  2581. if (left.nodetype=stringconstn) and (tstringconstnode(left).len=0) then
  2582. begin
  2583. result:=right;
  2584. left.free;
  2585. left:=nil;
  2586. right:=nil;
  2587. exit;
  2588. end;
  2589. if (right.nodetype=stringconstn) and (tstringconstnode(right).len=0) then
  2590. begin
  2591. result:=left;
  2592. left:=nil;
  2593. right.free;
  2594. right:=nil;
  2595. exit;
  2596. end;
  2597. { create the call to the concat routine both strings as arguments }
  2598. if assigned(aktassignmentnode) and
  2599. (aktassignmentnode.right=self) and
  2600. (aktassignmentnode.left.resultdef=resultdef) and
  2601. valid_for_var(aktassignmentnode.left,false) then
  2602. begin
  2603. para:=ccallparanode.create(
  2604. right,
  2605. ccallparanode.create(
  2606. left,
  2607. ccallparanode.create(aktassignmentnode.left.getcopy,nil)
  2608. )
  2609. );
  2610. if is_ansistring(resultdef) then
  2611. para:=ccallparanode.create(
  2612. cordconstnode.create(
  2613. { don't use getparaencoding(), we have to know
  2614. when the result is rawbytestring }
  2615. tstringdef(resultdef).encoding,
  2616. u16inttype,
  2617. true
  2618. ),
  2619. para
  2620. );
  2621. result:=ccallnode.createintern(
  2622. 'fpc_'+tstringdef(resultdef).stringtypname+'_concat',
  2623. para
  2624. );
  2625. include(aktassignmentnode.flags,nf_assign_done_in_right);
  2626. firstpass(result);
  2627. end
  2628. else
  2629. begin
  2630. result:=internalstatements(newstatement);
  2631. tempnode:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2632. addstatement(newstatement,tempnode);
  2633. { initialize the temp, since it will be passed to a
  2634. var-parameter (and finalization, which is performed by the
  2635. ttempcreate node and which takes care of the initialization
  2636. on native targets, is a noop on managed VM targets) }
  2637. if (target_info.system in systems_managed_vm) and
  2638. is_managed_type(resultdef) then
  2639. addstatement(newstatement,cinlinenode.create(in_setlength_x,
  2640. false,
  2641. ccallparanode.create(genintconstnode(0),
  2642. ccallparanode.create(ctemprefnode.create(tempnode),nil))));
  2643. para:=ccallparanode.create(
  2644. right,
  2645. ccallparanode.create(
  2646. left,
  2647. ccallparanode.create(ctemprefnode.create(tempnode),nil)
  2648. )
  2649. );
  2650. if is_ansistring(resultdef) then
  2651. para:=ccallparanode.create(
  2652. cordconstnode.create(
  2653. { don't use getparaencoding(), we have to know
  2654. when the result is rawbytestring }
  2655. tstringdef(resultdef).encoding,
  2656. u16inttype,
  2657. true
  2658. ),
  2659. para
  2660. );
  2661. addstatement(
  2662. newstatement,
  2663. ccallnode.createintern(
  2664. 'fpc_'+tstringdef(resultdef).stringtypname+'_concat',
  2665. para
  2666. )
  2667. );
  2668. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2669. addstatement(newstatement,ctemprefnode.create(tempnode));
  2670. end;
  2671. { we reused the arguments }
  2672. left := nil;
  2673. right := nil;
  2674. end;
  2675. ltn,lten,gtn,gten,equaln,unequaln :
  2676. begin
  2677. { generate better code for comparison with empty string, we
  2678. only need to compare the length with 0 }
  2679. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  2680. { windows widestrings are too complicated to be handled optimized }
  2681. not(is_widestring(left.resultdef) and (target_info.system in systems_windows)) and
  2682. (((left.nodetype=stringconstn) and (tstringconstnode(left).len=0)) or
  2683. ((right.nodetype=stringconstn) and (tstringconstnode(right).len=0))) then
  2684. begin
  2685. { switch so that the constant is always on the right }
  2686. if left.nodetype = stringconstn then
  2687. begin
  2688. p := left;
  2689. left := right;
  2690. right := p;
  2691. nodetype:=swap_relation[nodetype];
  2692. end;
  2693. if is_shortstring(left.resultdef) or
  2694. (nodetype in [gtn,gten,ltn,lten]) or
  2695. (target_info.system in systems_managed_vm) then
  2696. { compare the length with 0 }
  2697. result := caddnode.create(nodetype,
  2698. cinlinenode.create(in_length_x,false,left),
  2699. cordconstnode.create(0,s8inttype,false))
  2700. else
  2701. begin
  2702. (*
  2703. if is_widestring(left.resultdef) and
  2704. (target_info.system in system_windows) then
  2705. begin
  2706. { windows like widestrings requires that we also check the length }
  2707. result:=internalstatements(newstatement);
  2708. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  2709. tempnode2:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2710. addstatement(newstatement,tempnode);
  2711. addstatement(newstatement,tempnode2);
  2712. { poor man's cse }
  2713. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2714. ctypeconvnode.create_internal(left,voidpointertype))
  2715. );
  2716. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode2),
  2717. caddnode.create(orn,
  2718. caddnode.create(nodetype,
  2719. ctemprefnode.create(tempnode),
  2720. cpointerconstnode.create(0,voidpointertype)
  2721. ),
  2722. caddnode.create(nodetype,
  2723. ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),s32inttype),
  2724. cordconstnode.create(0,s32inttype,false)
  2725. )
  2726. )
  2727. ));
  2728. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2729. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode2));
  2730. addstatement(newstatement,ctemprefnode.create(tempnode2));
  2731. end
  2732. else
  2733. *)
  2734. begin
  2735. { compare the pointer with nil (for ansistrings etc), }
  2736. { faster than getting the length (JM) }
  2737. result:= caddnode.create(nodetype,
  2738. ctypeconvnode.create_internal(left,voidpointertype),
  2739. cpointerconstnode.create(0,voidpointertype));
  2740. end;
  2741. end;
  2742. { left is reused }
  2743. left := nil;
  2744. { right isn't }
  2745. right.free;
  2746. right := nil;
  2747. exit;
  2748. end;
  2749. { no string constant -> call compare routine }
  2750. cmpfuncname := 'fpc_'+tstringdef(left.resultdef).stringtypname+'_compare';
  2751. { for equality checks use optimized version }
  2752. if nodetype in [equaln,unequaln] then
  2753. cmpfuncname := cmpfuncname + '_equal';
  2754. result := ccallnode.createintern(cmpfuncname,
  2755. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2756. { and compare its result with 0 according to the original operator }
  2757. result := caddnode.create(nodetype,result,
  2758. cordconstnode.create(0,s8inttype,false));
  2759. left := nil;
  2760. right := nil;
  2761. end;
  2762. else
  2763. internalerror(2019050520);
  2764. end;
  2765. end;
  2766. function taddnode.first_addset : tnode;
  2767. procedure call_varset_helper(const n : string);
  2768. var
  2769. newstatement : tstatementnode;
  2770. temp : ttempcreatenode;
  2771. begin
  2772. { add two var sets }
  2773. result:=internalstatements(newstatement);
  2774. { create temp for result }
  2775. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2776. addstatement(newstatement,temp);
  2777. addstatement(newstatement,ccallnode.createintern(n,
  2778. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2779. ccallparanode.create(ctemprefnode.create(temp),
  2780. ccallparanode.create(right,
  2781. ccallparanode.create(left,nil)))))
  2782. );
  2783. { remove reused parts from original node }
  2784. left:=nil;
  2785. right:=nil;
  2786. { the last statement should return the value as
  2787. location and type, this is done be referencing the
  2788. temp and converting it first from a persistent temp to
  2789. normal temp }
  2790. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2791. addstatement(newstatement,ctemprefnode.create(temp));
  2792. end;
  2793. var
  2794. procname: string[31];
  2795. tempn: tnode;
  2796. newstatement : tstatementnode;
  2797. temp : ttempcreatenode;
  2798. begin
  2799. result:=nil;
  2800. case nodetype of
  2801. equaln,unequaln,lten,gten:
  2802. begin
  2803. case nodetype of
  2804. equaln,unequaln:
  2805. procname := 'fpc_varset_comp_sets';
  2806. lten,gten:
  2807. begin
  2808. procname := 'fpc_varset_contains_sets';
  2809. { (left >= right) = (right <= left) }
  2810. if nodetype = gten then
  2811. begin
  2812. tempn := left;
  2813. left := right;
  2814. right := tempn;
  2815. end;
  2816. end;
  2817. else
  2818. internalerror(2013112911);
  2819. end;
  2820. result := ccallnode.createinternres(procname,
  2821. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  2822. ccallparanode.create(right,
  2823. ccallparanode.create(left,nil))),resultdef);
  2824. { left and right are reused as parameters }
  2825. left := nil;
  2826. right := nil;
  2827. { for an unequaln, we have to negate the result of comp_sets }
  2828. if nodetype = unequaln then
  2829. result := cnotnode.create(result);
  2830. end;
  2831. addn:
  2832. begin
  2833. { optimize first loading of a set }
  2834. if (right.nodetype=setelementn) and
  2835. not(assigned(tsetelementnode(right).right)) and
  2836. is_emptyset(left) then
  2837. begin
  2838. result:=internalstatements(newstatement);
  2839. { create temp for result }
  2840. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2841. addstatement(newstatement,temp);
  2842. { adjust for set base }
  2843. tsetelementnode(right).left:=caddnode.create(subn,
  2844. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2845. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2846. addstatement(newstatement,ccallnode.createintern('fpc_varset_create_element',
  2847. ccallparanode.create(ctemprefnode.create(temp),
  2848. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2849. ccallparanode.create(tsetelementnode(right).left,nil))))
  2850. );
  2851. { the last statement should return the value as
  2852. location and type, this is done be referencing the
  2853. temp and converting it first from a persistent temp to
  2854. normal temp }
  2855. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2856. addstatement(newstatement,ctemprefnode.create(temp));
  2857. tsetelementnode(right).left := nil;
  2858. end
  2859. else
  2860. begin
  2861. if right.nodetype=setelementn then
  2862. begin
  2863. result:=internalstatements(newstatement);
  2864. { create temp for result }
  2865. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2866. addstatement(newstatement,temp);
  2867. { adjust for set base }
  2868. tsetelementnode(right).left:=caddnode.create(subn,
  2869. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2870. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2871. { add a range or a single element? }
  2872. if assigned(tsetelementnode(right).right) then
  2873. begin
  2874. { adjust for set base }
  2875. tsetelementnode(right).right:=caddnode.create(subn,
  2876. ctypeconvnode.create_internal(tsetelementnode(right).right,sinttype),
  2877. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2878. addstatement(newstatement,ccallnode.createintern('fpc_varset_set_range',
  2879. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2880. ccallparanode.create(tsetelementnode(right).right,
  2881. ccallparanode.create(tsetelementnode(right).left,
  2882. ccallparanode.create(ctemprefnode.create(temp),
  2883. ccallparanode.create(left,nil))))))
  2884. );
  2885. end
  2886. else
  2887. addstatement(newstatement,ccallnode.createintern('fpc_varset_set',
  2888. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2889. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2890. ccallparanode.create(ctemprefnode.create(temp),
  2891. ccallparanode.create(left,nil)))))
  2892. );
  2893. { remove reused parts from original node }
  2894. tsetelementnode(right).right:=nil;
  2895. tsetelementnode(right).left:=nil;
  2896. left:=nil;
  2897. { the last statement should return the value as
  2898. location and type, this is done be referencing the
  2899. temp and converting it first from a persistent temp to
  2900. normal temp }
  2901. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2902. addstatement(newstatement,ctemprefnode.create(temp));
  2903. end
  2904. else
  2905. call_varset_helper('fpc_varset_add_sets');
  2906. end
  2907. end;
  2908. subn:
  2909. call_varset_helper('fpc_varset_sub_sets');
  2910. symdifn:
  2911. call_varset_helper('fpc_varset_symdif_sets');
  2912. muln:
  2913. call_varset_helper('fpc_varset_mul_sets');
  2914. else
  2915. internalerror(200609241);
  2916. end;
  2917. end;
  2918. function taddnode.first_adddynarray : tnode;
  2919. var
  2920. newstatement : tstatementnode;
  2921. tempnode (*,tempnode2*) : ttempcreatenode;
  2922. para: tcallparanode;
  2923. begin
  2924. result:=nil;
  2925. { when we get here, we are sure that both the left and the right }
  2926. { node are both strings of the same stringtype (JM) }
  2927. case nodetype of
  2928. addn:
  2929. begin
  2930. if (left.nodetype=arrayconstructorn) and (tarrayconstructornode(left).isempty) then
  2931. begin
  2932. result:=right;
  2933. left.free;
  2934. left:=nil;
  2935. right:=nil;
  2936. exit;
  2937. end;
  2938. if (right.nodetype=arrayconstructorn) and (tarrayconstructornode(right).isempty) then
  2939. begin
  2940. result:=left;
  2941. left:=nil;
  2942. right.free;
  2943. right:=nil;
  2944. exit;
  2945. end;
  2946. { create the call to the concat routine both strings as arguments }
  2947. if assigned(aktassignmentnode) and
  2948. (aktassignmentnode.right=self) and
  2949. (aktassignmentnode.left.resultdef=resultdef) and
  2950. valid_for_var(aktassignmentnode.left,false) then
  2951. begin
  2952. para:=ccallparanode.create(
  2953. ctypeconvnode.create_internal(right,voidcodepointertype),
  2954. ccallparanode.create(
  2955. ctypeconvnode.create_internal(left,voidcodepointertype),
  2956. ccallparanode.create(
  2957. caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  2958. ccallparanode.create(
  2959. ctypeconvnode.create_internal(aktassignmentnode.left.getcopy,voidcodepointertype),nil)
  2960. )));
  2961. result:=ccallnode.createintern(
  2962. 'fpc_dynarray_concat',
  2963. para
  2964. );
  2965. include(aktassignmentnode.flags,nf_assign_done_in_right);
  2966. firstpass(result);
  2967. end
  2968. else
  2969. begin
  2970. result:=internalstatements(newstatement);
  2971. tempnode:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2972. addstatement(newstatement,tempnode);
  2973. { initialize the temp, since it will be passed to a
  2974. var-parameter (and finalization, which is performed by the
  2975. ttempcreate node and which takes care of the initialization
  2976. on native targets, is a noop on managed VM targets) }
  2977. if (target_info.system in systems_managed_vm) and
  2978. is_managed_type(resultdef) then
  2979. addstatement(newstatement,cinlinenode.create(in_setlength_x,
  2980. false,
  2981. ccallparanode.create(genintconstnode(0),
  2982. ccallparanode.create(ctemprefnode.create(tempnode),nil))));
  2983. para:=ccallparanode.create(
  2984. ctypeconvnode.create_internal(right,voidcodepointertype),
  2985. ccallparanode.create(
  2986. ctypeconvnode.create_internal(left,voidcodepointertype),
  2987. ccallparanode.create(
  2988. caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  2989. ccallparanode.create(
  2990. ctypeconvnode.create_internal(ctemprefnode.create(tempnode),voidcodepointertype),nil)
  2991. )));
  2992. addstatement(
  2993. newstatement,
  2994. ccallnode.createintern(
  2995. 'fpc_dynarray_concat',
  2996. para
  2997. )
  2998. );
  2999. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  3000. addstatement(newstatement,ctemprefnode.create(tempnode));
  3001. end;
  3002. { we reused the arguments }
  3003. left := nil;
  3004. right := nil;
  3005. end;
  3006. unequaln,equaln:
  3007. { nothing to do }
  3008. ;
  3009. else
  3010. Internalerror(2018030301);
  3011. end;
  3012. end;
  3013. function taddnode.use_generic_mul32to64: boolean;
  3014. begin
  3015. result := true;
  3016. end;
  3017. function taddnode.use_generic_mul64bit: boolean;
  3018. begin
  3019. result := true;
  3020. end;
  3021. function taddnode.try_make_mul32to64: boolean;
  3022. function canbe32bitint(v: tconstexprint; out canbesignedconst, canbeunsignedconst: boolean): boolean;
  3023. begin
  3024. result := ((v >= int64(low(longint))) and (v <= int64(high(longint)))) or
  3025. ((v >= qword(low(cardinal))) and (v <= qword(high(cardinal))));
  3026. canbesignedconst:=v<=int64(high(longint));
  3027. canbeunsignedconst:=v>=0;
  3028. end;
  3029. function is_32bitordconst(n: tnode; out canbesignedconst, canbeunsignedconst: boolean): boolean;
  3030. begin
  3031. canbesignedconst:=false;
  3032. canbeunsignedconst:=false;
  3033. result := (n.nodetype = ordconstn) and
  3034. canbe32bitint(tordconstnode(n).value, canbesignedconst, canbeunsignedconst);
  3035. end;
  3036. function is_32to64typeconv(n: tnode): boolean;
  3037. begin
  3038. result := (n.nodetype = typeconvn) and
  3039. is_integer(ttypeconvnode(n).left.resultdef) and
  3040. not is_64bit(ttypeconvnode(n).left.resultdef);
  3041. end;
  3042. var
  3043. temp: tnode;
  3044. leftoriginallysigned,
  3045. canbesignedconst, canbeunsignedconst: boolean;
  3046. begin
  3047. result := false;
  3048. if is_32to64typeconv(left) then
  3049. begin
  3050. leftoriginallysigned:=is_signed(ttypeconvnode(left).left.resultdef);
  3051. if ((is_32bitordconst(right,canbesignedconst, canbeunsignedconst) and
  3052. ((leftoriginallysigned and canbesignedconst) or
  3053. (not leftoriginallysigned and canbeunsignedconst))) or
  3054. (is_32to64typeconv(right) and
  3055. ((leftoriginallysigned =
  3056. is_signed(ttypeconvnode(right).left.resultdef)) or
  3057. (leftoriginallysigned and
  3058. (torddef(ttypeconvnode(right).left.resultdef).ordtype in [u8bit,u16bit]))))) then
  3059. begin
  3060. temp := ttypeconvnode(left).left;
  3061. ttypeconvnode(left).left := nil;
  3062. left.free;
  3063. left := temp;
  3064. if (right.nodetype = typeconvn) then
  3065. begin
  3066. temp := ttypeconvnode(right).left;
  3067. ttypeconvnode(right).left := nil;
  3068. right.free;
  3069. right := temp;
  3070. end;
  3071. if (is_signed(left.resultdef)) then
  3072. begin
  3073. inserttypeconv_internal(left,s32inttype);
  3074. inserttypeconv_internal(right,s32inttype);
  3075. end
  3076. else
  3077. begin
  3078. inserttypeconv_internal(left,u32inttype);
  3079. inserttypeconv_internal(right,u32inttype);
  3080. end;
  3081. firstpass(left);
  3082. firstpass(right);
  3083. result := true;
  3084. end;
  3085. end;
  3086. end;
  3087. function taddnode.use_fma : boolean;
  3088. begin
  3089. result:=false;
  3090. end;
  3091. function taddnode.try_fma(ld,rd : tdef) : tnode;
  3092. var
  3093. inlinennr : tinlinenumber;
  3094. begin
  3095. result:=nil;
  3096. if (cs_opt_fastmath in current_settings.optimizerswitches) and
  3097. use_fma and
  3098. (nodetype in [addn,subn]) and
  3099. (rd.typ=floatdef) and (ld.typ=floatdef) and
  3100. (is_single(rd) or is_double(rd)) and
  3101. equal_defs(rd,ld) and
  3102. { transforming a*b+c into fma(a,b,c) makes only sense if c can be
  3103. calculated easily. Consider a*b+c*d which results in
  3104. fmul
  3105. fmul
  3106. fadd
  3107. and in
  3108. fmul
  3109. fma
  3110. when using the fma optimization. On a super scalar architecture, the first instruction
  3111. sequence requires clock_cycles(fmul)+clock_cycles(fadd) clock cycles because the fmuls can be executed in parallel.
  3112. The second sequence requires clock_cycles(fmul)+clock_cycles(fma) because the fma has to wait for the
  3113. result of the fmul. Since typically clock_cycles(fma)>clock_cycles(fadd) applies, the first sequence is better.
  3114. }
  3115. (((left.nodetype=muln) and (node_complexity(right)<3)) or
  3116. ((right.nodetype=muln) and (node_complexity(left)<3)) or
  3117. ((left.nodetype=inlinen) and
  3118. (tinlinenode(left).inlinenumber=in_sqr_real) and
  3119. (node_complexity(right)<3)) or
  3120. ((right.nodetype=inlinen) and
  3121. (tinlinenode(right).inlinenumber=in_sqr_real) and
  3122. (node_complexity(left)<3))
  3123. ) then
  3124. begin
  3125. case tfloatdef(ld).floattype of
  3126. s32real:
  3127. inlinennr:=in_fma_single;
  3128. s64real:
  3129. inlinennr:=in_fma_double;
  3130. s80real:
  3131. inlinennr:=in_fma_extended;
  3132. s128real:
  3133. inlinennr:=in_fma_float128;
  3134. else
  3135. internalerror(2014042601);
  3136. end;
  3137. if left.nodetype=muln then
  3138. begin
  3139. if nodetype=subn then
  3140. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(cunaryminusnode.create(right),
  3141. ccallparanode.create(taddnode(left).right,
  3142. ccallparanode.create(taddnode(left).left,nil
  3143. ))))
  3144. else
  3145. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(right,
  3146. ccallparanode.create(taddnode(left).right,
  3147. ccallparanode.create(taddnode(left).left,nil
  3148. ))));
  3149. right:=nil;
  3150. taddnode(left).right:=nil;
  3151. taddnode(left).left:=nil;
  3152. end
  3153. else if right.nodetype=muln then
  3154. begin
  3155. if nodetype=subn then
  3156. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  3157. ccallparanode.create(cunaryminusnode.create(taddnode(right).right),
  3158. ccallparanode.create(taddnode(right).left,nil
  3159. ))))
  3160. else
  3161. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  3162. ccallparanode.create(taddnode(right).right,
  3163. ccallparanode.create(taddnode(right).left,nil
  3164. ))));
  3165. left:=nil;
  3166. taddnode(right).right:=nil;
  3167. taddnode(right).left:=nil;
  3168. end
  3169. else if (left.nodetype=inlinen) and (tinlinenode(left).inlinenumber=in_sqr_real) then
  3170. begin
  3171. if nodetype=subn then
  3172. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(cunaryminusnode.create(right),
  3173. ccallparanode.create(tinlinenode(left).left.getcopy,
  3174. ccallparanode.create(tinlinenode(left).left.getcopy,nil
  3175. ))))
  3176. else
  3177. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(right,
  3178. ccallparanode.create(tinlinenode(left).left.getcopy,
  3179. ccallparanode.create(tinlinenode(left).left.getcopy,nil
  3180. ))));
  3181. right:=nil;
  3182. end
  3183. { we get here only if right is a sqr node }
  3184. else if (right.nodetype=inlinen) and (tinlinenode(right).inlinenumber=in_sqr_real) then
  3185. begin
  3186. if nodetype=subn then
  3187. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  3188. ccallparanode.create(cunaryminusnode.create(tinlinenode(right).left.getcopy),
  3189. ccallparanode.create(tinlinenode(right).left.getcopy,nil
  3190. ))))
  3191. else
  3192. result:=cinlinenode.create(inlinennr,false,ccallparanode.create(left,
  3193. ccallparanode.create(tinlinenode(right).left.getcopy,
  3194. ccallparanode.create(tinlinenode(right).left.getcopy,nil
  3195. ))));
  3196. left:=nil;
  3197. end;
  3198. end;
  3199. end;
  3200. function taddnode.first_add64bitint: tnode;
  3201. var
  3202. procname: string[31];
  3203. temp: tnode;
  3204. power: longint;
  3205. begin
  3206. result := nil;
  3207. { create helper calls mul }
  3208. if nodetype <> muln then
  3209. exit;
  3210. { make sure that if there is a constant, that it's on the right }
  3211. if left.nodetype = ordconstn then
  3212. begin
  3213. temp := right;
  3214. right := left;
  3215. left := temp;
  3216. end;
  3217. { can we use a shift instead of a mul? }
  3218. if not (cs_check_overflow in current_settings.localswitches) and
  3219. (right.nodetype = ordconstn) and
  3220. ispowerof2(tordconstnode(right).value,power) then
  3221. begin
  3222. tordconstnode(right).value := power;
  3223. result := cshlshrnode.create(shln,left,right);
  3224. { left and right are reused }
  3225. left := nil;
  3226. right := nil;
  3227. { return firstpassed new node }
  3228. exit;
  3229. end;
  3230. if try_make_mul32to64 then
  3231. begin
  3232. { this uses the same criteria for signedness as the 32 to 64-bit mul
  3233. handling in the i386 code generator }
  3234. if is_signed(left.resultdef) and is_signed(right.resultdef) then
  3235. procname := 'fpc_mul_longint_to_int64'
  3236. else
  3237. procname := 'fpc_mul_dword_to_qword';
  3238. right := ccallparanode.create(right,ccallparanode.create(left,nil));
  3239. result := ccallnode.createintern(procname,right);
  3240. left := nil;
  3241. right := nil;
  3242. end
  3243. else
  3244. begin
  3245. { can full 64-bit multiplication be handled inline? }
  3246. if not use_generic_mul64bit then
  3247. begin
  3248. { generic handling replaces this node with call to fpc_mul_int64,
  3249. whose result is int64 }
  3250. if is_currency(resultdef) then
  3251. resultdef:=s64inttype;
  3252. exit;
  3253. end;
  3254. { when currency is used set the result of the
  3255. parameters to s64bit, so they are not converted }
  3256. if is_currency(resultdef) then
  3257. begin
  3258. left.resultdef:=s64inttype;
  3259. right.resultdef:=s64inttype;
  3260. end;
  3261. { otherwise, create the parameters for the helper }
  3262. right := ccallparanode.create(right,ccallparanode.create(left,nil));
  3263. left := nil;
  3264. { only qword needs the unsigned code, the
  3265. signed code is also used for currency }
  3266. if is_signed(resultdef) then
  3267. procname := 'fpc_mul_int64'
  3268. else
  3269. procname := 'fpc_mul_qword';
  3270. if cs_check_overflow in current_settings.localswitches then
  3271. procname := procname + '_checkoverflow';
  3272. result := ccallnode.createintern(procname,right);
  3273. right := nil;
  3274. end;
  3275. end;
  3276. function taddnode.first_addpointer: tnode;
  3277. begin
  3278. result:=nil;
  3279. expectloc:=LOC_REGISTER;
  3280. end;
  3281. function taddnode.first_cmppointer: tnode;
  3282. begin
  3283. result:=nil;
  3284. expectloc:=LOC_FLAGS;
  3285. end;
  3286. function taddnode.first_addfloat : tnode;
  3287. var
  3288. procname: string[31];
  3289. { do we need to reverse the result ? }
  3290. notnode : boolean;
  3291. fdef : tdef;
  3292. begin
  3293. result := nil;
  3294. notnode := false;
  3295. fdef := nil;
  3296. { In non-emulation mode, real opcodes are
  3297. emitted for floating point values.
  3298. }
  3299. if not ((cs_fp_emulation in current_settings.moduleswitches)
  3300. {$ifdef cpufpemu}
  3301. or (current_settings.fputype=fpu_soft)
  3302. {$endif cpufpemu}
  3303. ) then
  3304. exit;
  3305. if not(target_info.system in systems_wince) then
  3306. begin
  3307. case tfloatdef(left.resultdef).floattype of
  3308. s32real:
  3309. begin
  3310. fdef:=search_system_type('FLOAT32REC').typedef;
  3311. procname:='float32';
  3312. end;
  3313. s64real:
  3314. begin
  3315. fdef:=search_system_type('FLOAT64').typedef;
  3316. procname:='float64';
  3317. end;
  3318. {!!! not yet implemented
  3319. s128real:
  3320. }
  3321. else
  3322. internalerror(2005082601);
  3323. end;
  3324. case nodetype of
  3325. addn:
  3326. procname:=procname+'_add';
  3327. muln:
  3328. procname:=procname+'_mul';
  3329. subn:
  3330. procname:=procname+'_sub';
  3331. slashn:
  3332. procname:=procname+'_div';
  3333. ltn:
  3334. procname:=procname+'_lt';
  3335. lten:
  3336. procname:=procname+'_le';
  3337. gtn:
  3338. begin
  3339. procname:=procname+'_lt';
  3340. swapleftright;
  3341. end;
  3342. gten:
  3343. begin
  3344. procname:=procname+'_le';
  3345. swapleftright;
  3346. end;
  3347. equaln:
  3348. procname:=procname+'_eq';
  3349. unequaln:
  3350. begin
  3351. procname:=procname+'_eq';
  3352. notnode:=true;
  3353. end;
  3354. else
  3355. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  3356. end;
  3357. end
  3358. else
  3359. begin
  3360. case nodetype of
  3361. addn:
  3362. procname:='add';
  3363. muln:
  3364. procname:='mul';
  3365. subn:
  3366. procname:='sub';
  3367. slashn:
  3368. procname:='div';
  3369. ltn:
  3370. procname:='lt';
  3371. lten:
  3372. procname:='le';
  3373. gtn:
  3374. procname:='gt';
  3375. gten:
  3376. procname:='ge';
  3377. equaln:
  3378. procname:='eq';
  3379. unequaln:
  3380. procname:='ne';
  3381. else
  3382. begin
  3383. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  3384. exit;
  3385. end;
  3386. end;
  3387. case tfloatdef(left.resultdef).floattype of
  3388. s32real:
  3389. begin
  3390. procname:=procname+'s';
  3391. if nodetype in [addn,muln,subn,slashn] then
  3392. procname:=lower(procname);
  3393. end;
  3394. s64real:
  3395. procname:=procname+'d';
  3396. {!!! not yet implemented
  3397. s128real:
  3398. }
  3399. else
  3400. internalerror(2005082602);
  3401. end;
  3402. end;
  3403. { cast softfpu result? }
  3404. if not(target_info.system in systems_wince) then
  3405. begin
  3406. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  3407. resultdef:=pasbool1type;
  3408. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  3409. ctypeconvnode.create_internal(right,fdef),
  3410. ccallparanode.create(
  3411. ctypeconvnode.create_internal(left,fdef),nil))),resultdef);
  3412. end
  3413. else
  3414. result:=ccallnode.createintern(procname,ccallparanode.create(right,
  3415. ccallparanode.create(left,nil)));
  3416. left:=nil;
  3417. right:=nil;
  3418. { do we need to reverse the result }
  3419. if notnode then
  3420. result:=cnotnode.create(result);
  3421. end;
  3422. {$ifdef cpuneedsmulhelper}
  3423. function taddnode.use_mul_helper: boolean;
  3424. begin
  3425. result:=(nodetype=muln) and
  3426. not(torddef(resultdef).ordtype in [u8bit,s8bit
  3427. {$if defined(cpu16bitalu) or defined(avr)},u16bit,s16bit{$endif}]);
  3428. end;
  3429. {$endif cpuneedsmulhelper}
  3430. function taddnode.pass_1 : tnode;
  3431. function isconstsetfewelements(p : tnode) : boolean;
  3432. begin
  3433. result:=(p.nodetype=setconstn) and (tsetconstnode(p).elements<=4);
  3434. end;
  3435. var
  3436. {$ifdef addstringopt}
  3437. hp : tnode;
  3438. {$endif addstringopt}
  3439. rd,ld : tdef;
  3440. i,i2 : longint;
  3441. lt,rt : tnodetype;
  3442. {$ifdef cpuneedsmulhelper}
  3443. procname : string[32];
  3444. {$endif cpuneedsmulhelper}
  3445. tempn,varsetnode: tnode;
  3446. mulnode : taddnode;
  3447. constsetnode : tsetconstnode;
  3448. trycreateinnodes : Boolean;
  3449. begin
  3450. result:=nil;
  3451. { Can we optimize multiple string additions into a single call?
  3452. This need to be done on a complete tree to detect the multiple
  3453. add nodes and is therefor done before the subtrees are processed }
  3454. if canbemultistringadd(self) then
  3455. begin
  3456. result:=genmultistringadd(self);
  3457. exit;
  3458. end;
  3459. { Can we optimize multiple dyn. array additions into a single call?
  3460. This need to be done on a complete tree to detect the multiple
  3461. add nodes and is therefor done before the subtrees are processed }
  3462. if (m_array_operators in current_settings.modeswitches) and canbemultidynarrayadd(self) then
  3463. begin
  3464. result:=genmultidynarrayadd(self);
  3465. exit;
  3466. end;
  3467. { typical set tests like (s*[const. set])<>/=[] can be converted into an or'ed chain of in tests
  3468. for var sets if const. set contains only a few elements }
  3469. 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
  3470. begin
  3471. trycreateinnodes:=false;
  3472. mulnode:=nil;
  3473. if (is_emptyset(right) and (left.nodetype=muln) and
  3474. (isconstsetfewelements(taddnode(left).right) or isconstsetfewelements(taddnode(left).left))) then
  3475. begin
  3476. trycreateinnodes:=true;
  3477. mulnode:=taddnode(left);
  3478. end
  3479. else if (is_emptyset(left) and (right.nodetype=muln) and
  3480. (isconstsetfewelements(taddnode(right).right) or isconstsetfewelements(taddnode(right).left))) then
  3481. begin
  3482. trycreateinnodes:=true;
  3483. mulnode:=taddnode(right);
  3484. end;
  3485. if trycreateinnodes then
  3486. begin
  3487. constsetnode:=nil;
  3488. varsetnode:=nil;
  3489. if isconstsetfewelements(mulnode.right) then
  3490. begin
  3491. constsetnode:=tsetconstnode(mulnode.right);
  3492. varsetnode:=mulnode.left;
  3493. end
  3494. else
  3495. begin
  3496. constsetnode:=tsetconstnode(mulnode.left);
  3497. varsetnode:=mulnode.right;
  3498. end;
  3499. { the node is copied so it might have no side effects, if the complexity is too, cse should fix it, so
  3500. do not check complexity }
  3501. if not(might_have_sideeffects(varsetnode)) then
  3502. begin
  3503. result:=nil;
  3504. for i:=low(tconstset) to high(tconstset) do
  3505. if i in constsetnode.value_set^ then
  3506. begin
  3507. tempn:=cinnode.create(cordconstnode.create(i,tsetdef(constsetnode.resultdef).elementdef,false),varsetnode.getcopy);
  3508. if assigned(result) then
  3509. result:=caddnode.create_internal(orn,result,tempn)
  3510. else
  3511. result:=tempn;
  3512. end;
  3513. if nodetype=equaln then
  3514. result:=cnotnode.create(result);
  3515. exit;
  3516. end;
  3517. end;
  3518. end;
  3519. { first do the two subtrees }
  3520. firstpass(left);
  3521. firstpass(right);
  3522. if codegenerror then
  3523. exit;
  3524. { load easier access variables }
  3525. rd:=right.resultdef;
  3526. ld:=left.resultdef;
  3527. rt:=right.nodetype;
  3528. lt:=left.nodetype;
  3529. { int/int gives real/real! }
  3530. if nodetype=slashn then
  3531. begin
  3532. {$ifdef cpufpemu}
  3533. result:=first_addfloat;
  3534. if assigned(result) then
  3535. exit;
  3536. {$endif cpufpemu}
  3537. expectloc:=LOC_FPUREGISTER;
  3538. end
  3539. { if both are orddefs then check sub types }
  3540. else if (ld.typ=orddef) and (rd.typ=orddef) then
  3541. begin
  3542. { optimize multiplacation by a power of 2 }
  3543. if not(cs_check_overflow in current_settings.localswitches) and
  3544. (nodetype = muln) and
  3545. (((left.nodetype = ordconstn) and
  3546. ispowerof2(tordconstnode(left).value,i)) or
  3547. ((right.nodetype = ordconstn) and
  3548. ispowerof2(tordconstnode(right).value,i2))) then
  3549. begin
  3550. if ((left.nodetype = ordconstn) and
  3551. ispowerof2(tordconstnode(left).value,i)) then
  3552. begin
  3553. tordconstnode(left).value := i;
  3554. result := cshlshrnode.create(shln,right,left);
  3555. end
  3556. else
  3557. begin
  3558. tordconstnode(right).value := i2;
  3559. result := cshlshrnode.create(shln,left,right);
  3560. end;
  3561. result.resultdef := resultdef;
  3562. left := nil;
  3563. right := nil;
  3564. exit;
  3565. end;
  3566. { 2 booleans ? }
  3567. if is_boolean(ld) and is_boolean(rd) then
  3568. begin
  3569. if (not(cs_full_boolean_eval in current_settings.localswitches) or
  3570. (nf_short_bool in flags)) and
  3571. (nodetype in [andn,orn]) then
  3572. expectloc:=LOC_JUMP
  3573. else
  3574. begin
  3575. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  3576. expectloc:=LOC_FLAGS
  3577. else
  3578. expectloc:=LOC_REGISTER;
  3579. end;
  3580. end
  3581. else
  3582. { Both are chars? only convert to shortstrings for addn }
  3583. if is_char(ld) then
  3584. begin
  3585. if nodetype=addn then
  3586. internalerror(200103291);
  3587. expectloc:=LOC_FLAGS;
  3588. end
  3589. else if (nodetype=muln) and
  3590. is_64bitint(resultdef) and
  3591. not use_generic_mul32to64 and
  3592. try_make_mul32to64 then
  3593. begin
  3594. { if the code generator can handle 32 to 64-bit muls,
  3595. we're done here }
  3596. expectloc:=LOC_REGISTER;
  3597. end
  3598. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  3599. { is there a 64 bit type ? }
  3600. else if (torddef(ld).ordtype in [s64bit,u64bit,scurrency]) then
  3601. begin
  3602. result := first_add64bitint;
  3603. if assigned(result) then
  3604. exit;
  3605. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  3606. expectloc:=LOC_REGISTER
  3607. else
  3608. expectloc:=LOC_JUMP;
  3609. end
  3610. {$else if defined(llvm) and cpu32bitalu}
  3611. { llvm does not support 128 bit math on 32 bit targets, which is
  3612. necessary for overflow checking 64 bit operations }
  3613. else if (torddef(ld).ordtype in [s64bit,u64bit,scurrency]) and
  3614. (cs_check_overflow in current_settings.localswitches) and
  3615. (nodetype in [addn,subn,muln]) then
  3616. begin
  3617. result := first_add64bitint;
  3618. if assigned(result) then
  3619. exit;
  3620. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  3621. expectloc:=LOC_REGISTER
  3622. else
  3623. expectloc:=LOC_JUMP;
  3624. end
  3625. {$endif not(cpu64bitalu) and not(cpuhighleveltarget)}
  3626. { generic 32bit conversion }
  3627. else
  3628. begin
  3629. {$ifdef cpuneedsmulhelper}
  3630. if use_mul_helper then
  3631. begin
  3632. result := nil;
  3633. case torddef(resultdef).ordtype of
  3634. s16bit:
  3635. procname := 'fpc_mul_integer';
  3636. u16bit:
  3637. procname := 'fpc_mul_word';
  3638. s32bit:
  3639. procname := 'fpc_mul_longint';
  3640. u32bit:
  3641. procname := 'fpc_mul_dword';
  3642. else
  3643. internalerror(2011022301);
  3644. end;
  3645. if cs_check_overflow in current_settings.localswitches then
  3646. procname:=procname+'_checkoverflow';
  3647. result := ccallnode.createintern(procname,
  3648. ccallparanode.create(right,
  3649. ccallparanode.create(left,nil)));
  3650. left := nil;
  3651. right := nil;
  3652. firstpass(result);
  3653. exit;
  3654. end;
  3655. {$endif cpuneedsmulhelper}
  3656. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  3657. expectloc:=LOC_REGISTER
  3658. {$if not defined(cpuhighleveltarget)}
  3659. else if torddef(ld).size>sizeof(aint) then
  3660. expectloc:=LOC_JUMP
  3661. {$endif}
  3662. else
  3663. expectloc:=LOC_FLAGS;
  3664. end;
  3665. end
  3666. { left side a setdef, must be before string processing,
  3667. else array constructor can be seen as array of char (PFV) }
  3668. else if (ld.typ=setdef) then
  3669. begin
  3670. { small sets are handled inline by the compiler.
  3671. small set doesn't have support for adding ranges }
  3672. if is_smallset(ld) and
  3673. not(
  3674. (right.nodetype=setelementn) and
  3675. assigned(tsetelementnode(right).right)
  3676. ) then
  3677. begin
  3678. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  3679. expectloc:=LOC_FLAGS
  3680. else
  3681. expectloc:=LOC_REGISTER;
  3682. end
  3683. else
  3684. begin
  3685. result := first_addset;
  3686. if assigned(result) then
  3687. exit;
  3688. expectloc:=LOC_CREFERENCE;
  3689. end;
  3690. end
  3691. { compare pchar by addresses like BP/Delphi }
  3692. else if is_pchar(ld) then
  3693. begin
  3694. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  3695. result:=first_addpointer
  3696. else
  3697. result:=first_cmppointer;
  3698. end
  3699. { is one of the operands a string }
  3700. else if (ld.typ=stringdef) then
  3701. begin
  3702. if is_widestring(ld) then
  3703. begin
  3704. { this is only for add, the comparisaion is handled later }
  3705. expectloc:=LOC_REGISTER;
  3706. end
  3707. else if is_unicodestring(ld) then
  3708. begin
  3709. { this is only for add, the comparisaion is handled later }
  3710. expectloc:=LOC_REGISTER;
  3711. end
  3712. else if is_ansistring(ld) then
  3713. begin
  3714. { this is only for add, the comparisaion is handled later }
  3715. expectloc:=LOC_REGISTER;
  3716. end
  3717. else if is_longstring(ld) then
  3718. begin
  3719. { this is only for add, the comparisaion is handled later }
  3720. expectloc:=LOC_REFERENCE;
  3721. end
  3722. else
  3723. begin
  3724. {$ifdef addstringopt}
  3725. { can create a call which isn't handled by callparatemp }
  3726. if canbeaddsstringcharoptnode(self) then
  3727. begin
  3728. hp := genaddsstringcharoptnode(self);
  3729. pass_1 := hp;
  3730. exit;
  3731. end
  3732. else
  3733. {$endif addstringopt}
  3734. begin
  3735. { Fix right to be shortstring }
  3736. if is_char(right.resultdef) then
  3737. begin
  3738. inserttypeconv(right,cshortstringtype);
  3739. firstpass(right);
  3740. end;
  3741. end;
  3742. {$ifdef addstringopt}
  3743. { can create a call which isn't handled by callparatemp }
  3744. if canbeaddsstringcsstringoptnode(self) then
  3745. begin
  3746. hp := genaddsstringcsstringoptnode(self);
  3747. pass_1 := hp;
  3748. exit;
  3749. end;
  3750. {$endif addstringopt}
  3751. end;
  3752. { otherwise, let addstring convert everything }
  3753. result := first_addstring;
  3754. exit;
  3755. end
  3756. { is one a real float ? }
  3757. else if (rd.typ=floatdef) or (ld.typ=floatdef) then
  3758. begin
  3759. {$ifdef cpufpemu}
  3760. result:=first_addfloat;
  3761. if assigned(result) then
  3762. exit;
  3763. {$endif cpufpemu}
  3764. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  3765. expectloc:=LOC_FPUREGISTER
  3766. else
  3767. expectloc:=LOC_FLAGS;
  3768. result:=try_fma(ld,rd);
  3769. if assigned(result) then
  3770. exit;
  3771. end
  3772. { pointer comperation and subtraction }
  3773. else if (ld.typ=pointerdef) then
  3774. begin
  3775. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  3776. result:=first_addpointer
  3777. else
  3778. result:=first_cmppointer;
  3779. end
  3780. else if is_implicit_pointer_object_type(ld) then
  3781. begin
  3782. expectloc:=LOC_FLAGS;
  3783. end
  3784. else if (ld.typ=classrefdef) then
  3785. begin
  3786. expectloc:=LOC_FLAGS;
  3787. end
  3788. { support procvar=nil,procvar<>nil }
  3789. else if ((ld.typ=procvardef) and (rt=niln)) or
  3790. ((rd.typ=procvardef) and (lt=niln)) then
  3791. begin
  3792. expectloc:=LOC_FLAGS;
  3793. end
  3794. {$ifdef SUPPORT_MMX}
  3795. { mmx support, this must be before the zero based array
  3796. check }
  3797. else if (cs_mmx in current_settings.localswitches) and is_mmx_able_array(ld) and
  3798. is_mmx_able_array(rd) then
  3799. begin
  3800. expectloc:=LOC_MMXREGISTER;
  3801. end
  3802. {$endif SUPPORT_MMX}
  3803. else if (rd.typ=pointerdef) or (ld.typ=pointerdef) then
  3804. begin
  3805. result:=first_addpointer;
  3806. end
  3807. else if (rd.typ=procvardef) and
  3808. (ld.typ=procvardef) and
  3809. equal_defs(rd,ld) then
  3810. begin
  3811. expectloc:=LOC_FLAGS;
  3812. end
  3813. else if (ld.typ=enumdef) then
  3814. begin
  3815. expectloc:=LOC_FLAGS;
  3816. end
  3817. {$ifdef SUPPORT_MMX}
  3818. else if (cs_mmx in current_settings.localswitches) and
  3819. is_mmx_able_array(ld) and
  3820. is_mmx_able_array(rd) then
  3821. begin
  3822. expectloc:=LOC_MMXREGISTER;
  3823. end
  3824. {$endif SUPPORT_MMX}
  3825. else if is_dynamic_array(ld) or is_dynamic_array(rd) then
  3826. begin
  3827. result:=first_adddynarray;
  3828. exit;
  3829. end
  3830. { the general solution is to convert to 32 bit int }
  3831. else
  3832. begin
  3833. expectloc:=LOC_REGISTER;
  3834. end;
  3835. end;
  3836. {$ifdef state_tracking}
  3837. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  3838. var factval:Tnode;
  3839. begin
  3840. track_state_pass:=false;
  3841. if left.track_state_pass(exec_known) then
  3842. begin
  3843. track_state_pass:=true;
  3844. left.resultdef:=nil;
  3845. do_typecheckpass(left);
  3846. end;
  3847. factval:=aktstate.find_fact(left);
  3848. if factval<>nil then
  3849. begin
  3850. track_state_pass:=true;
  3851. left.destroy;
  3852. left:=factval.getcopy;
  3853. end;
  3854. if right.track_state_pass(exec_known) then
  3855. begin
  3856. track_state_pass:=true;
  3857. right.resultdef:=nil;
  3858. do_typecheckpass(right);
  3859. end;
  3860. factval:=aktstate.find_fact(right);
  3861. if factval<>nil then
  3862. begin
  3863. track_state_pass:=true;
  3864. right.destroy;
  3865. right:=factval.getcopy;
  3866. end;
  3867. end;
  3868. {$endif}
  3869. end.