nadd.pas 110 KB

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