nadd.pas 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203
  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. end
  1237. { support dynamicarray=nil,dynamicarray<>nil }
  1238. else if (is_dynamic_array(ld) and (rt=niln)) or
  1239. (is_dynamic_array(rd) and (lt=niln)) or
  1240. (is_dynamic_array(ld) and is_dynamic_array(rd)) then
  1241. begin
  1242. if not(nodetype in [equaln,unequaln]) then
  1243. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1244. end
  1245. {$ifdef SUPPORT_MMX}
  1246. { mmx support, this must be before the zero based array
  1247. check }
  1248. else if (cs_mmx in aktlocalswitches) and
  1249. is_mmx_able_array(ld) and
  1250. is_mmx_able_array(rd) and
  1251. equal_defs(ld,rd) then
  1252. begin
  1253. case nodetype of
  1254. addn,subn,xorn,orn,andn:
  1255. ;
  1256. { mul is a little bit restricted }
  1257. muln:
  1258. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  1259. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1260. else
  1261. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1262. end;
  1263. end
  1264. {$endif SUPPORT_MMX}
  1265. { this is a little bit dangerous, also the left type }
  1266. { pointer to should be checked! This broke the mmx support }
  1267. else if (rd.deftype=pointerdef) or is_zero_based_array(rd) then
  1268. begin
  1269. if is_zero_based_array(rd) then
  1270. begin
  1271. resulttype.setdef(tpointerdef.create(tarraydef(rd).elementtype));
  1272. inserttypeconv(right,resulttype);
  1273. end
  1274. else
  1275. resulttype:=right.resulttype;
  1276. inserttypeconv(left,sinttype);
  1277. if nodetype=addn then
  1278. begin
  1279. if not(cs_extsyntax in aktmoduleswitches) or
  1280. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  1281. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1282. if (rd.deftype=pointerdef) and
  1283. (tpointerdef(rd).pointertype.def.size>1) then
  1284. begin
  1285. left:=caddnode.create(muln,left,
  1286. cordconstnode.create(tpointerdef(rd).pointertype.def.size,sinttype,true));
  1287. end;
  1288. end
  1289. else
  1290. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1291. end
  1292. else if (ld.deftype=pointerdef) or is_zero_based_array(ld) then
  1293. begin
  1294. if is_zero_based_array(ld) then
  1295. begin
  1296. resulttype.setdef(tpointerdef.create(tarraydef(ld).elementtype));
  1297. inserttypeconv(left,resulttype);
  1298. end
  1299. else
  1300. resulttype:=left.resulttype;
  1301. inserttypeconv(right,sinttype);
  1302. if nodetype in [addn,subn] then
  1303. begin
  1304. if not(cs_extsyntax in aktmoduleswitches) or
  1305. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  1306. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1307. if (ld.deftype=pointerdef) and
  1308. (tpointerdef(ld).pointertype.def.size>1) then
  1309. begin
  1310. right:=caddnode.create(muln,right,
  1311. cordconstnode.create(tpointerdef(ld).pointertype.def.size,sinttype,true));
  1312. end
  1313. else
  1314. if is_zero_based_array(ld) and
  1315. (tarraydef(ld).elementtype.def.size>1) then
  1316. begin
  1317. right:=caddnode.create(muln,right,
  1318. cordconstnode.create(tarraydef(ld).elementtype.def.size,sinttype,true));
  1319. end;
  1320. end
  1321. else
  1322. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1323. end
  1324. else if (rd.deftype=procvardef) and
  1325. (ld.deftype=procvardef) and
  1326. equal_defs(rd,ld) then
  1327. begin
  1328. if (nodetype in [equaln,unequaln]) then
  1329. begin
  1330. if tprocvardef(rd).is_addressonly then
  1331. begin
  1332. inserttypeconv_internal(right,voidpointertype);
  1333. inserttypeconv_internal(left,voidpointertype);
  1334. end
  1335. else
  1336. begin
  1337. { find proc field in methodpointer record }
  1338. hsym:=tfieldvarsym(trecorddef(methodpointertype.def).symtable.search('proc'));
  1339. if not assigned(hsym) then
  1340. internalerror(200412043);
  1341. { Compare tmehodpointer(left).proc }
  1342. right:=csubscriptnode.create(
  1343. hsym,
  1344. ctypeconvnode.create_internal(right,methodpointertype));
  1345. left:=csubscriptnode.create(
  1346. hsym,
  1347. ctypeconvnode.create_internal(left,methodpointertype));
  1348. end;
  1349. end
  1350. else
  1351. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1352. end
  1353. { enums }
  1354. else if (ld.deftype=enumdef) and (rd.deftype=enumdef) then
  1355. begin
  1356. if allowenumop(nodetype) then
  1357. inserttypeconv(right,left.resulttype)
  1358. else
  1359. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1360. end
  1361. { generic conversion, this is for error recovery }
  1362. else
  1363. begin
  1364. inserttypeconv(left,sinttype);
  1365. inserttypeconv(right,sinttype);
  1366. end;
  1367. { set resulttype if not already done }
  1368. if not assigned(resulttype.def) then
  1369. begin
  1370. case nodetype of
  1371. ltn,lten,gtn,gten,equaln,unequaln :
  1372. resulttype:=booltype;
  1373. slashn :
  1374. resulttype:=resultrealtype;
  1375. addn:
  1376. begin
  1377. { for strings, return is always a 255 char string }
  1378. if is_shortstring(left.resulttype.def) then
  1379. resulttype:=cshortstringtype
  1380. else
  1381. resulttype:=left.resulttype;
  1382. end;
  1383. else
  1384. resulttype:=left.resulttype;
  1385. end;
  1386. end;
  1387. { when the result is currency we need some extra code for
  1388. multiplication and division. this should not be done when
  1389. the muln or slashn node is created internally }
  1390. if not(nf_is_currency in flags) and
  1391. is_currency(resulttype.def) then
  1392. begin
  1393. case nodetype of
  1394. slashn :
  1395. begin
  1396. { slashn will only work with floats }
  1397. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  1398. include(hp.flags,nf_is_currency);
  1399. result:=hp;
  1400. end;
  1401. muln :
  1402. begin
  1403. if s64currencytype.def.deftype=floatdef then
  1404. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype))
  1405. else
  1406. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  1407. include(hp.flags,nf_is_currency);
  1408. result:=hp
  1409. end;
  1410. end;
  1411. end;
  1412. end;
  1413. function taddnode.first_addstring: tnode;
  1414. var
  1415. p: tnode;
  1416. begin
  1417. { when we get here, we are sure that both the left and the right }
  1418. { node are both strings of the same stringtype (JM) }
  1419. case nodetype of
  1420. addn:
  1421. begin
  1422. { create the call to the concat routine both strings as arguments }
  1423. result := ccallnode.createintern('fpc_'+
  1424. tstringdef(resulttype.def).stringtypname+'_concat',
  1425. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1426. { we reused the arguments }
  1427. left := nil;
  1428. right := nil;
  1429. end;
  1430. ltn,lten,gtn,gten,equaln,unequaln :
  1431. begin
  1432. { generate better code for comparison with empty string, we
  1433. only need to compare the length with 0 }
  1434. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  1435. (((left.nodetype=stringconstn) and (str_length(left)=0)) or
  1436. ((right.nodetype=stringconstn) and (str_length(right)=0))) then
  1437. begin
  1438. { switch so that the constant is always on the right }
  1439. if left.nodetype = stringconstn then
  1440. begin
  1441. p := left;
  1442. left := right;
  1443. right := p;
  1444. end;
  1445. if is_shortstring(left.resulttype.def) or
  1446. (nodetype in [gtn,gten,ltn,lten]) then
  1447. { compare the length with 0 }
  1448. result := caddnode.create(nodetype,
  1449. cinlinenode.create(in_length_x,false,left),
  1450. cordconstnode.create(0,s32inttype,false))
  1451. else
  1452. begin
  1453. { compare the pointer with nil (for ansistrings etc), }
  1454. { faster than getting the length (JM) }
  1455. result:= caddnode.create(nodetype,
  1456. ctypeconvnode.create_internal(left,voidpointertype),
  1457. cpointerconstnode.create(0,voidpointertype));
  1458. end;
  1459. { left is reused }
  1460. left := nil;
  1461. { right isn't }
  1462. right.free;
  1463. right := nil;
  1464. exit;
  1465. end;
  1466. { no string constant -> call compare routine }
  1467. result := ccallnode.createintern('fpc_'+
  1468. tstringdef(left.resulttype.def).stringtypname+'_compare',
  1469. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1470. { and compare its result with 0 according to the original operator }
  1471. result := caddnode.create(nodetype,result,
  1472. cordconstnode.create(0,s32inttype,false));
  1473. left := nil;
  1474. right := nil;
  1475. end;
  1476. end;
  1477. end;
  1478. function taddnode.first_addset: tnode;
  1479. var
  1480. procname: string[31];
  1481. tempn: tnode;
  1482. paras: tcallparanode;
  1483. srsym: ttypesym;
  1484. begin
  1485. { get the sym that represents the fpc_normal_set type }
  1486. if not searchsystype('FPC_NORMAL_SET',srsym) then
  1487. internalerror(200108313);
  1488. case nodetype of
  1489. equaln,unequaln,lten,gten:
  1490. begin
  1491. case nodetype of
  1492. equaln,unequaln:
  1493. procname := 'fpc_set_comp_sets';
  1494. lten,gten:
  1495. begin
  1496. procname := 'fpc_set_contains_sets';
  1497. { (left >= right) = (right <= left) }
  1498. if nodetype = gten then
  1499. begin
  1500. tempn := left;
  1501. left := right;
  1502. right := tempn;
  1503. end;
  1504. end;
  1505. end;
  1506. { convert the arguments (explicitely) to fpc_normal_set's }
  1507. left := ctypeconvnode.create_internal(left,srsym.restype);
  1508. right := ctypeconvnode.create_internal(right,srsym.restype);
  1509. result := ccallnode.createintern(procname,ccallparanode.create(right,
  1510. ccallparanode.create(left,nil)));
  1511. { left and right are reused as parameters }
  1512. left := nil;
  1513. right := nil;
  1514. { for an unequaln, we have to negate the result of comp_sets }
  1515. if nodetype = unequaln then
  1516. result := cnotnode.create(result);
  1517. end;
  1518. addn:
  1519. begin
  1520. { optimize first loading of a set }
  1521. if (right.nodetype=setelementn) and
  1522. not(assigned(tsetelementnode(right).right)) and
  1523. is_emptyset(left) then
  1524. begin
  1525. { type cast the value to pass as argument to a byte, }
  1526. { since that's what the helper expects }
  1527. tsetelementnode(right).left :=
  1528. ctypeconvnode.create_internal(tsetelementnode(right).left,u8inttype);
  1529. { set the resulttype to the actual one (otherwise it's }
  1530. { "fpc_normal_set") }
  1531. result := ccallnode.createinternres('fpc_set_create_element',
  1532. ccallparanode.create(tsetelementnode(right).left,nil),
  1533. resulttype);
  1534. { reused }
  1535. tsetelementnode(right).left := nil;
  1536. end
  1537. else
  1538. begin
  1539. if right.nodetype=setelementn then
  1540. begin
  1541. { convert the arguments to bytes, since that's what }
  1542. { the helper expects }
  1543. tsetelementnode(right).left :=
  1544. ctypeconvnode.create_internal(tsetelementnode(right).left,
  1545. u8inttype);
  1546. { convert the original set (explicitely) to an }
  1547. { fpc_normal_set so we can pass it to the helper }
  1548. left := ctypeconvnode.create_internal(left,srsym.restype);
  1549. { add a range or a single element? }
  1550. if assigned(tsetelementnode(right).right) then
  1551. begin
  1552. tsetelementnode(right).right :=
  1553. ctypeconvnode.create_internal(tsetelementnode(right).right,
  1554. u8inttype);
  1555. { create the call }
  1556. result := ccallnode.createinternres('fpc_set_set_range',
  1557. ccallparanode.create(tsetelementnode(right).right,
  1558. ccallparanode.create(tsetelementnode(right).left,
  1559. ccallparanode.create(left,nil))),resulttype);
  1560. end
  1561. else
  1562. begin
  1563. result := ccallnode.createinternres('fpc_set_set_byte',
  1564. ccallparanode.create(tsetelementnode(right).left,
  1565. ccallparanode.create(left,nil)),resulttype);
  1566. end;
  1567. { remove reused parts from original node }
  1568. tsetelementnode(right).right := nil;
  1569. tsetelementnode(right).left := nil;
  1570. left := nil;
  1571. end
  1572. else
  1573. begin
  1574. { add two sets }
  1575. { convert the sets to fpc_normal_set's }
  1576. result := ccallnode.createinternres('fpc_set_add_sets',
  1577. ccallparanode.create(
  1578. ctypeconvnode.create_explicit(right,srsym.restype),
  1579. ccallparanode.create(
  1580. ctypeconvnode.create_internal(left,srsym.restype),nil)),resulttype);
  1581. { remove reused parts from original node }
  1582. left := nil;
  1583. right := nil;
  1584. end;
  1585. end
  1586. end;
  1587. subn,symdifn,muln:
  1588. begin
  1589. { convert the sets to fpc_normal_set's }
  1590. paras := ccallparanode.create(ctypeconvnode.create_internal(right,srsym.restype),
  1591. ccallparanode.create(ctypeconvnode.create_internal(left,srsym.restype),nil));
  1592. case nodetype of
  1593. subn:
  1594. result := ccallnode.createinternres('fpc_set_sub_sets',
  1595. paras,resulttype);
  1596. symdifn:
  1597. result := ccallnode.createinternres('fpc_set_symdif_sets',
  1598. paras,resulttype);
  1599. muln:
  1600. result := ccallnode.createinternres('fpc_set_mul_sets',
  1601. paras,resulttype);
  1602. end;
  1603. { remove reused parts from original node }
  1604. left := nil;
  1605. right := nil;
  1606. end;
  1607. else
  1608. internalerror(200108311);
  1609. end;
  1610. end;
  1611. function taddnode.first_add64bitint: tnode;
  1612. var
  1613. procname: string[31];
  1614. temp: tnode;
  1615. power: longint;
  1616. begin
  1617. result := nil;
  1618. { create helper calls mul }
  1619. if nodetype <> muln then
  1620. exit;
  1621. { make sure that if there is a constant, that it's on the right }
  1622. if left.nodetype = ordconstn then
  1623. begin
  1624. temp := right;
  1625. right := left;
  1626. left := temp;
  1627. end;
  1628. { can we use a shift instead of a mul? }
  1629. if not (cs_check_overflow in aktlocalswitches) and
  1630. (right.nodetype = ordconstn) and
  1631. ispowerof2(tordconstnode(right).value,power) then
  1632. begin
  1633. tordconstnode(right).value := power;
  1634. result := cshlshrnode.create(shln,left,right);
  1635. { left and right are reused }
  1636. left := nil;
  1637. right := nil;
  1638. { return firstpassed new node }
  1639. exit;
  1640. end;
  1641. { when currency is used set the result of the
  1642. parameters to s64bit, so they are not converted }
  1643. if is_currency(resulttype.def) then
  1644. begin
  1645. left.resulttype:=s64inttype;
  1646. right.resulttype:=s64inttype;
  1647. end;
  1648. { otherwise, create the parameters for the helper }
  1649. right := ccallparanode.create(
  1650. cordconstnode.create(ord(cs_check_overflow in aktlocalswitches),booltype,true),
  1651. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1652. left := nil;
  1653. { only qword needs the unsigned code, the
  1654. signed code is also used for currency }
  1655. if is_signed(resulttype.def) then
  1656. procname := 'fpc_mul_int64'
  1657. else
  1658. procname := 'fpc_mul_qword';
  1659. result := ccallnode.createintern(procname,right);
  1660. right := nil;
  1661. end;
  1662. {$ifdef cpufpemu}
  1663. function taddnode.first_addfloat: tnode;
  1664. var
  1665. procname: string[31];
  1666. temp: tnode;
  1667. power: longint;
  1668. { do we need to reverse the result ? }
  1669. notnode : boolean;
  1670. begin
  1671. result := nil;
  1672. notnode := false;
  1673. { In non-emulation mode, real opcodes are
  1674. emitted for floating point values.
  1675. }
  1676. if not (cs_fp_emulation in aktmoduleswitches) then
  1677. exit;
  1678. case nodetype of
  1679. addn : procname := 'fpc_single_add';
  1680. muln : procname := 'fpc_single_mul';
  1681. subn : procname := 'fpc_single_sub';
  1682. slashn : procname := 'fpc_single_div';
  1683. ltn : procname := 'fpc_single_lt';
  1684. lten: procname := 'fpc_single_le';
  1685. gtn:
  1686. begin
  1687. procname := 'fpc_single_le';
  1688. notnode := true;
  1689. end;
  1690. gten:
  1691. begin
  1692. procname := 'fpc_single_lt';
  1693. notnode := true;
  1694. end;
  1695. equaln: procname := 'fpc_single_eq';
  1696. unequaln :
  1697. begin
  1698. procname := 'fpc_single_eq';
  1699. notnode := true;
  1700. end;
  1701. else
  1702. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resulttype.def.typename,right.resulttype.def.typename);
  1703. end;
  1704. { convert the arguments (explicitely) to fpc_normal_set's }
  1705. result := ccallnode.createintern(procname,ccallparanode.create(right,
  1706. ccallparanode.create(left,nil)));
  1707. left:=nil;
  1708. right:=nil;
  1709. { do we need to reverse the result }
  1710. if notnode then
  1711. result := cnotnode.create(result);
  1712. end;
  1713. {$endif cpufpemu}
  1714. function taddnode.pass_1 : tnode;
  1715. var
  1716. {$ifdef addstringopt}
  1717. hp : tnode;
  1718. {$endif addstringopt}
  1719. lt,rt : tnodetype;
  1720. rd,ld : tdef;
  1721. begin
  1722. result:=nil;
  1723. { first do the two subtrees }
  1724. firstpass(left);
  1725. firstpass(right);
  1726. if codegenerror then
  1727. exit;
  1728. { load easier access variables }
  1729. rd:=right.resulttype.def;
  1730. ld:=left.resulttype.def;
  1731. rt:=right.nodetype;
  1732. lt:=left.nodetype;
  1733. { int/int gives real/real! }
  1734. if nodetype=slashn then
  1735. begin
  1736. {$ifdef cpufpemu}
  1737. result := first_addfloat;
  1738. if assigned(result) then
  1739. exit;
  1740. {$endif cpufpemu}
  1741. expectloc:=LOC_FPUREGISTER;
  1742. { maybe we need an integer register to save }
  1743. { a reference }
  1744. if ((left.expectloc<>LOC_FPUREGISTER) or
  1745. (right.expectloc<>LOC_FPUREGISTER)) and
  1746. (left.registersint=right.registersint) then
  1747. calcregisters(self,1,1,0)
  1748. else
  1749. calcregisters(self,0,1,0);
  1750. { an add node always first loads both the left and the }
  1751. { right in the fpu before doing the calculation. However, }
  1752. { calcregisters(0,2,0) will overestimate the number of }
  1753. { necessary registers (it will make it 3 in case one of }
  1754. { the operands is already in the fpu) (JM) }
  1755. if ((left.expectloc<>LOC_FPUREGISTER) or
  1756. (right.expectloc<>LOC_FPUREGISTER)) and
  1757. (registersfpu < 2) then
  1758. inc(registersfpu);
  1759. end
  1760. { if both are orddefs then check sub types }
  1761. else if (ld.deftype=orddef) and (rd.deftype=orddef) then
  1762. begin
  1763. { 2 booleans ? }
  1764. if is_boolean(ld) and is_boolean(rd) then
  1765. begin
  1766. if not(cs_full_boolean_eval in aktlocalswitches) and
  1767. (nodetype in [andn,orn]) then
  1768. begin
  1769. expectloc:=LOC_JUMP;
  1770. calcregisters(self,0,0,0);
  1771. end
  1772. else
  1773. begin
  1774. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  1775. begin
  1776. expectloc:=LOC_FLAGS;
  1777. if (left.expectloc in [LOC_JUMP,LOC_FLAGS]) and
  1778. (left.expectloc in [LOC_JUMP,LOC_FLAGS]) then
  1779. calcregisters(self,2,0,0)
  1780. else
  1781. calcregisters(self,1,0,0);
  1782. end
  1783. else
  1784. begin
  1785. expectloc:=LOC_REGISTER;
  1786. calcregisters(self,0,0,0);
  1787. end;
  1788. end;
  1789. end
  1790. else
  1791. { Both are chars? only convert to shortstrings for addn }
  1792. if is_char(ld) then
  1793. begin
  1794. if nodetype=addn then
  1795. internalerror(200103291);
  1796. expectloc:=LOC_FLAGS;
  1797. calcregisters(self,1,0,0);
  1798. end
  1799. {$ifndef cpu64bit}
  1800. { is there a 64 bit type ? }
  1801. else if (torddef(ld).typ in [s64bit,u64bit,scurrency]) then
  1802. begin
  1803. result := first_add64bitint;
  1804. if assigned(result) then
  1805. exit;
  1806. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1807. expectloc:=LOC_REGISTER
  1808. else
  1809. expectloc:=LOC_JUMP;
  1810. calcregisters(self,2,0,0)
  1811. end
  1812. {$endif cpu64bit}
  1813. { is there a cardinal? }
  1814. else if (torddef(ld).typ=u32bit) then
  1815. begin
  1816. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1817. expectloc:=LOC_REGISTER
  1818. else
  1819. expectloc:=LOC_FLAGS;
  1820. calcregisters(self,1,0,0);
  1821. { for unsigned mul we need an extra register }
  1822. if nodetype=muln then
  1823. inc(registersint);
  1824. end
  1825. { generic s32bit conversion }
  1826. else
  1827. begin
  1828. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1829. expectloc:=LOC_REGISTER
  1830. else
  1831. expectloc:=LOC_FLAGS;
  1832. calcregisters(self,1,0,0);
  1833. end;
  1834. end
  1835. { left side a setdef, must be before string processing,
  1836. else array constructor can be seen as array of char (PFV) }
  1837. else if (ld.deftype=setdef) then
  1838. begin
  1839. if tsetdef(ld).settype=smallset then
  1840. begin
  1841. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  1842. expectloc:=LOC_FLAGS
  1843. else
  1844. expectloc:=LOC_REGISTER;
  1845. { are we adding set elements ? }
  1846. if right.nodetype=setelementn then
  1847. calcregisters(self,2,0,0)
  1848. else
  1849. calcregisters(self,1,0,0);
  1850. end
  1851. else
  1852. {$ifdef MMXSET}
  1853. {$ifdef i386}
  1854. if cs_mmx in aktlocalswitches then
  1855. begin
  1856. expectloc:=LOC_MMXREGISTER;
  1857. calcregisters(self,0,0,4);
  1858. end
  1859. else
  1860. {$endif}
  1861. {$endif MMXSET}
  1862. begin
  1863. result := first_addset;
  1864. if assigned(result) then
  1865. exit;
  1866. expectloc:=LOC_CREFERENCE;
  1867. calcregisters(self,0,0,0);
  1868. { here we call SET... }
  1869. include(current_procinfo.flags,pi_do_call);
  1870. end;
  1871. end
  1872. { compare pchar by addresses like BP/Delphi }
  1873. else if is_pchar(ld) then
  1874. begin
  1875. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1876. expectloc:=LOC_REGISTER
  1877. else
  1878. expectloc:=LOC_FLAGS;
  1879. calcregisters(self,1,0,0);
  1880. end
  1881. { is one of the operands a string }
  1882. else if (ld.deftype=stringdef) then
  1883. begin
  1884. if is_widestring(ld) then
  1885. begin
  1886. { this is only for add, the comparisaion is handled later }
  1887. expectloc:=LOC_REGISTER;
  1888. end
  1889. else if is_ansistring(ld) then
  1890. begin
  1891. { this is only for add, the comparisaion is handled later }
  1892. expectloc:=LOC_REGISTER;
  1893. end
  1894. else if is_longstring(ld) then
  1895. begin
  1896. { this is only for add, the comparisaion is handled later }
  1897. expectloc:=LOC_REFERENCE;
  1898. end
  1899. else
  1900. begin
  1901. {$ifdef addstringopt}
  1902. { can create a call which isn't handled by callparatemp }
  1903. if canbeaddsstringcharoptnode(self) then
  1904. begin
  1905. hp := genaddsstringcharoptnode(self);
  1906. pass_1 := hp;
  1907. exit;
  1908. end
  1909. else
  1910. {$endif addstringopt}
  1911. begin
  1912. { Fix right to be shortstring }
  1913. if is_char(right.resulttype.def) then
  1914. begin
  1915. inserttypeconv(right,cshortstringtype);
  1916. firstpass(right);
  1917. end;
  1918. end;
  1919. {$ifdef addstringopt}
  1920. { can create a call which isn't handled by callparatemp }
  1921. if canbeaddsstringcsstringoptnode(self) then
  1922. begin
  1923. hp := genaddsstringcsstringoptnode(self);
  1924. pass_1 := hp;
  1925. exit;
  1926. end;
  1927. {$endif addstringopt}
  1928. end;
  1929. { otherwise, let addstring convert everything }
  1930. result := first_addstring;
  1931. exit;
  1932. end
  1933. { is one a real float ? }
  1934. else if (rd.deftype=floatdef) or (ld.deftype=floatdef) then
  1935. begin
  1936. {$ifdef cpufpemu}
  1937. result := first_addfloat;
  1938. if assigned(result) then
  1939. exit;
  1940. {$endif cpufpemu}
  1941. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1942. expectloc:=LOC_FPUREGISTER
  1943. else
  1944. expectloc:=LOC_FLAGS;
  1945. calcregisters(self,0,1,0);
  1946. { an add node always first loads both the left and the }
  1947. { right in the fpu before doing the calculation. However, }
  1948. { calcregisters(0,2,0) will overestimate the number of }
  1949. { necessary registers (it will make it 3 in case one of }
  1950. { the operands is already in the fpu) (JM) }
  1951. if ((left.expectloc<>LOC_FPUREGISTER) or
  1952. (right.expectloc<>LOC_FPUREGISTER)) and
  1953. (registersfpu < 2) then
  1954. inc(registersfpu);
  1955. end
  1956. { pointer comperation and subtraction }
  1957. else if (ld.deftype=pointerdef) then
  1958. begin
  1959. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1960. expectloc:=LOC_REGISTER
  1961. else
  1962. expectloc:=LOC_FLAGS;
  1963. calcregisters(self,1,0,0);
  1964. end
  1965. else if is_class_or_interface(ld) then
  1966. begin
  1967. expectloc:=LOC_FLAGS;
  1968. calcregisters(self,1,0,0);
  1969. end
  1970. else if (ld.deftype=classrefdef) then
  1971. begin
  1972. expectloc:=LOC_FLAGS;
  1973. calcregisters(self,1,0,0);
  1974. end
  1975. { support procvar=nil,procvar<>nil }
  1976. else if ((ld.deftype=procvardef) and (rt=niln)) or
  1977. ((rd.deftype=procvardef) and (lt=niln)) then
  1978. begin
  1979. expectloc:=LOC_FLAGS;
  1980. calcregisters(self,1,0,0);
  1981. end
  1982. {$ifdef SUPPORT_MMX}
  1983. { mmx support, this must be before the zero based array
  1984. check }
  1985. else if (cs_mmx in aktlocalswitches) and is_mmx_able_array(ld) and
  1986. is_mmx_able_array(rd) then
  1987. begin
  1988. expectloc:=LOC_MMXREGISTER;
  1989. calcregisters(self,0,0,1);
  1990. end
  1991. {$endif SUPPORT_MMX}
  1992. else if (rd.deftype=pointerdef) or (ld.deftype=pointerdef) then
  1993. begin
  1994. expectloc:=LOC_REGISTER;
  1995. calcregisters(self,1,0,0);
  1996. end
  1997. else if (rd.deftype=procvardef) and
  1998. (ld.deftype=procvardef) and
  1999. equal_defs(rd,ld) then
  2000. begin
  2001. expectloc:=LOC_FLAGS;
  2002. calcregisters(self,1,0,0);
  2003. end
  2004. else if (ld.deftype=enumdef) then
  2005. begin
  2006. expectloc:=LOC_FLAGS;
  2007. calcregisters(self,1,0,0);
  2008. end
  2009. {$ifdef SUPPORT_MMX}
  2010. else if (cs_mmx in aktlocalswitches) and
  2011. is_mmx_able_array(ld) and
  2012. is_mmx_able_array(rd) then
  2013. begin
  2014. expectloc:=LOC_MMXREGISTER;
  2015. calcregisters(self,0,0,1);
  2016. end
  2017. {$endif SUPPORT_MMX}
  2018. { the general solution is to convert to 32 bit int }
  2019. else
  2020. begin
  2021. expectloc:=LOC_REGISTER;
  2022. calcregisters(self,1,0,0);
  2023. end;
  2024. end;
  2025. {$ifdef state_tracking}
  2026. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  2027. var factval:Tnode;
  2028. begin
  2029. track_state_pass:=false;
  2030. if left.track_state_pass(exec_known) then
  2031. begin
  2032. track_state_pass:=true;
  2033. left.resulttype.def:=nil;
  2034. do_resulttypepass(left);
  2035. end;
  2036. factval:=aktstate.find_fact(left);
  2037. if factval<>nil then
  2038. begin
  2039. track_state_pass:=true;
  2040. left.destroy;
  2041. left:=factval.getcopy;
  2042. end;
  2043. if right.track_state_pass(exec_known) then
  2044. begin
  2045. track_state_pass:=true;
  2046. right.resulttype.def:=nil;
  2047. do_resulttypepass(right);
  2048. end;
  2049. factval:=aktstate.find_fact(right);
  2050. if factval<>nil then
  2051. begin
  2052. track_state_pass:=true;
  2053. right.destroy;
  2054. right:=factval.getcopy;
  2055. end;
  2056. end;
  2057. {$endif}
  2058. begin
  2059. caddnode:=taddnode;
  2060. end.
  2061. {
  2062. $Log$
  2063. Revision 1.141 2005-02-17 17:52:39 peter
  2064. * allow enum arithmetics inside an enum def, compatible with delphi
  2065. Revision 1.140 2005/02/14 17:13:06 peter
  2066. * truncate log
  2067. Revision 1.139 2005/01/31 21:30:56 olle
  2068. + Added fake Exception classes, only for MACOS.
  2069. Revision 1.138 2005/01/31 16:15:04 peter
  2070. * zero based array with elementsize>1 fix
  2071. Revision 1.137 2005/01/26 16:23:28 peter
  2072. * detect arithmetic overflows for constants at compile time
  2073. * use try..except instead of setjmp
  2074. Revision 1.136 2005/01/16 11:56:37 peter
  2075. * fixed some tabs
  2076. Revision 1.135 2005/01/16 11:13:40 peter
  2077. * ord-ord always gives a signed result
  2078. Revision 1.134 2005/01/10 22:10:26 peter
  2079. * widestring patches from Alexey Barkovoy
  2080. Revision 1.133 2005/01/02 17:31:07 peter
  2081. unsigned*unsigned will also have unsigned result.
  2082. }