tccal.pas 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Type checking and register allocation for call 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 tccal;
  19. interface
  20. uses
  21. symtable,tree;
  22. procedure firstcallparan(var p : ptree;defcoll : pdefcoll);
  23. procedure firstcalln(var p : ptree);
  24. procedure firstprocinline(var p : ptree);
  25. implementation
  26. uses
  27. globtype,systems,
  28. cobjects,verbose,globals,
  29. aasm,types,
  30. hcodegen,htypechk,pass_1
  31. {$ifdef i386}
  32. ,i386,tgeni386
  33. {$endif}
  34. {$ifdef m68k}
  35. ,m68k,tgen68k
  36. {$endif}
  37. {$ifdef UseBrowser}
  38. ,browser
  39. {$endif UseBrowser}
  40. ;
  41. {*****************************************************************************
  42. FirstCallParaN
  43. *****************************************************************************}
  44. procedure firstcallparan(var p : ptree;defcoll : pdefcoll);
  45. var
  46. old_array_constructor : boolean;
  47. store_valid : boolean;
  48. oldtype : pdef;
  49. {convtyp : tconverttype;}
  50. begin
  51. inc(parsing_para_level);
  52. if assigned(p^.right) then
  53. begin
  54. if defcoll=nil then
  55. firstcallparan(p^.right,nil)
  56. else
  57. firstcallparan(p^.right,defcoll^.next);
  58. p^.registers32:=p^.right^.registers32;
  59. p^.registersfpu:=p^.right^.registersfpu;
  60. {$ifdef SUPPORT_MMX}
  61. p^.registersmmx:=p^.right^.registersmmx;
  62. {$endif}
  63. end;
  64. if defcoll=nil then
  65. begin
  66. old_array_constructor:=allow_array_constructor;
  67. allow_array_constructor:=true;
  68. if not(assigned(p^.resulttype)) or
  69. (p^.left^.treetype=typeconvn) then
  70. firstpass(p^.left);
  71. allow_array_constructor:=old_array_constructor;
  72. if codegenerror then
  73. begin
  74. dec(parsing_para_level);
  75. exit;
  76. end;
  77. p^.resulttype:=p^.left^.resulttype;
  78. end
  79. { if we know the routine which is called, then the type }
  80. { conversions are inserted }
  81. else
  82. begin
  83. if count_ref then
  84. begin
  85. store_valid:=must_be_valid;
  86. if (defcoll^.paratyp=vs_var) then
  87. test_protected(p^.left);
  88. if (defcoll^.paratyp<>vs_var) then
  89. must_be_valid:=true
  90. else
  91. must_be_valid:=false;
  92. { here we must add something for the implicit type }
  93. { conversion from array of char to pchar }
  94. { if isconvertable(p^.left^.resulttype,defcoll^.data,convtyp,
  95. p^.left^.treetype,false) then
  96. if convtyp=tc_array_to_pointer then
  97. must_be_valid:=false; }
  98. { only process typeconvn, else it will break other trees }
  99. old_array_constructor:=allow_array_constructor;
  100. allow_array_constructor:=true;
  101. if (p^.left^.treetype=typeconvn) then
  102. firstpass(p^.left);
  103. allow_array_constructor:=old_array_constructor;
  104. must_be_valid:=store_valid;
  105. end;
  106. if not(is_shortstring(p^.left^.resulttype) and
  107. is_shortstring(defcoll^.data)) and
  108. (defcoll^.data^.deftype<>formaldef) then
  109. begin
  110. if (defcoll^.paratyp=vs_var) and
  111. { allows conversion from word to integer and
  112. byte to shortint }
  113. (not(
  114. (p^.left^.resulttype^.deftype=orddef) and
  115. (defcoll^.data^.deftype=orddef) and
  116. (p^.left^.resulttype^.size=defcoll^.data^.size)
  117. ) and
  118. { an implicit pointer conversion is allowed }
  119. not(
  120. (p^.left^.resulttype^.deftype=pointerdef) and
  121. (defcoll^.data^.deftype=pointerdef)
  122. ) and
  123. { child classes can be also passed }
  124. not(
  125. (p^.left^.resulttype^.deftype=objectdef) and
  126. (defcoll^.data^.deftype=objectdef) and
  127. pobjectdef(p^.left^.resulttype)^.isrelated(pobjectdef(defcoll^.data))
  128. ) and
  129. { passing a single element to a openarray of the same type }
  130. not(
  131. (is_open_array(defcoll^.data) and
  132. is_equal(parraydef(defcoll^.data)^.definition,p^.left^.resulttype))
  133. ) and
  134. { an implicit file conversion is also allowed }
  135. { from a typed file to an untyped one }
  136. not(
  137. (p^.left^.resulttype^.deftype=filedef) and
  138. (defcoll^.data^.deftype=filedef) and
  139. (pfiledef(defcoll^.data)^.filetype = ft_untyped) and
  140. (pfiledef(p^.left^.resulttype)^.filetype = ft_typed)
  141. ) and
  142. not(is_equal(p^.left^.resulttype,defcoll^.data))) then
  143. CGMessage(parser_e_call_by_ref_without_typeconv);
  144. { process cargs arrayconstructor }
  145. if is_array_constructor(p^.left^.resulttype) and
  146. (aktcallprocsym^.definition^.options and pocdecl<>0) and
  147. (aktcallprocsym^.definition^.options and poexternal<>0) then
  148. begin
  149. p^.left^.cargs:=true;
  150. old_array_constructor:=allow_array_constructor;
  151. allow_array_constructor:=true;
  152. firstpass(p^.left);
  153. allow_array_constructor:=old_array_constructor;
  154. end;
  155. { don't generate an type conversion for open arrays and
  156. openstring, else we loss the ranges }
  157. if is_open_array(defcoll^.data) or
  158. is_open_string(defcoll^.data) then
  159. begin
  160. { insert type conv but hold the ranges of the array }
  161. oldtype:=p^.left^.resulttype;
  162. p^.left:=gentypeconvnode(p^.left,defcoll^.data);
  163. firstpass(p^.left);
  164. p^.left^.resulttype:=oldtype;
  165. end
  166. else
  167. begin
  168. p^.left:=gentypeconvnode(p^.left,defcoll^.data);
  169. firstpass(p^.left);
  170. end;
  171. if codegenerror then
  172. begin
  173. dec(parsing_para_level);
  174. exit;
  175. end;
  176. end;
  177. { check var strings }
  178. if (cs_strict_var_strings in aktlocalswitches) and
  179. is_shortstring(p^.left^.resulttype) and
  180. is_shortstring(defcoll^.data) and
  181. (defcoll^.paratyp=vs_var) and
  182. not(is_open_string(defcoll^.data)) and
  183. not(is_equal(p^.left^.resulttype,defcoll^.data)) then
  184. CGMessage(type_e_strict_var_string_violation);
  185. { Variablen for call by reference may not be copied }
  186. { into a register }
  187. { is this usefull here ? }
  188. { this was missing in formal parameter list }
  189. if defcoll^.paratyp=vs_var then
  190. begin
  191. set_unique(p^.left);
  192. make_not_regable(p^.left);
  193. end;
  194. p^.resulttype:=defcoll^.data;
  195. end;
  196. if p^.left^.registers32>p^.registers32 then
  197. p^.registers32:=p^.left^.registers32;
  198. if p^.left^.registersfpu>p^.registersfpu then
  199. p^.registersfpu:=p^.left^.registersfpu;
  200. {$ifdef SUPPORT_MMX}
  201. if p^.left^.registersmmx>p^.registersmmx then
  202. p^.registersmmx:=p^.left^.registersmmx;
  203. {$endif SUPPORT_MMX}
  204. dec(parsing_para_level);
  205. end;
  206. {*****************************************************************************
  207. FirstCallN
  208. *****************************************************************************}
  209. procedure firstcalln(var p : ptree);
  210. type
  211. pprocdefcoll = ^tprocdefcoll;
  212. tprocdefcoll = record
  213. data : pprocdef;
  214. nextpara : pdefcoll;
  215. firstpara : pdefcoll;
  216. next : pprocdefcoll;
  217. end;
  218. var
  219. hp,procs,hp2 : pprocdefcoll;
  220. pd : pprocdef;
  221. oldcallprocsym : pprocsym;
  222. nextprocsym : pprocsym;
  223. def_from,def_to,conv_to : pdef;
  224. pt,inlinecode : ptree;
  225. exactmatch,inlined : boolean;
  226. paralength,l : longint;
  227. pdc : pdefcoll;
  228. {$ifdef TEST_PROCSYMS}
  229. symt : psymtable;
  230. {$endif TEST_PROCSYMS}
  231. { only Dummy }
  232. hcvt : tconverttype;
  233. regi : tregister;
  234. store_valid, old_count_ref : boolean;
  235. { check if the resulttype from tree p is equal with def, needed
  236. for stringconstn and formaldef }
  237. function is_equal(p:ptree;def:pdef) : boolean;
  238. begin
  239. { safety check }
  240. if not (assigned(def) or assigned(p^.resulttype)) then
  241. begin
  242. is_equal:=false;
  243. exit;
  244. end;
  245. { all types can be passed to a formaldef }
  246. is_equal:=(def^.deftype=formaldef) or
  247. (types.is_equal(p^.resulttype,def))
  248. { to support ansi/long/wide strings in a proper way }
  249. { string and string[10] are assumed as equal }
  250. { when searching the correct overloaded procedure }
  251. or
  252. (
  253. (def^.deftype=stringdef) and (p^.resulttype^.deftype=stringdef) and
  254. (pstringdef(def)^.string_typ=pstringdef(p^.resulttype)^.string_typ)
  255. )
  256. or
  257. (
  258. (p^.left^.treetype=stringconstn) and
  259. (is_ansistring(p^.resulttype) and is_pchar(def))
  260. )
  261. or
  262. (
  263. (p^.left^.treetype=ordconstn) and
  264. (is_char(p^.resulttype) and (is_shortstring(def) or is_ansistring(def)))
  265. )
  266. { set can also be a not yet converted array constructor }
  267. or
  268. (
  269. (def^.deftype=setdef) and (p^.resulttype^.deftype=arraydef) and
  270. (parraydef(p^.resulttype)^.IsConstructor) and not(parraydef(p^.resulttype)^.IsVariant)
  271. )
  272. ;
  273. end;
  274. function is_in_limit(def_from,def_to : pdef) : boolean;
  275. begin
  276. is_in_limit:=(def_from^.deftype = orddef) and
  277. (def_to^.deftype = orddef) and
  278. (porddef(def_from)^.low>porddef(def_to)^.low) and
  279. (porddef(def_from)^.high<porddef(def_to)^.high);
  280. end;
  281. var
  282. is_const : boolean;
  283. begin
  284. { release registers! }
  285. { if procdefinition<>nil then we called firstpass already }
  286. { it seems to be bad because of the registers }
  287. { at least we can avoid the overloaded search !! }
  288. procs:=nil;
  289. { made this global for disposing !! }
  290. store_valid:=must_be_valid;
  291. must_be_valid:=false;
  292. oldcallprocsym:=aktcallprocsym;
  293. aktcallprocsym:=nil;
  294. inlined:=false;
  295. if assigned(p^.procdefinition) and
  296. ((p^.procdefinition^.options and poinline)<>0) then
  297. begin
  298. inlinecode:=p^.right;
  299. if assigned(inlinecode) then
  300. begin
  301. inlined:=true;
  302. p^.procdefinition^.options:=p^.procdefinition^.options and (not poinline);
  303. end;
  304. p^.right:=nil;
  305. end;
  306. { procedure variable ? }
  307. if assigned(p^.right) then
  308. begin
  309. { procedure does a call }
  310. procinfo.flags:=procinfo.flags or pi_do_call;
  311. { calc the correture value for the register }
  312. {$ifdef i386}
  313. for regi:=R_EAX to R_EDI do
  314. inc(reg_pushes[regi],t_times*2);
  315. {$endif}
  316. {$ifdef m68k}
  317. for regi:=R_D0 to R_A6 do
  318. inc(reg_pushes[regi],t_times*2);
  319. {$endif}
  320. { calculate the type of the parameters }
  321. if assigned(p^.left) then
  322. begin
  323. old_count_ref:=count_ref;
  324. count_ref:=false;
  325. firstcallparan(p^.left,nil);
  326. count_ref:=old_count_ref;
  327. if codegenerror then
  328. exit;
  329. end;
  330. firstpass(p^.right);
  331. { check the parameters }
  332. pdc:=pprocvardef(p^.right^.resulttype)^.para1;
  333. pt:=p^.left;
  334. while assigned(pdc) and assigned(pt) do
  335. begin
  336. pt:=pt^.right;
  337. pdc:=pdc^.next;
  338. end;
  339. if assigned(pt) or assigned(pdc) then
  340. CGMessage(parser_e_illegal_parameter_list);
  341. { insert type conversions }
  342. if assigned(p^.left) then
  343. begin
  344. old_count_ref:=count_ref;
  345. count_ref:=true;
  346. firstcallparan(p^.left,pprocvardef(p^.right^.resulttype)^.para1);
  347. count_ref:=old_count_ref;
  348. if codegenerror then
  349. exit;
  350. end;
  351. p^.resulttype:=pprocvardef(p^.right^.resulttype)^.retdef;
  352. { this was missing, leads to a bug below if
  353. the procvar is a function }
  354. p^.procdefinition:=pprocdef(p^.right^.resulttype);
  355. end
  356. else
  357. { not a procedure variable }
  358. begin
  359. { determine the type of the parameters }
  360. if assigned(p^.left) then
  361. begin
  362. old_count_ref:=count_ref;
  363. count_ref:=false;
  364. store_valid:=must_be_valid;
  365. must_be_valid:=false;
  366. firstcallparan(p^.left,nil);
  367. count_ref:=old_count_ref;
  368. must_be_valid:=store_valid;
  369. if codegenerror then
  370. exit;
  371. end;
  372. aktcallprocsym:=pprocsym(p^.symtableprocentry);
  373. { do we know the procedure to call ? }
  374. if not(assigned(p^.procdefinition)) then
  375. begin
  376. {$ifdef TEST_PROCSYMS}
  377. if (p^.unit_specific) or
  378. assigned(p^.methodpointer) then
  379. nextprocsym:=nil
  380. else while not assigned(procs) do
  381. begin
  382. symt:=p^.symtableproc;
  383. srsym:=nil;
  384. while assigned(symt^.next) and not assigned(srsym) do
  385. begin
  386. symt:=symt^.next;
  387. getsymonlyin(symt,actprocsym^.name);
  388. if assigned(srsym) then
  389. if srsym^.typ<>procsym then
  390. begin
  391. { reject all that is not a procedure }
  392. srsym:=nil;
  393. { don't search elsewhere }
  394. while assigned(symt^.next) do
  395. symt:=symt^.next;
  396. end;
  397. end;
  398. nextprocsym:=srsym;
  399. end;
  400. {$else TEST_PROCSYMS}
  401. nextprocsym:=nil;
  402. {$endif TEST_PROCSYMS}
  403. { determine length of parameter list }
  404. pt:=p^.left;
  405. paralength:=0;
  406. while assigned(pt) do
  407. begin
  408. inc(paralength);
  409. pt:=pt^.right;
  410. end;
  411. { link all procedures which have the same # of parameters }
  412. pd:=aktcallprocsym^.definition;
  413. while assigned(pd) do
  414. begin
  415. { we should also check that the overloaded function
  416. has been declared in a unit that is in the uses !! }
  417. { pd^.owner should be in the symtablestack !! }
  418. { Laenge der deklarierten Parameterliste feststellen: }
  419. { not necessary why nextprocsym field }
  420. {st:=symtablestack;
  421. if (pd^.owner^.symtabletype<>objectsymtable) then
  422. while assigned(st) do
  423. begin
  424. if (st=pd^.owner) then break;
  425. st:=st^.next;
  426. end;
  427. if assigned(st) then }
  428. begin
  429. pdc:=pd^.para1;
  430. l:=0;
  431. while assigned(pdc) do
  432. begin
  433. inc(l);
  434. pdc:=pdc^.next;
  435. end;
  436. { only when the # of parameter are equal }
  437. if (l=paralength) then
  438. begin
  439. new(hp);
  440. hp^.data:=pd;
  441. hp^.next:=procs;
  442. hp^.nextpara:=pd^.para1;
  443. hp^.firstpara:=pd^.para1;
  444. procs:=hp;
  445. end;
  446. end;
  447. pd:=pd^.nextoverloaded;
  448. end;
  449. { no procedures found? then there is something wrong
  450. with the parameter size }
  451. if not assigned(procs) and
  452. ((parsing_para_level=0) or assigned(p^.left)) and
  453. (nextprocsym=nil) then
  454. begin
  455. CGMessage(parser_e_wrong_parameter_size);
  456. aktcallprocsym^.write_parameter_lists;
  457. exit;
  458. end;
  459. { now we can compare parameter after parameter }
  460. pt:=p^.left;
  461. while assigned(pt) do
  462. begin
  463. { matches a parameter of one procedure exact ? }
  464. exactmatch:=false;
  465. hp:=procs;
  466. while assigned(hp) do
  467. begin
  468. if is_equal(pt,hp^.nextpara^.data) then
  469. begin
  470. if hp^.nextpara^.data=pt^.resulttype then
  471. begin
  472. pt^.exact_match_found:=true;
  473. hp^.nextpara^.argconvtyp:=act_exact;
  474. end
  475. else
  476. hp^.nextpara^.argconvtyp:=act_equal;
  477. exactmatch:=true;
  478. end
  479. else
  480. hp^.nextpara^.argconvtyp:=act_convertable;
  481. hp:=hp^.next;
  482. end;
  483. { .... if yes, del all the other procedures }
  484. if exactmatch then
  485. begin
  486. { the first .... }
  487. while (assigned(procs)) and not(is_equal(pt,procs^.nextpara^.data)) do
  488. begin
  489. hp:=procs^.next;
  490. dispose(procs);
  491. procs:=hp;
  492. end;
  493. { and the others }
  494. hp:=procs;
  495. while (assigned(hp)) and assigned(hp^.next) do
  496. begin
  497. if not(is_equal(pt,hp^.next^.nextpara^.data)) then
  498. begin
  499. hp2:=hp^.next^.next;
  500. dispose(hp^.next);
  501. hp^.next:=hp2;
  502. end
  503. else
  504. hp:=hp^.next;
  505. end;
  506. end
  507. { when a parameter matches exact, remove all procs
  508. which need typeconvs }
  509. else
  510. begin
  511. { the first... }
  512. while (assigned(procs)) and
  513. not(isconvertable(pt^.resulttype,procs^.nextpara^.data,
  514. hcvt,pt^.left^.treetype,false)) do
  515. begin
  516. hp:=procs^.next;
  517. dispose(procs);
  518. procs:=hp;
  519. end;
  520. { and the others }
  521. hp:=procs;
  522. while (assigned(hp)) and assigned(hp^.next) do
  523. begin
  524. if not(isconvertable(pt^.resulttype,hp^.next^.nextpara^.data,
  525. hcvt,pt^.left^.treetype,false)) then
  526. begin
  527. hp2:=hp^.next^.next;
  528. dispose(hp^.next);
  529. hp^.next:=hp2;
  530. end
  531. else
  532. hp:=hp^.next;
  533. end;
  534. end;
  535. { update nextpara for all procedures }
  536. hp:=procs;
  537. while assigned(hp) do
  538. begin
  539. hp^.nextpara:=hp^.nextpara^.next;
  540. hp:=hp^.next;
  541. end;
  542. { load next parameter }
  543. pt:=pt^.right;
  544. end;
  545. if not assigned(procs) then
  546. begin
  547. { there is an error, must be wrong type, because
  548. wrong size is already checked (PFV) }
  549. if ((parsing_para_level=0) or (p^.left<>nil)) and
  550. (nextprocsym=nil) then
  551. begin
  552. CGMessage(parser_e_wrong_parameter_type);
  553. aktcallprocsym^.write_parameter_lists;
  554. exit;
  555. end
  556. else
  557. begin
  558. { try to convert to procvar }
  559. p^.treetype:=loadn;
  560. p^.resulttype:=pprocsym(p^.symtableprocentry)^.definition;
  561. p^.symtableentry:=p^.symtableprocentry;
  562. p^.is_first:=false;
  563. p^.disposetyp:=dt_nothing;
  564. firstpass(p);
  565. exit;
  566. end;
  567. end;
  568. { if there are several choices left then for orddef }
  569. { if a type is totally included in the other }
  570. { we don't fear an overflow , }
  571. { so we can do as if it is an exact match }
  572. { this will convert integer to longint }
  573. { rather than to words }
  574. { conversion of byte to integer or longint }
  575. {would still not be solved }
  576. if assigned(procs) and assigned(procs^.next) then
  577. begin
  578. hp:=procs;
  579. while assigned(hp) do
  580. begin
  581. hp^.nextpara:=hp^.firstpara;
  582. hp:=hp^.next;
  583. end;
  584. pt:=p^.left;
  585. while assigned(pt) do
  586. begin
  587. { matches a parameter of one procedure exact ? }
  588. exactmatch:=false;
  589. def_from:=pt^.resulttype;
  590. hp:=procs;
  591. while assigned(hp) do
  592. begin
  593. if not is_equal(pt,hp^.nextpara^.data) then
  594. begin
  595. def_to:=hp^.nextpara^.data;
  596. if ((def_from^.deftype=orddef) and (def_to^.deftype=orddef)) and
  597. (is_in_limit(def_from,def_to) or
  598. ((hp^.nextpara^.paratyp=vs_var) and
  599. (def_from^.size=def_to^.size))) then
  600. begin
  601. exactmatch:=true;
  602. conv_to:=def_to;
  603. end;
  604. end;
  605. hp:=hp^.next;
  606. end;
  607. { .... if yes, del all the other procedures }
  608. if exactmatch then
  609. begin
  610. { the first .... }
  611. while (assigned(procs)) and not(is_in_limit(def_from,procs^.nextpara^.data)) do
  612. begin
  613. hp:=procs^.next;
  614. dispose(procs);
  615. procs:=hp;
  616. end;
  617. { and the others }
  618. hp:=procs;
  619. while (assigned(hp)) and assigned(hp^.next) do
  620. begin
  621. if not(is_in_limit(def_from,hp^.next^.nextpara^.data)) then
  622. begin
  623. hp2:=hp^.next^.next;
  624. dispose(hp^.next);
  625. hp^.next:=hp2;
  626. end
  627. else
  628. begin
  629. def_to:=hp^.next^.nextpara^.data;
  630. if (conv_to^.size>def_to^.size) or
  631. ((porddef(conv_to)^.low<porddef(def_to)^.low) and
  632. (porddef(conv_to)^.high>porddef(def_to)^.high)) then
  633. begin
  634. hp2:=procs;
  635. procs:=hp;
  636. conv_to:=def_to;
  637. dispose(hp2);
  638. end
  639. else
  640. hp:=hp^.next;
  641. end;
  642. end;
  643. end;
  644. { update nextpara for all procedures }
  645. hp:=procs;
  646. while assigned(hp) do
  647. begin
  648. hp^.nextpara:=hp^.nextpara^.next;
  649. hp:=hp^.next;
  650. end;
  651. pt:=pt^.right;
  652. end;
  653. end;
  654. { reset nextpara for all procs left }
  655. hp:=procs;
  656. while assigned(hp) do
  657. begin
  658. hp^.nextpara:=hp^.firstpara;
  659. hp:=hp^.next;
  660. end;
  661. { let's try to eliminate equal is exact is there }
  662. if assigned(procs^.next) then
  663. begin
  664. pt:=p^.left;
  665. while assigned(pt) do
  666. begin
  667. if pt^.exact_match_found then
  668. begin
  669. hp:=procs;
  670. procs:=nil;
  671. while assigned(hp) do
  672. begin
  673. hp2:=hp^.next;
  674. { keep the exact matches, dispose the others }
  675. if (hp^.nextpara^.data=pt^.resulttype) then
  676. begin
  677. hp^.next:=procs;
  678. procs:=hp;
  679. end
  680. else
  681. begin
  682. dispose(hp);
  683. end;
  684. hp:=hp2;
  685. end;
  686. end;
  687. { update nextpara for all procedures }
  688. hp:=procs;
  689. while assigned(hp) do
  690. begin
  691. hp^.nextpara:=hp^.nextpara^.next;
  692. hp:=hp^.next;
  693. end;
  694. pt:=pt^.right;
  695. end;
  696. end;
  697. if assigned(procs^.next) then
  698. begin
  699. CGMessage(cg_e_cant_choose_overload_function);
  700. aktcallprocsym^.write_parameter_lists;
  701. end;
  702. {$ifdef TEST_PROCSYMS}
  703. if (procs=nil) and assigned(nextprocsym) then
  704. begin
  705. p^.symtableprocentry:=nextprocsym;
  706. p^.symtableproc:=symt;
  707. end;
  708. end ; { of while assigned(p^.symtableprocentry) do }
  709. {$endif TEST_PROCSYMS}
  710. {$ifdef UseBrowser}
  711. if make_ref then
  712. begin
  713. procs^.data^.lastref:=new(pref,init(procs^.data^.lastref,@p^.fileinfo));
  714. if procs^.data^.defref=nil then
  715. procs^.data^.defref:=procs^.data^.lastref;
  716. end;
  717. {$endif UseBrowser}
  718. p^.procdefinition:=procs^.data;
  719. p^.resulttype:=procs^.data^.retdef;
  720. { big error for with statements
  721. p^.symtableproc:=p^.procdefinition^.owner;
  722. but neede for overloaded operators !! }
  723. if p^.symtableproc=nil then
  724. p^.symtableproc:=p^.procdefinition^.owner;
  725. p^.location.loc:=LOC_MEM;
  726. {$ifdef CHAINPROCSYMS}
  727. { object with method read;
  728. call to read(x) will be a usual procedure call }
  729. if assigned(p^.methodpointer) and
  730. (p^.procdefinition^._class=nil) then
  731. begin
  732. { not ok for extended }
  733. case p^.methodpointer^.treetype of
  734. typen,hnewn : fatalerror(no_para_match);
  735. end;
  736. disposetree(p^.methodpointer);
  737. p^.methodpointer:=nil;
  738. end;
  739. {$endif CHAINPROCSYMS}
  740. end; { end of procedure to call determination }
  741. is_const:=((p^.procdefinition^.options and pointernconst)<>0) and
  742. ((block_type=bt_const) or
  743. (assigned(p^.left) and (p^.left^.left^.treetype in [realconstn,ordconstn])));
  744. { handle predefined procedures }
  745. if ((p^.procdefinition^.options and pointernproc)<>0) or is_const then
  746. begin
  747. if assigned(p^.left) then
  748. begin
  749. { settextbuf needs two args }
  750. if assigned(p^.left^.right) then
  751. pt:=geninlinenode(pprocdef(p^.procdefinition)^.extnumber,is_const,p^.left)
  752. else
  753. begin
  754. pt:=geninlinenode(pprocdef(p^.procdefinition)^.extnumber,is_const,p^.left^.left);
  755. putnode(p^.left);
  756. end;
  757. end
  758. else
  759. begin
  760. pt:=geninlinenode(pprocdef(p^.procdefinition)^.extnumber,is_const,nil);
  761. end;
  762. putnode(p);
  763. firstpass(pt);
  764. p:=pt;
  765. must_be_valid:=store_valid;
  766. if codegenerror then
  767. exit;
  768. dispose(procs);
  769. exit;
  770. end
  771. else
  772. { no intern procedure => we do a call }
  773. { calc the correture value for the register }
  774. { handle predefined procedures }
  775. if (p^.procdefinition^.options and poinline)<>0 then
  776. begin
  777. if assigned(p^.methodpointer) then
  778. CGMessage(cg_e_unable_inline_object_methods);
  779. if assigned(p^.right) and (p^.right^.treetype<>procinlinen) then
  780. CGMessage(cg_e_unable_inline_procvar);
  781. { p^.treetype:=procinlinen; }
  782. if not assigned(p^.right) then
  783. begin
  784. if assigned(p^.procdefinition^.code) then
  785. inlinecode:=genprocinlinenode(p,ptree(p^.procdefinition^.code))
  786. else
  787. CGMessage(cg_e_no_code_for_inline_stored);
  788. if assigned(inlinecode) then
  789. begin
  790. { consider it has not inlined if called
  791. again inside the args }
  792. p^.procdefinition^.options:=p^.procdefinition^.options and (not poinline);
  793. firstpass(inlinecode);
  794. inlined:=true;
  795. end;
  796. end;
  797. end
  798. else
  799. procinfo.flags:=procinfo.flags or pi_do_call;
  800. { work trough all parameters to insert the type conversions }
  801. { !!! done now after internproc !! (PM) }
  802. if assigned(p^.left) then
  803. begin
  804. old_count_ref:=count_ref;
  805. count_ref:=true;
  806. firstcallparan(p^.left,p^.procdefinition^.para1);
  807. count_ref:=old_count_ref;
  808. end;
  809. {$ifdef i386}
  810. for regi:=R_EAX to R_EDI do
  811. begin
  812. if (p^.procdefinition^.usedregisters and ($80 shr word(regi)))<>0 then
  813. inc(reg_pushes[regi],t_times*2);
  814. end;
  815. {$endif}
  816. {$ifdef m68k}
  817. for regi:=R_D0 to R_A6 do
  818. begin
  819. if (p^.procdefinition^.usedregisters and ($800 shr word(regi)))<>0 then
  820. inc(reg_pushes[regi],t_times*2);
  821. end;
  822. {$endif}
  823. end;
  824. { ensure that the result type is set }
  825. p^.resulttype:=p^.procdefinition^.retdef;
  826. { get a register for the return value }
  827. if (p^.resulttype<>pdef(voiddef)) then
  828. begin
  829. if (p^.procdefinition^.options and poconstructor)<>0 then
  830. begin
  831. { extra handling of classes }
  832. { p^.methodpointer should be assigned! }
  833. if assigned(p^.methodpointer) and assigned(p^.methodpointer^.resulttype) and
  834. (p^.methodpointer^.resulttype^.deftype=classrefdef) then
  835. begin
  836. p^.location.loc:=LOC_REGISTER;
  837. p^.registers32:=1;
  838. { the result type depends on the classref }
  839. p^.resulttype:=pclassrefdef(p^.methodpointer^.resulttype)^.definition;
  840. end
  841. { a object constructor returns the result with the flags }
  842. else
  843. p^.location.loc:=LOC_FLAGS;
  844. end
  845. else
  846. begin
  847. {$ifdef SUPPORT_MMX}
  848. if (cs_mmx in aktlocalswitches) and
  849. is_mmx_able_array(p^.resulttype) then
  850. begin
  851. p^.location.loc:=LOC_MMXREGISTER;
  852. p^.registersmmx:=1;
  853. end
  854. else
  855. {$endif SUPPORT_MMX}
  856. if ret_in_acc(p^.resulttype) then
  857. begin
  858. p^.location.loc:=LOC_REGISTER;
  859. if is_64bitint(p^.resulttype) then
  860. p^.registers32:=2
  861. else
  862. p^.registers32:=1;
  863. end
  864. else if (p^.resulttype^.deftype=floatdef) then
  865. begin
  866. p^.location.loc:=LOC_FPU;
  867. p^.registersfpu:=1;
  868. end
  869. end;
  870. end;
  871. { a fpu can be used in any procedure !! }
  872. p^.registersfpu:=p^.procdefinition^.fpu_used;
  873. { if this is a call to a method calc the registers }
  874. if (p^.methodpointer<>nil) then
  875. begin
  876. case p^.methodpointer^.treetype of
  877. { but only, if this is not a supporting node }
  878. typen,hnewn : ;
  879. else
  880. begin
  881. { if ((p^.procdefinition^.options and (poconstructor or podestructor)) <> 0) and
  882. (assigned(p^.symtable) and (p^.symtable^.symtabletype=withsymtable) then
  883. begin
  884. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  885. end; Is accpeted by Delphi !! }
  886. { R.Assign is not a constructor !!! }
  887. { but for R^.Assign, R must be valid !! }
  888. if ((p^.procdefinition^.options and poconstructor) <> 0) or
  889. ((p^.methodpointer^.treetype=loadn) and
  890. ((pobjectdef(p^.methodpointer^.resulttype)^.options and oo_hasvirtual) = 0)) then
  891. must_be_valid:=false
  892. else
  893. must_be_valid:=true;
  894. firstpass(p^.methodpointer);
  895. p^.registersfpu:=max(p^.methodpointer^.registersfpu,p^.registersfpu);
  896. p^.registers32:=max(p^.methodpointer^.registers32,p^.registers32);
  897. {$ifdef SUPPORT_MMX}
  898. p^.registersmmx:=max(p^.methodpointer^.registersmmx,p^.registersmmx);
  899. {$endif SUPPORT_MMX}
  900. end;
  901. end;
  902. end;
  903. if inlined then
  904. begin
  905. p^.right:=inlinecode;
  906. p^.procdefinition^.options:=p^.procdefinition^.options or poinline;
  907. end;
  908. { determine the registers of the procedure variable }
  909. { is this OK for inlined procs also ?? (PM) }
  910. if assigned(p^.right) then
  911. begin
  912. p^.registersfpu:=max(p^.right^.registersfpu,p^.registersfpu);
  913. p^.registers32:=max(p^.right^.registers32,p^.registers32);
  914. {$ifdef SUPPORT_MMX}
  915. p^.registersmmx:=max(p^.right^.registersmmx,p^.registersmmx);
  916. {$endif SUPPORT_MMX}
  917. end;
  918. { determine the registers of the procedure }
  919. if assigned(p^.left) then
  920. begin
  921. p^.registersfpu:=max(p^.left^.registersfpu,p^.registersfpu);
  922. p^.registers32:=max(p^.left^.registers32,p^.registers32);
  923. {$ifdef SUPPORT_MMX}
  924. p^.registersmmx:=max(p^.left^.registersmmx,p^.registersmmx);
  925. {$endif SUPPORT_MMX}
  926. end;
  927. if assigned(procs) then
  928. dispose(procs);
  929. aktcallprocsym:=oldcallprocsym;
  930. must_be_valid:=store_valid;
  931. end;
  932. {*****************************************************************************
  933. FirstProcInlineN
  934. *****************************************************************************}
  935. procedure firstprocinline(var p : ptree);
  936. begin
  937. { left contains the code in tree form }
  938. { but it has already been firstpassed }
  939. { so firstpass(p^.left); does not seem required }
  940. { might be required later if we change the arg handling !! }
  941. end;
  942. end.
  943. {
  944. $Log$
  945. Revision 1.17 1998-12-11 00:03:52 peter
  946. + globtype,tokens,version unit splitted from globals
  947. Revision 1.16 1998/12/10 14:57:52 pierre
  948. * fix for operators
  949. Revision 1.15 1998/12/10 09:47:32 florian
  950. + basic operations with int64/qord (compiler with -dint64)
  951. + rtti of enumerations extended: names are now written
  952. Revision 1.14 1998/11/27 14:50:52 peter
  953. + open strings, $P switch support
  954. Revision 1.13 1998/11/24 17:03:51 peter
  955. * fixed exactmatch removings
  956. Revision 1.12 1998/11/16 10:18:10 peter
  957. * fixes for ansistrings
  958. Revision 1.11 1998/11/10 10:09:17 peter
  959. * va_list -> array of const
  960. Revision 1.10 1998/11/09 11:44:41 peter
  961. + va_list for printf support
  962. Revision 1.9 1998/10/28 18:26:22 pierre
  963. * removed some erros after other errors (introduced by useexcept)
  964. * stabs works again correctly (for how long !)
  965. Revision 1.8 1998/10/09 16:36:09 pierre
  966. * some memory leaks specific to usebrowser define fixed
  967. * removed tmodule.implsymtable (was like tmodule.localsymtable)
  968. Revision 1.7 1998/10/06 20:49:09 peter
  969. * m68k compiler compiles again
  970. Revision 1.6 1998/10/02 09:24:22 peter
  971. * more constant expression evaluators
  972. Revision 1.5 1998/09/28 11:22:17 pierre
  973. * did not compile for browser
  974. * merge from fixes
  975. Revision 1.4 1998/09/27 10:16:24 florian
  976. * type casts pchar<->ansistring fixed
  977. * ansistring[..] calls does now an unique call
  978. Revision 1.3 1998/09/24 14:27:40 peter
  979. * some better support for openarray
  980. Revision 1.2 1998/09/24 09:02:16 peter
  981. * rewritten isconvertable to use case
  982. * array of .. and single variable are compatible
  983. Revision 1.1 1998/09/23 20:42:24 peter
  984. * splitted pass_1
  985. }