ptconst.pas 52 KB

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