tcinl.pas 48 KB

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