nadd.pas 88 KB

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