ptconst.pas 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Reads typed constants
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ptconst;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses symtype,symsym;
  21. { this procedure reads typed constants }
  22. { sym is only needed for ansi strings }
  23. { the assembler label is in the middle (PM) }
  24. procedure readtypedconst(const t:ttype;sym : ttypedconstsym;writable : boolean);
  25. implementation
  26. uses
  27. strings,
  28. globtype,systems,tokens,verbose,
  29. cutils,globals,widestr,scanner,
  30. symconst,symbase,symdef,symtable,
  31. aasmbase,aasmtai,aasmcpu,defutil,defcmp,
  32. { pass 1 }
  33. node,htypechk,procinfo,
  34. nmat,nadd,ncal,nmem,nset,ncnv,ninl,ncon,nld,nflw,
  35. { parser specific stuff }
  36. pbase,pexpr,
  37. { codegen }
  38. cpuinfo,cgbase
  39. ;
  40. {$ifdef fpc}
  41. {$maxfpuregisters 0}
  42. {$endif fpc}
  43. { this procedure reads typed constants }
  44. procedure readtypedconst(const t:ttype;sym : ttypedconstsym;writable : boolean);
  45. type
  46. setbytes = array[0..31] of byte;
  47. Psetbytes = ^setbytes;
  48. var
  49. len,base : longint;
  50. p,hp : tnode;
  51. i,j,l,
  52. varalign : longint;
  53. offset,
  54. strlength : aint;
  55. ll : tasmlabel;
  56. s,sorg : 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. intvalue : tconstexprint;
  67. strval : pchar;
  68. pw : pcompilerwidestring;
  69. error : boolean;
  70. old_block_type : tblock_type;
  71. storefilepos : tfileposinfo;
  72. cursectype : TAsmSectionType;
  73. cural : tasmlist;
  74. procedure check_range(def:torddef);
  75. begin
  76. if ((tordconstnode(p).value>def.high) or
  77. (tordconstnode(p).value<def.low)) then
  78. begin
  79. if (cs_check_range in aktlocalswitches) then
  80. Message(parser_e_range_check_error)
  81. else
  82. Message(parser_w_range_check_error);
  83. end;
  84. end;
  85. begin
  86. old_block_type:=block_type;
  87. block_type:=bt_const;
  88. if writable then
  89. begin
  90. cural:=al_typedconsts;
  91. cursectype:=sec_data;
  92. end
  93. else
  94. begin
  95. cural:=al_rotypedconsts;
  96. cursectype:=sec_rodata;
  97. end;
  98. { Add symbol name if this is specified. For array
  99. elements sym=nil and we should skip this }
  100. if assigned(sym) then
  101. begin
  102. storefilepos:=aktfilepos;
  103. aktfilepos:=sym.fileinfo;
  104. { insert cut for smartlinking or alignment }
  105. l:=sym.getsize;
  106. maybe_new_object_file(asmlist[cural]);
  107. new_section(asmlist[cural],cursectype,lower(sym.mangledname),const_align(l));
  108. {$ifdef GDB}
  109. if (cs_debuginfo in aktmoduleswitches) then
  110. sym.concatstabto(asmlist[cural]);
  111. {$endif GDB}
  112. if (sym.owner.symtabletype=globalsymtable) or
  113. maybe_smartlink_symbol or
  114. (assigned(current_procinfo) and
  115. (po_inline in current_procinfo.procdef.procoptions)) or
  116. DLLSource then
  117. asmlist[cural].concat(Tai_symbol.Createname_global(sym.mangledname,AT_DATA,l))
  118. else
  119. asmlist[cural].concat(Tai_symbol.Createname(sym.mangledname,AT_DATA,l));
  120. aktfilepos:=storefilepos;
  121. end;
  122. case t.def.deftype of
  123. orddef:
  124. begin
  125. p:=comp_expr(true);
  126. case torddef(t.def).typ of
  127. bool8bit :
  128. begin
  129. if is_constboolnode(p) then
  130. asmlist[cural].concat(Tai_const.Create_8bit(byte(tordconstnode(p).value)))
  131. else
  132. Message(parser_e_illegal_expression);
  133. end;
  134. bool16bit :
  135. begin
  136. if is_constboolnode(p) then
  137. asmlist[cural].concat(Tai_const.Create_16bit(word(tordconstnode(p).value)))
  138. else
  139. Message(parser_e_illegal_expression);
  140. end;
  141. bool32bit :
  142. begin
  143. if is_constboolnode(p) then
  144. asmlist[cural].concat(Tai_const.Create_32bit(longint(tordconstnode(p).value)))
  145. else
  146. Message(parser_e_illegal_expression);
  147. end;
  148. uchar :
  149. begin
  150. if is_constcharnode(p) then
  151. asmlist[cural].concat(Tai_const.Create_8bit(byte(tordconstnode(p).value)))
  152. else
  153. Message(parser_e_illegal_expression);
  154. end;
  155. uwidechar :
  156. begin
  157. if is_constcharnode(p) then
  158. inserttypeconv(p,cwidechartype);
  159. if is_constwidecharnode(p) then
  160. asmlist[cural].concat(Tai_const.Create_16bit(word(tordconstnode(p).value)))
  161. else
  162. Message(parser_e_illegal_expression);
  163. end;
  164. s8bit,
  165. u8bit :
  166. begin
  167. if is_constintnode(p) then
  168. begin
  169. asmlist[cural].concat(Tai_const.Create_8bit(byte(tordconstnode(p).value)));
  170. check_range(torddef(t.def));
  171. end
  172. else
  173. Message(parser_e_illegal_expression);
  174. end;
  175. u16bit,
  176. s16bit :
  177. begin
  178. if is_constintnode(p) then
  179. begin
  180. asmlist[cural].concat(Tai_const.Create_16bit(word(tordconstnode(p).value)));
  181. check_range(torddef(t.def));
  182. end
  183. else
  184. Message(parser_e_illegal_expression);
  185. end;
  186. s32bit,
  187. u32bit :
  188. begin
  189. if is_constintnode(p) then
  190. begin
  191. asmlist[cural].concat(Tai_const.Create_32bit(longint(tordconstnode(p).value)));
  192. if torddef(t.def).typ<>u32bit then
  193. check_range(torddef(t.def));
  194. end
  195. else
  196. Message(parser_e_illegal_expression);
  197. end;
  198. s64bit,
  199. u64bit,
  200. scurrency:
  201. begin
  202. if is_constintnode(p) then
  203. intvalue := tordconstnode(p).value
  204. else if is_constrealnode(p) and
  205. (torddef(t.def).typ=scurrency)
  206. { allow bootstrapping }
  207. then
  208. begin
  209. intvalue:=round(trealconstnode(p).value_real*10000);
  210. end
  211. else
  212. begin
  213. intvalue:=0;
  214. Message(parser_e_illegal_expression);
  215. end;
  216. asmlist[cural].concat(Tai_const.Create_64bit(intvalue));
  217. end;
  218. else
  219. internalerror(3799);
  220. end;
  221. p.free;
  222. end;
  223. floatdef:
  224. begin
  225. p:=comp_expr(true);
  226. if is_constrealnode(p) then
  227. value:=trealconstnode(p).value_real
  228. else if is_constintnode(p) then
  229. value:=tordconstnode(p).value
  230. else
  231. Message(parser_e_illegal_expression);
  232. case tfloatdef(t.def).typ of
  233. s32real :
  234. asmlist[cural].concat(Tai_real_32bit.Create(ts32real(value)));
  235. s64real :
  236. {$ifdef ARM}
  237. if aktfputype in [fpu_fpa,fpu_fpa10,fpu_fpa11] then
  238. asmlist[cural].concat(Tai_real_64bit.Create_hiloswapped(ts64real(value)))
  239. else
  240. {$endif ARM}
  241. asmlist[cural].concat(Tai_real_64bit.Create(ts64real(value)));
  242. s80real :
  243. asmlist[cural].concat(Tai_real_80bit.Create(value));
  244. { the round is necessary for native compilers where comp isn't a float }
  245. s64comp :
  246. asmlist[cural].concat(Tai_comp_64bit.Create(round(value)));
  247. s64currency:
  248. asmlist[cural].concat(Tai_comp_64bit.Create(round(value*10000)));
  249. s128real:
  250. asmlist[cural].concat(Tai_real_128bit.Create(value));
  251. else
  252. internalerror(18);
  253. end;
  254. p.free;
  255. end;
  256. classrefdef:
  257. begin
  258. p:=comp_expr(true);
  259. case p.nodetype of
  260. loadvmtaddrn:
  261. with Tclassrefdef(p.resulttype.def) do
  262. begin
  263. if not Tobjectdef(pointertype.def).is_related(Tobjectdef(pointertype.def)) then
  264. message(parser_e_illegal_expression);
  265. asmlist[cural].concat(Tai_const.Create_sym(objectlibrary.newasmsymbol(
  266. Tobjectdef(pointertype.def).vmt_mangledname,AB_EXTERNAL,AT_DATA)));
  267. end;
  268. niln:
  269. asmlist[cural].concat(Tai_const.Create_sym(nil));
  270. else Message(parser_e_illegal_expression);
  271. end;
  272. p.free;
  273. end;
  274. pointerdef:
  275. begin
  276. p:=comp_expr(true);
  277. if (p.nodetype=typeconvn) then
  278. with Ttypeconvnode(p) do
  279. if (left.nodetype in [addrn,niln]) and equal_defs(t.def,p.resulttype.def) then
  280. begin
  281. hp:=left;
  282. left:=nil;
  283. p.free;
  284. p:=hp;
  285. end;
  286. { allows horrible ofs(typeof(TButton)^) code !! }
  287. if (p.nodetype=addrn) then
  288. with Taddrnode(p) do
  289. if left.nodetype=derefn then
  290. begin
  291. hp:=tderefnode(left).left;
  292. tderefnode(left).left:=nil;
  293. p.free;
  294. p:=hp;
  295. end;
  296. { const pointer ? }
  297. if (p.nodetype = pointerconstn) then
  298. begin
  299. if sizeof(TConstPtrUInt)=8 then
  300. asmlist[cural].concat(Tai_const.Create_64bit(TConstPtrUInt(tpointerconstnode(p).value)))
  301. else
  302. if sizeof(TConstPtrUInt)=4 then
  303. asmlist[cural].concat(Tai_const.Create_32bit(TConstPtrUInt(tpointerconstnode(p).value)))
  304. else
  305. internalerror(200404122);
  306. end
  307. { nil pointer ? }
  308. else if p.nodetype=niln then
  309. asmlist[cural].concat(Tai_const.Create_sym(nil))
  310. { maybe pchar ? }
  311. else
  312. if is_char(tpointerdef(t.def).pointertype.def) and
  313. (p.nodetype<>addrn) then
  314. begin
  315. objectlibrary.getdatalabel(ll);
  316. asmlist[cural].concat(Tai_const.Create_sym(ll));
  317. if p.nodetype=stringconstn then
  318. varalign:=size_2_align(tstringconstnode(p).len)
  319. else
  320. varalign:=0;
  321. varalign:=const_align(varalign);
  322. asmlist[al_rodata].concat(Tai_align.Create(varalign));
  323. asmlist[al_rodata].concat(Tai_label.Create(ll));
  324. if p.nodetype=stringconstn then
  325. begin
  326. len:=tstringconstnode(p).len;
  327. { For tp7 the maximum lentgh can be 255 }
  328. if (m_tp7 in aktmodeswitches) and
  329. (len>255) then
  330. len:=255;
  331. getmem(ca,len+2);
  332. move(tstringconstnode(p).value_str^,ca^,len+1);
  333. asmlist[al_rodata].concat(Tai_string.Create_length_pchar(ca,len+1));
  334. end
  335. else
  336. if is_constcharnode(p) then
  337. asmlist[al_rodata].concat(Tai_string.Create(char(byte(tordconstnode(p).value))+#0))
  338. else
  339. message(parser_e_illegal_expression);
  340. end
  341. { maybe pwidechar ? }
  342. else
  343. if is_widechar(tpointerdef(t.def).pointertype.def) and
  344. (p.nodetype<>addrn) then
  345. begin
  346. objectlibrary.getdatalabel(ll);
  347. asmlist[cural].concat(Tai_const.Create_sym(ll));
  348. asmlist[al_typedconsts].concat(tai_align.create(const_align(sizeof(aint))));
  349. asmlist[al_typedconsts].concat(Tai_label.Create(ll));
  350. if (p.nodetype in [stringconstn,ordconstn]) then
  351. begin
  352. { convert to widestring stringconstn }
  353. inserttypeconv(p,cwidestringtype);
  354. if (p.nodetype=stringconstn) and
  355. (tstringconstnode(p).st_type=st_widestring) then
  356. begin
  357. pw:=pcompilerwidestring(tstringconstnode(p).value_str);
  358. for i:=0 to tstringconstnode(p).len-1 do
  359. asmlist[al_typedconsts].concat(Tai_const.Create_16bit(pw^.data[i]));
  360. { ending #0 }
  361. asmlist[al_typedconsts].concat(Tai_const.Create_16bit(0))
  362. end;
  363. end
  364. else
  365. Message(parser_e_illegal_expression);
  366. end
  367. else
  368. if (p.nodetype=addrn) or
  369. is_procvar_load(p) then
  370. begin
  371. { insert typeconv }
  372. inserttypeconv(p,t);
  373. hp:=p;
  374. while assigned(hp) and (hp.nodetype in [addrn,typeconvn,subscriptn,vecn]) do
  375. hp:=tunarynode(hp).left;
  376. if (hp.nodetype=loadn) then
  377. begin
  378. hp:=p;
  379. offset:=0;
  380. while assigned(hp) and (hp.nodetype<>loadn) do
  381. begin
  382. case hp.nodetype of
  383. vecn :
  384. begin
  385. case tvecnode(hp).left.resulttype.def.deftype of
  386. stringdef :
  387. begin
  388. { this seems OK for shortstring and ansistrings PM }
  389. { it is wrong for widestrings !! }
  390. len:=1;
  391. base:=0;
  392. end;
  393. arraydef :
  394. begin
  395. len:=tarraydef(tvecnode(hp).left.resulttype.def).elesize;
  396. base:=tarraydef(tvecnode(hp).left.resulttype.def).lowrange;
  397. end
  398. else
  399. Message(parser_e_illegal_expression);
  400. end;
  401. if is_constintnode(tvecnode(hp).right) then
  402. inc(offset,len*(get_ordinal_value(tvecnode(hp).right)-base))
  403. else
  404. Message(parser_e_illegal_expression);
  405. end;
  406. subscriptn :
  407. inc(offset,tsubscriptnode(hp).vs.fieldoffset);
  408. typeconvn :
  409. begin
  410. if not(ttypeconvnode(hp).convtype in [tc_equal,tc_proc_2_procvar]) then
  411. Message(parser_e_illegal_expression);
  412. end;
  413. addrn :
  414. ;
  415. else
  416. Message(parser_e_illegal_expression);
  417. end;
  418. hp:=tunarynode(hp).left;
  419. end;
  420. srsym:=tloadnode(hp).symtableentry;
  421. case srsym.typ of
  422. procsym :
  423. begin
  424. if Tprocsym(srsym).procdef_count>1 then
  425. Message(parser_e_no_overloaded_procvars);
  426. if po_abstractmethod in tprocsym(srsym).first_procdef.procoptions then
  427. Message(type_e_cant_take_address_of_abstract_method)
  428. else
  429. asmlist[cural].concat(Tai_const.Createname(tprocsym(srsym).first_procdef.mangledname,AT_FUNCTION,offset));
  430. end;
  431. globalvarsym :
  432. asmlist[cural].concat(Tai_const.Createname(tglobalvarsym(srsym).mangledname,AT_DATA,offset));
  433. typedconstsym :
  434. asmlist[cural].concat(Tai_const.Createname(ttypedconstsym(srsym).mangledname,AT_DATA,offset));
  435. labelsym :
  436. asmlist[cural].concat(Tai_const.Createname(tlabelsym(srsym).mangledname,AT_FUNCTION,offset));
  437. constsym :
  438. if tconstsym(srsym).consttyp=constresourcestring then
  439. asmlist[cural].concat(Tai_const.Createname(make_mangledname('RESOURCESTRINGLIST',tconstsym(srsym).owner,''),AT_DATA,tconstsym(srsym).resstrindex*(4+sizeof(aint)*3)+4+sizeof(aint)))
  440. else
  441. Message(type_e_variable_id_expected);
  442. else
  443. Message(type_e_variable_id_expected);
  444. end;
  445. end
  446. else
  447. Message(parser_e_illegal_expression);
  448. end
  449. else
  450. { allow typeof(Object type)}
  451. if (p.nodetype=inlinen) and
  452. (tinlinenode(p).inlinenumber=in_typeof_x) then
  453. begin
  454. if (tinlinenode(p).left.nodetype=typen) then
  455. begin
  456. asmlist[cural].concat(Tai_const.createname(
  457. tobjectdef(tinlinenode(p).left.resulttype.def).vmt_mangledname,AT_DATA,0));
  458. end
  459. else
  460. Message(parser_e_illegal_expression);
  461. end
  462. else
  463. Message(parser_e_illegal_expression);
  464. p.free;
  465. end;
  466. setdef:
  467. begin
  468. p:=comp_expr(true);
  469. if p.nodetype=setconstn then
  470. begin
  471. { be sure to convert to the correct result, else
  472. it can generate smallset data instead of normalset (PFV) }
  473. inserttypeconv(p,t);
  474. { we only allow const sets }
  475. if assigned(tsetconstnode(p).left) then
  476. Message(parser_e_illegal_expression)
  477. else
  478. begin
  479. { this writing is endian independant }
  480. { untrue - because they are considered }
  481. { arrays of 32-bit values CEC }
  482. if source_info.endian = target_info.endian then
  483. begin
  484. for l:=0 to p.resulttype.def.size-1 do
  485. asmlist[cural].concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[l]));
  486. end
  487. else
  488. begin
  489. { store as longint values in swaped format }
  490. j:=0;
  491. for l:=0 to ((p.resulttype.def.size-1) div 4) do
  492. begin
  493. asmlist[cural].concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+3]));
  494. asmlist[cural].concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+2]));
  495. asmlist[cural].concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j+1]));
  496. asmlist[cural].concat(tai_const.create_8bit(Psetbytes(tsetconstnode(p).value_set)^[j]));
  497. Inc(j,4);
  498. end;
  499. end;
  500. end;
  501. end
  502. else
  503. Message(parser_e_illegal_expression);
  504. p.free;
  505. end;
  506. enumdef:
  507. begin
  508. p:=comp_expr(true);
  509. if p.nodetype=ordconstn then
  510. begin
  511. if equal_defs(p.resulttype.def,t.def) or
  512. is_subequal(p.resulttype.def,t.def) then
  513. begin
  514. case longint(p.resulttype.def.size) of
  515. 1 : asmlist[cural].concat(Tai_const.Create_8bit(Byte(tordconstnode(p).value)));
  516. 2 : asmlist[cural].concat(Tai_const.Create_16bit(Word(tordconstnode(p).value)));
  517. 4 : asmlist[cural].concat(Tai_const.Create_32bit(Longint(tordconstnode(p).value)));
  518. end;
  519. end
  520. else
  521. IncompatibleTypes(p.resulttype.def,t.def);
  522. end
  523. else
  524. Message(parser_e_illegal_expression);
  525. p.free;
  526. end;
  527. stringdef:
  528. begin
  529. p:=comp_expr(true);
  530. { load strval and strlength of the constant tree }
  531. if (p.nodetype=stringconstn) or is_widestring(t.def) then
  532. begin
  533. { convert to the expected string type so that
  534. for widestrings strval is a pcompilerwidestring }
  535. inserttypeconv(p,t);
  536. strlength:=tstringconstnode(p).len;
  537. strval:=tstringconstnode(p).value_str;
  538. end
  539. else if is_constcharnode(p) then
  540. begin
  541. { strval:=pchar(@tordconstnode(p).value);
  542. THIS FAIL on BIG_ENDIAN MACHINES PM }
  543. c:=chr(tordconstnode(p).value and $ff);
  544. strval:=@c;
  545. strlength:=1
  546. end
  547. else if is_constresourcestringnode(p) then
  548. begin
  549. strval:=pchar(tconstsym(tloadnode(p).symtableentry).value.valueptr);
  550. strlength:=tconstsym(tloadnode(p).symtableentry).value.len;
  551. end
  552. else
  553. begin
  554. Message(parser_e_illegal_expression);
  555. strlength:=-1;
  556. end;
  557. if strlength>=0 then
  558. begin
  559. case tstringdef(t.def).string_typ of
  560. st_shortstring:
  561. begin
  562. if strlength>=t.def.size then
  563. begin
  564. message2(parser_w_string_too_long,strpas(strval),tostr(t.def.size-1));
  565. strlength:=t.def.size-1;
  566. end;
  567. asmlist[cural].concat(Tai_const.Create_8bit(strlength));
  568. { this can also handle longer strings }
  569. getmem(ca,strlength+1);
  570. move(strval^,ca^,strlength);
  571. ca[strlength]:=#0;
  572. asmlist[cural].concat(Tai_string.Create_length_pchar(ca,strlength));
  573. { fillup with spaces if size is shorter }
  574. if t.def.size>strlength then
  575. begin
  576. getmem(ca,t.def.size-strlength);
  577. { def.size contains also the leading length, so we }
  578. { we have to subtract one }
  579. fillchar(ca[0],t.def.size-strlength-1,' ');
  580. ca[t.def.size-strlength-1]:=#0;
  581. { this can also handle longer strings }
  582. asmlist[cural].concat(Tai_string.Create_length_pchar(ca,t.def.size-strlength-1));
  583. end;
  584. end;
  585. st_ansistring:
  586. begin
  587. { an empty ansi string is nil! }
  588. if (strlength=0) then
  589. asmlist[cural].concat(Tai_const.Create_sym(nil))
  590. else
  591. begin
  592. objectlibrary.getdatalabel(ll);
  593. asmlist[cural].concat(Tai_const.Create_sym(ll));
  594. asmlist[al_rodata].concat(tai_align.create(const_align(sizeof(aint))));
  595. asmlist[al_rodata].concat(Tai_const.Create_aint(-1));
  596. asmlist[al_rodata].concat(Tai_const.Create_aint(strlength));
  597. asmlist[al_rodata].concat(Tai_label.Create(ll));
  598. getmem(ca,strlength+2);
  599. move(strval^,ca^,strlength);
  600. { The terminating #0 to be stored in the .data section (JM) }
  601. ca[strlength]:=#0;
  602. { End of the PChar. The memory has to be allocated because in }
  603. { tai_string.done, there is a freemem(len+1) (JM) }
  604. ca[strlength+1]:=#0;
  605. asmlist[al_rodata].concat(Tai_string.Create_length_pchar(ca,strlength+1));
  606. end;
  607. end;
  608. st_widestring:
  609. begin
  610. { an empty ansi string is nil! }
  611. if (strlength=0) then
  612. asmlist[cural].concat(Tai_const.Create_sym(nil))
  613. else
  614. begin
  615. objectlibrary.getdatalabel(ll);
  616. asmlist[cural].concat(Tai_const.Create_sym(ll));
  617. asmlist[al_rodata].concat(tai_align.create(const_align(sizeof(aint))));
  618. asmlist[al_rodata].concat(Tai_const.Create_aint(-1));
  619. asmlist[al_rodata].concat(Tai_const.Create_aint(strlength));
  620. asmlist[al_rodata].concat(Tai_label.Create(ll));
  621. for i:=0 to strlength-1 do
  622. asmlist[al_rodata].concat(Tai_const.Create_16bit(pcompilerwidestring(strval)^.data[i]));
  623. { ending #0 }
  624. asmlist[al_rodata].concat(Tai_const.Create_16bit(0))
  625. end;
  626. end;
  627. st_longstring:
  628. begin
  629. internalerror(200107081);
  630. end;
  631. end;
  632. end;
  633. p.free;
  634. end;
  635. arraydef:
  636. begin
  637. { dynamic array nil }
  638. if is_dynamic_array(t.def) then
  639. begin
  640. { Only allow nil initialization }
  641. consume(_NIL);
  642. asmlist[cural].concat(Tai_const.Create_sym(nil));
  643. end
  644. else
  645. if try_to_consume(_LKLAMMER) then
  646. begin
  647. for l:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange-1 do
  648. begin
  649. readtypedconst(tarraydef(t.def).elementtype,nil,writable);
  650. consume(_COMMA);
  651. end;
  652. readtypedconst(tarraydef(t.def).elementtype,nil,writable);
  653. consume(_RKLAMMER);
  654. end
  655. else
  656. { if array of char then we allow also a string }
  657. if is_char(tarraydef(t.def).elementtype.def) then
  658. begin
  659. p:=comp_expr(true);
  660. if p.nodetype=stringconstn then
  661. begin
  662. len:=tstringconstnode(p).len;
  663. { For tp7 the maximum lentgh can be 255 }
  664. if (m_tp7 in aktmodeswitches) and
  665. (len>255) then
  666. len:=255;
  667. ca:=tstringconstnode(p).value_str;
  668. end
  669. else
  670. if is_constcharnode(p) then
  671. begin
  672. c:=chr(tordconstnode(p).value and $ff);
  673. ca:=@c;
  674. len:=1;
  675. end
  676. else
  677. begin
  678. Message(parser_e_illegal_expression);
  679. len:=0;
  680. end;
  681. if len>(tarraydef(t.def).highrange-tarraydef(t.def).lowrange+1) then
  682. Message(parser_e_string_larger_array);
  683. for i:=tarraydef(t.def).lowrange to tarraydef(t.def).highrange do
  684. begin
  685. if i+1-tarraydef(t.def).lowrange<=len then
  686. begin
  687. asmlist[cural].concat(Tai_const.Create_8bit(byte(ca^)));
  688. inc(ca);
  689. end
  690. else
  691. {Fill the remaining positions with #0.}
  692. asmlist[cural].concat(Tai_const.Create_8bit(0));
  693. end;
  694. p.free;
  695. end
  696. else
  697. begin
  698. { we want the ( }
  699. consume(_LKLAMMER);
  700. end;
  701. end;
  702. procvardef:
  703. begin
  704. { Procvars and pointers are no longer compatible. }
  705. { under tp: =nil or =var under fpc: =nil or =@var }
  706. if token=_NIL then
  707. begin
  708. asmlist[cural].concat(Tai_const.Create_sym(nil));
  709. if (po_methodpointer in tprocvardef(t.def).procoptions) then
  710. asmlist[cural].concat(Tai_const.Create_sym(nil));
  711. consume(_NIL);
  712. exit;
  713. end;
  714. { you can't assign a value other than NIL to a typed constant }
  715. { which is a "procedure of object", because this also requires }
  716. { address of an object/class instance, which is not known at }
  717. { compile time (JM) }
  718. if (po_methodpointer in tprocvardef(t.def).procoptions) then
  719. Message(parser_e_no_procvarobj_const);
  720. { parse the rest too, so we can continue with error checking }
  721. getprocvardef:=tprocvardef(t.def);
  722. p:=comp_expr(true);
  723. getprocvardef:=nil;
  724. if codegenerror then
  725. begin
  726. p.free;
  727. exit;
  728. end;
  729. { let type conversion check everything needed }
  730. inserttypeconv(p,t);
  731. if codegenerror then
  732. begin
  733. p.free;
  734. exit;
  735. end;
  736. { remove typeconvs, that will normally insert a lea
  737. instruction which is not necessary for us }
  738. while p.nodetype=typeconvn do
  739. begin
  740. hp:=ttypeconvnode(p).left;
  741. ttypeconvnode(p).left:=nil;
  742. p.free;
  743. p:=hp;
  744. end;
  745. { remove addrn which we also don't need here }
  746. if p.nodetype=addrn then
  747. begin
  748. hp:=taddrnode(p).left;
  749. taddrnode(p).left:=nil;
  750. p.free;
  751. p:=hp;
  752. end;
  753. { we now need to have a loadn with a procsym }
  754. if (p.nodetype=loadn) and
  755. (tloadnode(p).symtableentry.typ=procsym) then
  756. begin
  757. asmlist[cural].concat(Tai_const.createname(
  758. tprocsym(tloadnode(p).symtableentry).first_procdef.mangledname,AT_FUNCTION,0));
  759. end
  760. else
  761. Message(parser_e_illegal_expression);
  762. p.free;
  763. end;
  764. { reads a typed constant record }
  765. recorddef:
  766. begin
  767. { KAZ }
  768. if (trecorddef(t.def)=rec_tguid) and
  769. ((token=_CSTRING) or (token=_CCHAR) or (token=_ID)) then
  770. begin
  771. p:=comp_expr(true);
  772. inserttypeconv(p,cshortstringtype);
  773. if p.nodetype=stringconstn then
  774. begin
  775. s:=strpas(tstringconstnode(p).value_str);
  776. p.free;
  777. if string2guid(s,tmpguid) then
  778. begin
  779. asmlist[cural].concat(Tai_const.Create_32bit(longint(tmpguid.D1)));
  780. asmlist[cural].concat(Tai_const.Create_16bit(tmpguid.D2));
  781. asmlist[cural].concat(Tai_const.Create_16bit(tmpguid.D3));
  782. for i:=Low(tmpguid.D4) to High(tmpguid.D4) do
  783. asmlist[cural].concat(Tai_const.Create_8bit(tmpguid.D4[i]));
  784. end
  785. else
  786. Message(parser_e_improper_guid_syntax);
  787. end
  788. else
  789. begin
  790. p.free;
  791. Message(parser_e_illegal_expression);
  792. exit;
  793. end;
  794. end
  795. else
  796. begin
  797. consume(_LKLAMMER);
  798. sorg:='';
  799. aktpos:=0;
  800. srsym := tsym(trecorddef(t.def).symtable.symindex.first);
  801. recsym := nil;
  802. while token<>_RKLAMMER do
  803. begin
  804. s:=pattern;
  805. sorg:=orgpattern;
  806. consume(_ID);
  807. consume(_COLON);
  808. error := false;
  809. recsym := tsym(trecorddef(t.def).symtable.search(s));
  810. if not assigned(recsym) then
  811. begin
  812. Message1(sym_e_illegal_field,sorg);
  813. error := true;
  814. end;
  815. if (not error) and
  816. (not assigned(srsym) or
  817. (s <> srsym.name)) then
  818. { possible variant record (JM) }
  819. begin
  820. { All parts of a variant start at the same offset }
  821. { Also allow jumping from one variant part to another, }
  822. { as long as the offsets match }
  823. if (assigned(srsym) and
  824. (tfieldvarsym(recsym).fieldoffset = tfieldvarsym(srsym).fieldoffset)) or
  825. { srsym is not assigned after parsing w2 in the }
  826. { typed const in the next example: }
  827. { type tr = record case byte of }
  828. { 1: (l1,l2: dword); }
  829. { 2: (w1,w2: word); }
  830. { end; }
  831. { const r: tr = (w1:1;w2:1;l2:5); }
  832. (tfieldvarsym(recsym).fieldoffset = aktpos) then
  833. srsym := recsym
  834. { going backwards isn't allowed in any mode }
  835. else if (tfieldvarsym(recsym).fieldoffset<aktpos) then
  836. begin
  837. Message(parser_e_invalid_record_const);
  838. error := true;
  839. end
  840. { Delphi allows you to skip fields }
  841. else if (m_delphi in aktmodeswitches) then
  842. begin
  843. Message1(parser_w_skipped_fields_before,sorg);
  844. srsym := recsym;
  845. end
  846. { FPC and TP don't }
  847. else
  848. begin
  849. Message1(parser_e_skipped_fields_before,sorg);
  850. error := true;
  851. end;
  852. end;
  853. if error then
  854. consume_all_until(_SEMICOLON)
  855. else
  856. begin
  857. { if needed fill (alignment) }
  858. if tfieldvarsym(srsym).fieldoffset>aktpos then
  859. for i:=1 to tfieldvarsym(srsym).fieldoffset-aktpos do
  860. asmlist[cural].concat(Tai_const.Create_8bit(0));
  861. { new position }
  862. aktpos:=tfieldvarsym(srsym).fieldoffset+tfieldvarsym(srsym).vartype.def.size;
  863. { read the data }
  864. readtypedconst(tfieldvarsym(srsym).vartype,nil,writable);
  865. { keep previous field for checking whether whole }
  866. { record was initialized (JM) }
  867. recsym := srsym;
  868. { goto next field }
  869. srsym := tsym(srsym.indexnext);
  870. if token=_SEMICOLON then
  871. consume(_SEMICOLON)
  872. else break;
  873. end;
  874. end;
  875. { are there any fields left? }
  876. if assigned(srsym) and
  877. { don't complain if there only come other variant parts }
  878. { after the last initialized field }
  879. ((recsym=nil) or
  880. (tfieldvarsym(srsym).fieldoffset > tfieldvarsym(recsym).fieldoffset)) then
  881. Message1(parser_w_skipped_fields_after,sorg);
  882. for i:=1 to t.def.size-aktpos do
  883. asmlist[cural].concat(Tai_const.Create_8bit(0));
  884. consume(_RKLAMMER);
  885. end;
  886. end;
  887. { reads a typed object }
  888. objectdef:
  889. begin
  890. if is_class_or_interface(t.def) then
  891. begin
  892. p:=comp_expr(true);
  893. if p.nodetype<>niln then
  894. begin
  895. Message(parser_e_type_const_not_possible);
  896. consume_all_until(_RKLAMMER);
  897. end
  898. else
  899. begin
  900. asmlist[cural].concat(Tai_const.Create_sym(nil));
  901. end;
  902. p.free;
  903. end
  904. { for objects we allow it only if it doesn't contain a vmt }
  905. else if (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  906. (m_fpc in aktmodeswitches) then
  907. Message(parser_e_type_const_not_possible)
  908. else
  909. begin
  910. consume(_LKLAMMER);
  911. aktpos:=0;
  912. while token<>_RKLAMMER do
  913. begin
  914. s:=pattern;
  915. sorg:=orgpattern;
  916. consume(_ID);
  917. consume(_COLON);
  918. srsym:=nil;
  919. obj:=tobjectdef(t.def);
  920. symt:=obj.symtable;
  921. while (srsym=nil) and assigned(symt) do
  922. begin
  923. srsym:=tsym(symt.search(s));
  924. if assigned(obj) then
  925. obj:=obj.childof;
  926. if assigned(obj) then
  927. symt:=obj.symtable
  928. else
  929. symt:=nil;
  930. end;
  931. if srsym=nil then
  932. begin
  933. Message1(sym_e_id_not_found,sorg);
  934. consume_all_until(_SEMICOLON);
  935. end
  936. else
  937. with tfieldvarsym(srsym) do
  938. begin
  939. { check position }
  940. if fieldoffset<aktpos then
  941. message(parser_e_invalid_record_const);
  942. { check in VMT needs to be added for TP mode }
  943. with Tobjectdef(t.def) do
  944. if not(m_fpc in aktmodeswitches) and
  945. (oo_has_vmt in objectoptions) and
  946. (vmt_offset<fieldoffset) then
  947. begin
  948. for i:=1 to vmt_offset-aktpos do
  949. asmlist[cural].concat(tai_const.create_8bit(0));
  950. asmlist[cural].concat(tai_const.createname(vmt_mangledname,AT_DATA,0));
  951. { this is more general }
  952. aktpos:=vmt_offset + sizeof(aint);
  953. end;
  954. { if needed fill }
  955. if fieldoffset>aktpos then
  956. for i:=1 to fieldoffset-aktpos do
  957. asmlist[cural].concat(Tai_const.Create_8bit(0));
  958. { new position }
  959. aktpos:=fieldoffset+vartype.def.size;
  960. { read the data }
  961. readtypedconst(vartype,nil,writable);
  962. if token=_SEMICOLON then
  963. consume(_SEMICOLON)
  964. else break;
  965. end;
  966. end;
  967. if not(m_fpc in aktmodeswitches) and
  968. (oo_has_vmt in tobjectdef(t.def).objectoptions) and
  969. (tobjectdef(t.def).vmt_offset>=aktpos) then
  970. begin
  971. for i:=1 to tobjectdef(t.def).vmt_offset-aktpos do
  972. asmlist[cural].concat(tai_const.create_8bit(0));
  973. asmlist[cural].concat(tai_const.createname(tobjectdef(t.def).vmt_mangledname,AT_DATA,0));
  974. { this is more general }
  975. aktpos:=tobjectdef(t.def).vmt_offset + sizeof(aint);
  976. end;
  977. for i:=1 to t.def.size-aktpos do
  978. asmlist[cural].concat(Tai_const.Create_8bit(0));
  979. consume(_RKLAMMER);
  980. end;
  981. end;
  982. errordef:
  983. begin
  984. { try to consume something useful }
  985. if token=_LKLAMMER then
  986. consume_all_until(_RKLAMMER)
  987. else
  988. consume_all_until(_SEMICOLON);
  989. end;
  990. else Message(parser_e_type_const_not_possible);
  991. end;
  992. block_type:=old_block_type;
  993. end;
  994. {$ifdef fpc}
  995. {$maxfpuregisters default}
  996. {$endif fpc}
  997. end.