ptconst.pas 43 KB

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