ptconst.pas 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  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:=tunarynode(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. globalvarsym :
  427. curconstSegment.concat(Tai_const.Createname(tglobalvarsym(srsym).mangledname,AT_DATA,offset));
  428. typedconstsym :
  429. curconstSegment.concat(Tai_const.Createname(ttypedconstsym(srsym).mangledname,AT_DATA,offset));
  430. labelsym :
  431. curconstSegment.concat(Tai_const.Createname(tlabelsym(srsym).mangledname,AT_FUNCTION,offset));
  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. if try_to_consume(_LKLAMMER) then
  697. begin
  698. for l:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange-1 do
  699. begin
  700. readtypedconst(tarraydef(t.def).elementtype,nil,writable);
  701. consume(_COMMA);
  702. end;
  703. readtypedconst(tarraydef(t.def).elementtype,nil,writable);
  704. consume(_RKLAMMER);
  705. end
  706. else
  707. { if array of char then we allow also a string }
  708. if is_char(tarraydef(t.def).elementtype.def) then
  709. begin
  710. p:=comp_expr(true);
  711. if p.nodetype=stringconstn then
  712. begin
  713. len:=tstringconstnode(p).len;
  714. { For tp7 the maximum lentgh can be 255 }
  715. if (m_tp7 in aktmodeswitches) and
  716. (len>255) then
  717. len:=255;
  718. ca:=tstringconstnode(p).value_str;
  719. end
  720. else
  721. if is_constcharnode(p) then
  722. begin
  723. c:=chr(tordconstnode(p).value and $ff);
  724. ca:=@c;
  725. len:=1;
  726. end
  727. else
  728. begin
  729. Message(parser_e_illegal_expression);
  730. len:=0;
  731. end;
  732. if len>(tarraydef(t.def).highrange-tarraydef(t.def).lowrange+1) then
  733. Message(parser_e_string_larger_array);
  734. for i:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange do
  735. begin
  736. if i+1-tarraydef(t.def).lowrange<=len then
  737. begin
  738. curconstSegment.concat(Tai_const.Create_8bit(byte(ca^)));
  739. inc(ca);
  740. end
  741. else
  742. {Fill the remaining positions with #0.}
  743. curconstSegment.concat(Tai_const.Create_8bit(0));
  744. end;
  745. p.free;
  746. end
  747. else
  748. { dynamic array nil }
  749. if is_dynamic_array(t.def) then
  750. begin
  751. { Only allow nil initialization }
  752. consume(_NIL);
  753. curconstSegment.concat(Tai_const.Create_sym(nil));
  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 typeconvn, that will normally insert a lea
  796. instruction which is not necessary for us }
  797. if p.nodetype=typeconvn then
  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(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.
  1057. {
  1058. $Log$
  1059. Revision 1.96 2004-11-09 17:26:47 peter
  1060. * fixed wrong typecasts
  1061. Revision 1.95 2004/11/08 22:09:59 peter
  1062. * tvarsym splitted
  1063. Revision 1.94 2004/11/01 23:30:11 peter
  1064. * support > 32bit accesses for x86_64
  1065. * rewrote array size checking to support 64bit
  1066. Revision 1.93 2004/11/01 15:32:12 peter
  1067. * support @labelsym
  1068. Revision 1.92 2004/10/15 09:14:17 mazen
  1069. - remove $IFDEF DELPHI and related code
  1070. - remove $IFDEF FPCPROCVAR and related code
  1071. Revision 1.91 2004/07/12 17:58:19 peter
  1072. * remove maxlen field from ansistring/widestrings
  1073. Revision 1.90 2004/07/03 14:06:35 daniel
  1074. * Compile fix
  1075. Revision 1.89 2004/06/20 20:41:47 florian
  1076. * fixed bootstrapping problems
  1077. Revision 1.88 2004/06/20 08:55:30 florian
  1078. * logs truncated
  1079. Revision 1.87 2004/06/18 15:16:46 peter
  1080. * remove obsolete cardinal() typecasts
  1081. Revision 1.86 2004/06/16 20:07:09 florian
  1082. * dwarf branch merged
  1083. Revision 1.85 2004/05/23 15:23:30 peter
  1084. * fixed qword(longint) that removed sign from the number
  1085. * removed code in the compiler that relied on wrong qword(longint)
  1086. code generation
  1087. Revision 1.84 2004/04/29 19:56:37 daniel
  1088. * Prepare compiler infrastructure for multiple ansistring types
  1089. Revision 1.83 2004/04/11 10:44:23 peter
  1090. * block_type is bt_const when parsing typed consts
  1091. }