ptconst.pas 47 KB

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