ptconst.pas 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 fpcdefs.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;writable : boolean);
  26. implementation
  27. uses
  28. {$ifdef Delphi}
  29. sysutils,
  30. {$else}
  31. strings,
  32. {$endif Delphi}
  33. globtype,systems,tokens,
  34. cutils,globals,widestr,scanner,
  35. symconst,symbase,symdef,aasmbase,aasmtai,aasmcpu,types,verbose,
  36. { pass 1 }
  37. node,
  38. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,
  39. { parser specific stuff }
  40. pbase,pexpr,
  41. { codegen }
  42. cpuinfo,cgbase
  43. ;
  44. {$ifdef fpc}
  45. {$maxfpuregisters 0}
  46. {$endif fpc}
  47. { this procedure reads typed constants }
  48. procedure readtypedconst(const t:ttype;sym : ttypedconstsym;writable : boolean);
  49. var
  50. len,base : longint;
  51. p,hp,hpstart : tnode;
  52. i,j,l,offset,
  53. varalign,
  54. strlength : longint;
  55. curconstsegment : TAAsmoutput;
  56. ll : tasmlabel;
  57. s,sorg : string;
  58. c : char;
  59. ca : pchar;
  60. tmpguid : tguid;
  61. aktpos : longint;
  62. obj : tobjectdef;
  63. recsym,
  64. srsym : tsym;
  65. symt : tsymtable;
  66. value : bestreal;
  67. strval : pchar;
  68. pw : pcompilerwidestring;
  69. error : boolean;
  70. procedure check_range(def:torddef);
  71. begin
  72. if ((tordconstnode(p).value>def.high) or
  73. (tordconstnode(p).value<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 writable then
  84. curconstsegment:=datasegment
  85. else
  86. curconstsegment:=consts;
  87. case t.def.deftype of
  88. orddef:
  89. begin
  90. p:=comp_expr(true);
  91. case torddef(t.def).typ of
  92. bool8bit :
  93. begin
  94. if is_constboolnode(p) then
  95. curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value))
  96. else
  97. Message(cg_e_illegal_expression);
  98. end;
  99. bool16bit :
  100. begin
  101. if is_constboolnode(p) then
  102. curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value))
  103. else
  104. Message(cg_e_illegal_expression);
  105. end;
  106. bool32bit :
  107. begin
  108. if is_constboolnode(p) then
  109. curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value))
  110. else
  111. Message(cg_e_illegal_expression);
  112. end;
  113. uchar :
  114. begin
  115. if is_constcharnode(p) then
  116. curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value))
  117. else
  118. Message(cg_e_illegal_expression);
  119. end;
  120. uwidechar :
  121. begin
  122. if is_constcharnode(p) then
  123. curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value))
  124. else
  125. Message(cg_e_illegal_expression);
  126. end;
  127. s8bit,
  128. u8bit :
  129. begin
  130. if is_constintnode(p) then
  131. begin
  132. curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value));
  133. check_range(torddef(t.def));
  134. end
  135. else
  136. Message(cg_e_illegal_expression);
  137. end;
  138. u16bit,
  139. s16bit :
  140. begin
  141. if is_constintnode(p) then
  142. begin
  143. curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value));
  144. check_range(torddef(t.def));
  145. end
  146. else
  147. Message(cg_e_illegal_expression);
  148. end;
  149. s32bit,
  150. u32bit :
  151. begin
  152. if is_constintnode(p) then
  153. begin
  154. curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value));
  155. if torddef(t.def).typ<>u32bit then
  156. check_range(torddef(t.def));
  157. end
  158. else
  159. Message(cg_e_illegal_expression);
  160. end;
  161. s64bit,
  162. u64bit:
  163. begin
  164. if is_constintnode(p) then
  165. begin
  166. if target_info.endian = endian_little then
  167. begin
  168. curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value));
  169. if (tordconstnode(p).value<0) and (torddef(t.def).typ = s64bit) then
  170. curconstSegment.concat(Tai_const.Create_32bit(-1))
  171. else
  172. curconstSegment.concat(Tai_const.Create_32bit(0));
  173. end
  174. else
  175. begin
  176. if (tordconstnode(p).value<0) and (torddef(t.def).typ = s64bit) then
  177. curconstSegment.concat(Tai_const.Create_32bit(-1))
  178. else
  179. curconstSegment.concat(Tai_const.Create_32bit(0));
  180. curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value));
  181. end;
  182. end
  183. else
  184. Message(cg_e_illegal_expression);
  185. end;
  186. else
  187. internalerror(3799);
  188. end;
  189. p.free;
  190. end;
  191. floatdef:
  192. begin
  193. p:=comp_expr(true);
  194. if is_constrealnode(p) then
  195. value:=trealconstnode(p).value_real
  196. else if is_constintnode(p) then
  197. value:=tordconstnode(p).value
  198. else
  199. Message(cg_e_illegal_expression);
  200. case tfloatdef(t.def).typ of
  201. s32real :
  202. curconstSegment.concat(Tai_real_32bit.Create(value));
  203. s64real :
  204. curconstSegment.concat(Tai_real_64bit.Create(value));
  205. s80real :
  206. curconstSegment.concat(Tai_real_80bit.Create(value));
  207. s64comp :
  208. curconstSegment.concat(Tai_comp_64bit.Create(value));
  209. else
  210. internalerror(18);
  211. end;
  212. p.free;
  213. end;
  214. classrefdef:
  215. begin
  216. p:=comp_expr(true);
  217. case p.nodetype of
  218. loadvmtn:
  219. begin
  220. if not(tobjectdef(tclassrefdef(p.resulttype.def).pointertype.def).is_related(
  221. tobjectdef(tclassrefdef(t.def).pointertype.def))) then
  222. Message(cg_e_illegal_expression);
  223. curconstSegment.concat(Tai_const_symbol.Create(newasmsymbol(tobjectdef(
  224. tclassrefdef(p.resulttype.def).pointertype.def).vmt_mangledname)));
  225. end;
  226. niln:
  227. curconstSegment.concat(Tai_const.Create_32bit(0));
  228. else Message(cg_e_illegal_expression);
  229. end;
  230. p.free;
  231. end;
  232. pointerdef:
  233. begin
  234. p:=comp_expr(true);
  235. if (p.nodetype=typeconvn) and
  236. (ttypeconvnode(p).left.nodetype in [addrn,niln]) and
  237. is_equal(t.def,p.resulttype.def) then
  238. begin
  239. hp:=ttypeconvnode(p).left;
  240. ttypeconvnode(p).left:=nil;
  241. p.free;
  242. p:=hp;
  243. end;
  244. { allows horrible ofs(typeof(TButton)^) code !! }
  245. if (p.nodetype=addrn) and
  246. (taddrnode(p).left.nodetype=derefn) then
  247. begin
  248. hp:=tderefnode(taddrnode(p).left).left;
  249. tderefnode(taddrnode(p).left).left:=nil;
  250. p.free;
  251. p:=hp;
  252. end;
  253. { const pointer ? }
  254. if (p.nodetype = pointerconstn) then
  255. curconstsegment.concat(Tai_const.Create_32bit(
  256. tpointerconstnode(p).value))
  257. { nil pointer ? }
  258. else if p.nodetype=niln then
  259. curconstSegment.concat(Tai_const.Create_32bit(0))
  260. { maybe pchar ? }
  261. else
  262. if is_char(tpointerdef(t.def).pointertype.def) and
  263. (p.nodetype<>addrn) then
  264. begin
  265. getdatalabel(ll);
  266. curconstSegment.concat(Tai_const_symbol.Create(ll));
  267. if p.nodetype=stringconstn then
  268. varalign:=size_2_align(tstringconstnode(p).len)
  269. else
  270. varalign:=0;
  271. varalign:=used_align(varalign,aktalignment.varalignmin,aktalignment.varalignmax);
  272. Consts.concat(Tai_align.Create(varalign));
  273. Consts.concat(Tai_label.Create(ll));
  274. if p.nodetype=stringconstn then
  275. begin
  276. len:=tstringconstnode(p).len;
  277. { For tp7 the maximum lentgh can be 255 }
  278. if (m_tp7 in aktmodeswitches) and
  279. (len>255) then
  280. len:=255;
  281. getmem(ca,len+2);
  282. move(tstringconstnode(p).value_str^,ca^,len+1);
  283. Consts.concat(Tai_string.Create_length_pchar(ca,len+1));
  284. end
  285. else
  286. if is_constcharnode(p) then
  287. Consts.concat(Tai_string.Create(char(byte(tordconstnode(p).value))+#0))
  288. else
  289. Message(cg_e_illegal_expression);
  290. end
  291. { maybe pwidechar ? }
  292. else
  293. if is_widechar(tpointerdef(t.def).pointertype.def) and
  294. (p.nodetype<>addrn) then
  295. begin
  296. getdatalabel(ll);
  297. curconstSegment.concat(Tai_const_symbol.Create(ll));
  298. Consts.concat(Tai_label.Create(ll));
  299. if (p.nodetype in [stringconstn,ordconstn]) then
  300. begin
  301. { convert to widestring stringconstn }
  302. inserttypeconv(p,cwidestringtype);
  303. if (p.nodetype=stringconstn) and
  304. (tstringconstnode(p).st_type=st_widestring) then
  305. begin
  306. pw:=pcompilerwidestring(tstringconstnode(p).value_str);
  307. for i:=0 to tstringconstnode(p).len-1 do
  308. Consts.concat(Tai_const.Create_16bit(pw^.data[i]));
  309. { ending #0 }
  310. Consts.concat(Tai_const.Create_16bit(0))
  311. end;
  312. end
  313. else
  314. Message(cg_e_illegal_expression);
  315. end
  316. else
  317. if p.nodetype=addrn then
  318. begin
  319. inserttypeconv(p,t);
  320. { if a typeconv node was inserted then check if it was an tc_equal. If
  321. true then we remove the node. If not tc_equal then we leave the typeconvn
  322. and the nodetype=loadn will always be false and generate the error (PFV) }
  323. if (p.nodetype=typeconvn) then
  324. begin
  325. if (ttypeconvnode(p).convtype=tc_equal) then
  326. hpstart:=taddrnode(ttypeconvnode(p).left).left
  327. else
  328. hpstart:=p;
  329. end
  330. else
  331. hpstart:=taddrnode(p).left;
  332. hp:=hpstart;
  333. while assigned(hp) and (hp.nodetype in [subscriptn,vecn]) do
  334. hp:=tunarynode(hp).left;
  335. if (hp.nodetype=loadn) then
  336. begin
  337. hp:=hpstart;
  338. offset:=0;
  339. while assigned(hp) and (hp.nodetype<>loadn) do
  340. begin
  341. case hp.nodetype of
  342. vecn :
  343. begin
  344. case tvecnode(hp).left.resulttype.def.deftype of
  345. stringdef :
  346. begin
  347. { this seems OK for shortstring and ansistrings PM }
  348. { it is wrong for widestrings !! }
  349. len:=1;
  350. base:=0;
  351. end;
  352. arraydef :
  353. begin
  354. len:=tarraydef(tvecnode(hp).left.resulttype.def).elesize;
  355. base:=tarraydef(tvecnode(hp).left.resulttype.def).lowrange;
  356. end
  357. else
  358. Message(cg_e_illegal_expression);
  359. end;
  360. if is_constintnode(tvecnode(hp).right) then
  361. inc(offset,len*(get_ordinal_value(tvecnode(hp).right)-base))
  362. else
  363. Message(cg_e_illegal_expression);
  364. end;
  365. subscriptn :
  366. inc(offset,tsubscriptnode(hp).vs.address)
  367. else
  368. Message(cg_e_illegal_expression);
  369. end;
  370. hp:=tbinarynode(hp).left;
  371. end;
  372. srsym:=tloadnode(hp).symtableentry;
  373. case srsym.typ of
  374. procsym :
  375. begin
  376. if assigned(tprocsym(srsym).defs^.next) then
  377. Message(parser_e_no_overloaded_procvars);
  378. curconstSegment.concat(Tai_const_symbol.Createname_offset(tprocsym(srsym).defs^.def.mangledname,offset));
  379. end;
  380. varsym :
  381. curconstSegment.concat(Tai_const_symbol.Createname_offset(tvarsym(srsym).mangledname,offset));
  382. typedconstsym :
  383. curconstSegment.concat(Tai_const_symbol.Createname_offset(ttypedconstsym(srsym).mangledname,offset));
  384. else
  385. Message(type_e_variable_id_expected);
  386. end;
  387. end
  388. else
  389. Message(cg_e_illegal_expression);
  390. end
  391. else
  392. { allow typeof(Object type)}
  393. if (p.nodetype=inlinen) and
  394. (tinlinenode(p).inlinenumber=in_typeof_x) then
  395. begin
  396. if (tinlinenode(p).left.nodetype=typen) then
  397. begin
  398. curconstSegment.concat(Tai_const_symbol.createname(
  399. tobjectdef(tinlinenode(p).left.resulttype.def).vmt_mangledname));
  400. end
  401. else
  402. Message(cg_e_illegal_expression);
  403. end
  404. else
  405. Message(cg_e_illegal_expression);
  406. p.free;
  407. end;
  408. setdef:
  409. begin
  410. p:=comp_expr(true);
  411. if p.nodetype=setconstn then
  412. begin
  413. { be sure to convert to the correct result, else
  414. it can generate smallset data instead of normalset (PFV) }
  415. inserttypeconv(p,t);
  416. { we only allow const sets }
  417. if assigned(tsetconstnode(p).left) then
  418. Message(cg_e_illegal_expression)
  419. else
  420. begin
  421. { this writing is endian independant }
  422. { untrue - because they are considered }
  423. { arrays of 32-bit values CEC }
  424. if source_info.endian = target_info.endian then
  425. begin
  426. for l:= 0 to p.resulttype.def.size-1 do
  427. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[l]));
  428. end
  429. else
  430. begin
  431. { store as longint values in swaped format }
  432. j:=0;
  433. for l:=0 to ((p.resulttype.def.size-1) div 4) do
  434. begin
  435. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j+3]));
  436. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j+2]));
  437. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j+1]));
  438. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j]));
  439. Inc(j,4);
  440. end;
  441. end;
  442. end;
  443. end
  444. else
  445. Message(cg_e_illegal_expression);
  446. p.free;
  447. end;
  448. enumdef:
  449. begin
  450. p:=comp_expr(true);
  451. if p.nodetype=ordconstn then
  452. begin
  453. if is_equal(p.resulttype.def,t.def) or
  454. is_subequal(p.resulttype.def,t.def) then
  455. begin
  456. case p.resulttype.def.size of
  457. 1 : curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value));
  458. 2 : curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value));
  459. 4 : curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value));
  460. end;
  461. end
  462. else
  463. Message2(type_e_incompatible_types,t.def.typename,p.resulttype.def.typename);
  464. end
  465. else
  466. Message(cg_e_illegal_expression);
  467. p.free;
  468. end;
  469. stringdef:
  470. begin
  471. p:=comp_expr(true);
  472. { load strval and strlength of the constant tree }
  473. if p.nodetype=stringconstn then
  474. begin
  475. { convert to the expected string type so that
  476. for widestrings strval is a pcompilerwidestring }
  477. inserttypeconv(p,t);
  478. strlength:=tstringconstnode(p).len;
  479. strval:=tstringconstnode(p).value_str;
  480. end
  481. else if is_constcharnode(p) then
  482. begin
  483. { strval:=pchar(@tordconstnode(p).value);
  484. THIS FAIL on BIG_ENDIAN MACHINES PM }
  485. c:=chr(tordconstnode(p).value and $ff);
  486. strval:=@c;
  487. strlength:=1
  488. end
  489. else if is_constresourcestringnode(p) then
  490. begin
  491. strval:=pchar(tconstsym(tloadnode(p).symtableentry).valueptr);
  492. strlength:=tconstsym(tloadnode(p).symtableentry).len;
  493. end
  494. else
  495. begin
  496. Message(cg_e_illegal_expression);
  497. strlength:=-1;
  498. end;
  499. if strlength>=0 then
  500. begin
  501. case tstringdef(t.def).string_typ of
  502. st_shortstring:
  503. begin
  504. if strlength>=t.def.size then
  505. begin
  506. message2(parser_w_string_too_long,strpas(strval),tostr(t.def.size-1));
  507. strlength:=t.def.size-1;
  508. end;
  509. curconstSegment.concat(Tai_const.Create_8bit(strlength));
  510. { this can also handle longer strings }
  511. getmem(ca,strlength+1);
  512. move(strval^,ca^,strlength);
  513. ca[strlength]:=#0;
  514. curconstSegment.concat(Tai_string.Create_length_pchar(ca,strlength));
  515. { fillup with spaces if size is shorter }
  516. if t.def.size>strlength then
  517. begin
  518. getmem(ca,t.def.size-strlength);
  519. { def.size contains also the leading length, so we }
  520. { we have to subtract one }
  521. fillchar(ca[0],t.def.size-strlength-1,' ');
  522. ca[t.def.size-strlength-1]:=#0;
  523. { this can also handle longer strings }
  524. curconstSegment.concat(Tai_string.Create_length_pchar(ca,t.def.size-strlength-1));
  525. end;
  526. end;
  527. st_ansistring:
  528. begin
  529. { an empty ansi string is nil! }
  530. if (strlength=0) then
  531. curconstSegment.concat(Tai_const.Create_32bit(0))
  532. else
  533. begin
  534. getdatalabel(ll);
  535. curconstSegment.concat(Tai_const_symbol.Create(ll));
  536. { first write the maximum size }
  537. Consts.concat(Tai_const.Create_32bit(strlength));
  538. { second write the real length }
  539. Consts.concat(Tai_const.Create_32bit(strlength));
  540. { redondent with maxlength but who knows ... (PM) }
  541. { third write use count (set to -1 for safety ) }
  542. Consts.concat(Tai_const.Create_32bit(-1));
  543. Consts.concat(Tai_label.Create(ll));
  544. getmem(ca,strlength+2);
  545. move(strval^,ca^,strlength);
  546. { The terminating #0 to be stored in the .data section (JM) }
  547. ca[strlength]:=#0;
  548. { End of the PChar. The memory has to be allocated because in }
  549. { tai_string.done, there is a freemem(len+1) (JM) }
  550. ca[strlength+1]:=#0;
  551. Consts.concat(Tai_string.Create_length_pchar(ca,strlength+1));
  552. end;
  553. end;
  554. st_widestring:
  555. begin
  556. { an empty ansi string is nil! }
  557. if (strlength=0) then
  558. curconstSegment.concat(Tai_const.Create_32bit(0))
  559. else
  560. begin
  561. getdatalabel(ll);
  562. curconstSegment.concat(Tai_const_symbol.Create(ll));
  563. Consts.concat(Tai_const.Create_32bit(strlength));
  564. Consts.concat(Tai_const.Create_32bit(strlength));
  565. Consts.concat(Tai_const.Create_32bit(-1));
  566. Consts.concat(Tai_label.Create(ll));
  567. for i:=0 to strlength-1 do
  568. Consts.concat(Tai_const.Create_16bit(pcompilerwidestring(strval)^.data[i]));
  569. { ending #0 }
  570. Consts.concat(Tai_const.Create_16bit(0))
  571. end;
  572. end;
  573. st_longstring:
  574. begin
  575. internalerror(200107081);
  576. {curconstSegment.concat(Tai_const.Create_32bit(strlength))));
  577. curconstSegment.concat(Tai_const.Create_8bit(0));
  578. getmem(ca,strlength+1);
  579. move(strval^,ca^,strlength);
  580. ca[strlength]:=#0;
  581. generate_pascii(consts,ca,strlength);
  582. curconstSegment.concat(Tai_const.Create_8bit(0));}
  583. end;
  584. end;
  585. end;
  586. p.free;
  587. end;
  588. arraydef:
  589. begin
  590. if token=_LKLAMMER then
  591. begin
  592. consume(_LKLAMMER);
  593. for l:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange-1 do
  594. begin
  595. readtypedconst(tarraydef(t.def).elementtype,nil,writable);
  596. consume(_COMMA);
  597. end;
  598. readtypedconst(tarraydef(t.def).elementtype,nil,writable);
  599. consume(_RKLAMMER);
  600. end
  601. else
  602. { if array of char then we allow also a string }
  603. if is_char(tarraydef(t.def).elementtype.def) then
  604. begin
  605. p:=comp_expr(true);
  606. if p.nodetype=stringconstn then
  607. begin
  608. len:=tstringconstnode(p).len;
  609. { For tp7 the maximum lentgh can be 255 }
  610. if (m_tp7 in aktmodeswitches) and
  611. (len>255) then
  612. len:=255;
  613. ca:=tstringconstnode(p).value_str;
  614. end
  615. else
  616. if is_constcharnode(p) then
  617. begin
  618. c:=chr(tordconstnode(p).value and $ff);
  619. ca:=@c;
  620. len:=1;
  621. end
  622. else
  623. begin
  624. Message(cg_e_illegal_expression);
  625. len:=0;
  626. end;
  627. if len>(tarraydef(t.def).highrange-tarraydef(t.def).lowrange+1) then
  628. Message(parser_e_string_larger_array);
  629. for i:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange do
  630. begin
  631. if i+1-tarraydef(t.def).lowrange<=len then
  632. begin
  633. curconstSegment.concat(Tai_const.Create_8bit(byte(ca^)));
  634. inc(ca);
  635. end
  636. else
  637. {Fill the remaining positions with #0.}
  638. curconstSegment.concat(Tai_const.Create_8bit(0));
  639. end;
  640. p.free;
  641. end
  642. else
  643. begin
  644. { we want the ( }
  645. consume(_LKLAMMER);
  646. end;
  647. end;
  648. procvardef:
  649. begin
  650. { Procvars and pointers are no longer compatible. }
  651. { under tp: =nil or =var under fpc: =nil or =@var }
  652. if token=_NIL then
  653. begin
  654. curconstSegment.concat(Tai_const.Create_32bit(0));
  655. if (po_methodpointer in tprocvardef(t.def).procoptions) then
  656. curconstSegment.concat(Tai_const.Create_32bit(0));
  657. consume(_NIL);
  658. exit;
  659. end;
  660. { you can't assign a value other than NIL to a typed constant }
  661. { which is a "procedure of object", because this also requires }
  662. { address of an object/class instance, which is not known at }
  663. { compile time (JM) }
  664. if (po_methodpointer in tprocvardef(t.def).procoptions) then
  665. Message(parser_e_no_procvarobj_const);
  666. { parse the rest too, so we can continue with error checking }
  667. getprocvardef:=tprocvardef(t.def);
  668. p:=comp_expr(true);
  669. getprocvardef:=nil;
  670. if codegenerror then
  671. begin
  672. p.free;
  673. exit;
  674. end;
  675. { let type conversion check everything needed }
  676. inserttypeconv(p,t);
  677. if codegenerror then
  678. begin
  679. p.free;
  680. exit;
  681. end;
  682. { remove typeconvn, that will normally insert a lea
  683. instruction which is not necessary for us }
  684. if p.nodetype=typeconvn then
  685. begin
  686. hp:=ttypeconvnode(p).left;
  687. ttypeconvnode(p).left:=nil;
  688. p.free;
  689. p:=hp;
  690. end;
  691. { remove addrn which we also don't need here }
  692. if p.nodetype=addrn then
  693. begin
  694. hp:=taddrnode(p).left;
  695. taddrnode(p).left:=nil;
  696. p.free;
  697. p:=hp;
  698. end;
  699. { we now need to have a loadn with a procsym }
  700. if (p.nodetype=loadn) and
  701. (tloadnode(p).symtableentry.typ=procsym) then
  702. begin
  703. curconstSegment.concat(Tai_const_symbol.createname(
  704. tprocsym(tloadnode(p).symtableentry).defs^.def.mangledname));
  705. end
  706. else
  707. Message(cg_e_illegal_expression);
  708. p.free;
  709. end;
  710. { reads a typed constant record }
  711. recorddef:
  712. begin
  713. { KAZ }
  714. if (trecorddef(t.def)=rec_tguid) and
  715. ((token=_CSTRING) or (token=_CCHAR) or (token=_ID)) then
  716. begin
  717. p:=comp_expr(true);
  718. inserttypeconv(p,cshortstringtype);
  719. if p.nodetype=stringconstn then
  720. begin
  721. s:=strpas(tstringconstnode(p).value_str);
  722. p.free;
  723. if string2guid(s,tmpguid) then
  724. begin
  725. curconstSegment.concat(Tai_const.Create_32bit(tmpguid.D1));
  726. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D2));
  727. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D3));
  728. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  729. curconstSegment.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  730. end
  731. else
  732. Message(parser_e_improper_guid_syntax);
  733. end
  734. else
  735. begin
  736. p.free;
  737. Message(cg_e_illegal_expression);
  738. exit;
  739. end;
  740. end
  741. else
  742. begin
  743. consume(_LKLAMMER);
  744. aktpos:=0;
  745. srsym := tsym(trecorddef(t.def).symtable.symindex.first);
  746. recsym := nil;
  747. while token<>_RKLAMMER do
  748. begin
  749. s:=pattern;
  750. sorg:=orgpattern;
  751. consume(_ID);
  752. consume(_COLON);
  753. error := false;
  754. recsym := tsym(trecorddef(t.def).symtable.search(s));
  755. if not assigned(recsym) then
  756. begin
  757. Message1(sym_e_illegal_field,s);
  758. error := true;
  759. end;
  760. if (not error) and
  761. (not assigned(srsym) or
  762. (s <> srsym.name)) then
  763. { possible variant record (JM) }
  764. begin
  765. { All parts of a variant start at the same offset }
  766. { Also allow jumping from one variant part to another, }
  767. { as long as the offsets match }
  768. if (assigned(srsym) and
  769. (tvarsym(recsym).address = tvarsym(srsym).address)) or
  770. { srsym is not assigned after parsing w2 in the }
  771. { typed const in the next example: }
  772. { type tr = record case byte of }
  773. { 1: (l1,l2: dword); }
  774. { 2: (w1,w2: word); }
  775. { end; }
  776. { const r: tr = (w1:1;w2:1;l2:5); }
  777. (tvarsym(recsym).address = aktpos) then
  778. srsym := recsym
  779. { going backwards isn't allowed in any mode }
  780. else if (tvarsym(recsym).address<aktpos) then
  781. begin
  782. Message(parser_e_invalid_record_const);
  783. error := true;
  784. end
  785. { Delphi allows you to skip fields }
  786. else if (m_delphi in aktmodeswitches) then
  787. begin
  788. Message1(parser_w_skipped_fields_before,sorg);
  789. srsym := recsym;
  790. end
  791. { FPC and TP don't }
  792. else
  793. begin
  794. Message1(parser_e_skipped_fields_before,sorg);
  795. error := true;
  796. end;
  797. end;
  798. if error then
  799. consume_all_until(_SEMICOLON)
  800. else
  801. begin
  802. { if needed fill (alignment) }
  803. if tvarsym(srsym).address>aktpos then
  804. for i:=1 to tvarsym(srsym).address-aktpos do
  805. curconstSegment.concat(Tai_const.Create_8bit(0));
  806. { new position }
  807. aktpos:=tvarsym(srsym).address+tvarsym(srsym).vartype.def.size;
  808. { read the data }
  809. readtypedconst(tvarsym(srsym).vartype,nil,writable);
  810. { keep previous field for checking whether whole }
  811. { record was initialized (JM) }
  812. recsym := srsym;
  813. { goto next field }
  814. srsym := tsym(srsym.indexnext);
  815. if token=_SEMICOLON then
  816. consume(_SEMICOLON)
  817. else break;
  818. end;
  819. end;
  820. { are there any fields left? }
  821. if assigned(srsym) and
  822. { don't complain if there only come other variant parts }
  823. { after the last initialized field }
  824. ((recsym=nil) or
  825. (tvarsym(srsym).address > tvarsym(recsym).address)) then
  826. Message1(parser_h_skipped_fields_after,s);
  827. for i:=1 to t.def.size-aktpos do
  828. curconstSegment.concat(Tai_const.Create_8bit(0));
  829. consume(_RKLAMMER);
  830. end;
  831. end;
  832. { reads a typed object }
  833. objectdef:
  834. begin
  835. if is_class_or_interface(t.def) then
  836. begin
  837. p:=comp_expr(true);
  838. if p.nodetype<>niln then
  839. begin
  840. Message(parser_e_type_const_not_possible);
  841. consume_all_until(_RKLAMMER);
  842. end
  843. else
  844. begin
  845. curconstSegment.concat(Tai_const.Create_32bit(0));
  846. end;
  847. p.free;
  848. end
  849. { for objects we allow it only if it doesn't contain a vmt }
  850. else if (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  851. (m_fpc in aktmodeswitches) then
  852. Message(parser_e_type_const_not_possible)
  853. else
  854. begin
  855. consume(_LKLAMMER);
  856. aktpos:=0;
  857. while token<>_RKLAMMER do
  858. begin
  859. s:=pattern;
  860. sorg:=orgpattern;
  861. consume(_ID);
  862. consume(_COLON);
  863. srsym:=nil;
  864. obj:=tobjectdef(t.def);
  865. symt:=obj.symtable;
  866. while (srsym=nil) and assigned(symt) do
  867. begin
  868. srsym:=tsym(symt.search(s));
  869. if assigned(obj) then
  870. obj:=obj.childof;
  871. if assigned(obj) then
  872. symt:=obj.symtable
  873. else
  874. symt:=nil;
  875. end;
  876. if srsym=nil then
  877. begin
  878. Message1(sym_e_id_not_found,sorg);
  879. consume_all_until(_SEMICOLON);
  880. end
  881. else
  882. begin
  883. { check position }
  884. if tvarsym(srsym).address<aktpos then
  885. Message(parser_e_invalid_record_const);
  886. { check in VMT needs to be added for TP mode }
  887. if not(m_fpc in aktmodeswitches) and
  888. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  889. (tobjectdef(t.def).vmt_offset<tvarsym(srsym).address) then
  890. begin
  891. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  892. curconstsegment.concat(tai_const.create_8bit(0));
  893. curconstsegment.concat(tai_const_symbol.createname(tobjectdef(t.def).vmt_mangledname));
  894. { this is more general }
  895. aktpos:=tobjectdef(t.def).vmt_offset + pointer_size;
  896. end;
  897. { if needed fill }
  898. if tvarsym(srsym).address>aktpos then
  899. for i:=1 to tvarsym(srsym).address-aktpos do
  900. curconstSegment.concat(Tai_const.Create_8bit(0));
  901. { new position }
  902. aktpos:=tvarsym(srsym).address+tvarsym(srsym).vartype.def.size;
  903. { read the data }
  904. readtypedconst(tvarsym(srsym).vartype,nil,writable);
  905. if token=_SEMICOLON then
  906. consume(_SEMICOLON)
  907. else break;
  908. end;
  909. end;
  910. if not(m_fpc in aktmodeswitches) and
  911. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  912. (tobjectdef(t.def).vmt_offset>=aktpos) then
  913. begin
  914. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  915. curconstsegment.concat(tai_const.create_8bit(0));
  916. curconstsegment.concat(tai_const_symbol.createname(tobjectdef(t.def).vmt_mangledname));
  917. { this is more general }
  918. aktpos:=tobjectdef(t.def).vmt_offset + pointer_size;
  919. end;
  920. for i:=1 to t.def.size-aktpos do
  921. curconstSegment.concat(Tai_const.Create_8bit(0));
  922. consume(_RKLAMMER);
  923. end;
  924. end;
  925. errordef:
  926. begin
  927. { try to consume something useful }
  928. if token=_LKLAMMER then
  929. consume_all_until(_RKLAMMER)
  930. else
  931. consume_all_until(_SEMICOLON);
  932. end;
  933. else Message(parser_e_type_const_not_possible);
  934. end;
  935. end;
  936. {$ifdef fpc}
  937. {$maxfpuregisters default}
  938. {$endif fpc}
  939. end.
  940. {
  941. $Log$
  942. Revision 1.50 2002-07-01 18:46:25 peter
  943. * internal linker
  944. * reorganized aasm layer
  945. Revision 1.49 2002/05/18 13:34:16 peter
  946. * readded missing revisions
  947. Revision 1.48 2002/05/16 19:46:44 carl
  948. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  949. + try to fix temp allocation (still in ifdef)
  950. + generic constructor calls
  951. + start of tassembler / tmodulebase class cleanup
  952. Revision 1.46 2002/05/12 16:53:09 peter
  953. * moved entry and exitcode to ncgutil and cgobj
  954. * foreach gets extra argument for passing local data to the
  955. iterator function
  956. * -CR checks also class typecasts at runtime by changing them
  957. into as
  958. * fixed compiler to cycle with the -CR option
  959. * fixed stabs with elf writer, finally the global variables can
  960. be watched
  961. * removed a lot of routines from cga unit and replaced them by
  962. calls to cgobj
  963. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  964. u32bit then the other is typecasted also to u32bit without giving
  965. a rangecheck warning/error.
  966. * fixed pascal calling method with reversing also the high tree in
  967. the parast, detected by tcalcst3 test
  968. Revision 1.45 2002/04/23 19:16:35 peter
  969. * add pinline unit that inserts compiler supported functions using
  970. one or more statements
  971. * moved finalize and setlength from ninl to pinline
  972. Revision 1.44 2002/04/20 21:32:24 carl
  973. + generic FPC_CHECKPOINTER
  974. + first parameter offset in stack now portable
  975. * rename some constants
  976. + move some cpu stuff to other units
  977. - remove unused constents
  978. * fix stacksize for some targets
  979. * fix generic size problems which depend now on EXTEND_SIZE constant
  980. Revision 1.43 2002/04/15 19:01:53 carl
  981. + target_info.size_of_pointer -> pointer_Size
  982. Revision 1.42 2002/04/04 19:06:03 peter
  983. * removed unused units
  984. * use tlocation.size in cg.a_*loc*() routines
  985. Revision 1.41 2002/01/24 18:25:49 peter
  986. * implicit result variable generation for assembler routines
  987. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  988. Revision 1.40 2002/01/06 21:47:32 peter
  989. * removed getprocvar, use only getprocvardef
  990. }