cg386con.pas 14 KB

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