ptconst.pas 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.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,cpuinfo,
  34. cutils,globals,widestr,scanner,
  35. symconst,symbase,symdef,aasm,cpuasm,types,verbose,
  36. { pass 1 }
  37. node,pass_1,
  38. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,
  39. { parser specific stuff }
  40. pbase,pexpr,
  41. { codegen }
  42. cgbase
  43. ;
  44. {$ifdef fpc}
  45. {$maxfpuregisters 0}
  46. {$endif fpc}
  47. { this procedure reads typed constants }
  48. procedure readtypedconst(const t:ttype;sym : ttypedconstsym;writable : boolean);
  49. var
  50. len,base : longint;
  51. p,hp,hpstart : tnode;
  52. i,j,l,offset,
  53. varalign,
  54. strlength : longint;
  55. curconstsegment : TAAsmoutput;
  56. ll : tasmlabel;
  57. s,sorg : string;
  58. c : char;
  59. ca : pchar;
  60. tmpguid : tguid;
  61. aktpos : longint;
  62. obj : tobjectdef;
  63. recsym,
  64. srsym : tsym;
  65. symt : tsymtable;
  66. value : bestreal;
  67. strval : pchar;
  68. pw : pcompilerwidestring;
  69. error : boolean;
  70. procedure check_range(def:torddef);
  71. begin
  72. if ((tordconstnode(p).value>def.high) or
  73. (tordconstnode(p).value<def.low)) then
  74. begin
  75. if (cs_check_range in aktlocalswitches) then
  76. Message(parser_e_range_check_error)
  77. else
  78. Message(parser_w_range_check_error);
  79. end;
  80. end;
  81. {$R-} {Range check creates problem with init_8bit(-1) !!}
  82. begin
  83. if writable then
  84. curconstsegment:=datasegment
  85. else
  86. curconstsegment:=consts;
  87. case t.def.deftype of
  88. orddef:
  89. begin
  90. p:=comp_expr(true);
  91. case torddef(t.def).typ of
  92. bool8bit :
  93. begin
  94. if is_constboolnode(p) then
  95. curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value))
  96. else
  97. Message(cg_e_illegal_expression);
  98. end;
  99. bool16bit :
  100. begin
  101. if is_constboolnode(p) then
  102. curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value))
  103. else
  104. Message(cg_e_illegal_expression);
  105. end;
  106. bool32bit :
  107. begin
  108. if is_constboolnode(p) then
  109. curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value))
  110. else
  111. Message(cg_e_illegal_expression);
  112. end;
  113. uchar :
  114. begin
  115. if is_constcharnode(p) then
  116. curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value))
  117. else
  118. Message(cg_e_illegal_expression);
  119. end;
  120. uwidechar :
  121. begin
  122. if is_constcharnode(p) then
  123. curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value))
  124. else
  125. Message(cg_e_illegal_expression);
  126. end;
  127. s8bit,
  128. u8bit :
  129. begin
  130. if is_constintnode(p) then
  131. begin
  132. curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value));
  133. check_range(torddef(t.def));
  134. end
  135. else
  136. Message(cg_e_illegal_expression);
  137. end;
  138. u16bit,
  139. s16bit :
  140. begin
  141. if is_constintnode(p) then
  142. begin
  143. curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value));
  144. check_range(torddef(t.def));
  145. end
  146. else
  147. Message(cg_e_illegal_expression);
  148. end;
  149. s32bit,
  150. u32bit :
  151. begin
  152. if is_constintnode(p) then
  153. begin
  154. curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value));
  155. if torddef(t.def).typ<>u32bit then
  156. check_range(torddef(t.def));
  157. end
  158. else
  159. Message(cg_e_illegal_expression);
  160. end;
  161. s64bit,
  162. u64bit:
  163. begin
  164. if is_constintnode(p) then
  165. begin
  166. if target_info.endian = endian_little then
  167. begin
  168. curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value));
  169. if (tordconstnode(p).value<0) and (torddef(t.def).typ = s64bit) then
  170. curconstSegment.concat(Tai_const.Create_32bit(-1))
  171. else
  172. curconstSegment.concat(Tai_const.Create_32bit(0));
  173. end
  174. else
  175. begin
  176. if (tordconstnode(p).value<0) and (torddef(t.def).typ = s64bit) then
  177. curconstSegment.concat(Tai_const.Create_32bit(-1))
  178. else
  179. curconstSegment.concat(Tai_const.Create_32bit(0));
  180. curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value));
  181. end;
  182. end
  183. else
  184. Message(cg_e_illegal_expression);
  185. end;
  186. else
  187. internalerror(3799);
  188. end;
  189. p.free;
  190. end;
  191. floatdef:
  192. begin
  193. p:=comp_expr(true);
  194. if is_constrealnode(p) then
  195. value:=trealconstnode(p).value_real
  196. else if is_constintnode(p) then
  197. value:=tordconstnode(p).value
  198. else
  199. Message(cg_e_illegal_expression);
  200. case tfloatdef(t.def).typ of
  201. s32real :
  202. curconstSegment.concat(Tai_real_32bit.Create(value));
  203. s64real :
  204. curconstSegment.concat(Tai_real_64bit.Create(value));
  205. s80real :
  206. curconstSegment.concat(Tai_real_80bit.Create(value));
  207. s64comp :
  208. curconstSegment.concat(Tai_comp_64bit.Create(value));
  209. else
  210. internalerror(18);
  211. end;
  212. p.free;
  213. end;
  214. classrefdef:
  215. begin
  216. p:=comp_expr(true);
  217. case p.nodetype of
  218. loadvmtn:
  219. begin
  220. if not(tobjectdef(tclassrefdef(p.resulttype.def).pointertype.def).is_related(
  221. tobjectdef(tclassrefdef(t.def).pointertype.def))) then
  222. Message(cg_e_illegal_expression);
  223. curconstSegment.concat(Tai_const_symbol.Create(newasmsymbol(tobjectdef(
  224. tclassrefdef(p.resulttype.def).pointertype.def).vmt_mangledname)));
  225. end;
  226. niln:
  227. curconstSegment.concat(Tai_const.Create_32bit(0));
  228. else Message(cg_e_illegal_expression);
  229. end;
  230. p.free;
  231. end;
  232. pointerdef:
  233. begin
  234. p:=comp_expr(true);
  235. if (p.nodetype=typeconvn) and
  236. (ttypeconvnode(p).left.nodetype in [addrn,niln]) and
  237. is_equal(t.def,p.resulttype.def) then
  238. begin
  239. hp:=ttypeconvnode(p).left;
  240. ttypeconvnode(p).left:=nil;
  241. p.free;
  242. p:=hp;
  243. end;
  244. { allows horrible ofs(typeof(TButton)^) code !! }
  245. if (p.nodetype=addrn) and
  246. (taddrnode(p).left.nodetype=derefn) then
  247. begin
  248. hp:=tderefnode(taddrnode(p).left).left;
  249. tderefnode(taddrnode(p).left).left:=nil;
  250. p.free;
  251. p:=hp;
  252. end;
  253. { const pointer ? }
  254. if (p.nodetype = pointerconstn) then
  255. curconstsegment.concat(Tai_const.Create_32bit(
  256. tpointerconstnode(p).value))
  257. { nil pointer ? }
  258. else if p.nodetype=niln then
  259. curconstSegment.concat(Tai_const.Create_32bit(0))
  260. { maybe pchar ? }
  261. else
  262. if is_char(tpointerdef(t.def).pointertype.def) and
  263. (p.nodetype<>addrn) then
  264. begin
  265. getdatalabel(ll);
  266. curconstSegment.concat(Tai_const_symbol.Create(ll));
  267. if p.nodetype=stringconstn then
  268. varalign:=size_2_align(tstringconstnode(p).len)
  269. else
  270. varalign:=0;
  271. varalign:=used_align(varalign,aktalignment.varalignmin,aktalignment.varalignmax);
  272. Consts.concat(Tai_align.Create(varalign));
  273. Consts.concat(Tai_label.Create(ll));
  274. if p.nodetype=stringconstn then
  275. begin
  276. len:=tstringconstnode(p).len;
  277. { For tp7 the maximum lentgh can be 255 }
  278. if (m_tp in aktmodeswitches) and
  279. (len>255) then
  280. len:=255;
  281. getmem(ca,len+2);
  282. move(tstringconstnode(p).value_str^,ca^,len+1);
  283. Consts.concat(Tai_string.Create_length_pchar(ca,len+1));
  284. end
  285. else
  286. if is_constcharnode(p) then
  287. Consts.concat(Tai_string.Create(char(byte(tordconstnode(p).value))+#0))
  288. else
  289. Message(cg_e_illegal_expression);
  290. end
  291. { maybe pwidechar ? }
  292. else
  293. if is_widechar(tpointerdef(t.def).pointertype.def) and
  294. (p.nodetype<>addrn) then
  295. begin
  296. getdatalabel(ll);
  297. curconstSegment.concat(Tai_const_symbol.Create(ll));
  298. Consts.concat(Tai_label.Create(ll));
  299. if (p.nodetype in [stringconstn,ordconstn]) then
  300. begin
  301. { convert to widestring stringconstn }
  302. inserttypeconv(p,cwidestringtype);
  303. if (p.nodetype=stringconstn) and
  304. (tstringconstnode(p).st_type=st_widestring) then
  305. begin
  306. pw:=pcompilerwidestring(tstringconstnode(p).value_str);
  307. for i:=0 to tstringconstnode(p).len-1 do
  308. Consts.concat(Tai_const.Create_16bit(pw^.data[i]));
  309. { ending #0 }
  310. Consts.concat(Tai_const.Create_16bit(0))
  311. end;
  312. end
  313. else
  314. Message(cg_e_illegal_expression);
  315. end
  316. else
  317. if p.nodetype=addrn then
  318. begin
  319. inserttypeconv(p,t);
  320. { if a typeconv node was inserted then check if it was an tc_equal. If
  321. true then we remove the node. If not tc_equal then we leave the typeconvn
  322. and the nodetype=loadn will always be false and generate the error (PFV) }
  323. if (p.nodetype=typeconvn) then
  324. begin
  325. if (ttypeconvnode(p).convtype=tc_equal) then
  326. hpstart:=taddrnode(ttypeconvnode(p).left).left
  327. else
  328. hpstart:=p;
  329. end
  330. else
  331. hpstart:=taddrnode(p).left;
  332. hp:=hpstart;
  333. while assigned(hp) and (hp.nodetype in [subscriptn,vecn]) do
  334. hp:=tbinarynode(hp).left;
  335. if (hp.nodetype=loadn) then
  336. begin
  337. hp:=hpstart;
  338. offset:=0;
  339. while assigned(hp) and (hp.nodetype<>loadn) do
  340. begin
  341. case hp.nodetype of
  342. vecn :
  343. begin
  344. case tvecnode(hp).left.resulttype.def.deftype of
  345. stringdef :
  346. begin
  347. { this seems OK for shortstring and ansistrings PM }
  348. { it is wrong for widestrings !! }
  349. len:=1;
  350. base:=0;
  351. end;
  352. arraydef :
  353. begin
  354. len:=tarraydef(tvecnode(hp).left.resulttype.def).elesize;
  355. base:=tarraydef(tvecnode(hp).left.resulttype.def).lowrange;
  356. end
  357. else
  358. Message(cg_e_illegal_expression);
  359. end;
  360. if is_constintnode(tvecnode(hp).right) then
  361. inc(offset,len*(get_ordinal_value(tvecnode(hp).right)-base))
  362. else
  363. Message(cg_e_illegal_expression);
  364. end;
  365. subscriptn :
  366. inc(offset,tsubscriptnode(hp).vs.address)
  367. else
  368. Message(cg_e_illegal_expression);
  369. end;
  370. hp:=tbinarynode(hp).left;
  371. end;
  372. if tloadnode(hp).symtableentry.typ=constsym then
  373. Message(type_e_variable_id_expected);
  374. curconstSegment.concat(Tai_const_symbol.Createname_offset(tloadnode(hp).symtableentry.mangledname,offset));
  375. end
  376. else
  377. Message(cg_e_illegal_expression);
  378. end
  379. else
  380. { allow typeof(Object type)}
  381. if (p.nodetype=inlinen) and
  382. (tinlinenode(p).inlinenumber=in_typeof_x) then
  383. begin
  384. if (tinlinenode(p).left.nodetype=typen) then
  385. begin
  386. curconstSegment.concat(Tai_const_symbol.createname(
  387. tobjectdef(tinlinenode(p).left.resulttype.def).vmt_mangledname));
  388. end
  389. else
  390. Message(cg_e_illegal_expression);
  391. end
  392. else
  393. Message(cg_e_illegal_expression);
  394. p.free;
  395. end;
  396. setdef:
  397. begin
  398. p:=comp_expr(true);
  399. if p.nodetype=setconstn then
  400. begin
  401. { be sure to convert to the correct result, else
  402. it can generate smallset data instead of normalset (PFV) }
  403. inserttypeconv(p,t);
  404. { we only allow const sets }
  405. if assigned(tsetconstnode(p).left) then
  406. Message(cg_e_illegal_expression)
  407. else
  408. begin
  409. { this writing is endian independant }
  410. { untrue - because they are considered }
  411. { arrays of 32-bit values CEC }
  412. if source_info.endian = target_info.endian then
  413. begin
  414. for l:= 0 to p.resulttype.def.size-1 do
  415. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[l]));
  416. end
  417. else
  418. begin
  419. { store as longint values in swaped format }
  420. j:=0;
  421. for l:=0 to ((p.resulttype.def.size-1) div 4) do
  422. begin
  423. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j+3]));
  424. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j+2]));
  425. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j+1]));
  426. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j]));
  427. Inc(j,4);
  428. end;
  429. end;
  430. end;
  431. end
  432. else
  433. Message(cg_e_illegal_expression);
  434. p.free;
  435. end;
  436. enumdef:
  437. begin
  438. p:=comp_expr(true);
  439. if p.nodetype=ordconstn then
  440. begin
  441. if is_equal(p.resulttype.def,t.def) or
  442. is_subequal(p.resulttype.def,t.def) then
  443. begin
  444. case p.resulttype.def.size of
  445. 1 : curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value));
  446. 2 : curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value));
  447. 4 : curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value));
  448. end;
  449. end
  450. else
  451. Message2(type_e_incompatible_types,t.def.typename,p.resulttype.def.typename);
  452. end
  453. else
  454. Message(cg_e_illegal_expression);
  455. p.free;
  456. end;
  457. stringdef:
  458. begin
  459. p:=comp_expr(true);
  460. { load strval and strlength of the constant tree }
  461. if p.nodetype=stringconstn then
  462. begin
  463. { convert to the expected string type so that
  464. for widestrings strval is a pcompilerwidestring }
  465. inserttypeconv(p,t);
  466. strlength:=tstringconstnode(p).len;
  467. strval:=tstringconstnode(p).value_str;
  468. end
  469. else if is_constcharnode(p) then
  470. begin
  471. { strval:=pchar(@tordconstnode(p).value);
  472. THIS FAIL on BIG_ENDIAN MACHINES PM }
  473. c:=chr(tordconstnode(p).value and $ff);
  474. strval:=@c;
  475. strlength:=1
  476. end
  477. else if is_constresourcestringnode(p) then
  478. begin
  479. strval:=pchar(tconstsym(tloadnode(p).symtableentry).valueptr);
  480. strlength:=tconstsym(tloadnode(p).symtableentry).len;
  481. end
  482. else
  483. begin
  484. Message(cg_e_illegal_expression);
  485. strlength:=-1;
  486. end;
  487. if strlength>=0 then
  488. begin
  489. case tstringdef(t.def).string_typ of
  490. st_shortstring:
  491. begin
  492. if strlength>=t.def.size then
  493. begin
  494. message2(parser_w_string_too_long,strpas(strval),tostr(t.def.size-1));
  495. strlength:=t.def.size-1;
  496. end;
  497. curconstSegment.concat(Tai_const.Create_8bit(strlength));
  498. { this can also handle longer strings }
  499. getmem(ca,strlength+1);
  500. move(strval^,ca^,strlength);
  501. ca[strlength]:=#0;
  502. curconstSegment.concat(Tai_string.Create_length_pchar(ca,strlength));
  503. { fillup with spaces if size is shorter }
  504. if t.def.size>strlength then
  505. begin
  506. getmem(ca,t.def.size-strlength);
  507. { def.size contains also the leading length, so we }
  508. { we have to subtract one }
  509. fillchar(ca[0],t.def.size-strlength-1,' ');
  510. ca[t.def.size-strlength-1]:=#0;
  511. { this can also handle longer strings }
  512. curconstSegment.concat(Tai_string.Create_length_pchar(ca,t.def.size-strlength-1));
  513. end;
  514. end;
  515. st_ansistring:
  516. begin
  517. { an empty ansi string is nil! }
  518. if (strlength=0) then
  519. curconstSegment.concat(Tai_const.Create_32bit(0))
  520. else
  521. begin
  522. getdatalabel(ll);
  523. curconstSegment.concat(Tai_const_symbol.Create(ll));
  524. { first write the maximum size }
  525. Consts.concat(Tai_const.Create_32bit(strlength));
  526. { second write the real length }
  527. Consts.concat(Tai_const.Create_32bit(strlength));
  528. { redondent with maxlength but who knows ... (PM) }
  529. { third write use count (set to -1 for safety ) }
  530. Consts.concat(Tai_const.Create_32bit(-1));
  531. Consts.concat(Tai_label.Create(ll));
  532. getmem(ca,strlength+2);
  533. move(strval^,ca^,strlength);
  534. { The terminating #0 to be stored in the .data section (JM) }
  535. ca[strlength]:=#0;
  536. { End of the PChar. The memory has to be allocated because in }
  537. { tai_string.done, there is a freemem(len+1) (JM) }
  538. ca[strlength+1]:=#0;
  539. Consts.concat(Tai_string.Create_length_pchar(ca,strlength+1));
  540. end;
  541. end;
  542. st_widestring:
  543. begin
  544. { an empty ansi string is nil! }
  545. if (strlength=0) then
  546. curconstSegment.concat(Tai_const.Create_32bit(0))
  547. else
  548. begin
  549. getdatalabel(ll);
  550. curconstSegment.concat(Tai_const_symbol.Create(ll));
  551. Consts.concat(Tai_const.Create_32bit(strlength));
  552. Consts.concat(Tai_const.Create_32bit(strlength));
  553. Consts.concat(Tai_const.Create_32bit(-1));
  554. Consts.concat(Tai_label.Create(ll));
  555. for i:=0 to strlength-1 do
  556. Consts.concat(Tai_const.Create_16bit(pcompilerwidestring(strval)^.data[i]));
  557. { ending #0 }
  558. Consts.concat(Tai_const.Create_16bit(0))
  559. end;
  560. end;
  561. st_longstring:
  562. begin
  563. internalerror(200107081);
  564. {curconstSegment.concat(Tai_const.Create_32bit(strlength))));
  565. curconstSegment.concat(Tai_const.Create_8bit(0));
  566. getmem(ca,strlength+1);
  567. move(strval^,ca^,strlength);
  568. ca[strlength]:=#0;
  569. generate_pascii(consts,ca,strlength);
  570. curconstSegment.concat(Tai_const.Create_8bit(0));}
  571. end;
  572. end;
  573. end;
  574. p.free;
  575. end;
  576. arraydef:
  577. begin
  578. if token=_LKLAMMER then
  579. begin
  580. consume(_LKLAMMER);
  581. for l:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange-1 do
  582. begin
  583. readtypedconst(tarraydef(t.def).elementtype,nil,writable);
  584. consume(_COMMA);
  585. end;
  586. readtypedconst(tarraydef(t.def).elementtype,nil,writable);
  587. consume(_RKLAMMER);
  588. end
  589. else
  590. { if array of char then we allow also a string }
  591. if is_char(tarraydef(t.def).elementtype.def) then
  592. begin
  593. p:=comp_expr(true);
  594. if p.nodetype=stringconstn then
  595. begin
  596. len:=tstringconstnode(p).len;
  597. { For tp7 the maximum lentgh can be 255 }
  598. if (m_tp in aktmodeswitches) and
  599. (len>255) then
  600. len:=255;
  601. ca:=tstringconstnode(p).value_str;
  602. end
  603. else
  604. if is_constcharnode(p) then
  605. begin
  606. c:=chr(tordconstnode(p).value and $ff);
  607. ca:=@c;
  608. len:=1;
  609. end
  610. else
  611. begin
  612. Message(cg_e_illegal_expression);
  613. len:=0;
  614. end;
  615. if len>(tarraydef(t.def).highrange-tarraydef(t.def).lowrange+1) then
  616. Message(parser_e_string_larger_array);
  617. for i:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange do
  618. begin
  619. if i+1-tarraydef(t.def).lowrange<=len then
  620. begin
  621. curconstSegment.concat(Tai_const.Create_8bit(byte(ca^)));
  622. inc(ca);
  623. end
  624. else
  625. {Fill the remaining positions with #0.}
  626. curconstSegment.concat(Tai_const.Create_8bit(0));
  627. end;
  628. p.free;
  629. end
  630. else
  631. begin
  632. { we want the ( }
  633. consume(_LKLAMMER);
  634. end;
  635. end;
  636. procvardef:
  637. begin
  638. { Procvars and pointers are no longer compatible. }
  639. { under tp: =nil or =var under fpc: =nil or =@var }
  640. if token=_NIL then
  641. begin
  642. curconstSegment.concat(Tai_const.Create_32bit(0));
  643. if (po_methodpointer in tprocvardef(t.def).procoptions) then
  644. curconstSegment.concat(Tai_const.Create_32bit(0));
  645. consume(_NIL);
  646. exit;
  647. end;
  648. { you can't assign a value other than NIL to a typed constant }
  649. { which is a "procedure of object", because this also requires }
  650. { address of an object/class instance, which is not known at }
  651. { compile time (JM) }
  652. if (po_methodpointer in tprocvardef(t.def).procoptions) then
  653. Message(parser_e_no_procvarobj_const);
  654. { parse the rest too, so we can continue with error checking }
  655. getprocvar:=true;
  656. getprocvardef:=tprocvardef(t.def);
  657. p:=comp_expr(true);
  658. getprocvar:=false;
  659. if codegenerror then
  660. begin
  661. p.free;
  662. exit;
  663. end;
  664. { let type conversion check everything needed }
  665. inserttypeconv(p,t);
  666. if codegenerror then
  667. begin
  668. p.free;
  669. exit;
  670. end;
  671. { remove typeconvn, that will normally insert a lea
  672. instruction which is not necessary for us }
  673. if p.nodetype=typeconvn then
  674. begin
  675. hp:=ttypeconvnode(p).left;
  676. ttypeconvnode(p).left:=nil;
  677. p.free;
  678. p:=hp;
  679. end;
  680. { remove addrn which we also don't need here }
  681. if p.nodetype=addrn then
  682. begin
  683. hp:=taddrnode(p).left;
  684. taddrnode(p).left:=nil;
  685. p.free;
  686. p:=hp;
  687. end;
  688. { we now need to have a loadn with a procsym }
  689. if (p.nodetype=loadn) and
  690. (tloadnode(p).symtableentry.typ=procsym) then
  691. begin
  692. curconstSegment.concat(Tai_const_symbol.createname(
  693. tprocsym(tloadnode(p).symtableentry).definition.mangledname));
  694. end
  695. else
  696. Message(cg_e_illegal_expression);
  697. p.free;
  698. end;
  699. { reads a typed constant record }
  700. recorddef:
  701. begin
  702. { KAZ }
  703. if (trecorddef(t.def)=rec_tguid) and
  704. ((token=_CSTRING) or (token=_CCHAR) or (token=_ID)) then
  705. begin
  706. p:=comp_expr(true);
  707. inserttypeconv(p,cshortstringtype);
  708. if p.nodetype=stringconstn then
  709. begin
  710. s:=strpas(tstringconstnode(p).value_str);
  711. p.free;
  712. if string2guid(s,tmpguid) then
  713. begin
  714. curconstSegment.concat(Tai_const.Create_32bit(tmpguid.D1));
  715. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D2));
  716. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D3));
  717. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  718. curconstSegment.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  719. end
  720. else
  721. Message(parser_e_improper_guid_syntax);
  722. end
  723. else
  724. begin
  725. p.free;
  726. Message(cg_e_illegal_expression);
  727. exit;
  728. end;
  729. end
  730. else
  731. begin
  732. consume(_LKLAMMER);
  733. aktpos:=0;
  734. srsym := tsym(trecorddef(t.def).symtable.symindex.first);
  735. recsym := nil;
  736. while token<>_RKLAMMER do
  737. begin
  738. s:=pattern;
  739. sorg:=orgpattern;
  740. consume(_ID);
  741. consume(_COLON);
  742. error := false;
  743. recsym := tsym(trecorddef(t.def).symtable.search(s));
  744. if not assigned(recsym) then
  745. begin
  746. Message1(sym_e_illegal_field,s);
  747. error := true;
  748. end;
  749. if not assigned(srsym) or
  750. (s <> srsym.name) then
  751. { possible variant record (JM) }
  752. begin
  753. { All parts of a variant start at the same offset }
  754. { Also allow jumping from one variant part to another, }
  755. { as long as the offsets match }
  756. if (assigned(srsym) and
  757. (tvarsym(recsym).address = tvarsym(srsym).address)) or
  758. { srsym is not assigned after parsing w2 in the }
  759. { typed const in the next example: }
  760. { type tr = record case byte of }
  761. { 1: (l1,l2: dword); }
  762. { 2: (w1,w2: word); }
  763. { end; }
  764. { const r: tr = (w1:1;w2:1;l2:5); }
  765. (tvarsym(recsym).address = aktpos) then
  766. srsym := recsym
  767. { going backwards isn't allowed in any mode }
  768. else if (tvarsym(recsym).address<aktpos) then
  769. begin
  770. Message(parser_e_invalid_record_const);
  771. error := true;
  772. end
  773. { Delphi allows you to skip fields }
  774. else if (m_delphi in aktmodeswitches) then
  775. begin
  776. Message1(parser_w_skipped_fields_before,sorg);
  777. srsym := recsym;
  778. end
  779. { FPC and TP don't }
  780. else
  781. begin
  782. Message1(parser_e_skipped_fields_before,sorg);
  783. error := true;
  784. end;
  785. end;
  786. if error then
  787. consume_all_until(_SEMICOLON)
  788. else
  789. begin
  790. { if needed fill (alignment) }
  791. if tvarsym(srsym).address>aktpos then
  792. for i:=1 to tvarsym(srsym).address-aktpos do
  793. curconstSegment.concat(Tai_const.Create_8bit(0));
  794. { new position }
  795. aktpos:=tvarsym(srsym).address+tvarsym(srsym).vartype.def.size;
  796. { read the data }
  797. readtypedconst(tvarsym(srsym).vartype,nil,writable);
  798. { keep previous field for checking whether whole }
  799. { record was initialized (JM) }
  800. recsym := srsym;
  801. { goto next field }
  802. srsym := tsym(srsym.indexnext);
  803. if token=_SEMICOLON then
  804. consume(_SEMICOLON)
  805. else break;
  806. end;
  807. end;
  808. { are there any fields left? }
  809. if assigned(srsym) and
  810. { don't complain if there only come other variant parts }
  811. { after the last initialized field }
  812. ((recsym=nil) or
  813. (tvarsym(srsym).address > tvarsym(recsym).address)) then
  814. Message1(parser_h_skipped_fields_after,s);
  815. for i:=1 to t.def.size-aktpos do
  816. curconstSegment.concat(Tai_const.Create_8bit(0));
  817. consume(_RKLAMMER);
  818. end;
  819. end;
  820. { reads a typed object }
  821. objectdef:
  822. begin
  823. if is_class_or_interface(t.def) then
  824. begin
  825. p:=comp_expr(true);
  826. if p.nodetype<>niln then
  827. begin
  828. Message(parser_e_type_const_not_possible);
  829. consume_all_until(_RKLAMMER);
  830. end
  831. else
  832. begin
  833. curconstSegment.concat(Tai_const.Create_32bit(0));
  834. end;
  835. p.free;
  836. end
  837. { for objects we allow it only if it doesn't contain a vmt }
  838. else if (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  839. not(m_tp in aktmodeswitches) then
  840. Message(parser_e_type_const_not_possible)
  841. else
  842. begin
  843. consume(_LKLAMMER);
  844. aktpos:=0;
  845. while token<>_RKLAMMER do
  846. begin
  847. s:=pattern;
  848. sorg:=orgpattern;
  849. consume(_ID);
  850. consume(_COLON);
  851. srsym:=nil;
  852. obj:=tobjectdef(t.def);
  853. symt:=obj.symtable;
  854. while (srsym=nil) and assigned(symt) do
  855. begin
  856. srsym:=tsym(symt.search(s));
  857. if assigned(obj) then
  858. obj:=obj.childof;
  859. if assigned(obj) then
  860. symt:=obj.symtable
  861. else
  862. symt:=nil;
  863. end;
  864. if srsym=nil then
  865. begin
  866. Message1(sym_e_id_not_found,sorg);
  867. consume_all_until(_SEMICOLON);
  868. end
  869. else
  870. begin
  871. { check position }
  872. if tvarsym(srsym).address<aktpos then
  873. Message(parser_e_invalid_record_const);
  874. { check in VMT needs to be added for TP mode }
  875. if (m_tp in aktmodeswitches) and
  876. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  877. (tobjectdef(t.def).vmt_offset<tvarsym(srsym).address) then
  878. begin
  879. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  880. curconstsegment.concat(tai_const.create_8bit(0));
  881. curconstsegment.concat(tai_const_symbol.createname(tobjectdef(t.def).vmt_mangledname));
  882. { this is more general }
  883. aktpos:=tobjectdef(t.def).vmt_offset + target_info.size_of_pointer;
  884. end;
  885. { if needed fill }
  886. if tvarsym(srsym).address>aktpos then
  887. for i:=1 to tvarsym(srsym).address-aktpos do
  888. curconstSegment.concat(Tai_const.Create_8bit(0));
  889. { new position }
  890. aktpos:=tvarsym(srsym).address+tvarsym(srsym).vartype.def.size;
  891. { read the data }
  892. readtypedconst(tvarsym(srsym).vartype,nil,writable);
  893. if token=_SEMICOLON then
  894. consume(_SEMICOLON)
  895. else break;
  896. end;
  897. end;
  898. if (m_tp in aktmodeswitches) and
  899. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  900. (tobjectdef(t.def).vmt_offset>=aktpos) then
  901. begin
  902. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  903. curconstsegment.concat(tai_const.create_8bit(0));
  904. curconstsegment.concat(tai_const_symbol.createname(tobjectdef(t.def).vmt_mangledname));
  905. { this is more general }
  906. aktpos:=tobjectdef(t.def).vmt_offset + target_info.size_of_pointer;
  907. end;
  908. for i:=1 to t.def.size-aktpos do
  909. curconstSegment.concat(Tai_const.Create_8bit(0));
  910. consume(_RKLAMMER);
  911. end;
  912. end;
  913. errordef:
  914. begin
  915. { try to consume something useful }
  916. if token=_LKLAMMER then
  917. consume_all_until(_RKLAMMER)
  918. else
  919. consume_all_until(_SEMICOLON);
  920. end;
  921. else Message(parser_e_type_const_not_possible);
  922. end;
  923. end;
  924. {$ifdef fpc}
  925. {$maxfpuregisters default}
  926. {$endif fpc}
  927. end.
  928. {
  929. $Log$
  930. Revision 1.37 2001-10-29 14:59:48 jonas
  931. * typed constants that are "procedure of object" and which are assigned
  932. nil require 8 bytes of "0" (not 4)
  933. * fixed web bug 1655 (reject the code)
  934. Revision 1.36 2001/10/20 20:30:21 peter
  935. * read only typed const support, switch $J-
  936. Revision 1.35 2001/10/20 17:24:26 peter
  937. * make all sets equal when reading an array of sets. Before it could
  938. mix normal and small sets in the same array!
  939. Revision 1.34 2001/09/19 11:06:03 michael
  940. * realname updated for some hints
  941. * realname used for consts,labels
  942. Revision 1.33 2001/09/17 21:29:12 peter
  943. * merged netbsd, fpu-overflow from fixes branch
  944. Revision 1.32 2001/09/02 21:18:28 peter
  945. * split constsym.value in valueord,valueordptr,valueptr. The valueordptr
  946. is used for holding target platform pointer values. As those can be
  947. bigger than the source platform.
  948. Revision 1.31 2001/08/26 13:36:47 florian
  949. * some cg reorganisation
  950. * some PPC updates
  951. Revision 1.30 2001/08/01 21:46:41 peter
  952. * support pwidechar in typed const
  953. Revision 1.29 2001/07/30 21:39:26 peter
  954. * declare fpu in rtl for m68k linux
  955. Revision 1.28 2001/07/30 20:59:27 peter
  956. * m68k updates from v10 merged
  957. Revision 1.27 2001/07/08 21:00:15 peter
  958. * various widestring updates, it works now mostly without charset
  959. mapping supported
  960. Revision 1.26 2001/06/29 14:16:57 jonas
  961. * fixed inconsistent handling of procvars in FPC mode (sometimes @ was
  962. required to assign the address of a procedure to a procvar, sometimes
  963. not. Now it is always required) (merged)
  964. Revision 1.25 2001/06/27 21:37:36 peter
  965. * v10 merges
  966. Revision 1.24 2001/06/18 20:36:25 peter
  967. * -Ur switch (merged)
  968. * masm fixes (merged)
  969. * quoted filenames for go32v2 and win32
  970. Revision 1.23 2001/05/06 17:15:00 jonas
  971. + detect incomplete typed constant records
  972. Revision 1.22 2001/04/18 22:01:57 peter
  973. * registration of targets and assemblers
  974. Revision 1.21 2001/04/13 01:22:13 peter
  975. * symtable change to classes
  976. * range check generation and errors fixed, make cycle DEBUG=1 works
  977. * memory leaks fixed
  978. Revision 1.20 2001/04/04 22:43:53 peter
  979. * remove unnecessary calls to firstpass
  980. Revision 1.19 2001/04/02 21:20:34 peter
  981. * resulttype rewrite
  982. Revision 1.18 2001/03/11 22:58:50 peter
  983. * getsym redesign, removed the globals srsym,srsymtable
  984. Revision 1.17 2001/02/04 11:12:16 jonas
  985. * fixed web bug 1377 & const pointer arithmtic
  986. Revision 1.16 2001/02/03 00:26:35 peter
  987. * merged fix for bug 1365
  988. Revision 1.15 2000/12/25 00:07:28 peter
  989. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  990. tlinkedlist objects)
  991. Revision 1.14 2000/12/10 20:24:18 peter
  992. * allow subtypes for enums
  993. Revision 1.13 2000/11/29 00:30:38 florian
  994. * unused units removed from uses clause
  995. * some changes for widestrings
  996. Revision 1.12 2000/11/06 15:54:15 florian
  997. * fixed two bugs to get make cycle work, but it's not enough
  998. Revision 1.11 2000/11/04 14:25:21 florian
  999. + merged Attila's changes for interfaces, not tested yet
  1000. Revision 1.10 2000/10/31 22:02:51 peter
  1001. * symtable splitted, no real code changes
  1002. Revision 1.9 2000/10/14 10:14:52 peter
  1003. * moehrendorf oct 2000 rewrite
  1004. Revision 1.8 2000/09/30 13:23:04 peter
  1005. * const array of char and pchar length fixed (merged)
  1006. Revision 1.7 2000/09/24 15:06:25 peter
  1007. * use defines.inc
  1008. Revision 1.6 2000/08/27 16:11:52 peter
  1009. * moved some util functions from globals,cobjects to cutils
  1010. * splitted files into finput,fmodule
  1011. Revision 1.5 2000/08/24 19:13:18 peter
  1012. * allow nil for class typed consts (merged)
  1013. Revision 1.4 2000/08/16 13:06:06 florian
  1014. + support of 64 bit integer constants
  1015. Revision 1.3 2000/08/05 13:25:06 peter
  1016. * packenum 1 fixes (merged)
  1017. Revision 1.2 2000/07/13 11:32:47 michael
  1018. + removed logs
  1019. }