nadd.pas 93 KB

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