nadd.pas 111 KB

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