tcinl.pas 52 KB

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