ptconst.pas 34 KB

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