tccal.pas 46 KB

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