ptconst.pas 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. consume(_NIL);
  644. exit;
  645. end;
  646. getprocvar:=true;
  647. getprocvardef:=tprocvardef(t.def);
  648. p:=comp_expr(true);
  649. getprocvar:=false;
  650. if codegenerror then
  651. begin
  652. p.free;
  653. exit;
  654. end;
  655. { let type conversion check everything needed }
  656. inserttypeconv(p,t);
  657. if codegenerror then
  658. begin
  659. p.free;
  660. exit;
  661. end;
  662. { remove typeconvn, that will normally insert a lea
  663. instruction which is not necessary for us }
  664. if p.nodetype=typeconvn then
  665. begin
  666. hp:=ttypeconvnode(p).left;
  667. ttypeconvnode(p).left:=nil;
  668. p.free;
  669. p:=hp;
  670. end;
  671. { remove addrn which we also don't need here }
  672. if p.nodetype=addrn then
  673. begin
  674. hp:=taddrnode(p).left;
  675. taddrnode(p).left:=nil;
  676. p.free;
  677. p:=hp;
  678. end;
  679. { we now need to have a loadn with a procsym }
  680. if (p.nodetype=loadn) and
  681. (tloadnode(p).symtableentry.typ=procsym) then
  682. begin
  683. curconstSegment.concat(Tai_const_symbol.createname(
  684. tprocsym(tloadnode(p).symtableentry).definition.mangledname));
  685. end
  686. else
  687. Message(cg_e_illegal_expression);
  688. p.free;
  689. end;
  690. { reads a typed constant record }
  691. recorddef:
  692. begin
  693. { KAZ }
  694. if (trecorddef(t.def)=rec_tguid) and
  695. ((token=_CSTRING) or (token=_CCHAR) or (token=_ID)) then
  696. begin
  697. p:=comp_expr(true);
  698. inserttypeconv(p,cshortstringtype);
  699. if p.nodetype=stringconstn then
  700. begin
  701. s:=strpas(tstringconstnode(p).value_str);
  702. p.free;
  703. if string2guid(s,tmpguid) then
  704. begin
  705. curconstSegment.concat(Tai_const.Create_32bit(tmpguid.D1));
  706. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D2));
  707. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D3));
  708. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  709. curconstSegment.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  710. end
  711. else
  712. Message(parser_e_improper_guid_syntax);
  713. end
  714. else
  715. begin
  716. p.free;
  717. Message(cg_e_illegal_expression);
  718. exit;
  719. end;
  720. end
  721. else
  722. begin
  723. consume(_LKLAMMER);
  724. aktpos:=0;
  725. srsym := tsym(trecorddef(t.def).symtable.symindex.first);
  726. recsym := nil;
  727. while token<>_RKLAMMER do
  728. begin
  729. s:=pattern;
  730. sorg:=orgpattern;
  731. consume(_ID);
  732. consume(_COLON);
  733. error := false;
  734. recsym := tsym(trecorddef(t.def).symtable.search(s));
  735. if not assigned(recsym) then
  736. begin
  737. Message1(sym_e_illegal_field,s);
  738. error := true;
  739. end;
  740. if not assigned(srsym) or
  741. (s <> srsym.name) then
  742. { possible variant record (JM) }
  743. begin
  744. { All parts of a variant start at the same offset }
  745. { Also allow jumping from one variant part to another, }
  746. { as long as the offsets match }
  747. if (assigned(srsym) and
  748. (tvarsym(recsym).address = tvarsym(srsym).address)) or
  749. { srsym is not assigned after parsing w2 in the }
  750. { typed const in the next example: }
  751. { type tr = record case byte of }
  752. { 1: (l1,l2: dword); }
  753. { 2: (w1,w2: word); }
  754. { end; }
  755. { const r: tr = (w1:1;w2:1;l2:5); }
  756. (tvarsym(recsym).address = aktpos) then
  757. srsym := recsym
  758. { going backwards isn't allowed in any mode }
  759. else if (tvarsym(recsym).address<aktpos) then
  760. begin
  761. Message(parser_e_invalid_record_const);
  762. error := true;
  763. end
  764. { Delphi allows you to skip fields }
  765. else if (m_delphi in aktmodeswitches) then
  766. begin
  767. Message1(parser_w_skipped_fields_before,sorg);
  768. srsym := recsym;
  769. end
  770. { FPC and TP don't }
  771. else
  772. begin
  773. Message1(parser_e_skipped_fields_before,sorg);
  774. error := true;
  775. end;
  776. end;
  777. if error then
  778. consume_all_until(_SEMICOLON)
  779. else
  780. begin
  781. { if needed fill (alignment) }
  782. if tvarsym(srsym).address>aktpos then
  783. for i:=1 to tvarsym(srsym).address-aktpos do
  784. curconstSegment.concat(Tai_const.Create_8bit(0));
  785. { new position }
  786. aktpos:=tvarsym(srsym).address+tvarsym(srsym).vartype.def.size;
  787. { read the data }
  788. readtypedconst(tvarsym(srsym).vartype,nil,writable);
  789. { keep previous field for checking whether whole }
  790. { record was initialized (JM) }
  791. recsym := srsym;
  792. { goto next field }
  793. srsym := tsym(srsym.indexnext);
  794. if token=_SEMICOLON then
  795. consume(_SEMICOLON)
  796. else break;
  797. end;
  798. end;
  799. { are there any fields left? }
  800. if assigned(srsym) and
  801. { don't complain if there only come other variant parts }
  802. { after the last initialized field }
  803. ((recsym=nil) or
  804. (tvarsym(srsym).address > tvarsym(recsym).address)) then
  805. Message1(parser_h_skipped_fields_after,s);
  806. for i:=1 to t.def.size-aktpos do
  807. curconstSegment.concat(Tai_const.Create_8bit(0));
  808. consume(_RKLAMMER);
  809. end;
  810. end;
  811. { reads a typed object }
  812. objectdef:
  813. begin
  814. if is_class_or_interface(t.def) then
  815. begin
  816. p:=comp_expr(true);
  817. if p.nodetype<>niln then
  818. begin
  819. Message(parser_e_type_const_not_possible);
  820. consume_all_until(_RKLAMMER);
  821. end
  822. else
  823. begin
  824. curconstSegment.concat(Tai_const.Create_32bit(0));
  825. end;
  826. p.free;
  827. end
  828. { for objects we allow it only if it doesn't contain a vmt }
  829. else if (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  830. not(m_tp in aktmodeswitches) then
  831. Message(parser_e_type_const_not_possible)
  832. else
  833. begin
  834. consume(_LKLAMMER);
  835. aktpos:=0;
  836. while token<>_RKLAMMER do
  837. begin
  838. s:=pattern;
  839. sorg:=orgpattern;
  840. consume(_ID);
  841. consume(_COLON);
  842. srsym:=nil;
  843. obj:=tobjectdef(t.def);
  844. symt:=obj.symtable;
  845. while (srsym=nil) and assigned(symt) do
  846. begin
  847. srsym:=tsym(symt.search(s));
  848. if assigned(obj) then
  849. obj:=obj.childof;
  850. if assigned(obj) then
  851. symt:=obj.symtable
  852. else
  853. symt:=nil;
  854. end;
  855. if srsym=nil then
  856. begin
  857. Message1(sym_e_id_not_found,sorg);
  858. consume_all_until(_SEMICOLON);
  859. end
  860. else
  861. begin
  862. { check position }
  863. if tvarsym(srsym).address<aktpos then
  864. Message(parser_e_invalid_record_const);
  865. { check in VMT needs to be added for TP mode }
  866. if (m_tp in aktmodeswitches) and
  867. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  868. (tobjectdef(t.def).vmt_offset<tvarsym(srsym).address) then
  869. begin
  870. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  871. curconstsegment.concat(tai_const.create_8bit(0));
  872. curconstsegment.concat(tai_const_symbol.createname(tobjectdef(t.def).vmt_mangledname));
  873. { this is more general }
  874. aktpos:=tobjectdef(t.def).vmt_offset + target_info.size_of_pointer;
  875. end;
  876. { if needed fill }
  877. if tvarsym(srsym).address>aktpos then
  878. for i:=1 to tvarsym(srsym).address-aktpos do
  879. curconstSegment.concat(Tai_const.Create_8bit(0));
  880. { new position }
  881. aktpos:=tvarsym(srsym).address+tvarsym(srsym).vartype.def.size;
  882. { read the data }
  883. readtypedconst(tvarsym(srsym).vartype,nil,writable);
  884. if token=_SEMICOLON then
  885. consume(_SEMICOLON)
  886. else break;
  887. end;
  888. end;
  889. if (m_tp in aktmodeswitches) and
  890. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  891. (tobjectdef(t.def).vmt_offset>=aktpos) then
  892. begin
  893. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  894. curconstsegment.concat(tai_const.create_8bit(0));
  895. curconstsegment.concat(tai_const_symbol.createname(tobjectdef(t.def).vmt_mangledname));
  896. { this is more general }
  897. aktpos:=tobjectdef(t.def).vmt_offset + target_info.size_of_pointer;
  898. end;
  899. for i:=1 to t.def.size-aktpos do
  900. curconstSegment.concat(Tai_const.Create_8bit(0));
  901. consume(_RKLAMMER);
  902. end;
  903. end;
  904. errordef:
  905. begin
  906. { try to consume something useful }
  907. if token=_LKLAMMER then
  908. consume_all_until(_RKLAMMER)
  909. else
  910. consume_all_until(_SEMICOLON);
  911. end;
  912. else Message(parser_e_type_const_not_possible);
  913. end;
  914. end;
  915. {$ifdef fpc}
  916. {$maxfpuregisters default}
  917. {$endif fpc}
  918. end.
  919. {
  920. $Log$
  921. Revision 1.36 2001-10-20 20:30:21 peter
  922. * read only typed const support, switch $J-
  923. Revision 1.35 2001/10/20 17:24:26 peter
  924. * make all sets equal when reading an array of sets. Before it could
  925. mix normal and small sets in the same array!
  926. Revision 1.34 2001/09/19 11:06:03 michael
  927. * realname updated for some hints
  928. * realname used for consts,labels
  929. Revision 1.33 2001/09/17 21:29:12 peter
  930. * merged netbsd, fpu-overflow from fixes branch
  931. Revision 1.32 2001/09/02 21:18:28 peter
  932. * split constsym.value in valueord,valueordptr,valueptr. The valueordptr
  933. is used for holding target platform pointer values. As those can be
  934. bigger than the source platform.
  935. Revision 1.31 2001/08/26 13:36:47 florian
  936. * some cg reorganisation
  937. * some PPC updates
  938. Revision 1.30 2001/08/01 21:46:41 peter
  939. * support pwidechar in typed const
  940. Revision 1.29 2001/07/30 21:39:26 peter
  941. * declare fpu in rtl for m68k linux
  942. Revision 1.28 2001/07/30 20:59:27 peter
  943. * m68k updates from v10 merged
  944. Revision 1.27 2001/07/08 21:00:15 peter
  945. * various widestring updates, it works now mostly without charset
  946. mapping supported
  947. Revision 1.26 2001/06/29 14:16:57 jonas
  948. * fixed inconsistent handling of procvars in FPC mode (sometimes @ was
  949. required to assign the address of a procedure to a procvar, sometimes
  950. not. Now it is always required) (merged)
  951. Revision 1.25 2001/06/27 21:37:36 peter
  952. * v10 merges
  953. Revision 1.24 2001/06/18 20:36:25 peter
  954. * -Ur switch (merged)
  955. * masm fixes (merged)
  956. * quoted filenames for go32v2 and win32
  957. Revision 1.23 2001/05/06 17:15:00 jonas
  958. + detect incomplete typed constant records
  959. Revision 1.22 2001/04/18 22:01:57 peter
  960. * registration of targets and assemblers
  961. Revision 1.21 2001/04/13 01:22:13 peter
  962. * symtable change to classes
  963. * range check generation and errors fixed, make cycle DEBUG=1 works
  964. * memory leaks fixed
  965. Revision 1.20 2001/04/04 22:43:53 peter
  966. * remove unnecessary calls to firstpass
  967. Revision 1.19 2001/04/02 21:20:34 peter
  968. * resulttype rewrite
  969. Revision 1.18 2001/03/11 22:58:50 peter
  970. * getsym redesign, removed the globals srsym,srsymtable
  971. Revision 1.17 2001/02/04 11:12:16 jonas
  972. * fixed web bug 1377 & const pointer arithmtic
  973. Revision 1.16 2001/02/03 00:26:35 peter
  974. * merged fix for bug 1365
  975. Revision 1.15 2000/12/25 00:07:28 peter
  976. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  977. tlinkedlist objects)
  978. Revision 1.14 2000/12/10 20:24:18 peter
  979. * allow subtypes for enums
  980. Revision 1.13 2000/11/29 00:30:38 florian
  981. * unused units removed from uses clause
  982. * some changes for widestrings
  983. Revision 1.12 2000/11/06 15:54:15 florian
  984. * fixed two bugs to get make cycle work, but it's not enough
  985. Revision 1.11 2000/11/04 14:25:21 florian
  986. + merged Attila's changes for interfaces, not tested yet
  987. Revision 1.10 2000/10/31 22:02:51 peter
  988. * symtable splitted, no real code changes
  989. Revision 1.9 2000/10/14 10:14:52 peter
  990. * moehrendorf oct 2000 rewrite
  991. Revision 1.8 2000/09/30 13:23:04 peter
  992. * const array of char and pchar length fixed (merged)
  993. Revision 1.7 2000/09/24 15:06:25 peter
  994. * use defines.inc
  995. Revision 1.6 2000/08/27 16:11:52 peter
  996. * moved some util functions from globals,cobjects to cutils
  997. * splitted files into finput,fmodule
  998. Revision 1.5 2000/08/24 19:13:18 peter
  999. * allow nil for class typed consts (merged)
  1000. Revision 1.4 2000/08/16 13:06:06 florian
  1001. + support of 64 bit integer constants
  1002. Revision 1.3 2000/08/05 13:25:06 peter
  1003. * packenum 1 fixes (merged)
  1004. Revision 1.2 2000/07/13 11:32:47 michael
  1005. + removed logs
  1006. }