ninl.pas 56 KB

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