boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

129-extern-mem-builtins.c (2087B)


      1 /* Calls into mem* compiler-builtin helpers via plain C `extern`
      2  * declarations. memcpy / memcmp / memset are treated as compiler
      3  * builtins: every build process in this tree (cc.scm + libp1pp,
      4  * cc-libc, tcc-cc, tcc-gcc) supplies an implementation, so a TU can
      5  * reach them via bare `extern` decls without per-suite plumbing. The
      6  * test exists to lock cc.scm's `extern`-passthrough rule: bare-name
      7  * extern decls must NOT get prefixed with cc.scm's `cc__` namespace.
      8  */
      9 
     10 extern void *memcpy(void *, const void *, unsigned long);
     11 extern int   memcmp(const void *, const void *, unsigned long);
     12 extern void *memset(void *, int, unsigned long);
     13 
     14 int test_memcpy(void) {
     15     char buf[8];
     16     memcpy(buf, "abcdefg", 8);
     17     if (buf[0] != 'a') return 1;
     18     if (buf[3] != 'd') return 2;
     19     if (buf[6] != 'g') return 3;
     20     if (buf[7] != 0)   return 4;
     21     return 0;
     22 }
     23 
     24 int test_memcmp(void) {
     25     if (memcmp("hello", "hello", 5) != 0) return 1;
     26     if (memcmp("hello", "help!", 5) == 0) return 2;
     27     if (memcmp("a", "b", 1) == 0) return 3;
     28     return 0;
     29 }
     30 
     31 int test_memset(void) {
     32     char buf[6];
     33     memset(buf, 'X', 5);
     34     buf[5] = 0;
     35     if (buf[0] != 'X') return 1;
     36     if (buf[2] != 'X') return 2;
     37     if (buf[4] != 'X') return 3;
     38     if (buf[5] != 0)   return 4;
     39     return 0;
     40 }
     41 
     42 int test_extern_then_define(void) {
     43     /* If a function is declared extern AND later defined here in the
     44      * same TU, the definition's `cc__` prefix takes precedence — the
     45      * scope-bind! merge sets defined?=#t, the call resolves to the
     46      * local definition rather than the bare external symbol. */
     47     extern int helper_local(int);   /* declared local */
     48     return helper_local(7);          /* should call the cc__helper_local below */
     49 }
     50 
     51 int helper_local(int x) {
     52     return x == 7 ? 0 : 99;
     53 }
     54 
     55 int main(int argc, char **argv) {
     56     int r;
     57     if ((r = test_memcpy()))             return 20 + r;
     58     if ((r = test_memcmp()))             return 30 + r;
     59     if ((r = test_memset()))             return 40 + r;
     60     if ((r = test_extern_then_define())) return 50 + r;
     61     return 0;
     62 }