nadd.pas 89 KB

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