tcinl.pas 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Type checking and register allocation for inline 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 tcinl;
  19. interface
  20. uses
  21. tree;
  22. procedure firstinline(var p : ptree);
  23. implementation
  24. uses
  25. cobjects,verbose,globals,systems,
  26. globtype,
  27. symtable,aasm,types,
  28. hcodegen,htypechk,pass_1,
  29. tccal
  30. {$ifdef i386}
  31. {$ifdef ag386bin}
  32. ,i386base
  33. {$else}
  34. ,i386
  35. {$endif}
  36. ,tgeni386
  37. {$endif}
  38. {$ifdef m68k}
  39. ,m68k,tgen68k
  40. {$endif}
  41. ;
  42. {*****************************************************************************
  43. FirstInLine
  44. *****************************************************************************}
  45. procedure firstinline(var p : ptree);
  46. var
  47. vl,vl2 : longint;
  48. vr : bestreal;
  49. p1,hp,hpp : ptree;
  50. {$ifndef NOCOLONCHECK}
  51. frac_para,length_para : ptree;
  52. {$endif ndef NOCOLONCHECK}
  53. store_count_ref,
  54. isreal,
  55. dowrite,
  56. store_valid,
  57. file_is_typed : boolean;
  58. procedure do_lowhigh(adef : pdef);
  59. var
  60. v : longint;
  61. enum : penumsym;
  62. begin
  63. case Adef^.deftype of
  64. orddef:
  65. begin
  66. if p^.inlinenumber=in_low_x then
  67. v:=porddef(Adef)^.low
  68. else
  69. v:=porddef(Adef)^.high;
  70. hp:=genordinalconstnode(v,adef);
  71. firstpass(hp);
  72. disposetree(p);
  73. p:=hp;
  74. end;
  75. enumdef:
  76. begin
  77. enum:=Penumdef(Adef)^.firstenum;
  78. if p^.inlinenumber=in_high_x then
  79. while enum^.nextenum<>nil do
  80. enum:=enum^.nextenum;
  81. hp:=genenumnode(enum);
  82. disposetree(p);
  83. p:=hp;
  84. end
  85. end;
  86. end;
  87. begin
  88. store_valid:=must_be_valid;
  89. store_count_ref:=count_ref;
  90. count_ref:=false;
  91. if not (p^.inlinenumber in [in_read_x,in_readln_x,in_sizeof_x,
  92. in_typeof_x,in_ord_x,in_str_x_string,
  93. {$IfnDef OLDVAL}
  94. in_val_x,
  95. {$EndIf OLDVAL}
  96. in_reset_typedfile,in_rewrite_typedfile]) then
  97. must_be_valid:=true
  98. else
  99. must_be_valid:=false;
  100. { if we handle writeln; p^.left contains no valid address }
  101. if assigned(p^.left) then
  102. begin
  103. if p^.left^.treetype=callparan then
  104. firstcallparan(p^.left,nil)
  105. else
  106. firstpass(p^.left);
  107. left_right_max(p);
  108. set_location(p^.location,p^.left^.location);
  109. end;
  110. { handle intern constant functions in separate case }
  111. if p^.inlineconst then
  112. begin
  113. { no parameters? }
  114. if not assigned(p^.left) then
  115. begin
  116. case p^.inlinenumber of
  117. in_const_pi :
  118. hp:=genrealconstnode(pi);
  119. else
  120. internalerror(89);
  121. end;
  122. end
  123. else
  124. { process constant expression with parameter }
  125. begin
  126. vl:=0;
  127. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  128. vr:=0;
  129. isreal:=false;
  130. case p^.left^.treetype of
  131. realconstn :
  132. begin
  133. isreal:=true;
  134. vr:=p^.left^.value_real;
  135. end;
  136. ordconstn :
  137. vl:=p^.left^.value;
  138. callparan :
  139. begin
  140. { both exists, else it was not generated }
  141. vl:=p^.left^.left^.value;
  142. vl2:=p^.left^.right^.left^.value;
  143. end;
  144. else
  145. CGMessage(cg_e_illegal_expression);
  146. end;
  147. case p^.inlinenumber of
  148. in_const_trunc :
  149. begin
  150. if isreal then
  151. begin
  152. if (vr>=2147483648.0) or (vr<=-2147483649.0) then
  153. begin
  154. CGMessage(parser_e_range_check_error);
  155. hp:=genordinalconstnode(1,s32bitdef)
  156. end
  157. else
  158. hp:=genordinalconstnode(trunc(vr),s32bitdef)
  159. end
  160. else
  161. hp:=genordinalconstnode(trunc(vl),s32bitdef);
  162. end;
  163. in_const_round :
  164. begin
  165. if isreal then
  166. begin
  167. if (vr>=2147483647.5) or (vr<=-2147483648.5) then
  168. begin
  169. CGMessage(parser_e_range_check_error);
  170. hp:=genordinalconstnode(1,s32bitdef)
  171. end
  172. else
  173. hp:=genordinalconstnode(round(vr),s32bitdef)
  174. end
  175. else
  176. hp:=genordinalconstnode(round(vl),s32bitdef);
  177. end;
  178. in_const_frac :
  179. begin
  180. if isreal then
  181. hp:=genrealconstnode(frac(vr))
  182. else
  183. hp:=genrealconstnode(frac(vl));
  184. end;
  185. in_const_int :
  186. begin
  187. if isreal then
  188. hp:=genrealconstnode(int(vr))
  189. else
  190. hp:=genrealconstnode(int(vl));
  191. end;
  192. in_const_abs :
  193. begin
  194. if isreal then
  195. hp:=genrealconstnode(abs(vr))
  196. else
  197. hp:=genordinalconstnode(abs(vl),p^.left^.resulttype);
  198. end;
  199. in_const_sqr :
  200. begin
  201. if isreal then
  202. hp:=genrealconstnode(sqr(vr))
  203. else
  204. hp:=genordinalconstnode(sqr(vl),p^.left^.resulttype);
  205. end;
  206. in_const_odd :
  207. begin
  208. if isreal then
  209. CGMessage(type_e_integer_expr_expected)
  210. else
  211. hp:=genordinalconstnode(byte(odd(vl)),booldef);
  212. end;
  213. in_const_swap_word :
  214. begin
  215. if isreal then
  216. CGMessage(type_e_integer_expr_expected)
  217. else
  218. hp:=genordinalconstnode((vl and $ff) shl 8+(vl shr 8),p^.left^.resulttype);
  219. end;
  220. in_const_swap_long :
  221. begin
  222. if isreal then
  223. CGMessage(type_e_mismatch)
  224. else
  225. hp:=genordinalconstnode((vl and $ffff) shl 16+(vl shr 16),p^.left^.resulttype);
  226. end;
  227. in_const_ptr :
  228. begin
  229. if isreal then
  230. CGMessage(type_e_mismatch)
  231. else
  232. hp:=genordinalconstnode((vl2 shl 16) or vl,voidpointerdef);
  233. end;
  234. in_const_sqrt :
  235. begin
  236. if isreal then
  237. hp:=genrealconstnode(sqrt(vr))
  238. else
  239. hp:=genrealconstnode(sqrt(vl));
  240. end;
  241. in_const_arctan :
  242. begin
  243. if isreal then
  244. hp:=genrealconstnode(arctan(vr))
  245. else
  246. hp:=genrealconstnode(arctan(vl));
  247. end;
  248. in_const_cos :
  249. begin
  250. if isreal then
  251. hp:=genrealconstnode(cos(vr))
  252. else
  253. hp:=genrealconstnode(cos(vl));
  254. end;
  255. in_const_sin :
  256. begin
  257. if isreal then
  258. hp:=genrealconstnode(sin(vr))
  259. else
  260. hp:=genrealconstnode(sin(vl));
  261. end;
  262. in_const_exp :
  263. begin
  264. if isreal then
  265. hp:=genrealconstnode(exp(vr))
  266. else
  267. hp:=genrealconstnode(exp(vl));
  268. end;
  269. in_const_ln :
  270. begin
  271. if isreal then
  272. hp:=genrealconstnode(ln(vr))
  273. else
  274. hp:=genrealconstnode(ln(vl));
  275. end;
  276. else
  277. internalerror(88);
  278. end;
  279. end;
  280. disposetree(p);
  281. firstpass(hp);
  282. p:=hp;
  283. end
  284. else
  285. begin
  286. case p^.inlinenumber of
  287. in_lo_long,
  288. in_hi_long,
  289. in_lo_word,
  290. in_hi_word:
  291. begin
  292. if p^.registers32<1 then
  293. p^.registers32:=1;
  294. if p^.inlinenumber in [in_lo_word,in_hi_word] then
  295. p^.resulttype:=u8bitdef
  296. else
  297. begin
  298. p^.resulttype:=u16bitdef;
  299. if (m_tp in aktmodeswitches) or
  300. (m_delphi in aktmodeswitches) then
  301. CGMessage(type_w_maybe_wrong_hi_lo);
  302. end;
  303. p^.location.loc:=LOC_REGISTER;
  304. if not is_integer(p^.left^.resulttype) then
  305. CGMessage(type_e_mismatch)
  306. else
  307. begin
  308. if p^.left^.treetype=ordconstn then
  309. begin
  310. case p^.inlinenumber of
  311. in_lo_word : hp:=genordinalconstnode(p^.left^.value and $ff,p^.left^.resulttype);
  312. in_hi_word : hp:=genordinalconstnode(p^.left^.value shr 8,p^.left^.resulttype);
  313. in_lo_long : hp:=genordinalconstnode(p^.left^.value and $ffff,p^.left^.resulttype);
  314. in_hi_long : hp:=genordinalconstnode(p^.left^.value shr 16,p^.left^.resulttype);
  315. end;
  316. disposetree(p);
  317. firstpass(hp);
  318. p:=hp;
  319. end;
  320. end;
  321. end;
  322. in_sizeof_x:
  323. begin
  324. if push_high_param(p^.left^.resulttype) then
  325. begin
  326. getsymonlyin(p^.left^.symtable,'high'+pvarsym(p^.left^.symtableentry)^.name);
  327. hp:=gennode(addn,genloadnode(pvarsym(srsym),p^.left^.symtable),
  328. genordinalconstnode(1,s32bitdef));
  329. if (p^.left^.resulttype^.deftype=arraydef) and
  330. (parraydef(p^.left^.resulttype)^.elesize<>1) then
  331. hp:=gennode(muln,hp,genordinalconstnode(parraydef(p^.left^.resulttype)^.elesize,s32bitdef));
  332. disposetree(p);
  333. p:=hp;
  334. firstpass(p);
  335. end;
  336. if p^.registers32<1 then
  337. p^.registers32:=1;
  338. p^.resulttype:=s32bitdef;
  339. p^.location.loc:=LOC_REGISTER;
  340. end;
  341. in_typeof_x:
  342. begin
  343. if p^.registers32<1 then
  344. p^.registers32:=1;
  345. p^.location.loc:=LOC_REGISTER;
  346. p^.resulttype:=voidpointerdef;
  347. end;
  348. in_ord_x:
  349. begin
  350. if (p^.left^.treetype=ordconstn) then
  351. begin
  352. hp:=genordinalconstnode(p^.left^.value,s32bitdef);
  353. disposetree(p);
  354. p:=hp;
  355. firstpass(p);
  356. end
  357. else
  358. begin
  359. if (p^.left^.resulttype^.deftype=orddef) then
  360. if (porddef(p^.left^.resulttype)^.typ in [uchar,bool8bit]) then
  361. begin
  362. if porddef(p^.left^.resulttype)^.typ=bool8bit then
  363. begin
  364. hp:=gentypeconvnode(p^.left,u8bitdef);
  365. putnode(p);
  366. p:=hp;
  367. p^.convtyp:=tc_bool_2_int;
  368. p^.explizit:=true;
  369. firstpass(p);
  370. end
  371. else
  372. begin
  373. hp:=gentypeconvnode(p^.left,u8bitdef);
  374. putnode(p);
  375. p:=hp;
  376. p^.explizit:=true;
  377. firstpass(p);
  378. end;
  379. end
  380. { can this happen ? }
  381. else if (porddef(p^.left^.resulttype)^.typ=uvoid) then
  382. CGMessage(type_e_mismatch)
  383. else
  384. { all other orddef need no transformation }
  385. begin
  386. hp:=p^.left;
  387. putnode(p);
  388. p:=hp;
  389. end
  390. else if (p^.left^.resulttype^.deftype=enumdef) then
  391. begin
  392. hp:=gentypeconvnode(p^.left,s32bitdef);
  393. putnode(p);
  394. p:=hp;
  395. p^.explizit:=true;
  396. firstpass(p);
  397. end
  398. else
  399. begin
  400. { can anything else be ord() ?}
  401. CGMessage(type_e_mismatch);
  402. end;
  403. end;
  404. end;
  405. in_chr_byte:
  406. begin
  407. hp:=gentypeconvnode(p^.left,cchardef);
  408. putnode(p);
  409. p:=hp;
  410. p^.explizit:=true;
  411. firstpass(p);
  412. end;
  413. in_length_string:
  414. begin
  415. if is_ansistring(p^.left^.resulttype) then
  416. p^.resulttype:=s32bitdef
  417. else
  418. p^.resulttype:=u8bitdef;
  419. { we don't need string conversations here }
  420. if (p^.left^.treetype=typeconvn) and
  421. (p^.left^.left^.resulttype^.deftype=stringdef) then
  422. begin
  423. hp:=p^.left^.left;
  424. putnode(p^.left);
  425. p^.left:=hp;
  426. end;
  427. { check the type, must be string or char }
  428. if (p^.left^.resulttype^.deftype<>stringdef) and
  429. (not is_char(p^.left^.resulttype)) then
  430. CGMessage(type_e_mismatch);
  431. { evaluates length of constant strings direct }
  432. if (p^.left^.treetype=stringconstn) then
  433. begin
  434. hp:=genordinalconstnode(p^.left^.length,s32bitdef);
  435. disposetree(p);
  436. firstpass(hp);
  437. p:=hp;
  438. end
  439. { length of char is one allways }
  440. else if is_constcharnode(p^.left) then
  441. begin
  442. hp:=genordinalconstnode(1,s32bitdef);
  443. disposetree(p);
  444. firstpass(hp);
  445. p:=hp;
  446. end;
  447. end;
  448. in_assigned_x:
  449. begin
  450. p^.resulttype:=booldef;
  451. p^.location.loc:=LOC_FLAGS;
  452. end;
  453. in_pred_x,
  454. in_succ_x:
  455. begin
  456. inc(p^.registers32);
  457. p^.resulttype:=p^.left^.resulttype;
  458. p^.location.loc:=LOC_REGISTER;
  459. if not is_ordinal(p^.resulttype) then
  460. CGMessage(type_e_ordinal_expr_expected)
  461. else
  462. begin
  463. if (p^.resulttype^.deftype=enumdef) and
  464. (penumdef(p^.resulttype)^.has_jumps) then
  465. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible)
  466. else
  467. if p^.left^.treetype=ordconstn then
  468. begin
  469. if p^.inlinenumber=in_succ_x then
  470. hp:=genordinalconstnode(p^.left^.value+1,p^.left^.resulttype)
  471. else
  472. hp:=genordinalconstnode(p^.left^.value-1,p^.left^.resulttype);
  473. disposetree(p);
  474. firstpass(hp);
  475. p:=hp;
  476. end;
  477. end;
  478. end;
  479. in_inc_x,
  480. in_dec_x:
  481. begin
  482. p^.resulttype:=voiddef;
  483. if assigned(p^.left) then
  484. begin
  485. firstcallparan(p^.left,nil);
  486. if codegenerror then
  487. exit;
  488. { first param must be var }
  489. if is_constnode(p^.left^.left) then
  490. CGMessage(type_e_variable_id_expected);
  491. { check type }
  492. if (p^.left^.resulttype^.deftype in [enumdef,pointerdef]) or
  493. is_ordinal(p^.left^.resulttype) then
  494. begin
  495. { two paras ? }
  496. if assigned(p^.left^.right) then
  497. begin
  498. { insert a type conversion }
  499. { the second param is always longint }
  500. p^.left^.right^.left:=gentypeconvnode(p^.left^.right^.left,s32bitdef);
  501. { check the type conversion }
  502. firstpass(p^.left^.right^.left);
  503. { need we an additional register ? }
  504. if not(is_constintnode(p^.left^.right^.left)) and
  505. (p^.left^.right^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) and
  506. (p^.left^.right^.left^.registers32<1) then
  507. inc(p^.registers32);
  508. if assigned(p^.left^.right^.right) then
  509. CGMessage(cg_e_illegal_expression);
  510. end;
  511. end
  512. else
  513. CGMessage(type_e_ordinal_expr_expected);
  514. end
  515. else
  516. CGMessage(type_e_mismatch);
  517. end;
  518. in_read_x,
  519. in_readln_x,
  520. in_write_x,
  521. in_writeln_x :
  522. begin
  523. { needs a call }
  524. procinfo.flags:=procinfo.flags or pi_do_call;
  525. p^.resulttype:=voiddef;
  526. { we must know if it is a typed file or not }
  527. { but we must first do the firstpass for it }
  528. file_is_typed:=false;
  529. if assigned(p^.left) then
  530. begin
  531. dowrite:=(p^.inlinenumber in [in_write_x,in_writeln_x]);
  532. firstcallparan(p^.left,nil);
  533. { now we can check }
  534. hp:=p^.left;
  535. while assigned(hp^.right) do
  536. hp:=hp^.right;
  537. { if resulttype is not assigned, then automatically }
  538. { file is not typed. }
  539. if assigned(hp) and assigned(hp^.resulttype) then
  540. Begin
  541. if (hp^.resulttype^.deftype=filedef) and
  542. (pfiledef(hp^.resulttype)^.filetype=ft_typed) then
  543. begin
  544. file_is_typed:=true;
  545. { test the type }
  546. hpp:=p^.left;
  547. while (hpp<>hp) do
  548. begin
  549. if (hpp^.left^.treetype=typen) then
  550. CGMessage(type_e_cant_read_write_type);
  551. if not is_equal(hpp^.resulttype,pfiledef(hp^.resulttype)^.typed_as) then
  552. CGMessage(type_e_mismatch);
  553. if hpp^.resulttype^.deftype=stringdef then
  554. { generate the high() value for the shortstring }
  555. if (not dowrite) and
  556. is_shortstring(hpp^.left^.resulttype) then
  557. gen_high_tree(hpp,true);
  558. hpp:=hpp^.right;
  559. end;
  560. end;
  561. end; { endif assigned(hp) }
  562. { insert type conversions for write(ln) }
  563. if (not file_is_typed) then
  564. begin
  565. hp:=p^.left;
  566. while assigned(hp) do
  567. begin
  568. if (hp^.left^.treetype=typen) then
  569. CGMessage(type_e_cant_read_write_type);
  570. if assigned(hp^.left^.resulttype) then
  571. begin
  572. isreal:=false;
  573. { support writeln(procvar) for tp7 }
  574. if (m_tp_procvar in aktmodeswitches) and (hp^.left^.resulttype^.deftype=procvardef) then
  575. begin
  576. p1:=gencallnode(nil,nil);
  577. p1^.right:=hp^.left;
  578. p1^.resulttype:=pprocvardef(hp^.left^.resulttype)^.retdef;
  579. firstpass(p1);
  580. hp^.left:=p1;
  581. end;
  582. case hp^.left^.resulttype^.deftype of
  583. filedef :
  584. begin
  585. { only allowed as first parameter }
  586. if assigned(hp^.right) then
  587. CGMessage(type_e_cant_read_write_type);
  588. end;
  589. stringdef :
  590. begin
  591. { generate the high() value for the shortstring }
  592. if (not dowrite) and
  593. is_shortstring(hp^.left^.resulttype) then
  594. gen_high_tree(hp,true);
  595. end;
  596. pointerdef :
  597. begin
  598. if not is_pchar(hp^.left^.resulttype) then
  599. CGMessage(type_e_cant_read_write_type);
  600. end;
  601. floatdef :
  602. begin
  603. isreal:=true;
  604. end;
  605. orddef :
  606. begin
  607. case porddef(hp^.left^.resulttype)^.typ of
  608. uchar,
  609. u32bit,s32bit,
  610. u64bit,s64bitint:
  611. ;
  612. u8bit,s8bit,
  613. u16bit,s16bit :
  614. if dowrite then
  615. hp^.left:=gentypeconvnode(hp^.left,s32bitdef);
  616. bool8bit,
  617. bool16bit,
  618. bool32bit :
  619. if dowrite then
  620. hp^.left:=gentypeconvnode(hp^.left,booldef)
  621. else
  622. CGMessage(type_e_cant_read_write_type);
  623. else
  624. CGMessage(type_e_cant_read_write_type);
  625. end;
  626. end;
  627. arraydef :
  628. begin
  629. if not is_chararray(hp^.left^.resulttype) then
  630. CGMessage(type_e_cant_read_write_type);
  631. end;
  632. else
  633. CGMessage(type_e_cant_read_write_type);
  634. end;
  635. { some format options ? }
  636. if hp^.is_colon_para then
  637. begin
  638. if hp^.right^.is_colon_para then
  639. begin
  640. frac_para:=hp;
  641. length_para:=hp^.right;
  642. hp:=hp^.right;
  643. hpp:=hp^.right;
  644. end
  645. else
  646. begin
  647. length_para:=hp;
  648. frac_para:=nil;
  649. hpp:=hp^.right;
  650. end;
  651. isreal:=hpp^.resulttype^.deftype=floatdef;
  652. if (not is_integer(length_para^.resulttype)) then
  653. CGMessage(type_e_integer_expr_expected)
  654. else
  655. length_para^.left:=gentypeconvnode(length_para^.left,s32bitdef);
  656. if assigned(frac_para) then
  657. begin
  658. if isreal then
  659. begin
  660. if (not is_integer(frac_para^.resulttype)) then
  661. CGMessage(type_e_integer_expr_expected)
  662. else
  663. frac_para^.left:=gentypeconvnode(frac_para^.left,s32bitdef);
  664. end
  665. else
  666. CGMessage(parser_e_illegal_colon_qualifier);
  667. end;
  668. { do the checking for the colon'd arg }
  669. hp:=length_para;
  670. end;
  671. end;
  672. hp:=hp^.right;
  673. end;
  674. end;
  675. { pass all parameters again for the typeconversions }
  676. if codegenerror then
  677. exit;
  678. must_be_valid:=true;
  679. firstcallparan(p^.left,nil);
  680. { calc registers }
  681. left_right_max(p);
  682. end;
  683. end;
  684. in_settextbuf_file_x :
  685. begin
  686. { warning here p^.left is the callparannode
  687. not the argument directly }
  688. { p^.left^.left is text var }
  689. { p^.left^.right^.left is the buffer var }
  690. { firstcallparan(p^.left,nil);
  691. already done in firstcalln }
  692. { now we know the type of buffer }
  693. getsymonlyin(systemunit,'SETTEXTBUF');
  694. hp:=gencallnode(pprocsym(srsym),systemunit);
  695. hp^.left:=gencallparanode(
  696. genordinalconstnode(p^.left^.left^.resulttype^.size,s32bitdef),p^.left);
  697. putnode(p);
  698. p:=hp;
  699. firstpass(p);
  700. end;
  701. { the firstpass of the arg has been done in firstcalln ? }
  702. in_reset_typedfile,
  703. in_rewrite_typedfile :
  704. begin
  705. procinfo.flags:=procinfo.flags or pi_do_call;
  706. { to be sure the right definition is loaded }
  707. p^.left^.resulttype:=nil;
  708. firstpass(p^.left);
  709. p^.resulttype:=voiddef;
  710. end;
  711. in_str_x_string :
  712. begin
  713. procinfo.flags:=procinfo.flags or pi_do_call;
  714. p^.resulttype:=voiddef;
  715. { check the amount of parameters }
  716. if not(assigned(p^.left)) or
  717. not(assigned(p^.left^.right)) then
  718. begin
  719. CGMessage(parser_e_wrong_parameter_size);
  720. exit;
  721. end;
  722. { first pass just the string for first local use }
  723. hp:=p^.left^.right;
  724. must_be_valid:=false;
  725. count_ref:=true;
  726. p^.left^.right:=nil;
  727. firstcallparan(p^.left,nil);
  728. { remove warning when result is passed }
  729. if (p^.left^.left^.treetype=funcretn) then
  730. procinfo.funcret_is_valid:=true;
  731. must_be_valid:=true;
  732. p^.left^.right:=hp;
  733. firstcallparan(p^.left^.right,nil);
  734. hp:=p^.left;
  735. { valid string ? }
  736. if not assigned(hp) or
  737. (hp^.left^.resulttype^.deftype<>stringdef) or
  738. (hp^.right=nil) or
  739. (hp^.left^.location.loc<>LOC_REFERENCE) then
  740. CGMessage(cg_e_illegal_expression);
  741. { generate the high() value for the shortstring }
  742. if is_shortstring(hp^.left^.resulttype) then
  743. gen_high_tree(hp,true);
  744. { !!!! check length of string }
  745. while assigned(hp^.right) do
  746. hp:=hp^.right;
  747. { check and convert the first param }
  748. if hp^.is_colon_para then
  749. CGMessage(cg_e_illegal_expression);
  750. isreal:=false;
  751. case hp^.resulttype^.deftype of
  752. orddef :
  753. begin
  754. case porddef(hp^.left^.resulttype)^.typ of
  755. u32bit,s32bit,
  756. s64bitint,u64bit:
  757. ;
  758. u8bit,s8bit,
  759. u16bit,s16bit:
  760. hp^.left:=gentypeconvnode(hp^.left,s32bitdef);
  761. else
  762. CGMessage(type_e_integer_or_real_expr_expected);
  763. end;
  764. end;
  765. floatdef :
  766. begin
  767. isreal:=true;
  768. end;
  769. else
  770. CGMessage(type_e_integer_or_real_expr_expected);
  771. end;
  772. { some format options ? }
  773. hpp:=p^.left^.right;
  774. if assigned(hpp) and hpp^.is_colon_para then
  775. begin
  776. if (not is_integer(hpp^.resulttype)) then
  777. CGMessage(type_e_integer_expr_expected)
  778. else
  779. hpp^.left:=gentypeconvnode(hpp^.left,s32bitdef);
  780. hpp:=hpp^.right;
  781. if assigned(hpp) and hpp^.is_colon_para then
  782. begin
  783. if isreal then
  784. begin
  785. if (not is_integer(hpp^.resulttype)) then
  786. CGMessage(type_e_integer_expr_expected)
  787. else
  788. hpp^.left:=gentypeconvnode(hpp^.left,s32bitdef);
  789. end
  790. else
  791. CGMessage(parser_e_illegal_colon_qualifier);
  792. end;
  793. end;
  794. { for first local use }
  795. must_be_valid:=false;
  796. count_ref:=true;
  797. { pass all parameters again for the typeconversions }
  798. if codegenerror then
  799. exit;
  800. must_be_valid:=true;
  801. firstcallparan(p^.left,nil);
  802. { calc registers }
  803. left_right_max(p);
  804. end;
  805. {$IfnDef OLDVAL}
  806. in_val_x :
  807. begin
  808. procinfo.flags:=procinfo.flags or pi_do_call;
  809. p^.resulttype:=voiddef;
  810. { check the amount of parameters }
  811. if not(assigned(p^.left)) or
  812. not(assigned(p^.left^.right)) then
  813. begin
  814. CGMessage(parser_e_wrong_parameter_size);
  815. exit;
  816. end;
  817. If Assigned(p^.left^.right^.right) Then
  818. {there is a "code" parameter}
  819. Begin
  820. { first pass just the code parameter for first local use}
  821. hp := p^.left^.right;
  822. p^.left^.right := nil;
  823. must_be_valid := false;
  824. count_ref := true;
  825. firstcallparan(p^.left, nil);
  826. if codegenerror then exit;
  827. p^.left^.right := hp;
  828. {code has to be a var parameter}
  829. if (p^.left^.left^.location.loc<>LOC_REFERENCE) then
  830. CGMessage(type_e_variable_id_expected)
  831. else
  832. if (p^.left^.left^.resulttype^.deftype <> orddef) or
  833. not(porddef(p^.left^.left^.resulttype)^.typ in
  834. [u16bit,s16bit,u32bit,s32bit]) then
  835. CGMessage(type_e_mismatch);
  836. hpp := p^.left^.right
  837. End
  838. Else hpp := p^.left;
  839. {now hpp = the destination value tree}
  840. { first pass just the destination parameter for first local use}
  841. hp:=hpp^.right;
  842. must_be_valid:=false;
  843. count_ref:=true;
  844. hpp^.right:=nil;
  845. {hpp = destination}
  846. firstcallparan(hpp,nil);
  847. if codegenerror then
  848. exit;
  849. { remove warning when result is passed }
  850. if (hpp^.left^.treetype=funcretn) then
  851. procinfo.funcret_is_valid:=true;
  852. hpp^.right := hp;
  853. if (hpp^.left^.location.loc<>LOC_REFERENCE) then
  854. CGMessage(type_e_variable_id_expected)
  855. else
  856. If Not((hpp^.left^.resulttype^.deftype = floatdef) or
  857. ((hpp^.left^.resulttype^.deftype = orddef) And
  858. (POrdDef(hpp^.left^.resulttype)^.typ in
  859. [u32bit,s32bit,{s64bitint,u64bit, -- not supported yet in RTL}
  860. u8bit,s8bit,u16bit,s16bit])))
  861. Then CGMessage(type_e_mismatch);
  862. must_be_valid:=true;
  863. {hp = source (String)}
  864. count_ref := false;
  865. must_be_valid := true;
  866. firstcallparan(hp,nil);
  867. if codegenerror then exit;
  868. If (hp^.resulttype^.deftype<>stringdef) then
  869. CGMessage(type_e_mismatch);
  870. { firstcallparan(p^.left,nil);}
  871. { calc registers }
  872. left_right_max(p);
  873. end;
  874. {$EndIf OLDVAL}
  875. in_include_x_y,
  876. in_exclude_x_y:
  877. begin
  878. p^.resulttype:=voiddef;
  879. if assigned(p^.left) then
  880. begin
  881. firstcallparan(p^.left,nil);
  882. p^.registers32:=p^.left^.registers32;
  883. p^.registersfpu:=p^.left^.registersfpu;
  884. {$ifdef SUPPORT_MMX}
  885. p^.registersmmx:=p^.left^.registersmmx;
  886. {$endif SUPPORT_MMX}
  887. { remove warning when result is passed }
  888. if (p^.left^.left^.treetype=funcretn) then
  889. procinfo.funcret_is_valid:=true;
  890. { first param must be var }
  891. if (p^.left^.left^.location.loc<>LOC_REFERENCE) and
  892. (p^.left^.left^.location.loc<>LOC_CREGISTER) then
  893. CGMessage(cg_e_illegal_expression);
  894. { check type }
  895. if (p^.left^.resulttype^.deftype=setdef) then
  896. begin
  897. { two paras ? }
  898. if assigned(p^.left^.right) then
  899. begin
  900. { insert a type conversion }
  901. { to the type of the set elements }
  902. p^.left^.right^.left:=gentypeconvnode(
  903. p^.left^.right^.left,
  904. psetdef(p^.left^.resulttype)^.setof);
  905. { check the type conversion }
  906. firstpass(p^.left^.right^.left);
  907. { only three parameters are allowed }
  908. if assigned(p^.left^.right^.right) then
  909. CGMessage(cg_e_illegal_expression);
  910. end;
  911. end
  912. else
  913. CGMessage(type_e_mismatch);
  914. end
  915. else
  916. CGMessage(type_e_mismatch);
  917. end;
  918. in_low_x,
  919. in_high_x:
  920. begin
  921. if p^.left^.treetype in [typen,loadn,subscriptn] then
  922. begin
  923. case p^.left^.resulttype^.deftype of
  924. orddef,enumdef:
  925. begin
  926. do_lowhigh(p^.left^.resulttype);
  927. firstpass(p);
  928. end;
  929. setdef:
  930. begin
  931. do_lowhigh(Psetdef(p^.left^.resulttype)^.setof);
  932. firstpass(p);
  933. end;
  934. arraydef:
  935. begin
  936. if p^.inlinenumber=in_low_x then
  937. begin
  938. hp:=genordinalconstnode(Parraydef(p^.left^.resulttype)^.lowrange,s32bitdef);
  939. disposetree(p);
  940. p:=hp;
  941. firstpass(p);
  942. end
  943. else
  944. begin
  945. if is_open_array(p^.left^.resulttype) then
  946. begin
  947. getsymonlyin(p^.left^.symtable,'high'+pvarsym(p^.left^.symtableentry)^.name);
  948. hp:=genloadnode(pvarsym(srsym),p^.left^.symtable);
  949. disposetree(p);
  950. p:=hp;
  951. firstpass(p);
  952. end
  953. else
  954. begin
  955. hp:=genordinalconstnode(Parraydef(p^.left^.resulttype)^.highrange,s32bitdef);
  956. disposetree(p);
  957. p:=hp;
  958. firstpass(p);
  959. end;
  960. end;
  961. end;
  962. stringdef:
  963. begin
  964. if p^.inlinenumber=in_low_x then
  965. begin
  966. hp:=genordinalconstnode(0,u8bitdef);
  967. disposetree(p);
  968. p:=hp;
  969. firstpass(p);
  970. end
  971. else
  972. begin
  973. if is_open_string(p^.left^.resulttype) then
  974. begin
  975. getsymonlyin(p^.left^.symtable,'high'+pvarsym(p^.left^.symtableentry)^.name);
  976. hp:=genloadnode(pvarsym(srsym),p^.left^.symtable);
  977. disposetree(p);
  978. p:=hp;
  979. firstpass(p);
  980. end
  981. else
  982. begin
  983. hp:=genordinalconstnode(Pstringdef(p^.left^.resulttype)^.len,u8bitdef);
  984. disposetree(p);
  985. p:=hp;
  986. firstpass(p);
  987. end;
  988. end;
  989. end;
  990. else
  991. CGMessage(type_e_mismatch);
  992. end;
  993. end
  994. else
  995. CGMessage(type_e_varid_or_typeid_expected);
  996. end;
  997. in_assert_x_y :
  998. begin
  999. p^.resulttype:=voiddef;
  1000. if assigned(p^.left) then
  1001. begin
  1002. firstcallparan(p^.left,nil);
  1003. p^.registers32:=p^.left^.registers32;
  1004. p^.registersfpu:=p^.left^.registersfpu;
  1005. {$ifdef SUPPORT_MMX}
  1006. p^.registersmmx:=p^.left^.registersmmx;
  1007. {$endif SUPPORT_MMX}
  1008. { check type }
  1009. if is_boolean(p^.left^.resulttype) then
  1010. begin
  1011. { must always be a string }
  1012. p^.left^.right^.left:=gentypeconvnode(p^.left^.right^.left,cshortstringdef);
  1013. firstpass(p^.left^.right^.left);
  1014. end
  1015. else
  1016. CGMessage(type_e_mismatch);
  1017. end
  1018. else
  1019. CGMessage(type_e_mismatch);
  1020. end;
  1021. else
  1022. internalerror(8);
  1023. end;
  1024. end;
  1025. { generate an error if no resulttype is set }
  1026. if not assigned(p^.resulttype) then
  1027. p^.resulttype:=generrordef;
  1028. must_be_valid:=store_valid;
  1029. count_ref:=store_count_ref;
  1030. end;
  1031. end.
  1032. {
  1033. $Log$
  1034. Revision 1.26 1999-04-15 14:10:51 pierre
  1035. * fix for bug0238.pp
  1036. Revision 1.25 1999/04/15 10:00:35 peter
  1037. * writeln(procvar) support for tp7 mode
  1038. Revision 1.24 1999/04/14 09:15:07 peter
  1039. * first things to store the symbol/def number in the ppu
  1040. Revision 1.23 1999/04/08 10:16:48 peter
  1041. * funcret_valid flag is set for inline functions
  1042. Revision 1.22 1999/03/26 00:05:48 peter
  1043. * released valintern
  1044. + deffile is now removed when compiling is finished
  1045. * ^( compiles now correct
  1046. + static directive
  1047. * shrd fixed
  1048. Revision 1.21 1999/03/24 23:17:37 peter
  1049. * fixed bugs 212,222,225,227,229,231,233
  1050. Revision 1.20 1999/03/16 17:52:55 jonas
  1051. * changes for internal Val code (do a "make cycle OPT=-dvalintern" to test)
  1052. * in cgi386inl: also range checking for subrange types (compile with "-dreadrangecheck")
  1053. * in cgai386: also small fixes to emitrangecheck
  1054. Revision 1.19 1999/02/22 12:36:34 florian
  1055. + warning for lo/hi(longint/dword) in -So and -Sd mode added
  1056. Revision 1.18 1999/02/22 02:15:49 peter
  1057. * updates for ag386bin
  1058. Revision 1.17 1999/02/01 00:00:50 florian
  1059. * compiler crash fixed when constant arguments passed to round/trunc
  1060. exceeds the longint range
  1061. Revision 1.16 1999/01/28 19:43:43 peter
  1062. * fixed high generation for ansistrings with str,writeln
  1063. Revision 1.15 1999/01/27 16:28:22 pierre
  1064. * bug0157 solved : write(x:5.3) is rejected now
  1065. Revision 1.14 1999/01/21 22:10:50 peter
  1066. * fixed array of const
  1067. * generic platform independent high() support
  1068. Revision 1.13 1998/12/30 22:13:13 peter
  1069. * check the amount of paras for Str()
  1070. Revision 1.12 1998/12/15 10:23:31 peter
  1071. + -iSO, -iSP, -iTO, -iTP
  1072. Revision 1.11 1998/12/11 23:36:08 florian
  1073. + again more stuff for int64/qword:
  1074. - comparision operators
  1075. - code generation for: str, read(ln), write(ln)
  1076. Revision 1.10 1998/11/27 14:50:53 peter
  1077. + open strings, $P switch support
  1078. Revision 1.9 1998/11/24 17:04:28 peter
  1079. * fixed length(char) when char is a variable
  1080. Revision 1.8 1998/11/14 10:51:33 peter
  1081. * fixed low/high for record.field
  1082. Revision 1.7 1998/11/13 10:15:52 peter
  1083. * fixed ptr() with constants
  1084. Revision 1.6 1998/11/05 12:03:05 peter
  1085. * released useansistring
  1086. * removed -Sv, its now available in fpc modes
  1087. Revision 1.5 1998/10/20 11:16:47 pierre
  1088. + length(c) where C is a char is allways 1
  1089. Revision 1.4 1998/10/06 20:49:11 peter
  1090. * m68k compiler compiles again
  1091. Revision 1.3 1998/10/05 12:32:49 peter
  1092. + assert() support
  1093. Revision 1.2 1998/10/02 09:24:23 peter
  1094. * more constant expression evaluators
  1095. Revision 1.1 1998/09/23 20:42:24 peter
  1096. * splitted pass_1
  1097. }