cg68kcon.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Generate m68k 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 cg68kcon;
  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 secondsetcons(var p : ptree);
  28. procedure secondniln(var p : ptree);
  29. implementation
  30. uses
  31. cobjects,verbose,globals,
  32. symtable,aasm,types,
  33. hcodegen,temp_gen,pass_2,
  34. m68k,cga68k,tgen68k;
  35. {*****************************************************************************
  36. SecondRealConst
  37. *****************************************************************************}
  38. procedure secondrealconst(var p : ptree);
  39. var
  40. hp1 : pai;
  41. lastlabel : plabel;
  42. found : boolean;
  43. begin
  44. clear_reference(p^.location.reference);
  45. lastlabel:=nil;
  46. found:=false;
  47. { const already used ? }
  48. if p^.labnumber=-1 then
  49. begin
  50. { tries to found an old entry }
  51. hp1:=pai(consts^.first);
  52. while assigned(hp1) do
  53. begin
  54. if hp1^.typ=ait_label then
  55. lastlabel:=pai_label(hp1)^.l
  56. else
  57. begin
  58. if (hp1^.typ=p^.realtyp) and (lastlabel<>nil) then
  59. begin
  60. if ((p^.realtyp=ait_real_64bit) and (pai_double(hp1)^.value=p^.valued)) or
  61. ((p^.realtyp=ait_real_extended) and (pai_extended(hp1)^.value=p^.valued)) or
  62. ((p^.realtyp=ait_real_32bit) and (pai_single(hp1)^.value=p^.valued)) then
  63. begin
  64. { found! }
  65. p^.labnumber:=lastlabel^.nb;
  66. break;
  67. end;
  68. end;
  69. lastlabel:=nil;
  70. end;
  71. hp1:=pai(hp1^.next);
  72. end;
  73. { :-(, we must generate a new entry }
  74. if p^.labnumber=-1 then
  75. begin
  76. getlabel(lastlabel);
  77. p^.labnumber:=lastlabel^.nb;
  78. concat_constlabel(lastlabel,constreal);
  79. case p^.realtyp of
  80. ait_real_64bit : consts^.concat(new(pai_double,init(p^.valued)));
  81. ait_real_32bit : consts^.concat(new(pai_single,init(p^.valued)));
  82. ait_real_extended : consts^.concat(new(pai_extended,init(p^.valued)));
  83. else
  84. internalerror(10120);
  85. end;
  86. end;
  87. end;
  88. stringdispose(p^.location.reference.symbol);
  89. if assigned(lastlabel) then
  90. p^.location.reference.symbol:=stringdup(constlabel2str(lastlabel,constreal))
  91. else
  92. p^.location.reference.symbol:=stringdup(constlabelnb2str(p^.labnumber,constreal));
  93. end;
  94. {*****************************************************************************
  95. SecondFixConst
  96. *****************************************************************************}
  97. procedure secondfixconst(var p : ptree);
  98. begin
  99. { an fix comma const. behaves as a memory reference }
  100. p^.location.loc:=LOC_MEM;
  101. p^.location.reference.isintvalue:=true;
  102. p^.location.reference.offset:=p^.valuef;
  103. end;
  104. {*****************************************************************************
  105. SecondOrdConst
  106. *****************************************************************************}
  107. procedure secondordconst(var p : ptree);
  108. begin
  109. { an integer const. behaves as a memory reference }
  110. p^.location.loc:=LOC_MEM;
  111. p^.location.reference.isintvalue:=true;
  112. p^.location.reference.offset:=p^.value;
  113. end;
  114. {*****************************************************************************
  115. SecondStringConst
  116. *****************************************************************************}
  117. procedure secondstringconst(var p : ptree);
  118. var
  119. hp1 : pai;
  120. {$ifdef UseAnsiString}
  121. l1,
  122. {$endif}
  123. lastlabel : plabel;
  124. pc : pchar;
  125. same_string : boolean;
  126. i : word;
  127. begin
  128. clear_reference(p^.location.reference);
  129. lastlabel:=nil;
  130. { const already used ? }
  131. if p^.labstrnumber=-1 then
  132. begin
  133. { tries to found an old entry }
  134. hp1:=pai(consts^.first);
  135. while assigned(hp1) do
  136. begin
  137. if hp1^.typ=ait_label then
  138. lastlabel:=pai_label(hp1)^.l
  139. else
  140. begin
  141. { when changing that code, be careful that }
  142. { you don't use typed consts, which are }
  143. { are also written to consts }
  144. { currently, this is no problem, because }
  145. { typed consts have no leading length or }
  146. { they have no trailing zero }
  147. {$ifdef UseAnsiString}
  148. if (hp1^.typ=ait_string) and (lastlabel<>nil) and
  149. (pai_string(hp1)^.len=p^.length+2) then
  150. {$else UseAnsiString}
  151. if (hp1^.typ=ait_string) and (lastlabel<>nil) and
  152. (pai_string(hp1)^.len=length(p^.values^)+2) then
  153. {$endif UseAnsiString}
  154. begin
  155. same_string:=true;
  156. {$ifndef UseAnsiString}
  157. { weird error here !!! }
  158. { pchar ' ' was found equal to string '' !!!! }
  159. { gave strange output in exceptions !! PM }
  160. for i:=0 to length(p^.values^) do
  161. if pai_string(hp1)^.str[i]<>p^.values^[i] then
  162. {$else}
  163. for i:=0 to p^.length do
  164. if pai_string(hp1)^.str[i]<>p^.values[i] then
  165. {$endif}
  166. begin
  167. same_string:=false;
  168. break;
  169. end;
  170. if same_string then
  171. begin
  172. { found! }
  173. p^.labstrnumber:=lastlabel^.nb;
  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 p^.labstrnumber=-1 then
  183. begin
  184. getlabel(lastlabel);
  185. p^.labstrnumber:=lastlabel^.nb;
  186. {$ifndef UseAnsiString}
  187. getmem(pc,length(p^.values^)+3);
  188. move(p^.values^,pc^,length(p^.values^)+1);
  189. pc[length(p^.values^)+1]:=#0;
  190. concat_constlabel(lastlabel,conststring);
  191. { we still will have a problem if there is a #0 inside the pchar }
  192. consts^.concat(new(pai_string,init_length_pchar(pc,length(p^.values^)+2)));
  193. {$else UseAnsiString}
  194. { generate an ansi string ? }
  195. case p^.stringtype of
  196. st_ansistring:
  197. begin
  198. { an empty ansi string is nil! }
  199. concat_constlabel(lastlabel,conststring);
  200. if p^.length=0 then
  201. consts^.concat(new(pai_const,init_32bit(0)))
  202. else
  203. begin
  204. getlabel(l1);
  205. consts^.concat(new(pai_const,init_symbol(strpnew(lab2str(l1)))));
  206. consts^.concat(new(pai_const,init_32bit(p^.length)));
  207. consts^.concat(new(pai_const,init_32bit(p^.length)));
  208. consts^.concat(new(pai_const,init_32bit(-1)));
  209. consts^.concat(new(pai_label,init(l1)));
  210. getmem(pc,p^.length+1);
  211. move(p^.values^,pc^,p^.length+1);
  212. { to overcome this problem we set the length explicitly }
  213. { with the ending null char }
  214. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+1)));
  215. end;
  216. end;
  217. st_shortstring:
  218. begin
  219. getmem(pc,p^.length+3);
  220. move(p^.values^,pc[1],p^.length+1);
  221. pc[0]:=chr(p^.length);
  222. concat_constlabel(lastlabel,conststring);
  223. { to overcome this problem we set the length explicitly }
  224. { with the ending null char }
  225. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+2)));
  226. end;
  227. end;
  228. {$endif UseAnsiString}
  229. end;
  230. end;
  231. stringdispose(p^.location.reference.symbol);
  232. if assigned(lastlabel) then
  233. p^.location.reference.symbol:=stringdup(constlabel2str(lastlabel,conststring))
  234. else
  235. p^.location.reference.symbol:=stringdup(constlabelnb2str(p^.labstrnumber,conststring));
  236. p^.location.loc := LOC_MEM;
  237. end;
  238. {*****************************************************************************
  239. SecondSetCons
  240. *****************************************************************************}
  241. procedure secondsetcons(var p : ptree);
  242. var
  243. l : plabel;
  244. i : longint;
  245. href : treference;
  246. begin
  247. {$ifdef SMALLSETORD}
  248. if psetdef(p^.resulttype)^.settype=smallset then
  249. begin
  250. p^.location.loc:=LOC_MEM;
  251. p^.location.reference.isintvalue:=true;
  252. p^.location.reference.offset:=p^.constset^[0];
  253. end
  254. else
  255. begin
  256. reset_reference(href);
  257. getlabel(l);
  258. stringdispose(p^.location.reference.symbol);
  259. href.symbol:=stringdup(constlabel2str(l,constseta));
  260. concat_constlabel(l,constseta);
  261. for i:=0 to 31 do
  262. consts^.concat(new(pai_const,init_8bit(p^.constset^[i])));
  263. p^.location.reference:=href;
  264. end;
  265. {$else}
  266. reset_reference(href);
  267. getlabel(l);
  268. stringdispose(p^.location.reference.symbol);
  269. href.symbol:=stringdup(constlabel2str(l,constseta));
  270. concat_constlabel(l,constseta);
  271. if psetdef(p^.resulttype)^.settype=smallset then
  272. begin
  273. move(p^.constset^,i,sizeof(longint));
  274. consts^.concat(new(pai_const,init_32bit(i)));
  275. end
  276. else
  277. begin
  278. for i:=0 to 31 do
  279. consts^.concat(new(pai_const,init_8bit(p^.constset^[i])));
  280. end;
  281. p^.location.reference:=href;
  282. {$endif SMALLSETORD}
  283. end;
  284. {*****************************************************************************
  285. SecondNilN
  286. *****************************************************************************}
  287. procedure secondniln(var p : ptree);
  288. begin
  289. p^.location.loc:=LOC_MEM;
  290. p^.location.reference.isintvalue:=true;
  291. p^.location.reference.offset:=0;
  292. end;
  293. end.
  294. {
  295. $Log$
  296. Revision 1.1 1998-09-01 09:07:09 peter
  297. * m68k fixes, splitted cg68k like cgi386
  298. }