ptconst.pas 35 KB

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