tcinl.pas 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  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,
  888. u8bit,s8bit,u16bit,s16bit,s64bitint,u64bit])))
  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. if is_64bitint(hpp^.left^.resulttype) then
  909. inc(p^.registers32,2)
  910. else
  911. inc(p^.registers32,1);
  912. end;
  913. {$EndIf OLDVAL}
  914. in_include_x_y,
  915. in_exclude_x_y:
  916. begin
  917. p^.resulttype:=voiddef;
  918. if assigned(p^.left) then
  919. begin
  920. firstcallparan(p^.left,nil);
  921. p^.registers32:=p^.left^.registers32;
  922. p^.registersfpu:=p^.left^.registersfpu;
  923. {$ifdef SUPPORT_MMX}
  924. p^.registersmmx:=p^.left^.registersmmx;
  925. {$endif SUPPORT_MMX}
  926. { remove warning when result is passed }
  927. if (p^.left^.left^.treetype=funcretn) then
  928. procinfo.funcret_is_valid:=true;
  929. { first param must be var }
  930. if (p^.left^.left^.location.loc<>LOC_REFERENCE) and
  931. (p^.left^.left^.location.loc<>LOC_CREGISTER) then
  932. CGMessage(cg_e_illegal_expression);
  933. { check type }
  934. if (p^.left^.resulttype^.deftype=setdef) then
  935. begin
  936. { two paras ? }
  937. if assigned(p^.left^.right) then
  938. begin
  939. { insert a type conversion }
  940. { to the type of the set elements }
  941. p^.left^.right^.left:=gentypeconvnode(
  942. p^.left^.right^.left,
  943. psetdef(p^.left^.resulttype)^.setof);
  944. { check the type conversion }
  945. firstpass(p^.left^.right^.left);
  946. { only three parameters are allowed }
  947. if assigned(p^.left^.right^.right) then
  948. CGMessage(cg_e_illegal_expression);
  949. end;
  950. end
  951. else
  952. CGMessage(type_e_mismatch);
  953. end
  954. else
  955. CGMessage(type_e_mismatch);
  956. end;
  957. in_low_x,
  958. in_high_x:
  959. begin
  960. if p^.left^.treetype in [typen,loadn,subscriptn] then
  961. begin
  962. case p^.left^.resulttype^.deftype of
  963. orddef,enumdef:
  964. begin
  965. do_lowhigh(p^.left^.resulttype);
  966. firstpass(p);
  967. end;
  968. setdef:
  969. begin
  970. do_lowhigh(Psetdef(p^.left^.resulttype)^.setof);
  971. firstpass(p);
  972. end;
  973. arraydef:
  974. begin
  975. if p^.inlinenumber=in_low_x then
  976. begin
  977. hp:=genordinalconstnode(Parraydef(p^.left^.resulttype)^.lowrange,
  978. Parraydef(p^.left^.resulttype)^.rangedef);
  979. disposetree(p);
  980. p:=hp;
  981. firstpass(p);
  982. end
  983. else
  984. begin
  985. if is_open_array(p^.left^.resulttype) or
  986. is_array_of_const(p^.left^.resulttype) then
  987. begin
  988. getsymonlyin(p^.left^.symtable,'high'+pvarsym(p^.left^.symtableentry)^.name);
  989. hp:=genloadnode(pvarsym(srsym),p^.left^.symtable);
  990. disposetree(p);
  991. p:=hp;
  992. firstpass(p);
  993. end
  994. else
  995. begin
  996. hp:=genordinalconstnode(Parraydef(p^.left^.resulttype)^.highrange,
  997. Parraydef(p^.left^.resulttype)^.rangedef);
  998. disposetree(p);
  999. p:=hp;
  1000. firstpass(p);
  1001. end;
  1002. end;
  1003. end;
  1004. stringdef:
  1005. begin
  1006. if p^.inlinenumber=in_low_x then
  1007. begin
  1008. hp:=genordinalconstnode(0,u8bitdef);
  1009. disposetree(p);
  1010. p:=hp;
  1011. firstpass(p);
  1012. end
  1013. else
  1014. begin
  1015. if is_open_string(p^.left^.resulttype) then
  1016. begin
  1017. getsymonlyin(p^.left^.symtable,'high'+pvarsym(p^.left^.symtableentry)^.name);
  1018. hp:=genloadnode(pvarsym(srsym),p^.left^.symtable);
  1019. disposetree(p);
  1020. p:=hp;
  1021. firstpass(p);
  1022. end
  1023. else
  1024. begin
  1025. hp:=genordinalconstnode(Pstringdef(p^.left^.resulttype)^.len,u8bitdef);
  1026. disposetree(p);
  1027. p:=hp;
  1028. firstpass(p);
  1029. end;
  1030. end;
  1031. end;
  1032. else
  1033. CGMessage(type_e_mismatch);
  1034. end;
  1035. end
  1036. else
  1037. CGMessage(type_e_varid_or_typeid_expected);
  1038. end;
  1039. in_assert_x_y :
  1040. begin
  1041. p^.resulttype:=voiddef;
  1042. if assigned(p^.left) then
  1043. begin
  1044. firstcallparan(p^.left,nil);
  1045. p^.registers32:=p^.left^.registers32;
  1046. p^.registersfpu:=p^.left^.registersfpu;
  1047. {$ifdef SUPPORT_MMX}
  1048. p^.registersmmx:=p^.left^.registersmmx;
  1049. {$endif SUPPORT_MMX}
  1050. { check type }
  1051. if is_boolean(p^.left^.resulttype) then
  1052. begin
  1053. { must always be a string }
  1054. p^.left^.right^.left:=gentypeconvnode(p^.left^.right^.left,cshortstringdef);
  1055. firstpass(p^.left^.right^.left);
  1056. end
  1057. else
  1058. CGMessage(type_e_mismatch);
  1059. end
  1060. else
  1061. CGMessage(type_e_mismatch);
  1062. end;
  1063. else
  1064. internalerror(8);
  1065. end;
  1066. end;
  1067. { generate an error if no resulttype is set }
  1068. if not assigned(p^.resulttype) then
  1069. p^.resulttype:=generrordef;
  1070. must_be_valid:=store_valid;
  1071. count_ref:=store_count_ref;
  1072. end;
  1073. end.
  1074. {
  1075. $Log$
  1076. Revision 1.39 1999-07-03 14:14:31 florian
  1077. + start of val(int64/qword)
  1078. * longbool, wordbool constants weren't written, fixed
  1079. Revision 1.38 1999/07/01 15:49:22 florian
  1080. * int64/qword type release
  1081. + lo/hi for int64/qword
  1082. Revision 1.37 1999/06/25 10:02:56 florian
  1083. * bug 459 fixed
  1084. Revision 1.36 1999/06/15 18:58:36 peter
  1085. * merged
  1086. Revision 1.35.2.1 1999/06/15 18:54:54 peter
  1087. * more procvar fixes
  1088. Revision 1.35 1999/05/27 19:45:19 peter
  1089. * removed oldasm
  1090. * plabel -> pasmlabel
  1091. * -a switches to source writing automaticly
  1092. * assembler readers OOPed
  1093. * asmsymbol automaticly external
  1094. * jumptables and other label fixes for asm readers
  1095. Revision 1.34 1999/05/23 18:42:20 florian
  1096. * better error recovering in typed constants
  1097. * some problems with arrays of const fixed, some problems
  1098. due my previous
  1099. - the location type of array constructor is now LOC_MEM
  1100. - the pushing of high fixed
  1101. - parameter copying fixed
  1102. - zero temp. allocation removed
  1103. * small problem in the assembler writers fixed:
  1104. ref to nil wasn't written correctly
  1105. Revision 1.33 1999/05/06 09:05:35 peter
  1106. * generic write_float and str_float
  1107. * fixed constant float conversions
  1108. Revision 1.32 1999/05/05 22:25:21 florian
  1109. * fixed register allocation for val
  1110. Revision 1.31 1999/05/02 21:33:57 florian
  1111. * several bugs regarding -Or fixed
  1112. Revision 1.30 1999/05/01 13:24:53 peter
  1113. * merged nasm compiler
  1114. * old asm moved to oldasm/
  1115. Revision 1.29 1999/04/28 06:02:15 florian
  1116. * changes of Bruessel:
  1117. + message handler can now take an explicit self
  1118. * typinfo fixed: sometimes the type names weren't written
  1119. * the type checking for pointer comparisations and subtraction
  1120. and are now more strict (was also buggy)
  1121. * small bug fix to link.pas to support compiling on another
  1122. drive
  1123. * probable bug in popt386 fixed: call/jmp => push/jmp
  1124. transformation didn't count correctly the jmp references
  1125. + threadvar support
  1126. * warning if ln/sqrt gets an invalid constant argument
  1127. Revision 1.28 1999/04/26 18:28:12 peter
  1128. * better read/write array
  1129. Revision 1.27 1999/04/26 09:32:22 peter
  1130. * try to convert to string for val()
  1131. Revision 1.26 1999/04/15 14:10:51 pierre
  1132. * fix for bug0238.pp
  1133. Revision 1.25 1999/04/15 10:00:35 peter
  1134. * writeln(procvar) support for tp7 mode
  1135. Revision 1.24 1999/04/14 09:15:07 peter
  1136. * first things to store the symbol/def number in the ppu
  1137. Revision 1.23 1999/04/08 10:16:48 peter
  1138. * funcret_valid flag is set for inline functions
  1139. Revision 1.22 1999/03/26 00:05:48 peter
  1140. * released valintern
  1141. + deffile is now removed when compiling is finished
  1142. * ^( compiles now correct
  1143. + static directive
  1144. * shrd fixed
  1145. Revision 1.21 1999/03/24 23:17:37 peter
  1146. * fixed bugs 212,222,225,227,229,231,233
  1147. Revision 1.20 1999/03/16 17:52:55 jonas
  1148. * changes for internal Val code (do a "make cycle OPT=-dvalintern" to test)
  1149. * in cgi386inl: also range checking for subrange types (compile with "-dreadrangecheck")
  1150. * in cgai386: also small fixes to emitrangecheck
  1151. Revision 1.19 1999/02/22 12:36:34 florian
  1152. + warning for lo/hi(longint/dword) in -So and -Sd mode added
  1153. Revision 1.18 1999/02/22 02:15:49 peter
  1154. * updates for ag386bin
  1155. Revision 1.17 1999/02/01 00:00:50 florian
  1156. * compiler crash fixed when constant arguments passed to round/trunc
  1157. exceeds the longint range
  1158. Revision 1.16 1999/01/28 19:43:43 peter
  1159. * fixed high generation for ansistrings with str,writeln
  1160. Revision 1.15 1999/01/27 16:28:22 pierre
  1161. * bug0157 solved : write(x:5.3) is rejected now
  1162. Revision 1.14 1999/01/21 22:10:50 peter
  1163. * fixed array of const
  1164. * generic platform independent high() support
  1165. Revision 1.13 1998/12/30 22:13:13 peter
  1166. * check the amount of paras for Str()
  1167. Revision 1.12 1998/12/15 10:23:31 peter
  1168. + -iSO, -iSP, -iTO, -iTP
  1169. Revision 1.11 1998/12/11 23:36:08 florian
  1170. + again more stuff for int64/qword:
  1171. - comparision operators
  1172. - code generation for: str, read(ln), write(ln)
  1173. Revision 1.10 1998/11/27 14:50:53 peter
  1174. + open strings, $P switch support
  1175. Revision 1.9 1998/11/24 17:04:28 peter
  1176. * fixed length(char) when char is a variable
  1177. Revision 1.8 1998/11/14 10:51:33 peter
  1178. * fixed low/high for record.field
  1179. Revision 1.7 1998/11/13 10:15:52 peter
  1180. * fixed ptr() with constants
  1181. Revision 1.6 1998/11/05 12:03:05 peter
  1182. * released useansistring
  1183. * removed -Sv, its now available in fpc modes
  1184. Revision 1.5 1998/10/20 11:16:47 pierre
  1185. + length(c) where C is a char is allways 1
  1186. Revision 1.4 1998/10/06 20:49:11 peter
  1187. * m68k compiler compiles again
  1188. Revision 1.3 1998/10/05 12:32:49 peter
  1189. + assert() support
  1190. Revision 1.2 1998/10/02 09:24:23 peter
  1191. * more constant expression evaluators
  1192. Revision 1.1 1998/09/23 20:42:24 peter
  1193. * splitted pass_1
  1194. }