nadd.pas 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717
  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. function pass_typecheck_internal:tnode;
  27. public
  28. resultrealdef : tdef;
  29. constructor create(tt : tnodetype;l,r : tnode);override;
  30. function pass_1 : tnode;override;
  31. function pass_typecheck:tnode;override;
  32. function simplify : tnode;override;
  33. {$ifdef state_tracking}
  34. function track_state_pass(exec_known:boolean):boolean;override;
  35. {$endif}
  36. protected
  37. { override the following if you want to implement }
  38. { parts explicitely in the code generator (JM) }
  39. function first_addstring: tnode; virtual;
  40. function first_addset: tnode; virtual;
  41. { only implements "muln" nodes, the rest always has to be done in }
  42. { the code generator for performance reasons (JM) }
  43. function first_add64bitint: tnode; virtual;
  44. { override and return false if you can handle 32x32->64 }
  45. { bit multiplies directly in your code generator. If }
  46. { this function is overridden to return false, you can }
  47. { get multiplies with left/right both s32bit or u32bit, }
  48. { and resultdef of the muln s64bit or u64bit }
  49. function use_generic_mul32to64: boolean; virtual;
  50. { This routine calls internal runtime library helpers
  51. for all floating point arithmetic in the case
  52. where the emulation switches is on. Otherwise
  53. returns nil, and everything must be done in
  54. the code generation phase.
  55. }
  56. function first_addfloat : tnode; virtual;
  57. private
  58. { checks whether a muln can be calculated as a 32bit }
  59. { * 32bit -> 64 bit }
  60. function try_make_mul32to64: boolean;
  61. end;
  62. taddnodeclass = class of taddnode;
  63. var
  64. { caddnode is used to create nodes of the add type }
  65. { the virtual constructor allows to assign }
  66. { another class type to caddnode => processor }
  67. { specific node types can be created }
  68. caddnode : taddnodeclass;
  69. implementation
  70. uses
  71. {$IFNDEF USE_FAKE_SYSUTILS}
  72. sysutils,
  73. {$ELSE}
  74. fksysutl,
  75. {$ENDIF}
  76. globtype,systems,constexp,
  77. cutils,verbose,globals,widestr,
  78. symconst,symdef,symsym,symtable,defutil,defcmp,
  79. cgbase,
  80. htypechk,pass_1,
  81. nld,nbas,nmat,ncnv,ncon,nset,nopt,ncal,ninl,nmem,nutils,
  82. {$ifdef state_tracking}
  83. nstate,
  84. {$endif}
  85. cpuinfo,procinfo;
  86. {*****************************************************************************
  87. TADDNODE
  88. *****************************************************************************}
  89. {$maxfpuregisters 0}
  90. function getbestreal(t1,t2 : tdef) : tdef;
  91. const
  92. floatweight : array[tfloattype] of byte =
  93. (2,3,4,0,1,5);
  94. begin
  95. if t1.typ=floatdef then
  96. begin
  97. result:=t1;
  98. if t2.typ=floatdef then
  99. begin
  100. { when a comp or currency is used, use always the
  101. best float type to calculate the result }
  102. if (tfloatdef(t2).floattype in [s64comp,s64currency]) or
  103. (tfloatdef(t2).floattype in [s64comp,s64currency]) then
  104. result:=pbestrealtype^
  105. else
  106. if floatweight[tfloatdef(t2).floattype]>floatweight[tfloatdef(t1).floattype] then
  107. result:=t2;
  108. end;
  109. end
  110. else if t2.typ=floatdef then
  111. result:=t2
  112. else internalerror(200508061);
  113. end;
  114. constructor taddnode.create(tt : tnodetype;l,r : tnode);
  115. begin
  116. inherited create(tt,l,r);
  117. end;
  118. function taddnode.simplify : tnode;
  119. var
  120. t, hp : tnode;
  121. lt,rt : tnodetype;
  122. realdef : tdef;
  123. rd,ld : tdef;
  124. rv,lv,v : tconstexprint;
  125. rvd,lvd : bestreal;
  126. ws1,ws2 : pcompilerwidestring;
  127. concatstrings : boolean;
  128. c1,c2 : array[0..1] of char;
  129. s1,s2 : pchar;
  130. l1,l2 : longint;
  131. resultset : Tconstset;
  132. b : boolean;
  133. begin
  134. result:=nil;
  135. { load easier access variables }
  136. rd:=right.resultdef;
  137. ld:=left.resultdef;
  138. rt:=right.nodetype;
  139. lt:=left.nodetype;
  140. if (nodetype = slashn) and
  141. (((rt = ordconstn) and
  142. (tordconstnode(right).value = 0)) or
  143. ((rt = realconstn) and
  144. (trealconstnode(right).value_real = 0.0))) then
  145. begin
  146. if floating_point_range_check_error then
  147. begin
  148. result:=crealconstnode.create(1,pbestrealtype^);
  149. Message(parser_e_division_by_zero);
  150. exit;
  151. end;
  152. end;
  153. { both are int constants }
  154. if (
  155. (
  156. is_constintnode(left) and
  157. is_constintnode(right)
  158. ) or
  159. (
  160. is_constboolnode(left) and
  161. is_constboolnode(right) and
  162. (nodetype in [slashn,ltn,lten,gtn,gten,equaln,unequaln,andn,xorn,orn])
  163. ) or
  164. (
  165. is_constenumnode(left) and
  166. is_constenumnode(right) and
  167. allowenumop(nodetype))
  168. ) or
  169. (
  170. (lt = pointerconstn) and
  171. is_constintnode(right) and
  172. (nodetype in [addn,subn])
  173. ) or
  174. (
  175. (lt in [pointerconstn,niln]) and
  176. (rt in [pointerconstn,niln]) and
  177. (nodetype in [ltn,lten,gtn,gten,equaln,unequaln,subn])
  178. ) then
  179. begin
  180. t:=nil;
  181. { load values }
  182. case lt of
  183. ordconstn:
  184. lv:=tordconstnode(left).value;
  185. pointerconstn:
  186. lv:=tpointerconstnode(left).value;
  187. niln:
  188. lv:=0;
  189. else
  190. internalerror(2002080202);
  191. end;
  192. case rt of
  193. ordconstn:
  194. rv:=tordconstnode(right).value;
  195. pointerconstn:
  196. rv:=tpointerconstnode(right).value;
  197. niln:
  198. rv:=0;
  199. else
  200. internalerror(2002080203);
  201. end;
  202. { type checking already took care of multiplying }
  203. { integer constants with pointeddef.size if necessary }
  204. case nodetype of
  205. addn :
  206. begin
  207. v:=lv+rv;
  208. if v.overflow then
  209. begin
  210. Message(parser_e_arithmetic_operation_overflow);
  211. { Recover }
  212. t:=genintconstnode(0)
  213. end
  214. else if (lt=pointerconstn) then
  215. t := cpointerconstnode.create(qword(v),resultdef)
  216. else
  217. if is_integer(ld) then
  218. t := genintconstnode(v)
  219. else
  220. t := cordconstnode.create(v,resultdef,(ld.typ<>enumdef));
  221. end;
  222. subn :
  223. begin
  224. v:=lv-rv;
  225. if v.overflow then
  226. begin
  227. Message(parser_e_arithmetic_operation_overflow);
  228. { Recover }
  229. t:=genintconstnode(0)
  230. end
  231. else if (lt=pointerconstn) then
  232. { pointer-pointer results in an integer }
  233. if (rt=pointerconstn) then
  234. begin
  235. if not(nf_has_pointerdiv in flags) then
  236. internalerror(2008030101);
  237. t := genintconstnode(v)
  238. end
  239. else
  240. t := cpointerconstnode.create(qword(v),resultdef)
  241. else
  242. if is_integer(ld) then
  243. t:=genintconstnode(v)
  244. else
  245. t:=cordconstnode.create(v,resultdef,(ld.typ<>enumdef));
  246. end;
  247. muln :
  248. begin
  249. v:=lv*rv;
  250. if v.overflow then
  251. begin
  252. message(parser_e_arithmetic_operation_overflow);
  253. { Recover }
  254. t:=genintconstnode(0)
  255. end
  256. else
  257. t:=genintconstnode(v)
  258. end;
  259. xorn :
  260. if is_integer(ld) then
  261. t:=genintconstnode(lv xor rv)
  262. else
  263. t:=cordconstnode.create(lv xor rv,resultdef,true);
  264. orn :
  265. if is_integer(ld) then
  266. t:=genintconstnode(lv or rv)
  267. else
  268. t:=cordconstnode.create(lv or rv,resultdef,true);
  269. andn :
  270. if is_integer(ld) then
  271. t:=genintconstnode(lv and rv)
  272. else
  273. t:=cordconstnode.create(lv and rv,resultdef,true);
  274. ltn :
  275. t:=cordconstnode.create(ord(lv<rv),booltype,true);
  276. lten :
  277. t:=cordconstnode.create(ord(lv<=rv),booltype,true);
  278. gtn :
  279. t:=cordconstnode.create(ord(lv>rv),booltype,true);
  280. gten :
  281. t:=cordconstnode.create(ord(lv>=rv),booltype,true);
  282. equaln :
  283. t:=cordconstnode.create(ord(lv=rv),booltype,true);
  284. unequaln :
  285. t:=cordconstnode.create(ord(lv<>rv),booltype,true);
  286. slashn :
  287. begin
  288. { int/int becomes a real }
  289. rvd:=rv;
  290. lvd:=lv;
  291. t:=crealconstnode.create(lvd/rvd,resultrealdef);
  292. end;
  293. else
  294. internalerror(2008022101);
  295. end;
  296. result:=t;
  297. exit;
  298. end
  299. {Match against the ranges, i.e.:
  300. var a:1..10;
  301. begin
  302. if a>0 then
  303. ... always evaluates to true. (DM)}
  304. else if is_constintnode(left) and (right.resultdef.typ=orddef) and
  305. { don't ignore type checks }
  306. is_subequal(left.resultdef,right.resultdef) then
  307. begin
  308. t:=nil;
  309. hp:=right;
  310. realdef:=hp.resultdef;
  311. while (hp.nodetype=typeconvn) and
  312. ([nf_internal,nf_explicit] * hp.flags = []) and
  313. is_in_limit(ttypeconvnode(hp).left.resultdef,realdef) do
  314. begin
  315. hp:=ttypeconvnode(hp).left;
  316. realdef:=hp.resultdef;
  317. end;
  318. lv:=Tordconstnode(left).value;
  319. with torddef(realdef) do
  320. case nodetype of
  321. ltn:
  322. if lv<low then
  323. t:=Cordconstnode.create(1,booltype,true)
  324. else if lv>=high then
  325. t:=Cordconstnode.create(0,booltype,true);
  326. lten:
  327. if lv<=low then
  328. t:=Cordconstnode.create(1,booltype,true)
  329. else if lv>high then
  330. t:=Cordconstnode.create(0,booltype,true);
  331. gtn:
  332. if lv<=low then
  333. t:=Cordconstnode.create(0,booltype,true)
  334. else if lv>high then
  335. t:=Cordconstnode.create(1,booltype,true);
  336. gten :
  337. if lv<low then
  338. t:=Cordconstnode.create(0,booltype,true)
  339. else if lv>=high then
  340. t:=Cordconstnode.create(1,booltype,true);
  341. equaln:
  342. if (lv<low) or (lv>high) then
  343. t:=Cordconstnode.create(0,booltype,true);
  344. unequaln:
  345. if (lv<low) or (lv>high) then
  346. t:=Cordconstnode.create(1,booltype,true);
  347. end;
  348. if t<>nil then
  349. begin
  350. result:=t;
  351. exit;
  352. end
  353. end
  354. else if (left.resultdef.typ=orddef) and is_constintnode(right) and
  355. { don't ignore type checks }
  356. is_subequal(left.resultdef,right.resultdef) then
  357. begin
  358. t:=nil;
  359. hp:=left;
  360. realdef:=hp.resultdef;
  361. while (hp.nodetype=typeconvn) and
  362. ([nf_internal,nf_explicit] * hp.flags = []) and
  363. is_in_limit(ttypeconvnode(hp).left.resultdef,realdef) do
  364. begin
  365. hp:=ttypeconvnode(hp).left;
  366. realdef:=hp.resultdef;
  367. end;
  368. rv:=Tordconstnode(right).value;
  369. with torddef(realdef) do
  370. case nodetype of
  371. ltn:
  372. if high<rv then
  373. t:=Cordconstnode.create(1,booltype,true)
  374. else if low>=rv then
  375. t:=Cordconstnode.create(0,booltype,true);
  376. lten:
  377. if high<=rv then
  378. t:=Cordconstnode.create(1,booltype,true)
  379. else if low>rv then
  380. t:=Cordconstnode.create(0,booltype,true);
  381. gtn:
  382. if high<=rv then
  383. t:=Cordconstnode.create(0,booltype,true)
  384. else if low>rv then
  385. t:=Cordconstnode.create(1,booltype,true);
  386. gten:
  387. if high<rv then
  388. t:=Cordconstnode.create(0,booltype,true)
  389. else if low>=rv then
  390. t:=Cordconstnode.create(1,booltype,true);
  391. equaln:
  392. if (rv<low) or (rv>high) then
  393. t:=Cordconstnode.create(0,booltype,true);
  394. unequaln:
  395. if (rv<low) or (rv>high) then
  396. t:=Cordconstnode.create(1,booltype,true);
  397. end;
  398. if t<>nil then
  399. begin
  400. result:=t;
  401. exit;
  402. end
  403. end;
  404. { Add,Sub,Mul with constant 0, 1 or -1? }
  405. if is_constintnode(right) and is_integer(left.resultdef) then
  406. begin
  407. if tordconstnode(right).value = 0 then
  408. begin
  409. case nodetype of
  410. addn,subn:
  411. result := left.getcopy;
  412. muln:
  413. result:=cordconstnode.create(0,resultdef,true);
  414. end;
  415. end
  416. else if tordconstnode(right).value = 1 then
  417. begin
  418. case nodetype of
  419. muln:
  420. result := left.getcopy;
  421. end;
  422. end
  423. {$ifdef VER2_2}
  424. else if (tordconstnode(right).value.svalue = -1) and (tordconstnode(right).value.signed) then
  425. {$else}
  426. else if tordconstnode(right).value = -1 then
  427. {$endif}
  428. begin
  429. case nodetype of
  430. muln:
  431. result := cunaryminusnode.create(left.getcopy);
  432. end;
  433. end;
  434. if assigned(result) then
  435. exit;
  436. end;
  437. if is_constintnode(left) and is_integer(right.resultdef) then
  438. begin
  439. if tordconstnode(left).value = 0 then
  440. begin
  441. case nodetype of
  442. addn:
  443. result := right.getcopy;
  444. subn:
  445. result := cunaryminusnode.create(right.getcopy);
  446. muln:
  447. result:=cordconstnode.create(0,right.resultdef,true);
  448. end;
  449. end
  450. else if tordconstnode(left).value = 1 then
  451. begin
  452. case nodetype of
  453. muln:
  454. result := right.getcopy;
  455. end;
  456. end
  457. {$ifdef VER2_2}
  458. else if (tordconstnode(left).value.svalue = -1) and (tordconstnode(left).value.signed) then
  459. {$else}
  460. else if tordconstnode(left).value = -1 then
  461. {$endif}
  462. begin
  463. case nodetype of
  464. muln:
  465. result := cunaryminusnode.create(right.getcopy);
  466. end;
  467. end;
  468. if assigned(result) then
  469. exit;
  470. end;
  471. { both real constants ? }
  472. if (lt=realconstn) and (rt=realconstn) then
  473. begin
  474. lvd:=trealconstnode(left).value_real;
  475. rvd:=trealconstnode(right).value_real;
  476. case nodetype of
  477. addn :
  478. t:=crealconstnode.create(lvd+rvd,resultrealdef);
  479. subn :
  480. t:=crealconstnode.create(lvd-rvd,resultrealdef);
  481. muln :
  482. t:=crealconstnode.create(lvd*rvd,resultrealdef);
  483. starstarn:
  484. begin
  485. if lvd<0 then
  486. begin
  487. Message(parser_e_invalid_float_operation);
  488. t:=crealconstnode.create(0,resultrealdef);
  489. end
  490. else if lvd=0 then
  491. t:=crealconstnode.create(1.0,resultrealdef)
  492. else
  493. t:=crealconstnode.create(exp(ln(lvd)*rvd),resultrealdef);
  494. end;
  495. slashn :
  496. t:=crealconstnode.create(lvd/rvd,resultrealdef);
  497. ltn :
  498. t:=cordconstnode.create(ord(lvd<rvd),booltype,true);
  499. lten :
  500. t:=cordconstnode.create(ord(lvd<=rvd),booltype,true);
  501. gtn :
  502. t:=cordconstnode.create(ord(lvd>rvd),booltype,true);
  503. gten :
  504. t:=cordconstnode.create(ord(lvd>=rvd),booltype,true);
  505. equaln :
  506. t:=cordconstnode.create(ord(lvd=rvd),booltype,true);
  507. unequaln :
  508. t:=cordconstnode.create(ord(lvd<>rvd),booltype,true);
  509. else
  510. internalerror(2008022102);
  511. end;
  512. result:=t;
  513. exit;
  514. end;
  515. { first, we handle widestrings, so we can check later for }
  516. { stringconstn only }
  517. { widechars are converted above to widestrings too }
  518. { this isn't ver y efficient, but I don't think }
  519. { that it does matter that much (FK) }
  520. if (lt=stringconstn) and (rt=stringconstn) and
  521. (tstringconstnode(left).cst_type in [cst_widestring,cst_unicodestring]) and
  522. (tstringconstnode(right).cst_type in [cst_widestring,cst_unicodestring]) then
  523. begin
  524. initwidestring(ws1);
  525. initwidestring(ws2);
  526. copywidestring(pcompilerwidestring(tstringconstnode(left).value_str),ws1);
  527. copywidestring(pcompilerwidestring(tstringconstnode(right).value_str),ws2);
  528. case nodetype of
  529. addn :
  530. begin
  531. concatwidestrings(ws1,ws2);
  532. t:=cstringconstnode.createwstr(ws1);
  533. end;
  534. ltn :
  535. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<0),booltype,true);
  536. lten :
  537. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<=0),booltype,true);
  538. gtn :
  539. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)>0),booltype,true);
  540. gten :
  541. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)>=0),booltype,true);
  542. equaln :
  543. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)=0),booltype,true);
  544. unequaln :
  545. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<>0),booltype,true);
  546. else
  547. internalerror(2008022103);
  548. end;
  549. donewidestring(ws1);
  550. donewidestring(ws2);
  551. result:=t;
  552. exit;
  553. end;
  554. { concating strings ? }
  555. concatstrings:=false;
  556. if (lt=ordconstn) and (rt=ordconstn) and
  557. is_char(ld) and is_char(rd) then
  558. begin
  559. c1[0]:=char(int64(tordconstnode(left).value));
  560. c1[1]:=#0;
  561. l1:=1;
  562. c2[0]:=char(int64(tordconstnode(right).value));
  563. c2[1]:=#0;
  564. l2:=1;
  565. s1:=@c1[0];
  566. s2:=@c2[0];
  567. concatstrings:=true;
  568. end
  569. else if (lt=stringconstn) and (rt=ordconstn) and is_char(rd) then
  570. begin
  571. s1:=tstringconstnode(left).value_str;
  572. l1:=tstringconstnode(left).len;
  573. c2[0]:=char(int64(tordconstnode(right).value));
  574. c2[1]:=#0;
  575. s2:=@c2[0];
  576. l2:=1;
  577. concatstrings:=true;
  578. end
  579. else if (lt=ordconstn) and (rt=stringconstn) and is_char(ld) then
  580. begin
  581. c1[0]:=char(int64(tordconstnode(left).value));
  582. c1[1]:=#0;
  583. l1:=1;
  584. s1:=@c1[0];
  585. s2:=tstringconstnode(right).value_str;
  586. l2:=tstringconstnode(right).len;
  587. concatstrings:=true;
  588. end
  589. else if (lt=stringconstn) and (rt=stringconstn) then
  590. begin
  591. s1:=tstringconstnode(left).value_str;
  592. l1:=tstringconstnode(left).len;
  593. s2:=tstringconstnode(right).value_str;
  594. l2:=tstringconstnode(right).len;
  595. concatstrings:=true;
  596. end;
  597. if concatstrings then
  598. begin
  599. case nodetype of
  600. addn :
  601. t:=cstringconstnode.createpchar(concatansistrings(s1,s2,l1,l2),l1+l2);
  602. ltn :
  603. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<0),booltype,true);
  604. lten :
  605. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<=0),booltype,true);
  606. gtn :
  607. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)>0),booltype,true);
  608. gten :
  609. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)>=0),booltype,true);
  610. equaln :
  611. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)=0),booltype,true);
  612. unequaln :
  613. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<>0),booltype,true);
  614. else
  615. internalerror(2008022104);
  616. end;
  617. result:=t;
  618. exit;
  619. end;
  620. { set constant evaluation }
  621. if (right.nodetype=setconstn) and
  622. not assigned(tsetconstnode(right).left) and
  623. (left.nodetype=setconstn) and
  624. not assigned(tsetconstnode(left).left) then
  625. begin
  626. case nodetype of
  627. addn :
  628. begin
  629. resultset:=tsetconstnode(right).value_set^ + tsetconstnode(left).value_set^;
  630. t:=csetconstnode.create(@resultset,resultdef);
  631. end;
  632. muln :
  633. begin
  634. resultset:=tsetconstnode(right).value_set^ * tsetconstnode(left).value_set^;
  635. t:=csetconstnode.create(@resultset,resultdef);
  636. end;
  637. subn :
  638. begin
  639. resultset:=tsetconstnode(left).value_set^ - tsetconstnode(right).value_set^;
  640. t:=csetconstnode.create(@resultset,resultdef);
  641. end;
  642. symdifn :
  643. begin
  644. resultset:=tsetconstnode(right).value_set^ >< tsetconstnode(left).value_set^;
  645. t:=csetconstnode.create(@resultset,resultdef);
  646. end;
  647. unequaln :
  648. begin
  649. b:=tsetconstnode(right).value_set^ <> tsetconstnode(left).value_set^;
  650. t:=cordconstnode.create(byte(b),booltype,true);
  651. end;
  652. equaln :
  653. begin
  654. b:=tsetconstnode(right).value_set^ = tsetconstnode(left).value_set^;
  655. t:=cordconstnode.create(byte(b),booltype,true);
  656. end;
  657. lten :
  658. begin
  659. b:=tsetconstnode(left).value_set^ <= tsetconstnode(right).value_set^;
  660. t:=cordconstnode.create(byte(b),booltype,true);
  661. end;
  662. gten :
  663. begin
  664. b:=tsetconstnode(left).value_set^ >= tsetconstnode(right).value_set^;
  665. t:=cordconstnode.create(byte(b),booltype,true);
  666. end;
  667. else
  668. internalerror(2008022105);
  669. end;
  670. result:=t;
  671. exit;
  672. end;
  673. end;
  674. function taddnode.pass_typecheck:tnode;
  675. begin
  676. { This function is small to keep the stack small for recursive of
  677. large + operations }
  678. typecheckpass(left);
  679. typecheckpass(right);
  680. result:=pass_typecheck_internal;
  681. end;
  682. function taddnode.pass_typecheck_internal:tnode;
  683. var
  684. hp : tnode;
  685. rd,ld,nd : tdef;
  686. hsym : tfieldvarsym;
  687. llow,lhigh,
  688. rlow,rhigh : tconstexprint;
  689. strtype : tstringtype;
  690. b : boolean;
  691. lt,rt : tnodetype;
  692. ot : tnodetype;
  693. {$ifdef state_tracking}
  694. factval : Tnode;
  695. change : boolean;
  696. {$endif}
  697. begin
  698. result:=nil;
  699. { both left and right need to be valid }
  700. set_varstate(left,vs_read,[vsf_must_be_valid]);
  701. set_varstate(right,vs_read,[vsf_must_be_valid]);
  702. if codegenerror then
  703. exit;
  704. { tp procvar support }
  705. maybe_call_procvar(left,true);
  706. maybe_call_procvar(right,true);
  707. { convert array constructors to sets, because there is no other operator
  708. possible for array constructors }
  709. if is_array_constructor(left.resultdef) then
  710. begin
  711. arrayconstructor_to_set(left);
  712. typecheckpass(left);
  713. end;
  714. if is_array_constructor(right.resultdef) then
  715. begin
  716. arrayconstructor_to_set(right);
  717. typecheckpass(right);
  718. end;
  719. { allow operator overloading }
  720. hp:=self;
  721. if isbinaryoverloaded(hp) then
  722. begin
  723. result:=hp;
  724. exit;
  725. end;
  726. { Stop checking when an error was found in the operator checking }
  727. if codegenerror then
  728. begin
  729. result:=cerrornode.create;
  730. exit;
  731. end;
  732. { Kylix allows enum+ordconstn in an enum type declaration, we need to do
  733. the conversion here before the constant folding }
  734. if (m_delphi in current_settings.modeswitches) and
  735. (blocktype in [bt_type,bt_const_type,bt_var_type]) then
  736. begin
  737. if (left.resultdef.typ=enumdef) and
  738. (right.resultdef.typ=orddef) then
  739. begin
  740. { insert explicit typecast to default signed int }
  741. left:=ctypeconvnode.create_internal(left,sinttype);
  742. typecheckpass(left);
  743. end
  744. else
  745. if (left.resultdef.typ=orddef) and
  746. (right.resultdef.typ=enumdef) then
  747. begin
  748. { insert explicit typecast to default signed int }
  749. right:=ctypeconvnode.create_internal(right,sinttype);
  750. typecheckpass(right);
  751. end;
  752. end;
  753. { is one a real float, then both need to be floats, this
  754. need to be done before the constant folding so constant
  755. operation on a float and int are also handled }
  756. resultrealdef:=pbestrealtype^;
  757. if (right.resultdef.typ=floatdef) or (left.resultdef.typ=floatdef) then
  758. begin
  759. { when both floattypes are already equal then use that
  760. floattype for results }
  761. if (right.resultdef.typ=floatdef) and
  762. (left.resultdef.typ=floatdef) and
  763. (tfloatdef(left.resultdef).floattype=tfloatdef(right.resultdef).floattype) then
  764. resultrealdef:=left.resultdef
  765. { when there is a currency type then use currency, but
  766. only when currency is defined as float }
  767. else
  768. if (is_currency(right.resultdef) or
  769. is_currency(left.resultdef)) and
  770. ((s64currencytype.typ = floatdef) or
  771. (nodetype <> slashn)) then
  772. begin
  773. resultrealdef:=s64currencytype;
  774. inserttypeconv(right,resultrealdef);
  775. inserttypeconv(left,resultrealdef);
  776. end
  777. else
  778. begin
  779. resultrealdef:=getbestreal(left.resultdef,right.resultdef);
  780. inserttypeconv(right,resultrealdef);
  781. inserttypeconv(left,resultrealdef);
  782. end;
  783. end;
  784. { If both operands are constant and there is a unicodestring
  785. or unicodestring then convert everything to unicodestring }
  786. if is_constnode(right) and is_constnode(left) and
  787. (is_unicodestring(right.resultdef) or
  788. is_unicodestring(left.resultdef)) then
  789. begin
  790. inserttypeconv(right,cunicodestringtype);
  791. inserttypeconv(left,cunicodestringtype);
  792. end;
  793. { If both operands are constant and there is a widechar
  794. or widestring then convert everything to widestring. This
  795. allows constant folding like char+widechar }
  796. if is_constnode(right) and is_constnode(left) and
  797. (is_widestring(right.resultdef) or
  798. is_widestring(left.resultdef) or
  799. is_widechar(right.resultdef) or
  800. is_widechar(left.resultdef)) then
  801. begin
  802. inserttypeconv(right,cwidestringtype);
  803. inserttypeconv(left,cwidestringtype);
  804. end;
  805. { load easier access variables }
  806. rd:=right.resultdef;
  807. ld:=left.resultdef;
  808. rt:=right.nodetype;
  809. lt:=left.nodetype;
  810. { 4 character constant strings are compatible with orddef }
  811. { in macpas mode (become cardinals) }
  812. if (m_mac in current_settings.modeswitches) and
  813. { only allow for comparisons, additions etc are }
  814. { normally program errors }
  815. (nodetype in [ltn,lten,gtn,gten,unequaln,equaln]) and
  816. (((lt=stringconstn) and
  817. (tstringconstnode(left).len=4) and
  818. (rd.typ=orddef)) or
  819. ((rt=stringconstn) and
  820. (tstringconstnode(right).len=4) and
  821. (ld.typ=orddef))) then
  822. begin
  823. if (rt=stringconstn) then
  824. begin
  825. inserttypeconv(right,u32inttype);
  826. rt:=right.nodetype;
  827. rd:=right.resultdef;
  828. end
  829. else
  830. begin
  831. inserttypeconv(left,u32inttype);
  832. lt:=left.nodetype;
  833. ld:=left.resultdef;
  834. end;
  835. end;
  836. { but an int/int gives real/real! }
  837. if (nodetype=slashn) and not(is_vector(left.resultdef)) and not(is_vector(right.resultdef)) then
  838. begin
  839. if is_currency(left.resultdef) and
  840. is_currency(right.resultdef) then
  841. { In case of currency, converting to float means dividing by 10000 }
  842. { However, since this is already a division, both divisions by }
  843. { 10000 are eliminated when we divide the results -> we can skip }
  844. { them. }
  845. if s64currencytype.typ = floatdef then
  846. begin
  847. { there's no s64comptype or so, how do we avoid the type conversion?
  848. left.resultdef := s64comptype;
  849. right.resultdef := s64comptype; }
  850. end
  851. else
  852. begin
  853. left.resultdef := s64inttype;
  854. right.resultdef := s64inttype;
  855. end;
  856. inserttypeconv(right,resultrealdef);
  857. inserttypeconv(left,resultrealdef);
  858. end
  859. { if both are orddefs then check sub types }
  860. else if (ld.typ=orddef) and (rd.typ=orddef) then
  861. begin
  862. { set for & and | operations in macpas mode: they only work on }
  863. { booleans, and always short circuit evaluation }
  864. if (nf_short_bool in flags) then
  865. begin
  866. if not is_boolean(ld) then
  867. begin
  868. inserttypeconv(left,booltype);
  869. ld := left.resultdef;
  870. end;
  871. if not is_boolean(rd) then
  872. begin
  873. inserttypeconv(right,booltype);
  874. rd := right.resultdef;
  875. end;
  876. end;
  877. { 2 booleans? Make them equal to the largest boolean }
  878. if (is_boolean(ld) and is_boolean(rd)) or
  879. (nf_short_bool in flags) then
  880. begin
  881. if (torddef(left.resultdef).size>torddef(right.resultdef).size) or
  882. (is_cbool(left.resultdef) and not is_cbool(right.resultdef)) then
  883. begin
  884. right:=ctypeconvnode.create_internal(right,left.resultdef);
  885. ttypeconvnode(right).convtype:=tc_bool_2_bool;
  886. typecheckpass(right);
  887. end
  888. else if (torddef(left.resultdef).size<torddef(right.resultdef).size) or
  889. (not is_cbool(left.resultdef) and is_cbool(right.resultdef)) then
  890. begin
  891. left:=ctypeconvnode.create_internal(left,right.resultdef);
  892. ttypeconvnode(left).convtype:=tc_bool_2_bool;
  893. typecheckpass(left);
  894. end;
  895. case nodetype of
  896. xorn,
  897. ltn,
  898. lten,
  899. gtn,
  900. gten,
  901. andn,
  902. orn:
  903. begin
  904. end;
  905. unequaln,
  906. equaln:
  907. begin
  908. if not(cs_full_boolean_eval in current_settings.localswitches) or
  909. (nf_short_bool in flags) then
  910. begin
  911. { Remove any compares with constants }
  912. if (left.nodetype=ordconstn) then
  913. begin
  914. hp:=right;
  915. b:=(tordconstnode(left).value<>0);
  916. ot:=nodetype;
  917. left.free;
  918. left:=nil;
  919. right:=nil;
  920. if (not(b) and (ot=equaln)) or
  921. (b and (ot=unequaln)) then
  922. begin
  923. hp:=cnotnode.create(hp);
  924. end;
  925. result:=hp;
  926. exit;
  927. end;
  928. if (right.nodetype=ordconstn) then
  929. begin
  930. hp:=left;
  931. b:=(tordconstnode(right).value<>0);
  932. ot:=nodetype;
  933. right.free;
  934. right:=nil;
  935. left:=nil;
  936. if (not(b) and (ot=equaln)) or
  937. (b and (ot=unequaln)) then
  938. begin
  939. hp:=cnotnode.create(hp);
  940. end;
  941. result:=hp;
  942. exit;
  943. end;
  944. end;
  945. end;
  946. else
  947. begin
  948. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  949. result:=cnothingnode.create;
  950. exit;
  951. end;
  952. end;
  953. end
  954. { Both are chars? }
  955. else if is_char(rd) and is_char(ld) then
  956. begin
  957. if nodetype=addn then
  958. begin
  959. resultdef:=cshortstringtype;
  960. if not(is_constcharnode(left) and is_constcharnode(right)) then
  961. begin
  962. inserttypeconv(left,cshortstringtype);
  963. {$ifdef addstringopt}
  964. hp := genaddsstringcharoptnode(self);
  965. result := hp;
  966. exit;
  967. {$endif addstringopt}
  968. end
  969. end
  970. else if not(nodetype in [ltn,lten,gtn,gten,unequaln,equaln]) then
  971. begin
  972. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  973. result:=cnothingnode.create;
  974. exit;
  975. end;
  976. end
  977. { There is a widechar? }
  978. else if is_widechar(rd) or is_widechar(ld) then
  979. begin
  980. { widechar+widechar gives widestring }
  981. if nodetype=addn then
  982. begin
  983. inserttypeconv(left,cwidestringtype);
  984. if (torddef(rd).ordtype<>uwidechar) then
  985. inserttypeconv(right,cwidechartype);
  986. resultdef:=cwidestringtype;
  987. end
  988. else
  989. begin
  990. if (torddef(ld).ordtype<>uwidechar) then
  991. inserttypeconv(left,cwidechartype);
  992. if (torddef(rd).ordtype<>uwidechar) then
  993. inserttypeconv(right,cwidechartype);
  994. end;
  995. end
  996. { is there a currency type ? }
  997. else if ((torddef(rd).ordtype=scurrency) or (torddef(ld).ordtype=scurrency)) then
  998. begin
  999. if (torddef(ld).ordtype<>scurrency) then
  1000. inserttypeconv(left,s64currencytype);
  1001. if (torddef(rd).ordtype<>scurrency) then
  1002. inserttypeconv(right,s64currencytype);
  1003. end
  1004. { "and" does't care about the sign of integers }
  1005. { "xor", "or" and compares don't need extension to native int }
  1006. { size either as long as both values are signed or unsigned }
  1007. { "xor" and "or" also don't care about the sign if the values }
  1008. { occupy an entire register }
  1009. else if is_integer(ld) and is_integer(rd) and
  1010. ((nodetype=andn) or
  1011. ((nodetype in [orn,xorn,equaln,unequaln,gtn,gten,ltn,lten]) and
  1012. not(is_signed(ld) xor is_signed(rd)))) then
  1013. begin
  1014. if (rd.size>ld.size) or
  1015. { Delphi-compatible: prefer unsigned type for "and" with equal size }
  1016. ((rd.size=ld.size) and
  1017. not is_signed(rd)) then
  1018. begin
  1019. if (rd.size=ld.size) and
  1020. is_signed(ld) then
  1021. inserttypeconv_internal(left,right.resultdef)
  1022. else
  1023. inserttypeconv(left,right.resultdef)
  1024. end
  1025. else
  1026. begin
  1027. if (rd.size=ld.size) and
  1028. is_signed(rd) then
  1029. inserttypeconv_internal(right,left.resultdef)
  1030. else
  1031. inserttypeconv(right,left.resultdef)
  1032. end
  1033. end
  1034. { is there a signed 64 bit type ? }
  1035. else if ((torddef(rd).ordtype=s64bit) or (torddef(ld).ordtype=s64bit)) then
  1036. begin
  1037. if (torddef(ld).ordtype<>s64bit) then
  1038. inserttypeconv(left,s64inttype);
  1039. if (torddef(rd).ordtype<>s64bit) then
  1040. inserttypeconv(right,s64inttype);
  1041. end
  1042. { is there a unsigned 64 bit type ? }
  1043. else if ((torddef(rd).ordtype=u64bit) or (torddef(ld).ordtype=u64bit)) then
  1044. begin
  1045. if (torddef(ld).ordtype<>u64bit) then
  1046. inserttypeconv(left,u64inttype);
  1047. if (torddef(rd).ordtype<>u64bit) then
  1048. inserttypeconv(right,u64inttype);
  1049. end
  1050. { 64 bit cpus do calculations always in 64 bit }
  1051. {$ifndef cpu64bitaddr}
  1052. { is there a cardinal? }
  1053. else if ((torddef(rd).ordtype=u32bit) or (torddef(ld).ordtype=u32bit)) then
  1054. begin
  1055. { convert positive constants to u32bit }
  1056. if (torddef(ld).ordtype<>u32bit) and
  1057. is_constintnode(left) and
  1058. (tordconstnode(left).value >= 0) then
  1059. inserttypeconv(left,u32inttype);
  1060. if (torddef(rd).ordtype<>u32bit) and
  1061. is_constintnode(right) and
  1062. (tordconstnode(right).value >= 0) then
  1063. inserttypeconv(right,u32inttype);
  1064. { when one of the operand is signed or the operation is subn then perform
  1065. the operation in 64bit, can't use rd/ld here because there
  1066. could be already typeconvs inserted.
  1067. This is compatible with the code below for other unsigned types (PFV) }
  1068. if is_signed(left.resultdef) or
  1069. is_signed(right.resultdef) or
  1070. (nodetype=subn) then
  1071. begin
  1072. if nodetype<>subn then
  1073. CGMessage(type_h_mixed_signed_unsigned);
  1074. { mark as internal in case added for a subn, so }
  1075. { ttypeconvnode.simplify can remove the 64 bit }
  1076. { typecast again if semantically correct. Even }
  1077. { if we could detect that here already, we }
  1078. { mustn't do it here because that would change }
  1079. { overload choosing behaviour etc. The code in }
  1080. { ncnv.pas is run after that is already decided }
  1081. if (not is_signed(left.resultdef) and
  1082. not is_signed(right.resultdef)) or
  1083. (nodetype in [orn,xorn]) then
  1084. include(flags,nf_internal);
  1085. inserttypeconv(left,s64inttype);
  1086. inserttypeconv(right,s64inttype);
  1087. end
  1088. else
  1089. begin
  1090. if (torddef(left.resultdef).ordtype<>u32bit) then
  1091. inserttypeconv(left,u32inttype);
  1092. if (torddef(right.resultdef).ordtype<>u32bit) then
  1093. inserttypeconv(right,u32inttype);
  1094. end;
  1095. end
  1096. {$endif cpu64bitaddr}
  1097. { generic ord conversion is sinttype }
  1098. else
  1099. begin
  1100. { if the left or right value is smaller than the normal
  1101. type sinttype and is unsigned, and the other value
  1102. is a constant < 0, the result will always be false/true
  1103. for equal / unequal nodes.
  1104. }
  1105. if (
  1106. { left : unsigned ordinal var, right : < 0 constant }
  1107. (
  1108. ((is_signed(ld)=false) and (is_constintnode(left) =false)) and
  1109. ((is_constintnode(right)) and (tordconstnode(right).value < 0))
  1110. ) or
  1111. { right : unsigned ordinal var, left : < 0 constant }
  1112. (
  1113. ((is_signed(rd)=false) and (is_constintnode(right) =false)) and
  1114. ((is_constintnode(left)) and (tordconstnode(left).value < 0))
  1115. )
  1116. ) then
  1117. begin
  1118. if nodetype = equaln then
  1119. CGMessage(type_w_signed_unsigned_always_false)
  1120. else
  1121. if nodetype = unequaln then
  1122. CGMessage(type_w_signed_unsigned_always_true)
  1123. else
  1124. if (is_constintnode(left) and (nodetype in [ltn,lten])) or
  1125. (is_constintnode(right) and (nodetype in [gtn,gten])) then
  1126. CGMessage(type_w_signed_unsigned_always_true)
  1127. else
  1128. if (is_constintnode(right) and (nodetype in [ltn,lten])) or
  1129. (is_constintnode(left) and (nodetype in [gtn,gten])) then
  1130. CGMessage(type_w_signed_unsigned_always_false);
  1131. end;
  1132. { When there is a signed type or there is a minus operation
  1133. we convert to signed int. Otherwise (both are unsigned) we keep
  1134. the result also unsigned. This is compatible with Delphi (PFV) }
  1135. if is_signed(ld) or
  1136. is_signed(rd) or
  1137. (nodetype=subn) then
  1138. begin
  1139. {$ifdef cpunodefaultint}
  1140. { for small cpus we use the smallest common type }
  1141. llow:=torddef(rd).low;
  1142. if llow<torddef(ld).low then
  1143. llow:=torddef(ld).low;
  1144. lhigh:=torddef(rd).high;
  1145. if lhigh<torddef(ld).high then
  1146. lhigh:=torddef(ld).high;
  1147. case range_to_basetype(llow,lhigh) of
  1148. s8bit:
  1149. nd:=s8inttype;
  1150. u8bit:
  1151. nd:=u8inttype;
  1152. s16bit:
  1153. nd:=s16inttype;
  1154. u16bit:
  1155. nd:=u16inttype;
  1156. s32bit:
  1157. nd:=s32inttype;
  1158. u32bit:
  1159. nd:=u32inttype;
  1160. s64bit:
  1161. nd:=s64inttype;
  1162. u64bit:
  1163. nd:=u64inttype;
  1164. else
  1165. internalerror(200802291);
  1166. end;
  1167. inserttypeconv(right,nd);
  1168. inserttypeconv(left,nd);
  1169. {$else cpunodefaultint}
  1170. inserttypeconv(right,sinttype);
  1171. inserttypeconv(left,sinttype);
  1172. {$endif cpunodefaultint}
  1173. end
  1174. else
  1175. begin
  1176. inserttypeconv(right,uinttype);
  1177. inserttypeconv(left,uinttype);
  1178. end;
  1179. end;
  1180. end
  1181. { if both are floatdefs, conversion is already done before constant folding }
  1182. else if (ld.typ=floatdef) then
  1183. begin
  1184. if not(nodetype in [addn,subn,muln,slashn,equaln,unequaln,ltn,lten,gtn,gten]) then
  1185. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1186. end
  1187. { left side a setdef, must be before string processing,
  1188. else array constructor can be seen as array of char (PFV) }
  1189. else if (ld.typ=setdef) then
  1190. begin
  1191. if not(nodetype in [addn,subn,symdifn,muln,equaln,unequaln,lten,gten]) then
  1192. CGMessage(type_e_set_operation_unknown);
  1193. { right must either be a set or a set element }
  1194. if (rd.typ<>setdef) and
  1195. (rt<>setelementn) then
  1196. CGMessage(type_e_mismatch)
  1197. { Make operands the same setdef. If one's elementtype fits }
  1198. { entirely inside the other's, pick the one with the largest }
  1199. { range. Otherwise create a new setdef with a range which }
  1200. { can contain both. }
  1201. else if not(equal_defs(ld,rd)) then
  1202. begin
  1203. { note: ld cannot be an empty set with elementdef=nil in }
  1204. { case right is not a set, arrayconstructor_to_set takes }
  1205. { care of that }
  1206. { 1: rd is a set with an assigned elementdef, and ld is }
  1207. { either an empty set without elementdef or a set whose }
  1208. { elementdef fits in rd's elementdef -> convert to rd }
  1209. if ((rd.typ=setdef) and
  1210. assigned(tsetdef(rd).elementdef) and
  1211. (not assigned(tsetdef(ld).elementdef) or
  1212. is_in_limit(ld,rd))) then
  1213. inserttypeconv(left,rd)
  1214. { 2: rd is either an empty set without elementdef or a set }
  1215. { whose elementdef fits in ld's elementdef, or a set }
  1216. { element whose def fits in ld's elementdef -> convert }
  1217. { to ld. ld's elementdef can't be nil here, is caught }
  1218. { previous case and "note:" above }
  1219. else if ((rd.typ=setdef) and
  1220. (not assigned(tsetdef(rd).elementdef) or
  1221. is_in_limit(rd,ld))) or
  1222. ((rd.typ<>setdef) and
  1223. is_in_limit(rd,tsetdef(ld).elementdef)) then
  1224. if (rd.typ=setdef) then
  1225. inserttypeconv(right,ld)
  1226. else
  1227. inserttypeconv(right,tsetdef(ld).elementdef)
  1228. { 3: otherwise create setdef which encompasses both, taking }
  1229. { into account empty sets without elementdef }
  1230. else
  1231. begin
  1232. if assigned(tsetdef(ld).elementdef) then
  1233. begin
  1234. llow:=tsetdef(ld).setbase;
  1235. lhigh:=tsetdef(ld).setmax;
  1236. end;
  1237. if (rd.typ=setdef) then
  1238. if assigned(tsetdef(rd).elementdef) then
  1239. begin
  1240. rlow:=tsetdef(rd).setbase;
  1241. rhigh:=tsetdef(rd).setmax;
  1242. end
  1243. else
  1244. begin
  1245. { ld's elementdef must have been valid }
  1246. rlow:=llow;
  1247. rhigh:=lhigh;
  1248. end
  1249. else
  1250. getrange(rd,rlow,rhigh);
  1251. if not assigned(tsetdef(ld).elementdef) then
  1252. begin
  1253. llow:=rlow;
  1254. lhigh:=rhigh;
  1255. end;
  1256. nd:=tsetdef.create(tsetdef(ld).elementdef,min(llow,rlow),max(lhigh,rhigh));
  1257. inserttypeconv(left,nd);
  1258. if (rd.typ=setdef) then
  1259. inserttypeconv(right,nd)
  1260. else
  1261. inserttypeconv(right,tsetdef(nd).elementdef);
  1262. end;
  1263. end;
  1264. end
  1265. { pointer comparision and subtraction }
  1266. else if (
  1267. (rd.typ=pointerdef) and (ld.typ=pointerdef)
  1268. ) or
  1269. { compare/add pchar to variable (not stringconst) char arrays
  1270. by addresses like BP/Delphi }
  1271. (
  1272. (nodetype in [equaln,unequaln,subn,addn]) and
  1273. (
  1274. ((is_pchar(ld) or (lt=niln)) and is_chararray(rd) and (rt<>stringconstn)) or
  1275. ((is_pchar(rd) or (rt=niln)) and is_chararray(ld) and (lt<>stringconstn))
  1276. )
  1277. ) then
  1278. begin
  1279. { convert char array to pointer }
  1280. if is_chararray(rd) then
  1281. begin
  1282. inserttypeconv(right,charpointertype);
  1283. rd:=right.resultdef;
  1284. end
  1285. else if is_chararray(ld) then
  1286. begin
  1287. inserttypeconv(left,charpointertype);
  1288. ld:=left.resultdef;
  1289. end;
  1290. case nodetype of
  1291. equaln,unequaln :
  1292. begin
  1293. if is_voidpointer(right.resultdef) then
  1294. inserttypeconv(right,left.resultdef)
  1295. else if is_voidpointer(left.resultdef) then
  1296. inserttypeconv(left,right.resultdef)
  1297. else if not(equal_defs(ld,rd)) then
  1298. IncompatibleTypes(ld,rd);
  1299. { now that the type checking is done, convert both to charpointer, }
  1300. { because methodpointers are 8 bytes even though only the first 4 }
  1301. { bytes must be compared. This can happen here if we are in }
  1302. { TP/Delphi mode, because there @methodpointer = voidpointer (but }
  1303. { a voidpointer of 8 bytes). A conversion to voidpointer would be }
  1304. { optimized away, since the result already was a voidpointer, so }
  1305. { use a charpointer instead (JM) }
  1306. inserttypeconv_internal(left,charpointertype);
  1307. inserttypeconv_internal(right,charpointertype);
  1308. end;
  1309. ltn,lten,gtn,gten:
  1310. begin
  1311. if (cs_extsyntax in current_settings.moduleswitches) then
  1312. begin
  1313. if is_voidpointer(right.resultdef) then
  1314. inserttypeconv(right,left.resultdef)
  1315. else if is_voidpointer(left.resultdef) then
  1316. inserttypeconv(left,right.resultdef)
  1317. else if not(equal_defs(ld,rd)) then
  1318. IncompatibleTypes(ld,rd);
  1319. end
  1320. else
  1321. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1322. end;
  1323. subn:
  1324. begin
  1325. if (cs_extsyntax in current_settings.moduleswitches) then
  1326. begin
  1327. if is_voidpointer(right.resultdef) then
  1328. begin
  1329. if is_big_untyped_addrnode(right) then
  1330. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  1331. inserttypeconv(right,left.resultdef)
  1332. end
  1333. else if is_voidpointer(left.resultdef) then
  1334. inserttypeconv(left,right.resultdef)
  1335. else if not(equal_defs(ld,rd)) then
  1336. IncompatibleTypes(ld,rd);
  1337. end
  1338. else
  1339. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1340. if not(nf_has_pointerdiv in flags) and
  1341. (tpointerdef(rd).pointeddef.size>1) then
  1342. begin
  1343. hp:=getcopy;
  1344. include(hp.flags,nf_has_pointerdiv);
  1345. result:=cmoddivnode.create(divn,hp,cordconstnode.create(tpointerdef(rd).pointeddef.size,sinttype,false));
  1346. end;
  1347. resultdef:=sinttype;
  1348. exit;
  1349. end;
  1350. else
  1351. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1352. end;
  1353. end
  1354. { is one of the operands a string?,
  1355. chararrays are also handled as strings (after conversion), also take
  1356. care of chararray+chararray and chararray+char.
  1357. Note: Must be done after pointerdef+pointerdef has been checked, else
  1358. pchar is converted to string }
  1359. else if (rd.typ=stringdef) or
  1360. (ld.typ=stringdef) or
  1361. { stringconstn's can be arraydefs }
  1362. (lt=stringconstn) or
  1363. (rt=stringconstn) or
  1364. ((is_pchar(rd) or is_chararray(rd) or is_char(rd) or is_open_chararray(rd) or
  1365. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd)) and
  1366. (is_pchar(ld) or is_chararray(ld) or is_char(ld) or is_open_chararray(ld) or
  1367. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld))) then
  1368. begin
  1369. if (nodetype in [addn,equaln,unequaln,lten,gten,ltn,gtn]) then
  1370. begin
  1371. { Is there a unicodestring? }
  1372. if is_unicodestring(rd) or is_unicodestring(ld) then
  1373. strtype:=st_unicodestring
  1374. else
  1375. { Is there a widestring? }
  1376. if is_widestring(rd) or is_widestring(ld) or
  1377. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd) or
  1378. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld) then
  1379. strtype:=st_widestring
  1380. else
  1381. if is_ansistring(rd) or is_ansistring(ld) or
  1382. ((cs_ansistrings in current_settings.localswitches) and
  1383. //todo: Move some of this to longstring's then they are implemented?
  1384. (
  1385. is_pchar(rd) or (is_chararray(rd) and (rd.size > 255)) or is_open_chararray(rd) or
  1386. is_pchar(ld) or (is_chararray(ld) and (ld.size > 255)) or is_open_chararray(ld)
  1387. )
  1388. ) then
  1389. strtype:=st_ansistring
  1390. else
  1391. if is_longstring(rd) or is_longstring(ld) then
  1392. strtype:=st_longstring
  1393. else
  1394. begin
  1395. { TODO: todo: add a warning/hint here if one converting a too large array}
  1396. { nodes is PChar, array [with size > 255] or OpenArrayOfChar.
  1397. Note: Delphi halts with error if "array [0..xx] of char"
  1398. is assigned to ShortString and string length is less
  1399. then array size }
  1400. strtype:= st_shortstring;
  1401. end;
  1402. // Now convert nodes to common string type
  1403. case strtype of
  1404. st_widestring :
  1405. begin
  1406. if not(is_widestring(rd)) then
  1407. inserttypeconv(right,cwidestringtype);
  1408. if not(is_widestring(ld)) then
  1409. inserttypeconv(left,cwidestringtype);
  1410. end;
  1411. st_unicodestring :
  1412. begin
  1413. if not(is_unicodestring(rd)) then
  1414. inserttypeconv(right,cunicodestringtype);
  1415. if not(is_unicodestring(ld)) then
  1416. inserttypeconv(left,cunicodestringtype);
  1417. end;
  1418. st_ansistring :
  1419. begin
  1420. if not(is_ansistring(rd)) then
  1421. inserttypeconv(right,cansistringtype);
  1422. if not(is_ansistring(ld)) then
  1423. inserttypeconv(left,cansistringtype);
  1424. end;
  1425. st_longstring :
  1426. begin
  1427. if not(is_longstring(rd)) then
  1428. inserttypeconv(right,clongstringtype);
  1429. if not(is_longstring(ld)) then
  1430. inserttypeconv(left,clongstringtype);
  1431. end;
  1432. st_shortstring :
  1433. begin
  1434. if not(is_shortstring(ld)) then
  1435. inserttypeconv(left,cshortstringtype);
  1436. { don't convert char, that can be handled by the optimized node }
  1437. if not(is_shortstring(rd) or is_char(rd)) then
  1438. inserttypeconv(right,cshortstringtype);
  1439. end;
  1440. else
  1441. internalerror(2005101);
  1442. end;
  1443. end
  1444. else
  1445. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1446. end
  1447. { class or interface equation }
  1448. else if is_class_or_interface(rd) or is_class_or_interface(ld) then
  1449. begin
  1450. if (nodetype in [equaln,unequaln]) then
  1451. begin
  1452. if is_class_or_interface(rd) and is_class_or_interface(ld) then
  1453. begin
  1454. if tobjectdef(rd).is_related(tobjectdef(ld)) then
  1455. inserttypeconv(right,left.resultdef)
  1456. else
  1457. inserttypeconv(left,right.resultdef);
  1458. end
  1459. else if is_class_or_interface(rd) then
  1460. inserttypeconv(left,right.resultdef)
  1461. else
  1462. inserttypeconv(right,left.resultdef);
  1463. end
  1464. else
  1465. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1466. end
  1467. else if (rd.typ=classrefdef) and (ld.typ=classrefdef) then
  1468. begin
  1469. if (nodetype in [equaln,unequaln]) then
  1470. begin
  1471. if tobjectdef(tclassrefdef(rd).pointeddef).is_related(
  1472. tobjectdef(tclassrefdef(ld).pointeddef)) then
  1473. inserttypeconv(right,left.resultdef)
  1474. else
  1475. inserttypeconv(left,right.resultdef);
  1476. end
  1477. else
  1478. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1479. end
  1480. { allows comperasion with nil pointer }
  1481. else if is_class_or_interface(rd) or (rd.typ=classrefdef) then
  1482. begin
  1483. if (nodetype in [equaln,unequaln]) then
  1484. inserttypeconv(left,right.resultdef)
  1485. else
  1486. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1487. end
  1488. else if is_class_or_interface(ld) or (ld.typ=classrefdef) then
  1489. begin
  1490. if (nodetype in [equaln,unequaln]) then
  1491. inserttypeconv(right,left.resultdef)
  1492. else
  1493. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1494. end
  1495. { support procvar=nil,procvar<>nil }
  1496. else if ((ld.typ=procvardef) and (rt=niln)) or
  1497. ((rd.typ=procvardef) and (lt=niln)) then
  1498. begin
  1499. if not(nodetype in [equaln,unequaln]) then
  1500. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1501. { find proc field in methodpointer record }
  1502. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  1503. if not assigned(hsym) then
  1504. internalerror(200412043);
  1505. { For methodpointers compare only tmethodpointer.proc }
  1506. if (rd.typ=procvardef) and
  1507. (not tprocvardef(rd).is_addressonly) then
  1508. begin
  1509. right:=csubscriptnode.create(
  1510. hsym,
  1511. ctypeconvnode.create_internal(right,methodpointertype));
  1512. typecheckpass(right);
  1513. end;
  1514. if (ld.typ=procvardef) and
  1515. (not tprocvardef(ld).is_addressonly) then
  1516. begin
  1517. left:=csubscriptnode.create(
  1518. hsym,
  1519. ctypeconvnode.create_internal(left,methodpointertype));
  1520. typecheckpass(left);
  1521. end;
  1522. end
  1523. { support dynamicarray=nil,dynamicarray<>nil }
  1524. else if (is_dynamic_array(ld) and (rt=niln)) or
  1525. (is_dynamic_array(rd) and (lt=niln)) or
  1526. (is_dynamic_array(ld) and is_dynamic_array(rd)) then
  1527. begin
  1528. if not(nodetype in [equaln,unequaln]) then
  1529. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1530. end
  1531. {$ifdef SUPPORT_MMX}
  1532. { mmx support, this must be before the zero based array
  1533. check }
  1534. else if (cs_mmx in current_settings.localswitches) and
  1535. is_mmx_able_array(ld) and
  1536. is_mmx_able_array(rd) and
  1537. equal_defs(ld,rd) then
  1538. begin
  1539. case nodetype of
  1540. addn,subn,xorn,orn,andn:
  1541. ;
  1542. { mul is a little bit restricted }
  1543. muln:
  1544. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  1545. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1546. else
  1547. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1548. end;
  1549. end
  1550. {$endif SUPPORT_MMX}
  1551. { vector support, this must be before the zero based array
  1552. check }
  1553. else if (cs_support_vectors in current_settings.globalswitches) and
  1554. is_vector(ld) and
  1555. is_vector(rd) and
  1556. equal_defs(ld,rd) then
  1557. begin
  1558. if not(nodetype in [addn,subn,xorn,orn,andn,muln,slashn]) then
  1559. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1560. { both defs must be equal, so taking left or right as resultdef doesn't matter }
  1561. resultdef:=left.resultdef;
  1562. end
  1563. { this is a little bit dangerous, also the left type }
  1564. { pointer to should be checked! This broke the mmx support }
  1565. else if (rd.typ=pointerdef) or
  1566. (is_zero_based_array(rd) and (rt<>stringconstn)) then
  1567. begin
  1568. if is_zero_based_array(rd) then
  1569. begin
  1570. resultdef:=tpointerdef.create(tarraydef(rd).elementdef);
  1571. inserttypeconv(right,resultdef);
  1572. end
  1573. else
  1574. resultdef:=right.resultdef;
  1575. inserttypeconv(left,sinttype);
  1576. if nodetype=addn then
  1577. begin
  1578. if not(cs_extsyntax in current_settings.moduleswitches) or
  1579. (not(is_pchar(ld)) and not(m_add_pointer in current_settings.modeswitches)) then
  1580. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1581. if (rd.typ=pointerdef) and
  1582. (tpointerdef(rd).pointeddef.size>1) then
  1583. begin
  1584. left:=caddnode.create(muln,left,
  1585. cordconstnode.create(tpointerdef(rd).pointeddef.size,sinttype,true));
  1586. typecheckpass(left);
  1587. end;
  1588. end
  1589. else
  1590. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1591. end
  1592. else if (ld.typ=pointerdef) or
  1593. (is_zero_based_array(ld) and (lt<>stringconstn)) then
  1594. begin
  1595. if is_zero_based_array(ld) then
  1596. begin
  1597. resultdef:=tpointerdef.create(tarraydef(ld).elementdef);
  1598. inserttypeconv(left,resultdef);
  1599. end
  1600. else
  1601. resultdef:=left.resultdef;
  1602. inserttypeconv(right,sinttype);
  1603. if nodetype in [addn,subn] then
  1604. begin
  1605. if not(cs_extsyntax in current_settings.moduleswitches) or
  1606. (not(is_pchar(ld)) and not(m_add_pointer in current_settings.modeswitches)) then
  1607. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1608. if (ld.typ=pointerdef) then
  1609. begin
  1610. if is_big_untyped_addrnode(left) then
  1611. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  1612. if (tpointerdef(ld).pointeddef.size>1) then
  1613. begin
  1614. right:=caddnode.create(muln,right,
  1615. cordconstnode.create(tpointerdef(ld).pointeddef.size,sinttype,true));
  1616. typecheckpass(right);
  1617. end
  1618. end else
  1619. if is_zero_based_array(ld) and
  1620. (tarraydef(ld).elementdef.size>1) then
  1621. begin
  1622. right:=caddnode.create(muln,right,
  1623. cordconstnode.create(tarraydef(ld).elementdef.size,sinttype,true));
  1624. typecheckpass(right);
  1625. end;
  1626. end
  1627. else
  1628. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1629. end
  1630. else if (rd.typ=procvardef) and
  1631. (ld.typ=procvardef) and
  1632. equal_defs(rd,ld) then
  1633. begin
  1634. if (nodetype in [equaln,unequaln]) then
  1635. begin
  1636. if tprocvardef(rd).is_addressonly then
  1637. begin
  1638. inserttypeconv_internal(right,voidpointertype);
  1639. inserttypeconv_internal(left,voidpointertype);
  1640. end
  1641. else
  1642. begin
  1643. { find proc field in methodpointer record }
  1644. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  1645. if not assigned(hsym) then
  1646. internalerror(200412043);
  1647. { Compare tmehodpointer(left).proc }
  1648. right:=csubscriptnode.create(
  1649. hsym,
  1650. ctypeconvnode.create_internal(right,methodpointertype));
  1651. typecheckpass(right);
  1652. left:=csubscriptnode.create(
  1653. hsym,
  1654. ctypeconvnode.create_internal(left,methodpointertype));
  1655. typecheckpass(left);
  1656. end;
  1657. end
  1658. else
  1659. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1660. end
  1661. { enums }
  1662. else if (ld.typ=enumdef) and (rd.typ=enumdef) then
  1663. begin
  1664. if allowenumop(nodetype) then
  1665. inserttypeconv(right,left.resultdef)
  1666. else
  1667. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1668. end
  1669. { generic conversion, this is for error recovery }
  1670. else
  1671. begin
  1672. inserttypeconv(left,sinttype);
  1673. inserttypeconv(right,sinttype);
  1674. end;
  1675. { set resultdef if not already done }
  1676. if not assigned(resultdef) then
  1677. begin
  1678. case nodetype of
  1679. ltn,lten,gtn,gten,equaln,unequaln :
  1680. resultdef:=booltype;
  1681. slashn :
  1682. resultdef:=resultrealdef;
  1683. addn:
  1684. begin
  1685. { for strings, return is always a 255 char string }
  1686. if is_shortstring(left.resultdef) then
  1687. resultdef:=cshortstringtype
  1688. else
  1689. resultdef:=left.resultdef;
  1690. end;
  1691. else
  1692. resultdef:=left.resultdef;
  1693. end;
  1694. end;
  1695. { when the result is currency we need some extra code for
  1696. multiplication and division. this should not be done when
  1697. the muln or slashn node is created internally }
  1698. if not(nf_is_currency in flags) and
  1699. is_currency(resultdef) then
  1700. begin
  1701. case nodetype of
  1702. slashn :
  1703. begin
  1704. { slashn will only work with floats }
  1705. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  1706. include(hp.flags,nf_is_currency);
  1707. result:=hp;
  1708. end;
  1709. muln :
  1710. begin
  1711. if s64currencytype.typ=floatdef then
  1712. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype))
  1713. else
  1714. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  1715. include(hp.flags,nf_is_currency);
  1716. result:=hp
  1717. end;
  1718. end;
  1719. end;
  1720. if not codegenerror and
  1721. not assigned(result) then
  1722. result:=simplify;
  1723. end;
  1724. function taddnode.first_addstring: tnode;
  1725. const
  1726. swap_relation: array [ltn..unequaln] of Tnodetype=(gtn, gten, ltn, lten, equaln, unequaln);
  1727. var
  1728. p: tnode;
  1729. newstatement : tstatementnode;
  1730. tempnode (*,tempnode2*) : ttempcreatenode;
  1731. cmpfuncname: string;
  1732. begin
  1733. { when we get here, we are sure that both the left and the right }
  1734. { node are both strings of the same stringtype (JM) }
  1735. case nodetype of
  1736. addn:
  1737. begin
  1738. if (left.nodetype=stringconstn) and (tstringconstnode(left).len=0) then
  1739. begin
  1740. result:=right;
  1741. left.free;
  1742. left:=nil;
  1743. right:=nil;
  1744. exit;
  1745. end;
  1746. if (right.nodetype=stringconstn) and (tstringconstnode(right).len=0) then
  1747. begin
  1748. result:=left;
  1749. left:=nil;
  1750. right.free;
  1751. right:=nil;
  1752. exit;
  1753. end;
  1754. { create the call to the concat routine both strings as arguments }
  1755. if assigned(aktassignmentnode) and
  1756. (aktassignmentnode.right=self) and
  1757. (aktassignmentnode.left.resultdef=resultdef) and
  1758. valid_for_var(aktassignmentnode.left,false) then
  1759. begin
  1760. result:=ccallnode.createintern('fpc_'+
  1761. tstringdef(resultdef).stringtypname+'_concat',
  1762. ccallparanode.create(right,
  1763. ccallparanode.create(left,
  1764. ccallparanode.create(aktassignmentnode.left.getcopy,nil))));
  1765. include(aktassignmentnode.flags,nf_assign_done_in_right);
  1766. firstpass(result);
  1767. end
  1768. else
  1769. begin
  1770. result:=internalstatements(newstatement);
  1771. tempnode:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1772. addstatement(newstatement,tempnode);
  1773. addstatement(newstatement,ccallnode.createintern('fpc_'+
  1774. tstringdef(resultdef).stringtypname+'_concat',
  1775. ccallparanode.create(right,
  1776. ccallparanode.create(left,
  1777. ccallparanode.create(ctemprefnode.create(tempnode),nil)))));
  1778. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  1779. addstatement(newstatement,ctemprefnode.create(tempnode));
  1780. end;
  1781. { we reused the arguments }
  1782. left := nil;
  1783. right := nil;
  1784. end;
  1785. ltn,lten,gtn,gten,equaln,unequaln :
  1786. begin
  1787. { generate better code for comparison with empty string, we
  1788. only need to compare the length with 0 }
  1789. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  1790. { windows widestrings are too complicated to be handled optimized }
  1791. not(is_widestring(left.resultdef) and (target_info.system in system_windows)) and
  1792. (((left.nodetype=stringconstn) and (tstringconstnode(left).len=0)) or
  1793. ((right.nodetype=stringconstn) and (tstringconstnode(right).len=0))) then
  1794. begin
  1795. { switch so that the constant is always on the right }
  1796. if left.nodetype = stringconstn then
  1797. begin
  1798. p := left;
  1799. left := right;
  1800. right := p;
  1801. nodetype:=swap_relation[nodetype];
  1802. end;
  1803. if is_shortstring(left.resultdef) or
  1804. (nodetype in [gtn,gten,ltn,lten]) then
  1805. { compare the length with 0 }
  1806. result := caddnode.create(nodetype,
  1807. cinlinenode.create(in_length_x,false,left),
  1808. cordconstnode.create(0,s32inttype,false))
  1809. else
  1810. begin
  1811. (*
  1812. if is_widestring(left.resultdef) and
  1813. (target_info.system in system_windows) then
  1814. begin
  1815. { windows like widestrings requires that we also check the length }
  1816. result:=internalstatements(newstatement);
  1817. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1818. tempnode2:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1819. addstatement(newstatement,tempnode);
  1820. addstatement(newstatement,tempnode2);
  1821. { poor man's cse }
  1822. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  1823. ctypeconvnode.create_internal(left,voidpointertype))
  1824. );
  1825. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode2),
  1826. caddnode.create(orn,
  1827. caddnode.create(nodetype,
  1828. ctemprefnode.create(tempnode),
  1829. cpointerconstnode.create(0,voidpointertype)
  1830. ),
  1831. caddnode.create(nodetype,
  1832. ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),s32inttype),
  1833. cordconstnode.create(0,s32inttype,false)
  1834. )
  1835. )
  1836. ));
  1837. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  1838. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode2));
  1839. addstatement(newstatement,ctemprefnode.create(tempnode2));
  1840. end
  1841. else
  1842. *)
  1843. begin
  1844. { compare the pointer with nil (for ansistrings etc), }
  1845. { faster than getting the length (JM) }
  1846. result:= caddnode.create(nodetype,
  1847. ctypeconvnode.create_internal(left,voidpointertype),
  1848. cpointerconstnode.create(0,voidpointertype));
  1849. end;
  1850. end;
  1851. { left is reused }
  1852. left := nil;
  1853. { right isn't }
  1854. right.free;
  1855. right := nil;
  1856. exit;
  1857. end;
  1858. { no string constant -> call compare routine }
  1859. cmpfuncname := 'fpc_'+tstringdef(left.resultdef).stringtypname+'_compare';
  1860. { for equality checks use optimized version }
  1861. if nodetype in [equaln,unequaln] then
  1862. cmpfuncname := cmpfuncname + '_equal';
  1863. result := ccallnode.createintern(cmpfuncname,
  1864. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1865. { and compare its result with 0 according to the original operator }
  1866. result := caddnode.create(nodetype,result,
  1867. cordconstnode.create(0,s32inttype,false));
  1868. left := nil;
  1869. right := nil;
  1870. end;
  1871. end;
  1872. end;
  1873. function taddnode.first_addset : tnode;
  1874. procedure call_varset_helper(const n : string);
  1875. var
  1876. newstatement : tstatementnode;
  1877. temp : ttempcreatenode;
  1878. begin
  1879. { add two var sets }
  1880. result:=internalstatements(newstatement);
  1881. { create temp for result }
  1882. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1883. addstatement(newstatement,temp);
  1884. addstatement(newstatement,ccallnode.createintern(n,
  1885. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  1886. ccallparanode.create(ctemprefnode.create(temp),
  1887. ccallparanode.create(right,
  1888. ccallparanode.create(left,nil)))))
  1889. );
  1890. { remove reused parts from original node }
  1891. left:=nil;
  1892. right:=nil;
  1893. { the last statement should return the value as
  1894. location and type, this is done be referencing the
  1895. temp and converting it first from a persistent temp to
  1896. normal temp }
  1897. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1898. addstatement(newstatement,ctemprefnode.create(temp));
  1899. end;
  1900. var
  1901. procname: string[31];
  1902. tempn: tnode;
  1903. newstatement : tstatementnode;
  1904. temp : ttempcreatenode;
  1905. begin
  1906. result:=nil;
  1907. case nodetype of
  1908. equaln,unequaln,lten,gten:
  1909. begin
  1910. case nodetype of
  1911. equaln,unequaln:
  1912. procname := 'fpc_varset_comp_sets';
  1913. lten,gten:
  1914. begin
  1915. procname := 'fpc_varset_contains_sets';
  1916. { (left >= right) = (right <= left) }
  1917. if nodetype = gten then
  1918. begin
  1919. tempn := left;
  1920. left := right;
  1921. right := tempn;
  1922. end;
  1923. end;
  1924. end;
  1925. result := ccallnode.createinternres(procname,
  1926. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  1927. ccallparanode.create(right,
  1928. ccallparanode.create(left,nil))),resultdef);
  1929. { left and right are reused as parameters }
  1930. left := nil;
  1931. right := nil;
  1932. { for an unequaln, we have to negate the result of comp_sets }
  1933. if nodetype = unequaln then
  1934. result := cnotnode.create(result);
  1935. end;
  1936. addn:
  1937. begin
  1938. { optimize first loading of a set }
  1939. if (right.nodetype=setelementn) and
  1940. not(assigned(tsetelementnode(right).right)) and
  1941. is_emptyset(left) then
  1942. begin
  1943. result:=internalstatements(newstatement);
  1944. { create temp for result }
  1945. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1946. addstatement(newstatement,temp);
  1947. { adjust for set base }
  1948. tsetelementnode(right).left:=caddnode.create(subn,
  1949. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  1950. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  1951. addstatement(newstatement,ccallnode.createintern('fpc_varset_create_element',
  1952. ccallparanode.create(ctemprefnode.create(temp),
  1953. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  1954. ccallparanode.create(tsetelementnode(right).left,nil))))
  1955. );
  1956. { the last statement should return the value as
  1957. location and type, this is done be referencing the
  1958. temp and converting it first from a persistent temp to
  1959. normal temp }
  1960. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1961. addstatement(newstatement,ctemprefnode.create(temp));
  1962. tsetelementnode(right).left := nil;
  1963. end
  1964. else
  1965. begin
  1966. if right.nodetype=setelementn then
  1967. begin
  1968. result:=internalstatements(newstatement);
  1969. { create temp for result }
  1970. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1971. addstatement(newstatement,temp);
  1972. { adjust for set base }
  1973. tsetelementnode(right).left:=caddnode.create(subn,
  1974. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  1975. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  1976. { add a range or a single element? }
  1977. if assigned(tsetelementnode(right).right) then
  1978. begin
  1979. { adjust for set base }
  1980. tsetelementnode(right).right:=caddnode.create(subn,
  1981. ctypeconvnode.create_internal(tsetelementnode(right).right,sinttype),
  1982. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  1983. addstatement(newstatement,ccallnode.createintern('fpc_varset_set_range',
  1984. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  1985. ccallparanode.create(tsetelementnode(right).right,
  1986. ccallparanode.create(tsetelementnode(right).left,
  1987. ccallparanode.create(ctemprefnode.create(temp),
  1988. ccallparanode.create(left,nil))))))
  1989. );
  1990. end
  1991. else
  1992. addstatement(newstatement,ccallnode.createintern('fpc_varset_set',
  1993. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  1994. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  1995. ccallparanode.create(ctemprefnode.create(temp),
  1996. ccallparanode.create(left,nil)))))
  1997. );
  1998. { remove reused parts from original node }
  1999. tsetelementnode(right).right:=nil;
  2000. tsetelementnode(right).left:=nil;
  2001. left:=nil;
  2002. { the last statement should return the value as
  2003. location and type, this is done be referencing the
  2004. temp and converting it first from a persistent temp to
  2005. normal temp }
  2006. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2007. addstatement(newstatement,ctemprefnode.create(temp));
  2008. end
  2009. else
  2010. call_varset_helper('fpc_varset_add_sets');
  2011. end
  2012. end;
  2013. subn:
  2014. call_varset_helper('fpc_varset_sub_sets');
  2015. symdifn:
  2016. call_varset_helper('fpc_varset_symdif_sets');
  2017. muln:
  2018. call_varset_helper('fpc_varset_mul_sets');
  2019. else
  2020. internalerror(200609241);
  2021. end;
  2022. end;
  2023. function taddnode.use_generic_mul32to64: boolean;
  2024. begin
  2025. result := true;
  2026. end;
  2027. function taddnode.try_make_mul32to64: boolean;
  2028. function canbe32bitint(v: tconstexprint): boolean;
  2029. begin
  2030. result := ((v >= int64(low(longint))) and (v <= int64(high(longint)))) or
  2031. ((v >= qword(low(cardinal))) and (v <= qword(high(cardinal))))
  2032. end;
  2033. var
  2034. temp: tnode;
  2035. begin
  2036. result := false;
  2037. if ((left.nodetype = typeconvn) and
  2038. is_integer(ttypeconvnode(left).left.resultdef) and
  2039. (not(torddef(ttypeconvnode(left).left.resultdef).ordtype in [u64bit,s64bit])) and
  2040. (((right.nodetype = ordconstn) and canbe32bitint(tordconstnode(right).value)) or
  2041. ((right.nodetype = typeconvn) and
  2042. is_integer(ttypeconvnode(right).left.resultdef) and
  2043. not(torddef(ttypeconvnode(right).left.resultdef).ordtype in [u64bit,s64bit])) and
  2044. ((is_signed(ttypeconvnode(left).left.resultdef) =
  2045. is_signed(ttypeconvnode(right).left.resultdef)) or
  2046. (is_signed(ttypeconvnode(left).left.resultdef) and
  2047. (torddef(ttypeconvnode(right).left.resultdef).ordtype in [u8bit,u16bit]))))) then
  2048. begin
  2049. temp := ttypeconvnode(left).left;
  2050. ttypeconvnode(left).left := nil;
  2051. left.free;
  2052. left := temp;
  2053. if (right.nodetype = typeconvn) then
  2054. begin
  2055. temp := ttypeconvnode(right).left;
  2056. ttypeconvnode(right).left := nil;
  2057. right.free;
  2058. right := temp;
  2059. end;
  2060. if (is_signed(left.resultdef)) then
  2061. begin
  2062. inserttypeconv(left,s32inttype);
  2063. inserttypeconv(right,s32inttype);
  2064. end
  2065. else
  2066. begin
  2067. inserttypeconv(left,u32inttype);
  2068. inserttypeconv(right,u32inttype);
  2069. end;
  2070. firstpass(left);
  2071. firstpass(right);
  2072. result := true;
  2073. end;
  2074. end;
  2075. function taddnode.first_add64bitint: tnode;
  2076. var
  2077. procname: string[31];
  2078. temp: tnode;
  2079. power: longint;
  2080. begin
  2081. result := nil;
  2082. { create helper calls mul }
  2083. if nodetype <> muln then
  2084. exit;
  2085. { make sure that if there is a constant, that it's on the right }
  2086. if left.nodetype = ordconstn then
  2087. begin
  2088. temp := right;
  2089. right := left;
  2090. left := temp;
  2091. end;
  2092. { can we use a shift instead of a mul? }
  2093. if not (cs_check_overflow in current_settings.localswitches) and
  2094. (right.nodetype = ordconstn) and
  2095. ispowerof2(tordconstnode(right).value,power) then
  2096. begin
  2097. tordconstnode(right).value := power;
  2098. result := cshlshrnode.create(shln,left,right);
  2099. { left and right are reused }
  2100. left := nil;
  2101. right := nil;
  2102. { return firstpassed new node }
  2103. exit;
  2104. end;
  2105. if not(use_generic_mul32to64) and
  2106. try_make_mul32to64 then
  2107. exit;
  2108. { when currency is used set the result of the
  2109. parameters to s64bit, so they are not converted }
  2110. if is_currency(resultdef) then
  2111. begin
  2112. left.resultdef:=s64inttype;
  2113. right.resultdef:=s64inttype;
  2114. end;
  2115. { otherwise, create the parameters for the helper }
  2116. right := ccallparanode.create(
  2117. cordconstnode.create(ord(cs_check_overflow in current_settings.localswitches),booltype,true),
  2118. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2119. left := nil;
  2120. { only qword needs the unsigned code, the
  2121. signed code is also used for currency }
  2122. if is_signed(resultdef) then
  2123. procname := 'fpc_mul_int64'
  2124. else
  2125. procname := 'fpc_mul_qword';
  2126. result := ccallnode.createintern(procname,right);
  2127. right := nil;
  2128. end;
  2129. function taddnode.first_addfloat : tnode;
  2130. var
  2131. procname: string[31];
  2132. { do we need to reverse the result ? }
  2133. notnode : boolean;
  2134. fdef : tdef;
  2135. begin
  2136. result := nil;
  2137. notnode := false;
  2138. { In non-emulation mode, real opcodes are
  2139. emitted for floating point values.
  2140. }
  2141. if not (cs_fp_emulation in current_settings.moduleswitches) then
  2142. exit;
  2143. if not(target_info.system in system_wince) then
  2144. begin
  2145. case tfloatdef(left.resultdef).floattype of
  2146. s32real:
  2147. begin
  2148. fdef:=search_system_type('FLOAT32REC').typedef;
  2149. procname:='float32';
  2150. end;
  2151. s64real:
  2152. begin
  2153. fdef:=search_system_type('FLOAT64').typedef;
  2154. procname:='float64';
  2155. end;
  2156. {!!! not yet implemented
  2157. s128real:
  2158. }
  2159. else
  2160. internalerror(2005082601);
  2161. end;
  2162. case nodetype of
  2163. addn:
  2164. procname:=procname+'_add';
  2165. muln:
  2166. procname:=procname+'_mul';
  2167. subn:
  2168. procname:=procname+'_sub';
  2169. slashn:
  2170. procname:=procname+'_div';
  2171. ltn:
  2172. procname:=procname+'_lt';
  2173. lten:
  2174. procname:=procname+'_le';
  2175. gtn:
  2176. begin
  2177. procname:=procname+'_le';
  2178. notnode:=true;
  2179. end;
  2180. gten:
  2181. begin
  2182. procname:=procname+'_lt';
  2183. notnode:=true;
  2184. end;
  2185. equaln:
  2186. procname:=procname+'_eq';
  2187. unequaln:
  2188. begin
  2189. procname:=procname+'_eq';
  2190. notnode:=true;
  2191. end;
  2192. else
  2193. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  2194. end;
  2195. end
  2196. else
  2197. begin
  2198. case nodetype of
  2199. addn:
  2200. procname:='ADD';
  2201. muln:
  2202. procname:='MUL';
  2203. subn:
  2204. procname:='SUB';
  2205. slashn:
  2206. procname:='DIV';
  2207. ltn:
  2208. procname:='LT';
  2209. lten:
  2210. procname:='LE';
  2211. gtn:
  2212. procname:='GT';
  2213. gten:
  2214. procname:='GE';
  2215. equaln:
  2216. procname:='EQ';
  2217. unequaln:
  2218. procname:='NE';
  2219. else
  2220. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  2221. end;
  2222. case tfloatdef(left.resultdef).floattype of
  2223. s32real:
  2224. begin
  2225. procname:=procname+'S';
  2226. if nodetype in [addn,muln,subn,slashn] then
  2227. procname:=lower(procname);
  2228. end;
  2229. s64real:
  2230. procname:=procname+'D';
  2231. {!!! not yet implemented
  2232. s128real:
  2233. }
  2234. else
  2235. internalerror(2005082602);
  2236. end;
  2237. end;
  2238. { cast softfpu result? }
  2239. if not(target_info.system in system_wince) then
  2240. begin
  2241. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2242. resultdef:=booltype;
  2243. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  2244. ctypeconvnode.create_internal(right,fdef),
  2245. ccallparanode.create(
  2246. ctypeconvnode.create_internal(left,fdef),nil))),resultdef);
  2247. end
  2248. else
  2249. result:=ccallnode.createintern(procname,ccallparanode.create(right,
  2250. ccallparanode.create(left,nil)));
  2251. left:=nil;
  2252. right:=nil;
  2253. { do we need to reverse the result }
  2254. if notnode then
  2255. result:=cnotnode.create(result);
  2256. end;
  2257. function taddnode.pass_1 : tnode;
  2258. var
  2259. {$ifdef addstringopt}
  2260. hp : tnode;
  2261. {$endif addstringopt}
  2262. rd,ld : tdef;
  2263. i : longint;
  2264. lt,rt : tnodetype;
  2265. begin
  2266. result:=nil;
  2267. { Can we optimize multiple string additions into a single call?
  2268. This need to be done on a complete tree to detect the multiple
  2269. add nodes and is therefor done before the subtrees are processed }
  2270. if canbemultistringadd(self) then
  2271. begin
  2272. result := genmultistringadd(self);
  2273. exit;
  2274. end;
  2275. { first do the two subtrees }
  2276. firstpass(left);
  2277. firstpass(right);
  2278. if codegenerror then
  2279. exit;
  2280. { load easier access variables }
  2281. rd:=right.resultdef;
  2282. ld:=left.resultdef;
  2283. rt:=right.nodetype;
  2284. lt:=left.nodetype;
  2285. { int/int gives real/real! }
  2286. if nodetype=slashn then
  2287. begin
  2288. {$ifdef cpufpemu}
  2289. if (current_settings.fputype=fpu_soft) or (cs_fp_emulation in current_settings.moduleswitches) then
  2290. begin
  2291. result:=first_addfloat;
  2292. if assigned(result) then
  2293. exit;
  2294. end;
  2295. {$endif cpufpemu}
  2296. expectloc:=LOC_FPUREGISTER;
  2297. end
  2298. { if both are orddefs then check sub types }
  2299. else if (ld.typ=orddef) and (rd.typ=orddef) then
  2300. begin
  2301. { optimize multiplacation by a power of 2 }
  2302. if not(cs_check_overflow in current_settings.localswitches) and
  2303. (nodetype = muln) and
  2304. (((left.nodetype = ordconstn) and
  2305. ispowerof2(tordconstnode(left).value,i)) or
  2306. ((right.nodetype = ordconstn) and
  2307. ispowerof2(tordconstnode(right).value,i))) then
  2308. begin
  2309. if left.nodetype = ordconstn then
  2310. begin
  2311. tordconstnode(left).value := i;
  2312. result := cshlshrnode.create(shln,right,left);
  2313. end
  2314. else
  2315. begin
  2316. tordconstnode(right).value := i;
  2317. result := cshlshrnode.create(shln,left,right);
  2318. end;
  2319. left := nil;
  2320. right := nil;
  2321. exit;
  2322. end;
  2323. { 2 booleans ? }
  2324. if is_boolean(ld) and is_boolean(rd) then
  2325. begin
  2326. if (not(cs_full_boolean_eval in current_settings.localswitches) or
  2327. (nf_short_bool in flags)) and
  2328. (nodetype in [andn,orn]) then
  2329. expectloc:=LOC_JUMP
  2330. else
  2331. begin
  2332. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2333. expectloc:=LOC_FLAGS
  2334. else
  2335. expectloc:=LOC_REGISTER;
  2336. end;
  2337. end
  2338. else
  2339. { Both are chars? only convert to shortstrings for addn }
  2340. if is_char(ld) then
  2341. begin
  2342. if nodetype=addn then
  2343. internalerror(200103291);
  2344. expectloc:=LOC_FLAGS;
  2345. end
  2346. {$ifndef cpu64bitaddr}
  2347. { is there a 64 bit type ? }
  2348. else if (torddef(ld).ordtype in [s64bit,u64bit,scurrency]) then
  2349. begin
  2350. result := first_add64bitint;
  2351. if assigned(result) then
  2352. exit;
  2353. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2354. expectloc:=LOC_REGISTER
  2355. else
  2356. expectloc:=LOC_JUMP;
  2357. end
  2358. {$endif cpu64bitaddr}
  2359. { is there a cardinal? }
  2360. else if (torddef(ld).ordtype=u32bit) then
  2361. begin
  2362. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2363. expectloc:=LOC_REGISTER
  2364. else
  2365. expectloc:=LOC_FLAGS;
  2366. end
  2367. { generic s32bit conversion }
  2368. else
  2369. begin
  2370. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2371. expectloc:=LOC_REGISTER
  2372. else
  2373. expectloc:=LOC_FLAGS;
  2374. end;
  2375. end
  2376. { left side a setdef, must be before string processing,
  2377. else array constructor can be seen as array of char (PFV) }
  2378. else if (ld.typ=setdef) then
  2379. begin
  2380. { small sets are handled inline by the compiler.
  2381. small set doesn't have support for adding ranges }
  2382. if is_smallset(ld) and
  2383. not(
  2384. (right.nodetype=setelementn) and
  2385. assigned(tsetelementnode(right).right)
  2386. ) then
  2387. begin
  2388. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2389. expectloc:=LOC_FLAGS
  2390. else
  2391. expectloc:=LOC_REGISTER;
  2392. end
  2393. else
  2394. begin
  2395. result := first_addset;
  2396. if assigned(result) then
  2397. exit;
  2398. expectloc:=LOC_CREFERENCE;
  2399. end;
  2400. end
  2401. { compare pchar by addresses like BP/Delphi }
  2402. else if is_pchar(ld) then
  2403. begin
  2404. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2405. expectloc:=LOC_REGISTER
  2406. else
  2407. expectloc:=LOC_FLAGS;
  2408. end
  2409. { is one of the operands a string }
  2410. else if (ld.typ=stringdef) then
  2411. begin
  2412. if is_widestring(ld) then
  2413. begin
  2414. { this is only for add, the comparisaion is handled later }
  2415. expectloc:=LOC_REGISTER;
  2416. end
  2417. else if is_unicodestring(ld) then
  2418. begin
  2419. { this is only for add, the comparisaion is handled later }
  2420. expectloc:=LOC_REGISTER;
  2421. end
  2422. else if is_ansistring(ld) then
  2423. begin
  2424. { this is only for add, the comparisaion is handled later }
  2425. expectloc:=LOC_REGISTER;
  2426. end
  2427. else if is_longstring(ld) then
  2428. begin
  2429. { this is only for add, the comparisaion is handled later }
  2430. expectloc:=LOC_REFERENCE;
  2431. end
  2432. else
  2433. begin
  2434. {$ifdef addstringopt}
  2435. { can create a call which isn't handled by callparatemp }
  2436. if canbeaddsstringcharoptnode(self) then
  2437. begin
  2438. hp := genaddsstringcharoptnode(self);
  2439. pass_1 := hp;
  2440. exit;
  2441. end
  2442. else
  2443. {$endif addstringopt}
  2444. begin
  2445. { Fix right to be shortstring }
  2446. if is_char(right.resultdef) then
  2447. begin
  2448. inserttypeconv(right,cshortstringtype);
  2449. firstpass(right);
  2450. end;
  2451. end;
  2452. {$ifdef addstringopt}
  2453. { can create a call which isn't handled by callparatemp }
  2454. if canbeaddsstringcsstringoptnode(self) then
  2455. begin
  2456. hp := genaddsstringcsstringoptnode(self);
  2457. pass_1 := hp;
  2458. exit;
  2459. end;
  2460. {$endif addstringopt}
  2461. end;
  2462. { otherwise, let addstring convert everything }
  2463. result := first_addstring;
  2464. exit;
  2465. end
  2466. { is one a real float ? }
  2467. else if (rd.typ=floatdef) or (ld.typ=floatdef) then
  2468. begin
  2469. {$ifdef cpufpemu}
  2470. if (current_settings.fputype=fpu_soft) or (cs_fp_emulation in current_settings.moduleswitches) then
  2471. begin
  2472. result:=first_addfloat;
  2473. if assigned(result) then
  2474. exit;
  2475. end;
  2476. {$endif cpufpemu}
  2477. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2478. expectloc:=LOC_FPUREGISTER
  2479. else
  2480. expectloc:=LOC_FLAGS;
  2481. end
  2482. { pointer comperation and subtraction }
  2483. else if (ld.typ=pointerdef) then
  2484. begin
  2485. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2486. expectloc:=LOC_REGISTER
  2487. else
  2488. expectloc:=LOC_FLAGS;
  2489. end
  2490. else if is_class_or_interface(ld) then
  2491. begin
  2492. expectloc:=LOC_FLAGS;
  2493. end
  2494. else if (ld.typ=classrefdef) then
  2495. begin
  2496. expectloc:=LOC_FLAGS;
  2497. end
  2498. { support procvar=nil,procvar<>nil }
  2499. else if ((ld.typ=procvardef) and (rt=niln)) or
  2500. ((rd.typ=procvardef) and (lt=niln)) then
  2501. begin
  2502. expectloc:=LOC_FLAGS;
  2503. end
  2504. {$ifdef SUPPORT_MMX}
  2505. { mmx support, this must be before the zero based array
  2506. check }
  2507. else if (cs_mmx in current_settings.localswitches) and is_mmx_able_array(ld) and
  2508. is_mmx_able_array(rd) then
  2509. begin
  2510. expectloc:=LOC_MMXREGISTER;
  2511. end
  2512. {$endif SUPPORT_MMX}
  2513. else if (rd.typ=pointerdef) or (ld.typ=pointerdef) then
  2514. begin
  2515. expectloc:=LOC_REGISTER;
  2516. end
  2517. else if (rd.typ=procvardef) and
  2518. (ld.typ=procvardef) and
  2519. equal_defs(rd,ld) then
  2520. begin
  2521. expectloc:=LOC_FLAGS;
  2522. end
  2523. else if (ld.typ=enumdef) then
  2524. begin
  2525. expectloc:=LOC_FLAGS;
  2526. end
  2527. {$ifdef SUPPORT_MMX}
  2528. else if (cs_mmx in current_settings.localswitches) and
  2529. is_mmx_able_array(ld) and
  2530. is_mmx_able_array(rd) then
  2531. begin
  2532. expectloc:=LOC_MMXREGISTER;
  2533. end
  2534. {$endif SUPPORT_MMX}
  2535. { the general solution is to convert to 32 bit int }
  2536. else
  2537. begin
  2538. expectloc:=LOC_REGISTER;
  2539. end;
  2540. end;
  2541. {$ifdef state_tracking}
  2542. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  2543. var factval:Tnode;
  2544. begin
  2545. track_state_pass:=false;
  2546. if left.track_state_pass(exec_known) then
  2547. begin
  2548. track_state_pass:=true;
  2549. left.resultdef:=nil;
  2550. do_typecheckpass(left);
  2551. end;
  2552. factval:=aktstate.find_fact(left);
  2553. if factval<>nil then
  2554. begin
  2555. track_state_pass:=true;
  2556. left.destroy;
  2557. left:=factval.getcopy;
  2558. end;
  2559. if right.track_state_pass(exec_known) then
  2560. begin
  2561. track_state_pass:=true;
  2562. right.resultdef:=nil;
  2563. do_typecheckpass(right);
  2564. end;
  2565. factval:=aktstate.find_fact(right);
  2566. if factval<>nil then
  2567. begin
  2568. track_state_pass:=true;
  2569. right.destroy;
  2570. right:=factval.getcopy;
  2571. end;
  2572. end;
  2573. {$endif}
  2574. begin
  2575. caddnode:=taddnode;
  2576. end.