tcinl.pas 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  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. { generate the high() value for the shortstring }
  554. if ((not dowrite) and is_shortstring(hpp^.left^.resulttype)) or
  555. (is_chararray(hpp^.left^.resulttype)) then
  556. gen_high_tree(hpp,true);
  557. hpp:=hpp^.right;
  558. end;
  559. end;
  560. end; { endif assigned(hp) }
  561. { insert type conversions for write(ln) }
  562. if (not file_is_typed) then
  563. begin
  564. hp:=p^.left;
  565. while assigned(hp) do
  566. begin
  567. if (hp^.left^.treetype=typen) then
  568. CGMessage(type_e_cant_read_write_type);
  569. if assigned(hp^.left^.resulttype) then
  570. begin
  571. isreal:=false;
  572. { support writeln(procvar) for tp7 }
  573. if (m_tp_procvar in aktmodeswitches) and (hp^.left^.resulttype^.deftype=procvardef) then
  574. begin
  575. p1:=gencallnode(nil,nil);
  576. p1^.right:=hp^.left;
  577. p1^.resulttype:=pprocvardef(hp^.left^.resulttype)^.retdef;
  578. firstpass(p1);
  579. hp^.left:=p1;
  580. end;
  581. case hp^.left^.resulttype^.deftype of
  582. filedef :
  583. begin
  584. { only allowed as first parameter }
  585. if assigned(hp^.right) then
  586. CGMessage(type_e_cant_read_write_type);
  587. end;
  588. stringdef :
  589. begin
  590. { generate the high() value for the shortstring }
  591. if (not dowrite) and
  592. is_shortstring(hp^.left^.resulttype) then
  593. gen_high_tree(hp,true);
  594. end;
  595. pointerdef :
  596. begin
  597. if not is_pchar(hp^.left^.resulttype) then
  598. CGMessage(type_e_cant_read_write_type);
  599. end;
  600. floatdef :
  601. begin
  602. isreal:=true;
  603. end;
  604. orddef :
  605. begin
  606. case porddef(hp^.left^.resulttype)^.typ of
  607. uchar,
  608. u32bit,s32bit,
  609. u64bit,s64bitint:
  610. ;
  611. u8bit,s8bit,
  612. u16bit,s16bit :
  613. if dowrite then
  614. hp^.left:=gentypeconvnode(hp^.left,s32bitdef);
  615. bool8bit,
  616. bool16bit,
  617. bool32bit :
  618. if dowrite then
  619. hp^.left:=gentypeconvnode(hp^.left,booldef)
  620. else
  621. CGMessage(type_e_cant_read_write_type);
  622. else
  623. CGMessage(type_e_cant_read_write_type);
  624. end;
  625. end;
  626. arraydef :
  627. begin
  628. if is_chararray(hp^.left^.resulttype) then
  629. gen_high_tree(hp,true)
  630. else
  631. CGMessage(type_e_cant_read_write_type);
  632. end;
  633. else
  634. CGMessage(type_e_cant_read_write_type);
  635. end;
  636. { some format options ? }
  637. if hp^.is_colon_para then
  638. begin
  639. if hp^.right^.is_colon_para then
  640. begin
  641. frac_para:=hp;
  642. length_para:=hp^.right;
  643. hp:=hp^.right;
  644. hpp:=hp^.right;
  645. end
  646. else
  647. begin
  648. length_para:=hp;
  649. frac_para:=nil;
  650. hpp:=hp^.right;
  651. end;
  652. isreal:=hpp^.resulttype^.deftype=floatdef;
  653. if (not is_integer(length_para^.resulttype)) then
  654. CGMessage(type_e_integer_expr_expected)
  655. else
  656. length_para^.left:=gentypeconvnode(length_para^.left,s32bitdef);
  657. if assigned(frac_para) then
  658. begin
  659. if isreal then
  660. begin
  661. if (not is_integer(frac_para^.resulttype)) then
  662. CGMessage(type_e_integer_expr_expected)
  663. else
  664. frac_para^.left:=gentypeconvnode(frac_para^.left,s32bitdef);
  665. end
  666. else
  667. CGMessage(parser_e_illegal_colon_qualifier);
  668. end;
  669. { do the checking for the colon'd arg }
  670. hp:=length_para;
  671. end;
  672. end;
  673. hp:=hp^.right;
  674. end;
  675. end;
  676. { pass all parameters again for the typeconversions }
  677. if codegenerror then
  678. exit;
  679. must_be_valid:=true;
  680. firstcallparan(p^.left,nil);
  681. { calc registers }
  682. left_right_max(p);
  683. end;
  684. end;
  685. in_settextbuf_file_x :
  686. begin
  687. { warning here p^.left is the callparannode
  688. not the argument directly }
  689. { p^.left^.left is text var }
  690. { p^.left^.right^.left is the buffer var }
  691. { firstcallparan(p^.left,nil);
  692. already done in firstcalln }
  693. { now we know the type of buffer }
  694. getsymonlyin(systemunit,'SETTEXTBUF');
  695. hp:=gencallnode(pprocsym(srsym),systemunit);
  696. hp^.left:=gencallparanode(
  697. genordinalconstnode(p^.left^.left^.resulttype^.size,s32bitdef),p^.left);
  698. putnode(p);
  699. p:=hp;
  700. firstpass(p);
  701. end;
  702. { the firstpass of the arg has been done in firstcalln ? }
  703. in_reset_typedfile,
  704. in_rewrite_typedfile :
  705. begin
  706. procinfo.flags:=procinfo.flags or pi_do_call;
  707. { to be sure the right definition is loaded }
  708. p^.left^.resulttype:=nil;
  709. firstpass(p^.left);
  710. p^.resulttype:=voiddef;
  711. end;
  712. in_str_x_string :
  713. begin
  714. procinfo.flags:=procinfo.flags or pi_do_call;
  715. p^.resulttype:=voiddef;
  716. { check the amount of parameters }
  717. if not(assigned(p^.left)) or
  718. not(assigned(p^.left^.right)) then
  719. begin
  720. CGMessage(parser_e_wrong_parameter_size);
  721. exit;
  722. end;
  723. { first pass just the string for first local use }
  724. hp:=p^.left^.right;
  725. must_be_valid:=false;
  726. count_ref:=true;
  727. p^.left^.right:=nil;
  728. firstcallparan(p^.left,nil);
  729. { remove warning when result is passed }
  730. if (p^.left^.left^.treetype=funcretn) then
  731. procinfo.funcret_is_valid:=true;
  732. must_be_valid:=true;
  733. p^.left^.right:=hp;
  734. firstcallparan(p^.left^.right,nil);
  735. hp:=p^.left;
  736. { valid string ? }
  737. if not assigned(hp) or
  738. (hp^.left^.resulttype^.deftype<>stringdef) or
  739. (hp^.right=nil) or
  740. (hp^.left^.location.loc<>LOC_REFERENCE) then
  741. CGMessage(cg_e_illegal_expression);
  742. { generate the high() value for the shortstring }
  743. if is_shortstring(hp^.left^.resulttype) then
  744. gen_high_tree(hp,true);
  745. { !!!! check length of string }
  746. while assigned(hp^.right) do
  747. hp:=hp^.right;
  748. { check and convert the first param }
  749. if hp^.is_colon_para then
  750. CGMessage(cg_e_illegal_expression);
  751. isreal:=false;
  752. case hp^.resulttype^.deftype of
  753. orddef :
  754. begin
  755. case porddef(hp^.left^.resulttype)^.typ of
  756. u32bit,s32bit,
  757. s64bitint,u64bit:
  758. ;
  759. u8bit,s8bit,
  760. u16bit,s16bit:
  761. hp^.left:=gentypeconvnode(hp^.left,s32bitdef);
  762. else
  763. CGMessage(type_e_integer_or_real_expr_expected);
  764. end;
  765. end;
  766. floatdef :
  767. begin
  768. isreal:=true;
  769. end;
  770. else
  771. CGMessage(type_e_integer_or_real_expr_expected);
  772. end;
  773. { some format options ? }
  774. hpp:=p^.left^.right;
  775. if assigned(hpp) and hpp^.is_colon_para then
  776. begin
  777. if (not is_integer(hpp^.resulttype)) then
  778. CGMessage(type_e_integer_expr_expected)
  779. else
  780. hpp^.left:=gentypeconvnode(hpp^.left,s32bitdef);
  781. hpp:=hpp^.right;
  782. if assigned(hpp) and hpp^.is_colon_para then
  783. begin
  784. if isreal then
  785. begin
  786. if (not is_integer(hpp^.resulttype)) then
  787. CGMessage(type_e_integer_expr_expected)
  788. else
  789. hpp^.left:=gentypeconvnode(hpp^.left,s32bitdef);
  790. end
  791. else
  792. CGMessage(parser_e_illegal_colon_qualifier);
  793. end;
  794. end;
  795. { for first local use }
  796. must_be_valid:=false;
  797. count_ref:=true;
  798. { pass all parameters again for the typeconversions }
  799. if codegenerror then
  800. exit;
  801. must_be_valid:=true;
  802. firstcallparan(p^.left,nil);
  803. { calc registers }
  804. left_right_max(p);
  805. end;
  806. {$IfnDef OLDVAL}
  807. in_val_x :
  808. begin
  809. procinfo.flags:=procinfo.flags or pi_do_call;
  810. p^.resulttype:=voiddef;
  811. { check the amount of parameters }
  812. if not(assigned(p^.left)) or
  813. not(assigned(p^.left^.right)) then
  814. begin
  815. CGMessage(parser_e_wrong_parameter_size);
  816. exit;
  817. end;
  818. If Assigned(p^.left^.right^.right) Then
  819. {there is a "code" parameter}
  820. Begin
  821. { first pass just the code parameter for first local use}
  822. hp := p^.left^.right;
  823. p^.left^.right := nil;
  824. must_be_valid := false;
  825. count_ref := true;
  826. firstcallparan(p^.left, nil);
  827. if codegenerror then exit;
  828. p^.left^.right := hp;
  829. {code has to be a var parameter}
  830. if (p^.left^.left^.location.loc<>LOC_REFERENCE) then
  831. CGMessage(type_e_variable_id_expected)
  832. else
  833. if (p^.left^.left^.resulttype^.deftype <> orddef) or
  834. not(porddef(p^.left^.left^.resulttype)^.typ in
  835. [u16bit,s16bit,u32bit,s32bit]) then
  836. CGMessage(type_e_mismatch);
  837. hpp := p^.left^.right
  838. End
  839. Else hpp := p^.left;
  840. {now hpp = the destination value tree}
  841. { first pass just the destination parameter for first local use}
  842. hp:=hpp^.right;
  843. must_be_valid:=false;
  844. count_ref:=true;
  845. hpp^.right:=nil;
  846. {hpp = destination}
  847. firstcallparan(hpp,nil);
  848. if codegenerror then
  849. exit;
  850. { remove warning when result is passed }
  851. if (hpp^.left^.treetype=funcretn) then
  852. procinfo.funcret_is_valid:=true;
  853. hpp^.right := hp;
  854. if (hpp^.left^.location.loc<>LOC_REFERENCE) then
  855. CGMessage(type_e_variable_id_expected)
  856. else
  857. If Not((hpp^.left^.resulttype^.deftype = floatdef) or
  858. ((hpp^.left^.resulttype^.deftype = orddef) And
  859. (POrdDef(hpp^.left^.resulttype)^.typ in
  860. [u32bit,s32bit,{s64bitint,u64bit, -- not supported yet in RTL}
  861. u8bit,s8bit,u16bit,s16bit])))
  862. Then CGMessage(type_e_mismatch);
  863. must_be_valid:=true;
  864. {hp = source (String)}
  865. count_ref := false;
  866. must_be_valid := true;
  867. firstcallparan(hp,nil);
  868. if codegenerror then
  869. exit;
  870. { if not a stringdef then insert a type conv which
  871. does the other type checking }
  872. If (hp^.left^.resulttype^.deftype<>stringdef) then
  873. begin
  874. hp^.left:=gentypeconvnode(hp^.left,cshortstringdef);
  875. firstpass(hp);
  876. end;
  877. { calc registers }
  878. left_right_max(p);
  879. end;
  880. {$EndIf OLDVAL}
  881. in_include_x_y,
  882. in_exclude_x_y:
  883. begin
  884. p^.resulttype:=voiddef;
  885. if assigned(p^.left) then
  886. begin
  887. firstcallparan(p^.left,nil);
  888. p^.registers32:=p^.left^.registers32;
  889. p^.registersfpu:=p^.left^.registersfpu;
  890. {$ifdef SUPPORT_MMX}
  891. p^.registersmmx:=p^.left^.registersmmx;
  892. {$endif SUPPORT_MMX}
  893. { remove warning when result is passed }
  894. if (p^.left^.left^.treetype=funcretn) then
  895. procinfo.funcret_is_valid:=true;
  896. { first param must be var }
  897. if (p^.left^.left^.location.loc<>LOC_REFERENCE) and
  898. (p^.left^.left^.location.loc<>LOC_CREGISTER) then
  899. CGMessage(cg_e_illegal_expression);
  900. { check type }
  901. if (p^.left^.resulttype^.deftype=setdef) then
  902. begin
  903. { two paras ? }
  904. if assigned(p^.left^.right) then
  905. begin
  906. { insert a type conversion }
  907. { to the type of the set elements }
  908. p^.left^.right^.left:=gentypeconvnode(
  909. p^.left^.right^.left,
  910. psetdef(p^.left^.resulttype)^.setof);
  911. { check the type conversion }
  912. firstpass(p^.left^.right^.left);
  913. { only three parameters are allowed }
  914. if assigned(p^.left^.right^.right) then
  915. CGMessage(cg_e_illegal_expression);
  916. end;
  917. end
  918. else
  919. CGMessage(type_e_mismatch);
  920. end
  921. else
  922. CGMessage(type_e_mismatch);
  923. end;
  924. in_low_x,
  925. in_high_x:
  926. begin
  927. if p^.left^.treetype in [typen,loadn,subscriptn] then
  928. begin
  929. case p^.left^.resulttype^.deftype of
  930. orddef,enumdef:
  931. begin
  932. do_lowhigh(p^.left^.resulttype);
  933. firstpass(p);
  934. end;
  935. setdef:
  936. begin
  937. do_lowhigh(Psetdef(p^.left^.resulttype)^.setof);
  938. firstpass(p);
  939. end;
  940. arraydef:
  941. begin
  942. if p^.inlinenumber=in_low_x then
  943. begin
  944. hp:=genordinalconstnode(Parraydef(p^.left^.resulttype)^.lowrange,s32bitdef);
  945. disposetree(p);
  946. p:=hp;
  947. firstpass(p);
  948. end
  949. else
  950. begin
  951. if is_open_array(p^.left^.resulttype) then
  952. begin
  953. getsymonlyin(p^.left^.symtable,'high'+pvarsym(p^.left^.symtableentry)^.name);
  954. hp:=genloadnode(pvarsym(srsym),p^.left^.symtable);
  955. disposetree(p);
  956. p:=hp;
  957. firstpass(p);
  958. end
  959. else
  960. begin
  961. hp:=genordinalconstnode(Parraydef(p^.left^.resulttype)^.highrange,s32bitdef);
  962. disposetree(p);
  963. p:=hp;
  964. firstpass(p);
  965. end;
  966. end;
  967. end;
  968. stringdef:
  969. begin
  970. if p^.inlinenumber=in_low_x then
  971. begin
  972. hp:=genordinalconstnode(0,u8bitdef);
  973. disposetree(p);
  974. p:=hp;
  975. firstpass(p);
  976. end
  977. else
  978. begin
  979. if is_open_string(p^.left^.resulttype) then
  980. begin
  981. getsymonlyin(p^.left^.symtable,'high'+pvarsym(p^.left^.symtableentry)^.name);
  982. hp:=genloadnode(pvarsym(srsym),p^.left^.symtable);
  983. disposetree(p);
  984. p:=hp;
  985. firstpass(p);
  986. end
  987. else
  988. begin
  989. hp:=genordinalconstnode(Pstringdef(p^.left^.resulttype)^.len,u8bitdef);
  990. disposetree(p);
  991. p:=hp;
  992. firstpass(p);
  993. end;
  994. end;
  995. end;
  996. else
  997. CGMessage(type_e_mismatch);
  998. end;
  999. end
  1000. else
  1001. CGMessage(type_e_varid_or_typeid_expected);
  1002. end;
  1003. in_assert_x_y :
  1004. begin
  1005. p^.resulttype:=voiddef;
  1006. if assigned(p^.left) then
  1007. begin
  1008. firstcallparan(p^.left,nil);
  1009. p^.registers32:=p^.left^.registers32;
  1010. p^.registersfpu:=p^.left^.registersfpu;
  1011. {$ifdef SUPPORT_MMX}
  1012. p^.registersmmx:=p^.left^.registersmmx;
  1013. {$endif SUPPORT_MMX}
  1014. { check type }
  1015. if is_boolean(p^.left^.resulttype) then
  1016. begin
  1017. { must always be a string }
  1018. p^.left^.right^.left:=gentypeconvnode(p^.left^.right^.left,cshortstringdef);
  1019. firstpass(p^.left^.right^.left);
  1020. end
  1021. else
  1022. CGMessage(type_e_mismatch);
  1023. end
  1024. else
  1025. CGMessage(type_e_mismatch);
  1026. end;
  1027. else
  1028. internalerror(8);
  1029. end;
  1030. end;
  1031. { generate an error if no resulttype is set }
  1032. if not assigned(p^.resulttype) then
  1033. p^.resulttype:=generrordef;
  1034. must_be_valid:=store_valid;
  1035. count_ref:=store_count_ref;
  1036. end;
  1037. end.
  1038. {
  1039. $Log$
  1040. Revision 1.28 1999-04-26 18:28:12 peter
  1041. * better read/write array
  1042. Revision 1.27 1999/04/26 09:32:22 peter
  1043. * try to convert to string for val()
  1044. Revision 1.26 1999/04/15 14:10:51 pierre
  1045. * fix for bug0238.pp
  1046. Revision 1.25 1999/04/15 10:00:35 peter
  1047. * writeln(procvar) support for tp7 mode
  1048. Revision 1.24 1999/04/14 09:15:07 peter
  1049. * first things to store the symbol/def number in the ppu
  1050. Revision 1.23 1999/04/08 10:16:48 peter
  1051. * funcret_valid flag is set for inline functions
  1052. Revision 1.22 1999/03/26 00:05:48 peter
  1053. * released valintern
  1054. + deffile is now removed when compiling is finished
  1055. * ^( compiles now correct
  1056. + static directive
  1057. * shrd fixed
  1058. Revision 1.21 1999/03/24 23:17:37 peter
  1059. * fixed bugs 212,222,225,227,229,231,233
  1060. Revision 1.20 1999/03/16 17:52:55 jonas
  1061. * changes for internal Val code (do a "make cycle OPT=-dvalintern" to test)
  1062. * in cgi386inl: also range checking for subrange types (compile with "-dreadrangecheck")
  1063. * in cgai386: also small fixes to emitrangecheck
  1064. Revision 1.19 1999/02/22 12:36:34 florian
  1065. + warning for lo/hi(longint/dword) in -So and -Sd mode added
  1066. Revision 1.18 1999/02/22 02:15:49 peter
  1067. * updates for ag386bin
  1068. Revision 1.17 1999/02/01 00:00:50 florian
  1069. * compiler crash fixed when constant arguments passed to round/trunc
  1070. exceeds the longint range
  1071. Revision 1.16 1999/01/28 19:43:43 peter
  1072. * fixed high generation for ansistrings with str,writeln
  1073. Revision 1.15 1999/01/27 16:28:22 pierre
  1074. * bug0157 solved : write(x:5.3) is rejected now
  1075. Revision 1.14 1999/01/21 22:10:50 peter
  1076. * fixed array of const
  1077. * generic platform independent high() support
  1078. Revision 1.13 1998/12/30 22:13:13 peter
  1079. * check the amount of paras for Str()
  1080. Revision 1.12 1998/12/15 10:23:31 peter
  1081. + -iSO, -iSP, -iTO, -iTP
  1082. Revision 1.11 1998/12/11 23:36:08 florian
  1083. + again more stuff for int64/qword:
  1084. - comparision operators
  1085. - code generation for: str, read(ln), write(ln)
  1086. Revision 1.10 1998/11/27 14:50:53 peter
  1087. + open strings, $P switch support
  1088. Revision 1.9 1998/11/24 17:04:28 peter
  1089. * fixed length(char) when char is a variable
  1090. Revision 1.8 1998/11/14 10:51:33 peter
  1091. * fixed low/high for record.field
  1092. Revision 1.7 1998/11/13 10:15:52 peter
  1093. * fixed ptr() with constants
  1094. Revision 1.6 1998/11/05 12:03:05 peter
  1095. * released useansistring
  1096. * removed -Sv, its now available in fpc modes
  1097. Revision 1.5 1998/10/20 11:16:47 pierre
  1098. + length(c) where C is a char is allways 1
  1099. Revision 1.4 1998/10/06 20:49:11 peter
  1100. * m68k compiler compiles again
  1101. Revision 1.3 1998/10/05 12:32:49 peter
  1102. + assert() support
  1103. Revision 1.2 1998/10/02 09:24:23 peter
  1104. * more constant expression evaluators
  1105. Revision 1.1 1998/09/23 20:42:24 peter
  1106. * splitted pass_1
  1107. }