ptconst.pas 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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 : 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. hp:=taddrnode(p).left;
  300. while assigned(hp) and (hp.nodetype in [subscriptn,vecn]) do
  301. hp:=tbinarynode(hp).left;
  302. if (is_equal(tpointerdef(p.resulttype.def).pointertype.def,tpointerdef(t.def).pointertype.def) or
  303. (is_void(tpointerdef(p.resulttype.def).pointertype.def)) or
  304. (is_void(tpointerdef(t.def).pointertype.def))) and
  305. (hp.nodetype=loadn) then
  306. begin
  307. do_resulttypepass(taddrnode(p).left);
  308. hp:=taddrnode(p).left;
  309. offset:=0;
  310. while assigned(hp) and (hp.nodetype<>loadn) do
  311. begin
  312. case hp.nodetype of
  313. vecn :
  314. begin
  315. case tvecnode(hp).left.resulttype.def.deftype of
  316. stringdef :
  317. begin
  318. { this seems OK for shortstring and ansistrings PM }
  319. { it is wrong for widestrings !! }
  320. len:=1;
  321. base:=0;
  322. end;
  323. arraydef :
  324. begin
  325. len:=tarraydef(tvecnode(hp).left.resulttype.def).elesize;
  326. base:=tarraydef(tvecnode(hp).left.resulttype.def).lowrange;
  327. end
  328. else
  329. Message(cg_e_illegal_expression);
  330. end;
  331. if is_constintnode(tvecnode(hp).right) then
  332. inc(offset,len*(get_ordinal_value(tvecnode(hp).right)-base))
  333. else
  334. Message(cg_e_illegal_expression);
  335. end;
  336. subscriptn :
  337. inc(offset,tsubscriptnode(hp).vs.address)
  338. else
  339. Message(cg_e_illegal_expression);
  340. end;
  341. hp:=tbinarynode(hp).left;
  342. end;
  343. if tloadnode(hp).symtableentry.typ=constsym then
  344. Message(type_e_variable_id_expected);
  345. curconstSegment.concat(Tai_const_symbol.Createname_offset(tloadnode(hp).symtableentry.mangledname,offset));
  346. end
  347. else
  348. Message(cg_e_illegal_expression);
  349. end
  350. else
  351. { allow typeof(Object type)}
  352. if (p.nodetype=inlinen) and
  353. (tinlinenode(p).inlinenumber=in_typeof_x) then
  354. begin
  355. if (tinlinenode(p).left.nodetype=typen) then
  356. begin
  357. curconstSegment.concat(Tai_const_symbol.createname(
  358. tobjectdef(tinlinenode(p).left.resulttype.def).vmt_mangledname));
  359. end
  360. else
  361. Message(cg_e_illegal_expression);
  362. end
  363. else
  364. Message(cg_e_illegal_expression);
  365. p.free;
  366. end;
  367. setdef:
  368. begin
  369. p:=comp_expr(true);
  370. if p.nodetype=setconstn then
  371. begin
  372. { we only allow const sets }
  373. if assigned(tsetconstnode(p).left) then
  374. Message(cg_e_illegal_expression)
  375. else
  376. begin
  377. { this writing is endian independant }
  378. { untrue - because they are considered }
  379. { arrays of 32-bit values CEC }
  380. if source_info.endian = target_info.endian then
  381. begin
  382. for l:= 0 to p.resulttype.def.size-1 do
  383. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[l]));
  384. end
  385. else
  386. begin
  387. { store as longint values in swaped format }
  388. j:=0;
  389. for l:=0 to ((p.resulttype.def.size-1) div 4) do
  390. begin
  391. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j+3]));
  392. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j+2]));
  393. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j+1]));
  394. curconstsegment.concat(tai_const.create_8bit(tsetconstnode(p).value_set^[j]));
  395. Inc(j,4);
  396. end;
  397. end;
  398. end;
  399. end
  400. else
  401. Message(cg_e_illegal_expression);
  402. p.free;
  403. end;
  404. enumdef:
  405. begin
  406. p:=comp_expr(true);
  407. if p.nodetype=ordconstn then
  408. begin
  409. if is_equal(p.resulttype.def,t.def) or
  410. is_subequal(p.resulttype.def,t.def) then
  411. begin
  412. case p.resulttype.def.size of
  413. 1 : curconstSegment.concat(Tai_const.Create_8bit(tordconstnode(p).value));
  414. 2 : curconstSegment.concat(Tai_const.Create_16bit(tordconstnode(p).value));
  415. 4 : curconstSegment.concat(Tai_const.Create_32bit(tordconstnode(p).value));
  416. end;
  417. end
  418. else
  419. Message2(type_e_incompatible_types,t.def.typename,p.resulttype.def.typename);
  420. end
  421. else
  422. Message(cg_e_illegal_expression);
  423. p.free;
  424. end;
  425. stringdef:
  426. begin
  427. p:=comp_expr(true);
  428. { load strval and strlength of the constant tree }
  429. if p.nodetype=stringconstn then
  430. begin
  431. { convert to the expected string type so that
  432. for widestrings strval is a pcompilerwidestring }
  433. inserttypeconv(p,t);
  434. strlength:=tstringconstnode(p).len;
  435. strval:=tstringconstnode(p).value_str;
  436. end
  437. else if is_constcharnode(p) then
  438. begin
  439. { strval:=pchar(@tordconstnode(p).value);
  440. THIS FAIL on BIG_ENDIAN MACHINES PM }
  441. c:=chr(tordconstnode(p).value and $ff);
  442. strval:=@c;
  443. strlength:=1
  444. end
  445. else if is_constresourcestringnode(p) then
  446. begin
  447. strval:=pchar(tpointerord(tconstsym(tloadnode(p).symtableentry).value));
  448. strlength:=tconstsym(tloadnode(p).symtableentry).len;
  449. end
  450. else
  451. begin
  452. Message(cg_e_illegal_expression);
  453. strlength:=-1;
  454. end;
  455. if strlength>=0 then
  456. begin
  457. case tstringdef(t.def).string_typ of
  458. st_shortstring:
  459. begin
  460. if strlength>=t.def.size then
  461. begin
  462. message2(parser_w_string_too_long,strpas(strval),tostr(t.def.size-1));
  463. strlength:=t.def.size-1;
  464. end;
  465. curconstSegment.concat(Tai_const.Create_8bit(strlength));
  466. { this can also handle longer strings }
  467. getmem(ca,strlength+1);
  468. move(strval^,ca^,strlength);
  469. ca[strlength]:=#0;
  470. curconstSegment.concat(Tai_string.Create_length_pchar(ca,strlength));
  471. { fillup with spaces if size is shorter }
  472. if t.def.size>strlength then
  473. begin
  474. getmem(ca,t.def.size-strlength);
  475. { def.size contains also the leading length, so we }
  476. { we have to subtract one }
  477. fillchar(ca[0],t.def.size-strlength-1,' ');
  478. ca[t.def.size-strlength-1]:=#0;
  479. { this can also handle longer strings }
  480. curconstSegment.concat(Tai_string.Create_length_pchar(ca,t.def.size-strlength-1));
  481. end;
  482. end;
  483. st_ansistring:
  484. begin
  485. { an empty ansi string is nil! }
  486. if (strlength=0) then
  487. curconstSegment.concat(Tai_const.Create_32bit(0))
  488. else
  489. begin
  490. getdatalabel(ll);
  491. curconstSegment.concat(Tai_const_symbol.Create(ll));
  492. { first write the maximum size }
  493. Consts.concat(Tai_const.Create_32bit(strlength));
  494. { second write the real length }
  495. Consts.concat(Tai_const.Create_32bit(strlength));
  496. { redondent with maxlength but who knows ... (PM) }
  497. { third write use count (set to -1 for safety ) }
  498. Consts.concat(Tai_const.Create_32bit(-1));
  499. Consts.concat(Tai_label.Create(ll));
  500. getmem(ca,strlength+2);
  501. move(strval^,ca^,strlength);
  502. { The terminating #0 to be stored in the .data section (JM) }
  503. ca[strlength]:=#0;
  504. { End of the PChar. The memory has to be allocated because in }
  505. { tai_string.done, there is a freemem(len+1) (JM) }
  506. ca[strlength+1]:=#0;
  507. Consts.concat(Tai_string.Create_length_pchar(ca,strlength+1));
  508. end;
  509. end;
  510. st_widestring:
  511. begin
  512. { an empty ansi string is nil! }
  513. if (strlength=0) then
  514. curconstSegment.concat(Tai_const.Create_32bit(0))
  515. else
  516. begin
  517. getdatalabel(ll);
  518. curconstSegment.concat(Tai_const_symbol.Create(ll));
  519. Consts.concat(Tai_const.Create_32bit(strlength));
  520. Consts.concat(Tai_const.Create_32bit(strlength));
  521. Consts.concat(Tai_const.Create_32bit(-1));
  522. Consts.concat(Tai_label.Create(ll));
  523. for i:=0 to strlength-1 do
  524. Consts.concat(Tai_const.Create_16bit(pcompilerwidestring(strval)^.data[i]));
  525. { ending #0 }
  526. Consts.concat(Tai_const.Create_16bit(0))
  527. end;
  528. end;
  529. st_longstring:
  530. begin
  531. internalerror(200107081);
  532. {curconstSegment.concat(Tai_const.Create_32bit(strlength))));
  533. curconstSegment.concat(Tai_const.Create_8bit(0));
  534. getmem(ca,strlength+1);
  535. move(strval^,ca^,strlength);
  536. ca[strlength]:=#0;
  537. generate_pascii(consts,ca,strlength);
  538. curconstSegment.concat(Tai_const.Create_8bit(0));}
  539. end;
  540. end;
  541. end;
  542. p.free;
  543. end;
  544. arraydef:
  545. begin
  546. if token=_LKLAMMER then
  547. begin
  548. consume(_LKLAMMER);
  549. for l:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange-1 do
  550. begin
  551. readtypedconst(tarraydef(t.def).elementtype,nil,no_change_allowed);
  552. consume(_COMMA);
  553. end;
  554. readtypedconst(tarraydef(t.def).elementtype,nil,no_change_allowed);
  555. consume(_RKLAMMER);
  556. end
  557. else
  558. { if array of char then we allow also a string }
  559. if is_char(tarraydef(t.def).elementtype.def) then
  560. begin
  561. p:=comp_expr(true);
  562. if p.nodetype=stringconstn then
  563. begin
  564. len:=tstringconstnode(p).len;
  565. { For tp7 the maximum lentgh can be 255 }
  566. if (m_tp in aktmodeswitches) and
  567. (len>255) then
  568. len:=255;
  569. ca:=tstringconstnode(p).value_str;
  570. end
  571. else
  572. if is_constcharnode(p) then
  573. begin
  574. c:=chr(tordconstnode(p).value and $ff);
  575. ca:=@c;
  576. len:=1;
  577. end
  578. else
  579. begin
  580. Message(cg_e_illegal_expression);
  581. len:=0;
  582. end;
  583. if len>(tarraydef(t.def).highrange-tarraydef(t.def).lowrange+1) then
  584. Message(parser_e_string_larger_array);
  585. for i:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange do
  586. begin
  587. if i+1-tarraydef(t.def).lowrange<=len then
  588. begin
  589. curconstSegment.concat(Tai_const.Create_8bit(byte(ca^)));
  590. inc(ca);
  591. end
  592. else
  593. {Fill the remaining positions with #0.}
  594. curconstSegment.concat(Tai_const.Create_8bit(0));
  595. end;
  596. p.free;
  597. end
  598. else
  599. begin
  600. { we want the ( }
  601. consume(_LKLAMMER);
  602. end;
  603. end;
  604. procvardef:
  605. begin
  606. { Procvars and pointers are no longer compatible. }
  607. { under tp: =nil or =var under fpc: =nil or =@var }
  608. if token=_NIL then
  609. begin
  610. curconstSegment.concat(Tai_const.Create_32bit(0));
  611. consume(_NIL);
  612. exit;
  613. end;
  614. getprocvar:=true;
  615. getprocvardef:=tprocvardef(t.def);
  616. p:=comp_expr(true);
  617. getprocvar:=false;
  618. if codegenerror then
  619. begin
  620. p.free;
  621. exit;
  622. end;
  623. { let type conversion check everything needed }
  624. inserttypeconv(p,t);
  625. if codegenerror then
  626. begin
  627. p.free;
  628. exit;
  629. end;
  630. { remove typeconvn, that will normally insert a lea
  631. instruction which is not necessary for us }
  632. if p.nodetype=typeconvn then
  633. begin
  634. hp:=ttypeconvnode(p).left;
  635. ttypeconvnode(p).left:=nil;
  636. p.free;
  637. p:=hp;
  638. end;
  639. { remove addrn which we also don't need here }
  640. if p.nodetype=addrn then
  641. begin
  642. hp:=taddrnode(p).left;
  643. taddrnode(p).left:=nil;
  644. p.free;
  645. p:=hp;
  646. end;
  647. { we now need to have a loadn with a procsym }
  648. if (p.nodetype=loadn) and
  649. (tloadnode(p).symtableentry.typ=procsym) then
  650. begin
  651. curconstSegment.concat(Tai_const_symbol.createname(
  652. tprocsym(tloadnode(p).symtableentry).definition.mangledname));
  653. end
  654. else
  655. Message(cg_e_illegal_expression);
  656. p.free;
  657. end;
  658. { reads a typed constant record }
  659. recorddef:
  660. begin
  661. { KAZ }
  662. if (trecorddef(t.def)=rec_tguid) and
  663. ((token=_CSTRING) or (token=_CCHAR) or (token=_ID)) then
  664. begin
  665. p:=comp_expr(true);
  666. inserttypeconv(p,cshortstringtype);
  667. if p.nodetype=stringconstn then
  668. begin
  669. s:=strpas(tstringconstnode(p).value_str);
  670. p.free;
  671. if string2guid(s,tmpguid) then
  672. begin
  673. curconstSegment.concat(Tai_const.Create_32bit(tmpguid.D1));
  674. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D2));
  675. curconstSegment.concat(Tai_const.Create_16bit(tmpguid.D3));
  676. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  677. curconstSegment.concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  678. end
  679. else
  680. Message(parser_e_improper_guid_syntax);
  681. end
  682. else
  683. begin
  684. p.free;
  685. Message(cg_e_illegal_expression);
  686. exit;
  687. end;
  688. end
  689. else
  690. begin
  691. consume(_LKLAMMER);
  692. aktpos:=0;
  693. srsym := tsym(trecorddef(t.def).symtable.symindex.first);
  694. recsym := nil;
  695. while token<>_RKLAMMER do
  696. begin
  697. s:=pattern;
  698. consume(_ID);
  699. consume(_COLON);
  700. error := false;
  701. recsym := tsym(trecorddef(t.def).symtable.search(s));
  702. if not assigned(recsym) then
  703. begin
  704. Message1(sym_e_illegal_field,s);
  705. error := true;
  706. end;
  707. if not assigned(srsym) or
  708. (s <> srsym.name) then
  709. { possible variant record (JM) }
  710. begin
  711. { All parts of a variant start at the same offset }
  712. { Also allow jumping from one variant part to another, }
  713. { as long as the offsets match }
  714. if (assigned(srsym) and
  715. (tvarsym(recsym).address = tvarsym(srsym).address)) or
  716. { srsym is not assigned after parsing w2 in the }
  717. { typed const in the next example: }
  718. { type tr = record case byte of }
  719. { 1: (l1,l2: dword); }
  720. { 2: (w1,w2: word); }
  721. { end; }
  722. { const r: tr = (w1:1;w2:1;l2:5); }
  723. (tvarsym(recsym).address = aktpos) then
  724. srsym := recsym
  725. { going backwards isn't allowed in any mode }
  726. else if (tvarsym(recsym).address<aktpos) then
  727. begin
  728. Message(parser_e_invalid_record_const);
  729. error := true;
  730. end
  731. { Delphi allows you to skip fields }
  732. else if (m_delphi in aktmodeswitches) then
  733. begin
  734. Message1(parser_w_skipped_fields_before,s);
  735. srsym := recsym;
  736. end
  737. { FPC and TP don't }
  738. else
  739. begin
  740. Message1(parser_e_skipped_fields_before,s);
  741. error := true;
  742. end;
  743. end;
  744. if error then
  745. consume_all_until(_SEMICOLON)
  746. else
  747. begin
  748. { if needed fill (alignment) }
  749. if tvarsym(srsym).address>aktpos then
  750. for i:=1 to tvarsym(srsym).address-aktpos do
  751. curconstSegment.concat(Tai_const.Create_8bit(0));
  752. { new position }
  753. aktpos:=tvarsym(srsym).address+tvarsym(srsym).vartype.def.size;
  754. { read the data }
  755. readtypedconst(tvarsym(srsym).vartype,nil,no_change_allowed);
  756. { keep previous field for checking whether whole }
  757. { record was initialized (JM) }
  758. recsym := srsym;
  759. { goto next field }
  760. srsym := tsym(srsym.indexnext);
  761. if token=_SEMICOLON then
  762. consume(_SEMICOLON)
  763. else break;
  764. end;
  765. end;
  766. { are there any fields left? }
  767. if assigned(srsym) and
  768. { don't complain if there only come other variant parts }
  769. { after the last initialized field }
  770. ((recsym=nil) or
  771. (tvarsym(srsym).address > tvarsym(recsym).address)) then
  772. Message1(parser_h_skipped_fields_after,s);
  773. for i:=1 to t.def.size-aktpos do
  774. curconstSegment.concat(Tai_const.Create_8bit(0));
  775. consume(_RKLAMMER);
  776. end;
  777. end;
  778. { reads a typed object }
  779. objectdef:
  780. begin
  781. if is_class_or_interface(t.def) then
  782. begin
  783. p:=comp_expr(true);
  784. if p.nodetype<>niln then
  785. begin
  786. Message(parser_e_type_const_not_possible);
  787. consume_all_until(_RKLAMMER);
  788. end
  789. else
  790. begin
  791. curconstSegment.concat(Tai_const.Create_32bit(0));
  792. end;
  793. p.free;
  794. end
  795. { for objects we allow it only if it doesn't contain a vmt }
  796. else if (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  797. not(m_tp in aktmodeswitches) then
  798. Message(parser_e_type_const_not_possible)
  799. else
  800. begin
  801. consume(_LKLAMMER);
  802. aktpos:=0;
  803. while token<>_RKLAMMER do
  804. begin
  805. s:=pattern;
  806. consume(_ID);
  807. consume(_COLON);
  808. srsym:=nil;
  809. obj:=tobjectdef(t.def);
  810. symt:=obj.symtable;
  811. while (srsym=nil) and assigned(symt) do
  812. begin
  813. srsym:=tsym(symt.search(s));
  814. if assigned(obj) then
  815. obj:=obj.childof;
  816. if assigned(obj) then
  817. symt:=obj.symtable
  818. else
  819. symt:=nil;
  820. end;
  821. if srsym=nil then
  822. begin
  823. Message1(sym_e_id_not_found,s);
  824. consume_all_until(_SEMICOLON);
  825. end
  826. else
  827. begin
  828. { check position }
  829. if tvarsym(srsym).address<aktpos then
  830. Message(parser_e_invalid_record_const);
  831. { check in VMT needs to be added for TP mode }
  832. if (m_tp in aktmodeswitches) and
  833. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  834. (tobjectdef(t.def).vmt_offset<tvarsym(srsym).address) then
  835. begin
  836. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  837. curconstsegment.concat(tai_const.create_8bit(0));
  838. curconstsegment.concat(tai_const_symbol.createname(tobjectdef(t.def).vmt_mangledname));
  839. { this is more general }
  840. aktpos:=tobjectdef(t.def).vmt_offset + target_info.size_of_pointer;
  841. end;
  842. { if needed fill }
  843. if tvarsym(srsym).address>aktpos then
  844. for i:=1 to tvarsym(srsym).address-aktpos do
  845. curconstSegment.concat(Tai_const.Create_8bit(0));
  846. { new position }
  847. aktpos:=tvarsym(srsym).address+tvarsym(srsym).vartype.def.size;
  848. { read the data }
  849. readtypedconst(tvarsym(srsym).vartype,nil,no_change_allowed);
  850. if token=_SEMICOLON then
  851. consume(_SEMICOLON)
  852. else break;
  853. end;
  854. end;
  855. if (m_tp in aktmodeswitches) and
  856. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  857. (tobjectdef(t.def).vmt_offset>=aktpos) then
  858. begin
  859. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  860. curconstsegment.concat(tai_const.create_8bit(0));
  861. curconstsegment.concat(tai_const_symbol.createname(tobjectdef(t.def).vmt_mangledname));
  862. { this is more general }
  863. aktpos:=tobjectdef(t.def).vmt_offset + target_info.size_of_pointer;
  864. end;
  865. for i:=1 to t.def.size-aktpos do
  866. curconstSegment.concat(Tai_const.Create_8bit(0));
  867. consume(_RKLAMMER);
  868. end;
  869. end;
  870. errordef:
  871. begin
  872. { try to consume something useful }
  873. if token=_LKLAMMER then
  874. consume_all_until(_RKLAMMER)
  875. else
  876. consume_all_until(_SEMICOLON);
  877. end;
  878. else Message(parser_e_type_const_not_possible);
  879. end;
  880. end;
  881. {$ifdef fpc}
  882. {$maxfpuregisters default}
  883. {$endif fpc}
  884. end.
  885. {
  886. $Log$
  887. Revision 1.31 2001-08-26 13:36:47 florian
  888. * some cg reorganisation
  889. * some PPC updates
  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. }