changeset 920:485f13206916

blocked string termination fix
author kono
date Fri, 11 Apr 2014 14:01:03 +0900
parents 08dcc3b7c39b
children 35b339b36c88
files mc-code-arm.c mc-code-i64.c mc-code-ia32.c mc-code-mips.c mc-code-powerpc.c mc-code-spu.c mc-code.h mc-codegen.c mc-parse.c
diffstat 9 files changed, 101 insertions(+), 85 deletions(-) [+]
line wrap: on
line diff
--- a/mc-code-arm.c	Fri Apr 11 10:45:41 2014 +0900
+++ b/mc-code-arm.c	Fri Apr 11 14:01:03 2014 +0900
@@ -103,7 +103,7 @@
 static void code_assign_input_float_int(int e1,int e2) ;
 #endif
 static void use_input_reg(int reg,int mode);
-static void ascii(char *s);
+static void ascii(char *s, int len);
 
 static int creg;
 static int output_mode = TEXT_EMIT_MODE;
@@ -1918,7 +1918,7 @@
 
     s=n->nm;
     lb = emit_string_label();
-    ascii(s);
+    ascii(s, n->dsp);
     text_mode(2);
 
     disp = search_const(LABEL,lb,&label);
@@ -1933,7 +1933,7 @@
     int l = emit_string_label();
     int i;
     for(i = n->dsp; i; i = cadr(i)) {
-        ascii(scaddr(i));
+        ascii(scaddr(i),car(i));
     }
     emit_label(l);
 }
@@ -1944,7 +1944,7 @@
     int l = emit_string_label();
     int i;
     for(i = e2; i; i = cadr(i)) {
-        ascii(scaddr(i));
+        ascii(scaddr(i),car(i));
     }
     text_mode(2);
     code_label_value(l,reg);
@@ -3647,10 +3647,10 @@
 }
 
 static void
