nadd.pas 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291
  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,st_conststring);
  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 (
  1036. (rd.deftype=pointerdef) and (ld.deftype=pointerdef)
  1037. ) or
  1038. { compare/add pchar to variable (not stringconst) char arrays
  1039. by addresses like BP/Delphi }
  1040. (
  1041. (nodetype in [equaln,unequaln,subn,addn]) and
  1042. (
  1043. ((is_pchar(ld) or (lt=niln)) and is_chararray(rd) and (rt<>stringconstn)) or
  1044. ((is_pchar(rd) or (rt=niln)) and is_chararray(ld) and (lt<>stringconstn))
  1045. )
  1046. ) then
  1047. begin
  1048. { convert char array to pointer }
  1049. if is_chararray(rd) then
  1050. begin
  1051. inserttypeconv(right,charpointertype);
  1052. rd:=right.resulttype.def;
  1053. end
  1054. else if is_chararray(ld) then
  1055. begin
  1056. inserttypeconv(left,charpointertype);
  1057. ld:=left.resulttype.def;
  1058. end;
  1059. case nodetype of
  1060. equaln,unequaln :
  1061. begin
  1062. if is_voidpointer(right.resulttype.def) then
  1063. inserttypeconv(right,left.resulttype)
  1064. else if is_voidpointer(left.resulttype.def) then
  1065. inserttypeconv(left,right.resulttype)
  1066. else if not(equal_defs(ld,rd)) then
  1067. IncompatibleTypes(ld,rd);
  1068. { now that the type checking is done, convert both to charpointer, }
  1069. { because methodpointers are 8 bytes even though only the first 4 }
  1070. { bytes must be compared. This can happen here if we are in }
  1071. { TP/Delphi mode, because there @methodpointer = voidpointer (but }
  1072. { a voidpointer of 8 bytes). A conversion to voidpointer would be }
  1073. { optimized away, since the result already was a voidpointer, so }
  1074. { use a charpointer instead (JM) }
  1075. inserttypeconv_internal(left,charpointertype);
  1076. inserttypeconv_internal(right,charpointertype);
  1077. end;
  1078. ltn,lten,gtn,gten:
  1079. begin
  1080. if (cs_extsyntax in aktmoduleswitches) then
  1081. begin
  1082. if is_voidpointer(right.resulttype.def) then
  1083. inserttypeconv(right,left.resulttype)
  1084. else if is_voidpointer(left.resulttype.def) then
  1085. inserttypeconv(left,right.resulttype)
  1086. else if not(equal_defs(ld,rd)) then
  1087. IncompatibleTypes(ld,rd);
  1088. end
  1089. else
  1090. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1091. end;
  1092. subn:
  1093. begin
  1094. if (cs_extsyntax in aktmoduleswitches) then
  1095. begin
  1096. if is_voidpointer(right.resulttype.def) then
  1097. inserttypeconv(right,left.resulttype)
  1098. else if is_voidpointer(left.resulttype.def) then
  1099. inserttypeconv(left,right.resulttype)
  1100. else if not(equal_defs(ld,rd)) then
  1101. IncompatibleTypes(ld,rd);
  1102. end
  1103. else
  1104. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1105. if not(nf_has_pointerdiv in flags) and
  1106. (tpointerdef(rd).pointertype.def.size>1) then
  1107. begin
  1108. hp:=getcopy;
  1109. include(hp.flags,nf_has_pointerdiv);
  1110. result:=cmoddivnode.create(divn,hp,cordconstnode.create(tpointerdef(rd).pointertype.def.size,sinttype,false));
  1111. end;
  1112. resulttype:=sinttype;
  1113. exit;
  1114. end;
  1115. addn:
  1116. begin
  1117. if (cs_extsyntax in aktmoduleswitches) then
  1118. begin
  1119. if is_voidpointer(right.resulttype.def) then
  1120. inserttypeconv(right,left.resulttype)
  1121. else if is_voidpointer(left.resulttype.def) then
  1122. inserttypeconv(left,right.resulttype)
  1123. else if not(equal_defs(ld,rd)) then
  1124. IncompatibleTypes(ld,rd);
  1125. end
  1126. else
  1127. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1128. resulttype:=sinttype;
  1129. exit;
  1130. end;
  1131. else
  1132. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1133. end;
  1134. end
  1135. { is one of the operands a string?,
  1136. chararrays are also handled as strings (after conversion), also take
  1137. care of chararray+chararray and chararray+char.
  1138. Note: Must be done after pointerdef+pointerdef has been checked, else
  1139. pchar is converted to string }
  1140. else if (rd.deftype=stringdef) or
  1141. (ld.deftype=stringdef) or
  1142. ((is_pchar(rd) or is_chararray(rd) or is_char(rd) or is_open_chararray(rd) or
  1143. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd)) and
  1144. (is_pchar(ld) or is_chararray(ld) or is_char(ld) or is_open_chararray(ld) or
  1145. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld))) then
  1146. begin
  1147. if (nodetype in [addn,equaln,unequaln,lten,gten,ltn,gtn]) then
  1148. begin
  1149. { Is there a widestring? }
  1150. if is_widestring(rd) or is_widestring(ld) or
  1151. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd) or
  1152. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld) then
  1153. strtype:= st_widestring
  1154. else
  1155. if is_ansistring(rd) or is_ansistring(ld) or
  1156. ((cs_ansistrings in aktlocalswitches) and
  1157. //todo: Move some of this to longstring's then they are implemented?
  1158. (
  1159. is_pchar(rd) or (is_chararray(rd) and (rd.size > 255)) or is_open_chararray(rd) or
  1160. is_pchar(ld) or (is_chararray(ld) and (ld.size > 255)) or is_open_chararray(ld)
  1161. )
  1162. ) then
  1163. strtype:= st_ansistring
  1164. else
  1165. if is_longstring(rd) or is_longstring(ld) then
  1166. strtype:= st_longstring
  1167. else
  1168. begin
  1169. {$warning todo: add a warning/hint here if one converting a too large array}
  1170. { nodes is PChar, array [with size > 255] or OpenArrayOfChar.
  1171. Note: Delphi halts with error if "array [0..xx] of char"
  1172. is assigned to ShortString and string length is less
  1173. then array size }
  1174. strtype:= st_shortstring;
  1175. end;
  1176. // Now convert nodes to common string type
  1177. case strtype of
  1178. st_widestring :
  1179. begin
  1180. if not(is_widestring(rd)) then
  1181. inserttypeconv(right,cwidestringtype);
  1182. if not(is_widestring(ld)) then
  1183. inserttypeconv(left,cwidestringtype);
  1184. end;
  1185. st_ansistring :
  1186. begin
  1187. if not(is_ansistring(rd)) then
  1188. inserttypeconv(right,cansistringtype);
  1189. if not(is_ansistring(ld)) then
  1190. inserttypeconv(left,cansistringtype);
  1191. end;
  1192. st_longstring :
  1193. begin
  1194. if not(is_longstring(rd)) then
  1195. inserttypeconv(right,clongstringtype);
  1196. if not(is_longstring(ld)) then
  1197. inserttypeconv(left,clongstringtype);
  1198. end;
  1199. st_shortstring :
  1200. begin
  1201. if not(is_shortstring(ld)) then
  1202. inserttypeconv(left,cshortstringtype);
  1203. { don't convert char, that can be handled by the optimized node }
  1204. if not(is_shortstring(rd) or is_char(rd)) then
  1205. inserttypeconv(right,cshortstringtype);
  1206. end;
  1207. else
  1208. internalerror(2005101);
  1209. end;
  1210. end
  1211. else
  1212. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1213. end
  1214. { class or interface equation }
  1215. else if is_class_or_interface(rd) or is_class_or_interface(ld) then
  1216. begin
  1217. if (nodetype in [equaln,unequaln]) then
  1218. begin
  1219. if is_class_or_interface(rd) and is_class_or_interface(ld) then
  1220. begin
  1221. if tobjectdef(rd).is_related(tobjectdef(ld)) then
  1222. inserttypeconv(right,left.resulttype)
  1223. else
  1224. inserttypeconv(left,right.resulttype);
  1225. end
  1226. else if is_class_or_interface(rd) then
  1227. inserttypeconv(left,right.resulttype)
  1228. else
  1229. inserttypeconv(right,left.resulttype);
  1230. end
  1231. else
  1232. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1233. end
  1234. else if (rd.deftype=classrefdef) and (ld.deftype=classrefdef) then
  1235. begin
  1236. if (nodetype in [equaln,unequaln]) then
  1237. begin
  1238. if tobjectdef(tclassrefdef(rd).pointertype.def).is_related(
  1239. tobjectdef(tclassrefdef(ld).pointertype.def)) then
  1240. inserttypeconv(right,left.resulttype)
  1241. else
  1242. inserttypeconv(left,right.resulttype);
  1243. end
  1244. else
  1245. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1246. end
  1247. { allows comperasion with nil pointer }
  1248. else if is_class_or_interface(rd) or (rd.deftype=classrefdef) then
  1249. begin
  1250. if (nodetype in [equaln,unequaln]) then
  1251. inserttypeconv(left,right.resulttype)
  1252. else
  1253. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1254. end
  1255. else if is_class_or_interface(ld) or (ld.deftype=classrefdef) then
  1256. begin
  1257. if (nodetype in [equaln,unequaln]) then
  1258. inserttypeconv(right,left.resulttype)
  1259. else
  1260. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1261. end
  1262. { support procvar=nil,procvar<>nil }
  1263. else if ((ld.deftype=procvardef) and (rt=niln)) or
  1264. ((rd.deftype=procvardef) and (lt=niln)) then
  1265. begin
  1266. if not(nodetype in [equaln,unequaln]) then
  1267. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1268. { find proc field in methodpointer record }
  1269. hsym:=tfieldvarsym(trecorddef(methodpointertype.def).symtable.search('proc'));
  1270. if not assigned(hsym) then
  1271. internalerror(200412043);
  1272. { For methodpointers compare only tmethodpointer.proc }
  1273. if (rd.deftype=procvardef) and
  1274. (not tprocvardef(rd).is_addressonly) then
  1275. begin
  1276. right:=csubscriptnode.create(
  1277. hsym,
  1278. ctypeconvnode.create_internal(right,methodpointertype));
  1279. end;
  1280. if (ld.deftype=procvardef) and
  1281. (not tprocvardef(ld).is_addressonly) then
  1282. begin
  1283. left:=csubscriptnode.create(
  1284. hsym,
  1285. ctypeconvnode.create_internal(left,methodpointertype));
  1286. end;
  1287. end
  1288. { support dynamicarray=nil,dynamicarray<>nil }
  1289. else if (is_dynamic_array(ld) and (rt=niln)) or
  1290. (is_dynamic_array(rd) and (lt=niln)) or
  1291. (is_dynamic_array(ld) and is_dynamic_array(rd)) then
  1292. begin
  1293. if not(nodetype in [equaln,unequaln]) then
  1294. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1295. end
  1296. {$ifdef SUPPORT_MMX}
  1297. { mmx support, this must be before the zero based array
  1298. check }
  1299. else if (cs_mmx in aktlocalswitches) and
  1300. is_mmx_able_array(ld) and
  1301. is_mmx_able_array(rd) and
  1302. equal_defs(ld,rd) then
  1303. begin
  1304. case nodetype of
  1305. addn,subn,xorn,orn,andn:
  1306. ;
  1307. { mul is a little bit restricted }
  1308. muln:
  1309. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  1310. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1311. else
  1312. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1313. end;
  1314. end
  1315. {$endif SUPPORT_MMX}
  1316. { this is a little bit dangerous, also the left type }
  1317. { pointer to should be checked! This broke the mmx support }
  1318. else if (rd.deftype=pointerdef) or is_zero_based_array(rd) then
  1319. begin
  1320. if is_zero_based_array(rd) then
  1321. begin
  1322. resulttype.setdef(tpointerdef.create(tarraydef(rd).elementtype));
  1323. inserttypeconv(right,resulttype);
  1324. end
  1325. else
  1326. resulttype:=right.resulttype;
  1327. inserttypeconv(left,sinttype);
  1328. if nodetype=addn then
  1329. begin
  1330. if not(cs_extsyntax in aktmoduleswitches) or
  1331. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  1332. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1333. if (rd.deftype=pointerdef) and
  1334. (tpointerdef(rd).pointertype.def.size>1) then
  1335. begin
  1336. left:=caddnode.create(muln,left,
  1337. cordconstnode.create(tpointerdef(rd).pointertype.def.size,sinttype,true));
  1338. end;
  1339. end
  1340. else
  1341. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1342. end
  1343. else if (ld.deftype=pointerdef) or is_zero_based_array(ld) then
  1344. begin
  1345. if is_zero_based_array(ld) then
  1346. begin
  1347. resulttype.setdef(tpointerdef.create(tarraydef(ld).elementtype));
  1348. inserttypeconv(left,resulttype);
  1349. end
  1350. else
  1351. resulttype:=left.resulttype;
  1352. inserttypeconv(right,sinttype);
  1353. if nodetype in [addn,subn] then
  1354. begin
  1355. if not(cs_extsyntax in aktmoduleswitches) or
  1356. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  1357. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1358. if (ld.deftype=pointerdef) and
  1359. (tpointerdef(ld).pointertype.def.size>1) then
  1360. begin
  1361. right:=caddnode.create(muln,right,
  1362. cordconstnode.create(tpointerdef(ld).pointertype.def.size,sinttype,true));
  1363. end
  1364. else
  1365. if is_zero_based_array(ld) and
  1366. (tarraydef(ld).elementtype.def.size>1) then
  1367. begin
  1368. right:=caddnode.create(muln,right,
  1369. cordconstnode.create(tarraydef(ld).elementtype.def.size,sinttype,true));
  1370. end;
  1371. end
  1372. else
  1373. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1374. end
  1375. else if (rd.deftype=procvardef) and
  1376. (ld.deftype=procvardef) and
  1377. equal_defs(rd,ld) then
  1378. begin
  1379. if (nodetype in [equaln,unequaln]) then
  1380. begin
  1381. if tprocvardef(rd).is_addressonly then
  1382. begin
  1383. inserttypeconv_internal(right,voidpointertype);
  1384. inserttypeconv_internal(left,voidpointertype);
  1385. end
  1386. else
  1387. begin
  1388. { find proc field in methodpointer record }
  1389. hsym:=tfieldvarsym(trecorddef(methodpointertype.def).symtable.search('proc'));
  1390. if not assigned(hsym) then
  1391. internalerror(200412043);
  1392. { Compare tmehodpointer(left).proc }
  1393. right:=csubscriptnode.create(
  1394. hsym,
  1395. ctypeconvnode.create_internal(right,methodpointertype));
  1396. left:=csubscriptnode.create(
  1397. hsym,
  1398. ctypeconvnode.create_internal(left,methodpointertype));
  1399. end;
  1400. end
  1401. else
  1402. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1403. end
  1404. { enums }
  1405. else if (ld.deftype=enumdef) and (rd.deftype=enumdef) then
  1406. begin
  1407. if allowenumop(nodetype) then
  1408. inserttypeconv(right,left.resulttype)
  1409. else
  1410. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1411. end
  1412. { generic conversion, this is for error recovery }
  1413. else
  1414. begin
  1415. inserttypeconv(left,sinttype);
  1416. inserttypeconv(right,sinttype);
  1417. end;
  1418. { set resulttype if not already done }
  1419. if not assigned(resulttype.def) then
  1420. begin
  1421. case nodetype of
  1422. ltn,lten,gtn,gten,equaln,unequaln :
  1423. resulttype:=booltype;
  1424. slashn :
  1425. resulttype:=resultrealtype;
  1426. addn:
  1427. begin
  1428. { for strings, return is always a 255 char string }
  1429. if is_shortstring(left.resulttype.def) then
  1430. resulttype:=cshortstringtype
  1431. else
  1432. resulttype:=left.resulttype;
  1433. end;
  1434. else
  1435. resulttype:=left.resulttype;
  1436. end;
  1437. end;
  1438. { when the result is currency we need some extra code for
  1439. multiplication and division. this should not be done when
  1440. the muln or slashn node is created internally }
  1441. if not(nf_is_currency in flags) and
  1442. is_currency(resulttype.def) then
  1443. begin
  1444. case nodetype of
  1445. slashn :
  1446. begin
  1447. { slashn will only work with floats }
  1448. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  1449. include(hp.flags,nf_is_currency);
  1450. result:=hp;
  1451. end;
  1452. muln :
  1453. begin
  1454. if s64currencytype.def.deftype=floatdef then
  1455. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype))
  1456. else
  1457. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  1458. include(hp.flags,nf_is_currency);
  1459. result:=hp
  1460. end;
  1461. end;
  1462. end;
  1463. end;
  1464. function taddnode.first_addstring: tnode;
  1465. var
  1466. p: tnode;
  1467. begin
  1468. { when we get here, we are sure that both the left and the right }
  1469. { node are both strings of the same stringtype (JM) }
  1470. case nodetype of
  1471. addn:
  1472. begin
  1473. { create the call to the concat routine both strings as arguments }
  1474. result := ccallnode.createintern('fpc_'+
  1475. tstringdef(resulttype.def).stringtypname+'_concat',
  1476. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1477. { we reused the arguments }
  1478. left := nil;
  1479. right := nil;
  1480. end;
  1481. ltn,lten,gtn,gten,equaln,unequaln :
  1482. begin
  1483. { generate better code for comparison with empty string, we
  1484. only need to compare the length with 0 }
  1485. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  1486. (((left.nodetype=stringconstn) and (str_length(left)=0)) or
  1487. ((right.nodetype=stringconstn) and (str_length(right)=0))) then
  1488. begin
  1489. { switch so that the constant is always on the right }
  1490. if left.nodetype = stringconstn then
  1491. begin
  1492. p := left;
  1493. left := right;
  1494. right := p;
  1495. end;
  1496. if is_shortstring(left.resulttype.def) or
  1497. (nodetype in [gtn,gten,ltn,lten]) then
  1498. { compare the length with 0 }
  1499. result := caddnode.create(nodetype,
  1500. cinlinenode.create(in_length_x,false,left),
  1501. cordconstnode.create(0,s32inttype,false))
  1502. else
  1503. begin
  1504. { compare the pointer with nil (for ansistrings etc), }
  1505. { faster than getting the length (JM) }
  1506. result:= caddnode.create(nodetype,
  1507. ctypeconvnode.create_internal(left,voidpointertype),
  1508. cpointerconstnode.create(0,voidpointertype));
  1509. end;
  1510. { left is reused }
  1511. left := nil;
  1512. { right isn't }
  1513. right.free;
  1514. right := nil;
  1515. exit;
  1516. end;
  1517. { no string constant -> call compare routine }
  1518. result := ccallnode.createintern('fpc_'+
  1519. tstringdef(left.resulttype.def).stringtypname+'_compare',
  1520. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1521. { and compare its result with 0 according to the original operator }
  1522. result := caddnode.create(nodetype,result,
  1523. cordconstnode.create(0,s32inttype,false));
  1524. left := nil;
  1525. right := nil;
  1526. end;
  1527. end;
  1528. end;
  1529. function taddnode.first_addset: tnode;
  1530. var
  1531. procname: string[31];
  1532. tempn: tnode;
  1533. paras: tcallparanode;
  1534. srsym: ttypesym;
  1535. begin
  1536. { get the sym that represents the fpc_normal_set type }
  1537. if not searchsystype('FPC_NORMAL_SET',srsym) then
  1538. internalerror(200108313);
  1539. case nodetype of
  1540. equaln,unequaln,lten,gten:
  1541. begin
  1542. case nodetype of
  1543. equaln,unequaln:
  1544. procname := 'fpc_set_comp_sets';
  1545. lten,gten:
  1546. begin
  1547. procname := 'fpc_set_contains_sets';
  1548. { (left >= right) = (right <= left) }
  1549. if nodetype = gten then
  1550. begin
  1551. tempn := left;
  1552. left := right;
  1553. right := tempn;
  1554. end;
  1555. end;
  1556. end;
  1557. { convert the arguments (explicitely) to fpc_normal_set's }
  1558. left := ctypeconvnode.create_internal(left,srsym.restype);
  1559. right := ctypeconvnode.create_internal(right,srsym.restype);
  1560. result := ccallnode.createintern(procname,ccallparanode.create(right,
  1561. ccallparanode.create(left,nil)));
  1562. { left and right are reused as parameters }
  1563. left := nil;
  1564. right := nil;
  1565. { for an unequaln, we have to negate the result of comp_sets }
  1566. if nodetype = unequaln then
  1567. result := cnotnode.create(result);
  1568. end;
  1569. addn:
  1570. begin
  1571. { optimize first loading of a set }
  1572. if (right.nodetype=setelementn) and
  1573. not(assigned(tsetelementnode(right).right)) and
  1574. is_emptyset(left) then
  1575. begin
  1576. { type cast the value to pass as argument to a byte, }
  1577. { since that's what the helper expects }
  1578. tsetelementnode(right).left :=
  1579. ctypeconvnode.create_internal(tsetelementnode(right).left,u8inttype);
  1580. { set the resulttype to the actual one (otherwise it's }
  1581. { "fpc_normal_set") }
  1582. result := ccallnode.createinternres('fpc_set_create_element',
  1583. ccallparanode.create(tsetelementnode(right).left,nil),
  1584. resulttype);
  1585. { reused }
  1586. tsetelementnode(right).left := nil;
  1587. end
  1588. else
  1589. begin
  1590. if right.nodetype=setelementn then
  1591. begin
  1592. { convert the arguments to bytes, since that's what }
  1593. { the helper expects }
  1594. tsetelementnode(right).left :=
  1595. ctypeconvnode.create_internal(tsetelementnode(right).left,
  1596. u8inttype);
  1597. { convert the original set (explicitely) to an }
  1598. { fpc_normal_set so we can pass it to the helper }
  1599. left := ctypeconvnode.create_internal(left,srsym.restype);
  1600. { add a range or a single element? }
  1601. if assigned(tsetelementnode(right).right) then
  1602. begin
  1603. tsetelementnode(right).right :=
  1604. ctypeconvnode.create_internal(tsetelementnode(right).right,
  1605. u8inttype);
  1606. { create the call }
  1607. result := ccallnode.createinternres('fpc_set_set_range',
  1608. ccallparanode.create(tsetelementnode(right).right,
  1609. ccallparanode.create(tsetelementnode(right).left,
  1610. ccallparanode.create(left,nil))),resulttype);
  1611. end
  1612. else
  1613. begin
  1614. result := ccallnode.createinternres('fpc_set_set_byte',
  1615. ccallparanode.create(tsetelementnode(right).left,
  1616. ccallparanode.create(left,nil)),resulttype);
  1617. end;
  1618. { remove reused parts from original node }
  1619. tsetelementnode(right).right := nil;
  1620. tsetelementnode(right).left := nil;
  1621. left := nil;
  1622. end
  1623. else
  1624. begin
  1625. { add two sets }
  1626. { convert the sets to fpc_normal_set's }
  1627. result := ccallnode.createinternres('fpc_set_add_sets',
  1628. ccallparanode.create(
  1629. ctypeconvnode.create_explicit(right,srsym.restype),
  1630. ccallparanode.create(
  1631. ctypeconvnode.create_internal(left,srsym.restype),nil)),resulttype);
  1632. { remove reused parts from original node }
  1633. left := nil;
  1634. right := nil;
  1635. end;
  1636. end
  1637. end;
  1638. subn,symdifn,muln:
  1639. begin
  1640. { convert the sets to fpc_normal_set's }
  1641. paras := ccallparanode.create(ctypeconvnode.create_internal(right,srsym.restype),
  1642. ccallparanode.create(ctypeconvnode.create_internal(left,srsym.restype),nil));
  1643. case nodetype of
  1644. subn:
  1645. result := ccallnode.createinternres('fpc_set_sub_sets',
  1646. paras,resulttype);
  1647. symdifn:
  1648. result := ccallnode.createinternres('fpc_set_symdif_sets',
  1649. paras,resulttype);
  1650. muln:
  1651. result := ccallnode.createinternres('fpc_set_mul_sets',
  1652. paras,resulttype);
  1653. end;
  1654. { remove reused parts from original node }
  1655. left := nil;
  1656. right := nil;
  1657. end;
  1658. else
  1659. internalerror(200108311);
  1660. end;
  1661. end;
  1662. function taddnode.first_add64bitint: tnode;
  1663. var
  1664. procname: string[31];
  1665. temp: tnode;
  1666. power: longint;
  1667. begin
  1668. result := nil;
  1669. { create helper calls mul }
  1670. if nodetype <> muln then
  1671. exit;
  1672. { make sure that if there is a constant, that it's on the right }
  1673. if left.nodetype = ordconstn then
  1674. begin
  1675. temp := right;
  1676. right := left;
  1677. left := temp;
  1678. end;
  1679. { can we use a shift instead of a mul? }
  1680. if not (cs_check_overflow in aktlocalswitches) and
  1681. (right.nodetype = ordconstn) and
  1682. ispowerof2(tordconstnode(right).value,power) then
  1683. begin
  1684. tordconstnode(right).value := power;
  1685. result := cshlshrnode.create(shln,left,right);
  1686. { left and right are reused }
  1687. left := nil;
  1688. right := nil;
  1689. { return firstpassed new node }
  1690. exit;
  1691. end;
  1692. { when currency is used set the result of the
  1693. parameters to s64bit, so they are not converted }
  1694. if is_currency(resulttype.def) then
  1695. begin
  1696. left.resulttype:=s64inttype;
  1697. right.resulttype:=s64inttype;
  1698. end;
  1699. { otherwise, create the parameters for the helper }
  1700. right := ccallparanode.create(
  1701. cordconstnode.create(ord(cs_check_overflow in aktlocalswitches),booltype,true),
  1702. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1703. left := nil;
  1704. { only qword needs the unsigned code, the
  1705. signed code is also used for currency }
  1706. if is_signed(resulttype.def) then
  1707. procname := 'fpc_mul_int64'
  1708. else
  1709. procname := 'fpc_mul_qword';
  1710. result := ccallnode.createintern(procname,right);
  1711. right := nil;
  1712. end;
  1713. function taddnode.first_addfloat : tnode;
  1714. var
  1715. procname: string[31];
  1716. temp: tnode;
  1717. power: longint;
  1718. { do we need to reverse the result ? }
  1719. notnode : boolean;
  1720. begin
  1721. result := nil;
  1722. notnode := false;
  1723. { In non-emulation mode, real opcodes are
  1724. emitted for floating point values.
  1725. }
  1726. if not (cs_fp_emulation in aktmoduleswitches) then
  1727. exit;
  1728. if not(target_info.system in system_wince) then
  1729. begin
  1730. case tfloatdef(left.resulttype.def).typ of
  1731. s32real:
  1732. procname:='float32';
  1733. s64real:
  1734. procname:='float64';
  1735. {!!! not yet implemented
  1736. s128real:
  1737. }
  1738. else
  1739. internalerror(2005082601);
  1740. end;
  1741. case nodetype of
  1742. addn:
  1743. procname:=procname+'_add';
  1744. muln:
  1745. procname:=procname+'_mul';
  1746. subn:
  1747. procname:=procname+'_sub';
  1748. slashn:
  1749. procname:=procname+'_div';
  1750. ltn:
  1751. procname:=procname+'_lt';
  1752. lten:
  1753. procname:=procname+'_le';
  1754. gtn:
  1755. begin
  1756. procname:=procname+'_le';
  1757. notnode:=true;
  1758. end;
  1759. gten:
  1760. begin
  1761. procname:=procname+'_lt';
  1762. notnode:=true;
  1763. end;
  1764. equaln:
  1765. procname:=procname+'_eq';
  1766. unequaln:
  1767. begin
  1768. procname:=procname+'_eq';
  1769. notnode:=true;
  1770. end;
  1771. else
  1772. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resulttype.def.typename,right.resulttype.def.typename);
  1773. end;
  1774. end
  1775. else
  1776. begin
  1777. case nodetype of
  1778. addn:
  1779. procname:='ADD';
  1780. muln:
  1781. procname:='MUL';
  1782. subn:
  1783. procname:='SUB';
  1784. slashn:
  1785. procname:='DIV';
  1786. ltn:
  1787. procname:='LT';
  1788. lten:
  1789. procname:='LE';
  1790. gtn:
  1791. procname:='GT';
  1792. gten:
  1793. procname:='GE';
  1794. equaln:
  1795. procname:='EQ';
  1796. unequaln:
  1797. procname:='NE';
  1798. else
  1799. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resulttype.def.typename,right.resulttype.def.typename);
  1800. end;
  1801. case tfloatdef(left.resulttype.def).typ of
  1802. s32real:
  1803. procname:=procname+'S';
  1804. s64real:
  1805. procname:=procname+'D';
  1806. {!!! not yet implemented
  1807. s128real:
  1808. }
  1809. else
  1810. internalerror(2005082602);
  1811. end;
  1812. end;
  1813. result:=ccallnode.createintern(procname,ccallparanode.create(right,
  1814. ccallparanode.create(left,nil)));
  1815. left:=nil;
  1816. right:=nil;
  1817. { do we need to reverse the result }
  1818. if notnode then
  1819. result:=cnotnode.create(result);
  1820. end;
  1821. function taddnode.pass_1 : tnode;
  1822. var
  1823. {$ifdef addstringopt}
  1824. hp : tnode;
  1825. {$endif addstringopt}
  1826. lt,rt : tnodetype;
  1827. rd,ld : tdef;
  1828. begin
  1829. result:=nil;
  1830. { first do the two subtrees }
  1831. firstpass(left);
  1832. firstpass(right);
  1833. if codegenerror then
  1834. exit;
  1835. { load easier access variables }
  1836. rd:=right.resulttype.def;
  1837. ld:=left.resulttype.def;
  1838. rt:=right.nodetype;
  1839. lt:=left.nodetype;
  1840. { int/int gives real/real! }
  1841. if nodetype=slashn then
  1842. begin
  1843. {$ifdef cpufpemu}
  1844. if (aktfputype=fpu_soft) or (cs_fp_emulation in aktmoduleswitches) then
  1845. begin
  1846. result:=first_addfloat;
  1847. if assigned(result) then
  1848. exit;
  1849. end;
  1850. {$endif cpufpemu}
  1851. expectloc:=LOC_FPUREGISTER;
  1852. { maybe we need an integer register to save }
  1853. { a reference }
  1854. if ((left.expectloc<>LOC_FPUREGISTER) or
  1855. (right.expectloc<>LOC_FPUREGISTER)) and
  1856. (left.registersint=right.registersint) then
  1857. calcregisters(self,1,1,0)
  1858. else
  1859. calcregisters(self,0,1,0);
  1860. { an add node always first loads both the left and the }
  1861. { right in the fpu before doing the calculation. However, }
  1862. { calcregisters(0,2,0) will overestimate the number of }
  1863. { necessary registers (it will make it 3 in case one of }
  1864. { the operands is already in the fpu) (JM) }
  1865. if ((left.expectloc<>LOC_FPUREGISTER) or
  1866. (right.expectloc<>LOC_FPUREGISTER)) and
  1867. (registersfpu < 2) then
  1868. inc(registersfpu);
  1869. end
  1870. { if both are orddefs then check sub types }
  1871. else if (ld.deftype=orddef) and (rd.deftype=orddef) then
  1872. begin
  1873. { 2 booleans ? }
  1874. if is_boolean(ld) and is_boolean(rd) then
  1875. begin
  1876. if not(cs_full_boolean_eval in aktlocalswitches) and
  1877. (nodetype in [andn,orn]) then
  1878. begin
  1879. expectloc:=LOC_JUMP;
  1880. calcregisters(self,0,0,0);
  1881. end
  1882. else
  1883. begin
  1884. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  1885. begin
  1886. expectloc:=LOC_FLAGS;
  1887. if (left.expectloc in [LOC_JUMP,LOC_FLAGS]) and
  1888. (left.expectloc in [LOC_JUMP,LOC_FLAGS]) then
  1889. calcregisters(self,2,0,0)
  1890. else
  1891. calcregisters(self,1,0,0);
  1892. end
  1893. else
  1894. begin
  1895. expectloc:=LOC_REGISTER;
  1896. calcregisters(self,0,0,0);
  1897. end;
  1898. end;
  1899. end
  1900. else
  1901. { Both are chars? only convert to shortstrings for addn }
  1902. if is_char(ld) then
  1903. begin
  1904. if nodetype=addn then
  1905. internalerror(200103291);
  1906. expectloc:=LOC_FLAGS;
  1907. calcregisters(self,1,0,0);
  1908. end
  1909. {$ifndef cpu64bit}
  1910. { is there a 64 bit type ? }
  1911. else if (torddef(ld).typ in [s64bit,u64bit,scurrency]) then
  1912. begin
  1913. result := first_add64bitint;
  1914. if assigned(result) then
  1915. exit;
  1916. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1917. expectloc:=LOC_REGISTER
  1918. else
  1919. expectloc:=LOC_JUMP;
  1920. calcregisters(self,2,0,0)
  1921. end
  1922. {$endif cpu64bit}
  1923. { is there a cardinal? }
  1924. else if (torddef(ld).typ=u32bit) then
  1925. begin
  1926. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1927. expectloc:=LOC_REGISTER
  1928. else
  1929. expectloc:=LOC_FLAGS;
  1930. calcregisters(self,1,0,0);
  1931. { for unsigned mul we need an extra register }
  1932. if nodetype=muln then
  1933. inc(registersint);
  1934. end
  1935. { generic s32bit conversion }
  1936. else
  1937. begin
  1938. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1939. expectloc:=LOC_REGISTER
  1940. else
  1941. expectloc:=LOC_FLAGS;
  1942. calcregisters(self,1,0,0);
  1943. end;
  1944. end
  1945. { left side a setdef, must be before string processing,
  1946. else array constructor can be seen as array of char (PFV) }
  1947. else if (ld.deftype=setdef) then
  1948. begin
  1949. if tsetdef(ld).settype=smallset then
  1950. begin
  1951. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  1952. expectloc:=LOC_FLAGS
  1953. else
  1954. expectloc:=LOC_REGISTER;
  1955. { are we adding set elements ? }
  1956. if right.nodetype=setelementn then
  1957. calcregisters(self,2,0,0)
  1958. else
  1959. calcregisters(self,1,0,0);
  1960. end
  1961. else
  1962. {$ifdef MMXSET}
  1963. {$ifdef i386}
  1964. if cs_mmx in aktlocalswitches then
  1965. begin
  1966. expectloc:=LOC_MMXREGISTER;
  1967. calcregisters(self,0,0,4);
  1968. end
  1969. else
  1970. {$endif}
  1971. {$endif MMXSET}
  1972. begin
  1973. result := first_addset;
  1974. if assigned(result) then
  1975. exit;
  1976. expectloc:=LOC_CREFERENCE;
  1977. calcregisters(self,0,0,0);
  1978. { here we call SET... }
  1979. include(current_procinfo.flags,pi_do_call);
  1980. end;
  1981. end
  1982. { compare pchar by addresses like BP/Delphi }
  1983. else if is_pchar(ld) then
  1984. begin
  1985. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1986. expectloc:=LOC_REGISTER
  1987. else
  1988. expectloc:=LOC_FLAGS;
  1989. calcregisters(self,1,0,0);
  1990. end
  1991. { is one of the operands a string }
  1992. else if (ld.deftype=stringdef) then
  1993. begin
  1994. if is_widestring(ld) then
  1995. begin
  1996. { this is only for add, the comparisaion is handled later }
  1997. expectloc:=LOC_REGISTER;
  1998. end
  1999. else if is_ansistring(ld) then
  2000. begin
  2001. { this is only for add, the comparisaion is handled later }
  2002. expectloc:=LOC_REGISTER;
  2003. end
  2004. else if is_longstring(ld) then
  2005. begin
  2006. { this is only for add, the comparisaion is handled later }
  2007. expectloc:=LOC_REFERENCE;
  2008. end
  2009. else
  2010. begin
  2011. {$ifdef addstringopt}
  2012. { can create a call which isn't handled by callparatemp }
  2013. if canbeaddsstringcharoptnode(self) then
  2014. begin
  2015. hp := genaddsstringcharoptnode(self);
  2016. pass_1 := hp;
  2017. exit;
  2018. end
  2019. else
  2020. {$endif addstringopt}
  2021. begin
  2022. { Fix right to be shortstring }
  2023. if is_char(right.resulttype.def) then
  2024. begin
  2025. inserttypeconv(right,cshortstringtype);
  2026. firstpass(right);
  2027. end;
  2028. end;
  2029. {$ifdef addstringopt}
  2030. { can create a call which isn't handled by callparatemp }
  2031. if canbeaddsstringcsstringoptnode(self) then
  2032. begin
  2033. hp := genaddsstringcsstringoptnode(self);
  2034. pass_1 := hp;
  2035. exit;
  2036. end;
  2037. {$endif addstringopt}
  2038. end;
  2039. { otherwise, let addstring convert everything }
  2040. result := first_addstring;
  2041. exit;
  2042. end
  2043. { is one a real float ? }
  2044. else if (rd.deftype=floatdef) or (ld.deftype=floatdef) then
  2045. begin
  2046. {$ifdef cpufpemu}
  2047. if (aktfputype=fpu_soft) or (cs_fp_emulation in aktmoduleswitches) then
  2048. begin
  2049. result:=first_addfloat;
  2050. if assigned(result) then
  2051. exit;
  2052. end;
  2053. {$endif cpufpemu}
  2054. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2055. expectloc:=LOC_FPUREGISTER
  2056. else
  2057. expectloc:=LOC_FLAGS;
  2058. calcregisters(self,0,1,0);
  2059. { an add node always first loads both the left and the }
  2060. { right in the fpu before doing the calculation. However, }
  2061. { calcregisters(0,2,0) will overestimate the number of }
  2062. { necessary registers (it will make it 3 in case one of }
  2063. { the operands is already in the fpu) (JM) }
  2064. if ((left.expectloc<>LOC_FPUREGISTER) or
  2065. (right.expectloc<>LOC_FPUREGISTER)) and
  2066. (registersfpu < 2) then
  2067. inc(registersfpu);
  2068. end
  2069. { pointer comperation and subtraction }
  2070. else if (ld.deftype=pointerdef) then
  2071. begin
  2072. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2073. expectloc:=LOC_REGISTER
  2074. else
  2075. expectloc:=LOC_FLAGS;
  2076. calcregisters(self,1,0,0);
  2077. end
  2078. else if is_class_or_interface(ld) then
  2079. begin
  2080. expectloc:=LOC_FLAGS;
  2081. calcregisters(self,1,0,0);
  2082. end
  2083. else if (ld.deftype=classrefdef) then
  2084. begin
  2085. expectloc:=LOC_FLAGS;
  2086. calcregisters(self,1,0,0);
  2087. end
  2088. { support procvar=nil,procvar<>nil }
  2089. else if ((ld.deftype=procvardef) and (rt=niln)) or
  2090. ((rd.deftype=procvardef) and (lt=niln)) then
  2091. begin
  2092. expectloc:=LOC_FLAGS;
  2093. calcregisters(self,1,0,0);
  2094. end
  2095. {$ifdef SUPPORT_MMX}
  2096. { mmx support, this must be before the zero based array
  2097. check }
  2098. else if (cs_mmx in aktlocalswitches) and is_mmx_able_array(ld) and
  2099. is_mmx_able_array(rd) then
  2100. begin
  2101. expectloc:=LOC_MMXREGISTER;
  2102. calcregisters(self,0,0,1);
  2103. end
  2104. {$endif SUPPORT_MMX}
  2105. else if (rd.deftype=pointerdef) or (ld.deftype=pointerdef) then
  2106. begin
  2107. expectloc:=LOC_REGISTER;
  2108. calcregisters(self,1,0,0);
  2109. end
  2110. else if (rd.deftype=procvardef) and
  2111. (ld.deftype=procvardef) and
  2112. equal_defs(rd,ld) then
  2113. begin
  2114. expectloc:=LOC_FLAGS;
  2115. calcregisters(self,1,0,0);
  2116. end
  2117. else if (ld.deftype=enumdef) then
  2118. begin
  2119. expectloc:=LOC_FLAGS;
  2120. calcregisters(self,1,0,0);
  2121. end
  2122. {$ifdef SUPPORT_MMX}
  2123. else if (cs_mmx in aktlocalswitches) and
  2124. is_mmx_able_array(ld) and
  2125. is_mmx_able_array(rd) then
  2126. begin
  2127. expectloc:=LOC_MMXREGISTER;
  2128. calcregisters(self,0,0,1);
  2129. end
  2130. {$endif SUPPORT_MMX}
  2131. { the general solution is to convert to 32 bit int }
  2132. else
  2133. begin
  2134. expectloc:=LOC_REGISTER;
  2135. calcregisters(self,1,0,0);
  2136. end;
  2137. end;
  2138. {$ifdef state_tracking}
  2139. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  2140. var factval:Tnode;
  2141. begin
  2142. track_state_pass:=false;
  2143. if left.track_state_pass(exec_known) then
  2144. begin
  2145. track_state_pass:=true;
  2146. left.resulttype.def:=nil;
  2147. do_resulttypepass(left);
  2148. end;
  2149. factval:=aktstate.find_fact(left);
  2150. if factval<>nil then
  2151. begin
  2152. track_state_pass:=true;
  2153. left.destroy;
  2154. left:=factval.getcopy;
  2155. end;
  2156. if right.track_state_pass(exec_known) then
  2157. begin
  2158. track_state_pass:=true;
  2159. right.resulttype.def:=nil;
  2160. do_resulttypepass(right);
  2161. end;
  2162. factval:=aktstate.find_fact(right);
  2163. if factval<>nil then
  2164. begin
  2165. track_state_pass:=true;
  2166. right.destroy;
  2167. right:=factval.getcopy;
  2168. end;
  2169. end;
  2170. {$endif}
  2171. begin
  2172. caddnode:=taddnode;
  2173. end.