tcinl.pas 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Type checking and register allocation for inline nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit tcinl;
  19. interface
  20. uses
  21. tree;
  22. procedure firstinline(var p : ptree);
  23. implementation
  24. uses
  25. cobjects,verbose,globals,systems,
  26. globtype,
  27. symtable,aasm,types,
  28. hcodegen,htypechk,pass_1,
  29. tccal
  30. {$ifdef i386}
  31. {$ifdef ag386bin}
  32. ,i386base
  33. {$else}
  34. ,i386
  35. {$endif}
  36. ,tgeni386
  37. {$endif}
  38. {$ifdef m68k}
  39. ,m68k,tgen68k
  40. {$endif}
  41. ;
  42. {*****************************************************************************
  43. FirstInLine
  44. *****************************************************************************}
  45. procedure firstinline(var p : ptree);
  46. var
  47. vl,vl2 : longint;
  48. vr : bestreal;
  49. hp,hpp : ptree;
  50. {$ifndef NOCOLONCHECK}
  51. frac_para,length_para : ptree;
  52. {$endif ndef NOCOLONCHECK}
  53. store_count_ref,
  54. isreal,
  55. dowrite,
  56. store_valid,
  57. file_is_typed : boolean;
  58. procedure do_lowhigh(adef : pdef);
  59. var
  60. v : longint;
  61. enum : penumsym;
  62. begin
  63. case Adef^.deftype of
  64. orddef:
  65. begin
  66. if p^.inlinenumber=in_low_x then
  67. v:=porddef(Adef)^.low
  68. else
  69. v:=porddef(Adef)^.high;
  70. hp:=genordinalconstnode(v,adef);
  71. firstpass(hp);
  72. disposetree(p);
  73. p:=hp;
  74. end;
  75. enumdef:
  76. begin
  77. enum:=Penumdef(Adef)^.first;
  78. if p^.inlinenumber=in_high_x then
  79. while enum^.next<>nil do
  80. enum:=enum^.next;
  81. hp:=genenumnode(enum);
  82. disposetree(p);
  83. p:=hp;
  84. end
  85. end;
  86. end;
  87. begin
  88. store_valid:=must_be_valid;
  89. store_count_ref:=count_ref;
  90. count_ref:=false;
  91. if not (p^.inlinenumber in [in_read_x,in_readln_x,in_sizeof_x,
  92. in_typeof_x,in_ord_x,in_str_x_string,
  93. {$IfnDef OLDVAL}
  94. in_val_x,
  95. {$EndIf OLDVAL}
  96. in_reset_typedfile,in_rewrite_typedfile]) then
  97. must_be_valid:=true
  98. else
  99. must_be_valid:=false;
  100. { if we handle writeln; p^.left contains no valid address }
  101. if assigned(p^.left) then
  102. begin
  103. if p^.left^.treetype=callparan then
  104. firstcallparan(p^.left,nil)
  105. else
  106. firstpass(p^.left);
  107. left_right_max(p);
  108. set_location(p^.location,p^.left^.location);
  109. end;
  110. { handle intern constant functions in separate case }
  111. if p^.inlineconst then
  112. begin
  113. { no parameters? }
  114. if not assigned(p^.left) then
  115. begin
  116. case p^.inlinenumber of
  117. in_const_pi :
  118. hp:=genrealconstnode(pi);
  119. else
  120. internalerror(89);
  121. end;
  122. end
  123. else
  124. { process constant expression with parameter }
  125. begin
  126. vl:=0;
  127. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  128. vr:=0;
  129. isreal:=false;
  130. case p^.left^.treetype of
  131. realconstn :
  132. begin
  133. isreal:=true;
  134. vr:=p^.left^.value_real;
  135. end;
  136. ordconstn :
  137. vl:=p^.left^.value;
  138. callparan :
  139. begin
  140. { both exists, else it was not generated }
  141. vl:=p^.left^.left^.value;
  142. vl2:=p^.left^.right^.left^.value;
  143. end;
  144. else
  145. CGMessage(cg_e_illegal_expression);
  146. end;
  147. case p^.inlinenumber of
  148. in_const_trunc :
  149. begin
  150. if isreal then
  151. begin
  152. if (vr>=2147483648.0) or (vr<=-2147483649.0) then
  153. begin
  154. CGMessage(parser_e_range_check_error);
  155. hp:=genordinalconstnode(1,s32bitdef)
  156. end
  157. else
  158. hp:=genordinalconstnode(trunc(vr),s32bitdef)
  159. end
  160. else
  161. hp:=genordinalconstnode(trunc(vl),s32bitdef);
  162. end;
  163. in_const_round :
  164. begin
  165. if isreal then
  166. begin
  167. if (vr>=2147483647.5) or (vr<=-2147483648.5) then
  168. begin
  169. CGMessage(parser_e_range_check_error);
  170. hp:=genordinalconstnode(1,s32bitdef)
  171. end
  172. else
  173. hp:=genordinalconstnode(round(vr),s32bitdef)
  174. end
  175. else
  176. hp:=genordinalconstnode(round(vl),s32bitdef);
  177. end;
  178. in_const_frac :
  179. begin
  180. if isreal then
  181. hp:=genrealconstnode(frac(vr))
  182. else
  183. hp:=genrealconstnode(frac(vl));
  184. end;
  185. in_const_int :
  186. begin
  187. if isreal then
  188. hp:=genrealconstnode(int(vr))
  189. else
  190. hp:=genrealconstnode(int(vl));
  191. end;
  192. in_const_abs :
  193. begin
  194. if isreal then
  195. hp:=genrealconstnode(abs(vr))
  196. else
  197. hp:=genordinalconstnode(abs(vl),p^.left^.resulttype);
  198. end;
  199. in_const_sqr :
  200. begin
  201. if isreal then
  202. hp:=genrealconstnode(sqr(vr))
  203. else
  204. hp:=genordinalconstnode(sqr(vl),p^.left^.resulttype);
  205. end;
  206. in_const_odd :
  207. begin
  208. if isreal then
  209. CGMessage(type_e_integer_expr_expected)
  210. else
  211. hp:=genordinalconstnode(byte(odd(vl)),booldef);
  212. end;
  213. in_const_swap_word :
  214. begin
  215. if isreal then
  216. CGMessage(type_e_integer_expr_expected)
  217. else
  218. hp:=genordinalconstnode((vl and $ff) shl 8+(vl shr 8),p^.left^.resulttype);
  219. end;
  220. in_const_swap_long :
  221. begin
  222. if isreal then
  223. CGMessage(type_e_mismatch)
  224. else
  225. hp:=genordinalconstnode((vl and $ffff) shl 16+(vl shr 16),p^.left^.resulttype);
  226. end;
  227. in_const_ptr :
  228. begin
  229. if isreal then
  230. CGMessage(type_e_mismatch)
  231. else
  232. hp:=genordinalconstnode((vl2 shl 16) or vl,voidpointerdef);
  233. end;
  234. in_const_sqrt :
  235. begin
  236. if isreal then
  237. hp:=genrealconstnode(sqrt(vr))
  238. else
  239. hp:=genrealconstnode(sqrt(vl));
  240. end;
  241. in_const_arctan :
  242. begin
  243. if isreal then
  244. hp:=genrealconstnode(arctan(vr))
  245. else
  246. hp:=genrealconstnode(arctan(vl));
  247. end;
  248. in_const_cos :
  249. begin
  250. if isreal then
  251. hp:=genrealconstnode(cos(vr))
  252. else
  253. hp:=genrealconstnode(cos(vl));
  254. end;
  255. in_const_sin :
  256. begin
  257. if isreal then
  258. hp:=genrealconstnode(sin(vr))
  259. else
  260. hp:=genrealconstnode(sin(vl));
  261. end;
  262. in_const_exp :
  263. begin
  264. if isreal then
  265. hp:=genrealconstnode(exp(vr))
  266. else
  267. hp:=genrealconstnode(exp(vl));
  268. end;
  269. in_const_ln :
  270. begin
  271. if isreal then
  272. hp:=genrealconstnode(ln(vr))
  273. else
  274. hp:=genrealconstnode(ln(vl));
  275. end;
  276. else
  277. internalerror(88);
  278. end;
  279. end;
  280. disposetree(p);
  281. firstpass(hp);
  282. p:=hp;
  283. end
  284. else
  285. begin
  286. case p^.inlinenumber of
  287. in_lo_long,
  288. in_hi_long,
  289. in_lo_word,
  290. in_hi_word:
  291. begin
  292. if p^.registers32<1 then
  293. p^.registers32:=1;
  294. if p^.inlinenumber in [in_lo_word,in_hi_word] then
  295. p^.resulttype:=u8bitdef
  296. else
  297. begin
  298. p^.resulttype:=u16bitdef;
  299. if (m_tp in aktmodeswitches) or
  300. (m_delphi in aktmodeswitches) then
  301. CGMessage(type_w_maybe_wrong_hi_lo);
  302. end;
  303. p^.location.loc:=LOC_REGISTER;
  304. if not is_integer(p^.left^.resulttype) then
  305. CGMessage(type_e_mismatch)
  306. else
  307. begin
  308. if p^.left^.treetype=ordconstn then
  309. begin
  310. case p^.inlinenumber of
  311. in_lo_word : hp:=genordinalconstnode(p^.left^.value and $ff,p^.left^.resulttype);
  312. in_hi_word : hp:=genordinalconstnode(p^.left^.value shr 8,p^.left^.resulttype);
  313. in_lo_long : hp:=genordinalconstnode(p^.left^.value and $ffff,p^.left^.resulttype);
  314. in_hi_long : hp:=genordinalconstnode(p^.left^.value shr 16,p^.left^.resulttype);
  315. end;
  316. disposetree(p);
  317. firstpass(hp);
  318. p:=hp;
  319. end;
  320. end;
  321. end;
  322. in_sizeof_x:
  323. begin
  324. if push_high_param(p^.left^.resulttype) then
  325. begin
  326. getsymonlyin(p^.left^.symtable,'high'+pvarsym(p^.left^.symtableentry)^.name);
  327. hp:=gennode(addn,genloadnode(pvarsym(srsym),p^.left^.symtable),
  328. genordinalconstnode(1,s32bitdef));
  329. if (p^.left^.resulttype^.deftype=arraydef) and
  330. (parraydef(p^.left^.resulttype)^.elesize<>1) then
  331. hp:=gennode(muln,hp,genordinalconstnode(parraydef(p^.left^.resulttype)^.elesize,s32bitdef));
  332. disposetree(p);
  333. p:=hp;
  334. firstpass(p);
  335. end;
  336. if p^.registers32<1 then
  337. p^.registers32:=1;
  338. p^.resulttype:=s32bitdef;
  339. p^.location.loc:=LOC_REGISTER;
  340. end;
  341. in_typeof_x:
  342. begin
  343. if p^.registers32<1 then
  344. p^.registers32:=1;
  345. p^.location.loc:=LOC_REGISTER;
  346. p^.resulttype:=voidpointerdef;
  347. end;
  348. in_ord_x:
  349. begin
  350. if (p^.left^.treetype=ordconstn) then
  351. begin
  352. hp:=genordinalconstnode(p^.left^.value,s32bitdef);
  353. disposetree(p);
  354. p:=hp;
  355. firstpass(p);
  356. end
  357. else
  358. begin
  359. if (p^.left^.resulttype^.deftype=orddef) then
  360. if (porddef(p^.left^.resulttype)^.typ in [uchar,bool8bit]) then
  361. begin
  362. if porddef(p^.left^.resulttype)^.typ=bool8bit then
  363. begin
  364. hp:=gentypeconvnode(p^.left,u8bitdef);
  365. putnode(p);
  366. p:=hp;
  367. p^.convtyp:=tc_bool_2_int;
  368. p^.explizit:=true;
  369. firstpass(p);
  370. end
  371. else
  372. begin
  373. hp:=gentypeconvnode(p^.left,u8bitdef);
  374. putnode(p);
  375. p:=hp;
  376. p^.explizit:=true;
  377. firstpass(p);
  378. end;
  379. end
  380. { can this happen ? }
  381. else if (porddef(p^.left^.resulttype)^.typ=uvoid) then
  382. CGMessage(type_e_mismatch)
  383. else
  384. { all other orddef need no transformation }
  385. begin
  386. hp:=p^.left;
  387. putnode(p);
  388. p:=hp;
  389. end
  390. else if (p^.left^.resulttype^.deftype=enumdef) then
  391. begin
  392. hp:=gentypeconvnode(p^.left,s32bitdef);
  393. putnode(p);
  394. p:=hp;
  395. p^.explizit:=true;
  396. firstpass(p);
  397. end
  398. else
  399. begin
  400. { can anything else be ord() ?}
  401. CGMessage(type_e_mismatch);
  402. end;
  403. end;
  404. end;
  405. in_chr_byte:
  406. begin
  407. hp:=gentypeconvnode(p^.left,cchardef);
  408. putnode(p);
  409. p:=hp;
  410. p^.explizit:=true;
  411. firstpass(p);
  412. end;
  413. in_length_string:
  414. begin
  415. if is_ansistring(p^.left^.resulttype) then
  416. p^.resulttype:=s32bitdef
  417. else
  418. p^.resulttype:=u8bitdef;
  419. { we don't need string conversations here }
  420. if (p^.left^.treetype=typeconvn) and
  421. (p^.left^.left^.resulttype^.deftype=stringdef) then
  422. begin
  423. hp:=p^.left^.left;
  424. putnode(p^.left);
  425. p^.left:=hp;
  426. end;
  427. { check the type, must be string or char }
  428. if (p^.left^.resulttype^.deftype<>stringdef) and
  429. (not is_char(p^.left^.resulttype)) then
  430. CGMessage(type_e_mismatch);
  431. { evaluates length of constant strings direct }
  432. if (p^.left^.treetype=stringconstn) then
  433. begin
  434. hp:=genordinalconstnode(p^.left^.length,s32bitdef);
  435. disposetree(p);
  436. firstpass(hp);
  437. p:=hp;
  438. end
  439. { length of char is one allways }
  440. else if is_constcharnode(p^.left) then
  441. begin
  442. hp:=genordinalconstnode(1,s32bitdef);
  443. disposetree(p);
  444. firstpass(hp);
  445. p:=hp;
  446. end;
  447. end;
  448. in_assigned_x:
  449. begin
  450. p^.resulttype:=booldef;
  451. p^.location.loc:=LOC_FLAGS;
  452. end;
  453. in_pred_x,
  454. in_succ_x:
  455. begin
  456. inc(p^.registers32);
  457. p^.resulttype:=p^.left^.resulttype;
  458. p^.location.loc:=LOC_REGISTER;
  459. if not is_ordinal(p^.resulttype) then
  460. CGMessage(type_e_ordinal_expr_expected)
  461. else
  462. begin
  463. if (p^.resulttype^.deftype=enumdef) and
  464. (penumdef(p^.resulttype)^.has_jumps) then
  465. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible)
  466. else
  467. if p^.left^.treetype=ordconstn then
  468. begin
  469. if p^.inlinenumber=in_succ_x then
  470. hp:=genordinalconstnode(p^.left^.value+1,p^.left^.resulttype)
  471. else
  472. hp:=genordinalconstnode(p^.left^.value-1,p^.left^.resulttype);
  473. disposetree(p);
  474. firstpass(hp);
  475. p:=hp;
  476. end;
  477. end;
  478. end;
  479. in_inc_x,
  480. in_dec_x:
  481. begin
  482. p^.resulttype:=voiddef;
  483. if assigned(p^.left) then
  484. begin
  485. firstcallparan(p^.left,nil);
  486. if codegenerror then
  487. exit;
  488. { first param must be var }
  489. if is_constnode(p^.left^.left) then
  490. CGMessage(type_e_variable_id_expected);
  491. { check type }
  492. if (p^.left^.resulttype^.deftype in [enumdef,pointerdef]) or
  493. is_ordinal(p^.left^.resulttype) then
  494. begin
  495. { two paras ? }
  496. if assigned(p^.left^.right) then
  497. begin
  498. { insert a type conversion }
  499. { the second param is always longint }
  500. p^.left^.right^.left:=gentypeconvnode(p^.left^.right^.left,s32bitdef);
  501. { check the type conversion }
  502. firstpass(p^.left^.right^.left);
  503. { need we an additional register ? }
  504. if not(is_constintnode(p^.left^.right^.left)) and
  505. (p^.left^.right^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) and
  506. (p^.left^.right^.left^.registers32<1) then
  507. inc(p^.registers32);
  508. if assigned(p^.left^.right^.right) then
  509. CGMessage(cg_e_illegal_expression);
  510. end;
  511. end
  512. else
  513. CGMessage(type_e_ordinal_expr_expected);
  514. end
  515. else
  516. CGMessage(type_e_mismatch);
  517. end;
  518. in_read_x,
  519. in_readln_x,
  520. in_write_x,
  521. in_writeln_x :
  522. begin
  523. { needs a call }
  524. procinfo.flags:=procinfo.flags or pi_do_call;
  525. p^.resulttype:=voiddef;
  526. { we must know if it is a typed file or not }
  527. { but we must first do the firstpass for it }
  528. file_is_typed:=false;
  529. if assigned(p^.left) then
  530. begin
  531. firstcallparan(p^.left,nil);
  532. { now we can check }
  533. hp:=p^.left;
  534. while assigned(hp^.right) do
  535. hp:=hp^.right;
  536. { if resulttype is not assigned, then automatically }
  537. { file is not typed. }
  538. if assigned(hp) and assigned(hp^.resulttype) then
  539. Begin
  540. if (hp^.resulttype^.deftype=filedef) and
  541. (pfiledef(hp^.resulttype)^.filetype=ft_typed) then
  542. begin
  543. file_is_typed:=true;
  544. { test the type }
  545. hpp:=p^.left;
  546. while (hpp<>hp) do
  547. begin
  548. if (hpp^.left^.treetype=typen) then
  549. CGMessage(type_e_cant_read_write_type);
  550. if not is_equal(hpp^.resulttype,pfiledef(hp^.resulttype)^.typed_as) then
  551. CGMessage(type_e_mismatch);
  552. hpp:=hpp^.right;
  553. end;
  554. end;
  555. end; { endif assigned(hp) }
  556. { insert type conversions for write(ln) }
  557. if (not file_is_typed) then
  558. begin
  559. dowrite:=(p^.inlinenumber in [in_write_x,in_writeln_x]);
  560. hp:=p^.left;
  561. while assigned(hp) do
  562. begin
  563. if (hp^.left^.treetype=typen) then
  564. CGMessage(type_e_cant_read_write_type);
  565. if assigned(hp^.left^.resulttype) then
  566. begin
  567. isreal:=false;
  568. case hp^.left^.resulttype^.deftype of
  569. filedef :
  570. begin
  571. { only allowed as first parameter }
  572. if assigned(hp^.right) then
  573. CGMessage(type_e_cant_read_write_type);
  574. end;
  575. stringdef :
  576. begin
  577. { generate the high() value for the shortstring }
  578. if (not dowrite) and
  579. is_shortstring(hp^.left^.resulttype) then
  580. gen_high_tree(hp,true);
  581. end;
  582. pointerdef :
  583. begin
  584. if not is_pchar(hp^.left^.resulttype) then
  585. CGMessage(type_e_cant_read_write_type);
  586. end;
  587. floatdef :
  588. begin
  589. isreal:=true;
  590. end;
  591. orddef :
  592. begin
  593. case porddef(hp^.left^.resulttype)^.typ of
  594. uchar,
  595. u32bit,s32bit,
  596. u64bit,s64bitint:
  597. ;
  598. u8bit,s8bit,
  599. u16bit,s16bit :
  600. if dowrite then
  601. hp^.left:=gentypeconvnode(hp^.left,s32bitdef);
  602. bool8bit,
  603. bool16bit,
  604. bool32bit :
  605. if dowrite then
  606. hp^.left:=gentypeconvnode(hp^.left,booldef)
  607. else
  608. CGMessage(type_e_cant_read_write_type);
  609. else
  610. CGMessage(type_e_cant_read_write_type);
  611. end;
  612. end;
  613. arraydef :
  614. begin
  615. if not is_chararray(hp^.left^.resulttype) then
  616. CGMessage(type_e_cant_read_write_type);
  617. end;
  618. else
  619. CGMessage(type_e_cant_read_write_type);
  620. end;
  621. { some format options ? }
  622. if hp^.is_colon_para then
  623. begin
  624. if hp^.right^.is_colon_para then
  625. begin
  626. frac_para:=hp;
  627. length_para:=hp^.right;
  628. hp:=hp^.right;
  629. hpp:=hp^.right;
  630. end
  631. else
  632. begin
  633. length_para:=hp;
  634. frac_para:=nil;
  635. hpp:=hp^.right;
  636. end;
  637. isreal:=hpp^.resulttype^.deftype=floatdef;
  638. if (not is_integer(length_para^.resulttype)) then
  639. CGMessage(type_e_integer_expr_expected)
  640. else
  641. length_para^.left:=gentypeconvnode(length_para^.left,s32bitdef);
  642. if assigned(frac_para) then
  643. begin
  644. if isreal then
  645. begin
  646. if (not is_integer(frac_para^.resulttype)) then
  647. CGMessage(type_e_integer_expr_expected)
  648. else
  649. frac_para^.left:=gentypeconvnode(frac_para^.left,s32bitdef);
  650. end
  651. else
  652. CGMessage(parser_e_illegal_colon_qualifier);
  653. end;
  654. { do the checking for the colon'd arg }
  655. hp:=length_para;
  656. end;
  657. end;
  658. hp:=hp^.right;
  659. end;
  660. end;
  661. { pass all parameters again for the typeconversions }
  662. if codegenerror then
  663. exit;
  664. must_be_valid:=true;
  665. firstcallparan(p^.left,nil);
  666. { calc registers }
  667. left_right_max(p);
  668. end;
  669. end;
  670. in_settextbuf_file_x :
  671. begin
  672. { warning here p^.left is the callparannode
  673. not the argument directly }
  674. { p^.left^.left is text var }
  675. { p^.left^.right^.left is the buffer var }
  676. { firstcallparan(p^.left,nil);
  677. already done in firstcalln }
  678. { now we know the type of buffer }
  679. getsymonlyin(systemunit,'SETTEXTBUF');
  680. hp:=gencallnode(pprocsym(srsym),systemunit);
  681. hp^.left:=gencallparanode(
  682. genordinalconstnode(p^.left^.left^.resulttype^.size,s32bitdef),p^.left);
  683. putnode(p);
  684. p:=hp;
  685. firstpass(p);
  686. end;
  687. { the firstpass of the arg has been done in firstcalln ? }
  688. in_reset_typedfile,
  689. in_rewrite_typedfile :
  690. begin
  691. procinfo.flags:=procinfo.flags or pi_do_call;
  692. { to be sure the right definition is loaded }
  693. p^.left^.resulttype:=nil;
  694. firstpass(p^.left);
  695. p^.resulttype:=voiddef;
  696. end;
  697. in_str_x_string :
  698. begin
  699. procinfo.flags:=procinfo.flags or pi_do_call;
  700. p^.resulttype:=voiddef;
  701. { check the amount of parameters }
  702. if not(assigned(p^.left)) or
  703. not(assigned(p^.left^.right)) then
  704. begin
  705. CGMessage(parser_e_wrong_parameter_size);
  706. exit;
  707. end;
  708. { first pass just the string for first local use }
  709. hp:=p^.left^.right;
  710. must_be_valid:=false;
  711. count_ref:=true;
  712. p^.left^.right:=nil;
  713. firstcallparan(p^.left,nil);
  714. { remove warning when result is passed }
  715. if (p^.left^.left^.treetype=funcretn) then
  716. procinfo.funcret_is_valid:=true;
  717. must_be_valid:=true;
  718. p^.left^.right:=hp;
  719. firstcallparan(p^.left^.right,nil);
  720. hp:=p^.left;
  721. { valid string ? }
  722. if not assigned(hp) or
  723. (hp^.left^.resulttype^.deftype<>stringdef) or
  724. (hp^.right=nil) or
  725. (hp^.left^.location.loc<>LOC_REFERENCE) then
  726. CGMessage(cg_e_illegal_expression);
  727. { generate the high() value for the shortstring }
  728. if is_shortstring(hp^.left^.resulttype) then
  729. gen_high_tree(hp,true);
  730. { !!!! check length of string }
  731. while assigned(hp^.right) do
  732. hp:=hp^.right;
  733. { check and convert the first param }
  734. if hp^.is_colon_para then
  735. CGMessage(cg_e_illegal_expression);
  736. isreal:=false;
  737. case hp^.resulttype^.deftype of
  738. orddef :
  739. begin
  740. case porddef(hp^.left^.resulttype)^.typ of
  741. u32bit,s32bit,
  742. s64bitint,u64bit:
  743. ;
  744. u8bit,s8bit,
  745. u16bit,s16bit:
  746. hp^.left:=gentypeconvnode(hp^.left,s32bitdef);
  747. else
  748. CGMessage(type_e_integer_or_real_expr_expected);
  749. end;
  750. end;
  751. floatdef :
  752. begin
  753. isreal:=true;
  754. end;
  755. else
  756. CGMessage(type_e_integer_or_real_expr_expected);
  757. end;
  758. { some format options ? }
  759. hpp:=p^.left^.right;
  760. if assigned(hpp) and hpp^.is_colon_para then
  761. begin
  762. if (not is_integer(hpp^.resulttype)) then
  763. CGMessage(type_e_integer_expr_expected)
  764. else
  765. hpp^.left:=gentypeconvnode(hpp^.left,s32bitdef);
  766. hpp:=hpp^.right;
  767. if assigned(hpp) and hpp^.is_colon_para then
  768. begin
  769. if isreal then
  770. begin
  771. if (not is_integer(hpp^.resulttype)) then
  772. CGMessage(type_e_integer_expr_expected)
  773. else
  774. hpp^.left:=gentypeconvnode(hpp^.left,s32bitdef);
  775. end
  776. else
  777. CGMessage(parser_e_illegal_colon_qualifier);
  778. end;
  779. end;
  780. { for first local use }
  781. must_be_valid:=false;
  782. count_ref:=true;
  783. { pass all parameters again for the typeconversions }
  784. if codegenerror then
  785. exit;
  786. must_be_valid:=true;
  787. firstcallparan(p^.left,nil);
  788. { calc registers }
  789. left_right_max(p);
  790. end;
  791. {$IfnDef OLDVAL}
  792. in_val_x :
  793. begin
  794. procinfo.flags:=procinfo.flags or pi_do_call;
  795. p^.resulttype:=voiddef;
  796. { check the amount of parameters }
  797. if not(assigned(p^.left)) or
  798. not(assigned(p^.left^.right)) then
  799. begin
  800. CGMessage(parser_e_wrong_parameter_size);
  801. exit;
  802. end;
  803. If Assigned(p^.left^.right^.right) Then
  804. {there is a "code" parameter}
  805. Begin
  806. { first pass just the code parameter for first local use}
  807. hp := p^.left^.right;
  808. p^.left^.right := nil;
  809. must_be_valid := false;
  810. count_ref := true;
  811. firstcallparan(p^.left, nil);
  812. if codegenerror then exit;
  813. p^.left^.right := hp;
  814. {code has to be a var parameter}
  815. if (p^.left^.left^.location.loc<>LOC_REFERENCE) then
  816. CGMessage(type_e_variable_id_expected)
  817. else
  818. if (p^.left^.left^.resulttype^.deftype <> orddef) or
  819. not(porddef(p^.left^.left^.resulttype)^.typ in
  820. [u16bit,s16bit,u32bit,s32bit]) then
  821. CGMessage(type_e_mismatch);
  822. hpp := p^.left^.right
  823. End
  824. Else hpp := p^.left;
  825. {now hpp = the destination value tree}
  826. { first pass just the destination parameter for first local use}
  827. hp:=hpp^.right;
  828. must_be_valid:=false;
  829. count_ref:=true;
  830. hpp^.right:=nil;
  831. {hpp = destination}
  832. firstcallparan(hpp,nil);
  833. if codegenerror then
  834. exit;
  835. { remove warning when result is passed }
  836. if (hpp^.left^.treetype=funcretn) then
  837. procinfo.funcret_is_valid:=true;
  838. hpp^.right := hp;
  839. if (hpp^.left^.location.loc<>LOC_REFERENCE) then
  840. CGMessage(type_e_variable_id_expected)
  841. else
  842. If Not((hpp^.left^.resulttype^.deftype = floatdef) or
  843. ((hpp^.left^.resulttype^.deftype = orddef) And
  844. (POrdDef(hpp^.left^.resulttype)^.typ in
  845. [u32bit,s32bit,{s64bitint,u64bit, -- not supported yet in RTL}
  846. u8bit,s8bit,u16bit,s16bit])))
  847. Then CGMessage(type_e_mismatch);
  848. must_be_valid:=true;
  849. {hp = source (String)}
  850. count_ref := false;
  851. must_be_valid := true;
  852. firstcallparan(hp,nil);
  853. if codegenerror then exit;
  854. If (hp^.resulttype^.deftype<>stringdef) then
  855. CGMessage(type_e_mismatch);
  856. { firstcallparan(p^.left,nil);}
  857. { calc registers }
  858. left_right_max(p);
  859. end;
  860. {$EndIf OLDVAL}
  861. in_include_x_y,
  862. in_exclude_x_y:
  863. begin
  864. p^.resulttype:=voiddef;
  865. if assigned(p^.left) then
  866. begin
  867. firstcallparan(p^.left,nil);
  868. p^.registers32:=p^.left^.registers32;
  869. p^.registersfpu:=p^.left^.registersfpu;
  870. {$ifdef SUPPORT_MMX}
  871. p^.registersmmx:=p^.left^.registersmmx;
  872. {$endif SUPPORT_MMX}
  873. { remove warning when result is passed }
  874. if (p^.left^.left^.treetype=funcretn) then
  875. procinfo.funcret_is_valid:=true;
  876. { first param must be var }
  877. if (p^.left^.left^.location.loc<>LOC_REFERENCE) and
  878. (p^.left^.left^.location.loc<>LOC_CREGISTER) then
  879. CGMessage(cg_e_illegal_expression);
  880. { check type }
  881. if (p^.left^.resulttype^.deftype=setdef) then
  882. begin
  883. { two paras ? }
  884. if assigned(p^.left^.right) then
  885. begin
  886. { insert a type conversion }
  887. { to the type of the set elements }
  888. p^.left^.right^.left:=gentypeconvnode(
  889. p^.left^.right^.left,
  890. psetdef(p^.left^.resulttype)^.setof);
  891. { check the type conversion }
  892. firstpass(p^.left^.right^.left);
  893. { only three parameters are allowed }
  894. if assigned(p^.left^.right^.right) then
  895. CGMessage(cg_e_illegal_expression);
  896. end;
  897. end
  898. else
  899. CGMessage(type_e_mismatch);
  900. end
  901. else
  902. CGMessage(type_e_mismatch);
  903. end;
  904. in_low_x,
  905. in_high_x:
  906. begin
  907. if p^.left^.treetype in [typen,loadn,subscriptn] then
  908. begin
  909. case p^.left^.resulttype^.deftype of
  910. orddef,enumdef:
  911. begin
  912. do_lowhigh(p^.left^.resulttype);
  913. firstpass(p);
  914. end;
  915. setdef:
  916. begin
  917. do_lowhigh(Psetdef(p^.left^.resulttype)^.setof);
  918. firstpass(p);
  919. end;
  920. arraydef:
  921. begin
  922. if p^.inlinenumber=in_low_x then
  923. begin
  924. hp:=genordinalconstnode(Parraydef(p^.left^.resulttype)^.lowrange,s32bitdef);
  925. disposetree(p);
  926. p:=hp;
  927. firstpass(p);
  928. end
  929. else
  930. begin
  931. if is_open_array(p^.left^.resulttype) then
  932. begin
  933. getsymonlyin(p^.left^.symtable,'high'+pvarsym(p^.left^.symtableentry)^.name);
  934. hp:=genloadnode(pvarsym(srsym),p^.left^.symtable);
  935. disposetree(p);
  936. p:=hp;
  937. firstpass(p);
  938. end
  939. else
  940. begin
  941. hp:=genordinalconstnode(Parraydef(p^.left^.resulttype)^.highrange,s32bitdef);
  942. disposetree(p);
  943. p:=hp;
  944. firstpass(p);
  945. end;
  946. end;
  947. end;
  948. stringdef:
  949. begin
  950. if p^.inlinenumber=in_low_x then
  951. begin
  952. hp:=genordinalconstnode(0,u8bitdef);
  953. disposetree(p);
  954. p:=hp;
  955. firstpass(p);
  956. end
  957. else
  958. begin
  959. if is_open_string(p^.left^.resulttype) then
  960. begin
  961. getsymonlyin(p^.left^.symtable,'high'+pvarsym(p^.left^.symtableentry)^.name);
  962. hp:=genloadnode(pvarsym(srsym),p^.left^.symtable);
  963. disposetree(p);
  964. p:=hp;
  965. firstpass(p);
  966. end
  967. else
  968. begin
  969. hp:=genordinalconstnode(Pstringdef(p^.left^.resulttype)^.len,u8bitdef);
  970. disposetree(p);
  971. p:=hp;
  972. firstpass(p);
  973. end;
  974. end;
  975. end;
  976. else
  977. CGMessage(type_e_mismatch);
  978. end;
  979. end
  980. else
  981. CGMessage(type_e_varid_or_typeid_expected);
  982. end;
  983. in_assert_x_y :
  984. begin
  985. p^.resulttype:=voiddef;
  986. if assigned(p^.left) then
  987. begin
  988. firstcallparan(p^.left,nil);
  989. p^.registers32:=p^.left^.registers32;
  990. p^.registersfpu:=p^.left^.registersfpu;
  991. {$ifdef SUPPORT_MMX}
  992. p^.registersmmx:=p^.left^.registersmmx;
  993. {$endif SUPPORT_MMX}
  994. { check type }
  995. if is_boolean(p^.left^.resulttype) then
  996. begin
  997. { must always be a string }
  998. p^.left^.right^.left:=gentypeconvnode(p^.left^.right^.left,cshortstringdef);
  999. firstpass(p^.left^.right^.left);
  1000. end
  1001. else
  1002. CGMessage(type_e_mismatch);
  1003. end
  1004. else
  1005. CGMessage(type_e_mismatch);
  1006. end;
  1007. else
  1008. internalerror(8);
  1009. end;
  1010. end;
  1011. { generate an error if no resulttype is set }
  1012. if not assigned(p^.resulttype) then
  1013. p^.resulttype:=generrordef;
  1014. must_be_valid:=store_valid;
  1015. count_ref:=store_count_ref;
  1016. end;
  1017. end.
  1018. {
  1019. $Log$
  1020. Revision 1.23 1999-04-08 10:16:48 peter
  1021. * funcret_valid flag is set for inline functions
  1022. Revision 1.22 1999/03/26 00:05:48 peter
  1023. * released valintern
  1024. + deffile is now removed when compiling is finished
  1025. * ^( compiles now correct
  1026. + static directive
  1027. * shrd fixed
  1028. Revision 1.21 1999/03/24 23:17:37 peter
  1029. * fixed bugs 212,222,225,227,229,231,233
  1030. Revision 1.20 1999/03/16 17:52:55 jonas
  1031. * changes for internal Val code (do a "make cycle OPT=-dvalintern" to test)
  1032. * in cgi386inl: also range checking for subrange types (compile with "-dreadrangecheck")
  1033. * in cgai386: also small fixes to emitrangecheck
  1034. Revision 1.19 1999/02/22 12:36:34 florian
  1035. + warning for lo/hi(longint/dword) in -So and -Sd mode added
  1036. Revision 1.18 1999/02/22 02:15:49 peter
  1037. * updates for ag386bin
  1038. Revision 1.17 1999/02/01 00:00:50 florian
  1039. * compiler crash fixed when constant arguments passed to round/trunc
  1040. exceeds the longint range
  1041. Revision 1.16 1999/01/28 19:43:43 peter
  1042. * fixed high generation for ansistrings with str,writeln
  1043. Revision 1.15 1999/01/27 16:28:22 pierre
  1044. * bug0157 solved : write(x:5.3) is rejected now
  1045. Revision 1.14 1999/01/21 22:10:50 peter
  1046. * fixed array of const
  1047. * generic platform independent high() support
  1048. Revision 1.13 1998/12/30 22:13:13 peter
  1049. * check the amount of paras for Str()
  1050. Revision 1.12 1998/12/15 10:23:31 peter
  1051. + -iSO, -iSP, -iTO, -iTP
  1052. Revision 1.11 1998/12/11 23:36:08 florian
  1053. + again more stuff for int64/qword:
  1054. - comparision operators
  1055. - code generation for: str, read(ln), write(ln)
  1056. Revision 1.10 1998/11/27 14:50:53 peter
  1057. + open strings, $P switch support
  1058. Revision 1.9 1998/11/24 17:04:28 peter
  1059. * fixed length(char) when char is a variable
  1060. Revision 1.8 1998/11/14 10:51:33 peter
  1061. * fixed low/high for record.field
  1062. Revision 1.7 1998/11/13 10:15:52 peter
  1063. * fixed ptr() with constants
  1064. Revision 1.6 1998/11/05 12:03:05 peter
  1065. * released useansistring
  1066. * removed -Sv, its now available in fpc modes
  1067. Revision 1.5 1998/10/20 11:16:47 pierre
  1068. + length(c) where C is a char is allways 1
  1069. Revision 1.4 1998/10/06 20:49:11 peter
  1070. * m68k compiler compiles again
  1071. Revision 1.3 1998/10/05 12:32:49 peter
  1072. + assert() support
  1073. Revision 1.2 1998/10/02 09:24:23 peter
  1074. * more constant expression evaluators
  1075. Revision 1.1 1998/09/23 20:42:24 peter
  1076. * splitted pass_1
  1077. }