tcinl.pas 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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
  868. exit;
  869. { if not a stringdef then insert a type conv which
  870. does the other type checking }
  871. If (hp^.left^.resulttype^.deftype<>stringdef) then
  872. begin
  873. hp^.left:=gentypeconvnode(hp^.left,cshortstringdef);
  874. firstpass(hp);
  875. end;
  876. { calc registers }
  877. left_right_max(p);
  878. end;
  879. {$EndIf OLDVAL}
  880. in_include_x_y,
  881. in_exclude_x_y:
  882. begin
  883. p^.resulttype:=voiddef;
  884. if assigned(p^.left) then
  885. begin
  886. firstcallparan(p^.left,nil);
  887. p^.registers32:=p^.left^.registers32;
  888. p^.registersfpu:=p^.left^.registersfpu;
  889. {$ifdef SUPPORT_MMX}
  890. p^.registersmmx:=p^.left^.registersmmx;
  891. {$endif SUPPORT_MMX}
  892. { remove warning when result is passed }
  893. if (p^.left^.left^.treetype=funcretn) then
  894. procinfo.funcret_is_valid:=true;
  895. { first param must be var }
  896. if (p^.left^.left^.location.loc<>LOC_REFERENCE) and
  897. (p^.left^.left^.location.loc<>LOC_CREGISTER) then
  898. CGMessage(cg_e_illegal_expression);
  899. { check type }
  900. if (p^.left^.resulttype^.deftype=setdef) then
  901. begin
  902. { two paras ? }
  903. if assigned(p^.left^.right) then
  904. begin
  905. { insert a type conversion }
  906. { to the type of the set elements }
  907. p^.left^.right^.left:=gentypeconvnode(
  908. p^.left^.right^.left,
  909. psetdef(p^.left^.resulttype)^.setof);
  910. { check the type conversion }
  911. firstpass(p^.left^.right^.left);
  912. { only three parameters are allowed }
  913. if assigned(p^.left^.right^.right) then
  914. CGMessage(cg_e_illegal_expression);
  915. end;
  916. end
  917. else
  918. CGMessage(type_e_mismatch);
  919. end
  920. else
  921. CGMessage(type_e_mismatch);
  922. end;
  923. in_low_x,
  924. in_high_x:
  925. begin
  926. if p^.left^.treetype in [typen,loadn,subscriptn] then
  927. begin
  928. case p^.left^.resulttype^.deftype of
  929. orddef,enumdef:
  930. begin
  931. do_lowhigh(p^.left^.resulttype);
  932. firstpass(p);
  933. end;
  934. setdef:
  935. begin
  936. do_lowhigh(Psetdef(p^.left^.resulttype)^.setof);
  937. firstpass(p);
  938. end;
  939. arraydef:
  940. begin
  941. if p^.inlinenumber=in_low_x then
  942. begin
  943. hp:=genordinalconstnode(Parraydef(p^.left^.resulttype)^.lowrange,s32bitdef);
  944. disposetree(p);
  945. p:=hp;
  946. firstpass(p);
  947. end
  948. else
  949. begin
  950. if is_open_array(p^.left^.resulttype) then
  951. begin
  952. getsymonlyin(p^.left^.symtable,'high'+pvarsym(p^.left^.symtableentry)^.name);
  953. hp:=genloadnode(pvarsym(srsym),p^.left^.symtable);
  954. disposetree(p);
  955. p:=hp;
  956. firstpass(p);
  957. end
  958. else
  959. begin
  960. hp:=genordinalconstnode(Parraydef(p^.left^.resulttype)^.highrange,s32bitdef);
  961. disposetree(p);
  962. p:=hp;
  963. firstpass(p);
  964. end;
  965. end;
  966. end;
  967. stringdef:
  968. begin
  969. if p^.inlinenumber=in_low_x then
  970. begin
  971. hp:=genordinalconstnode(0,u8bitdef);
  972. disposetree(p);
  973. p:=hp;
  974. firstpass(p);
  975. end
  976. else
  977. begin
  978. if is_open_string(p^.left^.resulttype) then
  979. begin
  980. getsymonlyin(p^.left^.symtable,'high'+pvarsym(p^.left^.symtableentry)^.name);
  981. hp:=genloadnode(pvarsym(srsym),p^.left^.symtable);
  982. disposetree(p);
  983. p:=hp;
  984. firstpass(p);
  985. end
  986. else
  987. begin
  988. hp:=genordinalconstnode(Pstringdef(p^.left^.resulttype)^.len,u8bitdef);
  989. disposetree(p);
  990. p:=hp;
  991. firstpass(p);
  992. end;
  993. end;
  994. end;
  995. else
  996. CGMessage(type_e_mismatch);
  997. end;
  998. end
  999. else
  1000. CGMessage(type_e_varid_or_typeid_expected);
  1001. end;
  1002. in_assert_x_y :
  1003. begin
  1004. p^.resulttype:=voiddef;
  1005. if assigned(p^.left) then
  1006. begin
  1007. firstcallparan(p^.left,nil);
  1008. p^.registers32:=p^.left^.registers32;
  1009. p^.registersfpu:=p^.left^.registersfpu;
  1010. {$ifdef SUPPORT_MMX}
  1011. p^.registersmmx:=p^.left^.registersmmx;
  1012. {$endif SUPPORT_MMX}
  1013. { check type }
  1014. if is_boolean(p^.left^.resulttype) then
  1015. begin
  1016. { must always be a string }
  1017. p^.left^.right^.left:=gentypeconvnode(p^.left^.right^.left,cshortstringdef);
  1018. firstpass(p^.left^.right^.left);
  1019. end
  1020. else
  1021. CGMessage(type_e_mismatch);
  1022. end
  1023. else
  1024. CGMessage(type_e_mismatch);
  1025. end;
  1026. else
  1027. internalerror(8);
  1028. end;
  1029. end;
  1030. { generate an error if no resulttype is set }
  1031. if not assigned(p^.resulttype) then
  1032. p^.resulttype:=generrordef;
  1033. must_be_valid:=store_valid;
  1034. count_ref:=store_count_ref;
  1035. end;
  1036. end.
  1037. {
  1038. $Log$
  1039. Revision 1.27 1999-04-26 09:32:22 peter
  1040. * try to convert to string for val()
  1041. Revision 1.26 1999/04/15 14:10:51 pierre
  1042. * fix for bug0238.pp
  1043. Revision 1.25 1999/04/15 10:00:35 peter
  1044. * writeln(procvar) support for tp7 mode
  1045. Revision 1.24 1999/04/14 09:15:07 peter
  1046. * first things to store the symbol/def number in the ppu
  1047. Revision 1.23 1999/04/08 10:16:48 peter
  1048. * funcret_valid flag is set for inline functions
  1049. Revision 1.22 1999/03/26 00:05:48 peter
  1050. * released valintern
  1051. + deffile is now removed when compiling is finished
  1052. * ^( compiles now correct
  1053. + static directive
  1054. * shrd fixed
  1055. Revision 1.21 1999/03/24 23:17:37 peter
  1056. * fixed bugs 212,222,225,227,229,231,233
  1057. Revision 1.20 1999/03/16 17:52:55 jonas
  1058. * changes for internal Val code (do a "make cycle OPT=-dvalintern" to test)
  1059. * in cgi386inl: also range checking for subrange types (compile with "-dreadrangecheck")
  1060. * in cgai386: also small fixes to emitrangecheck
  1061. Revision 1.19 1999/02/22 12:36:34 florian
  1062. + warning for lo/hi(longint/dword) in -So and -Sd mode added
  1063. Revision 1.18 1999/02/22 02:15:49 peter
  1064. * updates for ag386bin
  1065. Revision 1.17 1999/02/01 00:00:50 florian
  1066. * compiler crash fixed when constant arguments passed to round/trunc
  1067. exceeds the longint range
  1068. Revision 1.16 1999/01/28 19:43:43 peter
  1069. * fixed high generation for ansistrings with str,writeln
  1070. Revision 1.15 1999/01/27 16:28:22 pierre
  1071. * bug0157 solved : write(x:5.3) is rejected now
  1072. Revision 1.14 1999/01/21 22:10:50 peter
  1073. * fixed array of const
  1074. * generic platform independent high() support
  1075. Revision 1.13 1998/12/30 22:13:13 peter
  1076. * check the amount of paras for Str()
  1077. Revision 1.12 1998/12/15 10:23:31 peter
  1078. + -iSO, -iSP, -iTO, -iTP
  1079. Revision 1.11 1998/12/11 23:36:08 florian
  1080. + again more stuff for int64/qword:
  1081. - comparision operators
  1082. - code generation for: str, read(ln), write(ln)
  1083. Revision 1.10 1998/11/27 14:50:53 peter
  1084. + open strings, $P switch support
  1085. Revision 1.9 1998/11/24 17:04:28 peter
  1086. * fixed length(char) when char is a variable
  1087. Revision 1.8 1998/11/14 10:51:33 peter
  1088. * fixed low/high for record.field
  1089. Revision 1.7 1998/11/13 10:15:52 peter
  1090. * fixed ptr() with constants
  1091. Revision 1.6 1998/11/05 12:03:05 peter
  1092. * released useansistring
  1093. * removed -Sv, its now available in fpc modes
  1094. Revision 1.5 1998/10/20 11:16:47 pierre
  1095. + length(c) where C is a char is allways 1
  1096. Revision 1.4 1998/10/06 20:49:11 peter
  1097. * m68k compiler compiles again
  1098. Revision 1.3 1998/10/05 12:32:49 peter
  1099. + assert() support
  1100. Revision 1.2 1998/10/02 09:24:23 peter
  1101. * more constant expression evaluators
  1102. Revision 1.1 1998/09/23 20:42:24 peter
  1103. * splitted pass_1
  1104. }