ncnv.pas 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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. symtable,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 call_helper(c : tconverttype) : tnode;
  56. end;
  57. tasnode = class(tbinarynode)
  58. constructor create(l,r : tnode);virtual;
  59. function pass_1 : tnode;override;
  60. end;
  61. tisnode = class(tbinarynode)
  62. constructor create(l,r : tnode);virtual;
  63. function pass_1 : tnode;override;
  64. end;
  65. var
  66. ctypeconvnode : class of ttypeconvnode;
  67. casnode : class of tasnode;
  68. cisnode : class of tisnode;
  69. function gentypeconvnode(node : tnode;t : pdef) : ttypeconvnode;
  70. procedure arrayconstructor_to_set(var p : tarrayconstructnode);
  71. implementation
  72. uses
  73. globtype,systems,tokens,
  74. cutils,cobjects,verbose,globals,
  75. symconst,aasm,
  76. ncon,ncal,nset,nadd,
  77. {$ifdef newcg}
  78. cgbase,
  79. {$else newcg}
  80. hcodegen,
  81. {$endif newcg}
  82. htypechk,pass_1,cpubase;
  83. function gentypeconvnode(node : tnode;t : pdef) : ttypeconvnode;
  84. begin
  85. gentypeconvnode:=ctypeconvnode.create(node,t);
  86. end;
  87. {*****************************************************************************
  88. Array constructor to Set Conversion
  89. *****************************************************************************}
  90. procedure arrayconstructor_to_set(var p : tarrayconstructnode);
  91. var
  92. constp : tsetconstnode;
  93. buildp,
  94. p2,p3,p4 : tnode;
  95. pd : pdef;
  96. constset : pconstset;
  97. constsetlo,
  98. constsethi : longint;
  99. procedure update_constsethi(p:pdef);
  100. begin
  101. if ((p^.deftype=orddef) and
  102. (porddef(p)^.high>=constsethi)) then
  103. begin
  104. constsethi:=porddef(p)^.high;
  105. if pd=nil then
  106. begin
  107. if (constsethi>255) or
  108. (porddef(p)^.low<0) then
  109. pd:=u8bitdef
  110. else
  111. pd:=p;
  112. end;
  113. if constsethi>255 then
  114. constsethi:=255;
  115. end
  116. else if ((p^.deftype=enumdef) and
  117. (penumdef(p)^.max>=constsethi)) then
  118. begin
  119. if pd=nil then
  120. pd:=p;
  121. constsethi:=penumdef(p)^.max;
  122. end;
  123. end;
  124. procedure do_set(pos : longint);
  125. var
  126. mask,l : longint;
  127. begin
  128. if (pos>255) or (pos<0) then
  129. Message(parser_e_illegal_set_expr);
  130. if pos>constsethi then
  131. constsethi:=pos;
  132. if pos<constsetlo then
  133. constsetlo:=pos;
  134. l:=pos shr 3;
  135. mask:=1 shl (pos mod 8);
  136. { do we allow the same twice }
  137. if (constset^[l] and mask)<>0 then
  138. Message(parser_e_illegal_set_expr);
  139. constset^[l]:=constset^[l] or mask;
  140. end;
  141. var
  142. l : longint;
  143. lr,hr : longint;
  144. begin
  145. new(constset);
  146. FillChar(constset^,sizeof(constset^),0);
  147. pd:=nil;
  148. constsetlo:=0;
  149. constsethi:=0;
  150. constp:=csetconstnode.create(nil,nil);
  151. constp.value_set:=constset;
  152. if assigned(p.left) then
  153. begin
  154. while assigned(p) do
  155. begin
  156. p4:=nil; { will contain the tree to create the set }
  157. { split a range into p2 and p3 }
  158. if p.left.nodetype=arrayconstructrangen then
  159. begin
  160. p2:=tarrayconstructorrangenode(p.left).left;
  161. p3:=tarrayconstructorrangenode(p.left).right;
  162. tarrayconstructorrangenode(p.left).left:=nil;
  163. tarrayconstructorrangenode(p.left).right:=nil;
  164. { node is not used anymore }
  165. p.left.free;
  166. end
  167. else
  168. begin
  169. p2:=p.left;
  170. p3:=nil;
  171. end;
  172. firstpass(p2);
  173. if assigned(p3) then
  174. firstpass(p3);
  175. if codegenerror then
  176. break;
  177. case p2.resulttype^.deftype of
  178. enumdef,
  179. orddef:
  180. begin
  181. getrange(p2.resulttype,lr,hr);
  182. if assigned(p3) then
  183. begin
  184. { this isn't good, you'll get problems with
  185. type t010 = 0..10;
  186. ts = set of t010;
  187. var s : ts;b : t010
  188. begin s:=[1,2,b]; end.
  189. if is_integer(p3^.resulttype) then
  190. begin
  191. p3:=gentypeconvnode(p3,u8bitdef);
  192. firstpass(p3);
  193. end;
  194. }
  195. if assigned(pd) and not(is_equal(pd,p3.resulttype)) then
  196. begin
  197. aktfilepos:=p3.fileinfo;
  198. CGMessage(type_e_typeconflict_in_set);
  199. end
  200. else
  201. begin
  202. if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
  203. begin
  204. if not(is_integer(p3.resulttype)) then
  205. pd:=p3.resulttype
  206. else
  207. begin
  208. p3:=gentypeconvnode(p3,u8bitdef);
  209. p2:=gentypeconvnode(p2,u8bitdef);
  210. firstpass(p2);
  211. firstpass(p3);
  212. end;
  213. for l:=tordconstnode(p2).value to tordconstnode(p3).value do
  214. do_set(l);
  215. p2.free;
  216. p3.free;
  217. end
  218. else
  219. begin
  220. update_constsethi(p2.resulttype);
  221. p2:=gentypeconvnode(p2,pd);
  222. firstpass(p2);
  223. update_constsethi(p3.resulttype);
  224. p3:=gentypeconvnode(p3,pd);
  225. firstpass(p3);
  226. if assigned(pd) then
  227. p3:=gentypeconvnode(p3,pd)
  228. else
  229. p3:=gentypeconvnode(p3,u8bitdef);
  230. firstpass(p3);
  231. p4:=csetelementnode.create(p2,p3);
  232. end;
  233. end;
  234. end
  235. else
  236. begin
  237. { Single value }
  238. if p2.nodetype=ordconstn then
  239. begin
  240. if not(is_integer(p2.resulttype)) then
  241. update_constsethi(p2.resulttype)
  242. else
  243. begin
  244. p2:=gentypeconvnode(p2,u8bitdef);
  245. firstpass(p2);
  246. end;
  247. do_set(tordconstnode(p2).value);
  248. p2.free;
  249. end
  250. else
  251. begin
  252. update_constsethi(p2.resulttype);
  253. if assigned(pd) then
  254. p2:=gentypeconvnode(p2,pd)
  255. else
  256. p2:=gentypeconvnode(p2,u8bitdef);
  257. firstpass(p2);
  258. p4:=csetelementnode.create(p2,nil);
  259. end;
  260. end;
  261. end;
  262. stringdef : begin
  263. { if we've already set elements which are constants }
  264. { throw an error }
  265. if ((pd=nil) and assigned(buildp)) or
  266. not(is_equal(pd,cchardef)) then
  267. CGMessage(type_e_typeconflict_in_set)
  268. else
  269. for l:=1 to length(pstring(tstringconstnode(p2).value_str)^) do
  270. do_set(ord(pstring(tstringconstnode(p2).value_str)^[l]));
  271. if pd=nil then
  272. pd:=cchardef;
  273. p2.free;
  274. end;
  275. else
  276. CGMessage(type_e_ordinal_expr_expected);
  277. end;
  278. { insert the set creation tree }
  279. if assigned(p4) then
  280. buildp:=caddnode.create(addn,buildp,p4);
  281. { load next and dispose current node }
  282. p2:=p;
  283. p:=tarrayconstructnode(p.right);
  284. tarrayconstructnode(p2).right:=nil;
  285. p2.free;
  286. end;
  287. if (pd=nil) then
  288. begin
  289. pd:=u8bitdef;
  290. constsethi:=255;
  291. end;
  292. end
  293. else
  294. begin
  295. { empty set [], only remove node }
  296. p.free;
  297. end;
  298. { set the initial set type }
  299. constp.resulttype:=new(psetdef,init(pd,constsethi));
  300. { set the new tree }
  301. p:=tarrayconstructnode(buildp);
  302. end;
  303. {*****************************************************************************
  304. TTYPECONVNODE
  305. *****************************************************************************}
  306. constructor ttypeconvnode.create(node : tnode;t : pdef);
  307. begin
  308. inherited create(typeconvn,node);
  309. convtype:=tc_not_possible;
  310. resulttype:=t;
  311. set_file_line(node);
  312. end;
  313. function ttypeconvnode.getcopy : tnode;
  314. var
  315. n : ttypeconvnode;
  316. begin
  317. n:=ttypeconvnode(inherited getcopy);
  318. n.convtype:=convtype;
  319. getcopy:=n;
  320. end;
  321. function ttypeconvnode.first_int_to_int : tnode;
  322. begin
  323. first_int_to_int:=nil;
  324. if (left.location.loc<>LOC_REGISTER) and
  325. (resulttype^.size>left.resulttype^.size) then
  326. location.loc:=LOC_REGISTER;
  327. if is_64bitint(resulttype) then
  328. registers32:=max(registers32,2)
  329. else
  330. registers32:=max(registers32,1);
  331. end;
  332. function ttypeconvnode.first_cstring_to_pchar : tnode;
  333. begin
  334. first_cstring_to_pchar:=nil;
  335. registers32:=1;
  336. location.loc:=LOC_REGISTER;
  337. end;
  338. function ttypeconvnode.first_string_to_chararray : tnode;
  339. begin
  340. first_string_to_chararray:=nil;
  341. registers32:=1;
  342. location.loc:=LOC_REGISTER;
  343. end;
  344. function ttypeconvnode.first_string_to_string : tnode;
  345. var
  346. t : 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<>arrayconstructn then
  601. internalerror(5546);
  602. { remove typeconv node }
  603. hp:=left;
  604. left:=nil;
  605. { create a set constructor tree }
  606. // !!!!!!!arrayconstructor_to_set(hp);
  607. internalerror(2609001);
  608. {$warning FIX ME !!!!!!!!}
  609. { now firstpass the set }
  610. firstpass(hp);
  611. first_arrayconstructor_to_set:=hp;
  612. end;
  613. function ttypeconvnode.call_helper(c : tconverttype) : tnode;
  614. {$warning FIX ME !!!!!!!!!}
  615. {
  616. const
  617. firstconvert : array[tconverttype] of pointer = (
  618. @ttypeconvnode.first_nothing), {equal}
  619. @ttypeconvnode.first_nothing, {not_possible}
  620. @ttypeconvnode.first_string_to_string,
  621. @ttypeconvnode.first_char_to_string,
  622. @ttypeconvnode.first_pchar_to_string,
  623. @ttypeconvnode.first_cchar_to_pchar,
  624. @ttypeconvnode.first_cstring_to_pchar,
  625. @ttypeconvnode.first_ansistring_to_pchar,
  626. @ttypeconvnode.first_string_to_chararray,
  627. @ttypeconvnode.first_chararray_to_string,
  628. @ttypeconvnode.first_array_to_pointer,
  629. @ttypeconvnode.first_pointer_to_array,
  630. @ttypeconvnode.first_int_to_int,
  631. @ttypeconvnode.first_int_to_bool,
  632. @ttypeconvnode.first_bool_to_bool,
  633. @ttypeconvnode.first_bool_to_int,
  634. @ttypeconvnode.first_real_to_real,
  635. @ttypeconvnode.first_int_to_real,
  636. @ttypeconvnode.first_int_to_fix,
  637. @ttypeconvnode.first_real_to_fix,
  638. @ttypeconvnode.first_fix_to_real,
  639. @ttypeconvnode.first_proc_to_procvar,
  640. @ttypeconvnode.first_arrayconstructor_to_set,
  641. @ttypeconvnode.first_load_smallset,
  642. @ttypeconvnode.first_cord_to_pointer
  643. );
  644. }
  645. type
  646. tprocedureofobject = function : tnode of object;
  647. var
  648. r : packed record
  649. proc : pointer;
  650. obj : pointer;
  651. end;
  652. begin
  653. { this is a little bit dirty but it works }
  654. { and should be quite portable too }
  655. // !!!! r.proc:=firstconvert[c];
  656. {$warning FIX ME !!!!!}
  657. internalerror(2609002);
  658. r.obj:=self;
  659. call_helper:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  660. end;
  661. function ttypeconvnode.pass_1 : tnode;
  662. var
  663. hp : tnode;
  664. aprocdef : pprocdef;
  665. begin
  666. pass_1:=nil;
  667. aprocdef:=nil;
  668. { if explicite type cast, then run firstpass }
  669. if (nf_explizit in flags) or not assigned(left.resulttype) then
  670. firstpass(left);
  671. if (left.nodetype=typen) and (left.resulttype=generrordef) then
  672. begin
  673. codegenerror:=true;
  674. Message(parser_e_no_type_not_allowed_here);
  675. end;
  676. if codegenerror then
  677. begin
  678. resulttype:=generrordef;
  679. exit;
  680. end;
  681. if not assigned(left.resulttype) then
  682. begin
  683. codegenerror:=true;
  684. internalerror(52349);
  685. exit;
  686. end;
  687. { load the value_str from the left part }
  688. registers32:=left.registers32;
  689. registersfpu:=left.registersfpu;
  690. {$ifdef SUPPORT_MMX}
  691. registersmmx:=left.registersmmx;
  692. {$endif}
  693. set_location(location,left.location);
  694. { remove obsolete type conversions }
  695. if is_equal(left.resulttype,resulttype) then
  696. begin
  697. { becuase is_equal only checks the basetype for sets we need to
  698. check here if we are loading a smallset into a normalset }
  699. if (resulttype^.deftype=setdef) and
  700. (left.resulttype^.deftype=setdef) and
  701. (psetdef(resulttype)^.settype<>smallset) and
  702. (psetdef(left.resulttype)^.settype=smallset) then
  703. begin
  704. { try to define the set as a normalset if it's a constant set }
  705. if left.nodetype=setconstn then
  706. begin
  707. resulttype:=left.resulttype;
  708. psetdef(resulttype)^.settype:=normset
  709. end
  710. else
  711. convtype:=tc_load_smallset;
  712. exit;
  713. end
  714. else
  715. begin
  716. pass_1:=left;
  717. left.resulttype:=resulttype;
  718. left:=nil;
  719. exit;
  720. end;
  721. end;
  722. aprocdef:=assignment_overloaded(left.resulttype,resulttype);
  723. if assigned(aprocdef) then
  724. begin
  725. procinfo^.flags:=procinfo^.flags or pi_do_call;
  726. hp:=gencallnode(overloaded_operators[_assignment],nil);
  727. { tell explicitly which def we must use !! (PM) }
  728. tcallnode(hp).procdefinition:=aprocdef;
  729. tcallnode(hp).left:=gencallparanode(left,nil);
  730. left:=nil;
  731. firstpass(hp);
  732. pass_1:=hp;
  733. exit;
  734. end;
  735. if isconvertable(left.resulttype,resulttype,convtype,left.nodetype,nf_explizit in flags)=0 then
  736. begin
  737. {Procedures have a resulttype of voiddef and functions of their
  738. own resulttype. They will therefore always be incompatible with
  739. a procvar. Because isconvertable cannot check for procedures we
  740. use an extra check for them.}
  741. if (m_tp_procvar in aktmodeswitches) then
  742. begin
  743. if (resulttype^.deftype=procvardef) and
  744. (is_procsym_load(left) or is_procsym_call(left)) then
  745. begin
  746. if is_procsym_call(left) then
  747. begin
  748. {if left.right=nil then
  749. begin}
  750. if (tcallnode(left).symtableprocentry^.owner^.symtabletype=objectsymtable){ and
  751. (pobjectdef(left.symtableprocentry^.owner^.defowner)^.is_class) }then
  752. hp:=genloadmethodcallnode(pprocsym(tcallnode(left).symtableprocentry),
  753. tcallnode(left).symtableproc,
  754. tcallnode(left).methodpointer.getcopy)
  755. else
  756. hp:=genloadcallnode(pprocsym(tcallnode(left).symtableprocentry),
  757. tcallnode(left).symtableproc);
  758. left.free;
  759. firstpass(hp);
  760. left:=hp;
  761. aprocdef:=pprocdef(left.resulttype);
  762. (* end
  763. else
  764. begin
  765. left.right.nodetype:=loadn;
  766. left.right.symtableentry:=left.right.symtableentry;
  767. left.right.resulttype:=pvarsym(left.symtableentry)^.definition;
  768. hp:=left.right;
  769. putnode(left);
  770. left:=hp;
  771. { should we do that ? }
  772. firstpass(left);
  773. if not is_equal(left.resulttype,resulttype) then
  774. begin
  775. CGMessage(type_e_mismatch);
  776. exit;
  777. end
  778. else
  779. begin
  780. hp:=p;
  781. p:=left;
  782. resulttype:=hp.resulttype;
  783. putnode(hp);
  784. exit;
  785. end;
  786. end; *)
  787. end
  788. else
  789. begin
  790. if (left.nodetype<>addrn) then
  791. aprocdef:=pprocsym(tloadnode(left).symtableentry)^.definition;
  792. end;
  793. convtype:=tc_proc_2_procvar;
  794. { Now check if the procedure we are going to assign to
  795. the procvar, is compatible with the procvar's type }
  796. if assigned(aprocdef) then
  797. begin
  798. if not proc_to_procvar_equal(aprocdef,pprocvardef(resulttype)) then
  799. CGMessage2(type_e_incompatible_types,aprocdef^.typename,resulttype^.typename);
  800. pass_1:=call_helper(convtype);
  801. end
  802. else
  803. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  804. exit;
  805. end;
  806. end;
  807. if nf_explizit in flags then
  808. begin
  809. { check if the result could be in a register }
  810. if not(resulttype^.is_intregable) and
  811. not(resulttype^.is_fpuregable) then
  812. make_not_regable(left);
  813. { boolean to byte are special because the
  814. location can be different }
  815. if is_integer(resulttype) and
  816. is_boolean(left.resulttype) then
  817. begin
  818. convtype:=tc_bool_2_int;
  819. pass_1:=call_helper(convtype);
  820. exit;
  821. end;
  822. { ansistring to pchar }
  823. if is_pchar(resulttype) and
  824. is_ansistring(left.resulttype) then
  825. begin
  826. convtype:=tc_ansistring_2_pchar;
  827. pass_1:=call_helper(convtype);
  828. exit;
  829. end;
  830. { do common tc_equal cast }
  831. convtype:=tc_equal;
  832. { enum to ordinal will always be s32bit }
  833. if (left.resulttype^.deftype=enumdef) and
  834. is_ordinal(resulttype) then
  835. begin
  836. if left.nodetype=ordconstn then
  837. begin
  838. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  839. firstpass(hp);
  840. pass_1:=hp;
  841. exit;
  842. end
  843. else
  844. begin
  845. if isconvertable(s32bitdef,resulttype,convtype,ordconstn,false)=0 then
  846. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  847. end;
  848. end
  849. { ordinal to enumeration }
  850. else
  851. if (resulttype^.deftype=enumdef) and
  852. is_ordinal(left.resulttype) then
  853. begin
  854. if left.nodetype=ordconstn then
  855. begin
  856. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  857. firstpass(hp);
  858. pass_1:=hp;
  859. exit;
  860. end
  861. else
  862. begin
  863. if IsConvertable(left.resulttype,s32bitdef,convtype,ordconstn,false)=0 then
  864. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  865. end;
  866. end
  867. { nil to ordinal node }
  868. else if is_ordinal(resulttype) and
  869. (left.nodetype=niln) then
  870. begin
  871. hp:=genordinalconstnode(0,resulttype);
  872. firstpass(hp);
  873. pass_1:=hp;
  874. exit;
  875. end
  876. {Are we typecasting an ordconst to a char?}
  877. else
  878. if is_char(resulttype) and
  879. is_ordinal(left.resulttype) then
  880. begin
  881. if left.nodetype=ordconstn then
  882. begin
  883. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  884. firstpass(hp);
  885. pass_1:=hp;
  886. exit;
  887. end
  888. else
  889. begin
  890. if IsConvertable(left.resulttype,u8bitdef,convtype,ordconstn,false)=0 then
  891. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  892. end;
  893. end
  894. { Are we char to ordinal }
  895. else
  896. if is_char(left.resulttype) and
  897. is_ordinal(resulttype) then
  898. begin
  899. if left.nodetype=ordconstn then
  900. begin
  901. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  902. firstpass(hp);
  903. pass_1:=hp;
  904. exit;
  905. end
  906. else
  907. begin
  908. if IsConvertable(u8bitdef,resulttype,convtype,ordconstn,false)=0 then
  909. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  910. end;
  911. end
  912. { only if the same size or formal def }
  913. { why do we allow typecasting of voiddef ?? (PM) }
  914. else
  915. begin
  916. if not(
  917. (left.resulttype^.deftype=formaldef) or
  918. (left.resulttype^.size=resulttype^.size) or
  919. (is_equal(left.resulttype,voiddef) and
  920. (left.nodetype=derefn))
  921. ) then
  922. CGMessage(cg_e_illegal_type_conversion);
  923. if ((left.resulttype^.deftype=orddef) and
  924. (resulttype^.deftype=pointerdef)) or
  925. ((resulttype^.deftype=orddef) and
  926. (left.resulttype^.deftype=pointerdef))
  927. {$ifdef extdebug}and (firstpasscount=0){$endif} then
  928. CGMessage(cg_d_pointer_to_longint_conv_not_portable);
  929. end;
  930. { the conversion into a strutured type is only }
  931. { possible, if the source is no register }
  932. if ((resulttype^.deftype in [recorddef,stringdef,arraydef]) or
  933. ((resulttype^.deftype=objectdef) and not(pobjectdef(resulttype)^.is_class))
  934. ) and (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) { and
  935. it also works if the assignment is overloaded
  936. YES but this code is not executed if assignment is overloaded (PM)
  937. not assigned(assignment_overloaded(left.resulttype,resulttype))} then
  938. CGMessage(cg_e_illegal_type_conversion);
  939. end
  940. else
  941. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  942. end;
  943. { tp7 procvar support, when right is not a procvardef and we got a
  944. loadn of a procvar then convert to a calln, the check for the
  945. result is already done in is_convertible, also no conflict with
  946. @procvar is here because that has an extra addrn }
  947. if (m_tp_procvar in aktmodeswitches) and
  948. (resulttype^.deftype<>procvardef) and
  949. (left.resulttype^.deftype=procvardef) and
  950. (left.nodetype=loadn) then
  951. begin
  952. hp:=gencallnode(nil,nil);
  953. tcallnode(hp).right:=left;
  954. firstpass(hp);
  955. left:=hp;
  956. end;
  957. { ordinal contants can be directly converted }
  958. { but not int64/qword }
  959. if (left.nodetype=ordconstn) and is_ordinal(resulttype) and
  960. not(is_64bitint(resulttype)) then
  961. begin
  962. { range checking is done in genordinalconstnode (PFV) }
  963. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  964. firstpass(hp);
  965. pass_1:=hp;
  966. exit;
  967. end;
  968. if convtype<>tc_equal then
  969. pass_1:=call_helper(convtype);
  970. end;
  971. {*****************************************************************************
  972. TISNODE
  973. *****************************************************************************}
  974. constructor tisnode.create(l,r : tnode);
  975. begin
  976. inherited create(isn,l,r);
  977. end;
  978. function tisnode.pass_1 : tnode;
  979. begin
  980. pass_1:=nil;
  981. firstpass(left);
  982. set_varstate(left,true);
  983. firstpass(right);
  984. set_varstate(right,true);
  985. if codegenerror then
  986. exit;
  987. if (right.resulttype^.deftype<>classrefdef) then
  988. CGMessage(type_e_mismatch);
  989. left_right_max;
  990. { left must be a class }
  991. if (left.resulttype^.deftype<>objectdef) or
  992. not(pobjectdef(left.resulttype)^.is_class) then
  993. CGMessage(type_e_mismatch);
  994. { the operands must be related }
  995. if (not(pobjectdef(left.resulttype)^.is_related(
  996. pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)))) and
  997. (not(pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)^.is_related(
  998. pobjectdef(left.resulttype)))) then
  999. CGMessage(type_e_mismatch);
  1000. location.loc:=LOC_FLAGS;
  1001. resulttype:=booldef;
  1002. end;
  1003. {*****************************************************************************
  1004. TASNODE
  1005. *****************************************************************************}
  1006. constructor tasnode.create(l,r : tnode);
  1007. begin
  1008. inherited create(asn,l,r);
  1009. end;
  1010. function tasnode.pass_1 : tnode;
  1011. begin
  1012. pass_1:=nil;
  1013. firstpass(right);
  1014. set_varstate(right,true);
  1015. firstpass(left);
  1016. set_varstate(left,true);
  1017. if codegenerror then
  1018. exit;
  1019. if (right.resulttype^.deftype<>classrefdef) then
  1020. CGMessage(type_e_mismatch);
  1021. left_right_max;
  1022. { left must be a class }
  1023. if (left.resulttype^.deftype<>objectdef) or
  1024. not(pobjectdef(left.resulttype)^.is_class) then
  1025. CGMessage(type_e_mismatch);
  1026. { the operands must be related }
  1027. if (not(pobjectdef(left.resulttype)^.is_related(
  1028. pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)))) and
  1029. (not(pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)^.is_related(
  1030. pobjectdef(left.resulttype)))) then
  1031. CGMessage(type_e_mismatch);
  1032. set_location(location,left.location);
  1033. resulttype:=pclassrefdef(right.resulttype)^.pointertype.def;
  1034. end;
  1035. begin
  1036. ctypeconvnode:=ttypeconvnode;
  1037. casnode:=tasnode;
  1038. cisnode:=tisnode;
  1039. end.
  1040. {
  1041. $Log$
  1042. Revision 1.6 2000-10-01 19:48:24 peter
  1043. * lot of compile updates for cg11
  1044. Revision 1.5 2000/09/28 19:49:52 florian
  1045. *** empty log message ***
  1046. Revision 1.4 2000/09/27 18:14:31 florian
  1047. * fixed a lot of syntax errors in the n*.pas stuff
  1048. Revision 1.3 2000/09/26 20:06:13 florian
  1049. * hmm, still a lot of work to get things compilable
  1050. Revision 1.2 2000/09/26 14:59:34 florian
  1051. * more conversion work done
  1052. Revision 1.1 2000/09/25 15:37:14 florian
  1053. * more fixes
  1054. }