nadd.pas 88 KB

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