ptconst.pas 51 KB

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