60
|
1 #define EXTERN /**/
|
|
2
|
607
|
3 /************************************************************************
|
|
4 ** Copyright (C) 2006 Shinji Kono
|
|
5 ** 連絡先: 琉球大学情報工学科 河野 真治
|
|
6 ** (E-Mail Address: kono@ie.u-ryukyu.ac.jp)
|
|
7 **
|
|
8 ** このソースのいかなる複写,改変,修正も許諾します。ただし、
|
|
9 ** その際には、誰が貢献したを示すこの部分を残すこと。
|
|
10 ** 再配布や雑誌の付録などの問い合わせも必要ありません。
|
|
11 ** 営利利用も上記に反しない範囲で許可します。
|
|
12 ** バイナリの配布の際にはversion messageを保存することを条件とします。
|
|
13 ** このプログラムについては特に何の保証もしない、悪しからず。
|
|
14 **
|
|
15 ** Everyone is permitted to do anything on this program
|
|
16 ** including copying, modifying, improving,
|
|
17 ** as long as you don't try to pretend that you wrote it.
|
|
18 ** i.e., the above copyright notice has to appear in all copies.
|
|
19 ** Binary distribution requires original version messages.
|
|
20 ** You don't have to ask before copying, redistribution or publishing.
|
|
21 ** THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE.
|
|
22 ***********************************************************************/
|
599
|
23 #include <stdio.h>
|
60
|
24 #include "mc.h"
|
599
|
25 #include "mc-parse.h"
|
|
26 #include "mc-codegen.h"
|
60
|
27
|
599
|
28 #include "conv/conv.h"
|
|
29 #include "conv/convdef.h"
|
|
30 #include "conv/cbc2c.h"
|
60
|
31
|
599
|
32 extern void type_print(int type,NMTBL *nptr,FILE *out);
|
|
33 extern void type_print1(int type,NMTBL *nptr,FILE *out,int cont);
|
|
34 extern void sym_print(int type,FILE *out);
|
|
35
|
|
36 static FILE *vout,*svout;
|
|
37 static FILE *null;
|
|
38 static FILE *hout;
|
60
|
39
|
|
40 static void
|
599
|
41 comment_(int c)
|
66
|
42 {
|
599
|
43 fprintf(vout,"%c",c);
|
66
|
44 }
|
|
45
|
|
46 static void
|
599
|
47 open_(char *s)
|
65
|
48 {
|
599
|
49 char *p;
|
|
50 p = make_filename_with_extension(s,"cbc.c");
|
|
51 #if 1
|
65
|
52 vout = fopen(p,"w");
|
599
|
53 #else
|
|
54 vout = stderr;p=p;
|
|
55 #endif
|
|
56 p = make_filename_with_extension(s,"cbc.h");
|
|
57 hout = fopen(p,"w");
|
|
58 null=fopen("/dev/null","w");
|
|
59 if (!vout) error(-1);
|
65
|
60 }
|
|
61
|
|
62 static void
|
599
|
63 conv_()
|
|
64 {
|
|
65 vout=svout;
|
|
66 }
|
|
67
|
|
68 static void
|
|
69 noconv_(int c)
|
|
70 {
|
|
71 svout=vout;
|
|
72 vout=c?null:vout;
|
|
73 }
|
|
74
|
|
75 static void
|
|
76 print_(char *s)
|
60
|
77 {
|
|
78 fprintf(vout,"c: %s\n",s);
|
|
79 }
|
65
|
80
|
|
81 static void
|
599
|
82 close_()
|
65
|
83 {
|
|
84 fclose(vout);
|
|
85 }
|
|
86
|
599
|
87 void
|
|
88 case_begin_(int cases,int def){
|
|
89 fprintf(vout,"case ");
|
|
90 }
|
|
91
|
|
92 void
|
|
93 case_(int cases,int def){
|
|
94 if (def)
|
|
95 fprintf(vout,"default:");
|
|
96 else
|
|
97 fprintf(vout,"%d:",car(cases));
|
|
98 }
|
|
99
|
|
100 void
|
|
101 code_(NMTBL *n){
|
|
102 int args;
|
|
103 args=n->dsp;
|
|
104 fprintf(vout,"code %s(",n->nm);
|
|
105 while(args) {
|
|
106 n=(NMTBL *)caddr(args);
|
|
107 type_print(n->ty,n,vout);
|
|
108 args=cadr(args);
|
|
109 if(args) fprintf(vout,",");
|
|
110 }
|
|
111 fprintf(vout,")");
|
|
112 }
|
|
113
|
|
114 void
|
|
115 code_end_(){
|
|
116 fprintf(vout,"}");
|
|
117 }
|
|
118
|
|
119 void
|
|
120 comma_(){
|
|
121 fprintf(vout,",");
|
|
122 }
|
|
123
|
|
124 void
|
|
125 cond_(){
|
|
126
|
|
127 }
|
|
128
|
|
129 void
|
|
130 cond1_(){
|
|
131 fprintf(vout,"?");
|
|
132 }
|
|
133
|
|
134 void
|
|
135 cond2_(){
|
|
136 fprintf(vout,":");
|
|
137 }
|
|
138
|
|
139 void
|
|
140 cond_end_(){
|
|
141
|
|
142 }
|
|
143
|
|
144 void
|
|
145 decl_data_begin_(){
|
|
146
|
|
147 }
|
|
148
|
|
149 void
|
|
150 decl_data_end_(){
|
|
151
|
|
152 }
|
|
153
|
|
154 void
|
|
155 def_(NMTBL *n){
|
|
156 }
|
|
157
|
|
158 void
|
|
159 dowhile_(){
|
|
160 fprintf(vout,"do");
|
|
161 }
|
|
162
|
|
163 void
|
|
164 dowhile_cond_(){
|
|
165 fprintf(vout,"while(");
|
|
166 }
|
|
167
|
|
168 void
|
|
169 dowhile_end_(){
|
|
170 fprintf(vout,");");
|
|
171 }
|
|
172
|
|
173 int
|
|
174 error_(int n){
|
|
175 return 0;
|
|
176 }
|
|
177
|
|
178 void
|
|
179 extern_(){
|
|
180 fprintf(vout,"extern ");
|
|
181 }
|
|
182
|
|
183 void
|
|
184 for_(){
|
|
185 fprintf(vout,"for(");
|
|
186 }
|
|
187
|
|
188 void
|
|
189 for1_(){
|
|
190 fprintf(vout,";");
|
|
191 }
|
|
192
|
|
193 void
|
|
194 for2_(){
|
|
195 fprintf(vout,";");
|
|
196 }
|
|
197
|
|
198 void
|
|
199 for_body_(){
|
|
200 fprintf(vout,")");
|
|
201 }
|
|
202
|
|
203 void
|
|
204 for_end_(){
|
|
205
|
|
206 }
|
|
207
|
|
208 void
|
|
209 funcall_(int type){
|
|
210 fprintf(vout,"(");
|
|
211 }
|
|
212
|
|
213 void
|
|
214 funcall_args_(){
|
|
215 fprintf(vout,")");
|
|
216 }
|
|
217
|
|
218 void
|
|
219 function_(NMTBL *n,int cont){
|
|
220 int args;
|
|
221 type_print1(cadr(n->ty),0,vout,cont);
|
|
222 fprintf(vout,"%s(",n->nm);
|
|
223 args=n->dsp;
|
|
224 while(args) {
|
|
225 n=(NMTBL *)caddr(args);
|
|
226 if (n==&str_ret) {
|
|
227 args=cadr(args); continue;
|
|
228 }
|
|
229 type_print(n->ty,n,vout);
|
|
230 args=cadr(args);
|
|
231 if(args) {
|
|
232 /* if(caddr(args)==(int)&str_ret) break; */
|
|
233 fprintf(vout,",");
|
|
234 }
|
|
235 }
|
|
236 fprintf(vout,")");
|
|
237 }
|
|
238
|
|
239 void
|
|
240 function_end_(){
|
|
241
|
|
242 }
|
|
243
|
|
244 void
|
|
245 goto_(){
|
|
246 fprintf(vout,"goto ");
|
|
247 }
|
|
248
|
|
249 void
|
|
250 goto_label_(){
|
|
251
|
|
252 }
|
|
253
|
|
254 void
|
|
255 id_(int sym,NMTBL *nptr){
|
|
256 char *p = nptr->nm;
|
|
257 for(;*p && *p!='.';p++) fputc(*p,vout);
|
|
258 }
|
|
259
|
|
260 void
|
|
261 string_(char *s,int len){
|
|
262 int c;
|
|
263 fprintf(vout,"\"");
|
|
264 while((c=*s++)) {
|
|
265 if(c=='\n') fprintf(vout,"\\n");
|
|
266 else if(c=='\r') fprintf(vout,"\\r");
|
|
267 else if(c=='\t') fprintf(vout,"\\t");
|
|
268 else if(c=='\e') fprintf(vout,"\\e");
|
|
269 else if(c=='"') fprintf(vout,"\\\"");
|
|
270 else if(c=='\\') fprintf(vout,"\\\\");
|
|
271 else if(!(' '<=c&&c<=0x7f)) fprintf(vout,"\\%03o",c);
|
|
272 else fprintf(vout,"%c",c);
|
|
273 }
|
|
274 fprintf(vout,"\"");
|
|
275 }
|
|
276
|
|
277 void
|
|
278 const_(int symval){
|
|
279 fprintf(vout,"%d",symval);
|
|
280 }
|
|
281
|
|
282 void
|
|
283 return_f_(){
|
|
284 fprintf(vout,"return");
|
|
285 }
|
|
286
|
|
287 void
|
|
288 defined_(char *s){
|
|
289 fprintf(vout,"defined(%s)",s);
|
|
290 }
|
|
291
|
|
292 void
|
|
293 environment_(){
|
|
294 fprintf(vout,"environment");
|
|
295 }
|
|
296
|
|
297 void
|
|
298 if_(){
|
|
299 fprintf(vout,"if (");
|
|
300 }
|
|
301
|
|
302 void
|
|
303 if_else_(){
|
|
304 fprintf(vout," else ");
|
|
305 }
|
|
306
|
|
307 void
|
|
308 if_endif_(){
|
|
309 }
|
|
310
|
|
311 void
|
|
312 if_then_(){
|
|
313 fprintf(vout,")");
|
|
314 }
|
|
315
|
|
316 void
|
|
317 jump_(int env){
|
|
318
|
|
319 }
|
|
320
|
|
321 void
|
|
322 label_(){
|
|
323 fprintf(vout,"%s:",nptr->nm);
|
|
324 }
|
|
325
|
|
326 void
|
|
327 lpar_(){
|
|
328 fprintf(vout,"(");
|
|
329 }
|
|
330
|
|
331 void
|
|
332 rpar_(){
|
|
333 fprintf(vout,")");
|
|
334 }
|
|
335
|
|
336 void
|
|
337 lbra_(int sym){
|
|
338 fprintf(vout,"[");
|
|
339 }
|
|
340
|
|
341 void
|
|
342 rbra_(int sym){
|
|
343 fprintf(vout,"]");
|
|
344 }
|
|
345
|
|
346 void
|
|
347 lc_(){
|
|
348 fprintf(vout,"{");
|
|
349 }
|
|
350
|
|
351 void
|
|
352 rc_(){
|
|
353 fprintf(vout,"}");
|
|
354 }
|
|
355
|
|
356 void
|
|
357 localvar_end_(){
|
|
358
|
|
359 }
|
|
360
|
|
361 void
|
|
362 op_(int sym){
|
|
363 sym_print(sym,vout);
|
|
364 }
|
|
365
|
|
366 void
|
|
367 postfix_(int sym){
|
|
368 sym_print(sym,vout);
|
|
369 }
|
|
370
|
|
371 void
|
|
372 prefix_(int sym){
|
|
373 sym_print(sym,vout);
|
|
374 }
|
|
375
|
|
376 void
|
|
377 register_(){
|
|
378 fprintf(vout,"register ");
|
|
379 }
|
|
380
|
|
381 void
|
|
382 return_(){
|
|
383 fprintf(vout,"return ");
|
|
384 }
|
|
385
|
|
386 void
|
|
387 return_end_(){
|
|
388 fprintf(vout,";");
|
|
389 }
|
|
390
|
|
391 void
|
|
392 return_type_(int t,NMTBL *nptr,int cont){
|
|
393 type_print1(t,nptr,vout,cont);
|
|
394 }
|
|
395
|
|
396 void
|
|
397 sm_(){
|
|
398 fprintf(vout,";");
|
|
399 }
|
|
400
|
|
401 void
|
|
402 static_(){
|
|
403 fprintf(vout,"static ");
|
|
404 }
|
|
405
|
|
406 void
|
|
407 switch_(){
|
|
408 fprintf(vout,"switch (");
|
|
409 }
|
|
410
|
|
411 void
|
|
412 switch_body_(){
|
|
413 fprintf(vout,")");
|
|
414 }
|
|
415
|
|
416 void
|
|
417 switch_end_(){
|
|
418 }
|
|
419
|
|
420 void
|
|
421 typedef_(){
|
|
422 fprintf(vout,"typedef ");
|
|
423 }
|
|
424
|
|
425 void
|
|
426 type_(int t){
|
|
427 type_print(t,0,vout);
|
|
428 }
|
|
429
|
|
430 void
|
|
431 sdecl_(int s)
|
|
432 {
|
|
433 fprintf(vout,s==UNION?"union ":"struct ");
|
|
434 }
|
|
435
|
|
436 void
|
|
437 while_(){
|
|
438 fprintf(vout,"while (");
|
|
439 }
|
|
440
|
|
441 void
|
|
442 while_body_(){
|
|
443 fprintf(vout,")");
|
|
444 }
|
|
445
|
|
446 void
|
|
447 while_end_(){
|
|
448 }
|
|
449
|
|
450 void
|
|
451 decl_data_()
|
|
452 {
|
|
453 }
|
|
454
|
|
455 void
|
|
456 break_()
|
|
457 {
|
|
458 fprintf(vout,"break;");
|
|
459 }
|
|
460
|
|
461 void
|
|
462 continue_()
|
|
463 {
|
|
464 fprintf(vout,"continue;");
|
|
465 }
|
|
466
|
|
467 /* end */
|