nadd.pas 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Type checking and register allocation 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. resultrealtype : ttype;
  26. constructor create(tt : tnodetype;l,r : tnode);override;
  27. function pass_1 : tnode;override;
  28. function det_resulttype:tnode;override;
  29. function simplify : tnode;override;
  30. {$ifdef state_tracking}
  31. function track_state_pass(exec_known:boolean):boolean;override;
  32. {$endif}
  33. protected
  34. { override the following if you want to implement }
  35. { parts explicitely in the code generator (JM) }
  36. function first_addstring: tnode; virtual;
  37. function first_addset: tnode; virtual;
  38. { only implements "muln" nodes, the rest always has to be done in }
  39. { the code generator for performance reasons (JM) }
  40. function first_add64bitint: tnode; virtual;
  41. { This routine calls internal runtime library helpers
  42. for all floating point arithmetic in the case
  43. where the emulation switches is on. Otherwise
  44. returns nil, and everything must be done in
  45. the code generation phase.
  46. }
  47. function first_addfloat : tnode; virtual;
  48. end;
  49. taddnodeclass = class of taddnode;
  50. var
  51. { caddnode is used to create nodes of the add type }
  52. { the virtual constructor allows to assign }
  53. { another class type to caddnode => processor }
  54. { specific node types can be created }
  55. caddnode : taddnodeclass;
  56. implementation
  57. uses
  58. {$IFNDEF MACOS_USE_FAKE_SYSUTILS}
  59. sysutils,
  60. {$ENDIF MACOS_USE_FAKE_SYSUTILS}
  61. globtype,systems,
  62. cutils,verbose,globals,widestr,
  63. symconst,symdef,symsym,symtable,defutil,defcmp,
  64. cgbase,
  65. htypechk,pass_1,
  66. nbas,nmat,ncnv,ncon,nset,nopt,ncal,ninl,nmem,nutils,
  67. {$ifdef state_tracking}
  68. nstate,
  69. {$endif}
  70. cpuinfo,procinfo;
  71. {*****************************************************************************
  72. TADDNODE
  73. *****************************************************************************}
  74. {$ifdef fpc}
  75. {$maxfpuregisters 0}
  76. {$endif fpc}
  77. function getbestreal(const t1,t2 : ttype) : ttype;
  78. const
  79. floatweight : array[tfloattype] of byte =
  80. (2,3,4,0,1,5);
  81. begin
  82. if t1.def.deftype=floatdef then
  83. begin
  84. result:=t1;
  85. if t2.def.deftype=floatdef then
  86. begin
  87. { when a comp or currency is used, use always the
  88. best float type to calculate the result }
  89. if (tfloatdef(t2.def).typ in [s64comp,s64currency]) or
  90. (tfloatdef(t2.def).typ in [s64comp,s64currency]) then
  91. result:=pbestrealtype^
  92. else
  93. if floatweight[tfloatdef(t2.def).typ]>floatweight[tfloatdef(t1.def).typ] then
  94. result:=t2;
  95. end;
  96. end
  97. else if t2.def.deftype=floatdef then
  98. result:=t2
  99. else internalerror(200508061);
  100. end;
  101. constructor taddnode.create(tt : tnodetype;l,r : tnode);
  102. begin
  103. inherited create(tt,l,r);
  104. end;
  105. function taddnode.simplify : tnode;
  106. var
  107. t : tnode;
  108. lt,rt : tnodetype;
  109. rd,ld : tdef;
  110. rv,lv : tconstexprint;
  111. rvd,lvd : bestreal;
  112. ws1,ws2 : pcompilerwidestring;
  113. concatstrings : boolean;
  114. c1,c2 : array[0..1] of char;
  115. s1,s2 : pchar;
  116. l1,l2 : longint;
  117. resultset : Tconstset;
  118. b : boolean;
  119. begin
  120. result:=nil;
  121. { is one a real float, then both need to be floats, this
  122. need to be done before the constant folding so constant
  123. operation on a float and int are also handled }
  124. resultrealtype:=pbestrealtype^;
  125. if (right.resulttype.def.deftype=floatdef) or (left.resulttype.def.deftype=floatdef) then
  126. begin
  127. { when both floattypes are already equal then use that
  128. floattype for results }
  129. if (right.resulttype.def.deftype=floatdef) and
  130. (left.resulttype.def.deftype=floatdef) and
  131. (tfloatdef(left.resulttype.def).typ=tfloatdef(right.resulttype.def).typ) then
  132. resultrealtype:=left.resulttype
  133. { when there is a currency type then use currency, but
  134. only when currency is defined as float }
  135. else
  136. if (is_currency(right.resulttype.def) or
  137. is_currency(left.resulttype.def)) and
  138. ((s64currencytype.def.deftype = floatdef) or
  139. (nodetype <> slashn)) then
  140. begin
  141. resultrealtype:=s64currencytype;
  142. inserttypeconv(right,resultrealtype);
  143. inserttypeconv(left,resultrealtype);
  144. end
  145. else
  146. begin
  147. resultrealtype:=getbestreal(left.resulttype,right.resulttype);
  148. inserttypeconv(right,resultrealtype);
  149. inserttypeconv(left,resultrealtype);
  150. end;
  151. end;
  152. { If both operands are constant and there is a widechar
  153. or widestring then convert everything to widestring. This
  154. allows constant folding like char+widechar }
  155. if is_constnode(right) and is_constnode(left) and
  156. (is_widestring(right.resulttype.def) or
  157. is_widestring(left.resulttype.def) or
  158. is_widechar(right.resulttype.def) or
  159. is_widechar(left.resulttype.def)) then
  160. begin
  161. inserttypeconv(right,cwidestringtype);
  162. inserttypeconv(left,cwidestringtype);
  163. end;
  164. { load easier access variables }
  165. rd:=right.resulttype.def;
  166. ld:=left.resulttype.def;
  167. rt:=right.nodetype;
  168. lt:=left.nodetype;
  169. if (nodetype = slashn) and
  170. (((rt = ordconstn) and
  171. (tordconstnode(right).value = 0)) or
  172. ((rt = realconstn) and
  173. (trealconstnode(right).value_real = 0.0))) then
  174. begin
  175. if (cs_check_range in aktlocalswitches) or
  176. (cs_check_overflow in aktlocalswitches) then
  177. begin
  178. result:=crealconstnode.create(1,pbestrealtype^);
  179. Message(parser_e_division_by_zero);
  180. exit;
  181. end;
  182. end;
  183. { both are int constants }
  184. if (
  185. (
  186. is_constintnode(left) and
  187. is_constintnode(right)
  188. ) or
  189. (
  190. is_constboolnode(left) and
  191. is_constboolnode(right) and
  192. (nodetype in [slashn,ltn,lten,gtn,gten,equaln,unequaln,andn,xorn,orn])
  193. ) or
  194. (
  195. is_constenumnode(left) and
  196. is_constenumnode(right) and
  197. allowenumop(nodetype))
  198. ) or
  199. (
  200. (lt = pointerconstn) and
  201. is_constintnode(right) and
  202. (nodetype in [addn,subn])
  203. ) or
  204. (
  205. (lt in [pointerconstn,niln]) and
  206. (rt in [pointerconstn,niln]) and
  207. (nodetype in [ltn,lten,gtn,gten,equaln,unequaln,subn])
  208. ) then
  209. begin
  210. t:=nil;
  211. { when comparing/substracting pointers, make sure they are }
  212. { of the same type (JM) }
  213. if (lt = pointerconstn) and (rt = pointerconstn) then
  214. begin
  215. if not(cs_extsyntax in aktmoduleswitches) and
  216. not(nodetype in [equaln,unequaln]) then
  217. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename)
  218. else
  219. if (nodetype <> subn) and
  220. is_voidpointer(rd) then
  221. inserttypeconv(right,left.resulttype)
  222. else if (nodetype <> subn) and
  223. is_voidpointer(ld) then
  224. inserttypeconv(left,right.resulttype)
  225. else if not(equal_defs(ld,rd)) then
  226. IncompatibleTypes(ld,rd);
  227. end
  228. else if (ld.deftype=enumdef) and (rd.deftype=enumdef) then
  229. begin
  230. if not(equal_defs(ld,rd)) then
  231. inserttypeconv(right,left.resulttype);
  232. end;
  233. { load values }
  234. case lt of
  235. ordconstn:
  236. lv:=tordconstnode(left).value;
  237. pointerconstn:
  238. lv:=tpointerconstnode(left).value;
  239. niln:
  240. lv:=0;
  241. else
  242. internalerror(2002080202);
  243. end;
  244. case rt of
  245. ordconstn:
  246. rv:=tordconstnode(right).value;
  247. pointerconstn:
  248. rv:=tpointerconstnode(right).value;
  249. niln:
  250. rv:=0;
  251. else
  252. internalerror(2002080203);
  253. end;
  254. if (lt = pointerconstn) and
  255. (rt <> pointerconstn) then
  256. rv := rv * tpointerdef(left.resulttype.def).pointertype.def.size;
  257. if (rt = pointerconstn) and
  258. (lt <> pointerconstn) then
  259. lv := lv * tpointerdef(right.resulttype.def).pointertype.def.size;
  260. case nodetype of
  261. addn :
  262. begin
  263. {$ifopt Q-}
  264. {$define OVERFLOW_OFF}
  265. {$Q+}
  266. {$endif}
  267. try
  268. if (lt=pointerconstn) then
  269. t := cpointerconstnode.create(lv+rv,left.resulttype)
  270. else
  271. if is_integer(ld) then
  272. t := genintconstnode(lv+rv)
  273. else
  274. t := cordconstnode.create(lv+rv,left.resulttype,(ld.deftype<>enumdef));
  275. except
  276. on E:EIntOverflow do
  277. begin
  278. Message(parser_e_arithmetic_operation_overflow);
  279. { Recover }
  280. t:=genintconstnode(0)
  281. end;
  282. end;
  283. {$ifdef OVERFLOW_OFF}
  284. {$Q-}
  285. {$undef OVERFLOW_OFF}
  286. {$endif}
  287. end;
  288. subn :
  289. begin
  290. {$ifopt Q-}
  291. {$define OVERFLOW_OFF}
  292. {$Q+}
  293. {$endif}
  294. try
  295. if (lt=pointerconstn) then
  296. begin
  297. { pointer-pointer results in an integer }
  298. if (rt=pointerconstn) then
  299. t := genintconstnode((lv-rv) div tpointerdef(ld).pointertype.def.size)
  300. else
  301. t := cpointerconstnode.create(lv-rv,left.resulttype);
  302. end
  303. else
  304. begin
  305. if is_integer(ld) then
  306. t:=genintconstnode(lv-rv)
  307. else
  308. t:=cordconstnode.create(lv-rv,left.resulttype,(ld.deftype<>enumdef));
  309. end;
  310. except
  311. on E:EIntOverflow do
  312. begin
  313. Message(parser_e_arithmetic_operation_overflow);
  314. { Recover }
  315. t:=genintconstnode(0)
  316. end;
  317. end;
  318. {$ifdef OVERFLOW_OFF}
  319. {$Q-}
  320. {$undef OVERFLOW_OFF}
  321. {$endif}
  322. end;
  323. muln :
  324. begin
  325. {$ifopt Q-}
  326. {$define OVERFLOW_OFF}
  327. {$Q+}
  328. {$endif}
  329. try
  330. if (torddef(ld).typ <> u64bit) or
  331. (torddef(rd).typ <> u64bit) then
  332. t:=genintconstnode(lv*rv)
  333. else
  334. t:=genintconstnode(int64(qword(lv)*qword(rv)));
  335. except
  336. on E:EIntOverflow do
  337. begin
  338. Message(parser_e_arithmetic_operation_overflow);
  339. { Recover }
  340. t:=genintconstnode(0)
  341. end;
  342. end;
  343. {$ifdef OVERFLOW_OFF}
  344. {$Q-}
  345. {$undef OVERFLOW_OFF}
  346. {$endif}
  347. end;
  348. xorn :
  349. if is_integer(ld) then
  350. t:=genintconstnode(lv xor rv)
  351. else
  352. t:=cordconstnode.create(lv xor rv,left.resulttype,true);
  353. orn :
  354. if is_integer(ld) then
  355. t:=genintconstnode(lv or rv)
  356. else
  357. t:=cordconstnode.create(lv or rv,left.resulttype,true);
  358. andn :
  359. if is_integer(ld) then
  360. t:=genintconstnode(lv and rv)
  361. else
  362. t:=cordconstnode.create(lv and rv,left.resulttype,true);
  363. ltn :
  364. t:=cordconstnode.create(ord(lv<rv),booltype,true);
  365. lten :
  366. t:=cordconstnode.create(ord(lv<=rv),booltype,true);
  367. gtn :
  368. t:=cordconstnode.create(ord(lv>rv),booltype,true);
  369. gten :
  370. t:=cordconstnode.create(ord(lv>=rv),booltype,true);
  371. equaln :
  372. t:=cordconstnode.create(ord(lv=rv),booltype,true);
  373. unequaln :
  374. t:=cordconstnode.create(ord(lv<>rv),booltype,true);
  375. slashn :
  376. begin
  377. { int/int becomes a real }
  378. rvd:=rv;
  379. lvd:=lv;
  380. t:=crealconstnode.create(lvd/rvd,resultrealtype);
  381. end;
  382. else
  383. begin
  384. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  385. t:=cnothingnode.create;
  386. end;
  387. end;
  388. result:=t;
  389. exit;
  390. end;
  391. { both real constants ? }
  392. if (lt=realconstn) and (rt=realconstn) then
  393. begin
  394. lvd:=trealconstnode(left).value_real;
  395. rvd:=trealconstnode(right).value_real;
  396. case nodetype of
  397. addn :
  398. t:=crealconstnode.create(lvd+rvd,resultrealtype);
  399. subn :
  400. t:=crealconstnode.create(lvd-rvd,resultrealtype);
  401. muln :
  402. t:=crealconstnode.create(lvd*rvd,resultrealtype);
  403. starstarn,
  404. caretn :
  405. begin
  406. if lvd<0 then
  407. begin
  408. Message(parser_e_invalid_float_operation);
  409. t:=crealconstnode.create(0,resultrealtype);
  410. end
  411. else if lvd=0 then
  412. t:=crealconstnode.create(1.0,resultrealtype)
  413. else
  414. t:=crealconstnode.create(exp(ln(lvd)*rvd),resultrealtype);
  415. end;
  416. slashn :
  417. t:=crealconstnode.create(lvd/rvd,resultrealtype);
  418. ltn :
  419. t:=cordconstnode.create(ord(lvd<rvd),booltype,true);
  420. lten :
  421. t:=cordconstnode.create(ord(lvd<=rvd),booltype,true);
  422. gtn :
  423. t:=cordconstnode.create(ord(lvd>rvd),booltype,true);
  424. gten :
  425. t:=cordconstnode.create(ord(lvd>=rvd),booltype,true);
  426. equaln :
  427. t:=cordconstnode.create(ord(lvd=rvd),booltype,true);
  428. unequaln :
  429. t:=cordconstnode.create(ord(lvd<>rvd),booltype,true);
  430. else
  431. begin
  432. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  433. t:=cnothingnode.create;
  434. end;
  435. end;
  436. result:=t;
  437. exit;
  438. end;
  439. { first, we handle widestrings, so we can check later for }
  440. { stringconstn only }
  441. { widechars are converted above to widestrings too }
  442. { this isn't veryy efficient, but I don't think }
  443. { that it does matter that much (FK) }
  444. if (lt=stringconstn) and (rt=stringconstn) and
  445. (tstringconstnode(left).st_type=st_widestring) and
  446. (tstringconstnode(right).st_type=st_widestring) then
  447. begin
  448. initwidestring(ws1);
  449. initwidestring(ws2);
  450. copywidestring(pcompilerwidestring(tstringconstnode(left).value_str),ws1);
  451. copywidestring(pcompilerwidestring(tstringconstnode(right).value_str),ws2);
  452. case nodetype of
  453. addn :
  454. begin
  455. concatwidestrings(ws1,ws2);
  456. t:=cstringconstnode.createwstr(ws1);
  457. end;
  458. ltn :
  459. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<0),booltype,true);
  460. lten :
  461. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<=0),booltype,true);
  462. gtn :
  463. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)>0),booltype,true);
  464. gten :
  465. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)>=0),booltype,true);
  466. equaln :
  467. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)=0),booltype,true);
  468. unequaln :
  469. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<>0),booltype,true);
  470. else
  471. begin
  472. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  473. t:=cnothingnode.create;
  474. end;
  475. end;
  476. donewidestring(ws1);
  477. donewidestring(ws2);
  478. result:=t;
  479. exit;
  480. end;
  481. { concating strings ? }
  482. concatstrings:=false;
  483. if (lt=ordconstn) and (rt=ordconstn) and
  484. is_char(ld) and is_char(rd) then
  485. begin
  486. c1[0]:=char(byte(tordconstnode(left).value));
  487. c1[1]:=#0;
  488. l1:=1;
  489. c2[0]:=char(byte(tordconstnode(right).value));
  490. c2[1]:=#0;
  491. l2:=1;
  492. s1:=@c1;
  493. s2:=@c2;
  494. concatstrings:=true;
  495. end
  496. else if (lt=stringconstn) and (rt=ordconstn) and is_char(rd) then
  497. begin
  498. s1:=tstringconstnode(left).value_str;
  499. l1:=tstringconstnode(left).len;
  500. c2[0]:=char(byte(tordconstnode(right).value));
  501. c2[1]:=#0;
  502. s2:=@c2;
  503. l2:=1;
  504. concatstrings:=true;
  505. end
  506. else if (lt=ordconstn) and (rt=stringconstn) and is_char(ld) then
  507. begin
  508. c1[0]:=char(byte(tordconstnode(left).value));
  509. c1[1]:=#0;
  510. l1:=1;
  511. s1:=@c1;
  512. s2:=tstringconstnode(right).value_str;
  513. l2:=tstringconstnode(right).len;
  514. concatstrings:=true;
  515. end
  516. else if (lt=stringconstn) and (rt=stringconstn) then
  517. begin
  518. s1:=tstringconstnode(left).value_str;
  519. l1:=tstringconstnode(left).len;
  520. s2:=tstringconstnode(right).value_str;
  521. l2:=tstringconstnode(right).len;
  522. concatstrings:=true;
  523. end;
  524. if concatstrings then
  525. begin
  526. case nodetype of
  527. addn :
  528. t:=cstringconstnode.createpchar(concatansistrings(s1,s2,l1,l2),l1+l2);
  529. ltn :
  530. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<0),booltype,true);
  531. lten :
  532. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<=0),booltype,true);
  533. gtn :
  534. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)>0),booltype,true);
  535. gten :
  536. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)>=0),booltype,true);
  537. equaln :
  538. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)=0),booltype,true);
  539. unequaln :
  540. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<>0),booltype,true);
  541. else
  542. begin
  543. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  544. t:=cnothingnode.create;
  545. end;
  546. end;
  547. result:=t;
  548. exit;
  549. end;
  550. { set constant evaluation }
  551. if (right.nodetype=setconstn) and
  552. not assigned(tsetconstnode(right).left) and
  553. (left.nodetype=setconstn) and
  554. not assigned(tsetconstnode(left).left) then
  555. begin
  556. { check if size adjusting is needed, only for left
  557. to right as the other way is checked in the typeconv }
  558. if (tsetdef(right.resulttype.def).settype=smallset) and
  559. (tsetdef(left.resulttype.def).settype<>smallset) then
  560. right.resulttype.setdef(tsetdef.create(tsetdef(right.resulttype.def).elementtype,255));
  561. { check base types }
  562. inserttypeconv(left,right.resulttype);
  563. if codegenerror then
  564. begin
  565. { recover by only returning the left part }
  566. result:=left;
  567. left:=nil;
  568. exit;
  569. end;
  570. case nodetype of
  571. addn :
  572. begin
  573. resultset:=tsetconstnode(right).value_set^ + tsetconstnode(left).value_set^;
  574. t:=csetconstnode.create(@resultset,left.resulttype);
  575. end;
  576. muln :
  577. begin
  578. resultset:=tsetconstnode(right).value_set^ * tsetconstnode(left).value_set^;
  579. t:=csetconstnode.create(@resultset,left.resulttype);
  580. end;
  581. subn :
  582. begin
  583. resultset:=tsetconstnode(left).value_set^ - tsetconstnode(right).value_set^;
  584. t:=csetconstnode.create(@resultset,left.resulttype);
  585. end;
  586. symdifn :
  587. begin
  588. resultset:=tsetconstnode(right).value_set^ >< tsetconstnode(left).value_set^;
  589. t:=csetconstnode.create(@resultset,left.resulttype);
  590. end;
  591. unequaln :
  592. begin
  593. b:=tsetconstnode(right).value_set^ <> tsetconstnode(left).value_set^;
  594. t:=cordconstnode.create(byte(b),booltype,true);
  595. end;
  596. equaln :
  597. begin
  598. b:=tsetconstnode(right).value_set^ = tsetconstnode(left).value_set^;
  599. t:=cordconstnode.create(byte(b),booltype,true);
  600. end;
  601. lten :
  602. begin
  603. b:=tsetconstnode(left).value_set^ <= tsetconstnode(right).value_set^;
  604. t:=cordconstnode.create(byte(b),booltype,true);
  605. end;
  606. gten :
  607. begin
  608. b:=tsetconstnode(left).value_set^ >= tsetconstnode(right).value_set^;
  609. t:=cordconstnode.create(byte(b),booltype,true);
  610. end;
  611. else
  612. begin
  613. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  614. t:=cnothingnode.create;
  615. end;
  616. end;
  617. result:=t;
  618. exit;
  619. end;
  620. end;
  621. function taddnode.det_resulttype:tnode;
  622. var
  623. hp : tnode;
  624. lt,rt : tnodetype;
  625. rd,ld : tdef;
  626. htype : ttype;
  627. ot : tnodetype;
  628. hsym : tfieldvarsym;
  629. i : longint;
  630. strtype : tstringtype;
  631. b : boolean;
  632. {$ifdef state_tracking}
  633. factval : Tnode;
  634. change : boolean;
  635. {$endif}
  636. begin
  637. result:=nil;
  638. { first do the two subtrees }
  639. resulttypepass(left);
  640. resulttypepass(right);
  641. { both left and right need to be valid }
  642. set_varstate(left,vs_used,[vsf_must_be_valid]);
  643. set_varstate(right,vs_used,[vsf_must_be_valid]);
  644. if codegenerror then
  645. exit;
  646. { tp procvar support }
  647. maybe_call_procvar(left,true);
  648. maybe_call_procvar(right,true);
  649. { convert array constructors to sets, because there is no other operator
  650. possible for array constructors }
  651. if is_array_constructor(left.resulttype.def) then
  652. begin
  653. arrayconstructor_to_set(left);
  654. resulttypepass(left);
  655. end;
  656. if is_array_constructor(right.resulttype.def) then
  657. begin
  658. arrayconstructor_to_set(right);
  659. resulttypepass(right);
  660. end;
  661. { allow operator overloading }
  662. hp:=self;
  663. if isbinaryoverloaded(hp) then
  664. begin
  665. result:=hp;
  666. exit;
  667. end;
  668. { Stop checking when an error was found in the operator checking }
  669. if codegenerror then
  670. begin
  671. result:=cerrornode.create;
  672. exit;
  673. end;
  674. { Kylix allows enum+ordconstn in an enum declaration (blocktype
  675. is bt_type), we need to do the conversion here before the
  676. constant folding }
  677. if (m_delphi in aktmodeswitches) and
  678. (blocktype=bt_type) then
  679. begin
  680. if (left.resulttype.def.deftype=enumdef) and
  681. (right.resulttype.def.deftype=orddef) then
  682. begin
  683. { insert explicit typecast to default signed int }
  684. left:=ctypeconvnode.create_internal(left,sinttype);
  685. resulttypepass(left);
  686. end
  687. else
  688. if (left.resulttype.def.deftype=orddef) and
  689. (right.resulttype.def.deftype=enumdef) then
  690. begin
  691. { insert explicit typecast to default signed int }
  692. right:=ctypeconvnode.create_internal(right,sinttype);
  693. resulttypepass(right);
  694. end;
  695. end;
  696. result:=simplify;
  697. if assigned(result) then
  698. exit;
  699. { load easier access variables }
  700. rd:=right.resulttype.def;
  701. ld:=left.resulttype.def;
  702. rt:=right.nodetype;
  703. lt:=left.nodetype;
  704. { but an int/int gives real/real! }
  705. if nodetype=slashn then
  706. begin
  707. if is_currency(left.resulttype.def) and
  708. is_currency(right.resulttype.def) then
  709. { In case of currency, converting to float means dividing by 10000 }
  710. { However, since this is already a division, both divisions by }
  711. { 10000 are eliminated when we divide the results -> we can skip }
  712. { them. }
  713. if s64currencytype.def.deftype = floatdef then
  714. begin
  715. { there's no s64comptype or so, how do we avoid the type conversion?
  716. left.resulttype := s64comptype;
  717. right.resulttype := s64comptype; }
  718. end
  719. else
  720. begin
  721. left.resulttype := s64inttype;
  722. right.resulttype := s64inttype;
  723. end
  724. else if (left.resulttype.def.deftype <> floatdef) and
  725. (right.resulttype.def.deftype <> floatdef) then
  726. CGMessage(type_h_use_div_for_int);
  727. inserttypeconv(right,resultrealtype);
  728. inserttypeconv(left,resultrealtype);
  729. end
  730. { if both are orddefs then check sub types }
  731. else if (ld.deftype=orddef) and (rd.deftype=orddef) then
  732. begin
  733. { optimize multiplacation by a power of 2 }
  734. if not(cs_check_overflow in aktlocalswitches) and
  735. (nodetype = muln) and
  736. (((left.nodetype = ordconstn) and
  737. ispowerof2(tordconstnode(left).value,i)) or
  738. ((right.nodetype = ordconstn) and
  739. ispowerof2(tordconstnode(right).value,i))) then
  740. begin
  741. if left.nodetype = ordconstn then
  742. begin
  743. tordconstnode(left).value := i;
  744. result := cshlshrnode.create(shln,right,left);
  745. end
  746. else
  747. begin
  748. tordconstnode(right).value := i;
  749. result := cshlshrnode.create(shln,left,right);
  750. end;
  751. left := nil;
  752. right := nil;
  753. exit;
  754. end;
  755. { 2 booleans? Make them equal to the largest boolean }
  756. if is_boolean(ld) and is_boolean(rd) then
  757. begin
  758. if torddef(left.resulttype.def).size>torddef(right.resulttype.def).size then
  759. begin
  760. right:=ctypeconvnode.create_internal(right,left.resulttype);
  761. ttypeconvnode(right).convtype:=tc_bool_2_int;
  762. resulttypepass(right);
  763. end
  764. else if torddef(left.resulttype.def).size<torddef(right.resulttype.def).size then
  765. begin
  766. left:=ctypeconvnode.create_internal(left,right.resulttype);
  767. ttypeconvnode(left).convtype:=tc_bool_2_int;
  768. resulttypepass(left);
  769. end;
  770. case nodetype of
  771. xorn,
  772. ltn,
  773. lten,
  774. gtn,
  775. gten,
  776. andn,
  777. orn:
  778. begin
  779. end;
  780. unequaln,
  781. equaln:
  782. begin
  783. if not(cs_full_boolean_eval in aktlocalswitches) then
  784. begin
  785. { Remove any compares with constants }
  786. if (left.nodetype=ordconstn) then
  787. begin
  788. hp:=right;
  789. b:=(tordconstnode(left).value<>0);
  790. ot:=nodetype;
  791. left.free;
  792. left:=nil;
  793. right:=nil;
  794. if (not(b) and (ot=equaln)) or
  795. (b and (ot=unequaln)) then
  796. begin
  797. hp:=cnotnode.create(hp);
  798. end;
  799. result:=hp;
  800. exit;
  801. end;
  802. if (right.nodetype=ordconstn) then
  803. begin
  804. hp:=left;
  805. b:=(tordconstnode(right).value<>0);
  806. ot:=nodetype;
  807. right.free;
  808. right:=nil;
  809. left:=nil;
  810. if (not(b) and (ot=equaln)) or
  811. (b and (ot=unequaln)) then
  812. begin
  813. hp:=cnotnode.create(hp);
  814. end;
  815. result:=hp;
  816. exit;
  817. end;
  818. end;
  819. end;
  820. else
  821. begin
  822. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  823. result:=cnothingnode.create;
  824. exit;
  825. end;
  826. end;
  827. end
  828. { Both are chars? }
  829. else if is_char(rd) and is_char(ld) then
  830. begin
  831. if nodetype=addn then
  832. begin
  833. resulttype:=cshortstringtype;
  834. if not(is_constcharnode(left) and is_constcharnode(right)) then
  835. begin
  836. inserttypeconv(left,cshortstringtype);
  837. {$ifdef addstringopt}
  838. hp := genaddsstringcharoptnode(self);
  839. result := hp;
  840. exit;
  841. {$endif addstringopt}
  842. end;
  843. end;
  844. end
  845. { There is a widechar? }
  846. else if is_widechar(rd) or is_widechar(ld) then
  847. begin
  848. { widechar+widechar gives widestring }
  849. if nodetype=addn then
  850. begin
  851. inserttypeconv(left,cwidestringtype);
  852. if (torddef(rd).typ<>uwidechar) then
  853. inserttypeconv(right,cwidechartype);
  854. resulttype:=cwidestringtype;
  855. end
  856. else
  857. begin
  858. if (torddef(ld).typ<>uwidechar) then
  859. inserttypeconv(left,cwidechartype);
  860. if (torddef(rd).typ<>uwidechar) then
  861. inserttypeconv(right,cwidechartype);
  862. end;
  863. end
  864. { is there a currency type ? }
  865. else if ((torddef(rd).typ=scurrency) or (torddef(ld).typ=scurrency)) then
  866. begin
  867. if (torddef(ld).typ<>scurrency) then
  868. inserttypeconv(left,s64currencytype);
  869. if (torddef(rd).typ<>scurrency) then
  870. inserttypeconv(right,s64currencytype);
  871. end
  872. { and,or,xor work on bit patterns and don't care
  873. about the sign of integers }
  874. else if (nodetype in [andn,orn,xorn]) and
  875. is_integer(ld) and is_integer(rd) then
  876. begin
  877. if rd.size>ld.size then
  878. inserttypeconv_internal(left,right.resulttype)
  879. else
  880. inserttypeconv_internal(right,left.resulttype);
  881. end
  882. { is there a signed 64 bit type ? }
  883. else if ((torddef(rd).typ=s64bit) or (torddef(ld).typ=s64bit)) then
  884. begin
  885. if (torddef(ld).typ<>s64bit) then
  886. inserttypeconv(left,s64inttype);
  887. if (torddef(rd).typ<>s64bit) then
  888. inserttypeconv(right,s64inttype);
  889. end
  890. { is there a unsigned 64 bit type ? }
  891. else if ((torddef(rd).typ=u64bit) or (torddef(ld).typ=u64bit)) then
  892. begin
  893. if (torddef(ld).typ<>u64bit) then
  894. inserttypeconv(left,u64inttype);
  895. if (torddef(rd).typ<>u64bit) then
  896. inserttypeconv(right,u64inttype);
  897. end
  898. { 64 bit cpus do calculations always in 64 bit }
  899. {$ifndef cpu64bit}
  900. { is there a cardinal? }
  901. else if ((torddef(rd).typ=u32bit) or (torddef(ld).typ=u32bit)) then
  902. begin
  903. { convert positive constants to u32bit }
  904. if (torddef(ld).typ<>u32bit) and
  905. is_constintnode(left) and
  906. (tordconstnode(left).value >= 0) then
  907. inserttypeconv(left,u32inttype);
  908. if (torddef(rd).typ<>u32bit) and
  909. is_constintnode(right) and
  910. (tordconstnode(right).value >= 0) then
  911. inserttypeconv(right,u32inttype);
  912. { when one of the operand is signed perform
  913. the operation in 64bit, can't use rd/ld here because there
  914. could be already typeconvs inserted }
  915. if is_signed(left.resulttype.def) or
  916. is_signed(right.resulttype.def) then
  917. begin
  918. CGMessage(type_w_mixed_signed_unsigned);
  919. inserttypeconv(left,s64inttype);
  920. inserttypeconv(right,s64inttype);
  921. end
  922. else
  923. begin
  924. if (torddef(left.resulttype.def).typ<>u32bit) then
  925. inserttypeconv(left,u32inttype);
  926. if (torddef(right.resulttype.def).typ<>u32bit) then
  927. inserttypeconv(right,u32inttype);
  928. end;
  929. end
  930. {$endif cpu64bit}
  931. { generic ord conversion is sinttype }
  932. else
  933. begin
  934. { if the left or right value is smaller than the normal
  935. type sinttype and is unsigned, and the other value
  936. is a constant < 0, the result will always be false/true
  937. for equal / unequal nodes.
  938. }
  939. if (
  940. { left : unsigned ordinal var, right : < 0 constant }
  941. (
  942. ((is_signed(ld)=false) and (is_constintnode(left) =false)) and
  943. ((is_constintnode(right)) and (tordconstnode(right).value < 0))
  944. ) or
  945. { right : unsigned ordinal var, left : < 0 constant }
  946. (
  947. ((is_signed(rd)=false) and (is_constintnode(right) =false)) and
  948. ((is_constintnode(left)) and (tordconstnode(left).value < 0))
  949. )
  950. ) then
  951. begin
  952. if nodetype = equaln then
  953. CGMessage(type_w_signed_unsigned_always_false)
  954. else
  955. if nodetype = unequaln then
  956. CGMessage(type_w_signed_unsigned_always_true)
  957. else
  958. if (is_constintnode(left) and (nodetype in [ltn,lten])) or
  959. (is_constintnode(right) and (nodetype in [gtn,gten])) then
  960. CGMessage(type_w_signed_unsigned_always_true)
  961. else
  962. if (is_constintnode(right) and (nodetype in [ltn,lten])) or
  963. (is_constintnode(left) and (nodetype in [gtn,gten])) then
  964. CGMessage(type_w_signed_unsigned_always_false);
  965. end;
  966. { When there is a signed type or there is a minus operation
  967. we convert to signed int. Otherwise (both are unsigned) we keep
  968. the result also unsigned. This is compatible with Delphi (PFV) }
  969. if is_signed(ld) or
  970. is_signed(rd) or
  971. (nodetype=subn) then
  972. begin
  973. inserttypeconv(right,sinttype);
  974. inserttypeconv(left,sinttype);
  975. end
  976. else
  977. begin
  978. inserttypeconv(right,uinttype);
  979. inserttypeconv(left,uinttype);
  980. end;
  981. end;
  982. end
  983. { if both are floatdefs, conversion is already done before constant folding }
  984. else if (ld.deftype=floatdef) then
  985. begin
  986. { already converted }
  987. end
  988. { left side a setdef, must be before string processing,
  989. else array constructor can be seen as array of char (PFV) }
  990. else if (ld.deftype=setdef) then
  991. begin
  992. { trying to add a set element? }
  993. if (nodetype=addn) and (rd.deftype<>setdef) then
  994. begin
  995. if (rt=setelementn) then
  996. begin
  997. if not(equal_defs(tsetdef(ld).elementtype.def,rd)) then
  998. CGMessage(type_e_set_element_are_not_comp);
  999. end
  1000. else
  1001. CGMessage(type_e_mismatch)
  1002. end
  1003. else
  1004. begin
  1005. if not(nodetype in [addn,subn,symdifn,muln,equaln,unequaln,lten,gten]) then
  1006. CGMessage(type_e_set_operation_unknown);
  1007. { right def must be a also be set }
  1008. if (rd.deftype<>setdef) or not(equal_defs(rd,ld)) then
  1009. CGMessage(type_e_set_element_are_not_comp);
  1010. end;
  1011. { ranges require normsets }
  1012. if (tsetdef(ld).settype=smallset) and
  1013. (rt=setelementn) and
  1014. assigned(tsetelementnode(right).right) then
  1015. begin
  1016. { generate a temporary normset def, it'll be destroyed
  1017. when the symtable is unloaded }
  1018. htype.setdef(tsetdef.create(tsetdef(ld).elementtype,255));
  1019. inserttypeconv(left,htype);
  1020. end;
  1021. { if the right side is also a setdef then the settype must
  1022. be the same as the left setdef }
  1023. if (rd.deftype=setdef) and
  1024. (tsetdef(ld).settype<>tsetdef(rd).settype) then
  1025. begin
  1026. { when right is a normset we need to typecast both
  1027. to normsets }
  1028. if (tsetdef(rd).settype=normset) then
  1029. inserttypeconv(left,right.resulttype)
  1030. else
  1031. inserttypeconv(right,left.resulttype);
  1032. end;
  1033. end
  1034. { pointer comparision and subtraction }
  1035. else if ((rd.deftype=pointerdef) and (ld.deftype=pointerdef)) or
  1036. { compare pchar to char arrays by addresses like BP/Delphi }
  1037. ((is_pchar(ld) or (lt=niln)) and is_chararray(rd)) or
  1038. ((is_pchar(rd) or (rt=niln)) and is_chararray(ld)) then
  1039. begin
  1040. { convert char array to pointer }
  1041. if is_chararray(rd) then
  1042. begin
  1043. inserttypeconv(right,charpointertype);
  1044. rd:=right.resulttype.def;
  1045. end
  1046. else if is_chararray(ld) then
  1047. begin
  1048. inserttypeconv(left,charpointertype);
  1049. ld:=left.resulttype.def;
  1050. end;
  1051. case nodetype of
  1052. equaln,unequaln :
  1053. begin
  1054. if is_voidpointer(right.resulttype.def) then
  1055. inserttypeconv(right,left.resulttype)
  1056. else if is_voidpointer(left.resulttype.def) then
  1057. inserttypeconv(left,right.resulttype)
  1058. else if not(equal_defs(ld,rd)) then
  1059. IncompatibleTypes(ld,rd);
  1060. { now that the type checking is done, convert both to charpointer, }
  1061. { because methodpointers are 8 bytes even though only the first 4 }
  1062. { bytes must be compared. This can happen here if we are in }
  1063. { TP/Delphi mode, because there @methodpointer = voidpointer (but }
  1064. { a voidpointer of 8 bytes). A conversion to voidpointer would be }
  1065. { optimized away, since the result already was a voidpointer, so }
  1066. { use a charpointer instead (JM) }
  1067. inserttypeconv_internal(left,charpointertype);
  1068. inserttypeconv_internal(right,charpointertype);
  1069. end;
  1070. ltn,lten,gtn,gten:
  1071. begin
  1072. if (cs_extsyntax in aktmoduleswitches) then
  1073. begin
  1074. if is_voidpointer(right.resulttype.def) then
  1075. inserttypeconv(right,left.resulttype)
  1076. else if is_voidpointer(left.resulttype.def) then
  1077. inserttypeconv(left,right.resulttype)
  1078. else if not(equal_defs(ld,rd)) then
  1079. IncompatibleTypes(ld,rd);
  1080. end
  1081. else
  1082. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1083. end;
  1084. subn:
  1085. begin
  1086. if (cs_extsyntax in aktmoduleswitches) then
  1087. begin
  1088. if is_voidpointer(right.resulttype.def) then
  1089. inserttypeconv(right,left.resulttype)
  1090. else if is_voidpointer(left.resulttype.def) then
  1091. inserttypeconv(left,right.resulttype)
  1092. else if not(equal_defs(ld,rd)) then
  1093. IncompatibleTypes(ld,rd);
  1094. end
  1095. else
  1096. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1097. if not(nf_has_pointerdiv in flags) and
  1098. (tpointerdef(rd).pointertype.def.size>1) then
  1099. begin
  1100. hp:=getcopy;
  1101. include(hp.flags,nf_has_pointerdiv);
  1102. result:=cmoddivnode.create(divn,hp,cordconstnode.create(tpointerdef(rd).pointertype.def.size,sinttype,false));
  1103. end;
  1104. resulttype:=sinttype;
  1105. exit;
  1106. end;
  1107. addn:
  1108. begin
  1109. if (cs_extsyntax in aktmoduleswitches) then
  1110. begin
  1111. if is_voidpointer(right.resulttype.def) then
  1112. inserttypeconv(right,left.resulttype)
  1113. else if is_voidpointer(left.resulttype.def) then
  1114. inserttypeconv(left,right.resulttype)
  1115. else if not(equal_defs(ld,rd)) then
  1116. IncompatibleTypes(ld,rd);
  1117. end
  1118. else
  1119. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1120. resulttype:=sinttype;
  1121. exit;
  1122. end;
  1123. else
  1124. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1125. end;
  1126. end
  1127. { is one of the operands a string?,
  1128. chararrays are also handled as strings (after conversion), also take
  1129. care of chararray+chararray and chararray+char.
  1130. Note: Must be done after pointerdef+pointerdef has been checked, else
  1131. pchar is converted to string }
  1132. else if (rd.deftype=stringdef) or
  1133. (ld.deftype=stringdef) or
  1134. ((is_pchar(rd) or is_chararray(rd) or is_char(rd) or is_open_chararray(rd) or
  1135. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd)) and
  1136. (is_pchar(ld) or is_chararray(ld) or is_char(ld) or is_open_chararray(ld) or
  1137. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld))) then
  1138. begin
  1139. if (nodetype in [addn,equaln,unequaln,lten,gten,ltn,gtn]) then
  1140. begin
  1141. { Is there a widestring? }
  1142. if is_widestring(rd) or is_widestring(ld) or
  1143. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd) or
  1144. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld) then
  1145. strtype:= st_widestring
  1146. else
  1147. if is_ansistring(rd) or is_ansistring(ld) or
  1148. ((cs_ansistrings in aktlocalswitches) and
  1149. //todo: Move some of this to longstring's then they are implemented?
  1150. (
  1151. is_pchar(rd) or (is_chararray(rd) and (rd.size > 255)) or is_open_chararray(rd) or
  1152. is_pchar(ld) or (is_chararray(ld) and (ld.size > 255)) or is_open_chararray(ld)
  1153. )
  1154. ) then
  1155. strtype:= st_ansistring
  1156. else
  1157. if is_longstring(rd) or is_longstring(ld) then
  1158. strtype:= st_longstring
  1159. else
  1160. begin
  1161. {$warning todo: add a warning/hint here if one converting a too large array}
  1162. { nodes is PChar, array [with size > 255] or OpenArrayOfChar.
  1163. Note: Delphi halts with error if "array [0..xx] of char"
  1164. is assigned to ShortString and string length is less
  1165. then array size }
  1166. strtype:= st_shortstring;
  1167. end;
  1168. // Now convert nodes to common string type
  1169. case strtype of
  1170. st_widestring :
  1171. begin
  1172. if not(is_widestring(rd)) then
  1173. inserttypeconv(right,cwidestringtype);
  1174. if not(is_widestring(ld)) then
  1175. inserttypeconv(left,cwidestringtype);
  1176. end;
  1177. st_ansistring :
  1178. begin
  1179. if not(is_ansistring(rd)) then
  1180. inserttypeconv(right,cansistringtype);
  1181. if not(is_ansistring(ld)) then
  1182. inserttypeconv(left,cansistringtype);
  1183. end;
  1184. st_longstring :
  1185. begin
  1186. if not(is_longstring(rd)) then
  1187. inserttypeconv(right,clongstringtype);
  1188. if not(is_longstring(ld)) then
  1189. inserttypeconv(left,clongstringtype);
  1190. end;
  1191. st_shortstring :
  1192. begin
  1193. if not(is_shortstring(ld)) then
  1194. inserttypeconv(left,cshortstringtype);
  1195. { don't convert char, that can be handled by the optimized node }
  1196. if not(is_shortstring(rd) or is_char(rd)) then
  1197. inserttypeconv(right,cshortstringtype);
  1198. end;
  1199. else
  1200. internalerror(2005101);
  1201. end;
  1202. end
  1203. else
  1204. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1205. end
  1206. { class or interface equation }
  1207. else if is_class_or_interface(rd) or is_class_or_interface(ld) then
  1208. begin
  1209. if (nodetype in [equaln,unequaln]) then
  1210. begin
  1211. if is_class_or_interface(rd) and is_class_or_interface(ld) then
  1212. begin
  1213. if tobjectdef(rd).is_related(tobjectdef(ld)) then
  1214. inserttypeconv(right,left.resulttype)
  1215. else
  1216. inserttypeconv(left,right.resulttype);
  1217. end
  1218. else if is_class_or_interface(rd) then
  1219. inserttypeconv(left,right.resulttype)
  1220. else
  1221. inserttypeconv(right,left.resulttype);
  1222. end
  1223. else
  1224. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1225. end
  1226. else if (rd.deftype=classrefdef) and (ld.deftype=classrefdef) then
  1227. begin
  1228. if (nodetype in [equaln,unequaln]) then
  1229. begin
  1230. if tobjectdef(tclassrefdef(rd).pointertype.def).is_related(
  1231. tobjectdef(tclassrefdef(ld).pointertype.def)) then
  1232. inserttypeconv(right,left.resulttype)
  1233. else
  1234. inserttypeconv(left,right.resulttype);
  1235. end
  1236. else
  1237. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1238. end
  1239. { allows comperasion with nil pointer }
  1240. else if is_class_or_interface(rd) or (rd.deftype=classrefdef) then
  1241. begin
  1242. if (nodetype in [equaln,unequaln]) then
  1243. inserttypeconv(left,right.resulttype)
  1244. else
  1245. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1246. end
  1247. else if is_class_or_interface(ld) or (ld.deftype=classrefdef) then
  1248. begin
  1249. if (nodetype in [equaln,unequaln]) then
  1250. inserttypeconv(right,left.resulttype)
  1251. else
  1252. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1253. end
  1254. { support procvar=nil,procvar<>nil }
  1255. else if ((ld.deftype=procvardef) and (rt=niln)) or
  1256. ((rd.deftype=procvardef) and (lt=niln)) then
  1257. begin
  1258. if not(nodetype in [equaln,unequaln]) then
  1259. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1260. { find proc field in methodpointer record }
  1261. hsym:=tfieldvarsym(trecorddef(methodpointertype.def).symtable.search('proc'));
  1262. if not assigned(hsym) then
  1263. internalerror(200412043);
  1264. { For methodpointers compare only tmethodpointer.proc }
  1265. if (rd.deftype=procvardef) and
  1266. (not tprocvardef(rd).is_addressonly) then
  1267. begin
  1268. right:=csubscriptnode.create(
  1269. hsym,
  1270. ctypeconvnode.create_internal(right,methodpointertype));
  1271. end;
  1272. if (ld.deftype=procvardef) and
  1273. (not tprocvardef(ld).is_addressonly) then
  1274. begin
  1275. left:=csubscriptnode.create(
  1276. hsym,
  1277. ctypeconvnode.create_internal(left,methodpointertype));
  1278. end;
  1279. end
  1280. { support dynamicarray=nil,dynamicarray<>nil }
  1281. else if (is_dynamic_array(ld) and (rt=niln)) or
  1282. (is_dynamic_array(rd) and (lt=niln)) or
  1283. (is_dynamic_array(ld) and is_dynamic_array(rd)) then
  1284. begin
  1285. if not(nodetype in [equaln,unequaln]) then
  1286. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1287. end
  1288. {$ifdef SUPPORT_MMX}
  1289. { mmx support, this must be before the zero based array
  1290. check }
  1291. else if (cs_mmx in aktlocalswitches) and
  1292. is_mmx_able_array(ld) and
  1293. is_mmx_able_array(rd) and
  1294. equal_defs(ld,rd) then
  1295. begin
  1296. case nodetype of
  1297. addn,subn,xorn,orn,andn:
  1298. ;
  1299. { mul is a little bit restricted }
  1300. muln:
  1301. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  1302. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1303. else
  1304. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1305. end;
  1306. end
  1307. {$endif SUPPORT_MMX}
  1308. { this is a little bit dangerous, also the left type }
  1309. { pointer to should be checked! This broke the mmx support }
  1310. else if (rd.deftype=pointerdef) or is_zero_based_array(rd) then
  1311. begin
  1312. if is_zero_based_array(rd) then
  1313. begin
  1314. resulttype.setdef(tpointerdef.create(tarraydef(rd).elementtype));
  1315. inserttypeconv(right,resulttype);
  1316. end
  1317. else
  1318. resulttype:=right.resulttype;
  1319. inserttypeconv(left,sinttype);
  1320. if nodetype=addn then
  1321. begin
  1322. if not(cs_extsyntax in aktmoduleswitches) or
  1323. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  1324. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1325. if (rd.deftype=pointerdef) and
  1326. (tpointerdef(rd).pointertype.def.size>1) then
  1327. begin
  1328. left:=caddnode.create(muln,left,
  1329. cordconstnode.create(tpointerdef(rd).pointertype.def.size,sinttype,true));
  1330. end;
  1331. end
  1332. else
  1333. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1334. end
  1335. else if (ld.deftype=pointerdef) or is_zero_based_array(ld) then
  1336. begin
  1337. if is_zero_based_array(ld) then
  1338. begin
  1339. resulttype.setdef(tpointerdef.create(tarraydef(ld).elementtype));
  1340. inserttypeconv(left,resulttype);
  1341. end
  1342. else
  1343. resulttype:=left.resulttype;
  1344. inserttypeconv(right,sinttype);
  1345. if nodetype in [addn,subn] then
  1346. begin
  1347. if not(cs_extsyntax in aktmoduleswitches) or
  1348. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  1349. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1350. if (ld.deftype=pointerdef) and
  1351. (tpointerdef(ld).pointertype.def.size>1) then
  1352. begin
  1353. right:=caddnode.create(muln,right,
  1354. cordconstnode.create(tpointerdef(ld).pointertype.def.size,sinttype,true));
  1355. end
  1356. else
  1357. if is_zero_based_array(ld) and
  1358. (tarraydef(ld).elementtype.def.size>1) then
  1359. begin
  1360. right:=caddnode.create(muln,right,
  1361. cordconstnode.create(tarraydef(ld).elementtype.def.size,sinttype,true));
  1362. end;
  1363. end
  1364. else
  1365. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1366. end
  1367. else if (rd.deftype=procvardef) and
  1368. (ld.deftype=procvardef) and
  1369. equal_defs(rd,ld) then
  1370. begin
  1371. if (nodetype in [equaln,unequaln]) then
  1372. begin
  1373. if tprocvardef(rd).is_addressonly then
  1374. begin
  1375. inserttypeconv_internal(right,voidpointertype);
  1376. inserttypeconv_internal(left,voidpointertype);
  1377. end
  1378. else
  1379. begin
  1380. { find proc field in methodpointer record }
  1381. hsym:=tfieldvarsym(trecorddef(methodpointertype.def).symtable.search('proc'));
  1382. if not assigned(hsym) then
  1383. internalerror(200412043);
  1384. { Compare tmehodpointer(left).proc }
  1385. right:=csubscriptnode.create(
  1386. hsym,
  1387. ctypeconvnode.create_internal(right,methodpointertype));
  1388. left:=csubscriptnode.create(
  1389. hsym,
  1390. ctypeconvnode.create_internal(left,methodpointertype));
  1391. end;
  1392. end
  1393. else
  1394. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1395. end
  1396. { enums }
  1397. else if (ld.deftype=enumdef) and (rd.deftype=enumdef) then
  1398. begin
  1399. if allowenumop(nodetype) then
  1400. inserttypeconv(right,left.resulttype)
  1401. else
  1402. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1403. end
  1404. { generic conversion, this is for error recovery }
  1405. else
  1406. begin
  1407. inserttypeconv(left,sinttype);
  1408. inserttypeconv(right,sinttype);
  1409. end;
  1410. { set resulttype if not already done }
  1411. if not assigned(resulttype.def) then
  1412. begin
  1413. case nodetype of
  1414. ltn,lten,gtn,gten,equaln,unequaln :
  1415. resulttype:=booltype;
  1416. slashn :
  1417. resulttype:=resultrealtype;
  1418. addn:
  1419. begin
  1420. { for strings, return is always a 255 char string }
  1421. if is_shortstring(left.resulttype.def) then
  1422. resulttype:=cshortstringtype
  1423. else
  1424. resulttype:=left.resulttype;
  1425. end;
  1426. else
  1427. resulttype:=left.resulttype;
  1428. end;
  1429. end;
  1430. { when the result is currency we need some extra code for
  1431. multiplication and division. this should not be done when
  1432. the muln or slashn node is created internally }
  1433. if not(nf_is_currency in flags) and
  1434. is_currency(resulttype.def) then
  1435. begin
  1436. case nodetype of
  1437. slashn :
  1438. begin
  1439. { slashn will only work with floats }
  1440. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  1441. include(hp.flags,nf_is_currency);
  1442. result:=hp;
  1443. end;
  1444. muln :
  1445. begin
  1446. if s64currencytype.def.deftype=floatdef then
  1447. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype))
  1448. else
  1449. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  1450. include(hp.flags,nf_is_currency);
  1451. result:=hp
  1452. end;
  1453. end;
  1454. end;
  1455. end;
  1456. function taddnode.first_addstring: tnode;
  1457. var
  1458. p: tnode;
  1459. begin
  1460. { when we get here, we are sure that both the left and the right }
  1461. { node are both strings of the same stringtype (JM) }
  1462. case nodetype of
  1463. addn:
  1464. begin
  1465. { create the call to the concat routine both strings as arguments }
  1466. result := ccallnode.createintern('fpc_'+
  1467. tstringdef(resulttype.def).stringtypname+'_concat',
  1468. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1469. { we reused the arguments }
  1470. left := nil;
  1471. right := nil;
  1472. end;
  1473. ltn,lten,gtn,gten,equaln,unequaln :
  1474. begin
  1475. { generate better code for comparison with empty string, we
  1476. only need to compare the length with 0 }
  1477. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  1478. (((left.nodetype=stringconstn) and (str_length(left)=0)) or
  1479. ((right.nodetype=stringconstn) and (str_length(right)=0))) then
  1480. begin
  1481. { switch so that the constant is always on the right }
  1482. if left.nodetype = stringconstn then
  1483. begin
  1484. p := left;
  1485. left := right;
  1486. right := p;
  1487. end;
  1488. if is_shortstring(left.resulttype.def) or
  1489. (nodetype in [gtn,gten,ltn,lten]) then
  1490. { compare the length with 0 }
  1491. result := caddnode.create(nodetype,
  1492. cinlinenode.create(in_length_x,false,left),
  1493. cordconstnode.create(0,s32inttype,false))
  1494. else
  1495. begin
  1496. { compare the pointer with nil (for ansistrings etc), }
  1497. { faster than getting the length (JM) }
  1498. result:= caddnode.create(nodetype,
  1499. ctypeconvnode.create_internal(left,voidpointertype),
  1500. cpointerconstnode.create(0,voidpointertype));
  1501. end;
  1502. { left is reused }
  1503. left := nil;
  1504. { right isn't }
  1505. right.free;
  1506. right := nil;
  1507. exit;
  1508. end;
  1509. { no string constant -> call compare routine }
  1510. result := ccallnode.createintern('fpc_'+
  1511. tstringdef(left.resulttype.def).stringtypname+'_compare',
  1512. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1513. { and compare its result with 0 according to the original operator }
  1514. result := caddnode.create(nodetype,result,
  1515. cordconstnode.create(0,s32inttype,false));
  1516. left := nil;
  1517. right := nil;
  1518. end;
  1519. end;
  1520. end;
  1521. function taddnode.first_addset: tnode;
  1522. var
  1523. procname: string[31];
  1524. tempn: tnode;
  1525. paras: tcallparanode;
  1526. srsym: ttypesym;
  1527. begin
  1528. { get the sym that represents the fpc_normal_set type }
  1529. if not searchsystype('FPC_NORMAL_SET',srsym) then
  1530. internalerror(200108313);
  1531. case nodetype of
  1532. equaln,unequaln,lten,gten:
  1533. begin
  1534. case nodetype of
  1535. equaln,unequaln:
  1536. procname := 'fpc_set_comp_sets';
  1537. lten,gten:
  1538. begin
  1539. procname := 'fpc_set_contains_sets';
  1540. { (left >= right) = (right <= left) }
  1541. if nodetype = gten then
  1542. begin
  1543. tempn := left;
  1544. left := right;
  1545. right := tempn;
  1546. end;
  1547. end;
  1548. end;
  1549. { convert the arguments (explicitely) to fpc_normal_set's }
  1550. left := ctypeconvnode.create_internal(left,srsym.restype);
  1551. right := ctypeconvnode.create_internal(right,srsym.restype);
  1552. result := ccallnode.createintern(procname,ccallparanode.create(right,
  1553. ccallparanode.create(left,nil)));
  1554. { left and right are reused as parameters }
  1555. left := nil;
  1556. right := nil;
  1557. { for an unequaln, we have to negate the result of comp_sets }
  1558. if nodetype = unequaln then
  1559. result := cnotnode.create(result);
  1560. end;
  1561. addn:
  1562. begin
  1563. { optimize first loading of a set }
  1564. if (right.nodetype=setelementn) and
  1565. not(assigned(tsetelementnode(right).right)) and
  1566. is_emptyset(left) then
  1567. begin
  1568. { type cast the value to pass as argument to a byte, }
  1569. { since that's what the helper expects }
  1570. tsetelementnode(right).left :=
  1571. ctypeconvnode.create_internal(tsetelementnode(right).left,u8inttype);
  1572. { set the resulttype to the actual one (otherwise it's }
  1573. { "fpc_normal_set") }
  1574. result := ccallnode.createinternres('fpc_set_create_element',
  1575. ccallparanode.create(tsetelementnode(right).left,nil),
  1576. resulttype);
  1577. { reused }
  1578. tsetelementnode(right).left := nil;
  1579. end
  1580. else
  1581. begin
  1582. if right.nodetype=setelementn then
  1583. begin
  1584. { convert the arguments to bytes, since that's what }
  1585. { the helper expects }
  1586. tsetelementnode(right).left :=
  1587. ctypeconvnode.create_internal(tsetelementnode(right).left,
  1588. u8inttype);
  1589. { convert the original set (explicitely) to an }
  1590. { fpc_normal_set so we can pass it to the helper }
  1591. left := ctypeconvnode.create_internal(left,srsym.restype);
  1592. { add a range or a single element? }
  1593. if assigned(tsetelementnode(right).right) then
  1594. begin
  1595. tsetelementnode(right).right :=
  1596. ctypeconvnode.create_internal(tsetelementnode(right).right,
  1597. u8inttype);
  1598. { create the call }
  1599. result := ccallnode.createinternres('fpc_set_set_range',
  1600. ccallparanode.create(tsetelementnode(right).right,
  1601. ccallparanode.create(tsetelementnode(right).left,
  1602. ccallparanode.create(left,nil))),resulttype);
  1603. end
  1604. else
  1605. begin
  1606. result := ccallnode.createinternres('fpc_set_set_byte',
  1607. ccallparanode.create(tsetelementnode(right).left,
  1608. ccallparanode.create(left,nil)),resulttype);
  1609. end;
  1610. { remove reused parts from original node }
  1611. tsetelementnode(right).right := nil;
  1612. tsetelementnode(right).left := nil;
  1613. left := nil;
  1614. end
  1615. else
  1616. begin
  1617. { add two sets }
  1618. { convert the sets to fpc_normal_set's }
  1619. result := ccallnode.createinternres('fpc_set_add_sets',
  1620. ccallparanode.create(
  1621. ctypeconvnode.create_explicit(right,srsym.restype),
  1622. ccallparanode.create(
  1623. ctypeconvnode.create_internal(left,srsym.restype),nil)),resulttype);
  1624. { remove reused parts from original node }
  1625. left := nil;
  1626. right := nil;
  1627. end;
  1628. end
  1629. end;
  1630. subn,symdifn,muln:
  1631. begin
  1632. { convert the sets to fpc_normal_set's }
  1633. paras := ccallparanode.create(ctypeconvnode.create_internal(right,srsym.restype),
  1634. ccallparanode.create(ctypeconvnode.create_internal(left,srsym.restype),nil));
  1635. case nodetype of
  1636. subn:
  1637. result := ccallnode.createinternres('fpc_set_sub_sets',
  1638. paras,resulttype);
  1639. symdifn:
  1640. result := ccallnode.createinternres('fpc_set_symdif_sets',
  1641. paras,resulttype);
  1642. muln:
  1643. result := ccallnode.createinternres('fpc_set_mul_sets',
  1644. paras,resulttype);
  1645. end;
  1646. { remove reused parts from original node }
  1647. left := nil;
  1648. right := nil;
  1649. end;
  1650. else
  1651. internalerror(200108311);
  1652. end;
  1653. end;
  1654. function taddnode.first_add64bitint: tnode;
  1655. var
  1656. procname: string[31];
  1657. temp: tnode;
  1658. power: longint;
  1659. begin
  1660. result := nil;
  1661. { create helper calls mul }
  1662. if nodetype <> muln then
  1663. exit;
  1664. { make sure that if there is a constant, that it's on the right }
  1665. if left.nodetype = ordconstn then
  1666. begin
  1667. temp := right;
  1668. right := left;
  1669. left := temp;
  1670. end;
  1671. { can we use a shift instead of a mul? }
  1672. if not (cs_check_overflow in aktlocalswitches) and
  1673. (right.nodetype = ordconstn) and
  1674. ispowerof2(tordconstnode(right).value,power) then
  1675. begin
  1676. tordconstnode(right).value := power;
  1677. result := cshlshrnode.create(shln,left,right);
  1678. { left and right are reused }
  1679. left := nil;
  1680. right := nil;
  1681. { return firstpassed new node }
  1682. exit;
  1683. end;
  1684. { when currency is used set the result of the
  1685. parameters to s64bit, so they are not converted }
  1686. if is_currency(resulttype.def) then
  1687. begin
  1688. left.resulttype:=s64inttype;
  1689. right.resulttype:=s64inttype;
  1690. end;
  1691. { otherwise, create the parameters for the helper }
  1692. right := ccallparanode.create(
  1693. cordconstnode.create(ord(cs_check_overflow in aktlocalswitches),booltype,true),
  1694. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1695. left := nil;
  1696. { only qword needs the unsigned code, the
  1697. signed code is also used for currency }
  1698. if is_signed(resulttype.def) then
  1699. procname := 'fpc_mul_int64'
  1700. else
  1701. procname := 'fpc_mul_qword';
  1702. result := ccallnode.createintern(procname,right);
  1703. right := nil;
  1704. end;
  1705. function taddnode.first_addfloat : tnode;
  1706. var
  1707. procname: string[31];
  1708. temp: tnode;
  1709. power: longint;
  1710. { do we need to reverse the result ? }
  1711. notnode : boolean;
  1712. begin
  1713. result := nil;
  1714. notnode := false;
  1715. { In non-emulation mode, real opcodes are
  1716. emitted for floating point values.
  1717. }
  1718. if not (cs_fp_emulation in aktmoduleswitches) then
  1719. exit;
  1720. if not(target_info.system in system_wince) then
  1721. begin
  1722. case tfloatdef(resulttype.def).typ of
  1723. s32real:
  1724. procname:='float32';
  1725. s64real:
  1726. procname:='float64';
  1727. {!!! not yet implemented
  1728. s128real:
  1729. }
  1730. else
  1731. internalerror(2005082601);
  1732. end;
  1733. case nodetype of
  1734. addn:
  1735. procname:=procname+'_add';
  1736. muln:
  1737. procname:=procname+'_mul';
  1738. subn:
  1739. procname:=procname+'_sub';
  1740. slashn:
  1741. procname:=procname+'_div';
  1742. ltn:
  1743. procname:=procname+'_lt';
  1744. lten:
  1745. procname:=procname+'_le';
  1746. gtn:
  1747. begin
  1748. procname:=procname+'_le';
  1749. notnode:=true;
  1750. end;
  1751. gten:
  1752. begin
  1753. procname:=procname+'_lt';
  1754. notnode:=true;
  1755. end;
  1756. equaln:
  1757. procname:=procname+'_eq';
  1758. unequaln:
  1759. begin
  1760. procname:=procname+'_eq';
  1761. notnode:=true;
  1762. end;
  1763. else
  1764. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resulttype.def.typename,right.resulttype.def.typename);
  1765. end;
  1766. end
  1767. else
  1768. begin
  1769. case nodetype of
  1770. addn:
  1771. procname:='ADD';
  1772. muln:
  1773. procname:='MUL';
  1774. subn:
  1775. procname:='SUB';
  1776. slashn:
  1777. procname:='DIV';
  1778. ltn:
  1779. procname:='LT';
  1780. lten:
  1781. procname:='LE';
  1782. gtn:
  1783. procname:='GT';
  1784. gten:
  1785. procname:='GE';
  1786. equaln:
  1787. procname:='EQ';
  1788. unequaln:
  1789. procname:='NE';
  1790. else
  1791. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resulttype.def.typename,right.resulttype.def.typename);
  1792. end;
  1793. case tfloatdef(resulttype.def).typ of
  1794. s32real:
  1795. procname:=procname+'S';
  1796. s64real:
  1797. procname:=procname+'D';
  1798. {!!! not yet implemented
  1799. s128real:
  1800. }
  1801. else
  1802. internalerror(2005082602);
  1803. end;
  1804. end;
  1805. if procname='MULS' then
  1806. procname:='MULD';
  1807. result:=ccallnode.createintern(procname,ccallparanode.create(right,
  1808. ccallparanode.create(left,nil)));
  1809. left:=nil;
  1810. right:=nil;
  1811. { do we need to reverse the result }
  1812. if notnode then
  1813. result:=cnotnode.create(result);
  1814. end;
  1815. function taddnode.pass_1 : tnode;
  1816. var
  1817. {$ifdef addstringopt}
  1818. hp : tnode;
  1819. {$endif addstringopt}
  1820. lt,rt : tnodetype;
  1821. rd,ld : tdef;
  1822. begin
  1823. result:=nil;
  1824. { first do the two subtrees }
  1825. firstpass(left);
  1826. firstpass(right);
  1827. if codegenerror then
  1828. exit;
  1829. { load easier access variables }
  1830. rd:=right.resulttype.def;
  1831. ld:=left.resulttype.def;
  1832. rt:=right.nodetype;
  1833. lt:=left.nodetype;
  1834. { int/int gives real/real! }
  1835. if nodetype=slashn then
  1836. begin
  1837. {$ifdef cpufpemu}
  1838. if (aktfputype=fpu_soft) or (cs_fp_emulation in aktmoduleswitches) then
  1839. begin
  1840. result:=first_addfloat;
  1841. if assigned(result) then
  1842. exit;
  1843. end;
  1844. {$endif cpufpemu}
  1845. expectloc:=LOC_FPUREGISTER;
  1846. { maybe we need an integer register to save }
  1847. { a reference }
  1848. if ((left.expectloc<>LOC_FPUREGISTER) or
  1849. (right.expectloc<>LOC_FPUREGISTER)) and
  1850. (left.registersint=right.registersint) then
  1851. calcregisters(self,1,1,0)
  1852. else
  1853. calcregisters(self,0,1,0);
  1854. { an add node always first loads both the left and the }
  1855. { right in the fpu before doing the calculation. However, }
  1856. { calcregisters(0,2,0) will overestimate the number of }
  1857. { necessary registers (it will make it 3 in case one of }
  1858. { the operands is already in the fpu) (JM) }
  1859. if ((left.expectloc<>LOC_FPUREGISTER) or
  1860. (right.expectloc<>LOC_FPUREGISTER)) and
  1861. (registersfpu < 2) then
  1862. inc(registersfpu);
  1863. end
  1864. { if both are orddefs then check sub types }
  1865. else if (ld.deftype=orddef) and (rd.deftype=orddef) then
  1866. begin
  1867. { 2 booleans ? }
  1868. if is_boolean(ld) and is_boolean(rd) then
  1869. begin
  1870. if not(cs_full_boolean_eval in aktlocalswitches) and
  1871. (nodetype in [andn,orn]) then
  1872. begin
  1873. expectloc:=LOC_JUMP;
  1874. calcregisters(self,0,0,0);
  1875. end
  1876. else
  1877. begin
  1878. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  1879. begin
  1880. expectloc:=LOC_FLAGS;
  1881. if (left.expectloc in [LOC_JUMP,LOC_FLAGS]) and
  1882. (left.expectloc in [LOC_JUMP,LOC_FLAGS]) then
  1883. calcregisters(self,2,0,0)
  1884. else
  1885. calcregisters(self,1,0,0);
  1886. end
  1887. else
  1888. begin
  1889. expectloc:=LOC_REGISTER;
  1890. calcregisters(self,0,0,0);
  1891. end;
  1892. end;
  1893. end
  1894. else
  1895. { Both are chars? only convert to shortstrings for addn }
  1896. if is_char(ld) then
  1897. begin
  1898. if nodetype=addn then
  1899. internalerror(200103291);
  1900. expectloc:=LOC_FLAGS;
  1901. calcregisters(self,1,0,0);
  1902. end
  1903. {$ifndef cpu64bit}
  1904. { is there a 64 bit type ? }
  1905. else if (torddef(ld).typ in [s64bit,u64bit,scurrency]) then
  1906. begin
  1907. result := first_add64bitint;
  1908. if assigned(result) then
  1909. exit;
  1910. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1911. expectloc:=LOC_REGISTER
  1912. else
  1913. expectloc:=LOC_JUMP;
  1914. calcregisters(self,2,0,0)
  1915. end
  1916. {$endif cpu64bit}
  1917. { is there a cardinal? }
  1918. else if (torddef(ld).typ=u32bit) then
  1919. begin
  1920. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1921. expectloc:=LOC_REGISTER
  1922. else
  1923. expectloc:=LOC_FLAGS;
  1924. calcregisters(self,1,0,0);
  1925. { for unsigned mul we need an extra register }
  1926. if nodetype=muln then
  1927. inc(registersint);
  1928. end
  1929. { generic s32bit conversion }
  1930. else
  1931. begin
  1932. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1933. expectloc:=LOC_REGISTER
  1934. else
  1935. expectloc:=LOC_FLAGS;
  1936. calcregisters(self,1,0,0);
  1937. end;
  1938. end
  1939. { left side a setdef, must be before string processing,
  1940. else array constructor can be seen as array of char (PFV) }
  1941. else if (ld.deftype=setdef) then
  1942. begin
  1943. if tsetdef(ld).settype=smallset then
  1944. begin
  1945. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  1946. expectloc:=LOC_FLAGS
  1947. else
  1948. expectloc:=LOC_REGISTER;
  1949. { are we adding set elements ? }
  1950. if right.nodetype=setelementn then
  1951. calcregisters(self,2,0,0)
  1952. else
  1953. calcregisters(self,1,0,0);
  1954. end
  1955. else
  1956. {$ifdef MMXSET}
  1957. {$ifdef i386}
  1958. if cs_mmx in aktlocalswitches then
  1959. begin
  1960. expectloc:=LOC_MMXREGISTER;
  1961. calcregisters(self,0,0,4);
  1962. end
  1963. else
  1964. {$endif}
  1965. {$endif MMXSET}
  1966. begin
  1967. result := first_addset;
  1968. if assigned(result) then
  1969. exit;
  1970. expectloc:=LOC_CREFERENCE;
  1971. calcregisters(self,0,0,0);
  1972. { here we call SET... }
  1973. include(current_procinfo.flags,pi_do_call);
  1974. end;
  1975. end
  1976. { compare pchar by addresses like BP/Delphi }
  1977. else if is_pchar(ld) then
  1978. begin
  1979. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1980. expectloc:=LOC_REGISTER
  1981. else
  1982. expectloc:=LOC_FLAGS;
  1983. calcregisters(self,1,0,0);
  1984. end
  1985. { is one of the operands a string }
  1986. else if (ld.deftype=stringdef) then
  1987. begin
  1988. if is_widestring(ld) then
  1989. begin
  1990. { this is only for add, the comparisaion is handled later }
  1991. expectloc:=LOC_REGISTER;
  1992. end
  1993. else if is_ansistring(ld) then
  1994. begin
  1995. { this is only for add, the comparisaion is handled later }
  1996. expectloc:=LOC_REGISTER;
  1997. end
  1998. else if is_longstring(ld) then
  1999. begin
  2000. { this is only for add, the comparisaion is handled later }
  2001. expectloc:=LOC_REFERENCE;
  2002. end
  2003. else
  2004. begin
  2005. {$ifdef addstringopt}
  2006. { can create a call which isn't handled by callparatemp }
  2007. if canbeaddsstringcharoptnode(self) then
  2008. begin
  2009. hp := genaddsstringcharoptnode(self);
  2010. pass_1 := hp;
  2011. exit;
  2012. end
  2013. else
  2014. {$endif addstringopt}
  2015. begin
  2016. { Fix right to be shortstring }
  2017. if is_char(right.resulttype.def) then
  2018. begin
  2019. inserttypeconv(right,cshortstringtype);
  2020. firstpass(right);
  2021. end;
  2022. end;
  2023. {$ifdef addstringopt}
  2024. { can create a call which isn't handled by callparatemp }
  2025. if canbeaddsstringcsstringoptnode(self) then
  2026. begin
  2027. hp := genaddsstringcsstringoptnode(self);
  2028. pass_1 := hp;
  2029. exit;
  2030. end;
  2031. {$endif addstringopt}
  2032. end;
  2033. { otherwise, let addstring convert everything }
  2034. result := first_addstring;
  2035. exit;
  2036. end
  2037. { is one a real float ? }
  2038. else if (rd.deftype=floatdef) or (ld.deftype=floatdef) then
  2039. begin
  2040. {$ifdef cpufpemu}
  2041. if (aktfputype=fpu_soft) or (cs_fp_emulation in aktmoduleswitches) then
  2042. begin
  2043. result:=first_addfloat;
  2044. if assigned(result) then
  2045. exit;
  2046. end;
  2047. {$endif cpufpemu}
  2048. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2049. expectloc:=LOC_FPUREGISTER
  2050. else
  2051. expectloc:=LOC_FLAGS;
  2052. calcregisters(self,0,1,0);
  2053. { an add node always first loads both the left and the }
  2054. { right in the fpu before doing the calculation. However, }
  2055. { calcregisters(0,2,0) will overestimate the number of }
  2056. { necessary registers (it will make it 3 in case one of }
  2057. { the operands is already in the fpu) (JM) }
  2058. if ((left.expectloc<>LOC_FPUREGISTER) or
  2059. (right.expectloc<>LOC_FPUREGISTER)) and
  2060. (registersfpu < 2) then
  2061. inc(registersfpu);
  2062. end
  2063. { pointer comperation and subtraction }
  2064. else if (ld.deftype=pointerdef) then
  2065. begin
  2066. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2067. expectloc:=LOC_REGISTER
  2068. else
  2069. expectloc:=LOC_FLAGS;
  2070. calcregisters(self,1,0,0);
  2071. end
  2072. else if is_class_or_interface(ld) then
  2073. begin
  2074. expectloc:=LOC_FLAGS;
  2075. calcregisters(self,1,0,0);
  2076. end
  2077. else if (ld.deftype=classrefdef) then
  2078. begin
  2079. expectloc:=LOC_FLAGS;
  2080. calcregisters(self,1,0,0);
  2081. end
  2082. { support procvar=nil,procvar<>nil }
  2083. else if ((ld.deftype=procvardef) and (rt=niln)) or
  2084. ((rd.deftype=procvardef) and (lt=niln)) then
  2085. begin
  2086. expectloc:=LOC_FLAGS;
  2087. calcregisters(self,1,0,0);
  2088. end
  2089. {$ifdef SUPPORT_MMX}
  2090. { mmx support, this must be before the zero based array
  2091. check }
  2092. else if (cs_mmx in aktlocalswitches) and is_mmx_able_array(ld) and
  2093. is_mmx_able_array(rd) then
  2094. begin
  2095. expectloc:=LOC_MMXREGISTER;
  2096. calcregisters(self,0,0,1);
  2097. end
  2098. {$endif SUPPORT_MMX}
  2099. else if (rd.deftype=pointerdef) or (ld.deftype=pointerdef) then
  2100. begin
  2101. expectloc:=LOC_REGISTER;
  2102. calcregisters(self,1,0,0);
  2103. end
  2104. else if (rd.deftype=procvardef) and
  2105. (ld.deftype=procvardef) and
  2106. equal_defs(rd,ld) then
  2107. begin
  2108. expectloc:=LOC_FLAGS;
  2109. calcregisters(self,1,0,0);
  2110. end
  2111. else if (ld.deftype=enumdef) then
  2112. begin
  2113. expectloc:=LOC_FLAGS;
  2114. calcregisters(self,1,0,0);
  2115. end
  2116. {$ifdef SUPPORT_MMX}
  2117. else if (cs_mmx in aktlocalswitches) and
  2118. is_mmx_able_array(ld) and
  2119. is_mmx_able_array(rd) then
  2120. begin
  2121. expectloc:=LOC_MMXREGISTER;
  2122. calcregisters(self,0,0,1);
  2123. end
  2124. {$endif SUPPORT_MMX}
  2125. { the general solution is to convert to 32 bit int }
  2126. else
  2127. begin
  2128. expectloc:=LOC_REGISTER;
  2129. calcregisters(self,1,0,0);
  2130. end;
  2131. end;
  2132. {$ifdef state_tracking}
  2133. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  2134. var factval:Tnode;
  2135. begin
  2136. track_state_pass:=false;
  2137. if left.track_state_pass(exec_known) then
  2138. begin
  2139. track_state_pass:=true;
  2140. left.resulttype.def:=nil;
  2141. do_resulttypepass(left);
  2142. end;
  2143. factval:=aktstate.find_fact(left);
  2144. if factval<>nil then
  2145. begin
  2146. track_state_pass:=true;
  2147. left.destroy;
  2148. left:=factval.getcopy;
  2149. end;
  2150. if right.track_state_pass(exec_known) then
  2151. begin
  2152. track_state_pass:=true;
  2153. right.resulttype.def:=nil;
  2154. do_resulttypepass(right);
  2155. end;
  2156. factval:=aktstate.find_fact(right);
  2157. if factval<>nil then
  2158. begin
  2159. track_state_pass:=true;
  2160. right.destroy;
  2161. right:=factval.getcopy;
  2162. end;
  2163. end;
  2164. {$endif}
  2165. begin
  2166. caddnode:=taddnode;
  2167. end.