ptconst.pas 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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 symtype,symsym;
  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,globals,scanner,
  35. symconst,symbase,symdef,symtable,aasm,types,verbose,
  36. { pass 1 }
  37. node,pass_1,
  38. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,
  39. { parser specific stuff }
  40. pbase,pexpr,
  41. { codegen }
  42. hcodegen
  43. {$ifdef newcg}
  44. ,cgbase
  45. {$endif}
  46. ;
  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 : tnode;
  58. i,l,offset,
  59. strlength : longint;
  60. curconstsegment : TAAsmoutput;
  61. ll : pasmlabel;
  62. s : string;
  63. ca : pchar;
  64. tmpguid : tguid;
  65. aktpos : longint;
  66. obj : pobjectdef;
  67. symt : psymtable;
  68. value : bestreal;
  69. strval : pchar;
  70. procedure check_range;
  71. begin
  72. if ((tordconstnode(p).value>porddef(def)^.high) or
  73. (tordconstnode(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. {$R-} {Range check creates problem with init_8bit(-1) !!}
  82. begin
  83. if no_change_allowed then
  84. curconstsegment:=consts
  85. else
  86. curconstsegment:=datasegment;
  87. case def^.deftype of
  88. orddef:
  89. begin
  90. p:=comp_expr(true);
  91. do_firstpass(p);
  92. case porddef(def)^.typ of
  93. bool8bit :
  94. begin
  95. if is_constboolnode(p) then
  96. curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value))
  97. else
  98. Message(cg_e_illegal_expression);
  99. end;
  100. bool16bit :
  101. begin
  102. if is_constboolnode(p) then
  103. curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value))
  104. else
  105. Message(cg_e_illegal_expression);
  106. end;
  107. bool32bit :
  108. begin
  109. if is_constboolnode(p) then
  110. curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value))
  111. else
  112. Message(cg_e_illegal_expression);
  113. end;
  114. uchar :
  115. begin
  116. if is_constcharnode(p) then
  117. curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value))
  118. else
  119. Message(cg_e_illegal_expression);
  120. end;
  121. uwidechar :
  122. begin
  123. if is_constcharnode(p) then
  124. curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value))
  125. else
  126. Message(cg_e_illegal_expression);
  127. end;
  128. s8bit,
  129. u8bit :
  130. begin
  131. if is_constintnode(p) then
  132. begin
  133. curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value));
  134. check_range;
  135. end
  136. else
  137. Message(cg_e_illegal_expression);
  138. end;
  139. u16bit,
  140. s16bit :
  141. begin
  142. if is_constintnode(p) then
  143. begin
  144. curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value));
  145. check_range;
  146. end
  147. else
  148. Message(cg_e_illegal_expression);
  149. end;
  150. s32bit,
  151. u32bit :
  152. begin
  153. if is_constintnode(p) then
  154. begin
  155. curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value));
  156. if porddef(def)^.typ<>u32bit then
  157. check_range;
  158. end
  159. else
  160. Message(cg_e_illegal_expression);
  161. end;
  162. s64bit,
  163. u64bit:
  164. begin
  165. if is_constintnode(p) then
  166. begin
  167. {!!!!! hmmm, we can write yet only consts til 2^32-1 :( (FK) }
  168. curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value));
  169. curconstSegment.concat(Tai_const.Create_32bit(0));
  170. end
  171. else
  172. Message(cg_e_illegal_expression);
  173. end;
  174. else
  175. internalerror(3799);
  176. end;
  177. p.free;
  178. end;
  179. floatdef:
  180. begin
  181. p:=comp_expr(true);
  182. do_firstpass(p);
  183. if is_constrealnode(p) then
  184. value:=trealconstnode(p).value_real
  185. else if is_constintnode(p) then
  186. value:=tordconstnode(p).value
  187. else
  188. Message(cg_e_illegal_expression);
  189. case pfloatdef(def)^.typ of
  190. s32real :
  191. curconstSegment.concat(Tai_real_32bit.Create(value));
  192. s64real :
  193. curconstSegment.concat(Tai_real_64bit.Create(value));
  194. s80real :
  195. curconstSegment.concat(Tai_real_80bit.Create(value));
  196. s64comp :
  197. curconstSegment.concat(Tai_comp_64bit.Create(value));
  198. f32bit :
  199. curconstSegment.concat(Tai_const.Create_32bit(trunc(value*65536)));
  200. else
  201. internalerror(18);
  202. end;
  203. p.free;
  204. end;
  205. classrefdef:
  206. begin
  207. p:=comp_expr(true);
  208. do_firstpass(p);
  209. case p.nodetype of
  210. loadvmtn:
  211. begin
  212. if not(pobjectdef(pclassrefdef(p.resulttype)^.pointertype.def)^.is_related(
  213. pobjectdef(pclassrefdef(def)^.pointertype.def))) then
  214. Message(cg_e_illegal_expression);
  215. curconstSegment.concat(Tai_const_symbol.Create(newasmsymbol(pobjectdef(
  216. pclassrefdef(p.resulttype)^.pointertype.def)^.vmt_mangledname)));
  217. end;
  218. niln:
  219. curconstSegment.concat(Tai_const.Create_32bit(0));
  220. else Message(cg_e_illegal_expression);
  221. end;
  222. p.free;
  223. end;
  224. pointerdef:
  225. begin
  226. p:=comp_expr(true);
  227. do_firstpass(p);
  228. if (p.nodetype=typeconvn) and
  229. (ttypeconvnode(p).left.nodetype in [addrn,niln]) and
  230. is_equal(def,p.resulttype) then
  231. begin
  232. hp:=ttypeconvnode(p).left;
  233. ttypeconvnode(p).left:=nil;
  234. p.free;
  235. p:=hp;
  236. end;
  237. { allows horrible ofs(typeof(TButton)^) code !! }
  238. if (p.nodetype=addrn) and
  239. (taddrnode(p).left.nodetype=derefn) then
  240. begin
  241. hp:=tderefnode(taddrnode(p).left).left;
  242. tderefnode(taddrnode(p).left).left:=nil;
  243. p.free;
  244. p:=hp;
  245. end;
  246. { nil pointer ? }
  247. if p.nodetype=niln then
  248. curconstSegment.concat(Tai_const.Create_32bit(0))
  249. { maybe pchar ? }
  250. else
  251. if is_char(ppointerdef(def)^.pointertype.def) and
  252. (p.nodetype<>addrn) then
  253. begin
  254. getdatalabel(ll);
  255. curconstSegment.concat(Tai_const_symbol.Create(ll));
  256. Consts.concat(Tai_label.Create(ll));
  257. if p.nodetype=stringconstn then
  258. begin
  259. len:=tstringconstnode(p).len;
  260. { For tp7 the maximum lentgh can be 255 }
  261. if (m_tp in aktmodeswitches) and
  262. (len>255) then
  263. len:=255;
  264. getmem(ca,len+2);
  265. move(tstringconstnode(p).value_str^,ca^,len+1);
  266. Consts.concat(Tai_string.Create_length_pchar(ca,len+1));
  267. end
  268. else
  269. if is_constcharnode(p) then
  270. Consts.concat(Tai_string.Create(char(byte(tordconstnode(p).value))+#0))
  271. else
  272. Message(cg_e_illegal_expression);
  273. end
  274. else
  275. if p.nodetype=addrn then
  276. begin
  277. hp:=taddrnode(p).left;
  278. while assigned(hp) and (hp.nodetype in [subscriptn,vecn]) do
  279. hp:=tbinarynode(hp).left;
  280. if (is_equal(ppointerdef(p.resulttype)^.pointertype.def,ppointerdef(def)^.pointertype.def) or
  281. (is_equal(ppointerdef(p.resulttype)^.pointertype.def,voiddef)) or
  282. (is_equal(ppointerdef(def)^.pointertype.def,voiddef))) and
  283. (hp.nodetype=loadn) then
  284. begin
  285. do_firstpass(taddrnode(p).left);
  286. hp:=taddrnode(p).left;
  287. offset:=0;
  288. while assigned(hp) and (hp.nodetype<>loadn) do
  289. begin
  290. case hp.nodetype of
  291. vecn :
  292. begin
  293. case tvecnode(hp).left.resulttype^.deftype of
  294. stringdef :
  295. begin
  296. { this seems OK for shortstring and ansistrings PM }
  297. { it is wrong for widestrings !! }
  298. len:=1;
  299. base:=0;
  300. end;
  301. arraydef :
  302. begin
  303. len:=parraydef(tvecnode(hp).left.resulttype)^.elesize;
  304. base:=parraydef(tvecnode(hp).left.resulttype)^.lowrange;
  305. end
  306. else
  307. Message(cg_e_illegal_expression);
  308. end;
  309. if is_constintnode(tvecnode(hp).right) then
  310. inc(offset,len*(get_ordinal_value(tvecnode(hp).right)-base))
  311. else
  312. Message(cg_e_illegal_expression);
  313. end;
  314. subscriptn :
  315. inc(offset,tsubscriptnode(hp).vs^.address)
  316. else
  317. Message(cg_e_illegal_expression);
  318. end;
  319. hp:=tbinarynode(hp).left;
  320. end;
  321. if tloadnode(hp).symtableentry^.typ=constsym then
  322. Message(type_e_variable_id_expected);
  323. curconstSegment.concat(Tai_const_symbol.Createname_offset(tloadnode(hp).symtableentry^.mangledname,offset));
  324. end
  325. else
  326. Message(cg_e_illegal_expression);
  327. end
  328. else
  329. { allow typeof(Object type)}
  330. if (p.nodetype=inlinen) and
  331. (tinlinenode(p).inlinenumber=in_typeof_x) then
  332. begin
  333. if (tinlinenode(p).left.nodetype=typen) then
  334. begin
  335. curconstSegment.concat(Tai_const_symbol.createname(
  336. pobjectdef(tinlinenode(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. p.free;
  344. end;
  345. setdef:
  346. begin
  347. p:=comp_expr(true);
  348. do_firstpass(p);
  349. if p.nodetype=setconstn then
  350. begin
  351. { we only allow const sets }
  352. if assigned(tsetconstnode(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(Tai_const.Create_8bit(tsetconstnode(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(Tai_const.Create_8bit(tordconstnode(p).value_set^[j+3]));
  367. curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value_set^[j+2]));
  368. curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value_set^[j+1]));
  369. curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(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. p.free;
  378. end;
  379. enumdef:
  380. begin
  381. p:=comp_expr(true);
  382. do_firstpass(p);
  383. if p.nodetype=ordconstn then
  384. begin
  385. if is_equal(p.resulttype,def) or
  386. is_subequal(p.resulttype,def) then
  387. begin
  388. case p.resulttype^.size of
  389. 1 : curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value));
  390. 2 : curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value));
  391. 4 : curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value));
  392. end;
  393. end
  394. else
  395. Message2(type_e_incompatible_types,def^.typename,p.resulttype^.typename);
  396. end
  397. else
  398. Message(cg_e_illegal_expression);
  399. p.free;
  400. end;
  401. stringdef:
  402. begin
  403. p:=comp_expr(true);
  404. do_firstpass(p);
  405. { load strval and strlength of the constant tree }
  406. if p.nodetype=stringconstn then
  407. begin
  408. strlength:=tstringconstnode(p).len;
  409. strval:=tstringconstnode(p).value_str;
  410. end
  411. else if is_constcharnode(p) then
  412. begin
  413. strval:=pchar(@tordconstnode(p).value);
  414. strlength:=1
  415. end
  416. else if is_constresourcestringnode(p) then
  417. begin
  418. strval:=pchar(tpointerord(pconstsym(tloadnode(p).symtableentry)^.value));
  419. strlength:=pconstsym(tloadnode(p).symtableentry)^.len;
  420. end
  421. else
  422. begin
  423. Message(cg_e_illegal_expression);
  424. strlength:=-1;
  425. end;
  426. if strlength>=0 then
  427. begin
  428. case pstringdef(def)^.string_typ of
  429. st_shortstring:
  430. begin
  431. if strlength>=def^.size then
  432. begin
  433. message2(parser_w_string_too_long,strpas(strval),tostr(def^.size-1));
  434. strlength:=def^.size-1;
  435. end;
  436. curconstSegment.concat(Tai_const.Create_8bit(strlength));
  437. { this can also handle longer strings }
  438. getmem(ca,strlength+1);
  439. move(strval^,ca^,strlength);
  440. ca[strlength]:=#0;
  441. curconstSegment.concat(Tai_string.Create_length_pchar(ca,strlength));
  442. { fillup with spaces if size is shorter }
  443. if def^.size>strlength then
  444. begin
  445. getmem(ca,def^.size-strlength);
  446. { def^.size contains also the leading length, so we }
  447. { we have to subtract one }
  448. fillchar(ca[0],def^.size-strlength-1,' ');
  449. ca[def^.size-strlength-1]:=#0;
  450. { this can also handle longer strings }
  451. curconstSegment.concat(Tai_string.Create_length_pchar(ca,def^.size-strlength-1));
  452. end;
  453. end;
  454. {$ifdef UseLongString}
  455. st_longstring:
  456. begin
  457. { first write the maximum size }
  458. curconstSegment.concat(Tai_const.Create_32bit(strlength))));
  459. { fill byte }
  460. curconstSegment.concat(Tai_const.Create_8bit(0));
  461. getmem(ca,strlength+1);
  462. move(strval^,ca^,strlength);
  463. ca[strlength]:=#0;
  464. generate_pascii(consts,ca,strlength);
  465. curconstSegment.concat(Tai_const.Create_8bit(0));
  466. end;
  467. {$endif UseLongString}
  468. st_ansistring:
  469. begin
  470. { an empty ansi string is nil! }
  471. if (strlength=0) then
  472. curconstSegment.concat(Tai_const.Create_32bit(0))
  473. else
  474. begin
  475. getdatalabel(ll);
  476. curconstSegment.concat(Tai_const_symbol.Create(ll));
  477. { first write the maximum size }
  478. Consts.concat(Tai_const.Create_32bit(strlength));
  479. { second write the real length }
  480. Consts.concat(Tai_const.Create_32bit(strlength));
  481. { redondent with maxlength but who knows ... (PM) }
  482. { third write use count (set to -1 for safety ) }
  483. Consts.concat(Tai_const.Create_32bit(-1));
  484. Consts.concat(Tai_label.Create(ll));
  485. getmem(ca,strlength+2);
  486. move(strval^,ca^,strlength);
  487. { The terminating #0 to be stored in the .data section (JM) }
  488. ca[strlength]:=#0;
  489. { End of the PChar. The memory has to be allocated because in }
  490. { tai_string.done, there is a freemem(len+1) (JM) }
  491. ca[strlength+1]:=#0;
  492. Consts.concat(Tai_string.Create_length_pchar(ca,strlength+1));
  493. end;
  494. end;
  495. end;
  496. end;
  497. p.free;
  498. end;
  499. arraydef:
  500. begin
  501. if token=_LKLAMMER then
  502. begin
  503. consume(_LKLAMMER);
  504. for l:=parraydef(def)^.lowrange to parraydef(def)^.highrange-1 do
  505. begin
  506. readtypedconst(parraydef(def)^.elementtype.def,nil,no_change_allowed);
  507. consume(_COMMA);
  508. end;
  509. readtypedconst(parraydef(def)^.elementtype.def,nil,no_change_allowed);
  510. consume(_RKLAMMER);
  511. end
  512. else
  513. { if array of char then we allow also a string }
  514. if is_char(parraydef(def)^.elementtype.def) then
  515. begin
  516. p:=comp_expr(true);
  517. do_firstpass(p);
  518. if p.nodetype=stringconstn then
  519. begin
  520. len:=tstringconstnode(p).len;
  521. { For tp7 the maximum lentgh can be 255 }
  522. if (m_tp in aktmodeswitches) and
  523. (len>255) then
  524. len:=255;
  525. ca:=tstringconstnode(p).value_str;
  526. end
  527. else
  528. if is_constcharnode(p) then
  529. begin
  530. ca:=pchar(@tordconstnode(p).value);
  531. len:=1;
  532. end
  533. else
  534. begin
  535. Message(cg_e_illegal_expression);
  536. len:=0;
  537. end;
  538. if len>(Parraydef(def)^.highrange-Parraydef(def)^.lowrange+1) then
  539. Message(parser_e_string_larger_array);
  540. for i:=Parraydef(def)^.lowrange to Parraydef(def)^.highrange do
  541. begin
  542. if i+1-Parraydef(def)^.lowrange<=len then
  543. begin
  544. curconstSegment.concat(Tai_const.Create_8bit(byte(ca^)));
  545. inc(ca);
  546. end
  547. else
  548. {Fill the remaining positions with #0.}
  549. curconstSegment.concat(Tai_const.Create_8bit(0));
  550. end;
  551. p.free;
  552. end
  553. else
  554. begin
  555. { we want the ( }
  556. consume(_LKLAMMER);
  557. end;
  558. end;
  559. procvardef:
  560. begin
  561. { Procvars and pointers are no longer compatible. }
  562. { under tp: =nil or =var under fpc: =nil or =@var }
  563. if token=_NIL then
  564. begin
  565. curconstSegment.concat(Tai_const.Create_32bit(0));
  566. consume(_NIL);
  567. exit;
  568. end
  569. else
  570. if not(m_tp_procvar in aktmodeswitches) then
  571. if token=_KLAMMERAFFE then
  572. consume(_KLAMMERAFFE);
  573. getprocvar:=true;
  574. getprocvardef:=pprocvardef(def);
  575. p:=comp_expr(true);
  576. getprocvar:=false;
  577. do_firstpass(p);
  578. if codegenerror then
  579. begin
  580. p.free;
  581. exit;
  582. end;
  583. { convert calln to loadn }
  584. if p.nodetype=calln then
  585. begin
  586. if (tcallnode(p).symtableprocentry^.owner^.symtabletype=objectsymtable) and
  587. is_class(pdef(tcallnode(p).symtableprocentry^.owner^.defowner)) then
  588. hp:=genloadmethodcallnode(pprocsym(tcallnode(p).symtableprocentry),tcallnode(p).symtableproc,
  589. tcallnode(p).methodpointer.getcopy)
  590. else
  591. hp:=genloadcallnode(pprocsym(tcallnode(p).symtableprocentry),tcallnode(p).symtableproc);
  592. p.free;
  593. do_firstpass(hp);
  594. p:=hp;
  595. if codegenerror then
  596. begin
  597. p.free;
  598. exit;
  599. end;
  600. end
  601. else if (p.nodetype=addrn) and assigned(taddrnode(p).left) and
  602. (taddrnode(p).left.nodetype=calln) then
  603. begin
  604. if (tcallnode(taddrnode(p).left).symtableprocentry^.owner^.symtabletype=objectsymtable) and
  605. is_class(pdef(tcallnode(taddrnode(p).left).symtableprocentry^.owner^.defowner)) then
  606. hp:=genloadmethodcallnode(pprocsym(tcallnode(taddrnode(p).left).symtableprocentry),
  607. tcallnode(taddrnode(p).left).symtableproc,tcallnode(taddrnode(p).left).methodpointer.getcopy)
  608. else
  609. hp:=genloadcallnode(pprocsym(tcallnode(taddrnode(p).left).symtableprocentry),
  610. tcallnode(taddrnode(p).left).symtableproc);
  611. p.free;
  612. do_firstpass(hp);
  613. p:=hp;
  614. if codegenerror then
  615. begin
  616. p.free;
  617. exit;
  618. end;
  619. end;
  620. { let type conversion check everything needed }
  621. p:=gentypeconvnode(p,def);
  622. do_firstpass(p);
  623. if codegenerror then
  624. begin
  625. p.free;
  626. exit;
  627. end;
  628. { remove typeconvn, that will normally insert a lea
  629. instruction which is not necessary for us }
  630. if p.nodetype=typeconvn then
  631. begin
  632. hp:=ttypeconvnode(p).left;
  633. ttypeconvnode(p).left:=nil;
  634. p.free;
  635. p:=hp;
  636. end;
  637. { remove addrn which we also don't need here }
  638. if p.nodetype=addrn then
  639. begin
  640. hp:=taddrnode(p).left;
  641. taddrnode(p).left:=nil;
  642. p.free;
  643. p:=hp;
  644. end;
  645. { we now need to have a loadn with a procsym }
  646. if (p.nodetype=loadn) and
  647. (tloadnode(p).symtableentry^.typ=procsym) then
  648. begin
  649. curconstSegment.concat(Tai_const_symbol.createname(
  650. pprocsym(tloadnode(p).symtableentry)^.definition^.mangledname));
  651. end
  652. else
  653. Message(cg_e_illegal_expression);
  654. p.free;
  655. end;
  656. { reads a typed constant record }
  657. recorddef:
  658. begin
  659. { KAZ }
  660. if (precorddef(def)=rec_tguid) and
  661. ((token=_CSTRING) or (token=_CCHAR) or (token=_ID)) then
  662. begin
  663. p:=comp_expr(true);
  664. p:=gentypeconvnode(p,cshortstringdef);
  665. do_firstpass(p);
  666. if p.nodetype=stringconstn then
  667. begin
  668. s:=strpas(tstringconstnode(p).value_str);
  669. p.free;
  670. if string2guid(s,tmpguid) then
  671. begin
  672. curconstSegment.concat(Tai_const.Create_32bit(tmpguid.D1));
  673. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D2));
  674. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D3));
  675. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  676. curconstSegment.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  677. end
  678. else
  679. Message(parser_e_improper_guid_syntax);
  680. end
  681. else
  682. begin
  683. p.free;
  684. Message(cg_e_illegal_expression);
  685. exit;
  686. end;
  687. end
  688. else
  689. begin
  690. consume(_LKLAMMER);
  691. aktpos:=0;
  692. while token<>_RKLAMMER do
  693. begin
  694. s:=pattern;
  695. consume(_ID);
  696. consume(_COLON);
  697. srsym:=psym(precorddef(def)^.symtable^.search(s));
  698. if srsym=nil then
  699. begin
  700. Message1(sym_e_id_not_found,s);
  701. consume_all_until(_SEMICOLON);
  702. end
  703. else
  704. begin
  705. { check position }
  706. if pvarsym(srsym)^.address<aktpos then
  707. Message(parser_e_invalid_record_const);
  708. { if needed fill }
  709. if pvarsym(srsym)^.address>aktpos then
  710. for i:=1 to pvarsym(srsym)^.address-aktpos do
  711. curconstSegment.concat(Tai_const.Create_8bit(0));
  712. { new position }
  713. aktpos:=pvarsym(srsym)^.address+pvarsym(srsym)^.vartype.def^.size;
  714. { read the data }
  715. readtypedconst(pvarsym(srsym)^.vartype.def,nil,no_change_allowed);
  716. if token=_SEMICOLON then
  717. consume(_SEMICOLON)
  718. else break;
  719. end;
  720. end;
  721. for i:=1 to def^.size-aktpos do
  722. curconstSegment.concat(Tai_const.Create_8bit(0));
  723. consume(_RKLAMMER);
  724. end;
  725. end;
  726. { reads a typed object }
  727. objectdef:
  728. begin
  729. if is_class_or_interface(def) then
  730. begin
  731. p:=comp_expr(true);
  732. do_firstpass(p);
  733. if p.nodetype<>niln then
  734. begin
  735. Message(parser_e_type_const_not_possible);
  736. consume_all_until(_RKLAMMER);
  737. end
  738. else
  739. begin
  740. curconstSegment.concat(Tai_const.Create_32bit(0));
  741. end;
  742. p.free;
  743. end
  744. { for objects we allow it only if it doesn't contain a vmt }
  745. else if (oo_has_vmt in pobjectdef(def)^.objectoptions) then
  746. Message(parser_e_type_const_not_possible)
  747. else
  748. begin
  749. consume(_LKLAMMER);
  750. aktpos:=0;
  751. while token<>_RKLAMMER do
  752. begin
  753. s:=pattern;
  754. consume(_ID);
  755. consume(_COLON);
  756. srsym:=nil;
  757. obj:=pobjectdef(def);
  758. symt:=obj^.symtable;
  759. while (srsym=nil) and assigned(symt) do
  760. begin
  761. srsym:=psym(symt^.search(s));
  762. if assigned(obj) then
  763. obj:=obj^.childof;
  764. if assigned(obj) then
  765. symt:=obj^.symtable
  766. else
  767. symt:=nil;
  768. end;
  769. if srsym=nil then
  770. begin
  771. Message1(sym_e_id_not_found,s);
  772. consume_all_until(_SEMICOLON);
  773. end
  774. else
  775. begin
  776. { check position }
  777. if pvarsym(srsym)^.address<aktpos then
  778. Message(parser_e_invalid_record_const);
  779. { if needed fill }
  780. if pvarsym(srsym)^.address>aktpos then
  781. for i:=1 to pvarsym(srsym)^.address-aktpos do
  782. curconstSegment.concat(Tai_const.Create_8bit(0));
  783. { new position }
  784. aktpos:=pvarsym(srsym)^.address+pvarsym(srsym)^.vartype.def^.size;
  785. { read the data }
  786. readtypedconst(pvarsym(srsym)^.vartype.def,nil,no_change_allowed);
  787. if token=_SEMICOLON then
  788. consume(_SEMICOLON)
  789. else break;
  790. end;
  791. end;
  792. for i:=1 to def^.size-aktpos do
  793. curconstSegment.concat(Tai_const.Create_8bit(0));
  794. consume(_RKLAMMER);
  795. end;
  796. end;
  797. errordef:
  798. begin
  799. { try to consume something useful }
  800. if token=_LKLAMMER then
  801. consume_all_until(_RKLAMMER)
  802. else
  803. consume_all_until(_SEMICOLON);
  804. end;
  805. else Message(parser_e_type_const_not_possible);
  806. end;
  807. end;
  808. {$ifdef fpc}
  809. {$maxfpuregisters default}
  810. {$endif fpc}
  811. end.
  812. {
  813. $Log$
  814. Revision 1.15 2000-12-25 00:07:28 peter
  815. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  816. tlinkedlist objects)
  817. Revision 1.14 2000/12/10 20:24:18 peter
  818. * allow subtypes for enums
  819. Revision 1.13 2000/11/29 00:30:38 florian
  820. * unused units removed from uses clause
  821. * some changes for widestrings
  822. Revision 1.12 2000/11/06 15:54:15 florian
  823. * fixed two bugs to get make cycle work, but it's not enough
  824. Revision 1.11 2000/11/04 14:25:21 florian
  825. + merged Attila's changes for interfaces, not tested yet
  826. Revision 1.10 2000/10/31 22:02:51 peter
  827. * symtable splitted, no real code changes
  828. Revision 1.9 2000/10/14 10:14:52 peter
  829. * moehrendorf oct 2000 rewrite
  830. Revision 1.8 2000/09/30 13:23:04 peter
  831. * const array of char and pchar length fixed (merged)
  832. Revision 1.7 2000/09/24 15:06:25 peter
  833. * use defines.inc
  834. Revision 1.6 2000/08/27 16:11:52 peter
  835. * moved some util functions from globals,cobjects to cutils
  836. * splitted files into finput,fmodule
  837. Revision 1.5 2000/08/24 19:13:18 peter
  838. * allow nil for class typed consts (merged)
  839. Revision 1.4 2000/08/16 13:06:06 florian
  840. + support of 64 bit integer constants
  841. Revision 1.3 2000/08/05 13:25:06 peter
  842. * packenum 1 fixes (merged)
  843. Revision 1.2 2000/07/13 11:32:47 michael
  844. + removed logs
  845. }