cg386con.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. {$ifdef ag386bin}
  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,
  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. for i:=0 to p^.length do
  157. if pai_string(hp1)^.str[i]<>p^.value_str[i] then
  158. begin
  159. same_string:=false;
  160. break;
  161. end;
  162. if same_string then
  163. begin
  164. { found! }
  165. p^.lab_str:=lastlabel;
  166. if (p^.stringtype in [st_ansistring,st_widestring]) then
  167. begin
  168. getdatalabel(l2);
  169. consts^.concat(new(pai_label,init(l2)));
  170. consts^.concat(new(pai_const_symbol,init(lab2str(p^.lab_str))));
  171. { return the offset of the real string }
  172. p^.lab_str:=l2;
  173. end;
  174. break;
  175. end;
  176. end;
  177. lastlabel:=nil;
  178. end;
  179. hp1:=pai(hp1^.next);
  180. end;
  181. { :-(, we must generate a new entry }
  182. if not assigned(p^.lab_str) then
  183. begin
  184. getdatalabel(lastlabel);
  185. p^.lab_str:=lastlabel;
  186. if (cs_smartlink in aktmoduleswitches) then
  187. consts^.concat(new(pai_cut,init));
  188. consts^.concat(new(pai_label,init(lastlabel)));
  189. { generate an ansi string ? }
  190. case p^.stringtype of
  191. st_ansistring:
  192. begin
  193. { an empty ansi string is nil! }
  194. if p^.length=0 then
  195. consts^.concat(new(pai_const,init_32bit(0)))
  196. else
  197. begin
  198. getdatalabel(l1);
  199. getdatalabel(l2);
  200. consts^.concat(new(pai_label,init(l2)));
  201. consts^.concat(new(pai_const_symbol,init(lab2str(l1))));
  202. consts^.concat(new(pai_const,init_32bit(p^.length)));
  203. consts^.concat(new(pai_const,init_32bit(p^.length)));
  204. consts^.concat(new(pai_const,init_32bit(-1)));
  205. consts^.concat(new(pai_label,init(l1)));
  206. getmem(pc,p^.length+2);
  207. move(p^.value_str^,pc^,p^.length);
  208. pc[p^.length]:=#0;
  209. { to overcome this problem we set the length explicitly }
  210. { with the ending null char }
  211. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+1)));
  212. { return the offset of the real string }
  213. p^.lab_str:=l2;
  214. end;
  215. end;
  216. st_shortstring:
  217. begin
  218. { empty strings }
  219. if p^.length=0 then
  220. consts^.concat(new(pai_const,init_16bit(0)))
  221. else
  222. begin
  223. { truncate strings larger than 255 chars }
  224. if p^.length>255 then
  225. l:=255
  226. else
  227. l:=p^.length;
  228. { also length and terminating zero }
  229. getmem(pc,l+3);
  230. move(p^.value_str^,pc[1],l+1);
  231. pc[0]:=chr(l);
  232. { to overcome this problem we set the length explicitly }
  233. { with the ending null char }
  234. pc[l+1]:=#0;
  235. consts^.concat(new(pai_string,init_length_pchar(pc,l+2)));
  236. end;
  237. end;
  238. end;
  239. end;
  240. end;
  241. clear_reference(p^.location.reference);
  242. p^.location.reference.symbol:=newasmsymbol(lab2str(p^.lab_str));
  243. p^.location.loc:=LOC_MEM;
  244. end;
  245. {*****************************************************************************
  246. SecondSetCons
  247. *****************************************************************************}
  248. procedure secondsetconst(var p : ptree);
  249. var
  250. hp1 : pai;
  251. lastlabel : plabel;
  252. i : longint;
  253. neededtyp : tait;
  254. begin
  255. {$ifdef SMALLSETORD}
  256. { small sets are loaded as constants }
  257. if psetdef(p^.resulttype)^.settype=smallset then
  258. begin
  259. p^.location.loc:=LOC_MEM;
  260. p^.location.reference.is_immediate:=true;
  261. p^.location.reference.offset:=plongint(p^.value_set)^;
  262. exit;
  263. end;
  264. {$endif}
  265. if psetdef(p^.resulttype)^.settype=smallset then
  266. neededtyp:=ait_const_32bit
  267. else
  268. neededtyp:=ait_const_8bit;
  269. lastlabel:=nil;
  270. { const already used ? }
  271. if not assigned(p^.lab_set) then
  272. begin
  273. { tries to found an old entry }
  274. hp1:=pai(consts^.first);
  275. while assigned(hp1) do
  276. begin
  277. if hp1^.typ=ait_label then
  278. lastlabel:=pai_label(hp1)^.l
  279. else
  280. begin
  281. if (lastlabel<>nil) and (hp1^.typ=neededtyp) then
  282. begin
  283. if (hp1^.typ=ait_const_8bit) then
  284. begin
  285. { compare normal set }
  286. i:=0;
  287. while assigned(hp1) and (i<32) do
  288. begin
  289. if pai_const(hp1)^.value<>p^.value_set^[i] then
  290. break;
  291. inc(i);
  292. hp1:=pai(hp1^.next);
  293. end;
  294. if i=32 then
  295. begin
  296. { found! }
  297. p^.lab_set:=lastlabel;
  298. break;
  299. end;
  300. { leave when the end of consts is reached, so no
  301. hp1^.next is done }
  302. if not assigned(hp1) then
  303. break;
  304. end
  305. else
  306. begin
  307. { compare small set }
  308. if plongint(p^.value_set)^=pai_const(hp1)^.value then
  309. begin
  310. { found! }
  311. p^.lab_set:=lastlabel;
  312. break;
  313. end;
  314. end;
  315. end;
  316. lastlabel:=nil;
  317. end;
  318. hp1:=pai(hp1^.next);
  319. end;
  320. { :-(, we must generate a new entry }
  321. if not assigned(p^.lab_set) then
  322. begin
  323. getdatalabel(lastlabel);
  324. p^.lab_set:=lastlabel;
  325. if (cs_smartlink in aktmoduleswitches) then
  326. consts^.concat(new(pai_cut,init));
  327. consts^.concat(new(pai_label,init(lastlabel)));
  328. if psetdef(p^.resulttype)^.settype=smallset then
  329. begin
  330. move(p^.value_set^,i,sizeof(longint));
  331. consts^.concat(new(pai_const,init_32bit(i)));
  332. end
  333. else
  334. begin
  335. for i:=0 to 31 do
  336. consts^.concat(new(pai_const,init_8bit(p^.value_set^[i])));
  337. end;
  338. end;
  339. end;
  340. clear_reference(p^.location.reference);
  341. p^.location.reference.symbol:=newasmsymbol(lab2str(p^.lab_set));
  342. p^.location.loc:=LOC_MEM;
  343. end;
  344. {*****************************************************************************
  345. SecondNilN
  346. *****************************************************************************}
  347. procedure secondniln(var p : ptree);
  348. begin
  349. p^.location.loc:=LOC_MEM;
  350. p^.location.reference.is_immediate:=true;
  351. p^.location.reference.offset:=0;
  352. end;
  353. end.
  354. {
  355. $Log$
  356. Revision 1.29 1999-02-25 21:02:25 peter
  357. * ag386bin updates
  358. + coff writer
  359. Revision 1.28 1999/02/22 02:15:08 peter
  360. * updates for ag386bin
  361. Revision 1.27 1999/01/19 14:21:59 peter
  362. * shortstring truncated after 255 chars
  363. Revision 1.26 1998/12/11 00:02:49 peter
  364. + globtype,tokens,version unit splitted from globals
  365. Revision 1.25 1998/12/10 14:39:30 florian
  366. * bug with p(const a : ansistring) fixed
  367. * duplicate constant ansistrings were handled wrong, fixed
  368. Revision 1.24 1998/11/28 15:36:02 michael
  369. Fixed generation of constant ansistrings
  370. Revision 1.23 1998/11/26 14:39:12 peter
  371. * ansistring -> pchar fixed
  372. * ansistring constants fixed
  373. * ansistring constants are now written once
  374. Revision 1.22 1998/11/24 13:40:59 peter
  375. * release smallsetord, so small sets constant are handled like longints
  376. Revision 1.21 1998/11/24 12:52:41 peter
  377. * sets are not written twice anymore
  378. * optimize for emptyset+single element which uses a new routine from
  379. set.inc FPC_SET_CREATE_ELEMENT
  380. Revision 1.20 1998/11/16 12:11:29 peter
  381. * fixed ansistring crash
  382. Revision 1.19 1998/11/05 23:40:45 pierre
  383. * fix for const strings
  384. Revision 1.18 1998/11/05 15:26:38 pierre
  385. * fix for missing zero after string const
  386. Revision 1.17 1998/11/05 12:02:32 peter
  387. * released useansistring
  388. * removed -Sv, its now available in fpc modes
  389. Revision 1.16 1998/11/04 21:07:43 michael
  390. * undid peters change. Constant ansistrings should end on null too cd ..
  391. Revision 1.15 1998/11/04 10:11:36 peter
  392. * ansistring fixes
  393. Revision 1.14 1998/09/17 09:42:13 peter
  394. + pass_2 for cg386
  395. * Message() -> CGMessage() for pass_1/pass_2
  396. Revision 1.13 1998/09/07 18:45:53 peter
  397. * update smartlinking, uses getdatalabel
  398. * renamed ptree.value vars to value_str,value_real,value_set
  399. Revision 1.12 1998/08/28 10:56:57 peter
  400. * removed warnings
  401. Revision 1.11 1998/08/14 18:18:39 peter
  402. + dynamic set contruction
  403. * smallsets are now working (always longint size)
  404. Revision 1.10 1998/08/04 13:22:46 pierre
  405. * weird bug fixed :
  406. a pchar ' ' (simple space or any other letter) was found to
  407. be equal to a string of length zero !!!
  408. thus printing out non sense
  409. found that out while checking Control-C !!
  410. + added column info also in RHIDE format as
  411. it might be usefull later
  412. Revision 1.9 1998/07/20 18:40:10 florian
  413. * handling of ansi string constants should now work
  414. Revision 1.8 1998/07/20 10:23:00 florian
  415. * better ansi string assignement
  416. Revision 1.7 1998/07/18 22:54:25 florian
  417. * some ansi/wide/longstring support fixed:
  418. o parameter passing
  419. o returning as result from functions
  420. Revision 1.6 1998/07/18 17:11:07 florian
  421. + ansi string constants fixed
  422. + switch $H partial implemented
  423. Revision 1.5 1998/06/25 08:48:07 florian
  424. * first version of rtti support
  425. Revision 1.4 1998/06/08 13:13:31 pierre
  426. + temporary variables now in temp_gen.pas unit
  427. because it is processor independent
  428. * mppc68k.bat modified to undefine i386 and support_mmx
  429. (which are defaults for i386)
  430. Revision 1.3 1998/06/05 17:44:11 peter
  431. * splitted cgi386
  432. Revision 1.2 1998/06/05 16:13:31 pierre
  433. * fix for real and string consts inside inlined procs
  434. Revision 1.1 1998/05/23 01:21:02 peter
  435. + aktasmmode, aktoptprocessor, aktoutputformat
  436. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  437. + $LIBNAME to set the library name where the unit will be put in
  438. * splitted cgi386 a bit (codeseg to large for bp7)
  439. * nasm, tasm works again. nasm moved to ag386nsm.pas
  440. }