nadd.pas 113 KB

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