-ascii(char *s)
+ascii(char *s,int len)
 {
     printf("\t.ascii \"");
-    while(*s) {
+    while(len-->0) {
         if (*s=='\n')
             printf("%cn",92);
         else if (*s<' ')
@@ -3678,15 +3678,15 @@
 }
 
 extern void 
-emit_string(char *s,int t)
+emit_string(char *s,int t,long len)
 {
     t = type_value(t);
     if (car(t)==ARRAY &&  
             (type_value(cadr(t))==CHAR||type_value(cadr(t))==UCHAR)) {
-        ascii(s);
+        ascii(s,len);
     } else {
         int l = emit_string_label();
-        ascii(s);
+        ascii(s,len);
         emit_label(l);
     }
     return;
@@ -6035,9 +6035,9 @@
             e1=list3n(FNAME,0,ncaddr(e1));
         } else if (car(e1)==FNAME) {
             e1=list3n(FNAME,0,ncaddr(e1));
-        } else if (car(e1)==STRING) {
+        } else if (car(e1)==STRING) { // STRINGS?
             val = emit_string_label();
-            ascii(ncaddr(e1)->nm);
+            ascii(ncaddr(e1)->nm, ncaddr(e1)->dsp);
             e1=list2(LABEL,val);
         } else if (car(e1)==CONST) {
         } else error(-1);
--- a/mc-code-i64.c	Fri Apr 11 10:45:41 2014 +0900
+++ b/mc-code-i64.c	Fri Apr 11 14:01:03 2014 +0900
@@ -858,7 +858,7 @@
     "%xmm14",
     "%xmm15" };
 
-static void ascii(char *s);
+static void ascii(char *s,int len);
 
 static int use_register(int virt, int real, int move);
 static void shift(char *op, int reg,int creg, int sz);
@@ -2194,7 +2194,7 @@
     use_int(creg);
     s=n->nm;
     lb = emit_string_label();
-    ascii(s);
+    ascii(s,n->dsp);
     if (output_mode==TEXT_EMIT_MODE) {
         printf(".text\n");
     } else {
@@ -2215,7 +2215,7 @@
     int l = emit_string_label();
     int i;
     for(i = n->dsp; i; i = cadr(i)) {
-        ascii(scaddr(i));
+        ascii(scaddr(i),car(i));
     }
     emit_label(l);
 }
@@ -2226,7 +2226,7 @@
     int l = emit_string_label();
     int i;
     for(i = e2; i; i = cadr(i)) {
-        ascii(scaddr(i));
+        ascii(scaddr(i),car(i));
     }
     if (output_mode==TEXT_EMIT_MODE) {
         printf(".text\n");
@@ -3537,14 +3537,13 @@
 }
 
 extern void
-ascii(char *s)
+ascii(char *s,int len)
 {
-#ifdef __APPLE__
-    printf("\t.ascii \"");
-#else
-    printf("\t.string \"");
-#endif
-    while(*s) {
+    if (s[len-1] == 0) 
+	printf("\t.string \"");
+    else
+	printf("\t.ascii \"");
+    while(len-- > 0) {
         if (*s=='\n')
             printf("%cn",92);
         else if (*s<' ')
@@ -3557,11 +3556,7 @@
             printf("%c",*s);
         s++;
     }
-#ifdef __APPLE__
-    printf("\\0%c\n",34);
-#else
     printf("%c\n",34);
-#endif
 }
 
 extern int
@@ -3575,15 +3570,15 @@
 }
 
 extern void 
-emit_string(char *s,int t)
+emit_string(char *s,int t,int len)
 {
     t = type_value(t);
     if (car(t)==ARRAY &&  
             (type_value(cadr(t))==CHAR||type_value(cadr(t))==UCHAR)) {
-        ascii(s);
+        ascii(s,len);
     } else {
         int l = emit_string_label();
-        ascii(s);
+        ascii(s,len);
         emit_label(l);
     }
     return;
@@ -3608,7 +3603,7 @@
 emit_global(NMTBL *n,int a,int e)
 {
     int t = type_value(n->ty);
-    if (e>0 && car(e)==STRING && t>0 && car(t)==ARRAY &&  
+    if (e>0 && (car(e)==STRING || car(e)==STRINGS )&& t>0 && car(t)==ARRAY &&  
             (type_value(cadr(t))==CHAR||type_value(cadr(t))==UCHAR)) {
         cstring_mode();
     } else
@@ -5218,9 +5213,9 @@
             e1=list3n(FNAME,0,ncaddr(e1));
         } else if (car(e1)==FNAME) {
             e1=list3n(FNAME,0,ncaddr(e1));
-        } else if (car(e1)==STRING) {
+        } else if (car(e1)==STRING) { // STRINGS?
             val = emit_string_label();
-            ascii(ncaddr(e1)->nm);
+            ascii(ncaddr(e1)->nm,ncaddr(e1)->dsp);
             e1=list2(LABEL,val);
         } else if (car(e1)==CONST) {
         } else error(-1);
--- a/mc-code-ia32.c	Fri Apr 11 10:45:41 2014 +0900
+++ b/mc-code-ia32.c	Fri Apr 11 14:01:03 2014 +0900
@@ -393,7 +393,7 @@
 static char *reg_name_l[4+1];
 static char *reg_name_w[4+1];
 
-static void ascii(char *s);
+static void ascii(char *s,int len);
 
 static int use_register(int virt, int real, int move);
 static void shift(char *op, int reg,int creg);
@@ -1555,7 +1555,7 @@
     int l = emit_string_label();
     int i;
     for(i = n->dsp; i; i = cadr(i)) {
-        ascii(scaddr(i));
+        ascii(scaddr(i),car(i));
     }
     emit_label(l);
 }
@@ -1566,7 +1566,7 @@
     int l = emit_string_label();
     int i;
     for(i = e2; i; i = cadr(i)) {
-        ascii(scaddr(i));
+        ascii(scaddr(i),car(i));
     }
     if (output_mode==TEXT_EMIT_MODE) {
         printf(".text\n");
@@ -2713,14 +2713,14 @@
 }
 
 extern void
-ascii(char *s)
+ascii(char *s,int len)
 {
 #ifdef __APPLE__
     printf("\t.ascii \"");
 #else
     printf("\t.string \"");
 #endif
-    while(*s) {
+    while(len-->0) {
         if (*s=='\n')
             printf("%cn",92);
         else if (*s<' ')
@@ -2751,15 +2751,15 @@
 }
 
 extern void 
-emit_string(char *s,int t)
+emit_string(char *s,int t,int len)
 {
     t = type_value(t);
     if (car(t)==ARRAY &&  
             (type_value(cadr(t))==CHAR||type_value(cadr(t))==UCHAR)) {
-        ascii(s);
+        ascii(s, len);
     } else {
         int l = emit_string_label();
-        ascii(s);
+        ascii(s, len);
         emit_label(l);
     }
     return;
@@ -4508,9 +4508,9 @@
             e1=list3n(FNAME,0,ncaddr(e1));
         } else if (car(e1)==FNAME) {
             e1=list3n(FNAME,0,ncaddr(e1));
-        } else if (car(e1)==STRING) {
+        } else if (car(e1)==STRING) { // STRINGS?
             val = emit_string_label();
-            ascii(ncaddr(e1)->nm);
+            ascii(ncaddr(e1)->nm,ncaddr(e1)->dsp);
             e1=list2(LABEL,val);
         } else if (car(e1)==CONST) {
         } else error(-1);
--- a/mc-code-mips.c	Fri Apr 11 10:45:41 2014 +0900
+++ b/mc-code-mips.c	Fri Apr 11 14:01:03 2014 +0900
@@ -81,7 +81,7 @@
 static void register_usage(char *s);
 static int get_input_dregister_var0(int i,int reg_var,NMTBL *n,int is_code,int d);
 
-static void ascii(char *s);
+static void ascii(char *s,int len);
 
 static int creg;
 
@@ -1566,7 +1566,7 @@
 
     s=n->nm;
     lb = emit_string_label();
-    ascii(s);
+    ascii(s,n->dsp);
     text_mode(2);
     printf("\tla %s,$L_%d\n",crn,lb);
     set_attr(n,LABEL,lb);
@@ -1578,7 +1578,7 @@
     int l = emit_string_label();
     int i;
     for(i = n->dsp; i; i = cadr(i)) {
-        ascii(scaddr(i));
+        ascii(scaddr(i),car(i));
     }
     emit_label(l);
 }
@@ -1589,7 +1589,7 @@
     int l = emit_string_label();
     int i;
     for(i = e2; i; i = cadr(i)) {
-        ascii(scaddr(i));
+        ascii(scaddr(i),car(i));
     }
     text_mode(2);
     code_label_value(l,reg);
@@ -3317,10 +3317,10 @@
 }
 
 void
-ascii(char *s)
+ascii(char *s,int len)
 {
     printf("\t.ascii \"");
-    while(*s) {
+    while(len-->0) {
         if (*s=='\n')
             printf("%cn",92);
         else if (*s<' ')
@@ -3347,15 +3347,15 @@
 }
 
 extern void 
-emit_string(char *s,int t)
+emit_string(char *s,int t,int len)
 {
     t = type_value(t);
     if (car(t)==ARRAY &&  
             (type_value(cadr(t))==CHAR||type_value(cadr(t))==UCHAR)) {
-        ascii(s);
+        ascii(s,len);
     } else {
         int l = emit_string_label();
-        ascii(s);
+        ascii(s,len);
         emit_label(l);
     }
     return;
@@ -5555,7 +5555,7 @@
             e1=list3n(FNAME,0,ncaddr(e1));
         } else if (car(e1)==STRING) {
             val = emit_string_label();
-            ascii(ncaddr(e1)->nm);
+            ascii(ncaddr(e1)->nm,ncaddr(e1)->dsp);
             e1=list3(LABEL,val,0);
         } else if (car(e1)==CONST) {
         } else error(-1);
--- a/mc-code-powerpc.c	Fri Apr 11 10:45:41 2014 +0900
+++ b/mc-code-powerpc.c	Fri Apr 11 14:01:03 2014 +0900
@@ -209,7 +209,7 @@
 static void local_table(void);
 static void shift(char *op, int creg,int reg);
 static int push_struct(int e4,int t,int arg);
-static void ascii(char *s);
+static void ascii(char *s,int len);
 
 #ifdef __APPLE__
 static char lpfx[] = "L_";
@@ -1973,7 +1973,7 @@
 
     use_int(creg);
     lb = emit_string_label();
-    ascii(n->nm);
+    ascii(n->nm,n->dsp);
     if (output_mode==TEXT_EMIT_MODE) {
         printf(".text\n");
     } else {
@@ -1990,7 +1990,7 @@
     int l = emit_string_label();
     int i;
     for(i = n->dsp; i; i = cadr(i)) {
-        ascii(scaddr(i));
+        ascii(scaddr(i),car(i));
     }
     emit_label(l);
 }
@@ -2001,7 +2001,7 @@
     int l = emit_string_label();
     int i;
     for(i = e2; i; i = cadr(i)) {
-        ascii(scaddr(i));
+        ascii(scaddr(i),car(i));
     }
     if (output_mode==TEXT_EMIT_MODE) {
         printf(".text\n");
@@ -3851,7 +3851,7 @@
 }
 
 extern void
-ascii(char *s)
+ascii(char *s,int len)
 {
     cstring_mode();
 #ifdef __APPLE__
@@ -3859,7 +3859,7 @@
 #else
     printf("\t.string \"");
 #endif
-    while(*s) {
+    while(len-->0) {
         if (*s=='\n')
             printf("%cn",92);
         else if (*s<' ')
@@ -3886,15 +3886,15 @@
 }
 
 extern void 
-emit_string(char *s,int t)
+emit_string(char *s,int t,int len)
 {
     t = type_value(t);
     if (car(t)==ARRAY &&  
             (type_value(cadr(t))==CHAR||type_value(cadr(t))==UCHAR)) {
-        ascii(s);
+        ascii(s,len);
     } else {
         int l = emit_string_label();
-        ascii(s);
+        ascii(s,len);
         emit_label(l);
     }
     return;
@@ -6539,7 +6539,7 @@
             e1=list3n(FNAME,0,ncaddr(e1));
         } else if (car(e1)==STRING) {
             val = emit_string_label();
-            ascii(ncaddr(e1)->nm);
+            ascii(ncaddr(e1)->nm,ncaddr(e1)->dsp);
             e1=list2(LABEL,val);
         } else if (car(e1)==CONST) {
         } else error(-1);
--- a/mc-code-spu.c	Fri Apr 11 10:45:41 2014 +0900
+++ b/mc-code-spu.c	Fri Apr 11 14:01:03 2014 +0900
@@ -96,7 +96,7 @@
 
 #endif
 static void use_input_reg(int reg,int mode);
-static void ascii(char *s);
+static void ascii(char *s,int len);
 
 static int creg;
 static int output_mode = TEXT_EMIT_MODE;
@@ -1300,7 +1300,7 @@
 
     s=n->nm;
     lb = emit_string_label();
-    ascii(s);
+    ascii(s,n->dsp);
     text_mode(2);
 
     printf("\tila\t%s, .LC%d\n",crn,lb);
@@ -1313,7 +1313,7 @@
     int l = emit_string_label();
     int i;
     for(i = n->dsp; i; i = cadr(i)) {
-        ascii(scaddr(i));
+        ascii(scaddr(i),car(i));
     }
     emit_label(l);
 }
@@ -1324,7 +1324,7 @@
     int l = emit_string_label();
     int i;
     for(i = e2; i; i = cadr(i)) {
-        ascii(scaddr(i));
+        ascii(scaddr(i),car(i));
     }
     text_mode(2);
     code_label_value(l,reg);
@@ -2640,10 +2640,10 @@
 }
 
 static void
-ascii(char *s)
+ascii(char *s,int len)
 {
     printf("\t.string \"");
-    while(*s) {
+    while(len-->0) {
         if (*s=='\n')
             printf("%cn",92);
         else if (*s<' ')
@@ -2671,15 +2671,15 @@
 }
 
 extern void 
-emit_string(char *s,int t)
+emit_string(char *s,int t,int len)
 {
     t = type_value(t);
     if (car(t)==ARRAY &&  
             (type_value(cadr(t))==CHAR||type_value(cadr(t))==UCHAR)) {
-        ascii(s);
+        ascii(s,len);
     } else {
         int l = emit_string_label();
-        ascii(s);
+        ascii(s,len);
         emit_label(l);
     }
     return;
@@ -4393,7 +4393,7 @@
             e1=list3n(FNAME,0,ncaddr(e1));
         } else if (car(e1)==STRING) {
             val = emit_string_label();
-            ascii(ncaddr(e1)->nm);
+            ascii(ncaddr(e1)->nm,ncaddr(e1)->dsp);
             e1=list2(LABEL,val);
         } else if (car(e1)==CONST) {
         } else error(-1);
--- a/mc-code.h	Fri Apr 11 10:45:41 2014 +0900
+++ b/mc-code.h	Fri Apr 11 14:01:03 2014 +0900
@@ -180,7 +180,7 @@
 extern void emit_address(char *s,int offset);
 extern void emit_label(int labelno);
 extern int emit_string_label();
-extern void emit_string(char *,int type);
+extern void emit_string(char *,int type,int len);
 void emit_strings(NMTBL *n);
 void code_strings(int e2,int reg);
 
--- a/mc-codegen.c	Fri Apr 11 10:45:41 2014 +0900
+++ b/mc-codegen.c	Fri Apr 11 14:01:03 2014 +0900
@@ -3709,7 +3709,7 @@
             emit_address((ncaddr(e))->nm,0);
             return 1;
         case STRING:
-            emit_string((ncaddr(e))->nm,n->ty);
+            emit_string((ncaddr(e))->nm,n->ty,(ncaddr(e))->dsp);
             return 1;
         case STRINGS:
             emit_strings(ncaddr(e));
--- a/mc-parse.c	Fri Apr 11 10:45:41 2014 +0900
+++ b/mc-parse.c	Fri Apr 11 14:01:03 2014 +0900
@@ -4256,6 +4256,13 @@
     return expr16(e1);
 }
 
+static int
+strlen1(char *s)
+{
+    int i = 0;
+    while(*s++) i++;
+    return i;
+}
 /**
  *  basic term
  */
@@ -4339,7 +4346,8 @@
         break;
     case C_FILE:
         // return current file name
-        nptr=get_name(filep->name0,0,0);
+        nptr=name_space_search(get_name(filep->name0,0,0),STRING);
+	nptr->dsp = strlen1(nptr->nm);
         type=list3(ARRAY,CHAR,nptr->dsp);
         e1=list3n(STRING,nptr->dsp,nptr);
         getsym(0);
@@ -4347,6 +4355,8 @@
     case C_FUNCTION:
         // return current function name
         nptr=get_name(fnptr->nm,0,0);
+        nptr=name_space_search(nptr,STRING);
+	nptr->dsp = strlen1(nptr->nm);
         type=list3(ARRAY,CHAR,nptr->dsp);
         e1=list3n(STRING,nptr->dsp,nptr);
         getsym(0);
@@ -4957,7 +4967,6 @@
 getstring(void)
 {
     char *name = cheap->ptr;
-    int i= 0;
     int c;
     unsigned int hash = 0;
     int strings = 0;
@@ -4969,10 +4978,11 @@
             in_quote = 1;
             getch();  
             while (ch != '"') {
-                if (i>STRSIZE-1) {
-                    strings = glist3s(i,strings,name);
+                if (cheap->ptr - scheap.ptr >STRSIZE-1) {
+                    *cheap->ptr = 0;
+                    cheap = increment_cheap(cheap,&name);
+                    strings = glist3s(cheap->ptr - scheap.ptr-1 ,strings,name);
                     save_cheap(&scheap,cheap);
-                    i = 0;
                     name = cheap->ptr;
                 }
                 if ((c = escape())==0 && ch=='"') {
@@ -4982,31 +4992,42 @@
                 }
                 hash_value(hash, *cheap->ptr = c);
                 cheap = increment_cheap(cheap,&name);
-                i++;
             }
             in_quote = 0;
             getch();
         }
 possible_string_concatenate:
+	*cheap->ptr = 0;
+	cheap = increment_cheap(cheap,&name);
+	char *s = cheap->ptr;
         skipspc();
 	if (ch=='/') 
 	    skip_comment();
+	if (ch=='"') {
+	    // concatenated string does not terminated by 0
+	    if (s != cheap->ptr) {
+		// discontinuous cheap case
+		strings = glist3s(s - scheap.ptr -1,strings,name);
+		save_cheap(&scheap,cheap);
+		name = cheap->ptr;
+	    } else {
+		cheap->ptr--; 
+	    }
+	}
     } while (ch=='"'); 
-    *cheap->ptr = 0;
-    cheap = increment_cheap(cheap,&name);
-    i++;
     if (strings) {
         // too long string is stored in glist3
-        strings = glist3s(i,strings,name);
+        strings = glist3s(cheap->ptr - scheap.ptr,strings,name);
         nptr = get_nptr();
         nptr->dsp = strings = reverse0(strings);
-        int j;
+        int i,j;
         for(j = strings, i = 0; j ; j = cadr(j)) {
             i += car(j);
         }
         symval = i;
         return STRINGS;
     }
+    int i = cheap->ptr - scheap.ptr;
     nptr = name_space_search(hash_search(name,&scheap,i,hash,DEF),STRING);
     // if we already have this, hash_search will reset cheap
     //    should do the same thing for float/double constant