ptconst.pas 50 KB

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