ptconst.pas 54 KB

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