tcinl.pas 48 KB

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