ptconst.pas 38 KB

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