cg386con.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. var
  46. hp1 : pai;
  47. lastlabel : plabel;
  48. begin
  49. lastlabel:=nil;
  50. { const already used ? }
  51. if not assigned(p^.lab_real) then
  52. begin
  53. { tries to found an old entry }
  54. hp1:=pai(consts^.first);
  55. while assigned(hp1) do
  56. begin
  57. if hp1^.typ=ait_label then
  58. lastlabel:=pai_label(hp1)^.l
  59. else
  60. begin
  61. if (hp1^.typ=p^.realtyp) and (lastlabel<>nil) then
  62. begin
  63. if ((p^.realtyp=ait_real_64bit) and (pai_double(hp1)^.value=p^.value_real)) or
  64. ((p^.realtyp=ait_real_extended) and (pai_extended(hp1)^.value=p^.value_real)) or
  65. ((p^.realtyp=ait_real_32bit) and (pai_single(hp1)^.value=p^.value_real)) then
  66. begin
  67. { found! }
  68. p^.lab_real:=lastlabel;
  69. break;
  70. end;
  71. end;
  72. lastlabel:=nil;
  73. end;
  74. hp1:=pai(hp1^.next);
  75. end;
  76. { :-(, we must generate a new entry }
  77. if not assigned(p^.lab_real) then
  78. begin
  79. getdatalabel(lastlabel);
  80. p^.lab_real:=lastlabel;
  81. if (cs_smartlink in aktmoduleswitches) then
  82. consts^.concat(new(pai_cut,init));
  83. consts^.concat(new(pai_label,init(lastlabel)));
  84. case p^.realtyp of
  85. ait_real_64bit : consts^.concat(new(pai_double,init(p^.value_real)));
  86. ait_real_32bit : consts^.concat(new(pai_single,init(p^.value_real)));
  87. ait_real_extended : consts^.concat(new(pai_extended,init(p^.value_real)));
  88. else
  89. internalerror(10120);
  90. end;
  91. end;
  92. end;
  93. clear_reference(p^.location.reference);
  94. p^.location.reference.symbol:=newasmsymbol(lab2str(p^.lab_real));
  95. p^.location.loc:=LOC_MEM;
  96. end;
  97. {*****************************************************************************
  98. SecondFixConst
  99. *****************************************************************************}
  100. procedure secondfixconst(var p : ptree);
  101. begin
  102. { an fix comma const. behaves as a memory reference }
  103. p^.location.loc:=LOC_MEM;
  104. p^.location.reference.is_immediate:=true;
  105. p^.location.reference.offset:=p^.value_fix;
  106. end;
  107. {*****************************************************************************
  108. SecondOrdConst
  109. *****************************************************************************}
  110. procedure secondordconst(var p : ptree);
  111. begin
  112. { an integer const. behaves as a memory reference }
  113. p^.location.loc:=LOC_MEM;
  114. p^.location.reference.is_immediate:=true;
  115. p^.location.reference.offset:=p^.value;
  116. end;
  117. {*****************************************************************************
  118. SecondStringConst
  119. *****************************************************************************}
  120. procedure secondstringconst(var p : ptree);
  121. var
  122. hp1 : pai;
  123. l1,l2,
  124. lastlabel : plabel;
  125. pc : pchar;
  126. same_string : boolean;
  127. l,j,
  128. i,mylength : longint;
  129. begin
  130. lastlabel:=nil;
  131. { const already used ? }
  132. if not assigned(p^.lab_str) then
  133. begin
  134. if is_shortstring(p^.resulttype) then
  135. mylength:=p^.length+2
  136. else
  137. mylength:=p^.length+1;
  138. { tries to found an old entry }
  139. hp1:=pai(consts^.first);
  140. while assigned(hp1) do
  141. begin
  142. if hp1^.typ=ait_label then
  143. lastlabel:=pai_label(hp1)^.l
  144. else
  145. begin
  146. { when changing that code, be careful that }
  147. { you don't use typed consts, which are }
  148. { are also written to consts }
  149. { currently, this is no problem, because }
  150. { typed consts have no leading length or }
  151. { they have no trailing zero }
  152. if (hp1^.typ=ait_string) and (lastlabel<>nil) and
  153. (pai_string(hp1)^.len=mylength) then
  154. begin
  155. same_string:=true;
  156. { if shortstring then check the length byte first and
  157. set the start index to 1 }
  158. if is_shortstring(p^.resulttype) then
  159. begin
  160. if p^.length<>ord(pai_string(hp1)^.str[0]) then
  161. same_string:=false;
  162. j:=1;
  163. end
  164. else
  165. j:=0;
  166. { don't check if the length byte was already wrong }
  167. if same_string then
  168. begin
  169. for i:=0 to p^.length do
  170. begin
  171. if pai_string(hp1)^.str[j]<>p^.value_str[i] then
  172. begin
  173. same_string:=false;
  174. break;
  175. end;
  176. inc(j);
  177. end;
  178. end;
  179. { found ? }
  180. if same_string then
  181. begin
  182. p^.lab_str:=lastlabel;
  183. { create a new entry for ansistrings, but reuse the data }
  184. if (p^.stringtype in [st_ansistring,st_widestring]) then
  185. begin
  186. getdatalabel(l2);
  187. consts^.concat(new(pai_label,init(l2)));
  188. consts^.concat(new(pai_const_symbol,init(lab2str(p^.lab_str))));
  189. { return the offset of the real string }
  190. p^.lab_str:=l2;
  191. end;
  192. break;
  193. end;
  194. end;
  195. lastlabel:=nil;
  196. end;
  197. hp1:=pai(hp1^.next);
  198. end;
  199. { :-(, we must generate a new entry }
  200. if not assigned(p^.lab_str) then
  201. begin
  202. getdatalabel(lastlabel);
  203. p^.lab_str:=lastlabel;
  204. if (cs_smartlink in aktmoduleswitches) then
  205. consts^.concat(new(pai_cut,init));
  206. consts^.concat(new(pai_label,init(lastlabel)));
  207. { generate an ansi string ? }
  208. case p^.stringtype of
  209. st_ansistring:
  210. begin
  211. { an empty ansi string is nil! }
  212. if p^.length=0 then
  213. consts^.concat(new(pai_const,init_32bit(0)))
  214. else
  215. begin
  216. getdatalabel(l1);
  217. getdatalabel(l2);
  218. consts^.concat(new(pai_label,init(l2)));
  219. consts^.concat(new(pai_const_symbol,init(lab2str(l1))));
  220. consts^.concat(new(pai_const,init_32bit(p^.length)));
  221. consts^.concat(new(pai_const,init_32bit(p^.length)));
  222. consts^.concat(new(pai_const,init_32bit(-1)));
  223. consts^.concat(new(pai_label,init(l1)));
  224. getmem(pc,p^.length+2);
  225. move(p^.value_str^,pc^,p^.length);
  226. pc[p^.length]:=#0;
  227. { to overcome this problem we set the length explicitly }
  228. { with the ending null char }
  229. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+1)));
  230. { return the offset of the real string }
  231. p^.lab_str:=l2;
  232. end;
  233. end;
  234. st_shortstring:
  235. begin
  236. { empty strings }
  237. (* if p^.length=0 then
  238. { consts^.concat(new(pai_const,init_16bit(0)))}
  239. { this was not very good because several occurence
  240. needed several data space ! }
  241. begin
  242. getmem(pc,3);
  243. pc[0]:=#0;pc[1]:=#0;
  244. consts^.concat(new(pai_string,init_length_pchar(pc,2)));
  245. end
  246. else *)
  247. begin
  248. { truncate strings larger than 255 chars }
  249. if p^.length>255 then
  250. l:=255
  251. else
  252. l:=p^.length;
  253. { also length and terminating zero }
  254. getmem(pc,l+3);
  255. move(p^.value_str^,pc[1],l+1);
  256. pc[0]:=chr(l);
  257. { to overcome this problem we set the length explicitly }
  258. { with the ending null char }
  259. pc[l+1]:=#0;
  260. consts^.concat(new(pai_string,init_length_pchar(pc,l+2)));
  261. end;
  262. end;
  263. end;
  264. end;
  265. end;
  266. clear_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. clear_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.32 1999-05-01 13:24:06 peter
  382. * merged nasm compiler
  383. * old asm moved to oldasm/
  384. Revision 1.31 1999/04/07 15:16:43 pierre
  385. * zero length string were generated multiple times
  386. Revision 1.30 1999/03/31 13:51:49 peter
  387. * shortstring reuse fixed
  388. Revision 1.29 1999/02/25 21:02:25 peter
  389. * ag386bin updates
  390. + coff writer
  391. Revision 1.28 1999/02/22 02:15:08 peter
  392. * updates for ag386bin
  393. Revision 1.27 1999/01/19 14:21:59 peter
  394. * shortstring truncated after 255 chars
  395. Revision 1.26 1998/12/11 00:02:49 peter
  396. + globtype,tokens,version unit splitted from globals
  397. Revision 1.25 1998/12/10 14:39:30 florian
  398. * bug with p(const a : ansistring) fixed
  399. * duplicate constant ansistrings were handled wrong, fixed
  400. Revision 1.24 1998/11/28 15:36:02 michael
  401. Fixed generation of constant ansistrings
  402. Revision 1.23 1998/11/26 14:39:12 peter
  403. * ansistring -> pchar fixed
  404. * ansistring constants fixed
  405. * ansistring constants are now written once
  406. Revision 1.22 1998/11/24 13:40:59 peter
  407. * release smallsetord, so small sets constant are handled like longints
  408. Revision 1.21 1998/11/24 12:52:41 peter
  409. * sets are not written twice anymore
  410. * optimize for emptyset+single element which uses a new routine from
  411. set.inc FPC_SET_CREATE_ELEMENT
  412. Revision 1.20 1998/11/16 12:11:29 peter
  413. * fixed ansistring crash
  414. Revision 1.19 1998/11/05 23:40:45 pierre
  415. * fix for const strings
  416. Revision 1.18 1998/11/05 15:26:38 pierre
  417. * fix for missing zero after string const
  418. Revision 1.17 1998/11/05 12:02:32 peter
  419. * released useansistring
  420. * removed -Sv, its now available in fpc modes
  421. Revision 1.16 1998/11/04 21:07:43 michael
  422. * undid peters change. Constant ansistrings should end on null too cd ..
  423. Revision 1.15 1998/11/04 10:11:36 peter
  424. * ansistring fixes
  425. Revision 1.14 1998/09/17 09:42:13 peter
  426. + pass_2 for cg386
  427. * Message() -> CGMessage() for pass_1/pass_2
  428. Revision 1.13 1998/09/07 18:45:53 peter
  429. * update smartlinking, uses getdatalabel
  430. * renamed ptree.value vars to value_str,value_real,value_set
  431. Revision 1.12 1998/08/28 10:56:57 peter
  432. * removed warnings
  433. Revision 1.11 1998/08/14 18:18:39 peter
  434. + dynamic set contruction
  435. * smallsets are now working (always longint size)
  436. Revision 1.10 1998/08/04 13:22:46 pierre
  437. * weird bug fixed :
  438. a pchar ' ' (simple space or any other letter) was found to
  439. be equal to a string of length zero !!!
  440. thus printing out non sense
  441. found that out while checking Control-C !!
  442. + added column info also in RHIDE format as
  443. it might be usefull later
  444. Revision 1.9 1998/07/20 18:40:10 florian
  445. * handling of ansi string constants should now work
  446. Revision 1.8 1998/07/20 10:23:00 florian
  447. * better ansi string assignement
  448. Revision 1.7 1998/07/18 22:54:25 florian
  449. * some ansi/wide/longstring support fixed:
  450. o parameter passing
  451. o returning as result from functions
  452. Revision 1.6 1998/07/18 17:11:07 florian
  453. + ansi string constants fixed
  454. + switch $H partial implemented
  455. Revision 1.5 1998/06/25 08:48:07 florian
  456. * first version of rtti support
  457. Revision 1.4 1998/06/08 13:13:31 pierre
  458. + temporary variables now in temp_gen.pas unit
  459. because it is processor independent
  460. * mppc68k.bat modified to undefine i386 and support_mmx
  461. (which are defaults for i386)
  462. Revision 1.3 1998/06/05 17:44:11 peter
  463. * splitted cgi386
  464. Revision 1.2 1998/06/05 16:13:31 pierre
  465. * fix for real and string consts inside inlined procs
  466. Revision 1.1 1998/05/23 01:21:02 peter
  467. + aktasmmode, aktoptprocessor, aktoutputformat
  468. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  469. + $LIBNAME to set the library name where the unit will be put in
  470. * splitted cgi386 a bit (codeseg to large for bp7)
  471. * nasm, tasm works again. nasm moved to ag386nsm.pas
  472. }