cg68kcon.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. globtype,systems,
  32. cobjects,verbose,globals,
  33. symtable,aasm,types,
  34. hcodegen,temp_gen,pass_2,
  35. m68k,cga68k,tgen68k;
  36. {*****************************************************************************
  37. SecondRealConst
  38. *****************************************************************************}
  39. procedure secondrealconst(var p : ptree);
  40. var
  41. hp1 : pai;
  42. lastlabel : plabel;
  43. begin
  44. lastlabel:=nil;
  45. { const already used ? }
  46. if not assigned(p^.lab_real) 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^.value_real)) or
  59. ((p^.realtyp=ait_real_extended) and (pai_extended(hp1)^.value=p^.value_real)) or
  60. ((p^.realtyp=ait_real_32bit) and (pai_single(hp1)^.value=p^.value_real)) then
  61. begin
  62. { found! }
  63. p^.lab_real:=lastlabel;
  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 not assigned(p^.lab_real) then
  73. begin
  74. getdatalabel(lastlabel);
  75. p^.lab_real:=lastlabel;
  76. if (cs_smartlink in aktmoduleswitches) then
  77. consts^.concat(new(pai_cut,init));
  78. consts^.concat(new(pai_label,init(lastlabel)));
  79. case p^.realtyp of
  80. ait_real_64bit : consts^.concat(new(pai_double,init(p^.value_real)));
  81. ait_real_32bit : consts^.concat(new(pai_single,init(p^.value_real)));
  82. ait_real_extended : consts^.concat(new(pai_extended,init(p^.value_real)));
  83. else
  84. internalerror(10120);
  85. end;
  86. end;
  87. end;
  88. clear_reference(p^.location.reference);
  89. p^.location.reference.symbol:=stringdup(lab2str(p^.lab_real));
  90. p^.location.loc:=LOC_MEM;
  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^.value_fix;
  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. l1,l2,
  119. lastlabel : plabel;
  120. pc : pchar;
  121. same_string : boolean;
  122. i,mylength : longint;
  123. begin
  124. lastlabel:=nil;
  125. { const already used ? }
  126. if not assigned(p^.lab_str) then
  127. begin
  128. if is_shortstring(p^.resulttype) then
  129. mylength:=p^.length+2
  130. else
  131. mylength:=p^.length+1;
  132. { tries to found an old entry }
  133. hp1:=pai(consts^.first);
  134. while assigned(hp1) do
  135. begin
  136. if hp1^.typ=ait_label then
  137. lastlabel:=pai_label(hp1)^.l
  138. else
  139. begin
  140. { when changing that code, be careful that }
  141. { you don't use typed consts, which are }
  142. { are also written to consts }
  143. { currently, this is no problem, because }
  144. { typed consts have no leading length or }
  145. { they have no trailing zero }
  146. if (hp1^.typ=ait_string) and (lastlabel<>nil) and
  147. (pai_string(hp1)^.len=mylength) then
  148. begin
  149. same_string:=true;
  150. for i:=0 to p^.length do
  151. if pai_string(hp1)^.str[i]<>p^.value_str[i] then
  152. begin
  153. same_string:=false;
  154. break;
  155. end;
  156. if same_string then
  157. begin
  158. { found! }
  159. p^.lab_str:=lastlabel;
  160. break;
  161. end;
  162. end;
  163. lastlabel:=nil;
  164. end;
  165. hp1:=pai(hp1^.next);
  166. end;
  167. { :-(, we must generate a new entry }
  168. if not assigned(p^.lab_str) then
  169. begin
  170. getdatalabel(lastlabel);
  171. p^.lab_str:=lastlabel;
  172. if (cs_smartlink in aktmoduleswitches) then
  173. consts^.concat(new(pai_cut,init));
  174. consts^.concat(new(pai_label,init(lastlabel)));
  175. { generate an ansi string ? }
  176. case p^.stringtype of
  177. st_ansistring:
  178. begin
  179. { an empty ansi string is nil! }
  180. if p^.length=0 then
  181. consts^.concat(new(pai_const,init_32bit(0)))
  182. else
  183. begin
  184. getdatalabel(l1);
  185. getdatalabel(l2);
  186. consts^.concat(new(pai_label,init(l2)));
  187. consts^.concat(new(pai_const,init_symbol(strpnew(lab2str(l1)))));
  188. consts^.concat(new(pai_const,init_32bit(p^.length)));
  189. consts^.concat(new(pai_const,init_32bit(p^.length)));
  190. consts^.concat(new(pai_const,init_32bit(-1)));
  191. consts^.concat(new(pai_label,init(l1)));
  192. getmem(pc,p^.length+2);
  193. move(p^.value_str^,pc^,p^.length);
  194. pc[p^.length]:=#0;
  195. { to overcome this problem we set the length explicitly }
  196. { with the ending null char }
  197. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+1)));
  198. { return the offset of the real string }
  199. p^.lab_str:=l2;
  200. end;
  201. end;
  202. st_shortstring:
  203. begin
  204. { empty strings }
  205. if p^.length=0 then
  206. consts^.concat(new(pai_const,init_16bit(0)))
  207. else
  208. begin
  209. { also length and terminating zero }
  210. getmem(pc,p^.length+3);
  211. move(p^.value_str^,pc[1],p^.length+1);
  212. pc[0]:=chr(p^.length);
  213. { to overcome this problem we set the length explicitly }
  214. { with the ending null char }
  215. pc[p^.length+1]:=#0;
  216. consts^.concat(new(pai_string,init_length_pchar(pc,p^.length+2)));
  217. end;
  218. end;
  219. end;
  220. end;
  221. end;
  222. clear_reference(p^.location.reference);
  223. p^.location.reference.symbol:=stringdup(lab2str(p^.lab_str));
  224. p^.location.loc:=LOC_MEM;
  225. end;
  226. {*****************************************************************************
  227. SecondSetCons
  228. *****************************************************************************}
  229. procedure secondsetconst(var p : ptree);
  230. var
  231. hp1 : pai;
  232. lastlabel : plabel;
  233. i : longint;
  234. neededtyp : tait;
  235. begin
  236. {$ifdef SMALLSETORD}
  237. { small sets are loaded as constants }
  238. if psetdef(p^.resulttype)^.settype=smallset then
  239. begin
  240. p^.location.loc:=LOC_MEM;
  241. p^.location.reference.isintvalue:=true;
  242. p^.location.reference.offset:=plongint(p^.value_set)^;
  243. exit;
  244. end;
  245. {$endif}
  246. if psetdef(p^.resulttype)^.settype=smallset then
  247. neededtyp:=ait_const_32bit
  248. else
  249. neededtyp:=ait_const_8bit;
  250. lastlabel:=nil;
  251. { const already used ? }
  252. if not assigned(p^.lab_set) then
  253. begin
  254. { tries to found an old entry }
  255. hp1:=pai(consts^.first);
  256. while assigned(hp1) do
  257. begin
  258. if hp1^.typ=ait_label then
  259. lastlabel:=pai_label(hp1)^.l
  260. else
  261. begin
  262. if (lastlabel<>nil) and (hp1^.typ=neededtyp) then
  263. begin
  264. if (hp1^.typ=ait_const_8bit) then
  265. begin
  266. { compare normal set }
  267. i:=0;
  268. while assigned(hp1) and (i<32) do
  269. begin
  270. if pai_const(hp1)^.value<>p^.value_set^[i] then
  271. break;
  272. inc(i);
  273. hp1:=pai(hp1^.next);
  274. end;
  275. if i=32 then
  276. begin
  277. { found! }
  278. p^.lab_set:=lastlabel;
  279. break;
  280. end;
  281. { leave when the end of consts is reached, so no
  282. hp1^.next is done }
  283. if not assigned(hp1) then
  284. break;
  285. end
  286. else
  287. begin
  288. { compare small set }
  289. if plongint(p^.value_set)^=pai_const(hp1)^.value then
  290. begin
  291. { found! }
  292. p^.lab_set:=lastlabel;
  293. break;
  294. end;
  295. end;
  296. end;
  297. lastlabel:=nil;
  298. end;
  299. hp1:=pai(hp1^.next);
  300. end;
  301. { :-(, we must generate a new entry }
  302. if not assigned(p^.lab_set) then
  303. begin
  304. getdatalabel(lastlabel);
  305. p^.lab_set:=lastlabel;
  306. if (cs_smartlink in aktmoduleswitches) then
  307. consts^.concat(new(pai_cut,init));
  308. consts^.concat(new(pai_label,init(lastlabel)));
  309. if psetdef(p^.resulttype)^.settype=smallset then
  310. begin
  311. move(p^.value_set^,i,sizeof(longint));
  312. consts^.concat(new(pai_const,init_32bit(i)));
  313. end
  314. else
  315. begin
  316. for i:=0 to 31 do
  317. consts^.concat(new(pai_const,init_8bit(p^.value_set^[i])));
  318. end;
  319. end;
  320. end;
  321. clear_reference(p^.location.reference);
  322. p^.location.reference.symbol:=stringdup(lab2str(p^.lab_set));
  323. p^.location.loc:=LOC_MEM;
  324. end;
  325. {*****************************************************************************
  326. SecondNilN
  327. *****************************************************************************}
  328. procedure secondniln(var p : ptree);
  329. begin
  330. p^.location.loc:=LOC_MEM;
  331. p^.location.reference.isintvalue:=true;
  332. p^.location.reference.offset:=0;
  333. end;
  334. end.
  335. {
  336. $Log$
  337. Revision 1.5 1998-12-11 00:03:01 peter
  338. + globtype,tokens,version unit splitted from globals
  339. Revision 1.4 1998/11/06 09:47:29 pierre
  340. * problem of const with ansi fixed
  341. Revision 1.3 1998/11/05 12:02:37 peter
  342. * released useansistring
  343. * removed -Sv, its now available in fpc modes
  344. Revision 1.2 1998/09/07 18:45:56 peter
  345. * update smartlinking, uses getdatalabel
  346. * renamed ptree.value vars to value_str,value_real,value_set
  347. }