tcadd.pas 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  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) then
  1144. begin
  1145. if not(is_equal(ld,rd)) then
  1146. begin
  1147. p^.right:=gentypeconvnode(p^.right,ld);
  1148. firstpass(p^.right);
  1149. end;
  1150. calcregisters(p,1,0,0);
  1151. case p^.treetype of
  1152. equaln,unequaln,
  1153. ltn,lten,gtn,gten : ;
  1154. else CGMessage(type_e_mismatch);
  1155. end;
  1156. convdone:=true;
  1157. end;
  1158. { the general solution is to convert to 32 bit int }
  1159. if not convdone then
  1160. begin
  1161. { but an int/int gives real/real! }
  1162. if p^.treetype=slashn then
  1163. begin
  1164. CGMessage(type_h_use_div_for_int);
  1165. p^.right:=gentypeconvnode(p^.right,bestrealdef^);
  1166. p^.left:=gentypeconvnode(p^.left,bestrealdef^);
  1167. firstpass(p^.left);
  1168. firstpass(p^.right);
  1169. { maybe we need an integer register to save }
  1170. { a reference }
  1171. if ((p^.left^.location.loc<>LOC_FPU) or
  1172. (p^.right^.location.loc<>LOC_FPU)) and
  1173. (p^.left^.registers32=p^.right^.registers32) then
  1174. calcregisters(p,1,1,0)
  1175. else
  1176. calcregisters(p,0,1,0);
  1177. p^.location.loc:=LOC_FPU;
  1178. end
  1179. else
  1180. begin
  1181. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  1182. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  1183. firstpass(p^.left);
  1184. firstpass(p^.right);
  1185. calcregisters(p,1,0,0);
  1186. p^.location.loc:=LOC_REGISTER;
  1187. end;
  1188. end;
  1189. if codegenerror then
  1190. exit;
  1191. { determines result type for comparions }
  1192. { here the is a problem with multiple passes }
  1193. { example length(s)+1 gets internal 'longint' type first }
  1194. { if it is a arg it is converted to 'LONGINT' }
  1195. { but a second first pass will reset this to 'longint' }
  1196. case p^.treetype of
  1197. ltn,lten,gtn,gten,equaln,unequaln:
  1198. begin
  1199. if (not assigned(p^.resulttype)) or
  1200. (p^.resulttype^.deftype=stringdef) then
  1201. p^.resulttype:=booldef;
  1202. if is_64bitint(p^.left^.resulttype) then
  1203. p^.location.loc:=LOC_JUMP
  1204. else
  1205. p^.location.loc:=LOC_FLAGS;
  1206. end;
  1207. xorn:
  1208. begin
  1209. if not assigned(p^.resulttype) then
  1210. p^.resulttype:=p^.left^.resulttype;
  1211. p^.location.loc:=LOC_REGISTER;
  1212. end;
  1213. addn:
  1214. begin
  1215. if not assigned(p^.resulttype) then
  1216. begin
  1217. { for strings, return is always a 255 char string }
  1218. if is_shortstring(p^.left^.resulttype) then
  1219. p^.resulttype:=cshortstringdef
  1220. else
  1221. p^.resulttype:=p^.left^.resulttype;
  1222. end;
  1223. end;
  1224. {$ifdef cardinalmulfix}
  1225. muln:
  1226. { if we multiply an unsigned with a signed number, the result is signed }
  1227. { in the other cases, the result remains signed or unsigned depending on }
  1228. { the multiplication factors (JM) }
  1229. if (p^.left^.resulttype^.deftype = orddef) and
  1230. (p^.right^.resulttype^.deftype = orddef) and
  1231. is_signed(p^.right^.resulttype) then
  1232. p^.resulttype := p^.right^.resulttype
  1233. else p^.resulttype := p^.left^.resulttype;
  1234. (*
  1235. subn:
  1236. { if we substract a u32bit from a positive constant, the result becomes }
  1237. { s32bit as well (JM) }
  1238. begin
  1239. if (p^.right^.resulttype^.deftype = orddef) and
  1240. (p^.left^.resulttype^.deftype = orddef) and
  1241. (porddef(p^.right^.resulttype)^.typ = u32bit) and
  1242. is_constintnode(p^.left) and
  1243. { (porddef(p^.left^.resulttype)^.typ <> u32bit) and}
  1244. (p^.left^.value > 0) then
  1245. begin
  1246. p^.left := gentypeconvnode(p^.left,u32bitdef);
  1247. firstpass(p^.left);
  1248. end;
  1249. p^.resulttype:=p^.left^.resulttype;
  1250. end;
  1251. *)
  1252. {$endif cardinalmulfix}
  1253. else
  1254. p^.resulttype:=p^.left^.resulttype;
  1255. end;
  1256. end;
  1257. end.
  1258. {
  1259. $Log$
  1260. Revision 1.78 2000-05-31 06:58:41 florian
  1261. * forgot to commit a fix for the enumeration subrange problem, yesterday
  1262. Revision 1.77 2000/05/11 17:53:40 peter
  1263. * small fix for previous commit
  1264. Revision 1.76 2000/05/11 16:47:37 peter
  1265. * fixed check for overloaded operator with array and chararray check
  1266. Revision 1.75 2000/04/25 14:43:36 jonas
  1267. - disabled "string_var := string_var + ... " and "string_var + char_var"
  1268. optimizations (were only active with -dnewoptimizations) because of
  1269. several internal issues
  1270. Revision 1.74 2000/04/21 12:35:05 jonas
  1271. + special code for string + char, between -dnewoptimizations
  1272. Revision 1.73 2000/03/28 21:14:18 pierre
  1273. * fix for bug 891
  1274. Revision 1.72 2000/03/20 10:16:51 florian
  1275. * fixed <dword>/<dword>, <int64>/<int64> and <qword>/<qword>
  1276. Revision 1.71 2000/03/18 15:01:19 jonas
  1277. * moved a $maxfpuregisters directive a bit up because it was being
  1278. ignored
  1279. Revision 1.70 2000/02/19 10:12:48 florian
  1280. * fixed one more internalerror 10
  1281. Revision 1.69 2000/02/17 14:53:42 florian
  1282. * some updates for the newcg
  1283. Revision 1.68 2000/02/14 22:34:28 florian
  1284. * fixed another internalerror
  1285. Revision 1.67 2000/02/13 22:46:28 florian
  1286. * fixed an internalerror with writeln
  1287. * fixed arrayconstructor_to_set to force the generation of better code
  1288. and added a more strict type checking
  1289. Revision 1.66 2000/02/13 14:21:51 jonas
  1290. * modifications to make the compiler functional when compiled with
  1291. -Or
  1292. Revision 1.65 2000/02/09 13:23:06 peter
  1293. * log truncated
  1294. Revision 1.64 2000/02/04 08:47:10 florian
  1295. * better register variable allocation in -Or mode
  1296. Revision 1.63 2000/01/07 01:14:43 peter
  1297. * updated copyright to 2000
  1298. Revision 1.62 2000/01/04 20:10:20 florian
  1299. * mmx support fixed
  1300. Revision 1.61 1999/12/11 18:53:31 jonas
  1301. * fixed type conversions of results of operations with cardinals
  1302. (between -dcardinalmulfix)
  1303. Revision 1.60 1999/12/09 23:18:04 pierre
  1304. * no_fast_exit if procedure contains implicit termination code
  1305. Revision 1.59 1999/12/01 12:42:33 peter
  1306. * fixed bug 698
  1307. * removed some notes about unused vars
  1308. Revision 1.58 1999/11/30 10:40:56 peter
  1309. + ttype, tsymlist
  1310. Revision 1.57 1999/11/26 13:51:29 pierre
  1311. * fix for overloading of shr shl mod and div
  1312. Revision 1.56 1999/11/18 15:34:48 pierre
  1313. * Notes/Hints for local syms changed to
  1314. Set_varstate function
  1315. Revision 1.55 1999/11/17 17:05:06 pierre
  1316. * Notes/hints changes
  1317. Revision 1.54 1999/11/16 23:45:28 pierre
  1318. * global var token was changed by overload code (form bug 707)
  1319. Revision 1.53 1999/11/15 21:53:42 peter
  1320. * fixed constant eval for bool xor/or/and bool
  1321. Revision 1.52 1999/11/15 17:53:00 pierre
  1322. + one field added for ttoken record for operator
  1323. linking the id to the corresponding operator token that
  1324. can now now all be overloaded
  1325. * overloaded operators are resetted to nil in InitSymtable
  1326. (bug when trying to compile a uint that overloads operators twice)
  1327. Revision 1.51 1999/11/06 14:34:29 peter
  1328. * truncated log to 20 revs
  1329. Revision 1.50 1999/09/27 23:45:00 peter
  1330. * procinfo is now a pointer
  1331. * support for result setting in sub procedure
  1332. Revision 1.49 1999/09/16 13:39:14 peter
  1333. * arrayconstructor 2 set conversion is now called always in the
  1334. beginning of firstadd
  1335. Revision 1.48 1999/09/15 20:35:45 florian
  1336. * small fix to operator overloading when in MMX mode
  1337. + the compiler uses now fldz and fld1 if possible
  1338. + some fixes to floating point registers
  1339. + some math. functions (arctan, ln, sin, cos, sqrt, sqr, pi) are now inlined
  1340. * .... ???
  1341. Revision 1.47 1999/09/13 16:28:05 peter
  1342. * typo in previous commit open_array -> chararray :(
  1343. Revision 1.46 1999/09/10 15:40:46 peter
  1344. * fixed array check for operators, becuase array can also be a set
  1345. Revision 1.45 1999/09/08 16:05:29 peter
  1346. * pointer add/sub is now as expected and the same results as inc/dec
  1347. }