nadd.pas 110 KB

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