tccal.pas 42 KB

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