nadd.pas 110 KB

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