tcinl.pas 52 KB

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