nadd.pas 89 KB

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