Mercurial > hg > CbC > old > device
changeset 340:0150de6a3244
emit_data moved.
author | kono |
---|---|
date | Sat, 26 Jun 2004 00:26:01 +0900 |
parents | 375d21a2b845 |
children | ca34f02b2056 |
files | mc-code-ia32.c mc-code-mips.c mc-code-powerpc.c mc-code.h mc-codegen.c |
diffstat | 5 files changed, 454 insertions(+), 355 deletions(-) [+] |
line wrap: on
line diff
--- a/mc-code-ia32.c Fri Jun 25 21:28:01 2004 +0900 +++ b/mc-code-ia32.c Sat Jun 26 00:26:01 2004 +0900 @@ -17,6 +17,7 @@ 0 }; +int data_alignment = 0; #define SIZE_OF_INT 4 #define SIZE_OF_SHORT 2 @@ -31,8 +32,9 @@ #define DATA_EMIT_MODE 1 #define RODATA_EMIT_MODE 2 +#define DOT_SIZE 1 + static int output_mode = TEXT_EMIT_MODE; -static int data_alignment = 0; static int creg; static int lreg; @@ -178,6 +180,10 @@ #endif static void code_save_stacks(); static void jcond(int l, char cond); +#if LONGLONG_CODE +static int code_l1(long long d); +static int code_l2(long long d); +#endif #define use_int(reg) if (reg==-1) reg=use_int0() static int use_int0() { lreg = 0; if (!is_int_reg(creg)) { creg = virtual(REG_EBX); regs[creg]=1;} return creg; } @@ -935,35 +941,6 @@ void -ascii(char *s) -{ - printf("\t.string \""); - while(*s) { - if (*s=='\n') - printf("%cn",92); - else if (*s<' ') - printf("%c%03o",92,*s); - else if (*s==34) - printf("%c%c",92,34); - else - printf("%c",*s); - s++; - } - printf("%c\n",34); -} - -static int -emit_string_label() -{ - int lb; - printf(".section\t.rodata\n"); - lb=fwdlabel(); - printf("_%d:\n",lb); - output_mode = RODATA_EMIT_MODE; - return lb; -} - -void code_string(int e1,int creg) { char *s; @@ -1886,6 +1863,35 @@ */ } +extern void +ascii(char *s) +{ + printf("\t.string \""); + while(*s) { + if (*s=='\n') + printf("%cn",92); + else if (*s<' ') + printf("%c%03o",92,*s); + else if (*s==34) + printf("%c%c",92,34); + else + printf("%c",*s); + s++; + } + printf("%c\n",34); +} + +extern int +emit_string_label() +{ + int lb; + printf(".section\t.rodata\n"); + lb=fwdlabel(); + printf("_%d:\n",lb); + output_mode = RODATA_EMIT_MODE; + return lb; +} + void align(int t) { @@ -1896,6 +1902,112 @@ } } +extern void +emit_global(char *name,int t) +{ + printf(".globl\t%s\n",name); + data_mode(name); + align(t); + printf("%s:\n",name); +} + +extern void +emit_space(int sp) +{ + data_mode(0); + printf("\t.space\t%d\n",sp); +} + +extern void +emit_char(int d) +{ + data_mode(0); + printf("\t.byte %d\n",d); +} + +extern void +emit_short(int d) +{ + data_mode(0); + printf("\t.short %d\n",d); +} + +extern void +emit_int(int d) +{ + data_mode(0); + printf("\t.long %d\n",d); +} + +extern void +emit_longlong(int e) +{ +#if LONGLONG_CODE + long long ll = lcadr(e); + data_mode(0); +#if (ENDIAN==0) + printf("\t.long\t0x%x,0x%x\n",code_l1(ll),code_l2(ll)); +#else + printf("\t.long\t0x%x,0x%x\n",code_l2(ll),code_l1(ll)); +#endif +#endif +} + +extern void +emit_double(int e) +{ +#if FLOAT_CODE + double d = dcadr(e); + data_mode(0); +#if (ENDIAN==0) + printf("\t.long\t0x%x,0x%x\n",code_d1(d),code_d2(d)); +#else + printf("\t.long\t0x%x,0x%x\n",code_d2(d),code_d1(d)); +#endif +#endif +} + +extern void +emit_float(int e) +{ +#if FLOAT_CODE + float f = dcadr(e); + data_mode(0); + printf("\t.long\t0x%x\n",*(int *)&f); +#endif +} + +extern void +emit_address(char *s) +{ + data_mode(0); + printf("\t.long %s\n",s); +} + +extern void +emit_label(int labelno) +{ + data_mode(0); + printf("\t.long _%d\n",labelno); +} + +extern void +emit_data_closing(NMTBL *n) +{ +#ifdef DOT_SIZE + int lb; +#endif + if (chk) return; + if (mode==GDECL) { + data_mode(0); +#ifdef DOT_SIZE + lb=fwdlabel(); + printf("_%d:\n",lb); + printf("\t.size\t%s,_%d-%s\n",n->nm,lb,n->nm); +#endif + } +} + #if LONGLONG_CODE static long long ll0 = 1LL; @@ -1915,97 +2027,6 @@ #endif void -emit_data(int e, int t, NMTBL *n) -{ - int l; -#if FLOAT_CODE - double d; - float f; -#endif -#if LONGLONG_CODE - long long ll; -#endif - char *name; - name = n->nm; - if(mode!=GDECL && mode!=STADECL) { - error(-1); return; - } - if (chk) return; - if (n->dsp != -1) { - n->dsp = -1; /* initialized flag */ - printf(".globl\t%s\n",name); - data_mode(name); - align(t); - printf("%s:\n",name); - } else { - data_mode(0); - } - if (t==EMPTY) { - if(car(e)!=CONST) error(-1); - printf("\t.space\t%d\n",cadr(e)); - return; - } - if(car(e)==CONST) { - if (t==CHAR||t==UCHAR) { - printf("\t.byte %d\n",cadr(e)); - if (data_alignment>0) - data_alignment++; - gpc += 1; - } else if (t==SHORT||t==USHORT) { - printf("\t.short %d\n",cadr(e)); - if (data_alignment>0) data_alignment++; - gpc += SIZE_OF_SHORT; - } else { - printf("\t.long %d\n",cadr(e)); - gpc += SIZE_OF_INT; - } -#if LONGLONG_CODE - } else if(t==LONGLONG||t==ULONGLONG) { - ll = lcadr(e); - printf("\t.long\t0x%x,0x%x\n",code_l1(ll),code_l2(ll)); -#endif -#if FLOAT_CODE - } else if(t==DOUBLE) { - d = dcadr(e); - printf("\t.long\t0x%x,0x%x\n",code_d1(d),code_d2(d)); - } else if(t==FLOAT) { - f = dcadr(e); - printf("\t.long\t0x%x\n",*(int *)&f); -#endif - } else if(t!=CHAR) { - gpc += SIZE_OF_INT; - if(car(e)==ADDRESS&&car(cadr(e))==GVAR) { - printf("\t.long %s\n",((NMTBL *)cadr(cadr(e)))->nm); - } else if(car(e)==GVAR) { - printf("\t.long %s\n",((NMTBL *)cadr(e))->nm); - } else if(car(e)==FNAME) { - printf("\t.long %s\n",((NMTBL *)cadr(e))->nm); - } else if(car(e)==STRING) { - if (car(n->ty)!=ARRAY || cadr(n->ty)!=CHAR) { - l = emit_string_label(); - ascii((char *)cadr(e)); - data_mode(0); - printf("\t.long _%d\n",l); - } else - ascii((char *)cadr(e)); - } else error(TYERR); - } else error(TYERR); -} - -void -emit_data_closing(NMTBL *n) -{ - int lb; - if (chk) return; - if (mode==GDECL) { - data_mode(0); - lb=fwdlabel(); - printf("_%d:\n",lb); - printf("\t.size\t%s,_%d-%s\n",n->nm,lb,n->nm); - } -} - -void global_table(void) { NMTBL *n; @@ -2112,14 +2133,14 @@ static double d0 = 1.0; -int +static int code_d1(double d) { int *i = (int *)&d0; int *j = (int *)&d; return (i[1] == 0x3ff00000)?j[0]:j[1]; } -int +static int code_d2(double d) { int *i = (int *)&d0; int *j = (int *)&d;
--- a/mc-code-mips.c Fri Jun 25 21:28:01 2004 +0900 +++ b/mc-code-mips.c Sat Jun 26 00:26:01 2004 +0900 @@ -14,10 +14,14 @@ 0 }; +int data_alignment = 0; + #define TEXT_EMIT_MODE 0 #define DATA_EMIT_MODE 1 #define RODATA_EMIT_MODE 2 +#define DOT_SIZE 1 + static void data_mode(char *name); static void text_mode(int alignment); static void init_ptr_cache(); @@ -30,7 +34,6 @@ static int creg; static int output_mode = TEXT_EMIT_MODE; -static int data_alignment = 0; static FILE *asi; @@ -1468,38 +1471,6 @@ void -ascii(char *s) -{ - printf("\t.ascii \""); - while(*s) { - if (*s=='\n') - printf("%cn",92); - else if (*s<' ') - printf("%c%03o",92,*s); - else if (*s=='\\') - printf("\\\\"); - else if (*s==34) - printf("%c%c",92,34); - else - printf("%c",*s); - s++; - } - printf("\\0%c\n\t.align 2\n",34); -} - -static int -emit_string_label() { - int lb; - - lb=fwdlabel(); - // should put on different segement - printf("\t.rdata\n\t.align 2\n"); - printf("$L_%d:\n",lb); - output_mode = RODATA_EMIT_MODE; - return lb; -} - -void code_string(int e1,int creg) { char *s,*crn; @@ -3251,93 +3222,128 @@ } void -emit_data(int e, int t, NMTBL *n) +ascii(char *s) { - int l; -#if FLOAT_CODE - double d; - float f; -#endif -#if LONGLONG_CODE - long long ll; -#endif - char *name; - name = n->nm; - if(mode!=GDECL && mode!=STADECL) { - error(-1); return; + printf("\t.ascii \""); + while(*s) { + if (*s=='\n') + printf("%cn",92); + else if (*s<' ') + printf("%c%03o",92,*s); + else if (*s=='\\') + printf("\\\\"); + else if (*s==34) + printf("%c%c",92,34); + else + printf("%c",*s); + s++; } - if (chk) return; - if (n->dsp != -1) { - n->dsp = -1; /* initialized flag */ - if (n->sc!=STATIC) - printf(".globl\t%s\n",name); - data_mode(name); - align(t); - printf("%s:\n",name); - } else { - data_mode(0); - } - if (t==EMPTY) { - if(car(e)!=CONST) error(-1); - printf("\t.space\t%d\n",cadr(e)); - return; - } - if(car(e)==CONST) { - if (t==CHAR||t==UCHAR) { - printf("\t.byte %d\n",cadr(e)); - if (data_alignment>0) - data_alignment++; - gpc += 1; - } else if (t==SHORT||t==USHORT) { - printf("\t.short %d\n",cadr(e)); - if (data_alignment>0) data_alignment++; - gpc += SIZE_OF_SHORT; - } else { - printf("\t.long %d\n",cadr(e)); - gpc += SIZE_OF_INT; - } + printf("\\0%c\n\t.align 2\n",34); +} + +extern int +emit_string_label() { + int lb; + + lb=fwdlabel(); + // should put on different segement + printf("\t.rdata\n\t.align 2\n"); + printf("$L_%d:\n",lb); + output_mode = RODATA_EMIT_MODE; + return lb; +} + + +extern void +emit_global(char *name,int t) +{ + printf(".globl\t%s\n",name); + data_mode(name); + align(t); + printf("%s:\n",name); +} + +extern void +emit_space(int sp) +{ + data_mode(0); + printf("\t.space\t%d\n",sp); +} + +extern void +emit_char(int d) +{ + data_mode(0); + printf("\t.byte %d\n",d); +} + +extern void +emit_short(int d) +{ + data_mode(0); + printf("\t.short %d\n",d); +} + +extern void +emit_int(int d) +{ + data_mode(0); + printf("\t.long %d\n",d); +} + +extern void +emit_longlong(int e) +{ #if LONGLONG_CODE - } else if(t==LONGLONG||t==ULONGLONG) { - ll = lcadr(e); + long long ll = lcadr(e); + data_mode(0); #if (ENDIAN==0) printf("\t.long\t0x%x,0x%x\n",code_l1(ll),code_l2(ll)); #else printf("\t.long\t0x%x,0x%x\n",code_l2(ll),code_l1(ll)); #endif #endif +} + +extern void +emit_double(int e) +{ #if FLOAT_CODE - } else if(t==DOUBLE) { - d = dcadr(e); + double d = dcadr(e); + data_mode(0); #if (ENDIAN==0) printf("\t.long\t0x%x,0x%x\n",code_d1(d),code_d2(d)); #else printf("\t.long\t0x%x,0x%x\n",code_d2(d),code_d1(d)); #endif - } else if(t==FLOAT) { - f = dcadr(e); - printf("\t.long\t0x%x\n",*(int *)&f); +#endif +} + +extern void +emit_float(int e) +{ +#if FLOAT_CODE + float f = dcadr(e); + data_mode(0); + printf("\t.long\t0x%x\n",*(int *)&f); #endif - } else if(t!=CHAR) { - gpc += SIZE_OF_INT; - if(car(e)==ADDRESS&&car(cadr(e))==GVAR) { - printf("\t.long %s\n",((NMTBL *)cadr(cadr(e)))->nm); - } else if(car(e)==FNAME) { - printf("\t.long %s\n",((NMTBL *)cadr(e))->nm); - } else if(car(e)==GVAR) { - printf("\t.long %s\n",((NMTBL *)cadr(e))->nm); - } else if(car(e)==STRING) { - if (car(n->ty)!=ARRAY || cadr(n->ty)!=CHAR) { - l = emit_string_label(); - ascii((char *)cadr(e)); - data_mode(0); - printf("\t.long $L_%d\n",l); - } else - ascii((char *)cadr(e)); - } else error(TYERR); - } else error(TYERR); -} - -void +} + +extern void +emit_address(char *s) +{ + data_mode(0); + printf("\t.long %s\n",s); +} + +extern void +emit_label(int labelno) +{ + data_mode(0); + printf("\t.long $L_%d\n",labelno); +} + +extern void emit_data_closing(NMTBL *n) { #ifdef DOT_SIZE @@ -3354,6 +3360,7 @@ } } + static void comm(NMTBL *n) {
--- a/mc-code-powerpc.c Fri Jun 25 21:28:01 2004 +0900 +++ b/mc-code-powerpc.c Sat Jun 26 00:26:01 2004 +0900 @@ -41,7 +41,7 @@ static int creg; static int output_mode = TEXT_EMIT_MODE; -static int data_alignment = 0; +int data_alignment = 0; static int code_disp_label; static int code_setup; @@ -1452,37 +1452,6 @@ void -ascii(char *s) -{ - printf("\t.ascii \""); - while(*s) { - if (*s=='\n') - printf("%cn",92); - else if (*s<' ') - printf("%c%03o",92,*s); - else if (*s=='\\') - printf("\\\\"); - else if (*s==34) - printf("%c%c",92,34); - else - printf("%c",*s); - s++; - } - printf("\\0%c\n\t.align 2\n",34); -} - -static int -emit_string_label() { - int lb; - - printf(".data\t\n.cstring\n\t.align 2\n"); - lb=fwdlabel(); - printf("L_%d:\n",lb); - output_mode = RODATA_EMIT_MODE; - return lb; -} - -void code_string(int e1,int creg) { int lb; @@ -2919,8 +2888,38 @@ */ } -void - +extern void +ascii(char *s) +{ + printf("\t.ascii \""); + while(*s) { + if (*s=='\n') + printf("%cn",92); + else if (*s<' ') + printf("%c%03o",92,*s); + else if (*s=='\\') + printf("\\\\"); + else if (*s==34) + printf("%c%c",92,34); + else + printf("%c",*s); + s++; + } + printf("\\0%c\n\t.align 2\n",34); +} + +extern int +emit_string_label() { + int lb; + + printf(".data\t\n.cstring\n\t.align 2\n"); + lb=fwdlabel(); + printf("L_%d:\n",lb); + output_mode = RODATA_EMIT_MODE; + return lb; +} + +extern void align(int t) { if (t!=CHAR) { @@ -2930,104 +2929,88 @@ } } -void -emit_data(int e, int t, NMTBL *n) +extern void +emit_global(char *name,int t) +{ + printf(".globl\t_%s\n",name); + data_mode(name); + align(t); + printf("_%s:\n",name); +} + +extern void +emit_space(int sp) +{ + data_mode(0); + printf("\t.space\t%d\n",sp); +} + +extern void +emit_char(int d) { - int l; -#if FLOAT_CODE - double d; - float f; -#endif + data_mode(0); + printf("\t.byte %d\n",d); +} + +extern void +emit_short(int d) +{ + data_mode(0); + printf("\t.short %d\n",d); +} + +extern void +emit_int(int d) +{ + data_mode(0); + printf("\t.long %d\n",d); +} + +extern void +emit_longlong(int e) +{ #if LONGLONG_CODE - long long ll; -#endif - char *name; - name = n->nm; - if(mode!=GDECL && mode!=STADECL) { - error(-1); return; - } - if (chk) return; - if (n->dsp != -1) { - n->dsp = -1; /* initialized flag */ - printf(".globl\t_%s\n",name); - data_mode(name); - align(t); - printf("_%s:\n",name); - } else { - data_mode(0); - } - switch(t) { - case EMPTY: - if(car(e)!=CONST) error(-1); - printf("\t.space\t%d\n",cadr(e)); - return; - case CHAR: case UCHAR: - printf("\t.byte %d\n",cadr(e)); - if (data_alignment>0) - data_alignment++; - return; - case SHORT: case USHORT: - printf("\t.short %d\n",cadr(e)); - if (data_alignment>0) data_alignment++; - return; - case INT: case UNSIGNED: case ENUM: - printf("\t.long %d\n",cadr(e)); - return; -#if LONGLONG_CODE - case LONGLONG: case ULONGLONG: - ll = lcadr(e); - printf("\t.long\t0x%x,0x%x\n",code_l2(ll),code_l1(ll)); - return; + data_mode(0); + long long d = lcadr(e); + printf("\t.long\t0x%x,0x%x\n",code_l2(d),code_l1(d)); #endif +} + +extern void +emit_double(int e) +{ #if FLOAT_CODE - case DOUBLE: - d = dcadr(e); - printf("\t.long\t0x%x,0x%x\n",code_d2(d),code_d1(d)); - return; - case FLOAT: - f = dcadr(e); - printf("\t.long\t0x%x\n",*(int *)&f); - return; -#endif - default: - if (t<0) error(-1); -#if BIT_FIELD_CODE - if (car(t)==BIT_FIELD) { - return; - } + data_mode(0); + double d = dcadr(e); + printf("\t.long\t0x%x,0x%x\n",code_d2(d),code_d1(d)); #endif - if (car(t)!=POINTER&&car(t)!=ARRAY) error(-1); - switch(car(e)) { - case CONST: - printf("\t.long %d\n",cadr(e)); - return; - case ADDRESS: - if (car(cadr(e))==GVAR) - printf("\t.long _%s\n",((NMTBL *)cadr(cadr(e)))->nm); - else error(INERR); - return; - case FNAME: - printf("\t.long _%s\n",((NMTBL *)cadr(e))->nm); - return; - case GVAR: - printf("\t.long _%s\n",((NMTBL *)cadr(e))->nm); - return; - case STRING: - if (car(n->ty)!=ARRAY || cadr(n->ty)!=CHAR) { - l = emit_string_label(); - ascii((char *)cadr(e)); - data_mode(0); - printf("\t.long L_%d\n",l); - } else - ascii((char *)cadr(e)); - return; - } - // fprintf(stderr,"# type= %d\n",t); - } - error(INERR); -} - -void +} + +extern void +emit_float(int e) +{ +#if FLOAT_CODE + data_mode(0); + float f = dcadr(e); + printf("\t.long\t0x%x\n",*(int *)&f); +#endif +} + +extern void +emit_address(char *s) +{ + data_mode(0); + printf("\t.long _%s\n",s); +} + +extern void +emit_label(int labelno) +{ + data_mode(0); + printf("\t.long L_%d\n",labelno); +} + +extern void emit_data_closing(NMTBL *n) { #ifdef DOT_SIZE
--- a/mc-code.h Fri Jun 25 21:28:01 2004 +0900 +++ b/mc-code.h Sat Jun 26 00:26:01 2004 +0900 @@ -23,6 +23,7 @@ #define REG_LVAR_OFFSET 2 +extern int data_alignment; extern int code_lassop_p; @@ -117,6 +118,20 @@ extern int code_const_op_p(int op,int v); extern void oprtc(int op,int reg,int v); +extern void align(int t); +extern void emit_global(char *name,int t); +extern void emit_space(int sp); +extern void emit_char(int d); +extern void emit_short(int d); +extern void emit_int(int d); +extern void emit_longlong(int e); +extern void emit_double(int e); +extern void emit_float(int e); +extern void emit_address(char *s); +extern void emit_label(int labelno); +extern int emit_string_label(); +extern void ascii(char *s); + #if FLOAT_CODE /* floating point part */ @@ -219,7 +234,6 @@ extern void free_register(int i) ; extern int pop_register(void); extern void emit_pop_free(int xreg); -extern void emit_data(int e, int t, NMTBL *n); extern void emit_data_closing(NMTBL *n);
--- a/mc-codegen.c Fri Jun 25 21:28:01 2004 +0900 +++ b/mc-codegen.c Sat Jun 26 00:26:01 2004 +0900 @@ -2409,6 +2409,80 @@ return 2; // allow override keep unique } +static void +emit_data(int e, int t, NMTBL *n) +{ + int l; + char *name; + name = n->nm; + if(mode!=GDECL && mode!=STADECL) { + error(-1); return; + } + if (chk) return; + if (n->dsp != -1) { + n->dsp = -1; /* initialized flag */ + emit_global(name,t); + } + switch(t) { + case EMPTY: + if(car(e)!=CONST) error(-1); + emit_space(cadr(e)); + return; + case CHAR: case UCHAR: + emit_char(cadr(e)); + if (data_alignment>0) + data_alignment++; + return; + case SHORT: case USHORT: + emit_short(cadr(e)); + if (data_alignment>0) data_alignment++; + return; + case INT: case UNSIGNED: case ENUM: + emit_int(cadr(e)); + return; + case LONGLONG: case ULONGLONG: + emit_longlong(e); + return; + case DOUBLE: + emit_double(e); + return; + case FLOAT: + emit_float(e); + return; + default: + if (t<0) error(-1); + if (car(t)==BIT_FIELD) { + return; + } + if (car(t)!=POINTER&&car(t)!=ARRAY) error(-1); + switch(car(e)) { + case CONST: + emit_int(cadr(e)); + return; + case ADDRESS: + if (car(cadr(e))==GVAR) + emit_address(((NMTBL *)cadr(cadr(e)))->nm); + else error(INERR); + return; + case FNAME: + case GVAR: + emit_address(((NMTBL *)cadr(e))->nm); + return; + case STRING: + if (car(n->ty)!=ARRAY || cadr(n->ty)!=CHAR) { + l = emit_string_label(); + ascii((char *)cadr(e)); + emit_label(l); + } else + ascii((char *)cadr(e)); + return; + } + // fprintf(stderr,"# type= %d\n",t); + } + error(INERR); +} + + extern int assign_data(int e, int t, NMTBL *n,int offset) {