ptconst.pas 22 KB

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