commit 86d9a1621b89d759d311c487e14bbd1adfb3bff1
parent e0551cf953f0fb6e572cfef301d18e25f50a25b2
Author: Ryan Sepassi <rsepassi@gmail.com>
Date: Sun, 26 Apr 2026 22:39:03 -0700
cc/cg+parse: char[] from string-literal initializer (§E.4)
cc-cg fixture: pre-built bv {97,98,99,0} drives cg-emit-global for the
char[4] global; main returns the first byte by deref.
cc-parse fixture: `char s[] = "abc"; char *p = s;` exercises both the
inferred-length+string-literal init path and the array-name decay path.
The string is laid out as raw bytes in .data with a NUL terminator;
parse-init-global infers the array length on first sight.
Diffstat:
4 files changed, 33 insertions(+), 0 deletions(-)
diff --git a/tests/cc-cg/52-init-array-str.expected-exit b/tests/cc-cg/52-init-array-str.expected-exit
@@ -0,0 +1 @@
+97
diff --git a/tests/cc-cg/52-init-array-str.scm b/tests/cc-cg/52-init-array-str.scm
@@ -0,0 +1,24 @@
+;; tests/cc-cg/52-init-array-str.scm — global char[] from a string-literal
+;; initializer. §E.4.
+;;
+;; Models: char s[] = "abc"; return *s; (== 'a' == 97)
+;; The init is a single 4-byte bv {97,98,99,0}.
+
+(let* ((cg (cg-init))
+ (elem %t-i8)
+ (aty (%ctype 'arr 4 1 (cons elem 4)))
+ (s (%sym "s" 'var 'static aty #f))
+ (bv (make-bytevector 4 0))
+ (_a (bytevector-u8-set! bv 0 97))
+ (_b (bytevector-u8-set! bv 1 98))
+ (_c (bytevector-u8-set! bv 2 99)))
+ (cg-emit-global cg s (list bv))
+ (cg-fn-begin cg "main" '() %t-i32)
+ ;; Read the first byte from cc__s. Just take address, cast to char*,
+ ;; deref.
+ (cg-push-sym cg s) (cg-take-addr cg)
+ (cg-cast cg (%ctype 'ptr 8 8 elem))
+ (cg-push-deref cg) (cg-load cg)
+ (cg-return cg)
+ (cg-fn-end cg)
+ (write-bv-fd 1 (cg-finish cg)))
diff --git a/tests/cc-parse/52-init-array-str.c b/tests/cc-parse/52-init-array-str.c
@@ -0,0 +1,7 @@
+// tests/cc-parse/52-init-array-str.c — global char[] from a string
+// literal initializer. §E.4.
+//
+// Returns the first byte of the array (97 = 'a').
+char s[] = "abc";
+char *p = s;
+int main(void) { return *p; }
diff --git a/tests/cc-parse/52-init-array-str.expected-exit b/tests/cc-parse/52-init-array-str.expected-exit
@@ -0,0 +1 @@
+97