ptconst.pas 37 KB

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