ptconst.pas 45 KB

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