cg386con.pas 21 KB

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