ptconst.pas 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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,widestr,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. len,base : longint;
  54. p,hp : tnode;
  55. i,j,l,offset,
  56. strlength : longint;
  57. curconstsegment : TAAsmoutput;
  58. ll : tasmlabel;
  59. s : string;
  60. c : char;
  61. ca : pchar;
  62. tmpguid : tguid;
  63. aktpos : longint;
  64. obj : tobjectdef;
  65. recsym,
  66. srsym : tsym;
  67. symt : tsymtable;
  68. value : bestreal;
  69. strval : pchar;
  70. error : boolean;
  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. { this writing is endian independant }
  355. { untrue - because they are considered }
  356. { arrays of 32-bit values CEC }
  357. if source_info.endian = target_info.endian then
  358. begin
  359. for l:= 0 to p.resulttype.def.size-1 do
  360. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[l]));
  361. end
  362. else
  363. begin
  364. { store as longint values in swaped format }
  365. j:=0;
  366. for l:=0 to ((p.resulttype.def.size-1) div 4) do
  367. begin
  368. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j+3]));
  369. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j+2]));
  370. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j+1]));
  371. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j]));
  372. Inc(j,4);
  373. end;
  374. end;
  375. end;
  376. end
  377. else
  378. Message(cg_e_illegal_expression);
  379. p.free;
  380. end;
  381. enumdef:
  382. begin
  383. p:=comp_expr(true);
  384. if p.nodetype=ordconstn then
  385. begin
  386. if is_equal(p.resulttype.def,t.def) or
  387. is_subequal(p.resulttype.def,t.def) then
  388. begin
  389. case p.resulttype.def.size of
  390. 1 : curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value));
  391. 2 : curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value));
  392. 4 : curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value));
  393. end;
  394. end
  395. else
  396. Message2(type_e_incompatible_types,t.def.typename,p.resulttype.def.typename);
  397. end
  398. else
  399. Message(cg_e_illegal_expression);
  400. p.free;
  401. end;
  402. stringdef:
  403. begin
  404. p:=comp_expr(true);
  405. { load strval and strlength of the constant tree }
  406. if p.nodetype=stringconstn then
  407. begin
  408. { convert to the expected string type so that
  409. for widestrings strval is a pcompilerwidestring }
  410. inserttypeconv(p,t);
  411. strlength:=tstringconstnode(p).len;
  412. strval:=tstringconstnode(p).value_str;
  413. end
  414. else if is_constcharnode(p) then
  415. begin
  416. { strval:=pchar(@tordconstnode(p).value);
  417. THIS FAIL on BIG_ENDIAN MACHINES PM }
  418. c:=chr(tordconstnode(p).value and $ff);
  419. strval:=@c;
  420. strlength:=1
  421. end
  422. else if is_constresourcestringnode(p) then
  423. begin
  424. strval:=pchar(tpointerord(tconstsym(tloadnode(p).symtableentry).value));
  425. strlength:=tconstsym(tloadnode(p).symtableentry).len;
  426. end
  427. else
  428. begin
  429. Message(cg_e_illegal_expression);
  430. strlength:=-1;
  431. end;
  432. if strlength>=0 then
  433. begin
  434. case tstringdef(t.def).string_typ of
  435. st_shortstring:
  436. begin
  437. if strlength>=t.def.size then
  438. begin
  439. message2(parser_w_string_too_long,strpas(strval),tostr(t.def.size-1));
  440. strlength:=t.def.size-1;
  441. end;
  442. curconstSegment.concat(Tai_const.Create_8bit(strlength));
  443. { this can also handle longer strings }
  444. getmem(ca,strlength+1);
  445. move(strval^,ca^,strlength);
  446. ca[strlength]:=#0;
  447. curconstSegment.concat(Tai_string.Create_length_pchar(ca,strlength));
  448. { fillup with spaces if size is shorter }
  449. if t.def.size>strlength then
  450. begin
  451. getmem(ca,t.def.size-strlength);
  452. { def.size contains also the leading length, so we }
  453. { we have to subtract one }
  454. fillchar(ca[0],t.def.size-strlength-1,' ');
  455. ca[t.def.size-strlength-1]:=#0;
  456. { this can also handle longer strings }
  457. curconstSegment.concat(Tai_string.Create_length_pchar(ca,t.def.size-strlength-1));
  458. end;
  459. end;
  460. st_ansistring:
  461. begin
  462. { an empty ansi string is nil! }
  463. if (strlength=0) then
  464. curconstSegment.concat(Tai_const.Create_32bit(0))
  465. else
  466. begin
  467. getdatalabel(ll);
  468. curconstSegment.concat(Tai_const_symbol.Create(ll));
  469. { first write the maximum size }
  470. Consts.concat(Tai_const.Create_32bit(strlength));
  471. { second write the real length }
  472. Consts.concat(Tai_const.Create_32bit(strlength));
  473. { redondent with maxlength but who knows ... (PM) }
  474. { third write use count (set to -1 for safety ) }
  475. Consts.concat(Tai_const.Create_32bit(-1));
  476. Consts.concat(Tai_label.Create(ll));
  477. getmem(ca,strlength+2);
  478. move(strval^,ca^,strlength);
  479. { The terminating #0 to be stored in the .data section (JM) }
  480. ca[strlength]:=#0;
  481. { End of the PChar. The memory has to be allocated because in }
  482. { tai_string.done, there is a freemem(len+1) (JM) }
  483. ca[strlength+1]:=#0;
  484. Consts.concat(Tai_string.Create_length_pchar(ca,strlength+1));
  485. end;
  486. end;
  487. st_widestring:
  488. begin
  489. { an empty ansi string is nil! }
  490. if (strlength=0) then
  491. curconstSegment.concat(Tai_const.Create_32bit(0))
  492. else
  493. begin
  494. getdatalabel(ll);
  495. curconstSegment.concat(Tai_const_symbol.Create(ll));
  496. Consts.concat(Tai_const.Create_32bit(strlength));
  497. Consts.concat(Tai_const.Create_32bit(strlength));
  498. Consts.concat(Tai_const.Create_32bit(-1));
  499. Consts.concat(Tai_label.Create(ll));
  500. for i:=0 to strlength-1 do
  501. Consts.concat(Tai_const.Create_16bit(pcompilerwidestring(strval)^.data[i]));
  502. end;
  503. end;
  504. st_longstring:
  505. begin
  506. internalerror(200107081);
  507. {curconstSegment.concat(Tai_const.Create_32bit(strlength))));
  508. curconstSegment.concat(Tai_const.Create_8bit(0));
  509. getmem(ca,strlength+1);
  510. move(strval^,ca^,strlength);
  511. ca[strlength]:=#0;
  512. generate_pascii(consts,ca,strlength);
  513. curconstSegment.concat(Tai_const.Create_8bit(0));}
  514. end;
  515. end;
  516. end;
  517. p.free;
  518. end;
  519. arraydef:
  520. begin
  521. if token=_LKLAMMER then
  522. begin
  523. consume(_LKLAMMER);
  524. for l:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange-1 do
  525. begin
  526. readtypedconst(tarraydef(t.def).elementtype,nil,no_change_allowed);
  527. consume(_COMMA);
  528. end;
  529. readtypedconst(tarraydef(t.def).elementtype,nil,no_change_allowed);
  530. consume(_RKLAMMER);
  531. end
  532. else
  533. { if array of char then we allow also a string }
  534. if is_char(tarraydef(t.def).elementtype.def) then
  535. begin
  536. p:=comp_expr(true);
  537. if p.nodetype=stringconstn then
  538. begin
  539. len:=tstringconstnode(p).len;
  540. { For tp7 the maximum lentgh can be 255 }
  541. if (m_tp in aktmodeswitches) and
  542. (len>255) then
  543. len:=255;
  544. ca:=tstringconstnode(p).value_str;
  545. end
  546. else
  547. if is_constcharnode(p) then
  548. begin
  549. c:=chr(tordconstnode(p).value and $ff);
  550. ca:=@c;
  551. len:=1;
  552. end
  553. else
  554. begin
  555. Message(cg_e_illegal_expression);
  556. len:=0;
  557. end;
  558. if len>(tarraydef(t.def).highrange-tarraydef(t.def).lowrange+1) then
  559. Message(parser_e_string_larger_array);
  560. for i:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange do
  561. begin
  562. if i+1-tarraydef(t.def).lowrange<=len then
  563. begin
  564. curconstSegment.concat(Tai_const.Create_8bit(byte(ca^)));
  565. inc(ca);
  566. end
  567. else
  568. {Fill the remaining positions with #0.}
  569. curconstSegment.concat(Tai_const.Create_8bit(0));
  570. end;
  571. p.free;
  572. end
  573. else
  574. begin
  575. { we want the ( }
  576. consume(_LKLAMMER);
  577. end;
  578. end;
  579. procvardef:
  580. begin
  581. { Procvars and pointers are no longer compatible. }
  582. { under tp: =nil or =var under fpc: =nil or =@var }
  583. if token=_NIL then
  584. begin
  585. curconstSegment.concat(Tai_const.Create_32bit(0));
  586. consume(_NIL);
  587. exit;
  588. end;
  589. getprocvar:=true;
  590. getprocvardef:=tprocvardef(t.def);
  591. p:=comp_expr(true);
  592. getprocvar:=false;
  593. if codegenerror then
  594. begin
  595. p.free;
  596. exit;
  597. end;
  598. { let type conversion check everything needed }
  599. inserttypeconv(p,t);
  600. if codegenerror then
  601. begin
  602. p.free;
  603. exit;
  604. end;
  605. { remove typeconvn, that will normally insert a lea
  606. instruction which is not necessary for us }
  607. if p.nodetype=typeconvn then
  608. begin
  609. hp:=ttypeconvnode(p).left;
  610. ttypeconvnode(p).left:=nil;
  611. p.free;
  612. p:=hp;
  613. end;
  614. { remove addrn which we also don't need here }
  615. if p.nodetype=addrn then
  616. begin
  617. hp:=taddrnode(p).left;
  618. taddrnode(p).left:=nil;
  619. p.free;
  620. p:=hp;
  621. end;
  622. { we now need to have a loadn with a procsym }
  623. if (p.nodetype=loadn) and
  624. (tloadnode(p).symtableentry.typ=procsym) then
  625. begin
  626. curconstSegment.concat(Tai_const_symbol.createname(
  627. tprocsym(tloadnode(p).symtableentry).definition.mangledname));
  628. end
  629. else
  630. Message(cg_e_illegal_expression);
  631. p.free;
  632. end;
  633. { reads a typed constant record }
  634. recorddef:
  635. begin
  636. { KAZ }
  637. if (trecorddef(t.def)=rec_tguid) and
  638. ((token=_CSTRING) or (token=_CCHAR) or (token=_ID)) then
  639. begin
  640. p:=comp_expr(true);
  641. inserttypeconv(p,cshortstringtype);
  642. if p.nodetype=stringconstn then
  643. begin
  644. s:=strpas(tstringconstnode(p).value_str);
  645. p.free;
  646. if string2guid(s,tmpguid) then
  647. begin
  648. curconstSegment.concat(Tai_const.Create_32bit(tmpguid.D1));
  649. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D2));
  650. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D3));
  651. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  652. curconstSegment.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  653. end
  654. else
  655. Message(parser_e_improper_guid_syntax);
  656. end
  657. else
  658. begin
  659. p.free;
  660. Message(cg_e_illegal_expression);
  661. exit;
  662. end;
  663. end
  664. else
  665. begin
  666. consume(_LKLAMMER);
  667. aktpos:=0;
  668. srsym := tsym(trecorddef(t.def).symtable.symindex.first);
  669. while token<>_RKLAMMER do
  670. begin
  671. s:=pattern;
  672. consume(_ID);
  673. consume(_COLON);
  674. error := false;
  675. recsym := tsym(trecorddef(t.def).symtable.search(s));
  676. if not assigned(recsym) then
  677. begin
  678. Message1(sym_e_illegal_field,s);
  679. error := true;
  680. end;
  681. if not assigned(srsym) or
  682. (s <> srsym.name) then
  683. { possible variant record (JM) }
  684. begin
  685. { All parts of a variant start at the same offset }
  686. { Also allow jumping from one variant part to another, }
  687. { as long as the offsets match }
  688. if (assigned(srsym) and
  689. (tvarsym(recsym).address = tvarsym(srsym).address)) or
  690. { srsym is not assigned after parsing w2 in the }
  691. { typed const in the next example: }
  692. { type tr = record case byte of }
  693. { 1: (l1,l2: dword); }
  694. { 2: (w1,w2: word); }
  695. { end; }
  696. { const r: tr = (w1:1;w2:1;l2:5); }
  697. (tvarsym(recsym).address = aktpos) then
  698. srsym := recsym
  699. { going backwards isn't allowed in any mode }
  700. else if (tvarsym(recsym).address<aktpos) then
  701. begin
  702. Message(parser_e_invalid_record_const);
  703. error := true;
  704. end
  705. { Delphi allows you to skip fields }
  706. else if (m_delphi in aktmodeswitches) then
  707. begin
  708. Message1(parser_w_skipped_fields_before,s);
  709. srsym := recsym;
  710. end
  711. { FPC and TP don't }
  712. else
  713. begin
  714. Message1(parser_e_skipped_fields_before,s);
  715. error := true;
  716. end;
  717. end;
  718. if error then
  719. consume_all_until(_SEMICOLON)
  720. else
  721. begin
  722. { if needed fill (alignment) }
  723. if tvarsym(srsym).address>aktpos then
  724. for i:=1 to tvarsym(srsym).address-aktpos do
  725. curconstSegment.concat(Tai_const.Create_8bit(0));
  726. { new position }
  727. aktpos:=tvarsym(srsym).address+tvarsym(srsym).vartype.def.size;
  728. { read the data }
  729. readtypedconst(tvarsym(srsym).vartype,nil,no_change_allowed);
  730. { keep previous field for checking whether whole }
  731. { record was initialized (JM) }
  732. recsym := srsym;
  733. { goto next field }
  734. srsym := tsym(srsym.indexnext);
  735. if token=_SEMICOLON then
  736. consume(_SEMICOLON)
  737. else break;
  738. end;
  739. end;
  740. { are there any fields left? }
  741. if assigned(srsym) and
  742. { don't complain if there only come other variant parts }
  743. { after the last initialized field }
  744. (tvarsym(srsym).address > tvarsym(recsym).address) then
  745. Message1(parser_h_skipped_fields_after,s);
  746. for i:=1 to t.def.size-aktpos do
  747. curconstSegment.concat(Tai_const.Create_8bit(0));
  748. consume(_RKLAMMER);
  749. end;
  750. end;
  751. { reads a typed object }
  752. objectdef:
  753. begin
  754. if is_class_or_interface(t.def) then
  755. begin
  756. p:=comp_expr(true);
  757. if p.nodetype<>niln then
  758. begin
  759. Message(parser_e_type_const_not_possible);
  760. consume_all_until(_RKLAMMER);
  761. end
  762. else
  763. begin
  764. curconstSegment.concat(Tai_const.Create_32bit(0));
  765. end;
  766. p.free;
  767. end
  768. { for objects we allow it only if it doesn't contain a vmt }
  769. else if (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  770. not(m_tp in aktmodeswitches) then
  771. Message(parser_e_type_const_not_possible)
  772. else
  773. begin
  774. consume(_LKLAMMER);
  775. aktpos:=0;
  776. while token<>_RKLAMMER do
  777. begin
  778. s:=pattern;
  779. consume(_ID);
  780. consume(_COLON);
  781. srsym:=nil;
  782. obj:=tobjectdef(t.def);
  783. symt:=obj.symtable;
  784. while (srsym=nil) and assigned(symt) do
  785. begin
  786. srsym:=tsym(symt.search(s));
  787. if assigned(obj) then
  788. obj:=obj.childof;
  789. if assigned(obj) then
  790. symt:=obj.symtable
  791. else
  792. symt:=nil;
  793. end;
  794. if srsym=nil then
  795. begin
  796. Message1(sym_e_id_not_found,s);
  797. consume_all_until(_SEMICOLON);
  798. end
  799. else
  800. begin
  801. { check position }
  802. if tvarsym(srsym).address<aktpos then
  803. Message(parser_e_invalid_record_const);
  804. { check in VMT needs to be added for TP mode }
  805. if (m_tp in aktmodeswitches) and
  806. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  807. (tobjectdef(t.def).vmt_offset<tvarsym(srsym).address) then
  808. begin
  809. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  810. curconstsegment.concat(tai_const.create_8bit(0));
  811. curconstsegment.concat(tai_const_symbol.createname(tobjectdef(t.def).vmt_mangledname));
  812. { this is more general }
  813. aktpos:=tobjectdef(t.def).vmt_offset + target_info.size_of_pointer;
  814. end;
  815. { if needed fill }
  816. if tvarsym(srsym).address>aktpos then
  817. for i:=1 to tvarsym(srsym).address-aktpos do
  818. curconstSegment.concat(Tai_const.Create_8bit(0));
  819. { new position }
  820. aktpos:=tvarsym(srsym).address+tvarsym(srsym).vartype.def.size;
  821. { read the data }
  822. readtypedconst(tvarsym(srsym).vartype,nil,no_change_allowed);
  823. if token=_SEMICOLON then
  824. consume(_SEMICOLON)
  825. else break;
  826. end;
  827. end;
  828. if (m_tp in aktmodeswitches) and
  829. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  830. (tobjectdef(t.def).vmt_offset>=aktpos) then
  831. begin
  832. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  833. curconstsegment.concat(tai_const.create_8bit(0));
  834. curconstsegment.concat(tai_const_symbol.createname(tobjectdef(t.def).vmt_mangledname));
  835. { this is more general }
  836. aktpos:=tobjectdef(t.def).vmt_offset + target_info.size_of_pointer;
  837. end;
  838. for i:=1 to t.def.size-aktpos do
  839. curconstSegment.concat(Tai_const.Create_8bit(0));
  840. consume(_RKLAMMER);
  841. end;
  842. end;
  843. errordef:
  844. begin
  845. { try to consume something useful }
  846. if token=_LKLAMMER then
  847. consume_all_until(_RKLAMMER)
  848. else
  849. consume_all_until(_SEMICOLON);
  850. end;
  851. else Message(parser_e_type_const_not_possible);
  852. end;
  853. end;
  854. {$ifdef fpc}
  855. {$maxfpuregisters default}
  856. {$endif fpc}
  857. end.
  858. {
  859. $Log$
  860. Revision 1.28 2001-07-30 20:59:27 peter
  861. * m68k updates from v10 merged
  862. Revision 1.27 2001/07/08 21:00:15 peter
  863. * various widestring updates, it works now mostly without charset
  864. mapping supported
  865. Revision 1.26 2001/06/29 14:16:57 jonas
  866. * fixed inconsistent handling of procvars in FPC mode (sometimes @ was
  867. required to assign the address of a procedure to a procvar, sometimes
  868. not. Now it is always required) (merged)
  869. Revision 1.25 2001/06/27 21:37:36 peter
  870. * v10 merges
  871. Revision 1.24 2001/06/18 20:36:25 peter
  872. * -Ur switch (merged)
  873. * masm fixes (merged)
  874. * quoted filenames for go32v2 and win32
  875. Revision 1.23 2001/05/06 17:15:00 jonas
  876. + detect incomplete typed constant records
  877. Revision 1.22 2001/04/18 22:01:57 peter
  878. * registration of targets and assemblers
  879. Revision 1.21 2001/04/13 01:22:13 peter
  880. * symtable change to classes
  881. * range check generation and errors fixed, make cycle DEBUG=1 works
  882. * memory leaks fixed
  883. Revision 1.20 2001/04/04 22:43:53 peter
  884. * remove unnecessary calls to firstpass
  885. Revision 1.19 2001/04/02 21:20:34 peter
  886. * resulttype rewrite
  887. Revision 1.18 2001/03/11 22:58:50 peter
  888. * getsym redesign, removed the globals srsym,srsymtable
  889. Revision 1.17 2001/02/04 11:12:16 jonas
  890. * fixed web bug 1377 & const pointer arithmtic
  891. Revision 1.16 2001/02/03 00:26:35 peter
  892. * merged fix for bug 1365
  893. Revision 1.15 2000/12/25 00:07:28 peter
  894. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  895. tlinkedlist objects)
  896. Revision 1.14 2000/12/10 20:24:18 peter
  897. * allow subtypes for enums
  898. Revision 1.13 2000/11/29 00:30:38 florian
  899. * unused units removed from uses clause
  900. * some changes for widestrings
  901. Revision 1.12 2000/11/06 15:54:15 florian
  902. * fixed two bugs to get make cycle work, but it's not enough
  903. Revision 1.11 2000/11/04 14:25:21 florian
  904. + merged Attila's changes for interfaces, not tested yet
  905. Revision 1.10 2000/10/31 22:02:51 peter
  906. * symtable splitted, no real code changes
  907. Revision 1.9 2000/10/14 10:14:52 peter
  908. * moehrendorf oct 2000 rewrite
  909. Revision 1.8 2000/09/30 13:23:04 peter
  910. * const array of char and pchar length fixed (merged)
  911. Revision 1.7 2000/09/24 15:06:25 peter
  912. * use defines.inc
  913. Revision 1.6 2000/08/27 16:11:52 peter
  914. * moved some util functions from globals,cobjects to cutils
  915. * splitted files into finput,fmodule
  916. Revision 1.5 2000/08/24 19:13:18 peter
  917. * allow nil for class typed consts (merged)
  918. Revision 1.4 2000/08/16 13:06:06 florian
  919. + support of 64 bit integer constants
  920. Revision 1.3 2000/08/05 13:25:06 peter
  921. * packenum 1 fixes (merged)
  922. Revision 1.2 2000/07/13 11:32:47 michael
  923. + removed logs
  924. }