nadd.pas 151 KB

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