ptconst.pas 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Reads typed constants
  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 ptconst;
  19. {$i defines.inc}
  20. interface
  21. uses symtable;
  22. { this procedure reads typed constants }
  23. { sym is only needed for ansi strings }
  24. { the assembler label is in the middle (PM) }
  25. procedure readtypedconst(def : pdef;sym : ptypedconstsym;no_change_allowed : boolean);
  26. implementation
  27. uses
  28. {$ifdef Delphi}
  29. sysutils,
  30. {$else}
  31. strings,
  32. {$endif Delphi}
  33. globtype,systems,tokens,cpuinfo,
  34. cutils,cobjects,globals,scanner,
  35. symconst,aasm,types,verbose,
  36. { pass 1 }
  37. node,pass_1,
  38. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,
  39. { parser specific stuff }
  40. pbase,pexpr,
  41. { codegen }
  42. {$ifdef newcg}
  43. cgbase,
  44. {$else}
  45. hcodegen,
  46. {$endif}
  47. hcgdata;
  48. {$ifdef fpc}
  49. {$maxfpuregisters 0}
  50. {$endif fpc}
  51. { this procedure reads typed constants }
  52. procedure readtypedconst(def : pdef;sym : ptypedconstsym;no_change_allowed : boolean);
  53. var
  54. {$ifdef m68k}
  55. j : longint;
  56. {$endif m68k}
  57. len,base : longint;
  58. p,hp : tnode;
  59. i,l,offset,
  60. strlength : longint;
  61. curconstsegment : paasmoutput;
  62. ll : pasmlabel;
  63. s : string;
  64. ca : pchar;
  65. aktpos : longint;
  66. obj : pobjectdef;
  67. symt : psymtable;
  68. value : bestreal;
  69. strval : pchar;
  70. procedure check_range;
  71. begin
  72. if ((tordconstnode(p).value>porddef(def)^.high) or
  73. (tordconstnode(p).value<porddef(def)^.low)) then
  74. begin
  75. if (cs_check_range in aktlocalswitches) then
  76. Message(parser_e_range_check_error)
  77. else
  78. Message(parser_w_range_check_error);
  79. end;
  80. end;
  81. {$R-} {Range check creates problem with init_8bit(-1) !!}
  82. begin
  83. if no_change_allowed then
  84. curconstsegment:=consts
  85. else
  86. curconstsegment:=datasegment;
  87. case def^.deftype of
  88. orddef:
  89. begin
  90. p:=comp_expr(true);
  91. do_firstpass(p);
  92. case porddef(def)^.typ of
  93. bool8bit :
  94. begin
  95. if is_constboolnode(p) then
  96. curconstsegment^.concat(new(pai_const,init_8bit(tordconstnode(p).value)))
  97. else
  98. Message(cg_e_illegal_expression);
  99. end;
  100. bool16bit :
  101. begin
  102. if is_constboolnode(p) then
  103. curconstsegment^.concat(new(pai_const,init_16bit(tordconstnode(p).value)))
  104. else
  105. Message(cg_e_illegal_expression);
  106. end;
  107. bool32bit :
  108. begin
  109. if is_constboolnode(p) then
  110. curconstsegment^.concat(new(pai_const,init_32bit(tordconstnode(p).value)))
  111. else
  112. Message(cg_e_illegal_expression);
  113. end;
  114. uchar :
  115. begin
  116. if is_constcharnode(p) then
  117. curconstsegment^.concat(new(pai_const,init_8bit(tordconstnode(p).value)))
  118. else
  119. Message(cg_e_illegal_expression);
  120. end;
  121. uwidechar :
  122. begin
  123. if is_constcharnode(p) then
  124. curconstsegment^.concat(new(pai_const,init_16bit(tordconstnode(p).value)))
  125. else
  126. Message(cg_e_illegal_expression);
  127. end;
  128. s8bit,
  129. u8bit :
  130. begin
  131. if is_constintnode(p) then
  132. begin
  133. curconstsegment^.concat(new(pai_const,init_8bit(tordconstnode(p).value)));
  134. check_range;
  135. end
  136. else
  137. Message(cg_e_illegal_expression);
  138. end;
  139. u16bit,
  140. s16bit :
  141. begin
  142. if is_constintnode(p) then
  143. begin
  144. curconstsegment^.concat(new(pai_const,init_16bit(tordconstnode(p).value)));
  145. check_range;
  146. end
  147. else
  148. Message(cg_e_illegal_expression);
  149. end;
  150. s32bit,
  151. u32bit :
  152. begin
  153. if is_constintnode(p) then
  154. begin
  155. curconstsegment^.concat(new(pai_const,init_32bit(tordconstnode(p).value)));
  156. if porddef(def)^.typ<>u32bit then
  157. check_range;
  158. end
  159. else
  160. Message(cg_e_illegal_expression);
  161. end;
  162. s64bit,
  163. u64bit:
  164. begin
  165. if is_constintnode(p) then
  166. begin
  167. {!!!!! hmmm, we can write yet only consts til 2^32-1 :( (FK) }
  168. curconstsegment^.concat(new(pai_const,init_32bit(tordconstnode(p).value)));
  169. curconstsegment^.concat(new(pai_const,init_32bit(0)));
  170. end
  171. else
  172. Message(cg_e_illegal_expression);
  173. end;
  174. else
  175. internalerror(3799);
  176. end;
  177. p.free;
  178. end;
  179. floatdef:
  180. begin
  181. p:=comp_expr(true);
  182. do_firstpass(p);
  183. if is_constrealnode(p) then
  184. value:=trealconstnode(p).value_real
  185. else if is_constintnode(p) then
  186. value:=tordconstnode(p).value
  187. else
  188. Message(cg_e_illegal_expression);
  189. case pfloatdef(def)^.typ of
  190. s32real :
  191. curconstsegment^.concat(new(pai_real_32bit,init(value)));
  192. s64real :
  193. curconstsegment^.concat(new(pai_real_64bit,init(value)));
  194. s80real :
  195. curconstsegment^.concat(new(pai_real_80bit,init(value)));
  196. s64comp :
  197. curconstsegment^.concat(new(pai_comp_64bit,init(value)));
  198. f32bit :
  199. curconstsegment^.concat(new(pai_const,init_32bit(trunc(value*65536))));
  200. else
  201. internalerror(18);
  202. end;
  203. p.free;
  204. end;
  205. classrefdef:
  206. begin
  207. p:=comp_expr(true);
  208. do_firstpass(p);
  209. case p.nodetype of
  210. loadvmtn:
  211. begin
  212. if not(pobjectdef(pclassrefdef(p.resulttype)^.pointertype.def)^.is_related(
  213. pobjectdef(pclassrefdef(def)^.pointertype.def))) then
  214. Message(cg_e_illegal_expression);
  215. curconstsegment^.concat(new(pai_const_symbol,init(newasmsymbol(pobjectdef(
  216. pclassrefdef(p.resulttype)^.pointertype.def)^.vmt_mangledname))));
  217. end;
  218. niln:
  219. curconstsegment^.concat(new(pai_const,init_32bit(0)));
  220. else Message(cg_e_illegal_expression);
  221. end;
  222. p.free;
  223. end;
  224. pointerdef:
  225. begin
  226. p:=comp_expr(true);
  227. do_firstpass(p);
  228. if (p.nodetype=typeconvn) and
  229. (ttypeconvnode(p).left.nodetype in [addrn,niln]) and
  230. is_equal(def,p.resulttype) then
  231. begin
  232. hp:=ttypeconvnode(p).left;
  233. ttypeconvnode(p).left:=nil;
  234. p.free;
  235. p:=hp;
  236. end;
  237. { allows horrible ofs(typeof(TButton)^) code !! }
  238. if (p.nodetype=addrn) and
  239. (taddrnode(p).left.nodetype=derefn) then
  240. begin
  241. hp:=tderefnode(taddrnode(p).left).left;
  242. tderefnode(taddrnode(p).left).left:=nil;
  243. p.free;
  244. p:=hp;
  245. end;
  246. { nil pointer ? }
  247. if p.nodetype=niln then
  248. curconstsegment^.concat(new(pai_const,init_32bit(0)))
  249. { maybe pchar ? }
  250. else
  251. if is_char(ppointerdef(def)^.pointertype.def) and
  252. (p.nodetype<>addrn) then
  253. begin
  254. getdatalabel(ll);
  255. curconstsegment^.concat(new(pai_const_symbol,init(ll)));
  256. consts^.concat(new(pai_label,init(ll)));
  257. if p.nodetype=stringconstn then
  258. begin
  259. len:=tstringconstnode(p).len;
  260. { For tp7 the maximum lentgh can be 255 }
  261. if (m_tp in aktmodeswitches) and
  262. (len>255) then
  263. len:=255;
  264. getmem(ca,len+2);
  265. move(tstringconstnode(p).value_str^,ca^,len+1);
  266. consts^.concat(new(pai_string,init_length_pchar(ca,len+1)));
  267. end
  268. else
  269. if is_constcharnode(p) then
  270. consts^.concat(new(pai_string,init(char(byte(tordconstnode(p).value))+#0)))
  271. else
  272. Message(cg_e_illegal_expression);
  273. end
  274. else
  275. if p.nodetype=addrn then
  276. begin
  277. hp:=taddrnode(p).left;
  278. while assigned(hp) and (hp.nodetype in [subscriptn,vecn]) do
  279. hp:=tbinarynode(hp).left;
  280. if (is_equal(ppointerdef(p.resulttype)^.pointertype.def,ppointerdef(def)^.pointertype.def) or
  281. (is_equal(ppointerdef(p.resulttype)^.pointertype.def,voiddef)) or
  282. (is_equal(ppointerdef(def)^.pointertype.def,voiddef))) and
  283. (hp.nodetype=loadn) then
  284. begin
  285. do_firstpass(taddrnode(p).left);
  286. hp:=taddrnode(p).left;
  287. offset:=0;
  288. while assigned(hp) and (hp.nodetype<>loadn) do
  289. begin
  290. case hp.nodetype of
  291. vecn :
  292. begin
  293. case tvecnode(hp).left.resulttype^.deftype of
  294. stringdef :
  295. begin
  296. { this seems OK for shortstring and ansistrings PM }
  297. { it is wrong for widestrings !! }
  298. len:=1;
  299. base:=0;
  300. end;
  301. arraydef :
  302. begin
  303. len:=parraydef(tvecnode(hp).left.resulttype)^.elesize;
  304. base:=parraydef(tvecnode(hp).left.resulttype)^.lowrange;
  305. end
  306. else
  307. Message(cg_e_illegal_expression);
  308. end;
  309. if is_constintnode(tvecnode(hp).right) then
  310. inc(offset,len*(get_ordinal_value(tvecnode(hp).right)-base))
  311. else
  312. Message(cg_e_illegal_expression);
  313. end;
  314. subscriptn :
  315. inc(offset,tsubscriptnode(hp).vs^.address)
  316. else
  317. Message(cg_e_illegal_expression);
  318. end;
  319. hp:=tbinarynode(hp).left;
  320. end;
  321. if tloadnode(hp).symtableentry^.typ=constsym then
  322. Message(type_e_variable_id_expected);
  323. curconstsegment^.concat(new(pai_const_symbol,initname_offset(tloadnode(hp).symtableentry^.mangledname,offset)));
  324. end
  325. else
  326. Message(cg_e_illegal_expression);
  327. end
  328. else
  329. { allow typeof(Object type)}
  330. if (p.nodetype=inlinen) and
  331. (tinlinenode(p).inlinenumber=in_typeof_x) then
  332. begin
  333. if (tinlinenode(p).left.nodetype=typen) then
  334. begin
  335. curconstsegment^.concat(new(pai_const_symbol,
  336. initname(pobjectdef(tinlinenode(p).left.resulttype)^.vmt_mangledname)));
  337. end
  338. else
  339. Message(cg_e_illegal_expression);
  340. end
  341. else
  342. Message(cg_e_illegal_expression);
  343. p.free;
  344. end;
  345. setdef:
  346. begin
  347. p:=comp_expr(true);
  348. do_firstpass(p);
  349. if p.nodetype=setconstn then
  350. begin
  351. { we only allow const sets }
  352. if assigned(tsetconstnode(p).left) then
  353. Message(cg_e_illegal_expression)
  354. else
  355. begin
  356. {$ifdef i386}
  357. for l:=0 to def^.size-1 do
  358. curconstsegment^.concat(new(pai_const,init_8bit(tsetconstnode(p).value_set^[l])));
  359. {$endif}
  360. {$ifdef m68k}
  361. j:=0;
  362. for l:=0 to ((def^.size-1) div 4) do
  363. { HORRIBLE HACK because of endian }
  364. { now use intel endian for constant sets }
  365. begin
  366. curconstsegment^.concat(new(pai_const,init_8bit(tordconstnode(p).value_set^[j+3])));
  367. curconstsegment^.concat(new(pai_const,init_8bit(tordconstnode(p).value_set^[j+2])));
  368. curconstsegment^.concat(new(pai_const,init_8bit(tordconstnode(p).value_set^[j+1])));
  369. curconstsegment^.concat(new(pai_const,init_8bit(tordconstnode(p).value_set^[j])));
  370. Inc(j,4);
  371. end;
  372. {$endif}
  373. end;
  374. end
  375. else
  376. Message(cg_e_illegal_expression);
  377. p.free;
  378. end;
  379. enumdef:
  380. begin
  381. p:=comp_expr(true);
  382. do_firstpass(p);
  383. if p.nodetype=ordconstn then
  384. begin
  385. if is_equal(p.resulttype,def) then
  386. begin
  387. case p.resulttype^.size of
  388. 1 : curconstsegment^.concat(new(pai_const,init_8bit(tordconstnode(p).value)));
  389. 2 : curconstsegment^.concat(new(pai_const,init_16bit(tordconstnode(p).value)));
  390. 4 : curconstsegment^.concat(new(pai_const,init_32bit(tordconstnode(p).value)));
  391. end;
  392. end
  393. else
  394. Message(cg_e_illegal_expression);
  395. end
  396. else
  397. Message(cg_e_illegal_expression);
  398. p.free;
  399. end;
  400. stringdef:
  401. begin
  402. p:=comp_expr(true);
  403. do_firstpass(p);
  404. { load strval and strlength of the constant tree }
  405. if p.nodetype=stringconstn then
  406. begin
  407. strlength:=tstringconstnode(p).len;
  408. strval:=tstringconstnode(p).value_str;
  409. end
  410. else if is_constcharnode(p) then
  411. begin
  412. strval:=pchar(@tordconstnode(p).value);
  413. strlength:=1
  414. end
  415. else if is_constresourcestringnode(p) then
  416. begin
  417. strval:=pchar(tpointerord(pconstsym(tloadnode(p).symtableentry)^.value));
  418. strlength:=pconstsym(tloadnode(p).symtableentry)^.len;
  419. end
  420. else
  421. begin
  422. Message(cg_e_illegal_expression);
  423. strlength:=-1;
  424. end;
  425. if strlength>=0 then
  426. begin
  427. case pstringdef(def)^.string_typ of
  428. st_shortstring:
  429. begin
  430. if strlength>=def^.size then
  431. begin
  432. message2(parser_w_string_too_long,strpas(strval),tostr(def^.size-1));
  433. strlength:=def^.size-1;
  434. end;
  435. curconstsegment^.concat(new(pai_const,init_8bit(strlength)));
  436. { this can also handle longer strings }
  437. getmem(ca,strlength+1);
  438. move(strval^,ca^,strlength);
  439. ca[strlength]:=#0;
  440. curconstsegment^.concat(new(pai_string,init_length_pchar(ca,strlength)));
  441. { fillup with spaces if size is shorter }
  442. if def^.size>strlength then
  443. begin
  444. getmem(ca,def^.size-strlength);
  445. { def^.size contains also the leading length, so we }
  446. { we have to subtract one }
  447. fillchar(ca[0],def^.size-strlength-1,' ');
  448. ca[def^.size-strlength-1]:=#0;
  449. { this can also handle longer strings }
  450. curconstsegment^.concat(new(pai_string,init_length_pchar(ca,def^.size-strlength-1)));
  451. end;
  452. end;
  453. {$ifdef UseLongString}
  454. st_longstring:
  455. begin
  456. { first write the maximum size }
  457. curconstsegment^.concat(new(pai_const,init_32bit(strlength)))));
  458. { fill byte }
  459. curconstsegment^.concat(new(pai_const,init_8bit(0)));
  460. getmem(ca,strlength+1);
  461. move(strval^,ca^,strlength);
  462. ca[strlength]:=#0;
  463. generate_pascii(consts,ca,strlength);
  464. curconstsegment^.concat(new(pai_const,init_8bit(0)));
  465. end;
  466. {$endif UseLongString}
  467. st_ansistring:
  468. begin
  469. { an empty ansi string is nil! }
  470. if (strlength=0) then
  471. curconstsegment^.concat(new(pai_const,init_32bit(0)))
  472. else
  473. begin
  474. getdatalabel(ll);
  475. curconstsegment^.concat(new(pai_const_symbol,init(ll)));
  476. { first write the maximum size }
  477. consts^.concat(new(pai_const,init_32bit(strlength)));
  478. { second write the real length }
  479. consts^.concat(new(pai_const,init_32bit(strlength)));
  480. { redondent with maxlength but who knows ... (PM) }
  481. { third write use count (set to -1 for safety ) }
  482. consts^.concat(new(pai_const,init_32bit(-1)));
  483. consts^.concat(new(pai_label,init(ll)));
  484. getmem(ca,strlength+2);
  485. move(strval^,ca^,strlength);
  486. { The terminating #0 to be stored in the .data section (JM) }
  487. ca[strlength]:=#0;
  488. { End of the PChar. The memory has to be allocated because in }
  489. { tai_string.done, there is a freemem(len+1) (JM) }
  490. ca[strlength+1]:=#0;
  491. consts^.concat(new(pai_string,init_length_pchar(ca,strlength+1)));
  492. end;
  493. end;
  494. end;
  495. end;
  496. p.free;
  497. end;
  498. arraydef:
  499. begin
  500. if token=_LKLAMMER then
  501. begin
  502. consume(_LKLAMMER);
  503. for l:=parraydef(def)^.lowrange to parraydef(def)^.highrange-1 do
  504. begin
  505. readtypedconst(parraydef(def)^.elementtype.def,nil,no_change_allowed);
  506. consume(_COMMA);
  507. end;
  508. readtypedconst(parraydef(def)^.elementtype.def,nil,no_change_allowed);
  509. consume(_RKLAMMER);
  510. end
  511. else
  512. { if array of char then we allow also a string }
  513. if is_char(parraydef(def)^.elementtype.def) then
  514. begin
  515. p:=comp_expr(true);
  516. do_firstpass(p);
  517. if p.nodetype=stringconstn then
  518. begin
  519. len:=tstringconstnode(p).len;
  520. { For tp7 the maximum lentgh can be 255 }
  521. if (m_tp in aktmodeswitches) and
  522. (len>255) then
  523. len:=255;
  524. ca:=tstringconstnode(p).value_str;
  525. end
  526. else
  527. if is_constcharnode(p) then
  528. begin
  529. ca:=pchar(@tordconstnode(p).value);
  530. len:=1;
  531. end
  532. else
  533. begin
  534. Message(cg_e_illegal_expression);
  535. len:=0;
  536. end;
  537. if len>(Parraydef(def)^.highrange-Parraydef(def)^.lowrange+1) then
  538. Message(parser_e_string_larger_array);
  539. for i:=Parraydef(def)^.lowrange to Parraydef(def)^.highrange do
  540. begin
  541. if i+1-Parraydef(def)^.lowrange<=len then
  542. begin
  543. curconstsegment^.concat(new(pai_const,init_8bit(byte(ca^))));
  544. inc(ca);
  545. end
  546. else
  547. {Fill the remaining positions with #0.}
  548. curconstsegment^.concat(new(pai_const,init_8bit(0)));
  549. end;
  550. p.free;
  551. end
  552. else
  553. begin
  554. { we want the ( }
  555. consume(_LKLAMMER);
  556. end;
  557. end;
  558. procvardef:
  559. begin
  560. { Procvars and pointers are no longer compatible. }
  561. { under tp: =nil or =var under fpc: =nil or =@var }
  562. if token=_NIL then
  563. begin
  564. curconstsegment^.concat(new(pai_const,init_32bit(0)));
  565. consume(_NIL);
  566. exit;
  567. end
  568. else
  569. if not(m_tp_procvar in aktmodeswitches) then
  570. if token=_KLAMMERAFFE then
  571. consume(_KLAMMERAFFE);
  572. getprocvar:=true;
  573. getprocvardef:=pprocvardef(def);
  574. p:=comp_expr(true);
  575. getprocvar:=false;
  576. do_firstpass(p);
  577. if codegenerror then
  578. begin
  579. p.free;
  580. exit;
  581. end;
  582. { convert calln to loadn }
  583. if p.nodetype=calln then
  584. begin
  585. if (tcallnode(p).symtableprocentry^.owner^.symtabletype=objectsymtable) and
  586. (pobjectdef(tcallnode(p).symtableprocentry^.owner^.defowner)^.is_class) then
  587. hp:=genloadmethodcallnode(pprocsym(tcallnode(p).symtableprocentry),tcallnode(p).symtableproc,
  588. tcallnode(p).methodpointer.getcopy)
  589. else
  590. hp:=genloadcallnode(pprocsym(tcallnode(p).symtableprocentry),tcallnode(p).symtableproc);
  591. p.free;
  592. do_firstpass(hp);
  593. p:=hp;
  594. if codegenerror then
  595. begin
  596. p.free;
  597. exit;
  598. end;
  599. end
  600. else if (p.nodetype=addrn) and assigned(taddrnode(p).left) and
  601. (taddrnode(p).left.nodetype=calln) then
  602. begin
  603. if (tcallnode(taddrnode(p).left).symtableprocentry^.owner^.symtabletype=objectsymtable) and
  604. (pobjectdef(tcallnode(taddrnode(p).left).symtableprocentry^.owner^.defowner)^.is_class) then
  605. hp:=genloadmethodcallnode(pprocsym(tcallnode(taddrnode(p).left).symtableprocentry),
  606. tcallnode(taddrnode(p).left).symtableproc,tcallnode(taddrnode(p).left).methodpointer.getcopy)
  607. else
  608. hp:=genloadcallnode(pprocsym(tcallnode(taddrnode(p).left).symtableprocentry),
  609. tcallnode(taddrnode(p).left).symtableproc);
  610. p.free;
  611. do_firstpass(hp);
  612. p:=hp;
  613. if codegenerror then
  614. begin
  615. p.free;
  616. exit;
  617. end;
  618. end;
  619. { let type conversion check everything needed }
  620. p:=gentypeconvnode(p,def);
  621. do_firstpass(p);
  622. if codegenerror then
  623. begin
  624. p.free;
  625. exit;
  626. end;
  627. { remove typeconvn, that will normally insert a lea
  628. instruction which is not necessary for us }
  629. if p.nodetype=typeconvn then
  630. begin
  631. hp:=ttypeconvnode(p).left;
  632. ttypeconvnode(p).left:=nil;
  633. p.free;
  634. p:=hp;
  635. end;
  636. { remove addrn which we also don't need here }
  637. if p.nodetype=addrn then
  638. begin
  639. hp:=taddrnode(p).left;
  640. taddrnode(p).left:=nil;
  641. p.free;
  642. p:=hp;
  643. end;
  644. { we now need to have a loadn with a procsym }
  645. if (p.nodetype=loadn) and
  646. (tloadnode(p).symtableentry^.typ=procsym) then
  647. begin
  648. curconstsegment^.concat(new(pai_const_symbol,
  649. initname(pprocsym(tloadnode(p).symtableentry)^.definition^.mangledname)));
  650. end
  651. else
  652. Message(cg_e_illegal_expression);
  653. p.free;
  654. end;
  655. { reads a typed constant record }
  656. recorddef:
  657. begin
  658. consume(_LKLAMMER);
  659. aktpos:=0;
  660. while token<>_RKLAMMER do
  661. begin
  662. s:=pattern;
  663. consume(_ID);
  664. consume(_COLON);
  665. srsym:=precorddef(def)^.symtable^.search(s);
  666. if srsym=nil then
  667. begin
  668. Message1(sym_e_id_not_found,s);
  669. consume_all_until(_SEMICOLON);
  670. end
  671. else
  672. begin
  673. { check position }
  674. if pvarsym(srsym)^.address<aktpos then
  675. Message(parser_e_invalid_record_const);
  676. { if needed fill }
  677. if pvarsym(srsym)^.address>aktpos then
  678. for i:=1 to pvarsym(srsym)^.address-aktpos do
  679. curconstsegment^.concat(new(pai_const,init_8bit(0)));
  680. { new position }
  681. aktpos:=pvarsym(srsym)^.address+pvarsym(srsym)^.vartype.def^.size;
  682. { read the data }
  683. readtypedconst(pvarsym(srsym)^.vartype.def,nil,no_change_allowed);
  684. if token=_SEMICOLON then
  685. consume(_SEMICOLON)
  686. else break;
  687. end;
  688. end;
  689. for i:=1 to def^.size-aktpos do
  690. curconstsegment^.concat(new(pai_const,init_8bit(0)));
  691. consume(_RKLAMMER);
  692. end;
  693. { reads a typed object }
  694. objectdef:
  695. begin
  696. if ([oo_has_vmt,oo_is_class]*pobjectdef(def)^.objectoptions)<>[] then
  697. begin
  698. { support nil assignment for classes }
  699. if pobjectdef(def)^.is_class and
  700. try_to_consume(_NIL) then
  701. begin
  702. curconstsegment^.concat(new(pai_const,init_32bit(0)));
  703. end
  704. else
  705. begin
  706. Message(parser_e_type_const_not_possible);
  707. consume_all_until(_RKLAMMER);
  708. end;
  709. end
  710. else
  711. begin
  712. consume(_LKLAMMER);
  713. aktpos:=0;
  714. while token<>_RKLAMMER do
  715. begin
  716. s:=pattern;
  717. consume(_ID);
  718. consume(_COLON);
  719. srsym:=nil;
  720. obj:=pobjectdef(def);
  721. symt:=obj^.symtable;
  722. while (srsym=nil) and assigned(symt) do
  723. begin
  724. srsym:=symt^.search(s);
  725. if assigned(obj) then
  726. obj:=obj^.childof;
  727. if assigned(obj) then
  728. symt:=obj^.symtable
  729. else
  730. symt:=nil;
  731. end;
  732. if srsym=nil then
  733. begin
  734. Message1(sym_e_id_not_found,s);
  735. consume_all_until(_SEMICOLON);
  736. end
  737. else
  738. begin
  739. { check position }
  740. if pvarsym(srsym)^.address<aktpos then
  741. Message(parser_e_invalid_record_const);
  742. { if needed fill }
  743. if pvarsym(srsym)^.address>aktpos then
  744. for i:=1 to pvarsym(srsym)^.address-aktpos do
  745. curconstsegment^.concat(new(pai_const,init_8bit(0)));
  746. { new position }
  747. aktpos:=pvarsym(srsym)^.address+pvarsym(srsym)^.vartype.def^.size;
  748. { read the data }
  749. readtypedconst(pvarsym(srsym)^.vartype.def,nil,no_change_allowed);
  750. if token=_SEMICOLON then
  751. consume(_SEMICOLON)
  752. else break;
  753. end;
  754. end;
  755. for i:=1 to def^.size-aktpos do
  756. curconstsegment^.concat(new(pai_const,init_8bit(0)));
  757. consume(_RKLAMMER);
  758. end;
  759. end;
  760. errordef:
  761. begin
  762. { try to consume something useful }
  763. if token=_LKLAMMER then
  764. consume_all_until(_RKLAMMER)
  765. else
  766. consume_all_until(_SEMICOLON);
  767. end;
  768. else Message(parser_e_type_const_not_possible);
  769. end;
  770. end;
  771. {$ifdef fpc}
  772. {$maxfpuregisters default}
  773. {$endif fpc}
  774. end.
  775. {
  776. $Log$
  777. Revision 1.9 2000-10-14 10:14:52 peter
  778. * moehrendorf oct 2000 rewrite
  779. Revision 1.8 2000/09/30 13:23:04 peter
  780. * const array of char and pchar length fixed (merged)
  781. Revision 1.7 2000/09/24 15:06:25 peter
  782. * use defines.inc
  783. Revision 1.6 2000/08/27 16:11:52 peter
  784. * moved some util functions from globals,cobjects to cutils
  785. * splitted files into finput,fmodule
  786. Revision 1.5 2000/08/24 19:13:18 peter
  787. * allow nil for class typed consts (merged)
  788. Revision 1.4 2000/08/16 13:06:06 florian
  789. + support of 64 bit integer constants
  790. Revision 1.3 2000/08/05 13:25:06 peter
  791. * packenum 1 fixes (merged)
  792. Revision 1.2 2000/07/13 11:32:47 michael
  793. + removed logs
  794. }