ptconst.pas 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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,
  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. curconstsegment^.concat(new(pai_const,init_32bit(p^.value)))
  390. else
  391. Message(cg_e_illegal_expression);
  392. end
  393. else
  394. Message(cg_e_illegal_expression);
  395. disposetree(p);
  396. end;
  397. stringdef:
  398. begin
  399. p:=comp_expr(true);
  400. do_firstpass(p);
  401. { load strval and strlength of the constant tree }
  402. if p^.treetype=stringconstn then
  403. begin
  404. strlength:=p^.length;
  405. strval:=p^.value_str;
  406. end
  407. else if is_constcharnode(p) then
  408. begin
  409. strval:=pchar(@p^.value);
  410. strlength:=1
  411. end
  412. else if is_constresourcestringnode(p) then
  413. begin
  414. strval:=pchar(pconstsym(p^.symtableentry)^.value);
  415. strlength:=pconstsym(p^.symtableentry)^.len;
  416. end
  417. else
  418. begin
  419. Message(cg_e_illegal_expression);
  420. strlength:=-1;
  421. end;
  422. if strlength>=0 then
  423. begin
  424. case pstringdef(def)^.string_typ of
  425. st_shortstring:
  426. begin
  427. if strlength>=def^.size then
  428. begin
  429. message2(parser_w_string_too_long,strpas(strval),tostr(def^.size-1));
  430. strlength:=def^.size-1;
  431. end;
  432. curconstsegment^.concat(new(pai_const,init_8bit(strlength)));
  433. { this can also handle longer strings }
  434. getmem(ca,strlength+1);
  435. move(strval^,ca^,strlength);
  436. ca[strlength]:=#0;
  437. curconstsegment^.concat(new(pai_string,init_length_pchar(ca,strlength)));
  438. { fillup with spaces if size is shorter }
  439. if def^.size>strlength then
  440. begin
  441. getmem(ca,def^.size-strlength);
  442. { def^.size contains also the leading length, so we }
  443. { we have to subtract one }
  444. fillchar(ca[0],def^.size-strlength-1,' ');
  445. ca[def^.size-strlength-1]:=#0;
  446. { this can also handle longer strings }
  447. curconstsegment^.concat(new(pai_string,init_length_pchar(ca,def^.size-strlength-1)));
  448. end;
  449. end;
  450. {$ifdef UseLongString}
  451. st_longstring:
  452. begin
  453. { first write the maximum size }
  454. curconstsegment^.concat(new(pai_const,init_32bit(strlength)))));
  455. { fill byte }
  456. curconstsegment^.concat(new(pai_const,init_8bit(0)));
  457. getmem(ca,strlength+1);
  458. move(strval^,ca^,strlength);
  459. ca[strlength]:=#0;
  460. generate_pascii(consts,ca,strlength);
  461. curconstsegment^.concat(new(pai_const,init_8bit(0)));
  462. end;
  463. {$endif UseLongString}
  464. st_ansistring:
  465. begin
  466. { an empty ansi string is nil! }
  467. if (strlength=0) then
  468. curconstsegment^.concat(new(pai_const,init_32bit(0)))
  469. else
  470. begin
  471. getdatalabel(ll);
  472. curconstsegment^.concat(new(pai_const_symbol,init(ll)));
  473. { first write the maximum size }
  474. consts^.concat(new(pai_const,init_32bit(strlength)));
  475. { second write the real length }
  476. consts^.concat(new(pai_const,init_32bit(strlength)));
  477. { redondent with maxlength but who knows ... (PM) }
  478. { third write use count (set to -1 for safety ) }
  479. consts^.concat(new(pai_const,init_32bit(-1)));
  480. consts^.concat(new(pai_label,init(ll)));
  481. getmem(ca,strlength+2);
  482. move(strval^,ca^,strlength);
  483. { The terminating #0 to be stored in the .data section (JM) }
  484. ca[strlength]:=#0;
  485. { End of the PChar. The memory has to be allocated because in }
  486. { tai_string.done, there is a freemem(len+1) (JM) }
  487. ca[strlength+1]:=#0;
  488. consts^.concat(new(pai_string,init_length_pchar(ca,strlength+1)));
  489. end;
  490. end;
  491. end;
  492. end;
  493. disposetree(p);
  494. end;
  495. arraydef:
  496. begin
  497. if token=_LKLAMMER then
  498. begin
  499. consume(_LKLAMMER);
  500. for l:=parraydef(def)^.lowrange to parraydef(def)^.highrange-1 do
  501. begin
  502. readtypedconst(parraydef(def)^.elementtype.def,nil,no_change_allowed);
  503. consume(_COMMA);
  504. end;
  505. readtypedconst(parraydef(def)^.elementtype.def,nil,no_change_allowed);
  506. consume(_RKLAMMER);
  507. end
  508. else
  509. { if array of char then we allow also a string }
  510. if is_char(parraydef(def)^.elementtype.def) then
  511. begin
  512. p:=comp_expr(true);
  513. do_firstpass(p);
  514. if p^.treetype=stringconstn then
  515. begin
  516. if p^.length>255 then
  517. len:=255
  518. else
  519. len:=p^.length;
  520. {$ifndef TP}
  521. {$ifopt H+}
  522. setlength(s,len);
  523. {$else}
  524. s[0]:=chr(len);
  525. {$endif}
  526. {$else}
  527. s[0]:=chr(len);
  528. {$endif}
  529. move(p^.value_str^,s[1],len);
  530. end
  531. else
  532. if is_constcharnode(p) then
  533. s:=char(byte(p^.value))
  534. else
  535. begin
  536. Message(cg_e_illegal_expression);
  537. s:='';
  538. end;
  539. disposetree(p);
  540. l:=length(s);
  541. for i:=Parraydef(def)^.lowrange to Parraydef(def)^.highrange do
  542. begin
  543. if i+1-Parraydef(def)^.lowrange<=l then
  544. begin
  545. curconstsegment^.concat(new(pai_const,init_8bit(byte(s[1]))));
  546. delete(s,1,1);
  547. end
  548. else
  549. {Fill the remaining positions with #0.}
  550. curconstsegment^.concat(new(pai_const,init_8bit(0)));
  551. end;
  552. if length(s)>0 then
  553. Message(parser_e_string_larger_array);
  554. end
  555. else
  556. begin
  557. { we want the ( }
  558. consume(_LKLAMMER);
  559. end;
  560. end;
  561. procvardef:
  562. begin
  563. { Procvars and pointers are no longer compatible. }
  564. { under tp: =nil or =var under fpc: =nil or =@var }
  565. if token=_NIL then
  566. begin
  567. curconstsegment^.concat(new(pai_const,init_32bit(0)));
  568. consume(_NIL);
  569. exit;
  570. end
  571. else
  572. if not(m_tp_procvar in aktmodeswitches) then
  573. if token=_KLAMMERAFFE then
  574. consume(_KLAMMERAFFE);
  575. getprocvar:=true;
  576. getprocvardef:=pprocvardef(def);
  577. p:=comp_expr(true);
  578. getprocvar:=false;
  579. do_firstpass(p);
  580. if codegenerror then
  581. begin
  582. disposetree(p);
  583. exit;
  584. end;
  585. { convert calln to loadn }
  586. if p^.treetype=calln then
  587. begin
  588. if (p^.symtableprocentry^.owner^.symtabletype=objectsymtable) and
  589. (pobjectdef(p^.symtableprocentry^.owner^.defowner)^.is_class) then
  590. hp:=genloadmethodcallnode(pprocsym(p^.symtableprocentry),p^.symtableproc,
  591. getcopy(p^.methodpointer))
  592. else
  593. hp:=genloadcallnode(pprocsym(p^.symtableprocentry),p^.symtableproc);
  594. disposetree(p);
  595. do_firstpass(hp);
  596. p:=hp;
  597. if codegenerror then
  598. begin
  599. disposetree(p);
  600. exit;
  601. end;
  602. end
  603. else if (p^.treetype=addrn) and assigned(p^.left) and
  604. (p^.left^.treetype=calln) then
  605. begin
  606. if (p^.left^.symtableprocentry^.owner^.symtabletype=objectsymtable) and
  607. (pobjectdef(p^.left^.symtableprocentry^.owner^.defowner)^.is_class) then
  608. hp:=genloadmethodcallnode(pprocsym(p^.left^.symtableprocentry),
  609. p^.left^.symtableproc,getcopy(p^.left^.methodpointer))
  610. else
  611. hp:=genloadcallnode(pprocsym(p^.left^.symtableprocentry),
  612. p^.left^.symtableproc);
  613. disposetree(p);
  614. do_firstpass(hp);
  615. p:=hp;
  616. if codegenerror then
  617. begin
  618. disposetree(p);
  619. exit;
  620. end;
  621. end;
  622. { let type conversion check everything needed }
  623. p:=gentypeconvnode(p,def);
  624. do_firstpass(p);
  625. if codegenerror then
  626. begin
  627. disposetree(p);
  628. exit;
  629. end;
  630. { remove typeconvn, that will normally insert a lea
  631. instruction which is not necessary for us }
  632. if p^.treetype=typeconvn then
  633. begin
  634. hp:=p^.left;
  635. putnode(p);
  636. p:=hp;
  637. end;
  638. { remove addrn which we also don't need here }
  639. if p^.treetype=addrn then
  640. begin
  641. hp:=p^.left;
  642. putnode(p);
  643. p:=hp;
  644. end;
  645. { we now need to have a loadn with a procsym }
  646. if (p^.treetype=loadn) and
  647. (p^.symtableentry^.typ=procsym) then
  648. begin
  649. curconstsegment^.concat(new(pai_const_symbol,
  650. initname(pprocsym(p^.symtableentry)^.definition^.mangledname)));
  651. end
  652. else
  653. Message(cg_e_illegal_expression);
  654. disposetree(p);
  655. end;
  656. { reads a typed constant record }
  657. recorddef:
  658. begin
  659. consume(_LKLAMMER);
  660. aktpos:=0;
  661. while token<>_RKLAMMER do
  662. begin
  663. s:=pattern;
  664. consume(_ID);
  665. consume(_COLON);
  666. srsym:=precorddef(def)^.symtable^.search(s);
  667. if srsym=nil then
  668. begin
  669. Message1(sym_e_id_not_found,s);
  670. consume_all_until(_SEMICOLON);
  671. end
  672. else
  673. begin
  674. { check position }
  675. if pvarsym(srsym)^.address<aktpos then
  676. Message(parser_e_invalid_record_const);
  677. { if needed fill }
  678. if pvarsym(srsym)^.address>aktpos then
  679. for i:=1 to pvarsym(srsym)^.address-aktpos do
  680. curconstsegment^.concat(new(pai_const,init_8bit(0)));
  681. { new position }
  682. aktpos:=pvarsym(srsym)^.address+pvarsym(srsym)^.vartype.def^.size;
  683. { read the data }
  684. readtypedconst(pvarsym(srsym)^.vartype.def,nil,no_change_allowed);
  685. if token=_SEMICOLON then
  686. consume(_SEMICOLON)
  687. else break;
  688. end;
  689. end;
  690. for i:=1 to def^.size-aktpos do
  691. curconstsegment^.concat(new(pai_const,init_8bit(0)));
  692. consume(_RKLAMMER);
  693. end;
  694. { reads a typed object }
  695. objectdef:
  696. begin
  697. if ([oo_has_vmt,oo_is_class]*pobjectdef(def)^.objectoptions)<>[] then
  698. begin
  699. Message(parser_e_type_const_not_possible);
  700. consume_all_until(_RKLAMMER);
  701. end
  702. else
  703. begin
  704. consume(_LKLAMMER);
  705. aktpos:=0;
  706. while token<>_RKLAMMER do
  707. begin
  708. s:=pattern;
  709. consume(_ID);
  710. consume(_COLON);
  711. srsym:=nil;
  712. obj:=pobjectdef(def);
  713. symt:=obj^.symtable;
  714. while (srsym=nil) and assigned(symt) do
  715. begin
  716. srsym:=symt^.search(s);
  717. if assigned(obj) then
  718. obj:=obj^.childof;
  719. if assigned(obj) then
  720. symt:=obj^.symtable
  721. else
  722. symt:=nil;
  723. end;
  724. if srsym=nil then
  725. begin
  726. Message1(sym_e_id_not_found,s);
  727. consume_all_until(_SEMICOLON);
  728. end
  729. else
  730. begin
  731. { check position }
  732. if pvarsym(srsym)^.address<aktpos then
  733. Message(parser_e_invalid_record_const);
  734. { if needed fill }
  735. if pvarsym(srsym)^.address>aktpos then
  736. for i:=1 to pvarsym(srsym)^.address-aktpos do
  737. curconstsegment^.concat(new(pai_const,init_8bit(0)));
  738. { new position }
  739. aktpos:=pvarsym(srsym)^.address+pvarsym(srsym)^.vartype.def^.size;
  740. { read the data }
  741. readtypedconst(pvarsym(srsym)^.vartype.def,nil,no_change_allowed);
  742. if token=_SEMICOLON then
  743. consume(_SEMICOLON)
  744. else break;
  745. end;
  746. end;
  747. for i:=1 to def^.size-aktpos do
  748. curconstsegment^.concat(new(pai_const,init_8bit(0)));
  749. consume(_RKLAMMER);
  750. end;
  751. end;
  752. errordef:
  753. begin
  754. { try to consume something useful }
  755. if token=_LKLAMMER then
  756. consume_all_until(_RKLAMMER)
  757. else
  758. consume_all_until(_SEMICOLON);
  759. end;
  760. else Message(parser_e_type_const_not_possible);
  761. end;
  762. end;
  763. {$ifdef fpc}
  764. {$maxfpuregisters default}
  765. {$endif fpc}
  766. end.
  767. {
  768. $Log$
  769. Revision 1.68 2000-06-06 13:06:17 jonas
  770. * ansistring constants now also get a trailing #0 (bug reported by
  771. Thomas Schatzl)
  772. Revision 1.67 2000/05/17 17:10:06 peter
  773. * add support for loading of typed const strings with resourcestrings,
  774. made the loading also a bit more generic
  775. Revision 1.66 2000/05/12 06:02:01 pierre
  776. * * get it to compile with Delphi by Kovacs Attila Zoltan
  777. Revision 1.65 2000/05/11 09:15:15 pierre
  778. + add a warning if a const string is longer than the
  779. length of the string type
  780. Revision 1.64 2000/04/02 09:12:51 florian
  781. + constant procedure variables can have a @ in front:
  782. const p : procedure = @p;
  783. til now only
  784. const p : procedure = p;
  785. was allowed
  786. Revision 1.63 2000/02/13 14:21:51 jonas
  787. * modifications to make the compiler functional when compiled with
  788. -Or
  789. Revision 1.62 2000/02/09 13:23:01 peter
  790. * log truncated
  791. Revision 1.61 2000/01/07 01:14:33 peter
  792. * updated copyright to 2000
  793. Revision 1.60 1999/12/18 14:55:21 florian
  794. * very basic widestring support
  795. Revision 1.59 1999/11/30 10:40:51 peter
  796. + ttype, tsymlist
  797. Revision 1.58 1999/11/08 18:50:11 florian
  798. * disposetree for classrefdef added
  799. Revision 1.57 1999/11/08 16:24:28 pierre
  800. * missing disposetree added to avoid memory loss
  801. Revision 1.56 1999/11/08 14:02:16 florian
  802. * problem with "index X"-properties solved
  803. * typed constants of class references are now allowed
  804. Revision 1.55 1999/11/06 14:34:23 peter
  805. * truncated log to 20 revs
  806. Revision 1.54 1999/10/14 14:57:54 florian
  807. - removed the hcodegen use in the new cg, use cgbase instead
  808. Revision 1.53 1999/09/26 21:30:20 peter
  809. + constant pointer support which can happend with typecasting like
  810. const p=pointer(1)
  811. * better procvar parsing in typed consts
  812. Revision 1.52 1999/08/10 12:30:02 pierre
  813. * avoid unused locals
  814. Revision 1.51 1999/08/04 13:03:02 jonas
  815. * all tokens now start with an underscore
  816. * PowerPC compiles!!
  817. Revision 1.50 1999/08/04 00:23:21 florian
  818. * renamed i386asm and i386base to cpuasm and cpubase
  819. Revision 1.49 1999/08/03 22:03:08 peter
  820. * moved bitmask constants to sets
  821. * some other type/const renamings
  822. Revision 1.48 1999/07/23 16:05:26 peter
  823. * alignment is now saved in the symtable
  824. * C alignment added for records
  825. * PPU version increased to solve .12 <-> .13 probs
  826. }