nadd.pas 108 KB

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