├── Makefile ├── README ├── demo ├── Makefile └── test.c ├── neatdbg ├── Makefile ├── coredump.c ├── elfcore.h └── elfloc.c ├── neatrun ├── Makefile └── neatcc.c └── test ├── Makefile ├── arm.s ├── b01.c ├── b02.c ├── b03.c ├── b04.c ├── b05.c ├── b06.c ├── b07.c ├── b08.c ├── b09.c ├── b10.c ├── b11.c ├── b12.c ├── b13.c ├── b14.c ├── b15.c ├── b16.c ├── b17.c ├── b18.c ├── b19.c ├── b20.c ├── b21.c ├── b22.c ├── t00.c ├── t01.c ├── t02.c ├── t03.c ├── t04.c ├── t05.c ├── t06.c ├── t07.c ├── t08.c ├── t09.c ├── t0a.c ├── t0b.c ├── t0c.c ├── t0d.c ├── t0e.c ├── t0f.c ├── t10.c ├── t11.c ├── t12.c ├── t13.c ├── t14.c ├── t15.c ├── t16.c ├── t17.c ├── t18.c ├── t19.c ├── t1a.c ├── t1b.c ├── t1c.c ├── t1d.c ├── t1e.c ├── t1f.c ├── t20.c ├── t21.c ├── t22.c ├── t23.c ├── t24.c ├── t25.c ├── t26.c ├── t27.c ├── t28.c ├── t29.c ├── t2a.c ├── t2b.c ├── t2c.c ├── t2d.c ├── t2e.c ├── t2f.c ├── t30.c ├── t31.c ├── t32.c ├── t33.c ├── t34.c ├── t35.c ├── t36.c ├── t37.c ├── t38.c ├── t39.c ├── t3a.c ├── t3b.c ├── t3c.c ├── t3d.c ├── t3e.c ├── t3f.c ├── t40.c ├── t41.c ├── t42.c ├── t43.c ├── t44.c ├── t45.c ├── t46.c ├── t47.c ├── t48.c ├── t49.c ├── t4a.c ├── t4b.c ├── t4c.c ├── t4d.c ├── t4e.c ├── t4f.c ├── t50.c ├── t51.c ├── t52.c ├── t53.c ├── t54.c ├── t55.c ├── t55.h ├── t56.c ├── t57.c ├── t58.c ├── t59.c ├── t5a.c ├── t5b.c ├── t5c.c ├── t5d.c ├── t5e.c ├── t5f.c ├── t60.c ├── t61.c ├── t62.c ├── t63.c ├── t64.c ├── t65.c ├── t66.c ├── t67.c ├── t68.c ├── t69.c ├── t6a.c ├── t6b.c ├── t6c.c ├── t6d.c ├── t6e.c ├── t6f.c ├── t70.c ├── t71.c ├── t72.c ├── t73.c ├── t74.c ├── t75.c ├── t76.c ├── t77.c ├── t78.c ├── t79.c ├── t7a.c ├── t7b.c ├── t7c.c ├── t7d.c ├── t7e.c ├── t7f.c ├── t80.c ├── t81.c ├── t82.c ├── t83.c ├── t84.c ├── t85.c ├── t86.c ├── t87.c ├── t88.c ├── t89.c ├── t8a.c ├── t8b.c ├── t8c.c ├── t8d.c ├── t8e.c ├── t8f.c ├── t90.c ├── t91.c ├── t92.c ├── t93.c ├── t94.c ├── t95.c ├── t96.c ├── t97.c ├── t98.c ├── t99.c ├── test.sh ├── x64.s └── x86.s /Makefile: -------------------------------------------------------------------------------- 1 | # Neatcc top-level Makefile 2 | 3 | # output architecture: x64, x86, arm 4 | OUT = x64 5 | CC = cc 6 | 7 | BASE = $(PWD) 8 | 9 | all: help 10 | 11 | help: 12 | @echo "Neatcc top-level makefile" 13 | @echo 14 | @echo " init Initialise git repositories" 15 | @echo " neat Compile the programs" 16 | @echo " boot Compile neatcc using itself" 17 | @echo " pull Update git repositories" 18 | @echo " clean Remove the generated files" 19 | @echo 20 | 21 | init: 22 | @test -d neatcc || git clone https://github.com/aligrudi/neatcc.git 23 | @test -d neatld || git clone https://github.com/aligrudi/neatld.git neatld 24 | @test -d neatlibc || git clone https://github.com/aligrudi/neatlibc.git 25 | @test -d neatas || git clone https://repo.or.cz/neatas.git 26 | 27 | pull: 28 | cd neatcc && git pull 29 | cd neatld && git pull 30 | cd neatlibc && git pull 31 | cd neatas && git pull 32 | git pull 33 | 34 | neat: 35 | # compiling the programs 36 | @cd neatcc && $(MAKE) OUT=$(OUT) CC="$(CC)" 37 | @cd neatld && $(MAKE) OUT=$(OUT) CC="$(CC)" 38 | @cd neatlibc && $(MAKE) OUT=$(OUT) CC=../neatcc/ncc 39 | # setting up neatrun/neatcc 40 | @cd neatrun && $(MAKE) OUT=$(OUT) NCC=$(BASE)/neatcc/ncc \ 41 | NLD=$(BASE)/neatld/nld NLC=$(BASE)/neatlibc clean all 42 | # compiling the rest 43 | @cd neatas && $(MAKE) CC=../neatrun/neatcc OUT=$(OUT) 44 | @cd neatdbg && $(MAKE) CC=../neatrun/neatcc OUT=$(OUT) 45 | 46 | boot: 47 | # the previous version 48 | @cp neatrun/neatcc _neatcc 49 | @cd neatrun && $(MAKE) OUT=$(OUT) CC=../_neatcc \ 50 | NCC=../_ncc NLD=../_nld NLC=../neatlibc clean all 51 | @cp neatcc/ncc _ncc 52 | @cp neatld/nld _nld 53 | @cp neatrun/neatcc _neatcc 54 | # compiling the programs 55 | @cd neatcc && $(MAKE) OUT=$(OUT) CC=../_neatcc clean all 56 | @cd neatld && $(MAKE) OUT=$(OUT) CC=../_neatcc clean all 57 | @cd neatlibc && $(MAKE) OUT=$(OUT) CC=../neatcc/ncc clean all 58 | # setting up neatrun/neatcc 59 | @cd neatrun && $(MAKE) OUT=$(OUT) CC=../_neatcc NCC=$(BASE)/neatcc/ncc \ 60 | NLD=$(BASE)/neatld/nld NLC=$(BASE)/neatlibc clean all 61 | @rm _ncc _nld _neatcc 62 | # compiling the rest 63 | @cd neatas && $(MAKE) CC=../neatrun/neatcc OUT=$(OUT) clean all 64 | @cd neatdbg && $(MAKE) CC=../neatrun/neatcc OUT=$(OUT) clean all 65 | 66 | clean: 67 | @cd neatcc && $(MAKE) clean 68 | @cd neatlibc && $(MAKE) clean 69 | @cd neatas && $(MAKE) clean 70 | @cd neatld && $(MAKE) clean 71 | @cd neatrun && $(MAKE) clean 72 | @cd neatdbg && $(MAKE) clean 73 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | NEATCC TOP-LEVEL MAKEFILE 2 | ========================= 3 | 4 | This directory contains the top-level makefile of neatcc, to 5 | obtain and build neatcc, neatld, and neatlibc. 6 | 7 | $ make init # Check out the repositories 8 | $ make neat # Build the programs 9 | $ cp neatrun/neatcc ~/bin/ # The main executable 10 | $ cd demo && make # A small test 11 | -------------------------------------------------------------------------------- /demo/Makefile: -------------------------------------------------------------------------------- 1 | CC = ../neatrun/neatcc 2 | 3 | CFLAGS = -Wall -O2 4 | LDFLAGS = 5 | 6 | all: test 7 | 8 | %.o: %.c 9 | $(CC) -c $(CFLAGS) $< 10 | test: test.o 11 | $(CC) -o $@ test.o $(LDFLAGS) 12 | clean: 13 | rm -f *.o test 14 | -------------------------------------------------------------------------------- /demo/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | printf("NEATCC!\n"); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /neatdbg/Makefile: -------------------------------------------------------------------------------- 1 | CC = cc 2 | CFLAGS = -O2 -Wall 3 | LDFLAGS = 4 | 5 | all: elfloc coredump 6 | %.o: %.c 7 | $(CC) -c $(CFLAGS) $< 8 | elfloc: elfloc.o 9 | $(CC) $(LDFLAGS) -o $@ elfloc.o 10 | coredump: coredump.o 11 | $(CC) $(LDFLAGS) -o $@ coredump.o 12 | clean: 13 | rm -f *.o elfloc coredump 14 | -------------------------------------------------------------------------------- /neatdbg/coredump.c: -------------------------------------------------------------------------------- 1 | /* 2 | * a simple elf core file backtrace viewer 3 | * 4 | * Copyright (C) 2010-2013 Ali Gholami Rudi 5 | * 6 | * This program is released under the Modified BSD license. 7 | */ 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include "elfcore.h" 17 | 18 | /* simplified elf struct and macro names */ 19 | #ifdef __x86_64__ 20 | # define Elf_Phdr Elf64_Phdr 21 | # define Elf_Ehdr Elf64_Ehdr 22 | #else 23 | # define Elf_Phdr Elf32_Phdr 24 | # define Elf_Ehdr Elf32_Ehdr 25 | #endif 26 | 27 | #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) 28 | 29 | static long readlong(char *mem, unsigned long addr) 30 | { 31 | Elf_Ehdr *ehdr = (void *) mem; 32 | Elf_Phdr *phdr = (void *) (mem + ehdr->e_phoff); 33 | int i; 34 | for (i = 0; i < ehdr->e_phnum; i++) { 35 | unsigned long beg = phdr[i].p_vaddr; 36 | if (addr >= beg && addr < beg + phdr[i].p_filesz) { 37 | unsigned long diff = addr - beg; 38 | return *(long *) (mem + phdr[i].p_offset + diff); 39 | } 40 | } 41 | return 0; 42 | } 43 | 44 | static void dump_prstatus(char *mem, struct elf_prstatus *prs) 45 | { 46 | struct user_regs_struct *regs = (void *) &prs->pr_reg; 47 | unsigned long bp = regs->bp; 48 | printf("ip=%lx, sp=%lx, bp=%lx\n\n", regs->ip, regs->sp, regs->bp); 49 | printf("\t%lx: %lx\n", regs->ip, regs->bp); 50 | while (bp) { 51 | printf("\t%lx: %lx\n", 52 | readlong(mem, bp + sizeof(long)), readlong(mem, bp)); 53 | bp = readlong(mem, bp); 54 | } 55 | printf("\n"); 56 | } 57 | 58 | struct elfnote { 59 | int namesz; 60 | int descsz; 61 | int type; 62 | }; 63 | 64 | static void dumpnote(char *mem, char *note, int len) 65 | { 66 | struct elfnote *elfnote; 67 | char *end = note + len; 68 | while (note < end) { 69 | elfnote = (void *) note; 70 | note += sizeof(*elfnote); 71 | note += ALIGN(elfnote->namesz, 4); 72 | if (elfnote->type == NT_PRSTATUS) 73 | dump_prstatus(mem, (void *) note); 74 | note += ALIGN(elfnote->descsz, 4); 75 | } 76 | } 77 | 78 | static void dumpelf(char *mem) 79 | { 80 | Elf_Ehdr *ehdr = (void *) mem; 81 | Elf_Phdr *phdr = (void *) (mem + ehdr->e_phoff); 82 | if (ehdr->e_type == ET_CORE && phdr[0].p_type == PT_NOTE) 83 | dumpnote(mem, mem + phdr[0].p_offset, phdr[0].p_filesz); 84 | } 85 | 86 | static void die(char *msg) 87 | { 88 | perror(msg); 89 | exit(1); 90 | } 91 | 92 | static long filesize(int fd) 93 | { 94 | struct stat stat; 95 | fstat(fd, &stat); 96 | return stat.st_size; 97 | } 98 | 99 | int main(int argc, char **argv) 100 | { 101 | int fd; 102 | long size; 103 | void *mem; 104 | if (argc < 2) { 105 | printf("Usage: %s path\n", argv[0]); 106 | return 0; 107 | } 108 | fd = open(argv[1], O_RDONLY); 109 | if (fd == -1) 110 | die("open"); 111 | size = filesize(fd); 112 | mem = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); 113 | if (!mem) 114 | die("mmap"); 115 | dumpelf(mem); 116 | munmap(mem, size); 117 | close(fd); 118 | return 0; 119 | } 120 | -------------------------------------------------------------------------------- /neatdbg/elfcore.h: -------------------------------------------------------------------------------- 1 | /* register layout in coredumps */ 2 | #ifdef __x86_64__ 3 | struct user_regs_struct { 4 | unsigned long r15; 5 | unsigned long r14; 6 | unsigned long r13; 7 | unsigned long r12; 8 | unsigned long bp; 9 | unsigned long bx; 10 | unsigned long r11; 11 | unsigned long r10; 12 | unsigned long r9; 13 | unsigned long r8; 14 | unsigned long ax; 15 | unsigned long cx; 16 | unsigned long dx; 17 | unsigned long si; 18 | unsigned long di; 19 | unsigned long orig_ax; 20 | unsigned long ip; 21 | unsigned long cs; 22 | unsigned long flags; 23 | unsigned long sp; 24 | unsigned long ss; 25 | unsigned long fs_base; 26 | unsigned long gs_base; 27 | unsigned long ds; 28 | unsigned long es; 29 | unsigned long fs; 30 | unsigned long gs; 31 | }; 32 | #else 33 | struct user_regs_struct { 34 | unsigned long bx; 35 | unsigned long cx; 36 | unsigned long dx; 37 | unsigned long si; 38 | unsigned long di; 39 | unsigned long bp; 40 | unsigned long ax; 41 | unsigned long ds; 42 | unsigned long es; 43 | unsigned long fs; 44 | unsigned long gs; 45 | unsigned long orig_ax; 46 | unsigned long ip; 47 | unsigned long cs; 48 | unsigned long flags; 49 | unsigned long sp; 50 | unsigned long ss; 51 | }; 52 | #endif 53 | 54 | typedef unsigned long elf_greg_t; 55 | 56 | #define ELF_NGREG (sizeof(struct user_regs_struct) / sizeof(elf_greg_t)) 57 | typedef elf_greg_t elf_gregset_t[ELF_NGREG]; 58 | 59 | struct elf_siginfo { 60 | int si_signo; /* signal number */ 61 | int si_code; /* extra code */ 62 | int si_errno; /* errno */ 63 | }; 64 | 65 | typedef elf_greg_t greg_t; 66 | typedef elf_gregset_t gregset_t; 67 | 68 | struct elf_prstatus { 69 | struct elf_siginfo pr_info; /* Info associated with signal */ 70 | short pr_cursig; /* Current signal */ 71 | unsigned long pr_sigpend; /* Set of pending signals */ 72 | unsigned long pr_sighold; /* Set of held signals */ 73 | pid_t pr_pid; 74 | pid_t pr_ppid; 75 | pid_t pr_pgrp; 76 | pid_t pr_sid; 77 | struct timeval pr_utime; /* User time */ 78 | struct timeval pr_stime; /* System time */ 79 | struct timeval pr_cutime; /* Cumulative user time */ 80 | struct timeval pr_cstime; /* Cumulative system time */ 81 | elf_gregset_t pr_reg; /* GP registers */ 82 | int pr_fpvalid; /* True if math co-processor being used. */ 83 | }; 84 | 85 | #define ELF_PRARGSZ (80) /* Number of chars for args */ 86 | 87 | struct elf_prpsinfo { 88 | char pr_state; /* numeric process state */ 89 | char pr_sname; /* char for pr_state */ 90 | char pr_zomb; /* zombie */ 91 | char pr_nice; /* nice val */ 92 | unsigned long pr_flag; /* flags */ 93 | uid_t pr_uid; 94 | gid_t pr_gid; 95 | pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid; 96 | /* Lots missing */ 97 | char pr_fname[16]; /* filename of executable */ 98 | char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */ 99 | }; 100 | -------------------------------------------------------------------------------- /neatdbg/elfloc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * elfloc, find elf symbols around a virtual address 3 | * 4 | * Copyright (C) 2010-2013 Ali Gholami Rudi 5 | * 6 | * This program is released under the Modified BSD license. 7 | */ 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | /* simplified elf struct and macro names */ 18 | #ifdef __x86_64__ 19 | # define Elf_Phdr Elf64_Phdr 20 | # define Elf_Ehdr Elf64_Ehdr 21 | # define Elf_Shdr Elf64_Shdr 22 | # define Elf_Sym Elf64_Sym 23 | #else 24 | # define Elf_Phdr Elf32_Phdr 25 | # define Elf_Ehdr Elf32_Ehdr 26 | # define Elf_Shdr Elf32_Shdr 27 | # define Elf_Sym Elf32_Sym 28 | #endif 29 | 30 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) 31 | 32 | static long filesize(int fd) 33 | { 34 | struct stat stat; 35 | fstat(fd, &stat); 36 | return stat.st_size; 37 | } 38 | 39 | static char *fileread(char *path) 40 | { 41 | int fd = open(path, O_RDONLY); 42 | int size = filesize(fd); 43 | char *buf = malloc(size); 44 | read(fd, buf, size); 45 | close(fd); 46 | return buf; 47 | } 48 | 49 | static long htol(char *t) 50 | { 51 | long n = 0; 52 | while (*t && isalnum(*t)) { 53 | n <<= 4; 54 | if (*t >= '0' && *t <= '9') 55 | n |= *t - '0'; 56 | if (*t >= 'a' && *t <= 'f') 57 | n |= *t - 'a' + 10; 58 | t++; 59 | } 60 | return n; 61 | } 62 | 63 | static Elf_Ehdr *ehdr; 64 | static Elf_Phdr *phdr; 65 | static Elf_Shdr *shdr; 66 | static Elf_Sym *syms; 67 | static int nsyms; 68 | static char *symstr; 69 | 70 | static void init_data(void *mem) 71 | { 72 | int i; 73 | ehdr = mem; 74 | shdr = ehdr->e_shnum ? mem + ehdr->e_shoff : NULL; 75 | phdr = ehdr->e_phnum ? mem + ehdr->e_phoff : NULL; 76 | 77 | for (i = 0; i < ehdr->e_shnum; i++) { 78 | if (shdr[i].sh_type == SHT_SYMTAB) { 79 | syms = mem + shdr[i].sh_offset; 80 | nsyms = shdr[i].sh_size / sizeof(syms[0]); 81 | symstr = mem + shdr[shdr[i].sh_link].sh_offset; 82 | } 83 | } 84 | } 85 | 86 | static int mem_to_off(unsigned long vaddr) 87 | { 88 | int i; 89 | for (i = 0; i < ehdr->e_phnum; i++) { 90 | unsigned long base = phdr[i].p_vaddr; 91 | if (vaddr >= base && vaddr <= base + phdr[i].p_memsz) 92 | return phdr[i].p_offset + MIN(phdr[i].p_filesz, 93 | vaddr - base); 94 | } 95 | return 0; 96 | } 97 | 98 | static int sym_to_mem(Elf_Sym *sym) 99 | { 100 | if (sym->st_shndx == SHN_UNDEF || sym->st_shndx == SHN_COMMON) 101 | return 0; 102 | return sym->st_value; 103 | } 104 | 105 | static Elf_Sym *get_sym(char *name) 106 | { 107 | int i; 108 | for (i = 0; i < nsyms; i++) 109 | if (!strcmp(name, symstr + syms[i].st_name) && 110 | syms[i].st_shndx != SHN_UNDEF) 111 | return &syms[i]; 112 | return NULL; 113 | } 114 | 115 | static int sec_region(unsigned long vaddr, unsigned long *beg, unsigned long *end) 116 | { 117 | int i; 118 | for (i = 0; i < ehdr->e_phnum; i++) { 119 | unsigned long base = phdr[i].p_vaddr; 120 | if (vaddr >= base && vaddr < base + phdr[i].p_memsz) { 121 | *beg = base; 122 | *end = base + phdr[i].p_filesz; 123 | return 0; 124 | } 125 | } 126 | return 1; 127 | } 128 | 129 | static void boundaries(unsigned long vaddr) 130 | { 131 | char *beg_sym = NULL; 132 | char *end_sym = NULL; 133 | unsigned long beg = 0l; 134 | unsigned long end = -1l; 135 | int i; 136 | sec_region(vaddr, &beg, &end); 137 | for (i = 0; i < nsyms; i++) { 138 | unsigned long symaddr = sym_to_mem(&syms[i]); 139 | char *name = symstr + syms[i].st_name; 140 | if (!symaddr || !*name) 141 | continue; 142 | if (symaddr <= vaddr && symaddr >= beg) { 143 | beg_sym = name; 144 | beg = symaddr; 145 | } 146 | if (symaddr > vaddr && symaddr <= end) { 147 | end_sym = name; 148 | end = symaddr; 149 | } 150 | } 151 | printf("%s\t+%lx\t%lx\t%d\n", 152 | beg_sym ? beg_sym : "NULL", 153 | vaddr - beg, beg, mem_to_off(beg)); 154 | printf("%s\t-%lx\t%lx\t%d\n", 155 | end_sym ? end_sym : "NULL", 156 | end - vaddr, end, mem_to_off(end)); 157 | } 158 | 159 | int main(int argc, char **argv) 160 | { 161 | unsigned long addr; 162 | char *name; 163 | char *buf; 164 | if (argc < 3) { 165 | printf("usage: %s addr elf\n", argv[0]); 166 | return 1; 167 | } 168 | buf = fileread(argv[argc - 1]); 169 | init_data(buf); 170 | name = argv[1]; 171 | if (name[0] == '=') { 172 | Elf_Sym *sym = get_sym(name + 1); 173 | if (!sym) 174 | return 1; 175 | addr = sym_to_mem(sym); 176 | } else { 177 | addr = htol(argv[1]); 178 | } 179 | if (syms) 180 | boundaries(addr); 181 | free(buf); 182 | return 0; 183 | } 184 | -------------------------------------------------------------------------------- /neatrun/Makefile: -------------------------------------------------------------------------------- 1 | NCC = "/path/to/neatcc/ncc" 2 | NLD = "/path/to/neatld/nld" 3 | NLC = "/path/to/neatlibc/" 4 | 5 | CC = cc 6 | CFLAGS = -DNCC=\"$(NCC)\" -DNLD=\"$(NLD)\" -DNLC=\"$(NLC)\" 7 | LDFLAGS = 8 | 9 | all: neatcc 10 | 11 | %.o: %.c 12 | $(CC) $(CFLAGS) -c $< 13 | neatcc: neatcc.o 14 | $(CC) -o $@ neatcc.o $(LDFLAGS) 15 | clean: 16 | rm -f *.o neatcc 17 | -------------------------------------------------------------------------------- /neatrun/neatcc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define MAXARGS 512 7 | #define LDOPTS "lLsgmpe" /* neatld options */ 8 | #define CCOPTS "cIOEDW" /* neatcc options */ 9 | #define AROPTS "IDOlLome" /* options with an argument */ 10 | 11 | static void die(char *msg) 12 | { 13 | write(2, msg, strlen(msg)); 14 | exit(1); 15 | } 16 | 17 | int main(int argc, char *argv[], char *envp[]) 18 | { 19 | char *ccargs[MAXARGS]; /* neatcc options */ 20 | char *ldargs[MAXARGS]; /* neatld options */ 21 | int opt[MAXARGS]; /* opt[i] is one if argv[i] is an option */ 22 | int optarg[MAXARGS]; /* argv[i + 1] is an argument of argv[i] */ 23 | int ccargc = 0; /* number of neatcc options */ 24 | int ldargc = 0; /* number of neatld options */ 25 | int nold = 0; /* compile only */ 26 | int i; 27 | if (argc < 2) 28 | die("neatcc: ncc/nld wrapper\n"); 29 | /* looking for options that prevent linking + initialize opt[] and optarg[] */ 30 | for (i = 1; i < argc; i++) { 31 | opt[i] = argv[i][0] == '-' ? argv[i][1] : 0; 32 | optarg[i] = opt[i] > 0 && strchr(AROPTS, opt[i]) && !argv[i][2]; 33 | nold = nold || opt[i] == 'c' || opt[i] == 'E'; 34 | } 35 | /* initialize compiler options */ 36 | ccargs[ccargc++] = NCC; 37 | ccargs[ccargc++] = "-Dfloat=long"; 38 | ccargs[ccargc++] = "-Ddouble=long"; 39 | ccargs[ccargc++] = "-D__extension__="; 40 | ccargs[ccargc++] = "-I" NLC; 41 | for (i = 1; i < argc; i += 1 + optarg[i]) { 42 | if (opt[i] && strchr(CCOPTS, opt[i]) || (nold && opt[i] == 'o')) { 43 | ccargs[ccargc++] = argv[i]; 44 | if (optarg[i]) 45 | ccargs[ccargc++] = argv[i + 1]; 46 | } 47 | } 48 | /* invoke neatcc for every .c file */ 49 | for (i = 1; i < argc; i += 1 + optarg[i]) { 50 | char *arg = argv[i]; 51 | char *end = strchr(arg, '\0'); 52 | if (!opt[i] && arg + 2 < end && end[-2] == '.' && end[-1] == 'c') { 53 | int st; 54 | ccargs[ccargc] = arg; 55 | ccargs[ccargc + 1] = NULL; 56 | if (fork() == 0) { 57 | execve(ccargs[0], ccargs, envp); 58 | die("neatcc: could not find ncc\n"); 59 | return 1; 60 | } 61 | if (wait(&st) < 0 || WEXITSTATUS(st)) 62 | return 1; 63 | end[-1] = 'o'; /* for linker */ 64 | } 65 | } 66 | /* invoke neatld */ 67 | if (!nold) { 68 | ldargs[ldargc++] = NLD; 69 | for (i = 1; i < argc; i += 1 + optarg[i]) { 70 | if (!opt[i] || !strchr(CCOPTS, opt[i])) { 71 | ldargs[ldargc++] = argv[i]; 72 | if (optarg[i]) 73 | ldargs[ldargc++] = argv[i + 1]; 74 | } 75 | } 76 | ldargs[ldargc++] = NLC "/start.o"; 77 | ldargs[ldargc++] = NLC "/libc.a"; 78 | ldargs[ldargc] = NULL; 79 | execve(ldargs[0], ldargs, envp); 80 | die("neatcc: could not find nld\n"); 81 | } 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /test/Makefile: -------------------------------------------------------------------------------- 1 | # output architecture: x64, x86, arm 2 | OUT = x64 3 | 4 | CC = ../neatcc/ncc # the neatcc binary 5 | LD = ../neatld/nld # the neatld binary 6 | 7 | # default assemblers 8 | ASx64 = fasm 9 | ASx86 = fasm 10 | ASarm = neatas 11 | 12 | AS = $(AS$(OUT)) 13 | 14 | all: $(OUT).o \ 15 | b01.x b02.x b03.x b04.x b05.x b06.x b07.x b08.x b09.x b10.x \ 16 | b11.x b12.x b13.x b14.x b15.x b16.x b17.x b18.x b19.x b20.x \ 17 | b21.x b22.x \ 18 | t00.x t01.x t02.x t03.x t04.x t05.x t06.x t07.x \ 19 | t08.x t09.x t0a.x t0b.x t0c.x t0d.x t0e.x t0f.x \ 20 | t10.x t11.x t12.x t13.x t14.x t15.x t16.x t17.x \ 21 | t18.x t19.x t1a.x t1b.x t1c.x t1d.x t1e.x t1f.x \ 22 | t20.x t21.x t22.x t23.x t24.x t25.x t26.x t27.x \ 23 | t28.x t29.x t2a.x t2b.x t2c.x t2d.x t2e.x t2f.x \ 24 | t30.x t31.x t32.x t33.x t34.x t35.x t36.x t37.x \ 25 | t38.x t39.x t3a.x t3b.x t3c.x t3d.x t3e.x t3f.x \ 26 | t40.x t41.x t42.x t43.x t44.x t45.x t46.x t47.x \ 27 | t48.x t49.x t4a.x t4b.x t4c.x t4d.x t4e.x t4f.x \ 28 | t50.x t51.x t52.x t53.x t54.x t55.x t56.x t57.x \ 29 | t58.x t59.x t5a.x t5b.x t5c.x t5d.x t5e.x t5f.x \ 30 | t60.x t61.x t62.x t63.x t64.x t65.x t66.x t67.x \ 31 | t68.x t69.x t6a.x t6b.x t6c.x t6d.x t6e.x t6f.x \ 32 | t70.x t71.x t72.x t73.x t74.x t75.x t76.x t77.x \ 33 | t78.x t79.x t7a.x t7b.x t7c.x t7d.x t7e.x t7f.x \ 34 | t80.x t81.x t82.x t83.x t84.x t85.x t86.x t87.x \ 35 | t88.x t89.x t8a.x t8b.x t8c.x t8d.x t8e.x t8f.x \ 36 | t90.x t91.x t92.x t93.x t94.x t95.x t96.x t97.x \ 37 | t98.x t99.x 38 | 39 | .SECONDARY: 40 | %.o: %.s 41 | $(AS) $^ $@ 42 | %.o: %.c 43 | $(CC) -c $< 44 | %.x: %.o $(OUT).o 45 | $(LD) -o $@ $^ 46 | clean: 47 | rm -f *.o t??.x b??.x 48 | -------------------------------------------------------------------------------- /test/arm.s: -------------------------------------------------------------------------------- 1 | .global _start 2 | .extern main 3 | _start: 4 | mov fp, #0 5 | ldr r0, [sp], #4 6 | mov r1, sp 7 | add r2, r1, r0, lsl #2 8 | add r2, r2, #4 9 | bl main 10 | mov r7, #1 11 | swi #0 12 | -------------------------------------------------------------------------------- /test/b01.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | return 1; 4 | } 5 | -------------------------------------------------------------------------------- /test/b02.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | return 2; 4 | } 5 | -------------------------------------------------------------------------------- /test/b03.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int i; 4 | i = 3; 5 | return i; 6 | } 7 | -------------------------------------------------------------------------------- /test/b04.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int i = 4; 4 | return i; 5 | } 6 | -------------------------------------------------------------------------------- /test/b05.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | if (0) 4 | return 4; 5 | return 5; 6 | } 7 | -------------------------------------------------------------------------------- /test/b06.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int i = 0x12345678; 4 | return i - 0x12345672; 5 | } 6 | -------------------------------------------------------------------------------- /test/b07.c: -------------------------------------------------------------------------------- 1 | int g(void) 2 | { 3 | return 7; 4 | } 5 | 6 | int main(void) 7 | { 8 | return g(); 9 | } 10 | -------------------------------------------------------------------------------- /test/b08.c: -------------------------------------------------------------------------------- 1 | int g(int i) 2 | { 3 | return i; 4 | } 5 | 6 | int main(void) 7 | { 8 | return g(8); 9 | } 10 | -------------------------------------------------------------------------------- /test/b09.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | return h(9); 4 | } 5 | 6 | int h(int i) 7 | { 8 | return i; 9 | } 10 | -------------------------------------------------------------------------------- /test/b10.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | return 1 + 10 - 1; 4 | } 5 | -------------------------------------------------------------------------------- /test/b11.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int a = 3; 4 | int b = 2; 5 | a = a + 1; 6 | b = b + a; 7 | return a + b + 1; 8 | } 9 | -------------------------------------------------------------------------------- /test/b12.c: -------------------------------------------------------------------------------- 1 | int g(int p1, int p2, int p3, int p4, int p5, int p6) 2 | { 3 | return p1 + p2 + p3 + p4 + p5 + p6; 4 | } 5 | 6 | int main(void) 7 | { 8 | return g(7, 1, 1, 1, 1, 1); 9 | } 10 | -------------------------------------------------------------------------------- /test/b13.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int a = 12; 4 | int *b; 5 | b = &a; 6 | *b = *b + 1; 7 | return *b; 8 | } 9 | -------------------------------------------------------------------------------- /test/b14.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int a = 1; 4 | int b = 0; 5 | return a ? 13 + (b ? 0 : 1) : 0; 6 | } 7 | -------------------------------------------------------------------------------- /test/b15.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int a[3]; 4 | a[0] = 1; 5 | *(a + a[0]) = 2; 6 | a[2] = 12; 7 | a[1] = (a + 3) - (a + 1); 8 | return *a + a[1] + *(a + 2); 9 | } 10 | -------------------------------------------------------------------------------- /test/b16.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int result; 4 | if (2 < 1) 5 | result = 30; 6 | else 7 | result = 10; 8 | return result + (1 < 2 ? 6 : 40); 9 | } 10 | -------------------------------------------------------------------------------- /test/b17.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int a = 7; 4 | int b = a++; 5 | return a++ + ++b + 1; 6 | } 7 | -------------------------------------------------------------------------------- /test/b18.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int result = 0; 4 | int i; 5 | for (i = 0; i < 7; i++) 6 | result = result + i; 7 | return result - 3; 8 | } 9 | -------------------------------------------------------------------------------- /test/b19.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int a = 10; 4 | int b; 5 | b = a-- + 1; 6 | return --b + a--; 7 | } 8 | -------------------------------------------------------------------------------- /test/b20.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int a = 10; 4 | int b = 20; 5 | return (a * b) / 10 - 2 + 9 % 7; 6 | } 7 | -------------------------------------------------------------------------------- /test/b21.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int a = 10; 4 | int b = 10; 5 | int c = 20; 6 | if (a < b) 7 | return 0; 8 | if (a != b) 9 | return 1; 10 | if (a == c) 11 | return 2; 12 | if (a > c) 13 | return 3; 14 | if (!a) 15 | return 4; 16 | return (a + b) != c ? 0 : 21; 17 | } 18 | -------------------------------------------------------------------------------- /test/b22.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int a = 1 << 4; 4 | int b = 22 >> 2; 5 | return a + b + 1 << 0; 6 | } 7 | -------------------------------------------------------------------------------- /test/t00.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | return 0; 4 | } 5 | -------------------------------------------------------------------------------- /test/t01.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | char a[10]; 4 | long sum = 0; 5 | int i; 6 | for (i = 0; i < 10; i++) 7 | a[i] = i; 8 | for (i = 0; i < 10; i++) 9 | sum = sum + a[i]; 10 | return (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9) != sum; 11 | } 12 | -------------------------------------------------------------------------------- /test/t02.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int a = 1; 4 | int b = 7; 5 | a += 5; 6 | a += 5; 7 | a -= b; 8 | return a != 4; 9 | } 10 | -------------------------------------------------------------------------------- /test/t03.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int a = 1; 4 | int b = -a + -a; 5 | return (a + b + -2) != -3; 6 | } 7 | -------------------------------------------------------------------------------- /test/t04.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int a = 8; 4 | int b = 4; 5 | return (a & (b | 15)) != a; 6 | } 7 | -------------------------------------------------------------------------------- /test/t05.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int ret = 0; 4 | int a = 10; 5 | int b = 10 + (1 < 2); 6 | if (a < b && a == 10) 7 | ret++; 8 | if (a == b || a == 11) 9 | ret += 10; 10 | if (a == b || a == 10) 11 | ret++; 12 | return (ret + !0) != 3; 13 | } 14 | -------------------------------------------------------------------------------- /test/t06.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int a = 0x100 | 0x001; 4 | int b = 'a'; 5 | return !(b == 'a' && a == 0x101); 6 | } 7 | -------------------------------------------------------------------------------- /test/t07.c: -------------------------------------------------------------------------------- 1 | int a; 2 | 3 | int g(void) 4 | { 5 | return a; 6 | } 7 | 8 | int main(void) 9 | { 10 | a += 2; 11 | return a != g() || a != 2 ? 1 : 0; 12 | } 13 | -------------------------------------------------------------------------------- /test/t08.c: -------------------------------------------------------------------------------- 1 | int mstrcmp(char *s, char *r) 2 | { 3 | while (*s && *s == *r) 4 | s++, r++; 5 | return *s - *r; 6 | } 7 | 8 | int main(void) 9 | { 10 | if (!mstrcmp("abc", "123")) 11 | return 1; 12 | if (!mstrcmp("123", "abc")) 13 | return 2; 14 | if (mstrcmp("abc", "abc")) 15 | return 3; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /test/t09.c: -------------------------------------------------------------------------------- 1 | int mstrcmp(char *s, char *r) 2 | { 3 | while (*s && *s == *r) 4 | s++, r++; 5 | return *s - *r; 6 | } 7 | 8 | int main(void) 9 | { 10 | char *s1 = "abc"; 11 | char *s2 = "123"; 12 | char *s3 = "abc"; 13 | if (!mstrcmp(s1, s2)) 14 | return 1; 15 | if (!mstrcmp(s2, s3)) 16 | return 2; 17 | if (mstrcmp(s1, s3)) 18 | return 3; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /test/t0a.c: -------------------------------------------------------------------------------- 1 | int a[10]; 2 | 3 | int *g(void) 4 | { 5 | return a; 6 | } 7 | 8 | int main(void) 9 | { 10 | int i; 11 | for (i = 0; i < 10; i++) 12 | a[i] = i; 13 | return g()[3] != 3; 14 | } 15 | -------------------------------------------------------------------------------- /test/t0b.c: -------------------------------------------------------------------------------- 1 | struct t { 2 | int a; 3 | int b; 4 | int c; 5 | }; 6 | 7 | int main(void) 8 | { 9 | struct t t; 10 | t.a = 2; 11 | t.b = t.a + 3; 12 | t.c = t.a + t.b; 13 | return t.c != 7; 14 | } 15 | -------------------------------------------------------------------------------- /test/t0c.c: -------------------------------------------------------------------------------- 1 | struct t { 2 | int a; 3 | int b; 4 | int c; 5 | }; 6 | 7 | void g(struct t *tp) 8 | { 9 | tp->a = 2; 10 | } 11 | 12 | int main(void) 13 | { 14 | struct t t; 15 | struct t *tp = &t; 16 | g(tp); 17 | tp->b = tp->a + 3; 18 | tp->c = tp->a + t.b; 19 | return tp->c != 7; 20 | } 21 | -------------------------------------------------------------------------------- /test/t0d.c: -------------------------------------------------------------------------------- 1 | struct t { 2 | int a[10]; 3 | int n; 4 | }; 5 | 6 | int main(void) 7 | { 8 | struct t t; 9 | struct t *tp = &t; 10 | int i; 11 | int sum = 0; 12 | tp->n = 10; 13 | for (i = 0; i < tp->n; i++) 14 | tp->a[i] = i; 15 | for (i = 0; i < t.n; i++) 16 | sum += t.a[i]; 17 | return sum != 45; 18 | } 19 | -------------------------------------------------------------------------------- /test/t0e.c: -------------------------------------------------------------------------------- 1 | struct t { 2 | int a; 3 | int b; 4 | }; 5 | 6 | int main(void) 7 | { 8 | struct t t[10]; 9 | int i; 10 | int sum = 0; 11 | for (i = 0; i < 10; i++) 12 | t[i].a = t[i].b = i; 13 | for (i = 0; i < 10; i++) 14 | if (t[i].a != i || t[i].b != i) 15 | return 1; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /test/t0f.c: -------------------------------------------------------------------------------- 1 | struct t { 2 | int a[2]; 3 | int b; 4 | }; 5 | 6 | int main(void) 7 | { 8 | struct t t[2]; 9 | if (sizeof(t) != 6 * 4) 10 | return 1; 11 | if (sizeof(t[0]) != 3 * 4) 12 | return 1; 13 | if (sizeof(t[0].a) != 2 * 4) 14 | return 1; 15 | if (sizeof(t[0].b) != 4) 16 | return 1; 17 | if (sizeof(t[0].a[0]) != 4) 18 | return 1; 19 | if (sizeof(struct t) != 12) 20 | return 1; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /test/t10.c: -------------------------------------------------------------------------------- 1 | union t { 2 | int a[2]; 3 | int b; 4 | }; 5 | 6 | int main(void) 7 | { 8 | union t t; 9 | t.a[0] = 1; 10 | t.a[1] = 2; 11 | t.b = 3; 12 | if (sizeof(t) != 8) 13 | return 1; 14 | if (t.a[0] != 3) 15 | return 1; 16 | if (t.a[1] != 2) 17 | return 1; 18 | if (t.b != 3) 19 | return 1; 20 | if (&t.b != &t.a[0]) 21 | return 1; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /test/t11.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | char a1[1 ? 10 : 20]; 4 | char a2[5 + 5]; 5 | char a3[(1 << 3) + 2]; 6 | char a4[2 < 1 ? 20 : 10]; 7 | if (sizeof(a1) != 10 || sizeof(a2) != 10) 8 | return 1; 9 | if (sizeof(a3) != 10 || sizeof(a4) != 10) 10 | return 1; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /test/t12.c: -------------------------------------------------------------------------------- 1 | enum a { 2 | a0, 3 | a1, 4 | a2 = 5 + 5, 5 | a3, 6 | a4 = -2, 7 | a5, 8 | }; 9 | 10 | int main(void) 11 | { 12 | if (a0 != 0 || a1 != 1 || a2 != 10 || a3 != 11 || a4 != -2 || a5 != -1) 13 | return 1; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /test/t13.c: -------------------------------------------------------------------------------- 1 | typedef int mint; 2 | 3 | int main(void) 4 | { 5 | mint a; 6 | if (sizeof(a) != 4 || sizeof(mint) != 4) 7 | return 1; 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /test/t14.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int i; 4 | for (i = 0; i < 10; i++) { 5 | if (i == 3) 6 | continue; 7 | if (i >= 3) 8 | break; 9 | } 10 | if (i != 4) 11 | return 1; 12 | i = 0; 13 | while (i < 10) { 14 | i++; 15 | if (i == 3) 16 | continue; 17 | if (i >= 3) 18 | break; 19 | } 20 | if (i != 4) 21 | return 1; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /test/t15.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int i = 0; 4 | do { 5 | i++; 6 | } while (i < 10); 7 | if (i != 10) 8 | return 1; 9 | i = 0; 10 | do { 11 | i++; 12 | if (i == 3) 13 | continue; 14 | if (i == 4) 15 | break; 16 | } while (i < 10); 17 | if (i != 4) 18 | return 1; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /test/t16.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int i; 4 | for (i = 0; i < 10; i++) { 5 | switch (i) { 6 | case 0: 7 | if (i != 0) 8 | return 1; 9 | break; 10 | case 5: 11 | case 5 + 1: 12 | if (i != 5 && i != 6) 13 | return 2; 14 | break; 15 | default: 16 | if (i == 0 || i == 5 || i == 6) 17 | return 3; 18 | } 19 | } 20 | if (i != 10) 21 | return 4; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /test/t17.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | unsigned char c1 = 244; 4 | char c2 = 244; 5 | long l = (char) -1; 6 | if (c2 != (char) c1) 7 | return 1; 8 | if (l != -1) 9 | return 2; 10 | if ((long) (char) -1 != -1) 11 | return 3; 12 | if ((long) (unsigned char) (char) -1 != 255) 13 | return 4; 14 | if ((int) ((long) -1 >> 31) != -1) 15 | return 5; 16 | #ifdef __x86_64__ 17 | if ((int) ((long) -1 >> 32) != -1) 18 | return 6; 19 | #endif 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /test/t18.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | long l = -1; 4 | char c = 254; 5 | if (l >> 28 != -1) 6 | return 1; 7 | if ((long) (unsigned char) c != 254) 8 | return 2; 9 | if ((unsigned long) (c + 1) != -1) 10 | return 3; 11 | if ((unsigned long) (c * 10) != -20) 12 | return 4; 13 | if (((unsigned long) (unsigned int) -1 >> 16) != 0xffff) 14 | return 5; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /test/t19.c: -------------------------------------------------------------------------------- 1 | int g(void) 2 | { 3 | return 3; 4 | } 5 | 6 | int main(void) 7 | { 8 | int (*h1)(void) = g; 9 | int (*h2)(void) = &g; 10 | int (**h3)(void) = &h1; 11 | if (h1() != 3 || (*h1)() != 3) 12 | return 1; 13 | if (h2() != 3 || (*h2)() != 3) 14 | return 2; 15 | if ((*h3)() != 3 || (**h3)() != 3) 16 | return 3; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /test/t1a.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int a = 1; 4 | { 5 | int a = 2; 6 | if (a != 2) 7 | return 1; 8 | } 9 | if (a != 1) 10 | return 2; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /test/t1b.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int a[10][10]; 4 | int i, j; 5 | for (i = 0; i < 10; i++) 6 | for (j = 0; j < 10; j++) 7 | a[i][j] = i * 10 + j; 8 | for (i = 0; i < 10; i++) 9 | for (j = 0; j < 10; j++) 10 | if (a[i][j] != i * 10 + j) 11 | return j; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /test/t1c.c: -------------------------------------------------------------------------------- 1 | static int g1(void) 2 | { 3 | static int i; 4 | return i++; 5 | } 6 | 7 | static int g2(void) 8 | { 9 | static int i; 10 | return i++; 11 | } 12 | 13 | int main(void) 14 | { 15 | if (g1() != 0 || g1() != 1) 16 | return 1; 17 | if (g2() != 0 || g2() != 1) 18 | return 2; 19 | if (g1() != 2 || g1() != 3) 20 | return 3; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /test/t1d.c: -------------------------------------------------------------------------------- 1 | signed char g(void) 2 | { 3 | return 254; 4 | } 5 | 6 | int main(void) 7 | { 8 | if (g() != -2) 9 | return 1; 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /test/t1e.c: -------------------------------------------------------------------------------- 1 | int g1(void); 2 | 3 | int main(void) 4 | { 5 | if (g1() != 11) 6 | return 1; 7 | if (g2() != 21) 8 | return 2; 9 | return 0; 10 | } 11 | 12 | int g1(void) 13 | { 14 | return 11; 15 | } 16 | 17 | int g2(void) 18 | { 19 | return 21; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /test/t1f.c: -------------------------------------------------------------------------------- 1 | struct t { 2 | int c[3]; 3 | long a; 4 | long b; 5 | }; 6 | 7 | int main(void) 8 | { 9 | struct t t1, t2; 10 | int b[3]; 11 | t1.a = 10; 12 | t1.b = 20; 13 | t1.c[0] = 30; 14 | t1.c[1] = 40; 15 | t1.c[2] = 50; 16 | t2 = t1; 17 | if (t2.a != 10 || t2.b != 20) 18 | return 1; 19 | if (t2.c[0] != 30 || t2.c[1] != 40 || t2.c[2] != 50) 20 | return 1; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /test/t20.c: -------------------------------------------------------------------------------- 1 | struct t { 2 | int a[3]; 3 | int b; 4 | int c; 5 | }; 6 | 7 | int main(void) 8 | { 9 | int a[3] = {1, 2}; 10 | struct t t = {{10, 20}, 30, 40}; 11 | if (a[0] != 1 || a[1] != 2 || a[2] != 0) 12 | return 1; 13 | if (t.a[0] != 10 || t.a[1] != 20 || t.a[2] != 0) 14 | return 2; 15 | if (t.b != 30 || t.c != 40) 16 | return 3; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /test/t21.c: -------------------------------------------------------------------------------- 1 | int g(int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8, int p9) 2 | { 3 | return p1 == 1 && p2 == 2 && p3 == 3 && p4 == 4 && p5 == 5 && 4 | p6 == 6 && p7 == 7 && p8 == 8 && p9 == 9; 5 | } 6 | 7 | int main(void) 8 | { 9 | return !g(1, 2, 3, 4, 5, 6, 7, 8, 9); 10 | } 11 | -------------------------------------------------------------------------------- /test/t22.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int a[2][2] = {10, 20, 30}; 4 | int b[3] = {[1] = 40}; 5 | if (b[0] != 0 || b[1] != 40 || b[2] != 0) 6 | return 1; 7 | if (a[0][0] != 10 || a[0][1] != 20 || a[1][0] != 30 || a[1][1] != 0) 8 | return 2; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /test/t23.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int ret = 2; 4 | goto l1; 5 | l1: 6 | ret = 0; 7 | goto end; 8 | ret = 5; 9 | end: 10 | return ret; 11 | } 12 | -------------------------------------------------------------------------------- /test/t24.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | char a[10] = "ab"; 4 | if (sizeof(a) != 10) 5 | return 1; 6 | if (a[0] == 'a' && a[1] == 'b' && a[2] == 0 && a[3] == 0) 7 | return 0; 8 | return 2; 9 | } 10 | -------------------------------------------------------------------------------- /test/t25.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | char a[3] = "ab"; 4 | if (sizeof(a) != 3) 5 | return 1; 6 | if (a[0] == 'a' && a[1] == 'b' && a[2] == 0) 7 | return 0; 8 | return 2; 9 | } 10 | -------------------------------------------------------------------------------- /test/t26.c: -------------------------------------------------------------------------------- 1 | struct { 2 | int a; 3 | int b; 4 | } t; 5 | 6 | int main(void) 7 | { 8 | if (sizeof(t) != 8) 9 | return 1; 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /test/t27.c: -------------------------------------------------------------------------------- 1 | struct t *g(struct t *t) 2 | { 3 | return t; 4 | } 5 | 6 | struct t { 7 | int a; 8 | int b; 9 | }; 10 | 11 | int main(void) 12 | { 13 | struct t t = {10, 20}; 14 | if (g(&t)->a != 10 || g(&t)->b != 20) 15 | return 1; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /test/t28.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | char *s = "ab"; 4 | void *p = s; 5 | if (*(char *) p != 'a' || *(char *) ((char *) p + 1) != 'b') 6 | return 1; 7 | if (sizeof(*(char *) p) != 1) 8 | return 2; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /test/t29.c: -------------------------------------------------------------------------------- 1 | int g1(void) 2 | { 3 | return 10; 4 | } 5 | 6 | int g2(int (*x)(void)) 7 | { 8 | return x(); 9 | } 10 | 11 | int main(void) 12 | { 13 | if (g2(g1) != 10) 14 | return 1; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /test/t2a.c: -------------------------------------------------------------------------------- 1 | typedef struct t1 { 2 | int a[3]; 3 | } t1; 4 | 5 | typedef struct { 6 | int a[4]; 7 | } t2; 8 | 9 | int main(void) 10 | { 11 | t1 x1; 12 | t2 x2; 13 | if (sizeof(x1) != 12) 14 | return 1; 15 | if (sizeof(x2) != 16) 16 | return 2; 17 | x2.a[2] = 10; 18 | if (x2.a[2] != 10) 19 | return 3; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /test/t2b.c: -------------------------------------------------------------------------------- 1 | void g(char *a[]) 2 | { 3 | a[1] = "d"; 4 | } 5 | 6 | int main(void) 7 | { 8 | char *a[3] = {"a", "b", "c"}; 9 | g(a); 10 | if (a[1][0] != 'd') 11 | return 1; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /test/t2c.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | char a[] = "abc"; 4 | int b[] = {1, 2, 3, 4}; 5 | 6 | if (sizeof(a) != 4) 7 | return 1; 8 | if (a[0] != 'a' || a[1] != 'b' || a[2] != 'c' || a[3] != 0) 9 | return 2; 10 | if (sizeof(b) != 16) 11 | return 3; 12 | if (b[0] != 1 || b[1] != 2 || b[2] != 3 || b[3] != 4) 13 | return 4; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /test/t2d.c: -------------------------------------------------------------------------------- 1 | struct t { 2 | int a[3]; 3 | int b; 4 | int c; 5 | }; 6 | 7 | static int a[3] = {1, 2}; 8 | 9 | int main(void) 10 | { 11 | static struct t t = {{10, 20}, 30, 40}; 12 | if (a[0] != 1 || a[1] != 2 || a[2] != 0) 13 | return 1; 14 | if (t.a[0] != 10 || t.a[1] != 20 || t.a[2] != 0) 15 | return 2; 16 | if (t.b != 30 || t.c != 40) 17 | return 3; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /test/t2e.c: -------------------------------------------------------------------------------- 1 | int g(void) 2 | { 3 | return 10; 4 | } 5 | 6 | static int (*gp)(void) = &g; 7 | 8 | int main(void) 9 | { 10 | if (gp() != 10) 11 | return 1; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /test/t2f.c: -------------------------------------------------------------------------------- 1 | struct t { 2 | int a; 3 | int b; 4 | }; 5 | 6 | static struct t t1[] = {{10, 20}}; 7 | 8 | int main(void) 9 | { 10 | struct t t2[] = {{30, 40}}; 11 | if (t1->a != 10 || t2->b != 40) 12 | return 1; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /test/t30.c: -------------------------------------------------------------------------------- 1 | static char *a[] = { "a", "b", "c" }; 2 | 3 | int main(void) 4 | { 5 | if (a[0][0] != 'a' || a[1][0] != 'b' || a[2][0] != 'c') 6 | return 1; 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /test/t31.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | char *a = "a" "b" "c"; 4 | if (a[0] != 'a' || a[1] != 'b' || a[2] != 'c' || a[3] != 0) 5 | return 1; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /test/t32.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | long a = 0x10 + 010 + 10; 4 | if (a != 16 + 8 + 10) 5 | return 1; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /test/t33.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | char *s = "\33\xf\x11"; 4 | if ('\0' != 0) 5 | return 1; 6 | if (s[0] != 3 * 8 + 3) 7 | return 2; 8 | if (s[1] != 15 || s[2] != 1 * 16 + 1) 9 | return 3; 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /test/t34.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int i = 0; 4 | for (;;) { 5 | if (++i == 10) 6 | break; 7 | } 8 | if (i != 10) 9 | return 1; 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /test/t35.c: -------------------------------------------------------------------------------- 1 | #define N 10 2 | 3 | int main(void) 4 | { 5 | char a[N]; 6 | if (sizeof(a) != 10 || N != 10) 7 | return 1; 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /test/t36.c: -------------------------------------------------------------------------------- 1 | #define N 10 + \ 2 | 20 3 | 4 | int main(void) 5 | { 6 | if ((N) != 30) 7 | return 1; 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /test/t37.c: -------------------------------------------------------------------------------- 1 | #include "t55.h" 2 | 3 | int main(void) 4 | { 5 | if (g() != N) 6 | return 1; 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /test/t38.c: -------------------------------------------------------------------------------- 1 | #define M(a) ((a) + 1) /*((a) + 1)*/ 2 | 3 | int main(void) 4 | { 5 | if (M(2) != 3) 6 | return 1; 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /test/t39.c: -------------------------------------------------------------------------------- 1 | #define A 2 | 3 | int main(void) 4 | { 5 | int i = 0; 6 | #ifdef A 7 | i = 10; 8 | #else 9 | i = 20; 10 | #endif 11 | if (i != 10) 12 | return 1; 13 | #ifndef A 14 | i = 1; 15 | #else 16 | i = 2; 17 | #endif 18 | if (i != 2) 19 | return 2; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /test/t3a.c: -------------------------------------------------------------------------------- 1 | #define A 1 2 | #define B 2 3 | 4 | int main(void) 5 | { 6 | int i = 0; 7 | #if A == B 8 | i = 10; 9 | #elif A == B / 2 10 | i = 20; 11 | #else 12 | i = 30; 13 | #endif 14 | if (i != 20) 15 | return 1; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /test/t3b.c: -------------------------------------------------------------------------------- 1 | #define A 2 | 3 | #ifndef A 4 | #ifndef C 5 | #define B 10 6 | #else 7 | #define B 20 8 | #endif 9 | #else 10 | #if defined(A) && !defined(B) 11 | #define B 30 12 | #endif 13 | #endif 14 | 15 | #define D(a, b) ((a) + (b)) 16 | 17 | int main(void) 18 | { 19 | if (B != 30) 20 | return 1; 21 | if (D(B, 10) != 40) 22 | return 2; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /test/t3c.c: -------------------------------------------------------------------------------- 1 | int g1(int a) 2 | { 3 | return a; 4 | } 5 | 6 | int g2(int a, int b) 7 | { 8 | return a + b; 9 | } 10 | 11 | int main(void) 12 | { 13 | if (g2(g1(1), g2(g1(2), g1(3))) != 6) 14 | return 1; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /test/t3d.c: -------------------------------------------------------------------------------- 1 | #ifdef __x86_64__ 2 | int main(void) 3 | { 4 | char *s1[10]; 5 | char s3[10]; 6 | char (*s2)[10]; 7 | int (*x)(int a); 8 | if (sizeof(s1) != 80) 9 | return 1; 10 | if (sizeof(s2) != 8) 11 | return 2; 12 | if (sizeof(x) != 8) 13 | return 2; 14 | return 0; 15 | } 16 | #else 17 | int main(void) 18 | { 19 | char *s1[10]; 20 | char s3[10]; 21 | char (*s2)[10]; 22 | int (*x)(int a); 23 | if (sizeof(s1) != 40) 24 | return 1; 25 | if (sizeof(s2) != 4) 26 | return 2; 27 | if (sizeof(x) != 4) 28 | return 3; 29 | return 0; 30 | } 31 | #endif 32 | -------------------------------------------------------------------------------- /test/t3e.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int i = 2; 4 | if (!(i == 2)) 5 | return 1; 6 | if (!(i != 3)) 7 | return 2; 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /test/t3f.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int i = 0; 4 | int *p = &i; 5 | *p |= 4; 6 | if (i != 4) 7 | return 1; 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /test/t40.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | char a[] = "ab"; 4 | int cur = 0; 5 | if (a[cur++] != 'a') 6 | return 1; 7 | if (a[cur++] != 'b') 8 | return 2; 9 | if (a[cur++] != '\0') 10 | return 3; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /test/t41.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | unsigned long l = 1; 4 | if (l != 1) 5 | return 1; 6 | if (!l != 0) 7 | return 2; 8 | if (!!l != 1) 9 | return 3; 10 | if (!l) 11 | return 4; 12 | if (!!!l) 13 | return 5; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /test/t42.c: -------------------------------------------------------------------------------- 1 | int slen(char *s) 2 | { 3 | int n = 0; 4 | while (*s++) 5 | n++; 6 | return n; 7 | } 8 | 9 | int main(void) 10 | { 11 | char *s[] = {"ab", "c"}; 12 | int len[] = {slen(s[0]), slen(s[1])}; 13 | if (len[0] != 2 || len[1] != 1) 14 | return 1; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /test/t43.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | long x = 1; 4 | if (x << 31 != 1ul << 31) 5 | return 1; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /test/t44.c: -------------------------------------------------------------------------------- 1 | struct t { 2 | int a; 3 | }; 4 | 5 | int g(void) 6 | { 7 | return 1; 8 | } 9 | 10 | int main(void) 11 | { 12 | struct t t; 13 | struct t *p = &t; 14 | p->a = g(); 15 | if (t.a != 1) 16 | return 1; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /test/t45.c: -------------------------------------------------------------------------------- 1 | int g(void) 2 | { 3 | return 30; 4 | } 5 | 6 | int main(void) 7 | { 8 | int a = 10; 9 | int b = a + a - a + (a == 20 ? g() : 10); 10 | if (a != 10 || g() != 30 || b != 20) 11 | return 1; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /test/t46.c: -------------------------------------------------------------------------------- 1 | enum { 2 | TEN = 10, 3 | TWENTY = 20, 4 | THIRTY = 30 5 | }; 6 | int g(void) 7 | { 8 | return 30; 9 | } 10 | 11 | int main(void) 12 | { 13 | switch (g()) { 14 | case TEN: 15 | return 1; 16 | case TWENTY: 17 | return 2; 18 | case THIRTY: 19 | return 0; 20 | default: 21 | return 3; 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /test/t47.c: -------------------------------------------------------------------------------- 1 | int g(int i) 2 | { 3 | return i; 4 | } 5 | 6 | int main(void) 7 | { 8 | int (*p)(int i) = g; 9 | if (p(10) != 10) 10 | return 1; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /test/t48.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int off = -0x88; 4 | if (off == (char) off) 5 | return 1; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /test/t49.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int i[3] = {2, 0, 3}; 4 | for (i[1] = 9; i[1] >= 0; --i[1]) 5 | ; 6 | if (i[0] != 2 || i[2] != 3) 7 | return 1; 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /test/t4a.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | long i = 0xffffffff; 4 | if (i != (unsigned) i) 5 | return 1; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /test/t4b.c: -------------------------------------------------------------------------------- 1 | static int rr = 0; 2 | static int gr = 0; 3 | static int br = 0; 4 | static int rl = 16; 5 | static int gl = 8; 6 | static int bl = 0; 7 | 8 | unsigned int g(unsigned char r, unsigned char g, unsigned char b) 9 | { 10 | return ((r >> rr) << rl) | ((g >> gr) << gl) | ((b >> br) << bl); 11 | } 12 | 13 | int main(void) 14 | { 15 | unsigned char cr = 0x11; 16 | unsigned char cg = 0x22; 17 | unsigned char cb = 0x33; 18 | if (g(0, 0, 0) != 0) 19 | return 1; 20 | if (g(255, 255, 255) != 0x00ffffff) 21 | return 2; 22 | if (g(0x11, 0x22, 0x33) != 0x00112233) 23 | return 3; 24 | if (g(cr, cg, cb) != 0x00112233) 25 | return 4; 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /test/t4c.c: -------------------------------------------------------------------------------- 1 | static int i; 2 | static int a[2]; 3 | 4 | int main(void) 5 | { 6 | int j = 10; 7 | int b[2]; 8 | int *p = a; 9 | if (i++ != 0 || i != 1) 10 | return 1; 11 | if (--j != 9 || j != 9) 12 | return 2; 13 | p = a; 14 | *p++ = 10; 15 | *p++ = 20; 16 | if (a[0] != 10 || a[1] != 20) 17 | return 3; 18 | p = b + 1; 19 | *p-- = 30; 20 | *p-- = 40; 21 | if (b[0] != 40 || b[1] != 30) 22 | return 4; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /test/t4d.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int a[3] = {10, 20, 30}; 4 | int *p = a; 5 | p += 1; 6 | if (*p != 20) 7 | return 1; 8 | p++; 9 | if (*p != 30) 10 | return 2; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /test/t4e.c: -------------------------------------------------------------------------------- 1 | int g1(void) 2 | { 3 | return 10; 4 | } 5 | 6 | int g2(void) 7 | { 8 | return 20; 9 | } 10 | 11 | int main(void) 12 | { 13 | int (*x1[])(void) = {g1, g2}; 14 | int (*x2[][1])(void) = {{g1}, {g2}}; 15 | if (x1[0]() != 10 || x1[1]() != 20) 16 | return 1; 17 | if (x2[0][0]() != 10 || x2[1][0]() != 20) 18 | return 2; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /test/t4f.c: -------------------------------------------------------------------------------- 1 | #define TWICE(a) ((a) * 2) 2 | 3 | int STH(int a) 4 | { 5 | return a * 3; 6 | } 7 | 8 | #define STH(a) (STH(a) * 2) 9 | 10 | int main(void) 11 | { 12 | int a = 5; 13 | if (TWICE(1) != 2 && TWICE(TWICE(1)) != 4) 14 | return 1; 15 | if (STH(2) != 12) 16 | return 2; 17 | if (TWICE(TWICE(a)) != 20) 18 | return 3; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /test/t50.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int i = 0, j, k; 4 | j = 1; 5 | k = 2; 6 | return i + j + k != 3; 7 | } 8 | -------------------------------------------------------------------------------- /test/t51.c: -------------------------------------------------------------------------------- 1 | struct t { 2 | int f1; 3 | int f2; 4 | int f3; 5 | int f4; 6 | int f5; 7 | int f6; 8 | }; 9 | 10 | int g(int p1, int p2, int p3, int p4) 11 | { 12 | return p1 + p2 + p3 + p4; 13 | } 14 | 15 | int main(void) 16 | { 17 | struct t t = {1, 1, 2, 2, 3, 3}; 18 | struct t *tp = &t; 19 | int (*fp)(int a1, ...) = g; 20 | return fp(tp->f6, tp->f5, tp->f4, tp->f3, tp->f2, tp->f1) != 10; 21 | } 22 | -------------------------------------------------------------------------------- /test/t52.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | char *s = "\2\12\123\x32"; 4 | if (s[0] != 02 || s[1] != 012 || s[2] != 0123) 5 | return 1; 6 | if (s[3] != 0x32) 7 | return 2; 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /test/t53.c: -------------------------------------------------------------------------------- 1 | /* testing optimized version of mul/div/mod for powers of two */ 2 | 3 | static unsigned a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 4 | 5 | int main(void) 6 | { 7 | unsigned i; 8 | i = 5; 9 | if (a[i] != 5) 10 | return 1; 11 | if (i * 16 != 80) 12 | return 2; 13 | i = 22; 14 | if (i / 4 != 5) 15 | return 3; 16 | if (i % 4 != 2) 17 | return 4; 18 | i = 0x12345678; 19 | if (i % 0x00010000 != 0x00005678) 20 | return 4; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /test/t54.c: -------------------------------------------------------------------------------- 1 | /* testing signed multiplication */ 2 | 3 | int main(void) 4 | { 5 | long i1 = 2; 6 | long i2 = 3; 7 | long i3 = -4; 8 | long i4 = -5; 9 | if (i1 * i2 != 6) 10 | return 1; 11 | if (i1 * i3 != -8) 12 | return 2; 13 | if (i3 * i4 != 20) 14 | return 3; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /test/t55.c: -------------------------------------------------------------------------------- 1 | static int digits(unsigned long n, int base) 2 | { 3 | int i; 4 | for (i = 0; n; i++) 5 | n /= base; 6 | return i ? i : 1; 7 | } 8 | 9 | static char *digs = "0123456789abcdef"; 10 | 11 | static void putint(char *s, unsigned long n, int base) 12 | { 13 | int d; 14 | int i; 15 | d = digits(n, base); 16 | for (i = 0; i < d; i++) { 17 | s[d - i - 1] = digs[n % base]; 18 | n /= base; 19 | } 20 | s[d] = '\0'; 21 | } 22 | 23 | static int mstrcmp(char *s, char *r) 24 | { 25 | while (*s == *r) { 26 | if (!*s) 27 | return 0; 28 | s++; 29 | r++; 30 | } 31 | return *r - *s; 32 | } 33 | 34 | int main(void) 35 | { 36 | char dst[16]; 37 | if (digits(123, 10) != 3) 38 | return 1; 39 | putint(dst, 123, 10); 40 | if (mstrcmp("123", dst)) 41 | return 2; 42 | putint(dst, 130, 2); 43 | if (mstrcmp("10000010", dst)) 44 | return 3; 45 | putint(dst, 0x1234, 16); 46 | if (mstrcmp("1234", dst)) 47 | return 4; 48 | putint(dst, 0x8f8f8f8f, 16); 49 | if (mstrcmp("8f8f8f8f", dst)) 50 | return 5; 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /test/t55.h: -------------------------------------------------------------------------------- 1 | #define N 10 2 | 3 | int g(void) 4 | { 5 | return N; 6 | } 7 | -------------------------------------------------------------------------------- /test/t56.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | unsigned int s1 = 30; 4 | int s2 = -50; 5 | if (s1 * 4 != 120) 6 | return 1; 7 | if (s2 * 4 != -200) 8 | return 2; 9 | if (s1 / 2 != 15) 10 | return 3; 11 | if (s2 / 2 != -25) 12 | return 4; 13 | if (s2 % 2 != 0) 14 | return 5; 15 | if (s2 / 8 != -6) 16 | return 7; 17 | if (s2 % 8 != -2) 18 | return 8; 19 | if (s2 / -8 != 6) 20 | return 9; 21 | if (s2 % -8 != -2) 22 | return 10; 23 | if (s1 % 8 != 6) 24 | return 11; 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /test/t57.c: -------------------------------------------------------------------------------- 1 | #define SZ (1 << 10) 2 | 3 | int main(void) 4 | { 5 | int x[123]; 6 | int a[SZ]; 7 | int i; 8 | short j; 9 | i = 0x11223344; 10 | j = 0x5566; 11 | if (i != 0x11223344) 12 | return 1; 13 | if (j != 0x5566) 14 | return 2; 15 | for (i = 0; i < SZ; i++) 16 | a[i] = i; 17 | for (i = 0; i < SZ; i++) 18 | if (a[i] != i) 19 | return 3; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /test/t58.c: -------------------------------------------------------------------------------- 1 | /* should not optimize the last return if it is nested */ 2 | int f(int i) 3 | { 4 | if (i) 5 | return i; 6 | } 7 | 8 | int main(void) 9 | { 10 | f(1); 11 | f(2); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /test/t59.c: -------------------------------------------------------------------------------- 1 | #define SZ (1 << 18) 2 | 3 | int main(void) 4 | { 5 | int a[20]; 6 | int i = 10; 7 | int j; 8 | a[i] = 5; 9 | j = (unsigned char) a[i]; 10 | if (j != 5) 11 | return 1; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /test/t5a.c: -------------------------------------------------------------------------------- 1 | static int g(void) 2 | { 3 | return 0x1234; 4 | } 5 | 6 | static void *f(int (*x)(void)) 7 | { 8 | return x; 9 | } 10 | 11 | int main(void) 12 | { 13 | if (g != f(g)) 14 | return 1; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /test/t5b.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int c0 = 0; 4 | int c1 = 1; 5 | if (c0 || c0 && c1 || c0) 6 | return 1; 7 | if (c0 && (c0 || c1)) 8 | return 2; 9 | if (c1 != 0 && (c0 || c1)) 10 | return 0; 11 | return 3; 12 | } 13 | -------------------------------------------------------------------------------- /test/t5c.c: -------------------------------------------------------------------------------- 1 | #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) 2 | 3 | int main(void) 4 | { 5 | int c7 = 7; 6 | int c4 = 4; 7 | if (ALIGN(c7, c4) != 8) 8 | return 1; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /test/t5d.c: -------------------------------------------------------------------------------- 1 | struct type { 2 | unsigned bt; 3 | }; 4 | 5 | static void g(void) 6 | { 7 | } 8 | 9 | int f(struct type *t) 10 | { 11 | int sign = 1; 12 | int size = 4; 13 | int done = 0; 14 | int i = 0; 15 | g(); 16 | t->bt = size | (sign ? 0x100 : 0); 17 | return 0; 18 | } 19 | 20 | int main(void) 21 | { 22 | struct type t; 23 | f(&t); 24 | if (t.bt != 0x104) 25 | return 1; 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /test/t5e.c: -------------------------------------------------------------------------------- 1 | #define A() (10) 2 | #define B(a) (10) 3 | 4 | int main(void) 5 | { 6 | int c10 = 10; 7 | if (A() != c10) 8 | return 1; 9 | if (B(0) == c10) 10 | return 0; 11 | return 2; 12 | } 13 | -------------------------------------------------------------------------------- /test/t5f.c: -------------------------------------------------------------------------------- 1 | static int a; 2 | 3 | int main(void) 4 | { 5 | int f(void); 6 | if (f() != 0) 7 | return 1; 8 | a = 1; 9 | if (f() != 1) 10 | return 2; 11 | return 0; 12 | } 13 | 14 | int f(void) 15 | { 16 | return a; 17 | } 18 | -------------------------------------------------------------------------------- /test/t60.c: -------------------------------------------------------------------------------- 1 | int f(void) 2 | { 3 | extern int a; 4 | return a; 5 | } 6 | 7 | int a; 8 | 9 | int main(void) 10 | { 11 | if (f() != 0) 12 | return 1; 13 | a = 1; 14 | if (f() != 1) 15 | return 2; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /test/t61.c: -------------------------------------------------------------------------------- 1 | struct dat { 2 | int a[16]; 3 | }; 4 | 5 | static int a = 0; 6 | 7 | static struct dat *f(void) 8 | { 9 | a++; 10 | return 0; 11 | } 12 | 13 | int main(void) 14 | { 15 | int sz = sizeof(f()->a); 16 | if (sizeof(f()->a) != 16 * sizeof(int)) 17 | return 1; 18 | if (sz != 16 * sizeof(int)) 19 | return 2; 20 | if (a) 21 | return 3; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /test/t62.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | unsigned char a[10]; 4 | int i; 5 | for (i = 0; i < sizeof(a); i++) 6 | a[i] = i; 7 | a[5] += 2; 8 | for (i = 0; i < sizeof(a); i++) 9 | if (i != 5 && a[i] != i) 10 | return i; 11 | if (a[5] != 7) 12 | return 5; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /test/t63.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int a = 5; 4 | int r = 0; 5 | switch (a) { 6 | case 1: 7 | r |= 0x01; 8 | case 3: 9 | r |= 0x02; 10 | case 5: 11 | r |= 0x04; 12 | case 7: 13 | r |= 0x08; 14 | default: 15 | r |= 0x10; 16 | } 17 | if (r != 0x1c) 18 | return 1; 19 | r = 0; 20 | switch (a) { 21 | case 1: 22 | r |= 0x01; 23 | default: 24 | r |= 0x02; 25 | case 7: 26 | r |= 0x08; 27 | break; 28 | } 29 | if (r != 0x0a) 30 | return 2; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /test/t64.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int i = 0; 4 | if (1) 5 | do { 6 | i = 1; 7 | } while (0); 8 | else 9 | i = 2; 10 | return i != 1; 11 | } 12 | -------------------------------------------------------------------------------- /test/t65.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int v = 1; 4 | if ((1 ? v : 2) != 1) 5 | return 1; 6 | if ((0 ? v : 2) != 2) 7 | return 1; 8 | if ((1 ? 2 : v) != 2) 9 | return 1; 10 | if ((0 ? 2 : v) != 1) 11 | return 1; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /test/t66.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int t = 1; 4 | int f = 0; 5 | int v = 3; 6 | if ((t ? v : 2) != 3) 7 | return 1; 8 | if ((f ? v : 2) != 2) 9 | return 1; 10 | if ((t ? 2 : v) != 2) 11 | return 1; 12 | if ((f ? 2 : v) != 3) 13 | return 1; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /test/t67.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int a = 1; 4 | if ((sizeof a + 1) != sizeof(a) + 1) 5 | return 1; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /test/t68.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int t = 1, f = 0; 4 | int a = 1; 5 | return (t ? f ? a * 2 : a * 3 : f ? 5 : 7) != 3; 6 | } 7 | -------------------------------------------------------------------------------- /test/t69.c: -------------------------------------------------------------------------------- 1 | #define TABITEMS 0x1000 2 | 3 | int hash(char *s) 4 | { 5 | unsigned h = 0x12345678; 6 | while (*s) { 7 | h ^= (h >> ((h & 0xf) + 1)); 8 | h += *s++; 9 | h ^= (h << ((h & 0xf) + 5)); 10 | } 11 | h &= (TABITEMS - 1); 12 | return h ? h : 1; 13 | } 14 | 15 | int main(void) 16 | { 17 | hash("__STDC__"); 18 | hash("__i386__"); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /test/t6a.c: -------------------------------------------------------------------------------- 1 | static char *buf = "((x))"; 2 | static int cur = 1; 3 | static int len = 5; 4 | 5 | void readarg(void) 6 | { 7 | int depth = 0; 8 | while (cur < len && (depth || buf[cur] != ')')) { 9 | switch (buf[cur++]) { 10 | case '(': 11 | case '[': 12 | case '{': 13 | depth++; 14 | break; 15 | case ')': 16 | case ']': 17 | case '}': 18 | depth--; 19 | break; 20 | } 21 | } 22 | } 23 | 24 | int main(void) 25 | { 26 | readarg(); 27 | if (cur != 4) 28 | return 1; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /test/t6b.c: -------------------------------------------------------------------------------- 1 | #define B "//" 2 | 3 | int main(void) 4 | { 5 | char *s = "a" B; 6 | if (s[0] != 'a' || s[1] != '/' || s[2] != '/' || s[3] != '\0') 7 | return 1; 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /test/t6c.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | switch (2) { 4 | default: 5 | return 1; 6 | case 2: 7 | return 0; 8 | } 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /test/t6d.c: -------------------------------------------------------------------------------- 1 | int f1(void) 2 | { 3 | return 1; 4 | } 5 | 6 | int f2(void) 7 | { 8 | return 2; 9 | } 10 | 11 | int main(void) 12 | { 13 | int c = 0; 14 | if ((c ? f1 : f2)() != 2) 15 | return 1; 16 | c = 1; 17 | if ((c ? f1 : f2)() != 1) 18 | return 2; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /test/t6e.c: -------------------------------------------------------------------------------- 1 | #define ABC "ABC" 2 | 3 | char abc[80] = {ABC}; 4 | 5 | int main(void) 6 | { 7 | if (abc[0] != 'A' || abc[1] != 'B' || abc[2] != 'C' || abc[3] != '\0') 8 | return 1; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /test/t6f.c: -------------------------------------------------------------------------------- 1 | #define ABC "ABC" 2 | 3 | char s[][4] = {"a0", "a1", "a2"}; 4 | 5 | int main(void) 6 | { 7 | if (s[0][0] != 'a' || s[0][1] != '0' || s[0][2] != '\0') 8 | return 1; 9 | if (s[1][0] != 'a' || s[1][1] != '1' || s[1][2] != '\0') 10 | return 1; 11 | if (s[2][0] != 'a' || s[2][1] != '2' || s[2][2] != '\0') 12 | return 1; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /test/t70.c: -------------------------------------------------------------------------------- 1 | char f1(void) 2 | { 3 | return -1; 4 | } 5 | 6 | short f2(void) 7 | { 8 | return -1; 9 | } 10 | 11 | int f3(void) 12 | { 13 | return -1; 14 | } 15 | 16 | long f4(void) 17 | { 18 | return -1; 19 | } 20 | 21 | int main(void) 22 | { 23 | if (f1() >= 0) 24 | return 1; 25 | if (f2() >= 0) 26 | return 2; 27 | if (f3() >= 0) 28 | return 3; 29 | if (f4() >= 0) 30 | return 4; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /test/t71.c: -------------------------------------------------------------------------------- 1 | static struct tmp { 2 | long addr; 3 | unsigned loc; 4 | unsigned bt; 5 | } tmp[2]; 6 | 7 | int main(void) 8 | { 9 | tmp[0].addr = 1; 10 | tmp[0].loc = 1; 11 | tmp[0].bt = 1; 12 | tmp[1].addr = 2; 13 | tmp[1].loc = 2; 14 | tmp[1].bt = 2; 15 | if (tmp[0].addr != 1 || tmp[0].loc != 1 || tmp[0].bt != 1) 16 | return 1; 17 | if (tmp[1].addr != 2 || tmp[1].loc != 2 || tmp[1].bt != 2) 18 | return 2; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /test/t72.c: -------------------------------------------------------------------------------- 1 | #define BUF_FILE 0 2 | 3 | int main(void) 4 | { 5 | int type = 2; 6 | if (type & BUF_FILE) 7 | return 1; 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /test/t73.c: -------------------------------------------------------------------------------- 1 | int f(i, j) 2 | { 3 | return i + j; 4 | } 5 | 6 | int main(void) 7 | { 8 | return f(1, 2) - 3; 9 | } 10 | -------------------------------------------------------------------------------- /test/t74.c: -------------------------------------------------------------------------------- 1 | int f(s, i) 2 | char *s; 3 | int i; 4 | { 5 | while (*s++) 6 | i++; 7 | return i; 8 | } 9 | 10 | int main(void) 11 | { 12 | if (f("aaa", 1) != 4) 13 | return 1; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /test/t75.c: -------------------------------------------------------------------------------- 1 | #define HASH_BITS 13 2 | #define BITS 16 3 | 4 | #if HASH_BITS > BITS-1 5 | error: this should not be parsed! 6 | #endif 7 | 8 | int main(void) 9 | { 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /test/t76.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int a = 1; 4 | int b = 2; 5 | a += (a += 2, b = 4); 6 | return a - 7; 7 | } 8 | -------------------------------------------------------------------------------- /test/t77.c: -------------------------------------------------------------------------------- 1 | int f(int a[][2]) 2 | { 3 | a[0][0] = 0; 4 | a[0][1] = 1; 5 | a[1][0] = 2; 6 | a[1][1] = 3; 7 | } 8 | 9 | int main(void) 10 | { 11 | int a[2][2]; 12 | f(a); 13 | if (a[0][0] != 0 || a[0][1] != 1 || a[1][0] != 2 || a[1][1] != 3) 14 | return 1; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /test/t78.c: -------------------------------------------------------------------------------- 1 | f(a) 2 | { 3 | return a * 3; 4 | } 5 | 6 | int main(void) 7 | { 8 | return f(2) - 6; 9 | } 10 | -------------------------------------------------------------------------------- /test/t79.c: -------------------------------------------------------------------------------- 1 | #define NOFUNC (int (*)()) 0 2 | 3 | int main(void) 4 | { 5 | void *v = NOFUNC; 6 | return v != 0; 7 | } 8 | -------------------------------------------------------------------------------- /test/t7a.c: -------------------------------------------------------------------------------- 1 | int g(int x) 2 | { 3 | int a, b, c, d, e, f; 4 | int ret = 0; 5 | a = x; 6 | b = x; 7 | c = x; 8 | d = x; 9 | e = x; 10 | f = x; 11 | if (x > 0 && g(x - 1)) 12 | return 1; 13 | if (a != x || b != x || c != x || d != x || e != d || f != d) 14 | return 1; 15 | return ret; 16 | } 17 | 18 | int main(void) 19 | { 20 | return g(16); 21 | } 22 | -------------------------------------------------------------------------------- /test/t7b.c: -------------------------------------------------------------------------------- 1 | #define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf)) 2 | #define ELF64_ST_INFO(bind, type) ELF32_ST_INFO ((bind), (type)) 3 | #define ELF_ST_INFO ELF64_ST_INFO 4 | 5 | int main(void) 6 | { 7 | if (ELF_ST_INFO(1, 2) != 0x12) 8 | return 1; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /test/t7c.c: -------------------------------------------------------------------------------- 1 | static char *digs = "0123456789abcdef"; 2 | 3 | void putint(char *s, int n, int base) 4 | { 5 | int d = 10; 6 | int i; 7 | for (i = 0; i < d; i++) 8 | s[d - i] = digs[n % base]; 9 | s[d] = '\0'; 10 | } 11 | 12 | int main(void) 13 | { 14 | char dst[16]; 15 | putint(dst, 2, 10); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /test/t7d.c: -------------------------------------------------------------------------------- 1 | #define LONGSZ 4 2 | #define BT_SZMASK 0x00ff 3 | #define BT_SIGNED 0x0100 4 | #define BT_SZ(bt) ((bt) & BT_SZMASK) 5 | 6 | struct tmp { 7 | long addr; 8 | }; 9 | 10 | void num_cast(struct tmp *t, unsigned bt) 11 | { 12 | if (!(bt & BT_SIGNED) && BT_SZ(bt) != LONGSZ) 13 | t->addr &= ((1l << (long) (BT_SZ(bt) * 8)) - 1); 14 | if (bt & BT_SIGNED && BT_SZ(bt) != LONGSZ && 15 | t->addr > (1l << (BT_SZ(bt) * 8 - 1))) 16 | t->addr = -((1l << (BT_SZ(bt) * 8)) - t->addr); 17 | } 18 | 19 | int main(void) 20 | { 21 | struct tmp t[10]; 22 | struct tmp *t0 = &t[0]; 23 | struct tmp *t1 = &t[1]; 24 | struct tmp *t2 = &t[2]; 25 | struct tmp *t3 = &t[3]; 26 | num_cast(t0, 4); 27 | num_cast(t1, 4); 28 | num_cast(t2, 4); 29 | num_cast(t3, 4); 30 | if (t0 != &t[0]) 31 | return 1; 32 | if (t1 != &t[1]) 33 | return 1; 34 | if (t2 != &t[2]) 35 | return 1; 36 | if (t3 != &t[3]) 37 | return 1; 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /test/t7e.c: -------------------------------------------------------------------------------- 1 | static char scrs[0][32]; 2 | 3 | int main(void) 4 | { 5 | if (sizeof(scrs) != 0) 6 | return 1; 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /test/t7f.c: -------------------------------------------------------------------------------- 1 | static char saved[2]; 2 | 3 | int main(void) 4 | { 5 | if (sizeof(saved) != 2) 6 | return 1; 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /test/t80.c: -------------------------------------------------------------------------------- 1 | int a; 2 | 3 | void f(int v) 4 | { 5 | a = v; 6 | } 7 | 8 | int main(void) 9 | { 10 | a ? f(2) : f(1); 11 | return a != 1; 12 | } 13 | -------------------------------------------------------------------------------- /test/t81.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | return sizeof("123456789") != 10; 4 | } 5 | -------------------------------------------------------------------------------- /test/t82.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | return \ 4 | 0; 5 | } 6 | -------------------------------------------------------------------------------- /test/t83.c: -------------------------------------------------------------------------------- 1 | #define C1(x) (!((x) & ~0x7f)) 2 | #define C2(x) ((x) < 0x80) 3 | 4 | int main(void) 5 | { 6 | int i; 7 | for (i = 0; i < 0x1000; i++) 8 | if (C1(i) != C2(i)) 9 | return 1; 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /test/t84.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int a = 10; 4 | if (!a) 5 | return 1; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /test/t85.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | #ifdef __x86_64__ 4 | long a = 0x00001000000000ul; 5 | return a != (1l << 36); 6 | #endif 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /test/t86.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int a = + 3; 4 | int b = - 3; 5 | return a - - b; 6 | } 7 | -------------------------------------------------------------------------------- /test/t87.c: -------------------------------------------------------------------------------- 1 | static int files[16][1024]; 2 | static int nfiles; 3 | static int cfile; 4 | 5 | static int f(char *s) 6 | { 7 | return 0; 8 | } 9 | 10 | int main(void) 11 | { 12 | f(files[nfiles++]); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /test/t88.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int x = -200; 4 | x /= 6; 5 | return x != -33; 6 | } 7 | -------------------------------------------------------------------------------- /test/t89.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | char a[] = "\18"; 4 | return a[0] != 1 || a[1] != '8' || a[2] != 0; 5 | } 6 | -------------------------------------------------------------------------------- /test/t8a.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int i = 0; 4 | while (++i < 10) { 5 | if (i == 5) 6 | goto out; 7 | if (i == 3) 8 | continue; 9 | } 10 | out: 11 | return i != 5; 12 | } 13 | -------------------------------------------------------------------------------- /test/t8b.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The following requires operations to be performed using int values 3 | * and not using values with architecture word size. 4 | */ 5 | int main(void) 6 | { 7 | unsigned n = 0xffffff00; 8 | int shift = 24; 9 | return (n << shift) != 0; 10 | } 11 | -------------------------------------------------------------------------------- /test/t8c.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | unsigned n = 0x10000000; 4 | int m = 0x1000; 5 | unsigned a = 0x80000001; 6 | unsigned b = 0x80000002; 7 | if ((n * m / m) != 0) 8 | return 1; 9 | if (a + b != 0x00000003) 10 | return 2; 11 | if (sizeof(a + b) != 4) 12 | return 3; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /test/t8d.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | unsigned short a = 0x8001; 4 | unsigned short b = 0x8002; 5 | int c = a + b; 6 | if (c != 0x00010003) 7 | return 1; 8 | if (sizeof(a - b) != 4) 9 | return 2; 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /test/t8e.c: -------------------------------------------------------------------------------- 1 | /* Reported by Alexey Frunze */ 2 | int main(void) 3 | { 4 | int n = 0X01; 5 | if (n++, n++, n++, n++, ++n != 6) 6 | return 1; 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /test/t8f.c: -------------------------------------------------------------------------------- 1 | struct rstate { 2 | int mark[64 * 2]; 3 | int pc; 4 | char *s; 5 | char *o; 6 | int flg; 7 | }; 8 | 9 | static int flg(struct rstate *rs) 10 | { 11 | struct rstate x = *rs; 12 | return x.flg; 13 | } 14 | 15 | int main(void) 16 | { 17 | struct rstate y; 18 | y.flg = 0x11223344; 19 | if (flg(&y) != 0x11223344) 20 | return 1; 21 | if (y.flg != 0x11223344) 22 | return 2; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /test/t90.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | char (*p)[10]; 4 | char a[10] = "abcd"; 5 | p = a; 6 | if (sizeof(p) != sizeof(long)) 7 | return 1; 8 | if (sizeof(p[0]) != 10) 9 | return 1; 10 | if (p[0][2] != 'c') 11 | return 2; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /test/t91.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | char (*p[3])[10]; 4 | char a[10] = "abcd"; 5 | p[1] = a; 6 | if (sizeof(p) != sizeof(long) * 3) 7 | return 1; 8 | if (sizeof(p[1][0]) != 10) 9 | return 1; 10 | if (p[1][0][2] != 'c') 11 | return 2; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /test/t92.c: -------------------------------------------------------------------------------- 1 | #define n_itn (*nreg(map(".itn"))) /* .it lines left */ 2 | 3 | int x = 2; 4 | 5 | int map(char *x) 6 | { 7 | return 1; 8 | } 9 | 10 | int *nreg(int id) 11 | { 12 | return &x; 13 | } 14 | 15 | int main(void) 16 | { 17 | int i = --(n_itn); 18 | return x - 1; 19 | } 20 | -------------------------------------------------------------------------------- /test/t93.c: -------------------------------------------------------------------------------- 1 | static int f(void) 2 | { 3 | char s1[1] = ""; 4 | return 0; 5 | } 6 | 7 | int main(void) 8 | { 9 | return f(); 10 | } 11 | -------------------------------------------------------------------------------- /test/t94.c: -------------------------------------------------------------------------------- 1 | static int f(int a, int b, int c, int d) 2 | { 3 | int *p = &d; 4 | return p ? d : 0; 5 | } 6 | 7 | int main(void) 8 | { 9 | return f(1, 2, 3, 4) != 4; 10 | } 11 | -------------------------------------------------------------------------------- /test/t95.c: -------------------------------------------------------------------------------- 1 | int uc_slen(char *s) 2 | { 3 | return 5; 4 | } 5 | 6 | char **uc_chop(char *s, int *n) 7 | { 8 | char **chrs; 9 | *n = uc_slen(s); 10 | return 0; 11 | } 12 | 13 | int main(void) 14 | { 15 | int n; 16 | uc_chop(0, &n); 17 | if (n != 5) 18 | return 1; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /test/t96.c: -------------------------------------------------------------------------------- 1 | /* Reported by Morten Brøns-Pedersen */ 2 | int f(int a) 3 | { 4 | return 1 >= a; 5 | } 6 | int main() { 7 | return f(1) == 0; 8 | } 9 | -------------------------------------------------------------------------------- /test/t97.c: -------------------------------------------------------------------------------- 1 | int main(void) 2 | { 3 | int x = 0; 4 | unsigned int y = 0; 5 | if (x < -1) 6 | return 1; 7 | if (y > -1) 8 | return 1; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /test/t98.c: -------------------------------------------------------------------------------- 1 | #define S(x) #x 2 | #define S3(x, y, z) #x#y /* #x */ #z 3 | 4 | int main(void) 5 | { 6 | char *s = S(abc); 7 | char *r = S3(a, b, c); 8 | if (s[0] != 'a' || s[1] != 'b' || s[2] != 'c' || s[3]) 9 | return 1; 10 | if (r[0] != 'a' || r[1] != 'b' || r[2] != 'c' || r[3]) 11 | return 2; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /test/t99.c: -------------------------------------------------------------------------------- 1 | #define T(x) a##x 2 | #define T3(x, y, z) a##x##y##z 3 | 4 | int main(void) 5 | { 6 | int aabc = 1; 7 | if (T(abc) != 1) 8 | return 2; 9 | if (T3(a, b, c) != 1) 10 | return 2; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /test/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | for i in `seq -w 0 30` 3 | do 4 | f="b$i.x" 5 | if test -f $f 6 | then 7 | n=`echo $i | sed 's/^0\+//'` 8 | ./$f 9 | o="$?" 10 | if test $o != $n 11 | then 12 | echo "$f $o" 13 | fi 14 | fi 15 | done 16 | 17 | for x in t??.x 18 | do 19 | ./$x 20 | o=$? 21 | if test "$o" != "0" 22 | then 23 | echo "$x $o" 24 | fi 25 | done 26 | -------------------------------------------------------------------------------- /test/x64.s: -------------------------------------------------------------------------------- 1 | format ELF64 2 | 3 | extrn main 4 | public _start 5 | _start: 6 | xor rbp, rbp 7 | pop rdi ; argc 8 | mov rsi, rsp ; argv 9 | push rdi 10 | lea rdx, [rsi + rdi * 8 + 8]; envp 11 | and rsp, -16 ; align rsp 12 | 13 | call main 14 | mov rdi, rax 15 | mov rax, 60 16 | syscall 17 | -------------------------------------------------------------------------------- /test/x86.s: -------------------------------------------------------------------------------- 1 | format ELF 2 | 3 | extrn main 4 | public _start 5 | _start: 6 | xor ebp, ebp 7 | xor eax, eax 8 | push 6 9 | call main 10 | pop edx 11 | mov ebx, eax 12 | mov eax, 1 13 | int 0x80 14 | --------------------------------------------------------------------------------