cg386con.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. procedure secondrealconst(var p : ptree);
  23. procedure secondfixconst(var p : ptree);
  24. procedure secondordconst(var p : ptree);
  25. procedure secondstringconst(var p : ptree);
  26. procedure secondsetcons(var p : ptree);
  27. procedure secondniln(var p : ptree);
  28. implementation
  29. uses
  30. cobjects,verbose,globals,
  31. symtable,aasm,i386,types,
  32. hcodegen,cgai386,temp_gen,tgeni386,cgi386;
  33. {*****************************************************************************
  34. SecondRealConst
  35. *****************************************************************************}
  36. procedure secondrealconst(var p : ptree);
  37. var
  38. hp1 : pai;
  39. lastlabel : plabel;
  40. found : boolean;
  41. begin
  42. clear_reference(p^.location.reference);
  43. lastlabel:=nil;
  44. found:=false;
  45. { const already used ? }
  46. if p^.labnumber=-1 then
  47. begin
  48. { tries to found an old entry }
  49. hp1:=pai(consts^.first);
  50. while assigned(hp1) do
  51. begin
  52. if hp1^.typ=ait_label then
  53. lastlabel:=pai_label(hp1)^.l
  54. else
  55. begin
  56. if (hp1^.typ=p^.realtyp) and (lastlabel<>nil) then
  57. begin
  58. if ((p^.realtyp=ait_real_64bit) and (pai_double(hp1)^.value=p^.valued)) or
  59. ((p^.realtyp=ait_real_extended) and (pai_extended(hp1)^.value=p^.valued)) or
  60. ((p^.realtyp=ait_real_32bit) and (pai_single(hp1)^.value=p^.valued)) then
  61. begin
  62. { found! }
  63. p^.labnumber:=lastlabel^.nb;
  64. break;
  65. end;
  66. end;
  67. lastlabel:=nil;
  68. end;
  69. hp1:=pai(hp1^.next);
  70. end;
  71. { :-(, we must generate a new entry }
  72. if p^.labnumber=-1 then
  73. begin
  74. getlabel(lastlabel);
  75. p^.labnumber:=lastlabel^.nb;
  76. concat_constlabel(lastlabel,constreal);
  77. case p^.realtyp of
  78. ait_real_64bit : consts^.concat(new(pai_double,init(p^.valued)));
  79. ait_real_32bit : consts^.concat(new(pai_single,init(p^.valued)));
  80. ait_real_extended : consts^.concat(new(pai_extended,init(p^.valued)));
  81. else
  82. internalerror(10120);
  83. end;
  84. end;
  85. end;
  86. stringdispose(p^.location.reference.symbol);
  87. if assigned(lastlabel) then
  88. p^.location.reference.symbol:=stringdup(constlabel2str(lastlabel,constreal))
  89. else
  90. p^.location.reference.symbol:=stringdup(constlabelnb2str(p^.labnumber,constreal));
  91. end;
  92. {*****************************************************************************
  93. SecondFixConst
  94. *****************************************************************************}
  95. procedure secondfixconst(var p : ptree);
  96. begin
  97. { an fix comma const. behaves as a memory reference }
  98. p^.location.loc:=LOC_MEM;
  99. p^.location.reference.isintvalue:=true;
  100. p^.location.reference.offset:=p^.valuef;
  101. end;
  102. {*****************************************************************************
  103. SecondOrdConst
  104. *****************************************************************************}
  105. procedure secondordconst(var p : ptree);
  106. begin
  107. { an integer const. behaves as a memory reference }
  108. p^.location.loc:=LOC_MEM;
  109. p^.location.reference.isintvalue:=true;
  110. p^.location.reference.offset:=p^.value;
  111. end;
  112. {*****************************************************************************
  113. SecondStringConst
  114. *****************************************************************************}
  115. procedure secondstringconst(var p : ptree);
  116. var
  117. hp1 : pai;
  118. lastlabel,l1 : plabel;
  119. pc : pchar;
  120. same_string : boolean;
  121. i : word;
  122. begin
  123. clear_reference(p^.location.reference);
  124. lastlabel:=nil;
  125. { const already used ? }
  126. if p^.labstrnumber=-1 then
  127. begin
  128. { tries to found an old entry }
  129. hp1:=pai(consts^.first);
  130. while assigned(hp1) do
  131. begin
  132. if hp1^.typ=ait_label then
  133. lastlabel:=pai_label(hp1)^.l
  134. else
  135. begin
  136. { when changing that code, be careful that }
  137. { you don't use typed consts, which are }
  138. { are also written to consts }
  139. { currently, this is no problem, because }
  140. { typed consts have no leading length or }
  141. { they have no trailing zero }
  142. {$ifdef UseAnsiString}
  143. if (hp1^.typ=ait_string) and (lastlabel<>nil) and
  144. (pai_string(hp1)^.len=p^.length+2) then
  145. {$else UseAnsiString}
  146. if (hp1^.typ=ait_string) and (lastlabel<>nil) and
  147. (pai_string(hp1)^.len=length(p^.values^)+2) then
  148. {$endif UseAnsiString}
  149. begin
  150. same_string:=true;
  151. {$ifndef UseAnsiString}
  152. { weird error here !!! }
  153. { pchar ' ' was found equal to string '' !!!! }
  154. { gave strange output in exceptions !! PM }
  155. for i:=0 to length(p^.values^) do
  156. if pai_string(hp1)^.str[i]<>p^.values^[i] then
  157. {$else}
  158. for i:=0 to p^.length do
  159. if pai_string(hp1)^.str[i]<>p^.values[i] then
  160. {$endif}
  161. begin
  162. same_string:=false;
  163. break;
  164. end;
  165. if same_string then
  166. begin
  167. { found! }
  168. p^.labstrnumber:=lastlabel^.nb;
  169. break;
  170. end;
  171. end;
  172. lastlabel:=nil;
  173. end;
  174. hp1:=pai(hp1^.next);
  175. end;
  176. { :-(, we must generate a new entry }
  177. if p^.labstrnumber=-1 then
  178. begin
  179. getlabel(lastlabel);
  180. p^.labstrnumber:=lastlabel^.nb;
  181. {$ifndef UseAnsiString}
  182. getmem(pc,length(p^.values^)+3);
  183. move(p^.values^,pc^,length(p^.values^)+1);
  184. pc[length(p^.values^)+1]:=#0;
  185. concat_constlabel(lastlabel,conststring);
  186. { we still will have a problem if there is a #0 inside the pchar }
  187. consts^.concat(new(pai_string,init_length_pchar(pc,length(p^.values^)+2)));
  188. {$else UseAnsiString}
  189. { generate an ansi string ? }
  190. case p^.stringtype of
  191. st_ansistring:
  192. begin
  193. { an empty ansi string is nil! }
  194. concat_constlabel(lastlabel,conststring);
  195. if p^.length=0 then
  196. consts^.concat(new(pai_const,init_32bit(0)))
  197. else
  198. begin
  199. getlabel(l1);
  200. consts^.concat(new(pai_const,init_symbol(strpnew(lab2str(l1)))));
  201. consts^.concat(new(pai_const,init_32bit(p^.length)));
  202. consts^.concat(new(pai_const,init_32bit(p^.length)));
  203. consts^.concat(new(pai_const,init_32bit(-1)));
  204. consts^.concat(new(pai_label,init(l1)));
  205. getmem(pc,p^.length+1);
  206. move(p^.values^,pc^,p^.length+1);
  207. { to overcome this problem we set the length explicitly }
  208. { with the ending null char }
  209. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+1)));
  210. end;
  211. end;
  212. st_shortstring:
  213. begin
  214. getmem(pc,p^.length+3);
  215. move(p^.values^,pc[1],p^.length+1);
  216. pc[0]:=chr(p^.length);
  217. concat_constlabel(lastlabel,conststring);
  218. { to overcome this problem we set the length explicitly }
  219. { with the ending null char }
  220. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+2)));
  221. end;
  222. end;
  223. {$endif UseAnsiString}
  224. end;
  225. end;
  226. stringdispose(p^.location.reference.symbol);
  227. if assigned(lastlabel) then
  228. p^.location.reference.symbol:=stringdup(constlabel2str(lastlabel,conststring))
  229. else
  230. p^.location.reference.symbol:=stringdup(constlabelnb2str(p^.labstrnumber,conststring));
  231. p^.location.loc := LOC_MEM;
  232. end;
  233. {*****************************************************************************
  234. SecondSetCons
  235. *****************************************************************************}
  236. procedure secondsetcons(var p : ptree);
  237. var
  238. l : plabel;
  239. i : longint;
  240. hp : ptree;
  241. href,sref : treference;
  242. begin
  243. reset_reference(href);
  244. getlabel(l);
  245. stringdispose(p^.location.reference.symbol);
  246. href.symbol:=stringdup(constlabel2str(l,constseta));
  247. concat_constlabel(l,constseta);
  248. if psetdef(p^.resulttype)^.settype=smallset then
  249. begin
  250. move(p^.constset^,i,sizeof(longint));
  251. consts^.concat(new(pai_const,init_32bit(i)));
  252. end
  253. else
  254. begin
  255. for i:=0 to 31 do
  256. consts^.concat(new(pai_const,init_8bit(p^.constset^[i])));
  257. end;
  258. p^.location.reference:=href;
  259. end;
  260. {*****************************************************************************
  261. SecondNilN
  262. *****************************************************************************}
  263. procedure secondniln(var p : ptree);
  264. begin
  265. p^.location.loc:=LOC_MEM;
  266. p^.location.reference.isintvalue:=true;
  267. p^.location.reference.offset:=0;
  268. end;
  269. end.
  270. {
  271. $Log$
  272. Revision 1.11 1998-08-14 18:18:39 peter
  273. + dynamic set contruction
  274. * smallsets are now working (always longint size)
  275. Revision 1.10 1998/08/04 13:22:46 pierre
  276. * weird bug fixed :
  277. a pchar ' ' (simple space or any other letter) was found to
  278. be equal to a string of length zero !!!
  279. thus printing out non sense
  280. found that out while checking Control-C !!
  281. + added column info also in RHIDE format as
  282. it might be usefull later
  283. Revision 1.9 1998/07/20 18:40:10 florian
  284. * handling of ansi string constants should now work
  285. Revision 1.8 1998/07/20 10:23:00 florian
  286. * better ansi string assignement
  287. Revision 1.7 1998/07/18 22:54:25 florian
  288. * some ansi/wide/longstring support fixed:
  289. o parameter passing
  290. o returning as result from functions
  291. Revision 1.6 1998/07/18 17:11:07 florian
  292. + ansi string constants fixed
  293. + switch $H partial implemented
  294. Revision 1.5 1998/06/25 08:48:07 florian
  295. * first version of rtti support
  296. Revision 1.4 1998/06/08 13:13:31 pierre
  297. + temporary variables now in temp_gen.pas unit
  298. because it is processor independent
  299. * mppc68k.bat modified to undefine i386 and support_mmx
  300. (which are defaults for i386)
  301. Revision 1.3 1998/06/05 17:44:11 peter
  302. * splitted cgi386
  303. Revision 1.2 1998/06/05 16:13:31 pierre
  304. * fix for real and string consts inside inlined procs
  305. Revision 1.1 1998/05/23 01:21:02 peter
  306. + aktasmmode, aktoptprocessor, aktoutputformat
  307. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  308. + $LIBNAME to set the library name where the unit will be put in
  309. * splitted cgi386 a bit (codeseg to large for bp7)
  310. * nasm, tasm works again. nasm moved to ag386nsm.pas
  311. }