ptconst.pas 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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,verbose,
  34. cutils,globals,widestr,scanner,
  35. symconst,symbase,symdef,symtable,
  36. aasmbase,aasmtai,aasmcpu,defutil,defcmp,
  37. { pass 1 }
  38. node,
  39. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,
  40. { parser specific stuff }
  41. pbase,pexpr,
  42. { codegen }
  43. cpuinfo,cgbase
  44. ;
  45. {$ifdef fpc}
  46. {$maxfpuregisters 0}
  47. {$endif fpc}
  48. { this procedure reads typed constants }
  49. procedure readtypedconst(const t:ttype;sym : ttypedconstsym;writable : boolean);
  50. type
  51. setbytes = array[0..31] of byte;
  52. Psetbytes = ^setbytes;
  53. var
  54. len,base : longint;
  55. p,hp,hpstart : tnode;
  56. i,j,l,
  57. varalign : longint;
  58. offset,
  59. strlength : aint;
  60. curconstsegment : TAAsmoutput;
  61. ll : tasmlabel;
  62. s,sorg : string;
  63. c : char;
  64. ca : pchar;
  65. tmpguid : tguid;
  66. aktpos : longint;
  67. obj : tobjectdef;
  68. recsym,
  69. srsym : tsym;
  70. symt : tsymtable;
  71. value : bestreal;
  72. intvalue : tconstexprint;
  73. strval : pchar;
  74. pw : pcompilerwidestring;
  75. error : boolean;
  76. old_block_type : tblock_type;
  77. procedure check_range(def:torddef);
  78. begin
  79. if ((tordconstnode(p).value>def.high) or
  80. (tordconstnode(p).value<def.low)) then
  81. begin
  82. if (cs_check_range in aktlocalswitches) then
  83. Message(parser_e_range_check_error)
  84. else
  85. Message(parser_w_range_check_error);
  86. end;
  87. end;
  88. begin
  89. old_block_type:=block_type;
  90. block_type:=bt_const;
  91. if writable then
  92. curconstsegment:=datasegment
  93. else
  94. curconstsegment:=consts;
  95. case t.def.deftype of
  96. orddef:
  97. begin
  98. p:=comp_expr(true);
  99. case torddef(t.def).typ of
  100. bool8bit :
  101. begin
  102. if is_constboolnode(p) then
  103. curconstSegment.concat(Tai_const.Create_8bit(byte(tordconstnode(p).value)))
  104. else
  105. Message(parser_e_illegal_expression);
  106. end;
  107. bool16bit :
  108. begin
  109. if is_constboolnode(p) then
  110. curconstSegment.concat(Tai_const.Create_16bit(word(tordconstnode(p).value)))
  111. else
  112. Message(parser_e_illegal_expression);
  113. end;
  114. bool32bit :
  115. begin
  116. if is_constboolnode(p) then
  117. curconstSegment.concat(Tai_const.Create_32bit(longint(tordconstnode(p).value)))
  118. else
  119. Message(parser_e_illegal_expression);
  120. end;
  121. uchar :
  122. begin
  123. if is_constcharnode(p) then
  124. curconstSegment.concat(Tai_const.Create_8bit(byte(tordconstnode(p).value)))
  125. else
  126. Message(parser_e_illegal_expression);
  127. end;
  128. uwidechar :
  129. begin
  130. if is_constcharnode(p) then
  131. inserttypeconv(p,cwidechartype);
  132. if is_constwidecharnode(p) then
  133. curconstSegment.concat(Tai_const.Create_16bit(word(tordconstnode(p).value)))
  134. else
  135. Message(parser_e_illegal_expression);
  136. end;
  137. s8bit,
  138. u8bit :
  139. begin
  140. if is_constintnode(p) then
  141. begin
  142. curconstSegment.concat(Tai_const.Create_8bit(byte(tordconstnode(p).value)));
  143. check_range(torddef(t.def));
  144. end
  145. else
  146. Message(parser_e_illegal_expression);
  147. end;
  148. u16bit,
  149. s16bit :
  150. begin
  151. if is_constintnode(p) then
  152. begin
  153. curconstSegment.concat(Tai_const.Create_16bit(word(tordconstnode(p).value)));
  154. check_range(torddef(t.def));
  155. end
  156. else
  157. Message(parser_e_illegal_expression);
  158. end;
  159. s32bit,
  160. u32bit :
  161. begin
  162. if is_constintnode(p) then
  163. begin
  164. curconstSegment.concat(Tai_const.Create_32bit(longint(tordconstnode(p).value)));
  165. if torddef(t.def).typ<>u32bit then
  166. check_range(torddef(t.def));
  167. end
  168. else
  169. Message(parser_e_illegal_expression);
  170. end;
  171. s64bit,
  172. u64bit,
  173. scurrency:
  174. begin
  175. if is_constintnode(p) then
  176. intvalue := tordconstnode(p).value
  177. else if is_constrealnode(p) and
  178. (torddef(t.def).typ=scurrency)
  179. { allow bootstrapping }
  180. {$ifdef VER1_0}
  181. { I get IE 10 here. I guess it's no problem if a 1.0 bootstrapped
  182. compiler doesn't display the error here.
  183. and (trealconstnode(p).value_real*10000 >= real(low(int64))) and
  184. (trealconstnode(p).value_real*10000 <= real(high(int64)))}
  185. {$endif VER1_0}
  186. then
  187. begin
  188. {$ifdef VER1_0}
  189. { only trunc is 64 bit in 1.0.x so use trunc here to allow bootstrapping }
  190. intvalue:=trunc(trealconstnode(p).value_real*10000);
  191. {$else VER1_0}
  192. intvalue:=round(trealconstnode(p).value_real*10000);
  193. {$endif VER1_0}
  194. end
  195. else
  196. begin
  197. intvalue:=0;
  198. Message(parser_e_illegal_expression);
  199. end;
  200. curconstSegment.concat(Tai_const.Create_64bit(intvalue));
  201. end;
  202. else
  203. internalerror(3799);
  204. end;
  205. p.free;
  206. end;
  207. floatdef:
  208. begin
  209. p:=comp_expr(true);
  210. if is_constrealnode(p) then
  211. value:=trealconstnode(p).value_real
  212. else if is_constintnode(p) then
  213. value:=tordconstnode(p).value
  214. else
  215. Message(parser_e_illegal_expression);
  216. case tfloatdef(t.def).typ of
  217. s32real :
  218. curconstSegment.concat(Tai_real_32bit.Create(ts32real(value)));
  219. s64real :
  220. {$ifdef ARM}
  221. if aktfputype in [fpu_fpa,fpu_fpa10,fpu_fpa11] then
  222. curconstSegment.concat(Tai_real_64bit.Create_hiloswapped(ts64real(value)))
  223. else
  224. {$endif ARM}
  225. curconstSegment.concat(Tai_real_64bit.Create(ts64real(value)));
  226. s80real :
  227. curconstSegment.concat(Tai_real_80bit.Create(value));
  228. {$ifdef ver1_0}
  229. s64comp :
  230. curconstSegment.concat(Tai_comp_64bit.Create(value));
  231. s64currency:
  232. curconstSegment.concat(Tai_comp_64bit.Create(value*10000));
  233. {$else ver1_0}
  234. { the round is necessary for native compilers where comp isn't a float }
  235. s64comp :
  236. curconstSegment.concat(Tai_comp_64bit.Create(round(value)));
  237. s64currency:
  238. curconstSegment.concat(Tai_comp_64bit.Create(round(value*10000)));
  239. {$endif ver1_0}
  240. s128real:
  241. curconstSegment.concat(Tai_real_128bit.Create(value));
  242. else
  243. internalerror(18);
  244. end;
  245. p.free;
  246. end;
  247. classrefdef:
  248. begin
  249. p:=comp_expr(true);
  250. case p.nodetype of
  251. loadvmtaddrn:
  252. with Tclassrefdef(p.resulttype.def) do
  253. begin
  254. if not Tobjectdef(pointertype.def).is_related(Tobjectdef(pointertype.def)) then
  255. message(parser_e_illegal_expression);
  256. curconstSegment.concat(Tai_const.Create_sym(objectlibrary.newasmsymbol(
  257. Tobjectdef(pointertype.def).vmt_mangledname,AB_EXTERNAL,AT_DATA)));
  258. end;
  259. niln:
  260. curconstSegment.concat(Tai_const.Create_sym(nil));
  261. else Message(parser_e_illegal_expression);
  262. end;
  263. p.free;
  264. end;
  265. pointerdef:
  266. begin
  267. p:=comp_expr(true);
  268. if (p.nodetype=typeconvn) then
  269. with Ttypeconvnode(p) do
  270. if (left.nodetype in [addrn,niln]) and equal_defs(t.def,p.resulttype.def) then
  271. begin
  272. hp:=left;
  273. left:=nil;
  274. p.free;
  275. p:=hp;
  276. end;
  277. { allows horrible ofs(typeof(TButton)^) code !! }
  278. if (p.nodetype=addrn) then
  279. with Taddrnode(p) do
  280. if left.nodetype=derefn then
  281. begin
  282. hp:=tderefnode(left).left;
  283. tderefnode(left).left:=nil;
  284. p.free;
  285. p:=hp;
  286. end;
  287. { const pointer ? }
  288. if (p.nodetype = pointerconstn) then
  289. begin
  290. if sizeof(TConstPtrUInt)=8 then
  291. curconstsegment.concat(Tai_const.Create_64bit(TConstPtrUInt(tpointerconstnode(p).value)))
  292. else
  293. if sizeof(TConstPtrUInt)=4 then
  294. curconstsegment.concat(Tai_const.Create_32bit(TConstPtrUInt(tpointerconstnode(p).value)))
  295. else
  296. internalerror(200404122);
  297. end
  298. { nil pointer ? }
  299. else if p.nodetype=niln then
  300. curconstSegment.concat(Tai_const.Create_sym(nil))
  301. { maybe pchar ? }
  302. else
  303. if is_char(tpointerdef(t.def).pointertype.def) and
  304. (p.nodetype<>addrn) then
  305. begin
  306. objectlibrary.getdatalabel(ll);
  307. curconstSegment.concat(Tai_const.Create_sym(ll));
  308. if p.nodetype=stringconstn then
  309. varalign:=tstringconstnode(p).len
  310. else
  311. varalign:=0;
  312. varalign:=const_align(varalign);
  313. Consts.concat(Tai_align.Create(varalign));
  314. Consts.concat(Tai_label.Create(ll));
  315. if p.nodetype=stringconstn then
  316. begin
  317. len:=tstringconstnode(p).len;
  318. { For tp7 the maximum lentgh can be 255 }
  319. if (m_tp7 in aktmodeswitches) and
  320. (len>255) then
  321. len:=255;
  322. getmem(ca,len+2);
  323. move(tstringconstnode(p).value_str^,ca^,len+1);
  324. Consts.concat(Tai_string.Create_length_pchar(ca,len+1));
  325. end
  326. else
  327. if is_constcharnode(p) then
  328. Consts.concat(Tai_string.Create(char(byte(tordconstnode(p).value))+#0))
  329. else
  330. Message(parser_e_illegal_expression);
  331. end
  332. { maybe pwidechar ? }
  333. else
  334. if is_widechar(tpointerdef(t.def).pointertype.def) and
  335. (p.nodetype<>addrn) then
  336. begin
  337. objectlibrary.getdatalabel(ll);
  338. curconstSegment.concat(Tai_const.Create_sym(ll));
  339. Consts.concat(tai_align.create(const_align(sizeof(aint))));
  340. Consts.concat(Tai_label.Create(ll));
  341. if (p.nodetype in [stringconstn,ordconstn]) then
  342. begin
  343. { convert to widestring stringconstn }
  344. inserttypeconv(p,cwidestringtype);
  345. if (p.nodetype=stringconstn) and
  346. (tstringconstnode(p).st_type=st_widestring) then
  347. begin
  348. pw:=pcompilerwidestring(tstringconstnode(p).value_str);
  349. for i:=0 to tstringconstnode(p).len-1 do
  350. Consts.concat(Tai_const.Create_16bit(pw^.data[i]));
  351. { ending #0 }
  352. Consts.concat(Tai_const.Create_16bit(0))
  353. end;
  354. end
  355. else
  356. Message(parser_e_illegal_expression);
  357. end
  358. else
  359. if p.nodetype=addrn then
  360. begin
  361. { support @@procvar in tp mode }
  362. if (m_tp_procvar in aktmodeswitches) and
  363. (taddrnode(p).left.nodetype=addrn) then
  364. p:=taddrnode(p).left;
  365. { insert typeconv }
  366. inserttypeconv(p,t);
  367. { if a typeconv node was inserted then check if it was an tc_equal. If
  368. true then we remove the node. If not tc_equal then we leave the typeconvn
  369. and the nodetype=loadn will always be false and generate the error (PFV) }
  370. if (p.nodetype=typeconvn) then
  371. begin
  372. if (ttypeconvnode(p).convtype=tc_equal) then
  373. hpstart:=taddrnode(ttypeconvnode(p).left).left
  374. else
  375. hpstart:=p;
  376. end
  377. else
  378. hpstart:=taddrnode(p).left;
  379. hp:=hpstart;
  380. while assigned(hp) and (hp.nodetype in [subscriptn,vecn]) do
  381. hp:=tunarynode(hp).left;
  382. if (hp.nodetype=loadn) then
  383. begin
  384. hp:=hpstart;
  385. offset:=0;
  386. while assigned(hp) and (hp.nodetype<>loadn) do
  387. begin
  388. case hp.nodetype of
  389. vecn :
  390. begin
  391. case tvecnode(hp).left.resulttype.def.deftype of
  392. stringdef :
  393. begin
  394. { this seems OK for shortstring and ansistrings PM }
  395. { it is wrong for widestrings !! }
  396. len:=1;
  397. base:=0;
  398. end;
  399. arraydef :
  400. begin
  401. len:=tarraydef(tvecnode(hp).left.resulttype.def).elesize;
  402. base:=tarraydef(tvecnode(hp).left.resulttype.def).lowrange;
  403. end
  404. else
  405. Message(parser_e_illegal_expression);
  406. end;
  407. if is_constintnode(tvecnode(hp).right) then
  408. inc(offset,len*(get_ordinal_value(tvecnode(hp).right)-base))
  409. else
  410. Message(parser_e_illegal_expression);
  411. end;
  412. subscriptn :
  413. inc(offset,tsubscriptnode(hp).vs.fieldoffset)
  414. else
  415. Message(parser_e_illegal_expression);
  416. end;
  417. hp:=tbinarynode(hp).left;
  418. end;
  419. srsym:=tloadnode(hp).symtableentry;
  420. case srsym.typ of
  421. procsym :
  422. begin
  423. if Tprocsym(srsym).procdef_count>1 then
  424. Message(parser_e_no_overloaded_procvars);
  425. if po_abstractmethod in tprocsym(srsym).first_procdef.procoptions then
  426. Message(type_e_cant_take_address_of_abstract_method)
  427. else
  428. curconstSegment.concat(Tai_const.Createname(tprocsym(srsym).first_procdef.mangledname,AT_FUNCTION,offset));
  429. end;
  430. varsym :
  431. curconstSegment.concat(Tai_const.Createname(tvarsym(srsym).mangledname,AT_DATA,offset));
  432. typedconstsym :
  433. curconstSegment.concat(Tai_const.Createname(ttypedconstsym(srsym).mangledname,AT_DATA,offset));
  434. else
  435. Message(type_e_variable_id_expected);
  436. end;
  437. end
  438. else
  439. Message(parser_e_illegal_expression);
  440. end
  441. else
  442. { allow typeof(Object type)}
  443. if (p.nodetype=inlinen) and
  444. (tinlinenode(p).inlinenumber=in_typeof_x) then
  445. begin
  446. if (tinlinenode(p).left.nodetype=typen) then
  447. begin
  448. curconstSegment.concat(Tai_const.createname(
  449. tobjectdef(tinlinenode(p).left.resulttype.def).vmt_mangledname,AT_DATA,0));
  450. end
  451. else
  452. Message(parser_e_illegal_expression);
  453. end
  454. else
  455. Message(parser_e_illegal_expression);
  456. p.free;
  457. end;
  458. setdef:
  459. begin
  460. p:=comp_expr(true);
  461. if p.nodetype=setconstn then
  462. begin
  463. { be sure to convert to the correct result, else
  464. it can generate smallset data instead of normalset (PFV) }
  465. inserttypeconv(p,t);
  466. { we only allow const sets }
  467. if assigned(tsetconstnode(p).left) then
  468. Message(parser_e_illegal_expression)
  469. else
  470. begin
  471. { this writing is endian independant }
  472. { untrue - because they are considered }
  473. { arrays of 32-bit values CEC }
  474. if source_info.endian = target_info.endian then
  475. begin
  476. for l:=0 to p.resulttype.def.size-1 do
  477. curconstsegment.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[l]));
  478. end
  479. else
  480. begin
  481. { store as longint values in swaped format }
  482. j:=0;
  483. for l:=0 to ((p.resulttype.def.size-1) div 4) do
  484. begin
  485. curconstsegment.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+3]));
  486. curconstsegment.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+2]));
  487. curconstsegment.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+1]));
  488. curconstsegment.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j]));
  489. Inc(j,4);
  490. end;
  491. end;
  492. end;
  493. end
  494. else
  495. Message(parser_e_illegal_expression);
  496. p.free;
  497. end;
  498. enumdef:
  499. begin
  500. p:=comp_expr(true);
  501. if p.nodetype=ordconstn then
  502. begin
  503. if equal_defs(p.resulttype.def,t.def) or
  504. is_subequal(p.resulttype.def,t.def) then
  505. begin
  506. case p.resulttype.def.size of
  507. 1 : curconstSegment.concat(Tai_const.Create_8bit(Byte(tordconstnode(p).value)));
  508. 2 : curconstSegment.concat(Tai_const.Create_16bit(Word(tordconstnode(p).value)));
  509. 4 : curconstSegment.concat(Tai_const.Create_32bit(Longint(tordconstnode(p).value)));
  510. end;
  511. end
  512. else
  513. IncompatibleTypes(p.resulttype.def,t.def);
  514. end
  515. else
  516. Message(parser_e_illegal_expression);
  517. p.free;
  518. end;
  519. stringdef:
  520. begin
  521. p:=comp_expr(true);
  522. { load strval and strlength of the constant tree }
  523. if (p.nodetype=stringconstn) or is_widestring(t.def) then
  524. begin
  525. { convert to the expected string type so that
  526. for widestrings strval is a pcompilerwidestring }
  527. inserttypeconv(p,t);
  528. strlength:=tstringconstnode(p).len;
  529. strval:=tstringconstnode(p).value_str;
  530. end
  531. else if is_constcharnode(p) then
  532. begin
  533. { strval:=pchar(@tordconstnode(p).value);
  534. THIS FAIL on BIG_ENDIAN MACHINES PM }
  535. c:=chr(tordconstnode(p).value and $ff);
  536. strval:=@c;
  537. strlength:=1
  538. end
  539. else if is_constresourcestringnode(p) then
  540. begin
  541. strval:=pchar(tconstsym(tloadnode(p).symtableentry).value.valueptr);
  542. strlength:=tconstsym(tloadnode(p).symtableentry).value.len;
  543. end
  544. else
  545. begin
  546. Message(parser_e_illegal_expression);
  547. strlength:=-1;
  548. end;
  549. if strlength>=0 then
  550. begin
  551. case tstringdef(t.def).string_typ of
  552. st_shortstring:
  553. begin
  554. if strlength>=t.def.size then
  555. begin
  556. message2(parser_w_string_too_long,strpas(strval),tostr(t.def.size-1));
  557. strlength:=t.def.size-1;
  558. end;
  559. curconstSegment.concat(Tai_const.Create_8bit(strlength));
  560. { this can also handle longer strings }
  561. getmem(ca,strlength+1);
  562. move(strval^,ca^,strlength);
  563. ca[strlength]:=#0;
  564. curconstSegment.concat(Tai_string.Create_length_pchar(ca,strlength));
  565. { fillup with spaces if size is shorter }
  566. if t.def.size>strlength then
  567. begin
  568. getmem(ca,t.def.size-strlength);
  569. { def.size contains also the leading length, so we }
  570. { we have to subtract one }
  571. fillchar(ca[0],t.def.size-strlength-1,' ');
  572. ca[t.def.size-strlength-1]:=#0;
  573. { this can also handle longer strings }
  574. curconstSegment.concat(Tai_string.Create_length_pchar(ca,t.def.size-strlength-1));
  575. end;
  576. end;
  577. {$ifdef ansistrings_bits}
  578. st_ansistring16:
  579. begin
  580. { an empty ansi string is nil! }
  581. if (strlength=0) then
  582. curconstSegment.concat(Tai_const.Create_ptr(0))
  583. else
  584. begin
  585. objectlibrary.getdatalabel(ll);
  586. curconstSegment.concat(Tai_const_symbol.Create(ll));
  587. { the actual structure starts at -12 from start label - CEC }
  588. Consts.concat(tai_align.create(const_align(pointer_size)));
  589. { first write the maximum size }
  590. Consts.concat(Tai_const.Create_16bit(strlength));
  591. { second write the real length }
  592. Consts.concat(Tai_const.Create_16bit(strlength));
  593. { redondent with maxlength but who knows ... (PM) }
  594. { third write use count (set to -1 for safety ) }
  595. Consts.concat(Tai_const.Create_16bit(-1));
  596. Consts.concat(Tai_label.Create(ll));
  597. getmem(ca,strlength+2);
  598. move(strval^,ca^,strlength);
  599. { The terminating #0 to be stored in the .data section (JM) }
  600. ca[strlength]:=#0;
  601. { End of the PChar. The memory has to be allocated because in }
  602. { tai_string.done, there is a freemem(len+1) (JM) }
  603. ca[strlength+1]:=#0;
  604. Consts.concat(Tai_string.Create_length_pchar(ca,strlength+1));
  605. end;
  606. end;
  607. {$endif}
  608. {$ifdef ansistring_bits}st_ansistring32{$else}st_ansistring{$endif}:
  609. begin
  610. { an empty ansi string is nil! }
  611. if (strlength=0) then
  612. curconstSegment.concat(Tai_const.Create_sym(nil))
  613. else
  614. begin
  615. objectlibrary.getdatalabel(ll);
  616. curconstSegment.concat(Tai_const.Create_sym(ll));
  617. { the actual structure starts at -12 from start label - CEC }
  618. Consts.concat(tai_align.create(const_align(sizeof(aint))));
  619. { first write the maximum size }
  620. Consts.concat(Tai_const.Create_aint(strlength));
  621. { second write the real length }
  622. Consts.concat(Tai_const.Create_aint(strlength));
  623. { redondent with maxlength but who knows ... (PM) }
  624. { third write use count (set to -1 for safety ) }
  625. Consts.concat(Tai_const.Create_aint(-1));
  626. Consts.concat(Tai_label.Create(ll));
  627. getmem(ca,strlength+2);
  628. move(strval^,ca^,strlength);
  629. { The terminating #0 to be stored in the .data section (JM) }
  630. ca[strlength]:=#0;
  631. { End of the PChar. The memory has to be allocated because in }
  632. { tai_string.done, there is a freemem(len+1) (JM) }
  633. ca[strlength+1]:=#0;
  634. Consts.concat(Tai_string.Create_length_pchar(ca,strlength+1));
  635. end;
  636. end;
  637. {$ifdef ansistring_bits}
  638. st_ansistring64:
  639. begin
  640. { an empty ansi string is nil! }
  641. if (strlength=0) then
  642. curconstSegment.concat(Tai_const.Create_ptr(0))
  643. else
  644. begin
  645. objectlibrary.getdatalabel(ll);
  646. curconstSegment.concat(Tai_const_symbol.Create(ll));
  647. { the actual structure starts at -12 from start label - CEC }
  648. Consts.concat(tai_align.create(const_align(pointer_size)));
  649. { first write the maximum size }
  650. Consts.concat(Tai_const.Create_64bit(strlength));
  651. { second write the real length }
  652. Consts.concat(Tai_const.Create_64bit(strlength));
  653. { redondent with maxlength but who knows ... (PM) }
  654. { third write use count (set to -1 for safety ) }
  655. Consts.concat(Tai_const.Create_64bit(-1));
  656. Consts.concat(Tai_label.Create(ll));
  657. getmem(ca,strlength+2);
  658. move(strval^,ca^,strlength);
  659. { The terminating #0 to be stored in the .data section (JM) }
  660. ca[strlength]:=#0;
  661. { End of the PChar. The memory has to be allocated because in }
  662. { tai_string.done, there is a freemem(len+1) (JM) }
  663. ca[strlength+1]:=#0;
  664. Consts.concat(Tai_string.Create_length_pchar(ca,strlength+1));
  665. end;
  666. end;
  667. {$endif}
  668. st_widestring:
  669. begin
  670. { an empty ansi string is nil! }
  671. if (strlength=0) then
  672. curconstSegment.concat(Tai_const.Create_sym(nil))
  673. else
  674. begin
  675. objectlibrary.getdatalabel(ll);
  676. curconstSegment.concat(Tai_const.Create_sym(ll));
  677. { the actual structure starts at -12 from start label - CEC }
  678. Consts.concat(tai_align.create(const_align(sizeof(aint))));
  679. Consts.concat(Tai_const.Create_aint(strlength));
  680. Consts.concat(Tai_const.Create_aint(strlength));
  681. Consts.concat(Tai_const.Create_aint(-1));
  682. Consts.concat(Tai_label.Create(ll));
  683. for i:=0 to strlength-1 do
  684. Consts.concat(Tai_const.Create_16bit(pcompilerwidestring(strval)^.data[i]));
  685. { ending #0 }
  686. Consts.concat(Tai_const.Create_16bit(0))
  687. end;
  688. end;
  689. st_longstring:
  690. begin
  691. internalerror(200107081);
  692. {curconstSegment.concat(Tai_const.Create_32bit(strlength))));
  693. curconstSegment.concat(Tai_const.Create_8bit(0));
  694. getmem(ca,strlength+1);
  695. move(strval^,ca^,strlength);
  696. ca[strlength]:=#0;
  697. generate_pascii(consts,ca,strlength);
  698. curconstSegment.concat(Tai_const.Create_8bit(0));}
  699. end;
  700. end;
  701. end;
  702. p.free;
  703. end;
  704. arraydef:
  705. begin
  706. if try_to_consume(_LKLAMMER) then
  707. begin
  708. for l:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange-1 do
  709. begin
  710. readtypedconst(tarraydef(t.def).elementtype,nil,writable);
  711. consume(_COMMA);
  712. end;
  713. readtypedconst(tarraydef(t.def).elementtype,nil,writable);
  714. consume(_RKLAMMER);
  715. end
  716. else
  717. { if array of char then we allow also a string }
  718. if is_char(tarraydef(t.def).elementtype.def) then
  719. begin
  720. p:=comp_expr(true);
  721. if p.nodetype=stringconstn then
  722. begin
  723. len:=tstringconstnode(p).len;
  724. { For tp7 the maximum lentgh can be 255 }
  725. if (m_tp7 in aktmodeswitches) and
  726. (len>255) then
  727. len:=255;
  728. ca:=tstringconstnode(p).value_str;
  729. end
  730. else
  731. if is_constcharnode(p) then
  732. begin
  733. c:=chr(tordconstnode(p).value and $ff);
  734. ca:=@c;
  735. len:=1;
  736. end
  737. else
  738. begin
  739. Message(parser_e_illegal_expression);
  740. len:=0;
  741. end;
  742. if len>(tarraydef(t.def).highrange-tarraydef(t.def).lowrange+1) then
  743. Message(parser_e_string_larger_array);
  744. for i:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange do
  745. begin
  746. if i+1-tarraydef(t.def).lowrange<=len then
  747. begin
  748. curconstSegment.concat(Tai_const.Create_8bit(byte(ca^)));
  749. inc(ca);
  750. end
  751. else
  752. {Fill the remaining positions with #0.}
  753. curconstSegment.concat(Tai_const.Create_8bit(0));
  754. end;
  755. p.free;
  756. end
  757. else
  758. { dynamic array nil }
  759. if is_dynamic_array(t.def) then
  760. begin
  761. { Only allow nil initialization }
  762. consume(_NIL);
  763. curconstSegment.concat(Tai_const.Create_sym(nil));
  764. end
  765. else
  766. begin
  767. { we want the ( }
  768. consume(_LKLAMMER);
  769. end;
  770. end;
  771. procvardef:
  772. begin
  773. { Procvars and pointers are no longer compatible. }
  774. { under tp: =nil or =var under fpc: =nil or =@var }
  775. if token=_NIL then
  776. begin
  777. curconstSegment.concat(Tai_const.Create_sym(nil));
  778. if (po_methodpointer in tprocvardef(t.def).procoptions) then
  779. curconstSegment.concat(Tai_const.Create_sym(nil));
  780. consume(_NIL);
  781. exit;
  782. end;
  783. { you can't assign a value other than NIL to a typed constant }
  784. { which is a "procedure of object", because this also requires }
  785. { address of an object/class instance, which is not known at }
  786. { compile time (JM) }
  787. if (po_methodpointer in tprocvardef(t.def).procoptions) then
  788. Message(parser_e_no_procvarobj_const);
  789. { parse the rest too, so we can continue with error checking }
  790. getprocvardef:=tprocvardef(t.def);
  791. p:=comp_expr(true);
  792. getprocvardef:=nil;
  793. if codegenerror then
  794. begin
  795. p.free;
  796. exit;
  797. end;
  798. { let type conversion check everything needed }
  799. inserttypeconv(p,t);
  800. if codegenerror then
  801. begin
  802. p.free;
  803. exit;
  804. end;
  805. { remove typeconvn, that will normally insert a lea
  806. instruction which is not necessary for us }
  807. if p.nodetype=typeconvn then
  808. begin
  809. hp:=ttypeconvnode(p).left;
  810. ttypeconvnode(p).left:=nil;
  811. p.free;
  812. p:=hp;
  813. end;
  814. { remove addrn which we also don't need here }
  815. if p.nodetype=addrn then
  816. begin
  817. hp:=taddrnode(p).left;
  818. taddrnode(p).left:=nil;
  819. p.free;
  820. p:=hp;
  821. end;
  822. { we now need to have a loadn with a procsym }
  823. if (p.nodetype=loadn) and
  824. (tloadnode(p).symtableentry.typ=procsym) then
  825. begin
  826. curconstSegment.concat(Tai_const.createname(
  827. tprocsym(tloadnode(p).symtableentry).first_procdef.mangledname,AT_FUNCTION,0));
  828. end
  829. else
  830. Message(parser_e_illegal_expression);
  831. p.free;
  832. end;
  833. { reads a typed constant record }
  834. recorddef:
  835. begin
  836. { KAZ }
  837. if (trecorddef(t.def)=rec_tguid) and
  838. ((token=_CSTRING) or (token=_CCHAR) or (token=_ID)) then
  839. begin
  840. p:=comp_expr(true);
  841. inserttypeconv(p,cshortstringtype);
  842. if p.nodetype=stringconstn then
  843. begin
  844. s:=strpas(tstringconstnode(p).value_str);
  845. p.free;
  846. if string2guid(s,tmpguid) then
  847. begin
  848. curconstSegment.concat(Tai_const.Create_32bit(tmpguid.D1));
  849. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D2));
  850. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D3));
  851. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  852. curconstSegment.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  853. end
  854. else
  855. Message(parser_e_improper_guid_syntax);
  856. end
  857. else
  858. begin
  859. p.free;
  860. Message(parser_e_illegal_expression);
  861. exit;
  862. end;
  863. end
  864. else
  865. begin
  866. consume(_LKLAMMER);
  867. sorg:='';
  868. aktpos:=0;
  869. srsym := tsym(trecorddef(t.def).symtable.symindex.first);
  870. recsym := nil;
  871. while token<>_RKLAMMER do
  872. begin
  873. s:=pattern;
  874. sorg:=orgpattern;
  875. consume(_ID);
  876. consume(_COLON);
  877. error := false;
  878. recsym := tsym(trecorddef(t.def).symtable.search(s));
  879. if not assigned(recsym) then
  880. begin
  881. Message1(sym_e_illegal_field,sorg);
  882. error := true;
  883. end;
  884. if (not error) and
  885. (not assigned(srsym) or
  886. (s <> srsym.name)) then
  887. { possible variant record (JM) }
  888. begin
  889. { All parts of a variant start at the same offset }
  890. { Also allow jumping from one variant part to another, }
  891. { as long as the offsets match }
  892. if (assigned(srsym) and
  893. (tvarsym(recsym).fieldoffset = tvarsym(srsym).fieldoffset)) or
  894. { srsym is not assigned after parsing w2 in the }
  895. { typed const in the next example: }
  896. { type tr = record case byte of }
  897. { 1: (l1,l2: dword); }
  898. { 2: (w1,w2: word); }
  899. { end; }
  900. { const r: tr = (w1:1;w2:1;l2:5); }
  901. (tvarsym(recsym).fieldoffset = aktpos) then
  902. srsym := recsym
  903. { going backwards isn't allowed in any mode }
  904. else if (tvarsym(recsym).fieldoffset<aktpos) then
  905. begin
  906. Message(parser_e_invalid_record_const);
  907. error := true;
  908. end
  909. { Delphi allows you to skip fields }
  910. else if (m_delphi in aktmodeswitches) then
  911. begin
  912. Message1(parser_w_skipped_fields_before,sorg);
  913. srsym := recsym;
  914. end
  915. { FPC and TP don't }
  916. else
  917. begin
  918. Message1(parser_e_skipped_fields_before,sorg);
  919. error := true;
  920. end;
  921. end;
  922. if error then
  923. consume_all_until(_SEMICOLON)
  924. else
  925. begin
  926. { if needed fill (alignment) }
  927. if tvarsym(srsym).fieldoffset>aktpos then
  928. for i:=1 to tvarsym(srsym).fieldoffset-aktpos do
  929. curconstSegment.concat(Tai_const.Create_8bit(0));
  930. { new position }
  931. aktpos:=tvarsym(srsym).fieldoffset+tvarsym(srsym).vartype.def.size;
  932. { read the data }
  933. readtypedconst(tvarsym(srsym).vartype,nil,writable);
  934. { keep previous field for checking whether whole }
  935. { record was initialized (JM) }
  936. recsym := srsym;
  937. { goto next field }
  938. srsym := tsym(srsym.indexnext);
  939. if token=_SEMICOLON then
  940. consume(_SEMICOLON)
  941. else break;
  942. end;
  943. end;
  944. { are there any fields left? }
  945. if assigned(srsym) and
  946. { don't complain if there only come other variant parts }
  947. { after the last initialized field }
  948. ((recsym=nil) or
  949. (tvarsym(srsym).fieldoffset > tvarsym(recsym).fieldoffset)) then
  950. Message1(parser_w_skipped_fields_after,sorg);
  951. for i:=1 to t.def.size-aktpos do
  952. curconstSegment.concat(Tai_const.Create_8bit(0));
  953. consume(_RKLAMMER);
  954. end;
  955. end;
  956. { reads a typed object }
  957. objectdef:
  958. begin
  959. if is_class_or_interface(t.def) then
  960. begin
  961. p:=comp_expr(true);
  962. if p.nodetype<>niln then
  963. begin
  964. Message(parser_e_type_const_not_possible);
  965. consume_all_until(_RKLAMMER);
  966. end
  967. else
  968. begin
  969. curconstSegment.concat(Tai_const.Create_sym(nil));
  970. end;
  971. p.free;
  972. end
  973. { for objects we allow it only if it doesn't contain a vmt }
  974. else if (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  975. (m_fpc in aktmodeswitches) then
  976. Message(parser_e_type_const_not_possible)
  977. else
  978. begin
  979. consume(_LKLAMMER);
  980. aktpos:=0;
  981. while token<>_RKLAMMER do
  982. begin
  983. s:=pattern;
  984. sorg:=orgpattern;
  985. consume(_ID);
  986. consume(_COLON);
  987. srsym:=nil;
  988. obj:=tobjectdef(t.def);
  989. symt:=obj.symtable;
  990. while (srsym=nil) and assigned(symt) do
  991. begin
  992. srsym:=tsym(symt.search(s));
  993. if assigned(obj) then
  994. obj:=obj.childof;
  995. if assigned(obj) then
  996. symt:=obj.symtable
  997. else
  998. symt:=nil;
  999. end;
  1000. if srsym=nil then
  1001. begin
  1002. Message1(sym_e_id_not_found,sorg);
  1003. consume_all_until(_SEMICOLON);
  1004. end
  1005. else
  1006. with Tvarsym(srsym) do
  1007. begin
  1008. { check position }
  1009. if fieldoffset<aktpos then
  1010. message(parser_e_invalid_record_const);
  1011. { check in VMT needs to be added for TP mode }
  1012. with Tobjectdef(t.def) do
  1013. if not(m_fpc in aktmodeswitches) and
  1014. (oo_has_vmt in objectoptions) and
  1015. (vmt_offset<fieldoffset) then
  1016. begin
  1017. for i:=1 to vmt_offset-aktpos do
  1018. curconstsegment.concat(tai_const.create_8bit(0));
  1019. curconstsegment.concat(tai_const.createname(vmt_mangledname,AT_DATA,0));
  1020. { this is more general }
  1021. aktpos:=vmt_offset + sizeof(aint);
  1022. end;
  1023. { if needed fill }
  1024. if fieldoffset>aktpos then
  1025. for i:=1 to fieldoffset-aktpos do
  1026. curconstSegment.concat(Tai_const.Create_8bit(0));
  1027. { new position }
  1028. aktpos:=fieldoffset+vartype.def.size;
  1029. { read the data }
  1030. readtypedconst(vartype,nil,writable);
  1031. if token=_SEMICOLON then
  1032. consume(_SEMICOLON)
  1033. else break;
  1034. end;
  1035. end;
  1036. if not(m_fpc in aktmodeswitches) and
  1037. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  1038. (tobjectdef(t.def).vmt_offset>=aktpos) then
  1039. begin
  1040. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  1041. curconstsegment.concat(tai_const.create_8bit(0));
  1042. curconstsegment.concat(tai_const.createname(tobjectdef(t.def).vmt_mangledname,AT_DATA,0));
  1043. { this is more general }
  1044. aktpos:=tobjectdef(t.def).vmt_offset + sizeof(aint);
  1045. end;
  1046. for i:=1 to t.def.size-aktpos do
  1047. curconstSegment.concat(Tai_const.Create_8bit(0));
  1048. consume(_RKLAMMER);
  1049. end;
  1050. end;
  1051. errordef:
  1052. begin
  1053. { try to consume something useful }
  1054. if token=_LKLAMMER then
  1055. consume_all_until(_RKLAMMER)
  1056. else
  1057. consume_all_until(_SEMICOLON);
  1058. end;
  1059. else Message(parser_e_type_const_not_possible);
  1060. end;
  1061. block_type:=old_block_type;
  1062. end;
  1063. {$ifdef fpc}
  1064. {$maxfpuregisters default}
  1065. {$endif fpc}
  1066. end.
  1067. {
  1068. $Log$
  1069. Revision 1.90 2004-07-03 14:06:35 daniel
  1070. * Compile fix
  1071. Revision 1.89 2004/06/20 20:41:47 florian
  1072. * fixed bootstrapping problems
  1073. Revision 1.88 2004/06/20 08:55:30 florian
  1074. * logs truncated
  1075. Revision 1.87 2004/06/18 15:16:46 peter
  1076. * remove obsolete cardinal() typecasts
  1077. Revision 1.86 2004/06/16 20:07:09 florian
  1078. * dwarf branch merged
  1079. Revision 1.85 2004/05/23 15:23:30 peter
  1080. * fixed qword(longint) that removed sign from the number
  1081. * removed code in the compiler that relied on wrong qword(longint)
  1082. code generation
  1083. Revision 1.84 2004/04/29 19:56:37 daniel
  1084. * Prepare compiler infrastructure for multiple ansistring types
  1085. Revision 1.83 2004/04/11 10:44:23 peter
  1086. * block_type is bt_const when parsing typed consts
  1087. }