tcadd.pas 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 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. implementation
  24. uses
  25. globtype,systems,tokens,
  26. cobjects,verbose,globals,
  27. symconst,symtable,aasm,types,
  28. hcodegen,htypechk,pass_1,
  29. cpubase,tccnv
  30. ;
  31. {*****************************************************************************
  32. FirstAdd
  33. *****************************************************************************}
  34. procedure firstadd(var p : ptree);
  35. procedure make_bool_equal_size(var p:ptree);
  36. begin
  37. if porddef(p^.left^.resulttype)^.typ>porddef(p^.right^.resulttype)^.typ then
  38. begin
  39. p^.right:=gentypeconvnode(p^.right,porddef(p^.left^.resulttype));
  40. p^.right^.convtyp:=tc_bool_2_int;
  41. p^.right^.explizit:=true;
  42. firstpass(p^.right);
  43. end
  44. else
  45. if porddef(p^.left^.resulttype)^.typ<porddef(p^.right^.resulttype)^.typ then
  46. begin
  47. p^.left:=gentypeconvnode(p^.left,porddef(p^.right^.resulttype));
  48. p^.left^.convtyp:=tc_bool_2_int;
  49. p^.left^.explizit:=true;
  50. firstpass(p^.left);
  51. end;
  52. end;
  53. var
  54. t,hp : ptree;
  55. ot,
  56. lt,rt : ttreetyp;
  57. rv,lv : longint;
  58. rvd,lvd : bestreal;
  59. resdef,
  60. rd,ld : pdef;
  61. tempdef : pdef;
  62. concatstrings : boolean;
  63. { to evalute const sets }
  64. resultset : pconstset;
  65. i : longint;
  66. b : boolean;
  67. convdone : boolean;
  68. s1,s2 : pchar;
  69. l1,l2 : longint;
  70. { this totally forgets to set the pi_do_call flag !! }
  71. label
  72. no_overload;
  73. begin
  74. { first do the two subtrees }
  75. firstpass(p^.left);
  76. firstpass(p^.right);
  77. if codegenerror then
  78. exit;
  79. { convert array constructors to sets, because there is no other operator
  80. possible for array constructors }
  81. if is_array_constructor(p^.left^.resulttype) then
  82. arrayconstructor_to_set(p^.left);
  83. if is_array_constructor(p^.right^.resulttype) then
  84. arrayconstructor_to_set(p^.right);
  85. { load easier access variables }
  86. lt:=p^.left^.treetype;
  87. rt:=p^.right^.treetype;
  88. rd:=p^.right^.resulttype;
  89. ld:=p^.left^.resulttype;
  90. convdone:=false;
  91. { overloaded operator ? }
  92. if (p^.treetype=starstarn) or
  93. (ld^.deftype=recorddef) or
  94. ((ld^.deftype=arraydef) and
  95. not((cs_mmx in aktlocalswitches) and
  96. is_mmx_able_array(ld)) and
  97. (not (rd^.deftype in [orddef])) and
  98. (not is_chararray(ld))
  99. ) or
  100. { <> and = are defined for classes }
  101. ((ld^.deftype=objectdef) and
  102. (not(pobjectdef(ld)^.is_class) or
  103. not(p^.treetype in [equaln,unequaln])
  104. )
  105. ) or
  106. (rd^.deftype=recorddef) or
  107. ((rd^.deftype=arraydef) and
  108. not((cs_mmx in aktlocalswitches) and
  109. is_mmx_able_array(rd)) and
  110. (not (ld^.deftype in [orddef])) and
  111. (not is_chararray(rd))
  112. ) or
  113. { <> and = are defined for classes }
  114. ((rd^.deftype=objectdef) and
  115. (not(pobjectdef(rd)^.is_class) or
  116. not(p^.treetype in [equaln,unequaln])
  117. )
  118. ) then
  119. begin
  120. {!!!!!!!!! handle paras }
  121. case p^.treetype of
  122. { the nil as symtable signs firstcalln that this is
  123. an overloaded operator }
  124. addn:
  125. t:=gencallnode(overloaded_operators[_plus],nil);
  126. subn:
  127. t:=gencallnode(overloaded_operators[_minus],nil);
  128. muln:
  129. t:=gencallnode(overloaded_operators[_star],nil);
  130. starstarn:
  131. t:=gencallnode(overloaded_operators[_starstar],nil);
  132. slashn:
  133. t:=gencallnode(overloaded_operators[_slash],nil);
  134. ltn:
  135. t:=gencallnode(overloaded_operators[tokens._lt],nil);
  136. gtn:
  137. t:=gencallnode(overloaded_operators[_gt],nil);
  138. lten:
  139. t:=gencallnode(overloaded_operators[_lte],nil);
  140. gten:
  141. t:=gencallnode(overloaded_operators[_gte],nil);
  142. equaln,unequaln :
  143. t:=gencallnode(overloaded_operators[_equal],nil);
  144. else goto no_overload;
  145. end;
  146. { we have to convert p^.left and p^.right into
  147. callparanodes }
  148. if t^.symtableprocentry=nil then
  149. begin
  150. CGMessage(parser_e_operator_not_overloaded);
  151. putnode(t);
  152. end
  153. else
  154. begin
  155. t^.left:=gencallparanode(p^.left,nil);
  156. t^.left:=gencallparanode(p^.right,t^.left);
  157. if p^.treetype=unequaln then
  158. t:=gensinglenode(notn,t);
  159. firstpass(t);
  160. putnode(p);
  161. p:=t;
  162. exit;
  163. end;
  164. end;
  165. no_overload:
  166. { compact consts }
  167. { convert int consts to real consts, if the }
  168. { other operand is a real const }
  169. if (rt=realconstn) and is_constintnode(p^.left) then
  170. begin
  171. t:=genrealconstnode(p^.left^.value,p^.right^.resulttype);
  172. disposetree(p^.left);
  173. p^.left:=t;
  174. lt:=realconstn;
  175. end;
  176. if (lt=realconstn) and is_constintnode(p^.right) then
  177. begin
  178. t:=genrealconstnode(p^.right^.value,p^.left^.resulttype);
  179. disposetree(p^.right);
  180. p^.right:=t;
  181. rt:=realconstn;
  182. end;
  183. { both are int constants, also allow operations on two equal enums
  184. in fpc mode (Needed for conversion of C code) }
  185. if ((lt=ordconstn) and (rt=ordconstn)) and
  186. ((is_constintnode(p^.left) and is_constintnode(p^.right)) or
  187. (is_constboolnode(p^.left) and is_constboolnode(p^.right) and
  188. (p^.treetype in [ltn,lten,gtn,gten,equaln,unequaln]))) then
  189. begin
  190. resdef:=s32bitdef;
  191. lv:=p^.left^.value;
  192. rv:=p^.right^.value;
  193. case p^.treetype of
  194. addn : t:=genordinalconstnode(lv+rv,resdef);
  195. subn : t:=genordinalconstnode(lv-rv,resdef);
  196. muln : t:=genordinalconstnode(lv*rv,resdef);
  197. xorn : t:=genordinalconstnode(lv xor rv,resdef);
  198. orn : t:=genordinalconstnode(lv or rv,resdef);
  199. andn : t:=genordinalconstnode(lv and rv,resdef);
  200. ltn : t:=genordinalconstnode(ord(lv<rv),booldef);
  201. lten : t:=genordinalconstnode(ord(lv<=rv),booldef);
  202. gtn : t:=genordinalconstnode(ord(lv>rv),booldef);
  203. gten : t:=genordinalconstnode(ord(lv>=rv),booldef);
  204. equaln : t:=genordinalconstnode(ord(lv=rv),booldef);
  205. unequaln : t:=genordinalconstnode(ord(lv<>rv),booldef);
  206. slashn : begin
  207. { int/int becomes a real }
  208. if int(rv)=0 then
  209. begin
  210. Message(parser_e_invalid_float_operation);
  211. t:=genrealconstnode(0,bestrealdef^);
  212. end
  213. else
  214. t:=genrealconstnode(int(lv)/int(rv),bestrealdef^);
  215. firstpass(t);
  216. end;
  217. else
  218. CGMessage(type_e_mismatch);
  219. end;
  220. disposetree(p);
  221. firstpass(t);
  222. p:=t;
  223. exit;
  224. end;
  225. { both real constants ? }
  226. if (lt=realconstn) and (rt=realconstn) then
  227. begin
  228. lvd:=p^.left^.value_real;
  229. rvd:=p^.right^.value_real;
  230. case p^.treetype of
  231. addn : t:=genrealconstnode(lvd+rvd,bestrealdef^);
  232. subn : t:=genrealconstnode(lvd-rvd,bestrealdef^);
  233. muln : t:=genrealconstnode(lvd*rvd,bestrealdef^);
  234. caretn : t:=genrealconstnode(exp(ln(lvd)*rvd),bestrealdef^);
  235. slashn : begin
  236. if rvd=0 then
  237. begin
  238. Message(parser_e_invalid_float_operation);
  239. t:=genrealconstnode(0,bestrealdef^);
  240. end
  241. else
  242. t:=genrealconstnode(lvd/rvd,bestrealdef^);
  243. end;
  244. ltn : t:=genordinalconstnode(ord(lvd<rvd),booldef);
  245. lten : t:=genordinalconstnode(ord(lvd<=rvd),booldef);
  246. gtn : t:=genordinalconstnode(ord(lvd>rvd),booldef);
  247. gten : t:=genordinalconstnode(ord(lvd>=rvd),booldef);
  248. equaln : t:=genordinalconstnode(ord(lvd=rvd),booldef);
  249. unequaln : t:=genordinalconstnode(ord(lvd<>rvd),booldef);
  250. else
  251. CGMessage(type_e_mismatch);
  252. end;
  253. disposetree(p);
  254. p:=t;
  255. firstpass(p);
  256. exit;
  257. end;
  258. { concating strings ? }
  259. concatstrings:=false;
  260. s1:=nil;
  261. s2:=nil;
  262. if (lt=ordconstn) and (rt=ordconstn) and
  263. is_char(ld) and is_char(rd) then
  264. begin
  265. s1:=strpnew(char(byte(p^.left^.value)));
  266. s2:=strpnew(char(byte(p^.right^.value)));
  267. l1:=1;
  268. l2:=1;
  269. concatstrings:=true;
  270. end
  271. else
  272. if (lt=stringconstn) and (rt=ordconstn) and is_char(rd) then
  273. begin
  274. s1:=getpcharcopy(p^.left);
  275. l1:=p^.left^.length;
  276. s2:=strpnew(char(byte(p^.right^.value)));
  277. l2:=1;
  278. concatstrings:=true;
  279. end
  280. else
  281. if (lt=ordconstn) and (rt=stringconstn) and is_char(ld) then
  282. begin
  283. s1:=strpnew(char(byte(p^.left^.value)));
  284. l1:=1;
  285. s2:=getpcharcopy(p^.right);
  286. l2:=p^.right^.length;
  287. concatstrings:=true;
  288. end
  289. else if (lt=stringconstn) and (rt=stringconstn) then
  290. begin
  291. s1:=getpcharcopy(p^.left);
  292. l1:=p^.left^.length;
  293. s2:=getpcharcopy(p^.right);
  294. l2:=p^.right^.length;
  295. concatstrings:=true;
  296. end;
  297. { I will need to translate all this to ansistrings !!! }
  298. if concatstrings then
  299. begin
  300. case p^.treetype of
  301. addn :
  302. t:=genpcharconstnode(concatansistrings(s1,s2,l1,l2),l1+l2);
  303. ltn :
  304. t:=genordinalconstnode(byte(compareansistrings(s1,s2,l1,l2)<0),booldef);
  305. lten :
  306. t:=genordinalconstnode(byte(compareansistrings(s1,s2,l1,l2)<=0),booldef);
  307. gtn :
  308. t:=genordinalconstnode(byte(compareansistrings(s1,s2,l1,l2)>0),booldef);
  309. gten :
  310. t:=genordinalconstnode(byte(compareansistrings(s1,s2,l1,l2)>=0),booldef);
  311. equaln :
  312. t:=genordinalconstnode(byte(compareansistrings(s1,s2,l1,l2)=0),booldef);
  313. unequaln :
  314. t:=genordinalconstnode(byte(compareansistrings(s1,s2,l1,l2)<>0),booldef);
  315. end;
  316. ansistringdispose(s1,l1);
  317. ansistringdispose(s2,l2);
  318. disposetree(p);
  319. firstpass(t);
  320. p:=t;
  321. exit;
  322. end;
  323. { if both are orddefs then check sub types }
  324. if (ld^.deftype=orddef) and (rd^.deftype=orddef) then
  325. begin
  326. { 2 booleans ? }
  327. if is_boolean(ld) and is_boolean(rd) then
  328. begin
  329. case p^.treetype of
  330. andn,
  331. orn:
  332. begin
  333. calcregisters(p,0,0,0);
  334. make_bool_equal_size(p);
  335. p^.location.loc:=LOC_JUMP;
  336. end;
  337. xorn,ltn,lten,gtn,gten :
  338. begin
  339. make_bool_equal_size(p);
  340. if (p^.left^.location.loc in [LOC_JUMP,LOC_FLAGS]) and
  341. (p^.left^.location.loc in [LOC_JUMP,LOC_FLAGS]) then
  342. calcregisters(p,2,0,0)
  343. else
  344. calcregisters(p,1,0,0);
  345. end;
  346. unequaln,
  347. equaln:
  348. begin
  349. make_bool_equal_size(p);
  350. { Remove any compares with constants }
  351. if (p^.left^.treetype=ordconstn) then
  352. begin
  353. hp:=p^.right;
  354. b:=(p^.left^.value<>0);
  355. ot:=p^.treetype;
  356. disposetree(p^.left);
  357. putnode(p);
  358. p:=hp;
  359. if (not(b) and (ot=equaln)) or
  360. (b and (ot=unequaln)) then
  361. begin
  362. p:=gensinglenode(notn,p);
  363. firstpass(p);
  364. end;
  365. exit;
  366. end;
  367. if (p^.right^.treetype=ordconstn) then
  368. begin
  369. hp:=p^.left;
  370. b:=(p^.right^.value<>0);
  371. ot:=p^.treetype;
  372. disposetree(p^.right);
  373. putnode(p);
  374. p:=hp;
  375. if (not(b) and (ot=equaln)) or
  376. (b and (ot=unequaln)) then
  377. begin
  378. p:=gensinglenode(notn,p);
  379. firstpass(p);
  380. end;
  381. exit;
  382. end;
  383. if (p^.left^.location.loc in [LOC_JUMP,LOC_FLAGS]) and
  384. (p^.left^.location.loc in [LOC_JUMP,LOC_FLAGS]) then
  385. calcregisters(p,2,0,0)
  386. else
  387. calcregisters(p,1,0,0);
  388. end;
  389. else
  390. CGMessage(type_e_mismatch);
  391. end;
  392. { these one can't be in flags! }
  393. if p^.treetype in [xorn,unequaln,equaln] then
  394. begin
  395. if p^.left^.location.loc=LOC_FLAGS then
  396. begin
  397. p^.left:=gentypeconvnode(p^.left,porddef(p^.left^.resulttype));
  398. p^.left^.convtyp:=tc_bool_2_int;
  399. p^.left^.explizit:=true;
  400. firstpass(p^.left);
  401. end;
  402. if p^.right^.location.loc=LOC_FLAGS then
  403. begin
  404. p^.right:=gentypeconvnode(p^.right,porddef(p^.right^.resulttype));
  405. p^.right^.convtyp:=tc_bool_2_int;
  406. p^.right^.explizit:=true;
  407. firstpass(p^.right);
  408. end;
  409. { readjust registers }
  410. calcregisters(p,1,0,0);
  411. end;
  412. convdone:=true;
  413. end
  414. else
  415. { Both are chars? only convert to shortstrings for addn }
  416. if is_char(rd) and is_char(ld) then
  417. begin
  418. if p^.treetype=addn then
  419. begin
  420. p^.left:=gentypeconvnode(p^.left,cshortstringdef);
  421. p^.right:=gentypeconvnode(p^.right,cshortstringdef);
  422. firstpass(p^.left);
  423. firstpass(p^.right);
  424. { here we call STRCOPY }
  425. procinfo^.flags:=procinfo^.flags or pi_do_call;
  426. calcregisters(p,0,0,0);
  427. p^.location.loc:=LOC_MEM;
  428. end
  429. else
  430. calcregisters(p,1,0,0);
  431. convdone:=true;
  432. end
  433. { is there a 64 bit type ? }
  434. else if (porddef(rd)^.typ=s64bit) or (porddef(ld)^.typ=s64bit) then
  435. begin
  436. if (porddef(ld)^.typ<>s64bit) then
  437. begin
  438. p^.left:=gentypeconvnode(p^.left,cs64bitdef);
  439. firstpass(p^.left);
  440. end;
  441. if (porddef(rd)^.typ<>s64bit) then
  442. begin
  443. p^.right:=gentypeconvnode(p^.right,cs64bitdef);
  444. firstpass(p^.right);
  445. end;
  446. calcregisters(p,2,0,0);
  447. convdone:=true;
  448. end
  449. else if (porddef(rd)^.typ=u64bit) or (porddef(ld)^.typ=u64bit) then
  450. begin
  451. if (porddef(ld)^.typ<>u64bit) then
  452. begin
  453. p^.left:=gentypeconvnode(p^.left,cu64bitdef);
  454. firstpass(p^.left);
  455. end;
  456. if (porddef(rd)^.typ<>u64bit) then
  457. begin
  458. p^.right:=gentypeconvnode(p^.right,cu64bitdef);
  459. firstpass(p^.right);
  460. end;
  461. calcregisters(p,2,0,0);
  462. convdone:=true;
  463. end
  464. else
  465. { is there a cardinal? }
  466. if (porddef(rd)^.typ=u32bit) or (porddef(ld)^.typ=u32bit) then
  467. begin
  468. { convert constants to u32bit }
  469. if (porddef(ld)^.typ<>u32bit) then
  470. begin
  471. { s32bit will be used for when the other is also s32bit }
  472. if (porddef(rd)^.typ=s32bit) and (lt<>ordconstn) then
  473. p^.left:=gentypeconvnode(p^.left,s32bitdef)
  474. else
  475. p^.left:=gentypeconvnode(p^.left,u32bitdef);
  476. firstpass(p^.left);
  477. end;
  478. if (porddef(rd)^.typ<>u32bit) then
  479. begin
  480. { s32bit will be used for when the other is also s32bit }
  481. if (porddef(ld)^.typ=s32bit) and (rt<>ordconstn) then
  482. p^.right:=gentypeconvnode(p^.right,s32bitdef)
  483. else
  484. p^.right:=gentypeconvnode(p^.right,u32bitdef);
  485. firstpass(p^.right);
  486. end;
  487. calcregisters(p,1,0,0);
  488. { for unsigned mul we need an extra register }
  489. { p^.registers32:=p^.left^.registers32+p^.right^.registers32; }
  490. if p^.treetype=muln then
  491. inc(p^.registers32);
  492. convdone:=true;
  493. end;
  494. end
  495. else
  496. { left side a setdef, must be before string processing,
  497. else array constructor can be seen as array of char (PFV) }
  498. if (ld^.deftype=setdef) {or is_array_constructor(ld)} then
  499. begin
  500. { trying to add a set element? }
  501. if (p^.treetype=addn) and (rd^.deftype<>setdef) then
  502. begin
  503. if (rt=setelementn) then
  504. begin
  505. if not(is_equal(psetdef(ld)^.setof,rd)) then
  506. CGMessage(type_e_set_element_are_not_comp);
  507. end
  508. else
  509. CGMessage(type_e_mismatch)
  510. end
  511. else
  512. begin
  513. if not(p^.treetype in [addn,subn,symdifn,muln,equaln,unequaln
  514. {$IfNDef NoSetInclusion}
  515. ,lten,gten
  516. {$EndIf NoSetInclusion}
  517. ]) then
  518. CGMessage(type_e_set_operation_unknown);
  519. { right def must be a also be set }
  520. if (rd^.deftype<>setdef) or not(is_equal(rd,ld)) then
  521. CGMessage(type_e_set_element_are_not_comp);
  522. end;
  523. { ranges require normsets }
  524. if (psetdef(ld)^.settype=smallset) and
  525. (rt=setelementn) and
  526. assigned(p^.right^.right) then
  527. begin
  528. { generate a temporary normset def }
  529. tempdef:=new(psetdef,init(psetdef(ld)^.setof,255));
  530. p^.left:=gentypeconvnode(p^.left,tempdef);
  531. firstpass(p^.left);
  532. dispose(tempdef,done);
  533. ld:=p^.left^.resulttype;
  534. end;
  535. { if the destination is not a smallset then insert a typeconv
  536. which loads a smallset into a normal set }
  537. if (psetdef(ld)^.settype<>smallset) and
  538. (psetdef(rd)^.settype=smallset) then
  539. begin
  540. if (p^.right^.treetype=setconstn) then
  541. begin
  542. t:=gensetconstnode(p^.right^.value_set,psetdef(p^.left^.resulttype));
  543. t^.left:=p^.right^.left;
  544. putnode(p^.right);
  545. p^.right:=t;
  546. end
  547. else
  548. p^.right:=gentypeconvnode(p^.right,psetdef(p^.left^.resulttype));
  549. firstpass(p^.right);
  550. end;
  551. { do constant evaluation }
  552. if (p^.right^.treetype=setconstn) and
  553. not assigned(p^.right^.left) and
  554. (p^.left^.treetype=setconstn) and
  555. not assigned(p^.left^.left) then
  556. begin
  557. new(resultset);
  558. case p^.treetype of
  559. addn : begin
  560. for i:=0 to 31 do
  561. resultset^[i]:=
  562. p^.right^.value_set^[i] or p^.left^.value_set^[i];
  563. t:=gensetconstnode(resultset,psetdef(ld));
  564. end;
  565. muln : begin
  566. for i:=0 to 31 do
  567. resultset^[i]:=
  568. p^.right^.value_set^[i] and p^.left^.value_set^[i];
  569. t:=gensetconstnode(resultset,psetdef(ld));
  570. end;
  571. subn : begin
  572. for i:=0 to 31 do
  573. resultset^[i]:=
  574. p^.left^.value_set^[i] and not(p^.right^.value_set^[i]);
  575. t:=gensetconstnode(resultset,psetdef(ld));
  576. end;
  577. symdifn : begin
  578. for i:=0 to 31 do
  579. resultset^[i]:=
  580. p^.left^.value_set^[i] xor p^.right^.value_set^[i];
  581. t:=gensetconstnode(resultset,psetdef(ld));
  582. end;
  583. unequaln : begin
  584. b:=true;
  585. for i:=0 to 31 do
  586. if p^.right^.value_set^[i]=p^.left^.value_set^[i] then
  587. begin
  588. b:=false;
  589. break;
  590. end;
  591. t:=genordinalconstnode(ord(b),booldef);
  592. end;
  593. equaln : begin
  594. b:=true;
  595. for i:=0 to 31 do
  596. if p^.right^.value_set^[i]<>p^.left^.value_set^[i] then
  597. begin
  598. b:=false;
  599. break;
  600. end;
  601. t:=genordinalconstnode(ord(b),booldef);
  602. end;
  603. {$IfNDef NoSetInclusion}
  604. lten : Begin
  605. b := true;
  606. For i := 0 to 31 Do
  607. If (p^.right^.value_set^[i] And p^.left^.value_set^[i]) <>
  608. p^.left^.value_set^[i] Then
  609. Begin
  610. b := false;
  611. Break
  612. End;
  613. t := genordinalconstnode(ord(b),booldef);
  614. End;
  615. gten : Begin
  616. b := true;
  617. For i := 0 to 31 Do
  618. If (p^.left^.value_set^[i] And p^.right^.value_set^[i]) <>
  619. p^.right^.value_set^[i] Then
  620. Begin
  621. b := false;
  622. Break
  623. End;
  624. t := genordinalconstnode(ord(b),booldef);
  625. End;
  626. {$EndIf NoSetInclusion}
  627. end;
  628. dispose(resultset);
  629. disposetree(p);
  630. p:=t;
  631. firstpass(p);
  632. exit;
  633. end
  634. else
  635. if psetdef(ld)^.settype=smallset then
  636. begin
  637. calcregisters(p,1,0,0);
  638. { are we adding set elements ? }
  639. if p^.right^.treetype=setelementn then
  640. begin
  641. { we need at least two registers PM }
  642. if p^.registers32<2 then
  643. p^.registers32:=2;
  644. end;
  645. p^.location.loc:=LOC_REGISTER;
  646. end
  647. else
  648. begin
  649. calcregisters(p,0,0,0);
  650. { here we call SET... }
  651. procinfo^.flags:=procinfo^.flags or pi_do_call;
  652. p^.location.loc:=LOC_MEM;
  653. end;
  654. convdone:=true;
  655. end
  656. else
  657. { is one of the operands a string?,
  658. chararrays are also handled as strings (after conversion) }
  659. if (rd^.deftype=stringdef) or (ld^.deftype=stringdef) or
  660. (is_chararray(rd) and is_chararray(ld)) then
  661. begin
  662. if is_widestring(rd) or is_widestring(ld) then
  663. begin
  664. if not(is_widestring(rd)) then
  665. p^.right:=gentypeconvnode(p^.right,cwidestringdef);
  666. if not(is_widestring(ld)) then
  667. p^.left:=gentypeconvnode(p^.left,cwidestringdef);
  668. p^.resulttype:=cwidestringdef;
  669. { this is only for add, the comparisaion is handled later }
  670. p^.location.loc:=LOC_REGISTER;
  671. end
  672. else if is_ansistring(rd) or is_ansistring(ld) then
  673. begin
  674. if not(is_ansistring(rd)) then
  675. p^.right:=gentypeconvnode(p^.right,cansistringdef);
  676. if not(is_ansistring(ld)) then
  677. p^.left:=gentypeconvnode(p^.left,cansistringdef);
  678. p^.resulttype:=cansistringdef;
  679. { this is only for add, the comparisaion is handled later }
  680. p^.location.loc:=LOC_REGISTER;
  681. end
  682. else if is_longstring(rd) or is_longstring(ld) then
  683. begin
  684. if not(is_longstring(rd)) then
  685. p^.right:=gentypeconvnode(p^.right,clongstringdef);
  686. if not(is_longstring(ld)) then
  687. p^.left:=gentypeconvnode(p^.left,clongstringdef);
  688. p^.resulttype:=clongstringdef;
  689. { this is only for add, the comparisaion is handled later }
  690. p^.location.loc:=LOC_MEM;
  691. end
  692. else
  693. begin
  694. if not(is_shortstring(rd)) then
  695. p^.right:=gentypeconvnode(p^.right,cshortstringdef);
  696. if not(is_shortstring(ld)) then
  697. p^.left:=gentypeconvnode(p^.left,cshortstringdef);
  698. p^.resulttype:=cshortstringdef;
  699. { this is only for add, the comparisaion is handled later }
  700. p^.location.loc:=LOC_MEM;
  701. end;
  702. { only if there is a type cast we need to do again }
  703. { the first pass }
  704. if p^.left^.treetype=typeconvn then
  705. firstpass(p^.left);
  706. if p^.right^.treetype=typeconvn then
  707. firstpass(p^.right);
  708. { here we call STRCONCAT or STRCMP or STRCOPY }
  709. procinfo^.flags:=procinfo^.flags or pi_do_call;
  710. if p^.location.loc=LOC_MEM then
  711. calcregisters(p,0,0,0)
  712. else
  713. calcregisters(p,1,0,0);
  714. convdone:=true;
  715. end
  716. else
  717. { is one a real float ? }
  718. if (rd^.deftype=floatdef) or (ld^.deftype=floatdef) then
  719. begin
  720. { if one is a fixed, then convert to f32bit }
  721. if ((rd^.deftype=floatdef) and (pfloatdef(rd)^.typ=f32bit)) or
  722. ((ld^.deftype=floatdef) and (pfloatdef(ld)^.typ=f32bit)) then
  723. begin
  724. if not is_integer(rd) or (p^.treetype<>muln) then
  725. p^.right:=gentypeconvnode(p^.right,s32fixeddef);
  726. if not is_integer(ld) or (p^.treetype<>muln) then
  727. p^.left:=gentypeconvnode(p^.left,s32fixeddef);
  728. firstpass(p^.left);
  729. firstpass(p^.right);
  730. calcregisters(p,1,0,0);
  731. p^.location.loc:=LOC_REGISTER;
  732. end
  733. else
  734. { convert both to bestreal }
  735. begin
  736. p^.right:=gentypeconvnode(p^.right,bestrealdef^);
  737. p^.left:=gentypeconvnode(p^.left,bestrealdef^);
  738. firstpass(p^.left);
  739. firstpass(p^.right);
  740. calcregisters(p,1,1,0);
  741. p^.location.loc:=LOC_FPU;
  742. end;
  743. convdone:=true;
  744. end
  745. else
  746. { pointer comperation and subtraction }
  747. if (rd^.deftype=pointerdef) and (ld^.deftype=pointerdef) then
  748. begin
  749. p^.location.loc:=LOC_REGISTER;
  750. { p^.right:=gentypeconvnode(p^.right,ld); }
  751. { firstpass(p^.right); }
  752. calcregisters(p,1,0,0);
  753. case p^.treetype of
  754. equaln,unequaln :
  755. begin
  756. if is_equal(p^.right^.resulttype,voidpointerdef) then
  757. begin
  758. p^.right:=gentypeconvnode(p^.right,ld);
  759. firstpass(p^.right);
  760. end
  761. else if is_equal(p^.left^.resulttype,voidpointerdef) then
  762. begin
  763. p^.left:=gentypeconvnode(p^.left,rd);
  764. firstpass(p^.left);
  765. end
  766. else if not(is_equal(ld,rd)) then
  767. CGMessage(type_e_mismatch);
  768. end;
  769. ltn,lten,gtn,gten:
  770. begin
  771. if is_equal(p^.right^.resulttype,voidpointerdef) then
  772. begin
  773. p^.right:=gentypeconvnode(p^.right,ld);
  774. firstpass(p^.right);
  775. end
  776. else if is_equal(p^.left^.resulttype,voidpointerdef) then
  777. begin
  778. p^.left:=gentypeconvnode(p^.left,rd);
  779. firstpass(p^.left);
  780. end
  781. else if not(is_equal(ld,rd)) then
  782. CGMessage(type_e_mismatch);
  783. if not(cs_extsyntax in aktmoduleswitches) then
  784. CGMessage(type_e_mismatch);
  785. end;
  786. subn:
  787. begin
  788. if not(is_equal(ld,rd)) then
  789. CGMessage(type_e_mismatch);
  790. if not(cs_extsyntax in aktmoduleswitches) then
  791. CGMessage(type_e_mismatch);
  792. p^.resulttype:=s32bitdef;
  793. exit;
  794. end;
  795. else CGMessage(type_e_mismatch);
  796. end;
  797. convdone:=true;
  798. end
  799. else
  800. if (rd^.deftype=objectdef) and (ld^.deftype=objectdef) and
  801. pobjectdef(rd)^.is_class and pobjectdef(ld)^.is_class then
  802. begin
  803. p^.location.loc:=LOC_REGISTER;
  804. if pobjectdef(rd)^.is_related(pobjectdef(ld)) then
  805. p^.right:=gentypeconvnode(p^.right,ld)
  806. else
  807. p^.left:=gentypeconvnode(p^.left,rd);
  808. firstpass(p^.right);
  809. firstpass(p^.left);
  810. calcregisters(p,1,0,0);
  811. case p^.treetype of
  812. equaln,unequaln : ;
  813. else CGMessage(type_e_mismatch);
  814. end;
  815. convdone:=true;
  816. end
  817. else
  818. if (rd^.deftype=classrefdef) and (ld^.deftype=classrefdef) then
  819. begin
  820. p^.location.loc:=LOC_REGISTER;
  821. if pobjectdef(pclassrefdef(rd)^.definition)^.is_related(pobjectdef(
  822. pclassrefdef(ld)^.definition)) then
  823. p^.right:=gentypeconvnode(p^.right,ld)
  824. else
  825. p^.left:=gentypeconvnode(p^.left,rd);
  826. firstpass(p^.right);
  827. firstpass(p^.left);
  828. calcregisters(p,1,0,0);
  829. case p^.treetype of
  830. equaln,unequaln : ;
  831. else CGMessage(type_e_mismatch);
  832. end;
  833. convdone:=true;
  834. end
  835. else
  836. { allows comperasion with nil pointer }
  837. if (rd^.deftype=objectdef) and
  838. pobjectdef(rd)^.is_class then
  839. begin
  840. p^.location.loc:=LOC_REGISTER;
  841. p^.left:=gentypeconvnode(p^.left,rd);
  842. firstpass(p^.left);
  843. calcregisters(p,1,0,0);
  844. case p^.treetype of
  845. equaln,unequaln : ;
  846. else CGMessage(type_e_mismatch);
  847. end;
  848. convdone:=true;
  849. end
  850. else
  851. if (ld^.deftype=objectdef) and
  852. pobjectdef(ld)^.is_class then
  853. begin
  854. p^.location.loc:=LOC_REGISTER;
  855. p^.right:=gentypeconvnode(p^.right,ld);
  856. firstpass(p^.right);
  857. calcregisters(p,1,0,0);
  858. case p^.treetype of
  859. equaln,unequaln : ;
  860. else CGMessage(type_e_mismatch);
  861. end;
  862. convdone:=true;
  863. end
  864. else
  865. if (rd^.deftype=classrefdef) then
  866. begin
  867. p^.left:=gentypeconvnode(p^.left,rd);
  868. firstpass(p^.left);
  869. calcregisters(p,1,0,0);
  870. case p^.treetype of
  871. equaln,unequaln : ;
  872. else CGMessage(type_e_mismatch);
  873. end;
  874. convdone:=true;
  875. end
  876. else
  877. if (ld^.deftype=classrefdef) then
  878. begin
  879. p^.right:=gentypeconvnode(p^.right,ld);
  880. firstpass(p^.right);
  881. calcregisters(p,1,0,0);
  882. case p^.treetype of
  883. equaln,unequaln : ;
  884. else
  885. CGMessage(type_e_mismatch);
  886. end;
  887. convdone:=true;
  888. end
  889. else
  890. { support procvar=nil,procvar<>nil }
  891. if ((ld^.deftype=procvardef) and (rt=niln)) or
  892. ((rd^.deftype=procvardef) and (lt=niln)) then
  893. begin
  894. calcregisters(p,1,0,0);
  895. p^.location.loc:=LOC_REGISTER;
  896. case p^.treetype of
  897. equaln,unequaln : ;
  898. else
  899. CGMessage(type_e_mismatch);
  900. end;
  901. convdone:=true;
  902. end
  903. else
  904. if (rd^.deftype=pointerdef) or
  905. is_zero_based_array(rd) then
  906. begin
  907. if is_zero_based_array(rd) then
  908. begin
  909. p^.resulttype:=new(ppointerdef,init(parraydef(rd)^.definition));
  910. p^.right:=gentypeconvnode(p^.right,p^.resulttype);
  911. firstpass(p^.right);
  912. end;
  913. p^.location.loc:=LOC_REGISTER;
  914. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  915. firstpass(p^.left);
  916. calcregisters(p,1,0,0);
  917. if p^.treetype=addn then
  918. begin
  919. if not(cs_extsyntax in aktmoduleswitches) or
  920. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  921. CGMessage(type_e_mismatch);
  922. { Dirty hack, to support multiple firstpasses (PFV) }
  923. if (p^.resulttype=nil) and
  924. (rd^.deftype=pointerdef) and
  925. (ppointerdef(rd)^.definition^.size>1) then
  926. begin
  927. p^.left:=gennode(muln,p^.left,genordinalconstnode(ppointerdef(rd)^.definition^.size,s32bitdef));
  928. firstpass(p^.left);
  929. end;
  930. end
  931. else
  932. CGMessage(type_e_mismatch);
  933. convdone:=true;
  934. end
  935. else
  936. if (ld^.deftype=pointerdef) or
  937. is_zero_based_array(ld) then
  938. begin
  939. if is_zero_based_array(ld) then
  940. begin
  941. p^.resulttype:=new(ppointerdef,init(parraydef(ld)^.definition));
  942. p^.left:=gentypeconvnode(p^.left,p^.resulttype);
  943. firstpass(p^.left);
  944. end;
  945. p^.location.loc:=LOC_REGISTER;
  946. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  947. firstpass(p^.right);
  948. calcregisters(p,1,0,0);
  949. case p^.treetype of
  950. addn,subn : begin
  951. if not(cs_extsyntax in aktmoduleswitches) or
  952. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  953. CGMessage(type_e_mismatch);
  954. { Dirty hack, to support multiple firstpasses (PFV) }
  955. if (p^.resulttype=nil) and
  956. (ld^.deftype=pointerdef) and
  957. (ppointerdef(ld)^.definition^.size>1) then
  958. begin
  959. p^.right:=gennode(muln,p^.right,
  960. genordinalconstnode(ppointerdef(ld)^.definition^.size,s32bitdef));
  961. firstpass(p^.right);
  962. end;
  963. end;
  964. else
  965. CGMessage(type_e_mismatch);
  966. end;
  967. convdone:=true;
  968. end
  969. else
  970. if (rd^.deftype=procvardef) and (ld^.deftype=procvardef) and is_equal(rd,ld) then
  971. begin
  972. calcregisters(p,1,0,0);
  973. p^.location.loc:=LOC_REGISTER;
  974. case p^.treetype of
  975. equaln,unequaln : ;
  976. else
  977. CGMessage(type_e_mismatch);
  978. end;
  979. convdone:=true;
  980. end
  981. else
  982. {$ifdef SUPPORT_MMX}
  983. if (cs_mmx in aktlocalswitches) and is_mmx_able_array(ld) and
  984. is_mmx_able_array(rd) and is_equal(ld,rd) then
  985. begin
  986. firstpass(p^.right);
  987. firstpass(p^.left);
  988. case p^.treetype of
  989. addn,subn,xorn,orn,andn:
  990. ;
  991. { mul is a little bit restricted }
  992. muln:
  993. if not(mmx_type(p^.left^.resulttype) in
  994. [mmxu16bit,mmxs16bit,mmxfixed16]) then
  995. CGMessage(type_e_mismatch);
  996. else
  997. CGMessage(type_e_mismatch);
  998. end;
  999. p^.location.loc:=LOC_MMXREGISTER;
  1000. calcregisters(p,0,0,1);
  1001. convdone:=true;
  1002. end
  1003. else
  1004. {$endif SUPPORT_MMX}
  1005. if (ld^.deftype=enumdef) and (rd^.deftype=enumdef) and (is_equal(ld,rd)) then
  1006. begin
  1007. calcregisters(p,1,0,0);
  1008. case p^.treetype of
  1009. equaln,unequaln,
  1010. ltn,lten,gtn,gten : ;
  1011. else CGMessage(type_e_mismatch);
  1012. end;
  1013. convdone:=true;
  1014. end;
  1015. { the general solution is to convert to 32 bit int }
  1016. if not convdone then
  1017. begin
  1018. { but an int/int gives real/real! }
  1019. if p^.treetype=slashn then
  1020. begin
  1021. CGMessage(type_h_use_div_for_int);
  1022. p^.right:=gentypeconvnode(p^.right,bestrealdef^);
  1023. p^.left:=gentypeconvnode(p^.left,bestrealdef^);
  1024. firstpass(p^.left);
  1025. firstpass(p^.right);
  1026. { maybe we need an integer register to save }
  1027. { a reference }
  1028. if ((p^.left^.location.loc<>LOC_FPU) or
  1029. (p^.right^.location.loc<>LOC_FPU)) and
  1030. (p^.left^.registers32=p^.right^.registers32) then
  1031. calcregisters(p,1,1,0)
  1032. else
  1033. calcregisters(p,0,1,0);
  1034. p^.location.loc:=LOC_FPU;
  1035. end
  1036. else
  1037. begin
  1038. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  1039. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  1040. firstpass(p^.left);
  1041. firstpass(p^.right);
  1042. calcregisters(p,1,0,0);
  1043. p^.location.loc:=LOC_REGISTER;
  1044. end;
  1045. end;
  1046. if codegenerror then
  1047. exit;
  1048. { determines result type for comparions }
  1049. { here the is a problem with multiple passes }
  1050. { example length(s)+1 gets internal 'longint' type first }
  1051. { if it is a arg it is converted to 'LONGINT' }
  1052. { but a second first pass will reset this to 'longint' }
  1053. case p^.treetype of
  1054. ltn,lten,gtn,gten,equaln,unequaln:
  1055. begin
  1056. if (not assigned(p^.resulttype)) or
  1057. (p^.resulttype^.deftype=stringdef) then
  1058. p^.resulttype:=booldef;
  1059. if is_64bitint(p^.left^.resulttype) then
  1060. p^.location.loc:=LOC_JUMP
  1061. else
  1062. p^.location.loc:=LOC_FLAGS;
  1063. end;
  1064. xorn:
  1065. begin
  1066. if not assigned(p^.resulttype) then
  1067. p^.resulttype:=p^.left^.resulttype;
  1068. p^.location.loc:=LOC_REGISTER;
  1069. end;
  1070. addn:
  1071. begin
  1072. if not assigned(p^.resulttype) then
  1073. begin
  1074. { for strings, return is always a 255 char string }
  1075. if is_shortstring(p^.left^.resulttype) then
  1076. p^.resulttype:=cshortstringdef
  1077. else
  1078. p^.resulttype:=p^.left^.resulttype;
  1079. end;
  1080. end;
  1081. else
  1082. p^.resulttype:=p^.left^.resulttype;
  1083. end;
  1084. end;
  1085. end.
  1086. {
  1087. $Log$
  1088. Revision 1.51 1999-11-06 14:34:29 peter
  1089. * truncated log to 20 revs
  1090. Revision 1.50 1999/09/27 23:45:00 peter
  1091. * procinfo is now a pointer
  1092. * support for result setting in sub procedure
  1093. Revision 1.49 1999/09/16 13:39:14 peter
  1094. * arrayconstructor 2 set conversion is now called always in the
  1095. beginning of firstadd
  1096. Revision 1.48 1999/09/15 20:35:45 florian
  1097. * small fix to operator overloading when in MMX mode
  1098. + the compiler uses now fldz and fld1 if possible
  1099. + some fixes to floating point registers
  1100. + some math. functions (arctan, ln, sin, cos, sqrt, sqr, pi) are now inlined
  1101. * .... ???
  1102. Revision 1.47 1999/09/13 16:28:05 peter
  1103. * typo in previous commit open_array -> chararray :(
  1104. Revision 1.46 1999/09/10 15:40:46 peter
  1105. * fixed array check for operators, becuase array can also be a set
  1106. Revision 1.45 1999/09/08 16:05:29 peter
  1107. * pointer add/sub is now as expected and the same results as inc/dec
  1108. Revision 1.44 1999/09/07 07:52:19 peter
  1109. * > < >= <= support for boolean
  1110. * boolean constants are now calculated like integer constants
  1111. Revision 1.43 1999/08/23 23:44:05 pierre
  1112. * setelementn registers32 corrected
  1113. Revision 1.42 1999/08/07 11:29:27 peter
  1114. * better fix for muln register allocation
  1115. Revision 1.41 1999/08/05 21:58:57 peter
  1116. * fixed register count ord*ord
  1117. Revision 1.40 1999/08/04 13:03:13 jonas
  1118. * all tokens now start with an underscore
  1119. * PowerPC compiles!!
  1120. Revision 1.39 1999/08/04 00:23:33 florian
  1121. * renamed i386asm and i386base to cpuasm and cpubase
  1122. Revision 1.38 1999/08/03 22:03:24 peter
  1123. * moved bitmask constants to sets
  1124. * some other type/const renamings
  1125. Revision 1.37 1999/07/16 10:04:37 peter
  1126. * merged
  1127. Revision 1.36 1999/06/17 15:32:48 pierre
  1128. * merged from 0-99-12 branch
  1129. Revision 1.34.2.3 1999/07/16 09:54:58 peter
  1130. * @procvar support in tp7 mode works again
  1131. Revision 1.34.2.2 1999/06/17 15:25:07 pierre
  1132. * for arrays of char operators can not be overloaded
  1133. Revision 1.35 1999/06/17 13:19:57 pierre
  1134. * merged from 0_99_12 branch
  1135. Revision 1.34.2.1 1999/06/17 12:35:23 pierre
  1136. * allow array binary operator overloading if not with orddef
  1137. Revision 1.34 1999/06/02 10:11:52 florian
  1138. * make cycle fixed i.e. compilation with 0.99.10
  1139. * some fixes for qword
  1140. * start of register calling conventions
  1141. }