nadd.pas 164 KB

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