nadd.pas 158 KB

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