tcinl.pas 54 KB

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