nadd.pas 115 KB

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