ptconst.pas 45 KB

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