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