ptconst.pas 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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. tree,pass_1,
  37. { parser specific stuff }
  38. pbase,pexpr,
  39. { processor specific stuff }
  40. cpubase,
  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 : ptree;
  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 ((p^.value>porddef(def)^.high) or
  73. (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. (* function is_po_equal(o1,o2:longint):boolean;
  82. begin
  83. { assembler does not affect }
  84. is_po_equal:=(o1 and not(poassembler))=
  85. (o2 and not(poassembler));
  86. end; *)
  87. {$R-} {Range check creates problem with init_8bit(-1) !!}
  88. begin
  89. if no_change_allowed then
  90. curconstsegment:=consts
  91. else
  92. curconstsegment:=datasegment;
  93. case def^.deftype of
  94. orddef:
  95. begin
  96. p:=comp_expr(true);
  97. do_firstpass(p);
  98. case porddef(def)^.typ of
  99. s8bit,
  100. u8bit : begin
  101. if not is_constintnode(p) then
  102. { is't an int expected }
  103. Message(cg_e_illegal_expression)
  104. else
  105. begin
  106. curconstsegment^.concat(new(pai_const,init_8bit(p^.value)));
  107. check_range;
  108. end;
  109. end;
  110. s32bit : begin
  111. if not is_constintnode(p) then
  112. Message(cg_e_illegal_expression)
  113. else
  114. begin
  115. curconstsegment^.concat(new(pai_const,init_32bit(p^.value)));
  116. check_range;
  117. end;
  118. end;
  119. u32bit : begin
  120. if not is_constintnode(p) then
  121. Message(cg_e_illegal_expression)
  122. else
  123. curconstsegment^.concat(new(pai_const,init_32bit(p^.value)));
  124. end;
  125. bool8bit : begin
  126. if not is_constboolnode(p) then
  127. Message(cg_e_illegal_expression);
  128. curconstsegment^.concat(new(pai_const,init_8bit(p^.value)));
  129. end;
  130. bool16bit : begin
  131. if not is_constboolnode(p) then
  132. Message(cg_e_illegal_expression);
  133. curconstsegment^.concat(new(pai_const,init_16bit(p^.value)));
  134. end;
  135. bool32bit : begin
  136. if not is_constboolnode(p) then
  137. Message(cg_e_illegal_expression);
  138. curconstsegment^.concat(new(pai_const,init_32bit(p^.value)));
  139. end;
  140. uchar : begin
  141. if not is_constcharnode(p) then
  142. Message(cg_e_illegal_expression);
  143. curconstsegment^.concat(new(pai_const,init_8bit(p^.value)));
  144. end;
  145. uwidechar : begin
  146. if not is_constcharnode(p) then
  147. Message(cg_e_illegal_expression);
  148. curconstsegment^.concat(new(pai_const,init_16bit(p^.value)));
  149. end;
  150. u16bit,
  151. s16bit : begin
  152. if not is_constintnode(p) then
  153. Message(cg_e_illegal_expression);
  154. curconstsegment^.concat(new(pai_const,init_16bit(p^.value)));
  155. check_range;
  156. end;
  157. s64bit,
  158. u64bit:
  159. begin
  160. if not is_constintnode(p) then
  161. Message(cg_e_illegal_expression)
  162. else
  163. begin
  164. {!!!!! hmmm, we can write yet only consts til 2^32-1 :( (FK) }
  165. curconstsegment^.concat(new(pai_const,init_32bit(p^.value)));
  166. curconstsegment^.concat(new(pai_const,init_32bit(0)));
  167. end;
  168. end;
  169. else
  170. internalerror(3799);
  171. end;
  172. disposetree(p);
  173. end;
  174. floatdef:
  175. begin
  176. p:=comp_expr(true);
  177. do_firstpass(p);
  178. if is_constrealnode(p) then
  179. value:=p^.value_real
  180. else if is_constintnode(p) then
  181. value:=p^.value
  182. else
  183. Message(cg_e_illegal_expression);
  184. case pfloatdef(def)^.typ of
  185. s32real : curconstsegment^.concat(new(pai_real_32bit,init(value)));
  186. s64real : curconstsegment^.concat(new(pai_real_64bit,init(value)));
  187. s80real : curconstsegment^.concat(new(pai_real_80bit,init(value)));
  188. s64comp : curconstsegment^.concat(new(pai_comp_64bit,init(value)));
  189. f32bit : curconstsegment^.concat(new(pai_const,init_32bit(trunc(value*65536))));
  190. else internalerror(18);
  191. end;
  192. disposetree(p);
  193. end;
  194. classrefdef:
  195. begin
  196. p:=comp_expr(true);
  197. do_firstpass(p);
  198. case p^.treetype of
  199. loadvmtn:
  200. begin
  201. if not(pobjectdef(pclassrefdef(p^.resulttype)^.pointertype.def)^.is_related(
  202. pobjectdef(pclassrefdef(def)^.pointertype.def))) then
  203. Message(cg_e_illegal_expression);
  204. curconstsegment^.concat(new(pai_const_symbol,init(newasmsymbol(pobjectdef(
  205. pclassrefdef(p^.resulttype)^.pointertype.def)^.vmt_mangledname))));
  206. end;
  207. niln:
  208. curconstsegment^.concat(new(pai_const,init_32bit(0)));
  209. else Message(cg_e_illegal_expression);
  210. end;
  211. disposetree(p);
  212. end;
  213. pointerdef:
  214. begin
  215. p:=comp_expr(true);
  216. do_firstpass(p);
  217. if (p^.treetype=typeconvn) and
  218. ((p^.left^.treetype=addrn) or (p^.left^.treetype=niln)) and
  219. is_equal(def,p^.resulttype) then
  220. begin
  221. hp:=p^.left;
  222. putnode(p);
  223. p:=hp;
  224. end;
  225. { allows horrible ofs(typeof(TButton)^) code !! }
  226. if (p^.treetype=addrn) and (p^.left^.treetype=derefn) then
  227. begin
  228. hp:=p^.left^.left;
  229. p^.left^.left:=nil;
  230. disposetree(p);
  231. p:=hp;
  232. end;
  233. { nil pointer ? }
  234. if p^.treetype=niln then
  235. curconstsegment^.concat(new(pai_const,init_32bit(0)))
  236. { maybe pchar ? }
  237. else
  238. if is_char(ppointerdef(def)^.pointertype.def) and
  239. (p^.treetype<>addrn) then
  240. begin
  241. getdatalabel(ll);
  242. curconstsegment^.concat(new(pai_const_symbol,init(ll)));
  243. consts^.concat(new(pai_label,init(ll)));
  244. if p^.treetype=stringconstn then
  245. begin
  246. getmem(ca,p^.length+2);
  247. move(p^.value_str^,ca^,p^.length+1);
  248. consts^.concat(new(pai_string,init_length_pchar(ca,p^.length+1)));
  249. end
  250. else
  251. if is_constcharnode(p) then
  252. consts^.concat(new(pai_string,init(char(byte(p^.value))+#0)))
  253. else
  254. Message(cg_e_illegal_expression);
  255. end
  256. else
  257. if p^.treetype=addrn then
  258. begin
  259. hp:=p^.left;
  260. while assigned(hp) and (hp^.treetype in [subscriptn,vecn]) do
  261. hp:=hp^.left;
  262. if (is_equal(ppointerdef(p^.resulttype)^.pointertype.def,ppointerdef(def)^.pointertype.def) or
  263. (is_equal(ppointerdef(p^.resulttype)^.pointertype.def,voiddef)) or
  264. (is_equal(ppointerdef(def)^.pointertype.def,voiddef))) and
  265. (hp^.treetype=loadn) then
  266. begin
  267. do_firstpass(p^.left);
  268. hp:=p^.left;
  269. offset:=0;
  270. while assigned(hp) and (hp^.treetype<>loadn) do
  271. begin
  272. case hp^.treetype of
  273. vecn :
  274. begin
  275. if (hp^.left^.resulttype^.deftype=stringdef) then
  276. begin
  277. { this seems OK for shortstring and ansistrings PM }
  278. { it is wrong for widestrings !! }
  279. len:=1;
  280. base:=0;
  281. end
  282. else if (hp^.left^.resulttype^.deftype=arraydef) then
  283. begin
  284. len:=parraydef(hp^.left^.resulttype)^.elesize;
  285. base:=parraydef(hp^.left^.resulttype)^.lowrange;
  286. end
  287. else
  288. Message(cg_e_illegal_expression);
  289. if is_constintnode(hp^.right) then
  290. inc(offset,len*(get_ordinal_value(hp^.right)-base))
  291. else
  292. Message(cg_e_illegal_expression);
  293. {internalerror(9779);}
  294. end;
  295. subscriptn : inc(offset,hp^.vs^.address)
  296. else
  297. Message(cg_e_illegal_expression);
  298. end;
  299. hp:=hp^.left;
  300. end;
  301. if hp^.symtableentry^.typ=constsym then
  302. Message(type_e_variable_id_expected);
  303. curconstsegment^.concat(new(pai_const_symbol,initname_offset(hp^.symtableentry^.mangledname,offset)));
  304. (*if token=POINT then
  305. begin
  306. offset:=0;
  307. while token=_POINT do
  308. begin
  309. consume(_POINT);
  310. lsym:=pvarsym(precdef(
  311. ppointerdef(p^.resulttype)^.pointertype.def)^.symtable^.search(pattern));
  312. if assigned(sym) then
  313. offset:=offset+lsym^.address
  314. else
  315. begin
  316. Message1(sym_e_illegal_field,pattern);
  317. end;
  318. consume(_ID);
  319. end;
  320. curconstsegment^.concat(new(pai_const_symbol_offset,init(
  321. strpnew(p^.left^.symtableentry^.mangledname),offset)));
  322. end
  323. else
  324. begin
  325. curconstsegment^.concat(new(pai_const,init_symbol(
  326. strpnew(p^.left^.symtableentry^.mangledname))));
  327. end; *)
  328. end
  329. else
  330. Message(cg_e_illegal_expression);
  331. end
  332. else
  333. { allow typeof(Object type)}
  334. if (p^.treetype=inlinen) and
  335. (p^.inlinenumber=in_typeof_x) then
  336. begin
  337. if (p^.left^.treetype=typen) then
  338. begin
  339. curconstsegment^.concat(new(pai_const_symbol,
  340. initname(pobjectdef(p^.left^.resulttype)^.vmt_mangledname)));
  341. end
  342. else
  343. Message(cg_e_illegal_expression);
  344. end
  345. else
  346. Message(cg_e_illegal_expression);
  347. disposetree(p);
  348. end;
  349. setdef:
  350. begin
  351. p:=comp_expr(true);
  352. do_firstpass(p);
  353. if p^.treetype=setconstn then
  354. begin
  355. { we only allow const sets }
  356. if assigned(p^.left) then
  357. Message(cg_e_illegal_expression)
  358. else
  359. begin
  360. {$ifdef i386}
  361. for l:=0 to def^.size-1 do
  362. curconstsegment^.concat(new(pai_const,init_8bit(p^.value_set^[l])));
  363. {$endif}
  364. {$ifdef m68k}
  365. j:=0;
  366. for l:=0 to ((def^.size-1) div 4) do
  367. { HORRIBLE HACK because of endian }
  368. { now use intel endian for constant sets }
  369. begin
  370. curconstsegment^.concat(new(pai_const,init_8bit(p^.value_set^[j+3])));
  371. curconstsegment^.concat(new(pai_const,init_8bit(p^.value_set^[j+2])));
  372. curconstsegment^.concat(new(pai_const,init_8bit(p^.value_set^[j+1])));
  373. curconstsegment^.concat(new(pai_const,init_8bit(p^.value_set^[j])));
  374. Inc(j,4);
  375. end;
  376. {$endif}
  377. end;
  378. end
  379. else
  380. Message(cg_e_illegal_expression);
  381. disposetree(p);
  382. end;
  383. enumdef:
  384. begin
  385. p:=comp_expr(true);
  386. do_firstpass(p);
  387. if p^.treetype=ordconstn then
  388. begin
  389. if is_equal(p^.resulttype,def) then
  390. begin
  391. case p^.resulttype^.size of
  392. 1 : curconstsegment^.concat(new(pai_const,init_8bit(p^.value)));
  393. 2 : curconstsegment^.concat(new(pai_const,init_16bit(p^.value)));
  394. 4 : curconstsegment^.concat(new(pai_const,init_32bit(p^.value)));
  395. end;
  396. end
  397. else
  398. Message(cg_e_illegal_expression);
  399. end
  400. else
  401. Message(cg_e_illegal_expression);
  402. disposetree(p);
  403. end;
  404. stringdef:
  405. begin
  406. p:=comp_expr(true);
  407. do_firstpass(p);
  408. { load strval and strlength of the constant tree }
  409. if p^.treetype=stringconstn then
  410. begin
  411. strlength:=p^.length;
  412. strval:=p^.value_str;
  413. end
  414. else if is_constcharnode(p) then
  415. begin
  416. strval:=pchar(@p^.value);
  417. strlength:=1
  418. end
  419. else if is_constresourcestringnode(p) then
  420. begin
  421. strval:=pchar(tpointerord(pconstsym(p^.symtableentry)^.value));
  422. strlength:=pconstsym(p^.symtableentry)^.len;
  423. end
  424. else
  425. begin
  426. Message(cg_e_illegal_expression);
  427. strlength:=-1;
  428. end;
  429. if strlength>=0 then
  430. begin
  431. case pstringdef(def)^.string_typ of
  432. st_shortstring:
  433. begin
  434. if strlength>=def^.size then
  435. begin
  436. message2(parser_w_string_too_long,strpas(strval),tostr(def^.size-1));
  437. strlength:=def^.size-1;
  438. end;
  439. curconstsegment^.concat(new(pai_const,init_8bit(strlength)));
  440. { this can also handle longer strings }
  441. getmem(ca,strlength+1);
  442. move(strval^,ca^,strlength);
  443. ca[strlength]:=#0;
  444. curconstsegment^.concat(new(pai_string,init_length_pchar(ca,strlength)));
  445. { fillup with spaces if size is shorter }
  446. if def^.size>strlength then
  447. begin
  448. getmem(ca,def^.size-strlength);
  449. { def^.size contains also the leading length, so we }
  450. { we have to subtract one }
  451. fillchar(ca[0],def^.size-strlength-1,' ');
  452. ca[def^.size-strlength-1]:=#0;
  453. { this can also handle longer strings }
  454. curconstsegment^.concat(new(pai_string,init_length_pchar(ca,def^.size-strlength-1)));
  455. end;
  456. end;
  457. {$ifdef UseLongString}
  458. st_longstring:
  459. begin
  460. { first write the maximum size }
  461. curconstsegment^.concat(new(pai_const,init_32bit(strlength)))));
  462. { fill byte }
  463. curconstsegment^.concat(new(pai_const,init_8bit(0)));
  464. getmem(ca,strlength+1);
  465. move(strval^,ca^,strlength);
  466. ca[strlength]:=#0;
  467. generate_pascii(consts,ca,strlength);
  468. curconstsegment^.concat(new(pai_const,init_8bit(0)));
  469. end;
  470. {$endif UseLongString}
  471. st_ansistring:
  472. begin
  473. { an empty ansi string is nil! }
  474. if (strlength=0) then
  475. curconstsegment^.concat(new(pai_const,init_32bit(0)))
  476. else
  477. begin
  478. getdatalabel(ll);
  479. curconstsegment^.concat(new(pai_const_symbol,init(ll)));
  480. { first write the maximum size }
  481. consts^.concat(new(pai_const,init_32bit(strlength)));
  482. { second write the real length }
  483. consts^.concat(new(pai_const,init_32bit(strlength)));
  484. { redondent with maxlength but who knows ... (PM) }
  485. { third write use count (set to -1 for safety ) }
  486. consts^.concat(new(pai_const,init_32bit(-1)));
  487. consts^.concat(new(pai_label,init(ll)));
  488. getmem(ca,strlength+2);
  489. move(strval^,ca^,strlength);
  490. { The terminating #0 to be stored in the .data section (JM) }
  491. ca[strlength]:=#0;
  492. { End of the PChar. The memory has to be allocated because in }
  493. { tai_string.done, there is a freemem(len+1) (JM) }
  494. ca[strlength+1]:=#0;
  495. consts^.concat(new(pai_string,init_length_pchar(ca,strlength+1)));
  496. end;
  497. end;
  498. end;
  499. end;
  500. disposetree(p);
  501. end;
  502. arraydef:
  503. begin
  504. if token=_LKLAMMER then
  505. begin
  506. consume(_LKLAMMER);
  507. for l:=parraydef(def)^.lowrange to parraydef(def)^.highrange-1 do
  508. begin
  509. readtypedconst(parraydef(def)^.elementtype.def,nil,no_change_allowed);
  510. consume(_COMMA);
  511. end;
  512. readtypedconst(parraydef(def)^.elementtype.def,nil,no_change_allowed);
  513. consume(_RKLAMMER);
  514. end
  515. else
  516. { if array of char then we allow also a string }
  517. if is_char(parraydef(def)^.elementtype.def) then
  518. begin
  519. p:=comp_expr(true);
  520. do_firstpass(p);
  521. if p^.treetype=stringconstn then
  522. begin
  523. if p^.length>255 then
  524. len:=255
  525. else
  526. len:=p^.length;
  527. s[0]:=chr(len);
  528. move(p^.value_str^,s[1],len);
  529. end
  530. else
  531. if is_constcharnode(p) then
  532. s:=char(byte(p^.value))
  533. else
  534. begin
  535. Message(cg_e_illegal_expression);
  536. s:='';
  537. end;
  538. disposetree(p);
  539. l:=length(s);
  540. for i:=Parraydef(def)^.lowrange to Parraydef(def)^.highrange do
  541. begin
  542. if i+1-Parraydef(def)^.lowrange<=l then
  543. begin
  544. curconstsegment^.concat(new(pai_const,init_8bit(byte(s[1]))));
  545. delete(s,1,1);
  546. end
  547. else
  548. {Fill the remaining positions with #0.}
  549. curconstsegment^.concat(new(pai_const,init_8bit(0)));
  550. end;
  551. if length(s)>0 then
  552. Message(parser_e_string_larger_array);
  553. end
  554. else
  555. begin
  556. { we want the ( }
  557. consume(_LKLAMMER);
  558. end;
  559. end;
  560. procvardef:
  561. begin
  562. { Procvars and pointers are no longer compatible. }
  563. { under tp: =nil or =var under fpc: =nil or =@var }
  564. if token=_NIL then
  565. begin
  566. curconstsegment^.concat(new(pai_const,init_32bit(0)));
  567. consume(_NIL);
  568. exit;
  569. end
  570. else
  571. if not(m_tp_procvar in aktmodeswitches) then
  572. if token=_KLAMMERAFFE then
  573. consume(_KLAMMERAFFE);
  574. getprocvar:=true;
  575. getprocvardef:=pprocvardef(def);
  576. p:=comp_expr(true);
  577. getprocvar:=false;
  578. do_firstpass(p);
  579. if codegenerror then
  580. begin
  581. disposetree(p);
  582. exit;
  583. end;
  584. { convert calln to loadn }
  585. if p^.treetype=calln then
  586. begin
  587. if (p^.symtableprocentry^.owner^.symtabletype=objectsymtable) and
  588. (pobjectdef(p^.symtableprocentry^.owner^.defowner)^.is_class) then
  589. hp:=genloadmethodcallnode(pprocsym(p^.symtableprocentry),p^.symtableproc,
  590. getcopy(p^.methodpointer))
  591. else
  592. hp:=genloadcallnode(pprocsym(p^.symtableprocentry),p^.symtableproc);
  593. disposetree(p);
  594. do_firstpass(hp);
  595. p:=hp;
  596. if codegenerror then
  597. begin
  598. disposetree(p);
  599. exit;
  600. end;
  601. end
  602. else if (p^.treetype=addrn) and assigned(p^.left) and
  603. (p^.left^.treetype=calln) then
  604. begin
  605. if (p^.left^.symtableprocentry^.owner^.symtabletype=objectsymtable) and
  606. (pobjectdef(p^.left^.symtableprocentry^.owner^.defowner)^.is_class) then
  607. hp:=genloadmethodcallnode(pprocsym(p^.left^.symtableprocentry),
  608. p^.left^.symtableproc,getcopy(p^.left^.methodpointer))
  609. else
  610. hp:=genloadcallnode(pprocsym(p^.left^.symtableprocentry),
  611. p^.left^.symtableproc);
  612. disposetree(p);
  613. do_firstpass(hp);
  614. p:=hp;
  615. if codegenerror then
  616. begin
  617. disposetree(p);
  618. exit;
  619. end;
  620. end;
  621. { let type conversion check everything needed }
  622. p:=gentypeconvnode(p,def);
  623. do_firstpass(p);
  624. if codegenerror then
  625. begin
  626. disposetree(p);
  627. exit;
  628. end;
  629. { remove typeconvn, that will normally insert a lea
  630. instruction which is not necessary for us }
  631. if p^.treetype=typeconvn then
  632. begin
  633. hp:=p^.left;
  634. putnode(p);
  635. p:=hp;
  636. end;
  637. { remove addrn which we also don't need here }
  638. if p^.treetype=addrn then
  639. begin
  640. hp:=p^.left;
  641. putnode(p);
  642. p:=hp;
  643. end;
  644. { we now need to have a loadn with a procsym }
  645. if (p^.treetype=loadn) and
  646. (p^.symtableentry^.typ=procsym) then
  647. begin
  648. curconstsegment^.concat(new(pai_const_symbol,
  649. initname(pprocsym(p^.symtableentry)^.definition^.mangledname)));
  650. end
  651. else
  652. Message(cg_e_illegal_expression);
  653. disposetree(p);
  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.7 2000-09-24 15:06:25 peter
  778. * use defines.inc
  779. Revision 1.6 2000/08/27 16:11:52 peter
  780. * moved some util functions from globals,cobjects to cutils
  781. * splitted files into finput,fmodule
  782. Revision 1.5 2000/08/24 19:13:18 peter
  783. * allow nil for class typed consts (merged)
  784. Revision 1.4 2000/08/16 13:06:06 florian
  785. + support of 64 bit integer constants
  786. Revision 1.3 2000/08/05 13:25:06 peter
  787. * packenum 1 fixes (merged)
  788. Revision 1.2 2000/07/13 11:32:47 michael
  789. + removed logs
  790. }