ptconst.pas 43 KB

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