tcinl.pas 53 KB

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