tcadd.pas 55 KB

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