ptconst.pas 38 KB

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