1 
2 /*
3 #    Sfront, a SAOL to C translator
4 #    This file: Writes ascii SAOL and SASL files
5 #
6 # Copyright (c) 1999-2006, Regents of the University of California
7 # All rights reserved.
8 #
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions are
11 # met:
12 #
13 #  Redistributions of source code must retain the above copyright
14 #  notice, this list of conditions and the following disclaimer.
15 #
16 #  Redistributions in binary form must reproduce the above copyright
17 #  notice, this list of conditions and the following disclaimer in the
18 #  documentation and/or other materials provided with the distribution.
19 #
20 #  Neither the name of the University of California, Berkeley nor the
21 #  names of its contributors may be used to endorse or promote products
22 #  derived from this software without specific prior written permission.
23 #
24 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #
36 #    Maintainer: John Lazzaro, lazzaro@cs.berkeley.edu
37 */
38 
39 
40 #include "tree.h"
41 #include "parser.tab.h"
42 
43 /******************************************************************/
44 /*               writes saol tokens -- recursive                  */
45 /******************************************************************/
46 
ascsaoltreewrite(tnode * tbranch,int * indent)47 void ascsaoltreewrite(tnode * tbranch, int * indent)
48 
49 {
50   int i;
51   sigsym * sptr;
52 
53   while (tbranch != NULL)
54     {
55       if (tbranch->down != NULL)
56 	ascsaoltreewrite(tbranch->down, indent);
57       else
58 	{
59 	  if ((tbranch->ttype < 0xFF)||(tbranch->ttype == S_PRINTF))
60 	    {
61 	      switch(tbranch->ttype) {  /* space before */
62 	      case S_NUMBER:
63 	      case S_INTGR:
64 		fprintf(orcoutfile," ");
65 		break;
66 	      case S_STRCONST:
67 		fprintf(orcoutfile," \"");
68 		break;
69 	      case S_LC:
70 		*indent += 2;
71 		break;
72 	      case S_RC:
73 		*indent -= 2;
74 		break;
75 	      default:
76 		break;
77 	      }
78 	      if (bitsymin)
79 		{
80 		  sptr = getvsym(&bitsymin,tbranch->val);
81 		  if (sptr)
82 		    fprintf(orcoutfile,"%s",sptr->defnode->val);
83 		  else
84 		    fprintf(orcoutfile,"%s",tbranch->val);
85 		}
86 	      else
87 		fprintf(orcoutfile,"%s",tbranch->val);
88 	      switch(tbranch->ttype) {  /* space after */
89 	      case S_NUMBER:
90 	      case S_INTGR:
91 	      case S_INSTR:
92 	      case S_AOPCODE:
93 	      case S_KOPCODE:
94 	      case S_IOPCODE:
95 	      case S_OPCODE:
96 	      case S_ASIG:
97 	      case S_KSIG:
98 	      case S_IVAR:
99 	      case S_XSIG:
100 	      case S_OPARRAY:
101 	      case S_TABLE:
102 	      case S_TABLEMAP:
103 	      case S_IMPORTS:
104 	      case S_EXPORTS:
105 		fprintf(orcoutfile," ");
106 		break;
107 	      case S_STRCONST:
108 		fprintf(orcoutfile,"\" ");
109 	      default:
110 		break;
111 	      }
112 
113 	      switch(tbranch->ttype) { /* <CR>'s after */
114 	      case S_SEM:
115 	      case S_LC:
116 		fprintf(orcoutfile,"\n");
117 		for (i = 0; i<*indent; i++)
118 		  fprintf(orcoutfile," ");
119 		break;
120 	      case S_RC:
121 		fprintf(orcoutfile,"\n\n");
122 		for (i = 0;i<*indent; i++)
123 		  fprintf(orcoutfile," ");
124 		break;
125 	      default:
126 		break;
127 	      }
128 	    }
129 	}
130       tbranch = tbranch->next;
131     }
132 }
133 
134 
135 /******************************************************************/
136 /*               writes saol tokens -- wrapper                  */
137 /******************************************************************/
138 
ascsaolwrite(void)139 void ascsaolwrite (void)
140 
141 {
142   int indent = 0;
143   tnode * saolroot, * tptr;
144 
145   fprintf(orcoutfile,"\n//\n// automatically generated by sfront\n//\n\n");
146 
147   if (ascsaolptree)
148     ascsaoltreewrite(troot,&indent);
149   else
150     {
151       if (saolfilelist == NULL)
152 	readprepare(BINORC);
153       else
154 	{
155 	  currsaolfile = saolfilelist;
156 	  saollinenumber = 1;
157 	  saolsourcefile = currsaolfile->val;
158 	  if (currsaolfile->filename)
159 	    saolfile = fopen(currsaolfile->filename,"r");
160 	  else
161 	    saolfile = fopen(currsaolfile->val,"r");
162 	}
163 
164       yylex();
165       saolroot = tptr = yylval;
166       while (yylex())
167 	{
168 	  tptr->next = yylval;
169 	  tptr = yylval;
170 	}
171       ascsaoltreewrite(saolroot,&indent);
172     }
173 
174 }
175 
176 /******************************************************************/
177 /*                       writes sasl line                         */
178 /******************************************************************/
179 
slinewrite(tnode * tptr)180 void slinewrite(tnode * tptr)
181 
182 {
183   sigsym * sptr;
184 
185   while (tptr != NULL)
186     {
187       if (bitsymin)
188 	{
189 	  sptr = getvsym(&bitsymin, tptr->val);
190 	  if (sptr)
191 	    fprintf(scooutfile," %s ",sptr->defnode->val);
192 	  else
193 	    fprintf(scooutfile," %s ",tptr->val);
194 	}
195       else
196 	fprintf(scooutfile, " %s ", tptr->val);
197       tptr = tptr->next;
198     }
199   fprintf(scooutfile, "\n");
200 
201 }
202 
203 
204 /******************************************************************/
205 /*                       writes sasl file                         */
206 /******************************************************************/
207 
ascsaslwrite(sasdata * sdata)208 void ascsaslwrite(sasdata * sdata)
209 
210 {
211   tnode * tptr;
212 
213   for (tptr = sdata->temporoot; tptr != NULL; tptr = tptr->next)
214     slinewrite(tptr->down);
215 
216   for (tptr = sdata->tableroot; tptr != NULL; tptr = tptr->next)
217     slinewrite(tptr->down);
218 
219   for (tptr = sdata->controlroot; tptr != NULL; tptr = tptr->next)
220     slinewrite(tptr->down);
221 
222   for (tptr = sdata->instrroot; tptr != NULL; tptr = tptr->next)
223     slinewrite(tptr->down);
224 
225   if (sdata->endtimeval != NULL)
226     fprintf(scooutfile, "%s end\n", sdata->endtimeval);
227 
228 }
229