nadd.pas 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Type checking and register allocation for add nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit nadd;
  19. {$i fpcdefs.inc}
  20. { define addstringopt}
  21. interface
  22. uses
  23. node;
  24. type
  25. taddnode = class(tbinopnode)
  26. constructor create(tt : tnodetype;l,r : tnode);override;
  27. function pass_1 : tnode;override;
  28. function det_resulttype:tnode;override;
  29. {$ifdef state_tracking}
  30. function track_state_pass(exec_known:boolean):boolean;override;
  31. {$endif}
  32. protected
  33. { override the following if you want to implement }
  34. { parts explicitely in the code generator (JM) }
  35. function first_addstring: tnode; virtual;
  36. function first_addset: tnode; virtual;
  37. { only implements "muln" nodes, the rest always has to be done in }
  38. { the code generator for performance reasons (JM) }
  39. function first_add64bitint: tnode; virtual;
  40. {$ifdef cpufpemu}
  41. { This routine calls internal runtime library helpers
  42. for all floating point arithmetic in the case
  43. where the emulation switches is on. Otherwise
  44. returns nil, and everything must be done in
  45. the code generation phase.
  46. }
  47. function first_addfloat : tnode; virtual;
  48. {$endif cpufpemu}
  49. end;
  50. taddnodeclass = class of taddnode;
  51. var
  52. { caddnode is used to create nodes of the add type }
  53. { the virtual constructor allows to assign }
  54. { another class type to caddnode => processor }
  55. { specific node types can be created }
  56. caddnode : taddnodeclass;
  57. implementation
  58. uses
  59. globtype,systems,
  60. cutils,verbose,globals,widestr,
  61. symconst,symtype,symdef,symsym,symtable,defutil,defcmp,
  62. cgbase,
  63. htypechk,pass_1,
  64. nbas,nmat,ncnv,ncon,nset,nopt,ncal,ninl,nmem,nutils,
  65. {$ifdef state_tracking}
  66. nstate,
  67. {$endif}
  68. cpuinfo,procinfo;
  69. {*****************************************************************************
  70. TADDNODE
  71. *****************************************************************************}
  72. {$ifdef fpc}
  73. {$maxfpuregisters 0}
  74. {$endif fpc}
  75. constructor taddnode.create(tt : tnodetype;l,r : tnode);
  76. begin
  77. inherited create(tt,l,r);
  78. end;
  79. function taddnode.det_resulttype:tnode;
  80. var
  81. hp,t : tnode;
  82. lt,rt : tnodetype;
  83. rd,ld : tdef;
  84. htype : ttype;
  85. ot : tnodetype;
  86. concatstrings : boolean;
  87. resultset : Tconstset;
  88. i : longint;
  89. b : boolean;
  90. c1,c2 :char;
  91. s1,s2 : pchar;
  92. ws1,ws2 : pcompilerwidestring;
  93. l1,l2 : longint;
  94. rv,lv : tconstexprint;
  95. rvd,lvd : bestreal;
  96. resultrealtype : ttype;
  97. {$ifdef state_tracking}
  98. factval : Tnode;
  99. change : boolean;
  100. {$endif}
  101. begin
  102. result:=nil;
  103. { first do the two subtrees }
  104. resulttypepass(left);
  105. resulttypepass(right);
  106. { both left and right need to be valid }
  107. set_varstate(left,vs_used,true);
  108. set_varstate(right,vs_used,true);
  109. if codegenerror then
  110. exit;
  111. { tp procvar support }
  112. maybe_call_procvar(left,true);
  113. maybe_call_procvar(right,true);
  114. { convert array constructors to sets, because there is no other operator
  115. possible for array constructors }
  116. if is_array_constructor(left.resulttype.def) then
  117. begin
  118. arrayconstructor_to_set(left);
  119. resulttypepass(left);
  120. end;
  121. if is_array_constructor(right.resulttype.def) then
  122. begin
  123. arrayconstructor_to_set(right);
  124. resulttypepass(right);
  125. end;
  126. { allow operator overloading }
  127. hp:=self;
  128. if isbinaryoverloaded(hp) then
  129. begin
  130. result:=hp;
  131. exit;
  132. end;
  133. { Stop checking when an error was found in the operator checking }
  134. if codegenerror then
  135. begin
  136. result:=cerrornode.create;
  137. exit;
  138. end;
  139. { Kylix allows enum+ordconstn in an enum declaration (blocktype
  140. is bt_type), we need to do the conversion here before the
  141. constant folding }
  142. if (m_delphi in aktmodeswitches) and
  143. (blocktype=bt_type) then
  144. begin
  145. if (left.resulttype.def.deftype=enumdef) and
  146. (right.resulttype.def.deftype=orddef) then
  147. begin
  148. { insert explicit typecast to default signed int }
  149. left:=ctypeconvnode.create_explicit(left,sinttype);
  150. resulttypepass(left);
  151. end
  152. else
  153. if (left.resulttype.def.deftype=orddef) and
  154. (right.resulttype.def.deftype=enumdef) then
  155. begin
  156. { insert explicit typecast to default signed int }
  157. right:=ctypeconvnode.create_explicit(right,sinttype);
  158. resulttypepass(right);
  159. end;
  160. end;
  161. { is one a real float, then both need to be floats, this
  162. need to be done before the constant folding so constant
  163. operation on a float and int are also handled }
  164. resultrealtype:=pbestrealtype^;
  165. if (right.resulttype.def.deftype=floatdef) or (left.resulttype.def.deftype=floatdef) then
  166. begin
  167. { when both floattypes are already equal then use that
  168. floattype for results }
  169. if (right.resulttype.def.deftype=floatdef) and
  170. (left.resulttype.def.deftype=floatdef) and
  171. (tfloatdef(left.resulttype.def).typ=tfloatdef(right.resulttype.def).typ) then
  172. resultrealtype:=left.resulttype
  173. { when there is a currency type then use currency, but
  174. only when currency is defined as float }
  175. else
  176. if (is_currency(right.resulttype.def) or
  177. is_currency(left.resulttype.def)) and
  178. ((s64currencytype.def.deftype = floatdef) or
  179. (nodetype <> slashn)) then
  180. begin
  181. resultrealtype:=s64currencytype;
  182. inserttypeconv(right,resultrealtype);
  183. inserttypeconv(left,resultrealtype);
  184. end
  185. else
  186. begin
  187. inserttypeconv(right,resultrealtype);
  188. inserttypeconv(left,resultrealtype);
  189. end;
  190. end;
  191. { if one operand is a widechar or a widestring, both operands }
  192. { are converted to widestring. This must be done before constant }
  193. { folding to allow char+widechar etc. }
  194. if is_widestring(right.resulttype.def) or
  195. is_widestring(left.resulttype.def) or
  196. is_widechar(right.resulttype.def) or
  197. is_widechar(left.resulttype.def) then
  198. begin
  199. inserttypeconv(right,cwidestringtype);
  200. inserttypeconv(left,cwidestringtype);
  201. end;
  202. { load easier access variables }
  203. rd:=right.resulttype.def;
  204. ld:=left.resulttype.def;
  205. rt:=right.nodetype;
  206. lt:=left.nodetype;
  207. if (nodetype = slashn) and
  208. (((rt = ordconstn) and
  209. (tordconstnode(right).value = 0)) or
  210. ((rt = realconstn) and
  211. (trealconstnode(right).value_real = 0.0))) then
  212. begin
  213. if (cs_check_range in aktlocalswitches) or
  214. (cs_check_overflow in aktlocalswitches) then
  215. begin
  216. result:=crealconstnode.create(1,pbestrealtype^);
  217. Message(parser_e_division_by_zero);
  218. exit;
  219. end;
  220. end;
  221. { both are int constants }
  222. if ((is_constintnode(left) and is_constintnode(right)) or
  223. (is_constboolnode(left) and is_constboolnode(right) and
  224. (nodetype in [slashn,ltn,lten,gtn,gten,equaln,unequaln,andn,xorn,orn])) or
  225. (is_constenumnode(left) and is_constenumnode(right) and
  226. (nodetype in [equaln,unequaln,ltn,lten,gtn,gten]))) or
  227. { support pointer arithmetics on constants (JM) }
  228. ((lt = pointerconstn) and is_constintnode(right) and
  229. (nodetype in [addn,subn])) or
  230. (((lt = pointerconstn) or (lt = niln)) and
  231. ((rt = pointerconstn) or (rt = niln)) and
  232. (nodetype in [ltn,lten,gtn,gten,equaln,unequaln,subn])) then
  233. begin
  234. { when comparing/substracting pointers, make sure they are }
  235. { of the same type (JM) }
  236. if (lt = pointerconstn) and (rt = pointerconstn) then
  237. begin
  238. if not(cs_extsyntax in aktmoduleswitches) and
  239. not(nodetype in [equaln,unequaln]) then
  240. CGMessage(type_e_mismatch)
  241. else
  242. if (nodetype <> subn) and
  243. is_voidpointer(rd) then
  244. inserttypeconv(right,left.resulttype)
  245. else if (nodetype <> subn) and
  246. is_voidpointer(ld) then
  247. inserttypeconv(left,right.resulttype)
  248. else if not(equal_defs(ld,rd)) then
  249. IncompatibleTypes(ld,rd);
  250. end
  251. else if (ld.deftype=enumdef) and (rd.deftype=enumdef) then
  252. begin
  253. if not(equal_defs(ld,rd)) then
  254. inserttypeconv(right,left.resulttype);
  255. end
  256. else if (lt=ordconstn) and (rt=ordconstn) then
  257. begin
  258. { make left const type the biggest (u32bit is bigger than
  259. s32bit for or,and,xor) }
  260. if (rd.size>ld.size) or
  261. ((torddef(rd).typ=torddef(uinttype.def).typ) and
  262. (torddef(ld).typ=torddef(sinttype.def).typ) and
  263. (nodetype in [orn,andn,xorn])) then
  264. inserttypeconv(left,right.resulttype);
  265. end;
  266. { load values }
  267. case lt of
  268. ordconstn:
  269. lv:=tordconstnode(left).value;
  270. pointerconstn:
  271. lv:=tpointerconstnode(left).value;
  272. niln:
  273. lv:=0;
  274. else
  275. internalerror(2002080202);
  276. end;
  277. case rt of
  278. ordconstn:
  279. rv:=tordconstnode(right).value;
  280. pointerconstn:
  281. rv:=tpointerconstnode(right).value;
  282. niln:
  283. rv:=0;
  284. else
  285. internalerror(2002080203);
  286. end;
  287. if (lt = pointerconstn) and
  288. (rt <> pointerconstn) then
  289. rv := rv * tpointerdef(left.resulttype.def).pointertype.def.size;
  290. if (rt = pointerconstn) and
  291. (lt <> pointerconstn) then
  292. lv := lv * tpointerdef(right.resulttype.def).pointertype.def.size;
  293. case nodetype of
  294. addn :
  295. if (lt <> pointerconstn) then
  296. t := genintconstnode(lv+rv)
  297. else
  298. t := cpointerconstnode.create(lv+rv,left.resulttype);
  299. subn :
  300. if (lt <> pointerconstn) or (rt = pointerconstn) then
  301. t := genintconstnode(lv-rv)
  302. else
  303. t := cpointerconstnode.create(lv-rv,left.resulttype);
  304. muln :
  305. if (torddef(ld).typ <> u64bit) or
  306. (torddef(rd).typ <> u64bit) then
  307. t:=genintconstnode(lv*rv)
  308. else
  309. t:=genintconstnode(int64(qword(lv)*qword(rv)));
  310. xorn :
  311. t:=cordconstnode.create(lv xor rv,left.resulttype,true);
  312. orn :
  313. t:=cordconstnode.create(lv or rv,left.resulttype,true);
  314. andn :
  315. t:=cordconstnode.create(lv and rv,left.resulttype,true);
  316. ltn :
  317. t:=cordconstnode.create(ord(lv<rv),booltype,true);
  318. lten :
  319. t:=cordconstnode.create(ord(lv<=rv),booltype,true);
  320. gtn :
  321. t:=cordconstnode.create(ord(lv>rv),booltype,true);
  322. gten :
  323. t:=cordconstnode.create(ord(lv>=rv),booltype,true);
  324. equaln :
  325. t:=cordconstnode.create(ord(lv=rv),booltype,true);
  326. unequaln :
  327. t:=cordconstnode.create(ord(lv<>rv),booltype,true);
  328. slashn :
  329. begin
  330. { int/int becomes a real }
  331. rvd:=rv;
  332. lvd:=lv;
  333. t:=crealconstnode.create(lvd/rvd,resultrealtype);
  334. end;
  335. else
  336. begin
  337. CGMessage(type_e_mismatch);
  338. t:=cnothingnode.create;
  339. end;
  340. end;
  341. result:=t;
  342. exit;
  343. end;
  344. { both real constants ? }
  345. if (lt=realconstn) and (rt=realconstn) then
  346. begin
  347. lvd:=trealconstnode(left).value_real;
  348. rvd:=trealconstnode(right).value_real;
  349. case nodetype of
  350. addn :
  351. t:=crealconstnode.create(lvd+rvd,resultrealtype);
  352. subn :
  353. t:=crealconstnode.create(lvd-rvd,resultrealtype);
  354. muln :
  355. t:=crealconstnode.create(lvd*rvd,resultrealtype);
  356. starstarn,
  357. caretn :
  358. begin
  359. if lvd<0 then
  360. begin
  361. Message(parser_e_invalid_float_operation);
  362. t:=crealconstnode.create(0,resultrealtype);
  363. end
  364. else if lvd=0 then
  365. t:=crealconstnode.create(1.0,resultrealtype)
  366. else
  367. t:=crealconstnode.create(exp(ln(lvd)*rvd),resultrealtype);
  368. end;
  369. slashn :
  370. t:=crealconstnode.create(lvd/rvd,resultrealtype);
  371. ltn :
  372. t:=cordconstnode.create(ord(lvd<rvd),booltype,true);
  373. lten :
  374. t:=cordconstnode.create(ord(lvd<=rvd),booltype,true);
  375. gtn :
  376. t:=cordconstnode.create(ord(lvd>rvd),booltype,true);
  377. gten :
  378. t:=cordconstnode.create(ord(lvd>=rvd),booltype,true);
  379. equaln :
  380. t:=cordconstnode.create(ord(lvd=rvd),booltype,true);
  381. unequaln :
  382. t:=cordconstnode.create(ord(lvd<>rvd),booltype,true);
  383. else
  384. begin
  385. CGMessage(type_e_mismatch);
  386. t:=cnothingnode.create;
  387. end;
  388. end;
  389. result:=t;
  390. exit;
  391. end;
  392. { first, we handle widestrings, so we can check later for }
  393. { stringconstn only }
  394. { widechars are converted above to widestrings too }
  395. { this isn't veryy efficient, but I don't think }
  396. { that it does matter that much (FK) }
  397. if (lt=stringconstn) and (rt=stringconstn) and
  398. (tstringconstnode(left).st_type=st_widestring) and
  399. (tstringconstnode(right).st_type=st_widestring) then
  400. begin
  401. initwidestring(ws1);
  402. initwidestring(ws2);
  403. copywidestring(pcompilerwidestring(tstringconstnode(left).value_str),ws1);
  404. copywidestring(pcompilerwidestring(tstringconstnode(right).value_str),ws2);
  405. case nodetype of
  406. addn :
  407. begin
  408. concatwidestrings(ws1,ws2);
  409. t:=cstringconstnode.createwstr(ws1);
  410. end;
  411. ltn :
  412. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<0),booltype,true);
  413. lten :
  414. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<=0),booltype,true);
  415. gtn :
  416. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)>0),booltype,true);
  417. gten :
  418. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)>=0),booltype,true);
  419. equaln :
  420. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)=0),booltype,true);
  421. unequaln :
  422. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<>0),booltype,true);
  423. else
  424. begin
  425. CGMessage(type_e_mismatch);
  426. t:=cnothingnode.create;
  427. end;
  428. end;
  429. donewidestring(ws1);
  430. donewidestring(ws2);
  431. result:=t;
  432. exit;
  433. end;
  434. { concating strings ? }
  435. concatstrings:=false;
  436. if (lt=ordconstn) and (rt=ordconstn) and
  437. is_char(ld) and is_char(rd) then
  438. begin
  439. c1:=char(byte(tordconstnode(left).value));
  440. l1:=1;
  441. c2:=char(byte(tordconstnode(right).value));
  442. l2:=1;
  443. s1:=@c1;
  444. s2:=@c2;
  445. concatstrings:=true;
  446. end
  447. else if (lt=stringconstn) and (rt=ordconstn) and is_char(rd) then
  448. begin
  449. s1:=tstringconstnode(left).value_str;
  450. l1:=tstringconstnode(left).len;
  451. c2:=char(byte(tordconstnode(right).value));
  452. s2:=@c2;
  453. l2:=1;
  454. concatstrings:=true;
  455. end
  456. else if (lt=ordconstn) and (rt=stringconstn) and is_char(ld) then
  457. begin
  458. c1:=char(byte(tordconstnode(left).value));
  459. l1:=1;
  460. s1:=@c1;
  461. s2:=tstringconstnode(right).value_str;
  462. l2:=tstringconstnode(right).len;
  463. concatstrings:=true;
  464. end
  465. else if (lt=stringconstn) and (rt=stringconstn) then
  466. begin
  467. s1:=tstringconstnode(left).value_str;
  468. l1:=tstringconstnode(left).len;
  469. s2:=tstringconstnode(right).value_str;
  470. l2:=tstringconstnode(right).len;
  471. concatstrings:=true;
  472. end;
  473. if concatstrings then
  474. begin
  475. case nodetype of
  476. addn :
  477. t:=cstringconstnode.createpchar(concatansistrings(s1,s2,l1,l2),l1+l2);
  478. ltn :
  479. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<0),booltype,true);
  480. lten :
  481. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<=0),booltype,true);
  482. gtn :
  483. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)>0),booltype,true);
  484. gten :
  485. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)>=0),booltype,true);
  486. equaln :
  487. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)=0),booltype,true);
  488. unequaln :
  489. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<>0),booltype,true);
  490. end;
  491. result:=t;
  492. exit;
  493. end;
  494. { set constant evaluation }
  495. if (right.nodetype=setconstn) and
  496. not assigned(tsetconstnode(right).left) and
  497. (left.nodetype=setconstn) and
  498. not assigned(tsetconstnode(left).left) then
  499. begin
  500. { check if size adjusting is needed, only for left
  501. to right as the other way is checked in the typeconv }
  502. if (tsetdef(right.resulttype.def).settype=smallset) and
  503. (tsetdef(left.resulttype.def).settype<>smallset) then
  504. right.resulttype.setdef(tsetdef.create(tsetdef(right.resulttype.def).elementtype,255));
  505. { check base types }
  506. inserttypeconv(left,right.resulttype);
  507. if codegenerror then
  508. begin
  509. { recover by only returning the left part }
  510. result:=left;
  511. left:=nil;
  512. exit;
  513. end;
  514. case nodetype of
  515. addn :
  516. begin
  517. resultset:=tsetconstnode(right).value_set^ + tsetconstnode(left).value_set^;
  518. t:=csetconstnode.create(@resultset,left.resulttype);
  519. end;
  520. muln :
  521. begin
  522. resultset:=tsetconstnode(right).value_set^ * tsetconstnode(left).value_set^;
  523. t:=csetconstnode.create(@resultset,left.resulttype);
  524. end;
  525. subn :
  526. begin
  527. resultset:=tsetconstnode(left).value_set^ - tsetconstnode(right).value_set^;
  528. t:=csetconstnode.create(@resultset,left.resulttype);
  529. end;
  530. symdifn :
  531. begin
  532. resultset:=tsetconstnode(right).value_set^ >< tsetconstnode(left).value_set^;
  533. t:=csetconstnode.create(@resultset,left.resulttype);
  534. end;
  535. unequaln :
  536. begin
  537. b:=tsetconstnode(right).value_set^ <> tsetconstnode(left).value_set^;
  538. t:=cordconstnode.create(byte(b),booltype,true);
  539. end;
  540. equaln :
  541. begin
  542. b:=tsetconstnode(right).value_set^ = tsetconstnode(left).value_set^;
  543. t:=cordconstnode.create(byte(b),booltype,true);
  544. end;
  545. lten :
  546. begin
  547. b:=tsetconstnode(left).value_set^ <= tsetconstnode(right).value_set^;
  548. t:=cordconstnode.create(byte(b),booltype,true);
  549. end;
  550. gten :
  551. begin
  552. b:=tsetconstnode(left).value_set^ >= tsetconstnode(right).value_set^;
  553. t:=cordconstnode.create(byte(b),booltype,true);
  554. end;
  555. end;
  556. result:=t;
  557. exit;
  558. end;
  559. { but an int/int gives real/real! }
  560. if nodetype=slashn then
  561. begin
  562. if is_currency(left.resulttype.def) and
  563. is_currency(right.resulttype.def) then
  564. { In case of currency, converting to float means dividing by 10000 }
  565. { However, since this is already a division, both divisions by }
  566. { 10000 are eliminated when we divide the results -> we can skip }
  567. { them. }
  568. if s64currencytype.def.deftype = floatdef then
  569. begin
  570. { there's no s64comptype or so, how do we avoid the type conversion?
  571. left.resulttype := s64comptype;
  572. right.resulttype := s64comptype; }
  573. end
  574. else
  575. begin
  576. left.resulttype := s64inttype;
  577. right.resulttype := s64inttype;
  578. end
  579. else if (left.resulttype.def.deftype <> floatdef) and
  580. (right.resulttype.def.deftype <> floatdef) then
  581. CGMessage(type_h_use_div_for_int);
  582. inserttypeconv(right,resultrealtype);
  583. inserttypeconv(left,resultrealtype);
  584. end
  585. { if both are orddefs then check sub types }
  586. else if (ld.deftype=orddef) and (rd.deftype=orddef) then
  587. begin
  588. { optimize multiplacation by a power of 2 }
  589. if not(cs_check_overflow in aktlocalswitches) and
  590. (nodetype = muln) and
  591. (((left.nodetype = ordconstn) and
  592. ispowerof2(tordconstnode(left).value,i)) or
  593. ((right.nodetype = ordconstn) and
  594. ispowerof2(tordconstnode(right).value,i))) then
  595. begin
  596. if left.nodetype = ordconstn then
  597. begin
  598. tordconstnode(left).value := i;
  599. result := cshlshrnode.create(shln,right,left);
  600. end
  601. else
  602. begin
  603. tordconstnode(right).value := i;
  604. result := cshlshrnode.create(shln,left,right);
  605. end;
  606. left := nil;
  607. right := nil;
  608. exit;
  609. end;
  610. { 2 booleans? Make them equal to the largest boolean }
  611. if is_boolean(ld) and is_boolean(rd) then
  612. begin
  613. if torddef(left.resulttype.def).size>torddef(right.resulttype.def).size then
  614. begin
  615. right:=ctypeconvnode.create_explicit(right,left.resulttype);
  616. ttypeconvnode(right).convtype:=tc_bool_2_int;
  617. resulttypepass(right);
  618. end
  619. else if torddef(left.resulttype.def).size<torddef(right.resulttype.def).size then
  620. begin
  621. left:=ctypeconvnode.create_explicit(left,right.resulttype);
  622. ttypeconvnode(left).convtype:=tc_bool_2_int;
  623. resulttypepass(left);
  624. end;
  625. case nodetype of
  626. xorn,
  627. ltn,
  628. lten,
  629. gtn,
  630. gten,
  631. andn,
  632. orn:
  633. begin
  634. end;
  635. unequaln,
  636. equaln:
  637. begin
  638. if not(cs_full_boolean_eval in aktlocalswitches) then
  639. begin
  640. { Remove any compares with constants }
  641. if (left.nodetype=ordconstn) then
  642. begin
  643. hp:=right;
  644. b:=(tordconstnode(left).value<>0);
  645. ot:=nodetype;
  646. left.free;
  647. left:=nil;
  648. right:=nil;
  649. if (not(b) and (ot=equaln)) or
  650. (b and (ot=unequaln)) then
  651. begin
  652. hp:=cnotnode.create(hp);
  653. end;
  654. result:=hp;
  655. exit;
  656. end;
  657. if (right.nodetype=ordconstn) then
  658. begin
  659. hp:=left;
  660. b:=(tordconstnode(right).value<>0);
  661. ot:=nodetype;
  662. right.free;
  663. right:=nil;
  664. left:=nil;
  665. if (not(b) and (ot=equaln)) or
  666. (b and (ot=unequaln)) then
  667. begin
  668. hp:=cnotnode.create(hp);
  669. end;
  670. result:=hp;
  671. exit;
  672. end;
  673. end;
  674. end;
  675. else
  676. CGMessage(type_e_mismatch);
  677. end;
  678. end
  679. { Both are chars? }
  680. else if is_char(rd) and is_char(ld) then
  681. begin
  682. if nodetype=addn then
  683. begin
  684. resulttype:=cshortstringtype;
  685. if not(is_constcharnode(left) and is_constcharnode(right)) then
  686. begin
  687. inserttypeconv(left,cshortstringtype);
  688. {$ifdef addstringopt}
  689. hp := genaddsstringcharoptnode(self);
  690. result := hp;
  691. exit;
  692. {$endif addstringopt}
  693. end;
  694. end;
  695. end
  696. { is there a currency type ? }
  697. else if ((torddef(rd).typ=scurrency) or (torddef(ld).typ=scurrency)) then
  698. begin
  699. if (torddef(ld).typ<>scurrency) then
  700. inserttypeconv(left,s64currencytype);
  701. if (torddef(rd).typ<>scurrency) then
  702. inserttypeconv(right,s64currencytype);
  703. end
  704. { is there a signed 64 bit type ? }
  705. else if ((torddef(rd).typ=s64bit) or (torddef(ld).typ=s64bit)) then
  706. begin
  707. if (torddef(ld).typ<>s64bit) then
  708. inserttypeconv(left,s64inttype);
  709. if (torddef(rd).typ<>s64bit) then
  710. inserttypeconv(right,s64inttype);
  711. end
  712. { is there a unsigned 64 bit type ? }
  713. else if ((torddef(rd).typ=u64bit) or (torddef(ld).typ=u64bit)) then
  714. begin
  715. if (torddef(ld).typ<>u64bit) then
  716. inserttypeconv(left,u64inttype);
  717. if (torddef(rd).typ<>u64bit) then
  718. inserttypeconv(right,u64inttype);
  719. end
  720. { 64 bit cpus do calculations always in 64 bit }
  721. {$ifndef cpu64bit}
  722. { is there a cardinal? }
  723. else if ((torddef(rd).typ=u32bit) or (torddef(ld).typ=u32bit)) then
  724. begin
  725. { and,or,xor work on bit patterns and don't care
  726. about the sign }
  727. if nodetype in [andn,orn,xorn] then
  728. begin
  729. inserttypeconv_explicit(left,u32inttype);
  730. inserttypeconv_explicit(right,u32inttype);
  731. end
  732. else
  733. begin
  734. if is_signed(ld) and
  735. { then rd = u32bit }
  736. { convert positive constants to u32bit }
  737. not(is_constintnode(left) and
  738. (tordconstnode(left).value >= 0)) then
  739. begin
  740. { perform the operation in 64bit }
  741. CGMessage(type_w_mixed_signed_unsigned);
  742. inserttypeconv(left,s64inttype);
  743. inserttypeconv(right,s64inttype);
  744. end
  745. else
  746. begin
  747. if is_signed(ld) and
  748. not(is_constintnode(left) and
  749. (tordconstnode(left).value >= 0)) then
  750. CGMessage(type_w_mixed_signed_unsigned2);
  751. inserttypeconv(left,u32inttype);
  752. if is_signed(rd) and
  753. { then ld = u32bit }
  754. { convert positive constants to u32bit }
  755. not(is_constintnode(right) and
  756. (tordconstnode(right).value >= 0)) then
  757. begin
  758. { perform the operation in 64bit }
  759. CGMessage(type_w_mixed_signed_unsigned);
  760. inserttypeconv(left,s64inttype);
  761. inserttypeconv(right,s64inttype);
  762. end
  763. else
  764. begin
  765. if is_signed(rd) and
  766. not(is_constintnode(right) and
  767. (tordconstnode(right).value >= 0)) then
  768. CGMessage(type_w_mixed_signed_unsigned2);
  769. inserttypeconv(right,u32inttype);
  770. end;
  771. end;
  772. end;
  773. end
  774. {$endif cpu64bit}
  775. { generic ord conversion is sinttype }
  776. else
  777. begin
  778. { if the left or right value is smaller than the normal
  779. type s32inttype and is unsigned, and the other value
  780. is a constant < 0, the result will always be false/true
  781. for equal / unequal nodes.
  782. }
  783. if (
  784. { left : unsigned ordinal var, right : < 0 constant }
  785. (
  786. ((is_signed(ld)=false) and (is_constintnode(left) =false)) and
  787. ((is_constintnode(right)) and (tordconstnode(right).value < 0))
  788. ) or
  789. { right : unsigned ordinal var, left : < 0 constant }
  790. (
  791. ((is_signed(rd)=false) and (is_constintnode(right) =false)) and
  792. ((is_constintnode(left)) and (tordconstnode(left).value < 0))
  793. )
  794. ) then
  795. begin
  796. if nodetype = equaln then
  797. CGMessage(type_w_signed_unsigned_always_false)
  798. else
  799. if nodetype = unequaln then
  800. CGMessage(type_w_signed_unsigned_always_true)
  801. else
  802. if (is_constintnode(left) and (nodetype in [ltn,lten])) or
  803. (is_constintnode(right) and (nodetype in [gtn,gten])) then
  804. CGMessage(type_w_signed_unsigned_always_true)
  805. else
  806. if (is_constintnode(right) and (nodetype in [ltn,lten])) or
  807. (is_constintnode(left) and (nodetype in [gtn,gten])) then
  808. CGMessage(type_w_signed_unsigned_always_false);
  809. end;
  810. inserttypeconv(right,sinttype);
  811. inserttypeconv(left,sinttype);
  812. end;
  813. end
  814. { if both are floatdefs, conversion is already done before constant folding }
  815. else if (ld.deftype=floatdef) then
  816. begin
  817. { already converted }
  818. end
  819. { left side a setdef, must be before string processing,
  820. else array constructor can be seen as array of char (PFV) }
  821. else if (ld.deftype=setdef) then
  822. begin
  823. { trying to add a set element? }
  824. if (nodetype=addn) and (rd.deftype<>setdef) then
  825. begin
  826. if (rt=setelementn) then
  827. begin
  828. if not(equal_defs(tsetdef(ld).elementtype.def,rd)) then
  829. CGMessage(type_e_set_element_are_not_comp);
  830. end
  831. else
  832. CGMessage(type_e_mismatch)
  833. end
  834. else
  835. begin
  836. if not(nodetype in [addn,subn,symdifn,muln,equaln,unequaln,lten,gten]) then
  837. CGMessage(type_e_set_operation_unknown);
  838. { right def must be a also be set }
  839. if (rd.deftype<>setdef) or not(equal_defs(rd,ld)) then
  840. CGMessage(type_e_set_element_are_not_comp);
  841. end;
  842. { ranges require normsets }
  843. if (tsetdef(ld).settype=smallset) and
  844. (rt=setelementn) and
  845. assigned(tsetelementnode(right).right) then
  846. begin
  847. { generate a temporary normset def, it'll be destroyed
  848. when the symtable is unloaded }
  849. htype.setdef(tsetdef.create(tsetdef(ld).elementtype,255));
  850. inserttypeconv(left,htype);
  851. end;
  852. { if the right side is also a setdef then the settype must
  853. be the same as the left setdef }
  854. if (rd.deftype=setdef) and
  855. (tsetdef(ld).settype<>tsetdef(rd).settype) then
  856. begin
  857. { when right is a normset we need to typecast both
  858. to normsets }
  859. if (tsetdef(rd).settype=normset) then
  860. inserttypeconv(left,right.resulttype)
  861. else
  862. inserttypeconv(right,left.resulttype);
  863. end;
  864. end
  865. { compare pchar to char arrays by addresses like BP/Delphi }
  866. else if ((is_pchar(ld) or (lt=niln)) and is_chararray(rd)) or
  867. ((is_pchar(rd) or (rt=niln)) and is_chararray(ld)) then
  868. begin
  869. if is_chararray(rd) then
  870. inserttypeconv(right,charpointertype)
  871. else
  872. inserttypeconv(left,charpointertype);
  873. end
  874. { pointer comparision and subtraction }
  875. else if (rd.deftype=pointerdef) and (ld.deftype=pointerdef) then
  876. begin
  877. case nodetype of
  878. equaln,unequaln :
  879. begin
  880. if is_voidpointer(right.resulttype.def) then
  881. inserttypeconv(right,left.resulttype)
  882. else if is_voidpointer(left.resulttype.def) then
  883. inserttypeconv(left,right.resulttype)
  884. else if not(equal_defs(ld,rd)) then
  885. IncompatibleTypes(ld,rd);
  886. end;
  887. ltn,lten,gtn,gten:
  888. begin
  889. if (cs_extsyntax in aktmoduleswitches) then
  890. begin
  891. if is_voidpointer(right.resulttype.def) then
  892. inserttypeconv(right,left.resulttype)
  893. else if is_voidpointer(left.resulttype.def) then
  894. inserttypeconv(left,right.resulttype)
  895. else if not(equal_defs(ld,rd)) then
  896. IncompatibleTypes(ld,rd);
  897. end
  898. else
  899. CGMessage(type_e_mismatch);
  900. end;
  901. subn:
  902. begin
  903. if (cs_extsyntax in aktmoduleswitches) then
  904. begin
  905. if is_voidpointer(right.resulttype.def) then
  906. inserttypeconv(right,left.resulttype)
  907. else if is_voidpointer(left.resulttype.def) then
  908. inserttypeconv(left,right.resulttype)
  909. else if not(equal_defs(ld,rd)) then
  910. IncompatibleTypes(ld,rd);
  911. end
  912. else
  913. CGMessage(type_e_mismatch);
  914. resulttype:=sinttype;
  915. exit;
  916. end;
  917. addn:
  918. begin
  919. if (cs_extsyntax in aktmoduleswitches) then
  920. begin
  921. if is_voidpointer(right.resulttype.def) then
  922. inserttypeconv(right,left.resulttype)
  923. else if is_voidpointer(left.resulttype.def) then
  924. inserttypeconv(left,right.resulttype)
  925. else if not(equal_defs(ld,rd)) then
  926. IncompatibleTypes(ld,rd);
  927. end
  928. else
  929. CGMessage(type_e_mismatch);
  930. resulttype:=sinttype;
  931. exit;
  932. end;
  933. else
  934. CGMessage(type_e_mismatch);
  935. end;
  936. end
  937. { is one of the operands a string?,
  938. chararrays are also handled as strings (after conversion), also take
  939. care of chararray+chararray and chararray+char.
  940. Note: Must be done after pointerdef+pointerdef has been checked, else
  941. pchar is converted to string }
  942. else if (rd.deftype=stringdef) or (ld.deftype=stringdef) or
  943. ((is_pchar(rd) or is_chararray(rd) or is_char(rd)) and
  944. (is_pchar(ld) or is_chararray(ld) or is_char(ld))) then
  945. begin
  946. if is_widestring(rd) or is_widestring(ld) then
  947. begin
  948. if not(is_widestring(rd)) then
  949. inserttypeconv(right,cwidestringtype);
  950. if not(is_widestring(ld)) then
  951. inserttypeconv(left,cwidestringtype);
  952. end
  953. else if is_ansistring(rd) or is_ansistring(ld) then
  954. begin
  955. if not(is_ansistring(rd)) then
  956. inserttypeconv(right,cansistringtype);
  957. if not(is_ansistring(ld)) then
  958. inserttypeconv(left,cansistringtype);
  959. end
  960. else if is_longstring(rd) or is_longstring(ld) then
  961. begin
  962. if not(is_longstring(rd)) then
  963. inserttypeconv(right,clongstringtype);
  964. if not(is_longstring(ld)) then
  965. inserttypeconv(left,clongstringtype);
  966. end
  967. else
  968. begin
  969. if not(is_shortstring(ld)) then
  970. inserttypeconv(left,cshortstringtype);
  971. { don't convert char, that can be handled by the optimized node }
  972. if not(is_shortstring(rd) or is_char(rd)) then
  973. inserttypeconv(right,cshortstringtype);
  974. end;
  975. end
  976. { class or interface equation }
  977. else if is_class_or_interface(rd) or is_class_or_interface(ld) then
  978. begin
  979. if is_class_or_interface(rd) and is_class_or_interface(ld) then
  980. begin
  981. if tobjectdef(rd).is_related(tobjectdef(ld)) then
  982. inserttypeconv(right,left.resulttype)
  983. else
  984. inserttypeconv(left,right.resulttype);
  985. end
  986. else if is_class_or_interface(rd) then
  987. inserttypeconv(left,right.resulttype)
  988. else
  989. inserttypeconv(right,left.resulttype);
  990. if not(nodetype in [equaln,unequaln]) then
  991. CGMessage(type_e_mismatch);
  992. end
  993. else if (rd.deftype=classrefdef) and (ld.deftype=classrefdef) then
  994. begin
  995. if tobjectdef(tclassrefdef(rd).pointertype.def).is_related(
  996. tobjectdef(tclassrefdef(ld).pointertype.def)) then
  997. inserttypeconv(right,left.resulttype)
  998. else
  999. inserttypeconv(left,right.resulttype);
  1000. if not(nodetype in [equaln,unequaln]) then
  1001. CGMessage(type_e_mismatch);
  1002. end
  1003. { allows comperasion with nil pointer }
  1004. else if is_class_or_interface(rd) or (rd.deftype=classrefdef) then
  1005. begin
  1006. inserttypeconv(left,right.resulttype);
  1007. if not(nodetype in [equaln,unequaln]) then
  1008. CGMessage(type_e_mismatch);
  1009. end
  1010. else if is_class_or_interface(ld) or (ld.deftype=classrefdef) then
  1011. begin
  1012. inserttypeconv(right,left.resulttype);
  1013. if not(nodetype in [equaln,unequaln]) then
  1014. CGMessage(type_e_mismatch);
  1015. end
  1016. { support procvar=nil,procvar<>nil }
  1017. else if ((ld.deftype=procvardef) and (rt=niln)) or
  1018. ((rd.deftype=procvardef) and (lt=niln)) then
  1019. begin
  1020. if not(nodetype in [equaln,unequaln]) then
  1021. CGMessage(type_e_mismatch);
  1022. end
  1023. { support dynamicarray=nil,dynamicarray<>nil }
  1024. else if (is_dynamic_array(ld) and (rt=niln)) or
  1025. (is_dynamic_array(rd) and (lt=niln)) then
  1026. begin
  1027. if not(nodetype in [equaln,unequaln]) then
  1028. CGMessage(type_e_mismatch);
  1029. end
  1030. {$ifdef SUPPORT_MMX}
  1031. { mmx support, this must be before the zero based array
  1032. check }
  1033. else if (cs_mmx in aktlocalswitches) and
  1034. is_mmx_able_array(ld) and
  1035. is_mmx_able_array(rd) and
  1036. equal_defs(ld,rd) then
  1037. begin
  1038. case nodetype of
  1039. addn,subn,xorn,orn,andn:
  1040. ;
  1041. { mul is a little bit restricted }
  1042. muln:
  1043. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  1044. CGMessage(type_e_mismatch);
  1045. else
  1046. CGMessage(type_e_mismatch);
  1047. end;
  1048. end
  1049. {$endif SUPPORT_MMX}
  1050. { this is a little bit dangerous, also the left type }
  1051. { pointer to should be checked! This broke the mmx support }
  1052. else if (rd.deftype=pointerdef) or is_zero_based_array(rd) then
  1053. begin
  1054. if is_zero_based_array(rd) then
  1055. begin
  1056. resulttype.setdef(tpointerdef.create(tarraydef(rd).elementtype));
  1057. inserttypeconv(right,resulttype);
  1058. end;
  1059. inserttypeconv(left,sinttype);
  1060. if nodetype=addn then
  1061. begin
  1062. if not(cs_extsyntax in aktmoduleswitches) or
  1063. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  1064. CGMessage(type_e_mismatch);
  1065. if (rd.deftype=pointerdef) and
  1066. (tpointerdef(rd).pointertype.def.size>1) then
  1067. left:=caddnode.create(muln,left,
  1068. cordconstnode.create(tpointerdef(rd).pointertype.def.size,sinttype,true));
  1069. end
  1070. else
  1071. CGMessage(type_e_mismatch);
  1072. end
  1073. else if (ld.deftype=pointerdef) or is_zero_based_array(ld) then
  1074. begin
  1075. if is_zero_based_array(ld) then
  1076. begin
  1077. resulttype.setdef(tpointerdef.create(tarraydef(ld).elementtype));
  1078. inserttypeconv(left,resulttype);
  1079. end;
  1080. inserttypeconv(right,sinttype);
  1081. if nodetype in [addn,subn] then
  1082. begin
  1083. if not(cs_extsyntax in aktmoduleswitches) or
  1084. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  1085. CGMessage(type_e_mismatch);
  1086. if (ld.deftype=pointerdef) and
  1087. (tpointerdef(ld).pointertype.def.size>1) then
  1088. right:=caddnode.create(muln,right,
  1089. cordconstnode.create(tpointerdef(ld).pointertype.def.size,sinttype,true));
  1090. end
  1091. else
  1092. CGMessage(type_e_mismatch);
  1093. end
  1094. else if (rd.deftype=procvardef) and
  1095. (ld.deftype=procvardef) and
  1096. equal_defs(rd,ld) then
  1097. begin
  1098. if not (nodetype in [equaln,unequaln]) then
  1099. CGMessage(type_e_mismatch);
  1100. { convert both to voidpointer, because methodpointers are 8 bytes }
  1101. { even though only the first 4 bytes must be compared (JM) }
  1102. inserttypeconv_explicit(left,voidpointertype);
  1103. inserttypeconv_explicit(right,voidpointertype);
  1104. end
  1105. { enums }
  1106. else if (ld.deftype=enumdef) and (rd.deftype=enumdef) then
  1107. begin
  1108. if not(equal_defs(ld,rd)) then
  1109. inserttypeconv(right,left.resulttype);
  1110. if not(nodetype in [equaln,unequaln,ltn,lten,gtn,gten]) then
  1111. CGMessage(type_e_mismatch);
  1112. end
  1113. { generic conversion, this is for error recovery }
  1114. else
  1115. begin
  1116. inserttypeconv(left,sinttype);
  1117. inserttypeconv(right,sinttype);
  1118. end;
  1119. { set resulttype if not already done }
  1120. if not assigned(resulttype.def) then
  1121. begin
  1122. case nodetype of
  1123. ltn,lten,gtn,gten,equaln,unequaln :
  1124. resulttype:=booltype;
  1125. slashn :
  1126. resulttype:=resultrealtype;
  1127. addn:
  1128. begin
  1129. { for strings, return is always a 255 char string }
  1130. if is_shortstring(left.resulttype.def) then
  1131. resulttype:=cshortstringtype
  1132. else
  1133. resulttype:=left.resulttype;
  1134. end;
  1135. else
  1136. resulttype:=left.resulttype;
  1137. end;
  1138. end;
  1139. { when the result is currency we need some extra code for
  1140. multiplication and division. this should not be done when
  1141. the muln or slashn node is created internally }
  1142. if not(nf_is_currency in flags) and
  1143. is_currency(resulttype.def) then
  1144. begin
  1145. case nodetype of
  1146. slashn :
  1147. begin
  1148. { slashn will only work with floats }
  1149. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  1150. include(hp.flags,nf_is_currency);
  1151. result:=hp;
  1152. end;
  1153. muln :
  1154. begin
  1155. if s64currencytype.def.deftype=floatdef then
  1156. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype))
  1157. else
  1158. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  1159. include(hp.flags,nf_is_currency);
  1160. result:=hp
  1161. end;
  1162. end;
  1163. end;
  1164. end;
  1165. function taddnode.first_addstring: tnode;
  1166. var
  1167. p: tnode;
  1168. begin
  1169. { when we get here, we are sure that both the left and the right }
  1170. { node are both strings of the same stringtype (JM) }
  1171. case nodetype of
  1172. addn:
  1173. begin
  1174. { create the call to the concat routine both strings as arguments }
  1175. result := ccallnode.createintern('fpc_'+
  1176. tstringdef(resulttype.def).stringtypname+'_concat',
  1177. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1178. { we reused the arguments }
  1179. left := nil;
  1180. right := nil;
  1181. end;
  1182. ltn,lten,gtn,gten,equaln,unequaln :
  1183. begin
  1184. { generate better code for s='' and s<>'' }
  1185. if (nodetype in [equaln,unequaln]) and
  1186. (((left.nodetype=stringconstn) and (str_length(left)=0)) or
  1187. ((right.nodetype=stringconstn) and (str_length(right)=0))) then
  1188. begin
  1189. { switch so that the constant is always on the right }
  1190. if left.nodetype = stringconstn then
  1191. begin
  1192. p := left;
  1193. left := right;
  1194. right := p;
  1195. end;
  1196. if is_shortstring(left.resulttype.def) then
  1197. { compare the length with 0 }
  1198. result := caddnode.create(nodetype,
  1199. cinlinenode.create(in_length_x,false,left),
  1200. cordconstnode.create(0,s32inttype,false))
  1201. else
  1202. begin
  1203. { compare the pointer with nil (for ansistrings etc), }
  1204. { faster than getting the length (JM) }
  1205. result:= caddnode.create(nodetype,
  1206. ctypeconvnode.create_explicit(left,voidpointertype),
  1207. cpointerconstnode.create(0,voidpointertype));
  1208. end;
  1209. { left is reused }
  1210. left := nil;
  1211. { right isn't }
  1212. right.free;
  1213. right := nil;
  1214. exit;
  1215. end;
  1216. { no string constant -> call compare routine }
  1217. result := ccallnode.createintern('fpc_'+
  1218. tstringdef(left.resulttype.def).stringtypname+'_compare',
  1219. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1220. { and compare its result with 0 according to the original operator }
  1221. result := caddnode.create(nodetype,result,
  1222. cordconstnode.create(0,s32inttype,false));
  1223. left := nil;
  1224. right := nil;
  1225. end;
  1226. end;
  1227. end;
  1228. function taddnode.first_addset: tnode;
  1229. var
  1230. procname: string[31];
  1231. tempn: tnode;
  1232. paras: tcallparanode;
  1233. srsym: ttypesym;
  1234. begin
  1235. { get the sym that represents the fpc_normal_set type }
  1236. if not searchsystype('FPC_NORMAL_SET',srsym) then
  1237. internalerror(200108313);
  1238. case nodetype of
  1239. equaln,unequaln,lten,gten:
  1240. begin
  1241. case nodetype of
  1242. equaln,unequaln:
  1243. procname := 'fpc_set_comp_sets';
  1244. lten,gten:
  1245. begin
  1246. procname := 'fpc_set_contains_sets';
  1247. { (left >= right) = (right <= left) }
  1248. if nodetype = gten then
  1249. begin
  1250. tempn := left;
  1251. left := right;
  1252. right := tempn;
  1253. end;
  1254. end;
  1255. end;
  1256. { convert the arguments (explicitely) to fpc_normal_set's }
  1257. left := ctypeconvnode.create_explicit(left,srsym.restype);
  1258. right := ctypeconvnode.create_explicit(right,srsym.restype);
  1259. result := ccallnode.createintern(procname,ccallparanode.create(right,
  1260. ccallparanode.create(left,nil)));
  1261. { left and right are reused as parameters }
  1262. left := nil;
  1263. right := nil;
  1264. { for an unequaln, we have to negate the result of comp_sets }
  1265. if nodetype = unequaln then
  1266. result := cnotnode.create(result);
  1267. end;
  1268. addn:
  1269. begin
  1270. { optimize first loading of a set }
  1271. if (right.nodetype=setelementn) and
  1272. not(assigned(tsetelementnode(right).right)) and
  1273. is_emptyset(left) then
  1274. begin
  1275. { type cast the value to pass as argument to a byte, }
  1276. { since that's what the helper expects }
  1277. tsetelementnode(right).left :=
  1278. ctypeconvnode.create_explicit(tsetelementnode(right).left,u8inttype);
  1279. { set the resulttype to the actual one (otherwise it's }
  1280. { "fpc_normal_set") }
  1281. result := ccallnode.createinternres('fpc_set_create_element',
  1282. ccallparanode.create(tsetelementnode(right).left,nil),
  1283. resulttype);
  1284. { reused }
  1285. tsetelementnode(right).left := nil;
  1286. end
  1287. else
  1288. begin
  1289. if right.nodetype=setelementn then
  1290. begin
  1291. { convert the arguments to bytes, since that's what }
  1292. { the helper expects }
  1293. tsetelementnode(right).left :=
  1294. ctypeconvnode.create_explicit(tsetelementnode(right).left,
  1295. u8inttype);
  1296. { convert the original set (explicitely) to an }
  1297. { fpc_normal_set so we can pass it to the helper }
  1298. left := ctypeconvnode.create_explicit(left,srsym.restype);
  1299. { add a range or a single element? }
  1300. if assigned(tsetelementnode(right).right) then
  1301. begin
  1302. tsetelementnode(right).right :=
  1303. ctypeconvnode.create_explicit(tsetelementnode(right).right,
  1304. u8inttype);
  1305. { create the call }
  1306. result := ccallnode.createinternres('fpc_set_set_range',
  1307. ccallparanode.create(tsetelementnode(right).right,
  1308. ccallparanode.create(tsetelementnode(right).left,
  1309. ccallparanode.create(left,nil))),resulttype);
  1310. end
  1311. else
  1312. begin
  1313. result := ccallnode.createinternres('fpc_set_set_byte',
  1314. ccallparanode.create(tsetelementnode(right).left,
  1315. ccallparanode.create(left,nil)),resulttype);
  1316. end;
  1317. { remove reused parts from original node }
  1318. tsetelementnode(right).right := nil;
  1319. tsetelementnode(right).left := nil;
  1320. left := nil;
  1321. end
  1322. else
  1323. begin
  1324. { add two sets }
  1325. { convert the sets to fpc_normal_set's }
  1326. result := ccallnode.createinternres('fpc_set_add_sets',
  1327. ccallparanode.create(
  1328. ctypeconvnode.create_explicit(right,srsym.restype),
  1329. ccallparanode.create(
  1330. ctypeconvnode.create_explicit(left,srsym.restype),nil)),resulttype);
  1331. { remove reused parts from original node }
  1332. left := nil;
  1333. right := nil;
  1334. end;
  1335. end
  1336. end;
  1337. subn,symdifn,muln:
  1338. begin
  1339. { convert the sets to fpc_normal_set's }
  1340. paras := ccallparanode.create(ctypeconvnode.create_explicit(right,srsym.restype),
  1341. ccallparanode.create(ctypeconvnode.create_explicit(left,srsym.restype),nil));
  1342. case nodetype of
  1343. subn:
  1344. result := ccallnode.createinternres('fpc_set_sub_sets',
  1345. paras,resulttype);
  1346. symdifn:
  1347. result := ccallnode.createinternres('fpc_set_symdif_sets',
  1348. paras,resulttype);
  1349. muln:
  1350. result := ccallnode.createinternres('fpc_set_mul_sets',
  1351. paras,resulttype);
  1352. end;
  1353. { remove reused parts from original node }
  1354. left := nil;
  1355. right := nil;
  1356. end;
  1357. else
  1358. internalerror(200108311);
  1359. end;
  1360. end;
  1361. function taddnode.first_add64bitint: tnode;
  1362. var
  1363. procname: string[31];
  1364. temp: tnode;
  1365. power: longint;
  1366. begin
  1367. result := nil;
  1368. { create helper calls mul }
  1369. if nodetype <> muln then
  1370. exit;
  1371. { make sure that if there is a constant, that it's on the right }
  1372. if left.nodetype = ordconstn then
  1373. begin
  1374. temp := right;
  1375. right := left;
  1376. left := temp;
  1377. end;
  1378. { can we use a shift instead of a mul? }
  1379. if not (cs_check_overflow in aktlocalswitches) and
  1380. (right.nodetype = ordconstn) and
  1381. ispowerof2(tordconstnode(right).value,power) then
  1382. begin
  1383. tordconstnode(right).value := power;
  1384. result := cshlshrnode.create(shln,left,right);
  1385. { left and right are reused }
  1386. left := nil;
  1387. right := nil;
  1388. { return firstpassed new node }
  1389. exit;
  1390. end;
  1391. { when currency is used set the result of the
  1392. parameters to s64bit, so they are not converted }
  1393. if is_currency(resulttype.def) then
  1394. begin
  1395. left.resulttype:=s64inttype;
  1396. right.resulttype:=s64inttype;
  1397. end;
  1398. { otherwise, create the parameters for the helper }
  1399. right := ccallparanode.create(
  1400. cordconstnode.create(ord(cs_check_overflow in aktlocalswitches),booltype,true),
  1401. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1402. left := nil;
  1403. { only qword needs the unsigned code, the
  1404. signed code is also used for currency }
  1405. if is_signed(resulttype.def) then
  1406. procname := 'fpc_mul_int64'
  1407. else
  1408. procname := 'fpc_mul_qword';
  1409. result := ccallnode.createintern(procname,right);
  1410. right := nil;
  1411. end;
  1412. {$ifdef cpufpemu}
  1413. function taddnode.first_addfloat: tnode;
  1414. var
  1415. procname: string[31];
  1416. temp: tnode;
  1417. power: longint;
  1418. { do we need to reverse the result ? }
  1419. notnode : boolean;
  1420. begin
  1421. result := nil;
  1422. notnode := false;
  1423. { In non-emulation mode, real opcodes are
  1424. emitted for floating point values.
  1425. }
  1426. if not (cs_fp_emulation in aktmoduleswitches) then
  1427. exit;
  1428. case nodetype of
  1429. addn : procname := 'fpc_single_add';
  1430. muln : procname := 'fpc_single_mul';
  1431. subn : procname := 'fpc_single_sub';
  1432. slashn : procname := 'fpc_single_div';
  1433. ltn : procname := 'fpc_single_lt';
  1434. lten: procname := 'fpc_single_le';
  1435. gtn:
  1436. begin
  1437. procname := 'fpc_single_le';
  1438. notnode := true;
  1439. end;
  1440. gten:
  1441. begin
  1442. procname := 'fpc_single_lt';
  1443. notnode := true;
  1444. end;
  1445. equaln: procname := 'fpc_single_eq';
  1446. unequaln :
  1447. begin
  1448. procname := 'fpc_single_eq';
  1449. notnode := true;
  1450. end;
  1451. else
  1452. CGMessage(type_e_mismatch);
  1453. end;
  1454. { convert the arguments (explicitely) to fpc_normal_set's }
  1455. result := ccallnode.createintern(procname,ccallparanode.create(right,
  1456. ccallparanode.create(left,nil)));
  1457. left:=nil;
  1458. right:=nil;
  1459. { do we need to reverse the result }
  1460. if notnode then
  1461. result := cnotnode.create(result);
  1462. end;
  1463. {$endif cpufpemu}
  1464. function taddnode.pass_1 : tnode;
  1465. var
  1466. {$ifdef addstringopt}
  1467. hp : tnode;
  1468. {$endif addstringopt}
  1469. lt,rt : tnodetype;
  1470. rd,ld : tdef;
  1471. begin
  1472. result:=nil;
  1473. { first do the two subtrees }
  1474. firstpass(left);
  1475. firstpass(right);
  1476. if codegenerror then
  1477. exit;
  1478. { load easier access variables }
  1479. rd:=right.resulttype.def;
  1480. ld:=left.resulttype.def;
  1481. rt:=right.nodetype;
  1482. lt:=left.nodetype;
  1483. { int/int gives real/real! }
  1484. if nodetype=slashn then
  1485. begin
  1486. {$ifdef cpufpemu}
  1487. result := first_addfloat;
  1488. if assigned(result) then
  1489. exit;
  1490. {$endif cpufpemu}
  1491. expectloc:=LOC_FPUREGISTER;
  1492. { maybe we need an integer register to save }
  1493. { a reference }
  1494. if ((left.expectloc<>LOC_FPUREGISTER) or
  1495. (right.expectloc<>LOC_FPUREGISTER)) and
  1496. (left.registersint=right.registersint) then
  1497. calcregisters(self,1,1,0)
  1498. else
  1499. calcregisters(self,0,1,0);
  1500. { an add node always first loads both the left and the }
  1501. { right in the fpu before doing the calculation. However, }
  1502. { calcregisters(0,2,0) will overestimate the number of }
  1503. { necessary registers (it will make it 3 in case one of }
  1504. { the operands is already in the fpu) (JM) }
  1505. if ((left.expectloc<>LOC_FPUREGISTER) or
  1506. (right.expectloc<>LOC_FPUREGISTER)) and
  1507. (registersfpu < 2) then
  1508. inc(registersfpu);
  1509. end
  1510. { if both are orddefs then check sub types }
  1511. else if (ld.deftype=orddef) and (rd.deftype=orddef) then
  1512. begin
  1513. { 2 booleans ? }
  1514. if is_boolean(ld) and is_boolean(rd) then
  1515. begin
  1516. if not(cs_full_boolean_eval in aktlocalswitches) and
  1517. (nodetype in [andn,orn]) then
  1518. begin
  1519. expectloc:=LOC_JUMP;
  1520. calcregisters(self,0,0,0);
  1521. end
  1522. else
  1523. begin
  1524. expectloc:=LOC_FLAGS;
  1525. if (left.expectloc in [LOC_JUMP,LOC_FLAGS]) and
  1526. (left.expectloc in [LOC_JUMP,LOC_FLAGS]) then
  1527. calcregisters(self,2,0,0)
  1528. else
  1529. calcregisters(self,1,0,0);
  1530. end;
  1531. end
  1532. else
  1533. { Both are chars? only convert to shortstrings for addn }
  1534. if is_char(ld) then
  1535. begin
  1536. if nodetype=addn then
  1537. internalerror(200103291);
  1538. expectloc:=LOC_FLAGS;
  1539. calcregisters(self,1,0,0);
  1540. end
  1541. {$ifndef cpu64bit}
  1542. { is there a 64 bit type ? }
  1543. else if (torddef(ld).typ in [s64bit,u64bit,scurrency]) then
  1544. begin
  1545. result := first_add64bitint;
  1546. if assigned(result) then
  1547. exit;
  1548. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1549. expectloc:=LOC_REGISTER
  1550. else
  1551. expectloc:=LOC_JUMP;
  1552. calcregisters(self,2,0,0)
  1553. end
  1554. {$endif cpu64bit}
  1555. { is there a cardinal? }
  1556. else if (torddef(ld).typ=u32bit) then
  1557. begin
  1558. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1559. expectloc:=LOC_REGISTER
  1560. else
  1561. expectloc:=LOC_FLAGS;
  1562. calcregisters(self,1,0,0);
  1563. { for unsigned mul we need an extra register }
  1564. if nodetype=muln then
  1565. inc(registersint);
  1566. end
  1567. { generic s32bit conversion }
  1568. else
  1569. begin
  1570. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1571. expectloc:=LOC_REGISTER
  1572. else
  1573. expectloc:=LOC_FLAGS;
  1574. calcregisters(self,1,0,0);
  1575. end;
  1576. end
  1577. { left side a setdef, must be before string processing,
  1578. else array constructor can be seen as array of char (PFV) }
  1579. else if (ld.deftype=setdef) then
  1580. begin
  1581. if tsetdef(ld).settype=smallset then
  1582. begin
  1583. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  1584. expectloc:=LOC_FLAGS
  1585. else
  1586. expectloc:=LOC_REGISTER;
  1587. { are we adding set elements ? }
  1588. if right.nodetype=setelementn then
  1589. calcregisters(self,2,0,0)
  1590. else
  1591. calcregisters(self,1,0,0);
  1592. end
  1593. else
  1594. {$ifdef MMXSET}
  1595. {$ifdef i386}
  1596. if cs_mmx in aktlocalswitches then
  1597. begin
  1598. expectloc:=LOC_MMXREGISTER;
  1599. calcregisters(self,0,0,4);
  1600. end
  1601. else
  1602. {$endif}
  1603. {$endif MMXSET}
  1604. begin
  1605. result := first_addset;
  1606. if assigned(result) then
  1607. exit;
  1608. expectloc:=LOC_CREFERENCE;
  1609. calcregisters(self,0,0,0);
  1610. { here we call SET... }
  1611. include(current_procinfo.flags,pi_do_call);
  1612. end;
  1613. end
  1614. { compare pchar by addresses like BP/Delphi }
  1615. else if is_pchar(ld) then
  1616. begin
  1617. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1618. expectloc:=LOC_REGISTER
  1619. else
  1620. expectloc:=LOC_FLAGS;
  1621. calcregisters(self,1,0,0);
  1622. end
  1623. { is one of the operands a string }
  1624. else if (ld.deftype=stringdef) then
  1625. begin
  1626. if is_widestring(ld) then
  1627. begin
  1628. { this is only for add, the comparisaion is handled later }
  1629. expectloc:=LOC_REGISTER;
  1630. end
  1631. else if is_ansistring(ld) then
  1632. begin
  1633. { this is only for add, the comparisaion is handled later }
  1634. expectloc:=LOC_REGISTER;
  1635. end
  1636. else if is_longstring(ld) then
  1637. begin
  1638. { this is only for add, the comparisaion is handled later }
  1639. expectloc:=LOC_REFERENCE;
  1640. end
  1641. else
  1642. begin
  1643. {$ifdef addstringopt}
  1644. { can create a call which isn't handled by callparatemp }
  1645. if canbeaddsstringcharoptnode(self) then
  1646. begin
  1647. hp := genaddsstringcharoptnode(self);
  1648. pass_1 := hp;
  1649. exit;
  1650. end
  1651. else
  1652. {$endif addstringopt}
  1653. begin
  1654. { Fix right to be shortstring }
  1655. if is_char(right.resulttype.def) then
  1656. begin
  1657. inserttypeconv(right,cshortstringtype);
  1658. firstpass(right);
  1659. end;
  1660. end;
  1661. {$ifdef addstringopt}
  1662. { can create a call which isn't handled by callparatemp }
  1663. if canbeaddsstringcsstringoptnode(self) then
  1664. begin
  1665. hp := genaddsstringcsstringoptnode(self);
  1666. pass_1 := hp;
  1667. exit;
  1668. end;
  1669. {$endif addstringopt}
  1670. end;
  1671. { otherwise, let addstring convert everything }
  1672. result := first_addstring;
  1673. exit;
  1674. end
  1675. { is one a real float ? }
  1676. else if (rd.deftype=floatdef) or (ld.deftype=floatdef) then
  1677. begin
  1678. {$ifdef cpufpemu}
  1679. result := first_addfloat;
  1680. if assigned(result) then
  1681. exit;
  1682. {$endif cpufpemu}
  1683. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1684. expectloc:=LOC_FPUREGISTER
  1685. else
  1686. expectloc:=LOC_FLAGS;
  1687. calcregisters(self,0,1,0);
  1688. { an add node always first loads both the left and the }
  1689. { right in the fpu before doing the calculation. However, }
  1690. { calcregisters(0,2,0) will overestimate the number of }
  1691. { necessary registers (it will make it 3 in case one of }
  1692. { the operands is already in the fpu) (JM) }
  1693. if ((left.expectloc<>LOC_FPUREGISTER) or
  1694. (right.expectloc<>LOC_FPUREGISTER)) and
  1695. (registersfpu < 2) then
  1696. inc(registersfpu);
  1697. end
  1698. { pointer comperation and subtraction }
  1699. else if (ld.deftype=pointerdef) then
  1700. begin
  1701. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1702. expectloc:=LOC_REGISTER
  1703. else
  1704. expectloc:=LOC_FLAGS;
  1705. calcregisters(self,1,0,0);
  1706. end
  1707. else if is_class_or_interface(ld) then
  1708. begin
  1709. expectloc:=LOC_FLAGS;
  1710. calcregisters(self,1,0,0);
  1711. end
  1712. else if (ld.deftype=classrefdef) then
  1713. begin
  1714. expectloc:=LOC_FLAGS;
  1715. calcregisters(self,1,0,0);
  1716. end
  1717. { support procvar=nil,procvar<>nil }
  1718. else if ((ld.deftype=procvardef) and (rt=niln)) or
  1719. ((rd.deftype=procvardef) and (lt=niln)) then
  1720. begin
  1721. expectloc:=LOC_FLAGS;
  1722. calcregisters(self,1,0,0);
  1723. end
  1724. {$ifdef SUPPORT_MMX}
  1725. { mmx support, this must be before the zero based array
  1726. check }
  1727. else if (cs_mmx in aktlocalswitches) and is_mmx_able_array(ld) and
  1728. is_mmx_able_array(rd) then
  1729. begin
  1730. expectloc:=LOC_MMXREGISTER;
  1731. calcregisters(self,0,0,1);
  1732. end
  1733. {$endif SUPPORT_MMX}
  1734. else if (rd.deftype=pointerdef) or (ld.deftype=pointerdef) then
  1735. begin
  1736. expectloc:=LOC_REGISTER;
  1737. calcregisters(self,1,0,0);
  1738. end
  1739. else if (rd.deftype=procvardef) and
  1740. (ld.deftype=procvardef) and
  1741. equal_defs(rd,ld) then
  1742. begin
  1743. expectloc:=LOC_FLAGS;
  1744. calcregisters(self,1,0,0);
  1745. end
  1746. else if (ld.deftype=enumdef) then
  1747. begin
  1748. expectloc:=LOC_FLAGS;
  1749. calcregisters(self,1,0,0);
  1750. end
  1751. {$ifdef SUPPORT_MMX}
  1752. else if (cs_mmx in aktlocalswitches) and
  1753. is_mmx_able_array(ld) and
  1754. is_mmx_able_array(rd) then
  1755. begin
  1756. expectloc:=LOC_MMXREGISTER;
  1757. calcregisters(self,0,0,1);
  1758. end
  1759. {$endif SUPPORT_MMX}
  1760. { the general solution is to convert to 32 bit int }
  1761. else
  1762. begin
  1763. expectloc:=LOC_REGISTER;
  1764. calcregisters(self,1,0,0);
  1765. end;
  1766. end;
  1767. {$ifdef state_tracking}
  1768. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  1769. var factval:Tnode;
  1770. begin
  1771. track_state_pass:=false;
  1772. if left.track_state_pass(exec_known) then
  1773. begin
  1774. track_state_pass:=true;
  1775. left.resulttype.def:=nil;
  1776. do_resulttypepass(left);
  1777. end;
  1778. factval:=aktstate.find_fact(left);
  1779. if factval<>nil then
  1780. begin
  1781. track_state_pass:=true;
  1782. left.destroy;
  1783. left:=factval.getcopy;
  1784. end;
  1785. if right.track_state_pass(exec_known) then
  1786. begin
  1787. track_state_pass:=true;
  1788. right.resulttype.def:=nil;
  1789. do_resulttypepass(right);
  1790. end;
  1791. factval:=aktstate.find_fact(right);
  1792. if factval<>nil then
  1793. begin
  1794. track_state_pass:=true;
  1795. right.destroy;
  1796. right:=factval.getcopy;
  1797. end;
  1798. end;
  1799. {$endif}
  1800. begin
  1801. caddnode:=taddnode;
  1802. end.
  1803. {
  1804. $Log$
  1805. Revision 1.112 2004-02-21 22:08:46 daniel
  1806. * Avoid memory allocation and string copying
  1807. Revision 1.111 2004/02/20 21:55:59 peter
  1808. * procvar cleanup
  1809. Revision 1.110 2004/02/05 01:24:08 florian
  1810. * several fixes to compile x86-64 system
  1811. Revision 1.109 2004/02/03 22:32:54 peter
  1812. * renamed xNNbittype to xNNinttype
  1813. * renamed registers32 to registersint
  1814. * replace some s32bit,u32bit with torddef([su]inttype).def.typ
  1815. Revision 1.108 2004/02/02 20:41:59 florian
  1816. + added prefetch(const mem) support
  1817. Revision 1.107 2004/01/20 12:59:36 florian
  1818. * common addnode code for x86-64 and i386
  1819. Revision 1.106 2004/01/14 17:19:04 peter
  1820. * disable addmmxset
  1821. Revision 1.105 2004/01/02 17:19:04 jonas
  1822. * if currency = int64, FPC_CURRENCY_IS_INT64 is defined
  1823. + round and trunc for currency and comp if FPC_CURRENCY_IS_INT64 is
  1824. defined
  1825. * if currency = orddef, prefer currency -> int64/qword conversion over
  1826. currency -> float conversions
  1827. * optimized currency/currency if currency = orddef
  1828. * TODO: write FPC_DIV_CURRENCY and FPC_MUL_CURRENCY routines to prevent
  1829. precision loss if currency=int64 and bestreal = double
  1830. Revision 1.104 2003/12/31 20:47:02 jonas
  1831. * properly fixed assigned() mess (by handling it separately in ncginl)
  1832. -> all assigned()-related tests in the test suite work again
  1833. Revision 1.103 2003/12/30 16:30:50 jonas
  1834. * fixed previous commit for tp and delphi modes
  1835. Revision 1.102 2003/12/29 22:33:08 jonas
  1836. * fixed methodpointer comparing in a generic way (typecast both left and
  1837. right explicitly to voidpointer), instead of relying on strange
  1838. behvaiour or n386addnode.pass_2 (if size of def = 8, only use the first
  1839. 4 bytes instead of internalerror-ing or so)
  1840. Revision 1.101 2003/12/21 11:28:41 daniel
  1841. * Some work to allow mmx instructions to be used for 32 byte sets
  1842. Revision 1.100 2003/12/09 21:17:04 jonas
  1843. + support for evaluating qword constant expressions (both arguments have
  1844. to be a qword, constants have to be explicitly typecasted to qword)
  1845. Revision 1.99 2003/10/28 15:35:18 peter
  1846. * compare longint-cardinal also makes types wider
  1847. Revision 1.98 2003/10/21 18:16:13 peter
  1848. * IncompatibleTypes() added that will include unit names when
  1849. the typenames are the same
  1850. Revision 1.97 2003/10/08 19:19:45 peter
  1851. * set_varstate cleanup
  1852. Revision 1.96 2003/10/01 20:34:48 peter
  1853. * procinfo unit contains tprocinfo
  1854. * cginfo renamed to cgbase
  1855. * moved cgmessage to verbose
  1856. * fixed ppc and sparc compiles
  1857. Revision 1.95 2003/09/06 16:47:24 florian
  1858. + support of NaN and Inf in the compiler as values of real constants
  1859. Revision 1.94 2003/09/03 15:55:00 peter
  1860. * NEWRA branch merged
  1861. Revision 1.93.2.1 2003/08/31 21:07:44 daniel
  1862. * callparatemp ripped
  1863. Revision 1.93 2003/06/05 20:05:55 peter
  1864. * removed changesettype because that will change the definition
  1865. of the setdef forever and can result in a different between
  1866. original interface and current implementation definition
  1867. Revision 1.92 2003/06/03 21:04:43 peter
  1868. * widen cardinal+signed operations
  1869. Revision 1.91 2003/05/26 21:15:18 peter
  1870. * disable string node optimizations for the moment
  1871. Revision 1.90 2003/05/26 19:38:28 peter
  1872. * generic fpc_shorstr_concat
  1873. + fpc_shortstr_append_shortstr optimization
  1874. Revision 1.89 2003/05/24 21:12:57 florian
  1875. * if something doesn't work with callparatemp, the define callparatemp
  1876. should be used because other processors with reigster calling conventions
  1877. depend on this as well
  1878. Revision 1.88 2003/05/23 22:57:38 jonas
  1879. - disable addoptnodes for powerpc, because they can generate calls in
  1880. pass_2, so -dcallparatemp can't detect them as nested calls
  1881. Revision 1.87 2003/04/27 11:21:32 peter
  1882. * aktprocdef renamed to current_procdef
  1883. * procinfo renamed to current_procinfo
  1884. * procinfo will now be stored in current_module so it can be
  1885. cleaned up properly
  1886. * gen_main_procsym changed to create_main_proc and release_main_proc
  1887. to also generate a tprocinfo structure
  1888. * fixed unit implicit initfinal
  1889. Revision 1.86 2003/04/26 09:12:55 peter
  1890. * add string returns in LOC_REFERENCE
  1891. Revision 1.85 2003/04/24 22:29:57 florian
  1892. * fixed a lot of PowerPC related stuff
  1893. Revision 1.84 2003/04/23 20:16:04 peter
  1894. + added currency support based on int64
  1895. + is_64bit for use in cg units instead of is_64bitint
  1896. * removed cgmessage from n386add, replace with internalerrors
  1897. Revision 1.83 2003/04/23 10:10:07 peter
  1898. * expectloc fixes
  1899. Revision 1.82 2003/04/22 23:50:22 peter
  1900. * firstpass uses expectloc
  1901. * checks if there are differences between the expectloc and
  1902. location.loc from secondpass in EXTDEBUG
  1903. Revision 1.81 2003/02/15 22:20:14 carl
  1904. * bugfix for generic calls to FPU emulation code
  1905. Revision 1.80 2003/02/12 22:10:07 carl
  1906. * load_frame_pointer is now generic
  1907. * change fpu emulation routine names
  1908. Revision 1.79 2003/01/02 22:19:54 peter
  1909. * support pchar-char operations converting to string first
  1910. * support chararray-nil
  1911. Revision 1.78 2002/12/11 22:41:03 peter
  1912. * stop processing assignment node when the binaryoverload generates
  1913. a codegenerror
  1914. Revision 1.77 2002/12/06 16:56:57 peter
  1915. * only compile cs_fp_emulation support when cpufpuemu is defined
  1916. * define cpufpuemu for m68k only
  1917. Revision 1.76 2002/11/30 21:32:24 carl
  1918. + Add loading of softfpu in emulation mode
  1919. + Correct routine call for softfpu
  1920. * Extended type must also be defined even with softfpu
  1921. Revision 1.75 2002/11/27 13:11:38 peter
  1922. * more currency fixes, taddcurr runs now successfull
  1923. Revision 1.74 2002/11/27 11:28:40 peter
  1924. * when both flaottypes are the same then handle the addnode using
  1925. that floattype instead of bestrealtype
  1926. Revision 1.73 2002/11/25 18:43:32 carl
  1927. - removed the invalid if <> checking (Delphi is strange on this)
  1928. + implemented abstract warning on instance creation of class with
  1929. abstract methods.
  1930. * some error message cleanups
  1931. Revision 1.72 2002/11/25 17:43:17 peter
  1932. * splitted defbase in defutil,symutil,defcmp
  1933. * merged isconvertable and is_equal into compare_defs(_ext)
  1934. * made operator search faster by walking the list only once
  1935. Revision 1.71 2002/11/23 22:50:06 carl
  1936. * some small speed optimizations
  1937. + added several new warnings/hints
  1938. Revision 1.70 2002/11/16 14:20:22 peter
  1939. * fix tbs0417
  1940. Revision 1.69 2002/11/15 01:58:50 peter
  1941. * merged changes from 1.0.7 up to 04-11
  1942. - -V option for generating bug report tracing
  1943. - more tracing for option parsing
  1944. - errors for cdecl and high()
  1945. - win32 import stabs
  1946. - win32 records<=8 are returned in eax:edx (turned off by default)
  1947. - heaptrc update
  1948. - more info for temp management in .s file with EXTDEBUG
  1949. Revision 1.68 2002/10/08 16:50:43 jonas
  1950. * fixed web bug 2136
  1951. Revision 1.67 2002/10/05 00:47:03 peter
  1952. * support dynamicarray<>nil
  1953. Revision 1.66 2002/10/04 21:19:28 jonas
  1954. * fixed web bug 2139: checking for division by zero fixed
  1955. Revision 1.65 2002/09/07 15:25:02 peter
  1956. * old logs removed and tabs fixed
  1957. Revision 1.64 2002/09/07 12:16:05 carl
  1958. * second part bug report 1996 fix, testrange in cordconstnode
  1959. only called if option is set (also make parsing a tiny faster)
  1960. Revision 1.63 2002/09/04 19:32:56 jonas
  1961. * changed some ctypeconvnode/toggleflag(nf_explizit) combo's to
  1962. ctypeconvnode.create_explicit() statements
  1963. Revision 1.62 2002/08/17 09:23:34 florian
  1964. * first part of current_procinfo rewrite
  1965. Revision 1.61 2002/08/15 15:15:55 carl
  1966. * jmpbuf size allocation for exceptions is now cpu specific (as it should)
  1967. * more generic nodes for maths
  1968. * several fixes for better m68k support
  1969. Revision 1.60 2002/08/12 15:08:39 carl
  1970. + stab register indexes for powerpc (moved from gdb to cpubase)
  1971. + tprocessor enumeration moved to cpuinfo
  1972. + linker in target_info is now a class
  1973. * many many updates for m68k (will soon start to compile)
  1974. - removed some ifdef or correct them for correct cpu
  1975. Revision 1.59 2002/08/02 07:44:30 jonas
  1976. * made assigned() handling generic
  1977. * add nodes now can also evaluate constant expressions at compile time
  1978. that contain nil nodes
  1979. Revision 1.58 2002/07/26 11:17:52 jonas
  1980. * the optimization of converting a multiplication with a power of two to
  1981. a shl is moved from n386add/secondpass to nadd/resulttypepass
  1982. Revision 1.57 2002/07/23 13:08:16 jonas
  1983. * fixed constant set evaluation of new set handling for non-commutative
  1984. operators
  1985. Revision 1.56 2002/07/23 12:34:29 daniel
  1986. * Readded old set code. To use it define 'oldset'. Activated by default
  1987. for ppc.
  1988. Revision 1.55 2002/07/22 11:48:04 daniel
  1989. * Sets are now internally sets.
  1990. Revision 1.54 2002/07/20 11:57:53 florian
  1991. * types.pas renamed to defbase.pas because D6 contains a types
  1992. unit so this would conflicts if D6 programms are compiled
  1993. + Willamette/SSE2 instructions to assembler added
  1994. Revision 1.53 2002/07/19 11:41:34 daniel
  1995. * State tracker work
  1996. * The whilen and repeatn are now completely unified into whilerepeatn. This
  1997. allows the state tracker to change while nodes automatically into
  1998. repeat nodes.
  1999. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  2000. 'not(a>b)' is optimized into 'a<=b'.
  2001. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  2002. by removing the notn and later switchting the true and falselabels. The
  2003. same is done with 'repeat until not a'.
  2004. Revision 1.52 2002/07/14 18:00:43 daniel
  2005. + Added the beginning of a state tracker. This will track the values of
  2006. variables through procedures and optimize things away.
  2007. Revision 1.51 2002/05/18 13:34:08 peter
  2008. * readded missing revisions
  2009. Revision 1.50 2002/05/16 19:46:37 carl
  2010. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2011. + try to fix temp allocation (still in ifdef)
  2012. + generic constructor calls
  2013. + start of tassembler / tmodulebase class cleanup
  2014. Revision 1.48 2002/05/13 19:54:36 peter
  2015. * removed n386ld and n386util units
  2016. * maybe_save/maybe_restore added instead of the old maybe_push
  2017. Revision 1.47 2002/05/12 16:53:06 peter
  2018. * moved entry and exitcode to ncgutil and cgobj
  2019. * foreach gets extra argument for passing local data to the
  2020. iterator function
  2021. * -CR checks also class typecasts at runtime by changing them
  2022. into as
  2023. * fixed compiler to cycle with the -CR option
  2024. * fixed stabs with elf writer, finally the global variables can
  2025. be watched
  2026. * removed a lot of routines from cga unit and replaced them by
  2027. calls to cgobj
  2028. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  2029. u32bit then the other is typecasted also to u32bit without giving
  2030. a rangecheck warning/error.
  2031. * fixed pascal calling method with reversing also the high tree in
  2032. the parast, detected by tcalcst3 test
  2033. Revision 1.46 2002/04/23 19:16:34 peter
  2034. * add pinline unit that inserts compiler supported functions using
  2035. one or more statements
  2036. * moved finalize and setlength from ninl to pinline
  2037. Revision 1.45 2002/04/04 19:05:56 peter
  2038. * removed unused units
  2039. * use tlocation.size in cg.a_*loc*() routines
  2040. Revision 1.44 2002/04/02 17:11:28 peter
  2041. * tlocation,treference update
  2042. * LOC_CONSTANT added for better constant handling
  2043. * secondadd splitted in multiple routines
  2044. * location_force_reg added for loading a location to a register
  2045. of a specified size
  2046. * secondassignment parses now first the right and then the left node
  2047. (this is compatible with Kylix). This saves a lot of push/pop especially
  2048. with string operations
  2049. * adapted some routines to use the new cg methods
  2050. }