2
0

ptconst.pas 50 KB

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