ptconst.pas 35 KB

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