ptconst.pas 34 KB

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