tcinl.pas 50 KB

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