ncnv.pas 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  1. {
  2. $Id$
  3. Copyright (c) 2000 by Florian Klaempfl
  4. Type checking and register allocation for type converting nodes
  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 ncnv;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. node,
  23. symtype,types,
  24. nld;
  25. type
  26. ttypeconvnode = class(tunarynode)
  27. convtype : tconverttype;
  28. constructor create(node : tnode;t : pdef);virtual;
  29. function getcopy : tnode;override;
  30. function pass_1 : tnode;override;
  31. function first_int_to_int : tnode;virtual;
  32. function first_cstring_to_pchar : tnode;virtual;
  33. function first_string_to_chararray : tnode;virtual;
  34. function first_string_to_string : tnode;virtual;
  35. function first_char_to_string : tnode;virtual;
  36. function first_nothing : tnode;virtual;
  37. function first_array_to_pointer : tnode;virtual;
  38. function first_int_to_real : tnode;virtual;
  39. function first_int_to_fix : tnode;virtual;
  40. function first_real_to_fix : tnode;virtual;
  41. function first_fix_to_real : tnode;virtual;
  42. function first_real_to_real : tnode;virtual;
  43. function first_pointer_to_array : tnode;virtual;
  44. function first_chararray_to_string : tnode;virtual;
  45. function first_cchar_to_pchar : tnode;virtual;
  46. function first_bool_to_int : tnode;virtual;
  47. function first_int_to_bool : tnode;virtual;
  48. function first_bool_to_bool : tnode;virtual;
  49. function first_proc_to_procvar : tnode;virtual;
  50. function first_load_smallset : tnode;virtual;
  51. function first_cord_to_pointer : tnode;virtual;
  52. function first_pchar_to_string : tnode;virtual;
  53. function first_ansistring_to_pchar : tnode;virtual;
  54. function first_arrayconstructor_to_set : tnode;virtual;
  55. function first_class_to_intf : tnode;virtual;
  56. function first_call_helper(c : tconverttype) : tnode;
  57. function docompare(p: tnode) : boolean; override;
  58. end;
  59. tasnode = class(tbinarynode)
  60. constructor create(l,r : tnode);virtual;
  61. function pass_1 : tnode;override;
  62. end;
  63. tisnode = class(tbinarynode)
  64. constructor create(l,r : tnode);virtual;
  65. function pass_1 : tnode;override;
  66. end;
  67. var
  68. ctypeconvnode : class of ttypeconvnode;
  69. casnode : class of tasnode;
  70. cisnode : class of tisnode;
  71. function gentypeconvnode(node : tnode;t : pdef) : ttypeconvnode;
  72. procedure arrayconstructor_to_set(var p : tarrayconstructornode);
  73. implementation
  74. uses
  75. globtype,systems,tokens,
  76. cutils,verbose,globals,
  77. symconst,symdef,symsym,symtable,
  78. ncon,ncal,nset,nadd,
  79. {$ifdef newcg}
  80. cgbase,
  81. {$else newcg}
  82. hcodegen,
  83. {$endif newcg}
  84. htypechk,pass_1,cpubase;
  85. function gentypeconvnode(node : tnode;t : pdef) : ttypeconvnode;
  86. begin
  87. gentypeconvnode:=ctypeconvnode.create(node,t);
  88. end;
  89. {*****************************************************************************
  90. Array constructor to Set Conversion
  91. *****************************************************************************}
  92. procedure arrayconstructor_to_set(var p : tarrayconstructornode);
  93. var
  94. constp : tsetconstnode;
  95. buildp,
  96. p2,p3,p4 : tnode;
  97. pd : pdef;
  98. constset : pconstset;
  99. constsetlo,
  100. constsethi : longint;
  101. procedure update_constsethi(p:pdef);
  102. begin
  103. if ((p^.deftype=orddef) and
  104. (porddef(p)^.high>=constsethi)) then
  105. begin
  106. constsethi:=porddef(p)^.high;
  107. if pd=nil then
  108. begin
  109. if (constsethi>255) or
  110. (porddef(p)^.low<0) then
  111. pd:=u8bitdef
  112. else
  113. pd:=p;
  114. end;
  115. if constsethi>255 then
  116. constsethi:=255;
  117. end
  118. else if ((p^.deftype=enumdef) and
  119. (penumdef(p)^.max>=constsethi)) then
  120. begin
  121. if pd=nil then
  122. pd:=p;
  123. constsethi:=penumdef(p)^.max;
  124. end;
  125. end;
  126. procedure do_set(pos : longint);
  127. var
  128. mask,l : longint;
  129. begin
  130. if (pos>255) or (pos<0) then
  131. Message(parser_e_illegal_set_expr);
  132. if pos>constsethi then
  133. constsethi:=pos;
  134. if pos<constsetlo then
  135. constsetlo:=pos;
  136. l:=pos shr 3;
  137. mask:=1 shl (pos mod 8);
  138. { do we allow the same twice }
  139. if (constset^[l] and mask)<>0 then
  140. Message(parser_e_illegal_set_expr);
  141. constset^[l]:=constset^[l] or mask;
  142. end;
  143. var
  144. l : longint;
  145. lr,hr : longint;
  146. begin
  147. new(constset);
  148. FillChar(constset^,sizeof(constset^),0);
  149. pd:=nil;
  150. constsetlo:=0;
  151. constsethi:=0;
  152. constp:=csetconstnode.create(nil,nil);
  153. constp.value_set:=constset;
  154. buildp:=constp;
  155. if assigned(p.left) then
  156. begin
  157. while assigned(p) do
  158. begin
  159. p4:=nil; { will contain the tree to create the set }
  160. {split a range into p2 and p3 }
  161. if p.left.nodetype=arrayconstructorrangen then
  162. begin
  163. p2:=tarrayconstructorrangenode(p.left).left;
  164. p3:=tarrayconstructorrangenode(p.left).right;
  165. tarrayconstructorrangenode(p.left).left:=nil;
  166. tarrayconstructorrangenode(p.left).right:=nil;
  167. end
  168. else
  169. begin
  170. p2:=p.left;
  171. p.left:=nil;
  172. p3:=nil;
  173. end;
  174. firstpass(p2);
  175. if assigned(p3) then
  176. firstpass(p3);
  177. if codegenerror then
  178. break;
  179. case p2.resulttype^.deftype of
  180. enumdef,
  181. orddef:
  182. begin
  183. getrange(p2.resulttype,lr,hr);
  184. if assigned(p3) then
  185. begin
  186. { this isn't good, you'll get problems with
  187. type t010 = 0..10;
  188. ts = set of t010;
  189. var s : ts;b : t010
  190. begin s:=[1,2,b]; end.
  191. if is_integer(p3^.resulttype) then
  192. begin
  193. p3:=gentypeconvnode(p3,u8bitdef);
  194. firstpass(p3);
  195. end;
  196. }
  197. if assigned(pd) and not(is_equal(pd,p3.resulttype)) then
  198. begin
  199. aktfilepos:=p3.fileinfo;
  200. CGMessage(type_e_typeconflict_in_set);
  201. end
  202. else
  203. begin
  204. if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
  205. begin
  206. if not(is_integer(p3.resulttype)) then
  207. pd:=p3.resulttype
  208. else
  209. begin
  210. p3:=gentypeconvnode(p3,u8bitdef);
  211. p2:=gentypeconvnode(p2,u8bitdef);
  212. firstpass(p2);
  213. firstpass(p3);
  214. end;
  215. for l:=tordconstnode(p2).value to tordconstnode(p3).value do
  216. do_set(l);
  217. p2.free;
  218. p3.free;
  219. end
  220. else
  221. begin
  222. update_constsethi(p2.resulttype);
  223. p2:=gentypeconvnode(p2,pd);
  224. firstpass(p2);
  225. update_constsethi(p3.resulttype);
  226. p3:=gentypeconvnode(p3,pd);
  227. firstpass(p3);
  228. if assigned(pd) then
  229. p3:=gentypeconvnode(p3,pd)
  230. else
  231. p3:=gentypeconvnode(p3,u8bitdef);
  232. firstpass(p3);
  233. p4:=csetelementnode.create(p2,p3);
  234. end;
  235. end;
  236. end
  237. else
  238. begin
  239. { Single value }
  240. if p2.nodetype=ordconstn then
  241. begin
  242. if not(is_integer(p2.resulttype)) then
  243. update_constsethi(p2.resulttype)
  244. else
  245. begin
  246. p2:=gentypeconvnode(p2,u8bitdef);
  247. firstpass(p2);
  248. end;
  249. do_set(tordconstnode(p2).value);
  250. p2.free;
  251. end
  252. else
  253. begin
  254. update_constsethi(p2.resulttype);
  255. if assigned(pd) then
  256. p2:=gentypeconvnode(p2,pd)
  257. else
  258. p2:=gentypeconvnode(p2,u8bitdef);
  259. firstpass(p2);
  260. p4:=csetelementnode.create(p2,nil);
  261. end;
  262. end;
  263. end;
  264. stringdef : begin
  265. { if we've already set elements which are constants }
  266. { throw an error }
  267. if ((pd=nil) and assigned(buildp)) or
  268. not(is_equal(pd,cchardef)) then
  269. CGMessage(type_e_typeconflict_in_set)
  270. else
  271. for l:=1 to length(pstring(tstringconstnode(p2).value_str)^) do
  272. do_set(ord(pstring(tstringconstnode(p2).value_str)^[l]));
  273. if pd=nil then
  274. pd:=cchardef;
  275. p2.free;
  276. end;
  277. else
  278. CGMessage(type_e_ordinal_expr_expected);
  279. end;
  280. { insert the set creation tree }
  281. if assigned(p4) then
  282. buildp:=caddnode.create(addn,buildp,p4);
  283. { load next and dispose current node }
  284. p2:=p;
  285. p:=tarrayconstructornode(tarrayconstructornode(p2).right);
  286. tarrayconstructornode(p2).right:=nil;
  287. p2.free;
  288. end;
  289. if (pd=nil) then
  290. begin
  291. pd:=u8bitdef;
  292. constsethi:=255;
  293. end;
  294. end
  295. else
  296. begin
  297. { empty set [], only remove node }
  298. p.free;
  299. end;
  300. { set the initial set type }
  301. constp.resulttype:=new(psetdef,init(pd,constsethi));
  302. { set the new tree }
  303. p:=tarrayconstructornode(buildp);
  304. end;
  305. {*****************************************************************************
  306. TTYPECONVNODE
  307. *****************************************************************************}
  308. constructor ttypeconvnode.create(node : tnode;t : pdef);
  309. begin
  310. inherited create(typeconvn,node);
  311. convtype:=tc_not_possible;
  312. resulttype:=t;
  313. set_file_line(node);
  314. end;
  315. function ttypeconvnode.getcopy : tnode;
  316. var
  317. n : ttypeconvnode;
  318. begin
  319. n:=ttypeconvnode(inherited getcopy);
  320. n.convtype:=convtype;
  321. getcopy:=n;
  322. end;
  323. function ttypeconvnode.first_int_to_int : tnode;
  324. begin
  325. first_int_to_int:=nil;
  326. if (left.location.loc<>LOC_REGISTER) and
  327. (resulttype^.size>left.resulttype^.size) then
  328. location.loc:=LOC_REGISTER;
  329. if is_64bitint(resulttype) then
  330. registers32:=max(registers32,2)
  331. else
  332. registers32:=max(registers32,1);
  333. end;
  334. function ttypeconvnode.first_cstring_to_pchar : tnode;
  335. begin
  336. first_cstring_to_pchar:=nil;
  337. registers32:=1;
  338. location.loc:=LOC_REGISTER;
  339. end;
  340. function ttypeconvnode.first_string_to_chararray : tnode;
  341. begin
  342. first_string_to_chararray:=nil;
  343. registers32:=1;
  344. location.loc:=LOC_REGISTER;
  345. end;
  346. function ttypeconvnode.first_string_to_string : tnode;
  347. begin
  348. first_string_to_string:=nil;
  349. if pstringdef(resulttype)^.string_typ<>
  350. pstringdef(left.resulttype)^.string_typ then
  351. begin
  352. if left.nodetype=stringconstn then
  353. begin
  354. tstringconstnode(left).stringtype:=pstringdef(resulttype)^.string_typ;
  355. tstringconstnode(left).resulttype:=resulttype;
  356. { remove typeconv node }
  357. first_string_to_string:=left;
  358. left:=nil;
  359. exit;
  360. end
  361. else
  362. procinfo^.flags:=procinfo^.flags or pi_do_call;
  363. end;
  364. { for simplicity lets first keep all ansistrings
  365. as LOC_MEM, could also become LOC_REGISTER }
  366. if pstringdef(resulttype)^.string_typ in [st_ansistring,st_widestring] then
  367. { we may use ansistrings so no fast exit here }
  368. procinfo^.no_fast_exit:=true;
  369. location.loc:=LOC_MEM;
  370. end;
  371. function ttypeconvnode.first_char_to_string : tnode;
  372. var
  373. hp : tstringconstnode;
  374. begin
  375. first_char_to_string:=nil;
  376. if left.nodetype=ordconstn then
  377. begin
  378. hp:=genstringconstnode(chr(tordconstnode(left).value),st_default);
  379. hp.stringtype:=pstringdef(resulttype)^.string_typ;
  380. firstpass(hp);
  381. first_char_to_string:=hp;
  382. end
  383. else
  384. location.loc:=LOC_MEM;
  385. end;
  386. function ttypeconvnode.first_nothing : tnode;
  387. begin
  388. first_nothing:=nil;
  389. location.loc:=LOC_MEM;
  390. end;
  391. function ttypeconvnode.first_array_to_pointer : tnode;
  392. begin
  393. first_array_to_pointer:=nil;
  394. if registers32<1 then
  395. registers32:=1;
  396. location.loc:=LOC_REGISTER;
  397. end;
  398. function ttypeconvnode.first_int_to_real : tnode;
  399. var
  400. t : trealconstnode;
  401. begin
  402. first_int_to_real:=nil;
  403. if left.nodetype=ordconstn then
  404. begin
  405. t:=genrealconstnode(tordconstnode(left).value,pfloatdef(resulttype));
  406. firstpass(t);
  407. first_int_to_real:=t;
  408. exit;
  409. end;
  410. if registersfpu<1 then
  411. registersfpu:=1;
  412. location.loc:=LOC_FPU;
  413. end;
  414. function ttypeconvnode.first_int_to_fix : tnode;
  415. var
  416. t : tnode;
  417. begin
  418. first_int_to_fix:=nil;
  419. if left.nodetype=ordconstn then
  420. begin
  421. t:=genfixconstnode(tordconstnode(left).value shl 16,resulttype);
  422. firstpass(t);
  423. first_int_to_fix:=t;
  424. exit;
  425. end;
  426. if registers32<1 then
  427. registers32:=1;
  428. location.loc:=LOC_REGISTER;
  429. end;
  430. function ttypeconvnode.first_real_to_fix : tnode;
  431. var
  432. t : tnode;
  433. begin
  434. first_real_to_fix:=nil;
  435. if left.nodetype=realconstn then
  436. begin
  437. t:=genfixconstnode(round(trealconstnode(left).value_real*65536),resulttype);
  438. firstpass(t);
  439. first_real_to_fix:=t;
  440. exit;
  441. end;
  442. { at least one fpu and int register needed }
  443. if registers32<1 then
  444. registers32:=1;
  445. if registersfpu<1 then
  446. registersfpu:=1;
  447. location.loc:=LOC_REGISTER;
  448. end;
  449. function ttypeconvnode.first_fix_to_real : tnode;
  450. var
  451. t : tnode;
  452. begin
  453. first_fix_to_real:=nil;
  454. if left.nodetype=fixconstn then
  455. begin
  456. t:=genrealconstnode(round(tfixconstnode(left).value_fix/65536.0),resulttype);
  457. firstpass(t);
  458. first_fix_to_real:=t;
  459. exit;
  460. end;
  461. if registersfpu<1 then
  462. registersfpu:=1;
  463. location.loc:=LOC_FPU;
  464. end;
  465. function ttypeconvnode.first_real_to_real : tnode;
  466. var
  467. t : tnode;
  468. begin
  469. first_real_to_real:=nil;
  470. if left.nodetype=realconstn then
  471. begin
  472. t:=genrealconstnode(trealconstnode(left).value_real,resulttype);
  473. firstpass(t);
  474. first_real_to_real:=t;
  475. exit;
  476. end;
  477. { comp isn't a floating type }
  478. {$ifdef i386}
  479. if (pfloatdef(resulttype)^.typ=s64comp) and
  480. (pfloatdef(left.resulttype)^.typ<>s64comp) and
  481. not (nf_explizit in flags) then
  482. CGMessage(type_w_convert_real_2_comp);
  483. {$endif}
  484. if registersfpu<1 then
  485. registersfpu:=1;
  486. location.loc:=LOC_FPU;
  487. end;
  488. function ttypeconvnode.first_pointer_to_array : tnode;
  489. begin
  490. first_pointer_to_array:=nil;
  491. if registers32<1 then
  492. registers32:=1;
  493. location.loc:=LOC_REFERENCE;
  494. end;
  495. function ttypeconvnode.first_chararray_to_string : tnode;
  496. begin
  497. first_chararray_to_string:=nil;
  498. { the only important information is the location of the }
  499. { result }
  500. { other stuff is done by firsttypeconv }
  501. location.loc:=LOC_MEM;
  502. end;
  503. function ttypeconvnode.first_cchar_to_pchar : tnode;
  504. begin
  505. first_cchar_to_pchar:=nil;
  506. left:=gentypeconvnode(left,cshortstringdef);
  507. { convert constant char to constant string }
  508. firstpass(left);
  509. { evalute tree }
  510. first_cchar_to_pchar:=pass_1;
  511. end;
  512. function ttypeconvnode.first_bool_to_int : tnode;
  513. begin
  514. first_bool_to_int:=nil;
  515. { byte(boolean) or word(wordbool) or longint(longbool) must
  516. be accepted for var parameters }
  517. if (nf_explizit in flags) and
  518. (left.resulttype^.size=resulttype^.size) and
  519. (left.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  520. exit;
  521. location.loc:=LOC_REGISTER;
  522. if registers32<1 then
  523. registers32:=1;
  524. end;
  525. function ttypeconvnode.first_int_to_bool : tnode;
  526. begin
  527. first_int_to_bool:=nil;
  528. { byte(boolean) or word(wordbool) or longint(longbool) must
  529. be accepted for var parameters }
  530. if (nf_explizit in flags) and
  531. (left.resulttype^.size=resulttype^.size) and
  532. (left.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  533. exit;
  534. location.loc:=LOC_REGISTER;
  535. { need if bool to bool !!
  536. not very nice !!
  537. left:=gentypeconvnode(left,s32bitdef);
  538. left.explizit:=true;
  539. firstpass(left); }
  540. if registers32<1 then
  541. registers32:=1;
  542. end;
  543. function ttypeconvnode.first_bool_to_bool : tnode;
  544. begin
  545. first_bool_to_bool:=nil;
  546. location.loc:=LOC_REGISTER;
  547. if registers32<1 then
  548. registers32:=1;
  549. end;
  550. function ttypeconvnode.first_proc_to_procvar : tnode;
  551. begin
  552. first_proc_to_procvar:=nil;
  553. { hmmm, I'am not sure if that is necessary (FK) }
  554. firstpass(left);
  555. if codegenerror then
  556. exit;
  557. if (left.location.loc<>LOC_REFERENCE) then
  558. CGMessage(cg_e_illegal_expression);
  559. registers32:=left.registers32;
  560. if registers32<1 then
  561. registers32:=1;
  562. location.loc:=LOC_REGISTER;
  563. end;
  564. function ttypeconvnode.first_load_smallset : tnode;
  565. begin
  566. first_load_smallset:=nil;
  567. end;
  568. function ttypeconvnode.first_cord_to_pointer : tnode;
  569. var
  570. t : tnode;
  571. begin
  572. first_cord_to_pointer:=nil;
  573. if left.nodetype=ordconstn then
  574. begin
  575. t:=genpointerconstnode(tordconstnode(left).value,resulttype);
  576. firstpass(t);
  577. first_cord_to_pointer:=t;
  578. exit;
  579. end
  580. else
  581. internalerror(432472389);
  582. end;
  583. function ttypeconvnode.first_pchar_to_string : tnode;
  584. begin
  585. first_pchar_to_string:=nil;
  586. location.loc:=LOC_REFERENCE;
  587. end;
  588. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  589. begin
  590. first_ansistring_to_pchar:=nil;
  591. location.loc:=LOC_REGISTER;
  592. if registers32<1 then
  593. registers32:=1;
  594. end;
  595. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  596. var
  597. hp : tnode;
  598. begin
  599. first_arrayconstructor_to_set:=nil;
  600. if left.nodetype<>arrayconstructorn then
  601. internalerror(5546);
  602. { remove typeconv node }
  603. hp:=left;
  604. left:=nil;
  605. { create a set constructor tree }
  606. arrayconstructor_to_set(tarrayconstructornode(hp));
  607. { now firstpass the set }
  608. firstpass(hp);
  609. first_arrayconstructor_to_set:=hp;
  610. end;
  611. function ttypeconvnode.first_class_to_intf : tnode;
  612. begin
  613. first_class_to_intf:=nil;
  614. location.loc:=LOC_REFERENCE;
  615. if registers32<1 then
  616. registers32:=1;
  617. end;
  618. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  619. const
  620. firstconvert : array[tconverttype] of pointer = (
  621. @ttypeconvnode.first_nothing, {equal}
  622. @ttypeconvnode.first_nothing, {not_possible}
  623. @ttypeconvnode.first_string_to_string,
  624. @ttypeconvnode.first_char_to_string,
  625. @ttypeconvnode.first_pchar_to_string,
  626. @ttypeconvnode.first_cchar_to_pchar,
  627. @ttypeconvnode.first_cstring_to_pchar,
  628. @ttypeconvnode.first_ansistring_to_pchar,
  629. @ttypeconvnode.first_string_to_chararray,
  630. @ttypeconvnode.first_chararray_to_string,
  631. @ttypeconvnode.first_array_to_pointer,
  632. @ttypeconvnode.first_pointer_to_array,
  633. @ttypeconvnode.first_int_to_int,
  634. @ttypeconvnode.first_int_to_bool,
  635. @ttypeconvnode.first_bool_to_bool,
  636. @ttypeconvnode.first_bool_to_int,
  637. @ttypeconvnode.first_real_to_real,
  638. @ttypeconvnode.first_int_to_real,
  639. @ttypeconvnode.first_int_to_fix,
  640. @ttypeconvnode.first_real_to_fix,
  641. @ttypeconvnode.first_fix_to_real,
  642. @ttypeconvnode.first_proc_to_procvar,
  643. @ttypeconvnode.first_arrayconstructor_to_set,
  644. @ttypeconvnode.first_load_smallset,
  645. @ttypeconvnode.first_cord_to_pointer,
  646. @ttypeconvnode.first_nothing,
  647. @ttypeconvnode.first_nothing,
  648. @ttypeconvnode.first_class_to_intf
  649. );
  650. type
  651. tprocedureofobject = function : tnode of object;
  652. var
  653. r : packed record
  654. proc : pointer;
  655. obj : pointer;
  656. end;
  657. begin
  658. { this is a little bit dirty but it works }
  659. { and should be quite portable too }
  660. r.proc:=firstconvert[c];
  661. r.obj:=self;
  662. first_call_helper:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  663. end;
  664. function ttypeconvnode.pass_1 : tnode;
  665. var
  666. hp : tnode;
  667. aprocdef : pprocdef;
  668. enable_range_check: boolean;
  669. begin
  670. pass_1:=nil;
  671. aprocdef:=nil;
  672. { if explicite type cast, then run firstpass }
  673. if (nf_explizit in flags) or not assigned(left.resulttype) then
  674. firstpass(left);
  675. if (left.nodetype=typen) and (left.resulttype=generrordef) then
  676. begin
  677. codegenerror:=true;
  678. Message(parser_e_no_type_not_allowed_here);
  679. end;
  680. if codegenerror then
  681. begin
  682. resulttype:=generrordef;
  683. exit;
  684. end;
  685. if not assigned(left.resulttype) then
  686. begin
  687. codegenerror:=true;
  688. internalerror(52349);
  689. exit;
  690. end;
  691. { load the value_str from the left part }
  692. registers32:=left.registers32;
  693. registersfpu:=left.registersfpu;
  694. {$ifdef SUPPORT_MMX}
  695. registersmmx:=left.registersmmx;
  696. {$endif}
  697. set_location(location,left.location);
  698. { remove obsolete type conversions }
  699. if is_equal(left.resulttype,resulttype) then
  700. begin
  701. { becuase is_equal only checks the basetype for sets we need to
  702. check here if we are loading a smallset into a normalset }
  703. if (resulttype^.deftype=setdef) and
  704. (left.resulttype^.deftype=setdef) and
  705. (psetdef(resulttype)^.settype<>smallset) and
  706. (psetdef(left.resulttype)^.settype=smallset) then
  707. begin
  708. { try to define the set as a normalset if it's a constant set }
  709. if left.nodetype=setconstn then
  710. begin
  711. resulttype:=left.resulttype;
  712. psetdef(resulttype)^.settype:=normset
  713. end
  714. else
  715. convtype:=tc_load_smallset;
  716. exit;
  717. end
  718. else
  719. begin
  720. pass_1:=left;
  721. left.resulttype:=resulttype;
  722. left:=nil;
  723. exit;
  724. end;
  725. end;
  726. aprocdef:=assignment_overloaded(left.resulttype,resulttype);
  727. if assigned(aprocdef) then
  728. begin
  729. procinfo^.flags:=procinfo^.flags or pi_do_call;
  730. hp:=gencallnode(overloaded_operators[_assignment],nil);
  731. { tell explicitly which def we must use !! (PM) }
  732. tcallnode(hp).procdefinition:=aprocdef;
  733. tcallnode(hp).left:=gencallparanode(left,nil);
  734. left:=nil;
  735. firstpass(hp);
  736. pass_1:=hp;
  737. exit;
  738. end;
  739. if isconvertable(left.resulttype,resulttype,convtype,left,left.nodetype,nf_explizit in flags)=0 then
  740. begin
  741. {Procedures have a resulttype of voiddef and functions of their
  742. own resulttype. They will therefore always be incompatible with
  743. a procvar. Because isconvertable cannot check for procedures we
  744. use an extra check for them.}
  745. if (m_tp_procvar in aktmodeswitches) then
  746. begin
  747. if (resulttype^.deftype=procvardef) and
  748. (is_procsym_load(left) or is_procsym_call(left)) then
  749. begin
  750. if is_procsym_call(left) then
  751. begin
  752. {if left.right=nil then
  753. begin}
  754. if (tcallnode(left).symtableprocentry^.owner^.symtabletype=objectsymtable){ and
  755. (pobjectdef(left.symtableprocentry^.owner^.defowner)^.is_class) }then
  756. hp:=genloadmethodcallnode(pprocsym(tcallnode(left).symtableprocentry),
  757. tcallnode(left).symtableproc,
  758. tcallnode(left).methodpointer.getcopy)
  759. else
  760. hp:=genloadcallnode(pprocsym(tcallnode(left).symtableprocentry),
  761. tcallnode(left).symtableproc);
  762. firstpass(hp);
  763. left.free;
  764. left:=hp;
  765. aprocdef:=pprocdef(left.resulttype);
  766. (* end
  767. else
  768. begin
  769. left.right.nodetype:=loadn;
  770. left.right.symtableentry:=left.right.symtableentry;
  771. left.right.resulttype:=pvarsym(left.symtableentry)^.definition;
  772. hp:=left.right;
  773. putnode(left);
  774. left:=hp;
  775. { should we do that ? }
  776. firstpass(left);
  777. if not is_equal(left.resulttype,resulttype) then
  778. begin
  779. CGMessage(type_e_mismatch);
  780. exit;
  781. end
  782. else
  783. begin
  784. hp:=p;
  785. p:=left;
  786. resulttype:=hp.resulttype;
  787. putnode(hp);
  788. exit;
  789. end;
  790. end; *)
  791. end
  792. else
  793. begin
  794. if (left.nodetype<>addrn) then
  795. aprocdef:=pprocsym(tloadnode(left).symtableentry)^.definition;
  796. end;
  797. convtype:=tc_proc_2_procvar;
  798. { Now check if the procedure we are going to assign to
  799. the procvar, is compatible with the procvar's type }
  800. if assigned(aprocdef) then
  801. begin
  802. if not proc_to_procvar_equal(aprocdef,pprocvardef(resulttype)) then
  803. CGMessage2(type_e_incompatible_types,aprocdef^.typename,resulttype^.typename);
  804. pass_1:=first_call_helper(convtype);
  805. end
  806. else
  807. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  808. exit;
  809. end;
  810. end;
  811. if nf_explizit in flags then
  812. begin
  813. { check if the result could be in a register }
  814. if not(pstoreddef(resulttype)^.is_intregable) and
  815. not(pstoreddef(resulttype)^.is_fpuregable) then
  816. make_not_regable(left);
  817. { boolean to byte are special because the
  818. location can be different }
  819. if is_integer(resulttype) and
  820. is_boolean(left.resulttype) then
  821. begin
  822. convtype:=tc_bool_2_int;
  823. pass_1:=first_call_helper(convtype);
  824. exit;
  825. end;
  826. { ansistring to pchar }
  827. if is_pchar(resulttype) and
  828. is_ansistring(left.resulttype) then
  829. begin
  830. convtype:=tc_ansistring_2_pchar;
  831. pass_1:=first_call_helper(convtype);
  832. exit;
  833. end;
  834. { do common tc_equal cast }
  835. convtype:=tc_equal;
  836. { enum to ordinal will always be s32bit }
  837. if (left.resulttype^.deftype=enumdef) and
  838. is_ordinal(resulttype) then
  839. begin
  840. if left.nodetype=ordconstn then
  841. begin
  842. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  843. firstpass(hp);
  844. pass_1:=hp;
  845. exit;
  846. end
  847. else
  848. begin
  849. if isconvertable(s32bitdef,resulttype,convtype,nil,ordconstn,false)=0 then
  850. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  851. end;
  852. end
  853. { ordinal to enumeration }
  854. else
  855. if (resulttype^.deftype=enumdef) and
  856. is_ordinal(left.resulttype) then
  857. begin
  858. if left.nodetype=ordconstn then
  859. begin
  860. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  861. firstpass(hp);
  862. pass_1:=hp;
  863. exit;
  864. end
  865. else
  866. begin
  867. if IsConvertable(left.resulttype,s32bitdef,convtype,nil,ordconstn,false)=0 then
  868. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  869. end;
  870. end
  871. { nil to ordinal node }
  872. else if is_ordinal(resulttype) and
  873. (left.nodetype=niln) then
  874. begin
  875. hp:=genordinalconstnode(0,resulttype);
  876. firstpass(hp);
  877. pass_1:=hp;
  878. exit;
  879. end
  880. {Are we typecasting an ordconst to a char?}
  881. else
  882. if is_char(resulttype) and
  883. is_ordinal(left.resulttype) then
  884. begin
  885. if left.nodetype=ordconstn then
  886. begin
  887. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  888. firstpass(hp);
  889. pass_1:=hp;
  890. exit;
  891. end
  892. else
  893. begin
  894. if IsConvertable(left.resulttype,u8bitdef,convtype,nil,ordconstn,false)=0 then
  895. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  896. end;
  897. end
  898. { Are we char to ordinal }
  899. else
  900. if is_char(left.resulttype) and
  901. is_ordinal(resulttype) then
  902. begin
  903. if left.nodetype=ordconstn then
  904. begin
  905. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  906. firstpass(hp);
  907. pass_1:=hp;
  908. exit;
  909. end
  910. else
  911. begin
  912. if IsConvertable(u8bitdef,resulttype,convtype,nil,ordconstn,false)=0 then
  913. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  914. end;
  915. end
  916. { only if the same size or formal def }
  917. { why do we allow typecasting of voiddef ?? (PM) }
  918. else
  919. begin
  920. if not(
  921. (left.resulttype^.deftype=formaldef) or
  922. (left.resulttype^.size=resulttype^.size) or
  923. (is_equal(left.resulttype,voiddef) and
  924. (left.nodetype=derefn))
  925. ) then
  926. CGMessage(cg_e_illegal_type_conversion);
  927. if ((left.resulttype^.deftype=orddef) and
  928. (resulttype^.deftype=pointerdef)) or
  929. ((resulttype^.deftype=orddef) and
  930. (left.resulttype^.deftype=pointerdef))
  931. {$ifdef extdebug}and (firstpasscount=0){$endif} then
  932. CGMessage(cg_d_pointer_to_longint_conv_not_portable);
  933. end;
  934. { the conversion into a strutured type is only }
  935. { possible, if the source is no register }
  936. if ((resulttype^.deftype in [recorddef,stringdef,arraydef]) or
  937. ((resulttype^.deftype=objectdef) and not(is_class(resulttype)))
  938. ) and (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) { and
  939. it also works if the assignment is overloaded
  940. YES but this code is not executed if assignment is overloaded (PM)
  941. not assigned(assignment_overloaded(left.resulttype,resulttype))} then
  942. CGMessage(cg_e_illegal_type_conversion);
  943. end
  944. else
  945. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  946. end;
  947. { tp7 procvar support, when right is not a procvardef and we got a
  948. loadn of a procvar then convert to a calln, the check for the
  949. result is already done in is_convertible, also no conflict with
  950. @procvar is here because that has an extra addrn }
  951. if (m_tp_procvar in aktmodeswitches) and
  952. (resulttype^.deftype<>procvardef) and
  953. (left.resulttype^.deftype=procvardef) and
  954. (left.nodetype=loadn) then
  955. begin
  956. hp:=gencallnode(nil,nil);
  957. tcallnode(hp).right:=left;
  958. firstpass(hp);
  959. left:=hp;
  960. end;
  961. { ordinal contants can be directly converted }
  962. if (left.nodetype=ordconstn) and is_ordinal(resulttype) then
  963. begin
  964. { range checking is done in genordinalconstnode (PFV) }
  965. { disable for explicit type casts (JM) }
  966. if (nf_explizit in flags) and
  967. (cs_check_range in aktlocalswitches) then
  968. begin
  969. exclude(aktlocalswitches,cs_check_range);
  970. enable_range_check := true;
  971. end
  972. else
  973. enable_range_check := false;
  974. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  975. { do sign extension if necessary (JM) }
  976. if not (cs_check_range in aktlocalswitches) and
  977. is_signed(resulttype) then
  978. with tordconstnode(hp) do
  979. case resulttype^.size of
  980. 1: value := shortint(value);
  981. 2: value := smallint(value);
  982. 4: value := longint(value);
  983. end;
  984. if enable_range_check then
  985. include(aktlocalswitches,cs_check_range);
  986. firstpass(hp);
  987. pass_1:=hp;
  988. exit;
  989. end;
  990. if convtype<>tc_equal then
  991. pass_1:=first_call_helper(convtype);
  992. end;
  993. {*****************************************************************************
  994. TISNODE
  995. *****************************************************************************}
  996. constructor tisnode.create(l,r : tnode);
  997. begin
  998. inherited create(isn,l,r);
  999. end;
  1000. function tisnode.pass_1 : tnode;
  1001. begin
  1002. pass_1:=nil;
  1003. firstpass(left);
  1004. set_varstate(left,true);
  1005. firstpass(right);
  1006. set_varstate(right,true);
  1007. if codegenerror then
  1008. exit;
  1009. if (right.resulttype^.deftype<>classrefdef) then
  1010. CGMessage(type_e_mismatch);
  1011. left_right_max;
  1012. { left must be a class }
  1013. if (left.resulttype^.deftype<>objectdef) or
  1014. not(is_class(left.resulttype)) then
  1015. CGMessage(type_e_mismatch);
  1016. { the operands must be related }
  1017. if (not(pobjectdef(left.resulttype)^.is_related(
  1018. pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)))) and
  1019. (not(pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)^.is_related(
  1020. pobjectdef(left.resulttype)))) then
  1021. CGMessage(type_e_mismatch);
  1022. location.loc:=LOC_FLAGS;
  1023. resulttype:=booldef;
  1024. end;
  1025. {*****************************************************************************
  1026. TASNODE
  1027. *****************************************************************************}
  1028. constructor tasnode.create(l,r : tnode);
  1029. begin
  1030. inherited create(asn,l,r);
  1031. end;
  1032. function tasnode.pass_1 : tnode;
  1033. begin
  1034. pass_1:=nil;
  1035. firstpass(right);
  1036. set_varstate(right,true);
  1037. firstpass(left);
  1038. set_varstate(left,true);
  1039. if codegenerror then
  1040. exit;
  1041. if (right.resulttype^.deftype<>classrefdef) then
  1042. CGMessage(type_e_mismatch);
  1043. left_right_max;
  1044. { left must be a class }
  1045. if (left.resulttype^.deftype<>objectdef) or
  1046. not(is_class(left.resulttype)) then
  1047. CGMessage(type_e_mismatch);
  1048. { the operands must be related }
  1049. if (not(pobjectdef(left.resulttype)^.is_related(
  1050. pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)))) and
  1051. (not(pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)^.is_related(
  1052. pobjectdef(left.resulttype)))) then
  1053. CGMessage(type_e_mismatch);
  1054. set_location(location,left.location);
  1055. resulttype:=pclassrefdef(right.resulttype)^.pointertype.def;
  1056. end;
  1057. function ttypeconvnode.docompare(p: tnode) : boolean;
  1058. begin
  1059. docompare :=
  1060. inherited docompare(p) and
  1061. (convtype = ttypeconvnode(p).convtype);
  1062. end;
  1063. begin
  1064. ctypeconvnode:=ttypeconvnode;
  1065. casnode:=tasnode;
  1066. cisnode:=tisnode;
  1067. end.
  1068. {
  1069. $Log$
  1070. Revision 1.16 2000-12-31 11:14:10 jonas
  1071. + implemented/fixed docompare() mathods for all nodes (not tested)
  1072. + nopt.pas, nadd.pas, i386/n386opt.pas: optimized nodes for adding strings
  1073. and constant strings/chars together
  1074. * n386add.pas: don't copy temp strings (of size 256) to another temp string
  1075. when adding
  1076. Revision 1.15 2000/12/08 12:41:01 jonas
  1077. * fixed bug in sign extension patch
  1078. Revision 1.14 2000/12/07 17:19:42 jonas
  1079. * new constant handling: from now on, hex constants >$7fffffff are
  1080. parsed as unsigned constants (otherwise, $80000000 got sign extended
  1081. and became $ffffffff80000000), all constants in the longint range
  1082. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  1083. are cardinals and the rest are int64's.
  1084. * added lots of longint typecast to prevent range check errors in the
  1085. compiler and rtl
  1086. * type casts of symbolic ordinal constants are now preserved
  1087. * fixed bug where the original resulttype wasn't restored correctly
  1088. after doing a 64bit rangecheck
  1089. Revision 1.13 2000/11/29 00:30:32 florian
  1090. * unused units removed from uses clause
  1091. * some changes for widestrings
  1092. Revision 1.12 2000/11/20 16:06:04 jonas
  1093. + allow evaluation of 64bit constant expressions at compile time
  1094. * disable range checking for explicit typecasts of constant expressions
  1095. Revision 1.11 2000/11/12 23:24:11 florian
  1096. * interfaces are basically running
  1097. Revision 1.10 2000/11/04 14:25:20 florian
  1098. + merged Attila's changes for interfaces, not tested yet
  1099. Revision 1.9 2000/10/31 22:02:48 peter
  1100. * symtable splitted, no real code changes
  1101. Revision 1.8 2000/10/14 21:52:55 peter
  1102. * fixed memory leaks
  1103. Revision 1.7 2000/10/14 10:14:50 peter
  1104. * moehrendorf oct 2000 rewrite
  1105. Revision 1.6 2000/10/01 19:48:24 peter
  1106. * lot of compile updates for cg11
  1107. Revision 1.5 2000/09/28 19:49:52 florian
  1108. *** empty log message ***
  1109. Revision 1.4 2000/09/27 18:14:31 florian
  1110. * fixed a lot of syntax errors in the n*.pas stuff
  1111. Revision 1.3 2000/09/26 20:06:13 florian
  1112. * hmm, still a lot of work to get things compilable
  1113. Revision 1.2 2000/09/26 14:59:34 florian
  1114. * more conversion work done
  1115. Revision 1.1 2000/09/25 15:37:14 florian
  1116. * more fixes
  1117. }