2
0

ptconst.pas 56 KB

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