2
0

cg386con.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Generate i386 assembler for 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 cg386con;
  19. interface
  20. uses
  21. tree;
  22. {$define SMALLSETORD}
  23. procedure secondrealconst(var p : ptree);
  24. procedure secondfixconst(var p : ptree);
  25. procedure secondordconst(var p : ptree);
  26. procedure secondstringconst(var p : ptree);
  27. procedure secondsetconst(var p : ptree);
  28. procedure secondniln(var p : ptree);
  29. implementation
  30. uses
  31. globtype,systems,
  32. cobjects,verbose,globals,
  33. symtable,aasm,types,
  34. hcodegen,temp_gen,pass_2,
  35. i386base,i386asm,
  36. cgai386,tgeni386;
  37. {*****************************************************************************
  38. SecondRealConst
  39. *****************************************************************************}
  40. procedure secondrealconst(var p : ptree);
  41. const
  42. floattype2ait:array[tfloattype] of tait=
  43. (ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_none,ait_none);
  44. var
  45. hp1 : pai;
  46. lastlabel : pasmlabel;
  47. realait : tait;
  48. begin
  49. lastlabel:=nil;
  50. realait:=floattype2ait[pfloatdef(p^.resulttype)^.typ];
  51. { const already used ? }
  52. if not assigned(p^.lab_real) then
  53. begin
  54. { tries to found an old entry }
  55. hp1:=pai(consts^.first);
  56. while assigned(hp1) do
  57. begin
  58. if hp1^.typ=ait_label then
  59. lastlabel:=pai_label(hp1)^.l
  60. else
  61. begin
  62. if (hp1^.typ=realait) and (lastlabel<>nil) then
  63. begin
  64. if(
  65. ((realait=ait_real_32bit) and (pai_real_32bit(hp1)^.value=p^.value_real)) or
  66. ((realait=ait_real_64bit) and (pai_real_64bit(hp1)^.value=p^.value_real)) or
  67. ((realait=ait_real_80bit) and (pai_real_80bit(hp1)^.value=p^.value_real)) or
  68. ((realait=ait_comp_64bit) and (pai_comp_64bit(hp1)^.value=p^.value_real))
  69. ) then
  70. begin
  71. { found! }
  72. p^.lab_real:=lastlabel;
  73. break;
  74. end;
  75. end;
  76. lastlabel:=nil;
  77. end;
  78. hp1:=pai(hp1^.next);
  79. end;
  80. { :-(, we must generate a new entry }
  81. if not assigned(p^.lab_real) then
  82. begin
  83. getdatalabel(lastlabel);
  84. p^.lab_real:=lastlabel;
  85. if (cs_smartlink in aktmoduleswitches) then
  86. consts^.concat(new(pai_cut,init));
  87. consts^.concat(new(pai_label,init(lastlabel)));
  88. case realait of
  89. ait_real_32bit :
  90. consts^.concat(new(pai_real_32bit,init(p^.value_real)));
  91. ait_real_64bit :
  92. consts^.concat(new(pai_real_64bit,init(p^.value_real)));
  93. ait_real_80bit :
  94. consts^.concat(new(pai_real_80bit,init(p^.value_real)));
  95. ait_comp_64bit :
  96. consts^.concat(new(pai_comp_64bit,init(p^.value_real)));
  97. else
  98. internalerror(10120);
  99. end;
  100. end;
  101. end;
  102. reset_reference(p^.location.reference);
  103. p^.location.reference.symbol:=p^.lab_real;
  104. p^.location.loc:=LOC_MEM;
  105. end;
  106. {*****************************************************************************
  107. SecondFixConst
  108. *****************************************************************************}
  109. procedure secondfixconst(var p : ptree);
  110. begin
  111. { an fix comma const. behaves as a memory reference }
  112. p^.location.loc:=LOC_MEM;
  113. p^.location.reference.is_immediate:=true;
  114. p^.location.reference.offset:=p^.value_fix;
  115. end;
  116. {*****************************************************************************
  117. SecondOrdConst
  118. *****************************************************************************}
  119. procedure secondordconst(var p : ptree);
  120. begin
  121. { an integer const. behaves as a memory reference }
  122. p^.location.loc:=LOC_MEM;
  123. p^.location.reference.is_immediate:=true;
  124. p^.location.reference.offset:=p^.value;
  125. end;
  126. {*****************************************************************************
  127. SecondStringConst
  128. *****************************************************************************}
  129. procedure secondstringconst(var p : ptree);
  130. var
  131. hp1 : pai;
  132. l1,l2,
  133. lastlabel : pasmlabel;
  134. pc : pchar;
  135. same_string : boolean;
  136. l,j,
  137. i,mylength : longint;
  138. begin
  139. lastlabel:=nil;
  140. { const already used ? }
  141. if not assigned(p^.lab_str) then
  142. begin
  143. if is_shortstring(p^.resulttype) then
  144. mylength:=p^.length+2
  145. else
  146. mylength:=p^.length+1;
  147. { tries to found an old entry }
  148. hp1:=pai(consts^.first);
  149. while assigned(hp1) do
  150. begin
  151. if hp1^.typ=ait_label then
  152. lastlabel:=pai_label(hp1)^.l
  153. else
  154. begin
  155. { when changing that code, be careful that }
  156. { you don't use typed consts, which are }
  157. { are also written to consts }
  158. { currently, this is no problem, because }
  159. { typed consts have no leading length or }
  160. { they have no trailing zero }
  161. if (hp1^.typ=ait_string) and (lastlabel<>nil) and
  162. (pai_string(hp1)^.len=mylength) then
  163. begin
  164. same_string:=true;
  165. { if shortstring then check the length byte first and
  166. set the start index to 1 }
  167. if is_shortstring(p^.resulttype) then
  168. begin
  169. if p^.length<>ord(pai_string(hp1)^.str[0]) then
  170. same_string:=false;
  171. j:=1;
  172. end
  173. else
  174. j:=0;
  175. { don't check if the length byte was already wrong }
  176. if same_string then
  177. begin
  178. for i:=0 to p^.length do
  179. begin
  180. if pai_string(hp1)^.str[j]<>p^.value_str[i] then
  181. begin
  182. same_string:=false;
  183. break;
  184. end;
  185. inc(j);
  186. end;
  187. end;
  188. { found ? }
  189. if same_string then
  190. begin
  191. p^.lab_str:=lastlabel;
  192. { create a new entry for ansistrings, but reuse the data }
  193. if (p^.stringtype in [st_ansistring,st_widestring]) then
  194. begin
  195. getdatalabel(l2);
  196. consts^.concat(new(pai_label,init(l2)));
  197. consts^.concat(new(pai_const_symbol,init(p^.lab_str)));
  198. { return the offset of the real string }
  199. p^.lab_str:=l2;
  200. end;
  201. break;
  202. end;
  203. end;
  204. lastlabel:=nil;
  205. end;
  206. hp1:=pai(hp1^.next);
  207. end;
  208. { :-(, we must generate a new entry }
  209. if not assigned(p^.lab_str) then
  210. begin
  211. getdatalabel(lastlabel);
  212. p^.lab_str:=lastlabel;
  213. if (cs_smartlink in aktmoduleswitches) then
  214. consts^.concat(new(pai_cut,init));
  215. consts^.concat(new(pai_label,init(lastlabel)));
  216. { generate an ansi string ? }
  217. case p^.stringtype of
  218. st_ansistring:
  219. begin
  220. { an empty ansi string is nil! }
  221. if p^.length=0 then
  222. consts^.concat(new(pai_const,init_32bit(0)))
  223. else
  224. begin
  225. getdatalabel(l1);
  226. getdatalabel(l2);
  227. consts^.concat(new(pai_label,init(l2)));
  228. consts^.concat(new(pai_const_symbol,init(l1)));
  229. consts^.concat(new(pai_const,init_32bit(p^.length)));
  230. consts^.concat(new(pai_const,init_32bit(p^.length)));
  231. consts^.concat(new(pai_const,init_32bit(-1)));
  232. consts^.concat(new(pai_label,init(l1)));
  233. getmem(pc,p^.length+2);
  234. move(p^.value_str^,pc^,p^.length);
  235. pc[p^.length]:=#0;
  236. { to overcome this problem we set the length explicitly }
  237. { with the ending null char }
  238. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+1)));
  239. { return the offset of the real string }
  240. p^.lab_str:=l2;
  241. end;
  242. end;
  243. st_shortstring:
  244. begin
  245. { truncate strings larger than 255 chars }
  246. if p^.length>255 then
  247. l:=255
  248. else
  249. l:=p^.length;
  250. { also length and terminating zero }
  251. getmem(pc,l+3);
  252. move(p^.value_str^,pc[1],l+1);
  253. pc[0]:=chr(l);
  254. { to overcome this problem we set the length explicitly }
  255. { with the ending null char }
  256. pc[l+1]:=#0;
  257. consts^.concat(new(pai_string,init_length_pchar(pc,l+2)));
  258. end;
  259. end;
  260. end;
  261. end;
  262. reset_reference(p^.location.reference);
  263. p^.location.reference.symbol:=p^.lab_str;
  264. p^.location.loc:=LOC_MEM;
  265. end;
  266. {*****************************************************************************
  267. SecondSetCons
  268. *****************************************************************************}
  269. procedure secondsetconst(var p : ptree);
  270. var
  271. hp1 : pai;
  272. lastlabel : pasmlabel;
  273. i : longint;
  274. neededtyp : tait;
  275. begin
  276. {$ifdef SMALLSETORD}
  277. { small sets are loaded as constants }
  278. if psetdef(p^.resulttype)^.settype=smallset then
  279. begin
  280. p^.location.loc:=LOC_MEM;
  281. p^.location.reference.is_immediate:=true;
  282. p^.location.reference.offset:=plongint(p^.value_set)^;
  283. exit;
  284. end;
  285. {$endif}
  286. if psetdef(p^.resulttype)^.settype=smallset then
  287. neededtyp:=ait_const_32bit
  288. else
  289. neededtyp:=ait_const_8bit;
  290. lastlabel:=nil;
  291. { const already used ? }
  292. if not assigned(p^.lab_set) then
  293. begin
  294. { tries to found an old entry }
  295. hp1:=pai(consts^.first);
  296. while assigned(hp1) do
  297. begin
  298. if hp1^.typ=ait_label then
  299. lastlabel:=pai_label(hp1)^.l
  300. else
  301. begin
  302. if (lastlabel<>nil) and (hp1^.typ=neededtyp) then
  303. begin
  304. if (hp1^.typ=ait_const_8bit) then
  305. begin
  306. { compare normal set }
  307. i:=0;
  308. while assigned(hp1) and (i<32) do
  309. begin
  310. if pai_const(hp1)^.value<>p^.value_set^[i] then
  311. break;
  312. inc(i);
  313. hp1:=pai(hp1^.next);
  314. end;
  315. if i=32 then
  316. begin
  317. { found! }
  318. p^.lab_set:=lastlabel;
  319. break;
  320. end;
  321. { leave when the end of consts is reached, so no
  322. hp1^.next is done }
  323. if not assigned(hp1) then
  324. break;
  325. end
  326. else
  327. begin
  328. { compare small set }
  329. if plongint(p^.value_set)^=pai_const(hp1)^.value then
  330. begin
  331. { found! }
  332. p^.lab_set:=lastlabel;
  333. break;
  334. end;
  335. end;
  336. end;
  337. lastlabel:=nil;
  338. end;
  339. hp1:=pai(hp1^.next);
  340. end;
  341. { :-(, we must generate a new entry }
  342. if not assigned(p^.lab_set) then
  343. begin
  344. getdatalabel(lastlabel);
  345. p^.lab_set:=lastlabel;
  346. if (cs_smartlink in aktmoduleswitches) then
  347. consts^.concat(new(pai_cut,init));
  348. consts^.concat(new(pai_label,init(lastlabel)));
  349. if psetdef(p^.resulttype)^.settype=smallset then
  350. begin
  351. move(p^.value_set^,i,sizeof(longint));
  352. consts^.concat(new(pai_const,init_32bit(i)));
  353. end
  354. else
  355. begin
  356. for i:=0 to 31 do
  357. consts^.concat(new(pai_const,init_8bit(p^.value_set^[i])));
  358. end;
  359. end;
  360. end;
  361. reset_reference(p^.location.reference);
  362. p^.location.reference.symbol:=p^.lab_set;
  363. p^.location.loc:=LOC_MEM;
  364. end;
  365. {*****************************************************************************
  366. SecondNilN
  367. *****************************************************************************}
  368. procedure secondniln(var p : ptree);
  369. begin
  370. p^.location.loc:=LOC_MEM;
  371. p^.location.reference.is_immediate:=true;
  372. p^.location.reference.offset:=0;
  373. end;
  374. end.
  375. {
  376. $Log$
  377. Revision 1.36 1999-05-27 19:44:10 peter
  378. * removed oldasm
  379. * plabel -> pasmlabel
  380. * -a switches to source writing automaticly
  381. * assembler readers OOPed
  382. * asmsymbol automaticly external
  383. * jumptables and other label fixes for asm readers
  384. Revision 1.35 1999/05/21 13:54:47 peter
  385. * NEWLAB for label as symbol
  386. Revision 1.34 1999/05/12 00:19:41 peter
  387. * removed R_DEFAULT_SEG
  388. * uniform float names
  389. Revision 1.33 1999/05/06 09:05:12 peter
  390. * generic write_float and str_float
  391. * fixed constant float conversions
  392. Revision 1.32 1999/05/01 13:24:06 peter
  393. * merged nasm compiler
  394. * old asm moved to oldasm/
  395. Revision 1.31 1999/04/07 15:16:43 pierre
  396. * zero length string were generated multiple times
  397. Revision 1.30 1999/03/31 13:51:49 peter
  398. * shortstring reuse fixed
  399. Revision 1.29 1999/02/25 21:02:25 peter
  400. * ag386bin updates
  401. + coff writer
  402. Revision 1.28 1999/02/22 02:15:08 peter
  403. * updates for ag386bin
  404. Revision 1.27 1999/01/19 14:21:59 peter
  405. * shortstring truncated after 255 chars
  406. Revision 1.26 1998/12/11 00:02:49 peter
  407. + globtype,tokens,version unit splitted from globals
  408. Revision 1.25 1998/12/10 14:39:30 florian
  409. * bug with p(const a : ansistring) fixed
  410. * duplicate constant ansistrings were handled wrong, fixed
  411. Revision 1.24 1998/11/28 15:36:02 michael
  412. Fixed generation of constant ansistrings
  413. Revision 1.23 1998/11/26 14:39:12 peter
  414. * ansistring -> pchar fixed
  415. * ansistring constants fixed
  416. * ansistring constants are now written once
  417. Revision 1.22 1998/11/24 13:40:59 peter
  418. * release smallsetord, so small sets constant are handled like longints
  419. Revision 1.21 1998/11/24 12:52:41 peter
  420. * sets are not written twice anymore
  421. * optimize for emptyset+single element which uses a new routine from
  422. set.inc FPC_SET_CREATE_ELEMENT
  423. Revision 1.20 1998/11/16 12:11:29 peter
  424. * fixed ansistring crash
  425. Revision 1.19 1998/11/05 23:40:45 pierre
  426. * fix for const strings
  427. Revision 1.18 1998/11/05 15:26:38 pierre
  428. * fix for missing zero after string const
  429. Revision 1.17 1998/11/05 12:02:32 peter
  430. * released useansistring
  431. * removed -Sv, its now available in fpc modes
  432. Revision 1.16 1998/11/04 21:07:43 michael
  433. * undid peters change. Constant ansistrings should end on null too cd ..
  434. Revision 1.15 1998/11/04 10:11:36 peter
  435. * ansistring fixes
  436. Revision 1.14 1998/09/17 09:42:13 peter
  437. + pass_2 for cg386
  438. * Message() -> CGMessage() for pass_1/pass_2
  439. Revision 1.13 1998/09/07 18:45:53 peter
  440. * update smartlinking, uses getdatalabel
  441. * renamed ptree.value vars to value_str,value_real,value_set
  442. Revision 1.12 1998/08/28 10:56:57 peter
  443. * removed warnings
  444. Revision 1.11 1998/08/14 18:18:39 peter
  445. + dynamic set contruction
  446. * smallsets are now working (always longint size)
  447. Revision 1.10 1998/08/04 13:22:46 pierre
  448. * weird bug fixed :
  449. a pchar ' ' (simple space or any other letter) was found to
  450. be equal to a string of length zero !!!
  451. thus printing out non sense
  452. found that out while checking Control-C !!
  453. + added column info also in RHIDE format as
  454. it might be usefull later
  455. Revision 1.9 1998/07/20 18:40:10 florian
  456. * handling of ansi string constants should now work
  457. Revision 1.8 1998/07/20 10:23:00 florian
  458. * better ansi string assignement
  459. Revision 1.7 1998/07/18 22:54:25 florian
  460. * some ansi/wide/longstring support fixed:
  461. o parameter passing
  462. o returning as result from functions
  463. Revision 1.6 1998/07/18 17:11:07 florian
  464. + ansi string constants fixed
  465. + switch $H partial implemented
  466. Revision 1.5 1998/06/25 08:48:07 florian
  467. * first version of rtti support
  468. Revision 1.4 1998/06/08 13:13:31 pierre
  469. + temporary variables now in temp_gen.pas unit
  470. because it is processor independent
  471. * mppc68k.bat modified to undefine i386 and support_mmx
  472. (which are defaults for i386)
  473. Revision 1.3 1998/06/05 17:44:11 peter
  474. * splitted cgi386
  475. Revision 1.2 1998/06/05 16:13:31 pierre
  476. * fix for real and string consts inside inlined procs
  477. Revision 1.1 1998/05/23 01:21:02 peter
  478. + aktasmmode, aktoptprocessor, aktoutputformat
  479. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  480. + $LIBNAME to set the library name where the unit will be put in
  481. * splitted cgi386 a bit (codeseg to large for bp7)
  482. * nasm, tasm works again. nasm moved to ag386nsm.pas
  483. }