nadd.pas 110 KB

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