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 ***********************************************************************/
|
597
|
23 #include <stdio.h>
|
60
|
24 #include "mc.h"
|
597
|
25 #include "mc-parse.h"
|
|
26 #include "mc-codegen.h"
|
60
|
27
|
597
|
28 #include "conv/conv.h"
|
|
29 #include "conv/convdef.h"
|
|
30 #include "conv/c2cbc.h"
|
60
|
31
|
597
|
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;
|
66
|
39
|
|
40 static void
|
609
|
41 expr_(int c)
|
|
42 {
|
|
43 }
|
|
44
|
|
45 static void
|
651
|
46 bool_(int c)
|
|
47 {
|
|
48 }
|
|
49
|
|
50 static void
|
597
|
51 comment_(int c)
|
65
|
52 {
|
597
|
53 fprintf(vout,"%c",c);
|
65
|
54 }
|
60
|
55
|
|
56 static void
|
597
|
57 open_(char *s)
|
|
58 {
|
|
59 char *p;
|
|
60 p = make_filename_with_extension(s,"cbc.c");
|
|
61 #if 1
|
|
62 vout = fopen(p,"w");
|
|
63 #else
|
|
64 vout = stderr;p=p;
|
|
65 #endif
|
|
66 p = make_filename_with_extension(s,"cbc.h");
|
|
67 hout = fopen(p,"w");
|
|
68 null=fopen("/dev/null","w");
|
|
69 if (!vout) error(-1);
|
|
70 }
|
|
71
|
|
72 static void
|
|
73 conv_()
|
|
74 {
|
|
75 vout=svout;
|
|
76 }
|
|
77
|
|
78 static void
|
|
79 noconv_(int c)
|
|
80 {
|
|
81 svout=vout;
|
|
82 vout=c?null:vout;
|
|
83 }
|
|
84
|
|
85 static void
|
|
86 print_(char *s)
|
60
|
87 {
|
|
88 fprintf(vout,"c: %s\n",s);
|
|
89 }
|
65
|
90
|
|
91 static void
|
597
|
92 close_()
|
65
|
93 {
|
|
94 fclose(vout);
|
|
95 }
|
|
96
|
597
|
97 void
|
|
98 case_begin_(int cases,int def){
|
|
99 fprintf(vout,"case ");
|
|
100 }
|
|
101
|
|
102 void
|
|
103 case_(int cases,int def){
|
|
104 if (def)
|
|
105 fprintf(vout,"default:");
|
|
106 else
|
|
107 fprintf(vout,"%d:",car(cases));
|
|
108 }
|
|
109
|
|
110 void
|
|
111 code_(NMTBL *n){
|
|
112 int args;
|
|
113 args=n->dsp;
|
|
114 fprintf(vout,"code %s(",n->nm);
|
|
115 while(args) {
|
712
|
116 n=ncadddr(args);
|
597
|
117 type_print(n->ty,n,vout);
|
|
118 args=cadr(args);
|
|
119 if(args) fprintf(vout,",");
|
|
120 }
|
|
121 fprintf(vout,")");
|
|
122 }
|
|
123
|
|
124 void
|
|
125 code_end_(){
|
|
126 fprintf(vout,"}");
|
|
127 }
|
|
128
|
|
129 void
|
|
130 comma_(){
|
|
131 fprintf(vout,",");
|
|
132 }
|
|
133
|
|
134 void
|
|
135 cond_(){
|
|
136
|
65
|
137 }
|
|
138
|
597
|
139 void
|
|
140 cond1_(){
|
|
141 fprintf(vout,"?");
|
|
142 }
|
|
143
|
|
144 void
|
|
145 cond2_(){
|
|
146 fprintf(vout,":");
|
|
147 }
|
|
148
|
|
149 void
|
|
150 cond_end_(){
|
|
151
|
|
152 }
|
|
153
|
|
154 void
|
|
155 decl_data_begin_(){
|
|
156
|
|
157 }
|
|
158
|
|
159 void
|
|
160 decl_data_end_(){
|
|
161
|
|
162 }
|
|
163
|
|
164 void
|
|
165 def_(NMTBL *n){
|
|
166 }
|
|
167
|
|
168 void
|
|
169 dowhile_(){
|
|
170 fprintf(vout,"do");
|
|
171 }
|
|
172
|
|
173 void
|
|
174 dowhile_cond_(){
|
|
175 fprintf(vout,"while(");
|
|
176 }
|
|
177
|
|
178 void
|
609
|
179 dowhile_end_(int cond){
|
597
|
180 fprintf(vout,");");
|
|
181 }
|
|
182
|
|
183 int
|
|
184 error_(int n){
|
|
185 return 0;
|
65
|
186 }
|
|
187
|
597
|
188 void
|
|
189 extern_(){
|
|
190 fprintf(vout,"extern ");
|
|
191 }
|
|
192
|
|
193 void
|
|
194 for_(){
|
|
195 fprintf(vout,"for(");
|
|
196 }
|
|
197
|
|
198 void
|
|
199 for1_(){
|
|
200 fprintf(vout,";");
|
|
201 }
|
|
202
|
|
203 void
|
609
|
204 for2_(int cond){
|
597
|
205 fprintf(vout,";");
|
|
206 }
|
|
207
|
|
208 void
|
609
|
209 for_body_(int conti){
|
597
|
210 fprintf(vout,")");
|
|
211 }
|
|
212
|
|
213 void
|
609
|
214 for_end_(int p0,int p1,int e){
|
|
215
|
597
|
216
|
|
217 }
|
|
218
|
|
219 void
|
|
220 funcall_(int type){
|
|
221 fprintf(vout,"(");
|
|
222 }
|
|
223
|
|
224 void
|
609
|
225 funcall_args_(int f, int t, int args){
|
597
|
226 fprintf(vout,")");
|
|
227 }
|
|
228
|
|
229 void
|
|
230 function_(NMTBL *n,int cont){
|
|
231 int args;
|
|
232 type_print1(cadr(n->ty),0,vout,cont);
|
|
233 fprintf(vout,"%s(",n->nm);
|
|
234 args=n->dsp;
|
|
235 while(args) {
|
712
|
236 n=ncadddr(args);
|
597
|
237 if (n==&str_ret) {
|
|
238 args=cadr(args); continue;
|
|
239 }
|
|
240 type_print(n->ty,n,vout);
|
|
241 args=cadr(args);
|
|
242 if(args) {
|
|
243 /* if(caddr(args)==(int)&str_ret) break; */
|
|
244 fprintf(vout,",");
|
|
245 }
|
65
|
246 }
|
597
|
247 fprintf(vout,")");
|
|
248 }
|
|
249
|
|
250 void
|
|
251 function_end_(){
|
|
252
|
|
253 }
|
|
254
|
|
255 void
|
|
256 goto_(){
|
|
257 fprintf(vout,"goto ");
|
|
258 }
|
|
259
|
|
260 void
|
|
261 goto_label_(){
|
|
262
|
|
263 }
|
|
264
|
|
265 void
|
|
266 id_(int sym,NMTBL *nptr){
|
|
267 char *p = nptr->nm;
|
|
268 for(;*p && *p!='.';p++) fputc(*p,vout);
|
|
269 }
|
|
270
|
|
271 void
|
|
272 string_(char *s,int len){
|
|
273 int c;
|
|
274 fprintf(vout,"\"");
|
|
275 while((c=*s++)) {
|
|
276 if(c=='\n') fprintf(vout,"\\n");
|
|
277 else if(c=='\r') fprintf(vout,"\\r");
|
|
278 else if(c=='\t') fprintf(vout,"\\t");
|
|
279 else if(c=='\e') fprintf(vout,"\\e");
|
|
280 else if(c=='"') fprintf(vout,"\\\"");
|
|
281 else if(c=='\\') fprintf(vout,"\\\\");
|
|
282 else if(!(' '<=c&&c<=0x7f)) fprintf(vout,"\\%03o",c);
|
|
283 else fprintf(vout,"%c",c);
|
|
284 }
|
|
285 fprintf(vout,"\"");
|
65
|
286 }
|
|
287
|
597
|
288 void
|
|
289 const_(int symval){
|
|
290 fprintf(vout,"%d",symval);
|
|
291 }
|
|
292
|
|
293 void
|
|
294 return_f_(){
|
|
295 fprintf(vout,"return");
|
|
296 }
|
|
297
|
|
298 void
|
|
299 defined_(char *s){
|
|
300 fprintf(vout,"defined(%s)",s);
|
|
301 }
|
|
302
|
|
303 void
|
|
304 environment_(){
|
|
305 fprintf(vout,"environment");
|
|
306 }
|
|
307
|
|
308 void
|
|
309 if_(){
|
|
310 fprintf(vout,"if (");
|
|
311 }
|
|
312
|
|
313 void
|
|
314 if_else_(){
|
|
315 fprintf(vout," else ");
|
|
316 }
|
|
317
|
|
318 void
|
|
319 if_endif_(){
|
|
320 }
|
|
321
|
|
322 void
|
609
|
323 if_then_(int cond){
|
597
|
324 fprintf(vout,")");
|
|
325 }
|
|
326
|
|
327 void
|
|
328 jump_(int env){
|
|
329
|
|
330 }
|
|
331
|
|
332 void
|
|
333 label_(){
|
|
334 fprintf(vout,"%s:",nptr->nm);
|
|
335 }
|
|
336
|
|
337 void
|
|
338 lpar_(){
|
|
339 fprintf(vout,"(");
|
|
340 }
|
|
341
|
|
342 void
|
|
343 rpar_(){
|
|
344 fprintf(vout,")");
|
|
345 }
|
|
346
|
|
347 void
|
|
348 lbra_(int sym){
|
|
349 fprintf(vout,"[");
|
|
350 }
|
|
351
|
|
352 void
|
|
353 rbra_(int sym){
|
|
354 fprintf(vout,"]");
|
|
355 }
|
|
356
|
|
357 void
|
|
358 lc_(){
|
|
359 fprintf(vout,"{");
|
|
360 }
|
|
361
|
|
362 void
|
|
363 rc_(){
|
|
364 fprintf(vout,"}");
|
|
365 }
|
|
366
|
|
367 void
|
|
368 localvar_end_(){
|
|
369
|
|
370 }
|
|
371
|
|
372 void
|
|
373 op_(int sym){
|
|
374 sym_print(sym,vout);
|
|
375 }
|
|
376
|
|
377 void
|
|
378 postfix_(int sym){
|
|
379 sym_print(sym,vout);
|
65
|
380 }
|
|
381
|
597
|
382 void
|
|
383 prefix_(int sym){
|
|
384 sym_print(sym,vout);
|
|
385 }
|
|
386
|
|
387 void
|
|
388 register_(){
|
|
389 fprintf(vout,"register ");
|
|
390 }
|
|
391
|
|
392 void
|
|
393 return_(){
|
|
394 fprintf(vout,"return ");
|
|
395 }
|
|
396
|
|
397 void
|
609
|
398 return_end_(int e){
|
597
|
399 fprintf(vout,";");
|
|
400 }
|
|
401
|
|
402 void
|
|
403 return_type_(int t,NMTBL *nptr,int cont){
|
|
404 type_print1(t,nptr,vout,cont);
|
|
405 }
|
|
406
|
|
407 void
|
|
408 sm_(){
|
|
409 fprintf(vout,";");
|
|
410 }
|
|
411
|
|
412 void
|
|
413 static_(){
|
|
414 fprintf(vout,"static ");
|
|
415 }
|
|
416
|
|
417 void
|
|
418 switch_(){
|
|
419 fprintf(vout,"switch (");
|
|
420 }
|
|
421
|
|
422 void
|
609
|
423 switch_body_(int v){
|
597
|
424 fprintf(vout,")");
|
|
425 }
|
|
426
|
|
427 void
|
609
|
428 switch_end_(int cases){
|
597
|
429 }
|
|
430
|
|
431 void
|
|
432 typedef_(){
|
|
433 fprintf(vout,"typedef ");
|
|
434 }
|
|
435
|
|
436 void
|
|
437 type_(int t){
|
|
438 type_print(t,0,vout);
|
|
439 }
|
|
440
|
|
441 void
|
|
442 sdecl_(int s)
|
|
443 {
|
|
444 fprintf(vout,s==UNION?"union ":"struct ");
|
|
445 }
|
|
446
|
|
447 void
|
|
448 while_(){
|
|
449 fprintf(vout,"while (");
|
|
450 }
|
|
451
|
|
452 void
|
609
|
453 while_body_(int cond){
|
597
|
454 fprintf(vout,")");
|
|
455 }
|
|
456
|
|
457 void
|
|
458 while_end_(){
|
|
459 }
|
|
460
|
|
461 void
|
|
462 decl_data_()
|
|
463 {
|
|
464 }
|
|
465
|
|
466 void
|
|
467 break_()
|
|
468 {
|
|
469 fprintf(vout,"break;");
|
|
470 }
|
|
471
|
|
472 void
|
|
473 continue_()
|
|
474 {
|
|
475 fprintf(vout,"continue;");
|
|
476 }
|
65
|
477
|
609
|
478
|
65
|
479 /* end */
|