ptconst.pas 49 KB

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