tcadd.pas 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Type checking and register allocation for add node
  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 tcadd;
  19. interface
  20. uses
  21. tree;
  22. procedure firstadd(var p : ptree);
  23. function isbinaryoverloaded(var p : ptree) : boolean;
  24. implementation
  25. uses
  26. globtype,systems,tokens,
  27. cobjects,verbose,globals,
  28. symconst,symtable,aasm,types,
  29. {$ifdef newcg}
  30. cgbase,
  31. {$else newcg}
  32. hcodegen,
  33. {$endif newcg}
  34. htypechk,pass_1,
  35. cpubase,tccnv
  36. ;
  37. function isbinaryoverloaded(var p : ptree) : boolean;
  38. var
  39. rd,ld : pdef;
  40. t : ptree;
  41. optoken : ttoken;
  42. begin
  43. isbinaryoverloaded:=false;
  44. { overloaded operator ? }
  45. { load easier access variables }
  46. rd:=p^.right^.resulttype;
  47. ld:=p^.left^.resulttype;
  48. if isbinaryoperatoroverloadable(ld,rd,voiddef,p^.treetype) then
  49. begin
  50. isbinaryoverloaded:=true;
  51. {!!!!!!!!! handle paras }
  52. case p^.treetype of
  53. { the nil as symtable signs firstcalln that this is
  54. an overloaded operator }
  55. addn:
  56. optoken:=_PLUS;
  57. subn:
  58. optoken:=_MINUS;
  59. muln:
  60. optoken:=_STAR;
  61. starstarn:
  62. optoken:=_STARSTAR;
  63. slashn:
  64. optoken:=_SLASH;
  65. ltn:
  66. optoken:=tokens._lt;
  67. gtn:
  68. optoken:=tokens._gt;
  69. lten:
  70. optoken:=_lte;
  71. gten:
  72. optoken:=_gte;
  73. equaln,unequaln :
  74. optoken:=_EQUAL;
  75. symdifn :
  76. optoken:=_SYMDIF;
  77. modn :
  78. optoken:=_OP_MOD;
  79. orn :
  80. optoken:=_OP_OR;
  81. xorn :
  82. optoken:=_OP_XOR;
  83. andn :
  84. optoken:=_OP_AND;
  85. divn :
  86. optoken:=_OP_DIV;
  87. shln :
  88. optoken:=_OP_SHL;
  89. shrn :
  90. optoken:=_OP_SHR;
  91. else
  92. exit;
  93. end;
  94. t:=gencallnode(overloaded_operators[optoken],nil);
  95. { we have to convert p^.left and p^.right into
  96. callparanodes }
  97. if t^.symtableprocentry=nil then
  98. begin
  99. CGMessage(parser_e_operator_not_overloaded);
  100. putnode(t);
  101. end
  102. else
  103. begin
  104. inc(t^.symtableprocentry^.refs);
  105. t^.left:=gencallparanode(p^.left,nil);
  106. t^.left:=gencallparanode(p^.right,t^.left);
  107. if p^.treetype=unequaln then
  108. t:=gensinglenode(notn,t);
  109. firstpass(t);
  110. putnode(p);
  111. p:=t;
  112. end;
  113. end;
  114. end;
  115. {*****************************************************************************
  116. FirstAdd
  117. *****************************************************************************}
  118. {$ifdef fpc}
  119. {$maxfpuregisters 0}
  120. {$endif fpc}
  121. procedure firstadd(var p : ptree);
  122. procedure make_bool_equal_size(var p:ptree);
  123. begin
  124. if porddef(p^.left^.resulttype)^.typ>porddef(p^.right^.resulttype)^.typ then
  125. begin
  126. p^.right:=gentypeconvnode(p^.right,porddef(p^.left^.resulttype));
  127. p^.right^.convtyp:=tc_bool_2_int;
  128. p^.right^.explizit:=true;
  129. firstpass(p^.right);
  130. end
  131. else
  132. if porddef(p^.left^.resulttype)^.typ<porddef(p^.right^.resulttype)^.typ then
  133. begin
  134. p^.left:=gentypeconvnode(p^.left,porddef(p^.right^.resulttype));
  135. p^.left^.convtyp:=tc_bool_2_int;
  136. p^.left^.explizit:=true;
  137. firstpass(p^.left);
  138. end;
  139. end;
  140. var
  141. t,hp : ptree;
  142. ot,
  143. lt,rt : ttreetyp;
  144. rv,lv : longint;
  145. rvd,lvd : bestreal;
  146. resdef,
  147. rd,ld : pdef;
  148. tempdef : pdef;
  149. concatstrings : boolean;
  150. { to evalute const sets }
  151. resultset : pconstset;
  152. i : longint;
  153. b : boolean;
  154. convdone : boolean;
  155. s1,s2 : pchar;
  156. l1,l2 : longint;
  157. begin
  158. { first do the two subtrees }
  159. firstpass(p^.left);
  160. firstpass(p^.right);
  161. if codegenerror then
  162. exit;
  163. { convert array constructors to sets, because there is no other operator
  164. possible for array constructors }
  165. if is_array_constructor(p^.left^.resulttype) then
  166. arrayconstructor_to_set(p^.left);
  167. if is_array_constructor(p^.right^.resulttype) then
  168. arrayconstructor_to_set(p^.right);
  169. { both left and right need to be valid }
  170. set_varstate(p^.left,true);
  171. set_varstate(p^.right,true);
  172. { load easier access variables }
  173. lt:=p^.left^.treetype;
  174. rt:=p^.right^.treetype;
  175. rd:=p^.right^.resulttype;
  176. ld:=p^.left^.resulttype;
  177. convdone:=false;
  178. if isbinaryoverloaded(p) then
  179. exit;
  180. { compact consts }
  181. { convert int consts to real consts, if the }
  182. { other operand is a real const }
  183. if (rt=realconstn) and is_constintnode(p^.left) then
  184. begin
  185. t:=genrealconstnode(p^.left^.value,p^.right^.resulttype);
  186. disposetree(p^.left);
  187. p^.left:=t;
  188. lt:=realconstn;
  189. end;
  190. if (lt=realconstn) and is_constintnode(p^.right) then
  191. begin
  192. t:=genrealconstnode(p^.right^.value,p^.left^.resulttype);
  193. disposetree(p^.right);
  194. p^.right:=t;
  195. rt:=realconstn;
  196. end;
  197. { both are int constants, also allow operations on two equal enums
  198. in fpc mode (Needed for conversion of C code) }
  199. if ((lt=ordconstn) and (rt=ordconstn)) and
  200. ((is_constintnode(p^.left) and is_constintnode(p^.right)) or
  201. (is_constboolnode(p^.left) and is_constboolnode(p^.right) and
  202. (p^.treetype in [ltn,lten,gtn,gten,equaln,unequaln,andn,xorn,orn]))) then
  203. begin
  204. { return a boolean for boolean operations (and,xor,or) }
  205. if is_constboolnode(p^.left) then
  206. resdef:=booldef
  207. else
  208. resdef:=s32bitdef;
  209. lv:=p^.left^.value;
  210. rv:=p^.right^.value;
  211. case p^.treetype of
  212. addn : t:=genordinalconstnode(lv+rv,resdef);
  213. subn : t:=genordinalconstnode(lv-rv,resdef);
  214. muln : t:=genordinalconstnode(lv*rv,resdef);
  215. xorn : t:=genordinalconstnode(lv xor rv,resdef);
  216. orn : t:=genordinalconstnode(lv or rv,resdef);
  217. andn : t:=genordinalconstnode(lv and rv,resdef);
  218. ltn : t:=genordinalconstnode(ord(lv<rv),booldef);
  219. lten : t:=genordinalconstnode(ord(lv<=rv),booldef);
  220. gtn : t:=genordinalconstnode(ord(lv>rv),booldef);
  221. gten : t:=genordinalconstnode(ord(lv>=rv),booldef);
  222. equaln : t:=genordinalconstnode(ord(lv=rv),booldef);
  223. unequaln : t:=genordinalconstnode(ord(lv<>rv),booldef);
  224. slashn : begin
  225. { int/int becomes a real }
  226. if int(rv)=0 then
  227. begin
  228. Message(parser_e_invalid_float_operation);
  229. t:=genrealconstnode(0,bestrealdef^);
  230. end
  231. else
  232. t:=genrealconstnode(int(lv)/int(rv),bestrealdef^);
  233. firstpass(t);
  234. end;
  235. else
  236. CGMessage(type_e_mismatch);
  237. end;
  238. disposetree(p);
  239. firstpass(t);
  240. p:=t;
  241. exit;
  242. end;
  243. { both real constants ? }
  244. if (lt=realconstn) and (rt=realconstn) then
  245. begin
  246. lvd:=p^.left^.value_real;
  247. rvd:=p^.right^.value_real;
  248. case p^.treetype of
  249. addn : t:=genrealconstnode(lvd+rvd,bestrealdef^);
  250. subn : t:=genrealconstnode(lvd-rvd,bestrealdef^);
  251. muln : t:=genrealconstnode(lvd*rvd,bestrealdef^);
  252. starstarn,
  253. caretn : begin
  254. if lvd<0 then
  255. begin
  256. Message(parser_e_invalid_float_operation);
  257. t:=genrealconstnode(0,bestrealdef^);
  258. end
  259. else if lvd=0 then
  260. t:=genrealconstnode(1.0,bestrealdef^)
  261. else
  262. t:=genrealconstnode(exp(ln(lvd)*rvd),bestrealdef^);
  263. end;
  264. slashn :
  265. begin
  266. if rvd=0 then
  267. begin
  268. Message(parser_e_invalid_float_operation);
  269. t:=genrealconstnode(0,bestrealdef^);
  270. end
  271. else
  272. t:=genrealconstnode(lvd/rvd,bestrealdef^);
  273. end;
  274. ltn : t:=genordinalconstnode(ord(lvd<rvd),booldef);
  275. lten : t:=genordinalconstnode(ord(lvd<=rvd),booldef);
  276. gtn : t:=genordinalconstnode(ord(lvd>rvd),booldef);
  277. gten : t:=genordinalconstnode(ord(lvd>=rvd),booldef);
  278. equaln : t:=genordinalconstnode(ord(lvd=rvd),booldef);
  279. unequaln : t:=genordinalconstnode(ord(lvd<>rvd),booldef);
  280. else
  281. CGMessage(type_e_mismatch);
  282. end;
  283. disposetree(p);
  284. p:=t;
  285. firstpass(p);
  286. exit;
  287. end;
  288. { concating strings ? }
  289. concatstrings:=false;
  290. s1:=nil;
  291. s2:=nil;
  292. if (lt=ordconstn) and (rt=ordconstn) and
  293. is_char(ld) and is_char(rd) then
  294. begin
  295. s1:=strpnew(char(byte(p^.left^.value)));
  296. s2:=strpnew(char(byte(p^.right^.value)));
  297. l1:=1;
  298. l2:=1;
  299. concatstrings:=true;
  300. end
  301. else
  302. if (lt=stringconstn) and (rt=ordconstn) and is_char(rd) then
  303. begin
  304. s1:=getpcharcopy(p^.left);
  305. l1:=p^.left^.length;
  306. s2:=strpnew(char(byte(p^.right^.value)));
  307. l2:=1;
  308. concatstrings:=true;
  309. end
  310. else
  311. if (lt=ordconstn) and (rt=stringconstn) and is_char(ld) then
  312. begin
  313. s1:=strpnew(char(byte(p^.left^.value)));
  314. l1:=1;
  315. s2:=getpcharcopy(p^.right);
  316. l2:=p^.right^.length;
  317. concatstrings:=true;
  318. end
  319. else if (lt=stringconstn) and (rt=stringconstn) then
  320. begin
  321. s1:=getpcharcopy(p^.left);
  322. l1:=p^.left^.length;
  323. s2:=getpcharcopy(p^.right);
  324. l2:=p^.right^.length;
  325. concatstrings:=true;
  326. end;
  327. { I will need to translate all this to ansistrings !!! }
  328. if concatstrings then
  329. begin
  330. case p^.treetype of
  331. addn :
  332. t:=genpcharconstnode(concatansistrings(s1,s2,l1,l2),l1+l2);
  333. ltn :
  334. t:=genordinalconstnode(byte(compareansistrings(s1,s2,l1,l2)<0),booldef);
  335. lten :
  336. t:=genordinalconstnode(byte(compareansistrings(s1,s2,l1,l2)<=0),booldef);
  337. gtn :
  338. t:=genordinalconstnode(byte(compareansistrings(s1,s2,l1,l2)>0),booldef);
  339. gten :
  340. t:=genordinalconstnode(byte(compareansistrings(s1,s2,l1,l2)>=0),booldef);
  341. equaln :
  342. t:=genordinalconstnode(byte(compareansistrings(s1,s2,l1,l2)=0),booldef);
  343. unequaln :
  344. t:=genordinalconstnode(byte(compareansistrings(s1,s2,l1,l2)<>0),booldef);
  345. end;
  346. ansistringdispose(s1,l1);
  347. ansistringdispose(s2,l2);
  348. disposetree(p);
  349. firstpass(t);
  350. p:=t;
  351. exit;
  352. end;
  353. { if both are orddefs then check sub types }
  354. if (ld^.deftype=orddef) and (rd^.deftype=orddef) then
  355. begin
  356. { 2 booleans ? }
  357. if is_boolean(ld) and is_boolean(rd) then
  358. begin
  359. case p^.treetype of
  360. andn,
  361. orn:
  362. begin
  363. make_bool_equal_size(p);
  364. calcregisters(p,0,0,0);
  365. p^.location.loc:=LOC_JUMP;
  366. end;
  367. xorn,ltn,lten,gtn,gten:
  368. begin
  369. make_bool_equal_size(p);
  370. if (p^.left^.location.loc in [LOC_JUMP,LOC_FLAGS]) and
  371. (p^.left^.location.loc in [LOC_JUMP,LOC_FLAGS]) then
  372. calcregisters(p,2,0,0)
  373. else
  374. calcregisters(p,1,0,0);
  375. end;
  376. unequaln,
  377. equaln:
  378. begin
  379. make_bool_equal_size(p);
  380. { Remove any compares with constants }
  381. if (p^.left^.treetype=ordconstn) then
  382. begin
  383. hp:=p^.right;
  384. b:=(p^.left^.value<>0);
  385. ot:=p^.treetype;
  386. disposetree(p^.left);
  387. putnode(p);
  388. p:=hp;
  389. if (not(b) and (ot=equaln)) or
  390. (b and (ot=unequaln)) then
  391. begin
  392. p:=gensinglenode(notn,p);
  393. firstpass(p);
  394. end;
  395. exit;
  396. end;
  397. if (p^.right^.treetype=ordconstn) then
  398. begin
  399. hp:=p^.left;
  400. b:=(p^.right^.value<>0);
  401. ot:=p^.treetype;
  402. disposetree(p^.right);
  403. putnode(p);
  404. p:=hp;
  405. if (not(b) and (ot=equaln)) or
  406. (b and (ot=unequaln)) then
  407. begin
  408. p:=gensinglenode(notn,p);
  409. firstpass(p);
  410. end;
  411. exit;
  412. end;
  413. if (p^.left^.location.loc in [LOC_JUMP,LOC_FLAGS]) and
  414. (p^.left^.location.loc in [LOC_JUMP,LOC_FLAGS]) then
  415. calcregisters(p,2,0,0)
  416. else
  417. calcregisters(p,1,0,0);
  418. end;
  419. else
  420. CGMessage(type_e_mismatch);
  421. end;
  422. { these one can't be in flags! }
  423. if p^.treetype in [xorn,unequaln,equaln] then
  424. begin
  425. if p^.left^.location.loc=LOC_FLAGS then
  426. begin
  427. p^.left:=gentypeconvnode(p^.left,porddef(p^.left^.resulttype));
  428. p^.left^.convtyp:=tc_bool_2_int;
  429. p^.left^.explizit:=true;
  430. firstpass(p^.left);
  431. end;
  432. if p^.right^.location.loc=LOC_FLAGS then
  433. begin
  434. p^.right:=gentypeconvnode(p^.right,porddef(p^.right^.resulttype));
  435. p^.right^.convtyp:=tc_bool_2_int;
  436. p^.right^.explizit:=true;
  437. firstpass(p^.right);
  438. end;
  439. { readjust registers }
  440. calcregisters(p,1,0,0);
  441. end;
  442. convdone:=true;
  443. end
  444. else
  445. { Both are chars? only convert to shortstrings for addn }
  446. if is_char(rd) and is_char(ld) then
  447. begin
  448. if p^.treetype=addn then
  449. begin
  450. p^.left:=gentypeconvnode(p^.left,cshortstringdef);
  451. p^.right:=gentypeconvnode(p^.right,cshortstringdef);
  452. firstpass(p^.left);
  453. firstpass(p^.right);
  454. { here we call STRCOPY }
  455. procinfo^.flags:=procinfo^.flags or pi_do_call;
  456. calcregisters(p,0,0,0);
  457. p^.location.loc:=LOC_MEM;
  458. end
  459. else
  460. calcregisters(p,1,0,0);
  461. convdone:=true;
  462. end
  463. { is there a 64 bit type ? }
  464. else if ((porddef(rd)^.typ=s64bit) or (porddef(ld)^.typ=s64bit)) and
  465. { the / operator is handled later }
  466. (p^.treetype<>slashn) then
  467. begin
  468. if (porddef(ld)^.typ<>s64bit) then
  469. begin
  470. p^.left:=gentypeconvnode(p^.left,cs64bitdef);
  471. firstpass(p^.left);
  472. end;
  473. if (porddef(rd)^.typ<>s64bit) then
  474. begin
  475. p^.right:=gentypeconvnode(p^.right,cs64bitdef);
  476. firstpass(p^.right);
  477. end;
  478. calcregisters(p,2,0,0);
  479. convdone:=true;
  480. end
  481. else if ((porddef(rd)^.typ=u64bit) or (porddef(ld)^.typ=u64bit)) and
  482. { the / operator is handled later }
  483. (p^.treetype<>slashn) then
  484. begin
  485. if (porddef(ld)^.typ<>u64bit) then
  486. begin
  487. p^.left:=gentypeconvnode(p^.left,cu64bitdef);
  488. firstpass(p^.left);
  489. end;
  490. if (porddef(rd)^.typ<>u64bit) then
  491. begin
  492. p^.right:=gentypeconvnode(p^.right,cu64bitdef);
  493. firstpass(p^.right);
  494. end;
  495. calcregisters(p,2,0,0);
  496. convdone:=true;
  497. end
  498. else
  499. { is there a cardinal? }
  500. if ((porddef(rd)^.typ=u32bit) or (porddef(ld)^.typ=u32bit)) and
  501. { the / operator is handled later }
  502. (p^.treetype<>slashn) then
  503. begin
  504. { convert constants to u32bit }
  505. {$ifndef cardinalmulfix}
  506. if (porddef(ld)^.typ<>u32bit) then
  507. begin
  508. { s32bit will be used for when the other is also s32bit }
  509. { the following line doesn't make any sense: it's the same as }
  510. { if ((porddef(rd)^.typ=u32bit) or (porddef(ld)^.typ=u32bit)) and }
  511. { (porddef(ld)^.typ<>u32bit) and (porddef(rd)^.typ=s32bit) then }
  512. { which can be simplified to }
  513. { if ((porddef(rd)^.typ=u32bit) and (porddef(rd)^.typ=s32bit) then }
  514. { which can never be true (JM) }
  515. if (porddef(rd)^.typ=s32bit) and (lt<>ordconstn) then
  516. p^.left:=gentypeconvnode(p^.left,s32bitdef)
  517. else
  518. p^.left:=gentypeconvnode(p^.left,u32bitdef);
  519. firstpass(p^.left);
  520. end;
  521. if (porddef(rd)^.typ<>u32bit) then
  522. begin
  523. { s32bit will be used for when the other is also s32bit }
  524. if (porddef(ld)^.typ=s32bit) and (rt<>ordconstn) then
  525. p^.right:=gentypeconvnode(p^.right,s32bitdef)
  526. else
  527. p^.right:=gentypeconvnode(p^.right,u32bitdef);
  528. firstpass(p^.right);
  529. end;
  530. {$else cardinalmulfix}
  531. { only do a conversion if the nodes have different signs }
  532. if (porddef(rd)^.typ=u32bit) xor (porddef(ld)^.typ=u32bit) then
  533. if (porddef(rd)^.typ=u32bit) then
  534. begin
  535. { can we make them both unsigned? }
  536. if is_constintnode(p^.left) and
  537. ((p^.treetype <> subn) and
  538. (p^.left^.value > 0)) then
  539. p^.left:=gentypeconvnode(p^.left,u32bitdef)
  540. else
  541. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  542. firstpass(p^.left);
  543. end
  544. else {if (porddef(ld)^.typ=u32bit) then}
  545. begin
  546. { can we make them both unsigned? }
  547. if is_constintnode(p^.right) and
  548. (p^.right^.value > 0) then
  549. p^.right:=gentypeconvnode(p^.right,u32bitdef)
  550. else
  551. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  552. firstpass(p^.right);
  553. end;
  554. {$endif cardinalmulfix}
  555. calcregisters(p,1,0,0);
  556. { for unsigned mul we need an extra register }
  557. { p^.registers32:=p^.left^.registers32+p^.right^.registers32; }
  558. if p^.treetype=muln then
  559. inc(p^.registers32);
  560. convdone:=true;
  561. end;
  562. end
  563. else
  564. { left side a setdef, must be before string processing,
  565. else array constructor can be seen as array of char (PFV) }
  566. if (ld^.deftype=setdef) {or is_array_constructor(ld)} then
  567. begin
  568. { trying to add a set element? }
  569. if (p^.treetype=addn) and (rd^.deftype<>setdef) then
  570. begin
  571. if (rt=setelementn) then
  572. begin
  573. if not(is_equal(psetdef(ld)^.elementtype.def,rd)) then
  574. CGMessage(type_e_set_element_are_not_comp);
  575. end
  576. else
  577. CGMessage(type_e_mismatch)
  578. end
  579. else
  580. begin
  581. if not(p^.treetype in [addn,subn,symdifn,muln,equaln,unequaln
  582. {$IfNDef NoSetInclusion}
  583. ,lten,gten
  584. {$EndIf NoSetInclusion}
  585. ]) then
  586. CGMessage(type_e_set_operation_unknown);
  587. { right def must be a also be set }
  588. if (rd^.deftype<>setdef) or not(is_equal(rd,ld)) then
  589. CGMessage(type_e_set_element_are_not_comp);
  590. end;
  591. { ranges require normsets }
  592. if (psetdef(ld)^.settype=smallset) and
  593. (rt=setelementn) and
  594. assigned(p^.right^.right) then
  595. begin
  596. { generate a temporary normset def }
  597. tempdef:=new(psetdef,init(psetdef(ld)^.elementtype.def,255));
  598. p^.left:=gentypeconvnode(p^.left,tempdef);
  599. firstpass(p^.left);
  600. dispose(tempdef,done);
  601. ld:=p^.left^.resulttype;
  602. end;
  603. { if the destination is not a smallset then insert a typeconv
  604. which loads a smallset into a normal set }
  605. if (psetdef(ld)^.settype<>smallset) and
  606. (psetdef(rd)^.settype=smallset) then
  607. begin
  608. if (p^.right^.treetype=setconstn) then
  609. begin
  610. t:=gensetconstnode(p^.right^.value_set,psetdef(p^.left^.resulttype));
  611. t^.left:=p^.right^.left;
  612. putnode(p^.right);
  613. p^.right:=t;
  614. end
  615. else
  616. p^.right:=gentypeconvnode(p^.right,psetdef(p^.left^.resulttype));
  617. firstpass(p^.right);
  618. end;
  619. { do constant evaluation }
  620. if (p^.right^.treetype=setconstn) and
  621. not assigned(p^.right^.left) and
  622. (p^.left^.treetype=setconstn) and
  623. not assigned(p^.left^.left) then
  624. begin
  625. new(resultset);
  626. case p^.treetype of
  627. addn : begin
  628. for i:=0 to 31 do
  629. resultset^[i]:=
  630. p^.right^.value_set^[i] or p^.left^.value_set^[i];
  631. t:=gensetconstnode(resultset,psetdef(ld));
  632. end;
  633. muln : begin
  634. for i:=0 to 31 do
  635. resultset^[i]:=
  636. p^.right^.value_set^[i] and p^.left^.value_set^[i];
  637. t:=gensetconstnode(resultset,psetdef(ld));
  638. end;
  639. subn : begin
  640. for i:=0 to 31 do
  641. resultset^[i]:=
  642. p^.left^.value_set^[i] and not(p^.right^.value_set^[i]);
  643. t:=gensetconstnode(resultset,psetdef(ld));
  644. end;
  645. symdifn : begin
  646. for i:=0 to 31 do
  647. resultset^[i]:=
  648. p^.left^.value_set^[i] xor p^.right^.value_set^[i];
  649. t:=gensetconstnode(resultset,psetdef(ld));
  650. end;
  651. unequaln : begin
  652. b:=true;
  653. for i:=0 to 31 do
  654. if p^.right^.value_set^[i]=p^.left^.value_set^[i] then
  655. begin
  656. b:=false;
  657. break;
  658. end;
  659. t:=genordinalconstnode(ord(b),booldef);
  660. end;
  661. equaln : begin
  662. b:=true;
  663. for i:=0 to 31 do
  664. if p^.right^.value_set^[i]<>p^.left^.value_set^[i] then
  665. begin
  666. b:=false;
  667. break;
  668. end;
  669. t:=genordinalconstnode(ord(b),booldef);
  670. end;
  671. {$IfNDef NoSetInclusion}
  672. lten : Begin
  673. b := true;
  674. For i := 0 to 31 Do
  675. If (p^.right^.value_set^[i] And p^.left^.value_set^[i]) <>
  676. p^.left^.value_set^[i] Then
  677. Begin
  678. b := false;
  679. Break
  680. End;
  681. t := genordinalconstnode(ord(b),booldef);
  682. End;
  683. gten : Begin
  684. b := true;
  685. For i := 0 to 31 Do
  686. If (p^.left^.value_set^[i] And p^.right^.value_set^[i]) <>
  687. p^.right^.value_set^[i] Then
  688. Begin
  689. b := false;
  690. Break
  691. End;
  692. t := genordinalconstnode(ord(b),booldef);
  693. End;
  694. {$EndIf NoSetInclusion}
  695. end;
  696. dispose(resultset);
  697. disposetree(p);
  698. p:=t;
  699. firstpass(p);
  700. exit;
  701. end
  702. else
  703. if psetdef(ld)^.settype=smallset then
  704. begin
  705. { are we adding set elements ? }
  706. if p^.right^.treetype=setelementn then
  707. calcregisters(p,2,0,0)
  708. else
  709. calcregisters(p,1,0,0);
  710. p^.location.loc:=LOC_REGISTER;
  711. end
  712. else
  713. begin
  714. calcregisters(p,0,0,0);
  715. { here we call SET... }
  716. procinfo^.flags:=procinfo^.flags or pi_do_call;
  717. p^.location.loc:=LOC_MEM;
  718. end;
  719. convdone:=true;
  720. end
  721. else
  722. { compare pchar to char arrays by addresses
  723. like BP/Delphi }
  724. if (is_pchar(ld) and is_chararray(rd)) or
  725. (is_pchar(rd) and is_chararray(ld)) then
  726. begin
  727. if is_chararray(rd) then
  728. begin
  729. p^.right:=gentypeconvnode(p^.right,ld);
  730. firstpass(p^.right);
  731. end
  732. else
  733. begin
  734. p^.left:=gentypeconvnode(p^.left,rd);
  735. firstpass(p^.left);
  736. end;
  737. p^.location.loc:=LOC_REGISTER;
  738. calcregisters(p,1,0,0);
  739. convdone:=true;
  740. end
  741. else
  742. { is one of the operands a string?,
  743. chararrays are also handled as strings (after conversion) }
  744. if (rd^.deftype=stringdef) or (ld^.deftype=stringdef) or
  745. ((is_chararray(rd) or is_char(rd)) and
  746. (is_chararray(ld) or is_char(ld))) then
  747. begin
  748. if is_widestring(rd) or is_widestring(ld) then
  749. begin
  750. if not(is_widestring(rd)) then
  751. p^.right:=gentypeconvnode(p^.right,cwidestringdef);
  752. if not(is_widestring(ld)) then
  753. p^.left:=gentypeconvnode(p^.left,cwidestringdef);
  754. p^.resulttype:=cwidestringdef;
  755. { this is only for add, the comparisaion is handled later }
  756. p^.location.loc:=LOC_REGISTER;
  757. end
  758. else if is_ansistring(rd) or is_ansistring(ld) then
  759. begin
  760. if not(is_ansistring(rd)) then
  761. p^.right:=gentypeconvnode(p^.right,cansistringdef);
  762. if not(is_ansistring(ld)) then
  763. p^.left:=gentypeconvnode(p^.left,cansistringdef);
  764. { we use ansistrings so no fast exit here }
  765. procinfo^.no_fast_exit:=true;
  766. p^.resulttype:=cansistringdef;
  767. { this is only for add, the comparisaion is handled later }
  768. p^.location.loc:=LOC_REGISTER;
  769. end
  770. else if is_longstring(rd) or is_longstring(ld) then
  771. begin
  772. if not(is_longstring(rd)) then
  773. p^.right:=gentypeconvnode(p^.right,clongstringdef);
  774. if not(is_longstring(ld)) then
  775. p^.left:=gentypeconvnode(p^.left,clongstringdef);
  776. p^.resulttype:=clongstringdef;
  777. { this is only for add, the comparisaion is handled later }
  778. p^.location.loc:=LOC_MEM;
  779. end
  780. else
  781. begin
  782. if not(is_shortstring(rd))
  783. {$ifdef newoptimizations2}
  784. {$ifdef i386}
  785. { shortstring + char handled seperately (JM) }
  786. and (not(cs_optimize in aktglobalswitches) or
  787. (p^.treetype <> addn) or not(is_char(rd)))
  788. {$endif i386}
  789. {$endif newoptimizations2}
  790. then
  791. p^.right:=gentypeconvnode(p^.right,cshortstringdef);
  792. if not(is_shortstring(ld)) then
  793. p^.left:=gentypeconvnode(p^.left,cshortstringdef);
  794. p^.resulttype:=cshortstringdef;
  795. { this is only for add, the comparisaion is handled later }
  796. p^.location.loc:=LOC_MEM;
  797. end;
  798. { only if there is a type cast we need to do again }
  799. { the first pass }
  800. if p^.left^.treetype=typeconvn then
  801. firstpass(p^.left);
  802. if p^.right^.treetype=typeconvn then
  803. firstpass(p^.right);
  804. { here we call STRCONCAT or STRCMP or STRCOPY }
  805. procinfo^.flags:=procinfo^.flags or pi_do_call;
  806. if p^.location.loc=LOC_MEM then
  807. calcregisters(p,0,0,0)
  808. else
  809. calcregisters(p,1,0,0);
  810. {$ifdef newoptimizations}
  811. {$ifdef i386}
  812. { not always necessary, only if it is not a constant char and }
  813. { not a regvar, but don't know how to check this here (JM) }
  814. if is_char(rd) then
  815. inc(p^.registers32);
  816. {$endif i386}
  817. {$endif newoptimizations}
  818. convdone:=true;
  819. end
  820. else
  821. { is one a real float ? }
  822. if (rd^.deftype=floatdef) or (ld^.deftype=floatdef) then
  823. begin
  824. { if one is a fixed, then convert to f32bit }
  825. if ((rd^.deftype=floatdef) and (pfloatdef(rd)^.typ=f32bit)) or
  826. ((ld^.deftype=floatdef) and (pfloatdef(ld)^.typ=f32bit)) then
  827. begin
  828. if not is_integer(rd) or (p^.treetype<>muln) then
  829. p^.right:=gentypeconvnode(p^.right,s32fixeddef);
  830. if not is_integer(ld) or (p^.treetype<>muln) then
  831. p^.left:=gentypeconvnode(p^.left,s32fixeddef);
  832. firstpass(p^.left);
  833. firstpass(p^.right);
  834. calcregisters(p,1,0,0);
  835. p^.location.loc:=LOC_REGISTER;
  836. end
  837. else
  838. { convert both to bestreal }
  839. begin
  840. p^.right:=gentypeconvnode(p^.right,bestrealdef^);
  841. p^.left:=gentypeconvnode(p^.left,bestrealdef^);
  842. firstpass(p^.left);
  843. firstpass(p^.right);
  844. calcregisters(p,0,1,0);
  845. p^.location.loc:=LOC_FPU;
  846. end;
  847. convdone:=true;
  848. end
  849. else
  850. { pointer comperation and subtraction }
  851. if (rd^.deftype=pointerdef) and (ld^.deftype=pointerdef) then
  852. begin
  853. p^.location.loc:=LOC_REGISTER;
  854. { p^.right:=gentypeconvnode(p^.right,ld); }
  855. { firstpass(p^.right); }
  856. calcregisters(p,1,0,0);
  857. case p^.treetype of
  858. equaln,unequaln :
  859. begin
  860. if is_equal(p^.right^.resulttype,voidpointerdef) then
  861. begin
  862. p^.right:=gentypeconvnode(p^.right,ld);
  863. firstpass(p^.right);
  864. end
  865. else if is_equal(p^.left^.resulttype,voidpointerdef) then
  866. begin
  867. p^.left:=gentypeconvnode(p^.left,rd);
  868. firstpass(p^.left);
  869. end
  870. else if not(is_equal(ld,rd)) then
  871. CGMessage(type_e_mismatch);
  872. end;
  873. ltn,lten,gtn,gten:
  874. begin
  875. if is_equal(p^.right^.resulttype,voidpointerdef) then
  876. begin
  877. p^.right:=gentypeconvnode(p^.right,ld);
  878. firstpass(p^.right);
  879. end
  880. else if is_equal(p^.left^.resulttype,voidpointerdef) then
  881. begin
  882. p^.left:=gentypeconvnode(p^.left,rd);
  883. firstpass(p^.left);
  884. end
  885. else if not(is_equal(ld,rd)) then
  886. CGMessage(type_e_mismatch);
  887. if not(cs_extsyntax in aktmoduleswitches) then
  888. CGMessage(type_e_mismatch);
  889. end;
  890. subn:
  891. begin
  892. if not(is_equal(ld,rd)) then
  893. CGMessage(type_e_mismatch);
  894. if not(cs_extsyntax in aktmoduleswitches) then
  895. CGMessage(type_e_mismatch);
  896. p^.resulttype:=s32bitdef;
  897. exit;
  898. end;
  899. else CGMessage(type_e_mismatch);
  900. end;
  901. convdone:=true;
  902. end
  903. else
  904. if (rd^.deftype=objectdef) and (ld^.deftype=objectdef) and
  905. pobjectdef(rd)^.is_class and pobjectdef(ld)^.is_class then
  906. begin
  907. p^.location.loc:=LOC_REGISTER;
  908. if pobjectdef(rd)^.is_related(pobjectdef(ld)) then
  909. p^.right:=gentypeconvnode(p^.right,ld)
  910. else
  911. p^.left:=gentypeconvnode(p^.left,rd);
  912. firstpass(p^.right);
  913. firstpass(p^.left);
  914. calcregisters(p,1,0,0);
  915. case p^.treetype of
  916. equaln,unequaln : ;
  917. else CGMessage(type_e_mismatch);
  918. end;
  919. convdone:=true;
  920. end
  921. else
  922. if (rd^.deftype=classrefdef) and (ld^.deftype=classrefdef) then
  923. begin
  924. p^.location.loc:=LOC_REGISTER;
  925. if pobjectdef(pclassrefdef(rd)^.pointertype.def)^.is_related(pobjectdef(
  926. pclassrefdef(ld)^.pointertype.def)) then
  927. p^.right:=gentypeconvnode(p^.right,ld)
  928. else
  929. p^.left:=gentypeconvnode(p^.left,rd);
  930. firstpass(p^.right);
  931. firstpass(p^.left);
  932. calcregisters(p,1,0,0);
  933. case p^.treetype of
  934. equaln,unequaln : ;
  935. else CGMessage(type_e_mismatch);
  936. end;
  937. convdone:=true;
  938. end
  939. else
  940. { allows comperasion with nil pointer }
  941. if (rd^.deftype=objectdef) and
  942. pobjectdef(rd)^.is_class then
  943. begin
  944. p^.location.loc:=LOC_REGISTER;
  945. p^.left:=gentypeconvnode(p^.left,rd);
  946. firstpass(p^.left);
  947. calcregisters(p,1,0,0);
  948. case p^.treetype of
  949. equaln,unequaln : ;
  950. else CGMessage(type_e_mismatch);
  951. end;
  952. convdone:=true;
  953. end
  954. else
  955. if (ld^.deftype=objectdef) and
  956. pobjectdef(ld)^.is_class then
  957. begin
  958. p^.location.loc:=LOC_REGISTER;
  959. p^.right:=gentypeconvnode(p^.right,ld);
  960. firstpass(p^.right);
  961. calcregisters(p,1,0,0);
  962. case p^.treetype of
  963. equaln,unequaln : ;
  964. else CGMessage(type_e_mismatch);
  965. end;
  966. convdone:=true;
  967. end
  968. else
  969. if (rd^.deftype=classrefdef) then
  970. begin
  971. p^.left:=gentypeconvnode(p^.left,rd);
  972. firstpass(p^.left);
  973. calcregisters(p,1,0,0);
  974. case p^.treetype of
  975. equaln,unequaln : ;
  976. else CGMessage(type_e_mismatch);
  977. end;
  978. convdone:=true;
  979. end
  980. else
  981. if (ld^.deftype=classrefdef) then
  982. begin
  983. p^.right:=gentypeconvnode(p^.right,ld);
  984. firstpass(p^.right);
  985. calcregisters(p,1,0,0);
  986. case p^.treetype of
  987. equaln,unequaln : ;
  988. else
  989. CGMessage(type_e_mismatch);
  990. end;
  991. convdone:=true;
  992. end
  993. else
  994. { support procvar=nil,procvar<>nil }
  995. if ((ld^.deftype=procvardef) and (rt=niln)) or
  996. ((rd^.deftype=procvardef) and (lt=niln)) then
  997. begin
  998. calcregisters(p,1,0,0);
  999. p^.location.loc:=LOC_REGISTER;
  1000. case p^.treetype of
  1001. equaln,unequaln : ;
  1002. else
  1003. CGMessage(type_e_mismatch);
  1004. end;
  1005. convdone:=true;
  1006. end
  1007. else
  1008. {$ifdef SUPPORT_MMX}
  1009. if (cs_mmx in aktlocalswitches) and is_mmx_able_array(ld) and
  1010. is_mmx_able_array(rd) and is_equal(ld,rd) then
  1011. begin
  1012. firstpass(p^.right);
  1013. firstpass(p^.left);
  1014. case p^.treetype of
  1015. addn,subn,xorn,orn,andn:
  1016. ;
  1017. { mul is a little bit restricted }
  1018. muln:
  1019. if not(mmx_type(p^.left^.resulttype) in
  1020. [mmxu16bit,mmxs16bit,mmxfixed16]) then
  1021. CGMessage(type_e_mismatch);
  1022. else
  1023. CGMessage(type_e_mismatch);
  1024. end;
  1025. p^.location.loc:=LOC_MMXREGISTER;
  1026. calcregisters(p,0,0,1);
  1027. convdone:=true;
  1028. end
  1029. else
  1030. {$endif SUPPORT_MMX}
  1031. { this is a little bit dangerous, also the left type }
  1032. { should be checked! This broke the mmx support }
  1033. if (rd^.deftype=pointerdef) or
  1034. is_zero_based_array(rd) then
  1035. begin
  1036. if is_zero_based_array(rd) then
  1037. begin
  1038. p^.resulttype:=new(ppointerdef,init(parraydef(rd)^.elementtype));
  1039. p^.right:=gentypeconvnode(p^.right,p^.resulttype);
  1040. firstpass(p^.right);
  1041. end;
  1042. p^.location.loc:=LOC_REGISTER;
  1043. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  1044. firstpass(p^.left);
  1045. calcregisters(p,1,0,0);
  1046. if p^.treetype=addn then
  1047. begin
  1048. if not(cs_extsyntax in aktmoduleswitches) or
  1049. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  1050. CGMessage(type_e_mismatch);
  1051. { Dirty hack, to support multiple firstpasses (PFV) }
  1052. if (p^.resulttype=nil) and
  1053. (rd^.deftype=pointerdef) and
  1054. (ppointerdef(rd)^.pointertype.def^.size>1) then
  1055. begin
  1056. p^.left:=gennode(muln,p^.left,genordinalconstnode(ppointerdef(rd)^.pointertype.def^.size,s32bitdef));
  1057. firstpass(p^.left);
  1058. end;
  1059. end
  1060. else
  1061. CGMessage(type_e_mismatch);
  1062. convdone:=true;
  1063. end
  1064. else
  1065. if (ld^.deftype=pointerdef) or
  1066. is_zero_based_array(ld) then
  1067. begin
  1068. if is_zero_based_array(ld) then
  1069. begin
  1070. p^.resulttype:=new(ppointerdef,init(parraydef(ld)^.elementtype));
  1071. p^.left:=gentypeconvnode(p^.left,p^.resulttype);
  1072. firstpass(p^.left);
  1073. end;
  1074. p^.location.loc:=LOC_REGISTER;
  1075. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  1076. firstpass(p^.right);
  1077. calcregisters(p,1,0,0);
  1078. case p^.treetype of
  1079. addn,subn : begin
  1080. if not(cs_extsyntax in aktmoduleswitches) or
  1081. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  1082. CGMessage(type_e_mismatch);
  1083. { Dirty hack, to support multiple firstpasses (PFV) }
  1084. if (p^.resulttype=nil) and
  1085. (ld^.deftype=pointerdef) and
  1086. (ppointerdef(ld)^.pointertype.def^.size>1) then
  1087. begin
  1088. p^.right:=gennode(muln,p^.right,
  1089. genordinalconstnode(ppointerdef(ld)^.pointertype.def^.size,s32bitdef));
  1090. firstpass(p^.right);
  1091. end;
  1092. end;
  1093. else
  1094. CGMessage(type_e_mismatch);
  1095. end;
  1096. convdone:=true;
  1097. end
  1098. else
  1099. if (rd^.deftype=procvardef) and (ld^.deftype=procvardef) and is_equal(rd,ld) then
  1100. begin
  1101. calcregisters(p,1,0,0);
  1102. p^.location.loc:=LOC_REGISTER;
  1103. case p^.treetype of
  1104. equaln,unequaln : ;
  1105. else
  1106. CGMessage(type_e_mismatch);
  1107. end;
  1108. convdone:=true;
  1109. end
  1110. else
  1111. if (ld^.deftype=enumdef) and (rd^.deftype=enumdef) then
  1112. begin
  1113. if not(is_equal(ld,rd)) then
  1114. begin
  1115. p^.right:=gentypeconvnode(p^.right,ld);
  1116. firstpass(p^.right);
  1117. end;
  1118. calcregisters(p,1,0,0);
  1119. case p^.treetype of
  1120. equaln,unequaln,
  1121. ltn,lten,gtn,gten : ;
  1122. else CGMessage(type_e_mismatch);
  1123. end;
  1124. convdone:=true;
  1125. end;
  1126. { the general solution is to convert to 32 bit int }
  1127. if not convdone then
  1128. begin
  1129. { but an int/int gives real/real! }
  1130. if p^.treetype=slashn then
  1131. begin
  1132. CGMessage(type_h_use_div_for_int);
  1133. p^.right:=gentypeconvnode(p^.right,bestrealdef^);
  1134. p^.left:=gentypeconvnode(p^.left,bestrealdef^);
  1135. firstpass(p^.left);
  1136. firstpass(p^.right);
  1137. { maybe we need an integer register to save }
  1138. { a reference }
  1139. if ((p^.left^.location.loc<>LOC_FPU) or
  1140. (p^.right^.location.loc<>LOC_FPU)) and
  1141. (p^.left^.registers32=p^.right^.registers32) then
  1142. calcregisters(p,1,1,0)
  1143. else
  1144. calcregisters(p,0,1,0);
  1145. p^.location.loc:=LOC_FPU;
  1146. end
  1147. else
  1148. begin
  1149. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  1150. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  1151. firstpass(p^.left);
  1152. firstpass(p^.right);
  1153. calcregisters(p,1,0,0);
  1154. p^.location.loc:=LOC_REGISTER;
  1155. end;
  1156. end;
  1157. if codegenerror then
  1158. exit;
  1159. { determines result type for comparions }
  1160. { here the is a problem with multiple passes }
  1161. { example length(s)+1 gets internal 'longint' type first }
  1162. { if it is a arg it is converted to 'LONGINT' }
  1163. { but a second first pass will reset this to 'longint' }
  1164. case p^.treetype of
  1165. ltn,lten,gtn,gten,equaln,unequaln:
  1166. begin
  1167. if (not assigned(p^.resulttype)) or
  1168. (p^.resulttype^.deftype=stringdef) then
  1169. p^.resulttype:=booldef;
  1170. if is_64bitint(p^.left^.resulttype) then
  1171. p^.location.loc:=LOC_JUMP
  1172. else
  1173. p^.location.loc:=LOC_FLAGS;
  1174. end;
  1175. xorn:
  1176. begin
  1177. if not assigned(p^.resulttype) then
  1178. p^.resulttype:=p^.left^.resulttype;
  1179. p^.location.loc:=LOC_REGISTER;
  1180. end;
  1181. addn:
  1182. begin
  1183. if not assigned(p^.resulttype) then
  1184. begin
  1185. { for strings, return is always a 255 char string }
  1186. if is_shortstring(p^.left^.resulttype) then
  1187. p^.resulttype:=cshortstringdef
  1188. else
  1189. p^.resulttype:=p^.left^.resulttype;
  1190. end;
  1191. end;
  1192. {$ifdef cardinalmulfix}
  1193. muln:
  1194. { if we multiply an unsigned with a signed number, the result is signed }
  1195. { in the other cases, the result remains signed or unsigned depending on }
  1196. { the multiplication factors (JM) }
  1197. if (p^.left^.resulttype^.deftype = orddef) and
  1198. (p^.right^.resulttype^.deftype = orddef) and
  1199. is_signed(p^.right^.resulttype) then
  1200. p^.resulttype := p^.right^.resulttype
  1201. else p^.resulttype := p^.left^.resulttype;
  1202. (*
  1203. subn:
  1204. { if we substract a u32bit from a positive constant, the result becomes }
  1205. { s32bit as well (JM) }
  1206. begin
  1207. if (p^.right^.resulttype^.deftype = orddef) and
  1208. (p^.left^.resulttype^.deftype = orddef) and
  1209. (porddef(p^.right^.resulttype)^.typ = u32bit) and
  1210. is_constintnode(p^.left) and
  1211. { (porddef(p^.left^.resulttype)^.typ <> u32bit) and}
  1212. (p^.left^.value > 0) then
  1213. begin
  1214. p^.left := gentypeconvnode(p^.left,u32bitdef);
  1215. firstpass(p^.left);
  1216. end;
  1217. p^.resulttype:=p^.left^.resulttype;
  1218. end;
  1219. *)
  1220. {$endif cardinalmulfix}
  1221. else
  1222. p^.resulttype:=p^.left^.resulttype;
  1223. end;
  1224. end;
  1225. end.
  1226. {
  1227. $Log$
  1228. Revision 1.79 2000-06-02 21:24:48 pierre
  1229. * operator overloading now uses isbinaryoperatoracceptable
  1230. and is unaryoperatoracceptable
  1231. Revision 1.78 2000/05/31 06:58:41 florian
  1232. * forgot to commit a fix for the enumeration subrange problem, yesterday
  1233. Revision 1.77 2000/05/11 17:53:40 peter
  1234. * small fix for previous commit
  1235. Revision 1.76 2000/05/11 16:47:37 peter
  1236. * fixed check for overloaded operator with array and chararray check
  1237. Revision 1.75 2000/04/25 14:43:36 jonas
  1238. - disabled "string_var := string_var + ... " and "string_var + char_var"
  1239. optimizations (were only active with -dnewoptimizations) because of
  1240. several internal issues
  1241. Revision 1.74 2000/04/21 12:35:05 jonas
  1242. + special code for string + char, between -dnewoptimizations
  1243. Revision 1.73 2000/03/28 21:14:18 pierre
  1244. * fix for bug 891
  1245. Revision 1.72 2000/03/20 10:16:51 florian
  1246. * fixed <dword>/<dword>, <int64>/<int64> and <qword>/<qword>
  1247. Revision 1.71 2000/03/18 15:01:19 jonas
  1248. * moved a $maxfpuregisters directive a bit up because it was being
  1249. ignored
  1250. Revision 1.70 2000/02/19 10:12:48 florian
  1251. * fixed one more internalerror 10
  1252. Revision 1.69 2000/02/17 14:53:42 florian
  1253. * some updates for the newcg
  1254. Revision 1.68 2000/02/14 22:34:28 florian
  1255. * fixed another internalerror
  1256. Revision 1.67 2000/02/13 22:46:28 florian
  1257. * fixed an internalerror with writeln
  1258. * fixed arrayconstructor_to_set to force the generation of better code
  1259. and added a more strict type checking
  1260. Revision 1.66 2000/02/13 14:21:51 jonas
  1261. * modifications to make the compiler functional when compiled with
  1262. -Or
  1263. Revision 1.65 2000/02/09 13:23:06 peter
  1264. * log truncated
  1265. Revision 1.64 2000/02/04 08:47:10 florian
  1266. * better register variable allocation in -Or mode
  1267. Revision 1.63 2000/01/07 01:14:43 peter
  1268. * updated copyright to 2000
  1269. Revision 1.62 2000/01/04 20:10:20 florian
  1270. * mmx support fixed
  1271. Revision 1.61 1999/12/11 18:53:31 jonas
  1272. * fixed type conversions of results of operations with cardinals
  1273. (between -dcardinalmulfix)
  1274. Revision 1.60 1999/12/09 23:18:04 pierre
  1275. * no_fast_exit if procedure contains implicit termination code
  1276. Revision 1.59 1999/12/01 12:42:33 peter
  1277. * fixed bug 698
  1278. * removed some notes about unused vars
  1279. Revision 1.58 1999/11/30 10:40:56 peter
  1280. + ttype, tsymlist
  1281. Revision 1.57 1999/11/26 13:51:29 pierre
  1282. * fix for overloading of shr shl mod and div
  1283. Revision 1.56 1999/11/18 15:34:48 pierre
  1284. * Notes/Hints for local syms changed to
  1285. Set_varstate function
  1286. Revision 1.55 1999/11/17 17:05:06 pierre
  1287. * Notes/hints changes
  1288. Revision 1.54 1999/11/16 23:45:28 pierre
  1289. * global var token was changed by overload code (form bug 707)
  1290. Revision 1.53 1999/11/15 21:53:42 peter
  1291. * fixed constant eval for bool xor/or/and bool
  1292. Revision 1.52 1999/11/15 17:53:00 pierre
  1293. + one field added for ttoken record for operator
  1294. linking the id to the corresponding operator token that
  1295. can now now all be overloaded
  1296. * overloaded operators are resetted to nil in InitSymtable
  1297. (bug when trying to compile a uint that overloads operators twice)
  1298. Revision 1.51 1999/11/06 14:34:29 peter
  1299. * truncated log to 20 revs
  1300. Revision 1.50 1999/09/27 23:45:00 peter
  1301. * procinfo is now a pointer
  1302. * support for result setting in sub procedure
  1303. Revision 1.49 1999/09/16 13:39:14 peter
  1304. * arrayconstructor 2 set conversion is now called always in the
  1305. beginning of firstadd
  1306. Revision 1.48 1999/09/15 20:35:45 florian
  1307. * small fix to operator overloading when in MMX mode
  1308. + the compiler uses now fldz and fld1 if possible
  1309. + some fixes to floating point registers
  1310. + some math. functions (arctan, ln, sin, cos, sqrt, sqr, pi) are now inlined
  1311. * .... ???
  1312. Revision 1.47 1999/09/13 16:28:05 peter
  1313. * typo in previous commit open_array -> chararray :(
  1314. Revision 1.46 1999/09/10 15:40:46 peter
  1315. * fixed array check for operators, becuase array can also be a set
  1316. Revision 1.45 1999/09/08 16:05:29 peter
  1317. * pointer add/sub is now as expected and the same results as inc/dec
  1318. }