ptconst.pas 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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. globtype,systems,tokens,
  28. cobjects,globals,scanner,
  29. symconst,aasm,types,verbose,
  30. tree,pass_1,
  31. { parser specific stuff }
  32. pbase,pexpr,
  33. { processor specific stuff }
  34. cpubase,
  35. { codegen }
  36. {$ifdef newcg}
  37. cgbase,
  38. {$else}
  39. hcodegen,
  40. {$endif}
  41. hcgdata;
  42. {$ifdef fpc}
  43. {$maxfpuregisters 0}
  44. {$endif fpc}
  45. { this procedure reads typed constants }
  46. procedure readtypedconst(def : pdef;sym : ptypedconstsym;no_change_allowed : boolean);
  47. var
  48. {$ifdef m68k}
  49. j : longint;
  50. {$endif m68k}
  51. len,base : longint;
  52. p,hp : ptree;
  53. i,l,offset,
  54. strlength : longint;
  55. curconstsegment : paasmoutput;
  56. ll : pasmlabel;
  57. s : string;
  58. ca : pchar;
  59. aktpos : longint;
  60. obj : pobjectdef;
  61. symt : psymtable;
  62. value : bestreal;
  63. procedure check_range;
  64. begin
  65. if ((p^.value>porddef(def)^.high) or
  66. (p^.value<porddef(def)^.low)) then
  67. begin
  68. if (cs_check_range in aktlocalswitches) then
  69. Message(parser_e_range_check_error)
  70. else
  71. Message(parser_w_range_check_error);
  72. end;
  73. end;
  74. (* function is_po_equal(o1,o2:longint):boolean;
  75. begin
  76. { assembler does not affect }
  77. is_po_equal:=(o1 and not(poassembler))=
  78. (o2 and not(poassembler));
  79. end; *)
  80. {$R-} {Range check creates problem with init_8bit(-1) !!}
  81. begin
  82. if no_change_allowed then
  83. curconstsegment:=consts
  84. else
  85. curconstsegment:=datasegment;
  86. case def^.deftype of
  87. orddef:
  88. begin
  89. p:=comp_expr(true);
  90. do_firstpass(p);
  91. case porddef(def)^.typ of
  92. s8bit,
  93. u8bit : begin
  94. if not is_constintnode(p) then
  95. { is't an int expected }
  96. Message(cg_e_illegal_expression)
  97. else
  98. begin
  99. curconstsegment^.concat(new(pai_const,init_8bit(p^.value)));
  100. check_range;
  101. end;
  102. end;
  103. s32bit : begin
  104. if not is_constintnode(p) then
  105. Message(cg_e_illegal_expression)
  106. else
  107. begin
  108. curconstsegment^.concat(new(pai_const,init_32bit(p^.value)));
  109. check_range;
  110. end;
  111. end;
  112. u32bit : begin
  113. if not is_constintnode(p) then
  114. Message(cg_e_illegal_expression)
  115. else
  116. curconstsegment^.concat(new(pai_const,init_32bit(p^.value)));
  117. end;
  118. bool8bit : begin
  119. if not is_constboolnode(p) then
  120. Message(cg_e_illegal_expression);
  121. curconstsegment^.concat(new(pai_const,init_8bit(p^.value)));
  122. end;
  123. bool16bit : begin
  124. if not is_constboolnode(p) then
  125. Message(cg_e_illegal_expression);
  126. curconstsegment^.concat(new(pai_const,init_16bit(p^.value)));
  127. end;
  128. bool32bit : begin
  129. if not is_constboolnode(p) then
  130. Message(cg_e_illegal_expression);
  131. curconstsegment^.concat(new(pai_const,init_32bit(p^.value)));
  132. end;
  133. uchar : begin
  134. if not is_constcharnode(p) then
  135. Message(cg_e_illegal_expression);
  136. curconstsegment^.concat(new(pai_const,init_8bit(p^.value)));
  137. end;
  138. uwidechar : begin
  139. if not is_constcharnode(p) then
  140. Message(cg_e_illegal_expression);
  141. curconstsegment^.concat(new(pai_const,init_16bit(p^.value)));
  142. end;
  143. u16bit,
  144. s16bit : begin
  145. if not is_constintnode(p) then
  146. Message(cg_e_illegal_expression);
  147. curconstsegment^.concat(new(pai_const,init_16bit(p^.value)));
  148. check_range;
  149. end;
  150. s64bit,
  151. u64bit:
  152. begin
  153. if not is_constintnode(p) then
  154. Message(cg_e_illegal_expression)
  155. else
  156. begin
  157. {!!!!! hmmm, we can write yet only consts til 2^32-1 :( (FK) }
  158. curconstsegment^.concat(new(pai_const,init_32bit(p^.value)));
  159. curconstsegment^.concat(new(pai_const,init_32bit(0)));
  160. end;
  161. end;
  162. else
  163. internalerror(3799);
  164. end;
  165. disposetree(p);
  166. end;
  167. floatdef:
  168. begin
  169. p:=comp_expr(true);
  170. do_firstpass(p);
  171. if is_constrealnode(p) then
  172. value:=p^.value_real
  173. else if is_constintnode(p) then
  174. value:=p^.value
  175. else
  176. Message(cg_e_illegal_expression);
  177. case pfloatdef(def)^.typ of
  178. s32real : curconstsegment^.concat(new(pai_real_32bit,init(value)));
  179. s64real : curconstsegment^.concat(new(pai_real_64bit,init(value)));
  180. s80real : curconstsegment^.concat(new(pai_real_80bit,init(value)));
  181. s64comp : curconstsegment^.concat(new(pai_comp_64bit,init(value)));
  182. f32bit : curconstsegment^.concat(new(pai_const,init_32bit(trunc(value*65536))));
  183. else internalerror(18);
  184. end;
  185. disposetree(p);
  186. end;
  187. classrefdef:
  188. begin
  189. p:=comp_expr(true);
  190. do_firstpass(p);
  191. case p^.treetype of
  192. loadvmtn:
  193. begin
  194. if not(pobjectdef(pclassrefdef(p^.resulttype)^.pointertype.def)^.is_related(
  195. pobjectdef(pclassrefdef(def)^.pointertype.def))) then
  196. Message(cg_e_illegal_expression);
  197. curconstsegment^.concat(new(pai_const_symbol,init(newasmsymbol(pobjectdef(
  198. pclassrefdef(p^.resulttype)^.pointertype.def)^.vmt_mangledname))));
  199. end;
  200. niln:
  201. curconstsegment^.concat(new(pai_const,init_32bit(0)));
  202. else Message(cg_e_illegal_expression);
  203. end;
  204. disposetree(p);
  205. end;
  206. pointerdef:
  207. begin
  208. p:=comp_expr(true);
  209. do_firstpass(p);
  210. if (p^.treetype=typeconvn) and
  211. ((p^.left^.treetype=addrn) or (p^.left^.treetype=niln)) and
  212. is_equal(def,p^.resulttype) then
  213. begin
  214. hp:=p^.left;
  215. putnode(p);
  216. p:=hp;
  217. end;
  218. { allows horrible ofs(typeof(TButton)^) code !! }
  219. if (p^.treetype=addrn) and (p^.left^.treetype=derefn) then
  220. begin
  221. hp:=p^.left^.left;
  222. p^.left^.left:=nil;
  223. disposetree(p);
  224. p:=hp;
  225. end;
  226. { nil pointer ? }
  227. if p^.treetype=niln then
  228. curconstsegment^.concat(new(pai_const,init_32bit(0)))
  229. { maybe pchar ? }
  230. else
  231. if is_char(ppointerdef(def)^.pointertype.def) and
  232. (p^.treetype<>addrn) then
  233. begin
  234. getdatalabel(ll);
  235. curconstsegment^.concat(new(pai_const_symbol,init(ll)));
  236. consts^.concat(new(pai_label,init(ll)));
  237. if p^.treetype=stringconstn then
  238. begin
  239. getmem(ca,p^.length+2);
  240. move(p^.value_str^,ca^,p^.length+1);
  241. consts^.concat(new(pai_string,init_length_pchar(ca,p^.length+1)));
  242. end
  243. else
  244. if is_constcharnode(p) then
  245. consts^.concat(new(pai_string,init(char(byte(p^.value))+#0)))
  246. else
  247. Message(cg_e_illegal_expression);
  248. end
  249. else
  250. if p^.treetype=addrn then
  251. begin
  252. hp:=p^.left;
  253. while assigned(hp) and (hp^.treetype in [subscriptn,vecn]) do
  254. hp:=hp^.left;
  255. if (is_equal(ppointerdef(p^.resulttype)^.pointertype.def,ppointerdef(def)^.pointertype.def) or
  256. (is_equal(ppointerdef(p^.resulttype)^.pointertype.def,voiddef)) or
  257. (is_equal(ppointerdef(def)^.pointertype.def,voiddef))) and
  258. (hp^.treetype=loadn) then
  259. begin
  260. do_firstpass(p^.left);
  261. hp:=p^.left;
  262. offset:=0;
  263. while assigned(hp) and (hp^.treetype<>loadn) do
  264. begin
  265. case hp^.treetype of
  266. vecn :
  267. begin
  268. if (hp^.left^.resulttype^.deftype=stringdef) then
  269. begin
  270. { this seems OK for shortstring and ansistrings PM }
  271. { it is wrong for widestrings !! }
  272. len:=1;
  273. base:=0;
  274. end
  275. else if (hp^.left^.resulttype^.deftype=arraydef) then
  276. begin
  277. len:=parraydef(hp^.left^.resulttype)^.elesize;
  278. base:=parraydef(hp^.left^.resulttype)^.lowrange;
  279. end
  280. else
  281. Message(cg_e_illegal_expression);
  282. if is_constintnode(hp^.right) then
  283. inc(offset,len*(get_ordinal_value(hp^.right)-base))
  284. else
  285. Message(cg_e_illegal_expression);
  286. {internalerror(9779);}
  287. end;
  288. subscriptn : inc(offset,hp^.vs^.address)
  289. else
  290. Message(cg_e_illegal_expression);
  291. end;
  292. hp:=hp^.left;
  293. end;
  294. if hp^.symtableentry^.typ=constsym then
  295. Message(type_e_variable_id_expected);
  296. curconstsegment^.concat(new(pai_const_symbol,initname_offset(hp^.symtableentry^.mangledname,offset)));
  297. (*if token=POINT then
  298. begin
  299. offset:=0;
  300. while token=_POINT do
  301. begin
  302. consume(_POINT);
  303. lsym:=pvarsym(precdef(
  304. ppointerdef(p^.resulttype)^.pointertype.def)^.symtable^.search(pattern));
  305. if assigned(sym) then
  306. offset:=offset+lsym^.address
  307. else
  308. begin
  309. Message1(sym_e_illegal_field,pattern);
  310. end;
  311. consume(_ID);
  312. end;
  313. curconstsegment^.concat(new(pai_const_symbol_offset,init(
  314. strpnew(p^.left^.symtableentry^.mangledname),offset)));
  315. end
  316. else
  317. begin
  318. curconstsegment^.concat(new(pai_const,init_symbol(
  319. strpnew(p^.left^.symtableentry^.mangledname))));
  320. end; *)
  321. end
  322. else
  323. Message(cg_e_illegal_expression);
  324. end
  325. else
  326. { allow typeof(Object type)}
  327. if (p^.treetype=inlinen) and
  328. (p^.inlinenumber=in_typeof_x) then
  329. begin
  330. if (p^.left^.treetype=typen) then
  331. begin
  332. curconstsegment^.concat(new(pai_const_symbol,
  333. initname(pobjectdef(p^.left^.resulttype)^.vmt_mangledname)));
  334. end
  335. else
  336. Message(cg_e_illegal_expression);
  337. end
  338. else
  339. Message(cg_e_illegal_expression);
  340. disposetree(p);
  341. end;
  342. setdef:
  343. begin
  344. p:=comp_expr(true);
  345. do_firstpass(p);
  346. if p^.treetype=setconstn then
  347. begin
  348. { we only allow const sets }
  349. if assigned(p^.left) then
  350. Message(cg_e_illegal_expression)
  351. else
  352. begin
  353. {$ifdef i386}
  354. for l:=0 to def^.size-1 do
  355. curconstsegment^.concat(new(pai_const,init_8bit(p^.value_set^[l])));
  356. {$endif}
  357. {$ifdef m68k}
  358. j:=0;
  359. for l:=0 to ((def^.size-1) div 4) do
  360. { HORRIBLE HACK because of endian }
  361. { now use intel endian for constant sets }
  362. begin
  363. curconstsegment^.concat(new(pai_const,init_8bit(p^.value_set^[j+3])));
  364. curconstsegment^.concat(new(pai_const,init_8bit(p^.value_set^[j+2])));
  365. curconstsegment^.concat(new(pai_const,init_8bit(p^.value_set^[j+1])));
  366. curconstsegment^.concat(new(pai_const,init_8bit(p^.value_set^[j])));
  367. Inc(j,4);
  368. end;
  369. {$endif}
  370. end;
  371. end
  372. else
  373. Message(cg_e_illegal_expression);
  374. disposetree(p);
  375. end;
  376. enumdef:
  377. begin
  378. p:=comp_expr(true);
  379. do_firstpass(p);
  380. if p^.treetype=ordconstn then
  381. begin
  382. if is_equal(p^.resulttype,def) then
  383. curconstsegment^.concat(new(pai_const,init_32bit(p^.value)))
  384. else
  385. Message(cg_e_illegal_expression);
  386. end
  387. else
  388. Message(cg_e_illegal_expression);
  389. disposetree(p);
  390. end;
  391. stringdef:
  392. begin
  393. p:=comp_expr(true);
  394. do_firstpass(p);
  395. { first take care of prefixes for long and ansi strings }
  396. case pstringdef(def)^.string_typ of
  397. st_shortstring:
  398. begin
  399. if p^.treetype=stringconstn then
  400. begin
  401. if p^.length>=def^.size then
  402. strlength:=def^.size-1
  403. else
  404. strlength:=p^.length;
  405. curconstsegment^.concat(new(pai_const,init_8bit(strlength)));
  406. { this can also handle longer strings }
  407. getmem(ca,strlength+1);
  408. move(p^.value_str^,ca^,strlength);
  409. ca[strlength]:=#0;
  410. curconstsegment^.concat(new(pai_string,init_length_pchar(ca,strlength)));
  411. end
  412. else if is_constcharnode(p) then
  413. begin
  414. curconstsegment^.concat(new(pai_string,init(#1+char(byte(p^.value)))));
  415. strlength:=1;
  416. end
  417. else Message(cg_e_illegal_expression);
  418. if def^.size>strlength then
  419. begin
  420. getmem(ca,def^.size-strlength);
  421. { def^.size contains also the leading length, so we }
  422. { we have to subtract one }
  423. fillchar(ca[0],def^.size-strlength-1,' ');
  424. ca[def^.size-strlength-1]:=#0;
  425. { this can also handle longer strings }
  426. curconstsegment^.concat(new(pai_string,init_length_pchar(ca,def^.size-strlength-1)));
  427. end;
  428. end;
  429. {$ifdef UseLongString}
  430. st_longstring:
  431. begin
  432. if is_constcharnode(p) then
  433. strlength:=1
  434. else
  435. strlength:=p^.length;
  436. { first write the maximum size }
  437. curconstsegment^.concat(new(pai_const,init_32bit(strlength)))));
  438. { fill byte }
  439. curconstsegment^.concat(new(pai_const,init_8bit(0)));
  440. if p^.treetype=stringconstn then
  441. begin
  442. getmem(ca,strlength+1);
  443. move(p^.value_str^,ca^,strlength);
  444. ca[strlength]:=#0;
  445. generate_pascii(consts,ca,strlength);
  446. end
  447. else if is_constcharnode(p) then
  448. begin
  449. consts^.concat(new(pai_const,init_8bit(p^.value)));
  450. end
  451. else Message(cg_e_illegal_expression);
  452. curconstsegment^.concat(new(pai_const,init_8bit(0)));
  453. end;
  454. {$endif UseLongString}
  455. st_ansistring:
  456. begin
  457. { an empty ansi string is nil! }
  458. if (p^.treetype=stringconstn) and (p^.length=0) then
  459. curconstsegment^.concat(new(pai_const,init_32bit(0)))
  460. else
  461. begin
  462. if is_constcharnode(p) then
  463. strlength:=1
  464. else
  465. strlength:=p^.length;
  466. getdatalabel(ll);
  467. curconstsegment^.concat(new(pai_const_symbol,init(ll)));
  468. { first write the maximum size }
  469. consts^.concat(new(pai_const,init_32bit(strlength)));
  470. { second write the real length }
  471. consts^.concat(new(pai_const,init_32bit(strlength)));
  472. { redondent with maxlength but who knows ... (PM) }
  473. { third write use count (set to -1 for safety ) }
  474. consts^.concat(new(pai_const,init_32bit(-1)));
  475. consts^.concat(new(pai_label,init(ll)));
  476. if p^.treetype=stringconstn then
  477. begin
  478. getmem(ca,strlength+1);
  479. move(p^.value_str^,ca^,strlength);
  480. ca[strlength]:=#0;
  481. consts^.concat(new(pai_string,init_length_pchar(ca,strlength)));
  482. end
  483. else if is_constcharnode(p) then
  484. begin
  485. consts^.concat(new(pai_const,init_8bit(p^.value)));
  486. end
  487. else Message(cg_e_illegal_expression);
  488. consts^.concat(new(pai_const,init_8bit(0)));
  489. end;
  490. end;
  491. end;
  492. disposetree(p);
  493. end;
  494. arraydef:
  495. begin
  496. if token=_LKLAMMER then
  497. begin
  498. consume(_LKLAMMER);
  499. for l:=parraydef(def)^.lowrange to parraydef(def)^.highrange-1 do
  500. begin
  501. readtypedconst(parraydef(def)^.elementtype.def,nil,no_change_allowed);
  502. consume(_COMMA);
  503. end;
  504. readtypedconst(parraydef(def)^.elementtype.def,nil,no_change_allowed);
  505. consume(_RKLAMMER);
  506. end
  507. else
  508. { if array of char then we allow also a string }
  509. if is_char(parraydef(def)^.elementtype.def) then
  510. begin
  511. p:=comp_expr(true);
  512. do_firstpass(p);
  513. if p^.treetype=stringconstn then
  514. begin
  515. if p^.length>255 then
  516. len:=255
  517. else
  518. len:=p^.length;
  519. {$ifndef TP}
  520. {$ifopt H+}
  521. setlength(s,len);
  522. {$else}
  523. s[0]:=chr(len);
  524. {$endif}
  525. {$else}
  526. s[0]:=chr(len);
  527. {$endif}
  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. Message(parser_e_type_const_not_possible);
  699. consume_all_until(_RKLAMMER);
  700. end
  701. else
  702. begin
  703. consume(_LKLAMMER);
  704. aktpos:=0;
  705. while token<>_RKLAMMER do
  706. begin
  707. s:=pattern;
  708. consume(_ID);
  709. consume(_COLON);
  710. srsym:=nil;
  711. obj:=pobjectdef(def);
  712. symt:=obj^.symtable;
  713. while (srsym=nil) and assigned(symt) do
  714. begin
  715. srsym:=symt^.search(s);
  716. if assigned(obj) then
  717. obj:=obj^.childof;
  718. if assigned(obj) then
  719. symt:=obj^.symtable
  720. else
  721. symt:=nil;
  722. end;
  723. if srsym=nil then
  724. begin
  725. Message1(sym_e_id_not_found,s);
  726. consume_all_until(_SEMICOLON);
  727. end
  728. else
  729. begin
  730. { check position }
  731. if pvarsym(srsym)^.address<aktpos then
  732. Message(parser_e_invalid_record_const);
  733. { if needed fill }
  734. if pvarsym(srsym)^.address>aktpos then
  735. for i:=1 to pvarsym(srsym)^.address-aktpos do
  736. curconstsegment^.concat(new(pai_const,init_8bit(0)));
  737. { new position }
  738. aktpos:=pvarsym(srsym)^.address+pvarsym(srsym)^.vartype.def^.size;
  739. { read the data }
  740. readtypedconst(pvarsym(srsym)^.vartype.def,nil,no_change_allowed);
  741. if token=_SEMICOLON then
  742. consume(_SEMICOLON)
  743. else break;
  744. end;
  745. end;
  746. for i:=1 to def^.size-aktpos do
  747. curconstsegment^.concat(new(pai_const,init_8bit(0)));
  748. consume(_RKLAMMER);
  749. end;
  750. end;
  751. errordef:
  752. begin
  753. { try to consume something useful }
  754. if token=_LKLAMMER then
  755. consume_all_until(_RKLAMMER)
  756. else
  757. consume_all_until(_SEMICOLON);
  758. end;
  759. else Message(parser_e_type_const_not_possible);
  760. end;
  761. end;
  762. {$ifdef fpc}
  763. {$maxfpuregisters default}
  764. {$endif fpc}
  765. end.
  766. {
  767. $Log$
  768. Revision 1.64 2000-04-02 09:12:51 florian
  769. + constant procedure variables can have a @ in front:
  770. const p : procedure = @p;
  771. til now only
  772. const p : procedure = p;
  773. was allowed
  774. Revision 1.63 2000/02/13 14:21:51 jonas
  775. * modifications to make the compiler functional when compiled with
  776. -Or
  777. Revision 1.62 2000/02/09 13:23:01 peter
  778. * log truncated
  779. Revision 1.61 2000/01/07 01:14:33 peter
  780. * updated copyright to 2000
  781. Revision 1.60 1999/12/18 14:55:21 florian
  782. * very basic widestring support
  783. Revision 1.59 1999/11/30 10:40:51 peter
  784. + ttype, tsymlist
  785. Revision 1.58 1999/11/08 18:50:11 florian
  786. * disposetree for classrefdef added
  787. Revision 1.57 1999/11/08 16:24:28 pierre
  788. * missing disposetree added to avoid memory loss
  789. Revision 1.56 1999/11/08 14:02:16 florian
  790. * problem with "index X"-properties solved
  791. * typed constants of class references are now allowed
  792. Revision 1.55 1999/11/06 14:34:23 peter
  793. * truncated log to 20 revs
  794. Revision 1.54 1999/10/14 14:57:54 florian
  795. - removed the hcodegen use in the new cg, use cgbase instead
  796. Revision 1.53 1999/09/26 21:30:20 peter
  797. + constant pointer support which can happend with typecasting like
  798. const p=pointer(1)
  799. * better procvar parsing in typed consts
  800. Revision 1.52 1999/08/10 12:30:02 pierre
  801. * avoid unused locals
  802. Revision 1.51 1999/08/04 13:03:02 jonas
  803. * all tokens now start with an underscore
  804. * PowerPC compiles!!
  805. Revision 1.50 1999/08/04 00:23:21 florian
  806. * renamed i386asm and i386base to cpuasm and cpubase
  807. Revision 1.49 1999/08/03 22:03:08 peter
  808. * moved bitmask constants to sets
  809. * some other type/const renamings
  810. Revision 1.48 1999/07/23 16:05:26 peter
  811. * alignment is now saved in the symtable
  812. * C alignment added for records
  813. * PPU version increased to solve .12 <-> .13 probs
  814. }