nadd.pas 87 KB

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