tcadd.pas 55 KB

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