ptconst.pas 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Reads typed constants
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ptconst;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses symtype,symsym,aasmdata;
  21. procedure read_typed_const(list:tasmlist;sym:tstaticvarsym);
  22. implementation
  23. uses
  24. SysUtils,
  25. globtype,systems,tokens,verbose,constexp,
  26. cutils,globals,widestr,scanner,
  27. symconst,symbase,symdef,symtable,
  28. aasmbase,aasmtai,aasmcpu,defutil,defcmp,
  29. { pass 1 }
  30. node,htypechk,procinfo,
  31. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,
  32. { parser specific stuff }
  33. pbase,pexpr,
  34. { codegen }
  35. cpuinfo,cgbase,dbgbase
  36. ;
  37. {$maxfpuregisters 0}
  38. {*****************************************************************************
  39. Bitpacked value helpers
  40. *****************************************************************************}
  41. type
  42. tbitpackedval = record
  43. curval, nextval: aword;
  44. curbitoffset: smallint;
  45. loadbitsize,packedbitsize: byte;
  46. end;
  47. procedure initbitpackval(out bp: tbitpackedval; packedbitsize: byte);
  48. begin
  49. bp.curval:=0;
  50. bp.nextval:=0;
  51. bp.curbitoffset:=0;
  52. bp.packedbitsize:=packedbitsize;
  53. bp.loadbitsize:=packedbitsloadsize(bp.packedbitsize)*8;
  54. end;
  55. {$ifopt r+}
  56. {$define rangeon}
  57. {$r-}
  58. {$endif}
  59. {$ifopt q+}
  60. {$define overflowon}
  61. {$q-}
  62. {$endif}
  63. { (values between quotes below refer to fields of bp; fields not }
  64. { mentioned are unused by this routine) }
  65. { bitpacks "value" as bitpacked value of bitsize "packedbitsize" into }
  66. { "curval", which has already been filled up to "curbitoffset", and }
  67. { stores the spillover if any into "nextval". It also updates }
  68. { curbitoffset to reflect how many bits of currval are now used (can be }
  69. { > AIntBits in case of spillover) }
  70. procedure bitpackval(value: aword; var bp: tbitpackedval);
  71. var
  72. shiftcount: longint;
  73. begin
  74. if (target_info.endian=endian_big) then
  75. begin
  76. { bitpacked format: left-aligned (i.e., "big endian bitness") }
  77. bp.curval:=bp.curval or ((value shl (AIntBits-bp.packedbitsize)) shr bp.curbitoffset);
  78. shiftcount:=((AIntBits-bp.packedbitsize)-bp.curbitoffset);
  79. { carry-over to the next element? }
  80. if (shiftcount<0) then
  81. bp.nextval:=(value and ((aword(1) shl (-shiftcount))-1)) shl
  82. (AIntBits+shiftcount)
  83. end
  84. else
  85. begin
  86. { bitpacked format: right aligned (i.e., "little endian bitness") }
  87. bp.curval:=bp.curval or (value shl bp.curbitoffset);
  88. { carry-over to the next element? }
  89. if (bp.curbitoffset+bp.packedbitsize>AIntBits) then
  90. bp.nextval:=value shr (AIntBits-bp.curbitoffset)
  91. end;
  92. inc(bp.curbitoffset,bp.packedbitsize);
  93. end;
  94. {$ifdef rangeon}
  95. {$r+}
  96. {$undef rangeon}
  97. {$endif}
  98. {$ifdef overflowon}
  99. {$q+}
  100. {$undef overflowon}
  101. {$endif}
  102. procedure flush_packed_value(list: tasmlist; var bp: tbitpackedval);
  103. var
  104. bitstowrite: longint;
  105. writeval : byte;
  106. begin
  107. if (bp.curbitoffset < AIntBits) then
  108. begin
  109. { forced flush -> write multiple of loadsize }
  110. bitstowrite:=align(bp.curbitoffset,bp.loadbitsize);
  111. bp.curbitoffset:=0;
  112. end
  113. else
  114. begin
  115. bitstowrite:=AIntBits;
  116. dec(bp.curbitoffset,AIntBits);
  117. end;
  118. while (bitstowrite>=8) do
  119. begin
  120. if (target_info.endian=endian_little) then
  121. begin
  122. { write lowest byte }
  123. writeval:=byte(bp.curval);
  124. bp.curval:=bp.curval shr 8;
  125. end
  126. else
  127. begin
  128. { write highest byte }
  129. writeval:=bp.curval shr (AIntBits-8);
  130. bp.curval:=(bp.curval and (not($ff shl (AIntBits-8)))) shl 8;
  131. end;
  132. list.concat(tai_const.create_8bit(writeval));
  133. dec(bitstowrite,8);
  134. end;
  135. bp.curval:=bp.nextval;
  136. bp.nextval:=0;
  137. end;
  138. {*****************************************************************************
  139. read typed const
  140. *****************************************************************************}
  141. { this procedure reads typed constants }
  142. procedure read_typed_const_data(list:tasmlist;def:tdef);
  143. procedure parse_orddef(list:tasmlist;def:torddef);
  144. var
  145. n : tnode;
  146. intvalue : tconstexprint;
  147. begin
  148. n:=comp_expr(true);
  149. case def.ordtype of
  150. bool8bit :
  151. begin
  152. if is_constboolnode(n) then
  153. list.concat(Tai_const.Create_8bit(byte(tordconstnode(n).value.svalue)))
  154. else
  155. IncompatibleTypes(n.resultdef, def);
  156. end;
  157. bool16bit :
  158. begin
  159. if is_constboolnode(n) then
  160. list.concat(Tai_const.Create_16bit(word(tordconstnode(n).value.svalue)))
  161. else
  162. IncompatibleTypes(n.resultdef, def);
  163. end;
  164. bool32bit :
  165. begin
  166. if is_constboolnode(n) then
  167. list.concat(Tai_const.Create_32bit(longint(tordconstnode(n).value.svalue)))
  168. else
  169. IncompatibleTypes(n.resultdef, def);
  170. end;
  171. bool64bit :
  172. begin
  173. if is_constboolnode(n) then
  174. list.concat(Tai_const.Create_64bit(int64(tordconstnode(n).value.svalue)))
  175. else
  176. IncompatibleTypes(n.resultdef, def);
  177. end;
  178. uchar :
  179. begin
  180. if is_constcharnode(n) or
  181. ((m_delphi in current_settings.modeswitches) and
  182. is_constwidecharnode(n) and
  183. (tordconstnode(n).value <= 255)) then
  184. list.concat(Tai_const.Create_8bit(byte(tordconstnode(n).value.svalue)))
  185. else
  186. IncompatibleTypes(n.resultdef, def);
  187. end;
  188. uwidechar :
  189. begin
  190. if is_constcharnode(n) then
  191. inserttypeconv(n,cwidechartype);
  192. if is_constwidecharnode(n) then
  193. list.concat(Tai_const.Create_16bit(word(tordconstnode(n).value.svalue)))
  194. else
  195. IncompatibleTypes(n.resultdef, def);
  196. end;
  197. s8bit,u8bit,
  198. u16bit,s16bit,
  199. s32bit,u32bit,
  200. s64bit,u64bit :
  201. begin
  202. if is_constintnode(n) then
  203. begin
  204. testrange(def,tordconstnode(n).value,false);
  205. case def.size of
  206. 1 :
  207. list.concat(Tai_const.Create_8bit(byte(tordconstnode(n).value.svalue)));
  208. 2 :
  209. list.concat(Tai_const.Create_16bit(word(tordconstnode(n).value.svalue)));
  210. 4 :
  211. list.concat(Tai_const.Create_32bit(longint(tordconstnode(n).value.svalue)));
  212. 8 :
  213. list.concat(Tai_const.Create_64bit(tordconstnode(n).value.svalue));
  214. end;
  215. end
  216. else
  217. IncompatibleTypes(n.resultdef, def);
  218. end;
  219. scurrency:
  220. begin
  221. if is_constintnode(n) then
  222. intvalue := tordconstnode(n).value
  223. { allow bootstrapping }
  224. else if is_constrealnode(n) then
  225. intvalue:=PInt64(@trealconstnode(n).value_currency)^
  226. else
  227. begin
  228. intvalue:=0;
  229. IncompatibleTypes(n.resultdef, def);
  230. end;
  231. list.concat(Tai_const.Create_64bit(intvalue));
  232. end;
  233. else
  234. internalerror(200611052);
  235. end;
  236. n.free;
  237. end;
  238. procedure parse_floatdef(list:tasmlist;def:tfloatdef);
  239. var
  240. n : tnode;
  241. value : bestreal;
  242. begin
  243. n:=comp_expr(true);
  244. if is_constrealnode(n) then
  245. value:=trealconstnode(n).value_real
  246. else if is_constintnode(n) then
  247. value:=tordconstnode(n).value
  248. else
  249. IncompatibleTypes(n.resultdef, def);
  250. case def.floattype of
  251. s32real :
  252. list.concat(Tai_real_32bit.Create(ts32real(value)));
  253. s64real :
  254. {$ifdef ARM}
  255. if (current_settings.fputype in [fpu_fpa,fpu_fpa10,fpu_fpa11]) and
  256. not(cs_fp_emulation in current_settings.moduleswitches) then
  257. list.concat(Tai_real_64bit.Create_hiloswapped(ts64real(value)))
  258. else
  259. {$endif ARM}
  260. list.concat(Tai_real_64bit.Create(ts64real(value)));
  261. s80real :
  262. list.concat(Tai_real_80bit.Create(value));
  263. s64comp :
  264. { the round is necessary for native compilers where comp isn't a float }
  265. list.concat(Tai_comp_64bit.Create(round(value)));
  266. s64currency:
  267. list.concat(Tai_comp_64bit.Create(round(value*10000)));
  268. s128real:
  269. list.concat(Tai_real_128bit.Create(value));
  270. else
  271. internalerror(200611053);
  272. end;
  273. n.free;
  274. end;
  275. procedure parse_classrefdef(list:tasmlist;def:tclassrefdef);
  276. var
  277. n : tnode;
  278. begin
  279. n:=comp_expr(true);
  280. case n.nodetype of
  281. loadvmtaddrn:
  282. begin
  283. if not Tobjectdef(tclassrefdef(n.resultdef).pointeddef).is_related(tobjectdef(def.pointeddef)) then
  284. IncompatibleTypes(n.resultdef, def);
  285. list.concat(Tai_const.Create_sym(current_asmdata.RefAsmSymbol(Tobjectdef(tclassrefdef(n.resultdef).pointeddef).vmt_mangledname)));
  286. end;
  287. niln:
  288. list.concat(Tai_const.Create_sym(nil));
  289. else
  290. IncompatibleTypes(n.resultdef, def);
  291. end;
  292. n.free;
  293. end;
  294. procedure parse_pointerdef(list:tasmlist;def:tpointerdef);
  295. var
  296. hp,p : tnode;
  297. srsym : tsym;
  298. pd : tprocdef;
  299. ca : pchar;
  300. pw : pcompilerwidestring;
  301. i,len : longint;
  302. base,
  303. offset : aint;
  304. v : Tconstexprint;
  305. ll : tasmlabel;
  306. varalign : shortint;
  307. begin
  308. p:=comp_expr(true);
  309. { remove equal typecasts for pointer/nil addresses }
  310. if (p.nodetype=typeconvn) then
  311. with Ttypeconvnode(p) do
  312. if (left.nodetype in [addrn,niln]) and equal_defs(def,p.resultdef) then
  313. begin
  314. hp:=left;
  315. left:=nil;
  316. p.free;
  317. p:=hp;
  318. end;
  319. { allows horrible ofs(typeof(TButton)^) code !! }
  320. if (p.nodetype=addrn) then
  321. with Taddrnode(p) do
  322. if left.nodetype=derefn then
  323. begin
  324. hp:=tderefnode(left).left;
  325. tderefnode(left).left:=nil;
  326. p.free;
  327. p:=hp;
  328. end;
  329. { const pointer ? }
  330. if (p.nodetype = pointerconstn) then
  331. begin
  332. if sizeof(TConstPtrUInt)=8 then
  333. list.concat(Tai_const.Create_64bit(int64(tpointerconstnode(p).value)))
  334. else
  335. if sizeof(TConstPtrUInt)=4 then
  336. list.concat(Tai_const.Create_32bit(longint(tpointerconstnode(p).value)))
  337. else
  338. internalerror(200404122);
  339. end
  340. { nil pointer ? }
  341. else if p.nodetype=niln then
  342. list.concat(Tai_const.Create_sym(nil))
  343. { maybe pchar ? }
  344. else
  345. if is_char(def.pointeddef) and
  346. (p.nodetype<>addrn) then
  347. begin
  348. current_asmdata.getdatalabel(ll);
  349. list.concat(Tai_const.Create_sym(ll));
  350. if p.nodetype=stringconstn then
  351. varalign:=size_2_align(tstringconstnode(p).len)
  352. else
  353. varalign:=0;
  354. varalign:=const_align(varalign);
  355. current_asmdata.asmlists[al_const].concat(Tai_align.Create(varalign));
  356. current_asmdata.asmlists[al_const].concat(Tai_label.Create(ll));
  357. if p.nodetype=stringconstn then
  358. begin
  359. len:=tstringconstnode(p).len;
  360. { For tp7 the maximum lentgh can be 255 }
  361. if (m_tp7 in current_settings.modeswitches) and
  362. (len>255) then
  363. len:=255;
  364. getmem(ca,len+2);
  365. move(tstringconstnode(p).value_str^,ca^,len+1);
  366. current_asmdata.asmlists[al_const].concat(Tai_string.Create_pchar(ca,len+1));
  367. end
  368. else
  369. if is_constcharnode(p) then
  370. current_asmdata.asmlists[al_const].concat(Tai_string.Create(char(byte(tordconstnode(p).value.svalue))+#0))
  371. else
  372. IncompatibleTypes(p.resultdef, def);
  373. end
  374. { maybe pwidechar ? }
  375. else
  376. if is_widechar(def.pointeddef) and
  377. (p.nodetype<>addrn) then
  378. begin
  379. current_asmdata.getdatalabel(ll);
  380. list.concat(Tai_const.Create_sym(ll));
  381. current_asmdata.asmlists[al_typedconsts].concat(tai_align.create(const_align(sizeof(aint))));
  382. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(ll));
  383. if (p.nodetype in [stringconstn,ordconstn]) then
  384. begin
  385. { convert to widestring stringconstn }
  386. inserttypeconv(p,cwidestringtype);
  387. if (p.nodetype=stringconstn) and
  388. (tstringconstnode(p).cst_type=cst_widestring) then
  389. begin
  390. pw:=pcompilerwidestring(tstringconstnode(p).value_str);
  391. for i:=0 to tstringconstnode(p).len-1 do
  392. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_16bit(pw^.data[i]));
  393. { ending #0 }
  394. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_16bit(0))
  395. end;
  396. end
  397. else
  398. IncompatibleTypes(p.resultdef, def);
  399. end
  400. else
  401. if (p.nodetype=addrn) or
  402. is_procvar_load(p) then
  403. begin
  404. { insert typeconv }
  405. inserttypeconv(p,def);
  406. hp:=p;
  407. while assigned(hp) and (hp.nodetype in [addrn,typeconvn,subscriptn,vecn]) do
  408. hp:=tunarynode(hp).left;
  409. if (hp.nodetype=loadn) then
  410. begin
  411. hp:=p;
  412. offset:=0;
  413. while assigned(hp) and (hp.nodetype<>loadn) do
  414. begin
  415. case hp.nodetype of
  416. vecn :
  417. begin
  418. case tvecnode(hp).left.resultdef.typ of
  419. stringdef :
  420. begin
  421. { this seems OK for shortstring and ansistrings PM }
  422. { it is wrong for widestrings !! }
  423. len:=1;
  424. base:=0;
  425. end;
  426. arraydef :
  427. begin
  428. if not is_packed_array(tvecnode(hp).left.resultdef) then
  429. begin
  430. len:=tarraydef(tvecnode(hp).left.resultdef).elesize;
  431. base:=tarraydef(tvecnode(hp).left.resultdef).lowrange;
  432. end
  433. else
  434. begin
  435. Message(parser_e_packed_dynamic_open_array);
  436. len:=1;
  437. base:=0;
  438. end;
  439. end
  440. else
  441. Message(parser_e_illegal_expression);
  442. end;
  443. if is_constintnode(tvecnode(hp).right) then
  444. begin
  445. {Prevent overflow.}
  446. v:=get_ordinal_value(tvecnode(hp).right)-base;
  447. if (v<int64(low(offset))) or (v>int64(high(offset))) then
  448. message(parser_e_range_check_error);
  449. if high(offset)-offset div len>v then
  450. inc(offset,len*v.svalue)
  451. else
  452. message(parser_e_range_check_error);
  453. end
  454. else
  455. Message(parser_e_illegal_expression);
  456. end;
  457. subscriptn :
  458. inc(offset,tsubscriptnode(hp).vs.fieldoffset);
  459. typeconvn :
  460. begin
  461. if not(ttypeconvnode(hp).convtype in [tc_equal,tc_proc_2_procvar]) then
  462. Message(parser_e_illegal_expression);
  463. end;
  464. addrn :
  465. ;
  466. else
  467. Message(parser_e_illegal_expression);
  468. end;
  469. hp:=tunarynode(hp).left;
  470. end;
  471. srsym:=tloadnode(hp).symtableentry;
  472. case srsym.typ of
  473. procsym :
  474. begin
  475. pd:=tprocdef(tprocsym(srsym).ProcdefList[0]);
  476. if Tprocsym(srsym).ProcdefList.Count>1 then
  477. Message(parser_e_no_overloaded_procvars);
  478. if po_abstractmethod in pd.procoptions then
  479. Message(type_e_cant_take_address_of_abstract_method)
  480. else
  481. list.concat(Tai_const.Createname(pd.mangledname,offset));
  482. end;
  483. staticvarsym :
  484. list.concat(Tai_const.Createname(tstaticvarsym(srsym).mangledname,offset));
  485. labelsym :
  486. list.concat(Tai_const.Createname(tlabelsym(srsym).mangledname,offset));
  487. constsym :
  488. if tconstsym(srsym).consttyp=constresourcestring then
  489. list.concat(Tai_const.Createname(make_mangledname('RESSTR',tconstsym(srsym).owner,tconstsym(srsym).name),sizeof(aint)))
  490. else
  491. Message(type_e_variable_id_expected);
  492. else
  493. Message(type_e_variable_id_expected);
  494. end;
  495. end
  496. else
  497. Message(parser_e_illegal_expression);
  498. end
  499. else
  500. { allow typeof(Object type)}
  501. if (p.nodetype=inlinen) and
  502. (tinlinenode(p).inlinenumber=in_typeof_x) then
  503. begin
  504. if (tinlinenode(p).left.nodetype=typen) then
  505. begin
  506. list.concat(Tai_const.createname(
  507. tobjectdef(tinlinenode(p).left.resultdef).vmt_mangledname,0));
  508. end
  509. else
  510. Message(parser_e_illegal_expression);
  511. end
  512. else
  513. Message(parser_e_illegal_expression);
  514. p.free;
  515. end;
  516. procedure parse_setdef(list:tasmlist;def:tsetdef);
  517. type
  518. setbytes = array[0..31] of byte;
  519. Psetbytes = ^setbytes;
  520. var
  521. p : tnode;
  522. i : longint;
  523. begin
  524. p:=comp_expr(true);
  525. if p.nodetype=setconstn then
  526. begin
  527. { be sure to convert to the correct result, else
  528. it can generate smallset data instead of normalset (PFV) }
  529. inserttypeconv(p,def);
  530. { we only allow const sets }
  531. if assigned(tsetconstnode(p).left) then
  532. Message(parser_e_illegal_expression)
  533. else
  534. begin
  535. { this writing is endian independant }
  536. { untrue - because they are considered }
  537. { arrays of 32-bit values CEC }
  538. if source_info.endian = target_info.endian then
  539. begin
  540. {$if defined(FPC_NEW_BIGENDIAN_SETS) or defined(FPC_LITTLE_ENDIAN)}
  541. for i:=0 to p.resultdef.size-1 do
  542. list.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[i]));
  543. {$else}
  544. for i:=0 to p.resultdef.size-1 do
  545. list.concat(tai_const.create_8bit(reverse_byte(Psetbytes(tsetconstnode(p).value_set)^[i xor 3])));
  546. {$endif}
  547. end
  548. else
  549. begin
  550. for i:=0 to p.resultdef.size-1 do
  551. list.concat(tai_const.create_8bit(reverse_byte(Psetbytes(tsetconstnode(p).value_set)^[i])));
  552. end;
  553. end;
  554. end
  555. else
  556. Message(parser_e_illegal_expression);
  557. p.free;
  558. end;
  559. procedure parse_enumdef(list:tasmlist;def:tenumdef);
  560. var
  561. p : tnode;
  562. begin
  563. p:=comp_expr(true);
  564. if p.nodetype=ordconstn then
  565. begin
  566. if equal_defs(p.resultdef,def) or
  567. is_subequal(p.resultdef,def) then
  568. begin
  569. case longint(p.resultdef.size) of
  570. 1 : list.concat(Tai_const.Create_8bit(Byte(tordconstnode(p).value.svalue)));
  571. 2 : list.concat(Tai_const.Create_16bit(Word(tordconstnode(p).value.svalue)));
  572. 4 : list.concat(Tai_const.Create_32bit(Longint(tordconstnode(p).value.svalue)));
  573. end;
  574. end
  575. else
  576. IncompatibleTypes(p.resultdef,def);
  577. end
  578. else
  579. Message(parser_e_illegal_expression);
  580. p.free;
  581. end;
  582. procedure parse_stringdef(list:tasmlist;def:tstringdef);
  583. var
  584. n : tnode;
  585. i : longint;
  586. strlength : aint;
  587. strval : pchar;
  588. strch : char;
  589. ll : tasmlabel;
  590. ca : pchar;
  591. begin
  592. n:=comp_expr(true);
  593. { load strval and strlength of the constant tree }
  594. if (n.nodetype=stringconstn) or is_widestring(def) then
  595. begin
  596. { convert to the expected string type so that
  597. for widestrings strval is a pcompilerwidestring }
  598. inserttypeconv(n,def);
  599. strlength:=tstringconstnode(n).len;
  600. strval:=tstringconstnode(n).value_str;
  601. end
  602. else if is_constcharnode(n) then
  603. begin
  604. { strval:=pchar(@tordconstnode(n).value);
  605. THIS FAIL on BIG_ENDIAN MACHINES PM }
  606. strch:=chr(tordconstnode(n).value.svalue and $ff);
  607. strval:=@strch;
  608. strlength:=1
  609. end
  610. else if is_constresourcestringnode(n) then
  611. begin
  612. strval:=pchar(tconstsym(tloadnode(n).symtableentry).value.valueptr);
  613. strlength:=tconstsym(tloadnode(n).symtableentry).value.len;
  614. end
  615. else
  616. begin
  617. Message(parser_e_illegal_expression);
  618. strlength:=-1;
  619. end;
  620. if strlength>=0 then
  621. begin
  622. case def.stringtype of
  623. st_shortstring:
  624. begin
  625. if strlength>=def.size then
  626. begin
  627. message2(parser_w_string_too_long,strpas(strval),tostr(def.size-1));
  628. strlength:=def.size-1;
  629. end;
  630. list.concat(Tai_const.Create_8bit(strlength));
  631. { this can also handle longer strings }
  632. getmem(ca,strlength+1);
  633. move(strval^,ca^,strlength);
  634. ca[strlength]:=#0;
  635. list.concat(Tai_string.Create_pchar(ca,strlength));
  636. { fillup with spaces if size is shorter }
  637. if def.size>strlength then
  638. begin
  639. getmem(ca,def.size-strlength);
  640. { def.size contains also the leading length, so we }
  641. { we have to subtract one }
  642. fillchar(ca[0],def.size-strlength-1,' ');
  643. ca[def.size-strlength-1]:=#0;
  644. { this can also handle longer strings }
  645. list.concat(Tai_string.Create_pchar(ca,def.size-strlength-1));
  646. end;
  647. end;
  648. st_ansistring:
  649. begin
  650. { an empty ansi string is nil! }
  651. if (strlength=0) then
  652. list.concat(Tai_const.Create_sym(nil))
  653. else
  654. begin
  655. current_asmdata.getdatalabel(ll);
  656. list.concat(Tai_const.Create_sym(ll));
  657. current_asmdata.asmlists[al_const].concat(tai_align.create(const_align(sizeof(aint))));
  658. current_asmdata.asmlists[al_const].concat(Tai_const.Create_aint(-1));
  659. current_asmdata.asmlists[al_const].concat(Tai_const.Create_aint(strlength));
  660. current_asmdata.asmlists[al_const].concat(Tai_label.Create(ll));
  661. getmem(ca,strlength+1);
  662. move(strval^,ca^,strlength);
  663. { The terminating #0 to be stored in the .data section (JM) }
  664. ca[strlength]:=#0;
  665. current_asmdata.asmlists[al_const].concat(Tai_string.Create_pchar(ca,strlength+1));
  666. end;
  667. end;
  668. st_widestring:
  669. begin
  670. { an empty ansi string is nil! }
  671. if (strlength=0) then
  672. list.concat(Tai_const.Create_sym(nil))
  673. else
  674. begin
  675. current_asmdata.getdatalabel(ll);
  676. list.concat(Tai_const.Create_sym(ll));
  677. current_asmdata.asmlists[al_const].concat(tai_align.create(const_align(sizeof(aint))));
  678. if tf_winlikewidestring in target_info.flags then
  679. current_asmdata.asmlists[al_const].concat(Tai_const.Create_32bit(strlength*cwidechartype.size))
  680. else
  681. begin
  682. current_asmdata.asmlists[al_const].concat(Tai_const.Create_aint(-1));
  683. current_asmdata.asmlists[al_const].concat(Tai_const.Create_aint(strlength*cwidechartype.size));
  684. end;
  685. current_asmdata.asmlists[al_const].concat(Tai_label.Create(ll));
  686. for i:=0 to strlength-1 do
  687. current_asmdata.asmlists[al_const].concat(Tai_const.Create_16bit(pcompilerwidestring(strval)^.data[i]));
  688. { ending #0 }
  689. current_asmdata.asmlists[al_const].concat(Tai_const.Create_16bit(0))
  690. end;
  691. end;
  692. else
  693. internalerror(200107081);
  694. end;
  695. end;
  696. n.free;
  697. end;
  698. { parse a single constant and add it to the packed const info }
  699. { represented by curval etc (see explanation of bitpackval for }
  700. { what the different parameters mean) }
  701. function parse_single_packed_const(list: tasmlist; def: tdef; var bp: tbitpackedval): boolean;
  702. var
  703. n : tnode;
  704. begin
  705. result:=true;
  706. n:=comp_expr(true);
  707. if (n.nodetype <> ordconstn) or
  708. not equal_defs(n.resultdef,def) and
  709. not is_subequal(n.resultdef,def) then
  710. begin
  711. n.free;
  712. incompatibletypes(n.resultdef,def);
  713. consume_all_until(_SEMICOLON);
  714. result:=false;
  715. exit;
  716. end;
  717. if (Tordconstnode(n).value<qword(low(Aword))) or (Tordconstnode(n).value>qword(high(Aword))) then
  718. message(parser_e_range_check_error)
  719. else
  720. bitpackval(Tordconstnode(n).value.uvalue,bp);
  721. if (bp.curbitoffset>=AIntBits) then
  722. flush_packed_value(list,bp);
  723. n.free;
  724. end;
  725. { parses a packed array constant }
  726. procedure parse_packed_array_def(list: tasmlist; def: tarraydef);
  727. var
  728. i : aint;
  729. bp : tbitpackedval;
  730. begin
  731. if not(def.elementdef.typ in [orddef,enumdef]) then
  732. internalerror(2007022010);
  733. { begin of the array }
  734. consume(_LKLAMMER);
  735. initbitpackval(bp,def.elepackedbitsize);
  736. i:=def.lowrange;
  737. { can't use for-loop, fails when cross-compiling from }
  738. { 32 to 64 bit because i is then 64 bit }
  739. while (i<def.highrange) do
  740. begin
  741. { get next item of the packed array }
  742. if not parse_single_packed_const(list,def.elementdef,bp) then
  743. exit;
  744. consume(_COMMA);
  745. inc(i);
  746. end;
  747. { final item }
  748. if not parse_single_packed_const(list,def.elementdef,bp) then
  749. exit;
  750. { flush final incomplete value if necessary }
  751. if (bp.curbitoffset <> 0) then
  752. flush_packed_value(list,bp);
  753. consume(_RKLAMMER);
  754. end;
  755. procedure parse_arraydef(list:tasmlist;def:tarraydef);
  756. var
  757. n : tnode;
  758. i : longint;
  759. len : aint;
  760. ch : char;
  761. ca : pchar;
  762. begin
  763. { dynamic array nil }
  764. if is_dynamic_array(def) then
  765. begin
  766. { Only allow nil initialization }
  767. consume(_NIL);
  768. list.concat(Tai_const.Create_sym(nil));
  769. end
  770. { packed array constant }
  771. else if is_packed_array(def) and
  772. (def.elepackedbitsize mod 8 <> 0) then
  773. begin
  774. parse_packed_array_def(list,def);
  775. end
  776. { normal array const between brackets }
  777. else if try_to_consume(_LKLAMMER) then
  778. begin
  779. for i:=def.lowrange to def.highrange-1 do
  780. begin
  781. read_typed_const_data(list,def.elementdef);
  782. consume(_COMMA);
  783. end;
  784. read_typed_const_data(list,def.elementdef);
  785. consume(_RKLAMMER);
  786. end
  787. { if array of char then we allow also a string }
  788. else if is_char(def.elementdef) then
  789. begin
  790. n:=comp_expr(true);
  791. if n.nodetype=stringconstn then
  792. begin
  793. len:=tstringconstnode(n).len;
  794. { For tp7 the maximum lentgh can be 255 }
  795. if (m_tp7 in current_settings.modeswitches) and
  796. (len>255) then
  797. len:=255;
  798. ca:=tstringconstnode(n).value_str;
  799. end
  800. else
  801. if is_constcharnode(n) then
  802. begin
  803. ch:=chr(tordconstnode(n).value.uvalue and $ff);
  804. ca:=@ch;
  805. len:=1;
  806. end
  807. else
  808. begin
  809. Message(parser_e_illegal_expression);
  810. len:=0;
  811. end;
  812. if len>(def.highrange-def.lowrange+1) then
  813. Message(parser_e_string_larger_array);
  814. for i:=def.lowrange to def.highrange do
  815. begin
  816. if i+1-def.lowrange<=len then
  817. begin
  818. list.concat(Tai_const.Create_8bit(byte(ca^)));
  819. inc(ca);
  820. end
  821. else
  822. {Fill the remaining positions with #0.}
  823. list.concat(Tai_const.Create_8bit(0));
  824. end;
  825. n.free;
  826. end
  827. else
  828. begin
  829. { we want the ( }
  830. consume(_LKLAMMER);
  831. end;
  832. end;
  833. procedure parse_procvardef(list:tasmlist;def:tprocvardef);
  834. var
  835. tmpn,n : tnode;
  836. pd : tprocdef;
  837. begin
  838. { Procvars and pointers are no longer compatible. }
  839. { under tp: =nil or =var under fpc: =nil or =@var }
  840. if try_to_consume(_NIL) then
  841. begin
  842. list.concat(Tai_const.Create_sym(nil));
  843. if (po_methodpointer in def.procoptions) then
  844. list.concat(Tai_const.Create_sym(nil));
  845. exit;
  846. end;
  847. { you can't assign a value other than NIL to a typed constant }
  848. { which is a "procedure of object", because this also requires }
  849. { address of an object/class instance, which is not known at }
  850. { compile time (JM) }
  851. if (po_methodpointer in def.procoptions) then
  852. Message(parser_e_no_procvarobj_const);
  853. { parse the rest too, so we can continue with error checking }
  854. getprocvardef:=def;
  855. n:=comp_expr(true);
  856. getprocvardef:=nil;
  857. if codegenerror then
  858. begin
  859. n.free;
  860. exit;
  861. end;
  862. { let type conversion check everything needed }
  863. inserttypeconv(n,def);
  864. if codegenerror then
  865. begin
  866. n.free;
  867. exit;
  868. end;
  869. { remove typeconvs, that will normally insert a lea
  870. instruction which is not necessary for us }
  871. while n.nodetype=typeconvn do
  872. begin
  873. tmpn:=ttypeconvnode(n).left;
  874. ttypeconvnode(n).left:=nil;
  875. n.free;
  876. n:=tmpn;
  877. end;
  878. { remove addrn which we also don't need here }
  879. if n.nodetype=addrn then
  880. begin
  881. tmpn:=taddrnode(n).left;
  882. taddrnode(n).left:=nil;
  883. n.free;
  884. n:=tmpn;
  885. end;
  886. { we now need to have a loadn with a procsym }
  887. if (n.nodetype=loadn) and
  888. (tloadnode(n).symtableentry.typ=procsym) then
  889. begin
  890. pd:=tprocdef(tprocsym(tloadnode(n).symtableentry).ProcdefList[0]);
  891. list.concat(Tai_const.createname(pd.mangledname,0));
  892. end
  893. else
  894. Message(parser_e_illegal_expression);
  895. n.free;
  896. end;
  897. procedure parse_recorddef(list:tasmlist;def:trecorddef);
  898. var
  899. n : tnode;
  900. i,
  901. symidx : longint;
  902. recsym,
  903. srsym : tsym;
  904. hs : string;
  905. sorg,s : TIDString;
  906. tmpguid : tguid;
  907. curroffset,
  908. fillbytes : aint;
  909. bp : tbitpackedval;
  910. error,
  911. is_packed: boolean;
  912. begin
  913. { GUID }
  914. if (def=rec_tguid) and
  915. ((token=_CSTRING) or (token=_CCHAR) or (token=_ID)) then
  916. begin
  917. n:=comp_expr(true);
  918. inserttypeconv(n,cshortstringtype);
  919. if n.nodetype=stringconstn then
  920. begin
  921. hs:=strpas(tstringconstnode(n).value_str);
  922. if string2guid(hs,tmpguid) then
  923. begin
  924. list.concat(Tai_const.Create_32bit(longint(tmpguid.D1)));
  925. list.concat(Tai_const.Create_16bit(tmpguid.D2));
  926. list.concat(Tai_const.Create_16bit(tmpguid.D3));
  927. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  928. list.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  929. end
  930. else
  931. Message(parser_e_improper_guid_syntax);
  932. end
  933. else
  934. Message(parser_e_illegal_expression);
  935. n.free;
  936. exit;
  937. end;
  938. { bitpacked record? }
  939. is_packed:=is_packed_record_or_object(def);
  940. if (is_packed) then
  941. begin
  942. { loadbitsize = 8, bitpacked records are always padded to }
  943. { a multiple of a byte. packedbitsize will be set separately }
  944. { for each field }
  945. initbitpackval(bp,0);
  946. bp.loadbitsize:=8;
  947. end;
  948. { normal record }
  949. consume(_LKLAMMER);
  950. curroffset:=0;
  951. symidx:=0;
  952. sorg:='';
  953. srsym:=tsym(def.symtable.SymList[symidx]);
  954. recsym := nil;
  955. while token<>_RKLAMMER do
  956. begin
  957. s:=pattern;
  958. sorg:=orgpattern;
  959. consume(_ID);
  960. consume(_COLON);
  961. error := false;
  962. recsym := tsym(def.symtable.Find(s));
  963. if not assigned(recsym) then
  964. begin
  965. Message1(sym_e_illegal_field,sorg);
  966. error := true;
  967. end;
  968. if (not error) and
  969. (not assigned(srsym) or
  970. (s <> srsym.name)) then
  971. { possible variant record (JM) }
  972. begin
  973. { All parts of a variant start at the same offset }
  974. { Also allow jumping from one variant part to another, }
  975. { as long as the offsets match }
  976. if (assigned(srsym) and
  977. (tfieldvarsym(recsym).fieldoffset = tfieldvarsym(srsym).fieldoffset)) or
  978. { srsym is not assigned after parsing w2 in the }
  979. { typed const in the next example: }
  980. { type tr = record case byte of }
  981. { 1: (l1,l2: dword); }
  982. { 2: (w1,w2: word); }
  983. { end; }
  984. { const r: tr = (w1:1;w2:1;l2:5); }
  985. (tfieldvarsym(recsym).fieldoffset = curroffset) then
  986. srsym := recsym
  987. { going backwards isn't allowed in any mode }
  988. else if (tfieldvarsym(recsym).fieldoffset<curroffset) then
  989. begin
  990. Message(parser_e_invalid_record_const);
  991. error := true;
  992. end
  993. { Delphi allows you to skip fields }
  994. else if (m_delphi in current_settings.modeswitches) then
  995. begin
  996. Message1(parser_w_skipped_fields_before,sorg);
  997. srsym := recsym;
  998. end
  999. { FPC and TP don't }
  1000. else
  1001. begin
  1002. Message1(parser_e_skipped_fields_before,sorg);
  1003. error := true;
  1004. end;
  1005. end;
  1006. if error then
  1007. consume_all_until(_SEMICOLON)
  1008. else
  1009. begin
  1010. { if needed fill (alignment) }
  1011. if tfieldvarsym(srsym).fieldoffset>curroffset then
  1012. begin
  1013. if not(is_packed) then
  1014. fillbytes:=tfieldvarsym(srsym).fieldoffset-curroffset
  1015. else
  1016. begin
  1017. flush_packed_value(list,bp);
  1018. { curoffset is now aligned to the next byte }
  1019. curroffset:=align(curroffset,8);
  1020. { offsets are in bits in this case }
  1021. fillbytes:=(tfieldvarsym(srsym).fieldoffset-curroffset) div 8;
  1022. end;
  1023. for i:=1 to fillbytes do
  1024. list.concat(Tai_const.Create_8bit(0))
  1025. end;
  1026. { new position }
  1027. curroffset:=tfieldvarsym(srsym).fieldoffset;
  1028. if not(is_packed) then
  1029. inc(curroffset,tfieldvarsym(srsym).vardef.size)
  1030. else
  1031. inc(curroffset,tfieldvarsym(srsym).vardef.packedbitsize);
  1032. { read the data }
  1033. if not(is_packed) or
  1034. { only orddefs and enumdefs are bitpacked, as in gcc/gpc }
  1035. not(tfieldvarsym(srsym).vardef.typ in [orddef,enumdef]) then
  1036. begin
  1037. if is_packed then
  1038. begin
  1039. flush_packed_value(list,bp);
  1040. curroffset:=align(curroffset,8);
  1041. end;
  1042. read_typed_const_data(list,tfieldvarsym(srsym).vardef);
  1043. end
  1044. else
  1045. begin
  1046. bp.packedbitsize:=tfieldvarsym(srsym).vardef.packedbitsize;
  1047. parse_single_packed_const(list,tfieldvarsym(srsym).vardef,bp);
  1048. end;
  1049. { keep previous field for checking whether whole }
  1050. { record was initialized (JM) }
  1051. recsym := srsym;
  1052. { goto next field }
  1053. inc(symidx);
  1054. if symidx<def.symtable.SymList.Count then
  1055. srsym:=tsym(def.symtable.SymList[symidx])
  1056. else
  1057. srsym:=nil;
  1058. if token=_SEMICOLON then
  1059. consume(_SEMICOLON)
  1060. else
  1061. break;
  1062. end;
  1063. end;
  1064. { are there any fields left, but don't complain if there only
  1065. come other variant partsa fter the last initialized field }
  1066. if assigned(srsym) and
  1067. (
  1068. (recsym=nil) or
  1069. (tfieldvarsym(srsym).fieldoffset > tfieldvarsym(recsym).fieldoffset)
  1070. ) then
  1071. Message1(parser_w_skipped_fields_after,sorg);
  1072. if not(is_packed) then
  1073. fillbytes:=def.size-curroffset
  1074. else
  1075. begin
  1076. flush_packed_value(list,bp);
  1077. curroffset:=align(curroffset,8);
  1078. fillbytes:=def.size-(curroffset div 8);
  1079. end;
  1080. for i:=1 to fillbytes do
  1081. list.concat(Tai_const.Create_8bit(0));
  1082. consume(_RKLAMMER);
  1083. end;
  1084. procedure parse_objectdef(list:tasmlist;def:tobjectdef);
  1085. var
  1086. n : tnode;
  1087. i : longint;
  1088. obj : tobjectdef;
  1089. srsym : tsym;
  1090. st : tsymtable;
  1091. curroffset : aint;
  1092. s,sorg : TIDString;
  1093. vmtwritten : boolean;
  1094. begin
  1095. { no support for packed object }
  1096. if is_packed_record_or_object(def) then
  1097. begin
  1098. Message(type_e_no_const_packed_record);
  1099. exit;
  1100. end;
  1101. { only allow nil for class and interface }
  1102. if is_class_or_interface(def) then
  1103. begin
  1104. n:=comp_expr(true);
  1105. if n.nodetype<>niln then
  1106. begin
  1107. Message(parser_e_type_const_not_possible);
  1108. consume_all_until(_SEMICOLON);
  1109. end
  1110. else
  1111. list.concat(Tai_const.Create_sym(nil));
  1112. n.free;
  1113. exit;
  1114. end;
  1115. { for objects we allow it only if it doesn't contain a vmt }
  1116. if (oo_has_vmt in def.objectoptions) and
  1117. (m_fpc in current_settings.modeswitches) then
  1118. begin
  1119. Message(parser_e_type_object_constants);
  1120. exit;
  1121. end;
  1122. consume(_LKLAMMER);
  1123. curroffset:=0;
  1124. vmtwritten:=false;
  1125. while token<>_RKLAMMER do
  1126. begin
  1127. s:=pattern;
  1128. sorg:=orgpattern;
  1129. consume(_ID);
  1130. consume(_COLON);
  1131. srsym:=nil;
  1132. obj:=tobjectdef(def);
  1133. st:=obj.symtable;
  1134. while (srsym=nil) and assigned(st) do
  1135. begin
  1136. srsym:=tsym(st.Find(s));
  1137. if assigned(obj) then
  1138. obj:=obj.childof;
  1139. if assigned(obj) then
  1140. st:=obj.symtable
  1141. else
  1142. st:=nil;
  1143. end;
  1144. if srsym=nil then
  1145. begin
  1146. Message1(sym_e_id_not_found,sorg);
  1147. consume_all_until(_SEMICOLON);
  1148. end
  1149. else
  1150. with tfieldvarsym(srsym) do
  1151. begin
  1152. { check position }
  1153. if fieldoffset<curroffset then
  1154. message(parser_e_invalid_record_const);
  1155. { check in VMT needs to be added for TP mode }
  1156. if not(vmtwritten) and
  1157. not(m_fpc in current_settings.modeswitches) and
  1158. (oo_has_vmt in def.objectoptions) and
  1159. (def.vmt_offset<fieldoffset) then
  1160. begin
  1161. for i:=1 to def.vmt_offset-curroffset do
  1162. list.concat(tai_const.create_8bit(0));
  1163. list.concat(tai_const.createname(def.vmt_mangledname,0));
  1164. { this is more general }
  1165. curroffset:=def.vmt_offset + sizeof(aint);
  1166. vmtwritten:=true;
  1167. end;
  1168. { if needed fill }
  1169. if fieldoffset>curroffset then
  1170. for i:=1 to fieldoffset-curroffset do
  1171. list.concat(Tai_const.Create_8bit(0));
  1172. { new position }
  1173. curroffset:=fieldoffset+vardef.size;
  1174. { read the data }
  1175. read_typed_const_data(list,vardef);
  1176. if not try_to_consume(_SEMICOLON) then
  1177. break;
  1178. end;
  1179. end;
  1180. if not(m_fpc in current_settings.modeswitches) and
  1181. (oo_has_vmt in def.objectoptions) and
  1182. (def.vmt_offset>=curroffset) then
  1183. begin
  1184. for i:=1 to def.vmt_offset-curroffset do
  1185. list.concat(tai_const.create_8bit(0));
  1186. list.concat(tai_const.createname(def.vmt_mangledname,0));
  1187. { this is more general }
  1188. curroffset:=def.vmt_offset + sizeof(aint);
  1189. end;
  1190. for i:=1 to def.size-curroffset do
  1191. list.concat(Tai_const.Create_8bit(0));
  1192. consume(_RKLAMMER);
  1193. end;
  1194. var
  1195. old_block_type : tblock_type;
  1196. begin
  1197. old_block_type:=block_type;
  1198. block_type:=bt_const;
  1199. case def.typ of
  1200. orddef :
  1201. parse_orddef(list,torddef(def));
  1202. floatdef :
  1203. parse_floatdef(list,tfloatdef(def));
  1204. classrefdef :
  1205. parse_classrefdef(list,tclassrefdef(def));
  1206. pointerdef :
  1207. parse_pointerdef(list,tpointerdef(def));
  1208. setdef :
  1209. parse_setdef(list,tsetdef(def));
  1210. enumdef :
  1211. parse_enumdef(list,tenumdef(def));
  1212. stringdef :
  1213. parse_stringdef(list,tstringdef(def));
  1214. arraydef :
  1215. parse_arraydef(list,tarraydef(def));
  1216. procvardef:
  1217. parse_procvardef(list,tprocvardef(def));
  1218. recorddef:
  1219. parse_recorddef(list,trecorddef(def));
  1220. objectdef:
  1221. parse_objectdef(list,tobjectdef(def));
  1222. errordef:
  1223. begin
  1224. { try to consume something useful }
  1225. if token=_LKLAMMER then
  1226. consume_all_until(_RKLAMMER)
  1227. else
  1228. consume_all_until(_SEMICOLON);
  1229. end;
  1230. else
  1231. Message(parser_e_type_const_not_possible);
  1232. end;
  1233. block_type:=old_block_type;
  1234. end;
  1235. {$maxfpuregisters default}
  1236. procedure read_typed_const(list:tasmlist;sym:tstaticvarsym);
  1237. var
  1238. storefilepos : tfileposinfo;
  1239. cursectype : TAsmSectionType;
  1240. C_name : string;
  1241. begin
  1242. { mark the staticvarsym as typedconst }
  1243. include(sym.varoptions,vo_is_typed_const);
  1244. { The variable has a value assigned }
  1245. sym.varstate:=vs_initialised;
  1246. { the variable can't be placed in a register }
  1247. sym.varregable:=vr_none;
  1248. { generate data for typed const }
  1249. storefilepos:=current_filepos;
  1250. current_filepos:=sym.fileinfo;
  1251. if sym.varspez=vs_const then
  1252. cursectype:=sec_rodata
  1253. else
  1254. cursectype:=sec_data;
  1255. maybe_new_object_file(list);
  1256. new_section(list,cursectype,lower(sym.mangledname),const_align(sym.vardef.alignment));
  1257. if (sym.owner.symtabletype=globalsymtable) or
  1258. maybe_smartlink_symbol or
  1259. (assigned(current_procinfo) and
  1260. (po_inline in current_procinfo.procdef.procoptions)) or
  1261. DLLSource then
  1262. list.concat(Tai_symbol.Createname_global(sym.mangledname,AT_DATA,0))
  1263. else
  1264. list.concat(Tai_symbol.Createname(sym.mangledname,AT_DATA,0));
  1265. read_typed_const_data(list,sym.vardef);
  1266. list.concat(tai_symbol_end.Createname(sym.mangledname));
  1267. current_filepos:=storefilepos;
  1268. { Parse hints }
  1269. try_consume_hintdirective(sym.symoptions);
  1270. { Support public name directive }
  1271. if try_to_consume(_PUBLIC) then
  1272. begin
  1273. include(sym.varoptions,vo_is_public);
  1274. if try_to_consume(_NAME) then
  1275. C_name:=get_stringconst
  1276. else
  1277. C_name:=sym.realname;
  1278. sym.set_mangledname(C_Name);
  1279. end;
  1280. end;
  1281. end.