cg68kcon.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 secondsetconst(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. begin
  43. lastlabel:=nil;
  44. { const already used ? }
  45. if not assigned(p^.lab_real) then
  46. begin
  47. { tries to found an old entry }
  48. hp1:=pai(consts^.first);
  49. while assigned(hp1) do
  50. begin
  51. if hp1^.typ=ait_label then
  52. lastlabel:=pai_label(hp1)^.l
  53. else
  54. begin
  55. if (hp1^.typ=p^.realtyp) and (lastlabel<>nil) then
  56. begin
  57. if ((p^.realtyp=ait_real_64bit) and (pai_double(hp1)^.value=p^.value_real)) or
  58. ((p^.realtyp=ait_real_extended) and (pai_extended(hp1)^.value=p^.value_real)) or
  59. ((p^.realtyp=ait_real_32bit) and (pai_single(hp1)^.value=p^.value_real)) then
  60. begin
  61. { found! }
  62. p^.lab_real:=lastlabel;
  63. break;
  64. end;
  65. end;
  66. lastlabel:=nil;
  67. end;
  68. hp1:=pai(hp1^.next);
  69. end;
  70. { :-(, we must generate a new entry }
  71. if not assigned(p^.lab_real) then
  72. begin
  73. getdatalabel(lastlabel);
  74. p^.lab_real:=lastlabel;
  75. if (cs_smartlink in aktmoduleswitches) then
  76. consts^.concat(new(pai_cut,init));
  77. consts^.concat(new(pai_label,init(lastlabel)));
  78. case p^.realtyp of
  79. ait_real_64bit : consts^.concat(new(pai_double,init(p^.value_real)));
  80. ait_real_32bit : consts^.concat(new(pai_single,init(p^.value_real)));
  81. ait_real_extended : consts^.concat(new(pai_extended,init(p^.value_real)));
  82. else
  83. internalerror(10120);
  84. end;
  85. end;
  86. end;
  87. clear_reference(p^.location.reference);
  88. p^.location.reference.symbol:=stringdup(lab2str(p^.lab_real));
  89. p^.location.loc:=LOC_MEM;
  90. end;
  91. {*****************************************************************************
  92. SecondFixConst
  93. *****************************************************************************}
  94. procedure secondfixconst(var p : ptree);
  95. begin
  96. { an fix comma const. behaves as a memory reference }
  97. p^.location.loc:=LOC_MEM;
  98. p^.location.reference.isintvalue:=true;
  99. p^.location.reference.offset:=p^.value_fix;
  100. end;
  101. {*****************************************************************************
  102. SecondOrdConst
  103. *****************************************************************************}
  104. procedure secondordconst(var p : ptree);
  105. begin
  106. { an integer const. behaves as a memory reference }
  107. p^.location.loc:=LOC_MEM;
  108. p^.location.reference.isintvalue:=true;
  109. p^.location.reference.offset:=p^.value;
  110. end;
  111. {*****************************************************************************
  112. SecondStringConst
  113. *****************************************************************************}
  114. procedure secondstringconst(var p : ptree);
  115. var
  116. hp1 : pai;
  117. l1,
  118. lastlabel : plabel;
  119. pc : pchar;
  120. same_string : boolean;
  121. i : longint;
  122. begin
  123. lastlabel:=nil;
  124. { const already used ? }
  125. if not assigned(p^.lab_str) then
  126. begin
  127. { tries to found an old entry }
  128. hp1:=pai(consts^.first);
  129. while assigned(hp1) do
  130. begin
  131. if hp1^.typ=ait_label then
  132. lastlabel:=pai_label(hp1)^.l
  133. else
  134. begin
  135. { when changing that code, be careful that }
  136. { you don't use typed consts, which are }
  137. { are also written to consts }
  138. { currently, this is no problem, because }
  139. { typed consts have no leading length or }
  140. { they have no trailing zero }
  141. if (hp1^.typ=ait_string) and (lastlabel<>nil) and
  142. (pai_string(hp1)^.len=p^.length+2) then
  143. begin
  144. same_string:=true;
  145. for i:=0 to p^.length do
  146. if pai_string(hp1)^.str[i]<>p^.value_str[i] then
  147. begin
  148. same_string:=false;
  149. break;
  150. end;
  151. if same_string then
  152. begin
  153. { found! }
  154. p^.lab_str:=lastlabel;
  155. break;
  156. end;
  157. end;
  158. lastlabel:=nil;
  159. end;
  160. hp1:=pai(hp1^.next);
  161. end;
  162. { :-(, we must generate a new entry }
  163. if not assigned(p^.lab_str) then
  164. begin
  165. getdatalabel(lastlabel);
  166. p^.lab_str:=lastlabel;
  167. if (cs_smartlink in aktmoduleswitches) then
  168. consts^.concat(new(pai_cut,init));
  169. consts^.concat(new(pai_label,init(lastlabel)));
  170. { generate an ansi string ? }
  171. case p^.stringtype of
  172. st_ansistring:
  173. begin
  174. { an empty ansi string is nil! }
  175. if p^.length=0 then
  176. consts^.concat(new(pai_const,init_32bit(0)))
  177. else
  178. begin
  179. getlabel(l1);
  180. consts^.concat(new(pai_const,init_symbol(strpnew(lab2str(l1)))));
  181. consts^.concat(new(pai_const,init_32bit(p^.length)));
  182. consts^.concat(new(pai_const,init_32bit(p^.length)));
  183. consts^.concat(new(pai_const,init_32bit(-1)));
  184. consts^.concat(new(pai_label,init(l1)));
  185. getmem(pc,p^.length+1);
  186. move(p^.value_str^,pc^,p^.length+1);
  187. pc[p^.length]:=#0;
  188. { to overcome this problem we set the length explicitly }
  189. { with the ending null char }
  190. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+1)));
  191. end;
  192. end;
  193. st_shortstring:
  194. begin
  195. { empty strings }
  196. if p^.length=0 then
  197. consts^.concat(new(pai_const,init_16bit(0)))
  198. else
  199. begin
  200. { also length and terminating zero }
  201. getmem(pc,p^.length+3);
  202. move(p^.value_str^,pc[1],p^.length+1);
  203. pc[0]:=chr(p^.length);
  204. pc[p^.length+1]:=#0;
  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+2)));
  208. end;
  209. end;
  210. end;
  211. end;
  212. end;
  213. clear_reference(p^.location.reference);
  214. p^.location.reference.symbol:=stringdup(lab2str(p^.lab_str));
  215. p^.location.loc:=LOC_MEM;
  216. end;
  217. {*****************************************************************************
  218. SecondSetCons
  219. *****************************************************************************}
  220. procedure secondsetconst(var p : ptree);
  221. var
  222. lastlabel : plabel;
  223. i : longint;
  224. begin
  225. {$ifdef SMALLSETORD}
  226. if psetdef(p^.resulttype)^.settype=smallset then
  227. begin
  228. p^.location.loc:=LOC_MEM;
  229. p^.location.reference.isintvalue:=true;
  230. p^.location.reference.offset:=p^.value_set^[0];
  231. end
  232. else
  233. begin
  234. getdatalabel(lastlabel);
  235. p^.lab_set:=lastlabel;
  236. if (cs_smartlink in aktmoduleswitches) then
  237. consts^.concat(new(pai_cut,init));
  238. consts^.concat(new(pai_label,init(duplabel(lastlabel))));
  239. for i:=0 to 31 do
  240. consts^.concat(new(pai_const,init_8bit(p^.value_set^[i])));
  241. clear_reference(p^.location.reference);
  242. p^.location.reference.symbol:=stringdup(lab2str(p^.lab_set));
  243. p^.location.loc:=LOC_MEM;
  244. end;
  245. {$else}
  246. getdatalabel(lastlabel);
  247. p^.lab_set:=lastlabel;
  248. if (cs_smartlink in aktmoduleswitches) then
  249. consts^.concat(new(pai_cut,init));
  250. consts^.concat(new(pai_label,init(lastlabel)));
  251. if psetdef(p^.resulttype)^.settype=smallset then
  252. begin
  253. move(p^.value_set^,i,sizeof(longint));
  254. consts^.concat(new(pai_const,init_32bit(i)));
  255. end
  256. else
  257. begin
  258. for i:=0 to 31 do
  259. consts^.concat(new(pai_const,init_8bit(p^.value_set^[i])));
  260. end;
  261. clear_reference(p^.location.reference);
  262. p^.location.reference.symbol:=stringdup(lab2str(p^.lab_set));
  263. p^.location.loc:=LOC_MEM;
  264. {$endif SMALLSETORD}
  265. end;
  266. {*****************************************************************************
  267. SecondNilN
  268. *****************************************************************************}
  269. procedure secondniln(var p : ptree);
  270. begin
  271. p^.location.loc:=LOC_MEM;
  272. p^.location.reference.isintvalue:=true;
  273. p^.location.reference.offset:=0;
  274. end;
  275. end.
  276. {
  277. $Log$
  278. Revision 1.4 1998-11-06 09:47:29 pierre
  279. * problem of const with ansi fixed
  280. Revision 1.3 1998/11/05 12:02:37 peter
  281. * released useansistring
  282. * removed -Sv, its now available in fpc modes
  283. Revision 1.2 1998/09/07 18:45:56 peter
  284. * update smartlinking, uses getdatalabel
  285. * renamed ptree.value vars to value_str,value_real,value_set
  286. }