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