ninl.pas 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868
  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,htypechk,cpuinfo;
  23. {$i compinnr.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. function det_resulttype:tnode;override;
  31. function docompare(p: tnode): boolean; override;
  32. end;
  33. var
  34. cinlinenode : class of tinlinenode;
  35. function geninlinenode(number : byte;is_const:boolean;l : tnode) : tinlinenode;
  36. implementation
  37. uses
  38. verbose,globals,systems,
  39. globtype,
  40. symconst,symtype,symdef,symsym,symtable,types,
  41. pass_1,
  42. ncal,ncon,ncnv,nadd,nld,nbas,
  43. cpubase,hcodegen,tgcpu
  44. {$ifdef newcg}
  45. ,cgbase
  46. {$endif newcg}
  47. ;
  48. function geninlinenode(number : byte;is_const:boolean;l : tnode) : tinlinenode;
  49. begin
  50. geninlinenode:=cinlinenode.create(number,is_const,l);
  51. end;
  52. {*****************************************************************************
  53. TINLINENODE
  54. *****************************************************************************}
  55. constructor tinlinenode.create(number : byte;is_const:boolean;l : tnode);
  56. begin
  57. inherited create(inlinen,l);
  58. if is_const then
  59. include(flags,nf_inlineconst);
  60. inlinenumber:=number;
  61. end;
  62. function tinlinenode.getcopy : tnode;
  63. var
  64. n : tinlinenode;
  65. begin
  66. n:=tinlinenode(inherited getcopy);
  67. n.inlinenumber:=inlinenumber;
  68. result:=n;
  69. end;
  70. function tinlinenode.det_resulttype:tnode;
  71. function do_lowhigh(const t:ttype) : tnode;
  72. var
  73. v : tconstexprint;
  74. enum : tenumsym;
  75. hp : tnode;
  76. begin
  77. case t.def.deftype of
  78. orddef:
  79. begin
  80. if inlinenumber=in_low_x then
  81. v:=torddef(t.def).low
  82. else
  83. v:=torddef(t.def).high;
  84. { low/high of torddef are longints, so we need special }
  85. { handling for cardinal and 64bit types (JM) }
  86. if is_signed(t.def) and
  87. is_64bitint(t.def) then
  88. if (inlinenumber=in_low_x) then
  89. v := int64($80000000) shl 32
  90. else
  91. v := (int64($7fffffff) shl 32) or $ffffffff
  92. else
  93. if is_64bitint(t.def) then
  94. { we have to use a dirty trick for high(qword), }
  95. { because it's bigger than high(tconstexprint) (JM) }
  96. v := 0
  97. else
  98. if not is_signed(t.def) then
  99. v := cardinal(v);
  100. hp:=cordconstnode.create(v,t);
  101. resulttypepass(hp);
  102. { fix high(qword) }
  103. if not is_signed(t.def) and
  104. is_64bitint(t.def) and
  105. (inlinenumber = in_high_x) then
  106. tordconstnode(hp).value := -1; { is the same as qword($ffffffffffffffff) }
  107. do_lowhigh:=hp;
  108. end;
  109. enumdef:
  110. begin
  111. enum:=tenumsym(tenumdef(t.def).firstenum);
  112. v:=tenumdef(t.def).maxval;
  113. if inlinenumber=in_high_x then
  114. while assigned(enum) and (enum.value <> v) do
  115. enum:=enum.nextenum;
  116. if not assigned(enum) then
  117. internalerror(309993)
  118. else
  119. hp:=genenumnode(enum);
  120. do_lowhigh:=hp;
  121. end;
  122. else
  123. internalerror(87);
  124. end;
  125. end;
  126. function getconstrealvalue : bestreal;
  127. begin
  128. case left.nodetype of
  129. ordconstn:
  130. getconstrealvalue:=tordconstnode(left).value;
  131. realconstn:
  132. getconstrealvalue:=trealconstnode(left).value_real;
  133. else
  134. internalerror(309992);
  135. end;
  136. end;
  137. procedure setconstrealvalue(r : bestreal);
  138. var
  139. hp : tnode;
  140. begin
  141. hp:=crealconstnode.create(r,pbestrealtype^);
  142. resulttypepass(hp);
  143. result:=hp;
  144. end;
  145. var
  146. counter : longint;
  147. ppn : tcallparanode;
  148. dummycoll : tparaitem;
  149. vl,vl2 : longint;
  150. vr : bestreal;
  151. hp : tnode;
  152. srsym : tsym;
  153. p1,hpp : tnode;
  154. frac_para,
  155. length_para : tnode;
  156. isreal,
  157. iswrite,
  158. file_is_typed : boolean;
  159. label
  160. myexit;
  161. begin
  162. result:=nil;
  163. { if we handle writeln; left contains no valid address }
  164. if assigned(left) then
  165. begin
  166. if left.nodetype=callparan then
  167. tcallparanode(left).get_paratype
  168. else
  169. resulttypepass(left);
  170. end;
  171. inc(parsing_para_level);
  172. { handle intern constant functions in separate case }
  173. if nf_inlineconst in flags then
  174. begin
  175. { no parameters? }
  176. if not assigned(left) then
  177. begin
  178. case inlinenumber of
  179. in_const_pi :
  180. hp:=crealconstnode.create(pi,pbestrealtype^);
  181. else
  182. internalerror(89);
  183. end;
  184. end
  185. else
  186. begin
  187. vl:=0;
  188. vl2:=0; { second parameter Ex: ptr(vl,vl2) }
  189. vr:=0;
  190. isreal:=false;
  191. case left.nodetype of
  192. realconstn :
  193. begin
  194. isreal:=true;
  195. vr:=trealconstnode(left).value_real;
  196. end;
  197. ordconstn :
  198. vl:=tordconstnode(left).value;
  199. callparan :
  200. begin
  201. { both exists, else it was not generated }
  202. vl:=tordconstnode(tcallparanode(left).left).value;
  203. vl2:=tordconstnode(tcallparanode(tcallparanode(left).right).left).value;
  204. end;
  205. else
  206. CGMessage(cg_e_illegal_expression);
  207. end;
  208. case inlinenumber of
  209. in_const_trunc :
  210. begin
  211. if isreal then
  212. begin
  213. if (vr>=2147483648.0) or (vr<=-2147483649.0) then
  214. begin
  215. CGMessage(parser_e_range_check_error);
  216. hp:=cordconstnode.create(1,s32bittype)
  217. end
  218. else
  219. hp:=cordconstnode.create(trunc(vr),s32bittype)
  220. end
  221. else
  222. hp:=cordconstnode.create(trunc(vl),s32bittype);
  223. end;
  224. in_const_round :
  225. begin
  226. if isreal then
  227. begin
  228. if (vr>=2147483647.5) or (vr<=-2147483648.5) then
  229. begin
  230. CGMessage(parser_e_range_check_error);
  231. hp:=cordconstnode.create(1,s32bittype)
  232. end
  233. else
  234. hp:=cordconstnode.create(round(vr),s32bittype)
  235. end
  236. else
  237. hp:=cordconstnode.create(round(vl),s32bittype);
  238. end;
  239. in_const_frac :
  240. begin
  241. if isreal then
  242. hp:=crealconstnode.create(frac(vr),pbestrealtype^)
  243. else
  244. hp:=crealconstnode.create(frac(vl),pbestrealtype^);
  245. end;
  246. in_const_int :
  247. begin
  248. if isreal then
  249. hp:=crealconstnode.create(int(vr),pbestrealtype^)
  250. else
  251. hp:=crealconstnode.create(int(vl),pbestrealtype^);
  252. end;
  253. in_const_abs :
  254. begin
  255. if isreal then
  256. hp:=crealconstnode.create(abs(vr),pbestrealtype^)
  257. else
  258. hp:=cordconstnode.create(abs(vl),left.resulttype);
  259. end;
  260. in_const_sqr :
  261. begin
  262. if isreal then
  263. hp:=crealconstnode.create(sqr(vr),pbestrealtype^)
  264. else
  265. hp:=cordconstnode.create(sqr(vl),left.resulttype);
  266. end;
  267. in_const_odd :
  268. begin
  269. if isreal then
  270. CGMessage1(type_e_integer_expr_expected,left.resulttype.def.typename)
  271. else
  272. hp:=cordconstnode.create(byte(odd(vl)),booltype);
  273. end;
  274. in_const_swap_word :
  275. begin
  276. if isreal then
  277. CGMessage1(type_e_integer_expr_expected,left.resulttype.def.typename)
  278. else
  279. hp:=cordconstnode.create((vl and $ff) shl 8+(vl shr 8),left.resulttype);
  280. end;
  281. in_const_swap_long :
  282. begin
  283. if isreal then
  284. CGMessage(type_e_mismatch)
  285. else
  286. hp:=cordconstnode.create((vl and $ffff) shl 16+(vl shr 16),left.resulttype);
  287. end;
  288. in_const_ptr :
  289. begin
  290. if isreal then
  291. CGMessage(type_e_mismatch)
  292. else
  293. hp:=cpointerconstnode.create((vl2 shl 4)+vl,voidfarpointertype);
  294. end;
  295. in_const_sqrt :
  296. begin
  297. if isreal then
  298. begin
  299. if vr<0.0 then
  300. CGMessage(type_e_wrong_math_argument)
  301. else
  302. hp:=crealconstnode.create(sqrt(vr),pbestrealtype^)
  303. end
  304. else
  305. begin
  306. if vl<0 then
  307. CGMessage(type_e_wrong_math_argument)
  308. else
  309. hp:=crealconstnode.create(sqrt(vl),pbestrealtype^);
  310. end;
  311. end;
  312. in_const_arctan :
  313. begin
  314. if isreal then
  315. hp:=crealconstnode.create(arctan(vr),pbestrealtype^)
  316. else
  317. hp:=crealconstnode.create(arctan(vl),pbestrealtype^);
  318. end;
  319. in_const_cos :
  320. begin
  321. if isreal then
  322. hp:=crealconstnode.create(cos(vr),pbestrealtype^)
  323. else
  324. hp:=crealconstnode.create(cos(vl),pbestrealtype^);
  325. end;
  326. in_const_sin :
  327. begin
  328. if isreal then
  329. hp:=crealconstnode.create(sin(vr),pbestrealtype^)
  330. else
  331. hp:=crealconstnode.create(sin(vl),pbestrealtype^);
  332. end;
  333. in_const_exp :
  334. begin
  335. if isreal then
  336. hp:=crealconstnode.create(exp(vr),pbestrealtype^)
  337. else
  338. hp:=crealconstnode.create(exp(vl),pbestrealtype^);
  339. end;
  340. in_const_ln :
  341. begin
  342. if isreal then
  343. begin
  344. if vr<=0.0 then
  345. CGMessage(type_e_wrong_math_argument)
  346. else
  347. hp:=crealconstnode.create(ln(vr),pbestrealtype^)
  348. end
  349. else
  350. begin
  351. if vl<=0 then
  352. CGMessage(type_e_wrong_math_argument)
  353. else
  354. hp:=crealconstnode.create(ln(vl),pbestrealtype^);
  355. end;
  356. end;
  357. else
  358. internalerror(88);
  359. end;
  360. end;
  361. if hp=nil then
  362. hp:=tnode.create(errorn);
  363. resulttypepass(hp);
  364. result:=hp;
  365. goto myexit;
  366. end
  367. else
  368. begin
  369. case inlinenumber of
  370. in_lo_long,
  371. in_hi_long,
  372. in_lo_qword,
  373. in_hi_qword,
  374. in_lo_word,
  375. in_hi_word :
  376. begin
  377. { give warning for incompatibility with tp and delphi }
  378. if (inlinenumber in [in_lo_long,in_hi_long,in_lo_qword,in_hi_qword]) and
  379. ((m_tp in aktmodeswitches) or
  380. (m_delphi in aktmodeswitches)) then
  381. CGMessage(type_w_maybe_wrong_hi_lo);
  382. { constant folding }
  383. if left.nodetype=ordconstn then
  384. begin
  385. case inlinenumber of
  386. in_lo_word :
  387. hp:=cordconstnode.create(tordconstnode(left).value and $ff,left.resulttype);
  388. in_hi_word :
  389. hp:=cordconstnode.create(tordconstnode(left).value shr 8,left.resulttype);
  390. in_lo_long :
  391. hp:=cordconstnode.create(tordconstnode(left).value and $ffff,left.resulttype);
  392. in_hi_long :
  393. hp:=cordconstnode.create(tordconstnode(left).value shr 16,left.resulttype);
  394. in_lo_qword :
  395. hp:=cordconstnode.create(tordconstnode(left).value and $ffffffff,left.resulttype);
  396. in_hi_qword :
  397. hp:=cordconstnode.create(tordconstnode(left).value shr 32,left.resulttype);
  398. end;
  399. resulttypepass(hp);
  400. result:=hp;
  401. goto myexit;
  402. end;
  403. set_varstate(left,true);
  404. if not is_integer(left.resulttype.def) then
  405. CGMessage(type_e_mismatch);
  406. case inlinenumber of
  407. in_lo_word,
  408. in_hi_word :
  409. resulttype:=u8bittype;
  410. in_lo_long,
  411. in_hi_long :
  412. resulttype:=u16bittype;
  413. in_lo_qword,
  414. in_hi_qword :
  415. resulttype:=u32bittype;
  416. end;
  417. end;
  418. in_sizeof_x:
  419. begin
  420. set_varstate(left,false);
  421. resulttype:=s32bittype;
  422. end;
  423. in_typeof_x:
  424. begin
  425. set_varstate(left,false);
  426. resulttype:=voidpointertype;
  427. end;
  428. in_ord_x:
  429. begin
  430. if (left.nodetype=ordconstn) then
  431. begin
  432. hp:=cordconstnode.create(tordconstnode(left).value,s32bittype);
  433. resulttypepass(hp);
  434. result:=hp;
  435. goto myexit;
  436. end;
  437. set_varstate(left,true);
  438. case left.resulttype.def.deftype of
  439. orddef :
  440. begin
  441. case torddef(left.resulttype.def).typ of
  442. bool8bit,
  443. uchar:
  444. begin
  445. { change to byte() }
  446. hp:=ctypeconvnode.create(left,u8bittype);
  447. left:=nil;
  448. include(hp.flags,nf_explizit);
  449. resulttypepass(hp);
  450. result:=hp;
  451. end;
  452. bool16bit,
  453. uwidechar :
  454. begin
  455. { change to word() }
  456. hp:=ctypeconvnode.create(left,u16bittype);
  457. left:=nil;
  458. include(hp.flags,nf_explizit);
  459. resulttypepass(hp);
  460. result:=hp;
  461. end;
  462. bool32bit :
  463. begin
  464. { change to dword() }
  465. hp:=ctypeconvnode.create(left,u32bittype);
  466. left:=nil;
  467. include(hp.flags,nf_explizit);
  468. resulttypepass(hp);
  469. result:=hp;
  470. end;
  471. uvoid :
  472. CGMessage(type_e_mismatch)
  473. else
  474. begin
  475. { all other orddef need no transformation }
  476. hp:=left;
  477. left:=nil;
  478. result:=hp;
  479. end;
  480. end;
  481. end;
  482. enumdef :
  483. begin
  484. hp:=ctypeconvnode.create(left,s32bittype);
  485. left:=nil;
  486. include(hp.flags,nf_explizit);
  487. resulttypepass(hp);
  488. result:=hp;
  489. end;
  490. else
  491. CGMessage(type_e_mismatch);
  492. end;
  493. end;
  494. in_chr_byte:
  495. begin
  496. { convert to explicit char() }
  497. set_varstate(left,true);
  498. hp:=ctypeconvnode.create(left,cchartype);
  499. include(hp.flags,nf_explizit);
  500. left:=nil;
  501. resulttypepass(hp);
  502. result:=hp;
  503. end;
  504. in_length_string:
  505. begin
  506. set_varstate(left,true);
  507. { we don't need string convertions here }
  508. if (left.nodetype=typeconvn) and
  509. (ttypeconvnode(left).left.resulttype.def.deftype=stringdef) then
  510. begin
  511. hp:=ttypeconvnode(left).left;
  512. ttypeconvnode(left).left:=nil;
  513. left.free;
  514. left:=hp;
  515. end;
  516. { evaluates length of constant strings direct }
  517. if (left.nodetype=stringconstn) then
  518. begin
  519. hp:=cordconstnode.create(tstringconstnode(left).len,s32bittype);
  520. resulttypepass(hp);
  521. result:=hp;
  522. goto myexit;
  523. end
  524. { length of char is one allways }
  525. else if is_constcharnode(left) then
  526. begin
  527. hp:=cordconstnode.create(1,s32bittype);
  528. resulttypepass(hp);
  529. result:=hp;
  530. goto myexit;
  531. end;
  532. if is_ansistring(left.resulttype.def) or
  533. is_widestring(left.resulttype.def) or
  534. is_dynamic_array(left.resulttype.def) then
  535. resulttype:=s32bittype
  536. else
  537. resulttype:=u8bittype;
  538. { check the type, must be string or char }
  539. if (left.resulttype.def.deftype<>stringdef) and
  540. (not is_char(left.resulttype.def)) then
  541. CGMessage(type_e_mismatch);
  542. end;
  543. in_typeinfo_x:
  544. begin
  545. set_varstate(left,true);
  546. resulttype:=voidpointertype;
  547. end;
  548. in_assigned_x:
  549. begin
  550. set_varstate(left,true);
  551. resulttype:=booltype;
  552. end;
  553. in_ofs_x :
  554. internalerror(2000101001);
  555. in_seg_x :
  556. begin
  557. set_varstate(left,false);
  558. hp:=cordconstnode.create(0,s32bittype);
  559. resulttypepass(hp);
  560. result:=hp;
  561. goto myexit;
  562. end;
  563. in_pred_x,
  564. in_succ_x:
  565. begin
  566. set_varstate(left,true);
  567. resulttype:=left.resulttype;
  568. if not is_ordinal(resulttype.def) then
  569. CGMessage(type_e_ordinal_expr_expected)
  570. else
  571. begin
  572. if (resulttype.def.deftype=enumdef) and
  573. (tenumdef(resulttype.def).has_jumps) then
  574. CGMessage(type_e_succ_and_pred_enums_with_assign_not_possible);
  575. end;
  576. { do constant folding after check for jumps }
  577. if left.nodetype=ordconstn then
  578. begin
  579. if inlinenumber=in_succ_x then
  580. hp:=cordconstnode.create(tordconstnode(left).value+1,left.resulttype)
  581. else
  582. hp:=cordconstnode.create(tordconstnode(left).value-1,left.resulttype);
  583. resulttypepass(hp);
  584. result:=hp;
  585. end;
  586. end;
  587. in_setlength_x:
  588. begin
  589. resulttype:=voidtype;
  590. if assigned(left) then
  591. begin
  592. ppn:=tcallparanode(left);
  593. counter:=0;
  594. { check type }
  595. while assigned(ppn.right) do
  596. begin
  597. set_varstate(ppn.left,true);
  598. inserttypeconv(ppn.left,s32bittype);
  599. inc(counter);
  600. ppn:=tcallparanode(ppn.right);
  601. end;
  602. { last param must be var }
  603. valid_for_assign(ppn.left,false);
  604. set_varstate(ppn.left,false);
  605. { first param must be a string or dynamic array ...}
  606. if not((ppn.left.resulttype.def.deftype=stringdef) or
  607. (is_dynamic_array(ppn.left.resulttype.def))) then
  608. CGMessage(type_e_mismatch);
  609. { only dynamic arrays accept more dimensions }
  610. if (counter>1) and
  611. (not(is_dynamic_array(left.resulttype.def))) then
  612. CGMessage(type_e_mismatch);
  613. { convert shortstrings to openstring parameters }
  614. { (generate the hightree) (JM) }
  615. if (ppn.left.resulttype.def.deftype = stringdef) and
  616. (tstringdef(ppn.left.resulttype.def).string_typ =
  617. st_shortstring) then
  618. begin
  619. dummycoll:=tparaitem.create;
  620. dummycoll.paratyp:=vs_var;
  621. dummycoll.paratype:=openshortstringtype;
  622. tcallparanode(ppn).insert_typeconv(dummycoll,false);
  623. dummycoll.destroy;
  624. end;
  625. end
  626. else
  627. CGMessage(type_e_mismatch);
  628. end;
  629. in_finalize_x:
  630. begin
  631. resulttype:=voidtype;
  632. if assigned(left) and assigned(tcallparanode(left).left) then
  633. begin
  634. { first param must be var }
  635. valid_for_assign(tcallparanode(left).left,false);
  636. set_varstate(tcallparanode(left).left,true);
  637. { two parameters?, the last parameter must be a longint }
  638. if assigned(tcallparanode(left).right) then
  639. inserttypeconv(tcallparanode(tcallparanode(left).right).left,s32bittype);
  640. end
  641. else
  642. CGMessage(type_e_mismatch);
  643. end;
  644. in_inc_x,
  645. in_dec_x:
  646. begin
  647. resulttype:=voidtype;
  648. if assigned(left) then
  649. begin
  650. set_varstate(left,true);
  651. if codegenerror then
  652. exit;
  653. { first param must be var }
  654. valid_for_assign(tcallparanode(left).left,false);
  655. if (left.resulttype.def.deftype in [enumdef,pointerdef]) or
  656. is_ordinal(left.resulttype.def) then
  657. begin
  658. { two paras ? }
  659. if assigned(tcallparanode(left).right) then
  660. begin
  661. { insert a type conversion }
  662. { the second param is always longint }
  663. inserttypeconv(tcallparanode(tcallparanode(left).right).left,s32bittype);
  664. if assigned(tcallparanode(tcallparanode(left).right).right) then
  665. CGMessage(cg_e_illegal_expression);
  666. end;
  667. end
  668. else
  669. CGMessage(type_e_ordinal_expr_expected);
  670. end
  671. else
  672. CGMessage(type_e_mismatch);
  673. end;
  674. in_read_x,
  675. in_readln_x,
  676. in_write_x,
  677. in_writeln_x :
  678. begin
  679. resulttype:=voidtype;
  680. { we must know if it is a typed file or not }
  681. { but we must first do the firstpass for it }
  682. file_is_typed:=false;
  683. if assigned(left) then
  684. begin
  685. iswrite:=(inlinenumber in [in_write_x,in_writeln_x]);
  686. set_varstate(left,iswrite);
  687. { now we can check }
  688. hp:=left;
  689. while assigned(tcallparanode(hp).right) do
  690. hp:=tcallparanode(hp).right;
  691. { if resulttype.def is not assigned, then automatically }
  692. { file is not typed. }
  693. if assigned(hp) and assigned(hp.resulttype.def) then
  694. Begin
  695. if (hp.resulttype.def.deftype=filedef) then
  696. if (tfiledef(hp.resulttype.def).filetyp=ft_untyped) then
  697. begin
  698. if (inlinenumber in [in_readln_x,in_writeln_x]) then
  699. CGMessage(type_e_no_readln_writeln_for_typed_file)
  700. else
  701. CGMessage(type_e_no_read_write_for_untyped_file);
  702. end
  703. else if (tfiledef(hp.resulttype.def).filetyp=ft_typed) then
  704. begin
  705. file_is_typed:=true;
  706. { test the type }
  707. if (inlinenumber in [in_readln_x,in_writeln_x]) then
  708. CGMessage(type_e_no_readln_writeln_for_typed_file);
  709. hpp:=left;
  710. while (hpp<>hp) do
  711. begin
  712. if (tcallparanode(hpp).left.nodetype=typen) then
  713. CGMessage(type_e_cant_read_write_type);
  714. if not is_equal(hpp.resulttype.def,tfiledef(hp.resulttype.def).typedfiletype.def) then
  715. CGMessage(type_e_mismatch);
  716. { generate the high() value for the shortstring }
  717. if ((not iswrite) and is_shortstring(tcallparanode(hpp).left.resulttype.def)) or
  718. (is_chararray(tcallparanode(hpp).left.resulttype.def)) then
  719. tcallparanode(hpp).gen_high_tree(true);
  720. { read(ln) is call by reference (JM) }
  721. if not iswrite then
  722. make_not_regable(tcallparanode(hpp).left);
  723. hpp:=tcallparanode(hpp).right;
  724. end;
  725. end;
  726. end; { endif assigned(hp) }
  727. { insert type conversions for write(ln) }
  728. if (not file_is_typed) then
  729. begin
  730. hp:=left;
  731. while assigned(hp) do
  732. begin
  733. if (tcallparanode(hp).left.nodetype=typen) then
  734. CGMessage(type_e_cant_read_write_type);
  735. if assigned(tcallparanode(hp).left.resulttype.def) then
  736. begin
  737. isreal:=false;
  738. { support writeln(procvar) }
  739. if (tcallparanode(hp).left.resulttype.def.deftype=procvardef) then
  740. begin
  741. p1:=ccallnode.create(nil,nil,nil,nil);
  742. tcallnode(p1).set_procvar(tcallparanode(hp).left);
  743. resulttypepass(p1);
  744. tcallparanode(hp).left:=p1;
  745. end;
  746. case tcallparanode(hp).left.resulttype.def.deftype of
  747. filedef :
  748. begin
  749. { only allowed as first parameter }
  750. if assigned(tcallparanode(hp).right) then
  751. CGMessage(type_e_cant_read_write_type);
  752. end;
  753. stringdef :
  754. begin
  755. { generate the high() value for the shortstring }
  756. if (not iswrite) and
  757. is_shortstring(tcallparanode(hp).left.resulttype.def) then
  758. tcallparanode(hp).gen_high_tree(true);
  759. end;
  760. pointerdef :
  761. begin
  762. if not is_pchar(tcallparanode(hp).left.resulttype.def) then
  763. CGMessage(type_e_cant_read_write_type);
  764. end;
  765. floatdef :
  766. begin
  767. isreal:=true;
  768. end;
  769. orddef :
  770. begin
  771. case torddef(tcallparanode(hp).left.resulttype.def).typ of
  772. uchar,
  773. u32bit,s32bit,
  774. u64bit,s64bit:
  775. ;
  776. u8bit,s8bit,
  777. u16bit,s16bit :
  778. if iswrite then
  779. inserttypeconv(tcallparanode(hp).left,s32bittype);
  780. bool8bit,
  781. bool16bit,
  782. bool32bit :
  783. if iswrite then
  784. inserttypeconv(tcallparanode(hp).left,booltype)
  785. else
  786. CGMessage(type_e_cant_read_write_type);
  787. else
  788. CGMessage(type_e_cant_read_write_type);
  789. end;
  790. end;
  791. arraydef :
  792. begin
  793. if is_chararray(tcallparanode(hp).left.resulttype.def) then
  794. tcallparanode(hp).gen_high_tree(true)
  795. else
  796. CGMessage(type_e_cant_read_write_type);
  797. end;
  798. else
  799. CGMessage(type_e_cant_read_write_type);
  800. end;
  801. { some format options ? }
  802. if cpf_is_colon_para in tcallparanode(hp).callparaflags then
  803. begin
  804. if cpf_is_colon_para in tcallparanode(tcallparanode(hp).right).callparaflags then
  805. begin
  806. frac_para:=hp;
  807. length_para:=tcallparanode(hp).right;
  808. hp:=tcallparanode(hp).right;
  809. hpp:=tcallparanode(hp).right;
  810. end
  811. else
  812. begin
  813. length_para:=hp;
  814. frac_para:=nil;
  815. hpp:=tcallparanode(hp).right;
  816. end;
  817. { can be nil if you use "write(e:0:6)" while e is undeclared (JM) }
  818. if assigned(tcallparanode(hpp).left.resulttype.def) then
  819. isreal:=(tcallparanode(hpp).left.resulttype.def.deftype=floatdef)
  820. else
  821. exit;
  822. if (not is_integer(tcallparanode(length_para).left.resulttype.def)) then
  823. CGMessage1(type_e_integer_expr_expected,tcallparanode(length_para).left.resulttype.def.typename)
  824. else
  825. inserttypeconv(tcallparanode(length_para).left,s32bittype);
  826. if assigned(frac_para) then
  827. begin
  828. if isreal then
  829. begin
  830. if (not is_integer(tcallparanode(frac_para).left.resulttype.def)) then
  831. CGMessage1(type_e_integer_expr_expected,tcallparanode(frac_para).left.resulttype.def.typename)
  832. else
  833. inserttypeconv(tcallparanode(frac_para).left,s32bittype);
  834. end
  835. else
  836. CGMessage(parser_e_illegal_colon_qualifier);
  837. end;
  838. { do the checking for the colon'd arg }
  839. hp:=length_para;
  840. end;
  841. end;
  842. hp:=tcallparanode(hp).right;
  843. end;
  844. end;
  845. if codegenerror then
  846. exit;
  847. set_varstate(left,true);
  848. end;
  849. end;
  850. in_settextbuf_file_x :
  851. begin
  852. resulttype:=voidtype;
  853. { now we know the type of buffer }
  854. srsym:=searchsymonlyin(systemunit,'SETTEXTBUF');
  855. hp:=ccallparanode.create(cordconstnode.create(tcallparanode(left).left.resulttype.def.size,s32bittype),left);
  856. hp:=ccallnode.create(hp,tprocsym(srsym),systemunit,nil);
  857. left:=nil;
  858. resulttypepass(hp);
  859. result:=hp;
  860. end;
  861. { the firstpass of the arg has been done in firstcalln ? }
  862. in_reset_typedfile,
  863. in_rewrite_typedfile :
  864. begin
  865. set_varstate(left,true);
  866. resulttype:=voidtype;
  867. end;
  868. in_str_x_string :
  869. begin
  870. resulttype:=voidtype;
  871. set_varstate(left,false);
  872. { remove warning when result is passed }
  873. set_funcret_is_valid(tcallparanode(left).left);
  874. set_varstate(tcallparanode(tcallparanode(left).right).left,true);
  875. hp:=left;
  876. { valid string ? }
  877. if not assigned(hp) or
  878. (tcallparanode(hp).left.resulttype.def.deftype<>stringdef) or
  879. (tcallparanode(hp).right=nil) then
  880. CGMessage(cg_e_illegal_expression);
  881. { we need a var parameter }
  882. valid_for_assign(tcallparanode(hp).left,false);
  883. { generate the high() value for the shortstring }
  884. if is_shortstring(tcallparanode(hp).left.resulttype.def) then
  885. tcallparanode(hp).gen_high_tree(true);
  886. { !!!! check length of string }
  887. while assigned(tcallparanode(hp).right) do
  888. hp:=tcallparanode(hp).right;
  889. if not assigned(tcallparanode(hp).resulttype.def) then
  890. exit;
  891. { check and convert the first param }
  892. if (cpf_is_colon_para in tcallparanode(hp).callparaflags) or
  893. not assigned(hp.resulttype.def) then
  894. CGMessage(cg_e_illegal_expression);
  895. isreal:=false;
  896. case hp.resulttype.def.deftype of
  897. orddef :
  898. begin
  899. case torddef(tcallparanode(hp).left.resulttype.def).typ of
  900. u32bit,s32bit,
  901. s64bit,u64bit:
  902. ;
  903. u8bit,s8bit,
  904. u16bit,s16bit:
  905. inserttypeconv(tcallparanode(hp).left,s32bittype);
  906. else
  907. CGMessage(type_e_integer_or_real_expr_expected);
  908. end;
  909. end;
  910. floatdef :
  911. begin
  912. isreal:=true;
  913. end;
  914. else
  915. CGMessage(type_e_integer_or_real_expr_expected);
  916. end;
  917. { some format options ? }
  918. hpp:=tcallparanode(left).right;
  919. if assigned(hpp) and (cpf_is_colon_para in tcallparanode(hpp).callparaflags) then
  920. begin
  921. set_varstate(tcallparanode(hpp).left,true);
  922. if (not is_integer(tcallparanode(hpp).left.resulttype.def)) then
  923. CGMessage1(type_e_integer_expr_expected,tcallparanode(hpp).left.resulttype.def.typename)
  924. else
  925. inserttypeconv(tcallparanode(hpp).left,s32bittype);
  926. hpp:=tcallparanode(hpp).right;
  927. if assigned(hpp) and (cpf_is_colon_para in tcallparanode(hpp).callparaflags) then
  928. begin
  929. if isreal then
  930. begin
  931. if (not is_integer(tcallparanode(hpp).left.resulttype.def)) then
  932. CGMessage1(type_e_integer_expr_expected,tcallparanode(hpp).left.resulttype.def.typename)
  933. else
  934. begin
  935. set_varstate(tcallparanode(hpp).left,true);
  936. inserttypeconv(tcallparanode(hpp).left,s32bittype);
  937. end;
  938. end
  939. else
  940. CGMessage(parser_e_illegal_colon_qualifier);
  941. end;
  942. end;
  943. end;
  944. in_val_x :
  945. begin
  946. resulttype:=voidtype;
  947. { check the amount of parameters }
  948. if not(assigned(left)) or
  949. not(assigned(tcallparanode(left).right)) then
  950. begin
  951. CGMessage(parser_e_wrong_parameter_size);
  952. exit;
  953. end;
  954. { there is a "code" parameter }
  955. If Assigned(tcallparanode(tcallparanode(left).right).right) Then
  956. Begin
  957. { first pass just the code parameter for first local use}
  958. hp := tcallparanode(left).right;
  959. tcallparanode(left).right := nil;
  960. make_not_regable(tcallparanode(left).left);
  961. set_varstate(left,false);
  962. if codegenerror then
  963. exit;
  964. tcallparanode(left).right := hp;
  965. { code has to be a var parameter }
  966. if valid_for_assign(tcallparanode(left).left,false) then
  967. begin
  968. if (tcallparanode(left).left.resulttype.def.deftype <> orddef) or
  969. not(torddef(tcallparanode(left).left.resulttype.def).typ in [u16bit,s16bit,u32bit,s32bit]) then
  970. CGMessage(type_e_mismatch);
  971. end;
  972. hpp := tcallparanode(left).right
  973. End
  974. Else
  975. hpp := left;
  976. { now hpp = the destination value tree }
  977. { first pass just the destination parameter for first local use }
  978. hp:=tcallparanode(hpp).right;
  979. tcallparanode(hpp).right:=nil;
  980. { hpp = destination }
  981. make_not_regable(tcallparanode(hpp).left);
  982. set_varstate(hpp,false);
  983. if codegenerror then
  984. exit;
  985. { remove warning when result is passed }
  986. set_funcret_is_valid(tcallparanode(hpp).left);
  987. tcallparanode(hpp).right := hp;
  988. if valid_for_assign(tcallparanode(hpp).left,false) then
  989. begin
  990. If Not((tcallparanode(hpp).left.resulttype.def.deftype = floatdef) or
  991. is_integer(tcallparanode(hpp).left.resulttype.def)) then
  992. CGMessage(type_e_mismatch);
  993. end;
  994. { hp = source (String) }
  995. { if not a stringdef then insert a type conv which
  996. does the other type checking }
  997. If (tcallparanode(hp).left.resulttype.def.deftype<>stringdef) then
  998. inserttypeconv(tcallparanode(hp).left,cshortstringtype);
  999. set_varstate(hp,true);
  1000. end;
  1001. in_include_x_y,
  1002. in_exclude_x_y:
  1003. begin
  1004. resulttype:=voidtype;
  1005. if assigned(left) then
  1006. begin
  1007. set_varstate(left,true);
  1008. { remove warning when result is passed }
  1009. set_funcret_is_valid(tcallparanode(left).left);
  1010. { first param must be var }
  1011. valid_for_assign(tcallparanode(left).left,false);
  1012. { check type }
  1013. if assigned(left.resulttype.def) and
  1014. (left.resulttype.def.deftype=setdef) then
  1015. begin
  1016. { two paras ? }
  1017. if assigned(tcallparanode(left).right) then
  1018. begin
  1019. { insert a type conversion }
  1020. { to the type of the set elements }
  1021. inserttypeconv(tcallparanode(tcallparanode(left).right).left,
  1022. tsetdef(left.resulttype.def).elementtype);
  1023. { only three parameters are allowed }
  1024. if assigned(tcallparanode(tcallparanode(left).right).right) then
  1025. CGMessage(cg_e_illegal_expression);
  1026. end;
  1027. end
  1028. else
  1029. CGMessage(type_e_mismatch);
  1030. end
  1031. else
  1032. CGMessage(type_e_mismatch);
  1033. end;
  1034. in_low_x,
  1035. in_high_x:
  1036. begin
  1037. set_varstate(left,false);
  1038. case left.resulttype.def.deftype of
  1039. orddef,
  1040. enumdef:
  1041. begin
  1042. hp:=do_lowhigh(left.resulttype);
  1043. resulttypepass(hp);
  1044. result:=hp;
  1045. end;
  1046. setdef:
  1047. begin
  1048. hp:=do_lowhigh(tsetdef(left.resulttype.def).elementtype);
  1049. resulttypepass(hp);
  1050. result:=hp;
  1051. end;
  1052. arraydef:
  1053. begin
  1054. if inlinenumber=in_low_x then
  1055. begin
  1056. hp:=cordconstnode.create(tarraydef(left.resulttype.def).lowrange,tarraydef(left.resulttype.def).rangetype);
  1057. resulttypepass(hp);
  1058. result:=hp;
  1059. end
  1060. else
  1061. begin
  1062. if is_open_array(left.resulttype.def) or
  1063. is_array_of_const(left.resulttype.def) then
  1064. begin
  1065. srsym:=searchsymonlyin(tloadnode(left).symtable,'high'+tvarsym(tloadnode(left).symtableentry).name);
  1066. hp:=cloadnode.create(tvarsym(srsym),tloadnode(left).symtable);
  1067. resulttypepass(hp);
  1068. result:=hp;
  1069. end
  1070. else
  1071. begin
  1072. hp:=cordconstnode.create(tarraydef(left.resulttype.def).highrange,tarraydef(left.resulttype.def).rangetype);
  1073. resulttypepass(hp);
  1074. result:=hp;
  1075. end;
  1076. end;
  1077. end;
  1078. stringdef:
  1079. begin
  1080. if inlinenumber=in_low_x then
  1081. begin
  1082. hp:=cordconstnode.create(0,u8bittype);
  1083. resulttypepass(hp);
  1084. result:=hp;
  1085. end
  1086. else
  1087. begin
  1088. if is_open_string(left.resulttype.def) then
  1089. begin
  1090. srsym:=searchsymonlyin(tloadnode(left).symtable,'high'+tvarsym(tloadnode(left).symtableentry).name);
  1091. hp:=cloadnode.create(tvarsym(srsym),tloadnode(left).symtable);
  1092. resulttypepass(hp);
  1093. result:=hp;
  1094. end
  1095. else
  1096. begin
  1097. hp:=cordconstnode.create(tstringdef(left.resulttype.def).len,u8bittype);
  1098. resulttypepass(hp);
  1099. result:=hp;
  1100. end;
  1101. end;
  1102. end;
  1103. else
  1104. CGMessage(type_e_mismatch);
  1105. end;
  1106. end;
  1107. in_pi:
  1108. begin
  1109. if block_type=bt_const then
  1110. setconstrealvalue(pi)
  1111. else
  1112. resulttype:=s80floattype;
  1113. end;
  1114. in_cos_extended :
  1115. begin
  1116. if left.nodetype in [ordconstn,realconstn] then
  1117. setconstrealvalue(cos(getconstrealvalue))
  1118. else
  1119. begin
  1120. set_varstate(left,true);
  1121. inserttypeconv(left,s80floattype);
  1122. resulttype:=s80floattype;
  1123. end;
  1124. end;
  1125. in_sin_extended :
  1126. begin
  1127. if left.nodetype in [ordconstn,realconstn] then
  1128. setconstrealvalue(sin(getconstrealvalue))
  1129. else
  1130. begin
  1131. set_varstate(left,true);
  1132. inserttypeconv(left,s80floattype);
  1133. resulttype:=s80floattype;
  1134. end;
  1135. end;
  1136. in_arctan_extended :
  1137. begin
  1138. if left.nodetype in [ordconstn,realconstn] then
  1139. setconstrealvalue(arctan(getconstrealvalue))
  1140. else
  1141. begin
  1142. set_varstate(left,true);
  1143. inserttypeconv(left,s80floattype);
  1144. resulttype:=s80floattype;
  1145. end;
  1146. end;
  1147. in_abs_extended :
  1148. begin
  1149. if left.nodetype in [ordconstn,realconstn] then
  1150. setconstrealvalue(abs(getconstrealvalue))
  1151. else
  1152. begin
  1153. set_varstate(left,true);
  1154. inserttypeconv(left,s80floattype);
  1155. resulttype:=s80floattype;
  1156. end;
  1157. end;
  1158. in_sqr_extended :
  1159. begin
  1160. if left.nodetype in [ordconstn,realconstn] then
  1161. setconstrealvalue(sqr(getconstrealvalue))
  1162. else
  1163. begin
  1164. set_varstate(left,true);
  1165. inserttypeconv(left,s80floattype);
  1166. resulttype:=s80floattype;
  1167. end;
  1168. end;
  1169. in_sqrt_extended :
  1170. begin
  1171. if left.nodetype in [ordconstn,realconstn] then
  1172. begin
  1173. vr:=getconstrealvalue;
  1174. if vr<0.0 then
  1175. begin
  1176. CGMessage(type_e_wrong_math_argument);
  1177. setconstrealvalue(0);
  1178. end
  1179. else
  1180. setconstrealvalue(sqrt(vr));
  1181. end
  1182. else
  1183. begin
  1184. set_varstate(left,true);
  1185. inserttypeconv(left,s80floattype);
  1186. resulttype:=s80floattype;
  1187. end;
  1188. end;
  1189. in_ln_extended :
  1190. begin
  1191. if left.nodetype in [ordconstn,realconstn] then
  1192. begin
  1193. vr:=getconstrealvalue;
  1194. if vr<=0.0 then
  1195. begin
  1196. CGMessage(type_e_wrong_math_argument);
  1197. setconstrealvalue(0);
  1198. end
  1199. else
  1200. setconstrealvalue(ln(vr));
  1201. end
  1202. else
  1203. begin
  1204. set_varstate(left,true);
  1205. inserttypeconv(left,s80floattype);
  1206. resulttype:=s80floattype;
  1207. end;
  1208. end;
  1209. {$ifdef SUPPORT_MMX}
  1210. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  1211. begin
  1212. end;
  1213. {$endif SUPPORT_MMX}
  1214. in_assert_x_y :
  1215. begin
  1216. resulttype:=voidtype;
  1217. if assigned(left) then
  1218. begin
  1219. set_varstate(left,true);
  1220. { check type }
  1221. if is_boolean(left.resulttype.def) then
  1222. begin
  1223. { must always be a string }
  1224. inserttypeconv(tcallparanode(tcallparanode(left).right).left,cshortstringtype);
  1225. end
  1226. else
  1227. CGMessage(type_e_mismatch);
  1228. end
  1229. else
  1230. CGMessage(type_e_mismatch);
  1231. end;
  1232. else
  1233. internalerror(8);
  1234. end;
  1235. end;
  1236. myexit:
  1237. { Run get_paratype again to update maybe inserted typeconvs }
  1238. if not codegenerror then
  1239. begin
  1240. if assigned(left) and
  1241. (left.nodetype=callparan) then
  1242. tcallparanode(left).get_paratype;
  1243. end;
  1244. dec(parsing_para_level);
  1245. end;
  1246. {$ifdef fpc}
  1247. {$maxfpuregisters 0}
  1248. {$endif fpc}
  1249. function tinlinenode.pass_1 : tnode;
  1250. var
  1251. srsym : tsym;
  1252. hp,hpp : tnode;
  1253. extra_register,
  1254. iswrite,
  1255. file_is_typed : boolean;
  1256. begin
  1257. result:=nil;
  1258. { if we handle writeln; left contains no valid address }
  1259. if assigned(left) then
  1260. begin
  1261. if left.nodetype=callparan then
  1262. tcallparanode(left).firstcallparan(nil,false)
  1263. else
  1264. firstpass(left);
  1265. left_max;
  1266. set_location(location,left.location);
  1267. end;
  1268. inc(parsing_para_level);
  1269. { intern const should already be handled }
  1270. if nf_inlineconst in flags then
  1271. internalerror(200104044);
  1272. case inlinenumber of
  1273. in_lo_qword,
  1274. in_hi_qword,
  1275. in_lo_long,
  1276. in_hi_long,
  1277. in_lo_word,
  1278. in_hi_word:
  1279. begin
  1280. if registers32<1 then
  1281. registers32:=1;
  1282. location.loc:=LOC_REGISTER;
  1283. end;
  1284. in_sizeof_x:
  1285. begin
  1286. if push_high_param(left.resulttype.def) then
  1287. begin
  1288. srsym:=searchsymonlyin(tloadnode(left).symtable,'high'+tvarsym(tloadnode(left).symtableentry).name);
  1289. hp:=caddnode.create(addn,cloadnode.create(tvarsym(srsym),tloadnode(left).symtable),
  1290. cordconstnode.create(1,s32bittype));
  1291. if (left.resulttype.def.deftype=arraydef) and
  1292. (tarraydef(left.resulttype.def).elesize<>1) then
  1293. hp:=caddnode.create(muln,hp,cordconstnode.create(tarraydef(left.resulttype.def).elesize,s32bittype));
  1294. firstpass(hp);
  1295. result:=hp;
  1296. end
  1297. else
  1298. begin
  1299. if registers32<1 then
  1300. registers32:=1;
  1301. location.loc:=LOC_REGISTER;
  1302. end;
  1303. end;
  1304. in_typeof_x:
  1305. begin
  1306. if registers32<1 then
  1307. registers32:=1;
  1308. location.loc:=LOC_REGISTER;
  1309. end;
  1310. in_ord_x,
  1311. in_chr_byte:
  1312. begin
  1313. { should not happend as it's converted to typeconv }
  1314. internalerror(200104045);
  1315. end;
  1316. in_length_string:
  1317. begin
  1318. end;
  1319. in_typeinfo_x:
  1320. begin
  1321. location.loc:=LOC_REGISTER;
  1322. registers32:=1;
  1323. end;
  1324. in_assigned_x:
  1325. begin
  1326. location.loc:=LOC_FLAGS;
  1327. end;
  1328. in_ofs_x :
  1329. internalerror(2000101001);
  1330. in_seg_x :
  1331. internalerror(200104046);
  1332. in_pred_x,
  1333. in_succ_x:
  1334. begin
  1335. if is_64bitint(resulttype.def) then
  1336. begin
  1337. if (registers32<2) then
  1338. registers32:=2
  1339. end
  1340. else
  1341. begin
  1342. if (registers32<1) then
  1343. registers32:=1;
  1344. end;
  1345. location.loc:=LOC_REGISTER;
  1346. end;
  1347. in_setlength_x:
  1348. begin
  1349. end;
  1350. in_finalize_x:
  1351. begin
  1352. end;
  1353. in_inc_x,
  1354. in_dec_x:
  1355. begin
  1356. { check type }
  1357. if is_64bitint(left.resulttype.def) or
  1358. { range/overflow checking doesn't work properly }
  1359. { with the inc/dec code that's generated (JM) }
  1360. ((left.resulttype.def.deftype = orddef) and
  1361. not(is_char(left.resulttype.def)) and
  1362. not(is_boolean(left.resulttype.def)) and
  1363. (aktlocalswitches *
  1364. [cs_check_overflow,cs_check_range] <> [])) then
  1365. { convert to simple add (JM) }
  1366. begin
  1367. { extra parameter? }
  1368. if assigned(tcallparanode(left).right) then
  1369. begin
  1370. { Yes, use for add node }
  1371. hpp := tcallparanode(tcallparanode(left).right).left;
  1372. tcallparanode(tcallparanode(left).right).left := nil;
  1373. if assigned(tcallparanode(tcallparanode(left).right).right) then
  1374. CGMessage(cg_e_illegal_expression);
  1375. end
  1376. else
  1377. { no, create constant 1 }
  1378. hpp := cordconstnode.create(1,s32bittype);
  1379. { addition/substraction depending on inc/dec }
  1380. if inlinenumber = in_inc_x then
  1381. hp := caddnode.create(addn,tcallparanode(left).left.getcopy,hpp)
  1382. else
  1383. hp := caddnode.create(subn,tcallparanode(left).left.getcopy,hpp);
  1384. { assign result of addition }
  1385. hpp := cassignmentnode.create(tcallparanode(left).left,hp);
  1386. tcallparanode(left).left := nil;
  1387. { firstpass it }
  1388. firstpass(hpp);
  1389. { return new node }
  1390. result := hpp;
  1391. end
  1392. else if (left.resulttype.def.deftype in [enumdef,pointerdef]) or
  1393. is_ordinal(left.resulttype.def) then
  1394. begin
  1395. { two paras ? }
  1396. if assigned(tcallparanode(left).right) then
  1397. begin
  1398. { need we an additional register ? }
  1399. if not(is_constintnode(tcallparanode(tcallparanode(left).right).left)) and
  1400. (tcallparanode(tcallparanode(left).right).left.location.loc in [LOC_MEM,LOC_REFERENCE]) and
  1401. (tcallparanode(tcallparanode(left).right).left.registers32<=1) then
  1402. inc(registers32);
  1403. { do we need an additional register to restore the first parameter? }
  1404. if tcallparanode(tcallparanode(left).right).left.registers32>=registers32 then
  1405. inc(registers32);
  1406. end;
  1407. end;
  1408. end;
  1409. in_read_x,
  1410. in_readln_x,
  1411. in_write_x,
  1412. in_writeln_x :
  1413. begin
  1414. { needs a call }
  1415. procinfo^.flags:=procinfo^.flags or pi_do_call;
  1416. { true, if readln needs an extra register }
  1417. extra_register:=false;
  1418. { we must know if it is a typed file or not }
  1419. { but we must first do the firstpass for it }
  1420. file_is_typed:=false;
  1421. if assigned(left) then
  1422. begin
  1423. iswrite:=(inlinenumber in [in_write_x,in_writeln_x]);
  1424. { now we can check }
  1425. hp:=left;
  1426. while assigned(tcallparanode(hp).right) do
  1427. hp:=tcallparanode(hp).right;
  1428. { if resulttype.def is not assigned, then automatically }
  1429. { file is not typed. }
  1430. if assigned(hp) then
  1431. Begin
  1432. if (hp.resulttype.def.deftype=filedef) and
  1433. (tfiledef(hp.resulttype.def).filetyp=ft_typed) then
  1434. file_is_typed:=true;
  1435. end; { endif assigned(hp) }
  1436. if (not file_is_typed) then
  1437. begin
  1438. hp:=left;
  1439. while assigned(hp) do
  1440. begin
  1441. {$ifdef i386}
  1442. incrementregisterpushed($ff);
  1443. {$else}
  1444. incrementregisterpushed(ALL_REGISTERS);
  1445. {$endif}
  1446. if assigned(tcallparanode(hp).left.resulttype.def) then
  1447. begin
  1448. case tcallparanode(hp).left.resulttype.def.deftype of
  1449. orddef :
  1450. begin
  1451. if not(iswrite) and
  1452. not(is_64bitint(tcallparanode(hp).left.resulttype.def)) then
  1453. extra_register:=true;
  1454. end;
  1455. end;
  1456. end;
  1457. hp:=tcallparanode(hp).right;
  1458. end;
  1459. end;
  1460. { calc registers }
  1461. left_max;
  1462. if extra_register then
  1463. inc(registers32);
  1464. end;
  1465. end;
  1466. in_settextbuf_file_x :
  1467. internalerror(200104262);
  1468. in_reset_typedfile,
  1469. in_rewrite_typedfile :
  1470. begin
  1471. procinfo^.flags:=procinfo^.flags or pi_do_call;
  1472. end;
  1473. in_str_x_string :
  1474. begin
  1475. procinfo^.flags:=procinfo^.flags or pi_do_call;
  1476. { calc registers }
  1477. left_max;
  1478. end;
  1479. in_val_x :
  1480. begin
  1481. procinfo^.flags:=procinfo^.flags or pi_do_call;
  1482. { calc registers }
  1483. left_max;
  1484. { val doesn't calculate the registers really }
  1485. { correct, we need one register extra (FK) }
  1486. { there is a "code" parameter }
  1487. If Assigned(tcallparanode(tcallparanode(left).right).right) Then
  1488. hpp := tcallparanode(left).right
  1489. Else
  1490. hpp := left;
  1491. { now hpp = the destination value tree }
  1492. if is_64bitint(tcallparanode(hpp).left.resulttype.def) then
  1493. inc(registers32,2)
  1494. else
  1495. inc(registers32,1);
  1496. end;
  1497. in_include_x_y,
  1498. in_exclude_x_y:
  1499. begin
  1500. registers32:=left.registers32;
  1501. registersfpu:=left.registersfpu;
  1502. {$ifdef SUPPORT_MMX}
  1503. registersmmx:=left.registersmmx;
  1504. {$endif SUPPORT_MMX}
  1505. end;
  1506. in_low_x,
  1507. in_high_x:
  1508. internalerror(200104047);
  1509. in_cos_extended:
  1510. begin
  1511. location.loc:=LOC_FPU;
  1512. registers32:=left.registers32;
  1513. registersfpu:=left.registersfpu;
  1514. {$ifdef SUPPORT_MMX}
  1515. registersmmx:=left.registersmmx;
  1516. {$endif SUPPORT_MMX}
  1517. end;
  1518. in_sin_extended:
  1519. begin
  1520. location.loc:=LOC_FPU;
  1521. registers32:=left.registers32;
  1522. registersfpu:=left.registersfpu;
  1523. {$ifdef SUPPORT_MMX}
  1524. registersmmx:=left.registersmmx;
  1525. {$endif SUPPORT_MMX}
  1526. end;
  1527. in_arctan_extended:
  1528. begin
  1529. location.loc:=LOC_FPU;
  1530. registers32:=left.registers32;
  1531. registersfpu:=left.registersfpu;
  1532. {$ifdef SUPPORT_MMX}
  1533. registersmmx:=left.registersmmx;
  1534. {$endif SUPPORT_MMX}
  1535. end;
  1536. in_pi:
  1537. begin
  1538. location.loc:=LOC_FPU;
  1539. end;
  1540. in_abs_extended:
  1541. begin
  1542. location.loc:=LOC_FPU;
  1543. registers32:=left.registers32;
  1544. registersfpu:=left.registersfpu;
  1545. {$ifdef SUPPORT_MMX}
  1546. registersmmx:=left.registersmmx;
  1547. {$endif SUPPORT_MMX}
  1548. end;
  1549. in_sqr_extended:
  1550. begin
  1551. location.loc:=LOC_FPU;
  1552. registers32:=left.registers32;
  1553. registersfpu:=left.registersfpu;
  1554. {$ifdef SUPPORT_MMX}
  1555. registersmmx:=left.registersmmx;
  1556. {$endif SUPPORT_MMX}
  1557. end;
  1558. in_sqrt_extended:
  1559. begin
  1560. location.loc:=LOC_FPU;
  1561. registers32:=left.registers32;
  1562. registersfpu:=left.registersfpu;
  1563. {$ifdef SUPPORT_MMX}
  1564. registersmmx:=left.registersmmx;
  1565. {$endif SUPPORT_MMX}
  1566. end;
  1567. in_ln_extended:
  1568. begin
  1569. location.loc:=LOC_FPU;
  1570. registers32:=left.registers32;
  1571. registersfpu:=left.registersfpu;
  1572. {$ifdef SUPPORT_MMX}
  1573. registersmmx:=left.registersmmx;
  1574. {$endif SUPPORT_MMX}
  1575. end;
  1576. {$ifdef SUPPORT_MMX}
  1577. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  1578. begin
  1579. end;
  1580. {$endif SUPPORT_MMX}
  1581. in_assert_x_y :
  1582. begin
  1583. { We've checked the whole statement for correctness, now we
  1584. can remove it if assertions are off }
  1585. if not(cs_do_assertion in aktlocalswitches) then
  1586. begin
  1587. { we need a valid node, so insert a nothingn }
  1588. result:=cnothingnode.create;
  1589. end
  1590. else
  1591. begin
  1592. registers32:=left.registers32;
  1593. registersfpu:=left.registersfpu;
  1594. {$ifdef SUPPORT_MMX}
  1595. registersmmx:=left.registersmmx;
  1596. {$endif SUPPORT_MMX}
  1597. end;
  1598. end;
  1599. else
  1600. internalerror(8);
  1601. end;
  1602. dec(parsing_para_level);
  1603. end;
  1604. {$ifdef fpc}
  1605. {$maxfpuregisters default}
  1606. {$endif fpc}
  1607. function tinlinenode.docompare(p: tnode): boolean;
  1608. begin
  1609. docompare :=
  1610. inherited docompare(p) and
  1611. (inlinenumber = tinlinenode(p).inlinenumber);
  1612. end;
  1613. begin
  1614. cinlinenode:=tinlinenode;
  1615. end.
  1616. {
  1617. $Log$
  1618. Revision 1.39 2001-04-26 21:57:05 peter
  1619. * moved code from firstpass to det_resulttype and remove extraneous
  1620. calls to firstcallparan for in_str,in_write,in_val
  1621. Revision 1.38 2001/04/21 12:03:11 peter
  1622. * m68k updates merged from fixes branch
  1623. Revision 1.37 2001/04/13 22:22:30 peter
  1624. * call set_varstate for setlength
  1625. * ptr returns pointerconstnode instead of ordconstnode
  1626. Revision 1.36 2001/04/13 01:22:09 peter
  1627. * symtable change to classes
  1628. * range check generation and errors fixed, make cycle DEBUG=1 works
  1629. * memory leaks fixed
  1630. Revision 1.35 2001/04/05 21:02:13 peter
  1631. * fixed fpu inline functions typeconvs
  1632. Revision 1.34 2001/04/04 22:42:40 peter
  1633. * move constant folding into det_resulttype
  1634. Revision 1.33 2001/04/04 21:30:43 florian
  1635. * applied several fixes to get the DD8 Delphi Unit compiled
  1636. e.g. "forward"-interfaces are working now
  1637. Revision 1.32 2001/04/02 21:20:31 peter
  1638. * resulttype rewrite
  1639. Revision 1.31 2001/03/23 00:16:07 florian
  1640. + some stuff to compile FreeCLX added
  1641. Revision 1.30 2001/03/12 12:47:46 michael
  1642. + Patches from peter
  1643. Revision 1.29 2001/03/03 12:38:08 jonas
  1644. * fixed low() for signed types < 64bit
  1645. Revision 1.28 2001/02/26 19:44:53 peter
  1646. * merged generic m68k updates from fixes branch
  1647. Revision 1.27 2001/02/22 11:24:40 jonas
  1648. * fixed bug in previous fix (hopped over revision 1.26 because that one
  1649. also removed the fix for high(cardinal))
  1650. Revision 1.26 2001/02/21 20:50:59 peter
  1651. * fix to compile again, but high(cardinal) with $R+ still fails!
  1652. Revision 1.25 2001/02/21 12:57:46 jonas
  1653. * fixed high/low for cardinal, int64 and qword
  1654. Revision 1.24 2001/01/06 19:54:11 peter
  1655. * merged fix for 1310
  1656. Revision 1.23 2001/01/06 18:28:39 peter
  1657. * fixed wrong notes about locals
  1658. Revision 1.22 2000/12/31 11:14:10 jonas
  1659. + implemented/fixed docompare() mathods for all nodes (not tested)
  1660. + nopt.pas, nadd.pas, i386/n386opt.pas: optimized nodes for adding strings
  1661. and constant strings/chars together
  1662. * n386add.pas: don't copy temp strings (of size 256) to another temp string
  1663. when adding
  1664. Revision 1.21 2000/12/25 00:07:26 peter
  1665. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1666. tlinkedlist objects)
  1667. Revision 1.20 2000/12/17 14:35:41 peter
  1668. * fixed crash with val()
  1669. Revision 1.19 2000/11/29 00:30:33 florian
  1670. * unused units removed from uses clause
  1671. * some changes for widestrings
  1672. Revision 1.18 2000/11/12 15:27:22 jonas
  1673. * also don't do conversion for chars/booleans (hopefully final change :/)
  1674. Revision 1.17 2000/11/11 21:08:13 jonas
  1675. * don't do inc/dec to add/sub conversion for enums
  1676. Revision 1.16 2000/11/11 16:18:35 peter
  1677. * ptr returns farpointer
  1678. Revision 1.15 2000/11/11 15:59:07 jonas
  1679. * convert inc/dec to add/sub when range/overflow checking is on
  1680. Revision 1.14 2000/11/09 17:46:54 florian
  1681. * System.TypeInfo fixed
  1682. + System.Finalize implemented
  1683. + some new keywords for interface support added
  1684. Revision 1.13 2000/11/04 16:48:32 florian
  1685. * innr.inc renamed to make compiler compilation easier because the rtl contains
  1686. a file of the same name
  1687. Revision 1.12 2000/10/31 22:02:48 peter
  1688. * symtable splitted, no real code changes
  1689. Revision 1.11 2000/10/26 14:15:06 jonas
  1690. * fixed setlength for shortstrings
  1691. Revision 1.10 2000/10/21 18:16:11 florian
  1692. * a lot of changes:
  1693. - basic dyn. array support
  1694. - basic C++ support
  1695. - some work for interfaces done
  1696. ....
  1697. Revision 1.9 2000/10/15 08:38:46 jonas
  1698. * added missing getcopy for previous addition
  1699. Revision 1.8 2000/10/14 18:27:53 jonas
  1700. * merged fix for inc/dec on 64bit types from tcinl
  1701. Revision 1.7 2000/10/14 10:14:50 peter
  1702. * moehrendorf oct 2000 rewrite
  1703. Revision 1.6 2000/10/01 19:48:24 peter
  1704. * lot of compile updates for cg11
  1705. Revision 1.5 2000/09/28 19:49:52 florian
  1706. *** empty log message ***
  1707. Revision 1.4 2000/09/28 16:34:47 florian
  1708. *** empty log message ***
  1709. Revision 1.3 2000/09/27 21:33:22 florian
  1710. * finally nadd.pas compiles
  1711. Revision 1.2 2000/09/27 20:25:44 florian
  1712. * more stuff fixed
  1713. Revision 1.1 2000/09/26 14:59:34 florian
  1714. * more conversion work done
  1715. }