[hackers] [scc] Emit initializers for strings || Roberto E. Vargas Caballero
commit bcb8fd114a9d34c4b06a9f5ed02c5d6f54f35366
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Fri Jan 22 14:53:02 2016 +0100
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Fri Jan 22 14:53:02 2016 +0100
Emit initializers for strings
This modification removes strings from the expressions, which simplifies
the parsing of them, because we can be sure that lines are not too long.
diff --git a/cc1/code.c b/cc1/code.c
index ef33cb0..11e6d46 100644
--- a/cc1/code.c
+++ b/cc1/code.c
_AT_@ -1,4 +1,5 @@
+#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
_AT_@ -189,7 +190,6 @@ emitconst(Node *np)
Type *tp = np->type;
TUINT u;
size_t n;
- Node **p;
switch (tp->op) {
case PTR:
_AT_@ -200,27 +200,7 @@ emitconst(Node *np)
np->type->letter,
(long long) sym->u.i & ones(tp->size));
break;
- case ARY:
- /* TODO: All this code must go out */
- if (sym->flags & ISSTRING) {
- putchar('"');
- n = tp->n.elem;
- for (bp = sym->u.s; n-- > 0; ++bp)
- printf("%02X", (*bp) & 0xFF);
- /* TODO: Why we don't free here? */
- } else if (sym->flags & ISINITLST) {
- n = tp->n.elem;
- for (p = sym->u.init; n--; ++p)
- emitexp(OEXPR, *p);
- free(sym->u.init);
- } else {
- abort();
- }
- break;
- case STRUCT:
- return;
default:
- /* TODO: Handle other kind of constants */
abort();
}
}
_AT_@ -308,6 +288,32 @@ emittype(Type *tp)
}
static void
+emitstring(Symbol *sym, Type *tp)
+{
+ char *bp, *s, *lim;
+ int n;
+
+ bp = bp = sym->u.s;
+ lim = &sym->u.s[tp->n.elem];
+ while (bp < lim) {
+ s = bp;
+ while (isprint(*bp) && bp < lim)
+ ++bp;
+ if ((n = bp - s) > 1)
+ printf("\t#\"%.*s\n", n, s);
+ else
+ bp = s;
+ if (bp == lim)
+ break;
+ do {
+ printf("\t#%c%02X\n",
+ chartype->letter, (*bp++) & 0xFF);
+ } while (!isprint(*bp) && bp < lim);
+ }
+ /* TODO: Why we don't free here? */
+}
+
+static void
emitdesig(Node *np, Type *tp)
{
Symbol *sym;
_AT_@ -321,6 +327,10 @@ emitdesig(Node *np, Type *tp)
if (!np->sym)
goto emit_expression;
sym = np->sym;
+ if (sym->flags & ISSTRING) {
+ emitstring(sym, tp);
+ return;
+ }
if ((sym->flags & ISINITLST) == 0)
goto emit_expression;
}
diff --git a/cc1/expr.c b/cc1/expr.c
index 1342015..c893617 100644
--- a/cc1/expr.c
+++ b/cc1/expr.c
_AT_@ -617,6 +617,12 @@ primary(void)
sym = yylval.sym;
switch (yytoken) {
case STRING:
+ np = constnode(sym);
+ emit(ODECL, sym);
+ emit(OINIT, np);
+ np = decay(varnode(sym));
+ next();
+ break;
case CONSTANT:
np = constnode(sym);
next();
diff --git a/cc1/init.c b/cc1/init.c
index 4fae85a..a2afc73 100644
--- a/cc1/init.c
+++ b/cc1/init.c
_AT_@ -107,11 +107,13 @@ initialize(Type *tp)
}
np = (yytoken == '{') ? initlist(tp) : assign();
- sym = np->sym;
- if (sym && sym->flags&ISSTRING && tp->op == ARY) {
+ if (np->left &&
+ np->left->sym->flags & ISSTRING &&
+ tp->op == ARY) {
+ sym = np->left->sym;
btp = tp->type;
- if (btp != chartype &&
+ if (btp != chartype &&
btp != uchartype &&
btp != schartype) {
errorp("array of inappropriate type initialized from string constant");
diff --git a/cc1/symbol.c b/cc1/symbol.c
index 1cdfa0f..507fe49 100644
--- a/cc1/symbol.c
+++ b/cc1/symbol.c
_AT_@ -230,6 +230,7 @@ newstring(char *s, size_t len)
{
Symbol *sym = newsym(NS_IDEN);
+ sym->id = newid();
sym->flags |= ISSTRING | ISCONSTANT | ISPRIVATE;
sym->u.s = xmalloc(len);
if (s)
diff --git a/cc1/tests/test001.c b/cc1/tests/test001.c
index d2261a6..6c775d4 100644
--- a/cc1/tests/test001.c
+++ b/cc1/tests/test001.c
_AT_@ -9,7 +9,14 @@ F5 I
G6 F5 main
{
\
- X4 "68656C6C6F20776F726C640A00 'P pP cI
+V8 K #13
+Y7 V8
+(
+ #"hello world
+ #K0A
+ #K00
+)
+ X4 Y7 'P pP cI
r #I0
}
*/
diff --git a/cc1/tests/test026.c b/cc1/tests/test026.c
index a2f3958..100092d 100644
--- a/cc1/tests/test026.c
+++ b/cc1/tests/test026.c
_AT_@ -10,8 +10,14 @@ G3 F2 main
\
A4 I y
A6 P p
- A6 "746573743032362E6300 'P :P
- A4 #I1F :I
+V8 K #10
+Y7 V8
+(
+ #"test026.c
+ #K00
+)
+ A6 Y7 'P :P
+ A4 #I25 :I
A4 #I1 :I
A4 #I1 :I
A4 #I1 :I
diff --git a/cc1/tests/test027.c b/cc1/tests/test027.c
index ebc99aa..da62d41 100644
--- a/cc1/tests/test027.c
+++ b/cc1/tests/test027.c
_AT_@ -9,7 +9,13 @@ G3 F2 main
{
\
A5 P p
- A5 "68656C6C6F20697320626574746572207468616E2062796500 'P :P
+V7 K #25
+Y6 V7
+(
+ #"hello is better than bye
+ #K00
+)
+ A5 Y6 'P :P
r A5 _AT_K gI
}
*/
_AT_@ -24,4 +30,3 @@ main(void)
return *p;
}
-
diff --git a/cc1/tests/test028.c b/cc1/tests/test028.c
index a42b7dc..74e0278 100644
--- a/cc1/tests/test028.c
+++ b/cc1/tests/test028.c
_AT_@ -8,7 +8,13 @@ F5 P
G6 F5 foo
{
\
- r "686900 'P
+V8 K #3
+Y10 V8
+(
+ #"hi
+ #K00
+)
+ r Y10 'P
}
*/
diff --git a/cc1/tests/test032.c b/cc1/tests/test032.c
index 5fc3b3c..ae9da0a 100644
--- a/cc1/tests/test032.c
+++ b/cc1/tests/test032.c
_AT_@ -4,13 +4,19 @@ name: TEST032
description: test special characters _AT_ and $ in macro definitions
error:
output:
-F3 I
-G4 F3 main
+F4 I
+G5 F4 main
{
\
-A6 P p
- A6 "54686973206973206120737472696E672024206F722023206F72202323616E64206974206973206F6B202100 'P :P
- r A6 #P0 !I
+V9 K #44
+Y8 V9
+(
+ #"This is a string $ or # or ##and it is ok !
+ #K00
+)
+A7 P p
+ A7 Y8 'P :P
+ r A7 #P0 !I
}
*/
diff --git a/cc1/tests/test056.c b/cc1/tests/test056.c
index dbfdeeb..b8d7524 100644
--- a/cc1/tests/test056.c
+++ b/cc1/tests/test056.c
_AT_@ -20,13 +20,14 @@ G9 S2 s
#K0
#I0
)
-F10 I
-G11 F10 main
+V10 K #0
+G11 V10 m
+(
+)
+F12 I
+G13 F12 main
{
\
-V12 K #0
-A13 V12 m
- A13 :V12
r G9 M7 .V6 'P #P2 +P _AT_K gI gN #N0 !I
}
*/
_AT_@ -42,10 +43,10 @@ struct S {
.d = {[0] = 4, [1] = 6}
};
+char m[] = {};
+
int
main(void)
{
- char m[] = {};
-
return sizeof(m) == s.d[2];
}
Received on Fri Jan 22 2016 - 15:06:56 CET
This archive was generated by hypermail 2.3.0
: Fri Jan 22 2016 - 15:12:27 CET