ptconst.pas 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. {
  2. $Id$
  3. Copyright (c) 1998-2001 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. srsym:=tloadnode(hp).symtableentry;
  373. case srsym.typ of
  374. procsym :
  375. begin
  376. if assigned(tprocsym(srsym).defs^.next) then
  377. Message(parser_e_no_overloaded_procvars);
  378. curconstSegment.concat(Tai_const_symbol.Createname_offset(tprocsym(srsym).defs^.def.mangledname,offset));
  379. end;
  380. varsym :
  381. curconstSegment.concat(Tai_const_symbol.Createname_offset(tvarsym(srsym).mangledname,offset));
  382. typedconstsym :
  383. curconstSegment.concat(Tai_const_symbol.Createname_offset(ttypedconstsym(srsym).mangledname,offset));
  384. else
  385. Message(type_e_variable_id_expected);
  386. end;
  387. end
  388. else
  389. Message(cg_e_illegal_expression);
  390. end
  391. else
  392. { allow typeof(Object type)}
  393. if (p.nodetype=inlinen) and
  394. (tinlinenode(p).inlinenumber=in_typeof_x) then
  395. begin
  396. if (tinlinenode(p).left.nodetype=typen) then
  397. begin
  398. curconstSegment.concat(Tai_const_symbol.createname(
  399. tobjectdef(tinlinenode(p).left.resulttype.def).vmt_mangledname));
  400. end
  401. else
  402. Message(cg_e_illegal_expression);
  403. end
  404. else
  405. Message(cg_e_illegal_expression);
  406. p.free;
  407. end;
  408. setdef:
  409. begin
  410. p:=comp_expr(true);
  411. if p.nodetype=setconstn then
  412. begin
  413. { be sure to convert to the correct result, else
  414. it can generate smallset data instead of normalset (PFV) }
  415. inserttypeconv(p,t);
  416. { we only allow const sets }
  417. if assigned(tsetconstnode(p).left) then
  418. Message(cg_e_illegal_expression)
  419. else
  420. begin
  421. { this writing is endian independant }
  422. { untrue - because they are considered }
  423. { arrays of 32-bit values CEC }
  424. if source_info.endian = target_info.endian then
  425. begin
  426. for l:= 0 to p.resulttype.def.size-1 do
  427. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[l]));
  428. end
  429. else
  430. begin
  431. { store as longint values in swaped format }
  432. j:=0;
  433. for l:=0 to ((p.resulttype.def.size-1) div 4) do
  434. begin
  435. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j+3]));
  436. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j+2]));
  437. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j+1]));
  438. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j]));
  439. Inc(j,4);
  440. end;
  441. end;
  442. end;
  443. end
  444. else
  445. Message(cg_e_illegal_expression);
  446. p.free;
  447. end;
  448. enumdef:
  449. begin
  450. p:=comp_expr(true);
  451. if p.nodetype=ordconstn then
  452. begin
  453. if is_equal(p.resulttype.def,t.def) or
  454. is_subequal(p.resulttype.def,t.def) then
  455. begin
  456. case p.resulttype.def.size of
  457. 1 : curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value));
  458. 2 : curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value));
  459. 4 : curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value));
  460. end;
  461. end
  462. else
  463. Message2(type_e_incompatible_types,t.def.typename,p.resulttype.def.typename);
  464. end
  465. else
  466. Message(cg_e_illegal_expression);
  467. p.free;
  468. end;
  469. stringdef:
  470. begin
  471. p:=comp_expr(true);
  472. { load strval and strlength of the constant tree }
  473. if p.nodetype=stringconstn then
  474. begin
  475. { convert to the expected string type so that
  476. for widestrings strval is a pcompilerwidestring }
  477. inserttypeconv(p,t);
  478. strlength:=tstringconstnode(p).len;
  479. strval:=tstringconstnode(p).value_str;
  480. end
  481. else if is_constcharnode(p) then
  482. begin
  483. { strval:=pchar(@tordconstnode(p).value);
  484. THIS FAIL on BIG_ENDIAN MACHINES PM }
  485. c:=chr(tordconstnode(p).value and $ff);
  486. strval:=@c;
  487. strlength:=1
  488. end
  489. else if is_constresourcestringnode(p) then
  490. begin
  491. strval:=pchar(tconstsym(tloadnode(p).symtableentry).valueptr);
  492. strlength:=tconstsym(tloadnode(p).symtableentry).len;
  493. end
  494. else
  495. begin
  496. Message(cg_e_illegal_expression);
  497. strlength:=-1;
  498. end;
  499. if strlength>=0 then
  500. begin
  501. case tstringdef(t.def).string_typ of
  502. st_shortstring:
  503. begin
  504. if strlength>=t.def.size then
  505. begin
  506. message2(parser_w_string_too_long,strpas(strval),tostr(t.def.size-1));
  507. strlength:=t.def.size-1;
  508. end;
  509. curconstSegment.concat(Tai_const.Create_8bit(strlength));
  510. { this can also handle longer strings }
  511. getmem(ca,strlength+1);
  512. move(strval^,ca^,strlength);
  513. ca[strlength]:=#0;
  514. curconstSegment.concat(Tai_string.Create_length_pchar(ca,strlength));
  515. { fillup with spaces if size is shorter }
  516. if t.def.size>strlength then
  517. begin
  518. getmem(ca,t.def.size-strlength);
  519. { def.size contains also the leading length, so we }
  520. { we have to subtract one }
  521. fillchar(ca[0],t.def.size-strlength-1,' ');
  522. ca[t.def.size-strlength-1]:=#0;
  523. { this can also handle longer strings }
  524. curconstSegment.concat(Tai_string.Create_length_pchar(ca,t.def.size-strlength-1));
  525. end;
  526. end;
  527. st_ansistring:
  528. begin
  529. { an empty ansi string is nil! }
  530. if (strlength=0) then
  531. curconstSegment.concat(Tai_const.Create_32bit(0))
  532. else
  533. begin
  534. getdatalabel(ll);
  535. curconstSegment.concat(Tai_const_symbol.Create(ll));
  536. { first write the maximum size }
  537. Consts.concat(Tai_const.Create_32bit(strlength));
  538. { second write the real length }
  539. Consts.concat(Tai_const.Create_32bit(strlength));
  540. { redondent with maxlength but who knows ... (PM) }
  541. { third write use count (set to -1 for safety ) }
  542. Consts.concat(Tai_const.Create_32bit(-1));
  543. Consts.concat(Tai_label.Create(ll));
  544. getmem(ca,strlength+2);
  545. move(strval^,ca^,strlength);
  546. { The terminating #0 to be stored in the .data section (JM) }
  547. ca[strlength]:=#0;
  548. { End of the PChar. The memory has to be allocated because in }
  549. { tai_string.done, there is a freemem(len+1) (JM) }
  550. ca[strlength+1]:=#0;
  551. Consts.concat(Tai_string.Create_length_pchar(ca,strlength+1));
  552. end;
  553. end;
  554. st_widestring:
  555. begin
  556. { an empty ansi string is nil! }
  557. if (strlength=0) then
  558. curconstSegment.concat(Tai_const.Create_32bit(0))
  559. else
  560. begin
  561. getdatalabel(ll);
  562. curconstSegment.concat(Tai_const_symbol.Create(ll));
  563. Consts.concat(Tai_const.Create_32bit(strlength));
  564. Consts.concat(Tai_const.Create_32bit(strlength));
  565. Consts.concat(Tai_const.Create_32bit(-1));
  566. Consts.concat(Tai_label.Create(ll));
  567. for i:=0 to strlength-1 do
  568. Consts.concat(Tai_const.Create_16bit(pcompilerwidestring(strval)^.data[i]));
  569. { ending #0 }
  570. Consts.concat(Tai_const.Create_16bit(0))
  571. end;
  572. end;
  573. st_longstring:
  574. begin
  575. internalerror(200107081);
  576. {curconstSegment.concat(Tai_const.Create_32bit(strlength))));
  577. curconstSegment.concat(Tai_const.Create_8bit(0));
  578. getmem(ca,strlength+1);
  579. move(strval^,ca^,strlength);
  580. ca[strlength]:=#0;
  581. generate_pascii(consts,ca,strlength);
  582. curconstSegment.concat(Tai_const.Create_8bit(0));}
  583. end;
  584. end;
  585. end;
  586. p.free;
  587. end;
  588. arraydef:
  589. begin
  590. if token=_LKLAMMER then
  591. begin
  592. consume(_LKLAMMER);
  593. for l:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange-1 do
  594. begin
  595. readtypedconst(tarraydef(t.def).elementtype,nil,writable);
  596. consume(_COMMA);
  597. end;
  598. readtypedconst(tarraydef(t.def).elementtype,nil,writable);
  599. consume(_RKLAMMER);
  600. end
  601. else
  602. { if array of char then we allow also a string }
  603. if is_char(tarraydef(t.def).elementtype.def) then
  604. begin
  605. p:=comp_expr(true);
  606. if p.nodetype=stringconstn then
  607. begin
  608. len:=tstringconstnode(p).len;
  609. { For tp7 the maximum lentgh can be 255 }
  610. if (m_tp in aktmodeswitches) and
  611. (len>255) then
  612. len:=255;
  613. ca:=tstringconstnode(p).value_str;
  614. end
  615. else
  616. if is_constcharnode(p) then
  617. begin
  618. c:=chr(tordconstnode(p).value and $ff);
  619. ca:=@c;
  620. len:=1;
  621. end
  622. else
  623. begin
  624. Message(cg_e_illegal_expression);
  625. len:=0;
  626. end;
  627. if len>(tarraydef(t.def).highrange-tarraydef(t.def).lowrange+1) then
  628. Message(parser_e_string_larger_array);
  629. for i:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange do
  630. begin
  631. if i+1-tarraydef(t.def).lowrange<=len then
  632. begin
  633. curconstSegment.concat(Tai_const.Create_8bit(byte(ca^)));
  634. inc(ca);
  635. end
  636. else
  637. {Fill the remaining positions with #0.}
  638. curconstSegment.concat(Tai_const.Create_8bit(0));
  639. end;
  640. p.free;
  641. end
  642. else
  643. begin
  644. { we want the ( }
  645. consume(_LKLAMMER);
  646. end;
  647. end;
  648. procvardef:
  649. begin
  650. { Procvars and pointers are no longer compatible. }
  651. { under tp: =nil or =var under fpc: =nil or =@var }
  652. if token=_NIL then
  653. begin
  654. curconstSegment.concat(Tai_const.Create_32bit(0));
  655. if (po_methodpointer in tprocvardef(t.def).procoptions) then
  656. curconstSegment.concat(Tai_const.Create_32bit(0));
  657. consume(_NIL);
  658. exit;
  659. end;
  660. { you can't assign a value other than NIL to a typed constant }
  661. { which is a "procedure of object", because this also requires }
  662. { address of an object/class instance, which is not known at }
  663. { compile time (JM) }
  664. if (po_methodpointer in tprocvardef(t.def).procoptions) then
  665. Message(parser_e_no_procvarobj_const);
  666. { parse the rest too, so we can continue with error checking }
  667. getprocvar:=true;
  668. getprocvardef:=tprocvardef(t.def);
  669. p:=comp_expr(true);
  670. getprocvar:=false;
  671. if codegenerror then
  672. begin
  673. p.free;
  674. exit;
  675. end;
  676. { let type conversion check everything needed }
  677. inserttypeconv(p,t);
  678. if codegenerror then
  679. begin
  680. p.free;
  681. exit;
  682. end;
  683. { remove typeconvn, that will normally insert a lea
  684. instruction which is not necessary for us }
  685. if p.nodetype=typeconvn then
  686. begin
  687. hp:=ttypeconvnode(p).left;
  688. ttypeconvnode(p).left:=nil;
  689. p.free;
  690. p:=hp;
  691. end;
  692. { remove addrn which we also don't need here }
  693. if p.nodetype=addrn then
  694. begin
  695. hp:=taddrnode(p).left;
  696. taddrnode(p).left:=nil;
  697. p.free;
  698. p:=hp;
  699. end;
  700. { we now need to have a loadn with a procsym }
  701. if (p.nodetype=loadn) and
  702. (tloadnode(p).symtableentry.typ=procsym) then
  703. begin
  704. curconstSegment.concat(Tai_const_symbol.createname(
  705. tprocsym(tloadnode(p).symtableentry).defs^.def.mangledname));
  706. end
  707. else
  708. Message(cg_e_illegal_expression);
  709. p.free;
  710. end;
  711. { reads a typed constant record }
  712. recorddef:
  713. begin
  714. { KAZ }
  715. if (trecorddef(t.def)=rec_tguid) and
  716. ((token=_CSTRING) or (token=_CCHAR) or (token=_ID)) then
  717. begin
  718. p:=comp_expr(true);
  719. inserttypeconv(p,cshortstringtype);
  720. if p.nodetype=stringconstn then
  721. begin
  722. s:=strpas(tstringconstnode(p).value_str);
  723. p.free;
  724. if string2guid(s,tmpguid) then
  725. begin
  726. curconstSegment.concat(Tai_const.Create_32bit(tmpguid.D1));
  727. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D2));
  728. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D3));
  729. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  730. curconstSegment.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  731. end
  732. else
  733. Message(parser_e_improper_guid_syntax);
  734. end
  735. else
  736. begin
  737. p.free;
  738. Message(cg_e_illegal_expression);
  739. exit;
  740. end;
  741. end
  742. else
  743. begin
  744. consume(_LKLAMMER);
  745. aktpos:=0;
  746. srsym := tsym(trecorddef(t.def).symtable.symindex.first);
  747. recsym := nil;
  748. while token<>_RKLAMMER do
  749. begin
  750. s:=pattern;
  751. sorg:=orgpattern;
  752. consume(_ID);
  753. consume(_COLON);
  754. error := false;
  755. recsym := tsym(trecorddef(t.def).symtable.search(s));
  756. if not assigned(recsym) then
  757. begin
  758. Message1(sym_e_illegal_field,s);
  759. error := true;
  760. end;
  761. if not assigned(srsym) or
  762. (s <> srsym.name) then
  763. { possible variant record (JM) }
  764. begin
  765. { All parts of a variant start at the same offset }
  766. { Also allow jumping from one variant part to another, }
  767. { as long as the offsets match }
  768. if (assigned(srsym) and
  769. (tvarsym(recsym).address = tvarsym(srsym).address)) or
  770. { srsym is not assigned after parsing w2 in the }
  771. { typed const in the next example: }
  772. { type tr = record case byte of }
  773. { 1: (l1,l2: dword); }
  774. { 2: (w1,w2: word); }
  775. { end; }
  776. { const r: tr = (w1:1;w2:1;l2:5); }
  777. (tvarsym(recsym).address = aktpos) then
  778. srsym := recsym
  779. { going backwards isn't allowed in any mode }
  780. else if (tvarsym(recsym).address<aktpos) then
  781. begin
  782. Message(parser_e_invalid_record_const);
  783. error := true;
  784. end
  785. { Delphi allows you to skip fields }
  786. else if (m_delphi in aktmodeswitches) then
  787. begin
  788. Message1(parser_w_skipped_fields_before,sorg);
  789. srsym := recsym;
  790. end
  791. { FPC and TP don't }
  792. else
  793. begin
  794. Message1(parser_e_skipped_fields_before,sorg);
  795. error := true;
  796. end;
  797. end;
  798. if error then
  799. consume_all_until(_SEMICOLON)
  800. else
  801. begin
  802. { if needed fill (alignment) }
  803. if tvarsym(srsym).address>aktpos then
  804. for i:=1 to tvarsym(srsym).address-aktpos do
  805. curconstSegment.concat(Tai_const.Create_8bit(0));
  806. { new position }
  807. aktpos:=tvarsym(srsym).address+tvarsym(srsym).vartype.def.size;
  808. { read the data }
  809. readtypedconst(tvarsym(srsym).vartype,nil,writable);
  810. { keep previous field for checking whether whole }
  811. { record was initialized (JM) }
  812. recsym := srsym;
  813. { goto next field }
  814. srsym := tsym(srsym.indexnext);
  815. if token=_SEMICOLON then
  816. consume(_SEMICOLON)
  817. else break;
  818. end;
  819. end;
  820. { are there any fields left? }
  821. if assigned(srsym) and
  822. { don't complain if there only come other variant parts }
  823. { after the last initialized field }
  824. ((recsym=nil) or
  825. (tvarsym(srsym).address > tvarsym(recsym).address)) then
  826. Message1(parser_h_skipped_fields_after,s);
  827. for i:=1 to t.def.size-aktpos do
  828. curconstSegment.concat(Tai_const.Create_8bit(0));
  829. consume(_RKLAMMER);
  830. end;
  831. end;
  832. { reads a typed object }
  833. objectdef:
  834. begin
  835. if is_class_or_interface(t.def) then
  836. begin
  837. p:=comp_expr(true);
  838. if p.nodetype<>niln then
  839. begin
  840. Message(parser_e_type_const_not_possible);
  841. consume_all_until(_RKLAMMER);
  842. end
  843. else
  844. begin
  845. curconstSegment.concat(Tai_const.Create_32bit(0));
  846. end;
  847. p.free;
  848. end
  849. { for objects we allow it only if it doesn't contain a vmt }
  850. else if (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  851. not(m_tp in aktmodeswitches) then
  852. Message(parser_e_type_const_not_possible)
  853. else
  854. begin
  855. consume(_LKLAMMER);
  856. aktpos:=0;
  857. while token<>_RKLAMMER do
  858. begin
  859. s:=pattern;
  860. sorg:=orgpattern;
  861. consume(_ID);
  862. consume(_COLON);
  863. srsym:=nil;
  864. obj:=tobjectdef(t.def);
  865. symt:=obj.symtable;
  866. while (srsym=nil) and assigned(symt) do
  867. begin
  868. srsym:=tsym(symt.search(s));
  869. if assigned(obj) then
  870. obj:=obj.childof;
  871. if assigned(obj) then
  872. symt:=obj.symtable
  873. else
  874. symt:=nil;
  875. end;
  876. if srsym=nil then
  877. begin
  878. Message1(sym_e_id_not_found,sorg);
  879. consume_all_until(_SEMICOLON);
  880. end
  881. else
  882. begin
  883. { check position }
  884. if tvarsym(srsym).address<aktpos then
  885. Message(parser_e_invalid_record_const);
  886. { check in VMT needs to be added for TP mode }
  887. if (m_tp in aktmodeswitches) and
  888. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  889. (tobjectdef(t.def).vmt_offset<tvarsym(srsym).address) then
  890. begin
  891. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  892. curconstsegment.concat(tai_const.create_8bit(0));
  893. curconstsegment.concat(tai_const_symbol.createname(tobjectdef(t.def).vmt_mangledname));
  894. { this is more general }
  895. aktpos:=tobjectdef(t.def).vmt_offset + target_info.size_of_pointer;
  896. end;
  897. { if needed fill }
  898. if tvarsym(srsym).address>aktpos then
  899. for i:=1 to tvarsym(srsym).address-aktpos do
  900. curconstSegment.concat(Tai_const.Create_8bit(0));
  901. { new position }
  902. aktpos:=tvarsym(srsym).address+tvarsym(srsym).vartype.def.size;
  903. { read the data }
  904. readtypedconst(tvarsym(srsym).vartype,nil,writable);
  905. if token=_SEMICOLON then
  906. consume(_SEMICOLON)
  907. else break;
  908. end;
  909. end;
  910. if (m_tp in aktmodeswitches) and
  911. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  912. (tobjectdef(t.def).vmt_offset>=aktpos) then
  913. begin
  914. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  915. curconstsegment.concat(tai_const.create_8bit(0));
  916. curconstsegment.concat(tai_const_symbol.createname(tobjectdef(t.def).vmt_mangledname));
  917. { this is more general }
  918. aktpos:=tobjectdef(t.def).vmt_offset + target_info.size_of_pointer;
  919. end;
  920. for i:=1 to t.def.size-aktpos do
  921. curconstSegment.concat(Tai_const.Create_8bit(0));
  922. consume(_RKLAMMER);
  923. end;
  924. end;
  925. errordef:
  926. begin
  927. { try to consume something useful }
  928. if token=_LKLAMMER then
  929. consume_all_until(_RKLAMMER)
  930. else
  931. consume_all_until(_SEMICOLON);
  932. end;
  933. else Message(parser_e_type_const_not_possible);
  934. end;
  935. end;
  936. {$ifdef fpc}
  937. {$maxfpuregisters default}
  938. {$endif fpc}
  939. end.
  940. {
  941. $Log$
  942. Revision 1.39 2001-12-06 17:57:38 florian
  943. + parasym to tparaitem added
  944. Revision 1.38 2001/11/02 22:58:06 peter
  945. * procsym definition rewrite
  946. Revision 1.37 2001/10/29 14:59:48 jonas
  947. * typed constants that are "procedure of object" and which are assigned
  948. nil require 8 bytes of "0" (not 4)
  949. * fixed web bug 1655 (reject the code)
  950. Revision 1.36 2001/10/20 20:30:21 peter
  951. * read only typed const support, switch $J-
  952. Revision 1.35 2001/10/20 17:24:26 peter
  953. * make all sets equal when reading an array of sets. Before it could
  954. mix normal and small sets in the same array!
  955. Revision 1.34 2001/09/19 11:06:03 michael
  956. * realname updated for some hints
  957. * realname used for consts,labels
  958. Revision 1.33 2001/09/17 21:29:12 peter
  959. * merged netbsd, fpu-overflow from fixes branch
  960. Revision 1.32 2001/09/02 21:18:28 peter
  961. * split constsym.value in valueord,valueordptr,valueptr. The valueordptr
  962. is used for holding target platform pointer values. As those can be
  963. bigger than the source platform.
  964. Revision 1.31 2001/08/26 13:36:47 florian
  965. * some cg reorganisation
  966. * some PPC updates
  967. Revision 1.30 2001/08/01 21:46:41 peter
  968. * support pwidechar in typed const
  969. Revision 1.29 2001/07/30 21:39:26 peter
  970. * declare fpu in rtl for m68k linux
  971. Revision 1.28 2001/07/30 20:59:27 peter
  972. * m68k updates from v10 merged
  973. Revision 1.27 2001/07/08 21:00:15 peter
  974. * various widestring updates, it works now mostly without charset
  975. mapping supported
  976. Revision 1.26 2001/06/29 14:16:57 jonas
  977. * fixed inconsistent handling of procvars in FPC mode (sometimes @ was
  978. required to assign the address of a procedure to a procvar, sometimes
  979. not. Now it is always required) (merged)
  980. Revision 1.25 2001/06/27 21:37:36 peter
  981. * v10 merges
  982. Revision 1.24 2001/06/18 20:36:25 peter
  983. * -Ur switch (merged)
  984. * masm fixes (merged)
  985. * quoted filenames for go32v2 and win32
  986. Revision 1.23 2001/05/06 17:15:00 jonas
  987. + detect incomplete typed constant records
  988. Revision 1.22 2001/04/18 22:01:57 peter
  989. * registration of targets and assemblers
  990. Revision 1.21 2001/04/13 01:22:13 peter
  991. * symtable change to classes
  992. * range check generation and errors fixed, make cycle DEBUG=1 works
  993. * memory leaks fixed
  994. Revision 1.20 2001/04/04 22:43:53 peter
  995. * remove unnecessary calls to firstpass
  996. Revision 1.19 2001/04/02 21:20:34 peter
  997. * resulttype rewrite
  998. Revision 1.18 2001/03/11 22:58:50 peter
  999. * getsym redesign, removed the globals srsym,srsymtable
  1000. Revision 1.17 2001/02/04 11:12:16 jonas
  1001. * fixed web bug 1377 & const pointer arithmtic
  1002. Revision 1.16 2001/02/03 00:26:35 peter
  1003. * merged fix for bug 1365
  1004. Revision 1.15 2000/12/25 00:07:28 peter
  1005. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1006. tlinkedlist objects)
  1007. Revision 1.14 2000/12/10 20:24:18 peter
  1008. * allow subtypes for enums
  1009. Revision 1.13 2000/11/29 00:30:38 florian
  1010. * unused units removed from uses clause
  1011. * some changes for widestrings
  1012. Revision 1.12 2000/11/06 15:54:15 florian
  1013. * fixed two bugs to get make cycle work, but it's not enough
  1014. Revision 1.11 2000/11/04 14:25:21 florian
  1015. + merged Attila's changes for interfaces, not tested yet
  1016. Revision 1.10 2000/10/31 22:02:51 peter
  1017. * symtable splitted, no real code changes
  1018. Revision 1.9 2000/10/14 10:14:52 peter
  1019. * moehrendorf oct 2000 rewrite
  1020. Revision 1.8 2000/09/30 13:23:04 peter
  1021. * const array of char and pchar length fixed (merged)
  1022. Revision 1.7 2000/09/24 15:06:25 peter
  1023. * use defines.inc
  1024. Revision 1.6 2000/08/27 16:11:52 peter
  1025. * moved some util functions from globals,cobjects to cutils
  1026. * splitted files into finput,fmodule
  1027. Revision 1.5 2000/08/24 19:13:18 peter
  1028. * allow nil for class typed consts (merged)
  1029. Revision 1.4 2000/08/16 13:06:06 florian
  1030. + support of 64 bit integer constants
  1031. Revision 1.3 2000/08/05 13:25:06 peter
  1032. * packenum 1 fixes (merged)
  1033. Revision 1.2 2000/07/13 11:32:47 michael
  1034. + removed logs
  1035. }