nadd.pas 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245
  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 : array[0..1] of 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,[vsf_must_be_valid]);
  119. set_varstate(right,vs_used,[vsf_must_be_valid]);
  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[0]:=char(byte(tordconstnode(left).value));
  537. c1[1]:=#0;
  538. l1:=1;
  539. c2[0]:=char(byte(tordconstnode(right).value));
  540. c2[1]:=#0;
  541. l2:=1;
  542. s1:=@c1;
  543. s2:=@c2;
  544. concatstrings:=true;
  545. end
  546. else if (lt=stringconstn) and (rt=ordconstn) and is_char(rd) then
  547. begin
  548. s1:=tstringconstnode(left).value_str;
  549. l1:=tstringconstnode(left).len;
  550. c2[0]:=char(byte(tordconstnode(right).value));
  551. c2[1]:=#0;
  552. s2:=@c2;
  553. l2:=1;
  554. concatstrings:=true;
  555. end
  556. else if (lt=ordconstn) and (rt=stringconstn) and is_char(ld) then
  557. begin
  558. c1[0]:=char(byte(tordconstnode(left).value));
  559. c1[1]:=#0;
  560. l1:=1;
  561. s1:=@c1;
  562. s2:=tstringconstnode(right).value_str;
  563. l2:=tstringconstnode(right).len;
  564. concatstrings:=true;
  565. end
  566. else if (lt=stringconstn) and (rt=stringconstn) then
  567. begin
  568. s1:=tstringconstnode(left).value_str;
  569. l1:=tstringconstnode(left).len;
  570. s2:=tstringconstnode(right).value_str;
  571. l2:=tstringconstnode(right).len;
  572. concatstrings:=true;
  573. end;
  574. if concatstrings then
  575. begin
  576. case nodetype of
  577. addn :
  578. t:=cstringconstnode.createpchar(concatansistrings(s1,s2,l1,l2),l1+l2);
  579. ltn :
  580. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<0),booltype,true);
  581. lten :
  582. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<=0),booltype,true);
  583. gtn :
  584. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)>0),booltype,true);
  585. gten :
  586. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)>=0),booltype,true);
  587. equaln :
  588. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)=0),booltype,true);
  589. unequaln :
  590. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<>0),booltype,true);
  591. else
  592. begin
  593. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  594. t:=cnothingnode.create;
  595. end;
  596. end;
  597. result:=t;
  598. exit;
  599. end;
  600. { set constant evaluation }
  601. if (right.nodetype=setconstn) and
  602. not assigned(tsetconstnode(right).left) and
  603. (left.nodetype=setconstn) and
  604. not assigned(tsetconstnode(left).left) then
  605. begin
  606. { check if size adjusting is needed, only for left
  607. to right as the other way is checked in the typeconv }
  608. if (tsetdef(right.resulttype.def).settype=smallset) and
  609. (tsetdef(left.resulttype.def).settype<>smallset) then
  610. right.resulttype.setdef(tsetdef.create(tsetdef(right.resulttype.def).elementtype,255));
  611. { check base types }
  612. inserttypeconv(left,right.resulttype);
  613. if codegenerror then
  614. begin
  615. { recover by only returning the left part }
  616. result:=left;
  617. left:=nil;
  618. exit;
  619. end;
  620. case nodetype of
  621. addn :
  622. begin
  623. resultset:=tsetconstnode(right).value_set^ + tsetconstnode(left).value_set^;
  624. t:=csetconstnode.create(@resultset,left.resulttype);
  625. end;
  626. muln :
  627. begin
  628. resultset:=tsetconstnode(right).value_set^ * tsetconstnode(left).value_set^;
  629. t:=csetconstnode.create(@resultset,left.resulttype);
  630. end;
  631. subn :
  632. begin
  633. resultset:=tsetconstnode(left).value_set^ - tsetconstnode(right).value_set^;
  634. t:=csetconstnode.create(@resultset,left.resulttype);
  635. end;
  636. symdifn :
  637. begin
  638. resultset:=tsetconstnode(right).value_set^ >< tsetconstnode(left).value_set^;
  639. t:=csetconstnode.create(@resultset,left.resulttype);
  640. end;
  641. unequaln :
  642. begin
  643. b:=tsetconstnode(right).value_set^ <> tsetconstnode(left).value_set^;
  644. t:=cordconstnode.create(byte(b),booltype,true);
  645. end;
  646. equaln :
  647. begin
  648. b:=tsetconstnode(right).value_set^ = tsetconstnode(left).value_set^;
  649. t:=cordconstnode.create(byte(b),booltype,true);
  650. end;
  651. lten :
  652. begin
  653. b:=tsetconstnode(left).value_set^ <= tsetconstnode(right).value_set^;
  654. t:=cordconstnode.create(byte(b),booltype,true);
  655. end;
  656. gten :
  657. begin
  658. b:=tsetconstnode(left).value_set^ >= tsetconstnode(right).value_set^;
  659. t:=cordconstnode.create(byte(b),booltype,true);
  660. end;
  661. else
  662. begin
  663. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  664. t:=cnothingnode.create;
  665. end;
  666. end;
  667. result:=t;
  668. exit;
  669. end;
  670. { but an int/int gives real/real! }
  671. if nodetype=slashn then
  672. begin
  673. if is_currency(left.resulttype.def) and
  674. is_currency(right.resulttype.def) then
  675. { In case of currency, converting to float means dividing by 10000 }
  676. { However, since this is already a division, both divisions by }
  677. { 10000 are eliminated when we divide the results -> we can skip }
  678. { them. }
  679. if s64currencytype.def.deftype = floatdef then
  680. begin
  681. { there's no s64comptype or so, how do we avoid the type conversion?
  682. left.resulttype := s64comptype;
  683. right.resulttype := s64comptype; }
  684. end
  685. else
  686. begin
  687. left.resulttype := s64inttype;
  688. right.resulttype := s64inttype;
  689. end
  690. else if (left.resulttype.def.deftype <> floatdef) and
  691. (right.resulttype.def.deftype <> floatdef) then
  692. CGMessage(type_h_use_div_for_int);
  693. inserttypeconv(right,resultrealtype);
  694. inserttypeconv(left,resultrealtype);
  695. end
  696. { if both are orddefs then check sub types }
  697. else if (ld.deftype=orddef) and (rd.deftype=orddef) then
  698. begin
  699. { optimize multiplacation by a power of 2 }
  700. if not(cs_check_overflow in aktlocalswitches) and
  701. (nodetype = muln) and
  702. (((left.nodetype = ordconstn) and
  703. ispowerof2(tordconstnode(left).value,i)) or
  704. ((right.nodetype = ordconstn) and
  705. ispowerof2(tordconstnode(right).value,i))) then
  706. begin
  707. if left.nodetype = ordconstn then
  708. begin
  709. tordconstnode(left).value := i;
  710. result := cshlshrnode.create(shln,right,left);
  711. end
  712. else
  713. begin
  714. tordconstnode(right).value := i;
  715. result := cshlshrnode.create(shln,left,right);
  716. end;
  717. left := nil;
  718. right := nil;
  719. exit;
  720. end;
  721. { 2 booleans? Make them equal to the largest boolean }
  722. if is_boolean(ld) and is_boolean(rd) then
  723. begin
  724. if torddef(left.resulttype.def).size>torddef(right.resulttype.def).size then
  725. begin
  726. right:=ctypeconvnode.create_internal(right,left.resulttype);
  727. ttypeconvnode(right).convtype:=tc_bool_2_int;
  728. resulttypepass(right);
  729. end
  730. else if torddef(left.resulttype.def).size<torddef(right.resulttype.def).size then
  731. begin
  732. left:=ctypeconvnode.create_internal(left,right.resulttype);
  733. ttypeconvnode(left).convtype:=tc_bool_2_int;
  734. resulttypepass(left);
  735. end;
  736. case nodetype of
  737. xorn,
  738. ltn,
  739. lten,
  740. gtn,
  741. gten,
  742. andn,
  743. orn:
  744. begin
  745. end;
  746. unequaln,
  747. equaln:
  748. begin
  749. if not(cs_full_boolean_eval in aktlocalswitches) then
  750. begin
  751. { Remove any compares with constants }
  752. if (left.nodetype=ordconstn) then
  753. begin
  754. hp:=right;
  755. b:=(tordconstnode(left).value<>0);
  756. ot:=nodetype;
  757. left.free;
  758. left:=nil;
  759. right:=nil;
  760. if (not(b) and (ot=equaln)) or
  761. (b and (ot=unequaln)) then
  762. begin
  763. hp:=cnotnode.create(hp);
  764. end;
  765. result:=hp;
  766. exit;
  767. end;
  768. if (right.nodetype=ordconstn) then
  769. begin
  770. hp:=left;
  771. b:=(tordconstnode(right).value<>0);
  772. ot:=nodetype;
  773. right.free;
  774. right:=nil;
  775. left:=nil;
  776. if (not(b) and (ot=equaln)) or
  777. (b and (ot=unequaln)) then
  778. begin
  779. hp:=cnotnode.create(hp);
  780. end;
  781. result:=hp;
  782. exit;
  783. end;
  784. end;
  785. end;
  786. else
  787. begin
  788. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  789. result:=cnothingnode.create;
  790. exit;
  791. end;
  792. end;
  793. end
  794. { Both are chars? }
  795. else if is_char(rd) and is_char(ld) then
  796. begin
  797. if nodetype=addn then
  798. begin
  799. resulttype:=cshortstringtype;
  800. if not(is_constcharnode(left) and is_constcharnode(right)) then
  801. begin
  802. inserttypeconv(left,cshortstringtype);
  803. {$ifdef addstringopt}
  804. hp := genaddsstringcharoptnode(self);
  805. result := hp;
  806. exit;
  807. {$endif addstringopt}
  808. end;
  809. end;
  810. end
  811. { There is a widechar? }
  812. else if is_widechar(rd) or is_widechar(ld) then
  813. begin
  814. { widechar+widechar gives widestring }
  815. if nodetype=addn then
  816. begin
  817. inserttypeconv(left,cwidestringtype);
  818. if (torddef(rd).typ<>uwidechar) then
  819. inserttypeconv(right,cwidechartype);
  820. resulttype:=cwidestringtype;
  821. end
  822. else
  823. begin
  824. if (torddef(ld).typ<>uwidechar) then
  825. inserttypeconv(left,cwidechartype);
  826. if (torddef(rd).typ<>uwidechar) then
  827. inserttypeconv(right,cwidechartype);
  828. end;
  829. end
  830. { is there a currency type ? }
  831. else if ((torddef(rd).typ=scurrency) or (torddef(ld).typ=scurrency)) then
  832. begin
  833. if (torddef(ld).typ<>scurrency) then
  834. inserttypeconv(left,s64currencytype);
  835. if (torddef(rd).typ<>scurrency) then
  836. inserttypeconv(right,s64currencytype);
  837. end
  838. { and,or,xor work on bit patterns and don't care
  839. about the sign of integers }
  840. else if (nodetype in [andn,orn,xorn]) and
  841. is_integer(ld) and is_integer(rd) then
  842. begin
  843. if rd.size>ld.size then
  844. inserttypeconv_internal(left,right.resulttype)
  845. else
  846. inserttypeconv_internal(right,left.resulttype);
  847. end
  848. { is there a signed 64 bit type ? }
  849. else if ((torddef(rd).typ=s64bit) or (torddef(ld).typ=s64bit)) then
  850. begin
  851. if (torddef(ld).typ<>s64bit) then
  852. inserttypeconv(left,s64inttype);
  853. if (torddef(rd).typ<>s64bit) then
  854. inserttypeconv(right,s64inttype);
  855. end
  856. { is there a unsigned 64 bit type ? }
  857. else if ((torddef(rd).typ=u64bit) or (torddef(ld).typ=u64bit)) then
  858. begin
  859. if (torddef(ld).typ<>u64bit) then
  860. inserttypeconv(left,u64inttype);
  861. if (torddef(rd).typ<>u64bit) then
  862. inserttypeconv(right,u64inttype);
  863. end
  864. { 64 bit cpus do calculations always in 64 bit }
  865. {$ifndef cpu64bit}
  866. { is there a cardinal? }
  867. else if ((torddef(rd).typ=u32bit) or (torddef(ld).typ=u32bit)) then
  868. begin
  869. { convert positive constants to u32bit }
  870. if (torddef(ld).typ<>u32bit) and
  871. is_constintnode(left) and
  872. (tordconstnode(left).value >= 0) then
  873. inserttypeconv(left,u32inttype);
  874. if (torddef(rd).typ<>u32bit) and
  875. is_constintnode(right) and
  876. (tordconstnode(right).value >= 0) then
  877. inserttypeconv(right,u32inttype);
  878. { when one of the operand is signed perform
  879. the operation in 64bit, can't use rd/ld here because there
  880. could be already typeconvs inserted }
  881. if is_signed(left.resulttype.def) or is_signed(right.resulttype.def) then
  882. begin
  883. CGMessage(type_w_mixed_signed_unsigned);
  884. inserttypeconv(left,s64inttype);
  885. inserttypeconv(right,s64inttype);
  886. end
  887. else
  888. begin
  889. { convert positive constants to u32bit }
  890. if (torddef(ld).typ<>u32bit) and
  891. is_constintnode(left) and
  892. (tordconstnode(left).value >= 0) then
  893. inserttypeconv(left,u32inttype);
  894. if (torddef(rd).typ<>u32bit) and
  895. is_constintnode(right) and
  896. (tordconstnode(right).value >= 0) then
  897. inserttypeconv(right,u32inttype);
  898. { when one of the operand is signed perform
  899. the operation in 64bit, can't use rd/ld here because there
  900. could be already typeconvs inserted }
  901. if is_signed(left.resulttype.def) or is_signed(right.resulttype.def) then
  902. begin
  903. CGMessage(type_w_mixed_signed_unsigned);
  904. inserttypeconv(left,s64inttype);
  905. inserttypeconv(right,s64inttype);
  906. end
  907. { For substraction the result can be < 0 but also > maxlongint, we
  908. fallback to int64 that can handle both }
  909. else if (nodetype=subn) then
  910. begin
  911. inserttypeconv(left,s64inttype);
  912. inserttypeconv(right,s64inttype);
  913. end
  914. else
  915. begin
  916. if (torddef(left.resulttype.def).typ<>u32bit) then
  917. inserttypeconv(left,u32inttype);
  918. if (torddef(right.resulttype.def).typ<>u32bit) then
  919. inserttypeconv(right,u32inttype);
  920. end;
  921. end;
  922. end
  923. {$endif cpu64bit}
  924. { generic ord conversion is sinttype }
  925. else
  926. begin
  927. { if the left or right value is smaller than the normal
  928. type s32inttype and is unsigned, and the other value
  929. is a constant < 0, the result will always be false/true
  930. for equal / unequal nodes.
  931. }
  932. if (
  933. { left : unsigned ordinal var, right : < 0 constant }
  934. (
  935. ((is_signed(ld)=false) and (is_constintnode(left) =false)) and
  936. ((is_constintnode(right)) and (tordconstnode(right).value < 0))
  937. ) or
  938. { right : unsigned ordinal var, left : < 0 constant }
  939. (
  940. ((is_signed(rd)=false) and (is_constintnode(right) =false)) and
  941. ((is_constintnode(left)) and (tordconstnode(left).value < 0))
  942. )
  943. ) then
  944. begin
  945. if nodetype = equaln then
  946. CGMessage(type_w_signed_unsigned_always_false)
  947. else
  948. if nodetype = unequaln then
  949. CGMessage(type_w_signed_unsigned_always_true)
  950. else
  951. if (is_constintnode(left) and (nodetype in [ltn,lten])) or
  952. (is_constintnode(right) and (nodetype in [gtn,gten])) then
  953. CGMessage(type_w_signed_unsigned_always_true)
  954. else
  955. if (is_constintnode(right) and (nodetype in [ltn,lten])) or
  956. (is_constintnode(left) and (nodetype in [gtn,gten])) then
  957. CGMessage(type_w_signed_unsigned_always_false);
  958. end;
  959. { When there is a signed type we convert to signed int.
  960. Otherwise (both are unsigned) we keep the result also unsigned.
  961. Exception is substraction, that also gives an signed result }
  962. if (nodetype=subn) or
  963. (is_signed(ld) or is_signed(rd)) then
  964. begin
  965. inserttypeconv(right,sinttype);
  966. inserttypeconv(left,sinttype);
  967. end
  968. else
  969. begin
  970. inserttypeconv(right,uinttype);
  971. inserttypeconv(left,uinttype);
  972. end;
  973. end;
  974. end
  975. { if both are floatdefs, conversion is already done before constant folding }
  976. else if (ld.deftype=floatdef) then
  977. begin
  978. { already converted }
  979. end
  980. { left side a setdef, must be before string processing,
  981. else array constructor can be seen as array of char (PFV) }
  982. else if (ld.deftype=setdef) then
  983. begin
  984. { trying to add a set element? }
  985. if (nodetype=addn) and (rd.deftype<>setdef) then
  986. begin
  987. if (rt=setelementn) then
  988. begin
  989. if not(equal_defs(tsetdef(ld).elementtype.def,rd)) then
  990. CGMessage(type_e_set_element_are_not_comp);
  991. end
  992. else
  993. CGMessage(type_e_mismatch)
  994. end
  995. else
  996. begin
  997. if not(nodetype in [addn,subn,symdifn,muln,equaln,unequaln,lten,gten]) then
  998. CGMessage(type_e_set_operation_unknown);
  999. { right def must be a also be set }
  1000. if (rd.deftype<>setdef) or not(equal_defs(rd,ld)) then
  1001. CGMessage(type_e_set_element_are_not_comp);
  1002. end;
  1003. { ranges require normsets }
  1004. if (tsetdef(ld).settype=smallset) and
  1005. (rt=setelementn) and
  1006. assigned(tsetelementnode(right).right) then
  1007. begin
  1008. { generate a temporary normset def, it'll be destroyed
  1009. when the symtable is unloaded }
  1010. htype.setdef(tsetdef.create(tsetdef(ld).elementtype,255));
  1011. inserttypeconv(left,htype);
  1012. end;
  1013. { if the right side is also a setdef then the settype must
  1014. be the same as the left setdef }
  1015. if (rd.deftype=setdef) and
  1016. (tsetdef(ld).settype<>tsetdef(rd).settype) then
  1017. begin
  1018. { when right is a normset we need to typecast both
  1019. to normsets }
  1020. if (tsetdef(rd).settype=normset) then
  1021. inserttypeconv(left,right.resulttype)
  1022. else
  1023. inserttypeconv(right,left.resulttype);
  1024. end;
  1025. end
  1026. { compare pchar to char arrays by addresses like BP/Delphi }
  1027. else if ((is_pchar(ld) or (lt=niln)) and is_chararray(rd)) or
  1028. ((is_pchar(rd) or (rt=niln)) and is_chararray(ld)) then
  1029. begin
  1030. if is_chararray(rd) then
  1031. inserttypeconv(right,charpointertype)
  1032. else
  1033. inserttypeconv(left,charpointertype);
  1034. end
  1035. { pointer comparision and subtraction }
  1036. else if (rd.deftype=pointerdef) and (ld.deftype=pointerdef) then
  1037. begin
  1038. case nodetype of
  1039. equaln,unequaln :
  1040. begin
  1041. if is_voidpointer(right.resulttype.def) then
  1042. inserttypeconv(right,left.resulttype)
  1043. else if is_voidpointer(left.resulttype.def) then
  1044. inserttypeconv(left,right.resulttype)
  1045. else if not(equal_defs(ld,rd)) then
  1046. IncompatibleTypes(ld,rd);
  1047. { now that the type checking is done, convert both to charpointer, }
  1048. { because methodpointers are 8 bytes even though only the first 4 }
  1049. { bytes must be compared. This can happen here if we are in }
  1050. { TP/Delphi mode, because there @methodpointer = voidpointer (but }
  1051. { a voidpointer of 8 bytes). A conversion to voidpointer would be }
  1052. { optimized away, since the result already was a voidpointer, so }
  1053. { use a charpointer instead (JM) }
  1054. inserttypeconv_internal(left,charpointertype);
  1055. inserttypeconv_internal(right,charpointertype);
  1056. end;
  1057. ltn,lten,gtn,gten:
  1058. begin
  1059. if (cs_extsyntax in aktmoduleswitches) then
  1060. begin
  1061. if is_voidpointer(right.resulttype.def) then
  1062. inserttypeconv(right,left.resulttype)
  1063. else if is_voidpointer(left.resulttype.def) then
  1064. inserttypeconv(left,right.resulttype)
  1065. else if not(equal_defs(ld,rd)) then
  1066. IncompatibleTypes(ld,rd);
  1067. end
  1068. else
  1069. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1070. end;
  1071. subn:
  1072. begin
  1073. if (cs_extsyntax in aktmoduleswitches) then
  1074. begin
  1075. if is_voidpointer(right.resulttype.def) then
  1076. inserttypeconv(right,left.resulttype)
  1077. else if is_voidpointer(left.resulttype.def) then
  1078. inserttypeconv(left,right.resulttype)
  1079. else if not(equal_defs(ld,rd)) then
  1080. IncompatibleTypes(ld,rd);
  1081. end
  1082. else
  1083. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1084. if not(nf_has_pointerdiv in flags) and
  1085. (tpointerdef(rd).pointertype.def.size>1) then
  1086. begin
  1087. hp:=getcopy;
  1088. include(hp.flags,nf_has_pointerdiv);
  1089. result:=cmoddivnode.create(divn,hp,cordconstnode.create(tpointerdef(rd).pointertype.def.size,sinttype,false));
  1090. end;
  1091. resulttype:=sinttype;
  1092. exit;
  1093. end;
  1094. addn:
  1095. begin
  1096. if (cs_extsyntax in aktmoduleswitches) then
  1097. begin
  1098. if is_voidpointer(right.resulttype.def) then
  1099. inserttypeconv(right,left.resulttype)
  1100. else if is_voidpointer(left.resulttype.def) then
  1101. inserttypeconv(left,right.resulttype)
  1102. else if not(equal_defs(ld,rd)) then
  1103. IncompatibleTypes(ld,rd);
  1104. end
  1105. else
  1106. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1107. resulttype:=sinttype;
  1108. exit;
  1109. end;
  1110. else
  1111. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1112. end;
  1113. end
  1114. { is one of the operands a string?,
  1115. chararrays are also handled as strings (after conversion), also take
  1116. care of chararray+chararray and chararray+char.
  1117. Note: Must be done after pointerdef+pointerdef has been checked, else
  1118. pchar is converted to string }
  1119. else if (rd.deftype=stringdef) or
  1120. (ld.deftype=stringdef) or
  1121. ((is_pchar(rd) or is_chararray(rd) or is_char(rd) or is_open_chararray(rd) or
  1122. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd)) and
  1123. (is_pchar(ld) or is_chararray(ld) or is_char(ld) or is_open_chararray(ld) or
  1124. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld))) then
  1125. begin
  1126. if (nodetype in [addn,equaln,unequaln,lten,gten,ltn,gtn]) then
  1127. begin
  1128. { Is there a widestring? }
  1129. if is_widestring(rd) or is_widestring(ld) or
  1130. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd) or
  1131. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld) then
  1132. strtype:= st_widestring
  1133. else
  1134. if is_ansistring(rd) or is_ansistring(ld) or
  1135. ((cs_ansistrings in aktlocalswitches) and
  1136. //todo: Move some of this to longstring's then they are implemented?
  1137. (
  1138. is_pchar(rd) or (is_chararray(rd) and (rd.size > 255)) or is_open_chararray(rd) or
  1139. is_pchar(ld) or (is_chararray(ld) and (ld.size > 255)) or is_open_chararray(ld)
  1140. )
  1141. ) then
  1142. strtype:= st_ansistring
  1143. else
  1144. if is_longstring(rd) or is_longstring(ld) then
  1145. strtype:= st_longstring
  1146. else
  1147. begin
  1148. {$warning todo: add a warning/hint here if one converting a too large array}
  1149. { nodes is PChar, array [with size > 255] or OpenArrayOfChar.
  1150. Note: Delphi halts with error if "array [0..xx] of char"
  1151. is assigned to ShortString and string length is less
  1152. then array size }
  1153. strtype:= st_shortstring;
  1154. end;
  1155. // Now convert nodes to common string type
  1156. case strtype of
  1157. st_widestring :
  1158. begin
  1159. if not(is_widestring(rd)) then
  1160. inserttypeconv(right,cwidestringtype);
  1161. if not(is_widestring(ld)) then
  1162. inserttypeconv(left,cwidestringtype);
  1163. end;
  1164. st_ansistring :
  1165. begin
  1166. if not(is_ansistring(rd)) then
  1167. inserttypeconv(right,cansistringtype);
  1168. if not(is_ansistring(ld)) then
  1169. inserttypeconv(left,cansistringtype);
  1170. end;
  1171. st_longstring :
  1172. begin
  1173. if not(is_longstring(rd)) then
  1174. inserttypeconv(right,clongstringtype);
  1175. if not(is_longstring(ld)) then
  1176. inserttypeconv(left,clongstringtype);
  1177. end;
  1178. st_shortstring :
  1179. begin
  1180. if not(is_shortstring(ld)) then
  1181. inserttypeconv(left,cshortstringtype);
  1182. { don't convert char, that can be handled by the optimized node }
  1183. if not(is_shortstring(rd) or is_char(rd)) then
  1184. inserttypeconv(right,cshortstringtype);
  1185. end;
  1186. else
  1187. internalerror(2005101);
  1188. end;
  1189. end
  1190. else
  1191. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1192. end
  1193. { class or interface equation }
  1194. else if is_class_or_interface(rd) or is_class_or_interface(ld) then
  1195. begin
  1196. if (nodetype in [equaln,unequaln]) then
  1197. begin
  1198. if is_class_or_interface(rd) and is_class_or_interface(ld) then
  1199. begin
  1200. if tobjectdef(rd).is_related(tobjectdef(ld)) then
  1201. inserttypeconv(right,left.resulttype)
  1202. else
  1203. inserttypeconv(left,right.resulttype);
  1204. end
  1205. else if is_class_or_interface(rd) then
  1206. inserttypeconv(left,right.resulttype)
  1207. else
  1208. inserttypeconv(right,left.resulttype);
  1209. end
  1210. else
  1211. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1212. end
  1213. else if (rd.deftype=classrefdef) and (ld.deftype=classrefdef) then
  1214. begin
  1215. if (nodetype in [equaln,unequaln]) then
  1216. begin
  1217. if tobjectdef(tclassrefdef(rd).pointertype.def).is_related(
  1218. tobjectdef(tclassrefdef(ld).pointertype.def)) then
  1219. inserttypeconv(right,left.resulttype)
  1220. else
  1221. inserttypeconv(left,right.resulttype);
  1222. end
  1223. else
  1224. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1225. end
  1226. { allows comperasion with nil pointer }
  1227. else if is_class_or_interface(rd) or (rd.deftype=classrefdef) then
  1228. begin
  1229. if (nodetype in [equaln,unequaln]) then
  1230. inserttypeconv(left,right.resulttype)
  1231. else
  1232. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1233. end
  1234. else if is_class_or_interface(ld) or (ld.deftype=classrefdef) then
  1235. begin
  1236. if (nodetype in [equaln,unequaln]) then
  1237. inserttypeconv(right,left.resulttype)
  1238. else
  1239. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1240. end
  1241. { support procvar=nil,procvar<>nil }
  1242. else if ((ld.deftype=procvardef) and (rt=niln)) or
  1243. ((rd.deftype=procvardef) and (lt=niln)) then
  1244. begin
  1245. if not(nodetype in [equaln,unequaln]) then
  1246. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1247. { find proc field in methodpointer record }
  1248. hsym:=tfieldvarsym(trecorddef(methodpointertype.def).symtable.search('proc'));
  1249. if not assigned(hsym) then
  1250. internalerror(200412043);
  1251. { For methodpointers compare only tmethodpointer.proc }
  1252. if (rd.deftype=procvardef) and
  1253. (not tprocvardef(rd).is_addressonly) then
  1254. begin
  1255. right:=csubscriptnode.create(
  1256. hsym,
  1257. ctypeconvnode.create_internal(right,methodpointertype));
  1258. end;
  1259. if (ld.deftype=procvardef) and
  1260. (not tprocvardef(ld).is_addressonly) then
  1261. begin
  1262. left:=csubscriptnode.create(
  1263. hsym,
  1264. ctypeconvnode.create_internal(left,methodpointertype));
  1265. end;
  1266. end
  1267. { support dynamicarray=nil,dynamicarray<>nil }
  1268. else if (is_dynamic_array(ld) and (rt=niln)) or
  1269. (is_dynamic_array(rd) and (lt=niln)) or
  1270. (is_dynamic_array(ld) and is_dynamic_array(rd)) then
  1271. begin
  1272. if not(nodetype in [equaln,unequaln]) then
  1273. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1274. end
  1275. {$ifdef SUPPORT_MMX}
  1276. { mmx support, this must be before the zero based array
  1277. check }
  1278. else if (cs_mmx in aktlocalswitches) and
  1279. is_mmx_able_array(ld) and
  1280. is_mmx_able_array(rd) and
  1281. equal_defs(ld,rd) then
  1282. begin
  1283. case nodetype of
  1284. addn,subn,xorn,orn,andn:
  1285. ;
  1286. { mul is a little bit restricted }
  1287. muln:
  1288. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  1289. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1290. else
  1291. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1292. end;
  1293. end
  1294. {$endif SUPPORT_MMX}
  1295. { this is a little bit dangerous, also the left type }
  1296. { pointer to should be checked! This broke the mmx support }
  1297. else if (rd.deftype=pointerdef) or is_zero_based_array(rd) then
  1298. begin
  1299. if is_zero_based_array(rd) then
  1300. begin
  1301. resulttype.setdef(tpointerdef.create(tarraydef(rd).elementtype));
  1302. inserttypeconv(right,resulttype);
  1303. end
  1304. else
  1305. resulttype:=right.resulttype;
  1306. inserttypeconv(left,sinttype);
  1307. if nodetype=addn then
  1308. begin
  1309. if not(cs_extsyntax in aktmoduleswitches) or
  1310. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  1311. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1312. if (rd.deftype=pointerdef) and
  1313. (tpointerdef(rd).pointertype.def.size>1) then
  1314. begin
  1315. left:=caddnode.create(muln,left,
  1316. cordconstnode.create(tpointerdef(rd).pointertype.def.size,sinttype,true));
  1317. end;
  1318. end
  1319. else
  1320. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1321. end
  1322. else if (ld.deftype=pointerdef) or is_zero_based_array(ld) then
  1323. begin
  1324. if is_zero_based_array(ld) then
  1325. begin
  1326. resulttype.setdef(tpointerdef.create(tarraydef(ld).elementtype));
  1327. inserttypeconv(left,resulttype);
  1328. end
  1329. else
  1330. resulttype:=left.resulttype;
  1331. inserttypeconv(right,sinttype);
  1332. if nodetype in [addn,subn] then
  1333. begin
  1334. if not(cs_extsyntax in aktmoduleswitches) or
  1335. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  1336. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1337. if (ld.deftype=pointerdef) and
  1338. (tpointerdef(ld).pointertype.def.size>1) then
  1339. begin
  1340. right:=caddnode.create(muln,right,
  1341. cordconstnode.create(tpointerdef(ld).pointertype.def.size,sinttype,true));
  1342. end
  1343. else
  1344. if is_zero_based_array(ld) and
  1345. (tarraydef(ld).elementtype.def.size>1) then
  1346. begin
  1347. right:=caddnode.create(muln,right,
  1348. cordconstnode.create(tarraydef(ld).elementtype.def.size,sinttype,true));
  1349. end;
  1350. end
  1351. else
  1352. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1353. end
  1354. else if (rd.deftype=procvardef) and
  1355. (ld.deftype=procvardef) and
  1356. equal_defs(rd,ld) then
  1357. begin
  1358. if (nodetype in [equaln,unequaln]) then
  1359. begin
  1360. if tprocvardef(rd).is_addressonly then
  1361. begin
  1362. inserttypeconv_internal(right,voidpointertype);
  1363. inserttypeconv_internal(left,voidpointertype);
  1364. end
  1365. else
  1366. begin
  1367. { find proc field in methodpointer record }
  1368. hsym:=tfieldvarsym(trecorddef(methodpointertype.def).symtable.search('proc'));
  1369. if not assigned(hsym) then
  1370. internalerror(200412043);
  1371. { Compare tmehodpointer(left).proc }
  1372. right:=csubscriptnode.create(
  1373. hsym,
  1374. ctypeconvnode.create_internal(right,methodpointertype));
  1375. left:=csubscriptnode.create(
  1376. hsym,
  1377. ctypeconvnode.create_internal(left,methodpointertype));
  1378. end;
  1379. end
  1380. else
  1381. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1382. end
  1383. { enums }
  1384. else if (ld.deftype=enumdef) and (rd.deftype=enumdef) then
  1385. begin
  1386. if allowenumop(nodetype) then
  1387. inserttypeconv(right,left.resulttype)
  1388. else
  1389. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1390. end
  1391. { generic conversion, this is for error recovery }
  1392. else
  1393. begin
  1394. inserttypeconv(left,sinttype);
  1395. inserttypeconv(right,sinttype);
  1396. end;
  1397. { set resulttype if not already done }
  1398. if not assigned(resulttype.def) then
  1399. begin
  1400. case nodetype of
  1401. ltn,lten,gtn,gten,equaln,unequaln :
  1402. resulttype:=booltype;
  1403. slashn :
  1404. resulttype:=resultrealtype;
  1405. addn:
  1406. begin
  1407. { for strings, return is always a 255 char string }
  1408. if is_shortstring(left.resulttype.def) then
  1409. resulttype:=cshortstringtype
  1410. else
  1411. resulttype:=left.resulttype;
  1412. end;
  1413. else
  1414. resulttype:=left.resulttype;
  1415. end;
  1416. end;
  1417. { when the result is currency we need some extra code for
  1418. multiplication and division. this should not be done when
  1419. the muln or slashn node is created internally }
  1420. if not(nf_is_currency in flags) and
  1421. is_currency(resulttype.def) then
  1422. begin
  1423. case nodetype of
  1424. slashn :
  1425. begin
  1426. { slashn will only work with floats }
  1427. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  1428. include(hp.flags,nf_is_currency);
  1429. result:=hp;
  1430. end;
  1431. muln :
  1432. begin
  1433. if s64currencytype.def.deftype=floatdef then
  1434. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype))
  1435. else
  1436. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  1437. include(hp.flags,nf_is_currency);
  1438. result:=hp
  1439. end;
  1440. end;
  1441. end;
  1442. end;
  1443. function taddnode.first_addstring: tnode;
  1444. var
  1445. p: tnode;
  1446. begin
  1447. { when we get here, we are sure that both the left and the right }
  1448. { node are both strings of the same stringtype (JM) }
  1449. case nodetype of
  1450. addn:
  1451. begin
  1452. { create the call to the concat routine both strings as arguments }
  1453. result := ccallnode.createintern('fpc_'+
  1454. tstringdef(resulttype.def).stringtypname+'_concat',
  1455. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1456. { we reused the arguments }
  1457. left := nil;
  1458. right := nil;
  1459. end;
  1460. ltn,lten,gtn,gten,equaln,unequaln :
  1461. begin
  1462. { generate better code for comparison with empty string, we
  1463. only need to compare the length with 0 }
  1464. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  1465. (((left.nodetype=stringconstn) and (str_length(left)=0)) or
  1466. ((right.nodetype=stringconstn) and (str_length(right)=0))) then
  1467. begin
  1468. { switch so that the constant is always on the right }
  1469. if left.nodetype = stringconstn then
  1470. begin
  1471. p := left;
  1472. left := right;
  1473. right := p;
  1474. end;
  1475. if is_shortstring(left.resulttype.def) or
  1476. (nodetype in [gtn,gten,ltn,lten]) then
  1477. { compare the length with 0 }
  1478. result := caddnode.create(nodetype,
  1479. cinlinenode.create(in_length_x,false,left),
  1480. cordconstnode.create(0,s32inttype,false))
  1481. else
  1482. begin
  1483. { compare the pointer with nil (for ansistrings etc), }
  1484. { faster than getting the length (JM) }
  1485. result:= caddnode.create(nodetype,
  1486. ctypeconvnode.create_internal(left,voidpointertype),
  1487. cpointerconstnode.create(0,voidpointertype));
  1488. end;
  1489. { left is reused }
  1490. left := nil;
  1491. { right isn't }
  1492. right.free;
  1493. right := nil;
  1494. exit;
  1495. end;
  1496. { no string constant -> call compare routine }
  1497. result := ccallnode.createintern('fpc_'+
  1498. tstringdef(left.resulttype.def).stringtypname+'_compare',
  1499. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1500. { and compare its result with 0 according to the original operator }
  1501. result := caddnode.create(nodetype,result,
  1502. cordconstnode.create(0,s32inttype,false));
  1503. left := nil;
  1504. right := nil;
  1505. end;
  1506. end;
  1507. end;
  1508. function taddnode.first_addset: tnode;
  1509. var
  1510. procname: string[31];
  1511. tempn: tnode;
  1512. paras: tcallparanode;
  1513. srsym: ttypesym;
  1514. begin
  1515. { get the sym that represents the fpc_normal_set type }
  1516. if not searchsystype('FPC_NORMAL_SET',srsym) then
  1517. internalerror(200108313);
  1518. case nodetype of
  1519. equaln,unequaln,lten,gten:
  1520. begin
  1521. case nodetype of
  1522. equaln,unequaln:
  1523. procname := 'fpc_set_comp_sets';
  1524. lten,gten:
  1525. begin
  1526. procname := 'fpc_set_contains_sets';
  1527. { (left >= right) = (right <= left) }
  1528. if nodetype = gten then
  1529. begin
  1530. tempn := left;
  1531. left := right;
  1532. right := tempn;
  1533. end;
  1534. end;
  1535. end;
  1536. { convert the arguments (explicitely) to fpc_normal_set's }
  1537. left := ctypeconvnode.create_internal(left,srsym.restype);
  1538. right := ctypeconvnode.create_internal(right,srsym.restype);
  1539. result := ccallnode.createintern(procname,ccallparanode.create(right,
  1540. ccallparanode.create(left,nil)));
  1541. { left and right are reused as parameters }
  1542. left := nil;
  1543. right := nil;
  1544. { for an unequaln, we have to negate the result of comp_sets }
  1545. if nodetype = unequaln then
  1546. result := cnotnode.create(result);
  1547. end;
  1548. addn:
  1549. begin
  1550. { optimize first loading of a set }
  1551. if (right.nodetype=setelementn) and
  1552. not(assigned(tsetelementnode(right).right)) and
  1553. is_emptyset(left) then
  1554. begin
  1555. { type cast the value to pass as argument to a byte, }
  1556. { since that's what the helper expects }
  1557. tsetelementnode(right).left :=
  1558. ctypeconvnode.create_internal(tsetelementnode(right).left,u8inttype);
  1559. { set the resulttype to the actual one (otherwise it's }
  1560. { "fpc_normal_set") }
  1561. result := ccallnode.createinternres('fpc_set_create_element',
  1562. ccallparanode.create(tsetelementnode(right).left,nil),
  1563. resulttype);
  1564. { reused }
  1565. tsetelementnode(right).left := nil;
  1566. end
  1567. else
  1568. begin
  1569. if right.nodetype=setelementn then
  1570. begin
  1571. { convert the arguments to bytes, since that's what }
  1572. { the helper expects }
  1573. tsetelementnode(right).left :=
  1574. ctypeconvnode.create_internal(tsetelementnode(right).left,
  1575. u8inttype);
  1576. { convert the original set (explicitely) to an }
  1577. { fpc_normal_set so we can pass it to the helper }
  1578. left := ctypeconvnode.create_internal(left,srsym.restype);
  1579. { add a range or a single element? }
  1580. if assigned(tsetelementnode(right).right) then
  1581. begin
  1582. tsetelementnode(right).right :=
  1583. ctypeconvnode.create_internal(tsetelementnode(right).right,
  1584. u8inttype);
  1585. { create the call }
  1586. result := ccallnode.createinternres('fpc_set_set_range',
  1587. ccallparanode.create(tsetelementnode(right).right,
  1588. ccallparanode.create(tsetelementnode(right).left,
  1589. ccallparanode.create(left,nil))),resulttype);
  1590. end
  1591. else
  1592. begin
  1593. result := ccallnode.createinternres('fpc_set_set_byte',
  1594. ccallparanode.create(tsetelementnode(right).left,
  1595. ccallparanode.create(left,nil)),resulttype);
  1596. end;
  1597. { remove reused parts from original node }
  1598. tsetelementnode(right).right := nil;
  1599. tsetelementnode(right).left := nil;
  1600. left := nil;
  1601. end
  1602. else
  1603. begin
  1604. { add two sets }
  1605. { convert the sets to fpc_normal_set's }
  1606. result := ccallnode.createinternres('fpc_set_add_sets',
  1607. ccallparanode.create(
  1608. ctypeconvnode.create_explicit(right,srsym.restype),
  1609. ccallparanode.create(
  1610. ctypeconvnode.create_internal(left,srsym.restype),nil)),resulttype);
  1611. { remove reused parts from original node }
  1612. left := nil;
  1613. right := nil;
  1614. end;
  1615. end
  1616. end;
  1617. subn,symdifn,muln:
  1618. begin
  1619. { convert the sets to fpc_normal_set's }
  1620. paras := ccallparanode.create(ctypeconvnode.create_internal(right,srsym.restype),
  1621. ccallparanode.create(ctypeconvnode.create_internal(left,srsym.restype),nil));
  1622. case nodetype of
  1623. subn:
  1624. result := ccallnode.createinternres('fpc_set_sub_sets',
  1625. paras,resulttype);
  1626. symdifn:
  1627. result := ccallnode.createinternres('fpc_set_symdif_sets',
  1628. paras,resulttype);
  1629. muln:
  1630. result := ccallnode.createinternres('fpc_set_mul_sets',
  1631. paras,resulttype);
  1632. end;
  1633. { remove reused parts from original node }
  1634. left := nil;
  1635. right := nil;
  1636. end;
  1637. else
  1638. internalerror(200108311);
  1639. end;
  1640. end;
  1641. function taddnode.first_add64bitint: tnode;
  1642. var
  1643. procname: string[31];
  1644. temp: tnode;
  1645. power: longint;
  1646. begin
  1647. result := nil;
  1648. { create helper calls mul }
  1649. if nodetype <> muln then
  1650. exit;
  1651. { make sure that if there is a constant, that it's on the right }
  1652. if left.nodetype = ordconstn then
  1653. begin
  1654. temp := right;
  1655. right := left;
  1656. left := temp;
  1657. end;
  1658. { can we use a shift instead of a mul? }
  1659. if not (cs_check_overflow in aktlocalswitches) and
  1660. (right.nodetype = ordconstn) and
  1661. ispowerof2(tordconstnode(right).value,power) then
  1662. begin
  1663. tordconstnode(right).value := power;
  1664. result := cshlshrnode.create(shln,left,right);
  1665. { left and right are reused }
  1666. left := nil;
  1667. right := nil;
  1668. { return firstpassed new node }
  1669. exit;
  1670. end;
  1671. { when currency is used set the result of the
  1672. parameters to s64bit, so they are not converted }
  1673. if is_currency(resulttype.def) then
  1674. begin
  1675. left.resulttype:=s64inttype;
  1676. right.resulttype:=s64inttype;
  1677. end;
  1678. { otherwise, create the parameters for the helper }
  1679. right := ccallparanode.create(
  1680. cordconstnode.create(ord(cs_check_overflow in aktlocalswitches),booltype,true),
  1681. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1682. left := nil;
  1683. { only qword needs the unsigned code, the
  1684. signed code is also used for currency }
  1685. if is_signed(resulttype.def) then
  1686. procname := 'fpc_mul_int64'
  1687. else
  1688. procname := 'fpc_mul_qword';
  1689. result := ccallnode.createintern(procname,right);
  1690. right := nil;
  1691. end;
  1692. {$ifdef cpufpemu}
  1693. function taddnode.first_addfloat: tnode;
  1694. var
  1695. procname: string[31];
  1696. temp: tnode;
  1697. power: longint;
  1698. { do we need to reverse the result ? }
  1699. notnode : boolean;
  1700. begin
  1701. result := nil;
  1702. notnode := false;
  1703. { In non-emulation mode, real opcodes are
  1704. emitted for floating point values.
  1705. }
  1706. if not (cs_fp_emulation in aktmoduleswitches) then
  1707. exit;
  1708. case nodetype of
  1709. addn : procname := 'fpc_single_add';
  1710. muln : procname := 'fpc_single_mul';
  1711. subn : procname := 'fpc_single_sub';
  1712. slashn : procname := 'fpc_single_div';
  1713. ltn : procname := 'fpc_single_lt';
  1714. lten: procname := 'fpc_single_le';
  1715. gtn:
  1716. begin
  1717. procname := 'fpc_single_le';
  1718. notnode := true;
  1719. end;
  1720. gten:
  1721. begin
  1722. procname := 'fpc_single_lt';
  1723. notnode := true;
  1724. end;
  1725. equaln: procname := 'fpc_single_eq';
  1726. unequaln :
  1727. begin
  1728. procname := 'fpc_single_eq';
  1729. notnode := true;
  1730. end;
  1731. else
  1732. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resulttype.def.typename,right.resulttype.def.typename);
  1733. end;
  1734. { convert the arguments (explicitely) to fpc_normal_set's }
  1735. result := ccallnode.createintern(procname,ccallparanode.create(right,
  1736. ccallparanode.create(left,nil)));
  1737. left:=nil;
  1738. right:=nil;
  1739. { do we need to reverse the result }
  1740. if notnode then
  1741. result := cnotnode.create(result);
  1742. end;
  1743. {$endif cpufpemu}
  1744. function taddnode.pass_1 : tnode;
  1745. var
  1746. {$ifdef addstringopt}
  1747. hp : tnode;
  1748. {$endif addstringopt}
  1749. lt,rt : tnodetype;
  1750. rd,ld : tdef;
  1751. begin
  1752. result:=nil;
  1753. { first do the two subtrees }
  1754. firstpass(left);
  1755. firstpass(right);
  1756. if codegenerror then
  1757. exit;
  1758. { load easier access variables }
  1759. rd:=right.resulttype.def;
  1760. ld:=left.resulttype.def;
  1761. rt:=right.nodetype;
  1762. lt:=left.nodetype;
  1763. { int/int gives real/real! }
  1764. if nodetype=slashn then
  1765. begin
  1766. {$ifdef cpufpemu}
  1767. result := first_addfloat;
  1768. if assigned(result) then
  1769. exit;
  1770. {$endif cpufpemu}
  1771. expectloc:=LOC_FPUREGISTER;
  1772. { maybe we need an integer register to save }
  1773. { a reference }
  1774. if ((left.expectloc<>LOC_FPUREGISTER) or
  1775. (right.expectloc<>LOC_FPUREGISTER)) and
  1776. (left.registersint=right.registersint) then
  1777. calcregisters(self,1,1,0)
  1778. else
  1779. calcregisters(self,0,1,0);
  1780. { an add node always first loads both the left and the }
  1781. { right in the fpu before doing the calculation. However, }
  1782. { calcregisters(0,2,0) will overestimate the number of }
  1783. { necessary registers (it will make it 3 in case one of }
  1784. { the operands is already in the fpu) (JM) }
  1785. if ((left.expectloc<>LOC_FPUREGISTER) or
  1786. (right.expectloc<>LOC_FPUREGISTER)) and
  1787. (registersfpu < 2) then
  1788. inc(registersfpu);
  1789. end
  1790. { if both are orddefs then check sub types }
  1791. else if (ld.deftype=orddef) and (rd.deftype=orddef) then
  1792. begin
  1793. { 2 booleans ? }
  1794. if is_boolean(ld) and is_boolean(rd) then
  1795. begin
  1796. if not(cs_full_boolean_eval in aktlocalswitches) and
  1797. (nodetype in [andn,orn]) then
  1798. begin
  1799. expectloc:=LOC_JUMP;
  1800. calcregisters(self,0,0,0);
  1801. end
  1802. else
  1803. begin
  1804. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  1805. begin
  1806. expectloc:=LOC_FLAGS;
  1807. if (left.expectloc in [LOC_JUMP,LOC_FLAGS]) and
  1808. (left.expectloc in [LOC_JUMP,LOC_FLAGS]) then
  1809. calcregisters(self,2,0,0)
  1810. else
  1811. calcregisters(self,1,0,0);
  1812. end
  1813. else
  1814. begin
  1815. expectloc:=LOC_REGISTER;
  1816. calcregisters(self,0,0,0);
  1817. end;
  1818. end;
  1819. end
  1820. else
  1821. { Both are chars? only convert to shortstrings for addn }
  1822. if is_char(ld) then
  1823. begin
  1824. if nodetype=addn then
  1825. internalerror(200103291);
  1826. expectloc:=LOC_FLAGS;
  1827. calcregisters(self,1,0,0);
  1828. end
  1829. {$ifndef cpu64bit}
  1830. { is there a 64 bit type ? }
  1831. else if (torddef(ld).typ in [s64bit,u64bit,scurrency]) then
  1832. begin
  1833. result := first_add64bitint;
  1834. if assigned(result) then
  1835. exit;
  1836. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1837. expectloc:=LOC_REGISTER
  1838. else
  1839. expectloc:=LOC_JUMP;
  1840. calcregisters(self,2,0,0)
  1841. end
  1842. {$endif cpu64bit}
  1843. { is there a cardinal? }
  1844. else if (torddef(ld).typ=u32bit) then
  1845. begin
  1846. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1847. expectloc:=LOC_REGISTER
  1848. else
  1849. expectloc:=LOC_FLAGS;
  1850. calcregisters(self,1,0,0);
  1851. { for unsigned mul we need an extra register }
  1852. if nodetype=muln then
  1853. inc(registersint);
  1854. end
  1855. { generic s32bit conversion }
  1856. else
  1857. begin
  1858. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1859. expectloc:=LOC_REGISTER
  1860. else
  1861. expectloc:=LOC_FLAGS;
  1862. calcregisters(self,1,0,0);
  1863. end;
  1864. end
  1865. { left side a setdef, must be before string processing,
  1866. else array constructor can be seen as array of char (PFV) }
  1867. else if (ld.deftype=setdef) then
  1868. begin
  1869. if tsetdef(ld).settype=smallset then
  1870. begin
  1871. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  1872. expectloc:=LOC_FLAGS
  1873. else
  1874. expectloc:=LOC_REGISTER;
  1875. { are we adding set elements ? }
  1876. if right.nodetype=setelementn then
  1877. calcregisters(self,2,0,0)
  1878. else
  1879. calcregisters(self,1,0,0);
  1880. end
  1881. else
  1882. {$ifdef MMXSET}
  1883. {$ifdef i386}
  1884. if cs_mmx in aktlocalswitches then
  1885. begin
  1886. expectloc:=LOC_MMXREGISTER;
  1887. calcregisters(self,0,0,4);
  1888. end
  1889. else
  1890. {$endif}
  1891. {$endif MMXSET}
  1892. begin
  1893. result := first_addset;
  1894. if assigned(result) then
  1895. exit;
  1896. expectloc:=LOC_CREFERENCE;
  1897. calcregisters(self,0,0,0);
  1898. { here we call SET... }
  1899. include(current_procinfo.flags,pi_do_call);
  1900. end;
  1901. end
  1902. { compare pchar by addresses like BP/Delphi }
  1903. else if is_pchar(ld) then
  1904. begin
  1905. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1906. expectloc:=LOC_REGISTER
  1907. else
  1908. expectloc:=LOC_FLAGS;
  1909. calcregisters(self,1,0,0);
  1910. end
  1911. { is one of the operands a string }
  1912. else if (ld.deftype=stringdef) then
  1913. begin
  1914. if is_widestring(ld) then
  1915. begin
  1916. { this is only for add, the comparisaion is handled later }
  1917. expectloc:=LOC_REGISTER;
  1918. end
  1919. else if is_ansistring(ld) then
  1920. begin
  1921. { this is only for add, the comparisaion is handled later }
  1922. expectloc:=LOC_REGISTER;
  1923. end
  1924. else if is_longstring(ld) then
  1925. begin
  1926. { this is only for add, the comparisaion is handled later }
  1927. expectloc:=LOC_REFERENCE;
  1928. end
  1929. else
  1930. begin
  1931. {$ifdef addstringopt}
  1932. { can create a call which isn't handled by callparatemp }
  1933. if canbeaddsstringcharoptnode(self) then
  1934. begin
  1935. hp := genaddsstringcharoptnode(self);
  1936. pass_1 := hp;
  1937. exit;
  1938. end
  1939. else
  1940. {$endif addstringopt}
  1941. begin
  1942. { Fix right to be shortstring }
  1943. if is_char(right.resulttype.def) then
  1944. begin
  1945. inserttypeconv(right,cshortstringtype);
  1946. firstpass(right);
  1947. end;
  1948. end;
  1949. {$ifdef addstringopt}
  1950. { can create a call which isn't handled by callparatemp }
  1951. if canbeaddsstringcsstringoptnode(self) then
  1952. begin
  1953. hp := genaddsstringcsstringoptnode(self);
  1954. pass_1 := hp;
  1955. exit;
  1956. end;
  1957. {$endif addstringopt}
  1958. end;
  1959. { otherwise, let addstring convert everything }
  1960. result := first_addstring;
  1961. exit;
  1962. end
  1963. { is one a real float ? }
  1964. else if (rd.deftype=floatdef) or (ld.deftype=floatdef) then
  1965. begin
  1966. {$ifdef cpufpemu}
  1967. result := first_addfloat;
  1968. if assigned(result) then
  1969. exit;
  1970. {$endif cpufpemu}
  1971. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1972. expectloc:=LOC_FPUREGISTER
  1973. else
  1974. expectloc:=LOC_FLAGS;
  1975. calcregisters(self,0,1,0);
  1976. { an add node always first loads both the left and the }
  1977. { right in the fpu before doing the calculation. However, }
  1978. { calcregisters(0,2,0) will overestimate the number of }
  1979. { necessary registers (it will make it 3 in case one of }
  1980. { the operands is already in the fpu) (JM) }
  1981. if ((left.expectloc<>LOC_FPUREGISTER) or
  1982. (right.expectloc<>LOC_FPUREGISTER)) and
  1983. (registersfpu < 2) then
  1984. inc(registersfpu);
  1985. end
  1986. { pointer comperation and subtraction }
  1987. else if (ld.deftype=pointerdef) then
  1988. begin
  1989. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1990. expectloc:=LOC_REGISTER
  1991. else
  1992. expectloc:=LOC_FLAGS;
  1993. calcregisters(self,1,0,0);
  1994. end
  1995. else if is_class_or_interface(ld) then
  1996. begin
  1997. expectloc:=LOC_FLAGS;
  1998. calcregisters(self,1,0,0);
  1999. end
  2000. else if (ld.deftype=classrefdef) then
  2001. begin
  2002. expectloc:=LOC_FLAGS;
  2003. calcregisters(self,1,0,0);
  2004. end
  2005. { support procvar=nil,procvar<>nil }
  2006. else if ((ld.deftype=procvardef) and (rt=niln)) or
  2007. ((rd.deftype=procvardef) and (lt=niln)) then
  2008. begin
  2009. expectloc:=LOC_FLAGS;
  2010. calcregisters(self,1,0,0);
  2011. end
  2012. {$ifdef SUPPORT_MMX}
  2013. { mmx support, this must be before the zero based array
  2014. check }
  2015. else if (cs_mmx in aktlocalswitches) and is_mmx_able_array(ld) and
  2016. is_mmx_able_array(rd) then
  2017. begin
  2018. expectloc:=LOC_MMXREGISTER;
  2019. calcregisters(self,0,0,1);
  2020. end
  2021. {$endif SUPPORT_MMX}
  2022. else if (rd.deftype=pointerdef) or (ld.deftype=pointerdef) then
  2023. begin
  2024. expectloc:=LOC_REGISTER;
  2025. calcregisters(self,1,0,0);
  2026. end
  2027. else if (rd.deftype=procvardef) and
  2028. (ld.deftype=procvardef) and
  2029. equal_defs(rd,ld) then
  2030. begin
  2031. expectloc:=LOC_FLAGS;
  2032. calcregisters(self,1,0,0);
  2033. end
  2034. else if (ld.deftype=enumdef) then
  2035. begin
  2036. expectloc:=LOC_FLAGS;
  2037. calcregisters(self,1,0,0);
  2038. end
  2039. {$ifdef SUPPORT_MMX}
  2040. else if (cs_mmx in aktlocalswitches) and
  2041. is_mmx_able_array(ld) and
  2042. is_mmx_able_array(rd) then
  2043. begin
  2044. expectloc:=LOC_MMXREGISTER;
  2045. calcregisters(self,0,0,1);
  2046. end
  2047. {$endif SUPPORT_MMX}
  2048. { the general solution is to convert to 32 bit int }
  2049. else
  2050. begin
  2051. expectloc:=LOC_REGISTER;
  2052. calcregisters(self,1,0,0);
  2053. end;
  2054. end;
  2055. {$ifdef state_tracking}
  2056. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  2057. var factval:Tnode;
  2058. begin
  2059. track_state_pass:=false;
  2060. if left.track_state_pass(exec_known) then
  2061. begin
  2062. track_state_pass:=true;
  2063. left.resulttype.def:=nil;
  2064. do_resulttypepass(left);
  2065. end;
  2066. factval:=aktstate.find_fact(left);
  2067. if factval<>nil then
  2068. begin
  2069. track_state_pass:=true;
  2070. left.destroy;
  2071. left:=factval.getcopy;
  2072. end;
  2073. if right.track_state_pass(exec_known) then
  2074. begin
  2075. track_state_pass:=true;
  2076. right.resulttype.def:=nil;
  2077. do_resulttypepass(right);
  2078. end;
  2079. factval:=aktstate.find_fact(right);
  2080. if factval<>nil then
  2081. begin
  2082. track_state_pass:=true;
  2083. right.destroy;
  2084. right:=factval.getcopy;
  2085. end;
  2086. end;
  2087. {$endif}
  2088. begin
  2089. caddnode:=taddnode;
  2090. end.
  2091. {
  2092. $Log$
  2093. Revision 1.145 2005-04-25 08:29:00 peter
  2094. longword-longword gives int64
  2095. Revision 1.144 2005/04/06 07:31:51 michael
  2096. + * fix constant folding for string+char (from Peter)
  2097. Revision 1.143 2005/03/25 22:20:18 peter
  2098. * add hint when passing an uninitialized variable to a var parameter
  2099. Revision 1.142 2005/03/14 20:18:22 peter
  2100. * for methodpointers compare only proc field
  2101. Revision 1.141 2005/02/17 17:52:39 peter
  2102. * allow enum arithmetics inside an enum def, compatible with delphi
  2103. Revision 1.140 2005/02/14 17:13:06 peter
  2104. * truncate log
  2105. Revision 1.139 2005/01/31 21:30:56 olle
  2106. + Added fake Exception classes, only for MACOS.
  2107. Revision 1.138 2005/01/31 16:15:04 peter
  2108. * zero based array with elementsize>1 fix
  2109. Revision 1.137 2005/01/26 16:23:28 peter
  2110. * detect arithmetic overflows for constants at compile time
  2111. * use try..except instead of setjmp
  2112. Revision 1.136 2005/01/16 11:56:37 peter
  2113. * fixed some tabs
  2114. Revision 1.135 2005/01/16 11:13:40 peter
  2115. * ord-ord always gives a signed result
  2116. Revision 1.134 2005/01/10 22:10:26 peter
  2117. * widestring patches from Alexey Barkovoy
  2118. Revision 1.133 2005/01/02 17:31:07 peter
  2119. unsigned*unsigned will also have unsigned result.
  2120. }