nadd.pas 104 KB

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