ptconst.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. {
  2. $Id$
  3. Copyright (c) 1998 by Florian Klaempfl
  4. Reads typed constants
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ptconst;
  19. interface
  20. uses symtable;
  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(def : pdef;sym : ptypedconstsym);
  25. implementation
  26. uses
  27. cobjects,globals,scanner,aasm,tree,pass_1,
  28. hcodegen,types,verbose
  29. { parser specific stuff }
  30. ,pbase,pexpr
  31. { processor specific stuff }
  32. {$ifdef i386}
  33. ,i386
  34. {$endif}
  35. {$ifdef m68k}
  36. ,m68k
  37. {$endif}
  38. ;
  39. { this procedure reads typed constants }
  40. procedure readtypedconst(def : pdef;sym : ptypedconstsym);
  41. var
  42. p : ptree;
  43. i,l,strlength : longint;
  44. ll : plabel;
  45. s : string;
  46. ca : pchar;
  47. aktpos : longint;
  48. pd : pprocdef;
  49. hp1,hp2 : pdefcoll;
  50. value : bestreal;
  51. {problem with fldt !!
  52. anyway .valued is not extended !!
  53. value : double; }
  54. procedure check_range;
  55. begin
  56. if ((p^.value>porddef(def)^.high) or
  57. (p^.value<porddef(def)^.low)) then
  58. Message(parser_e_range_check_error);
  59. end;
  60. {$R-} {Range check creates problem with init_8bit(-1) !!}
  61. begin
  62. case def^.deftype of
  63. orddef:
  64. begin
  65. p:=comp_expr(true);
  66. do_firstpass(p);
  67. case porddef(def)^.typ of
  68. s8bit,
  69. u8bit : begin
  70. if not is_constintnode(p) then
  71. { is't an int expected }
  72. Message(cg_e_illegal_expression)
  73. else
  74. begin
  75. datasegment^.concat(new(pai_const,init_8bit(p^.value)));
  76. check_range;
  77. end;
  78. end;
  79. s32bit : begin
  80. if not is_constintnode(p) then
  81. Message(cg_e_illegal_expression)
  82. else
  83. begin
  84. datasegment^.concat(new(pai_const,init_32bit(p^.value)));
  85. check_range;
  86. end;
  87. end;
  88. u32bit : begin
  89. if not is_constintnode(p) then
  90. Message(cg_e_illegal_expression)
  91. else
  92. datasegment^.concat(new(pai_const,init_32bit(p^.value)));
  93. end;
  94. bool8bit : begin
  95. if not is_constboolnode(p) then
  96. Message(cg_e_illegal_expression);
  97. datasegment^.concat(new(pai_const,init_8bit(p^.value)));
  98. end;
  99. uchar : begin
  100. if not is_constcharnode(p) then
  101. Message(cg_e_illegal_expression);
  102. datasegment^.concat(new(pai_const,init_8bit(p^.value)));
  103. end;
  104. u16bit,
  105. s16bit : begin
  106. if not is_constintnode(p) then
  107. Message(cg_e_illegal_expression);
  108. datasegment^.concat(new(pai_const,init_16bit(p^.value)));
  109. check_range;
  110. end;
  111. end;
  112. disposetree(p);
  113. end;
  114. floatdef:
  115. begin
  116. p:=comp_expr(true);
  117. do_firstpass(p);
  118. if is_constrealnode(p) then
  119. value:=p^.valued
  120. else if is_constintnode(p) then
  121. value:=p^.value
  122. else
  123. Message(cg_e_illegal_expression);
  124. case pfloatdef(def)^.typ of
  125. s64real : datasegment^.concat(new(pai_double,init(value)));
  126. s32real : datasegment^.concat(new(pai_single,init(value)));
  127. s80real : datasegment^.concat(new(pai_extended,init(value)));
  128. s64bit : datasegment^.concat(new(pai_comp,init(value)));
  129. f32bit : datasegment^.concat(new(pai_const,init_32bit(trunc(value*65536))));
  130. else internalerror(18);
  131. end;
  132. disposetree(p);
  133. end;
  134. pointerdef:
  135. begin
  136. p:=comp_expr(true);
  137. do_firstpass(p);
  138. { nil pointer ? }
  139. if p^.treetype=niln then
  140. datasegment^.concat(new(pai_const,init_32bit(0)))
  141. { maybe pchar ? }
  142. else
  143. if (ppointerdef(def)^.definition^.deftype=orddef) and
  144. (porddef(ppointerdef(def)^.definition)^.typ=uchar) then
  145. begin
  146. getlabel(ll);
  147. datasegment^.concat(new(pai_const,init_symbol(strpnew(lab2str(ll)))));
  148. consts^.concat(new(pai_label,init(ll)));
  149. if p^.treetype=stringconstn then
  150. consts^.concat(new(pai_string,init(p^.values^+#0)))
  151. else
  152. if is_constcharnode(p) then
  153. consts^.concat(new(pai_string,init(char(byte(p^.value))+#0)))
  154. else
  155. Message(cg_e_illegal_expression);
  156. { insert label }
  157. end
  158. else
  159. if p^.treetype=addrn then
  160. begin
  161. if (is_equal(ppointerdef(p^.resulttype)^.definition,ppointerdef(def)^.definition) or
  162. (is_equal(ppointerdef(p^.resulttype)^.definition,voiddef)) or
  163. (is_equal(ppointerdef(def)^.definition,voiddef))) and
  164. (p^.left^.treetype = loadn) then
  165. begin
  166. datasegment^.concat(new(pai_const,init_symbol(
  167. strpnew(p^.left^.symtableentry^.mangledname))));
  168. maybe_concat_external(p^.left^.symtableentry^.owner,
  169. p^.left^.symtableentry^.mangledname);
  170. end
  171. else
  172. Message(cg_e_illegal_expression);
  173. end
  174. else
  175. { allow typeof(Object type)}
  176. if (p^.treetype=inlinen) and
  177. (p^.inlinenumber=in_typeof_x) then
  178. begin
  179. if (p^.left^.treetype=typen) then
  180. begin
  181. datasegment^.concat(new(pai_const,init_symbol(
  182. strpnew(pobjectdef(p^.left^.resulttype)^.vmt_mangledname))));
  183. if pobjectdef(p^.left^.resulttype)^.owner^.symtabletype=unitsymtable then
  184. concat_external(pobjectdef(p^.left^.resulttype)^.vmt_mangledname,EXT_NEAR);
  185. end
  186. else
  187. Message(cg_e_illegal_expression);
  188. end
  189. else
  190. Message(cg_e_illegal_expression);
  191. disposetree(p);
  192. end;
  193. setdef:
  194. begin
  195. p:=comp_expr(true);
  196. do_firstpass(p);
  197. if p^.treetype=setconstrn then
  198. begin
  199. { we only allow const sets }
  200. if assigned(p^.left) then
  201. Message(cg_e_illegal_expression)
  202. else
  203. begin
  204. for l:=0 to def^.savesize-1 do
  205. datasegment^.concat(new(pai_const,init_8bit(p^.constset^[l])));
  206. end;
  207. end
  208. else
  209. Message(cg_e_illegal_expression);
  210. disposetree(p);
  211. end;
  212. enumdef:
  213. begin
  214. p:=comp_expr(true);
  215. do_firstpass(p);
  216. if p^.treetype=ordconstn then
  217. begin
  218. if is_equal(p^.resulttype,def) then
  219. datasegment^.concat(new(pai_const,init_32bit(p^.value)))
  220. else
  221. Message(cg_e_illegal_expression);
  222. end
  223. else
  224. Message(cg_e_illegal_expression);
  225. disposetree(p);
  226. end;
  227. stringdef:
  228. begin
  229. p:=comp_expr(true);
  230. do_firstpass(p);
  231. { first take care of prefixes for long and ansi strings }
  232. case pstringdef(def)^.string_typ of
  233. st_shortstring:
  234. begin
  235. if p^.treetype=stringconstn then
  236. begin
  237. {$ifdef UseAnsiString}
  238. if p^.length>=def^.size then
  239. strlength:=def^.size-1
  240. else
  241. strlength:=p^.length;
  242. datasegment^.concat(new(pai_const,init_8bit(strlength)));
  243. { this can also handle longer strings }
  244. generate_pascii(datasegment,p^.values,strlength);
  245. {$else UseAnsiString}
  246. if length(p^.values^)>=def^.size then
  247. strlength:=def^.size-1
  248. else
  249. strlength:=length(p^.values^);
  250. generate_ascii(char(strlength)+p^.values^);
  251. {$endif UseAnsiString}
  252. end
  253. else if is_constcharnode(p) then
  254. begin
  255. datasegment^.concat(new(pai_string,init(#1+char(byte(p^.value)))));
  256. strlength:=1;
  257. end
  258. else Message(cg_e_illegal_expression);
  259. if def^.size>strlength then
  260. begin
  261. getmem(ca,def^.size-strlength);
  262. fillchar(ca[0],def^.size-strlength-1,' ');
  263. ca[def^.size-strlength-1]:=#0;
  264. {$ifdef UseAnsiString}
  265. { this can also handle longer strings }
  266. { def^.size contains also the leading length, so we }
  267. { we have to subtract one }
  268. generate_pascii(datasegment,ca,def^.size-strlength-1);
  269. {$else UseAnsiString}
  270. datasegment^.concat(new(pai_string,init_pchar(ca)));
  271. {$endif UseAnsiString}
  272. end;
  273. end;
  274. {$ifdef UseLongString}
  275. st_longstring:
  276. begin
  277. { first write the maximum size }
  278. datasegment^.concat(new(pai_const,init_32bit(p^.length)))));
  279. { fill byte }
  280. datasegment^.concat(new(pai_const,init_8bit(0)));
  281. if p^.treetype=stringconstn then
  282. begin
  283. { this can also handle longer strings }
  284. generate_pascii(consts,p^.values,p^.length);
  285. end
  286. else if is_constcharnode(p) then
  287. begin
  288. consts^.concat(new(pai_const,init_8bit(p^.value)));
  289. strlength:=1;
  290. end
  291. else Message(cg_e_illegal_expression);
  292. datasegment^.concat(new(pai_const,init_8bit(0)));
  293. end;
  294. {$endif UseLongString}
  295. {$ifdef UseAnsiString}
  296. st_ansistring:
  297. begin
  298. { an empty ansi string is nil! }
  299. if (p^.treetype=stringconstn) and (p^.length=0) then
  300. datasegment^.concat(new(pai_const,init_32bit(0)))
  301. else
  302. begin
  303. getlabel(ll);
  304. datasegment^.concat(new(pai_const,init_symbol(strpnew(lab2str(ll)))));
  305. { first write the maximum size }
  306. consts^.concat(new(pai_const,init_32bit(p^.length)));
  307. { second write the real length }
  308. consts^.concat(new(pai_const,init_32bit(p^.length)));
  309. { redondent with maxlength but who knows ... (PM) }
  310. { third write use count (set to -1 for safety ) }
  311. consts^.concat(new(pai_const,init_32bit(-1)));
  312. { not longer necessary, because it insert_indata
  313. if assigned(sym) then
  314. sym^.really_insert_in_data;
  315. }
  316. consts^.concat(new(pai_label,init(ll)));
  317. if p^.treetype=stringconstn then
  318. begin
  319. { this can also handle longer strings }
  320. generate_pascii(consts,p^.values,p^.length);
  321. end
  322. else if is_constcharnode(p) then
  323. begin
  324. consts^.concat(new(pai_const,init_8bit(p^.value)));
  325. strlength:=1;
  326. end
  327. else Message(cg_e_illegal_expression);
  328. consts^.concat(new(pai_const,init_8bit(0)));
  329. end;
  330. end;
  331. {$endif UseAnsiString}
  332. end;
  333. disposetree(p);
  334. end;
  335. arraydef:
  336. begin
  337. if token=LKLAMMER then
  338. begin
  339. consume(LKLAMMER);
  340. for l:=parraydef(def)^.lowrange to parraydef(def)^.highrange-1 do
  341. begin
  342. readtypedconst(parraydef(def)^.definition,nil);
  343. consume(COMMA);
  344. end;
  345. readtypedconst(parraydef(def)^.definition,nil);
  346. consume(RKLAMMER);
  347. end
  348. else
  349. begin
  350. p:=comp_expr(true);
  351. do_firstpass(p);
  352. if p^.treetype=stringconstn then
  353. s:=p^.values^
  354. else if is_constcharnode(p) then
  355. s:=char(byte(p^.value))
  356. else Message(cg_e_illegal_expression);
  357. l:=length(s);
  358. for i:=Parraydef(def)^.lowrange to Parraydef(def)^.highrange do
  359. begin
  360. if i+1-Parraydef(def)^.lowrange<=l then
  361. begin
  362. datasegment^.concat(new(pai_const,init_8bit(byte(s[1]))));
  363. delete(s,1,1);
  364. end
  365. else
  366. {Fill the remaining positions with #0.}
  367. datasegment^.concat(new(pai_const,init_8bit(0)));
  368. end;
  369. if length(s)>0 then
  370. Message(parser_e_string_too_long);
  371. end;
  372. end;
  373. procvardef:
  374. begin
  375. { Procvars and pointers are no longer compatible. }
  376. { under tp: =nil or =var under fpc: =nil or =@var }
  377. if token=_NIL then
  378. begin
  379. datasegment^.concat(new(pai_const,init_32bit(0)));
  380. consume(_NIL);
  381. exit;
  382. end
  383. else
  384. if not(cs_tp_compatible in aktmoduleswitches) then
  385. if token=KLAMMERAFFE then
  386. consume(KLAMMERAFFE);
  387. getsym(pattern,true);
  388. consume(ID);
  389. if srsym^.typ=unitsym then
  390. begin
  391. consume(POINT);
  392. getsymonlyin(punitsym(srsym)^.unitsymtable,pattern);
  393. consume(ID);
  394. end;
  395. if srsym^.typ<>procsym then
  396. Message(cg_e_illegal_expression)
  397. else
  398. begin
  399. pd:=pprocsym(srsym)^.definition;
  400. if assigned(pd^.nextoverloaded) then
  401. Message(parser_e_no_overloaded_procvars);
  402. if not((pprocvardef(def)^.options=pd^.options)) or
  403. not(is_equal(pprocvardef(def)^.retdef,pd^.retdef)) then
  404. Message(sym_e_type_mismatch)
  405. else
  406. begin
  407. hp1:=pprocvardef(def)^.para1;
  408. hp2:=pd^.para1;
  409. while assigned(hp1) and assigned(hp2) do
  410. begin
  411. if not(is_equal(hp1^.data,hp2^.data)) or
  412. not(hp1^.paratyp=hp2^.paratyp) then
  413. begin
  414. Message(sym_e_type_mismatch);
  415. break;
  416. end;
  417. hp1:=hp1^.next;
  418. hp2:=hp2^.next;
  419. end;
  420. if not((hp1=nil) and (hp2=nil)) then
  421. Message(sym_e_type_mismatch);
  422. end;
  423. datasegment^.concat(new(pai_const,init_symbol(strpnew(pd^.mangledname))));
  424. if pd^.owner^.symtabletype=unitsymtable then
  425. concat_external(pd^.mangledname,EXT_NEAR);
  426. end;
  427. end;
  428. { reads a typed constant record }
  429. recorddef:
  430. begin
  431. consume(LKLAMMER);
  432. aktpos:=0;
  433. while token<>RKLAMMER do
  434. begin
  435. s:=pattern;
  436. consume(ID);
  437. consume(COLON);
  438. srsym:=precdef(def)^.symtable^.search(s);
  439. if srsym=nil then
  440. begin
  441. Message1(sym_e_id_not_found,s);
  442. consume_all_until(SEMICOLON);
  443. end
  444. else
  445. begin
  446. { check position }
  447. if pvarsym(srsym)^.address<aktpos then
  448. Message(parser_e_invalid_record_const);
  449. { if needed fill }
  450. if pvarsym(srsym)^.address>aktpos then
  451. for i:=1 to pvarsym(srsym)^.address-aktpos do
  452. datasegment^.concat(new(pai_const,init_8bit(0)));
  453. { new position }
  454. aktpos:=pvarsym(srsym)^.address+pvarsym(srsym)^.definition^.size;
  455. { read the data }
  456. readtypedconst(pvarsym(srsym)^.definition,nil);
  457. if token=SEMICOLON then
  458. consume(SEMICOLON)
  459. else break;
  460. end;
  461. end;
  462. for i:=1 to def^.size-aktpos do
  463. datasegment^.concat(new(pai_const,init_8bit(0)));
  464. consume(RKLAMMER);
  465. end;
  466. else Message(parser_e_type_const_not_possible);
  467. end;
  468. end;
  469. end.
  470. {
  471. $Log$
  472. Revision 1.11 1998-08-10 14:50:20 peter
  473. + localswitches, moduleswitches, globalswitches splitting
  474. Revision 1.10 1998/07/21 11:16:25 florian
  475. * bug0147 fixed
  476. Revision 1.9 1998/07/20 22:17:16 florian
  477. * hex constants in numeric char (#$54#$43 ...) are now allowed
  478. * there was a bug in record_var_dec which prevents the used
  479. of nested variant records (for example drivers.tevent of tv)
  480. Revision 1.8 1998/07/20 18:40:15 florian
  481. * handling of ansi string constants should now work
  482. Revision 1.7 1998/07/18 22:54:29 florian
  483. * some ansi/wide/longstring support fixed:
  484. o parameter passing
  485. o returning as result from functions
  486. Revision 1.6 1998/06/08 22:59:52 peter
  487. * smartlinking works for win32
  488. * some defines to exclude some compiler parts
  489. Revision 1.5 1998/06/03 22:49:01 peter
  490. + wordbool,longbool
  491. * rename bis,von -> high,low
  492. * moved some systemunit loading/creating to psystem.pas
  493. Revision 1.4 1998/05/05 12:05:42 florian
  494. * problems with properties fixed
  495. * crash fixed: i:=l when i and l are undefined, was a problem with
  496. implementation of private/protected
  497. Revision 1.3 1998/04/29 10:34:00 pierre
  498. + added some code for ansistring (not complete nor working yet)
  499. * corrected operator overloading
  500. * corrected nasm output
  501. + started inline procedures
  502. + added starstarn : use ** for exponentiation (^ gave problems)
  503. + started UseTokenInfo cond to get accurate positions
  504. }