ptconst.pas 38 KB

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