nadd.pas 144 KB

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