commit 1e5ac4fd2262743177ed36dcae5c3642c208db8f
parent 3af7751e997c2aa02a8d12f0fbd6abbf9b333f8a
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Fri, 2 Dec 2022 13:20:14 -0500
comp/c: allow string literals to be placed in array literals
Diffstat:
2 files changed, 15 insertions(+), 11 deletions(-)
diff --git a/fs/comp/c/pgen.fs b/fs/comp/c/pgen.fs
@@ -15,8 +15,8 @@ require /sys/scratch.fs
\ than writing directly to here because at the time when we want to write the
\ literal, we might be in the middle of code generation. This arena, which is
\ never resetted, gives us a safe space to write literals. The idea is that at
-\ the prelude of each function prelude, we call :reserve to ensure that we
-\ won't allocate a new arena in the middle of the function (this might fail if a
+\ the prelude of each function, we call :reserve to ensure that we won't
+\ allocate a new arena in the middle of the function (this might fail if a
\ single function allocates more than ARENASZ bytes of literals).
Arena :new structbind Arena _litarena
@@ -247,14 +247,18 @@ alias noop parseFactor ( tok -- ) \ forward declaration
r@ to nexttputback
endcase ;
+\ We need to parse the entire list before we begin writing to _litarena if we
+\ want to support the possibility that some of these elements use _litarena
+\ themselves (for example, string literals). *then*, we write.
+MAXLITSZ Stack :new structbind Stack _list
: _, ( sz -- ) case 1 of = c, endof 2 of = 16b , endof , endcase ;
-: parseList ( typesize -- )
- MAXLITSZ _litarena :[ 0 , begin ( typesize )
- nextt parseFactor vmop :isconst#
- vmop arg vmop :init over _, ( )
- ',' readChar? not until ( typesize tok )
- '}' expectChar drop ( ) here _litarena :] ( end start )
- tuck - CELLSZ - ( start len ) over ! constarray>op ;
+: parseList ( typesize -- ) >r \ V1=typesize
+ _list :empty begin ( )
+ nextt parseFactor vmop :isconst# vmop arg _list :push vmop :init
+ ',' readChar? not until ( tok )
+ '}' expectChar _list :count V1 * dup CELLSZ + _litarena :[ ( listsize )
+ , _list :buf( _list :count >r begin ( a ) @+ V1 _, next ( a ) drop
+ _litarena :] constarray>op rdrop ;
\ A factor can be:
\ 1. A constant
diff --git a/fs/tests/comp/c/test.c b/fs/tests/comp/c/test.c
@@ -165,9 +165,9 @@ int funcsig(int a, int b) {
return fn(a, b);
}
-static char *msg = "Hello World!";
+static char *msgs[1] = {"Hello World!"};
void helloworld() {
- stype(msg);
+ stype(msgs[0]);
}
int forsum(int n) {
int i;