ptconst.pas 54 KB

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