ptconst.pas 54 KB

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