ptconst.pas 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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. { this procedure reads typed constants }
  22. { sym is only needed for ansi strings }
  23. { the assembler label is in the middle (PM) }
  24. procedure readtypedconst(list:tasmlist;def:tdef;sym : ttypedconstsym;writable : boolean);
  25. implementation
  26. uses
  27. strings,
  28. globtype,systems,tokens,verbose,
  29. cutils,globals,widestr,scanner,
  30. symconst,symbase,symdef,symtable,
  31. aasmbase,aasmtai,aasmcpu,defutil,defcmp,
  32. { pass 1 }
  33. node,htypechk,procinfo,
  34. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,
  35. { parser specific stuff }
  36. pbase,pexpr,
  37. { codegen }
  38. cpuinfo,cgbase,dbgbase
  39. ;
  40. {$maxfpuregisters 0}
  41. { this procedure reads typed constants }
  42. procedure readtypedconst(list:tasmlist;def:tdef;sym : ttypedconstsym;writable : boolean);
  43. label
  44. myexit;
  45. type
  46. setbytes = array[0..31] of byte;
  47. Psetbytes = ^setbytes;
  48. var
  49. len,base : longint;
  50. p,hp : tnode;
  51. i,j,l : longint;
  52. varalign : shortint;
  53. offset,
  54. strlength : aint;
  55. ll : tasmlabel;
  56. c_name,
  57. s,sorg : string;
  58. c : char;
  59. ca : pchar;
  60. tmpguid : tguid;
  61. aktpos : longint;
  62. obj : tobjectdef;
  63. recsym,
  64. srsym : tsym;
  65. symt : tsymtable;
  66. value : bestreal;
  67. intvalue : tconstexprint;
  68. strval : pchar;
  69. pw : pcompilerwidestring;
  70. error : boolean;
  71. old_block_type : tblock_type;
  72. storefilepos : tfileposinfo;
  73. cursectype : TAsmSectiontype;
  74. datalist : tasmlist;
  75. procedure check_range(def:torddef);
  76. begin
  77. if ((tordconstnode(p).value>def.high) or
  78. (tordconstnode(p).value<def.low)) then
  79. begin
  80. if (cs_check_range in current_settings.localswitches) then
  81. Message(parser_e_range_check_error)
  82. else
  83. Message(parser_w_range_check_error);
  84. end;
  85. end;
  86. begin
  87. old_block_type:=block_type;
  88. block_type:=bt_const;
  89. datalist:=tasmlist.create;
  90. case def.deftype of
  91. orddef:
  92. begin
  93. p:=comp_expr(true);
  94. case torddef(def).typ of
  95. bool8bit :
  96. begin
  97. if is_constboolnode(p) then
  98. datalist.concat(Tai_const.Create_8bit(byte(tordconstnode(p).value)))
  99. else
  100. Message(parser_e_illegal_expression);
  101. end;
  102. bool16bit :
  103. begin
  104. if is_constboolnode(p) then
  105. datalist.concat(Tai_const.Create_16bit(word(tordconstnode(p).value)))
  106. else
  107. Message(parser_e_illegal_expression);
  108. end;
  109. bool32bit :
  110. begin
  111. if is_constboolnode(p) then
  112. datalist.concat(Tai_const.Create_32bit(longint(tordconstnode(p).value)))
  113. else
  114. Message(parser_e_illegal_expression);
  115. end;
  116. bool64bit :
  117. begin
  118. if is_constboolnode(p) then
  119. datalist.concat(Tai_const.Create_64bit(int64(tordconstnode(p).value)))
  120. else
  121. Message(parser_e_illegal_expression);
  122. end;
  123. uchar :
  124. begin
  125. if is_constcharnode(p) then
  126. datalist.concat(Tai_const.Create_8bit(byte(tordconstnode(p).value)))
  127. else
  128. Message(parser_e_illegal_expression);
  129. end;
  130. uwidechar :
  131. begin
  132. if is_constcharnode(p) then
  133. inserttypeconv(p,cwidechartype);
  134. if is_constwidecharnode(p) then
  135. datalist.concat(Tai_const.Create_16bit(word(tordconstnode(p).value)))
  136. else
  137. Message(parser_e_illegal_expression);
  138. end;
  139. s8bit,
  140. u8bit :
  141. begin
  142. if is_constintnode(p) then
  143. begin
  144. datalist.concat(Tai_const.Create_8bit(byte(tordconstnode(p).value)));
  145. check_range(torddef(def));
  146. end
  147. else
  148. Message(parser_e_illegal_expression);
  149. end;
  150. u16bit,
  151. s16bit :
  152. begin
  153. if is_constintnode(p) then
  154. begin
  155. datalist.concat(Tai_const.Create_16bit(word(tordconstnode(p).value)));
  156. check_range(torddef(def));
  157. end
  158. else
  159. Message(parser_e_illegal_expression);
  160. end;
  161. s32bit,
  162. u32bit :
  163. begin
  164. if is_constintnode(p) then
  165. begin
  166. datalist.concat(Tai_const.Create_32bit(longint(tordconstnode(p).value)));
  167. if torddef(def).typ<>u32bit then
  168. check_range(torddef(def));
  169. end
  170. else
  171. Message(parser_e_illegal_expression);
  172. end;
  173. s64bit,
  174. u64bit,
  175. scurrency:
  176. begin
  177. if is_constintnode(p) then
  178. intvalue := tordconstnode(p).value
  179. else if is_constrealnode(p) and
  180. (torddef(def).typ=scurrency)
  181. { allow bootstrapping }
  182. then
  183. begin
  184. intvalue:=round(trealconstnode(p).value_real*10000);
  185. end
  186. else
  187. begin
  188. intvalue:=0;
  189. Message(parser_e_illegal_expression);
  190. end;
  191. datalist.concat(Tai_const.Create_64bit(intvalue));
  192. end;
  193. else
  194. internalerror(3799);
  195. end;
  196. p.free;
  197. end;
  198. floatdef:
  199. begin
  200. p:=comp_expr(true);
  201. if is_constrealnode(p) then
  202. value:=trealconstnode(p).value_real
  203. else if is_constintnode(p) then
  204. value:=tordconstnode(p).value
  205. else
  206. Message(parser_e_illegal_expression);
  207. case tfloatdef(def).typ of
  208. s32real :
  209. datalist.concat(Tai_real_32bit.Create(ts32real(value)));
  210. s64real :
  211. {$ifdef ARM}
  212. if current_settings.fputype in [fpu_fpa,fpu_fpa10,fpu_fpa11] then
  213. datalist.concat(Tai_real_64bit.Create_hiloswapped(ts64real(value)))
  214. else
  215. {$endif ARM}
  216. datalist.concat(Tai_real_64bit.Create(ts64real(value)));
  217. s80real :
  218. datalist.concat(Tai_real_80bit.Create(value));
  219. { the round is necessary for native compilers where comp isn't a float }
  220. s64comp :
  221. datalist.concat(Tai_comp_64bit.Create(round(value)));
  222. s64currency:
  223. datalist.concat(Tai_comp_64bit.Create(round(value*10000)));
  224. s128real:
  225. datalist.concat(Tai_real_128bit.Create(value));
  226. else
  227. internalerror(18);
  228. end;
  229. p.free;
  230. end;
  231. classrefdef:
  232. begin
  233. p:=comp_expr(true);
  234. case p.nodetype of
  235. loadvmtaddrn:
  236. with Tclassrefdef(p.resultdef) do
  237. begin
  238. if not Tobjectdef(pointeddef).is_related(Tobjectdef(tclassrefdef(def).pointeddef)) then
  239. message(parser_e_illegal_expression);
  240. datalist.concat(Tai_const.Create_sym(current_asmdata.RefAsmSymbol(
  241. Tobjectdef(pointeddef).vmt_mangledname)));
  242. end;
  243. niln:
  244. datalist.concat(Tai_const.Create_sym(nil));
  245. else Message(parser_e_illegal_expression);
  246. end;
  247. p.free;
  248. end;
  249. pointerdef:
  250. begin
  251. p:=comp_expr(true);
  252. if (p.nodetype=typeconvn) then
  253. with Ttypeconvnode(p) do
  254. if (left.nodetype in [addrn,niln]) and equal_defs(def,p.resultdef) then
  255. begin
  256. hp:=left;
  257. left:=nil;
  258. p.free;
  259. p:=hp;
  260. end;
  261. { allows horrible ofs(typeof(TButton)^) code !! }
  262. if (p.nodetype=addrn) then
  263. with Taddrnode(p) do
  264. if left.nodetype=derefn then
  265. begin
  266. hp:=tderefnode(left).left;
  267. tderefnode(left).left:=nil;
  268. p.free;
  269. p:=hp;
  270. end;
  271. { const pointer ? }
  272. if (p.nodetype = pointerconstn) then
  273. begin
  274. if sizeof(TConstPtrUInt)=8 then
  275. datalist.concat(Tai_const.Create_64bit(TConstPtrUInt(tpointerconstnode(p).value)))
  276. else
  277. if sizeof(TConstPtrUInt)=4 then
  278. datalist.concat(Tai_const.Create_32bit(TConstPtrUInt(tpointerconstnode(p).value)))
  279. else
  280. internalerror(200404122);
  281. end
  282. { nil pointer ? }
  283. else if p.nodetype=niln then
  284. datalist.concat(Tai_const.Create_sym(nil))
  285. { maybe pchar ? }
  286. else
  287. if is_char(tpointerdef(def).pointeddef) and
  288. (p.nodetype<>addrn) then
  289. begin
  290. current_asmdata.getdatalabel(ll);
  291. datalist.concat(Tai_const.Create_sym(ll));
  292. if p.nodetype=stringconstn then
  293. varalign:=size_2_align(tstringconstnode(p).len)
  294. else
  295. varalign:=0;
  296. varalign:=const_align(varalign);
  297. current_asmdata.asmlists[al_const].concat(Tai_align.Create(varalign));
  298. current_asmdata.asmlists[al_const].concat(Tai_label.Create(ll));
  299. if p.nodetype=stringconstn then
  300. begin
  301. len:=tstringconstnode(p).len;
  302. { For tp7 the maximum lentgh can be 255 }
  303. if (m_tp7 in current_settings.modeswitches) and
  304. (len>255) then
  305. len:=255;
  306. getmem(ca,len+2);
  307. move(tstringconstnode(p).value_str^,ca^,len+1);
  308. current_asmdata.asmlists[al_const].concat(Tai_string.Create_pchar(ca,len+1));
  309. end
  310. else
  311. if is_constcharnode(p) then
  312. current_asmdata.asmlists[al_const].concat(Tai_string.Create(char(byte(tordconstnode(p).value))+#0))
  313. else
  314. message(parser_e_illegal_expression);
  315. end
  316. { maybe pwidechar ? }
  317. else
  318. if is_widechar(tpointerdef(def).pointeddef) and
  319. (p.nodetype<>addrn) then
  320. begin
  321. current_asmdata.getdatalabel(ll);
  322. datalist.concat(Tai_const.Create_sym(ll));
  323. current_asmdata.asmlists[al_typedconsts].concat(tai_align.create(const_align(sizeof(aint))));
  324. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(ll));
  325. if (p.nodetype in [stringconstn,ordconstn]) then
  326. begin
  327. { convert to widestring stringconstn }
  328. inserttypeconv(p,cwidestringtype);
  329. if (p.nodetype=stringconstn) and
  330. (tstringconstnode(p).cst_type=cst_widestring) then
  331. begin
  332. pw:=pcompilerwidestring(tstringconstnode(p).value_str);
  333. for i:=0 to tstringconstnode(p).len-1 do
  334. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_16bit(pw^.data[i]));
  335. { ending #0 }
  336. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_16bit(0))
  337. end;
  338. end
  339. else
  340. Message(parser_e_illegal_expression);
  341. end
  342. else
  343. if (p.nodetype=addrn) or
  344. is_procvar_load(p) then
  345. begin
  346. { insert typeconv }
  347. inserttypeconv(p,def);
  348. hp:=p;
  349. while assigned(hp) and (hp.nodetype in [addrn,typeconvn,subscriptn,vecn]) do
  350. hp:=tunarynode(hp).left;
  351. if (hp.nodetype=loadn) then
  352. begin
  353. hp:=p;
  354. offset:=0;
  355. while assigned(hp) and (hp.nodetype<>loadn) do
  356. begin
  357. case hp.nodetype of
  358. vecn :
  359. begin
  360. case tvecnode(hp).left.resultdef.deftype of
  361. stringdef :
  362. begin
  363. { this seems OK for shortstring and ansistrings PM }
  364. { it is wrong for widestrings !! }
  365. len:=1;
  366. base:=0;
  367. end;
  368. arraydef :
  369. begin
  370. if not is_packed_array(tvecnode(hp).left.resultdef) then
  371. begin
  372. len:=tarraydef(tvecnode(hp).left.resultdef).elesize;
  373. base:=tarraydef(tvecnode(hp).left.resultdef).lowrange;
  374. end
  375. else
  376. begin
  377. Message(parser_e_packed_dynamic_open_array);
  378. len:=1;
  379. base:=0;
  380. end;
  381. end
  382. else
  383. Message(parser_e_illegal_expression);
  384. end;
  385. if is_constintnode(tvecnode(hp).right) then
  386. inc(offset,len*(get_ordinal_value(tvecnode(hp).right)-base))
  387. else
  388. Message(parser_e_illegal_expression);
  389. end;
  390. subscriptn :
  391. inc(offset,tsubscriptnode(hp).vs.fieldoffset);
  392. typeconvn :
  393. begin
  394. if not(ttypeconvnode(hp).convtype in [tc_equal,tc_proc_2_procvar]) then
  395. Message(parser_e_illegal_expression);
  396. end;
  397. addrn :
  398. ;
  399. else
  400. Message(parser_e_illegal_expression);
  401. end;
  402. hp:=tunarynode(hp).left;
  403. end;
  404. srsym:=tloadnode(hp).symtableentry;
  405. case srsym.typ of
  406. procsym :
  407. begin
  408. if Tprocsym(srsym).procdef_count>1 then
  409. Message(parser_e_no_overloaded_procvars);
  410. if po_abstractmethod in tprocsym(srsym).first_procdef.procoptions then
  411. Message(type_e_cant_take_address_of_abstract_method)
  412. else
  413. datalist.concat(Tai_const.Createname(tprocsym(srsym).first_procdef.mangledname,offset));
  414. end;
  415. globalvarsym :
  416. datalist.concat(Tai_const.Createname(tglobalvarsym(srsym).mangledname,offset));
  417. typedconstsym :
  418. datalist.concat(Tai_const.Createname(ttypedconstsym(srsym).mangledname,offset));
  419. labelsym :
  420. datalist.concat(Tai_const.Createname(tlabelsym(srsym).mangledname,offset));
  421. constsym :
  422. if tconstsym(srsym).consttyp=constresourcestring then
  423. datalist.concat(Tai_const.Createname(make_mangledname('RESOURCESTRINGLIST',tconstsym(srsym).owner,''),tconstsym(srsym).resstrindex*(4+sizeof(aint)*3)+4+sizeof(aint)))
  424. else
  425. Message(type_e_variable_id_expected);
  426. else
  427. Message(type_e_variable_id_expected);
  428. end;
  429. end
  430. else
  431. Message(parser_e_illegal_expression);
  432. end
  433. else
  434. { allow typeof(Object type)}
  435. if (p.nodetype=inlinen) and
  436. (tinlinenode(p).inlinenumber=in_typeof_x) then
  437. begin
  438. if (tinlinenode(p).left.nodetype=typen) then
  439. begin
  440. datalist.concat(Tai_const.createname(
  441. tobjectdef(tinlinenode(p).left.resultdef).vmt_mangledname,0));
  442. end
  443. else
  444. Message(parser_e_illegal_expression);
  445. end
  446. else
  447. Message(parser_e_illegal_expression);
  448. p.free;
  449. end;
  450. setdef:
  451. begin
  452. p:=comp_expr(true);
  453. if p.nodetype=setconstn then
  454. begin
  455. { be sure to convert to the correct result, else
  456. it can generate smallset data instead of normalset (PFV) }
  457. inserttypeconv(p,def);
  458. { we only allow const sets }
  459. if assigned(tsetconstnode(p).left) then
  460. Message(parser_e_illegal_expression)
  461. else
  462. begin
  463. { this writing is endian independant }
  464. { untrue - because they are considered }
  465. { arrays of 32-bit values CEC }
  466. if source_info.endian = target_info.endian then
  467. begin
  468. for l:=0 to p.resultdef.size-1 do
  469. datalist.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[l]));
  470. end
  471. else
  472. begin
  473. { store as longint values in swaped format }
  474. j:=0;
  475. for l:=0 to ((p.resultdef.size-1) div 4) do
  476. begin
  477. datalist.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+3]));
  478. datalist.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+2]));
  479. datalist.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+1]));
  480. datalist.concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j]));
  481. Inc(j,4);
  482. end;
  483. end;
  484. end;
  485. end
  486. else
  487. Message(parser_e_illegal_expression);
  488. p.free;
  489. end;
  490. enumdef:
  491. begin
  492. p:=comp_expr(true);
  493. if p.nodetype=ordconstn then
  494. begin
  495. if equal_defs(p.resultdef,def) or
  496. is_subequal(p.resultdef,def) then
  497. begin
  498. case longint(p.resultdef.size) of
  499. 1 : datalist.concat(Tai_const.Create_8bit(Byte(tordconstnode(p).value)));
  500. 2 : datalist.concat(Tai_const.Create_16bit(Word(tordconstnode(p).value)));
  501. 4 : datalist.concat(Tai_const.Create_32bit(Longint(tordconstnode(p).value)));
  502. end;
  503. end
  504. else
  505. IncompatibleTypes(p.resultdef,def);
  506. end
  507. else
  508. Message(parser_e_illegal_expression);
  509. p.free;
  510. end;
  511. stringdef:
  512. begin
  513. p:=comp_expr(true);
  514. { load strval and strlength of the constant tree }
  515. if (p.nodetype=stringconstn) or is_widestring(def) then
  516. begin
  517. { convert to the expected string type so that
  518. for widestrings strval is a pcompilerwidestring }
  519. inserttypeconv(p,def);
  520. strlength:=tstringconstnode(p).len;
  521. strval:=tstringconstnode(p).value_str;
  522. end
  523. else if is_constcharnode(p) then
  524. begin
  525. { strval:=pchar(@tordconstnode(p).value);
  526. THIS FAIL on BIG_ENDIAN MACHINES PM }
  527. c:=chr(tordconstnode(p).value and $ff);
  528. strval:=@c;
  529. strlength:=1
  530. end
  531. else if is_constresourcestringnode(p) then
  532. begin
  533. strval:=pchar(tconstsym(tloadnode(p).symtableentry).value.valueptr);
  534. strlength:=tconstsym(tloadnode(p).symtableentry).value.len;
  535. end
  536. else
  537. begin
  538. Message(parser_e_illegal_expression);
  539. strlength:=-1;
  540. end;
  541. if strlength>=0 then
  542. begin
  543. case tstringdef(def).string_typ of
  544. st_shortstring:
  545. begin
  546. if strlength>=def.size then
  547. begin
  548. message2(parser_w_string_too_long,strpas(strval),tostr(def.size-1));
  549. strlength:=def.size-1;
  550. end;
  551. datalist.concat(Tai_const.Create_8bit(strlength));
  552. { this can also handle longer strings }
  553. getmem(ca,strlength+1);
  554. move(strval^,ca^,strlength);
  555. ca[strlength]:=#0;
  556. datalist.concat(Tai_string.Create_pchar(ca,strlength));
  557. { fillup with spaces if size is shorter }
  558. if def.size>strlength then
  559. begin
  560. getmem(ca,def.size-strlength);
  561. { def.size contains also the leading length, so we }
  562. { we have to subtract one }
  563. fillchar(ca[0],def.size-strlength-1,' ');
  564. ca[def.size-strlength-1]:=#0;
  565. { this can also handle longer strings }
  566. datalist.concat(Tai_string.Create_pchar(ca,def.size-strlength-1));
  567. end;
  568. end;
  569. st_ansistring:
  570. begin
  571. { an empty ansi string is nil! }
  572. if (strlength=0) then
  573. datalist.concat(Tai_const.Create_sym(nil))
  574. else
  575. begin
  576. current_asmdata.getdatalabel(ll);
  577. datalist.concat(Tai_const.Create_sym(ll));
  578. current_asmdata.asmlists[al_const].concat(tai_align.create(const_align(sizeof(aint))));
  579. current_asmdata.asmlists[al_const].concat(Tai_const.Create_aint(-1));
  580. current_asmdata.asmlists[al_const].concat(Tai_const.Create_aint(strlength));
  581. current_asmdata.asmlists[al_const].concat(Tai_label.Create(ll));
  582. getmem(ca,strlength+1);
  583. move(strval^,ca^,strlength);
  584. { The terminating #0 to be stored in the .data section (JM) }
  585. ca[strlength]:=#0;
  586. current_asmdata.asmlists[al_const].concat(Tai_string.Create_pchar(ca,strlength+1));
  587. end;
  588. end;
  589. st_widestring:
  590. begin
  591. { an empty ansi string is nil! }
  592. if (strlength=0) then
  593. datalist.concat(Tai_const.Create_sym(nil))
  594. else
  595. begin
  596. current_asmdata.getdatalabel(ll);
  597. datalist.concat(Tai_const.Create_sym(ll));
  598. current_asmdata.asmlists[al_const].concat(tai_align.create(const_align(sizeof(aint))));
  599. if tf_winlikewidestring in target_info.flags then
  600. current_asmdata.asmlists[al_const].concat(Tai_const.Create_32bit(strlength*cwidechartype.size))
  601. else
  602. begin
  603. current_asmdata.asmlists[al_const].concat(Tai_const.Create_aint(-1));
  604. current_asmdata.asmlists[al_const].concat(Tai_const.Create_aint(strlength*cwidechartype.size));
  605. end;
  606. current_asmdata.asmlists[al_const].concat(Tai_label.Create(ll));
  607. for i:=0 to strlength-1 do
  608. current_asmdata.asmlists[al_const].concat(Tai_const.Create_16bit(pcompilerwidestring(strval)^.data[i]));
  609. { ending #0 }
  610. current_asmdata.asmlists[al_const].concat(Tai_const.Create_16bit(0))
  611. end;
  612. end;
  613. st_longstring:
  614. begin
  615. internalerror(200107081);
  616. end;
  617. end;
  618. end;
  619. p.free;
  620. end;
  621. arraydef:
  622. begin
  623. { dynamic array nil }
  624. if is_dynamic_array(def) then
  625. begin
  626. { Only allow nil initialization }
  627. consume(_NIL);
  628. datalist.concat(Tai_const.Create_sym(nil));
  629. end
  630. { no packed array constants supported }
  631. else if is_packed_array(def) then
  632. begin
  633. Message(type_e_no_const_packed_array);
  634. consume_all_until(_RKLAMMER);
  635. end
  636. else
  637. if try_to_consume(_LKLAMMER) then
  638. begin
  639. for l:=tarraydef(def).lowrange to tarraydef(def).highrange-1 do
  640. begin
  641. readtypedconst(datalist,tarraydef(def).elementdef,nil,writable);
  642. consume(_COMMA);
  643. end;
  644. readtypedconst(datalist,tarraydef(def).elementdef,nil,writable);
  645. consume(_RKLAMMER);
  646. end
  647. else
  648. { if array of char then we allow also a string }
  649. if is_char(tarraydef(def).elementdef) then
  650. begin
  651. p:=comp_expr(true);
  652. if p.nodetype=stringconstn then
  653. begin
  654. len:=tstringconstnode(p).len;
  655. { For tp7 the maximum lentgh can be 255 }
  656. if (m_tp7 in current_settings.modeswitches) and
  657. (len>255) then
  658. len:=255;
  659. ca:=tstringconstnode(p).value_str;
  660. end
  661. else
  662. if is_constcharnode(p) then
  663. begin
  664. c:=chr(tordconstnode(p).value and $ff);
  665. ca:=@c;
  666. len:=1;
  667. end
  668. else
  669. begin
  670. Message(parser_e_illegal_expression);
  671. len:=0;
  672. end;
  673. if len>(tarraydef(def).highrange-tarraydef(def).lowrange+1) then
  674. Message(parser_e_string_larger_array);
  675. for i:=tarraydef(def).lowrange to tarraydef(def).highrange do
  676. begin
  677. if i+1-tarraydef(def).lowrange<=len then
  678. begin
  679. datalist.concat(Tai_const.Create_8bit(byte(ca^)));
  680. inc(ca);
  681. end
  682. else
  683. {Fill the remaining positions with #0.}
  684. datalist.concat(Tai_const.Create_8bit(0));
  685. end;
  686. p.free;
  687. end
  688. else
  689. begin
  690. { we want the ( }
  691. consume(_LKLAMMER);
  692. end;
  693. end;
  694. procvardef:
  695. begin
  696. { Procvars and pointers are no longer compatible. }
  697. { under tp: =nil or =var under fpc: =nil or =@var }
  698. if token=_NIL then
  699. begin
  700. datalist.concat(Tai_const.Create_sym(nil));
  701. if (po_methodpointer in tprocvardef(def).procoptions) then
  702. datalist.concat(Tai_const.Create_sym(nil));
  703. consume(_NIL);
  704. goto myexit;
  705. end;
  706. { you can't assign a value other than NIL to a typed constant }
  707. { which is a "procedure of object", because this also requires }
  708. { address of an object/class instance, which is not known at }
  709. { compile time (JM) }
  710. if (po_methodpointer in tprocvardef(def).procoptions) then
  711. Message(parser_e_no_procvarobj_const);
  712. { parse the rest too, so we can continue with error checking }
  713. getprocvardef:=tprocvardef(def);
  714. p:=comp_expr(true);
  715. getprocvardef:=nil;
  716. if codegenerror then
  717. begin
  718. p.free;
  719. goto myexit;
  720. end;
  721. { let type conversion check everything needed }
  722. inserttypeconv(p,def);
  723. if codegenerror then
  724. begin
  725. p.free;
  726. goto myexit;
  727. end;
  728. { remove typeconvs, that will normally insert a lea
  729. instruction which is not necessary for us }
  730. while p.nodetype=typeconvn do
  731. begin
  732. hp:=ttypeconvnode(p).left;
  733. ttypeconvnode(p).left:=nil;
  734. p.free;
  735. p:=hp;
  736. end;
  737. { remove addrn which we also don't need here }
  738. if p.nodetype=addrn then
  739. begin
  740. hp:=taddrnode(p).left;
  741. taddrnode(p).left:=nil;
  742. p.free;
  743. p:=hp;
  744. end;
  745. { we now need to have a loadn with a procsym }
  746. if (p.nodetype=loadn) and
  747. (tloadnode(p).symtableentry.typ=procsym) then
  748. begin
  749. datalist.concat(Tai_const.createname(
  750. tprocsym(tloadnode(p).symtableentry).first_procdef.mangledname,0));
  751. end
  752. else
  753. Message(parser_e_illegal_expression);
  754. p.free;
  755. end;
  756. { reads a typed constant record }
  757. recorddef:
  758. begin
  759. { packed record }
  760. if is_packed_record_or_object(def) then
  761. Message(type_e_no_const_packed_record)
  762. { KAZ }
  763. else if (trecorddef(def)=rec_tguid) and
  764. ((token=_CSTRING) or (token=_CCHAR) or (token=_ID)) then
  765. begin
  766. p:=comp_expr(true);
  767. inserttypeconv(p,cshortstringtype);
  768. if p.nodetype=stringconstn then
  769. begin
  770. s:=strpas(tstringconstnode(p).value_str);
  771. p.free;
  772. if string2guid(s,tmpguid) then
  773. begin
  774. datalist.concat(Tai_const.Create_32bit(longint(tmpguid.D1)));
  775. datalist.concat(Tai_const.Create_16bit(tmpguid.D2));
  776. datalist.concat(Tai_const.Create_16bit(tmpguid.D3));
  777. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  778. datalist.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  779. end
  780. else
  781. Message(parser_e_improper_guid_syntax);
  782. end
  783. else
  784. begin
  785. p.free;
  786. Message(parser_e_illegal_expression);
  787. goto myexit;
  788. end;
  789. end
  790. else
  791. begin
  792. consume(_LKLAMMER);
  793. sorg:='';
  794. aktpos:=0;
  795. srsym := tsym(trecorddef(def).symtable.symindex.first);
  796. recsym := nil;
  797. while token<>_RKLAMMER do
  798. begin
  799. s:=pattern;
  800. sorg:=orgpattern;
  801. consume(_ID);
  802. consume(_COLON);
  803. error := false;
  804. recsym := tsym(trecorddef(def).symtable.search(s));
  805. if not assigned(recsym) then
  806. begin
  807. Message1(sym_e_illegal_field,sorg);
  808. error := true;
  809. end;
  810. if (not error) and
  811. (not assigned(srsym) or
  812. (s <> srsym.name)) then
  813. { possible variant record (JM) }
  814. begin
  815. { All parts of a variant start at the same offset }
  816. { Also allow jumping from one variant part to another, }
  817. { as long as the offsets match }
  818. if (assigned(srsym) and
  819. (tfieldvarsym(recsym).fieldoffset = tfieldvarsym(srsym).fieldoffset)) or
  820. { srsym is not assigned after parsing w2 in the }
  821. { typed const in the next example: }
  822. { type tr = record case byte of }
  823. { 1: (l1,l2: dword); }
  824. { 2: (w1,w2: word); }
  825. { end; }
  826. { const r: tr = (w1:1;w2:1;l2:5); }
  827. (tfieldvarsym(recsym).fieldoffset = aktpos) then
  828. srsym := recsym
  829. { going backwards isn't allowed in any mode }
  830. else if (tfieldvarsym(recsym).fieldoffset<aktpos) then
  831. begin
  832. Message(parser_e_invalid_record_const);
  833. error := true;
  834. end
  835. { Delphi allows you to skip fields }
  836. else if (m_delphi in current_settings.modeswitches) then
  837. begin
  838. Message1(parser_w_skipped_fields_before,sorg);
  839. srsym := recsym;
  840. end
  841. { FPC and TP don't }
  842. else
  843. begin
  844. Message1(parser_e_skipped_fields_before,sorg);
  845. error := true;
  846. end;
  847. end;
  848. if error then
  849. consume_all_until(_SEMICOLON)
  850. else
  851. begin
  852. { if needed fill (alignment) }
  853. if tfieldvarsym(srsym).fieldoffset>aktpos then
  854. for i:=1 to tfieldvarsym(srsym).fieldoffset-aktpos do
  855. datalist.concat(Tai_const.Create_8bit(0));
  856. { new position }
  857. aktpos:=tfieldvarsym(srsym).fieldoffset+tfieldvarsym(srsym).vardef.size;
  858. { read the data }
  859. readtypedconst(datalist,tfieldvarsym(srsym).vardef,nil,writable);
  860. { keep previous field for checking whether whole }
  861. { record was initialized (JM) }
  862. recsym := srsym;
  863. { goto next field }
  864. srsym := tsym(srsym.indexnext);
  865. if token=_SEMICOLON then
  866. consume(_SEMICOLON)
  867. else break;
  868. end;
  869. end;
  870. { are there any fields left? }
  871. if assigned(srsym) and
  872. { don't complain if there only come other variant parts }
  873. { after the last initialized field }
  874. ((recsym=nil) or
  875. (tfieldvarsym(srsym).fieldoffset > tfieldvarsym(recsym).fieldoffset)) then
  876. Message1(parser_w_skipped_fields_after,sorg);
  877. for i:=1 to def.size-aktpos do
  878. datalist.concat(Tai_const.Create_8bit(0));
  879. consume(_RKLAMMER);
  880. end;
  881. end;
  882. { reads a typed object }
  883. objectdef:
  884. begin
  885. if is_class_or_interface(def) then
  886. begin
  887. p:=comp_expr(true);
  888. if p.nodetype<>niln then
  889. begin
  890. Message(type_e_no_const_packed_array);
  891. consume_all_until(_SEMICOLON);
  892. end
  893. else
  894. begin
  895. datalist.concat(Tai_const.Create_sym(nil));
  896. end;
  897. p.free;
  898. end
  899. { for objects we allow it only if it doesn't contain a vmt }
  900. else if (oo_has_vmt in tobjectdef(def).objectoptions) and
  901. (m_fpc in current_settings.modeswitches) then
  902. Message(parser_e_type_const_not_possible)
  903. { packed object }
  904. else if is_packed_record_or_object(def) then
  905. Message(type_e_no_const_packed_record)
  906. else
  907. begin
  908. consume(_LKLAMMER);
  909. aktpos:=0;
  910. while token<>_RKLAMMER do
  911. begin
  912. s:=pattern;
  913. sorg:=orgpattern;
  914. consume(_ID);
  915. consume(_COLON);
  916. srsym:=nil;
  917. obj:=tobjectdef(def);
  918. symt:=obj.symtable;
  919. while (srsym=nil) and assigned(symt) do
  920. begin
  921. srsym:=tsym(symt.search(s));
  922. if assigned(obj) then
  923. obj:=obj.childof;
  924. if assigned(obj) then
  925. symt:=obj.symtable
  926. else
  927. symt:=nil;
  928. end;
  929. if srsym=nil then
  930. begin
  931. Message1(sym_e_id_not_found,sorg);
  932. consume_all_until(_SEMICOLON);
  933. end
  934. else
  935. with tfieldvarsym(srsym) do
  936. begin
  937. { check position }
  938. if fieldoffset<aktpos then
  939. message(parser_e_invalid_record_const);
  940. { check in VMT needs to be added for TP mode }
  941. with Tobjectdef(def) do
  942. if not(m_fpc in current_settings.modeswitches) and
  943. (oo_has_vmt in objectoptions) and
  944. (vmt_offset<fieldoffset) then
  945. begin
  946. for i:=1 to vmt_offset-aktpos do
  947. datalist.concat(tai_const.create_8bit(0));
  948. datalist.concat(tai_const.createname(vmt_mangledname,0));
  949. { this is more general }
  950. aktpos:=vmt_offset + sizeof(aint);
  951. end;
  952. { if needed fill }
  953. if fieldoffset>aktpos then
  954. for i:=1 to fieldoffset-aktpos do
  955. datalist.concat(Tai_const.Create_8bit(0));
  956. { new position }
  957. aktpos:=fieldoffset+vardef.size;
  958. { read the data }
  959. readtypedconst(datalist,vardef,nil,writable);
  960. if not try_to_consume(_SEMICOLON) then
  961. break;
  962. end;
  963. end;
  964. if not(m_fpc in current_settings.modeswitches) and
  965. (oo_has_vmt in tobjectdef(def).objectoptions) and
  966. (tobjectdef(def).vmt_offset>=aktpos) then
  967. begin
  968. for i:=1 to tobjectdef(def).vmt_offset-aktpos do
  969. datalist.concat(tai_const.create_8bit(0));
  970. datalist.concat(tai_const.createname(tobjectdef(def).vmt_mangledname,0));
  971. { this is more general }
  972. aktpos:=tobjectdef(def).vmt_offset + sizeof(aint);
  973. end;
  974. for i:=1 to def.size-aktpos do
  975. datalist.concat(Tai_const.Create_8bit(0));
  976. consume(_RKLAMMER);
  977. end;
  978. end;
  979. errordef:
  980. begin
  981. { try to consume something useful }
  982. if token=_LKLAMMER then
  983. consume_all_until(_RKLAMMER)
  984. else
  985. consume_all_until(_SEMICOLON);
  986. end;
  987. else Message(parser_e_type_const_not_possible);
  988. end;
  989. { Parse hints and public directive }
  990. if assigned(sym) then
  991. begin
  992. try_consume_hintdirective(sym.symoptions);
  993. { Support public name directive }
  994. if try_to_consume(_PUBLIC) then
  995. begin
  996. if try_to_consume(_NAME) then
  997. C_name:=get_stringconst
  998. else
  999. C_name:=sorg;
  1000. sym.set_mangledname(C_Name);
  1001. end;
  1002. end;
  1003. myexit:
  1004. block_type:=old_block_type;
  1005. { Add symbol name if this is specified. For array
  1006. elements sym=nil and we should skip this }
  1007. if assigned(sym) then
  1008. begin
  1009. storefilepos:=current_filepos;
  1010. current_filepos:=sym.fileinfo;
  1011. { insert cut for smartlinking or alignment }
  1012. if writable then
  1013. cursectype:=sec_data
  1014. else
  1015. cursectype:=sec_rodata;
  1016. maybe_new_object_file(list);
  1017. new_section(list,cursectype,lower(sym.mangledname),const_align(def.alignment));
  1018. if (sym.owner.symtabletype=globalsymtable) or
  1019. maybe_smartlink_symbol or
  1020. (assigned(current_procinfo) and
  1021. (po_inline in current_procinfo.procdef.procoptions)) or
  1022. DLLSource then
  1023. list.concat(Tai_symbol.Createname_global(sym.mangledname,AT_DATA,0))
  1024. else
  1025. list.concat(Tai_symbol.Createname(sym.mangledname,AT_DATA,0));
  1026. list.concatlist(datalist);
  1027. list.concat(tai_symbol_end.Createname(sym.mangledname));
  1028. current_filepos:=storefilepos;
  1029. end
  1030. else
  1031. list.concatlist(datalist);
  1032. datalist.free;
  1033. end;
  1034. {$maxfpuregisters default}
  1035. end.