ptconst.pas 43 KB

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