ptconst.pas 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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. srsym:=tsym(def.symtable.SymList[symidx]);
  775. recsym := nil;
  776. while token<>_RKLAMMER do
  777. begin
  778. s:=pattern;
  779. sorg:=orgpattern;
  780. consume(_ID);
  781. consume(_COLON);
  782. error := false;
  783. recsym := tsym(def.symtable.Find(s));
  784. if not assigned(recsym) then
  785. begin
  786. Message1(sym_e_illegal_field,sorg);
  787. error := true;
  788. end;
  789. if (not error) and
  790. (not assigned(srsym) or
  791. (s <> srsym.name)) then
  792. { possible variant record (JM) }
  793. begin
  794. { All parts of a variant start at the same offset }
  795. { Also allow jumping from one variant part to another, }
  796. { as long as the offsets match }
  797. if (assigned(srsym) and
  798. (tfieldvarsym(recsym).fieldoffset = tfieldvarsym(srsym).fieldoffset)) or
  799. { srsym is not assigned after parsing w2 in the }
  800. { typed const in the next example: }
  801. { type tr = record case byte of }
  802. { 1: (l1,l2: dword); }
  803. { 2: (w1,w2: word); }
  804. { end; }
  805. { const r: tr = (w1:1;w2:1;l2:5); }
  806. (tfieldvarsym(recsym).fieldoffset = curroffset) then
  807. srsym := recsym
  808. { going backwards isn't allowed in any mode }
  809. else if (tfieldvarsym(recsym).fieldoffset<curroffset) then
  810. begin
  811. Message(parser_e_invalid_record_const);
  812. error := true;
  813. end
  814. { Delphi allows you to skip fields }
  815. else if (m_delphi in current_settings.modeswitches) then
  816. begin
  817. Message1(parser_w_skipped_fields_before,sorg);
  818. srsym := recsym;
  819. end
  820. { FPC and TP don't }
  821. else
  822. begin
  823. Message1(parser_e_skipped_fields_before,sorg);
  824. error := true;
  825. end;
  826. end;
  827. if error then
  828. consume_all_until(_SEMICOLON)
  829. else
  830. begin
  831. { if needed fill (alignment) }
  832. if tfieldvarsym(srsym).fieldoffset>curroffset then
  833. for i:=1 to tfieldvarsym(srsym).fieldoffset-curroffset do
  834. list.concat(Tai_const.Create_8bit(0));
  835. { new position }
  836. curroffset:=tfieldvarsym(srsym).fieldoffset+tfieldvarsym(srsym).vardef.size;
  837. { read the data }
  838. read_typed_const_data(list,tfieldvarsym(srsym).vardef);
  839. { keep previous field for checking whether whole }
  840. { record was initialized (JM) }
  841. recsym := srsym;
  842. { goto next field }
  843. inc(symidx);
  844. if symidx<def.symtable.SymList.Count then
  845. srsym:=tsym(def.symtable.SymList[symidx])
  846. else
  847. srsym:=nil;
  848. if token=_SEMICOLON then
  849. consume(_SEMICOLON)
  850. else break;
  851. end;
  852. end;
  853. { are there any fields left, but don't complain if there only
  854. come other variant partsa fter the last initialized field }
  855. if assigned(srsym) and
  856. (
  857. (recsym=nil) or
  858. (tfieldvarsym(srsym).fieldoffset > tfieldvarsym(recsym).fieldoffset)
  859. ) then
  860. Message1(parser_w_skipped_fields_after,sorg);
  861. for i:=1 to def.size-curroffset do
  862. list.concat(Tai_const.Create_8bit(0));
  863. consume(_RKLAMMER);
  864. end;
  865. procedure parse_objectdef(list:tasmlist;def:tobjectdef);
  866. var
  867. n : tnode;
  868. i : longint;
  869. obj : tobjectdef;
  870. srsym : tsym;
  871. st : tsymtable;
  872. curroffset : aint;
  873. s,sorg : TIDString;
  874. begin
  875. { no support for packed object }
  876. if is_packed_record_or_object(def) then
  877. begin
  878. Message(type_e_no_const_packed_record);
  879. exit;
  880. end;
  881. { only allow nil for class and interface }
  882. if is_class_or_interface(def) then
  883. begin
  884. n:=comp_expr(true);
  885. if n.nodetype<>niln then
  886. begin
  887. Message(type_e_no_const_packed_array);
  888. consume_all_until(_SEMICOLON);
  889. end
  890. else
  891. list.concat(Tai_const.Create_sym(nil));
  892. n.free;
  893. exit;
  894. end;
  895. { for objects we allow it only if it doesn't contain a vmt }
  896. if (oo_has_vmt in def.objectoptions) and
  897. (m_fpc in current_settings.modeswitches) then
  898. begin
  899. Message(parser_e_type_const_not_possible);
  900. exit;
  901. end;
  902. consume(_LKLAMMER);
  903. curroffset:=0;
  904. while token<>_RKLAMMER do
  905. begin
  906. s:=pattern;
  907. sorg:=orgpattern;
  908. consume(_ID);
  909. consume(_COLON);
  910. srsym:=nil;
  911. obj:=tobjectdef(def);
  912. st:=obj.symtable;
  913. while (srsym=nil) and assigned(st) do
  914. begin
  915. srsym:=tsym(st.Find(s));
  916. if assigned(obj) then
  917. obj:=obj.childof;
  918. if assigned(obj) then
  919. st:=obj.symtable
  920. else
  921. st:=nil;
  922. end;
  923. if srsym=nil then
  924. begin
  925. Message1(sym_e_id_not_found,sorg);
  926. consume_all_until(_SEMICOLON);
  927. end
  928. else
  929. with tfieldvarsym(srsym) do
  930. begin
  931. { check position }
  932. if fieldoffset<curroffset then
  933. message(parser_e_invalid_record_const);
  934. { check in VMT needs to be added for TP mode }
  935. if not(m_fpc in current_settings.modeswitches) and
  936. (oo_has_vmt in def.objectoptions) and
  937. (def.vmt_offset<fieldoffset) then
  938. begin
  939. for i:=1 to def.vmt_offset-curroffset do
  940. list.concat(tai_const.create_8bit(0));
  941. list.concat(tai_const.createname(def.vmt_mangledname,0));
  942. { this is more general }
  943. curroffset:=def.vmt_offset + sizeof(aint);
  944. end;
  945. { if needed fill }
  946. if fieldoffset>curroffset then
  947. for i:=1 to fieldoffset-curroffset do
  948. list.concat(Tai_const.Create_8bit(0));
  949. { new position }
  950. curroffset:=fieldoffset+vardef.size;
  951. { read the data }
  952. read_typed_const_data(list,vardef);
  953. if not try_to_consume(_SEMICOLON) then
  954. break;
  955. end;
  956. end;
  957. if not(m_fpc in current_settings.modeswitches) and
  958. (oo_has_vmt in def.objectoptions) and
  959. (def.vmt_offset>=curroffset) then
  960. begin
  961. for i:=1 to def.vmt_offset-curroffset do
  962. list.concat(tai_const.create_8bit(0));
  963. list.concat(tai_const.createname(def.vmt_mangledname,0));
  964. { this is more general }
  965. curroffset:=def.vmt_offset + sizeof(aint);
  966. end;
  967. for i:=1 to def.size-curroffset do
  968. list.concat(Tai_const.Create_8bit(0));
  969. consume(_RKLAMMER);
  970. end;
  971. var
  972. old_block_type : tblock_type;
  973. begin
  974. old_block_type:=block_type;
  975. block_type:=bt_const;
  976. case def.typ of
  977. orddef :
  978. parse_orddef(list,torddef(def));
  979. floatdef :
  980. parse_floatdef(list,tfloatdef(def));
  981. classrefdef :
  982. parse_classrefdef(list,tclassrefdef(def));
  983. pointerdef :
  984. parse_pointerdef(list,tpointerdef(def));
  985. setdef :
  986. parse_setdef(list,tsetdef(def));
  987. enumdef :
  988. parse_enumdef(list,tenumdef(def));
  989. stringdef :
  990. parse_stringdef(list,tstringdef(def));
  991. arraydef :
  992. parse_arraydef(list,tarraydef(def));
  993. procvardef:
  994. parse_procvardef(list,tprocvardef(def));
  995. recorddef:
  996. parse_recorddef(list,trecorddef(def));
  997. objectdef:
  998. parse_objectdef(list,tobjectdef(def));
  999. errordef:
  1000. begin
  1001. { try to consume something useful }
  1002. if token=_LKLAMMER then
  1003. consume_all_until(_RKLAMMER)
  1004. else
  1005. consume_all_until(_SEMICOLON);
  1006. end;
  1007. else
  1008. Message(parser_e_type_const_not_possible);
  1009. end;
  1010. block_type:=old_block_type;
  1011. end;
  1012. {$maxfpuregisters default}
  1013. procedure read_typed_const(list:tasmlist;sym:tstaticvarsym);
  1014. var
  1015. storefilepos : tfileposinfo;
  1016. cursectype : TAsmSectionType;
  1017. C_name : string;
  1018. begin
  1019. { mark the staticvarsym as typedconst }
  1020. include(sym.varoptions,vo_is_typed_const);
  1021. { The variable has a value assigned }
  1022. sym.varstate:=vs_initialised;
  1023. { the variable can't be placed in a register }
  1024. sym.varregable:=vr_none;
  1025. { generate data for typed const }
  1026. storefilepos:=current_filepos;
  1027. current_filepos:=sym.fileinfo;
  1028. if sym.varspez=vs_const then
  1029. cursectype:=sec_rodata
  1030. else
  1031. cursectype:=sec_data;
  1032. maybe_new_object_file(list);
  1033. new_section(list,cursectype,lower(sym.mangledname),const_align(sym.vardef.alignment));
  1034. if (sym.owner.symtabletype=globalsymtable) or
  1035. maybe_smartlink_symbol or
  1036. (assigned(current_procinfo) and
  1037. (po_inline in current_procinfo.procdef.procoptions)) or
  1038. DLLSource then
  1039. list.concat(Tai_symbol.Createname_global(sym.mangledname,AT_DATA,0))
  1040. else
  1041. list.concat(Tai_symbol.Createname(sym.mangledname,AT_DATA,0));
  1042. read_typed_const_data(list,sym.vardef);
  1043. list.concat(tai_symbol_end.Createname(sym.mangledname));
  1044. current_filepos:=storefilepos;
  1045. { Parse hints }
  1046. try_consume_hintdirective(sym.symoptions);
  1047. { Support public name directive }
  1048. if try_to_consume(_PUBLIC) then
  1049. begin
  1050. include(sym.varoptions,vo_is_public);
  1051. if try_to_consume(_NAME) then
  1052. C_name:=get_stringconst
  1053. else
  1054. C_name:=sym.realname;
  1055. sym.set_mangledname(C_Name);
  1056. end;
  1057. end;
  1058. end.