tcinl.pas 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. symconst,symtable,aasm,types,
  28. htypechk,pass_1,
  29. tccal,cpubase
  30. {$ifdef newcg}
  31. ,cgbase
  32. ,tgobj
  33. ,tgcpu
  34. {$else newcg}
  35. ,hcodegen
  36. {$ifdef i386}
  37. ,tgeni386
  38. {$endif}
  39. {$endif newcg}
  40. ;
  41. {*****************************************************************************
  42. FirstInLine
  43. *****************************************************************************}
  44. {$ifdef fpc}
  45. {$maxfpuregisters 0}
  46. {$endif fpc}
  47. procedure firstinline(var p : ptree);
  48. var
  49. vl,vl2 : longint;
  50. vr : bestreal;
  51. p1,hp,hpp : ptree;
  52. {$ifndef NOCOLONCHECK}
  53. frac_para,length_para : ptree;
  54. {$endif ndef NOCOLONCHECK}
  55. extra_register,
  56. isreal,
  57. dowrite,
  58. file_is_typed : boolean;
  59. procedure do_lowhigh(adef : pdef);
  60. var
  61. v : longint;
  62. enum : penumsym;
  63. begin
  64. case Adef^.deftype of
  65. orddef:
  66. begin
  67. if p^.inlinenumber=in_low_x then
  68. v:=porddef(adef)^.low
  69. else
  70. v:=porddef(adef)^.high;
  71. hp:=genordinalconstnode(v,adef);
  72. firstpass(hp);
  73. disposetree(p);
  74. p:=hp;
  75. end;
  76. enumdef:
  77. begin
  78. enum:=Penumdef(Adef)^.firstenum;
  79. if p^.inlinenumber=in_high_x then
  80. while enum^.nextenum<>nil do
  81. enum:=enum^.nextenum;
  82. hp:=genenumnode(enum);
  83. disposetree(p);
  84. p:=hp;
  85. end;
  86. else
  87. internalerror(87);
  88. end;
  89. end;
  90. function getconstrealvalue : bestreal;
  91. begin
  92. case p^.left^.treetype of
  93. ordconstn:
  94. getconstrealvalue:=p^.left^.value;
  95. realconstn:
  96. getconstrealvalue:=p^.left^.value_real;
  97. else
  98. internalerror(309992);
  99. end;
  100. end;
  101. procedure setconstrealvalue(r : bestreal);
  102. var
  103. hp : ptree;
  104. begin
  105. hp:=genrealconstnode(r,bestrealdef^);
  106. disposetree(p);
  107. p:=hp;
  108. firstpass(p);
  109. end;
  110. procedure handleextendedfunction;
  111. begin
  112. p^.location.loc:=LOC_FPU;
  113. p^.resulttype:=s80floatdef;
  114. { redo firstpass for varstate status PM }
  115. set_varstate(p^.left,true);
  116. if (p^.left^.resulttype^.deftype<>floatdef) or
  117. (pfloatdef(p^.left^.resulttype)^.typ<>s80real) then
  118. begin
  119. p^.left:=gentypeconvnode(p^.left,s80floatdef);
  120. firstpass(p^.left);
  121. end;
  122. p^.registers32:=p^.left^.registers32;
  123. p^.registersfpu:=p^.left^.registersfpu;
  124. {$ifdef SUPPORT_MMX}
  125. p^.registersmmx:=p^.left^.registersmmx;
  126. {$endif SUPPORT_MMX}
  127. end;
  128. begin
  129. { if we handle writeln; p^.left contains no valid address }
  130. if assigned(p^.left) then
  131. begin
  132. if p^.left^.treetype=callparan then
  133. firstcallparan(p^.left,nil,false)
  134. else
  135. firstpass(p^.left);
  136. left_right_max(p);
  137. set_location(p^.location,p^.left^.location);
  138. end;
  139. inc(parsing_para_level);
  140. { handle intern constant functions in separate case }
  141. if p^.inlineconst then
  142. begin
  143. hp:=nil;
  144. { no parameters? }
  145. if not assigned(p^.left) then
  146. begin
  147. case p^.inlinenumber of
  148. in_const_pi :
  149. hp:=genrealconstnode(pi,bestrealdef^);
  150. else
  151. internalerror(89);
  152. end;
  153. end
  154. else
  155. { process constant expression with parameter }
  156. begin
  157. vl:=0;
  158. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  159. vr:=0;
  160. isreal:=false;
  161. case p^.left^.treetype of
  162. realconstn :
  163. begin
  164. isreal:=true;
  165. vr:=p^.left^.value_real;
  166. end;
  167. ordconstn :
  168. vl:=p^.left^.value;
  169. callparan :
  170. begin
  171. { both exists, else it was not generated }
  172. vl:=p^.left^.left^.value;
  173. vl2:=p^.left^.right^.left^.value;
  174. end;
  175. else
  176. CGMessage(cg_e_illegal_expression);
  177. end;
  178. case p^.inlinenumber of
  179. in_const_trunc :
  180. begin
  181. if isreal then
  182. begin
  183. if (vr>=2147483648.0) or (vr<=-2147483649.0) then
  184. begin
  185. CGMessage(parser_e_range_check_error);
  186. hp:=genordinalconstnode(1,s32bitdef)
  187. end
  188. else
  189. hp:=genordinalconstnode(trunc(vr),s32bitdef)
  190. end
  191. else
  192. hp:=genordinalconstnode(trunc(vl),s32bitdef);
  193. end;
  194. in_const_round :
  195. begin
  196. if isreal then
  197. begin
  198. if (vr>=2147483647.5) or (vr<=-2147483648.5) then
  199. begin
  200. CGMessage(parser_e_range_check_error);
  201. hp:=genordinalconstnode(1,s32bitdef)
  202. end
  203. else
  204. hp:=genordinalconstnode(round(vr),s32bitdef)
  205. end
  206. else
  207. hp:=genordinalconstnode(round(vl),s32bitdef);
  208. end;
  209. in_const_frac :
  210. begin
  211. if isreal then
  212. hp:=genrealconstnode(frac(vr),bestrealdef^)
  213. else
  214. hp:=genrealconstnode(frac(vl),bestrealdef^);
  215. end;
  216. in_const_int :
  217. begin
  218. if isreal then
  219. hp:=genrealconstnode(int(vr),bestrealdef^)
  220. else
  221. hp:=genrealconstnode(int(vl),bestrealdef^);
  222. end;
  223. in_const_abs :
  224. begin
  225. if isreal then
  226. hp:=genrealconstnode(abs(vr),bestrealdef^)
  227. else
  228. hp:=genordinalconstnode(abs(vl),p^.left^.resulttype);
  229. end;
  230. in_const_sqr :
  231. begin
  232. if isreal then
  233. hp:=genrealconstnode(sqr(vr),bestrealdef^)
  234. else
  235. hp:=genordinalconstnode(sqr(vl),p^.left^.resulttype);
  236. end;
  237. in_const_odd :
  238. begin
  239. if isreal then
  240. CGMessage1(type_e_integer_expr_expected,p^.left^.resulttype^.typename)
  241. else
  242. hp:=genordinalconstnode(byte(odd(vl)),booldef);
  243. end;
  244. in_const_swap_word :
  245. begin
  246. if isreal then
  247. CGMessage1(type_e_integer_expr_expected,p^.left^.resulttype^.typename)
  248. else
  249. hp:=genordinalconstnode((vl and $ff) shl 8+(vl shr 8),p^.left^.resulttype);
  250. end;
  251. in_const_swap_long :
  252. begin
  253. if isreal then
  254. CGMessage(type_e_mismatch)
  255. else
  256. hp:=genordinalconstnode((vl and $ffff) shl 16+(vl shr 16),p^.left^.resulttype);
  257. end;
  258. in_const_ptr :
  259. begin
  260. if isreal then
  261. CGMessage(type_e_mismatch)
  262. else
  263. hp:=genordinalconstnode((vl2 shl 16) or vl,voidpointerdef);
  264. end;
  265. in_const_sqrt :
  266. begin
  267. if isreal then
  268. begin
  269. if vr<0.0 then
  270. CGMessage(type_e_wrong_math_argument)
  271. else
  272. hp:=genrealconstnode(sqrt(vr),bestrealdef^)
  273. end
  274. else
  275. begin
  276. if vl<0 then
  277. CGMessage(type_e_wrong_math_argument)
  278. else
  279. hp:=genrealconstnode(sqrt(vl),bestrealdef^);
  280. end;
  281. end;
  282. in_const_arctan :
  283. begin
  284. if isreal then
  285. hp:=genrealconstnode(arctan(vr),bestrealdef^)
  286. else
  287. hp:=genrealconstnode(arctan(vl),bestrealdef^);
  288. end;
  289. in_const_cos :
  290. begin
  291. if isreal then
  292. hp:=genrealconstnode(cos(vr),bestrealdef^)
  293. else
  294. hp:=genrealconstnode(cos(vl),bestrealdef^);
  295. end;
  296. in_const_sin :
  297. begin
  298. if isreal then
  299. hp:=genrealconstnode(sin(vr),bestrealdef^)
  300. else
  301. hp:=genrealconstnode(sin(vl),bestrealdef^);
  302. end;
  303. in_const_exp :
  304. begin
  305. if isreal then
  306. hp:=genrealconstnode(exp(vr),bestrealdef^)
  307. else
  308. hp:=genrealconstnode(exp(vl),bestrealdef^);
  309. end;
  310. in_const_ln :
  311. begin
  312. if isreal then
  313. begin
  314. if vr<=0.0 then
  315. CGMessage(type_e_wrong_math_argument)
  316. else
  317. hp:=genrealconstnode(ln(vr),bestrealdef^)
  318. end
  319. else
  320. begin
  321. if vl<=0 then
  322. CGMessage(type_e_wrong_math_argument)
  323. else
  324. hp:=genrealconstnode(ln(vl),bestrealdef^);
  325. end;
  326. end;
  327. else
  328. internalerror(88);
  329. end;
  330. end;
  331. disposetree(p);
  332. if hp=nil then
  333. hp:=genzeronode(errorn);
  334. firstpass(hp);
  335. p:=hp;
  336. end
  337. else
  338. begin
  339. case p^.inlinenumber of
  340. in_lo_qword,
  341. in_hi_qword,
  342. in_lo_long,
  343. in_hi_long,
  344. in_lo_word,
  345. in_hi_word:
  346. begin
  347. set_varstate(p^.left,true);
  348. if p^.registers32<1 then
  349. p^.registers32:=1;
  350. if p^.inlinenumber in [in_lo_word,in_hi_word] then
  351. p^.resulttype:=u8bitdef
  352. else if p^.inlinenumber in [in_lo_qword,in_hi_qword] then
  353. begin
  354. p^.resulttype:=u32bitdef;
  355. if (m_tp in aktmodeswitches) or
  356. (m_delphi in aktmodeswitches) then
  357. CGMessage(type_w_maybe_wrong_hi_lo);
  358. end
  359. else
  360. begin
  361. p^.resulttype:=u16bitdef;
  362. if (m_tp in aktmodeswitches) or
  363. (m_delphi in aktmodeswitches) then
  364. CGMessage(type_w_maybe_wrong_hi_lo);
  365. end;
  366. p^.location.loc:=LOC_REGISTER;
  367. if not is_integer(p^.left^.resulttype) then
  368. CGMessage(type_e_mismatch)
  369. else
  370. begin
  371. if p^.left^.treetype=ordconstn then
  372. begin
  373. case p^.inlinenumber of
  374. in_lo_word : hp:=genordinalconstnode(p^.left^.value and $ff,p^.left^.resulttype);
  375. in_hi_word : hp:=genordinalconstnode(p^.left^.value shr 8,p^.left^.resulttype);
  376. in_lo_long : hp:=genordinalconstnode(p^.left^.value and $ffff,p^.left^.resulttype);
  377. in_hi_long : hp:=genordinalconstnode(p^.left^.value shr 16,p^.left^.resulttype);
  378. in_lo_qword : hp:=genordinalconstnode(p^.left^.value and $ffffffff,p^.left^.resulttype);
  379. in_hi_qword : hp:=genordinalconstnode(p^.left^.value shr 32,p^.left^.resulttype);
  380. end;
  381. disposetree(p);
  382. firstpass(hp);
  383. p:=hp;
  384. end;
  385. end;
  386. end;
  387. in_sizeof_x:
  388. begin
  389. set_varstate(p^.left,false);
  390. if push_high_param(p^.left^.resulttype) then
  391. begin
  392. getsymonlyin(p^.left^.symtable,'high'+pvarsym(p^.left^.symtableentry)^.name);
  393. hp:=gennode(addn,genloadnode(pvarsym(srsym),p^.left^.symtable),
  394. genordinalconstnode(1,s32bitdef));
  395. if (p^.left^.resulttype^.deftype=arraydef) and
  396. (parraydef(p^.left^.resulttype)^.elesize<>1) then
  397. hp:=gennode(muln,hp,genordinalconstnode(parraydef(p^.left^.resulttype)^.elesize,s32bitdef));
  398. disposetree(p);
  399. p:=hp;
  400. firstpass(p);
  401. end;
  402. if p^.registers32<1 then
  403. p^.registers32:=1;
  404. p^.resulttype:=s32bitdef;
  405. p^.location.loc:=LOC_REGISTER;
  406. end;
  407. in_typeof_x:
  408. begin
  409. set_varstate(p^.left,false);
  410. if p^.registers32<1 then
  411. p^.registers32:=1;
  412. p^.location.loc:=LOC_REGISTER;
  413. p^.resulttype:=voidpointerdef;
  414. end;
  415. in_ord_x:
  416. begin
  417. set_varstate(p^.left,true);
  418. if (p^.left^.treetype=ordconstn) then
  419. begin
  420. hp:=genordinalconstnode(p^.left^.value,s32bitdef);
  421. disposetree(p);
  422. p:=hp;
  423. firstpass(p);
  424. end
  425. else
  426. begin
  427. { otherwise you get a crash if you try ord on an expression containing }
  428. { an undeclared variable (JM) }
  429. if not assigned(p^.left^.resulttype) then
  430. exit;
  431. if (p^.left^.resulttype^.deftype=orddef) then
  432. if (porddef(p^.left^.resulttype)^.typ in [uchar,uwidechar,bool8bit]) then
  433. case porddef(p^.left^.resulttype)^.typ of
  434. uchar:
  435. begin
  436. hp:=gentypeconvnode(p^.left,u8bitdef);
  437. putnode(p);
  438. p:=hp;
  439. p^.explizit:=true;
  440. firstpass(p);
  441. end;
  442. uwidechar:
  443. begin
  444. hp:=gentypeconvnode(p^.left,u16bitdef);
  445. putnode(p);
  446. p:=hp;
  447. p^.explizit:=true;
  448. firstpass(p);
  449. end;
  450. bool8bit:
  451. begin
  452. hp:=gentypeconvnode(p^.left,u8bitdef);
  453. putnode(p);
  454. p:=hp;
  455. p^.convtyp:=tc_bool_2_int;
  456. p^.explizit:=true;
  457. firstpass(p);
  458. end
  459. end
  460. { can this happen ? }
  461. else if (porddef(p^.left^.resulttype)^.typ=uvoid) then
  462. CGMessage(type_e_mismatch)
  463. else
  464. { all other orddef need no transformation }
  465. begin
  466. hp:=p^.left;
  467. putnode(p);
  468. p:=hp;
  469. end
  470. else if (p^.left^.resulttype^.deftype=enumdef) then
  471. begin
  472. hp:=gentypeconvnode(p^.left,s32bitdef);
  473. putnode(p);
  474. p:=hp;
  475. p^.explizit:=true;
  476. firstpass(p);
  477. end
  478. else
  479. begin
  480. { can anything else be ord() ?}
  481. CGMessage(type_e_mismatch);
  482. end;
  483. end;
  484. end;
  485. in_chr_byte:
  486. begin
  487. set_varstate(p^.left,true);
  488. hp:=gentypeconvnode(p^.left,cchardef);
  489. putnode(p);
  490. p:=hp;
  491. p^.explizit:=true;
  492. firstpass(p);
  493. end;
  494. in_length_string:
  495. begin
  496. set_varstate(p^.left,true);
  497. if is_ansistring(p^.left^.resulttype) then
  498. p^.resulttype:=s32bitdef
  499. else
  500. p^.resulttype:=u8bitdef;
  501. { we don't need string conversations here }
  502. if (p^.left^.treetype=typeconvn) and
  503. (p^.left^.left^.resulttype^.deftype=stringdef) then
  504. begin
  505. hp:=p^.left^.left;
  506. putnode(p^.left);
  507. p^.left:=hp;
  508. end;
  509. { check the type, must be string or char }
  510. if (p^.left^.resulttype^.deftype<>stringdef) and
  511. (not is_char(p^.left^.resulttype)) then
  512. CGMessage(type_e_mismatch);
  513. { evaluates length of constant strings direct }
  514. if (p^.left^.treetype=stringconstn) then
  515. begin
  516. hp:=genordinalconstnode(p^.left^.length,s32bitdef);
  517. disposetree(p);
  518. firstpass(hp);
  519. p:=hp;
  520. end
  521. { length of char is one allways }
  522. else if is_constcharnode(p^.left) then
  523. begin
  524. hp:=genordinalconstnode(1,s32bitdef);
  525. disposetree(p);
  526. firstpass(hp);
  527. p:=hp;
  528. end;
  529. end;
  530. in_assigned_x:
  531. begin
  532. set_varstate(p^.left,true);
  533. p^.resulttype:=booldef;
  534. p^.location.loc:=LOC_FLAGS;
  535. end;
  536. in_ofs_x,
  537. in_seg_x :
  538. set_varstate(p^.left,false);
  539. in_pred_x,
  540. in_succ_x:
  541. begin
  542. p^.resulttype:=p^.left^.resulttype;
  543. if is_64bitint(p^.resulttype) then
  544. begin
  545. if (p^.registers32<2) then
  546. p^.registers32:=2
  547. end
  548. else
  549. begin
  550. if (p^.registers32<1) then
  551. p^.registers32:=1;
  552. end;
  553. p^.location.loc:=LOC_REGISTER;
  554. set_varstate(p^.left,true);
  555. if not is_ordinal(p^.resulttype) then
  556. CGMessage(type_e_ordinal_expr_expected)
  557. else
  558. begin
  559. if (p^.resulttype^.deftype=enumdef) and
  560. (penumdef(p^.resulttype)^.has_jumps) then
  561. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible)
  562. else
  563. if p^.left^.treetype=ordconstn then
  564. begin
  565. if p^.inlinenumber=in_succ_x then
  566. hp:=genordinalconstnode(p^.left^.value+1,p^.left^.resulttype)
  567. else
  568. hp:=genordinalconstnode(p^.left^.value-1,p^.left^.resulttype);
  569. disposetree(p);
  570. firstpass(hp);
  571. p:=hp;
  572. end;
  573. end;
  574. end;
  575. in_inc_x,
  576. in_dec_x:
  577. begin
  578. p^.resulttype:=voiddef;
  579. if assigned(p^.left) then
  580. begin
  581. firstcallparan(p^.left,nil,true);
  582. set_varstate(p^.left,true);
  583. if codegenerror then
  584. exit;
  585. { first param must be var }
  586. valid_for_assign(p^.left^.left,false);
  587. { check type }
  588. if (p^.left^.resulttype^.deftype in [enumdef,pointerdef]) or
  589. is_ordinal(p^.left^.resulttype) then
  590. begin
  591. { two paras ? }
  592. if assigned(p^.left^.right) then
  593. begin
  594. { insert a type conversion }
  595. { the second param is always longint }
  596. p^.left^.right^.left:=gentypeconvnode(p^.left^.right^.left,s32bitdef);
  597. { check the type conversion }
  598. firstpass(p^.left^.right^.left);
  599. { need we an additional register ? }
  600. if not(is_constintnode(p^.left^.right^.left)) and
  601. (p^.left^.right^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) and
  602. (p^.left^.right^.left^.registers32<=1) then
  603. inc(p^.registers32);
  604. { do we need an additional register to restore the first parameter? }
  605. if p^.left^.right^.left^.registers32>=p^.registers32 then
  606. inc(p^.registers32);
  607. if assigned(p^.left^.right^.right) then
  608. CGMessage(cg_e_illegal_expression);
  609. end;
  610. end
  611. else
  612. CGMessage(type_e_ordinal_expr_expected);
  613. end
  614. else
  615. CGMessage(type_e_mismatch);
  616. end;
  617. in_read_x,
  618. in_readln_x,
  619. in_write_x,
  620. in_writeln_x :
  621. begin
  622. { needs a call }
  623. procinfo^.flags:=procinfo^.flags or pi_do_call;
  624. p^.resulttype:=voiddef;
  625. { true, if readln needs an extra register }
  626. extra_register:=false;
  627. { we must know if it is a typed file or not }
  628. { but we must first do the firstpass for it }
  629. file_is_typed:=false;
  630. if assigned(p^.left) then
  631. begin
  632. dowrite:=(p^.inlinenumber in [in_write_x,in_writeln_x]);
  633. firstcallparan(p^.left,nil,true);
  634. set_varstate(p^.left,dowrite);
  635. { now we can check }
  636. hp:=p^.left;
  637. while assigned(hp^.right) do
  638. hp:=hp^.right;
  639. { if resulttype is not assigned, then automatically }
  640. { file is not typed. }
  641. if assigned(hp) and assigned(hp^.resulttype) then
  642. Begin
  643. if (hp^.resulttype^.deftype=filedef) then
  644. if (pfiledef(hp^.resulttype)^.filetyp=ft_untyped) then
  645. begin
  646. if (p^.inlinenumber in [in_readln_x,in_writeln_x]) then
  647. CGMessage(type_e_no_readln_writeln_for_typed_file)
  648. else
  649. CGMessage(type_e_no_read_write_for_untyped_file);
  650. end
  651. else if (pfiledef(hp^.resulttype)^.filetyp=ft_typed) then
  652. begin
  653. file_is_typed:=true;
  654. { test the type }
  655. if (p^.inlinenumber in [in_readln_x,in_writeln_x]) then
  656. CGMessage(type_e_no_readln_writeln_for_typed_file);
  657. hpp:=p^.left;
  658. while (hpp<>hp) do
  659. begin
  660. if (hpp^.left^.treetype=typen) then
  661. CGMessage(type_e_cant_read_write_type);
  662. if not is_equal(hpp^.resulttype,pfiledef(hp^.resulttype)^.typedfiletype.def) then
  663. CGMessage(type_e_mismatch);
  664. { generate the high() value for the shortstring }
  665. if ((not dowrite) and is_shortstring(hpp^.left^.resulttype)) or
  666. (is_chararray(hpp^.left^.resulttype)) then
  667. gen_high_tree(hpp,true);
  668. { read(ln) is call by reference (JM) }
  669. if not dowrite then
  670. make_not_regable(hpp^.left);
  671. hpp:=hpp^.right;
  672. end;
  673. end;
  674. end; { endif assigned(hp) }
  675. { insert type conversions for write(ln) }
  676. if (not file_is_typed) then
  677. begin
  678. hp:=p^.left;
  679. while assigned(hp) do
  680. begin
  681. incrementregisterpushed($ff);
  682. if (hp^.left^.treetype=typen) then
  683. CGMessage(type_e_cant_read_write_type);
  684. if assigned(hp^.left^.resulttype) then
  685. begin
  686. isreal:=false;
  687. { support writeln(procvar) }
  688. if (hp^.left^.resulttype^.deftype=procvardef) then
  689. begin
  690. p1:=gencallnode(nil,nil);
  691. p1^.right:=hp^.left;
  692. p1^.resulttype:=pprocvardef(hp^.left^.resulttype)^.rettype.def;
  693. firstpass(p1);
  694. hp^.left:=p1;
  695. end;
  696. case hp^.left^.resulttype^.deftype of
  697. filedef :
  698. begin
  699. { only allowed as first parameter }
  700. if assigned(hp^.right) then
  701. CGMessage(type_e_cant_read_write_type);
  702. end;
  703. stringdef :
  704. begin
  705. { generate the high() value for the shortstring }
  706. if (not dowrite) and
  707. is_shortstring(hp^.left^.resulttype) then
  708. gen_high_tree(hp,true);
  709. end;
  710. pointerdef :
  711. begin
  712. if not is_pchar(hp^.left^.resulttype) then
  713. CGMessage(type_e_cant_read_write_type);
  714. end;
  715. floatdef :
  716. begin
  717. isreal:=true;
  718. end;
  719. orddef :
  720. begin
  721. case porddef(hp^.left^.resulttype)^.typ of
  722. uchar,
  723. u32bit,s32bit,
  724. u64bit,s64bit:
  725. ;
  726. u8bit,s8bit,
  727. u16bit,s16bit :
  728. if dowrite then
  729. hp^.left:=gentypeconvnode(hp^.left,s32bitdef);
  730. bool8bit,
  731. bool16bit,
  732. bool32bit :
  733. if dowrite then
  734. hp^.left:=gentypeconvnode(hp^.left,booldef)
  735. else
  736. CGMessage(type_e_cant_read_write_type);
  737. else
  738. CGMessage(type_e_cant_read_write_type);
  739. end;
  740. if not(dowrite) and
  741. not(is_64bitint(hp^.left^.resulttype)) then
  742. extra_register:=true;
  743. end;
  744. arraydef :
  745. begin
  746. if is_chararray(hp^.left^.resulttype) then
  747. gen_high_tree(hp,true)
  748. else
  749. CGMessage(type_e_cant_read_write_type);
  750. end;
  751. else
  752. CGMessage(type_e_cant_read_write_type);
  753. end;
  754. { some format options ? }
  755. if hp^.is_colon_para then
  756. begin
  757. if hp^.right^.is_colon_para then
  758. begin
  759. frac_para:=hp;
  760. length_para:=hp^.right;
  761. hp:=hp^.right;
  762. hpp:=hp^.right;
  763. end
  764. else
  765. begin
  766. length_para:=hp;
  767. frac_para:=nil;
  768. hpp:=hp^.right;
  769. end;
  770. { can be nil if you use "write(e:0:6)" while e is undeclared (JM) }
  771. if assigned(hpp^.left^.resulttype) then
  772. isreal:=(hpp^.left^.resulttype^.deftype=floatdef)
  773. else exit;
  774. if (not is_integer(length_para^.left^.resulttype)) then
  775. CGMessage1(type_e_integer_expr_expected,length_para^.left^.resulttype^.typename)
  776. else
  777. length_para^.left:=gentypeconvnode(length_para^.left,s32bitdef);
  778. if assigned(frac_para) then
  779. begin
  780. if isreal then
  781. begin
  782. if (not is_integer(frac_para^.left^.resulttype)) then
  783. CGMessage1(type_e_integer_expr_expected,frac_para^.left^.resulttype^.typename)
  784. else
  785. frac_para^.left:=gentypeconvnode(frac_para^.left,s32bitdef);
  786. end
  787. else
  788. CGMessage(parser_e_illegal_colon_qualifier);
  789. end;
  790. { do the checking for the colon'd arg }
  791. hp:=length_para;
  792. end;
  793. end;
  794. hp:=hp^.right;
  795. end;
  796. end;
  797. { pass all parameters again for the typeconversions }
  798. if codegenerror then
  799. exit;
  800. firstcallparan(p^.left,nil,true);
  801. set_varstate(p^.left,true);
  802. { calc registers }
  803. left_right_max(p);
  804. if extra_register then
  805. inc(p^.registers32);
  806. end;
  807. end;
  808. in_settextbuf_file_x :
  809. begin
  810. { warning here p^.left is the callparannode
  811. not the argument directly }
  812. { p^.left^.left is text var }
  813. { p^.left^.right^.left is the buffer var }
  814. { firstcallparan(p^.left,nil);
  815. already done in firstcalln }
  816. { now we know the type of buffer }
  817. getsymonlyin(systemunit,'SETTEXTBUF');
  818. hp:=gencallnode(pprocsym(srsym),systemunit);
  819. hp^.left:=gencallparanode(
  820. genordinalconstnode(p^.left^.left^.resulttype^.size,s32bitdef),p^.left);
  821. putnode(p);
  822. p:=hp;
  823. firstpass(p);
  824. end;
  825. { the firstpass of the arg has been done in firstcalln ? }
  826. in_reset_typedfile,
  827. in_rewrite_typedfile :
  828. begin
  829. procinfo^.flags:=procinfo^.flags or pi_do_call;
  830. firstpass(p^.left);
  831. set_varstate(p^.left,true);
  832. p^.resulttype:=voiddef;
  833. end;
  834. in_str_x_string :
  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. { first pass just the string for first local use }
  846. hp:=p^.left^.right;
  847. p^.left^.right:=nil;
  848. firstcallparan(p^.left,nil,true);
  849. set_varstate(p^.left,false);
  850. { remove warning when result is passed }
  851. set_funcret_is_valid(p^.left^.left);
  852. p^.left^.right:=hp;
  853. firstcallparan(p^.left^.right,nil,true);
  854. set_varstate(p^.left^.right,true);
  855. hp:=p^.left;
  856. { valid string ? }
  857. if not assigned(hp) or
  858. (hp^.left^.resulttype^.deftype<>stringdef) or
  859. (hp^.right=nil) then
  860. CGMessage(cg_e_illegal_expression);
  861. { we need a var parameter }
  862. valid_for_assign(hp^.left,false);
  863. { generate the high() value for the shortstring }
  864. if is_shortstring(hp^.left^.resulttype) then
  865. gen_high_tree(hp,true);
  866. { !!!! check length of string }
  867. while assigned(hp^.right) do
  868. hp:=hp^.right;
  869. { check and convert the first param }
  870. if hp^.is_colon_para then
  871. CGMessage(cg_e_illegal_expression);
  872. isreal:=false;
  873. case hp^.resulttype^.deftype of
  874. orddef :
  875. begin
  876. case porddef(hp^.left^.resulttype)^.typ of
  877. u32bit,s32bit,
  878. s64bit,u64bit:
  879. ;
  880. u8bit,s8bit,
  881. u16bit,s16bit:
  882. hp^.left:=gentypeconvnode(hp^.left,s32bitdef);
  883. else
  884. CGMessage(type_e_integer_or_real_expr_expected);
  885. end;
  886. end;
  887. floatdef :
  888. begin
  889. isreal:=true;
  890. end;
  891. else
  892. CGMessage(type_e_integer_or_real_expr_expected);
  893. end;
  894. { some format options ? }
  895. hpp:=p^.left^.right;
  896. if assigned(hpp) and hpp^.is_colon_para then
  897. begin
  898. firstpass(hpp^.left);
  899. set_varstate(hpp^.left,true);
  900. if (not is_integer(hpp^.left^.resulttype)) then
  901. CGMessage1(type_e_integer_expr_expected,hpp^.left^.resulttype^.typename)
  902. else
  903. hpp^.left:=gentypeconvnode(hpp^.left,s32bitdef);
  904. hpp:=hpp^.right;
  905. if assigned(hpp) and hpp^.is_colon_para then
  906. begin
  907. if isreal then
  908. begin
  909. if (not is_integer(hpp^.left^.resulttype)) then
  910. CGMessage1(type_e_integer_expr_expected,hpp^.left^.resulttype^.typename)
  911. else
  912. begin
  913. firstpass(hpp^.left);
  914. set_varstate(hpp^.left,true);
  915. hpp^.left:=gentypeconvnode(hpp^.left,s32bitdef);
  916. end;
  917. end
  918. else
  919. CGMessage(parser_e_illegal_colon_qualifier);
  920. end;
  921. end;
  922. { pass all parameters again for the typeconversions }
  923. if codegenerror then
  924. exit;
  925. firstcallparan(p^.left,nil,true);
  926. { calc registers }
  927. left_right_max(p);
  928. end;
  929. in_val_x :
  930. begin
  931. procinfo^.flags:=procinfo^.flags or pi_do_call;
  932. p^.resulttype:=voiddef;
  933. { check the amount of parameters }
  934. if not(assigned(p^.left)) or
  935. not(assigned(p^.left^.right)) then
  936. begin
  937. CGMessage(parser_e_wrong_parameter_size);
  938. exit;
  939. end;
  940. If Assigned(p^.left^.right^.right) Then
  941. {there is a "code" parameter}
  942. Begin
  943. { first pass just the code parameter for first local use}
  944. hp := p^.left^.right;
  945. p^.left^.right := nil;
  946. make_not_regable(p^.left^.left);
  947. firstcallparan(p^.left, nil,true);
  948. set_varstate(p^.left,false);
  949. if codegenerror then exit;
  950. p^.left^.right := hp;
  951. {code has to be a var parameter}
  952. if valid_for_assign(p^.left^.left,false) then
  953. begin
  954. if (p^.left^.left^.resulttype^.deftype <> orddef) or
  955. not(porddef(p^.left^.left^.resulttype)^.typ in
  956. [u16bit,s16bit,u32bit,s32bit]) then
  957. CGMessage(type_e_mismatch);
  958. end;
  959. hpp := p^.left^.right
  960. End
  961. Else hpp := p^.left;
  962. {now hpp = the destination value tree}
  963. { first pass just the destination parameter for first local use}
  964. hp:=hpp^.right;
  965. hpp^.right:=nil;
  966. {hpp = destination}
  967. make_not_regable(hpp^.left);
  968. firstcallparan(hpp,nil,true);
  969. set_varstate(hpp,false);
  970. if codegenerror then
  971. exit;
  972. { remove warning when result is passed }
  973. set_funcret_is_valid(hpp^.left);
  974. hpp^.right := hp;
  975. if valid_for_assign(hpp^.left,false) then
  976. begin
  977. If Not((hpp^.left^.resulttype^.deftype = floatdef) or
  978. ((hpp^.left^.resulttype^.deftype = orddef) And
  979. (POrdDef(hpp^.left^.resulttype)^.typ in
  980. [u32bit,s32bit,
  981. u8bit,s8bit,u16bit,s16bit,s64bit,u64bit]))) Then
  982. CGMessage(type_e_mismatch);
  983. end;
  984. {hp = source (String)}
  985. { count_ref := false; WHY ?? }
  986. firstcallparan(hp,nil,true);
  987. set_varstate(hp,true);
  988. if codegenerror then
  989. exit;
  990. { if not a stringdef then insert a type conv which
  991. does the other type checking }
  992. If (hp^.left^.resulttype^.deftype<>stringdef) then
  993. begin
  994. hp^.left:=gentypeconvnode(hp^.left,cshortstringdef);
  995. firstpass(hp);
  996. end;
  997. { calc registers }
  998. left_right_max(p);
  999. { val doesn't calculate the registers really }
  1000. { correct, we need one register extra (FK) }
  1001. if is_64bitint(hpp^.left^.resulttype) then
  1002. inc(p^.registers32,2)
  1003. else
  1004. inc(p^.registers32,1);
  1005. end;
  1006. in_include_x_y,
  1007. in_exclude_x_y:
  1008. begin
  1009. p^.resulttype:=voiddef;
  1010. if assigned(p^.left) then
  1011. begin
  1012. firstcallparan(p^.left,nil,true);
  1013. set_varstate(p^.left,true);
  1014. p^.registers32:=p^.left^.registers32;
  1015. p^.registersfpu:=p^.left^.registersfpu;
  1016. {$ifdef SUPPORT_MMX}
  1017. p^.registersmmx:=p^.left^.registersmmx;
  1018. {$endif SUPPORT_MMX}
  1019. { remove warning when result is passed }
  1020. set_funcret_is_valid(p^.left^.left);
  1021. { first param must be var }
  1022. valid_for_assign(p^.left^.left,false);
  1023. { check type }
  1024. if (p^.left^.resulttype^.deftype=setdef) then
  1025. begin
  1026. { two paras ? }
  1027. if assigned(p^.left^.right) then
  1028. begin
  1029. { insert a type conversion }
  1030. { to the type of the set elements }
  1031. p^.left^.right^.left:=gentypeconvnode(
  1032. p^.left^.right^.left,
  1033. psetdef(p^.left^.resulttype)^.elementtype.def);
  1034. { check the type conversion }
  1035. firstpass(p^.left^.right^.left);
  1036. { only three parameters are allowed }
  1037. if assigned(p^.left^.right^.right) then
  1038. CGMessage(cg_e_illegal_expression);
  1039. end;
  1040. end
  1041. else
  1042. CGMessage(type_e_mismatch);
  1043. end
  1044. else
  1045. CGMessage(type_e_mismatch);
  1046. end;
  1047. in_low_x,
  1048. in_high_x:
  1049. begin
  1050. set_varstate(p^.left,false);
  1051. { this fixes tests\webtbs\tbug879.pp (FK)
  1052. if p^.left^.treetype in [typen,loadn,subscriptn] then
  1053. begin
  1054. }
  1055. case p^.left^.resulttype^.deftype of
  1056. orddef,enumdef:
  1057. begin
  1058. do_lowhigh(p^.left^.resulttype);
  1059. firstpass(p);
  1060. end;
  1061. setdef:
  1062. begin
  1063. do_lowhigh(Psetdef(p^.left^.resulttype)^.elementtype.def);
  1064. firstpass(p);
  1065. end;
  1066. arraydef:
  1067. begin
  1068. if p^.inlinenumber=in_low_x then
  1069. begin
  1070. hp:=genordinalconstnode(Parraydef(p^.left^.resulttype)^.lowrange,
  1071. Parraydef(p^.left^.resulttype)^.rangetype.def);
  1072. disposetree(p);
  1073. p:=hp;
  1074. firstpass(p);
  1075. end
  1076. else
  1077. begin
  1078. if is_open_array(p^.left^.resulttype) or
  1079. is_array_of_const(p^.left^.resulttype) then
  1080. begin
  1081. getsymonlyin(p^.left^.symtable,'high'+pvarsym(p^.left^.symtableentry)^.name);
  1082. hp:=genloadnode(pvarsym(srsym),p^.left^.symtable);
  1083. disposetree(p);
  1084. p:=hp;
  1085. firstpass(p);
  1086. end
  1087. else
  1088. begin
  1089. hp:=genordinalconstnode(Parraydef(p^.left^.resulttype)^.highrange,
  1090. Parraydef(p^.left^.resulttype)^.rangetype.def);
  1091. disposetree(p);
  1092. p:=hp;
  1093. firstpass(p);
  1094. end;
  1095. end;
  1096. end;
  1097. stringdef:
  1098. begin
  1099. if p^.inlinenumber=in_low_x then
  1100. begin
  1101. hp:=genordinalconstnode(0,u8bitdef);
  1102. disposetree(p);
  1103. p:=hp;
  1104. firstpass(p);
  1105. end
  1106. else
  1107. begin
  1108. if is_open_string(p^.left^.resulttype) then
  1109. begin
  1110. getsymonlyin(p^.left^.symtable,'high'+pvarsym(p^.left^.symtableentry)^.name);
  1111. hp:=genloadnode(pvarsym(srsym),p^.left^.symtable);
  1112. disposetree(p);
  1113. p:=hp;
  1114. firstpass(p);
  1115. end
  1116. else
  1117. begin
  1118. hp:=genordinalconstnode(Pstringdef(p^.left^.resulttype)^.len,u8bitdef);
  1119. disposetree(p);
  1120. p:=hp;
  1121. firstpass(p);
  1122. end;
  1123. end;
  1124. end;
  1125. else
  1126. CGMessage(type_e_mismatch);
  1127. end;
  1128. {
  1129. end
  1130. else
  1131. CGMessage(type_e_varid_or_typeid_expected);
  1132. }
  1133. end;
  1134. in_cos_extended:
  1135. begin
  1136. if p^.left^.treetype in [ordconstn,realconstn] then
  1137. setconstrealvalue(cos(getconstrealvalue))
  1138. else
  1139. handleextendedfunction;
  1140. end;
  1141. in_sin_extended:
  1142. begin
  1143. if p^.left^.treetype in [ordconstn,realconstn] then
  1144. setconstrealvalue(sin(getconstrealvalue))
  1145. else
  1146. handleextendedfunction;
  1147. end;
  1148. in_arctan_extended:
  1149. begin
  1150. if p^.left^.treetype in [ordconstn,realconstn] then
  1151. setconstrealvalue(arctan(getconstrealvalue))
  1152. else
  1153. handleextendedfunction;
  1154. end;
  1155. in_pi:
  1156. if block_type=bt_const then
  1157. setconstrealvalue(pi)
  1158. else
  1159. begin
  1160. p^.location.loc:=LOC_FPU;
  1161. p^.resulttype:=s80floatdef;
  1162. end;
  1163. in_abs_extended:
  1164. begin
  1165. if p^.left^.treetype in [ordconstn,realconstn] then
  1166. setconstrealvalue(abs(getconstrealvalue))
  1167. else
  1168. handleextendedfunction;
  1169. end;
  1170. in_sqr_extended:
  1171. begin
  1172. if p^.left^.treetype in [ordconstn,realconstn] then
  1173. setconstrealvalue(sqr(getconstrealvalue))
  1174. else
  1175. handleextendedfunction;
  1176. end;
  1177. in_sqrt_extended:
  1178. begin
  1179. if p^.left^.treetype in [ordconstn,realconstn] then
  1180. begin
  1181. vr:=getconstrealvalue;
  1182. if vr<0.0 then
  1183. begin
  1184. CGMessage(type_e_wrong_math_argument);
  1185. setconstrealvalue(0);
  1186. end
  1187. else
  1188. setconstrealvalue(sqrt(vr));
  1189. end
  1190. else
  1191. handleextendedfunction;
  1192. end;
  1193. in_ln_extended:
  1194. begin
  1195. if p^.left^.treetype in [ordconstn,realconstn] then
  1196. begin
  1197. vr:=getconstrealvalue;
  1198. if vr<=0.0 then
  1199. begin
  1200. CGMessage(type_e_wrong_math_argument);
  1201. setconstrealvalue(0);
  1202. end
  1203. else
  1204. setconstrealvalue(ln(vr));
  1205. end
  1206. else
  1207. handleextendedfunction;
  1208. end;
  1209. {$ifdef SUPPORT_MMX}
  1210. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  1211. begin
  1212. end;
  1213. {$endif SUPPORT_MMX}
  1214. in_assert_x_y :
  1215. begin
  1216. p^.resulttype:=voiddef;
  1217. if assigned(p^.left) then
  1218. begin
  1219. firstcallparan(p^.left,nil,true);
  1220. set_varstate(p^.left,true);
  1221. p^.registers32:=p^.left^.registers32;
  1222. p^.registersfpu:=p^.left^.registersfpu;
  1223. {$ifdef SUPPORT_MMX}
  1224. p^.registersmmx:=p^.left^.registersmmx;
  1225. {$endif SUPPORT_MMX}
  1226. { check type }
  1227. if is_boolean(p^.left^.resulttype) then
  1228. begin
  1229. { must always be a string }
  1230. p^.left^.right^.left:=gentypeconvnode(p^.left^.right^.left,cshortstringdef);
  1231. firstpass(p^.left^.right^.left);
  1232. end
  1233. else
  1234. CGMessage(type_e_mismatch);
  1235. end
  1236. else
  1237. CGMessage(type_e_mismatch);
  1238. { We've checked the whole statement for correctness, now we
  1239. can remove it if assertions are off }
  1240. if not(cs_do_assertion in aktlocalswitches) then
  1241. begin
  1242. disposetree(p^.left);
  1243. putnode(p);
  1244. { we need a valid node, so insert a nothingn }
  1245. p:=genzeronode(nothingn);
  1246. end;
  1247. end;
  1248. else
  1249. internalerror(8);
  1250. end;
  1251. end;
  1252. { generate an error if no resulttype is set }
  1253. if not assigned(p^.resulttype) then
  1254. p^.resulttype:=generrordef;
  1255. dec(parsing_para_level);
  1256. end;
  1257. {$ifdef fpc}
  1258. {$maxfpuregisters default}
  1259. {$endif fpc}
  1260. end.
  1261. {
  1262. $Log$
  1263. Revision 1.3 2000-07-22 11:53:26 sg
  1264. * Added WideChar support to inlined 'ord' function
  1265. Revision 1.2 2000/07/13 11:32:52 michael
  1266. + removed logs
  1267. }