nadd.pas 87 KB

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