nadd.pas 115 KB

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