path
stringlengths
14
112
content
stringlengths
0
6.32M
size
int64
0
6.32M
max_lines
int64
1
100k
repo_name
stringclasses
2 values
autogenerated
bool
1 class
cosmopolitan/third_party/chibicc/as.main.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/chibicc/chibicc.h" int main(int argc, char *argv[]) { ShowCrashReports(); Assembler(argc, argv); return 0; }
1,974
26
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/file.c
#include "libc/intrin/bsf.h" #include "third_party/chibicc/chibicc.h" // Slurps contents of file. char *read_file(const char *path) { char *p; FILE *fp; int buflen, nread, end, n; if (!strcmp(path, "-")) { fp = stdin; } else { fp = fopen(path, "r"); if (!fp) return NULL; } buflen = 4096; nread = 0; p = calloc(1, buflen); for (;;) { end = buflen - 2; n = fread(p + nread, 1, end - nread, fp); if (n == 0) break; nread += n; if (nread == end) { buflen *= 2; p = realloc(p, buflen); } } if (fp != stdin) fclose(fp); if (nread > 0 && p[nread - 1] == '\\') { p[nread - 1] = '\n'; } else if (nread == 0 || p[nread - 1] != '\n') { p[nread++] = '\n'; } p[nread] = '\0'; return p; } char *skip_bom(char *p) { // UTF-8 texts may start with a 3-byte "BOM" marker sequence. // If exists, just skip them because they are useless bytes. // (It is actually not recommended to add BOM markers to UTF-8 // texts, but it's not uncommon particularly on Windows.) if (!memcmp(p, "\357\273\277", 3)) p += 3; return p; } // Replaces \r or \r\n with \n. void canonicalize_newline(char *p) { char *q = p; for (;;) { #if defined(__GNUC__) && defined(__x86_64__) && !defined(__chibicc__) // :'( typedef char xmm_u __attribute__((__vector_size__(16), __aligned__(1))); typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16))); if (!((uintptr_t)p & 15)) { xmm_t v; unsigned m; xmm_t z = {0}; xmm_t s = {'\r', '\r', '\r', '\r', '\r', '\r', '\r', '\r', '\r', '\r', '\r', '\r', '\r', '\r', '\r', '\r'}; xmm_t t = {'\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n'}; for (;;) { v = *(const xmm_t *)p; m = __builtin_ia32_pmovmskb128((v == z) | (v == s) | (v == t)); if (!m) { *(xmm_u *)q = v; p += 16; q += 16; } else { m = _bsf(m); memmove(q, p, m); p += m; q += m; break; } } } #endif if (p[0]) { if (p[0] == '\r' && p[1] == '\n') { p += 2; *q++ = '\n'; } else if (p[0] == '\r') { p += 1; *q++ = '\n'; } else { *q++ = *p++; } } else { break; } } *q = '\0'; } // Removes backslashes followed by a newline. void remove_backslash_newline(char *p) { int i = 0, j = 0; // We want to keep the number of newline characters so that // the logical line number matches the physical one. // This counter maintain the number of newlines we have removed. int n = 0; bool instring = false; for (;;) { #if defined(__GNUC__) && defined(__x86_64__) && !defined(__chibicc__) // :'( typedef char xmm_u __attribute__((__vector_size__(16), __aligned__(1))); typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16))); if (!((uintptr_t)(p + i) & 15)) { xmm_t v; unsigned m; xmm_t A = {0}; xmm_t B = {'/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/'}; xmm_t C = {'"', '"', '"', '"', '"', '"', '"', '"', '"', '"', '"', '"', '"', '"', '"', '"'}; xmm_t D = {'\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\'}; xmm_t E = {'\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n'}; for (;;) { v = *(const xmm_t *)(p + i); m = __builtin_ia32_pmovmskb128((v == A) | (v == B) | (v == C) | (v == D) | (v == E)); if (!m) { *(xmm_u *)(p + j) = v; i += 16; j += 16; } else { m = _bsf(m); memmove(p + j, p + i, m); i += m; j += m; break; } } } #endif if (p[i]) { if (instring) { if (p[i] == '"' && p[i - 1] != '\\') { instring = false; } } else { if (p[i] == '"') { instring = true; } else if (p[i] == '/' && p[i + 1] == '*') { p[j++] = p[i++]; p[j++] = p[i++]; while (p[i]) { if (p[i] == '*' && p[i + 1] == '/') { p[j++] = p[i++]; p[j++] = p[i++]; break; } else { p[j++] = p[i++]; } } continue; } } if (p[i] == '\\' && p[i + 1] == '\n') { i += 2; n++; } else if (p[i] == '\n') { p[j++] = p[i++]; for (; n > 0; n--) p[j++] = '\n'; } else { p[j++] = p[i++]; } } else { break; } } for (; n > 0; n--) p[j++] = '\n'; p[j] = '\0'; }
4,880
177
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/chibicc.mk
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐ #───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘ # # SYNOPSIS # # C Compiler # # OVERVIEW # # This makefile compiles and runs each test twice. The first with # GCC-built chibicc, and a second time with chibicc-built chibicc ifeq ($(ARCH), x86_64) CHIBICC = o/$(MODE)/third_party/chibicc/chibicc.com CHIBICC_FLAGS = \ -fno-common \ -include libc/integral/normalize.inc \ -DIMAGE_BASE_VIRTUAL=$(IMAGE_BASE_VIRTUAL) \ -DMODE='"$(MODE)"' PKGS += THIRD_PARTY_CHIBICC THIRD_PARTY_CHIBICC_ARTIFACTS += THIRD_PARTY_CHIBICC_A THIRD_PARTY_CHIBICC = $(THIRD_PARTY_CHIBICC_A_DEPS) $(THIRD_PARTY_CHIBICC_A) THIRD_PARTY_CHIBICC_A = o/$(MODE)/third_party/chibicc/chibicc.a THIRD_PARTY_CHIBICC_A_FILES := $(wildcard third_party/chibicc/*) THIRD_PARTY_CHIBICC_A_HDRS = $(filter %.h,$(THIRD_PARTY_CHIBICC_A_FILES)) THIRD_PARTY_CHIBICC_A_SRCS = $(filter %.c,$(THIRD_PARTY_CHIBICC_A_FILES)) THIRD_PARTY_CHIBICC_A_INCS = $(filter %.inc,$(THIRD_PARTY_CHIBICC_A_FILES)) THIRD_PARTY_CHIBICC_DEFINES = \ -DCRT=\"$(CRT)\" \ -DAPE=\"o/$(MODE)/ape/ape.o\" \ -DLDS=\"o/$(MODE)/ape/ape.lds\" THIRD_PARTY_CHIBICC_BINS = \ o/$(MODE)/third_party/chibicc/chibicc.com.dbg \ o/$(MODE)/third_party/chibicc/chibicc.com THIRD_PARTY_CHIBICC_A_OBJS = \ $(THIRD_PARTY_CHIBICC_A_SRCS:%.c=o/$(MODE)/%.o) THIRD_PARTY_CHIBICC_A_CHECKS = \ $(THIRD_PARTY_CHIBICC_A).pkg \ $(THIRD_PARTY_CHIBICC_A_HDRS:%=o/$(MODE)/%.ok) THIRD_PARTY_CHIBICC_A_DIRECTDEPS = \ LIBC_CALLS \ LIBC_FMT \ LIBC_INTRIN \ LIBC_LOG \ LIBC_LOG \ LIBC_MEM \ LIBC_NEXGEN32E \ LIBC_RUNTIME \ LIBC_STDIO \ LIBC_STR \ LIBC_STUBS \ LIBC_SYSV \ LIBC_TIME \ LIBC_X \ THIRD_PARTY_COMPILER_RT \ THIRD_PARTY_DLMALLOC \ TOOL_BUILD_LIB \ THIRD_PARTY_GDTOA THIRD_PARTY_CHIBICC_A_DEPS := \ $(call uniq,$(foreach x,$(THIRD_PARTY_CHIBICC_A_DIRECTDEPS),$($(x)))) $(THIRD_PARTY_CHIBICC_A): \ third_party/chibicc/ \ $(THIRD_PARTY_CHIBICC_A).pkg \ $(THIRD_PARTY_CHIBICC_A_OBJS) $(THIRD_PARTY_CHIBICC_A).pkg: \ $(THIRD_PARTY_CHIBICC_A_OBJS) \ $(foreach x,$(THIRD_PARTY_CHIBICC_A_DIRECTDEPS),$($(x)_A).pkg) o/$(MODE)/third_party/chibicc/chibicc.com.dbg: \ $(THIRD_PARTY_CHIBICC_A_DEPS) \ $(THIRD_PARTY_CHIBICC_A) \ $(APE_NO_MODIFY_SELF) \ $(CRT) \ o/$(MODE)/third_party/chibicc/help.txt.zip.o \ o/$(MODE)/third_party/chibicc/chibicc.main.o \ $(THIRD_PARTY_CHIBICC_A).pkg @$(APELINK) o/$(MODE)/third_party/chibicc/chibicc.com: \ o/$(MODE)/third_party/chibicc/chibicc.com.dbg \ o/$(MODE)/third_party/zip/zip.com \ o/$(MODE)/tool/build/symtab.com @$(MAKE_OBJCOPY) @$(MAKE_SYMTAB_CREATE) @$(MAKE_SYMTAB_ZIP) o/$(MODE)/third_party/chibicc/as.com.dbg: \ $(THIRD_PARTY_CHIBICC_A_DEPS) \ $(THIRD_PARTY_CHIBICC_A) \ $(APE_NO_MODIFY_SELF) \ $(CRT) \ o/$(MODE)/third_party/chibicc/as.main.o \ $(THIRD_PARTY_CHIBICC_A).pkg @$(APELINK) o/$(MODE)/third_party/chibicc/chibicc.o: private \ OVERRIDE_CPPFLAGS += $(THIRD_PARTY_CHIBICC_DEFINES) THIRD_PARTY_CHIBICC_LIBS = $(foreach x,$(THIRD_PARTY_CHIBICC_ARTIFACTS),$($(x))) THIRD_PARTY_CHIBICC_SRCS = $(foreach x,$(THIRD_PARTY_CHIBICC_ARTIFACTS),$($(x)_SRCS)) THIRD_PARTY_CHIBICC_HDRS = $(foreach x,$(THIRD_PARTY_CHIBICC_ARTIFACTS),$($(x)_HDRS)) THIRD_PARTY_CHIBICC_INCS = $(foreach x,$(THIRD_PARTY_CHIBICC_ARTIFACTS),$($(x)_INCS)) THIRD_PARTY_CHIBICC_CHECKS = $(foreach x,$(THIRD_PARTY_CHIBICC_ARTIFACTS),$($(x)_CHECKS)) THIRD_PARTY_CHIBICC_OBJS = $(foreach x,$(THIRD_PARTY_CHIBICC_ARTIFACTS),$($(x)_OBJS)) $(THIRD_PARTY_CHIBICC_OBJS): $(BUILD_FILES) third_party/chibicc/chibicc.mk endif .PHONY: o/$(MODE)/third_party/chibicc o/$(MODE)/third_party/chibicc: \ o/$(MODE)/third_party/chibicc/test \ $(THIRD_PARTY_CHIBICC_BINS) \ $(THIRD_PARTY_CHIBICC_CHECKS)
4,092
123
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/kw.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2021 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/chibicc/chibicc.h" #include "third_party/chibicc/kw.h" #include "third_party/chibicc/kw.inc" /** * Returns small number for HTTP header, or -1 if not found. */ unsigned char GetKw(const char *str, size_t len) { const struct KwSlot *slot; if ((slot = LookupKw(str, len))) { return slot->code; } else { return 0; } }
2,192
34
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/README.cosmo
chibicc is the simplest/tiniest/hackable/readable c11 compiler in the world that can build projects like python but goes 2x slower than gcc with 2x the code size, because it doesn't optimize or color registers although it can compile code at 5x the speed and could be even faster which is great, considering it's a 220kb αcτµαlly pδrταblε εxεcµταblε local enhancements - add assembler - support dce - support gnu asm - support __int128 - support _Static_assert - support __vector_size__ - support __builtin_add_overflow, etc. - support GCC C11 __atomic_* primitives - support __builtin_memcpy, strlen, strpbrk, etc. - support __builtin_constant_p, __builtin_likely, etc. - support __builtin_isunordered, __builtin_islessgreater, etc. - support __builtin_ctz, __builtin_bswap, __builtin_popcount, etc. - support __force_align_arg_pointer__, __no_caller_saved_registers__, etc. - support __constructor__, __section__, __cold__, -ffunction-sections, etc. - support building -x assembler-with-cpp a.k.a. .S files - support profiling w/ -mcount / -mfentry / -mnop-mcount - improve error messages to trace macro expansions - reduce #lines of generated assembly by a third - reduce #bytes of generated binary by a third - report divide errors in constexprs - use perfect hash table for keywords local bug fixes - allow casted values to be lvalues - permit remainder operator in constexprs - permit parentheses around string-initializer - fix 64-bit bug in generated code for struct bitfields - fix struct_designator() so it won't crash on anonymous union members - fix bug where last statement in statement expression couldn't have label - print_tokens (chibicc -E) now works in the case of adjacent string literals - make enums unsigned (like gcc) so we don't suffer the msvc enum bitfield bug local changes - use tabs in generated output - parse javadoc-style markdown comments - don't fold backslash newline in comments - generated code no longer assumes red zone - emit .size directives for function definitions - use fisttp long double conversions if built w/ -msse3
2,085
50
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/kw.inc
/* ANSI-C code produced by gperf version 3.1 */ /* Command-line: gperf kw.gperf */ /* Computed positions: -k'1,4,11,14,17,$' */ // clang-format off #if !((' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \ && ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \ && (')' == 41) && ('*' == 42) && ('+' == 43) && (',' == 44) \ && ('-' == 45) && ('.' == 46) && ('/' == 47) && ('0' == 48) \ && ('1' == 49) && ('2' == 50) && ('3' == 51) && ('4' == 52) \ && ('5' == 53) && ('6' == 54) && ('7' == 55) && ('8' == 56) \ && ('9' == 57) && (':' == 58) && (';' == 59) && ('<' == 60) \ && ('=' == 61) && ('>' == 62) && ('?' == 63) && ('A' == 65) \ && ('B' == 66) && ('C' == 67) && ('D' == 68) && ('E' == 69) \ && ('F' == 70) && ('G' == 71) && ('H' == 72) && ('I' == 73) \ && ('J' == 74) && ('K' == 75) && ('L' == 76) && ('M' == 77) \ && ('N' == 78) && ('O' == 79) && ('P' == 80) && ('Q' == 81) \ && ('R' == 82) && ('S' == 83) && ('T' == 84) && ('U' == 85) \ && ('V' == 86) && ('W' == 87) && ('X' == 88) && ('Y' == 89) \ && ('Z' == 90) && ('[' == 91) && ('\\' == 92) && (']' == 93) \ && ('^' == 94) && ('_' == 95) && ('a' == 97) && ('b' == 98) \ && ('c' == 99) && ('d' == 100) && ('e' == 101) && ('f' == 102) \ && ('g' == 103) && ('h' == 104) && ('i' == 105) && ('j' == 106) \ && ('k' == 107) && ('l' == 108) && ('m' == 109) && ('n' == 110) \ && ('o' == 111) && ('p' == 112) && ('q' == 113) && ('r' == 114) \ && ('s' == 115) && ('t' == 116) && ('u' == 117) && ('v' == 118) \ && ('w' == 119) && ('x' == 120) && ('y' == 121) && ('z' == 122) \ && ('{' == 123) && ('|' == 124) && ('}' == 125) && ('~' == 126)) /* The character set is not based on ISO-646. */ #error "gperf generated tables don't work with this execution character set. Please report a bug to <[email protected]>." #endif #line 1 "kw.gperf" #include "libc/str/str.h" #include "third_party/chibicc/kw.h" #line 10 "kw.gperf" struct thatispacked KwSlot { char *name; unsigned char code; }; #define TOTAL_KEYWORDS 124 #define MIN_WORD_LENGTH 1 #define MAX_WORD_LENGTH 28 #define MIN_HASH_VALUE 1 #define MAX_HASH_VALUE 256 /* maximum key range = 256, duplicates = 0 */ static inline unsigned int hash (register const char *str, register size_t len) { static const unsigned short asso_values[] = { 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 100, 257, 257, 257, 257, 25, 257, 95, 90, 85, 5, 257, 0, 80, 257, 257, 257, 0, 257, 257, 257, 257, 257, 10, 257, 257, 257, 257, 257, 35, 257, 257, 257, 0, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 5, 257, 15, 120, 5, 25, 0, 0, 45, 85, 115, 65, 0, 25, 90, 40, 50, 5, 15, 25, 10, 0, 0, 10, 5, 20, 5, 90, 75, 20, 70, 65, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257, 257 }; register unsigned int hval = len; switch (hval) { default: hval += asso_values[(unsigned char)str[16]]; /*FALLTHROUGH*/ case 16: case 15: case 14: hval += asso_values[(unsigned char)str[13]]; /*FALLTHROUGH*/ case 13: case 12: case 11: hval += asso_values[(unsigned char)str[10]]; /*FALLTHROUGH*/ case 10: case 9: case 8: case 7: case 6: case 5: case 4: hval += asso_values[(unsigned char)str[3]+1]; /*FALLTHROUGH*/ case 3: case 2: case 1: hval += asso_values[(unsigned char)str[0]]; break; } return hval + asso_values[(unsigned char)str[len - 1]]; } static inline const struct thatispacked KwSlot * LookupKw (register const char *str, register size_t len) { static const struct thatispacked KwSlot wordlist[] = { {""}, #line 108 "kw.gperf" {"-", KW_MINUS}, #line 114 "kw.gperf" {"--", KW_DECREMENT}, {""}, #line 19 "kw.gperf" {"else", KW_ELSE}, #line 64 "kw.gperf" {"undef", KW_UNDEF}, #line 63 "kw.gperf" {"typeof", KW_TYPEOF}, #line 62 "kw.gperf" {"typedef", KW_TYPEDEF}, {""}, #line 15 "kw.gperf" {"case", KW_CASE}, #line 29 "kw.gperf" {"const", KW_CONST}, #line 107 "kw.gperf" {"+", KW_PLUS}, #line 113 "kw.gperf" {"++", KW_INCREMENT}, #line 35 "kw.gperf" {"continue", KW_CONTINUE}, {""}, #line 78 "kw.gperf" {"__restrict", KW_RESTRICT}, #line 22 "kw.gperf" {"sizeof", KW_SIZEOF}, #line 75 "kw.gperf" {"__asm__", KW___ASM__}, {""}, #line 121 "kw.gperf" {"__atomic_store", KW___ATOMIC_STORE}, #line 73 "kw.gperf" {"__VA_OPT__", KW___VA_OPT__}, #line 16 "kw.gperf" {"static", KW_STATIC}, #line 68 "kw.gperf" {"_Atomic", KW__ATOMIC}, #line 45 "kw.gperf" {"__attribute__", KW___ATTRIBUTE__}, {""}, #line 31 "kw.gperf" {"short", KW_SHORT}, #line 13 "kw.gperf" {"struct", KW_STRUCT}, #line 79 "kw.gperf" {"__restrict__", KW_RESTRICT}, #line 20 "kw.gperf" {"for", KW_FOR}, #line 55 "kw.gperf" {"line", KW_LINE}, {""}, #line 85 "kw.gperf" {"__builtin_expect", KW___BUILTIN_EXPECT}, #line 60 "kw.gperf" {"strpbrk", KW_STRPBRK}, #line 57 "kw.gperf" {"restrict", KW_RESTRICT}, {""}, #line 49 "kw.gperf" {"error", KW_ERROR}, #line 28 "kw.gperf" {"double", KW_DOUBLE}, #line 117 "kw.gperf" {"->", KW_ARROW}, #line 86 "kw.gperf" {"__builtin_ffs", KW___BUILTIN_FFS}, #line 17 "kw.gperf" {"void", KW_VOID}, #line 69 "kw.gperf" {"_Bool", KW__BOOL}, #line 61 "kw.gperf" {"strstr", KW_STRSTR}, #line 116 "kw.gperf" {"||", KW_LOGOR}, {""}, #line 18 "kw.gperf" {"char", KW_CHAR}, {""}, #line 50 "kw.gperf" {"extern", KW_EXTERN}, #line 99 "kw.gperf" {"__builtin_strpbrk", KW___BUILTIN_STRPBRK}, {""}, #line 47 "kw.gperf" {"elif", KW_ELIF}, #line 26 "kw.gperf" {"union", KW_UNION}, #line 109 "kw.gperf" {"&", KW_AMP}, #line 115 "kw.gperf" {"&&", KW_LOGAND}, #line 102 "kw.gperf" {"__builtin_types_compatible_p", KW___BUILTIN_TYPES_COMPATIBLE_P}, {""}, {""}, #line 129 "kw.gperf" {"__atomic_test_and_set", KW___ATOMIC_TEST_AND_SET}, #line 101 "kw.gperf" {"__builtin_sub_overflow", KW___BUILTIN_SUB_OVERFLOW}, #line 72 "kw.gperf" {"_Thread_local", KW__THREAD_LOCAL}, {""}, {""}, #line 122 "kw.gperf" {"__atomic_store_n", KW___ATOMIC_STORE_N}, #line 82 "kw.gperf" {"__builtin_add_overflow", KW___BUILTIN_ADD_OVERFLOW}, #line 76 "kw.gperf" {"__inline", KW_INLINE}, #line 46 "kw.gperf" {"_Noreturn", KW__NORETURN}, {""}, #line 58 "kw.gperf" {"strchr", KW_STRCHR}, {""}, #line 70 "kw.gperf" {"_Generic", KW__GENERIC}, {""}, #line 48 "kw.gperf" {"endif", KW_ENDIF}, #line 97 "kw.gperf" {"__builtin_strchr", KW___BUILTIN_STRCHR}, {""}, #line 77 "kw.gperf" {"__int128", KW___INT128}, #line 43 "kw.gperf" {"auto", KW_AUTO}, #line 89 "kw.gperf" {"__builtin_fpclassify", KW___BUILTIN_FPCLASSIFY}, #line 100 "kw.gperf" {"__builtin_strstr", KW___BUILTIN_STRSTR}, #line 21 "kw.gperf" {"do", KW_DO}, #line 67 "kw.gperf" {"_Alignof", KW__ALIGNOF}, #line 87 "kw.gperf" {"__builtin_ffsl", KW___BUILTIN_FFSL}, #line 88 "kw.gperf" {"__builtin_ffsll", KW___BUILTIN_FFSLL}, #line 14 "kw.gperf" {"return", KW_RETURN}, {""}, #line 93 "kw.gperf" {"__builtin_popcount", KW___BUILTIN_POPCOUNT}, #line 83 "kw.gperf" {"__builtin_assume_aligned", KW___BUILTIN_ASSUME_ALIGNED}, {""}, {""}, #line 91 "kw.gperf" {"__builtin_neg_overflow", KW___BUILTIN_NEG_OVERFLOW}, #line 66 "kw.gperf" {"_Alignas", KW__ALIGNAS}, #line 96 "kw.gperf" {"__builtin_reg_class", KW___BUILTIN_REG_CLASS}, {""}, #line 32 "kw.gperf" {"signed", KW_SIGNED}, {""}, #line 119 "kw.gperf" {"__atomic_load", KW___ATOMIC_LOAD}, #line 123 "kw.gperf" {"__atomic_clear", KW___ATOMIC_CLEAR}, #line 84 "kw.gperf" {"__builtin_constant_p", KW___BUILTIN_CONSTANT_P}, #line 39 "kw.gperf" {"define", KW_DEFINE}, {""}, #line 23 "kw.gperf" {"unsigned", KW_UNSIGNED}, #line 135 "kw.gperf" {"__atomic_exchange_n", KW___ATOMIC_EXCHANGE_N}, #line 25 "kw.gperf" {"while", KW_WHILE}, #line 27 "kw.gperf" {"switch", KW_SWITCH}, {""}, #line 81 "kw.gperf" {"__typeof", KW_TYPEOF}, #line 51 "kw.gperf" {"goto", KW_GOTO}, {""}, #line 98 "kw.gperf" {"__builtin_strlen", KW___BUILTIN_STRLEN}, {""}, #line 41 "kw.gperf" {"asm", KW_ASM}, #line 94 "kw.gperf" {"__builtin_popcountl", KW___BUILTIN_POPCOUNTL}, #line 95 "kw.gperf" {"__builtin_popcountll", KW___BUILTIN_POPCOUNTLL}, #line 56 "kw.gperf" {"pragma", KW_PRAGMA}, {""}, {""}, {""}, #line 120 "kw.gperf" {"__atomic_load_n", KW___ATOMIC_LOAD_N}, #line 74 "kw.gperf" {"__alignof__", KW___ALIGNOF__}, #line 12 "kw.gperf" {"if", KW_IF}, #line 54 "kw.gperf" {"int", KW_INT}, {""}, #line 37 "kw.gperf" {"ifdef", KW_IFDEF}, #line 38 "kw.gperf" {"ifndef", KW_IFNDEF}, #line 40 "kw.gperf" {"defined", KW_DEFINED}, #line 44 "kw.gperf" {"register", KW_REGISTER}, #line 130 "kw.gperf" {"__sync_lock_test_and_set", KW___SYNC_LOCK_TEST_AND_SET}, #line 30 "kw.gperf" {"float", KW_FLOAT}, {""}, {""}, {""}, #line 131 "kw.gperf" {"__sync_lock_release", KW___SYNC_LOCK_RELEASE}, {""}, #line 112 "kw.gperf" {"~", KW_TILDE}, {""}, {""}, #line 34 "kw.gperf" {"enum", KW_ENUM}, {""}, {""}, #line 90 "kw.gperf" {"__builtin_mul_overflow", KW___BUILTIN_MUL_OVERFLOW}, #line 65 "kw.gperf" {"volatile", KW_VOLATILE}, {""}, {""}, #line 106 "kw.gperf" {"}", KW_RB}, #line 134 "kw.gperf" {"__atomic_compare_exchange_n", KW___ATOMIC_COMPARE_EXCHANGE_N}, #line 92 "kw.gperf" {"__builtin_offsetof", KW___BUILTIN_OFFSETOF}, {""}, {""}, #line 59 "kw.gperf" {"strlen", KW_STRLEN}, {""}, {""}, #line 71 "kw.gperf" {"_Static_assert", KW__STATIC_ASSERT}, {""}, #line 105 "kw.gperf" {"{", KW_LB}, #line 42 "kw.gperf" {"default", KW_DEFAULT}, #line 80 "kw.gperf" {"__thread", KW__THREAD_LOCAL}, {""}, {""}, {""}, #line 128 "kw.gperf" {"__atomic_fetch_or", KW___ATOMIC_FETCH_OR}, #line 124 "kw.gperf" {"__atomic_fetch_add", KW___ATOMIC_FETCH_ADD}, #line 24 "kw.gperf" {"long", KW_LONG}, {""}, #line 118 "kw.gperf" {".", KW_DOT}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, #line 110 "kw.gperf" {"*", KW_STAR}, {""}, #line 126 "kw.gperf" {"__atomic_fetch_and", KW___ATOMIC_FETCH_AND}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, #line 104 "kw.gperf" {")", KW_RP}, {""}, #line 127 "kw.gperf" {"__atomic_fetch_xor", KW___ATOMIC_FETCH_XOR}, {""}, {""}, #line 53 "kw.gperf" {"inline", KW_INLINE}, {""}, {""}, {""}, {""}, #line 103 "kw.gperf" {"(", KW_LP}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, #line 111 "kw.gperf" {"!", KW_EXCLAIM}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, #line 36 "kw.gperf" {"include", KW_INCLUDE}, {""}, {""}, {""}, {""}, #line 132 "kw.gperf" {"__builtin_ia32_movntdq", KW___BUILTIN_IA32_MOVNTDQ}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, #line 125 "kw.gperf" {"__atomic_fetch_sub", KW___ATOMIC_FETCH_SUB}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, #line 52 "kw.gperf" {"include_next", KW_INCLUDE_NEXT}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, #line 33 "kw.gperf" {"break", KW_BREAK}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, #line 133 "kw.gperf" {"__builtin_ia32_pmovmskb128", KW___BUILTIN_IA32_PMOVMSKB128} }; if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH) { register unsigned int key = hash (str, len); if (key <= MAX_HASH_VALUE) { register const char *s = wordlist[key].name; if (*str == *s && !strncmp (str + 1, s + 1, len - 1) && s[len] == '\0') return &wordlist[key]; } } return 0; }
16,035
436
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/chibicc.h
#ifndef COSMOPOLITAN_THIRD_PARTY_CHIBICC_CHIBICC_H_ #define COSMOPOLITAN_THIRD_PARTY_CHIBICC_CHIBICC_H_ #include "libc/assert.h" #include "libc/calls/calls.h" #include "libc/calls/struct/stat.h" #include "libc/calls/weirdtypes.h" #include "libc/errno.h" #include "libc/fmt/conv.h" #include "libc/fmt/fmt.h" #include "libc/fmt/itoa.h" #include "libc/intrin/popcnt.h" #include "libc/limits.h" #include "libc/log/check.h" #include "libc/log/log.h" #include "libc/macros.internal.h" #include "libc/mem/mem.h" #include "libc/nexgen32e/crc32.h" #include "libc/runtime/runtime.h" #include "libc/stdio/stdio.h" #include "libc/stdio/temp.h" #include "libc/str/str.h" #include "libc/str/unicode.h" #include "libc/time/struct/tm.h" #include "libc/time/time.h" #include "libc/x/x.h" #include "third_party/gdtoa/gdtoa.h" #include "tool/build/lib/javadown.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ #pragma GCC diagnostic ignored "-Wswitch" typedef struct Asm Asm; typedef struct AsmOperand AsmOperand; typedef struct File File; typedef struct FpClassify FpClassify; typedef struct HashMap HashMap; typedef struct Hideset Hideset; typedef struct Macro Macro; typedef struct MacroParam MacroParam; typedef struct Member Member; typedef struct Node Node; typedef struct Obj Obj; typedef struct Relocation Relocation; typedef struct Relocation Relocation; typedef struct StaticAsm StaticAsm; typedef struct StringArray StringArray; typedef struct Token Token; typedef struct TokenStack TokenStack; typedef struct Type Type; typedef Token *macro_handler_fn(Token *); // // strarray.c // struct StringArray { char **data; int capacity; int len; }; void strarray_push(StringArray *, char *); // // tokenize.c // typedef enum { TK_IDENT, // Identifiers TK_PUNCT, // Punctuators TK_KEYWORD, // Keywords TK_STR, // String literals TK_NUM, // Numeric literals TK_PP_NUM, // Preprocessing numbers TK_JAVADOWN, // /** ... */ comments TK_EOF, // End-of-file markers } TokenKind; struct File { char *name; int file_no; char *contents; // For #line directive char *display_name; int line_delta; struct Javadown *javadown; }; struct thatispacked Token { Token *next; // Next token int len; // Token length int line_no; // Line number int line_delta; // Line number TokenKind kind; // Token kind uint8_t kw; // Keyword Phash bool at_bol; // True if this token is at beginning of line bool has_space; // True if this token follows a space character char *loc; // Token location Type *ty; // Used if TK_NUM or TK_STR File *file; // Source location char *filename; // Filename Hideset *hideset; // For macro expansion Token *origin; // If this is expanded from a macro, the original token struct Javadown *javadown; union { int64_t val; // If kind is TK_NUM, its value long double fval; // If kind is TK_NUM, its value char *str; // String literal contents including terminating '\0' }; }; void error(char *, ...) __attribute__((__noreturn__, __format__(__printf__, 1, 2))); void error_at(char *, char *, ...) __attribute__((__noreturn__, __format__(__printf__, 2, 3))); void error_tok(Token *, char *, ...) __attribute__((__noreturn__, __format__(__printf__, 2, 3))); void warn_tok(Token *, char *, ...) __attribute__((__format__(__printf__, 2, 3))); File **get_input_files(void); File *new_file(char *, int, char *); Token *skip(Token *, char); Token *tokenize(File *); Token *tokenize_file(char *); Token *tokenize_string_literal(Token *, Type *); bool consume(Token **, Token *, char *, size_t); bool equal(Token *, char *, size_t); void convert_pp_tokens(Token *); int read_escaped_char(char **, char *); #define UNREACHABLE() error("internal error at %s:%d", __FILE__, __LINE__) #define EQUAL(T, S) equal(T, S, sizeof(S) - 1) #define CONSUME(R, T, S) consume(R, T, S, sizeof(S) - 1) // // preprocess.c // struct MacroParam { MacroParam *next; char *name; }; struct Macro { char *name; bool is_objlike; // Object-like or function-like MacroParam *params; char *va_args_name; Token *body; macro_handler_fn *handler; Token *javadown; }; extern HashMap macros; char *search_include_paths(char *); void init_macros(void); void init_macros_conditional(void); void define_macro(char *, char *); void undef_macro(char *); Token *preprocess(Token *); // // asm.c // #define kAsmImm 1 #define kAsmMem 2 #define kAsmReg 4 #define kAsmMmx 8 #define kAsmXmm 16 #define kAsmFpu 32 #define kAsmRaw 64 #define kAsmFlag 128 struct AsmOperand { uint8_t reg; uint8_t type; char flow; char x87mask; bool isused; int regmask; int predicate; char *str; Node *node; Token *tok; int64_t val; char **label; }; struct Asm { int n; char *str; Token *tok; AsmOperand ops[20]; bool isgnu; bool flagclob; uint8_t x87clob; uint16_t regclob; uint16_t xmmclob; }; struct StaticAsm { StaticAsm *next; Asm *body; }; extern StaticAsm *staticasms; Asm *asm_stmt(Token **, Token *); void flushln(void); void gen_addr(Node *); void gen_asm(Asm *); void gen_expr(Node *); void pop(char *); void popreg(char *); void print_loc(int64_t, int64_t); void push(void); void pushreg(char *); // // fpclassify.c // struct FpClassify { Node *node; int args[5]; }; void gen_fpclassify(FpClassify *); // // parse.c // // Variable or function struct Obj { Obj *next; char *name; // Variable name Type *ty; // Type Token *tok; // representative token bool is_local; // local or global/function int align; // alignment // Local variable int offset; // Global variable or function bool is_function; bool is_definition; bool is_static; bool is_weak; bool is_externally_visible; char *asmname; char *section; char *visibility; Token *javadown; // Global variable bool is_tentative; bool is_string_literal; bool is_tls; char *init_data; Relocation *rel; // Function bool is_inline; bool is_aligned; bool is_noreturn; bool is_destructor; bool is_constructor; bool is_ms_abi; /* TODO */ bool is_no_instrument_function; bool is_force_align_arg_pointer; bool is_no_caller_saved_registers; int stack_size; Obj *params; Node *body; Obj *locals; Obj *va_area; Obj *alloca_bottom; // Dead Code Elimination bool is_live; bool is_root; StringArray refs; }; // Global variable can be initialized either by a constant expression // or a pointer to another global variable. This struct represents the // latter. struct Relocation { Relocation *next; int offset; char **label; long addend; }; typedef enum { ND_NULL_EXPR, // Do nothing ND_ADD, // + ND_SUB, // - ND_MUL, // * ND_DIV, // / ND_NEG, // unary - ND_REM, // % ND_BINAND, // & ND_BINOR, // | ND_BINXOR, // ^ ND_SHL, // << ND_SHR, // >> ND_EQ, // == ND_NE, // != ND_LT, // < ND_LE, // <= ND_ASSIGN, // = ND_COND, // ?: ND_COMMA, // , ND_MEMBER, // . (struct member access) ND_ADDR, // unary & ND_DEREF, // unary * ND_NOT, // ! ND_BITNOT, // ~ ND_LOGAND, // && ND_LOGOR, // || ND_RETURN, // "return" ND_IF, // "if" ND_FOR, // "for" or "while" ND_DO, // "do" ND_SWITCH, // "switch" ND_CASE, // "case" ND_BLOCK, // { ... } ND_GOTO, // "goto" ND_GOTO_EXPR, // "goto" labels-as-values ND_LABEL, // Labeled statement ND_LABEL_VAL, // [GNU] Labels-as-values ND_FUNCALL, // Function call ND_EXPR_STMT, // Expression statement ND_STMT_EXPR, // Statement expression ND_VAR, // Variable ND_VLA_PTR, // VLA designator ND_NUM, // Integer ND_CAST, // Type cast ND_MEMZERO, // Zero-clear a stack variable ND_ASM, // "asm" ND_CAS, // Atomic compare-and-swap ND_EXCH_N, // Atomic exchange with value ND_LOAD, // Atomic load to pointer ND_LOAD_N, // Atomic load to result ND_STORE, // Atomic store to pointer ND_STORE_N, // Atomic store to result ND_TESTANDSET, // Sync lock test and set ND_TESTANDSETA, // Atomic lock test and set ND_CLEAR, // Atomic clear ND_RELEASE, // Atomic lock release ND_FETCHADD, // Atomic fetch and add ND_FETCHSUB, // Atomic fetch and sub ND_FETCHXOR, // Atomic fetch and xor ND_FETCHAND, // Atomic fetch and and ND_FETCHOR, // Atomic fetch and or ND_SUBFETCH, // Atomic sub and fetch ND_FPCLASSIFY, // floating point classify ND_MOVNTDQ, // Intel MOVNTDQ ND_PMOVMSKB, // Intel PMOVMSKB } NodeKind; struct Node { NodeKind kind; // Node kind Node *next; // Next node Type *ty; // Type, e.g. int or pointer to int Token *tok; // Representative token Node *lhs; // Left-hand side Node *rhs; // Right-hand side // "if" or "for" statement Node *cond; Node *then; Node *els; Node *init; Node *inc; // Block or statement expression Node *body; // Function call Type *func_ty; Node *args; Obj *ret_buffer; bool pass_by_stack; bool realign_stack; // Switch Node *case_next; Node *default_case; // Goto or labeled statement, or labels-as-values char *label; char *unique_label; Node *goto_next; // "break" and "continue" labels char *brk_label; char *cont_label; // Case long begin; long end; // Struct member access Member *member; // Assembly Asm *azm; // Atomic compare-and-swap char memorder; Node *cas_addr; Node *cas_old; Node *cas_new; // Atomic op= operators Obj *atomic_addr; Node *atomic_expr; // Variable Obj *var; // Arithmetic Node *overflow; // Numeric literal int64_t val; long double fval; FpClassify *fpc; }; Node *expr(Token **, Token *); Node *new_cast(Node *, Type *); Node *new_node(NodeKind, Token *); Obj *parse(Token *); bool is_const_expr(Node *); char *ConsumeStringLiteral(Token **, Token *); int64_t const_expr(Token **, Token *); int64_t eval(Node *); int64_t eval2(Node *, char ***); // // debug.c // void print_ast(FILE *, Obj *); // // type.c // typedef enum { TY_VOID, TY_BOOL, TY_CHAR, TY_SHORT, TY_INT, TY_LONG, TY_INT128, TY_FLOAT, TY_DOUBLE, TY_LDOUBLE, TY_ENUM, TY_PTR, TY_FUNC, TY_ARRAY, TY_VLA, // variable-length array TY_STRUCT, TY_UNION, } TypeKind; struct Type { TypeKind kind; int size; // sizeof() value int align; // alignment bool is_unsigned; // unsigned or signed bool is_atomic; // true if _Atomic bool is_const; // const bool is_restrict; // restrict bool is_volatile; // volatile bool is_ms_abi; // microsoft abi bool is_static; // for array parameter pointer Type *origin; // for type compatibility check // Pointer-to or array-of type. We intentionally use the same member // to represent pointer/array duality in C. // // In many contexts in which a pointer is expected, we examine this // member instead of "kind" member to determine whether a type is a // pointer or not. That means in many contexts "array of T" is // naturally handled as if it were "pointer to T", as required by // the C spec. Type *base; // Declaration Token *name; Token *name_pos; // Array or decayed pointer int array_len; int vector_size; // Variable-length array Node *vla_len; // # of elements Obj *vla_size; // sizeof() value // Struct Member *members; bool is_flexible; bool is_packed; bool is_aligned; // Function type Type *return_ty; Type *params; bool is_variadic; Type *next; }; // Struct member struct Member { Member *next; Type *ty; Token *tok; // for error message Token *name; int idx; int align; int offset; // Bitfield bool is_bitfield; int bit_offset; int bit_width; }; extern Type ty_void[1]; extern Type ty_bool[1]; extern Type ty_char[1]; extern Type ty_short[1]; extern Type ty_int[1]; extern Type ty_long[1]; extern Type ty_int128[1]; extern Type ty_uchar[1]; extern Type ty_ushort[1]; extern Type ty_uint[1]; extern Type ty_ulong[1]; extern Type ty_uint128[1]; extern Type ty_float[1]; extern Type ty_double[1]; extern Type ty_ldouble[1]; bool is_integer(Type *); bool is_flonum(Type *); bool is_numeric(Type *); bool is_compatible(Type *, Type *); Type *copy_type(Type *); Type *pointer_to(Type *); Type *func_type(Type *); Type *array_of(Type *, int); Type *vla_of(Type *, Node *); Type *enum_type(void); Type *struct_type(void); void add_type(Node *); // // cast.c // void gen_cast(Type *, Type *); // // codegen.c // extern int depth; extern FILE *output_stream; int align_to(int, int); int count(void); void cmp_zero(Type *); void codegen(Obj *, FILE *); void gen_stmt(Node *); void println(char *, ...) __attribute__((__format__(__printf__, 1, 2))); // // unicode.c // int encode_utf8(char *, uint32_t); uint32_t decode_utf8(char **, char *); bool is_ident1(uint32_t); bool is_ident2(uint32_t); int display_width(char *, int); // // hashmap.c // typedef struct { char *key; int keylen; void *val; } HashEntry; struct HashMap { HashEntry *buckets; int capacity; int used; }; extern long chibicc_hashmap_hits; extern long chibicc_hashmap_miss; void *hashmap_get(HashMap *, char *); void *hashmap_get2(HashMap *, char *, int); void hashmap_put(HashMap *, char *, void *); void hashmap_put2(HashMap *, char *, int, void *); void hashmap_delete(HashMap *, char *); void hashmap_delete2(HashMap *, char *, int); void hashmap_test(void); // // chibicc.c // extern StringArray include_paths; extern bool opt_common; extern bool opt_data_sections; extern bool opt_fentry; extern bool opt_function_sections; extern bool opt_no_builtin; extern bool opt_nop_mcount; extern bool opt_pg; extern bool opt_pic; extern bool opt_popcnt; extern bool opt_record_mcount; extern bool opt_sse3; extern bool opt_sse4; extern bool opt_verbose; extern char *base_file; extern char **chibicc_tmpfiles; int chibicc(int, char **); void chibicc_cleanup(void); // // alloc.c // extern long alloc_node_count; extern long alloc_token_count; extern long alloc_obj_count; extern long alloc_type_count; Node *alloc_node(void); Token *alloc_token(void); Obj *alloc_obj(void); Type *alloc_type(void); // // dox1.c // void output_javadown(const char *, Obj *); void output_javadown_asm(const char *, const char *); // // dox2.c // void drop_dox(const StringArray *, const char *); // // as.c // extern long as_hashmap_hits; extern long as_hashmap_miss; void Assembler(int, char **); // // pybind.c // void output_bindings_python(const char *, Obj *, Token *); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_THIRD_PARTY_CHIBICC_CHIBICC_H_ */
15,152
672
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/dox1.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/mem/gc.h" #include "libc/sysv/consts/map.h" #include "libc/sysv/consts/o.h" #include "libc/sysv/consts/prot.h" #include "libc/x/xasprintf.h" #include "third_party/chibicc/chibicc.h" #include "tool/build/lib/asmdown.h" #define APPEND(L) L.p = realloc(L.p, ++L.n * sizeof(*L.p)) struct DoxWriter { struct Buffer { size_t n; char *p; } buf; struct Macros { size_t n; Macro **p; } macros; struct Objects { size_t n; Obj **p; } objects; }; static void SerializeData(struct Buffer *buf, const void *p, unsigned long n) { struct Slice *s; buf->p = realloc(buf->p, buf->n + n); memcpy(buf->p + buf->n, p, n); buf->n += n; } static int SerializeInt(struct Buffer *buf, int x) { unsigned char b[4]; b[0] = x >> 000; b[1] = x >> 010; b[2] = x >> 020; b[3] = x >> 030; SerializeData(buf, b, 4); return x; } static void SerializeStr(struct Buffer *buf, const char *s) { size_t n; if (!s) s = ""; n = strlen(s); n = MIN(INT_MAX, n); SerializeInt(buf, n); SerializeData(buf, s, n); } static void SerializeJavadown(struct Buffer *buf, struct Javadown *jd) { int i; SerializeInt(buf, jd->isfileoverview); SerializeStr(buf, jd->title); SerializeStr(buf, jd->text); SerializeInt(buf, jd->tags.n); for (i = 0; i < jd->tags.n; ++i) { SerializeStr(buf, jd->tags.p[i].tag); SerializeStr(buf, jd->tags.p[i].text); } } static char *DescribeScalar(struct Type *ty, char *name) { return xasprintf("%s%s%s%s", ty->is_atomic ? "_Atomic " : "", ty->is_const ? "const " : "", ty->is_unsigned ? "unsigned " : "", name); } static char *DescribeType(struct Type *ty) { switch (ty->kind) { case TY_VOID: return strdup("void"); case TY_BOOL: return strdup("_Bool"); case TY_CHAR: return DescribeScalar(ty, "char"); case TY_SHORT: return DescribeScalar(ty, "short"); case TY_INT: return DescribeScalar(ty, "int"); case TY_LONG: return DescribeScalar(ty, "long"); case TY_INT128: return DescribeScalar(ty, "__int128"); case TY_FLOAT: return DescribeScalar(ty, "float"); case TY_DOUBLE: return DescribeScalar(ty, "double"); case TY_LDOUBLE: return DescribeScalar(ty, "long double"); case TY_FUNC: return xasprintf("%s(*)()", _gc(DescribeType(ty->return_ty))); case TY_PTR: if (ty->base->kind == TY_FUNC) { return DescribeType(ty->base); } else { return xasprintf("%s*", _gc(DescribeType(ty->base))); } case TY_ARRAY: return xasprintf("%s[%d]", _gc(DescribeType(ty->base)), ty->array_len); case TY_ENUM: if (ty->name) { return xasprintf("enum %.*s", ty->name->len, ty->name->loc); } else { return strdup("ANONYMOUS-ENUM"); } case TY_STRUCT: if (ty->name) { return xasprintf("struct %.*s", ty->name->len, ty->name->loc); } else { return strdup("ANONYMOUS-STRUCT"); } case TY_UNION: if (ty->name) { return xasprintf("union %.*s", ty->name->len, ty->name->loc); } else { return strdup("ANONYMOUS-UNION"); } default: return strdup("UNKNOWN"); } } static int CountParams(Obj *params) { int n; for (n = 0; params; params = params->next) ++n; return n; } static int CountMacroParams(struct MacroParam *params) { int n; for (n = 0; params; params = params->next) ++n; return n; } static const char *GetFileName(Obj *obj) { if (obj->javadown && obj->javadown->file) return obj->javadown->file->name; if (obj->tok && obj->tok->file) return obj->tok->file->name; return "missingno.c"; } static int GetLine(Obj *obj) { if (obj->javadown && obj->javadown->file) return obj->javadown->line_no; if (obj->tok && obj->tok->file) return obj->tok->line_no; return 0; } static void SerializeDox(struct DoxWriter *dox, Obj *prog) { int i; char *s; Obj *param; MacroParam *mparam; SerializeInt(&dox->buf, dox->objects.n); for (i = 0; i < dox->objects.n; ++i) { s = DescribeType(dox->objects.p[i]->is_function ? dox->objects.p[i]->ty->return_ty : dox->objects.p[i]->ty); SerializeStr(&dox->buf, s); free(s); SerializeStr(&dox->buf, dox->objects.p[i]->name); SerializeStr(&dox->buf, GetFileName(dox->objects.p[i])); SerializeInt(&dox->buf, GetLine(dox->objects.p[i])); SerializeInt(&dox->buf, dox->objects.p[i]->is_function); SerializeInt(&dox->buf, dox->objects.p[i]->ty->is_variadic); SerializeInt(&dox->buf, dox->objects.p[i]->is_weak); SerializeInt(&dox->buf, dox->objects.p[i]->is_inline); SerializeInt(&dox->buf, dox->objects.p[i]->is_noreturn); SerializeInt(&dox->buf, dox->objects.p[i]->is_destructor); SerializeInt(&dox->buf, dox->objects.p[i]->is_constructor); SerializeInt(&dox->buf, dox->objects.p[i]->is_force_align_arg_pointer); SerializeInt(&dox->buf, dox->objects.p[i]->is_no_caller_saved_registers); SerializeStr(&dox->buf, dox->objects.p[i]->visibility); SerializeInt(&dox->buf, !!dox->objects.p[i]->javadown); if (dox->objects.p[i]->javadown) { SerializeJavadown(&dox->buf, dox->objects.p[i]->javadown->javadown); } SerializeInt(&dox->buf, CountParams(dox->objects.p[i]->params)); for (param = dox->objects.p[i]->params; param; param = param->next) { s = DescribeType(param->ty); SerializeStr(&dox->buf, s); free(s); SerializeStr(&dox->buf, param->name); } } SerializeInt(&dox->buf, dox->macros.n); for (i = 0; i < dox->macros.n; ++i) { SerializeStr(&dox->buf, dox->macros.p[i]->name); SerializeStr(&dox->buf, dox->macros.p[i]->javadown->file->name); SerializeInt(&dox->buf, dox->macros.p[i]->javadown->line_no); SerializeInt(&dox->buf, dox->macros.p[i]->is_objlike); SerializeStr(&dox->buf, dox->macros.p[i]->va_args_name); SerializeInt(&dox->buf, !!dox->macros.p[i]->javadown); if (dox->macros.p[i]->javadown) { SerializeJavadown(&dox->buf, dox->macros.p[i]->javadown->javadown); } SerializeInt(&dox->buf, CountMacroParams(dox->macros.p[i]->params)); for (mparam = dox->macros.p[i]->params; mparam; mparam = mparam->next) { SerializeStr(&dox->buf, mparam->name); } } SerializeInt(&dox->buf, 31337); } static int IsJavadownParam(struct JavadownTag *jt) { return !strcmp(jt->tag, "param") && strchr(jt->text, ' '); } static char *ExtractJavadownParamName(const char *text) { char *space; space = strchr(text, ' '); return strndup(text, space - text); } static int CountJavadownParams(struct Javadown *jd) { int i, n; for (n = i = 0; i < jd->tags.n; ++i) { if (IsJavadownParam(jd->tags.p + i)) { ++n; } } return n; } static void SerializeAsmdown(struct DoxWriter *dox, struct Asmdown *ad, const char *filename) { char *s; int i, j; SerializeInt(&dox->buf, ad->symbols.n); for (i = 0; i < ad->symbols.n; ++i) { SerializeStr(&dox->buf, ""); // type SerializeStr(&dox->buf, ad->symbols.p[i].name); SerializeStr(&dox->buf, filename); SerializeInt(&dox->buf, ad->symbols.p[i].line); SerializeInt(&dox->buf, true); // TODO: is_function SerializeInt(&dox->buf, false); // is_variadic SerializeInt(&dox->buf, false); // TODO: is_weak SerializeInt(&dox->buf, false); // is_inline SerializeInt(&dox->buf, false); // is_noreturn SerializeInt(&dox->buf, false); // is_destructor SerializeInt(&dox->buf, false); // is_constructor SerializeInt(&dox->buf, false); // is_force_align_arg_pointer SerializeInt(&dox->buf, false); // is_no_caller_saved_registers SerializeStr(&dox->buf, ""); // TODO: visibility SerializeInt(&dox->buf, true); // has_javadown SerializeJavadown(&dox->buf, ad->symbols.p[i].javadown); SerializeInt(&dox->buf, CountJavadownParams(ad->symbols.p[i].javadown)); for (j = 0; j < ad->symbols.p[i].javadown->tags.n; ++j) { if (IsJavadownParam(ad->symbols.p[i].javadown->tags.p + j)) { SerializeStr(&dox->buf, ""); // type s = ExtractJavadownParamName(ad->symbols.p[i].javadown->tags.p[j].text); SerializeStr(&dox->buf, s); // name free(s); } } } SerializeInt(&dox->buf, 0); // macros SerializeInt(&dox->buf, 31337); } static void LoadPublicDefinitions(struct DoxWriter *dox, Obj *prog) { int i; Obj *obj; Macro *macro; for (obj = prog; obj; obj = obj->next) { if (!obj->javadown) { if (*obj->name == '_') continue; if (strchr(obj->name, '$')) continue; if (obj->visibility && !strcmp(obj->visibility, "hidden")) continue; if (_startswith(obj->name, "nsync_") && _endswith(obj->name, "_")) continue; if (!obj->is_definition && (!obj->is_function || !obj->params || !obj->params->name || !*obj->params->name)) { continue; } } if (_startswith(obj->name, "__gdtoa_")) continue; if (_startswith(obj->name, "sys_")) continue; if (_startswith(obj->name, "ioctl_")) continue; if (_startswith(obj->name, "nsync_mu_semaphore_")) continue; if (_startswith(obj->name, "Describe")) continue; if (_startswith(obj->name, "__sig")) continue; if (_startswith(obj->name, "__zipos")) continue; if (obj->is_static) continue; if (obj->is_string_literal) continue; if (obj->section && _startswith(obj->section, ".init_array")) continue; APPEND(dox->objects); dox->objects.p[dox->objects.n - 1] = obj; } for (i = 0; i < macros.capacity; ++i) { if (!macros.buckets[i].key) continue; if (macros.buckets[i].key == (char *)-1) continue; macro = macros.buckets[i].val; if (!macro->javadown) continue; if (!macro->javadown->javadown) continue; /* if (*macro->name == '_') continue; */ /* if (strchr(macro->name, '$')) continue; */ APPEND(dox->macros); dox->macros.p[dox->macros.n - 1] = macro; } } static struct DoxWriter *NewDoxWriter(void) { return calloc(1, sizeof(struct DoxWriter)); } static void FreeDoxWriter(struct DoxWriter *dox) { if (dox) { free(dox->buf.p); free(dox->macros.p); free(dox->objects.p); free(dox); } } static void WriteDox(struct DoxWriter *dox, const char *path) { int fd; CHECK_NE(-1, (fd = creat(path, 0644))); CHECK_EQ(dox->buf.n, write(fd, dox->buf.p, dox->buf.n)); close(fd); } /** * Emits documentation data for compilation unit just parsed. */ void output_javadown(const char *path, Obj *prog) { struct DoxWriter *dox = NewDoxWriter(); LoadPublicDefinitions(dox, prog); SerializeDox(dox, prog); WriteDox(dox, path); FreeDoxWriter(dox); } /** * Emits documentation data for assembly source file. */ void output_javadown_asm(const char *path, const char *source) { int fd; void *map; struct stat st; struct Asmdown *ad; struct DoxWriter *dox; CHECK_NE(-1, (fd = open(source, O_RDONLY))); CHECK_NE(-1, fstat(fd, &st)); if (st.st_size) { CHECK_NE(MAP_FAILED, (map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0))); ad = ParseAsmdown(map, st.st_size); munmap(map, st.st_size); } else { ad = ParseAsmdown("", 0); } close(fd); dox = NewDoxWriter(); SerializeAsmdown(dox, ad, source); WriteDox(dox, path); FreeDoxWriter(dox); FreeAsmdown(ad); }
13,280
378
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/help.txt
SYNOPSIS chibicc.com [FLAGS] INPUTS DESCRIPTION chibicc - A Small GNU-Style ISO/IEC 9899:2011 Standard Compiler OVERVIEW chibicc is the simplest/tiniest/hackable/readable c11 compiler in the world that can compile code quickly and consistently across platforms FLAGS -o PATH Specifies path of output. -c Objectify the source file, but do not link. -S Compile the source file, but do not objectify. -E Preprocess the source file, but do not compile. Output defaults to stdout. -D TOKEN[=VALUE] Defines preprocessor token. -U TOKEN Undefines preprocessor token. -include DIR Add include. -I DIR -iquote DIR -isystem DIR Adds include directory. -x c (default for .c files) -x assembler (default for .s files) -x assembler-with-cpp (default for .S files) Explicitly specifies programming language. -Wa arg1[,arg2...] -Xassembler arg1[,arg2...] Appends opaque arguments passed along to assembler. -Wl arg1[,arg2...] -Xlinker arg1[,arg2...] Appends opaque arguments passed along to linker. -v Enables verbose mode. Lines with subprocess commands start with a space. -### Implies -v and enables shell command argument quoting. --version Shows compiler version. --help Shows this information. CODEGEN -pg -mfentry -mnop-mcount -mrecord-mcount Controls output of profiling hooks in function prologues. -fdata-sections -ffunction-sections Controls granularity of code visible to the linker. -msse3 -msse4 -msse4.1 -msse4.2 -mpopcnt Specifies microarchitectural features. Default is K8. -fpie -fpic -fPIC Controls output of position independent code. -fcommon -fno-common Controls usage of traditional STT_COMMON objects. MAKEFILE -M Generate makefile header dependency code. Output defaults to stdout. -MD Generate makefile header dependency code, and compile. Output defaults to output path with .d extension. -MMD Generate makefile header dependency code, and compile. Default include roots are excluded as dependencies. Output defaults to output path with .d extension. -MF PATH Specifies output path of header dependency code. -MT NAME Specifies name of target in generated makefile code. -MQ NAME Specifies name of target in makefile code w/ quoting. INTERNALS -P Generate Python bindings. -A Print abstract syntax tree. -J Generate HTML documentation for public APIs based on Javadoc comments containing Markdown. INTEGRAL TYPES _Bool char short int long long long __int128 signed char unsigned char unsigned short unsigned int unsigned long unsigned long long unsigned __int128 FLOATING POINT TYPES float double long double BUILTIN FUNCTIONS T __builtin_expect(T, int) unsigned long __builtin_offsetof(typename, token) int __builtin_reg_class(typename) num __builtin_constant_p(expr) int __builtin_unreachable() void * __builtin_frame_address(int) _Bool __builtin_types_compatible_p(typename, typename) T __builtin_atomic_exchange(T *addr, T neu) T * __builtin_assume_aligned(T *addr) _Bool __builtin_add_overflow(T, T, T *) _Bool __builtin_sub_overflow(T, T, T *) _Bool __builtin_mul_overflow(T, T, T *) _Bool __builtin_neg_overflow(T, T, T *) void * __builtin_alloca(unsigned long) void __builtin_trap() int __builtin_clz(int) int __builtin_clzl(long) int __builtin_clzll(long long) int __builtin_ctz(int) int __builtin_ctzl(long) int __builtin_ctzll(long long) int __builtin_ffs(int) int __builtin_ffsl(long) int __builtin_ffsll(long long) int __builtin_popcount(unsigned int) long __builtin_popcountl(unsigned long) long __builtin_popcountll(unsigned long) unsigned long __builtin_strlen(char *) char * __builtin_strstr(char *, char *) char * __builtin_strchr(char *, int) void * __builtin_memcpy(void *, void *, unsigned long) char * __builtin_strpbrk(char *, char *) unsigned short __builtin_bswap16(unsigned short) unsigned int __builtin_bswap32(unsigned int) unsigned long __builtin_bswap64(unsigned long) int __builtin_isnan(flonum) int __builtin_isinf(flonum) int __builtin_isfinite(flonum) int __builtin_fpclassify(flonum) int __builtin_isless(flonum, flonum) int __builtin_isgreater(flonum, flonum) int __builtin_isunordered(flonum, flonum) int __builtin_islessequal(flonum, flonum) int __builtin_islessgreater(flonum, flonum) int __builtin_isgreaterequal(flonum, flonum) double __builtin_nan(char *) float __builtin_nanf(char *) long double __builtin_nanl(char *) long __builtin_signbit(double) int __builtin_signbitf(float) int __builtin_signbitl(long double) double __builtin_huge_val() float __builtin_huge_valf() long double __builtin_huge_vall() double __builtin_fabs(double) float __builtin_fabsf(float) long double __builtin_fabsl(long double) double __builtin_logb(double) float __builtin_logbf(float) long double __builtin_logbl(long double) double __builtin_fmax(double, double) float __builtin_fmaxf(float, float) long double __builtin_fmaxl(long double, long double) double __builtin_fmin(double, double) float __builtin_fminf(float, float) long double __builtin_fminl(long double, long double) double __builtin_copysign(double, double) float __builtin_copysignf(float, float) long double __builtin_copysignl(long double, long double) void __builtin_ia32_movntq(di *, di) int __builtin_ia32_pmovmskb128(v16qi) T __atomic_load_n(T *addr, int memorder) void __atomic_load(T *addr, T *res, int memorder) void __atomic_store_n(T *addr, T val, int memorder) void __atomic_store(T *addr, T *val, int memorder) void __atomic_clear(_Bool *addr, int memorder) _Bool __atomic_test_and_set(void *addr, int memorder) T __atomic_fetch_add(T *addr, T amt, int memorder) T __atomic_fetch_sub(T *addr, T amt, int memorder) T __atomic_fetch_xor(T *addr, T amt, int memorder) T __atomic_fetch_and(T *addr, T amt, int memorder) T __atomic_fetch_or(T *addr, T amt, int memorder) _Bool __atomic_compare_exchange_n(T *addr, T *old, T neu, int weak, int goodorder, int failorder) T __sync_lock_test_and_set(T *addr, T value, ...) void __sync_lock_release(T *addr, ...) BUILTIN OBJECTS __func__ __FUNCTION__ __va_area__ __alloca_size__ BUILTIN MACROS __FILE__ __LINE__ __DATE__ __TIME__ __COUNTER__ __TIMESTAMP__ __BASE_FILE__ __chibicc__ __cosmopolitan__ __GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__ __NO_INLINE__ __GNUC_STDC_INLINE__ __BIGGEST_ALIGNMENT__ __C99_MACRO_WITH_VA_ARGS __GCC_ASM_FLAG_OUTPUTS__ __ELF__ __LP64__ _LP64 __STDC__ __STDC_HOSTED__ __STDC_NO_COMPLEX__ __STDC_UTF_16__ __STDC_UTF_32__ __STDC_VERSION__ __USER_LABEL_PREFIX__ __alignof__ __const__ __inline__ __signed__ __typeof__ __volatile__ __unix __unix__ __linux __linux__ __gnu_linux__ __BYTE_ORDER__ __FLOAT_WORD_ORDER__ __ORDER_BIG_ENDIAN__ __ORDER_LITTLE_ENDIAN__ __INT8_MAX__ __UINT8_MAX__ __INT16_MAX__ __UINT16_MAX__ __SHRT_MAX__ __INT_MAX__ __INT32_MAX__ __UINT32_MAX__ __INT64_MAX__ __LONG_MAX__ __LONG_LONG_MAX__ __UINT64_MAX__ __SIZE_MAX__ __INTPTR_MAX__ __UINTPTR_MAX__ __WINT_MAX__ __CHAR_BIT__ __SIZEOF_SHORT__ __SIZEOF_INT__ __SIZEOF_LONG__ __SIZEOF_LONG_LONG__ __SIZEOF_POINTER__ __SIZEOF_PTRDIFF_T__ __SIZEOF_SIZE_T__ __SIZEOF_WCHAR_T__ __SIZEOF_WINT_T__ __SIZEOF_FLOAT__ __SIZEOF_FLOAT128__ __SIZEOF_DOUBLE__ __SIZEOF_FLOAT80__ __SIZEOF_LONG_DOUBLE__ __INT8_TYPE__ __UINT8_TYPE__ __INT16_TYPE__ __UINT16_TYPE__ __INT32_TYPE__ __UINT32_TYPE__ __INT64_TYPE__ __UINT64_TYPE__ __INTPTR_TYPE__ __UINTPTR_TYPE__ __PTRDIFF_TYPE__ __SIZE_TYPE__ __WCHAR_TYPE__ __CHAR16_TYPE__ __CHAR32_TYPE__ __WINT_TYPE__ __CHAR16_TYPE__ __WCHAR_TYPE__ __CHAR32_TYPE__ __INT_LEAST8_TYPE__ __UINT_LEAST8_TYPE__ __INT_LEAST16_TYPE__ __UINT_LEAST16_TYPE__ __INT_LEAST32_TYPE__ __UINT_LEAST32_TYPE__ __INT_LEAST64_TYPE__ __UINT_LEAST64_TYPE__ __INT_FAST8_TYPE__ __UINT_FAST8_TYPE__ __INT_FAST16_TYPE__ __UINT_FAST16_TYPE__ __INT_FAST32_TYPE__ __UINT_FAST32_TYPE__ __INT_FAST64_TYPE__ __UINT_FAST64_TYPE__ __DBL_DECIMAL_DIG__ __DBL_DENORM_MIN__ __DBL_DIG__ __DBL_EPSILON__ __DBL_HAS_DENORM__ __DBL_HAS_INFINITY__ __DBL_HAS_QUIET_NAN__ __DBL_MANT_DIG__ __DBL_MAX_10_EXP__ __DBL_MAX_EXP__ __DBL_MAX__ __DBL_MIN_10_EXP__ __DBL_MIN_EXP__ __DBL_MIN__ __FLT_DECIMAL_DIG__ __FLT_DENORM_MIN__ __FLT_DIG__ __FLT_EPSILON__ __FLT_EVAL_METHOD_TS_18661_3__ __FLT_EVAL_METHOD__ __FLT_HAS_DENORM__ __FLT_HAS_INFINITY__ __FLT_HAS_QUIET_NAN__ __FLT_MANT_DIG__ __FLT_MAX_10_EXP__ __FLT_MAX_EXP__ __FLT_MAX__ __FLT_MIN_10_EXP__ __FLT_MIN_EXP__ __FLT_MIN__ __FLT_RADIX__ __LDBL_DECIMAL_DIG__ __LDBL_DENORM_MIN__ __LDBL_DIG__ __LDBL_EPSILON__ __LDBL_HAS_DENORM__ __LDBL_HAS_INFINITY__ __LDBL_HAS_QUIET_NAN__ __LDBL_MANT_DIG__ __LDBL_MAX_10_EXP__ __LDBL_MAX_EXP__ __LDBL_MAX__ __LDBL_MIN_10_EXP__ __LDBL_MIN_EXP__ __LDBL_MIN__ __x86_64 __x86_64__ __amd64 __amd64__ __MMX__ __SSE__ __SSE_MATH__ __SSE2__ __SSE2_MATH__ __SSE3__ [conditional] __SSE4__ [conditional] __POPCNT__ [conditional] __PG__ [conditional] __PIC__ [conditional] __MFENTRY__ [conditional] ASSEMBLER That process is normally an implementation detail of your compiler, which can embed this program or launch it as a subprocess. Much GNU style syntax is supported. Your code that gets embedded in an asm() statement will ultimately end up here. This implementation, has the advantage of behaving the same across platforms, in a simple single file implementation that compiles down to a 100kilo ape executable. Your assembler supports the following flags: -o FILE output path [default: a.out] -I DIR append include path [default: .] -W inhibit .warning -Z inhibit .error and .err Your assembler supports the following directives: .zero N emits 0's .byte INT... emits int8 .word INT... emits int16 .long INT... emits int32 .quad INT... emits int64 .octa INT... emits int128 .ascii STR... emits string .asciz STR... emits string and 0 byte .ident STR emits string to .comment section .float NUMBER... emits binary32 .double NUMBER... emits binary64 .float80 NUMBER... emits x86 float (10 bytes) .ldbl NUMBER... emits x86 float (16 bytes) .zleb128 NUMBER... emits LEB-128 zigzag varint .sleb128 NUMBER... emits LEB-128 signed varint .uleb128 NUMBER... emits LEB-128 unsigned varint .align BYTES [FILL [MAXSKIP]] emits fill bytes to boundary .end halts tokenization .abort crashes assembler .err aborts (ignorable w/ -Z) .error STR aborts (ignorable w/ -Z) .warning STR whines (ignorable w/ -W) .text enters text section (default) .data enters data section .bss enters bss section .section NAME [SFLG SHT] enters section .previous enters previous section .pushsection NAME [SFLG SHT] pushes section .popsection pops section .type SYM TYPE sets type of symbol .size SYM EXPR sets size of symbol .internal SYM... marks symbol STV_INTERNAL .hidden SYM... marks symbol STV_HIDDEN .protected SYM... marks symbol STV_PROTECTED .globl SYM... marks symbol STB_GLOBAL .local SYM... marks symbol STB_LOCAL .weak SYM... marks symbol STB_WEAK .include FILE assembles file source .incbin FILE emits file content .file FILENO PATH dwarf file define .loc FILENO LINENO dwarf source line TYPE can be one of the following: - @notype STT_NOTYPE (default) - @object STT_OBJECT - @function STT_FUNC - @common STT_COMMON - @tls_object STT_TLS SHT can be one of the following: - @progbits SHT_PROGBITS - @note SHT_NOTE - @nobits SHT_NOBITS - @preinit_array SHT_PREINIT_ARRAY - @init_array SHT_INIT_ARRAY - @fini_array SHT_FINI_ARRAY SFLG is a string which may have the following characters: - a SHF_ALLOC - w SHF_WRITE - x SHF_EXECINSTR - g SHF_GROUP - M SHF_MERGE - S SHF_STRINGS - T SHF_TLS PREFIXES addr32 cs data16 ds es fs gs lock rep repe repne repnz repz rex rex.b rex.r rex.rb rex.rx rex.rxb rex.w rex.wb rex.wr rex.wrb rex.wrx rex.wrxb rex.wx rex.wxb rex.x rex.xb ss REGISTERS 64-bit 32-bit 16-bit lo byte hi byte │ sse mmx │ fpu ─────── ─────── ─────── ─────── ─────── │ ─────── ─────── │ ─────── %rax %eax %ax %al %ah │ %xmm0 %mm0 │ %st(0) %rcx %ecx %cx %cl %ch │ %xmm1 %mm1 │ %st(1) %rdx %edx %dx %dl %dh │ %xmm2 %mm2 │ %st(2) %rbx %ebx %bx %bl %bh │ %xmm3 %mm3 │ %st(3) %rsp %esp %sp %spl │ %xmm4 %mm4 │ %st(4) %rbp %ebp %bp %bpl │ %xmm5 %mm5 │ %st(5) %rsi %esi %si %sil │ %xmm6 %mm6 │ %st(6) %rdi %edi %di %dil │ %xmm7 %mm7 │ %st(7) %r8 %r8d %r8w %r8b │ %xmm8 %r9 %r9d %r9w %r9b │ %xmm9 %r10 %r10d %r10w %r10b │ %xmm10 %r11 %r11d %r11w %r11b │ %xmm11 %r12 %r12d %r12w %r12b │ %xmm12 %r13 %r13d %r13w %r13b │ %xmm13 %r14 %r14d %r14w %r14b │ %xmm14 %r15 %r15d %r15w %r15b │ %xmm15 %riz %eiz RICHARD STALLMAN MATH55 ASM() NOTATION BEHAVIOR =: write-only +: read/writeable SELECTION Autonomous a: ax/eax/rax b: bx/ebx/rbx c: bx/ebx/rbx d: dx/edx/rdx S: si/esi/rsi D: di/edi/rdi Algorithmic r: pick one of a,b,c,d,D,S,bp,sp,r8-15 registers, referenced as %0,etc. l: pick one of a,b,c,d,D,S,bp,r8-15 for indexing, referenced as %0,etc. q: pick one of a,b,c,d,r8-r15 for lo-byte access, e.g. %b0,%w0,%k0,etc. Q: pick one of a,b,c,d for hi-byte access, e.g. %h0,etc. U: pick one of a,c,d,D,S,r8-11 (call-clobbered) R: pick one of a,b,c,d,D,S,bp,sp (all models) y: pick mmx register x: pick sse register X: pick anything m: memory o: memory offsetable by an immediate, referenced as %0,2+%0,etc. p: memory, intended for load/push address and segments (movl %@:%p1, %0) g: probably shorthand for "rmi" combo X: allow anything Combos rm: pick register or memory address (converting immediates) rmi: pick register or memory address (allowing immediates) Immediates i: integer literal or compiler/assembler constexpr or linker embedding e: i∊[-2^31,2^31) for sign-extending immediates Z: i∊[0,2^32) for zero-extending immediates I: i∊[0,31] (5 bits for 32-bit shifts) J: i∊[0,63] (6 bits for 64-bit shifts) K: i∊[-128,127] L: permit uncasted char/short literal as zero-extended operand to andl? M: i∊[0,3] (intended for index scaling, e.g. "mov\t(?,?,1<<%0),?") N: i∊[0,255] (for in & out) M: 2-bit integer constant (shifts for index scaling) I: 5-bit integer constant (for 32-bit shifts) J: 6-bit integer constant (for 64-bit shifts) Transcendentals f: any stack slot t: top of stack (%st(0)) and possibly converts xmm to ymm u: second top of stack (%st(1)) Specials %= generates number unique to each instance %%REG explicitly-supplied register (used w/ clobbers) AUGMENTATION %pN print raw %PN print w/ @plt %aN print address %zN print only opcode suffix for operand type %lN print label without punctuation, e.g. jumps %cN print immediate w/o punctuation, e.g. lea %c0(%1),%2 %bN print lo-byte form, e.g. xchgb %b0,%%al (QImode 8-bit) %hN print hi-byte form, e.g. xchgb %h0,%%ah (QImode 8-bit) %wN print word form, e.g. xchgw %w0,%%ax (HImode 16-bit) %kN print dword form, e.g. xchgl %k0,%%eax (SImode 32-bit) %qN print qword form, e.g. xchgq %q0,%%rax (DImode 64-bit) %HN access high 8 bytes of SSE register, or +8 displacement %nN negated literal, e.g. lea %n0(%1),%2 %VN print register name without %, e.g. call foo%V0 EXAMPLE static inline void MixAudio(short a[static 8], const short b[static 8]) { asm("paddsw\t%1,%0" : "+x"(a) : "x"(b) : "memory"); }
19,190
667
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/alloc.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/runtime/runtime.h" #include "libc/stdio/stdio.h" #include "third_party/chibicc/chibicc.h" long alloc_node_count; long alloc_token_count; long alloc_obj_count; long alloc_type_count; wontreturn void __oom_hook(size_t request) { fprintf(stderr, "error: chibicc ran out of memory\n"); exit(1); } static void *alloc(size_t n, long *c) { void *r; if ((r = calloc(1, n))) { ++*c; return r; } else { __oom_hook(n); } } Node *alloc_node(void) { return alloc(sizeof(Node), &alloc_node_count); } Token *alloc_token(void) { return alloc(sizeof(Token), &alloc_token_count); } Obj *alloc_obj(void) { return alloc(sizeof(Obj), &alloc_obj_count); } Type *alloc_type(void) { return alloc(sizeof(Type), &alloc_type_count); }
2,598
58
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/dox2.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/intrin/bits.h" #include "libc/mem/alg.h" #include "libc/sysv/consts/map.h" #include "libc/sysv/consts/o.h" #include "libc/sysv/consts/prot.h" #include "libc/x/xasprintf.h" #include "third_party/chibicc/chibicc.h" #define APPEND(L) L.p = realloc(L.p, ++L.n * sizeof(*L.p)) struct Dox { unsigned char *p; struct Freelist { size_t n; void **p; } freelist; struct Set { size_t n; struct SetEntry { unsigned h; char *s; } * p; } names; struct DoxObjects { size_t n; struct DoxObject { bool ignore; char *type; char *name; char *path; int line; bool is_function; bool is_variadic; bool is_weak; bool is_inline; bool is_noreturn; bool is_destructor; bool is_constructor; bool is_force_align_arg_pointer; bool is_no_caller_saved_registers; char *visibility; struct Javadown *javadown; struct DoxObjectParams { size_t n; struct DoxObjectParam { char *type; char *name; } * p; } params; } * p; } objects; struct DoxMacros { size_t n; struct DoxMacro { bool ignore; char *name; char *path; int line; bool is_objlike; char *va_args_name; struct Javadown *javadown; struct DoxMacroParams { size_t n; struct DoxMacroParam { char *name; } * p; } params; } * p; } macros; struct DoxIndex { size_t n; struct DoxIndexEntry { enum DoxIndexType { kObject, kMacro, } t; int i; } * p; } index; }; static unsigned Hash(const void *p, unsigned long n) { unsigned h, i; for (h = i = 0; i < n; i++) { h += ((unsigned char *)p)[i]; h *= 0x9e3779b1; } return MAX(1, h); } static struct Dox *NewDox(void) { return calloc(1, sizeof(struct Dox)); } static void FreeDox(struct Dox *dox) { int i; if (dox) { for (i = 0; i < dox->freelist.n; ++i) { free(dox->freelist.p[i]); } free(dox->names.p); free(dox->freelist.p); free(dox->objects.p); free(dox->macros.p); free(dox->index.p); free(dox); } } static void *FreeLater(struct Dox *dox, void *p) { APPEND(dox->freelist); dox->freelist.p[dox->freelist.n - 1] = p; return p; } static int DeserializeInt(struct Dox *dox) { int x; x = (unsigned)dox->p[0] << 000 | (unsigned)dox->p[1] << 010 | (unsigned)dox->p[2] << 020 | (unsigned)dox->p[3] << 030; dox->p += 4; return x; } static char *DeserializeStr(struct Dox *dox) { char *s; size_t n; n = DeserializeInt(dox); s = malloc(n + 1); memcpy(s, dox->p, n); s[n] = '\0'; dox->p += n; return FreeLater(dox, s); } static struct Javadown *DeserializeJavadown(struct Dox *dox) { int i; bool present; struct Javadown *jd; if (DeserializeInt(dox)) { jd = FreeLater(dox, calloc(1, sizeof(struct Javadown))); jd->isfileoverview = DeserializeInt(dox); jd->title = DeserializeStr(dox); jd->text = DeserializeStr(dox); jd->tags.n = DeserializeInt(dox); jd->tags.p = FreeLater(dox, malloc(jd->tags.n * sizeof(*jd->tags.p))); for (i = 0; i < jd->tags.n; ++i) { jd->tags.p[i].tag = DeserializeStr(dox); jd->tags.p[i].text = DeserializeStr(dox); } return jd; } else { return NULL; } } static void DeserializeObject(struct Dox *dox, struct DoxObject *o) { int i; o->ignore = false; o->type = DeserializeStr(dox); o->name = DeserializeStr(dox); o->path = DeserializeStr(dox); o->line = DeserializeInt(dox); o->is_function = DeserializeInt(dox); o->is_variadic = DeserializeInt(dox); o->is_weak = DeserializeInt(dox); o->is_inline = DeserializeInt(dox); o->is_noreturn = DeserializeInt(dox); o->is_destructor = DeserializeInt(dox); o->is_constructor = DeserializeInt(dox); o->is_force_align_arg_pointer = DeserializeInt(dox); o->is_no_caller_saved_registers = DeserializeInt(dox); o->visibility = DeserializeStr(dox); o->javadown = DeserializeJavadown(dox); o->params.n = DeserializeInt(dox); o->params.p = FreeLater(dox, malloc(o->params.n * sizeof(*o->params.p))); for (i = 0; i < o->params.n; ++i) { o->params.p[i].type = DeserializeStr(dox); o->params.p[i].name = DeserializeStr(dox); } } static void DeserializeMacro(struct Dox *dox, struct DoxMacro *m) { int i; m->ignore = false; m->name = DeserializeStr(dox); m->path = DeserializeStr(dox); m->line = DeserializeInt(dox); m->is_objlike = DeserializeInt(dox); m->va_args_name = DeserializeStr(dox); m->javadown = DeserializeJavadown(dox); m->params.n = DeserializeInt(dox); m->params.p = FreeLater(dox, malloc(m->params.n * sizeof(*m->params.p))); for (i = 0; i < m->params.n; ++i) { m->params.p[i].name = DeserializeStr(dox); } } static void DeserializeDox(struct Dox *dox, const char *path) { int i, j, n; i = dox->objects.n; n = DeserializeInt(dox); dox->objects.p = realloc(dox->objects.p, (dox->objects.n + n) * sizeof(*dox->objects.p)); for (j = 0; j < n; ++j) { DeserializeObject(dox, dox->objects.p + i + j); } i = dox->macros.n; dox->objects.n += n; n = DeserializeInt(dox); dox->macros.p = realloc(dox->macros.p, (dox->macros.n + n) * sizeof(*dox->macros.p)); for (j = 0; j < n; ++j) { DeserializeMacro(dox, dox->macros.p + i + j); } dox->macros.n += n; CHECK_EQ(31337, DeserializeInt(dox)); } static void ReadDox(struct Dox *dox, const StringArray *files) { int i, fd; void *map; struct stat st; for (i = 0; i < files->len; ++i) { CHECK_NE(-1, (fd = open(files->data[i], O_RDONLY))); CHECK_NE(-1, fstat(fd, &st)); if (st.st_size) { CHECK_NE(MAP_FAILED, (map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0))); dox->p = map; DeserializeDox(dox, files->data[i]); munmap(map, st.st_size); } close(fd); } } static bool AddSet(struct Set *set, char *s) { unsigned i, h, k; h = Hash(s, strlen(s)); k = 0; for (k = 0;; ++k) { i = (h + k + ((k + 1) >> 1)) & (set->n - 1); if (!set->p[i].h) { set->p[i].h = h; set->p[i].s = s; return true; } if (h == set->p[i].h && !strcmp(s, set->p[i].s)) { return false; } } } static int CompareDoxIndexEntry(const void *p1, const void *p2, void *arg) { struct Dox *dox; const char *s1, *s2; struct DoxIndexEntry *a, *b; dox = arg, a = p1, b = p2; s1 = a->t == kObject ? dox->objects.p[a->i].name : dox->macros.p[a->i].name; s2 = b->t == kObject ? dox->objects.p[b->i].name : dox->macros.p[b->i].name; while (*s1 == '_') ++s1; while (*s2 == '_') ++s2; return strcasecmp(s1, s2); } static void IndexDox(struct Dox *dox) { size_t i, j, n; dox->names.n = _roundup2pow(dox->objects.n + dox->macros.n) << 1; dox->names.p = calloc(dox->names.n, sizeof(*dox->names.p)); n = 0; for (i = 0; i < dox->objects.n; ++i) { if (AddSet(&dox->names, dox->objects.p[i].name)) { ++n; } else { dox->objects.p[i].ignore = true; } } for (i = 0; i < dox->macros.n; ++i) { if (AddSet(&dox->names, dox->macros.p[i].name)) { ++n; } else { dox->macros.p[i].ignore = true; } } dox->index.n = n; dox->index.p = malloc(n * sizeof(*dox->index.p)); j = 0; for (i = 0; i < dox->objects.n; ++i) { if (dox->objects.p[i].ignore) continue; dox->index.p[j].t = kObject; dox->index.p[j].i = i; ++j; } for (i = 0; i < dox->macros.n; ++i) { if (dox->macros.p[i].ignore) continue; dox->index.p[j].t = kMacro; dox->index.p[j].i = i; ++j; } CHECK_EQ(n, j); qsort_r(dox->index.p, dox->index.n, sizeof(*dox->index.p), CompareDoxIndexEntry, dox); } /** * Escapes HTML entities and interprets basic Markdown syntax. */ static void PrintText(FILE *f, const char *s) { int c; bool bol, pre, ul0, ul2, ol0, ol2, bt1, bt2; for (bt1 = bt2 = ul2 = ul0 = ol2 = ol0 = pre = false, bol = true;;) { switch ((c = *s++)) { case '\0': if (bt1 || bt2) fprintf(f, "</code>"); if (pre) fprintf(f, "</pre>"); if (ul0 || ul2) fprintf(f, "</ul>"); if (ol0 || ol2) fprintf(f, "</ol>"); return; case '&': fprintf(f, "&amp;"); bol = false; break; case '<': fprintf(f, "&lt;"); bol = false; break; case '>': fprintf(f, "&gt;"); bol = false; break; case '"': fprintf(f, "&quot;"); bol = false; break; case '\'': fprintf(f, "&#39;"); bol = false; break; case '`': if (!pre && !bt1 && !bt2 && *s != '`') { fprintf(f, "<code>"); bt1 = true; } else if (!pre && !bt1 && !bt2 && *s == '`') { fprintf(f, "<code>"); bt2 = true; ++s; } else if (bt1) { fprintf(f, "</code>"); bt1 = false; } else if (bt2 && *s == '`') { fprintf(f, "</code>"); bt2 = false; ++s; } else { fprintf(f, "`"); } bol = false; break; case '\n': if (!pre && !ul0 && !ul2 && !ol0 && !ol2 && *s == '\n') { fprintf(f, "\n<p>"); bol = true; } else if (pre && s[0] != '\n') { if (s[0] != ' ' || s[1] != ' ' || s[2] != ' ' || s[3] != ' ') { fprintf(f, "</pre>\n"); pre = false; bol = true; } else { fprintf(f, "\n"); bol = false; s += 4; } } else if (ul0 && s[0] == '-' && s[1] == ' ') { fprintf(f, "\n<li>"); s += 2; bol = false; } else if (ul2 && s[0] == ' ' && s[1] == ' ' && s[2] == '-' && s[3] == ' ') { fprintf(f, "\n<li>"); s += 4; bol = false; } else if (ul0 && s[0] != '\n' && (s[0] != ' ' || s[1] != ' ')) { fprintf(f, "\n</ul>\n"); bol = true; ul0 = false; } else if (ul2 && s[0] != '\n' && (s[0] != ' ' || s[1] != ' ' || s[2] != ' ' || s[3] != ' ')) { fprintf(f, "\n</ul>\n"); bol = true; ul2 = false; } else if (ol0 && ('0' <= s[0] && s[0] <= '9') && s[1] == '.' && s[2] == ' ') { fprintf(f, "\n<li>"); s += 3; bol = false; } else if (ol2 && s[0] == ' ' && s[1] == ' ' && ('0' <= s[2] && s[2] <= '9') && s[3] == '.' && s[3] == ' ') { fprintf(f, "\n<li>"); s += 5; bol = false; } else if (ol0 && s[0] != '\n' && (s[0] != ' ' || s[1] != ' ')) { fprintf(f, "\n</ol>\n"); bol = true; ol0 = false; } else if (ol2 && s[0] != '\n' && (s[0] != ' ' || s[1] != ' ' || s[2] != ' ' || s[3] != ' ')) { fprintf(f, "\n</ol>\n"); bol = true; ol2 = false; } else { fprintf(f, "\n"); bol = true; } break; case '-': if (bol && !ul0 && !ul2 && !ol0 && !ol2 && s[0] == ' ') { ul0 = true; fprintf(f, "<ul><li>"); ++s; } else { fprintf(f, "-"); } bol = false; break; case '1': if (bol && !ol0 && !ol2 && !ul0 && !ul2 && s[0] == '.' && s[1] == ' ') { ol0 = true; fprintf(f, "<ol><li>"); s += 2; } else { fprintf(f, "1"); } bol = false; break; case ' ': if (bol && !pre && s[0] == ' ' && s[1] == ' ' && s[2] == ' ') { pre = true; fprintf(f, "<pre>"); s += 3; } else if (bol && !ul0 && !ul2 && !ol0 && !ol2 && s[0] == ' ' && s[1] == '-' && s[2] == ' ') { ul2 = true; fprintf(f, "<ul><li>"); s += 3; } else if (bol && !ul0 && !ul2 && !ol0 && !ol2 && s[0] == ' ' && ('0' <= s[1] && s[1] <= '9') && s[2] == '.' && s[3] == ' ') { ol2 = true; fprintf(f, "<ol><li>"); s += 4; } else { fprintf(f, " "); } bol = false; break; default: fprintf(f, "%c", c); bol = false; break; } } } static bool HasTag(struct Javadown *jd, const char *tag) { int k; if (jd) { for (k = 0; k < jd->tags.n; ++k) { if (!strcmp(jd->tags.p[k].tag, tag)) { return true; } } } return false; } static bool IsNoReturn(struct DoxObject *o) { return o->is_noreturn || HasTag(o->javadown, "noreturn"); } static void PrintDox(struct Dox *dox, FILE *f) { int i, j, k; char *prefix; bool was_outputted; struct DoxMacro *m; struct DoxObject *o; // header fprintf(f, "\ <!doctype html>\n\ <html lang=\"en\">\n\ <meta charset=\"utf-8\">\n\ <title>Cosmopolitan C Library</title>\n\ <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n\ <link rel=\"canonical\" href=\"https://justine.lol/cosmopolitan/documentation.html\">\n\ <link rel=\"stylesheet\" href=\"style.css\">\n\ <link rel=\"apple-touch-icon\" sizes=\"180x180\" href=\"/apple-touch-icon.png\">\n\ <link rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/favicon-32x32.png\">\n\ <link rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/favicon-16x16.png\">\n\ <link rel=\"manifest\" href=\"/site.webmanifest\">\n\ <style>\n\ *, body {\n\ min-width: 0;\n\ }\n\ body {\n\ width: 100ch;\n\ max-width: calc(100vw - 3em);\n\ overflow-wrap: break-word;\n\ }\n\ body, .nav, .dox {\n\ margin: 0 auto;\n\ }\n\ .dox, header {\n\ display: flex;\n\ }\n\ header {\n\ flex-direction: column;\n\ align-items: center;\n\ margin: 4em auto 2em;\n\ }\n\ header>* {\n\ min-width: max-content;\n\ }\n\ header>img {\n\ max-width: 196px;\n\ min-width: 196px;\n\ max-height: 105px;\n\ min-height: 105px;\n\ }\n\ main {\n\ width: 80ch;\n\ }\n\ .toc {\n\ display: block;\n\ overflow-x: auto;\n\ }\n\ .toc a {\n\ text-decoration: none;\n\ }\n\ h3 a {\n\ color: inherit;\n\ text-decoration: none;\n\ }\n\ pre {\n\ margin-left: 0;\n\ padding: 12px;\n\ background: #f6f6f6;\n\ overflow-x: auto;\n\ border-radius: 5px;\n\ }\n\ hr {\n\ height: 1px;\n\ margin-bottom: 16px;\n\ color: #d6d9dc;\n\ background: #d6d9dc;\n\ border: 0;\n\ }\n\ .category {\n\ font-weight: bold;\n\ }\n\ .tagname {\n\ font-size: 12pt;\n\ font-weight: bold;\n\ }\n\ .tag {\n\ margin-top: .5em;\n\ }\n\ .tag dl {\n\ margin-top: .5em;\n\ margin-bottom: .5em;\n\ margin-left: 1em;\n\ }\n\ #top {\n\ position: fixed;\n\ left: max(0.5rem, calc(50vw - 56ch));\n\ bottom: 0.5rem;\n\ width: 2rem;\n\ height: 2rem;\n\ opacity: 0.45;\n\ transition: opacity 0.4s;\n\ }\n\ #top:hover {\n\ opacity: 0.9;\n\ }\n\ #search {\n\ top: 1ch;\n\ right: 1ch;\n\ float: right;\n\ position: sticky;\n\ margin: 1ch;\n\ }\n\ @media (max-width: 60ch) {\n\ .toc {\n\ display: none;\n\ }\n\ }\n\ @media print {\n\ #top, #search, nav, nav.toc { \n\ display: none;\n\ }\n\ }\n\ </style>\n\ <noscript><style>\n\ .toc { display: block; }\n\ #search { display: none; }\n\ </style></noscript>\n\ \n\ <header>\n\ <img width=\"196\" height=\"105\"\n\ src=\"//worker.jart.workers.dev/cosmopolitan/cosmopolitan.png\"\n\ title=\"cosmopolitan honeybadger\"\n\ alt=\"honeybadger\">\n\ <h1>cosmopolitan libc</h1>\n\ <div>your build-once run-anywhere c library</div>\n\ </header>\n\ \n\ <nav class=\"nav\">\n\ <ul>\n\ <li><a href=\"index.html\">Intro</a>\n\ <li><a href=\"download.html\">Download</a>\n\ <li><a href=\"functions.html\">Functions</a>\n\ <li><a class=\"active\" href=\"documentation.html\">Documentation</a>\n\ <li><a href=\"tutorials.html\">Tutorials</a>\n\ <li><a href=\"https://github.com/jart/cosmopolitan\">GitHub</a>\n\ <li><a href=\"license.html\">License</a>\n\ <li class=\"right\"><a href=\"../index.html\">» jart's web page</a>\n\ </ul>\n\ </nav>\n\ \n\ <div class=\"dox\">\n\ <nav class=\"toc\">\n\ "); /* // lefthand index: objects */ /* fprintf(f, "<p><span class=\"category\">macro objects</span>\n"); */ /* fprintf(f, "<p>\n"); */ /* for (i = 0; i < dox->index.n; ++i) { */ /* if (dox->index.p[i].t != kMacro) continue; */ /* m = dox->macros.p + dox->index.p[i].i; */ /* if (m->ignore) continue; */ /* if (!m->is_objlike) continue; */ /* fprintf(f, "<a href=\"#%s\">%s</a><br>\n", m->name, m->name); */ /* } */ /* // lefthand index: functions */ /* fprintf(f, "<p><span class=\"category\">macro functions</span>\n"); */ /* fprintf(f, "<p>\n"); */ /* for (i = 0; i < dox->index.n; ++i) { */ /* if (dox->index.p[i].t != kMacro) continue; */ /* m = dox->macros.p + dox->index.p[i].i; */ /* if (m->ignore) continue; */ /* if (m->is_objlike) continue; */ /* fprintf(f, "<a href=\"#%s\">%s</a><br>\n", m->name, m->name); */ /* } */ // lefthand index: objects fprintf(f, "<p><span class=\"category\">objects</span>\n"); fprintf(f, "<p>\n"); for (i = 0; i < dox->index.n; ++i) { if (dox->index.p[i].t != kObject) continue; o = dox->objects.p + dox->index.p[i].i; if (o->ignore) continue; if (o->is_function) continue; fprintf(f, "<a href=\"#%s\">%s</a><br>\n", o->name, o->name); } // lefthand index: functions fprintf(f, "<p><span class=\"category\">functions</span>\n"); fprintf(f, "<p>\n"); for (i = 0; i < dox->index.n; ++i) { if (dox->index.p[i].t != kObject) continue; o = dox->objects.p + dox->index.p[i].i; if (o->ignore) continue; if (!o->is_function) continue; fprintf(f, "<a href=\"#%s\">%s</a><br>\n", o->name, o->name); } fprintf(f, "</p></nav>\n\n"); // jump to top button fprintf(f, "\ <a href=\"#\" id=\"top\"><svg viewBox=\"-60 -60 120 120\" stroke=\"#404040\" stroke-width=\"10\">\n\ <title>Top of the page</title>\n\ <circle r=\"50\" fill=\"#f6f6f6\" />\n\ <path d=\"M-25,18l25,-40l25,40\" fill=\"none\" stroke-linecap=\"round\" />\n\ </svg></a>"); // right hand column fprintf(f, "<main>\n"); // search bar fprintf(f, "\ <input type=\"search\" id=\"search\" placeholder=\"Search...\" list=\"search-list\" spellcheck=\"false\" />\n\ <datalist id=\"search-list\"></datalist>\n\ <script>\n\ document.addEventListener('DOMContentLoaded', function () {\n\ var datalist = document.getElementById('search-list')\n\ document.querySelectorAll('.api').forEach(function (el) {\n\ var option = document.createElement('option')\n\ option.setAttribute('value', el.id)\n\ datalist.appendChild(option)\n\ })\n\ function scrollIntoView(event) {\n\ var value = event.target.value\n\ var el = document.getElementById(value) \n\ if (el) {\n\ location.hash = value\n\ el.scrollIntoView()\n\ }\n\ }\n\ var search = document.getElementById('search')\n\ search.addEventListener('change', scrollIntoView)\n\ search.addEventListener('keypress', function (event) {\n\ if (event.key === 'Enter') scrollIntoView(event)\n\ })\n\ })\n\ </script>\n\n"); // righthand contents for (i = 0; i < dox->index.n; ++i) { if (dox->index.p[i].t == kObject) { o = dox->objects.p + dox->index.p[i].i; if (o->ignore) continue; fprintf(f, "\n"); if (i) fprintf(f, "<hr>"); fprintf(f, "<div id=\"%s\" class=\"api\">\n", o->name); fprintf(f, "<h3><a href=\"#%s\">%s</a></h3>", o->name, o->name); // title if (o->javadown && *o->javadown->title) { fprintf(f, "<p>"); PrintText(f, o->javadown->title); fprintf(f, "\n"); } // text if (o->javadown && *o->javadown->text) { fprintf(f, "<p>"); PrintText(f, o->javadown->text); fprintf(f, "\n"); } // parameters if (o->is_function && (o->is_variadic || o->params.n || HasTag(o->javadown, "param"))) { fprintf(f, "<div class=\"tag\">\n"); fprintf(f, "<span class=\"tagname\">@param</span>\n"); fprintf(f, "<dl>\n"); if (o->params.n) { for (j = 0; j < o->params.n; ++j) { fprintf(f, "<dt>"); PrintText(f, o->params.p[j].type); fprintf(f, " <em>"); PrintText(f, o->params.p[j].name); fprintf(f, "</em>\n"); if (o->javadown) { prefix = xasprintf("%s ", o->params.p[j].name); for (k = 0; k < o->javadown->tags.n; ++k) { if (!strcmp(o->javadown->tags.p[k].tag, "param") && _startswith(o->javadown->tags.p[k].text, prefix)) { fprintf(f, "<dd>"); PrintText(f, o->javadown->tags.p[k].text + strlen(prefix)); fprintf(f, "\n"); break; } } free(prefix); } } } else if (o->javadown) { for (k = 0; k < o->javadown->tags.n; ++k) { if (!strcmp(o->javadown->tags.p[k].tag, "param")) { fprintf(f, "<dd>"); PrintText(f, o->javadown->tags.p[k].text); fprintf(f, "\n"); break; } } } if (o->is_variadic) { fprintf(f, "<dt>...\n"); } fprintf(f, "</dl>\n"); fprintf(f, "</div>\n"); // .tag } // return if (o->is_function) { fprintf(f, "<div class=\"tag\">\n"); if (IsNoReturn(o)) { fprintf(f, "<span class=\"tagname\">@noreturn</span>\n"); } else { fprintf(f, "<span class=\"tagname\">@return</span>\n"); was_outputted = false; fprintf(f, "<dl>\n"); if (o->javadown) { for (k = 0; k < o->javadown->tags.n; ++k) { if (strcmp(o->javadown->tags.p[k].tag, "return")) continue; if (!was_outputted) { fprintf(f, "<dt>"); PrintText(f, o->type); was_outputted = true; } fprintf(f, "\n<dd>"); PrintText(f, o->javadown->tags.p[k].text); fprintf(f, "\n"); } } if (!was_outputted) { fprintf(f, "<dt>"); PrintText(f, o->type); } fprintf(f, "</dl>\n"); } fprintf(f, "</div>\n"); // .tag } // type if (!o->is_function) { fprintf(f, "<div class=\"tag\">\n"); fprintf(f, "<span class=\"tagname\">@type</span>\n"); fprintf(f, "<dl>\n"); fprintf(f, "<dt>"); PrintText(f, o->type); fprintf(f, "</dl>\n"); fprintf(f, "</div>\n"); // .tag } // tags if (o->javadown) { for (k = 0; k < o->javadown->tags.n; ++k) { if (!strcmp(o->javadown->tags.p[k].tag, "param")) continue; if (!strcmp(o->javadown->tags.p[k].tag, "return")) continue; if (!strcmp(o->javadown->tags.p[k].tag, "noreturn")) continue; fprintf(f, "<div class=\"tag\">\n"); fprintf(f, "<span class=\"tagname\">@"); PrintText(f, o->javadown->tags.p[k].tag); fprintf(f, "</span>\n"); if (*o->javadown->tags.p[k].text) { PrintText(f, o->javadown->tags.p[k].text); fprintf(f, "\n"); } fprintf(f, "</div>\n"); // .tag } } // sauce if (strcmp(o->path, "missingno.c")) { fprintf(f, "<div class=\"tag\">\n"); fprintf(f, "<span class=\"tagname\">@see</span> <a " "href=\"https://github.com/jart/cosmopolitan/blob/master/" "%s#L%d\">%s</a>", o->path, o->line, o->path); fprintf(f, "</div>\n"); // .tag } fprintf(f, "</div>\n"); /* class=".api" */ } else { continue; m = dox->macros.p + dox->index.p[i].i; if (m->ignore) continue; fprintf(f, "\n"); if (i) fprintf(f, "<hr>"); fprintf(f, "<div id=\"%s\" class=\"api\">\n", m->name); fprintf(f, "<h3><a href=\"#%s\">%s</a></h3>", m->name, m->name); // title if (m->javadown && *m->javadown->title) { fprintf(f, "<p>"); PrintText(f, m->javadown->title); fprintf(f, "\n"); } // text if (m->javadown && *m->javadown->text) { fprintf(f, "<p>"); PrintText(f, m->javadown->text); fprintf(f, "\n"); } // parameters if (!m->is_objlike && (m->params.n || HasTag(m->javadown, "param"))) { fprintf(f, "<div class=\"tag\">\n"); fprintf(f, "<span class=\"tagname\">@param</span>\n"); fprintf(f, "<dl>\n"); if (m->params.n) { for (j = 0; j < m->params.n; ++j) { fprintf(f, "<dt>"); fprintf(f, "<em>"); PrintText(f, m->params.p[j].name); fprintf(f, "</em>\n"); if (m->javadown) { prefix = xasprintf("%s ", m->params.p[j].name); for (k = 0; k < m->javadown->tags.n; ++k) { if (!strcmp(m->javadown->tags.p[k].tag, "param") && _startswith(m->javadown->tags.p[k].text, prefix)) { fprintf(f, "<dd>"); PrintText(f, m->javadown->tags.p[k].text + strlen(prefix)); fprintf(f, "\n"); break; } } free(prefix); } } } else { for (k = 0; k < m->javadown->tags.n; ++k) { if (!strcmp(m->javadown->tags.p[k].tag, "param")) { fprintf(f, "<dd>"); PrintText(f, m->javadown->tags.p[k].text); fprintf(f, "\n"); break; } } } fprintf(f, "</dl>\n"); fprintf(f, "</div>\n"); // .tag } fprintf(f, "</div>\n"); /* class=".api" */ } } fprintf(f, "</main></div>\n"); // footer fprintf(f, "\ \n\ <footer>\n\ <p>\n\ <div style=\"float:right;text-align:right\">\n\ Free Libre &amp; Open Source<br>\n\ <a href=\"https://github.com/jart\">github.com/jart/cosmopolitan</a>\n\ </div>\n\ Feedback<br>\n\ [email protected]\n\ </p>\n\ <div style=\"clear:both\"></div>\n\ </footer>\n\ "); } /** * Merges documentation data and outputs HTML. */ void drop_dox(const StringArray *files, const char *path) { FILE *f; struct Dox *dox; dox = NewDox(); ReadDox(dox, files); IndexDox(dox); f = fopen(path, "w"); PrintDox(dox, f); fclose(f); FreeDox(dox); }
28,828
968
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/chibicc.c
#include "libc/calls/calls.h" #include "libc/calls/struct/sigaction.h" #include "libc/calls/struct/siginfo.h" #include "libc/calls/ucontext.h" #include "libc/mem/gc.internal.h" #include "libc/runtime/runtime.h" #include "libc/sysv/consts/sig.h" #include "libc/x/xasprintf.h" #include "third_party/chibicc/chibicc.h" asm(".ident\t\"\\n\\n\ chibicc (MIT/ISC License)\\n\ Copyright 2019 Rui Ueyama\\n\ Copyright 2020 Justine Alexandra Roberts Tunney\""); asm(".include \"libc/disclaimer.inc\""); typedef enum { FILE_NONE, FILE_C, FILE_ASM, FILE_ASM_CPP, FILE_OBJ, FILE_AR, FILE_DSO, } FileType; bool opt_common = true; bool opt_data_sections; bool opt_fentry; bool opt_function_sections; bool opt_no_builtin; bool opt_nop_mcount; bool opt_pg; bool opt_pic; bool opt_popcnt; bool opt_record_mcount; bool opt_sse3; bool opt_sse4; bool opt_verbose; static bool opt_A; static bool opt_E; static bool opt_J; static bool opt_P; static bool opt_M; static bool opt_MD; static bool opt_MMD; static bool opt_MP; static bool opt_S; static bool opt_c; static bool opt_cc1; static bool opt_hash_hash_hash; static bool opt_static; static bool opt_save_temps; static char *opt_MF; static char *opt_MT; static char *opt_o; static FileType opt_x; static StringArray opt_include; StringArray include_paths; static StringArray ld_extra_args; static StringArray as_extra_args; static StringArray std_include_paths; char *base_file; static char *output_file; static StringArray input_paths; char **chibicc_tmpfiles; static const char kChibiccVersion[] = "\ chibicc (cosmopolitan) 9.0.0\n\ copyright 2019 rui ueyama\n\ copyright 2020 justine alexandra roberts tunney\n"; static void chibicc_version(void) { xwrite(1, kChibiccVersion, sizeof(kChibiccVersion) - 1); _Exit(0); } static void chibicc_usage(int status) { char *p; size_t n; p = xslurp("/zip/third_party/chibicc/help.txt", &n); __paginate(1, p); _Exit(status); } void chibicc_cleanup(void) { size_t i; if (chibicc_tmpfiles && !opt_save_temps) { for (i = 0; chibicc_tmpfiles[i]; i++) { unlink(chibicc_tmpfiles[i]); } } } static bool take_arg(char *arg) { char *x[] = {"-o", "-I", "-idirafter", "-include", "-x", "-MF", "-MT", "-Xlinker"}; for (int i = 0; i < sizeof(x) / sizeof(*x); i++) { if (!strcmp(arg, x[i])) { return true; } } return false; } static void add_default_include_paths(char *argv0) { // We expect that chibicc-specific include files are installed // to ./include relative to argv[0]. /* char *buf = calloc(1, strlen(argv0) + 10); */ /* sprintf(buf, "%s/include", dirname(strdup(argv0))); */ /* strarray_push(&include_paths, buf); */ // Add standard include paths. /* strarray_push(&include_paths, "."); */ strarray_push(&include_paths, "/zip/.c"); // Keep a copy of the standard include paths for -MMD option. for (int i = 0; i < include_paths.len; i++) { strarray_push(&std_include_paths, include_paths.data[i]); } } static void define(char *str) { char *eq = strchr(str, '='); if (eq) { define_macro(strndup(str, eq - str), eq + 1); } else { define_macro(str, "1"); } } static FileType parse_opt_x(char *s) { if (!strcmp(s, "c")) return FILE_C; if (!strcmp(s, "assembler")) return FILE_ASM; if (!strcmp(s, "assembler-with-cpp")) return FILE_ASM_CPP; if (!strcmp(s, "none")) return FILE_NONE; error("<command line>: unknown argument for -x: %s", s); } static char *quote_makefile(char *s) { char *buf = calloc(1, strlen(s) * 2 + 1); for (int i = 0, j = 0; s[i]; i++) { switch (s[i]) { case '$': buf[j++] = '$'; buf[j++] = '$'; break; case '#': buf[j++] = '\\'; buf[j++] = '#'; break; case ' ': case '\t': for (int k = i - 1; k >= 0 && s[k] == '\\'; k--) buf[j++] = '\\'; buf[j++] = '\\'; buf[j++] = s[i]; break; default: buf[j++] = s[i]; break; } } return buf; } static void PrintMemoryUsage(void) { struct mallinfo mi; malloc_trim(0); mi = mallinfo(); fprintf(stderr, "\n"); fprintf(stderr, "allocated %,ld bytes of memory\n", mi.arena); fprintf(stderr, "allocated %,ld nodes (%,ld bytes)\n", alloc_node_count, sizeof(Node) * alloc_node_count); fprintf(stderr, "allocated %,ld tokens (%,ld bytes)\n", alloc_token_count, sizeof(Token) * alloc_token_count); fprintf(stderr, "allocated %,ld objs (%,ld bytes)\n", alloc_obj_count, sizeof(Obj) * alloc_obj_count); fprintf(stderr, "allocated %,ld types (%,ld bytes)\n", alloc_type_count, sizeof(Type) * alloc_type_count); fprintf(stderr, "chibicc hashmap hits %,ld\n", chibicc_hashmap_hits); fprintf(stderr, "chibicc hashmap miss %,ld\n", chibicc_hashmap_miss); fprintf(stderr, "as hashmap hits %,ld\n", as_hashmap_hits); fprintf(stderr, "as hashmap miss %,ld\n", as_hashmap_miss); } static void strarray_push_comma(StringArray *a, char *s) { char *p; for (; *s++ == ','; s = p) { p = strchrnul(s, ','); strarray_push(a, strndup(s, p - s)); } } static void parse_args(int argc, char **argv) { // Make sure that all command line options that take an argument // have an argument. for (int i = 1; i < argc; i++) { if (take_arg(argv[i])) { if (!argv[++i]) { chibicc_usage(1); } } } StringArray idirafter = {0}; for (int i = 1; i < argc; i++) { if (!strcmp(argv[i], "-###")) { opt_verbose = opt_hash_hash_hash = true; } else if (!strcmp(argv[i], "-cc1")) { opt_cc1 = true; } else if (!strcmp(argv[i], "--help")) { chibicc_usage(0); } else if (!strcmp(argv[i], "--version")) { chibicc_version(); } else if (!strcmp(argv[i], "-v")) { opt_verbose = true; atexit(PrintMemoryUsage); } else if (!strcmp(argv[i], "-o")) { opt_o = argv[++i]; } else if (_startswith(argv[i], "-o")) { opt_o = argv[i] + 2; } else if (!strcmp(argv[i], "-S")) { opt_S = true; } else if (!strcmp(argv[i], "-fcommon")) { opt_common = true; } else if (!strcmp(argv[i], "-fno-common")) { opt_common = false; } else if (!strcmp(argv[i], "-fno-builtin")) { opt_no_builtin = true; } else if (!strcmp(argv[i], "-save-temps")) { opt_save_temps = true; } else if (!strcmp(argv[i], "-c")) { opt_c = true; } else if (!strcmp(argv[i], "-E")) { opt_E = true; } else if (!strcmp(argv[i], "-J")) { opt_J = true; } else if (!strcmp(argv[i], "-A")) { opt_A = true; } else if (!strcmp(argv[i], "-P")) { opt_P = true; } else if (!strcmp(argv[i], "-I")) { strarray_push(&include_paths, argv[++i]); } else if (_startswith(argv[i], "-I")) { strarray_push(&include_paths, argv[i] + 2); } else if (!strcmp(argv[i], "-iquote")) { strarray_push(&include_paths, argv[++i]); } else if (_startswith(argv[i], "-iquote")) { strarray_push(&include_paths, argv[i] + strlen("-iquote")); } else if (!strcmp(argv[i], "-isystem")) { strarray_push(&include_paths, argv[++i]); } else if (_startswith(argv[i], "-isystem")) { strarray_push(&include_paths, argv[i] + strlen("-isystem")); } else if (!strcmp(argv[i], "-D")) { define(argv[++i]); } else if (_startswith(argv[i], "-D")) { define(argv[i] + 2); } else if (!strcmp(argv[i], "-U")) { undef_macro(argv[++i]); } else if (!strncmp(argv[i], "-U", 2)) { undef_macro(argv[i] + 2); } else if (!strcmp(argv[i], "-include")) { strarray_push(&opt_include, argv[++i]); } else if (!strcmp(argv[i], "-x")) { opt_x = parse_opt_x(argv[++i]); } else if (!strncmp(argv[i], "-x", 2)) { opt_x = parse_opt_x(argv[i] + 2); } else if (_startswith(argv[i], "-Wa")) { strarray_push_comma(&as_extra_args, argv[i] + 3); } else if (_startswith(argv[i], "-Wl")) { strarray_push_comma(&ld_extra_args, argv[i] + 3); } else if (!strcmp(argv[i], "-Xassembler")) { strarray_push(&as_extra_args, argv[++i]); } else if (!strcmp(argv[i], "-Xlinker")) { strarray_push(&ld_extra_args, argv[++i]); } else if (!strncmp(argv[i], "-l", 2) || !strncmp(argv[i], "-Wl,", 4)) { strarray_push(&input_paths, argv[i]); } else if (!strcmp(argv[i], "-s")) { strarray_push(&ld_extra_args, "-s"); } else if (!strcmp(argv[i], "-M")) { opt_M = true; } else if (!strcmp(argv[i], "-MF")) { opt_MF = argv[++i]; } else if (!strcmp(argv[i], "-MP")) { opt_MP = true; } else if (!strcmp(argv[i], "-MT")) { if (!opt_MT) { opt_MT = argv[++i]; } else { opt_MT = xasprintf("%s %s", opt_MT, argv[++i]); } } else if (!strcmp(argv[i], "-MD")) { opt_MD = true; } else if (!strcmp(argv[i], "-MQ")) { if (!opt_MT) { opt_MT = quote_makefile(argv[++i]); } else { opt_MT = xasprintf("%s %s", opt_MT, quote_makefile(argv[++i])); } } else if (!strcmp(argv[i], "-MMD")) { opt_MD = opt_MMD = true; } else if (!strcmp(argv[i], "-fpie") || !strcmp(argv[i], "-fpic") || !strcmp(argv[i], "-fPIC")) { opt_pic = true; } else if (!strcmp(argv[i], "-pg")) { opt_pg = true; } else if (!strcmp(argv[i], "-mfentry")) { opt_fentry = true; } else if (!strcmp(argv[i], "-ffunction-sections")) { opt_function_sections = true; } else if (!strcmp(argv[i], "-fdata-sections")) { opt_data_sections = true; } else if (!strcmp(argv[i], "-mrecord-mcount")) { opt_record_mcount = true; } else if (!strcmp(argv[i], "-mnop-mcount")) { opt_nop_mcount = true; } else if (!strcmp(argv[i], "-msse3")) { opt_sse3 = true; } else if (!strcmp(argv[i], "-msse4") || !strcmp(argv[i], "-msse4.2") || !strcmp(argv[i], "-msse4.1")) { opt_sse4 = true; } else if (!strcmp(argv[i], "-mpopcnt")) { opt_popcnt = true; } else if (!strcmp(argv[i], "-cc1-input")) { base_file = argv[++i]; } else if (!strcmp(argv[i], "-cc1-output")) { output_file = argv[++i]; } else if (!strcmp(argv[i], "-idirafter")) { strarray_push(&idirafter, argv[i++]); } else if (!strcmp(argv[i], "-static")) { opt_static = true; strarray_push(&ld_extra_args, "-static"); } else if (!strcmp(argv[i], "-shared")) { error("-shared not supported"); } else if (!strcmp(argv[i], "-L")) { strarray_push(&ld_extra_args, "-L"); strarray_push(&ld_extra_args, argv[++i]); } else if (_startswith(argv[i], "-L")) { strarray_push(&ld_extra_args, "-L"); strarray_push(&ld_extra_args, argv[i] + 2); } else { if (argv[i][0] == '-' && argv[i][1]) { /* compiler should not whine about the flags race */ if (opt_verbose) { fprintf(stderr, "unknown argument: %s\n", argv[i]); } } else { strarray_push(&input_paths, argv[i]); } } } for (int i = 0; i < idirafter.len; i++) { strarray_push(&include_paths, idirafter.data[i]); } if (!input_paths.len) { error("no input files"); } // -E implies that the input is the C macro language. if (opt_E) opt_x = FILE_C; } static FILE *open_file(char *path) { if (!path || strcmp(path, "-") == 0) return stdout; FILE *out = fopen(path, "w"); if (!out) error("cannot open output file: %s: %s", path, strerror(errno)); return out; } // Replace file extension static char *replace_extn(char *tmpl, char *extn) { char *filename = basename(strdup(tmpl)); int len1 = strlen(filename); int len2 = strlen(extn); char *buf = calloc(1, len1 + len2 + 2); char *dot = strrchr(filename, '.'); if (dot) *dot = '\0'; sprintf(buf, "%s%s", filename, extn); return buf; } static char *create_tmpfile(void) { char *path = xjoinpaths(kTmpPath, "chibicc-XXXXXX"); int fd = mkstemp(path); if (fd == -1) error("%s: mkstemp failed: %s", path, strerror(errno)); close(fd); static int len = 2; chibicc_tmpfiles = realloc(chibicc_tmpfiles, sizeof(char *) * len); chibicc_tmpfiles[len - 2] = path; chibicc_tmpfiles[len - 1] = NULL; len++; return path; } static void handle_exit(bool ok) { if (!ok) { opt_save_temps = true; exit(1); } } static bool NeedsShellQuotes(const char *s) { if (*s) { for (;;) { switch (*s++ & 255) { case 0: return false; case '-': case '.': case '/': case '_': case '0' ... '9': case 'A' ... 'Z': case 'a' ... 'z': break; default: return true; } } } else { return true; } } static bool run_subprocess(char **argv) { int rc, ws; size_t i, j, n; if (opt_verbose) { for (i = 0; argv[i]; i++) { fputc(' ', stderr); if (opt_hash_hash_hash && NeedsShellQuotes(argv[i])) { fputc('\'', stderr); for (j = 0; argv[i][j]; ++j) { if (argv[i][j] != '\'') { fputc(argv[i][j], stderr); } else { fputs("'\"'\"'", stderr); } } fputc('\'', stderr); } else { fputs(argv[i], stderr); } } fputc('\n', stderr); } if (!vfork()) { // Child process. Run a new command. execvp(argv[0], argv); _Exit(1); } // Wait for the child process to finish. do rc = wait(&ws); while (rc == -1 && errno == EINTR); return WIFEXITED(ws) && WEXITSTATUS(ws) == 0; } static bool run_cc1(int argc, char **argv, char *input, char *output) { char **args = calloc(argc + 10, sizeof(char *)); memcpy(args, argv, argc * sizeof(char *)); args[argc++] = "-cc1"; if (input) { args[argc++] = "-cc1-input"; args[argc++] = input; } if (output) { args[argc++] = "-cc1-output"; args[argc++] = output; } return run_subprocess(args); } static void print_token(FILE *out, Token *tok) { switch (tok->kind) { case TK_STR: switch (tok->ty->base->size) { case 1: fprintf(out, "%`'.*s", tok->ty->array_len - 1, tok->str); break; case 2: fprintf(out, "%`'.*hs", tok->ty->array_len - 1, tok->str); break; case 4: fprintf(out, "%`'.*ls", tok->ty->array_len - 1, tok->str); break; default: UNREACHABLE(); } break; default: fprintf(out, "%.*s", tok->len, tok->loc); break; } } static void print_tokens(Token *tok) { FILE *out = open_file(opt_o ? opt_o : "-"); int line = 1; for (; tok->kind != TK_EOF; tok = tok->next) { if (line > 1 && tok->at_bol) fprintf(out, "\n"); if (tok->has_space && !tok->at_bol) fprintf(out, " "); print_token(out, tok); line++; } fprintf(out, "\n"); } static bool in_std_include_path(char *path) { for (int i = 0; i < std_include_paths.len; i++) { char *dir = std_include_paths.data[i]; int len = strlen(dir); if (strncmp(dir, path, len) == 0 && path[len] == '/') return true; } return false; } // If -M options is given, the compiler write a list of input files to // stdout in a format that "make" command can read. This feature is // used to automate file dependency management. static void print_dependencies(void) { char *path; if (opt_MF) { path = opt_MF; } else if (opt_MD) { path = replace_extn(opt_o ? opt_o : base_file, ".d"); } else if (opt_o) { path = opt_o; } else { path = "-"; } FILE *out = open_file(path); if (opt_MT) fprintf(out, "%s:", opt_MT); else fprintf(out, "%s:", quote_makefile(replace_extn(base_file, ".o"))); File **files = get_input_files(); for (int i = 0; files[i]; i++) { if (opt_MMD && in_std_include_path(files[i]->name)) continue; fprintf(out, " \\\n\t%s", files[i]->name); } fprintf(out, "\n\n"); if (opt_MP) { for (int i = 1; files[i]; i++) { if (opt_MMD && in_std_include_path(files[i]->name)) continue; fprintf(out, "%s:\n\n", quote_makefile(files[i]->name)); } } } static Token *must_tokenize_file(char *path) { Token *tok = tokenize_file(path); if (!tok) error("%s: %s", path, strerror(errno)); return tok; } static Token *append_tokens(Token *tok1, Token *tok2) { if (!tok1 || tok1->kind == TK_EOF) return tok2; Token *t = tok1; while (t->next->kind != TK_EOF) t = t->next; t->next = tok2; return tok1; } static FileType get_file_type(const char *filename) { if (opt_x != FILE_NONE) return opt_x; if (_endswith(filename, ".a")) return FILE_AR; if (_endswith(filename, ".o")) return FILE_OBJ; if (_endswith(filename, ".c")) return FILE_C; if (_endswith(filename, ".s")) return FILE_ASM; if (_endswith(filename, ".S")) return FILE_ASM_CPP; error("<command line>: unknown file extension: %s", filename); } static void cc1(void) { FileType ft; Token *tok = NULL; ft = get_file_type(base_file); if (opt_J && (ft == FILE_ASM || ft == FILE_ASM_CPP)) { output_javadown_asm(output_file, base_file); return; } // Process -include option for (int i = 0; i < opt_include.len; i++) { char *incl = opt_include.data[i]; char *path; if (fileexists(incl)) { path = incl; } else { path = search_include_paths(incl); if (!path) error("-include: %s: %s", incl, strerror(errno)); } Token *tok2 = must_tokenize_file(path); tok = append_tokens(tok, tok2); } // Tokenize and parse. Token *tok2 = must_tokenize_file(base_file); tok = append_tokens(tok, tok2); tok = preprocess(tok); // If -M or -MD are given, print file dependencies. if (opt_M || opt_MD) { print_dependencies(); if (opt_M) return; } // If -E is given, print out preprocessed C code as a result. if (opt_E || ft == FILE_ASM_CPP) { print_tokens(tok); return; } Obj *prog = parse(tok); if (opt_A) { print_ast(stdout, prog); return; } if (opt_J) { output_javadown(output_file, prog); return; } if (opt_P) { output_bindings_python(output_file, prog, tok2); return; } FILE *out = open_file(output_file); codegen(prog, out); fclose(out); } static int CountArgv(char **argv) { int n = 0; while (*argv++) ++n; return n; } static void assemble(char *input, char *output) { char *as = getenv("AS"); if (!as || !*as) as = "as"; StringArray arr = {0}; strarray_push(&arr, as); strarray_push(&arr, "-W"); strarray_push(&arr, "-I."); strarray_push(&arr, "-c"); for (int i = 0; i < as_extra_args.len; i++) { strarray_push(&arr, as_extra_args.data[i]); } strarray_push(&arr, input); strarray_push(&arr, "-o"); strarray_push(&arr, output); if (1) { bool kludge = opt_save_temps; opt_save_temps = true; Assembler(CountArgv(arr.data), arr.data); opt_save_temps = kludge; } else { handle_exit(run_subprocess(arr.data)); } } static void run_linker(StringArray *inputs, char *output) { char *ld = getenv("LD"); if (!ld || !*ld) ld = "ld"; StringArray arr = {0}; strarray_push(&arr, ld); strarray_push(&arr, "-m"); strarray_push(&arr, "elf_x86_64"); strarray_push(&arr, "-z"); strarray_push(&arr, "max-page-size=0x1000"); strarray_push(&arr, "-static"); strarray_push(&arr, "-nostdlib"); strarray_push(&arr, "--gc-sections"); strarray_push(&arr, "--build-id=none"); strarray_push(&arr, "--no-dynamic-linker"); strarray_push(&arr, xasprintf("-Ttext-segment=%#x", IMAGE_BASE_VIRTUAL)); /* strarray_push(&arr, "-T"); */ /* strarray_push(&arr, LDS); */ /* strarray_push(&arr, APE); */ /* strarray_push(&arr, CRT); */ for (int i = 0; i < ld_extra_args.len; i++) { strarray_push(&arr, ld_extra_args.data[i]); } for (int i = 0; i < inputs->len; i++) { strarray_push(&arr, inputs->data[i]); } strarray_push(&arr, "-o"); strarray_push(&arr, output); handle_exit(run_subprocess(arr.data)); } static void OnCtrlC(int sig, siginfo_t *si, void *ctx) { exit(1); } int chibicc(int argc, char **argv) { ShowCrashReports(); atexit(chibicc_cleanup); sigaction(SIGINT, &(struct sigaction){.sa_sigaction = OnCtrlC}, NULL); for (int i = 1; i < argc; i++) { if (!strcmp(argv[i], "-cc1")) { opt_cc1 = true; break; } } if (opt_cc1) init_macros(); parse_args(argc, argv); if (opt_cc1) { init_macros_conditional(); add_default_include_paths(argv[0]); cc1(); return 0; } if (input_paths.len > 1 && opt_o && (opt_c || opt_S | opt_E)) { error("cannot specify '-o' with '-c,' '-S' or '-E' with multiple files"); } StringArray ld_args = {0}; StringArray dox_args = {0}; for (int i = 0; i < input_paths.len; i++) { char *input = input_paths.data[i]; if (!strncmp(input, "-l", 2)) { strarray_push(&ld_args, input); continue; } if (!strncmp(input, "-Wl,", 4)) { char *s = strdup(input + 4); char *arg = strtok(s, ","); while (arg) { strarray_push(&ld_args, arg); arg = strtok(NULL, ","); } continue; } char *output; if (opt_o) { output = opt_o; } else if (opt_S) { output = replace_extn(input, ".s"); } else { output = replace_extn(input, ".o"); } FileType type = get_file_type(input); // Handle .o or .a if (type == FILE_OBJ || type == FILE_AR || type == FILE_DSO) { strarray_push(&ld_args, input); continue; } // Dox if (opt_J) { if (opt_c) { handle_exit(run_cc1(argc, argv, input, output)); } else { char *tmp = create_tmpfile(); if (run_cc1(argc, argv, input, tmp)) { strarray_push(&dox_args, tmp); } } continue; } // Handle .s if (type == FILE_ASM) { if (!opt_S) { assemble(input, output); } continue; } assert(type == FILE_C || type == FILE_ASM_CPP); // Just print ast. if (opt_A) { handle_exit(run_cc1(argc, argv, input, NULL)); continue; } // Just preprocess if (opt_E || opt_M) { handle_exit(run_cc1(argc, argv, input, NULL)); continue; } // Python Bindings if (opt_P) { handle_exit(run_cc1(argc, argv, input, opt_o ? opt_o : "/dev/stdout")); continue; } // Compile if (opt_S) { handle_exit(run_cc1(argc, argv, input, output)); continue; } // Compile and assemble if (opt_c) { char *tmp = create_tmpfile(); handle_exit(run_cc1(argc, argv, input, tmp)); assemble(tmp, output); continue; } // Compile, assemble and link char *tmp1 = create_tmpfile(); char *tmp2 = create_tmpfile(); handle_exit(run_cc1(argc, argv, input, tmp1)); assemble(tmp1, tmp2); strarray_push(&ld_args, tmp2); continue; } if (ld_args.len > 0) { run_linker(&ld_args, opt_o ? opt_o : "a.out"); } if (dox_args.len > 0) { drop_dox(&dox_args, opt_o ? opt_o : "/dev/stdout"); } return 0; }
23,000
805
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/type.c
#include "libc/assert.h" #include "third_party/chibicc/chibicc.h" /* TODO(jart): Why can't these be const? */ Type ty_void[1] = {{TY_VOID, 1, 1}}; Type ty_bool[1] = {{TY_BOOL, 1, 1}}; Type ty_char[1] = {{TY_CHAR, 1, 1}}; Type ty_short[1] = {{TY_SHORT, 2, 2}}; Type ty_int[1] = {{TY_INT, 4, 4}}; Type ty_long[1] = {{TY_LONG, 8, 8}}; Type ty_int128[1] = {{TY_INT128, 16, 16}}; Type ty_uchar[1] = {{TY_CHAR, 1, 1, true}}; Type ty_ushort[1] = {{TY_SHORT, 2, 2, true}}; Type ty_uint[1] = {{TY_INT, 4, 4, true}}; Type ty_ulong[1] = {{TY_LONG, 8, 8, true}}; Type ty_uint128[1] = {{TY_INT128, 16, 16, true}}; Type ty_float[1] = {{TY_FLOAT, 4, 4}}; Type ty_double[1] = {{TY_DOUBLE, 8, 8}}; Type ty_ldouble[1] = {{TY_LDOUBLE, 16, 16}}; static Type *new_type(TypeKind kind, int size, int align) { Type *ty = alloc_type(); ty->kind = kind; ty->size = size; ty->align = align; return ty; } bool is_integer(Type *ty) { TypeKind k = ty->kind; return k == TY_BOOL || k == TY_CHAR || k == TY_SHORT || k == TY_INT || k == TY_LONG || k == TY_INT128 || k == TY_ENUM; } bool is_flonum(Type *ty) { return ty->kind == TY_FLOAT || ty->kind == TY_DOUBLE || ty->kind == TY_LDOUBLE; } bool is_numeric(Type *ty) { return is_integer(ty) || is_flonum(ty); } bool is_compatible(Type *t1, Type *t2) { if (t1 == t2) return true; if (t1->origin) return is_compatible(t1->origin, t2); if (t2->origin) return is_compatible(t1, t2->origin); if (t1->kind != t2->kind) return false; switch (t1->kind) { case TY_CHAR: case TY_SHORT: case TY_INT: case TY_LONG: case TY_INT128: return t1->is_unsigned == t2->is_unsigned; case TY_FLOAT: case TY_DOUBLE: case TY_LDOUBLE: return true; case TY_PTR: return is_compatible(t1->base, t2->base); case TY_FUNC: { if (!is_compatible(t1->return_ty, t2->return_ty)) return false; if (t1->is_variadic != t2->is_variadic) return false; Type *p1 = t1->params; Type *p2 = t2->params; for (; p1 && p2; p1 = p1->next, p2 = p2->next) { if (!is_compatible(p1, p2)) return false; } return p1 == NULL && p2 == NULL; } case TY_ARRAY: if (!is_compatible(t1->base, t2->base)) return false; return t1->array_len < 0 && t2->array_len < 0 && t1->array_len == t2->array_len; } return false; } Type *copy_type(Type *ty) { Type *ret = alloc_type(); *ret = *ty; ret->origin = ty; return ret; } Type *pointer_to(Type *base) { Type *ty = new_type(TY_PTR, 8, 8); ty->base = base; ty->is_unsigned = true; return ty; } Type *func_type(Type *return_ty) { // The C spec disallows sizeof(<function type>), but // GCC allows that and the expression is evaluated to 1. Type *ty = new_type(TY_FUNC, 1, 1); ty->return_ty = return_ty; return ty; } Type *array_of(Type *base, int len) { Type *ty = new_type(TY_ARRAY, base->size * len, base->align); ty->base = base; ty->array_len = len; return ty; } Type *vla_of(Type *base, Node *len) { Type *ty = new_type(TY_VLA, 8, 8); ty->base = base; ty->vla_len = len; return ty; } Type *enum_type(void) { Type *ty = new_type(TY_ENUM, 4, 4); ty->is_unsigned = true; return ty; } Type *struct_type(void) { return new_type(TY_STRUCT, 0, 1); } static Type *get_common_type(Type *ty1, Type *ty2) { if (ty1->base) return pointer_to(ty1->base); if (ty1->kind == TY_FUNC) return pointer_to(ty1); if (ty2->kind == TY_FUNC) return pointer_to(ty2); if (ty1->kind == TY_LDOUBLE || ty2->kind == TY_LDOUBLE) return ty_ldouble; if (ty1->kind == TY_DOUBLE || ty2->kind == TY_DOUBLE) return ty_double; if (ty1->kind == TY_FLOAT || ty2->kind == TY_FLOAT) return ty_float; if (ty1->size < 4) ty1 = ty_int; if (ty2->size < 4) ty2 = ty_int; if (ty1->size != ty2->size) return (ty1->size < ty2->size) ? ty2 : ty1; if (ty2->is_unsigned) return ty2; return ty1; } // For many binary operators, we implicitly promote operands so that // both operands have the same type. Any integral type smaller than // int is always promoted to int. If the type of one operand is larger // than the other's (e.g. "long" vs. "int"), the smaller operand will // be promoted to match with the other. // // This operation is called the "usual arithmetic conversion". static void usual_arith_conv(Node **lhs, Node **rhs) { if (!(*lhs)->ty || !(*rhs)->ty) { error_tok((*lhs)->tok, "internal npe error"); } Type *ty = get_common_type((*lhs)->ty, (*rhs)->ty); *lhs = new_cast(*lhs, ty); *rhs = new_cast(*rhs, ty); } void add_type(Node *node) { if (!node || node->ty) return; add_type(node->lhs); add_type(node->rhs); add_type(node->cond); add_type(node->then); add_type(node->els); add_type(node->init); add_type(node->inc); for (Node *n = node->body; n; n = n->next) add_type(n); for (Node *n = node->args; n; n = n->next) add_type(n); switch (node->kind) { case ND_NUM: node->ty = ty_int; return; case ND_ADD: case ND_SUB: case ND_MUL: case ND_DIV: case ND_REM: case ND_BINAND: case ND_BINOR: case ND_BINXOR: usual_arith_conv(&node->lhs, &node->rhs); node->ty = node->lhs->ty; return; case ND_NEG: { Type *ty = get_common_type(ty_int, node->lhs->ty); node->lhs = new_cast(node->lhs, ty); node->ty = ty; return; } case ND_ASSIGN: if (node->lhs->ty->kind == TY_ARRAY) error_tok(node->lhs->tok, "not an lvalue!"); if (node->lhs->ty->kind != TY_STRUCT) node->rhs = new_cast(node->rhs, node->lhs->ty); node->ty = node->lhs->ty; return; case ND_EQ: case ND_NE: case ND_LT: case ND_LE: usual_arith_conv(&node->lhs, &node->rhs); node->ty = ty_int; return; case ND_FUNCALL: node->ty = node->func_ty->return_ty; return; case ND_NOT: case ND_LOGOR: case ND_LOGAND: node->ty = ty_int; return; case ND_BITNOT: case ND_SHL: case ND_SHR: node->ty = node->lhs->ty; return; case ND_VAR: case ND_VLA_PTR: node->ty = node->var->ty; return; case ND_COND: if (node->then->ty->kind == TY_VOID || node->els->ty->kind == TY_VOID) { node->ty = ty_void; } else { usual_arith_conv(&node->then, &node->els); node->ty = node->then->ty; } return; case ND_COMMA: node->ty = node->rhs->ty; return; case ND_MEMBER: node->ty = node->member->ty; return; case ND_ADDR: { Type *ty = node->lhs->ty; if (ty->kind == TY_ARRAY) { node->ty = pointer_to(ty->base); } else { node->ty = pointer_to(ty); } return; } case ND_DEREF: #if 0 if (node->lhs->ty->size == 16 && (node->lhs->ty->kind == TY_FLOAT || node->lhs->ty->kind == TY_DOUBLE)) { node->ty = node->lhs->ty; } else { #endif if (!node->lhs->ty->base) { error_tok(node->tok, "invalid pointer dereference"); } if (node->lhs->ty->base->kind == TY_VOID) { /* TODO(jart): Does standard permit this? */ /* https://lkml.org/lkml/2018/3/20/845 */ error_tok(node->tok, "dereferencing a void pointer"); } node->ty = node->lhs->ty->base; #if 0 } #endif return; case ND_STMT_EXPR: if (node->body) { Node *stmt = node->body; for (;;) { if (stmt->next) { stmt = stmt->next; } else { if (stmt->kind == ND_LABEL && stmt->lhs) { stmt = stmt->lhs; } else { break; } } } if (stmt->kind == ND_EXPR_STMT) { node->ty = stmt->lhs->ty; return; } } error_tok(node->tok, "statement expression returning void is not supported"); return; case ND_LABEL_VAL: node->ty = pointer_to(ty_void); return; case ND_CAS: add_type(node->cas_addr); add_type(node->cas_old); add_type(node->cas_new); node->ty = ty_bool; if (node->cas_addr->ty->kind != TY_PTR) error_tok(node->cas_addr->tok, "pointer expected"); if (node->cas_old->ty->kind != TY_PTR) error_tok(node->cas_old->tok, "pointer expected"); return; case ND_EXCH_N: case ND_FETCHADD: case ND_FETCHSUB: case ND_FETCHXOR: case ND_FETCHAND: case ND_FETCHOR: case ND_SUBFETCH: if (node->lhs->ty->kind != TY_PTR) error_tok(node->lhs->tok, "pointer expected"); node->rhs = new_cast(node->rhs, node->lhs->ty->base); node->ty = node->lhs->ty->base; return; } }
8,746
311
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/strarray.c
#include "third_party/chibicc/chibicc.h" void strarray_push(StringArray *arr, char *s) { size_t i; if (!arr->data) { arr->data = calloc(8, sizeof(char *)); arr->capacity = 8; } if (arr->len + 1 == arr->capacity) { arr->capacity += arr->capacity >> 1; arr->data = realloc(arr->data, arr->capacity * sizeof(*arr->data)); for (i = arr->len; i < arr->capacity; i++) { arr->data[i] = 0; } } arr->data[arr->len++] = s; }
458
18
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/parse.c
// This file contains a recursive descent parser for C. // // Most functions in this file are named after the symbols they are // supposed to read from an input token list. For example, stmt() is // responsible for reading a statement from a token list. The function // then construct an AST node representing a statement. // // Each function conceptually returns two values, an AST node and // remaining part of the input tokens. Since C doesn't support // multiple return values, the remaining tokens are returned to the // caller via a pointer argument. // // Input tokens are represented by a linked list. Unlike many recursive // descent parsers, we don't have the notion of the "input token stream". // Most parsing functions don't change the global state of the parser. // So it is very easy to lookahead arbitrary number of tokens in this // parser. #include "libc/dce.h" #include "libc/intrin/asan.internal.h" #include "libc/log/libfatal.internal.h" #include "libc/log/log.h" #include "libc/mem/mem.h" #include "libc/nexgen32e/ffs.h" #include "libc/testlib/testlib.h" #include "libc/x/xasprintf.h" #include "third_party/chibicc/chibicc.h" #include "third_party/chibicc/kw.h" typedef struct InitDesg InitDesg; typedef struct Initializer Initializer; typedef struct Scope Scope; // Scope for local variables, global variables, typedefs // or enum constants typedef struct { Obj *var; Type *type_def; Type *enum_ty; int enum_val; } VarScope; // Represents a block scope. struct Scope { Scope *next; // C has two block scopes; one is for variables/typedefs and // the other is for struct/union/enum tags. HashMap vars; HashMap tags; }; // Variable attributes such as typedef or extern. typedef struct { bool is_typedef; bool is_static; bool is_extern; bool is_inline; bool is_tls; bool is_weak; bool is_ms_abi; bool is_aligned; bool is_noreturn; bool is_destructor; bool is_constructor; bool is_externally_visible; bool is_no_instrument_function; bool is_force_align_arg_pointer; bool is_no_caller_saved_registers; int align; char *section; char *visibility; } VarAttr; // This struct represents a variable initializer. Since initializers // can be nested (e.g. `int x[2][2] = {{1, 2}, {3, 4}}`), this struct // is a tree data structure. struct Initializer { Initializer *next; Type *ty; Token *tok; bool is_flexible; // If it's not an aggregate type and has an initializer, // `expr` has an initialization expression. Node *expr; // If it's an initializer for an aggregate type (e.g. array or struct), // `children` has initializers for its children. Initializer **children; // Only one member can be initialized for a union. // `mem` is used to clarify which member is initialized. Member *mem; }; // For local variable initializer. struct InitDesg { InitDesg *next; int idx; Member *member; Obj *var; }; // All local variable instances created during parsing are // accumulated to this list. static Obj *locals; // Likewise, global variables are accumulated to this list. static Obj *globals; static Scope *scope = &(Scope){}; // Points to the function object the parser is currently parsing. static Obj *current_fn; // Lists of all goto statements and labels in the curent function. static Node *gotos; static Node *labels; // Current "goto" and "continue" jump targets. static char *brk_label; static char *cont_label; // Points to a node representing a switch if we are parsing // a switch statement. Otherwise, NULL. static Node *current_switch; static Obj *builtin_alloca; static Token *current_javadown; static Initializer *initializer(Token **, Token *, Type *, Type **); static Member *get_struct_member(Type *, Token *); static Node *binor(Token **, Token *); static Node *add(Token **, Token *); static Node *assign(Token **, Token *); static Node *binand(Token **, Token *); static Node *binxor(Token **, Token *); static Node *cast(Token **, Token *); static Node *compound_stmt(Token **, Token *); static Node *conditional(Token **, Token *); static Node *declaration(Token **, Token *, Type *, VarAttr *); static Node *equality(Token **, Token *); static Node *expr_stmt(Token **, Token *); static Node *funcall(Token **, Token *, Node *); static Node *logand(Token **, Token *); static Node *logor(Token **, Token *); static Node *lvar_initializer(Token **, Token *, Obj *); static Node *mul(Token **, Token *); static Node *new_add(Node *, Node *, Token *); static Node *new_sub(Node *, Node *, Token *); static Node *postfix(Token **, Token *); static Node *primary(Token **, Token *); static Node *relational(Token **, Token *); static Node *shift(Token **, Token *); static Node *stmt(Token **, Token *); static Node *unary(Token **, Token *); static Token *function(Token *, Type *, VarAttr *); static Token *global_variable(Token *, Type *, VarAttr *); static Token *parse_typedef(Token *, Type *); static Type *declarator(Token **, Token *, Type *); static Type *enum_specifier(Token **, Token *); static Type *struct_decl(Token **, Token *); static Type *type_suffix(Token **, Token *, Type *); static Type *typename(Token **, Token *); static Type *typeof_specifier(Token **, Token *); static Type *union_decl(Token **, Token *); static bool is_function(Token *); static bool is_typename(Token *); static double eval_double(Node *); static int64_t eval_rval(Node *, char ***); static void array_initializer2(Token **, Token *, Initializer *, int); static void gvar_initializer(Token **, Token *, Obj *); static void initializer2(Token **, Token *, Initializer *); static void struct_initializer2(Token **, Token *, Initializer *, Member *); static void enter_scope(void) { Scope *sc = calloc(1, sizeof(Scope)); sc->next = scope; scope = sc; } static void leave_scope(void) { scope = scope->next; } // Find a variable by name. static VarScope *find_var(Token *tok) { for (Scope *sc = scope; sc; sc = sc->next) { VarScope *sc2 = hashmap_get2(&sc->vars, tok->loc, tok->len); if (sc2) return sc2; } return NULL; } static Type *find_tag(Token *tok) { for (Scope *sc = scope; sc; sc = sc->next) { Type *ty = hashmap_get2(&sc->tags, tok->loc, tok->len); if (ty) return ty; } return NULL; } Node *new_node(NodeKind kind, Token *tok) { Node *node = alloc_node(); node->kind = kind; node->tok = tok; return node; } static Node *new_binary(NodeKind kind, Node *lhs, Node *rhs, Token *tok) { Node *node = new_node(kind, tok); node->lhs = lhs; node->rhs = rhs; return node; } static Node *new_unary(NodeKind kind, Node *expr, Token *tok) { Node *node = new_node(kind, tok); node->lhs = expr; return node; } static Node *new_num(int64_t val, Token *tok) { Node *node = new_node(ND_NUM, tok); node->val = val; return node; } static Node *new_int(int64_t val, Token *tok) { Node *node = new_num(val, tok); node->ty = ty_int; return node; } static Node *new_bool(int64_t val, Token *tok) { Node *node = new_num(val, tok); node->ty = ty_bool; return node; } static Node *new_long(int64_t val, Token *tok) { Node *node = new_num(val, tok); node->ty = ty_long; return node; } static Node *new_ulong(int64_t val, Token *tok) { Node *node = new_num(val, tok); node->ty = ty_ulong; return node; } static Node *new_var_node(Obj *var, Token *tok) { CHECK_NOTNULL(var); Node *node = new_node(ND_VAR, tok); node->var = var; return node; } static Node *new_vla_ptr(Obj *var, Token *tok) { Node *node = new_node(ND_VLA_PTR, tok); node->var = var; return node; } Node *new_cast(Node *expr, Type *ty) { add_type(expr); Node *node = alloc_node(); node->kind = ND_CAST; node->tok = expr->tok; node->lhs = expr; node->ty = copy_type(ty); return node; } static VarScope *push_scope(char *name) { VarScope *sc = calloc(1, sizeof(VarScope)); hashmap_put(&scope->vars, name, sc); return sc; } static Initializer *new_initializer(Type *ty, bool is_flexible) { Initializer *init = calloc(1, sizeof(Initializer)); init->ty = ty; if (ty->kind == TY_ARRAY) { if (is_flexible && ty->size < 0) { init->is_flexible = true; return init; } init->children = calloc(ty->array_len, sizeof(Initializer *)); for (int i = 0; i < ty->array_len; i++) { init->children[i] = new_initializer(ty->base, false); } return init; } if (ty->kind == TY_STRUCT || ty->kind == TY_UNION) { // Count the number of struct members. int len = 0; for (Member *mem = ty->members; mem; mem = mem->next) len++; init->children = calloc(len, sizeof(Initializer *)); for (Member *mem = ty->members; mem; mem = mem->next) { if (is_flexible && ty->is_flexible && !mem->next) { Initializer *child = calloc(1, sizeof(Initializer)); child->ty = mem->ty; child->is_flexible = true; init->children[mem->idx] = child; } else { init->children[mem->idx] = new_initializer(mem->ty, false); } } return init; } return init; } static Obj *new_var(char *name, Type *ty) { Obj *var = alloc_obj(); var->name = name; var->ty = ty; var->align = ty->align; push_scope(name)->var = var; return var; } static Obj *new_lvar(char *name, Type *ty) { Obj *var = new_var(name, ty); var->is_local = true; var->next = locals; locals = var; return var; } static Obj *new_gvar(char *name, Type *ty) { Obj *var = new_var(name, ty); var->next = globals; var->is_static = true; var->is_definition = true; globals = var; return var; } static char *new_unique_name(void) { static int id = 0; return xasprintf(".L..%d", id++); } static Obj *new_anon_gvar(Type *ty) { return new_gvar(new_unique_name(), ty); } static Obj *new_string_literal(char *p, Type *ty) { Obj *var = new_anon_gvar(ty); var->init_data = p; var->is_string_literal = true; return var; } static char *get_ident(Token *tok) { if (tok->kind != TK_IDENT) { __die(); error_tok(tok, "expected an identifier"); } return strndup(tok->loc, tok->len); } static Type *find_typedef(Token *tok) { if (tok->kind == TK_IDENT) { VarScope *sc = find_var(tok); if (sc) return sc->type_def; } return NULL; } static void push_tag_scope(Token *tok, Type *ty) { hashmap_put2(&scope->tags, tok->loc, tok->len, ty); } // Consumes token if equal to STR or __STR__. static bool consume_attribute(Token **rest, Token *tok, char *name) { size_t n = strlen(name); if ((n == tok->len && !memcmp(tok->loc, name, n)) || (2 + n + 2 == tok->len && tok->loc[0] == '_' && tok->loc[1] == '_' && tok->loc[tok->len - 2] == '_' && tok->loc[tok->len - 1] == '_' && !memcmp(tok->loc + 2, name, n))) { *rest = tok->next; return true; } *rest = tok; return false; } static Token *attribute_list(Token *tok, void *arg, Token *(*f)(Token *, void *)) { while (CONSUME(&tok, tok, "__attribute__")) { tok = skip(tok, '('); tok = skip(tok, '('); bool first = true; while (!CONSUME(&tok, tok, ")")) { if (!first) tok = skip(tok, ','); first = false; tok = f(tok, arg); } tok = skip(tok, ')'); } return tok; } static Token *type_attributes(Token *tok, void *arg) { Type *ty = arg; if (consume_attribute(&tok, tok, "packed")) { ty->is_packed = true; return tok; } if (consume_attribute(&tok, tok, "ms_abi")) { ty->is_ms_abi = true; return tok; } if (consume_attribute(&tok, tok, "aligned")) { ty->is_aligned = true; if (CONSUME(&tok, tok, "(")) { Token *altok = tok; ty->align = const_expr(&tok, tok); if (popcnt(ty->align) != 1) error_tok(altok, "must be two power"); tok = skip(tok, ')'); } else { ty->align = 16; /* biggest alignment */ } return tok; } if (consume_attribute(&tok, tok, "vector_size")) { tok = skip(tok, '('); int vs = const_expr(&tok, tok); if (vs != 16) { error_tok(tok, "only vector_size 16 supported"); } if (vs != ty->vector_size) { ty->size = vs; ty->vector_size = vs; if (!ty->is_aligned) ty->align = vs; /* ty->base = ty; */ /* ty->array_len = vs / ty->size; */ } return skip(tok, ')'); } if (consume_attribute(&tok, tok, "warn_if_not_aligned")) { tok = skip(tok, '('); const_expr(&tok, tok); return skip(tok, ')'); } if (consume_attribute(&tok, tok, "deprecated") || consume_attribute(&tok, tok, "may_alias") || consume_attribute(&tok, tok, "unused")) { return tok; } if (consume_attribute(&tok, tok, "alloc_size") || consume_attribute(&tok, tok, "alloc_align")) { if (CONSUME(&tok, tok, "(")) { const_expr(&tok, tok); tok = skip(tok, ')'); } return tok; } error_tok(tok, "unknown type attribute"); } static Token *thing_attributes(Token *tok, void *arg) { VarAttr *attr = arg; if (consume_attribute(&tok, tok, "weak")) { attr->is_weak = true; return tok; } if (consume_attribute(&tok, tok, "hot")) { attr->section = ".text.likely"; return tok; } if (consume_attribute(&tok, tok, "cold")) { attr->section = ".text.unlikely"; return tok; } if (consume_attribute(&tok, tok, "section")) { tok = skip(tok, '('); attr->section = ConsumeStringLiteral(&tok, tok); return skip(tok, ')'); } if (consume_attribute(&tok, tok, "noreturn")) { attr->is_noreturn = true; return tok; } if (consume_attribute(&tok, tok, "always_inline")) { attr->is_inline = true; return tok; } if (consume_attribute(&tok, tok, "visibility")) { tok = skip(tok, '('); attr->visibility = ConsumeStringLiteral(&tok, tok); return skip(tok, ')'); } if (consume_attribute(&tok, tok, "externally_visible")) { attr->is_externally_visible = true; return tok; } if (consume_attribute(&tok, tok, "no_instrument_function")) { attr->is_no_instrument_function = true; return tok; } if (consume_attribute(&tok, tok, "force_align_arg_pointer")) { attr->is_force_align_arg_pointer = true; return tok; } if (consume_attribute(&tok, tok, "no_caller_saved_registers")) { attr->is_no_caller_saved_registers = true; return tok; } if (consume_attribute(&tok, tok, "ms_abi")) { attr->is_ms_abi = true; return tok; } if (consume_attribute(&tok, tok, "constructor")) { attr->is_constructor = true; if (CONSUME(&tok, tok, "(")) { const_expr(&tok, tok); tok = skip(tok, ')'); } return tok; } if (consume_attribute(&tok, tok, "destructor")) { attr->is_destructor = true; if (CONSUME(&tok, tok, "(")) { const_expr(&tok, tok); tok = skip(tok, ')'); } return tok; } if (consume_attribute(&tok, tok, "aligned")) { attr->is_aligned = true; if (CONSUME(&tok, tok, "(")) { Token *altok = tok; attr->align = const_expr(&tok, tok); if (popcnt(attr->align) != 1) error_tok(altok, "must be two power"); tok = skip(tok, ')'); } else { attr->align = 16; /* biggest alignment */ } return tok; } if (consume_attribute(&tok, tok, "warn_if_not_aligned")) { tok = skip(tok, '('); const_expr(&tok, tok); return skip(tok, ')'); } if (consume_attribute(&tok, tok, "error") || consume_attribute(&tok, tok, "warning")) { tok = skip(tok, '('); ConsumeStringLiteral(&tok, tok); return skip(tok, ')'); } if (consume_attribute(&tok, tok, "noinline") || consume_attribute(&tok, tok, "const") || consume_attribute(&tok, tok, "pure") || consume_attribute(&tok, tok, "dontclone") || consume_attribute(&tok, tok, "may_alias") || consume_attribute(&tok, tok, "warn_unused_result") || consume_attribute(&tok, tok, "flatten") || consume_attribute(&tok, tok, "leaf") || consume_attribute(&tok, tok, "no_reorder") || consume_attribute(&tok, tok, "dontthrow") || consume_attribute(&tok, tok, "optnone") || consume_attribute(&tok, tok, "returns_twice") || consume_attribute(&tok, tok, "nodebug") || consume_attribute(&tok, tok, "artificial") || consume_attribute(&tok, tok, "returns_nonnull") || consume_attribute(&tok, tok, "malloc") || consume_attribute(&tok, tok, "deprecated") || consume_attribute(&tok, tok, "gnu_inline") || consume_attribute(&tok, tok, "used") || consume_attribute(&tok, tok, "unused") || consume_attribute(&tok, tok, "no_icf") || consume_attribute(&tok, tok, "noipa") || consume_attribute(&tok, tok, "noplt") || consume_attribute(&tok, tok, "stack_protect") || consume_attribute(&tok, tok, "no_sanitize_address") || consume_attribute(&tok, tok, "no_sanitize_thread") || consume_attribute(&tok, tok, "no_split_stack") || consume_attribute(&tok, tok, "no_stack_limit") || consume_attribute(&tok, tok, "no_sanitize_undefined") || consume_attribute(&tok, tok, "no_profile_instrument_function")) { return tok; } if (consume_attribute(&tok, tok, "sentinel") || consume_attribute(&tok, tok, "nonnull") || consume_attribute(&tok, tok, "warning") || consume_attribute(&tok, tok, "optimize") || consume_attribute(&tok, tok, "target") || consume_attribute(&tok, tok, "assume_aligned") || consume_attribute(&tok, tok, "visibility") || consume_attribute(&tok, tok, "alloc_size") || consume_attribute(&tok, tok, "alloc_align")) { if (CONSUME(&tok, tok, "(")) { for (;;) { const_expr(&tok, tok); if (CONSUME(&tok, tok, ")")) break; tok = skip(tok, ','); } } return tok; } if (consume_attribute(&tok, tok, "format")) { tok = skip(tok, '('); consume_attribute(&tok, tok, "printf"); consume_attribute(&tok, tok, "scanf"); consume_attribute(&tok, tok, "strftime"); consume_attribute(&tok, tok, "strfmon"); consume_attribute(&tok, tok, "gnu_printf"); consume_attribute(&tok, tok, "gnu_scanf"); consume_attribute(&tok, tok, "gnu_strftime"); tok = skip(tok, ','); const_expr(&tok, tok); tok = skip(tok, ','); const_expr(&tok, tok); return skip(tok, ')'); } if (consume_attribute(&tok, tok, "format_arg")) { tok = skip(tok, '('); const_expr(&tok, tok); return skip(tok, ')'); } error_tok(tok, "unknown function attribute"); } // declspec = ("void" | "_Bool" | "char" | "short" | "int" | "long" // | "typedef" | "static" | "extern" | "inline" // | "_Thread_local" | "__thread" // | "signed" | "unsigned" // | struct-decl | union-decl | typedef-name // | enum-specifier | typeof-specifier // | "const" | "volatile" | "auto" | "register" | "restrict" // | "__restrict" | "__restrict__" | "_Noreturn")+ // // The order of typenames in a type-specifier doesn't matter. For // example, `int long static` means the same as `static long int`. // That can also be written as `static long` because you can omit // `int` if `long` or `short` are specified. However, something like // `char int` is not a valid type specifier. We have to accept only a // limited combinations of the typenames. // // In this function, we count the number of occurrences of each typename // while keeping the "current" type object that the typenames up // until that point represent. When we reach a non-typename token, // we returns the current type object. static Type *declspec(Token **rest, Token *tok, VarAttr *attr) { // We use a single integer as counters for all typenames. // For example, bits 0 and 1 represents how many times we saw the // keyword "void" so far. With this, we can use a switch statement // as you can see below. enum { VOID = 1 << 0, BOOL = 1 << 2, CHAR = 1 << 4, SHORT = 1 << 6, INT = 1 << 8, LONG = 1 << 10, FLOAT = 1 << 12, DOUBLE = 1 << 14, OTHER = 1 << 16, SIGNED = 1 << 17, UNSIGNED = 1 << 18, INT128 = 1 << 19, }; unsigned char kw; Type *ty = copy_type(ty_int); int counter = 0; bool is_const = false; bool is_atomic = false; while (is_typename(tok)) { if ((kw = GetKw(tok->loc, tok->len))) { // Handle storage class specifiers. if (kw == KW_TYPEDEF || kw == KW_STATIC || kw == KW_EXTERN || kw == KW_INLINE || kw == KW__THREAD_LOCAL) { if (!attr) error_tok(tok, "storage class specifier is not allowed in this context"); if (kw == KW_TYPEDEF) { attr->is_typedef = true; } else if (kw == KW_STATIC) { attr->is_static = true; } else if (kw == KW_EXTERN) { attr->is_extern = true; } else if (kw == KW_INLINE) { attr->is_inline = true; } else { attr->is_tls = true; } if (attr->is_typedef && attr->is_static + attr->is_extern + attr->is_inline + attr->is_tls > 1) { error_tok(tok, "typedef may not be used together with static," " extern, inline, __thread or _Thread_local"); } tok = tok->next; goto Continue; } if (kw == KW__NORETURN) { if (attr) attr->is_noreturn = true; tok = tok->next; goto Continue; } if (kw == KW_CONST) { is_const = true; tok = tok->next; goto Continue; } // These keywords are recognized but ignored. if (kw == KW_VOLATILE || kw == KW_AUTO || kw == KW_REGISTER || kw == KW_RESTRICT) { tok = tok->next; goto Continue; } if (kw == KW__ATOMIC) { tok = tok->next; if (EQUAL(tok, "(")) { ty = typename(&tok, tok->next); tok = skip(tok, ')'); } is_atomic = true; goto Continue; } if (kw == KW__ALIGNAS) { if (!attr) error_tok(tok, "_Alignas is not allowed in this context"); tok = skip(tok->next, '('); if (is_typename(tok)) { attr->align = typename(&tok, tok)->align; } else { Token *altok = tok; attr->align = const_expr(&tok, tok); if (popcnt(ty->align) != 1) { error_tok(altok, "_Alignas needs two power"); } } tok = skip(tok, ')'); goto Continue; } } // Handle user-defined types. Type *ty2 = find_typedef(tok); if (ty2 || kw == KW_STRUCT || kw == KW_UNION || kw == KW_ENUM || kw == KW_TYPEOF) { if (counter) break; if (kw == KW_STRUCT) { ty = struct_decl(&tok, tok->next); } else if (kw == KW_UNION) { ty = union_decl(&tok, tok->next); } else if (kw == KW_ENUM) { ty = enum_specifier(&tok, tok->next); } else if (kw == KW_TYPEOF) { ty = typeof_specifier(&tok, tok->next); } else { ty = ty2; tok = tok->next; } counter += OTHER; goto Continue; } // Handle built-in types. if (kw == KW_VOID) { counter += VOID; } else if (kw == KW__BOOL) { counter += BOOL; } else if (kw == KW_CHAR) { counter += CHAR; } else if (kw == KW_SHORT) { counter += SHORT; } else if (kw == KW_INT) { counter += INT; } else if (kw == KW_LONG) { counter += LONG; } else if (kw == KW___INT128) { counter += INT128; } else if (kw == KW_FLOAT) { counter += FLOAT; } else if (kw == KW_DOUBLE) { counter += DOUBLE; } else if (kw == KW_SIGNED) { counter |= SIGNED; } else if (kw == KW_UNSIGNED) { counter |= UNSIGNED; } else { UNREACHABLE(); } switch (counter) { case VOID: ty = copy_type(ty_void); break; case BOOL: ty = copy_type(ty_bool); break; case CHAR: case SIGNED + CHAR: ty = copy_type(ty_char); break; case UNSIGNED + CHAR: ty = copy_type(ty_uchar); break; case SHORT: case SHORT + INT: case SIGNED + SHORT: case SIGNED + SHORT + INT: ty = copy_type(ty_short); break; case UNSIGNED + SHORT: case UNSIGNED + SHORT + INT: ty = copy_type(ty_ushort); break; case INT: case SIGNED: case SIGNED + INT: ty = copy_type(ty_int); break; case UNSIGNED: case UNSIGNED + INT: ty = copy_type(ty_uint); break; case LONG: case LONG + INT: case LONG + LONG: case LONG + LONG + INT: case SIGNED + LONG: case SIGNED + LONG + INT: case SIGNED + LONG + LONG: case SIGNED + LONG + LONG + INT: ty = copy_type(ty_long); break; case UNSIGNED + LONG: case UNSIGNED + LONG + INT: case UNSIGNED + LONG + LONG: case UNSIGNED + LONG + LONG + INT: ty = copy_type(ty_ulong); break; case INT128: case SIGNED + INT128: ty = copy_type(ty_int128); break; case UNSIGNED + INT128: ty = copy_type(ty_uint128); break; case FLOAT: ty = copy_type(ty_float); break; case DOUBLE: ty = copy_type(ty_double); break; case LONG + DOUBLE: ty = copy_type(ty_ldouble); break; default: error_tok(tok, "invalid type"); } tok = tok->next; Continue: if (attr && attr->is_typedef) { tok = attribute_list(tok, ty, type_attributes); } else if (attr) { tok = attribute_list(tok, attr, thing_attributes); } } if (is_atomic) { ty = copy_type(ty); ty->is_atomic = true; } if (is_const) { ty = copy_type(ty); ty->is_const = true; } *rest = tok; return ty; } static Token *static_assertion(Token *tok) { char *msg; Token *start = tok; tok = skip(tok->next, '('); int64_t cond = const_expr(&tok, tok); if (CONSUME(&tok, tok, ",")) { msg = ConsumeStringLiteral(&tok, tok); } else { msg = "assertion failed"; } tok = skip(tok, ')'); tok = skip(tok, ';'); if (!cond) { error_tok(start, "%s", msg); } return tok; } // func-params = ("void" | param ("," param)* ("," "...")?)? ")" // param = declspec declarator static Type *func_params(Token **rest, Token *tok, Type *ty) { if (EQUAL(tok, "void") && EQUAL(tok->next, ")")) { *rest = tok->next->next; return func_type(ty); } Type head = {}; Type *cur = &head; bool is_variadic = false; enter_scope(); while (!EQUAL(tok, ")")) { if (cur != &head) tok = skip(tok, ','); if (EQUAL(tok, "...")) { is_variadic = true; tok = tok->next; skip(tok, ')'); break; } Type *ty2 = declspec(&tok, tok, NULL); ty2 = declarator(&tok, tok, ty2); Token *name = ty2->name; if (name) { new_lvar(strndup(name->loc, name->len), ty2); } if (ty2->kind == TY_ARRAY) { // "array of T" decays to "pointer to T" only in the parameter // context. For example, *argv[] is converted to **argv here. Type *ty3 = ty2; ty3 = pointer_to(ty2->base); ty3->name = name; ty3->array_len = ty2->array_len; ty3->is_static = ty2->is_static; ty3->is_restrict = ty2->is_restrict; ty2 = ty3; } else if (ty2->kind == TY_FUNC) { // Likewise, a function is converted to a pointer to a function // only in the parameter context. ty2 = pointer_to(ty2); ty2->name = name; } cur = cur->next = copy_type(ty2); } leave_scope(); if (cur == &head) is_variadic = true; ty = func_type(ty); ty->params = head.next; ty->is_variadic = is_variadic; *rest = tok->next; return ty; } // array-dimensions = ("static" | "restrict")* const-expr? "]" type-suffix static Type *array_dimensions(Token **rest, Token *tok, Type *ty) { Node *expr; bool is_static, is_restrict; is_static = false; is_restrict = false; for (;; tok = tok->next) { if (EQUAL(tok, "static")) { is_static = true; } else if (EQUAL(tok, "restrict")) { is_restrict = true; } else { break; } } if (EQUAL(tok, "]")) { ty = type_suffix(rest, tok->next, ty); ty = array_of(ty, -1); } else { expr = conditional(&tok, tok); tok = skip(tok, ']'); ty = type_suffix(rest, tok, ty); if (ty->kind == TY_VLA || !is_const_expr(expr)) return vla_of(ty, expr); ty = array_of(ty, eval(expr)); } ty->is_static = is_static; ty->is_restrict = is_restrict; return ty; } // type-suffix = "(" func-params // | "[" array-dimensions // | ε static Type *type_suffix(Token **rest, Token *tok, Type *ty) { if (EQUAL(tok, "(")) return func_params(rest, tok->next, ty); if (EQUAL(tok, "[")) return array_dimensions(rest, tok->next, ty); *rest = tok; return ty; } // pointers = ("*" ("const" | "volatile" | "restrict" | attribute)*)* static Type *pointers(Token **rest, Token *tok, Type *ty) { while (CONSUME(&tok, tok, "*")) { ty = pointer_to(ty); for (;;) { tok = attribute_list(tok, ty, type_attributes); if (EQUAL(tok, "const")) { ty->is_const = true; tok = tok->next; } else if (EQUAL(tok, "volatile")) { ty->is_volatile = true; tok = tok->next; } else if (EQUAL(tok, "restrict") || EQUAL(tok, "__restrict") || EQUAL(tok, "__restrict__")) { ty->is_restrict = true; tok = tok->next; } else { break; } } } *rest = tok; return ty; } // declarator = pointers ("(" ident ")" | "(" declarator ")" | ident) // type-suffix static Type *declarator(Token **rest, Token *tok, Type *ty) { ty = pointers(&tok, tok, ty); if (EQUAL(tok, "(")) { Token *start = tok; Type dummy = {}; declarator(&tok, start->next, &dummy); tok = skip(tok, ')'); ty = type_suffix(rest, tok, ty); ty = declarator(&tok, start->next, ty); return ty; } Token *name = NULL; Token *name_pos = tok; if (tok->kind == TK_IDENT) { name = tok; tok = tok->next; } ty = type_suffix(rest, tok, ty); ty->name = name; ty->name_pos = name_pos; return ty; } // abstract-declarator = pointers ("(" abstract-declarator ")")? type-suffix static Type *abstract_declarator(Token **rest, Token *tok, Type *ty) { ty = pointers(&tok, tok, ty); if (EQUAL(tok, "(")) { Token *start = tok; Type dummy = {}; abstract_declarator(&tok, start->next, &dummy); tok = skip(tok, ')'); ty = type_suffix(rest, tok, ty); return abstract_declarator(&tok, start->next, ty); } return type_suffix(rest, tok, ty); } // type-name = declspec abstract-declarator static Type *typename(Token **rest, Token *tok) { Type *ty = declspec(&tok, tok, NULL); return abstract_declarator(rest, tok, ty); } static bool is_end(Token *tok) { return EQUAL(tok, "}") || (EQUAL(tok, ",") && EQUAL(tok->next, "}")); } static bool consume_end(Token **rest, Token *tok) { if (EQUAL(tok, "}")) { *rest = tok->next; return true; } if (EQUAL(tok, ",") && EQUAL(tok->next, "}")) { *rest = tok->next->next; return true; } return false; } // enum-specifier = ident? "{" enum-list? "}" // | ident ("{" enum-list? "}")? // // enum-list = ident ("=" num)? ("," ident ("=" num)?)* ","? static Type *enum_specifier(Token **rest, Token *tok) { Type *ty = enum_type(); // Read a struct tag. Token *tag = NULL; if (tok->kind == TK_IDENT) { tag = tok; tok = tok->next; } if (tag && !EQUAL(tok, "{")) { Type *ty = find_tag(tag); if (!ty) error_tok(tag, "unknown enum type"); if (ty->kind != TY_ENUM) error_tok(tag, "not an enum tag"); *rest = tok; return ty; } ty->name = tag; tok = skip(tok, '{'); // Read an enum-list. int i = 0; int val = 0; while (!consume_end(rest, tok)) { if (i++ > 0) tok = skip(tok, ','); if (tok->kind == TK_JAVADOWN) { current_javadown = tok; tok = tok->next; } char *name = get_ident(tok); tok = tok->next; if (EQUAL(tok, "=")) val = const_expr(&tok, tok->next); VarScope *sc = push_scope(name); sc->enum_ty = ty; sc->enum_val = val++; } if (tag) push_tag_scope(tag, ty); return ty; } // typeof-specifier = "(" (expr | typename) ")" static Type *typeof_specifier(Token **rest, Token *tok) { tok = skip(tok, '('); Type *ty; if (is_typename(tok)) { ty = typename(&tok, tok); } else { Node *node = expr(&tok, tok); add_type(node); ty = node->ty; } *rest = skip(tok, ')'); return ty; } // Generate code for computing a VLA size. static Node *compute_vla_size(Type *ty, Token *tok) { Node *node = new_node(ND_NULL_EXPR, tok); if (ty->base) { node = new_binary(ND_COMMA, node, compute_vla_size(ty->base, tok), tok); } if (ty->kind != TY_VLA) return node; Node *base_sz; if (ty->base->kind == TY_VLA) { base_sz = new_var_node(ty->base->vla_size, tok); } else { base_sz = new_num(ty->base->size, tok); } ty->vla_size = new_lvar("", ty_ulong); Node *expr = new_binary(ND_ASSIGN, new_var_node(ty->vla_size, tok), new_binary(ND_MUL, ty->vla_len, base_sz, tok), tok); return new_binary(ND_COMMA, node, expr, tok); } static Node *new_alloca(Node *sz) { Node *node; node = new_unary(ND_FUNCALL, new_var_node(builtin_alloca, sz->tok), sz->tok); node->func_ty = builtin_alloca->ty; node->ty = builtin_alloca->ty->return_ty; node->args = sz; add_type(sz); return node; } // declaration = declspec (declarator ("=" expr)? // ("," declarator ("=" expr)?)*)? ";" static Node *declaration(Token **rest, Token *tok, Type *basety, VarAttr *attr) { Node head = {}; Node *cur = &head; int i = 0; while (!EQUAL(tok, ";")) { if (i++ > 0) tok = skip(tok, ','); Type *ty = declarator(&tok, tok, basety); if (ty->kind == TY_VOID) error_tok(tok, "variable declared void"); if (!ty->name) error_tok(ty->name_pos, "variable name omitted"); if (attr && attr->is_static) { // static local variable Obj *var = new_anon_gvar(ty); push_scope(get_ident(ty->name))->var = var; if (EQUAL(tok, "=")) gvar_initializer(&tok, tok->next, var); continue; } // Generate code for computing a VLA size. We need to do this // even if ty is not VLA because ty may be a pointer to VLA // (e.g. int (*foo)[n][m] where n and m are variables.) cur = cur->next = new_unary(ND_EXPR_STMT, compute_vla_size(ty, tok), tok); tok = attribute_list(tok, attr, thing_attributes); if (ty->kind == TY_VLA) { if (EQUAL(tok, "=")) error_tok(tok, "variable-sized object may not be initialized"); // Variable length arrays (VLAs) are translated to alloca() calls. // For example, `int x[n+2]` is translated to `tmp = n + 2, // x = alloca(tmp)`. Obj *var = new_lvar(get_ident(ty->name), ty); Token *tok = ty->name; Node *expr = new_binary(ND_ASSIGN, new_vla_ptr(var, tok), new_alloca(new_var_node(ty->vla_size, tok)), tok); cur = cur->next = new_unary(ND_EXPR_STMT, expr, tok); continue; } Obj *var = new_lvar(get_ident(ty->name), ty); if (attr && attr->align) var->align = attr->align; if (EQUAL(tok, "=")) { Node *expr = lvar_initializer(&tok, tok->next, var); cur = cur->next = new_unary(ND_EXPR_STMT, expr, tok); } if (var->ty->size < 0) error_tok(ty->name, "variable has incomplete type"); if (var->ty->kind == TY_VOID) error_tok(ty->name, "variable declared void"); } Node *node = new_node(ND_BLOCK, tok); node->body = head.next; *rest = tok->next; return node; } static Token *skip_excess_element(Token *tok) { if (EQUAL(tok, "{")) { tok = skip_excess_element(tok->next); return skip(tok, '}'); } assign(&tok, tok); return tok; } // string-initializer = string-literal static void string_initializer(Token **rest, Token *tok, Initializer *init) { if (init->is_flexible) { *init = *new_initializer(array_of(init->ty->base, tok->ty->array_len), false); } int len = MIN(init->ty->array_len, tok->ty->array_len); switch (init->ty->base->size) { case 1: { char *str = tok->str; for (int i = 0; i < len; i++) init->children[i]->expr = new_num(str[i], tok); break; } case 2: { uint16_t *str = (uint16_t *)tok->str; for (int i = 0; i < len; i++) init->children[i]->expr = new_num(str[i], tok); break; } case 4: { uint32_t *str = (uint32_t *)tok->str; for (int i = 0; i < len; i++) init->children[i]->expr = new_num(str[i], tok); break; } default: UNREACHABLE(); } *rest = tok->next; } // array-designator = "[" const-expr "]" // // C99 added the designated initializer to the language, which allows // programmers to move the "cursor" of an initializer to any element. // The syntax looks like this: // // int x[10] = { 1, 2, [5]=3, 4, 5, 6, 7 }; // // `[5]` moves the cursor to the 5th element, so the 5th element of x // is set to 3. Initialization then continues forward in order, so // 6th, 7th, 8th and 9th elements are initialized with 4, 5, 6 and 7, // respectively. Unspecified elements (in this case, 3rd and 4th // elements) are initialized with zero. // // Nesting is allowed, so the following initializer is valid: // // int x[5][10] = { [5][8]=1, 2, 3 }; // // It sets x[5][8], x[5][9] and x[6][0] to 1, 2 and 3, respectively. // // Use `.fieldname` to move the cursor for a struct initializer. E.g. // // struct { int a, b, c; } x = { .c=5 }; // // The above initializer sets x.c to 5. static void array_designator(Token **rest, Token *tok, Type *ty, int *begin, int *end) { *begin = const_expr(&tok, tok->next); if (*begin >= ty->array_len) error_tok(tok, "array designator index exceeds array bounds"); if (EQUAL(tok, "...")) { *end = const_expr(&tok, tok->next); if (*end >= ty->array_len) error_tok(tok, "array designator index exceeds array bounds"); if (*end < *begin) error_tok(tok, "array designator range [%d, %d] is empty", *begin, *end); } else { *end = *begin; } *rest = skip(tok, ']'); } // struct-designator = "." ident static Member *struct_designator(Token **rest, Token *tok, Type *ty) { Token *start = tok; tok = skip(tok, '.'); if (tok->kind == TK_JAVADOWN) { current_javadown = tok; tok = tok->next; } if (tok->kind != TK_IDENT) error_tok(tok, "expected a field designator"); for (Member *mem = ty->members; mem; mem = mem->next) { // Anonymous struct member if ((mem->ty->kind == TY_STRUCT || mem->ty->kind == TY_UNION) && !mem->name) { if (get_struct_member(mem->ty, tok)) { *rest = start; return mem; } continue; } // Regular struct member if (mem->name->len == tok->len && !strncmp(mem->name->loc, tok->loc, tok->len)) { *rest = tok->next; return mem; } } error_tok(tok, "struct has no such member"); } // designation = ("[" const-expr "]" | "." ident)* "="? initializer static void designation(Token **rest, Token *tok, Initializer *init) { if (EQUAL(tok, "[")) { if (init->ty->kind != TY_ARRAY) error_tok(tok, "array index in non-array initializer"); int begin, end; array_designator(&tok, tok, init->ty, &begin, &end); Token *tok2; for (int i = begin; i <= end; i++) designation(&tok2, tok, init->children[i]); array_initializer2(rest, tok2, init, begin + 1); return; } if (EQUAL(tok, ".") && init->ty->kind == TY_STRUCT) { Member *mem = struct_designator(&tok, tok, init->ty); designation(&tok, tok, init->children[mem->idx]); init->expr = NULL; struct_initializer2(rest, tok, init, mem->next); return; } if (EQUAL(tok, ".") && init->ty->kind == TY_UNION) { Member *mem = struct_designator(&tok, tok, init->ty); init->mem = mem; designation(rest, tok, init->children[mem->idx]); return; } if (EQUAL(tok, ".")) { error_tok(tok, "field name not in struct or union initializer"); } if (EQUAL(tok, "=")) tok = tok->next; initializer2(rest, tok, init); } // An array length can be omitted if an array has an initializer // (e.g. `int x[] = {1,2,3}`). If it's omitted, count the number // of initializer elements. static int count_array_init_elements(Token *tok, Type *ty) { bool first = true; Initializer *dummy = new_initializer(ty->base, true); int i = 0, max = 0; while (!consume_end(&tok, tok)) { if (!first) tok = skip(tok, ','); first = false; if (EQUAL(tok, "[")) { i = const_expr(&tok, tok->next); if (EQUAL(tok, "...")) i = const_expr(&tok, tok->next); tok = skip(tok, ']'); designation(&tok, tok, dummy); } else { initializer2(&tok, tok, dummy); } i++; max = MAX(max, i); } return max; } // array-initializer1 = "{" initializer ("," initializer)* ","? "}" static void array_initializer1(Token **rest, Token *tok, Initializer *init) { tok = skip(tok, '{'); if (init->is_flexible) { int len = count_array_init_elements(tok, init->ty); *init = *new_initializer(array_of(init->ty->base, len), false); } bool first = true; if (init->is_flexible) { int len = count_array_init_elements(tok, init->ty); *init = *new_initializer(array_of(init->ty->base, len), false); } for (int i = 0; !consume_end(rest, tok); i++) { if (!first) tok = skip(tok, ','); first = false; if (EQUAL(tok, "[")) { int begin, end; array_designator(&tok, tok, init->ty, &begin, &end); Token *tok2; for (int j = begin; j <= end; j++) designation(&tok2, tok, init->children[j]); tok = tok2; i = end; continue; } if (i < init->ty->array_len) { initializer2(&tok, tok, init->children[i]); } else { tok = skip_excess_element(tok); } } } // array-initializer2 = initializer (',' initializer)* static void array_initializer2(Token **rest, Token *tok, Initializer *init, int i) { if (init->is_flexible) { int len = count_array_init_elements(tok, init->ty); *init = *new_initializer(array_of(init->ty->base, len), false); } for (; i < init->ty->array_len && !is_end(tok); i++) { Token *start = tok; if (i > 0) tok = skip(tok, ','); if (EQUAL(tok, "[") || EQUAL(tok, ".")) { *rest = start; return; } initializer2(&tok, tok, init->children[i]); } *rest = tok; } // struct-initializer1 = "{" initializer ("," initializer)* ","? "}" static void struct_initializer1(Token **rest, Token *tok, Initializer *init) { tok = skip(tok, '{'); Member *mem = init->ty->members; bool first = true; while (!consume_end(rest, tok)) { if (!first) tok = skip(tok, ','); first = false; if (EQUAL(tok, ".")) { mem = struct_designator(&tok, tok, init->ty); designation(&tok, tok, init->children[mem->idx]); mem = mem->next; continue; } if (mem) { initializer2(&tok, tok, init->children[mem->idx]); mem = mem->next; } else { tok = skip_excess_element(tok); } } } // struct-initializer2 = initializer ("," initializer)* static void struct_initializer2(Token **rest, Token *tok, Initializer *init, Member *mem) { bool first = true; for (; mem && !is_end(tok); mem = mem->next) { Token *start = tok; if (!first) tok = skip(tok, ','); first = false; if (EQUAL(tok, "[") || EQUAL(tok, ".")) { *rest = start; return; } initializer2(&tok, tok, init->children[mem->idx]); } *rest = tok; } static void union_initializer(Token **rest, Token *tok, Initializer *init) { // Unlike structs, union initializers take only one initializer, // and that initializes the first union member by default. // You can initialize other member using a designated initializer. if (EQUAL(tok, "{") && EQUAL(tok->next, ".")) { Member *mem = struct_designator(&tok, tok->next, init->ty); init->mem = mem; designation(&tok, tok, init->children[mem->idx]); *rest = skip(tok, '}'); return; } init->mem = init->ty->members; if (EQUAL(tok, "{")) { initializer2(&tok, tok->next, init->children[0]); CONSUME(&tok, tok, ","); *rest = skip(tok, '}'); } else { initializer2(rest, tok, init->children[0]); } } // initializer = string-initializer | array-initializer // | struct-initializer | union-initializer // | assign static void initializer2(Token **rest, Token *tok, Initializer *init) { if (EQUAL(tok, "(") && init->ty->kind == TY_ARRAY && tok->next->kind == TK_STR) { /* XXX: Kludge so typeof("str") s = ("str"); macros work. */ initializer2(rest, tok->next, init); *rest = skip(*rest, ')'); return; } if (init->ty->kind == TY_ARRAY && tok->kind == TK_STR) { string_initializer(rest, tok, init); return; } if (init->ty->kind == TY_ARRAY) { if (EQUAL(tok, "{")) { array_initializer1(rest, tok, init); } else { array_initializer2(rest, tok, init, 0); } return; } if (init->ty->kind == TY_STRUCT) { if (EQUAL(tok, "{")) { struct_initializer1(rest, tok, init); return; } // A struct can be initialized with another struct. E.g. // `struct T x = y;` where y is a variable of type `struct T`. // Handle that case first. Node *expr = assign(rest, tok); add_type(expr); if (expr->ty->kind == TY_STRUCT) { init->expr = expr; return; } struct_initializer2(rest, tok, init, init->ty->members); return; } if (init->ty->kind == TY_UNION) { union_initializer(rest, tok, init); return; } if (EQUAL(tok, "{")) { // An initializer for a scalar variable can be surrounded by // braces. E.g. `int x = {3};`. Handle that case. initializer2(&tok, tok->next, init); *rest = skip(tok, '}'); return; } init->expr = assign(rest, tok); } static Type *copy_struct_type(Type *ty) { ty = copy_type(ty); Member head = {}; Member *cur = &head; for (Member *mem = ty->members; mem; mem = mem->next) { Member *m = calloc(1, sizeof(Member)); *m = *mem; cur = cur->next = m; } ty->members = head.next; return ty; } static Initializer *initializer(Token **rest, Token *tok, Type *ty, Type **new_ty) { Initializer *init = new_initializer(ty, true); initializer2(rest, tok, init); if ((ty->kind == TY_STRUCT || ty->kind == TY_UNION) && ty->is_flexible) { ty = copy_struct_type(ty); Member *mem = ty->members; while (mem->next) mem = mem->next; mem->ty = init->children[mem->idx]->ty; ty->size += mem->ty->size; *new_ty = ty; return init; } *new_ty = init->ty; return init; } static Node *init_desg_expr(InitDesg *desg, Token *tok) { if (desg->var) return new_var_node(desg->var, tok); if (desg->member) { Node *node = new_unary(ND_MEMBER, init_desg_expr(desg->next, tok), tok); node->member = desg->member; return node; } Node *lhs = init_desg_expr(desg->next, tok); Node *rhs = new_num(desg->idx, tok); return new_unary(ND_DEREF, new_add(lhs, rhs, tok), tok); } static Node *create_lvar_init(Initializer *init, Type *ty, InitDesg *desg, Token *tok) { if (ty->kind == TY_ARRAY) { Node *node = new_node(ND_NULL_EXPR, tok); for (int i = 0; i < ty->array_len; i++) { InitDesg desg2 = {desg, i}; Node *rhs = create_lvar_init(init->children[i], ty->base, &desg2, tok); node = new_binary(ND_COMMA, node, rhs, tok); } return node; } if (ty->kind == TY_STRUCT && !init->expr) { Node *node = new_node(ND_NULL_EXPR, tok); for (Member *mem = ty->members; mem; mem = mem->next) { InitDesg desg2 = {desg, 0, mem}; Node *rhs = create_lvar_init(init->children[mem->idx], mem->ty, &desg2, tok); node = new_binary(ND_COMMA, node, rhs, tok); } return node; } if (ty->kind == TY_UNION) { Member *mem = init->mem ? init->mem : ty->members; InitDesg desg2 = {desg, 0, mem}; return create_lvar_init(init->children[mem->idx], mem->ty, &desg2, tok); } if (!init->expr) return new_node(ND_NULL_EXPR, tok); Node *lhs = init_desg_expr(desg, tok); return new_binary(ND_ASSIGN, lhs, init->expr, tok); } // A variable definition with an initializer is a shorthand notation // for a variable definition followed by assignments. This function // generates assignment expressions for an initializer. For example, // `int x[2][2] = {{6, 7}, {8, 9}}` is converted to the following // expressions: // // x[0][0] = 6; // x[0][1] = 7; // x[1][0] = 8; // x[1][1] = 9; // static Node *lvar_initializer(Token **rest, Token *tok, Obj *var) { Initializer *init = initializer(rest, tok, var->ty, &var->ty); InitDesg desg = {NULL, 0, NULL, var}; // If a partial initializer list is given, the standard requires // that unspecified elements are set to 0. Here, we simply // zero-initialize the entire memory region of a variable before // initializing it with user-supplied values. Node *lhs = new_node(ND_MEMZERO, tok); lhs->var = var; Node *rhs = create_lvar_init(init, var->ty, &desg, tok); return new_binary(ND_COMMA, lhs, rhs, tok); } static uint64_t read_buf(char *buf, int sz) { if (sz == 1) return *buf; if (sz == 2) return *(uint16_t *)buf; if (sz == 4) return *(uint32_t *)buf; if (sz == 8) return *(uint64_t *)buf; UNREACHABLE(); } static void write_buf(char *buf, uint64_t val, int sz) { if (sz == 1) { *buf = val; } else if (sz == 2) { *(uint16_t *)buf = val; } else if (sz == 4) { *(uint32_t *)buf = val; } else if (sz == 8) { *(uint64_t *)buf = val; } else { UNREACHABLE(); } } static Relocation *write_gvar_data(Relocation *cur, Initializer *init, Type *ty, char *buf, int offset) { if (ty->kind == TY_ARRAY) { int sz = ty->base->size; for (int i = 0; i < ty->array_len; i++) cur = write_gvar_data(cur, init->children[i], ty->base, buf, offset + sz * i); return cur; } if (ty->kind == TY_STRUCT) { for (Member *mem = ty->members; mem; mem = mem->next) { if (mem->is_bitfield) { Node *expr = init->children[mem->idx]->expr; if (!expr) break; char *loc = buf + offset + mem->offset; uint64_t oldval = read_buf(loc, mem->ty->size); uint64_t newval = eval(expr); uint64_t mask = (1L << mem->bit_width) - 1; uint64_t combined = oldval | ((newval & mask) << mem->bit_offset); write_buf(loc, combined, mem->ty->size); } else { cur = write_gvar_data(cur, init->children[mem->idx], mem->ty, buf, offset + mem->offset); } } return cur; } if (ty->kind == TY_UNION) { if (!init->mem) return cur; return write_gvar_data(cur, init->children[init->mem->idx], init->mem->ty, buf, offset); } if (!init->expr) return cur; if (ty->kind == TY_FLOAT) { *(float *)(buf + offset) = eval_double(init->expr); return cur; } if (ty->kind == TY_DOUBLE) { *(double *)(buf + offset) = eval_double(init->expr); return cur; } char **label = NULL; uint64_t val = eval2(init->expr, &label); if (!label) { write_buf(buf + offset, val, ty->size); return cur; } Relocation *rel = calloc(1, sizeof(Relocation)); rel->offset = offset; rel->label = label; rel->addend = val; cur->next = rel; return cur->next; } // Initializers for global variables are evaluated at compile-time and // embedded to .data section. This function serializes Initializer // objects to a flat byte array. It is a compile error if an // initializer list contains a non-constant expression. static void gvar_initializer(Token **rest, Token *tok, Obj *var) { Initializer *init = initializer(rest, tok, var->ty, &var->ty); Relocation head = {}; char *buf = calloc(1, var->ty->size); write_gvar_data(&head, init, var->ty, buf, 0); var->init_data = buf; var->rel = head.next; } // Returns true if a given token represents a type. static bool is_typename(Token *tok) { unsigned char kw; kw = GetKw(tok->loc, tok->len); return (kw && !(kw & -32)) || find_typedef(tok); } static bool is_const_expr_true(Node *node) { if (is_flonum(node->ty)) { return !!eval_double(node); } else { return eval(node); } } // stmt = "return" expr? ";" // | "if" "(" expr ")" stmt ("else" stmt)? // | "switch" "(" expr ")" stmt // | "case" const-expr ("..." const-expr)? ":" stmt // | "default" ":" stmt // | "for" "(" expr-stmt expr? ";" expr? ")" stmt // | "while" "(" expr ")" stmt // | "do" stmt "while" "(" expr ")" ";" // | "asm" asm-stmt // | "goto" (ident | "*" expr) ";" // | "break" ";" // | "continue" ";" // | ident ":" stmt // | "{" compound-stmt // | expr-stmt static Node *stmt(Token **rest, Token *tok) { if (EQUAL(tok, "return")) { Node *node = new_node(ND_RETURN, tok); if (CONSUME(rest, tok->next, ";")) return node; Node *exp = expr(&tok, tok->next); *rest = skip(tok, ';'); add_type(exp); Type *ty = current_fn->ty->return_ty; if (ty->kind != TY_STRUCT && ty->kind != TY_UNION) exp = new_cast(exp, current_fn->ty->return_ty); node->lhs = exp; return node; } if (EQUAL(tok, "if")) { Node *node = new_node(ND_IF, tok); tok = skip(tok->next, '('); node->cond = expr(&tok, tok); tok = skip(tok, ')'); node->then = stmt(&tok, tok); if (EQUAL(tok, "else")) node->els = stmt(&tok, tok->next); *rest = tok; if (is_const_expr(node->cond)) { if (is_const_expr_true(node->cond)) { /* DCE */ return node->then; } else if (node->els) { return node->els; } else { return new_node(ND_BLOCK, node->tok); } } return node; } if (EQUAL(tok, "_Static_assert")) { Token *start = tok; *rest = static_assertion(tok); return new_node(ND_BLOCK, start); } if (EQUAL(tok, "switch")) { Node *node = new_node(ND_SWITCH, tok); tok = skip(tok->next, '('); node->cond = expr(&tok, tok); tok = skip(tok, ')'); Node *sw = current_switch; current_switch = node; char *brk = brk_label; brk_label = node->brk_label = new_unique_name(); node->then = stmt(rest, tok); current_switch = sw; brk_label = brk; return node; } if (EQUAL(tok, "case")) { if (!current_switch) error_tok(tok, "stray case"); Node *node = new_node(ND_CASE, tok); int begin = const_expr(&tok, tok->next); int end; if (EQUAL(tok, "...")) { // [GNU] Case ranges, e.g. "case 1 ... 5:" end = const_expr(&tok, tok->next); if (end < begin) error_tok(tok, "empty case range specified"); } else { end = begin; } tok = skip(tok, ':'); node->label = new_unique_name(); node->lhs = stmt(rest, tok); node->begin = begin; node->end = end; node->case_next = current_switch->case_next; current_switch->case_next = node; return node; } if (EQUAL(tok, "default")) { if (!current_switch) error_tok(tok, "stray default"); Node *node = new_node(ND_CASE, tok); tok = skip(tok->next, ':'); node->label = new_unique_name(); node->lhs = stmt(rest, tok); current_switch->default_case = node; return node; } if (EQUAL(tok, "for")) { Node *node = new_node(ND_FOR, tok); tok = skip(tok->next, '('); enter_scope(); char *brk = brk_label; char *cont = cont_label; brk_label = node->brk_label = new_unique_name(); cont_label = node->cont_label = new_unique_name(); if (is_typename(tok)) { Type *basety = declspec(&tok, tok, NULL); node->init = declaration(&tok, tok, basety, NULL); } else { node->init = expr_stmt(&tok, tok); } if (!EQUAL(tok, ";")) node->cond = expr(&tok, tok); tok = skip(tok, ';'); if (!EQUAL(tok, ")")) node->inc = expr(&tok, tok); tok = skip(tok, ')'); node->then = stmt(rest, tok); leave_scope(); brk_label = brk; cont_label = cont; return node; } if (EQUAL(tok, "while")) { Node *node = new_node(ND_FOR, tok); tok = skip(tok->next, '('); node->cond = expr(&tok, tok); tok = skip(tok, ')'); char *brk = brk_label; char *cont = cont_label; brk_label = node->brk_label = new_unique_name(); cont_label = node->cont_label = new_unique_name(); node->then = stmt(rest, tok); brk_label = brk; cont_label = cont; return node; } if (EQUAL(tok, "do")) { Node *node = new_node(ND_DO, tok); char *brk = brk_label; char *cont = cont_label; brk_label = node->brk_label = new_unique_name(); cont_label = node->cont_label = new_unique_name(); node->then = stmt(&tok, tok->next); brk_label = brk; cont_label = cont; if (!EQUAL(tok, "while")) { error_tok(tok, "expected while"); } tok = skip(tok->next, '('); node->cond = expr(&tok, tok); tok = skip(tok, ')'); *rest = skip(tok, ';'); return node; } if (EQUAL(tok, "asm") || EQUAL(tok, "__asm__")) { Node *node = new_node(ND_ASM, tok); node->azm = asm_stmt(rest, tok); return node; } if (EQUAL(tok, "goto")) { if (EQUAL(tok->next, "*")) { // [GNU] `goto *ptr` jumps to the address specified by `ptr`. Node *node = new_node(ND_GOTO_EXPR, tok); node->lhs = expr(&tok, tok->next->next); *rest = skip(tok, ';'); return node; } Node *node = new_node(ND_GOTO, tok); node->label = get_ident(tok->next); node->goto_next = gotos; gotos = node; *rest = skip(tok->next->next, ';'); return node; } if (EQUAL(tok, "break")) { if (!brk_label) error_tok(tok, "stray break"); Node *node = new_node(ND_GOTO, tok); node->unique_label = brk_label; *rest = skip(tok->next, ';'); return node; } if (EQUAL(tok, "continue")) { if (!cont_label) error_tok(tok, "stray continue"); Node *node = new_node(ND_GOTO, tok); node->unique_label = cont_label; *rest = skip(tok->next, ';'); return node; } if (tok->kind == TK_IDENT && EQUAL(tok->next, ":")) { Node *node = new_node(ND_LABEL, tok); node->label = strndup(tok->loc, tok->len); node->unique_label = new_unique_name(); node->lhs = stmt(rest, tok->next->next); node->goto_next = labels; labels = node; return node; } if (EQUAL(tok, "{")) return compound_stmt(rest, tok->next); return expr_stmt(rest, tok); } // compound-stmt = (typedef | declaration | stmt)* "}" static Node *compound_stmt(Token **rest, Token *tok) { Node *node = new_node(ND_BLOCK, tok); Node head = {}; Node *cur = &head; enter_scope(); while (!EQUAL(tok, "}")) { if (is_typename(tok) && !EQUAL(tok->next, ":")) { VarAttr attr = {}; Type *basety = declspec(&tok, tok, &attr); if (attr.is_typedef) { tok = parse_typedef(tok, basety); continue; } if (is_function(tok)) { tok = function(tok, basety, &attr); continue; } if (attr.is_extern) { tok = global_variable(tok, basety, &attr); continue; } cur = cur->next = declaration(&tok, tok, basety, &attr); } else { cur = cur->next = stmt(&tok, tok); } add_type(cur); } leave_scope(); node->body = head.next; *rest = tok->next; return node; } // expr-stmt = expr? ";" static Node *expr_stmt(Token **rest, Token *tok) { if (EQUAL(tok, ";")) { *rest = tok->next; return new_node(ND_BLOCK, tok); } Node *node = new_node(ND_EXPR_STMT, tok); node->lhs = expr(&tok, tok); *rest = skip(tok, ';'); return node; } // expr = assign ("," expr)? Node *expr(Token **rest, Token *tok) { Node *node = assign(&tok, tok); if (EQUAL(tok, ",")) { return new_binary(ND_COMMA, node, expr(rest, tok->next), tok); } *rest = tok; return node; } int64_t eval(Node *node) { return eval2(node, NULL); } // Evaluate a given node as a constant expression. // // A constant expression is either just a number or ptr+n where ptr // is a pointer to a global variable and n is a postiive/negative // number. The latter form is accepted only as an initialization // expression for a global variable. int64_t eval2(Node *node, char ***label) { int64_t x, y; add_type(node); if (is_flonum(node->ty)) return eval_double(node); switch (node->kind) { case ND_ADD: return eval2(node->lhs, label) + eval(node->rhs); case ND_SUB: return eval2(node->lhs, label) - eval(node->rhs); case ND_MUL: return eval(node->lhs) * eval(node->rhs); case ND_DIV: y = eval(node->rhs); if (!y) error_tok(node->rhs->tok, "constexpr div by zero"); if (node->ty->is_unsigned) { return (uint64_t)eval(node->lhs) / y; } x = eval(node->lhs); if (x == 0x8000000000000000 && y == -1) { error_tok(node->rhs->tok, "constexpr divide error"); } return x / y; case ND_NEG: return -eval(node->lhs); case ND_REM: y = eval(node->rhs); if (!y) error_tok(node->rhs->tok, "constexpr rem by zero"); if (node->ty->is_unsigned) { return (uint64_t)eval(node->lhs) % y; } return eval(node->lhs) % y; case ND_BINAND: return eval(node->lhs) & eval(node->rhs); case ND_BINOR: return eval(node->lhs) | eval(node->rhs); case ND_BINXOR: return eval(node->lhs) ^ eval(node->rhs); case ND_SHL: return eval(node->lhs) << eval(node->rhs); case ND_SHR: if (node->ty->is_unsigned && node->ty->size == 8) return (uint64_t)eval(node->lhs) >> eval(node->rhs); return eval(node->lhs) >> eval(node->rhs); case ND_EQ: { bool lhs = is_const_expr(node->lhs); bool rhs = is_const_expr(node->rhs); if (rhs && node->lhs->kind == ND_VAR && node->lhs->ty->kind == TY_ARRAY && node->lhs->var->is_string_literal) { return eval(node->rhs) == (intptr_t)node->lhs->var->init_data; } else if (lhs && node->rhs->kind == ND_VAR && node->rhs->ty->kind == TY_ARRAY && node->rhs->var->is_string_literal) { return eval(node->lhs) == (intptr_t)node->rhs->var->init_data; } else { return eval(node->lhs) == eval(node->rhs); } } case ND_NE: { bool lhs = is_const_expr(node->lhs); bool rhs = is_const_expr(node->rhs); if (rhs && node->lhs->kind == ND_VAR && node->lhs->ty->kind == TY_ARRAY && node->lhs->var->is_string_literal) { return eval(node->rhs) != (intptr_t)node->lhs->var->init_data; } else if (lhs && node->rhs->kind == ND_VAR && node->rhs->ty->kind == TY_ARRAY && node->rhs->var->is_string_literal) { return eval(node->lhs) != (intptr_t)node->rhs->var->init_data; } else { return eval(node->lhs) != eval(node->rhs); } } case ND_LT: if (node->lhs->ty->is_unsigned) return (uint64_t)eval(node->lhs) < eval(node->rhs); return eval(node->lhs) < eval(node->rhs); case ND_LE: if (node->lhs->ty->is_unsigned) return (uint64_t)eval(node->lhs) <= eval(node->rhs); return eval(node->lhs) <= eval(node->rhs); case ND_COND: return eval(node->cond) ? eval2(node->then, label) : eval2(node->els, label); case ND_COMMA: return eval2(node->rhs, label); case ND_NOT: return !eval(node->lhs); case ND_BITNOT: return ~eval(node->lhs); case ND_LOGAND: return eval(node->lhs) && eval(node->rhs); case ND_LOGOR: return eval(node->lhs) || eval(node->rhs); case ND_CAST: { int64_t val = eval2(node->lhs, label); if (is_integer(node->ty)) { switch (node->ty->size) { case 1: return node->ty->is_unsigned ? (uint8_t)val : (int8_t)val; case 2: return node->ty->is_unsigned ? (uint16_t)val : (int16_t)val; case 4: return node->ty->is_unsigned ? (uint32_t)val : (int32_t)val; } } return val; } case ND_ADDR: return eval_rval(node->lhs, label); case ND_LABEL_VAL: *label = &node->unique_label; return 0; case ND_MEMBER: if (!label) error_tok(node->tok, "not a compile-time constant"); if (node->ty->kind != TY_ARRAY) error_tok(node->tok, "invalid initializer"); return eval_rval(node->lhs, label) + node->member->offset; case ND_VAR: if (!label) { if (node->ty->kind == TY_ARRAY && node->var->is_string_literal) { return (intptr_t)node->var->init_data; } error_tok(node->tok, "not a compile-time constant"); } if (node->var->ty->kind != TY_ARRAY && node->var->ty->kind != TY_FUNC) { error_tok(node->tok, "invalid initializer"); } *label = &node->var->name; return 0; case ND_NUM: return node->val; } error_tok(node->tok, "not a compile-time constant"); } static int64_t eval_rval(Node *node, char ***label) { switch (node->kind) { case ND_VAR: if (node->var->is_local) { error_tok(node->tok, "not a compile-time constant"); } *label = &node->var->name; return 0; case ND_DEREF: return eval2(node->lhs, label); case ND_MEMBER: return eval_rval(node->lhs, label) + node->member->offset; } error_tok(node->tok, "invalid initializer"); } bool is_const_expr(Node *node) { add_type(node); switch (node->kind) { case ND_ADD: case ND_SUB: case ND_MUL: case ND_DIV: case ND_REM: case ND_BINAND: case ND_BINOR: case ND_BINXOR: case ND_SHL: case ND_SHR: case ND_LT: case ND_LE: case ND_LOGAND: case ND_LOGOR: return is_const_expr(node->lhs) && is_const_expr(node->rhs); case ND_EQ: case ND_NE: { bool lhs = is_const_expr(node->lhs); bool rhs = is_const_expr(node->rhs); return (lhs && rhs) || (rhs && node->lhs->kind == ND_VAR && node->lhs->var->is_string_literal) || (lhs && node->rhs->kind == ND_VAR && node->rhs->var->is_string_literal); } case ND_COND: if (!is_const_expr(node->cond)) return false; return is_const_expr(eval(node->cond) ? node->then : node->els); case ND_COMMA: return is_const_expr(node->rhs); case ND_NEG: case ND_NOT: case ND_BITNOT: case ND_CAST: return is_const_expr(node->lhs); case ND_NUM: return true; case ND_VAR: return node->var->is_string_literal; } return false; } int64_t const_expr(Token **rest, Token *tok) { Node *node = conditional(rest, tok); return eval(node); } static double eval_double(Node *node) { add_type(node); if (is_integer(node->ty)) { if (node->ty->is_unsigned) { return (unsigned long)eval(node); } return eval(node); } switch (node->kind) { case ND_ADD: return eval_double(node->lhs) + eval_double(node->rhs); case ND_SUB: return eval_double(node->lhs) - eval_double(node->rhs); case ND_MUL: return eval_double(node->lhs) * eval_double(node->rhs); case ND_DIV: return eval_double(node->lhs) / eval_double(node->rhs); case ND_NEG: return -eval_double(node->lhs); case ND_COND: return eval_double(node->cond) ? eval_double(node->then) : eval_double(node->els); case ND_COMMA: return eval_double(node->rhs); case ND_CAST: if (is_flonum(node->lhs->ty)) return eval_double(node->lhs); return eval(node->lhs); case ND_NUM: return node->fval; } error_tok(node->tok, "not a compile-time constant"); } // Convert op= operators to expressions containing an assignment. // // In general, `A op= C` is converted to ``tmp = &A, *tmp = *tmp op B`. // However, if a given expression is of form `A.x op= C`, the input is // converted to `tmp = &A, (*tmp).x = (*tmp).x op C` to handle assignments // to bitfields. static Node *to_assign(Node *binary) { add_type(binary->lhs); add_type(binary->rhs); Token *tok = binary->tok; // Convert `A.x op= C` to `tmp = &A, (*tmp).x = (*tmp).x op C`. if (binary->lhs->kind == ND_MEMBER) { Obj *var = new_lvar("", pointer_to(binary->lhs->lhs->ty)); Node *expr1 = new_binary(ND_ASSIGN, new_var_node(var, tok), new_unary(ND_ADDR, binary->lhs->lhs, tok), tok); Node *expr2 = new_unary( ND_MEMBER, new_unary(ND_DEREF, new_var_node(var, tok), tok), tok); expr2->member = binary->lhs->member; Node *expr3 = new_unary( ND_MEMBER, new_unary(ND_DEREF, new_var_node(var, tok), tok), tok); expr3->member = binary->lhs->member; Node *expr4 = new_binary(ND_ASSIGN, expr2, new_binary(binary->kind, expr3, binary->rhs, tok), tok); return new_binary(ND_COMMA, expr1, expr4, tok); } // If A is an atomic type, Convert `A op= B` to // // ({ // T1 *addr = &A; T2 val = (B); T1 old = *addr; T1 new; // do { // new = old op val; // } while (!atomic_compare_exchange_strong(addr, &old, new)); // new; // }) if (binary->lhs->ty->is_atomic) { Node head = {}; Node *cur = &head; Obj *addr = new_lvar("", pointer_to(binary->lhs->ty)); Obj *val = new_lvar("", binary->rhs->ty); Obj *old = new_lvar("", binary->lhs->ty); Obj *new = new_lvar("", binary->lhs->ty); cur = cur->next = new_unary(ND_EXPR_STMT, new_binary(ND_ASSIGN, new_var_node(addr, tok), new_unary(ND_ADDR, binary->lhs, tok), tok), tok); cur = cur->next = new_unary( ND_EXPR_STMT, new_binary(ND_ASSIGN, new_var_node(val, tok), binary->rhs, tok), tok); cur = cur->next = new_unary( ND_EXPR_STMT, new_binary(ND_ASSIGN, new_var_node(old, tok), new_unary(ND_DEREF, new_var_node(addr, tok), tok), tok), tok); Node *loop = new_node(ND_DO, tok); loop->brk_label = new_unique_name(); loop->cont_label = new_unique_name(); Node *body = new_binary(ND_ASSIGN, new_var_node(new, tok), new_binary(binary->kind, new_var_node(old, tok), new_var_node(val, tok), tok), tok); loop->then = new_node(ND_BLOCK, tok); loop->then->body = new_unary(ND_EXPR_STMT, body, tok); Node *cas = new_node(ND_CAS, tok); cas->cas_addr = new_var_node(addr, tok); cas->cas_old = new_unary(ND_ADDR, new_var_node(old, tok), tok); cas->cas_new = new_var_node(new, tok); loop->cond = new_unary(ND_NOT, cas, tok); cur = cur->next = loop; cur = cur->next = new_unary(ND_EXPR_STMT, new_var_node(new, tok), tok); Node *node = new_node(ND_STMT_EXPR, tok); node->body = head.next; return node; } // Convert `A op= B` to ``tmp = &A, *tmp = *tmp op B`. Obj *var = new_lvar("", pointer_to(binary->lhs->ty)); Node *expr1 = new_binary(ND_ASSIGN, new_var_node(var, tok), new_unary(ND_ADDR, binary->lhs, tok), tok); Node *expr2 = new_binary( ND_ASSIGN, new_unary(ND_DEREF, new_var_node(var, tok), tok), new_binary(binary->kind, new_unary(ND_DEREF, new_var_node(var, tok), tok), binary->rhs, tok), tok); return new_binary(ND_COMMA, expr1, expr2, tok); } // @hint it's expr() without the commas // assign = conditional (assign-op assign)? // assign-op = "=" | "+=" | "-=" | "*=" | "/=" | "%=" | "&=" | "|=" | "^=" // | "<<=" | ">>=" static Node *assign(Token **rest, Token *tok) { Node *node = conditional(&tok, tok); if (tok->len == 1) { if (tok->loc[0] == '=') { return new_binary(ND_ASSIGN, node, assign(rest, tok->next), tok); } } else if (tok->len == 2) { if (tok->loc[0] == '+' && tok->loc[1] == '=') { return to_assign(new_add(node, assign(rest, tok->next), tok)); } if (tok->loc[0] == '-' && tok->loc[1] == '=') { return to_assign(new_sub(node, assign(rest, tok->next), tok)); } if (tok->loc[0] == '*' && tok->loc[1] == '=') { return to_assign(new_binary(ND_MUL, node, assign(rest, tok->next), tok)); } if (tok->loc[0] == '/' && tok->loc[1] == '=') { return to_assign(new_binary(ND_DIV, node, assign(rest, tok->next), tok)); } if (tok->loc[0] == '%' && tok->loc[1] == '=') { return to_assign(new_binary(ND_REM, node, assign(rest, tok->next), tok)); } if (tok->loc[0] == '&' && tok->loc[1] == '=') { return to_assign( new_binary(ND_BINAND, node, assign(rest, tok->next), tok)); } if (tok->loc[0] == '|' && tok->loc[1] == '=') { return to_assign( new_binary(ND_BINOR, node, assign(rest, tok->next), tok)); } if (tok->loc[0] == '^' && tok->loc[1] == '=') { return to_assign( new_binary(ND_BINXOR, node, assign(rest, tok->next), tok)); } } else if (tok->len == 3) { if (tok->loc[0] == '<' && tok->loc[1] == '<' && tok->loc[2] == '=') { return to_assign(new_binary(ND_SHL, node, assign(rest, tok->next), tok)); } if (tok->loc[0] == '>' && tok->loc[1] == '>' && tok->loc[2] == '=') { return to_assign(new_binary(ND_SHR, node, assign(rest, tok->next), tok)); } } *rest = tok; return node; } // conditional = logor ("?" expr? ":" conditional)? static Node *conditional(Token **rest, Token *tok) { Node *cond = logor(&tok, tok); if (!EQUAL(tok, "?")) { *rest = tok; return cond; } if (EQUAL(tok->next, ":")) { // [GNU] Compile `a ?: b` as `tmp = a, tmp ? tmp : b`. Node *els = conditional(rest, tok->next->next); if (is_const_expr(cond)) { /* DCE */ if (is_const_expr_true(cond)) { return cond; } else { return els; } } Obj *var = new_lvar("", cond->ty); Node *lhs = new_binary(ND_ASSIGN, new_var_node(var, tok), cond, tok); Node *rhs = new_node(ND_COND, tok); rhs->cond = new_var_node(var, tok); rhs->then = new_var_node(var, tok); rhs->els = els; return new_binary(ND_COMMA, lhs, rhs, tok); } Node *node = new_node(ND_COND, tok); node->cond = cond; node->then = expr(&tok, tok->next); tok = skip(tok, ':'); node->els = conditional(rest, tok); add_type(node); if (is_const_expr(cond)) { /* DCE */ if (is_const_expr_true(cond)) { return new_cast(node->then, node->ty); } else { return new_cast(node->els, node->ty); } } return node; } // logor = logand ("||" logand)* static Node *logor(Token **rest, Token *tok) { Node *node = logand(&tok, tok); while (EQUAL(tok, "||")) { Token *start = tok; node = new_binary(ND_LOGOR, node, logand(&tok, tok->next), start); } *rest = tok; return node; } // logand = binor ("&&" binor)* static Node *logand(Token **rest, Token *tok) { Token *start = tok; Node *node = binor(&tok, tok); while (EQUAL(tok, "&&")) { Token *start = tok; node = new_binary(ND_LOGAND, node, binor(&tok, tok->next), start); } *rest = tok; #if 0 if (node->lhs && node->rhs) { add_type(node->lhs); add_type(node->rhs); if (((is_const_expr(node->lhs) && !is_const_expr_true(node->lhs)) || (is_const_expr(node->rhs) && !is_const_expr_true(node->rhs)))) { node->lhs = new_cast(new_num(0, start), node->lhs->ty); /* DCE */ node->rhs = new_cast(new_num(0, start), node->rhs->ty); } } #endif return node; } // binor = binxor ("|" binxor)* static Node *binor(Token **rest, Token *tok) { Node *node = binxor(&tok, tok); while (EQUAL(tok, "|")) { Token *start = tok; node = new_binary(ND_BINOR, node, binxor(&tok, tok->next), start); } *rest = tok; return node; } // binxor = binand ("^" binand)* static Node *binxor(Token **rest, Token *tok) { Node *node = binand(&tok, tok); while (EQUAL(tok, "^")) { Token *start = tok; node = new_binary(ND_BINXOR, node, binand(&tok, tok->next), start); } *rest = tok; return node; } // binand = equality ("&" equality)* static Node *binand(Token **rest, Token *tok) { Node *node = equality(&tok, tok); while (EQUAL(tok, "&")) { Token *start = tok; node = new_binary(ND_BINAND, node, equality(&tok, tok->next), start); } *rest = tok; return node; } // equality = relational ("==" relational | "!=" relational)* static Node *equality(Token **rest, Token *tok) { Node *node = relational(&tok, tok); for (;;) { Token *start = tok; if (EQUAL(tok, "==")) { node = new_binary(ND_EQ, node, relational(&tok, tok->next), start); continue; } if (EQUAL(tok, "!=")) { node = new_binary(ND_NE, node, relational(&tok, tok->next), start); continue; } *rest = tok; return node; } } // relational = shift ("<" shift | "<=" shift | ">" shift | ">=" shift)* static Node *relational(Token **rest, Token *tok) { Node *node = shift(&tok, tok); for (;;) { Token *start = tok; if (EQUAL(tok, "<")) { node = new_binary(ND_LT, node, shift(&tok, tok->next), start); continue; } if (tok->len == 2) { if (tok->loc[0] == '<' && tok->loc[1] == '=') { node = new_binary(ND_LE, node, shift(&tok, tok->next), start); continue; } if (tok->loc[0] == '>' && tok->loc[1] == '=') { node = new_binary(ND_LE, shift(&tok, tok->next), node, start); continue; } } else if (tok->len == 1) { if (tok->loc[0] == '>') { node = new_binary(ND_LT, shift(&tok, tok->next), node, start); continue; } } *rest = tok; return node; } } // shift = add ("<<" add | ">>" add)* static Node *shift(Token **rest, Token *tok) { Node *node = add(&tok, tok); for (;;) { Token *start = tok; if (tok->len == 2) { if (tok->loc[0] == '<' && tok->loc[1] == '<') { node = new_binary(ND_SHL, node, add(&tok, tok->next), start); continue; } if (tok->loc[0] == '>' && tok->loc[1] == '>') { node = new_binary(ND_SHR, node, add(&tok, tok->next), start); continue; } } *rest = tok; return node; } } static Node *get_vla_size(Type *ty, Token *tok) { if (ty->vla_size) { return new_var_node(ty->vla_size, tok); } else { Node *lhs = compute_vla_size(ty, tok); Node *rhs = new_var_node(ty->vla_size, tok); return new_binary(ND_COMMA, lhs, rhs, tok); } } // In C, `+` operator is overloaded to perform the pointer arithmetic. // If p is a pointer, p+n adds not n but sizeof(*p)*n to the value of p, // so that p+n points to the location n elements (not bytes) ahead of p. // In other words, we need to scale an integer value before adding to a // pointer value. This function takes care of the scaling. static Node *new_add(Node *lhs, Node *rhs, Token *tok) { add_type(lhs); add_type(rhs); // num + num if (is_numeric(lhs->ty) && is_numeric(rhs->ty)) return new_binary(ND_ADD, lhs, rhs, tok); if (lhs->ty->base && rhs->ty->base) error_tok(tok, "invalid operands"); // Canonicalize `num + ptr` to `ptr + num`. if (!lhs->ty->base && rhs->ty->base) { Node *tmp = lhs; lhs = rhs; rhs = tmp; } // VLA + num if (lhs->ty->base->kind == TY_VLA) { rhs = new_binary(ND_MUL, rhs, get_vla_size(lhs->ty->base, tok), tok); return new_binary(ND_ADD, lhs, rhs, tok); } // ptr + num rhs = new_binary(ND_MUL, rhs, new_long(lhs->ty->base->size, tok), tok); return new_binary(ND_ADD, lhs, rhs, tok); } // Like `+`, `-` is overloaded for the pointer type. static Node *new_sub(Node *lhs, Node *rhs, Token *tok) { add_type(lhs); add_type(rhs); // num - num if (is_numeric(lhs->ty) && is_numeric(rhs->ty)) return new_binary(ND_SUB, lhs, rhs, tok); // VLA + num if (lhs->ty->base->kind == TY_VLA) { rhs = new_binary(ND_MUL, rhs, get_vla_size(lhs->ty->base, tok), tok); add_type(rhs); Node *node = new_binary(ND_SUB, lhs, rhs, tok); node->ty = lhs->ty; return node; } // ptr - num if (lhs->ty->base && is_integer(rhs->ty)) { rhs = new_binary(ND_MUL, rhs, new_long(lhs->ty->base->size, tok), tok); add_type(rhs); Node *node = new_binary(ND_SUB, lhs, rhs, tok); node->ty = lhs->ty; return node; } // ptr - ptr, which returns how many elements are between the two. if (lhs->ty->base && rhs->ty->base) { Node *node = new_binary(ND_SUB, lhs, rhs, tok); node->ty = ty_long; return new_binary(ND_DIV, node, new_num(lhs->ty->base->size, tok), tok); } error_tok(tok, "invalid operands"); } static Node *new_mul(Node *lhs, Node *rhs, Token *tok) { return new_binary(ND_MUL, lhs, rhs, tok); } static Node *builtin_overflow(Token **rest, Token *tok, Node *new_op(Node *, Node *, Token *)) { Token *start = tok; tok = skip(tok->next, '('); Node *lhs = assign(&tok, tok); tok = skip(tok, ','); Node *rhs = assign(&tok, tok); tok = skip(tok, ','); Node *dst = assign(&tok, tok); *rest = skip(tok, ')'); Node *node = new_op(lhs, rhs, start); add_type(node); add_type(dst); if (!is_compatible(pointer_to(node->ty), dst->ty)) { error_tok(start, "output pointer type incompatible"); } node->overflow = dst; node->ty = copy_type(ty_bool); return node; } // add = mul ("+" mul | "-" mul)* static Node *add(Token **rest, Token *tok) { Node *node = mul(&tok, tok); for (;;) { Token *start = tok; if (tok->len == 1) { if (tok->loc[0] == '+') { node = new_add(node, mul(&tok, tok->next), start); continue; } if (tok->loc[0] == '-') { node = new_sub(node, mul(&tok, tok->next), start); continue; } } *rest = tok; return node; } } // mul = cast ("*" cast | "/" cast | "%" cast)* static Node *mul(Token **rest, Token *tok) { Node *node = cast(&tok, tok); for (;;) { Token *start = tok; if (tok->len == 1) { if (tok->loc[0] == '*') { node = new_mul(node, cast(&tok, tok->next), start); continue; } if (tok->loc[0] == '/') { node = new_binary(ND_DIV, node, cast(&tok, tok->next), start); continue; } if (tok->loc[0] == '%') { node = new_binary(ND_REM, node, cast(&tok, tok->next), start); continue; } } *rest = tok; return node; } } // cast = "(" type-name ")" cast | unary static Node *cast(Token **rest, Token *tok) { if (EQUAL(tok, "(") && is_typename(tok->next)) { Token *start = tok; Type *ty = typename(&tok, tok->next); tok = skip(tok, ')'); // compound literal if (EQUAL(tok, "{")) return unary(rest, start); // type cast Node *node = new_cast(cast(rest, tok), ty); node->tok = start; return node; } return unary(rest, tok); } // unary = ("+" | "-" | "*" | "&" | "!" | "~") cast // | ("++" | "--") unary // | "&&" ident // | postfix static Node *unary(Token **rest, Token *tok) { if (tok->len == 1) { if (tok->loc[0] == '+') { return cast(rest, tok->next); } if (tok->loc[0] == '-') { return new_unary(ND_NEG, cast(rest, tok->next), tok); } if (tok->loc[0] == '&') { Node *lhs = cast(rest, tok->next); add_type(lhs); if (lhs->kind == ND_MEMBER && lhs->member->is_bitfield) { error_tok(tok, "cannot take address of bitfield"); } return new_unary(ND_ADDR, lhs, tok); } if (tok->loc[0] == '*') { // [https://www.sigbus.info/n1570#6.5.3.2p4] This is an oddity // in the C spec, but dereferencing a function shouldn't do // anything. If foo is a function, `*foo`, `**foo` or `*****foo` // are all equivalent to just `foo`. Node *node = cast(rest, tok->next); add_type(node); if (node->ty->kind == TY_FUNC) return node; return new_unary(ND_DEREF, node, tok); } if (tok->loc[0] == '!') { return new_unary(ND_NOT, cast(rest, tok->next), tok); } if (tok->loc[0] == '~') { return new_unary(ND_BITNOT, cast(rest, tok->next), tok); } } else if (tok->len == 2) { // Read ++i as i+=1 if (tok->loc[0] == '+' && tok->loc[1] == '+') { return to_assign(new_add(unary(rest, tok->next), new_num(1, tok), tok)); } // Read --i as i-=1 if (tok->loc[0] == '-' && tok->loc[1] == '-') { return to_assign(new_sub(unary(rest, tok->next), new_num(1, tok), tok)); } // [GNU] labels-as-values if (tok->loc[0] == '&' && tok->loc[1] == '&') { Node *node = new_node(ND_LABEL_VAL, tok); node->label = get_ident(tok->next); node->goto_next = gotos; gotos = node; *rest = tok->next->next; return node; } } return postfix(rest, tok); } // struct-members = (declspec declarator ("," declarator)* ";" javadown?)* static void struct_members(Token **rest, Token *tok, Type *ty) { Member head = {}; Member *cur = &head; int idx = 0; while (!EQUAL(tok, "}")) { VarAttr attr = {}; Type *basety = declspec(&tok, tok, &attr); bool first = true; // Anonymous struct member if ((basety->kind == TY_STRUCT || basety->kind == TY_UNION) && CONSUME(&tok, tok, ";")) { Member *mem = calloc(1, sizeof(Member)); mem->ty = basety; mem->idx = idx++; mem->align = attr.align ? attr.align : mem->ty->align; cur = cur->next = mem; continue; } // Regular struct members while (!CONSUME(&tok, tok, ";")) { if (!first) tok = skip(tok, ','); if (tok->kind == TK_JAVADOWN) { current_javadown = tok; tok = tok->next; } first = false; Member *mem = calloc(1, sizeof(Member)); mem->ty = declarator(&tok, tok, basety); mem->name = mem->ty->name; mem->idx = idx++; mem->align = attr.align ? attr.align : mem->ty->align; if (CONSUME(&tok, tok, ":")) { mem->is_bitfield = true; mem->bit_width = const_expr(&tok, tok); } cur = cur->next = mem; } if (tok->kind == TK_JAVADOWN) { tok = tok->next; } } // If the last element is an array of incomplete type, it's // called a "flexible array member". It should behave as if // if were a zero-sized array. if (cur != &head && cur->ty->kind == TY_ARRAY && cur->ty->array_len < 0) { cur->ty = array_of(cur->ty->base, 0); ty->is_flexible = true; } *rest = tok->next; ty->members = head.next; } char *ConsumeStringLiteral(Token **rest, Token *tok) { char *s; if (tok->kind != TK_STR || tok->ty->base->kind != TY_CHAR) { error_tok(tok, "expected string literal"); } s = tok->str; *rest = tok->next; return s; } // struct-union-decl = attribute? ident? ("{" struct-members)? static Type *struct_union_decl(Token **rest, Token *tok) { Type *ty = struct_type(); tok = attribute_list(tok, ty, type_attributes); // Read a tag. Token *tag = NULL; if (tok->kind == TK_IDENT) { tag = tok; tok = tok->next; } if (tag && !EQUAL(tok, "{")) { *rest = tok; Type *ty2 = find_tag(tag); if (ty2) return ty2; ty->size = -1; push_tag_scope(tag, ty); return ty; } ty->name = tag; tok = skip(tok, '{'); // Construct a struct object. if (tok->kind == TK_JAVADOWN) { current_javadown = tok; tok = tok->next; } struct_members(&tok, tok, ty); *rest = attribute_list(tok, ty, type_attributes); if (tag) { // If this is a redefinition, overwrite a previous type. // Otherwise, register the struct type. Type *ty2 = hashmap_get2(&scope->tags, tag->loc, tag->len); if (ty2) { *ty2 = *ty; return ty2; } push_tag_scope(tag, ty); } return ty; } // struct-decl = struct-union-decl static Type *struct_decl(Token **rest, Token *tok) { Type *ty = struct_union_decl(rest, tok); ty->kind = TY_STRUCT; if (ty->size < 0) return ty; // Assign offsets within the struct to members. int bits = 0; for (Member *m = ty->members; m; m = m->next) { if (m->is_bitfield && m->bit_width == 0) { // Zero-width anonymous bitfield has a special meaning. // It affects only alignment. bits = ROUNDUP(bits, m->ty->size * 8); } else if (m->is_bitfield) { int sz = m->ty->size; if (bits / (sz * 8) != (bits + m->bit_width - 1) / (sz * 8)) { bits = ROUNDUP(bits, sz * 8); } m->offset = ROUNDDOWN(bits / 8, sz); m->bit_offset = bits % (sz * 8); bits += m->bit_width; } else { if (!ty->is_packed) { bits = ROUNDUP(bits, m->align * 8); } m->offset = bits / 8; bits += m->ty->size * 8; } if (!ty->is_packed && !ty->is_aligned && ty->align < m->align) { ty->align = m->align; } } ty->size = ROUNDUP(bits, ty->align * 8) / 8; return ty; } // union-decl = struct-union-decl static Type *union_decl(Token **rest, Token *tok) { Type *ty = struct_union_decl(rest, tok); ty->kind = TY_UNION; if (ty->size < 0) return ty; // If union, we don't have to assign offsets because they // are already initialized to zero. We need to compute the // alignment and the size though. for (Member *mem = ty->members; mem; mem = mem->next) { if (ty->align < mem->align) ty->align = mem->align; if (ty->size < mem->ty->size) ty->size = mem->ty->size; } ty->size = ROUNDUP(ty->size, ty->align); return ty; } // Find a struct member by name. static Member *get_struct_member(Type *ty, Token *tok) { for (Member *mem = ty->members; mem; mem = mem->next) { // Anonymous struct member if ((mem->ty->kind == TY_STRUCT || mem->ty->kind == TY_UNION) && !mem->name) { if (get_struct_member(mem->ty, tok)) return mem; continue; } // Regular struct member if (mem->name->len == tok->len && !strncmp(mem->name->loc, tok->loc, tok->len)) { return mem; } } return NULL; } // Create a node representing a struct member access, such as foo.bar // where foo is a struct and bar is a member name. // // C has a feature called "anonymous struct" which allows a struct to // have another unnamed struct as a member like this: // // struct { struct { int a; }; int b; } x; // // The members of an anonymous struct belong to the outer struct's // member namespace. Therefore, in the above example, you can access // member "a" of the anonymous struct as "x.a". // // This function takes care of anonymous structs. static Node *struct_ref(Node *node, Token *tok) { add_type(node); if (node->ty->kind != TY_STRUCT && node->ty->kind != TY_UNION) error_tok(node->tok, "not a struct nor a union"); Type *ty = node->ty; for (;;) { Member *mem = get_struct_member(ty, tok); if (!mem) error_tok(tok, "no such member"); node = new_unary(ND_MEMBER, node, tok); node->member = mem; if (mem->name) break; ty = mem->ty; } return node; } // Convert A++ to `(typeof A)((A += 1) - 1)` static Node *new_inc_dec(Node *node, Token *tok, int addend) { add_type(node); return new_cast(new_add(to_assign(new_add(node, new_num(addend, tok), tok)), new_num(-addend, tok), tok), node->ty); } // postfix = "(" type-name ")" "{" initializer-list "}" // | ident "(" func-args ")" postfix-tail* // | primary postfix-tail* // // postfix-tail = "[" expr "]" // | "(" func-args ")" // | "." ident // | "->" ident // | "++" // | "--" static Node *postfix(Token **rest, Token *tok) { if (EQUAL(tok, "(") && is_typename(tok->next)) { // Compound literal Token *start = tok; Type *ty = typename(&tok, tok->next); tok = skip(tok, ')'); if (scope->next == NULL) { Obj *var = new_anon_gvar(ty); gvar_initializer(rest, tok, var); return new_var_node(var, start); } Obj *var = new_lvar("", ty); Node *lhs = lvar_initializer(rest, tok, var); Node *rhs = new_var_node(var, tok); return new_binary(ND_COMMA, lhs, rhs, start); } Node *node = primary(&tok, tok); for (;;) { if (tok->len == 1) { if (tok->loc[0] == '(') { node = funcall(&tok, tok->next, node); continue; } if (tok->loc[0] == '[') { // x[y] is short for *(x+y) Token *start = tok; Node *idx = expr(&tok, tok->next); tok = skip(tok, ']'); node = new_unary(ND_DEREF, new_add(node, idx, start), start); continue; } if (tok->loc[0] == '.') { node = struct_ref(node, tok->next); tok = tok->next->next; continue; } } else if (tok->len == 2) { if (tok->loc[0] == '-' && tok->loc[1] == '>') { // x->y is short for (*x).y node = new_unary(ND_DEREF, node, tok); node = struct_ref(node, tok->next); tok = tok->next->next; continue; } if (tok->loc[0] == '+' && tok->loc[1] == '+') { node = new_inc_dec(node, tok, 1); tok = tok->next; continue; } if (tok->loc[0] == '-' && tok->loc[1] == '-') { node = new_inc_dec(node, tok, -1); tok = tok->next; continue; } } *rest = tok; return node; } } // funcall = (assign ("," assign)*)? ")" static Node *funcall(Token **rest, Token *tok, Node *fn) { add_type(fn); if (fn->ty->kind != TY_FUNC && (fn->ty->kind != TY_PTR || fn->ty->base->kind != TY_FUNC)) { error_tok(fn->tok, "not a function"); } Type *ty = (fn->ty->kind == TY_FUNC) ? fn->ty : fn->ty->base; Type *param_ty = ty->params; Node head = {}; Node *cur = &head; while (!EQUAL(tok, ")")) { if (cur != &head) tok = skip(tok, ','); Node *arg = assign(&tok, tok); add_type(arg); if (!param_ty && !ty->is_variadic) error_tok(tok, "too many arguments"); if (param_ty) { if (param_ty->kind != TY_STRUCT && param_ty->kind != TY_UNION) { arg = new_cast(arg, param_ty); } param_ty = param_ty->next; } else if (arg->ty->kind == TY_FLOAT) { // If parameter type is omitted (e.g. in "..."), float // arguments are promoted to double. arg = new_cast(arg, ty_double); } cur = cur->next = arg; } if (param_ty) error_tok(tok, "too few arguments"); *rest = skip(tok, ')'); Node *node = new_unary(ND_FUNCALL, fn, tok); node->func_ty = ty; node->ty = ty->return_ty; node->args = head.next; // If a function returns a struct, it is caller's responsibility // to allocate a space for the return value. if (node->ty->kind == TY_STRUCT || node->ty->kind == TY_UNION) { node->ret_buffer = new_lvar("", node->ty); } return node; } // generic-selection = "(" assign "," generic-assoc ("," generic-assoc)* ")" // // generic-assoc = type-name ":" assign // | "default" ":" assign static Node *generic_selection(Token **rest, Token *tok) { Token *start = tok; tok = skip(tok, '('); Node *ctrl = assign(&tok, tok); add_type(ctrl); Type *t1 = ctrl->ty; if (t1->kind == TY_FUNC) t1 = pointer_to(t1); else if (t1->kind == TY_ARRAY) t1 = pointer_to(t1->base); Node *ret = NULL; while (!CONSUME(rest, tok, ")")) { tok = skip(tok, ','); if (EQUAL(tok, "default")) { tok = skip(tok->next, ':'); Node *node = assign(&tok, tok); if (!ret) ret = node; continue; } Type *t2 = typename(&tok, tok); tok = skip(tok, ':'); Node *node = assign(&tok, tok); if (is_compatible(t1, t2)) ret = node; } if (!ret) error_tok(start, "controlling expression type not compatible with" " any generic association type"); return ret; } static Node *ParseAtomic2(NodeKind kind, Token *tok, Token **rest) { Node *node = new_node(kind, tok); tok = skip(tok->next, '('); node->lhs = assign(&tok, tok); add_type(node->lhs); node->ty = node->lhs->ty->base; tok = skip(tok, ','); node->memorder = const_expr(&tok, tok); *rest = skip(tok, ')'); return node; } static Node *ParseAtomic3(NodeKind kind, Token *tok, Token **rest) { Node *node = new_node(kind, tok); tok = skip(tok->next, '('); node->lhs = assign(&tok, tok); add_type(node->lhs); node->ty = node->lhs->ty->base; tok = skip(tok, ','); node->rhs = assign(&tok, tok); add_type(node->rhs); tok = skip(tok, ','); node->memorder = const_expr(&tok, tok); *rest = skip(tok, ')'); return node; } // primary = "(" "{" stmt+ "}" ")" // | "(" expr ")" // | "sizeof" "(" type-name ")" // | "sizeof" unary // | "_Alignof" "(" type-name ")" // | "_Alignof" unary // | "_Generic" generic-selection // | "__builtin_constant_p" "(" expr ")" // | "__builtin_reg_class" "(" type-name ")" // | "__builtin_types_compatible_p" "(" type-name, type-name, ")" // | ident // | str // | num static Node *primary(Token **rest, Token *tok) { Token *start; unsigned char kw; start = tok; if ((kw = GetKw(tok->loc, tok->len))) { if (kw == KW_LP && EQUAL(tok->next, "{")) { // This is a GNU statement expresssion. Node *node = new_node(ND_STMT_EXPR, tok); node->body = compound_stmt(&tok, tok->next->next)->body; *rest = skip(tok, ')'); return node; } if (kw == KW_LP) { Node *node = expr(&tok, tok->next); *rest = skip(tok, ')'); return node; } if (kw == KW_SIZEOF && EQUAL(tok->next, "(") && is_typename(tok->next->next)) { Type *ty = typename(&tok, tok->next->next); *rest = skip(tok, ')'); if (ty->kind == TY_VLA) { if (ty->vla_size) { return new_var_node(ty->vla_size, tok); } Node *lhs = compute_vla_size(ty, tok); Node *rhs = new_var_node(ty->vla_size, tok); return new_binary(ND_COMMA, lhs, rhs, tok); } return new_ulong(ty->size, start); } if (kw == KW_SIZEOF) { Node *node = unary(rest, tok->next); add_type(node); if (node->ty->kind == TY_VLA) { return get_vla_size(node->ty, tok); } return new_ulong(node->ty->size, tok); } if ((kw == KW__ALIGNOF || kw == KW___ALIGNOF__) && EQUAL(tok->next, "(") && is_typename(tok->next->next)) { Type *ty = typename(&tok, tok->next->next); *rest = skip(tok, ')'); return new_ulong(ty->align, tok); } if ((kw == KW__ALIGNOF || kw == KW___ALIGNOF__)) { Node *node = unary(rest, tok->next); add_type(node); return new_ulong(node->ty->align, tok); } if (kw == KW__GENERIC) { return generic_selection(rest, tok->next); } if (kw == KW___BUILTIN_CONSTANT_P) { tok = skip(tok->next, '('); Node *e = assign(&tok, tok); *rest = skip(tok, ')'); return new_bool(is_const_expr(e), start); /* DCE */ } if (kw == KW___BUILTIN_TYPES_COMPATIBLE_P) { tok = skip(tok->next, '('); Type *t1 = typename(&tok, tok); tok = skip(tok, ','); Type *t2 = typename(&tok, tok); *rest = skip(tok, ')'); return new_bool(is_compatible(t1, t2), start); } if (kw == KW___BUILTIN_OFFSETOF) { tok = skip(tok->next, '('); Token *stok = tok; Type *tstruct = typename(&tok, tok); if (tstruct->kind != TY_STRUCT && tstruct->kind != TY_UNION) { error_tok(stok, "not a structure or union type"); } tok = skip(tok, ','); Token *member = tok; tok = tok->next; *rest = skip(tok, ')'); for (Member *m = tstruct->members; m; m = m->next) { if (m->name->len == member->len && !memcmp(m->name->loc, member->loc, m->name->len)) { return new_ulong(m->offset, start); } } error_tok(member, "no such member"); } if (kw == KW___BUILTIN_REG_CLASS) { tok = skip(tok->next, '('); Type *ty = typename(&tok, tok); *rest = skip(tok, ')'); if (is_integer(ty) || ty->kind == TY_PTR) return new_int(0, start); if (is_flonum(ty)) return new_int(1, start); return new_int(2, start); } if (kw == KW___ATOMIC_COMPARE_EXCHANGE_N) { Node *node = new_node(ND_CAS, tok); tok = skip(tok->next, '('); node->cas_addr = assign(&tok, tok); add_type(node->cas_addr); tok = skip(tok, ','); node->cas_old = assign(&tok, tok); add_type(node->cas_old); tok = skip(tok, ','); node->cas_new = assign(&tok, tok); add_type(node->cas_new); tok = skip(tok, ','); /* weak = */ const_expr(&tok, tok); tok = skip(tok, ','); node->memorder = const_expr(&tok, tok); tok = skip(tok, ','); /* memorder_failure = */ const_expr(&tok, tok); *rest = skip(tok, ')'); node->ty = node->cas_addr->ty->base; return node; } if (kw == KW___ATOMIC_EXCHANGE_N) { return ParseAtomic3(ND_EXCH_N, tok, rest); } if (kw == KW___ATOMIC_LOAD) { return ParseAtomic3(ND_LOAD, tok, rest); } if (kw == KW___ATOMIC_STORE) { return ParseAtomic3(ND_STORE, tok, rest); } if (kw == KW___ATOMIC_LOAD_N) { return ParseAtomic2(ND_LOAD_N, tok, rest); } if (kw == KW___ATOMIC_STORE_N) { return ParseAtomic3(ND_STORE_N, tok, rest); } if (kw == KW___ATOMIC_FETCH_ADD) { return ParseAtomic3(ND_FETCHADD, tok, rest); } if (kw == KW___ATOMIC_FETCH_SUB) { return ParseAtomic3(ND_FETCHSUB, tok, rest); } if (kw == KW___ATOMIC_FETCH_XOR) { return ParseAtomic3(ND_FETCHXOR, tok, rest); } if (kw == KW___ATOMIC_FETCH_AND) { return ParseAtomic3(ND_FETCHAND, tok, rest); } if (kw == KW___ATOMIC_FETCH_OR) { return ParseAtomic3(ND_FETCHOR, tok, rest); } if (kw == KW___ATOMIC_TEST_AND_SET) { return ParseAtomic2(ND_TESTANDSETA, tok, rest); } if (kw == KW___ATOMIC_CLEAR) { return ParseAtomic2(ND_CLEAR, tok, rest); } if (kw == KW___SYNC_LOCK_TEST_AND_SET) { // TODO(jart): delete me Node *node = new_node(ND_TESTANDSET, tok); tok = skip(tok->next, '('); node->lhs = assign(&tok, tok); add_type(node->lhs); node->ty = node->lhs->ty->base; tok = skip(tok, ','); node->rhs = assign(&tok, tok); *rest = skip(tok, ')'); return node; } if (kw == KW___SYNC_LOCK_RELEASE) { // TODO(jart): delete me Node *node = new_node(ND_RELEASE, tok); tok = skip(tok->next, '('); node->lhs = assign(&tok, tok); add_type(node->lhs); node->ty = node->lhs->ty->base; *rest = skip(tok, ')'); return node; } if (kw == KW___BUILTIN_IA32_MOVNTDQ) { Node *node = new_node(ND_MOVNTDQ, tok); tok = skip(tok->next, '('); node->lhs = assign(&tok, tok); add_type(node->lhs); node->ty = node->lhs->ty->base; tok = skip(tok, ','); node->rhs = assign(&tok, tok); add_type(node->rhs); *rest = skip(tok, ')'); return node; } if (kw == KW___BUILTIN_IA32_PMOVMSKB128) { Node *node = new_node(ND_PMOVMSKB, tok); tok = skip(tok->next, '('); node->lhs = assign(&tok, tok); add_type(node->lhs); node->ty = node->lhs->ty->base; *rest = skip(tok, ')'); return node; } if (kw == KW___BUILTIN_EXPECT) { /* do nothing */ tok = skip(tok->next, '('); Node *node = assign(&tok, tok); tok = skip(tok, ','); const_expr(&tok, tok); *rest = skip(tok, ')'); return node; } if (kw == KW___BUILTIN_ASSUME_ALIGNED) { /* do nothing */ tok = skip(tok->next, '('); Node *node = assign(&tok, tok); tok = skip(tok, ','); const_expr(&tok, tok); if (EQUAL(tok, ",")) { const_expr(&tok, tok->next); } *rest = skip(tok, ')'); return node; } if (kw == KW___BUILTIN_ADD_OVERFLOW) { return builtin_overflow(rest, tok, new_add); } if (kw == KW___BUILTIN_SUB_OVERFLOW) { return builtin_overflow(rest, tok, new_sub); } if (kw == KW___BUILTIN_MUL_OVERFLOW) { return builtin_overflow(rest, tok, new_mul); } if (kw == KW___BUILTIN_NEG_OVERFLOW) { Token *start = tok; tok = skip(tok->next, '('); Node *lhs = assign(&tok, tok); tok = skip(tok, ','); Node *dst = assign(&tok, tok); *rest = skip(tok, ')'); Node *node = new_unary(ND_NEG, lhs, start); add_type(node); add_type(dst); if (!is_compatible(pointer_to(node->ty), dst->ty)) { error_tok(start, "output pointer type incompatible"); } node->overflow = dst; node->ty = copy_type(ty_bool); return node; } if (kw == KW___BUILTIN_FPCLASSIFY) { Node *node = new_node(ND_FPCLASSIFY, tok); node->fpc = calloc(1, sizeof(FpClassify)); node->ty = ty_int; tok = skip(tok->next, '('); for (int i = 0; i < 5; ++i) { node->fpc->args[i] = const_expr(&tok, tok); tok = skip(tok, ','); } node->fpc->node = expr(&tok, tok); add_type(node->fpc->node); if (!is_flonum(node->fpc->node->ty)) { error_tok(node->fpc->node->tok, "need floating point"); } *rest = skip(tok, ')'); return node; } if (kw == KW___BUILTIN_POPCOUNT) { Token *t = skip(tok->next, '('); Node *node = assign(&t, t); if (is_const_expr(node)) { *rest = skip(t, ')'); return new_num(__builtin_popcount(eval(node)), t); } } if (kw == KW___BUILTIN_POPCOUNTL || kw == KW___BUILTIN_POPCOUNTLL) { Token *t = skip(tok->next, '('); Node *node = assign(&t, t); if (is_const_expr(node)) { *rest = skip(t, ')'); return new_num(__builtin_popcountl(eval(node)), t); } } if (kw == KW___BUILTIN_FFS) { Token *t = skip(tok->next, '('); Node *node = assign(&t, t); if (is_const_expr(node)) { *rest = skip(t, ')'); return new_num(__builtin_ffs(eval(node)), t); } } if (kw == KW___BUILTIN_FFSL || kw == KW___BUILTIN_FFSLL) { Token *t = skip(tok->next, '('); Node *node = assign(&t, t); if (is_const_expr(node)) { *rest = skip(t, ')'); return new_num(__builtin_ffsl(eval(node)), t); } } if ((kw == KW___BUILTIN_STRLEN || (!opt_no_builtin && kw == KW_STRLEN)) && EQUAL(tok->next, "(") && tok->next->next->kind == TK_STR && EQUAL(tok->next->next->next, ")")) { *rest = tok->next->next->next->next; return new_num(strlen(tok->next->next->str), tok); } if ((kw == KW___BUILTIN_STRPBRK || (!opt_no_builtin && kw == KW_STRPBRK))) { if (EQUAL(tok->next, "(") && tok->next->next->kind == TK_STR && EQUAL(tok->next->next->next, ",") && tok->next->next->next->next->kind == TK_STR && EQUAL(tok->next->next->next->next->next, ")")) { *rest = tok->next->next->next->next->next->next; char *res = strpbrk(tok->next->next->str, tok->next->next->next->next->str); if (res) { return new_var_node( new_string_literal(res, array_of(ty_char, strlen(res) + 1)), tok); } else { return new_num(0, tok); } } } if (kw == KW___BUILTIN_STRSTR || (!opt_no_builtin && kw == KW_STRSTR)) { if (EQUAL(tok->next, "(") && tok->next->next->kind == TK_STR && EQUAL(tok->next->next->next, ",") && tok->next->next->next->next->kind == TK_STR && EQUAL(tok->next->next->next->next->next, ")")) { *rest = tok->next->next->next->next->next->next; char *res = strstr(tok->next->next->str, tok->next->next->next->next->str); if (res) { return new_var_node( new_string_literal(res, array_of(ty_char, strlen(res) + 1)), tok); } else { return new_num(0, tok); } } } if (kw == KW___BUILTIN_STRCHR || (!opt_no_builtin && kw == KW_STRCHR)) { if (EQUAL(tok->next, "(") && tok->next->next->kind == TK_STR && EQUAL(tok->next->next->next, ",") && tok->next->next->next->next->kind == TK_NUM && EQUAL(tok->next->next->next->next->next, ")")) { *rest = tok->next->next->next->next->next->next; char *res = strchr(tok->next->next->str, tok->next->next->next->next->val); if (res) { return new_var_node( new_string_literal(res, array_of(ty_char, strlen(res) + 1)), tok); } else { return new_num(0, tok); } } } } if (tok->kind == TK_IDENT) { // Variable or enum constant VarScope *sc = find_var(tok); *rest = tok->next; #ifdef IMPLICIT_FUNCTIONS if (!sc && EQUAL(tok->next, "(")) { Type *ty = func_type(ty_long); ty->is_variadic = true; return new_var_node(new_gvar(strndup(tok->loc, tok->len), ty), tok); } #endif // For "static inline" function if (sc && sc->var && sc->var->is_function) { if (current_fn) { strarray_push(&current_fn->refs, sc->var->name); } else { sc->var->is_root = true; } } if (sc) { if (sc->var) return new_var_node(sc->var, tok); if (sc->enum_ty) return new_num(sc->enum_val, tok); } if (EQUAL(tok->next, "(")) { error_tok(tok, "implicit declaration of a function"); } error_tok(tok, "undefined variable"); } if (tok->kind == TK_STR) { Obj *var = new_string_literal(tok->str, tok->ty); *rest = tok->next; return new_var_node(var, tok); } if (tok->kind == TK_NUM) { Node *node; if (is_flonum(tok->ty)) { node = new_node(ND_NUM, tok); node->fval = tok->fval; } else { node = new_num(tok->val, tok); } node->ty = tok->ty; *rest = tok->next; return node; } error_tok(tok, "expected an expression"); } static Token *parse_typedef(Token *tok, Type *basety) { bool first = true; while (!CONSUME(&tok, tok, ";")) { if (!first) { tok = skip(tok, ','); } first = false; Type *ty = declarator(&tok, tok, basety); if (!ty->name) error_tok(ty->name_pos, "typedef name omitted"); tok = attribute_list(tok, ty, type_attributes); push_scope(get_ident(ty->name))->type_def = ty; } return tok; } static void create_param_lvars(Type *param) { if (param) { create_param_lvars(param->next); if (!param->name) error_tok(param->name_pos, "parameter name omitted"); new_lvar(get_ident(param->name), param); } } // This function matches gotos or labels-as-values with labels. // // We cannot resolve gotos as we parse a function because gotos // can refer a label that appears later in the function. // So, we need to do this after we parse the entire function. static void resolve_goto_labels(void) { for (Node *x = gotos; x; x = x->goto_next) { for (Node *y = labels; y; y = y->goto_next) { if (!strcmp(x->label, y->label)) { x->unique_label = y->unique_label; break; } } if (x->unique_label == NULL) { error_tok(x->tok->next, "use of undeclared label"); } } gotos = labels = NULL; } static Obj *find_func(char *name) { Scope *sc = scope; while (sc->next) sc = sc->next; VarScope *sc2 = hashmap_get(&sc->vars, name); if (sc2 && sc2->var && sc2->var->is_function) return sc2->var; return NULL; } static void mark_live(Obj *var) { int i; Obj *fn; if (!var->is_function || var->is_live) return; var->is_live = true; for (i = 0; i < var->refs.len; i++) { fn = find_func(var->refs.data[i]); if (fn) mark_live(fn); } } static Token *function(Token *tok, Type *basety, VarAttr *attr) { Type *ty = declarator(&tok, tok, basety); if (!ty->name) error_tok(ty->name_pos, "function name omitted"); char *name_str = get_ident(ty->name); Obj *fn = find_func(name_str); if (fn) { // Redeclaration if (!fn->is_function) error_tok(tok, "redeclared as a different kind of symbol"); if (fn->is_definition && EQUAL(tok, "{")) error_tok(tok, "redefinition of %s", name_str); if (!fn->is_static && attr->is_static) error_tok(tok, "static declaration follows a non-static declaration"); fn->is_definition = fn->is_definition || EQUAL(tok, "{"); fn->is_weak |= attr->is_weak; fn->is_noreturn |= attr->is_noreturn; fn->tok = ty->name; } else { fn = new_gvar(name_str, ty); fn->tok = ty->name; fn->is_function = true; fn->is_definition = EQUAL(tok, "{"); fn->is_static = attr->is_static || (attr->is_inline && !attr->is_extern); fn->is_inline = attr->is_inline; } fn->align = MAX(fn->align, attr->align); fn->is_weak |= attr->is_weak; fn->section = fn->section ?: attr->section; fn->is_ms_abi |= attr->is_ms_abi; fn->visibility = fn->visibility ?: attr->visibility; fn->is_aligned |= attr->is_aligned; fn->is_noreturn |= attr->is_noreturn; fn->is_destructor |= attr->is_destructor; fn->is_constructor |= attr->is_constructor; fn->is_externally_visible |= attr->is_externally_visible; fn->is_no_instrument_function |= attr->is_no_instrument_function; fn->is_force_align_arg_pointer |= attr->is_force_align_arg_pointer; fn->is_no_caller_saved_registers |= attr->is_no_caller_saved_registers; fn->is_root = !(fn->is_static && fn->is_inline); fn->javadown = fn->javadown ?: current_javadown; current_javadown = NULL; if (consume_attribute(&tok, tok, "asm")) { tok = skip(tok, '('); fn->asmname = ConsumeStringLiteral(&tok, tok); tok = skip(tok, ')'); } tok = attribute_list(tok, attr, thing_attributes); if (CONSUME(&tok, tok, ";")) return tok; current_fn = fn; locals = NULL; enter_scope(); create_param_lvars(ty->params); // A buffer for a struct/union return value is passed // as the hidden first parameter. Type *rty = ty->return_ty; if ((rty->kind == TY_STRUCT || rty->kind == TY_UNION) && rty->size > 16) { new_lvar("", pointer_to(rty)); } fn->params = locals; if (ty->is_variadic) { fn->va_area = new_lvar("__va_area__", array_of(ty_char, 136)); } fn->alloca_bottom = new_lvar("__alloca_size__", pointer_to(ty_char)); tok = skip(tok, '{'); // [https://www.sigbus.info/n1570#6.4.2.2p1] "__func__" is // automatically defined as a local variable containing the // current function name. push_scope("__func__")->var = new_string_literal(fn->name, array_of(ty_char, strlen(fn->name) + 1)); // [GNU] __FUNCTION__ is yet another name of __func__. push_scope("__FUNCTION__")->var = new_string_literal(fn->name, array_of(ty_char, strlen(fn->name) + 1)); fn->body = compound_stmt(&tok, tok); fn->locals = locals; leave_scope(); resolve_goto_labels(); return tok; } static Token *global_variable(Token *tok, Type *basety, VarAttr *attr) { bool first = true; bool isjavadown = tok->kind == TK_JAVADOWN; while (!CONSUME(&tok, tok, ";")) { if (!first) tok = skip(tok, ','); first = false; Type *ty = declarator(&tok, tok, basety); if (!ty->name) { if (isjavadown) { return tok; } else { error_tok(ty->name_pos, "variable name omitted"); } } Obj *var = new_gvar(get_ident(ty->name), ty); if (!var->tok) var->tok = ty->name; var->javadown = current_javadown; if (consume_attribute(&tok, tok, "asm")) { tok = skip(tok, '('); var->asmname = ConsumeStringLiteral(&tok, tok); tok = skip(tok, ')'); } tok = attribute_list(tok, attr, thing_attributes); var->align = MAX(var->align, attr->align); var->is_weak = attr->is_weak; var->section = attr->section; var->visibility = attr->visibility; var->is_aligned = var->is_aligned | attr->is_aligned; var->is_externally_visible = attr->is_externally_visible; var->is_definition = !attr->is_extern; var->is_static = attr->is_static; var->is_tls = attr->is_tls; var->section = attr->section; if (attr->align) var->align = attr->align; if (EQUAL(tok, "=")) { gvar_initializer(&tok, tok->next, var); } else if (!attr->is_extern && !attr->is_tls) { var->is_tentative = true; } } return tok; } // Lookahead tokens and returns true if a given token is a start // of a function definition or declaration. static bool is_function(Token *tok) { if (EQUAL(tok, ";")) return false; Type dummy = {}; Type *ty = declarator(&tok, tok, &dummy); return ty->kind == TY_FUNC; } // Remove redundant tentative definitions. static void scan_globals(void) { Obj head; Obj *cur = &head; for (Obj *var = globals; var; var = var->next) { if (!var->is_tentative) { cur = cur->next = var; continue; } // Find another definition of the same identifier. Obj *var2 = globals; for (; var2; var2 = var2->next) { if (var != var2 && var2->is_definition && !strcmp(var->name, var2->name)) { break; } } // If there's another definition, the tentative definition // is redundant if (!var2) cur = cur->next = var; } cur->next = NULL; globals = head.next; } static char *prefix_builtin(const char *name) { return xstrcat("__builtin_", name); } static Obj *declare0(char *name, Type *ret) { if (!opt_no_builtin) new_gvar(name, func_type(ret)); return new_gvar(prefix_builtin(name), func_type(ret)); } static Obj *declare1(char *name, Type *ret, Type *p1) { Type *ty = func_type(ret); ty->params = copy_type(p1); if (!opt_no_builtin) new_gvar(name, ty); return new_gvar(prefix_builtin(name), ty); } static Obj *declare2(char *name, Type *ret, Type *p1, Type *p2) { Type *ty = func_type(ret); ty->params = copy_type(p1); ty->params->next = copy_type(p2); if (!opt_no_builtin) new_gvar(name, ty); return new_gvar(prefix_builtin(name), ty); } static Obj *declare3(char *s, Type *r, Type *a, Type *b, Type *c) { Type *ty = func_type(r); ty->params = copy_type(a); ty->params->next = copy_type(b); ty->params->next->next = copy_type(c); if (!opt_no_builtin) new_gvar(s, ty); return new_gvar(prefix_builtin(s), ty); } static void math0(char *name) { declare0(name, ty_double); declare0(xstrcat(name, 'f'), ty_float); declare0(xstrcat(name, 'l'), ty_ldouble); } static void math1(char *name) { declare1(name, ty_double, ty_double); declare1(xstrcat(name, 'f'), ty_float, ty_float); declare1(xstrcat(name, 'l'), ty_ldouble, ty_ldouble); } static void math2(char *name) { declare2(name, ty_double, ty_double, ty_double); declare2(xstrcat(name, 'f'), ty_float, ty_float, ty_float); declare2(xstrcat(name, 'l'), ty_ldouble, ty_ldouble, ty_ldouble); } void declare_builtin_functions(void) { Type *pvoid = pointer_to(ty_void); Type *pchar = pointer_to(ty_char); builtin_alloca = declare1("alloca", pointer_to(ty_void), ty_int); declare0("trap", ty_int); declare0("ia32_pause", ty_void); declare0("unreachable", ty_int); declare1("ctz", ty_int, ty_int); declare1("ctzl", ty_int, ty_long); declare1("ctzll", ty_int, ty_long); declare1("clz", ty_int, ty_int); declare1("clzl", ty_int, ty_long); declare1("clzll", ty_int, ty_long); declare1("ffs", ty_int, ty_int); declare1("ffsl", ty_int, ty_long); declare1("ffsll", ty_int, ty_long); declare1("bswap16", ty_ushort, ty_ushort); declare1("bswap32", ty_uint, ty_uint); declare1("bswap64", ty_ulong, ty_ulong); declare1("popcount", ty_int, ty_uint); declare1("popcountl", ty_long, ty_ulong); declare1("popcountll", ty_long, ty_ulong); declare1("signbitf", ty_int, ty_float); declare1("signbit", ty_long, ty_double); declare1("signbitl", ty_int, ty_ldouble); declare1("nanf", ty_float, pchar); declare1("nan", ty_double, pchar); declare1("nanl", ty_ldouble, pchar); declare1("isnan", ty_int, ty_double); declare1("isinf", ty_int, ty_double); declare1("isfinite", ty_int, ty_double); declare2("isgreater", ty_int, ty_double, ty_double); declare2("isgreaterequal", ty_int, ty_double, ty_double); declare2("isless", ty_int, ty_double, ty_double); declare2("islessequal", ty_int, ty_double, ty_double); declare2("islessgreater", ty_int, ty_double, ty_double); declare2("isunordered", ty_int, ty_double, ty_double); declare2("strpbrk", pchar, pchar, pchar); declare3("memcpy", pvoid, pvoid, pvoid, ty_ulong); declare3("memset", pvoid, pvoid, ty_int, ty_ulong); declare1("strlen", ty_ulong, pchar); declare2("strchr", pchar, pchar, ty_int); declare2("strstr", pchar, pchar, pchar); declare1("frame_address", pvoid, ty_int); declare2("scalbnf", ty_float, ty_float, ty_int); declare2("scalbn", ty_double, ty_double, ty_int); declare2("scalbnl", ty_ldouble, ty_ldouble, ty_int); math0("inf"); math0("huge_val"); math1("fabs"); math1("logb"); math2("fmax"); math2("fmin"); math2("copysign"); } // program = (typedef | function-definition | global-variable)* Obj *parse(Token *tok) { declare_builtin_functions(); globals = NULL; while (tok->kind != TK_EOF) { if (EQUAL(tok, "asm") || EQUAL(tok, "__asm__")) { StaticAsm *a = calloc(1, sizeof(StaticAsm)); a->next = staticasms; a->body = asm_stmt(&tok, tok); staticasms = a; continue; } if (EQUAL(tok, "_Static_assert")) { tok = static_assertion(tok); continue; } if (tok->kind == TK_JAVADOWN) { current_javadown = tok; tok = tok->next; } else { current_javadown = NULL; } VarAttr attr = {}; tok = attribute_list(tok, &attr, thing_attributes); Type *basety = declspec(&tok, tok, &attr); if (attr.is_typedef) { tok = parse_typedef(tok, basety); continue; } if (is_function(tok)) { tok = function(tok, basety, &attr); continue; } tok = global_variable(tok, basety, &attr); } for (Obj *var = globals; var; var = var->next) { if (var->is_root) { mark_live(var); } } scan_globals(); // remove redundant tentative definitions return globals; }
122,621
3,924
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/preprocess.c
// This file implements the C preprocessor. // // The preprocessor takes a list of tokens as an input and returns a // new list of tokens as an output. // // The preprocessing language is designed in such a way that that's // guaranteed to stop even if there is a recursive macro. // Informally speaking, a macro is applied only once for each token. // That is, if a macro token T appears in a result of direct or // indirect macro expansion of T, T won't be expanded any further. // For example, if T is defined as U, and U is defined as T, then // token T is expanded to U and then to T and the macro expansion // stops at that point. // // To achieve the above behavior, we attach for each token a set of // macro names from which the token is expanded. The set is called // "hideset". Hideset is initially empty, and every time we expand a // macro, the macro name is added to the resulting tokens' hidesets. // // The above macro expansion algorithm is explained in this document // written by Dave Prossor, which is used as a basis for the // standard's wording: // https://github.com/rui314/chibicc/wiki/cpp.algo.pdf #include "libc/log/libfatal.internal.h" #include "libc/mem/arena.h" #include "libc/runtime/runtime.h" #include "libc/stdio/stdio.h" #include "libc/x/xasprintf.h" #include "third_party/chibicc/chibicc.h" #include "third_party/chibicc/kw.h" typedef struct CondIncl CondIncl; typedef struct Hideset Hideset; typedef struct MacroArg MacroArg; typedef enum { STR_NONE, STR_UTF8, STR_UTF16, STR_UTF32, STR_WIDE, } StringKind; struct MacroArg { MacroArg *next; char *name; bool is_va_args; Token *tok; }; // `#if` can be nested, so we use a stack to manage nested `#if`s. struct CondIncl { CondIncl *next; enum { IN_THEN, IN_ELIF, IN_ELSE } ctx; Token *tok; bool included; }; struct Hideset { Hideset *next; char *name; }; HashMap macros; static CondIncl *cond_incl; static HashMap pragma_once; static int include_next_idx; static Token *preprocess2(Token *); static Macro *find_macro(Token *); static inline bool is_hash(Token *tok) { return tok->at_bol && tok->len == 1 && tok->loc[0] == '#'; } // Some preprocessor directives such as #include allow extraneous // tokens before newline. This function skips such tokens. static Token *skip_line(Token *tok) { if (tok->at_bol) return tok; warn_tok(tok, "extra token"); while (tok->at_bol) tok = tok->next; return tok; } static Token *copy_token(Token *tok) { Token *t = alloc_token(); *t = *tok; t->next = NULL; return t; } static Token *new_eof(Token *tok) { Token *t = copy_token(tok); t->kind = TK_EOF; t->len = 0; return t; } static Hideset *new_hideset(char *name) { Hideset *hs = calloc(1, sizeof(Hideset)); hs->name = name; return hs; } static Hideset *hideset_union(Hideset *hs1, Hideset *hs2) { Hideset head = {}; Hideset *cur = &head; for (; hs1; hs1 = hs1->next) { cur = cur->next = new_hideset(hs1->name); } cur->next = hs2; return head.next; } static bool hideset_contains(Hideset *hs, char *s, int len) { for (; hs; hs = hs->next) { if (strlen(hs->name) == len && !strncmp(hs->name, s, len)) { return true; } } return false; } static Hideset *hideset_intersection(Hideset *hs1, Hideset *hs2) { Hideset head = {}; Hideset *cur = &head; for (; hs1; hs1 = hs1->next) { if (hideset_contains(hs2, hs1->name, strlen(hs1->name))) { cur = cur->next = new_hideset(hs1->name); } } return head.next; } static Token *add_hideset(Token *tok, Hideset *hs) { Token head = {}; Token *cur = &head; for (; tok; tok = tok->next) { Token *t = copy_token(tok); t->hideset = hideset_union(t->hideset, hs); cur = cur->next = t; } return head.next; } // Append tok2 to the end of tok1. static Token *append(Token *tok1, Token *tok2) { if (tok1->kind == TK_EOF) return tok2; Token head = {}; Token *cur = &head; for (; tok1->kind != TK_EOF; tok1 = tok1->next) cur = cur->next = copy_token(tok1); cur->next = tok2; return head.next; } static Token *skip_cond_incl2(Token *tok) { unsigned char kw; while (tok->kind != TK_EOF) { if (is_hash(tok) && (kw = GetKw(tok->next->loc, tok->next->len))) { if (kw == KW_IF || kw == KW_IFDEF || kw == KW_IFNDEF) { tok = skip_cond_incl2(tok->next->next); continue; } if (kw == KW_ENDIF) { return tok->next->next; } } tok = tok->next; } return tok; } // Skip until next `#else`, `#elif` or `#endif`. // Nested `#if` and `#endif` are skipped. static Token *skip_cond_incl(Token *tok) { unsigned char kw; while (tok->kind != TK_EOF) { if (is_hash(tok) && (kw = GetKw(tok->next->loc, tok->next->len))) { if (kw == KW_IF || kw == KW_IFDEF || kw == KW_IFNDEF) { tok = skip_cond_incl2(tok->next->next); continue; } if (kw == KW_ELIF || kw == KW_ELSE || kw == KW_ENDIF) { break; } } tok = tok->next; } return tok; } // Double-quote a given string and returns it. static char *quote_string(char *str) { int bufsize = 3; for (int i = 0; str[i]; i++) { if (str[i] == '\\' || str[i] == '"') bufsize++; bufsize++; } char *buf = calloc(1, bufsize); char *p = buf; *p++ = '"'; for (int i = 0; str[i]; i++) { if (str[i] == '\\' || str[i] == '"') *p++ = '\\'; *p++ = str[i]; } *p++ = '"'; *p++ = '\0'; return buf; } static Token *new_str_token(char *str, Token *tmpl) { char *buf = quote_string(str); return tokenize(new_file(tmpl->file->name, tmpl->file->file_no, buf)); } // Copy all tokens until the next newline, terminate them with // an EOF token and then returns them. This function is used to // create a new list of tokens for `#if` arguments. static Token *copy_line(Token **rest, Token *tok) { Token head = {}; Token *cur = &head; for (; !tok->at_bol; tok = tok->next) { cur = cur->next = copy_token(tok); } cur->next = new_eof(tok); *rest = tok; return head.next; } static Token *new_num_token(int val, Token *tmpl) { char *p, *buf; p = buf = malloc(13); p = FormatInt32(p, val); p[0] = '\n'; p[1] = 0; return tokenize(new_file(tmpl->file->name, tmpl->file->file_no, buf)); } static Token *read_const_expr(Token **rest, Token *tok) { tok = copy_line(rest, tok); Token head = {}; Token *cur = &head; while (tok->kind != TK_EOF) { // "defined(foo)" or "defined foo" becomes "1" if macro "foo" // is defined. Otherwise "0". if (EQUAL(tok, "defined")) { Token *start = tok; bool has_paren = CONSUME(&tok, tok->next, "("); if (tok->kind != TK_IDENT) error_tok(start, "macro name must be an identifier"); Macro *m = find_macro(tok); tok = tok->next; if (has_paren) tok = skip(tok, ')'); cur = cur->next = new_num_token(m ? 1 : 0, start); continue; } cur = cur->next = tok; tok = tok->next; } cur->next = tok; return head.next; } // Read and evaluate a constant expression. static long eval_const_expr(Token **rest, Token *tok) { __arena_push(); Token *start = tok; Token *expr = read_const_expr(rest, tok->next); expr = preprocess2(expr); if (expr->kind == TK_EOF) error_tok(start, "no expression"); // [https://www.sigbus.info/n1570#6.10.1p4] The standard requires // we replace remaining non-macro identifiers with "0" before // evaluating a constant expression. For example, `#if foo` is // equivalent to `#if 0` if foo is not defined. for (Token *t = expr; t->kind != TK_EOF; t = t->next) { if (t->kind == TK_IDENT) { Token *next = t->next; *t = *new_num_token(0, t); t->next = next; } } // Convert pp-numbers to regular numbers convert_pp_tokens(expr); Token *rest2; long val = const_expr(&rest2, expr); if (rest2->kind != TK_EOF && rest2->kind != TK_JAVADOWN) { error_tok(rest2, "extra token"); } __arena_pop(); return val; } static CondIncl *push_cond_incl(Token *tok, bool included) { CondIncl *ci = calloc(1, sizeof(CondIncl)); ci->next = cond_incl; ci->ctx = IN_THEN; ci->tok = tok; ci->included = included; cond_incl = ci; return ci; } static Macro *find_macro(Token *tok) { if (tok->kind != TK_IDENT) return NULL; return hashmap_get2(&macros, tok->loc, tok->len); } static Macro *add_macro(char *name, bool is_objlike, Token *body) { Macro *m = calloc(1, sizeof(Macro)); m->name = name; m->is_objlike = is_objlike; m->body = body; hashmap_put(&macros, name, m); return m; } static MacroParam *read_macro_params(Token **rest, Token *tok, char **va_args_name) { MacroParam head = {}; MacroParam *cur = &head; while (!EQUAL(tok, ")")) { if (cur != &head) tok = skip(tok, ','); if (EQUAL(tok, "...")) { *va_args_name = "__VA_ARGS__"; *rest = skip(tok->next, ')'); return head.next; } if (tok->kind == TK_JAVADOWN) { tok = tok->next; } if (tok->kind != TK_IDENT) { error_tok(tok, "expected an identifier"); } if (EQUAL(tok->next, "...")) { *va_args_name = strndup(tok->loc, tok->len); *rest = skip(tok->next->next, ')'); return head.next; } MacroParam *m = calloc(1, sizeof(MacroParam)); m->name = strndup(tok->loc, tok->len); cur = cur->next = m; tok = tok->next; } *rest = tok->next; return head.next; } static Macro *read_macro_definition(Token **rest, Token *tok) { Macro *m; char *name; if (tok->kind != TK_IDENT) error_tok(tok, "macro name must be an identifier"); name = strndup(tok->loc, tok->len); tok = tok->next; if (!tok->has_space && tok->len == 1 && tok->loc[0] == '(') { // Function-like macro char *va_args_name = NULL; MacroParam *params = read_macro_params(&tok, tok->next, &va_args_name); m = add_macro(name, false, copy_line(rest, tok)); m->params = params; m->va_args_name = va_args_name; } else { // Object-like macro m = add_macro(name, true, copy_line(rest, tok)); } return m; } static MacroArg *read_macro_arg_one(Token **rest, Token *tok, bool read_rest) { Token head = {}; Token *cur = &head; int level = 0; for (;;) { if (level == 0 && tok->len == 1 && tok->loc[0] == ')') { break; } if (level == 0 && !read_rest && tok->len == 1 && tok->loc[0] == ',') { break; } if (tok->kind == TK_EOF) error_tok(tok, "premature end of input"); if (tok->len == 1 && tok->loc[0] == '(') { level++; } else if (tok->len == 1 && tok->loc[0] == ')') { level--; } cur = cur->next = copy_token(tok); tok = tok->next; } cur->next = new_eof(tok); MacroArg *arg = calloc(1, sizeof(MacroArg)); arg->tok = head.next; *rest = tok; return arg; } static MacroArg *read_macro_args(Token **rest, Token *tok, MacroParam *params, char *va_args_name) { Token *start = tok; tok = tok->next->next; MacroArg head = {}; MacroArg *cur = &head; MacroParam *pp = params; for (; pp; pp = pp->next) { if (cur != &head) tok = skip(tok, ','); cur = cur->next = read_macro_arg_one(&tok, tok, false); cur->name = pp->name; } if (va_args_name) { MacroArg *arg; if (tok->len == 1 && tok->loc[0] == ')') { arg = calloc(1, sizeof(MacroArg)); arg->tok = new_eof(tok); } else { if (pp != params) tok = skip(tok, ','); arg = read_macro_arg_one(&tok, tok, true); } arg->name = va_args_name; arg->is_va_args = true; cur = cur->next = arg; } else if (pp) { error_tok(start, "too many arguments"); } skip(tok, ')'); *rest = tok; return head.next; } static MacroArg *find_arg(MacroArg *args, Token *tok) { for (MacroArg *ap = args; ap; ap = ap->next) { if (tok->len == strlen(ap->name) && !strncmp(tok->loc, ap->name, tok->len)) { return ap; } } return NULL; } // Concatenates all tokens in `tok` and returns a new string. static char *join_tokens(Token *tok, Token *end) { // Compute the length of the resulting token. int len = 1; for (Token *t = tok; t != end && t->kind != TK_EOF; t = t->next) { if (t != tok && t->has_space) len++; len += t->len; } char *buf = calloc(1, len); // Copy token texts. int pos = 0; for (Token *t = tok; t != end && t->kind != TK_EOF; t = t->next) { if (t != tok && t->has_space) buf[pos++] = ' '; strncpy(buf + pos, t->loc, t->len); pos += t->len; } buf[pos] = '\0'; return buf; } // Concatenates all tokens in `arg` and returns a new string token. // This function is used for the stringizing operator (#). static Token *stringize(Token *hash, Token *arg) { // Create a new string token. We need to set some value to its // source location for error reporting function, so we use a macro // name token as a template. char *s = join_tokens(arg, NULL); return new_str_token(s, hash); } // Concatenate two tokens to create a new token. static Token *paste(Token *lhs, Token *rhs) { // Paste the two tokens. char *buf = xasprintf("%.*s%.*s", lhs->len, lhs->loc, rhs->len, rhs->loc); // Tokenize the resulting string. Token *tok = tokenize(new_file(lhs->file->name, lhs->file->file_no, buf)); if (tok->next->kind != TK_EOF) error_tok(lhs, "pasting forms '%s', an invalid token", buf); return tok; } static bool has_varargs(MacroArg *args) { for (MacroArg *ap = args; ap; ap = ap->next) { if (!strcmp(ap->name, "__VA_ARGS__")) { return ap->tok->kind != TK_EOF; } } return false; } // Replace func-like macro parameters with given arguments. static Token *subst(Token *tok, MacroArg *args) { Token head = {}; Token *cur = &head; while (tok->kind != TK_EOF) { // "#" followed by a parameter is replaced with stringized actuals. if (tok->len == 1 && tok->loc[0] == '#') { MacroArg *arg = find_arg(args, tok->next); if (!arg) { error_tok(tok->next, "'#' is not followed by a macro parameter"); } cur = cur->next = stringize(tok, arg->tok); tok = tok->next->next; continue; } // [GNU] If __VA_ARG__ is empty, `,##__VA_ARGS__` is expanded // to the empty token list. Otherwise, its expaned to `,` and // __VA_ARGS__. if (tok->len == 1 && tok->loc[0] == ',' && (tok->next->len == 2 && (tok->next->loc[0] == '#' && tok->next->loc[1] == '#'))) { MacroArg *arg = find_arg(args, tok->next->next); if (arg && arg->is_va_args) { if (arg->tok->kind == TK_EOF) { tok = tok->next->next->next; } else { cur = cur->next = copy_token(tok); tok = tok->next->next; } continue; } } if (tok->len == 2 && tok->loc[0] == '#' && tok->loc[1] == '#') { if (cur == &head) error_tok(tok, "'##' cannot appear at start of macro expansion"); if (tok->next->kind == TK_EOF) error_tok(tok, "'##' cannot appear at end of macro expansion"); MacroArg *arg = find_arg(args, tok->next); if (arg) { if (arg->tok->kind != TK_EOF) { *cur = *paste(cur, arg->tok); for (Token *t = arg->tok->next; t->kind != TK_EOF; t = t->next) cur = cur->next = copy_token(t); } tok = tok->next->next; continue; } *cur = *paste(cur, tok->next); tok = tok->next->next; continue; } MacroArg *arg = find_arg(args, tok); if (arg && (tok->next->len == 2 && (tok->next->loc[0] == '#' && tok->next->loc[1] == '#'))) { Token *rhs = tok->next->next; if (arg->tok->kind == TK_EOF) { MacroArg *arg2 = find_arg(args, rhs); if (arg2) { for (Token *t = arg2->tok; t->kind != TK_EOF; t = t->next) cur = cur->next = copy_token(t); } else { cur = cur->next = copy_token(rhs); } tok = rhs->next; continue; } for (Token *t = arg->tok; t->kind != TK_EOF; t = t->next) cur = cur->next = copy_token(t); tok = tok->next; continue; } // If __VA_ARG__ is empty, __VA_OPT__(x) is expanded to the // empty token list. Otherwise, __VA_OPT__(x) is expanded to x. if (EQUAL(tok, "__VA_OPT__") && EQUAL(tok->next, "(")) { MacroArg *arg = read_macro_arg_one(&tok, tok->next->next, true); if (has_varargs(args)) for (Token *t = arg->tok; t->kind != TK_EOF; t = t->next) cur = cur->next = t; tok = skip(tok, ')'); continue; } // Handle a macro token. Macro arguments are completely macro-expanded // before they are substituted into a macro body. if (arg) { Token *t = preprocess2(arg->tok); t->at_bol = tok->at_bol; t->has_space = tok->has_space; for (; t->kind != TK_EOF; t = t->next) cur = cur->next = copy_token(t); tok = tok->next; continue; } // Handle a non-macro token. cur = cur->next = copy_token(tok); tok = tok->next; continue; } cur->next = tok; return head.next; } // If tok is a macro, expand it and return true. // Otherwise, do nothing and return false. static bool expand_macro(Token **rest, Token *tok) { if (hideset_contains(tok->hideset, tok->loc, tok->len)) return false; Macro *m = find_macro(tok); if (!m) return false; // Built-in dynamic macro application such as __LINE__ if (m->handler) { *rest = m->handler(tok); (*rest)->next = tok->next; return true; } // Object-like macro application if (m->is_objlike) { Hideset *hs = hideset_union(tok->hideset, new_hideset(m->name)); Token *body = add_hideset(m->body, hs); for (Token *t = body; t->kind != TK_EOF; t = t->next) { t->origin = tok; } *rest = append(body, tok->next); (*rest)->at_bol = tok->at_bol; (*rest)->has_space = tok->has_space; return true; } // If a funclike macro token is not followed by an argument list, // treat it as a normal identifier. if (!(tok->next->len == 1 && tok->next->loc[0] == '(')) return false; // Function-like macro application Token *macro_token = tok; MacroArg *args = read_macro_args(&tok, tok, m->params, m->va_args_name); Token *rparen = tok; // Tokens that consist a func-like macro invocation may have different // hidesets, and if that's the case, it's not clear what the hideset // for the new tokens should be. We take the interesection of the // macro token and the closing parenthesis and use it as a new hideset // as explained in the Dave Prossor's algorithm. Hideset *hs = hideset_intersection(macro_token->hideset, rparen->hideset); hs = hideset_union(hs, new_hideset(m->name)); Token *body = subst(m->body, args); body = add_hideset(body, hs); for (Token *t = body; t->kind != TK_EOF; t = t->next) t->origin = macro_token; *rest = append(body, tok->next); (*rest)->at_bol = macro_token->at_bol; (*rest)->has_space = macro_token->has_space; return true; } char *search_include_paths(char *filename) { if (filename[0] == '/') return filename; static HashMap cache; char *cached = hashmap_get(&cache, filename); if (cached) return cached; // Search a file from the include paths. for (int i = 0; i < include_paths.len; i++) { char *path = xjoinpaths(include_paths.data[i], filename); if (!fileexists(path)) continue; hashmap_put(&cache, filename, path); include_next_idx = i + 1; return path; } return NULL; } static char *search_include_next(char *filename) { for (; include_next_idx < include_paths.len; include_next_idx++) { char *path = xasprintf("%s/%s", include_paths.data[include_next_idx], filename); if (fileexists(path)) return path; } return NULL; } // Read an #include argument. static char *read_include_filename(Token **rest, Token *tok, bool *is_dquote) { // Pattern 1: #include "foo.h" if (tok->kind == TK_STR) { // A double-quoted filename for #include is a special kind of // token, and we don't want to interpret any escape sequences in it. // For example, "\f" in "C:\foo" is not a formfeed character but // just two non-control characters, backslash and f. // So we don't want to use token->str. *is_dquote = true; *rest = skip_line(tok->next); return strndup(tok->loc + 1, tok->len - 2); } // Pattern 2: #include <foo.h> if (EQUAL(tok, "<")) { // Reconstruct a filename from a sequence of tokens between // "<" and ">". Token *start = tok; // Find closing ">". for (; !EQUAL(tok, ">"); tok = tok->next) if (tok->at_bol || tok->kind == TK_EOF) error_tok(tok, "expected '>'"); *is_dquote = false; *rest = skip_line(tok->next); return join_tokens(start->next, tok); } // Pattern 3: #include FOO // In this case FOO must be macro-expanded to either // a single string token or a sequence of "<" ... ">". if (tok->kind == TK_IDENT) { Token *tok2 = preprocess2(copy_line(rest, tok)); return read_include_filename(&tok2, tok2, is_dquote); } error_tok(tok, "expected a filename"); } // Detect the following "include guard" pattern. // // #ifndef FOO_H // #define FOO_H // ... // #endif static char *detect_include_guard(Token *tok) { // Detect the first two lines. if (!is_hash(tok) || !EQUAL(tok->next, "ifndef")) return NULL; tok = tok->next->next; if (tok->kind != TK_IDENT) return NULL; char *macro = strndup(tok->loc, tok->len); tok = tok->next; if (!is_hash(tok) || !EQUAL(tok->next, "define") || !equal(tok->next->next, macro, strlen(macro))) return NULL; // Read until the end of the file. while (tok->kind != TK_EOF) { if (!is_hash(tok)) { tok = tok->next; continue; } if (EQUAL(tok->next, "endif") && tok->next->next->kind == TK_EOF) return macro; if (EQUAL(tok, "if") || EQUAL(tok, "ifdef") || EQUAL(tok, "ifndef")) tok = skip_cond_incl(tok->next); else tok = tok->next; } return NULL; } static Token *include_file(Token *tok, char *path, Token *filename_tok) { // Check for "#pragma once" if (hashmap_get(&pragma_once, path)) return tok; // If we read the same file before, and if the file was guarded // by the usual #ifndef ... #endif pattern, we may be able to // skip the file without opening it. static HashMap include_guards; char *guard_name = hashmap_get(&include_guards, path); if (guard_name && hashmap_get(&macros, guard_name)) return tok; Token *tok2 = tokenize_file(path); if (!tok2) error_tok(filename_tok, "%s: cannot open file: %s", path, strerror(errno)); guard_name = detect_include_guard(tok2); if (guard_name) hashmap_put(&include_guards, path, guard_name); return append(tok2, tok); } // Read #line arguments static void read_line_marker(Token **rest, Token *tok) { // TODO: This is broken if file is different? See gperf codegen. Token *start = tok; tok = preprocess(copy_line(rest, tok)); if (tok->kind != TK_NUM || tok->ty->kind != TY_INT) error_tok(tok, "invalid line marker"); start->file->line_delta = tok->val - start->line_no; tok = tok->next; if (tok->kind == TK_EOF) return; if (tok->kind != TK_STR) error_tok(tok, "filename expected"); start->file->display_name = tok->str; } // Visit all tokens in `tok` while evaluating preprocessing // macros and directives. static Token *preprocess2(Token *tok) { unsigned char kw; Token head = {}; Token *cur = &head; while (tok->kind != TK_EOF) { // If it is a macro, expand it. if (expand_macro(&tok, tok)) continue; // make sure javadown is removed if it's for a macro definition if (tok->kind == TK_JAVADOWN && is_hash(tok->next) && EQUAL(tok->next->next, "define")) { read_macro_definition(&tok, tok->next->next->next)->javadown = tok; continue; } // Pass through if it is not a "#". if (!is_hash(tok)) { tok->line_delta = tok->file->line_delta; tok->filename = tok->file->display_name; cur = cur->next = tok; tok = tok->next; continue; } Token *start = tok; tok = tok->next; if ((kw = GetKw(tok->loc, tok->len))) { if (kw == KW_INCLUDE) { bool is_dquote; char *filename = read_include_filename(&tok, tok->next, &is_dquote); if (filename[0] != '/' && is_dquote) { char *tmp = strdup(start->file->name); char *path = xasprintf("%s/%s", dirname(tmp), filename); free(tmp); bool exists = fileexists(path); free(path); if (exists) { tok = include_file(tok, path, start->next->next); continue; } } char *path = search_include_paths(filename); tok = include_file(tok, path ? path : filename, start->next->next); continue; } if (kw == KW_INCLUDE_NEXT) { bool ignore; char *filename = read_include_filename(&tok, tok->next, &ignore); char *path = search_include_next(filename); tok = include_file(tok, path ? path : filename, start->next->next); continue; } if (kw == KW_DEFINE) { read_macro_definition(&tok, tok->next); continue; } if (kw == KW_UNDEF) { tok = tok->next; if (tok->kind != TK_IDENT) error_tok(tok, "macro name must be an identifier"); undef_macro(strndup(tok->loc, tok->len)); tok = skip_line(tok->next); continue; } if (kw == KW_IF) { long val = eval_const_expr(&tok, tok); push_cond_incl(start, val); if (!val) tok = skip_cond_incl(tok); continue; } if (kw == KW_IFDEF) { bool defined = find_macro(tok->next); push_cond_incl(tok, defined); tok = skip_line(tok->next->next); if (!defined) tok = skip_cond_incl(tok); continue; } if (kw == KW_IFNDEF) { bool defined = find_macro(tok->next); push_cond_incl(tok, !defined); tok = skip_line(tok->next->next); if (defined) tok = skip_cond_incl(tok); continue; } if (kw == KW_ELIF) { if (!cond_incl || cond_incl->ctx == IN_ELSE) error_tok(start, "stray #elif"); cond_incl->ctx = IN_ELIF; if (!cond_incl->included && eval_const_expr(&tok, tok)) cond_incl->included = true; else tok = skip_cond_incl(tok); continue; } if (kw == KW_ELSE) { if (!cond_incl || cond_incl->ctx == IN_ELSE) error_tok(start, "stray #else"); cond_incl->ctx = IN_ELSE; tok = skip_line(tok->next); if (cond_incl->included) tok = skip_cond_incl(tok); continue; } if (kw == KW_ENDIF) { if (!cond_incl) error_tok(start, "stray #endif"); cond_incl = cond_incl->next; tok = skip_line(tok->next); continue; } if (kw == KW_LINE) { read_line_marker(&tok, tok->next); continue; } } if (tok->kind == TK_PP_NUM) { read_line_marker(&tok, tok); continue; } if (kw == KW_PRAGMA && EQUAL(tok->next, "once")) { hashmap_put(&pragma_once, tok->file->name, (void *)1); tok = skip_line(tok->next->next); continue; } if (kw == KW_PRAGMA) { do { tok = tok->next; } while (!tok->at_bol); continue; } if (kw == KW_ERROR) { error_tok(tok, "error"); } // `#`-only line is legal. It's called a null directive. if (tok->at_bol) continue; error_tok(tok, "invalid preprocessor directive"); } cur->next = tok; return head.next; } void define_macro(char *name, char *buf) { Token *tok = tokenize(new_file("<built-in>", 1, buf)); add_macro(name, true, tok); } void undef_macro(char *name) { hashmap_delete(&macros, name); } static Macro *add_builtin(char *name, macro_handler_fn *fn) { Macro *m = add_macro(name, true, NULL); m->handler = fn; return m; } static Token *file_macro(Token *tmpl) { while (tmpl->origin) tmpl = tmpl->origin; return new_str_token(tmpl->file->display_name, tmpl); } static Token *line_macro(Token *tmpl) { while (tmpl->origin) tmpl = tmpl->origin; int i = tmpl->line_no + tmpl->file->line_delta; return new_num_token(i, tmpl); } // __COUNTER__ is expanded to serial values starting from 0. static Token *counter_macro(Token *tmpl) { static int i = 0; return new_num_token(i++, tmpl); } // __TIMESTAMP__ is expanded to a string describing the last // modification time of the current file. E.g. // "Fri Jul 24 01:32:50 2020" static Token *timestamp_macro(Token *tmpl) { struct stat st; if (stat(tmpl->file->name, &st) != 0) return new_str_token("??? ??? ?? ??:??:?? ????", tmpl); char buf[64]; ctime_r(&st.st_mtim.tv_sec, buf); buf[24] = '\0'; return new_str_token(buf, tmpl); } static Token *base_file_macro(Token *tmpl) { return new_str_token(base_file, tmpl); } // __DATE__ is expanded to the current date, e.g. "May 17 2020". static char *format_date(struct tm *tm) { return xasprintf("\"%s %2d %d\"", kMonthNameShort[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900); } // __TIME__ is expanded to the current time, e.g. "13:34:03". static char *format_time(struct tm *tm) { return xasprintf("\"%02d:%02d:%02d\"", tm->tm_hour, tm->tm_min, tm->tm_sec); } void init_macros_conditional(void) { if (opt_pg) define_macro("__PG__", "1"); if (opt_pic) define_macro("__PIC__", "1"); if (opt_sse3) define_macro("__SSE3__", "1"); if (opt_sse4) define_macro("__SSE4__", "1"); if (opt_popcnt) define_macro("__POPCNT__", "1"); if (opt_fentry) define_macro("__MFENTRY__", "1"); } void init_macros(void) { char *val, *name = "\ __chibicc__\000\ 1\000\ __cosmopolitan__\000\ 1\000\ __GNUC__\000\ 9\000\ __GNUC_MINOR__\000\ 0\000\ __GNUC_PATCHLEVEL__\000\ 0\000\ __NO_INLINE__\000\ 16\000\ __GNUC_STDC_INLINE__\000\ 1\000\ __BIGGEST_ALIGNMENT__\000\ 16\000\ __C99_MACRO_WITH_VA_ARGS\000\ 1\000\ __GCC_ASM_FLAG_OUTPUTS__\000\ 1\000\ __ELF__\000\ 1\000\ __LP64__\000\ 1\000\ _LP64\000\ 1\000\ __STDC__\000\ 1\000\ __STDC_HOSTED__\000\ 1\000\ __STDC_NO_COMPLEX__\000\ 1\000\ __STDC_UTF_16__\000\ 1\000\ __STDC_UTF_32__\000\ 1\000\ __STDC_VERSION__\000\ 201112L\000\ __USER_LABEL_PREFIX__\000\ \000\ __alignof__\000\ _Alignof\000\ __const__\000\ const\000\ __inline__\000\ inline\000\ __signed__\000\ signed\000\ __typeof__\000\ typeof\000\ __volatile__\000\ volatile\000\ __unix\000\ 1\000\ __unix__\000\ 1\000\ __linux\000\ 1\000\ __linux__\000\ 1\000\ __gnu_linux__\000\ 1\000\ __BYTE_ORDER__\000\ 1234\000\ __FLOAT_WORD_ORDER__\000\ 1234\000\ __ORDER_BIG_ENDIAN__\000\ 4321\000\ __ORDER_LITTLE_ENDIAN__\000\ 1234\000\ __INT8_MAX__\000\ 0x7f\000\ __UINT8_MAX__\000\ 0xff\000\ __INT16_MAX__\000\ 0x7fff\000\ __UINT16_MAX__\000\ 0xffff\000\ __SHRT_MAX__\000\ 0x7fff\000\ __INT_MAX__\000\ 0x7fffffff\000\ __INT32_MAX__\000\ 0x7fffffff\000\ __UINT32_MAX__\000\ 0xffffffffu\000\ __INT64_MAX__\000\ 0x7fffffffffffffffl\000\ __INTMAX_MAX__\000\ 0x7fffffffffffffffl\000\ __LONG_MAX__\000\ 0x7fffffffffffffffl\000\ __LONG_LONG_MAX__\000\ 0x7fffffffffffffffl\000\ __UINT64_MAX__\000\ 0xfffffffffffffffful\000\ __UINTMAX_MAX__\000\ 0xfffffffffffffffful\000\ __SIZE_MAX__\000\ 0xfffffffffffffffful\000\ __INTPTR_MAX__\000\ 0x7fffffffffffffffl\000\ __UINTPTR_MAX__\000\ 0xfffffffffffffffful\000\ __WINT_MAX__\000\ 0xffffffffu\000\ __CHAR_BIT__\000\ 8\000\ __SIZEOF_SHORT__\000\ 2\000\ __SIZEOF_INT__\000\ 4\000\ __SIZEOF_LONG__\000\ 8\000\ __SIZEOF_LONG_LONG__\000\ 8\000\ __SIZEOF_POINTER__\000\ 8\000\ __SIZEOF_PTRDIFF_T__\000\ 8\000\ __SIZEOF_SIZE_T__\000\ 8\000\ __SIZEOF_WCHAR_T__\000\ 4\000\ __SIZEOF_WINT_T__\000\ 4\000\ __SIZEOF_FLOAT__\000\ 4\000\ __SIZEOF_FLOAT128__\000\ 16\000\ __SIZEOF_DOUBLE__\000\ 8\000\ __SIZEOF_FLOAT80__\000\ 16\000\ __SIZEOF_LONG_DOUBLE__\000\ 16\000\ __INT8_TYPE__\000\ signed char\000\ __UINT8_TYPE__\000\ unsigned char\000\ __INT16_TYPE__\000\ short int\000\ __UINT16_TYPE__\000\ short unsigned int\000\ __INT32_TYPE__\000\ int\000\ __UINT32_TYPE__\000\ unsigned int\000\ __INT64_TYPE__\000\ long int\000\ __INTMAX_TYPE__\000\ long int\000\ __UINT64_TYPE__\000\ long unsigned int\000\ __UINTMAX_TYPE__\000\ long unsigned int\000\ __INTMAX_TYPE__\000\ long int\000\ __UINTMAX_TYPE__\000\ long unsigned int\000\ __INTPTR_TYPE__\000\ long int\000\ __UINTPTR_TYPE__\000\ long unsigned int\000\ __PTRDIFF_TYPE__\000\ long int\000\ __SIZE_TYPE__\000\ long unsigned int\000\ __WCHAR_TYPE__\000\ int\000\ __CHAR16_TYPE__\000\ short unsigned int\000\ __CHAR32_TYPE__\000\ unsigned int\000\ __WINT_TYPE__\000\ unsigned int\000\ __CHAR16_TYPE__\000\ short unsigned int\000\ __WCHAR_TYPE__\000\ int\000\ __CHAR32_TYPE__\000\ unsigned int\000\ __INT_LEAST8_TYPE__\000\ signed char\000\ __UINT_LEAST8_TYPE__\000\ unsigned char\000\ __INT_LEAST16_TYPE__\000\ int\000\ __UINT_LEAST16_TYPE__\000\ unsigned short\000\ __INT_LEAST32_TYPE__\000\ short\000\ __UINT_LEAST32_TYPE__\000\ unsigned int\000\ __INT_LEAST64_TYPE__\000\ long\000\ __UINT_LEAST64_TYPE__\000\ unsigned long\000\ __INT_FAST8_TYPE__\000\ signed char\000\ __UINT_FAST8_TYPE__\000\ unsigned char\000\ __INT_FAST16_TYPE__\000\ int\000\ __UINT_FAST16_TYPE__\000\ unsigned\000\ __INT_FAST32_TYPE__\000\ int\000\ __UINT_FAST32_TYPE__\000\ unsigned\000\ __INT_FAST8_MAX__\000\ 0x7f\000\ __INT_FAST16_MAX__\000\ 0x7fffffff\000\ __INT_FAST32_MAX__\000\ 0x7fffffff\000\ __INT_FAST64_MAX__\000\ 0x7fffffffffffffffl\000\ __INT_FAST64_TYPE__\000\ long\000\ __UINT_FAST64_TYPE__\000\ unsigned long\000\ __DBL_DECIMAL_DIG__\000\ 17\000\ __DBL_DENORM_MIN__\000\ ((double)4.94065645841246544176568792868221372e-324L)\000\ __DBL_DIG__\000\ 15\000\ __DBL_EPSILON__\000\ ((double)2.22044604925031308084726333618164062e-16L)\000\ __DBL_HAS_DENORM__\000\ 1\000\ __DBL_HAS_INFINITY__\000\ 1\000\ __DBL_HAS_QUIET_NAN__\000\ 1\000\ __DBL_MANT_DIG__\000\ 53\000\ __DBL_MAX_10_EXP__\000\ 308\000\ __DBL_MAX_EXP__\000\ 1024\000\ __DBL_MAX__\000\ ((double)1.79769313486231570814527423731704357e+308L)\000\ __DBL_MIN_10_EXP__\000\ (-307)\000\ __DBL_MIN_EXP__\000\ (-1021)\000\ __DBL_MIN__\000\ ((double)2.22507385850720138309023271733240406e-308L)\000\ __FLT_DECIMAL_DIG__\000\ 9\000\ __FLT_DENORM_MIN__\000\ 1.40129846432481707092372958328991613e-45F\000\ __FLT_DIG__\000\ 6\000\ __FLT_EPSILON__\000\ 1.19209289550781250000000000000000000e-7F\000\ __FLT_EVAL_METHOD_TS_18661_3__\000\ 0\000\ __FLT_EVAL_METHOD__\000\ 0\000\ __FLT_HAS_DENORM__\000\ 1\000\ __FLT_HAS_INFINITY__\000\ 1\000\ __FLT_HAS_QUIET_NAN__\000\ 1\000\ __FLT_MANT_DIG__\000\ 24\000\ __FLT_MAX_10_EXP__\000\ 38\000\ __FLT_MAX_EXP__\000\ 128\000\ __FLT_MAX__\000\ 3.40282346638528859811704183484516925e+38F\000\ __FLT_MIN_10_EXP__\000\ (-37)\000\ __FLT_MIN_EXP__\000\ (-125)\000\ __FLT_MIN__\000\ 1.17549435082228750796873653722224568e-38F\000\ __FLT_RADIX__\000\ 2\000\ __LDBL_DECIMAL_DIG__\000\ 21\000\ __LDBL_DENORM_MIN__\000\ 3.64519953188247460252840593361941982e-4951L\000\ __LDBL_DIG__\000\ 18\000\ __LDBL_EPSILON__\000\ 1.08420217248550443400745280086994171e-19L\000\ __LDBL_HAS_DENORM__\000\ 1\000\ __LDBL_HAS_INFINITY__\000\ 1\000\ __LDBL_HAS_QUIET_NAN__\000\ 1\000\ __LDBL_MANT_DIG__\000\ 64\000\ __LDBL_MAX_10_EXP__\000\ 4932\000\ __LDBL_MAX_EXP__\000\ 16384\000\ __LDBL_MAX__\000\ 1.18973149535723176502126385303097021e+4932L\000\ __LDBL_MIN_10_EXP__\000\ (-4931)\000\ __LDBL_MIN_EXP__\000\ (-16381)\000\ __LDBL_MIN__\000\ 3.36210314311209350626267781732175260e-4932L\000\ __x86_64\000\ 1\000\ __x86_64__\000\ 1\000\ __amd64\000\ 1\000\ __amd64__\000\ 1\000\ __MMX__\000\ 1\000\ __SSE__\000\ 1\000\ __SSE_MATH__\000\ 1\000\ __SSE2__\000\ 1\000\ __SSE2_MATH__\000\ 1\000\ __ATOMIC_ACQUIRE\000\ 2\000\ __ATOMIC_HLE_RELEASE\000\ 131072\000\ __ATOMIC_HLE_ACQUIRE\000\ 65536\000\ __ATOMIC_RELAXED\000\ 0\000\ __ATOMIC_CONSUME\000\ 1\000\ __ATOMIC_SEQ_CST\000\ 5\000\ __ATOMIC_ACQ_REL\000\ 4\000\ __ATOMIC_RELEASE\000\ 3\000\ \000"; do { val = name + strlen(name) + 1; define_macro(name, val); name = val + strlen(val) + 1; } while (*name); add_builtin("__FILE__", file_macro); add_builtin("__LINE__", line_macro); add_builtin("__COUNTER__", counter_macro); add_builtin("__TIMESTAMP__", timestamp_macro); add_builtin("__BASE_FILE__", base_file_macro); time_t now = time(NULL); struct tm *tm = localtime(&now); define_macro("__DATE__", format_date(tm)); define_macro("__TIME__", format_time(tm)); } static StringKind getStringKind(Token *tok) { if (!strcmp(tok->loc, "u8")) return STR_UTF8; switch (tok->loc[0]) { case '"': return STR_NONE; case 'u': return STR_UTF16; case 'U': return STR_UTF32; case 'L': return STR_WIDE; } UNREACHABLE(); } // Concatenate adjacent string literals into a single string literal // as per the C spec. static void join_adjacent_string_literals(Token *tok) { // First pass: If regular string literals are adjacent to wide // string literals, regular string literals are converted to a wide // type before concatenation. In this pass, we do the conversion. for (Token *tok1 = tok; tok1->kind != TK_EOF;) { if (tok1->kind != TK_STR || tok1->next->kind != TK_STR) { tok1 = tok1->next; continue; } StringKind kind = getStringKind(tok1); Type *basety = tok1->ty->base; for (Token *t = tok1->next; t->kind == TK_STR; t = t->next) { StringKind k = getStringKind(t); if (kind == STR_NONE) { kind = k; basety = t->ty->base; } else if (k != STR_NONE && kind != k) { error_tok(t, "unsupported non-standard concatenation of string literals"); } } if (basety->size > 1) { for (Token *t = tok1; t->kind == TK_STR; t = t->next) { if (t->ty->base->size == 1) { *t = *tokenize_string_literal(t, basety); } } } while (tok1->kind == TK_STR) tok1 = tok1->next; } // Second pass: concatenate adjacent string literals. for (Token *tok1 = tok; tok1->kind != TK_EOF;) { if (tok1->kind != TK_STR || tok1->next->kind != TK_STR) { tok1 = tok1->next; continue; } Token *tok2 = tok1->next; while (tok2->kind == TK_STR) tok2 = tok2->next; int len = tok1->ty->array_len; for (Token *t = tok1->next; t != tok2; t = t->next) { len = len + t->ty->array_len - 1; } char *buf = calloc(tok1->ty->base->size, len); int j = 0; int i = 0; for (Token *t = tok1; t != tok2; t = t->next) { ++j; memcpy(buf + i, t->str, t->ty->size); i = i + t->ty->size - t->ty->base->size; } *tok1 = *copy_token(tok1); tok1->ty = array_of(tok1->ty->base, len); tok1->str = buf; tok1->next = tok2; tok1 = tok2; } } // Entry point function of the preprocessor. Token *preprocess(Token *tok) { tok = preprocess2(tok); if (cond_incl) { error_tok(cond_incl->tok, "unterminated conditional directive"); } convert_pp_tokens(tok); join_adjacent_string_literals(tok); for (Token *t = tok; t; t = t->next) { t->line_no += t->line_delta; } return tok; }
39,157
1,420
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/printast.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/mem/arraylist2.internal.h" #include "third_party/chibicc/chibicc.h" static const char kBoolStr[2][6] = {"false", "true"}; static const char kTypeKindStr[17][8] = { "VOID", "BOOL", "CHAR", "SHORT", "INT", "LONG", "INT128", "FLOAT", "DOUBLE", "LDOUBLE", "ENUM", "PTR", "FUNC", "ARRAY", "VLA", "STRUCT", "UNION", }; static const char kNodeKindStr[50][11] = { "NULL_EXPR", "ADD", "SUB", "MUL", "DIV", "NEG", "REM", "BINAND", "BINOR", "BINXOR", "SHL", "SHR", "EQ", "NE", "LT", "LE", "ASSIGN", "COND", "COMMA", "MEMBER", "ADDR", "DEREF", "NOT", "BITNOT", "LOGAND", "LOGOR", "RETURN", "IF", "FOR", "DO", "SWITCH", "CASE", "BLOCK", "GOTO", "GOTO_EXPR", "LABEL", "LABEL_VAL", "FUNCALL", "EXPR_STMT", "STMT_EXPR", "VAR", "VLA_PTR", "NUM", "CAST", "MEMZERO", "ASM", "CAS", "EXCH", "FPCLASSIFY", }; static struct Visited { size_t i, n; intptr_t *p; } g_visited; static void PrintObj(FILE *, int, const char *, Obj *); static void PrintNode(FILE *, int, const char *, Node *); static void PrintType(FILE *, int, const char *, Type *); static bool Visit(void *ptr) { size_t i; intptr_t addr = (intptr_t)ptr; for (i = 0; i < g_visited.i; ++i) { if (addr == g_visited.p[i]) { return false; } } APPEND(&g_visited.p, &g_visited.i, &g_visited.n, &addr); return true; } static void PrintLine(FILE *f, int l, char *fmt, ...) { int i; va_list ap; for (i = 0; i < l; ++i) fputc(' ', f); va_start(ap, fmt); vfprintf(f, fmt, ap); va_end(ap); fputc('\n', f); } static void PrintBool(FILE *f, int l, const char *s, bool b) { if (!b) return; PrintLine(f, l, "%s%s", s, kBoolStr[b]); } static void PrintInt(FILE *f, int l, const char *s, long x) { if (!x) return; PrintLine(f, l, "%s%ld", s, x); } static void PrintStr(FILE *f, int l, const char *s, const char *t) { if (!t || !*t) return; PrintLine(f, l, "%s%`'s", s, t); } static void PrintTokStr(FILE *f, int l, const char *s, Token *t) { if (!t) return; PrintLine(f, l, "%s%`'.*s", s, t->len, t->loc); } static void PrintMember(FILE *f, int l, const char *s, Member *m) { if (!m) return; PrintLine(f, l, "%sMember { # %p", s, m); PrintTokStr(f, l + 2, "name: ", m->name); PrintInt(f, l + 2, "idx: ", m->idx); PrintInt(f, l + 2, "align: ", m->align); PrintInt(f, l + 2, "offset: ", m->offset); PrintBool(f, l + 2, "is_bitfield: ", m->is_bitfield); PrintInt(f, l + 2, "bit_offset: ", m->bit_offset); PrintInt(f, l + 2, "bit_width: ", m->bit_width); PrintType(f, l + 2, "ty: ", m->ty); PrintLine(f, l, "}"); } static void PrintMembers(FILE *f, int l, const char *s, Member *m) { for (; m; m = m->next) { PrintMember(f, l, s, m); } } static void PrintType(FILE *f, int l, const char *s, Type *t) { for (; t; t = t->next) { if (Visit(t)) { PrintLine(f, l, "%sType { # %p", s, t); PrintLine(f, l + 2, "kind: TY_%s", kTypeKindStr[t->kind]); PrintInt(f, l + 2, "size: ", t->size); PrintInt(f, l + 2, "align: ", t->align); PrintBool(f, l + 2, "is_unsigned: ", t->is_unsigned); PrintBool(f, l + 2, "is_atomic: ", t->is_atomic); PrintType(f, l + 2, "base: ", t->base); PrintTokStr(f, l + 2, "name: ", t->name); PrintTokStr(f, l + 2, "name_pos: ", t->name_pos); PrintInt(f, l + 2, "array_len: ", t->array_len); PrintInt(f, l + 2, "vector_size: ", t->vector_size); PrintNode(f, l + 2, "vla_len: ", t->vla_len); PrintObj(f, l + 2, "vla_size: ", t->vla_size); PrintMembers(f, l + 2, "members: ", t->members); PrintBool(f, l + 2, "is_flexible: ", t->is_flexible); PrintBool(f, l + 2, "is_packed: ", t->is_packed); PrintBool(f, l + 2, "is_aligned: ", t->is_aligned); PrintBool(f, l + 2, "is_const: ", t->is_const); PrintBool(f, l + 2, "is_static: ", t->is_static); PrintType(f, l + 2, "return_ty: ", t->return_ty); PrintType(f, l + 2, "params: ", t->params); PrintBool(f, l + 2, "is_variadic: ", t->is_variadic); PrintLine(f, l, "}"); } else if (t->name) { PrintLine(f, l, "%sTY_%s %.*s # %p", s, kTypeKindStr[t->kind], t->name->len, t->name->loc, t); } else { PrintLine(f, l, "%sTY_%s # %p", s, kTypeKindStr[t->kind], t); } } } static void PrintAsm(FILE *f, int l, const char *s, Asm *a) { int i; if (!a) return; PrintLine(f, l, "%sAsm { # %p", s, a); PrintStr(f, l + 2, "str: ", a->str); for (i = 0; i < a->n; ++i) { PrintLine(f, l + 2, "ops: AsmOperand {"); PrintStr(f, l + 4, "str: ", a->ops[i].str); PrintNode(f, l + 4, "node: ", a->ops[i].node); PrintLine(f, l + 2, "}"); } PrintLine(f, l, "}"); } static void PrintNode(FILE *f, int l, const char *s, Node *n) { for (; n; n = n->next) { PrintLine(f, l, "%sNode { # %p", s, n); PrintLine(f, l + 2, "kind: ND_%s", kNodeKindStr[n->kind]); PrintType(f, l + 2, "ty: ", n->ty); PrintNode(f, l + 2, "lhs: ", n->lhs); PrintNode(f, l + 2, "rhs: ", n->rhs); PrintNode(f, l + 2, "cond: ", n->cond); PrintNode(f, l + 2, "then: ", n->then); PrintNode(f, l + 2, "els: ", n->els); PrintNode(f, l + 2, "init: ", n->init); PrintNode(f, l + 2, "inc: ", n->inc); PrintNode(f, l + 2, "body: ", n->body); PrintType(f, l + 2, "func_ty: ", n->func_ty); PrintNode(f, l + 2, "args: ", n->args); PrintObj(f, l + 2, "ret_buffer: ", n->ret_buffer); PrintBool(f, l + 2, "pass_by_stack: ", n->pass_by_stack); PrintBool(f, l + 2, "realign_stack: ", n->realign_stack); PrintNode(f, l + 2, "case_next: ", n->case_next); PrintNode(f, l + 2, "default_case: ", n->default_case); PrintStr(f, l + 2, "label: ", n->label); PrintStr(f, l + 2, "unique_label: ", n->unique_label); PrintNode(f, l + 2, "goto_next: ", n->goto_next); PrintStr(f, l + 2, "brk_label: ", n->brk_label); PrintStr(f, l + 2, "cont_label: ", n->cont_label); PrintInt(f, l + 2, "begin: ", n->begin); PrintAsm(f, l + 2, "azm: ", n->azm); PrintInt(f, l + 2, "end: ", n->end); PrintMember(f, l + 2, "member: ", n->member); PrintObj(f, l + 2, "var: ", n->var); PrintNode(f, l + 2, "overflow: ", n->overflow); PrintInt(f, l + 2, "val: ", n->val); if (n->fval) PrintLine(f, l + 2, "fval: %Lf", n->fval); PrintLine(f, l, "}"); } } static void PrintRelo(FILE *f, int l, const char *s, Relocation *r) { for (; r; r = r->next) { PrintLine(f, l, "%sRelocation { # %p", s, r); PrintInt(f, l + 2, "offset: ", r->offset); if (r->label) PrintStr(f, l + 2, "label: ", *r->label); PrintInt(f, l + 2, "addend: ", r->addend); PrintLine(f, l, "}"); } } static void PrintObj(FILE *f, int l, const char *s, Obj *o) { if (!o) return; PrintLine(f, l, "%sObj { # %p", s, o); PrintStr(f, l + 2, "name: ", o->name); PrintType(f, l + 2, "ty: ", o->ty); PrintBool(f, l + 2, "is_local: ", o->is_local); PrintInt(f, l + 2, "align: ", o->align); PrintInt(f, l + 2, "offset: ", o->offset); PrintBool(f, l + 2, "is_function: ", o->is_function); PrintBool(f, l + 2, "is_definition: ", o->is_definition); PrintBool(f, l + 2, "is_static: ", o->is_static); PrintBool(f, l + 2, "is_weak: ", o->is_weak); PrintBool(f, l + 2, "is_externally_visible: ", o->is_externally_visible); PrintStr(f, l + 2, "asmname: ", o->asmname); PrintStr(f, l + 2, "section: ", o->section); PrintStr(f, l + 2, "visibility: ", o->visibility); PrintBool(f, l + 2, "is_tentative: ", o->is_tentative); PrintBool(f, l + 2, "is_string_literal: ", o->is_string_literal); PrintBool(f, l + 2, "is_tls: ", o->is_tls); PrintStr(f, l + 2, "init_data: ", o->init_data); PrintRelo(f, l + 2, "rel: ", o->rel); PrintBool(f, l + 2, "is_inline: ", o->is_inline); PrintBool(f, l + 2, "is_aligned: ", o->is_aligned); PrintBool(f, l + 2, "is_noreturn: ", o->is_noreturn); PrintBool(f, l + 2, "is_destructor: ", o->is_destructor); PrintBool(f, l + 2, "is_constructor: ", o->is_constructor); PrintBool(f, l + 2, "is_externally_visible: ", o->is_externally_visible); PrintBool(f, l + 2, "is_no_instrument_function: ", o->is_no_instrument_function); PrintBool(f, l + 2, "is_force_align_arg_pointer: ", o->is_force_align_arg_pointer); PrintBool(f, l + 2, "is_no_caller_saved_registers: ", o->is_no_caller_saved_registers); PrintInt(f, l + 2, "stack_size: ", o->stack_size); PrintObj(f, l + 2, "params: ", o->params); PrintNode(f, l + 2, "body: ", o->body); PrintObj(f, l + 2, "locals: ", o->locals); PrintObj(f, l + 2, "va_area: ", o->va_area); PrintObj(f, l + 2, "alloca_bottom: ", o->alloca_bottom); PrintBool(f, l + 2, "is_live: ", o->is_live); PrintBool(f, l + 2, "is_root: ", o->is_root); PrintLine(f, l, "}"); } void print_ast(FILE *f, Obj *o) { for (; o; o = o->next) { PrintObj(f, 0, "", o); } }
10,890
258
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/file.h
#ifndef COSMOPOLITAN_THIRD_PARTY_CHIBICC_FILE_H_ #define COSMOPOLITAN_THIRD_PARTY_CHIBICC_FILE_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ char *skip_bom(char *); char *read_file(const char *); void remove_backslash_newline(char *); void canonicalize_newline(char *); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_THIRD_PARTY_CHIBICC_FILE_H_ */
410
14
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/as.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" #include "libc/calls/struct/stat.h" #include "libc/elf/def.h" #include "libc/fmt/conv.h" #include "libc/intrin/bits.h" #include "libc/intrin/bsr.h" #include "libc/intrin/popcnt.h" #include "libc/log/check.h" #include "libc/log/log.h" #include "libc/macros.internal.h" #include "libc/mem/mem.h" #include "libc/nexgen32e/crc32.h" #include "libc/runtime/runtime.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" #include "libc/str/tab.internal.h" #include "libc/sysv/consts/o.h" #include "libc/sysv/consts/s.h" #include "libc/x/x.h" #include "libc/x/xasprintf.h" #include "third_party/chibicc/file.h" #include "third_party/gdtoa/gdtoa.h" #include "tool/build/lib/elfwriter.h" #define OSZ 0x66 #define ASZ 0x67 #define REX 0x40 // byte #define REXB 0x41 // src #define REXX 0x42 // index #define REXR 0x44 // dest #define REXW 0x48 // quad #define HASASZ 0x00010000 #define HASBASE 0x00020000 #define HASINDEX 0x00040000 #define ISRIP 0x00080000 #define ISREG 0x00100000 #define APPEND(L) \ if (++L.n > L.c) { \ L.c = L.n + 2 + (L.c >> 1); \ L.p = realloc(L.p, L.c * sizeof(*L.p)); \ } #define IS(P, N, S) (N == sizeof(S) - 1 && !strncasecmp(P, S, sizeof(S) - 1)) #define MAX(X, Y) ((Y) < (X) ? (X) : (Y)) #define READ128BE(S) ((uint128_t)READ64BE(S) << 64 | READ64BE((S) + 8)) struct As { int i; // things int section; // sections int previous; // sections int inpath; // strings int outpath; // strings int counter; int pcrelative; bool inhibiterr; bool inhibitwarn; struct Ints { unsigned long n, c; int128_t *p; } ints; struct Floats { unsigned long n, c; long double *p; } floats; struct Slices { unsigned long n, c; struct Slice { unsigned long n, c; char *p; } * p; } slices; struct Sauces { unsigned long n, c; struct Sauce { unsigned path; // strings unsigned line; // 1-indexed } * p; } sauces; struct Things { unsigned long n, c; struct Thing { enum ThingType { TT_INT, TT_FLOAT, TT_SLICE, TT_PUNCT, TT_FORWARD, TT_BACKWARD, } t : 4; unsigned s : 28; // sauces unsigned i; // identity,ints,floats,slices } * p; } things; struct Sections { unsigned long n, c; struct Section { unsigned name; // strings int flags; int type; int align; struct Slice binary; } * p; } sections; struct Symbols { unsigned long n, c; struct Symbol { bool isused; unsigned char stb; // STB_* unsigned char stv; // STV_* unsigned char type; // STT_* unsigned name; // slices unsigned section; // sections long offset; long size; struct ElfWriterSymRef ref; } * p; } symbols; struct HashTable { unsigned i, n; struct HashEntry { unsigned h; unsigned i; } * p; } symbolindex; struct Labels { unsigned long n, c; struct Label { unsigned id; unsigned tok; // things unsigned symbol; // symbols } * p; } labels; struct Relas { unsigned long n, c; struct Rela { bool isdead; int kind; // R_X86_64_{16,32,64,PC8,PC32,PLT32,GOTPCRELX,...} unsigned expr; // exprs unsigned section; // sections long offset; long addend; } * p; } relas; struct Exprs { unsigned long n, c; struct Expr { enum ExprKind { EX_INT, // integer EX_SYM, // things (then symbols after eval) EX_NEG, // unary - EX_NOT, // unary ! EX_BITNOT, // unary ~ EX_ADD, // + EX_SUB, // - EX_MUL, // * EX_DIV, // / EX_REM, // % EX_AND, // & EX_OR, // | EX_XOR, // ^ EX_SHL, // << EX_SHR, // >> EX_EQ, // == EX_NE, // != EX_LT, // < EX_LE, // <= } kind; enum ExprMod { EM_NORMAL, EM_GOTPCREL, EM_DTPOFF, EM_TPOFF, } em; unsigned tok; int lhs; int rhs; int128_t x; bool isvisited; bool isevaluated; } * p; } exprs; struct Strings { unsigned long n, c; char **p; } strings, incpaths; struct SectionStack { unsigned long n, c; int *p; } sectionstack; }; static const char kPrefixByte[30] = { 0x67, 0x2E, 0x66, 0x3E, 0x26, 0x64, 0x65, 0xF0, 0xF3, 0xF3, 0xF2, 0xF2, 0xF3, 0x40, 0x41, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4C, 0x4D, 0x4E, 0x4F, 0x4A, 0x4B, 0x42, 0x43, 0x36, }; static const char kPrefix[30][8] = { "addr32", "cs", "data16", "ds", "es", "fs", "gs", "lock", "rep", "repe", "repne", "repnz", "repz", "rex", "rex.b", "rex.r", "rex.rb", "rex.rx", "rex.rxb", "rex.w", "rex.wb", "rex.wr", "rex.wrb", "rex.wrx", "rex.wrxb", "rex.wx", "rex.wxb", "rex.x", "rex.xb", "ss", }; static const char kSegmentByte[6] = {0x2E, 0x3E, 0x26, 0x64, 0x65, 0x36}; static const char kSegment[6][2] = {"cs", "ds", "es", "fs", "gs", "ss"}; /** * Context-sensitive register encoding information. * * ┌rex * │ ┌log₂size * │ │ ┌reg * ├──────┐ ├─┐├─┐ * 0b0000000000000000 */ static const struct Reg { char s[8]; short reg; short rm; short base; short index; } kRegs[] = /* clang-format off */ { {"ah", 4, 4, -1, -1 }, {"al", 0, 0, -1, -1 }, {"ax", 0 | 1<<3, 0 | 1<<3, -1, -1 }, {"bh", 7, 7, -1, -1 }, {"bl", 3, 3, -1, -1 }, {"bp", 5 | 1<<3, 5 | 1<<3, -1, -1 }, {"bpl", 5 | REX<<8, 5 | REX<<8, -1, -1 }, {"bx", 3 | 1<<3, 3 | 1<<3, -1, -1 }, {"ch", 5, 5, -1, -1 }, {"cl", 1, 1, -1, -1 }, {"cx", 1 | 1<<3, 1 | 1<<3, -1, -1 }, {"dh", 6, 6, -1, -1 }, {"di", 7 | 1<<3, 7 | 1<<3, -1, -1 }, {"dil", 7 | REX<<8, 7 | REX<<8, -1, -1 }, {"dl", 2, 2, -1, -1 }, {"dx", 2 | 1<<3, 2 | 1<<3, -1, -1 }, {"eax", 0 | 2<<3, 0 | 2<<3, 0 | 2<<3, 0 | 2<<3 }, {"ebp", 5 | 2<<3, 5 | 2<<3, 5 | 2<<3, 5 | 2<<3 }, {"ebx", 3 | 2<<3, 3 | 2<<3, 3 | 2<<3, 3 | 2<<3 }, {"ecx", 1 | 2<<3, 1 | 2<<3, 1 | 2<<3, 1 | 2<<3 }, {"edi", 7 | 2<<3, 7 | 2<<3, 7 | 2<<3, 7 | 2<<3 }, {"edx", 2 | 2<<3, 2 | 2<<3, 2 | 2<<3, 2 | 2<<3 }, {"eiz", -1, -1, -1, 4 | 2<<3 }, {"esi", 6 | 2<<3, 6 | 2<<3, 6 | 2<<3, 6 | 2<<3 }, {"esp", 4 | 2<<3, 4 | 2<<3, 4 | 2<<3, 4 | 2<<3 }, {"mm0", 0 | 3<<3, 0 | 3<<3, -1, -1 }, {"mm1", 1 | 3<<3, 1 | 3<<3, -1, -1 }, {"mm2", 2 | 3<<3, 2 | 3<<3, -1, -1 }, {"mm3", 3 | 3<<3, 3 | 3<<3, -1, -1 }, {"mm4", 4 | 3<<3, 4 | 3<<3, -1, -1 }, {"mm5", 5 | 3<<3, 5 | 3<<3, -1, -1 }, {"mm6", 6 | 3<<3, 6 | 3<<3, -1, -1 }, {"mm7", 7 | 3<<3, 7 | 3<<3, -1, -1 }, {"mm8", 0 | 3<<3 | REXR<<8, 0 | 3<<3 | REXB<<8, -1, -1 }, {"mm9", 1 | 3<<3 | REXR<<8, 1 | 3<<3 | REXB<<8, -1, -1 }, {"r10", 2 | 3<<3 | REXR<<8 | REXW<<8, 2 | 3<<3 | REXB<<8 | REXW<<8, 2 | 3<<3 | REXB<<8, 2 | 3<<3 | REXX<<8 }, {"r10b", 2 | REXR<<8, 2 | REXB<<8, -1, -1 }, {"r10d", 2 | 2<<3 | REXR<<8, 2 | 2<<3 | REXB<<8, 2 | 2<<3 | REXB<<8, 2 | 2<<3 | REXX<<8 }, {"r10w", 2 | 1<<3 | REXR<<8, 2 | 1<<3 | REXB<<8, -1, -1 }, {"r11", 3 | 3<<3 | REXR<<8 | REXW<<8, 3 | 3<<3 | REXB<<8 | REXW<<8, 3 | 3<<3 | REXB<<8, 3 | 3<<3 | REXX<<8 }, {"r11b", 3 | REXR<<8, 3 | REXB<<8, -1, -1 }, {"r11d", 3 | 2<<3 | REXR<<8, 3 | 2<<3 | REXB<<8, 3 | 2<<3 | REXB<<8, 3 | 2<<3 | REXX<<8 }, {"r11w", 3 | 1<<3 | REXR<<8, 3 | 1<<3 | REXB<<8, -1, -1 }, {"r12", 4 | 3<<3 | REXR<<8 | REXW<<8, 4 | 3<<3 | REXB<<8 | REXW<<8, 4 | 3<<3 | REXB<<8, 4 | 3<<3 | REXX<<8 }, {"r12b", 4 | REXR<<8, 4 | REXB<<8, -1, -1 }, {"r12d", 4 | 2<<3 | REXR<<8, 4 | 2<<3 | REXB<<8, 4 | 2<<3 | REXB<<8, 4 | 2<<3 | REXX<<8 }, {"r12w", 4 | 1<<3 | REXR<<8, 4 | 1<<3 | REXB<<8, -1, -1 }, {"r13", 5 | 3<<3 | REXR<<8 | REXW<<8, 5 | 3<<3 | REXB<<8 | REXW<<8, 5 | 3<<3 | REXB<<8, 5 | 3<<3 | REXX<<8 }, {"r13b", 5 | REXR<<8, 5 | REXB<<8, -1, -1 }, {"r13d", 5 | 2<<3 | REXR<<8, 5 | 2<<3 | REXB<<8, 5 | 2<<3 | REXB<<8, 5 | 2<<3 | REXX<<8 }, {"r13w", 5 | 1<<3 | REXR<<8, 5 | 1<<3 | REXB<<8, -1, -1 }, {"r14", 6 | 3<<3 | REXR<<8 | REXW<<8, 6 | 3<<3 | REXB<<8 | REXW<<8, 6 | 3<<3 | REXB<<8, 6 | 3<<3 | REXX<<8 }, {"r14b", 6 | REXR<<8, 6 | REXB<<8, -1, -1 }, {"r14d", 6 | 2<<3 | REXR<<8, 6 | 2<<3 | REXB<<8, 6 | 2<<3 | REXB<<8, 6 | 2<<3 | REXX<<8 }, {"r14w", 6 | 1<<3 | REXR<<8, 6 | 1<<3 | REXB<<8, -1, -1 }, {"r15", 7 | 3<<3 | REXR<<8 | REXW<<8, 7 | 3<<3 | REXB<<8 | REXW<<8, 7 | 3<<3 | REXB<<8, 7 | 3<<3 | REXX<<8 }, {"r15b", 7 | REXR<<8, 7 | REXB<<8, -1, -1 }, {"r15d", 7 | 2<<3 | REXR<<8, 7 | 2<<3 | REXB<<8, 7 | 2<<3 | REXB<<8, 7 | 2<<3 | REXX<<8 }, {"r15w", 7 | 1<<3 | REXR<<8, 7 | 1<<3 | REXB<<8, -1, -1 }, {"r8", 0 | 3<<3 | REXR<<8 | REXW<<8, 0 | 3<<3 | REXB<<8 | REXW<<8, 0 | 3<<3 | REXB<<8, 0 | 3<<3 | REXX<<8 }, {"r8b", 0 | REXR<<8, 0 | REXB<<8, -1, -1 }, {"r8d", 0 | 2<<3 | REXR<<8, 0 | 2<<3 | REXB<<8, 0 | 2<<3 | REXB<<8, 0 | 2<<3 | REXX<<8 }, {"r8w", 0 | 1<<3 | REXR<<8, 0 | 1<<3 | REXB<<8, -1, -1 }, {"r9", 1 | 3<<3 | REXR<<8 | REXW<<8, 1 | 3<<3 | REXB<<8 | REXW<<8, 1 | 3<<3 | REXB<<8, 1 | 3<<3 | REXX<<8 }, {"r9b", 1 | REXR<<8, 1 | REXB<<8, -1, -1 }, {"r9d", 1 | 2<<3 | REXR<<8, 1 | 2<<3 | REXB<<8, 1 | 2<<3 | REXB<<8, 1 | 2<<3 | REXX<<8 }, {"r9w", 1 | 1<<3 | REXR<<8, 1 | 1<<3 | REXB<<8, -1, -1 }, {"rax", 0 | 3<<3 | REXW<<8, 0 | 3<<3 | REXW<<8, 0 | 3<<3, 0 | 3<<3 }, {"rbp", 5 | 3<<3 | REXW<<8, 5 | 3<<3 | REXW<<8, 5 | 3<<3, 5 | 3<<3 }, {"rbx", 3 | 3<<3 | REXW<<8, 3 | 3<<3 | REXW<<8, 3 | 3<<3, 3 | 3<<3 }, {"rcx", 1 | 3<<3 | REXW<<8, 1 | 3<<3 | REXW<<8, 1 | 3<<3, 1 | 3<<3 }, {"rdi", 7 | 3<<3 | REXW<<8, 7 | 3<<3 | REXW<<8, 7 | 3<<3, 7 | 3<<3 }, {"rdx", 2 | 3<<3 | REXW<<8, 2 | 3<<3 | REXW<<8, 2 | 3<<3, 2 | 3<<3 }, {"riz", -1, -1, -1, 4 | 3<<3 }, {"rsi", 6 | 3<<3 | REXW<<8, 6 | 3<<3 | REXW<<8, 6 | 3<<3, 6 | 3<<3 }, {"rsp", 4 | 3<<3 | REXW<<8, 4 | 3<<3 | REXW<<8, 4 | 3<<3, 4 | 3<<3 }, {"si", 6 | 1<<3, 6 | 1<<3, 6 | 1<<3, 6 | 1<<3 }, {"sil", 6 | REX<<8, 6 | REX<<8, 6 | REX<<8, 6 | REX<<8 }, {"sp", 4 | 1<<3, 4 | 1<<3, 4 | 1<<3, 4 | 1<<3 }, {"spl", 4 | REX<<8, 4 | REX<<8, 4 | REX<<8, 4 | REX<<8 }, {"st", 0 | 4<<3, 0 | 4<<3, -1, -1 }, {"st(0)", 0 | 4<<3, 0 | 4<<3, -1, -1 }, {"st(1)", 1 | 4<<3, 1 | 4<<3, -1, -1 }, {"st(2)", 2 | 4<<3, 2 | 4<<3, -1, -1 }, {"st(3)", 3 | 4<<3, 3 | 4<<3, -1, -1 }, {"st(4)", 4 | 4<<3, 4 | 4<<3, -1, -1 }, {"st(5)", 5 | 4<<3, 5 | 4<<3, -1, -1 }, {"st(6)", 6 | 4<<3, 6 | 4<<3, -1, -1 }, {"st(7)", 7 | 4<<3, 7 | 4<<3, -1, -1 }, {"xmm0", 0 | 4<<3, 0 | 4<<3, -1, -1 }, {"xmm1", 1 | 4<<3, 1 | 4<<3, -1, -1 }, {"xmm10", 2 | 4<<3 | REXR<<8, 2 | 4<<3 | REXB<<8, -1, -1 }, {"xmm11", 3 | 4<<3 | REXR<<8, 3 | 4<<3 | REXB<<8, -1, -1 }, {"xmm12", 4 | 4<<3 | REXR<<8, 4 | 4<<3 | REXB<<8, -1, -1 }, {"xmm13", 5 | 4<<3 | REXR<<8, 5 | 4<<3 | REXB<<8, -1, -1 }, {"xmm14", 6 | 4<<3 | REXR<<8, 6 | 4<<3 | REXB<<8, -1, -1 }, {"xmm15", 7 | 4<<3 | REXR<<8, 7 | 4<<3 | REXB<<8, -1, -1 }, {"xmm2", 2 | 4<<3, 2 | 4<<3, -1, -1 }, {"xmm3", 3 | 4<<3, 3 | 4<<3, -1, -1 }, {"xmm4", 4 | 4<<3, 4 | 4<<3, -1, -1 }, {"xmm5", 5 | 4<<3, 5 | 4<<3, -1, -1 }, {"xmm6", 6 | 4<<3, 6 | 4<<3, -1, -1 }, {"xmm7", 7 | 4<<3, 7 | 4<<3, -1, -1 }, {"xmm8", 0 | 4<<3 | REXR<<8, 0 | 4<<3 | REXB<<8, -1, -1 }, {"xmm9", 1 | 4<<3 | REXR<<8, 1 | 4<<3 | REXB<<8, -1, -1 }, } /* clang-format on */; long as_hashmap_hits; long as_hashmap_miss; static bool IsPunctMergeable(int c) { switch (c) { case ',': case ';': case '$': return false; default: return true; } } static char *PunctToStr(int p, char b[4]) { int c, i, j; bzero(b, 4); for (j = 0, i = 2; i >= 0; --i) { if ((c = (p >> (i * 8)) & 0xff)) { b[j++] = c; } } return b; } static void PrintSlice(struct Slice s) { fprintf(stderr, "%.*s\n", s.n, s.p); } static char *SaveString(struct Strings *l, char *p) { APPEND((*l)); l->p[l->n - 1] = p; return p; } static int StrDup(struct As *a, const char *s) { SaveString(&a->strings, strdup(s)); return a->strings.n - 1; } static int SliceDup(struct As *a, struct Slice s) { SaveString(&a->strings, strndup(s.p, s.n)); return a->strings.n - 1; } static int AppendSauce(struct As *a, int path, int line) { if (!a->sauces.n || (line != a->sauces.p[a->sauces.n - 1].line || path != a->sauces.p[a->sauces.n - 1].path)) { APPEND(a->sauces); a->sauces.p[a->sauces.n - 1].path = path; a->sauces.p[a->sauces.n - 1].line = line; } return a->sauces.n - 1; } static void AppendExpr(struct As *a) { APPEND(a->exprs); bzero(a->exprs.p + a->exprs.n - 1, sizeof(*a->exprs.p)); a->exprs.p[a->exprs.n - 1].tok = a->i; a->exprs.p[a->exprs.n - 1].lhs = -1; a->exprs.p[a->exprs.n - 1].rhs = -1; } static void AppendThing(struct As *a) { APPEND(a->things); bzero(a->things.p + a->things.n - 1, sizeof(*a->things.p)); } static void AppendRela(struct As *a) { APPEND(a->relas); bzero(a->relas.p + a->relas.n - 1, sizeof(*a->relas.p)); } static void AppendSlice(struct As *a) { APPEND(a->slices); bzero(a->slices.p + a->slices.n - 1, sizeof(*a->slices.p)); } static int AppendSection(struct As *a, int name, int flags, int type) { int i; APPEND(a->sections); i = a->sections.n - 1; CHECK_LT(i, SHN_LORESERVE); a->sections.p[i].name = name; a->sections.p[i].flags = flags; a->sections.p[i].type = type; a->sections.p[i].align = 1; a->sections.p[i].binary.p = NULL; a->sections.p[i].binary.n = 0; return i; } static struct As *NewAssembler(void) { struct As *a = calloc(1, sizeof(struct As)); AppendSlice(a); AppendSection(a, StrDup(a, ""), 0, SHT_NULL); AppendSection(a, StrDup(a, ".text"), SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS); AppendSection(a, StrDup(a, ".data"), SHF_ALLOC | SHF_WRITE, SHT_PROGBITS); AppendSection(a, StrDup(a, ".bss"), SHF_ALLOC | SHF_WRITE, SHT_NOBITS); a->section = 1; return a; } static void FreeAssembler(struct As *a) { int i; for (i = 0; i < a->sections.n; ++i) free(a->sections.p[i].binary.p); for (i = 0; i < a->strings.n; ++i) free(a->strings.p[i]); for (i = 0; i < a->incpaths.n; ++i) free(a->incpaths.p[i]); free(a->ints.p); free(a->floats.p); free(a->slices.p); free(a->sauces.p); free(a->things.p); free(a->sections.p); free(a->symbols.p); free(a->symbolindex.p); free(a->labels.p); free(a->relas.p); free(a->exprs.p); free(a->strings.p); free(a->incpaths.p); free(a->sectionstack.p); free(a); } static void ReadFlags(struct As *a, int argc, char *argv[]) { int i; a->inpath = StrDup(a, "-"); a->outpath = StrDup(a, "a.out"); for (i = 1; i < argc; ++i) { if (!strcmp(argv[i], "-o")) { a->outpath = StrDup(a, argv[++i]); } else if (_startswith(argv[i], "-o")) { a->outpath = StrDup(a, argv[i] + 2); } else if (!strcmp(argv[i], "-I")) { SaveString(&a->incpaths, strdup(argv[++i])); } else if (_startswith(argv[i], "-I")) { SaveString(&a->incpaths, strdup(argv[i] + 2)); } else if (!strcmp(argv[i], "-Z")) { a->inhibiterr = true; } else if (!strcmp(argv[i], "-W")) { a->inhibitwarn = true; } else if (argv[i][0] != '-') { a->inpath = StrDup(a, argv[i]); } } } static int ReadCharLiteral(struct Slice *buf, int c, char *p, int *i) { int x; if (c != '\\') return c; switch ((c = p[(*i)++])) { case 'a': return '\a'; case 'b': return '\b'; case 't': return '\t'; case 'n': return '\n'; case 'v': return '\v'; case 'f': return '\f'; case 'r': return '\r'; case 'e': return 033; case 'x': if ((x = kHexToInt[p[*i] & 255]) != -1) { *i += 1, c = x; if ((x = kHexToInt[p[*i] & 255]) != -1) { *i += 1, c = c << 4 | x; } } return c; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': c -= '0'; if ('0' <= p[*i] && p[*i] <= '7') { c = c * 8 + (p[(*i)++] - '0'); if ('0' <= p[*i] && p[*i] <= '7') { c = c * 8 + (p[(*i)++] - '0'); } } return c; default: return c; } } static void PrintLocation(struct As *a) { fprintf(stderr, "%s:%d:: ", a->strings.p[a->sauces.p[a->things.p[a->i].s].path], a->sauces.p[a->things.p[a->i].s].line); } static wontreturn void Fail(struct As *a, const char *fmt, ...) { va_list va; PrintLocation(a); va_start(va, fmt); vfprintf(stderr, fmt, va); va_end(va); fputc('\n', stderr); __die(); } static wontreturn void InvalidRegister(struct As *a) { Fail(a, "invalid register"); } static char *FindInclude(struct As *a, const char *file) { int i; char *path; struct stat st; for (i = 0; i < a->incpaths.n; ++i) { path = xjoinpaths(a->incpaths.p[i], file); if (stat(path, &st) != -1 && S_ISREG(st.st_mode)) return path; free(path); } return NULL; } static void Tokenize(struct As *a, int path) { int c, i, line; char *p, *path2; struct Slice buf; bool bol, isfloat, isfpu; if (!fileexists(a->strings.p[path])) { fprintf(stderr, "%s: file not found\n", a->strings.p[path]); exit(1); } p = SaveString(&a->strings, read_file(a->strings.p[path])); p = skip_bom(p); canonicalize_newline(p); remove_backslash_newline(p); line = 1; bol = true; while ((c = *p)) { if (c == '/' && p[1] == '*') { for (i = 2; p[i]; ++i) { if (p[i] == '\n') { ++line; bol = true; } else { bol = false; if (p[i] == '*' && p[i + 1] == '/') { i += 2; break; } } } p += i; continue; } if (c == '#' || (c == '/' && bol) || (c == '/' && p[1] == '/')) { p = strchr(p, '\n'); continue; } if (c == '\n') { AppendThing(a); a->things.p[a->things.n - 1].t = TT_PUNCT; a->things.p[a->things.n - 1].s = AppendSauce(a, path, line); a->things.p[a->things.n - 1].i = ';'; ++p; bol = true; ++line; continue; } bol = false; if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == '\v') { ++p; continue; } if ((c & 0x80) || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_' || c == '%' || c == '@' || (c == '.' && !('0' <= p[1] && p[1] <= '9'))) { isfpu = false; for (i = 1;; ++i) { if (!((p[i] & 0x80) || ('a' <= p[i] && p[i] <= 'z') || ('A' <= p[i] && p[i] <= 'Z') || ('0' <= p[i] && p[i] <= '9') || p[i] == '.' || p[i] == '_' || p[i] == '$' || (isfpu && (p[i] == '(' || p[i] == ')')))) { break; } if (i == 2 && p[i - 2] == '%' && p[i - 1] == 's' && p[i] == 't') { isfpu = true; } } if (i == 4 && !strncasecmp(p, ".end", 4)) break; AppendThing(a); a->things.p[a->things.n - 1].t = TT_SLICE; a->things.p[a->things.n - 1].s = AppendSauce(a, path, line); a->things.p[a->things.n - 1].i = a->slices.n; AppendSlice(a); a->slices.p[a->slices.n - 1].p = p; a->slices.p[a->slices.n - 1].n = i; p += i; continue; } if (('0' <= c && c <= '9') || (c == '.' && '0' <= p[1] && p[1] <= '9')) { isfloat = c == '.'; if (c == '0' && p[1] != '.') { if (p[1] == 'x' || p[1] == 'X') { for (i = 2;; ++i) { if (!(('0' <= p[i] && p[i] <= '9') || ('a' <= p[i] && p[i] <= 'f') || ('A' <= p[i] && p[i] <= 'F'))) { break; } } } else if ((p[1] == 'b' || p[1] == 'B') && ('0' <= p[2] && p[2] <= '9')) { for (i = 2;; ++i) { if (!(p[i] == '0' || p[i] == '1')) break; } } else { for (i = 1;; ++i) { if (!('0' <= p[i] && p[i] <= '7')) break; } } } else { for (i = 1;; ++i) { if (('0' <= p[i] && p[i] <= '9') || p[i] == '-' || p[i] == '+') { continue; } else if (p[i] == '.' || p[i] == 'e' || p[i] == 'E' || p[i] == 'e') { isfloat = true; continue; } break; } } AppendThing(a); if (isfloat) { APPEND(a->floats); a->floats.p[a->floats.n - 1] = strtold(p, NULL); a->things.p[a->things.n - 1].i = a->floats.n - 1; a->things.p[a->things.n - 1].t = TT_FLOAT; } else { APPEND(a->ints); a->ints.p[a->ints.n - 1] = strtoumax(p, NULL, 0); a->things.p[a->things.n - 1].i = a->ints.n - 1; if (p[i] == 'f' || p[i] == 'F') { a->things.p[a->things.n - 1].t = TT_FORWARD; ++i; } else if (p[i] == 'b' || p[i] == 'B') { a->things.p[a->things.n - 1].t = TT_BACKWARD; ++i; } else { a->things.p[a->things.n - 1].t = TT_INT; } } a->things.p[a->things.n - 1].s = AppendSauce(a, path, line); p += i; continue; } if (c == '\'') { i = 1; c = p[i++]; c = ReadCharLiteral(&buf, c, p, &i); if (p[i] == '\'') ++i; p += i; AppendThing(a); a->things.p[a->things.n - 1].t = TT_INT; a->things.p[a->things.n - 1].s = AppendSauce(a, path, line); a->things.p[a->things.n - 1].i = a->ints.n; APPEND(a->ints); a->ints.p[a->ints.n - 1] = c; continue; } if (c == '"') { bzero(&buf, sizeof(buf)); for (i = 1; (c = p[i++]);) { if (c == '"') break; c = ReadCharLiteral(&buf, c, p, &i); APPEND(buf); buf.p[buf.n - 1] = c; } p += i; if (a->things.n && a->things.p[a->things.n - 1].t == TT_SLICE && IS(a->slices.p[a->things.p[a->things.n - 1].i].p, a->slices.p[a->things.p[a->things.n - 1].i].n, ".include")) { APPEND(buf); buf.p[buf.n - 1] = '\0'; --a->things.n; if ((path2 = FindInclude(a, buf.p))) { Tokenize(a, StrDup(a, path2)); free(path2); free(buf.p); } else { Fail(a, "not found: %s", buf.p); } } else { SaveString(&a->strings, buf.p); AppendThing(a); a->things.p[a->things.n - 1].t = TT_SLICE; a->things.p[a->things.n - 1].s = AppendSauce(a, path, line); a->things.p[a->things.n - 1].i = a->slices.n; AppendSlice(a); a->slices.p[a->slices.n - 1] = buf; } continue; } if (IsPunctMergeable(c) && a->things.n && a->things.p[a->things.n - 1].t == TT_PUNCT && IsPunctMergeable(a->things.p[a->things.n - 1].i)) { a->things.p[a->things.n - 1].i = a->things.p[a->things.n - 1].i << 8 | c; } else { AppendThing(a); a->things.p[a->things.n - 1].t = TT_PUNCT; a->things.p[a->things.n - 1].s = AppendSauce(a, path, line); a->things.p[a->things.n - 1].i = c; } ++p; } } static int GetSymbol(struct As *a, int name) { struct HashEntry *p; unsigned i, j, k, n, m, h, n2; if (!(h = crc32c(0, a->slices.p[name].p, a->slices.p[name].n))) h = 1; n = a->symbolindex.n; i = 0; if (n) { k = 0; for (;;) { i = (h + k + ((k + 1) >> 1)) & (n - 1); if (a->symbolindex.p[i].h == h && a->slices.p[a->symbols.p[a->symbolindex.p[i].i].name].n == a->slices.p[name].n && !memcmp(a->slices.p[a->symbols.p[a->symbolindex.p[i].i].name].p, a->slices.p[name].p, a->slices.p[name].n)) { ++as_hashmap_hits; return a->symbolindex.p[i].i; } if (!a->symbolindex.p[i].h) { break; } else { ++k; ++as_hashmap_miss; } } } if (++a->symbolindex.i >= (n >> 1)) { m = n ? n << 1 : 16; p = calloc(m, sizeof(struct HashEntry)); for (j = 0; j < n; ++j) { if (a->symbolindex.p[j].h) { k = 0; do { i = (a->symbolindex.p[j].h + k + ((k + 1) >> 1)) & (m - 1); ++k; } while (p[i].h); p[i].h = a->symbolindex.p[j].h; p[i].i = a->symbolindex.p[j].i; } } k = 0; do { i = (h + k + ((k + 1) >> 1)) & (m - 1); ++k; } while (p[i].h); free(a->symbolindex.p); a->symbolindex.p = p; a->symbolindex.n = m; } APPEND(a->symbols); memset(a->symbols.p + a->symbols.n - 1, 0, sizeof(*a->symbols.p)); a->symbolindex.p[i].h = h; a->symbolindex.p[i].i = a->symbols.n - 1; a->symbols.p[a->symbols.n - 1].name = name; return a->symbols.n - 1; } static void OnSymbol(struct As *a, int name) { int i = GetSymbol(a, name); if (a->symbols.p[i].section) { Fail(a, "already defined: %.*s", a->slices.p[name].n, a->slices.p[name].p); } a->symbols.p[i].section = a->section; a->symbols.p[i].offset = a->sections.p[a->section].binary.n; a->i += 2; } static void OnLocalLabel(struct As *a, int id) { int i; char *name; name = xasprintf(".Label.%d", a->counter++); SaveString(&a->strings, name); AppendSlice(a); a->slices.p[a->slices.n - 1].p = name; a->slices.p[a->slices.n - 1].n = strlen(name); i = GetSymbol(a, a->slices.n - 1); a->symbols.p[i].section = a->section; a->symbols.p[i].offset = a->sections.p[a->section].binary.n; APPEND(a->labels); a->labels.p[a->labels.n - 1].id = id; a->labels.p[a->labels.n - 1].tok = a->i; a->labels.p[a->labels.n - 1].symbol = i; a->i += 2; } static void SetSection(struct As *a, int section) { a->previous = a->section; a->section = section; } static bool IsInt(struct As *a, int i) { return a->things.p[i].t == TT_INT; } static bool IsFloat(struct As *a, int i) { return a->things.p[i].t == TT_FLOAT; } static bool IsSlice(struct As *a, int i) { return a->things.p[i].t == TT_SLICE; } static bool IsPunct(struct As *a, int i, int c) { return a->things.p[i].t == TT_PUNCT && a->things.p[i].i == c; } static bool IsForward(struct As *a, int i) { return a->things.p[i].t == TT_FORWARD; } static bool IsBackward(struct As *a, int i) { return a->things.p[i].t == TT_BACKWARD; } static bool IsComma(struct As *a) { return IsPunct(a, a->i, ','); } static bool IsSemicolon(struct As *a) { return IsPunct(a, a->i, ';'); } static bool IsRegister(struct As *a, int i) { return IsSlice(a, i) && (a->slices.p[a->things.p[i].i].n && *a->slices.p[a->things.p[i].i].p == '%'); } static void ConsumePunct(struct As *a, int c) { char pb[4]; if (IsPunct(a, a->i, c)) { ++a->i; } else { Fail(a, "expected %s", PunctToStr(c, pb)); } } static void ConsumeComma(struct As *a) { ConsumePunct(a, ','); } static int NewPrimary(struct As *a, enum ExprKind k, int128_t x) { AppendExpr(a); a->exprs.p[a->exprs.n - 1].kind = k; a->exprs.p[a->exprs.n - 1].x = x; return a->exprs.n - 1; } static int NewUnary(struct As *a, enum ExprKind k, int lhs) { AppendExpr(a); a->exprs.p[a->exprs.n - 1].kind = k; a->exprs.p[a->exprs.n - 1].lhs = lhs; return a->exprs.n - 1; } static int NewBinary(struct As *a, enum ExprKind k, int lhs, int rhs) { AppendExpr(a); a->exprs.p[a->exprs.n - 1].kind = k; a->exprs.p[a->exprs.n - 1].lhs = lhs; a->exprs.p[a->exprs.n - 1].rhs = rhs; return a->exprs.n - 1; } // primary = int // | symbol // | reference static int ParsePrimary(struct As *a, int *rest, int i) { int e; if (IsInt(a, i)) { *rest = i + 1; return NewPrimary(a, EX_INT, a->ints.p[a->things.p[i].i]); } else if (IsForward(a, i) || IsBackward(a, i) || (IsSlice(a, i) && (a->slices.p[a->things.p[i].i].n && a->slices.p[a->things.p[i].i].p[0] != '%' && a->slices.p[a->things.p[i].i].p[0] != '@'))) { *rest = i + 1; return NewPrimary(a, EX_SYM, i); } else { Fail(a, "expected int or label"); } } // postfix = primary "@gotpcrel" // | primary "@dtpoff" // | primary "@tpoff" // | primary static int ParsePostfix(struct As *a, int *rest, int i) { int x; struct Slice suffix; x = ParsePrimary(a, &i, i); if (IsSlice(a, i)) { suffix = a->slices.p[a->things.p[i].i]; if (suffix.n && suffix.p[0] == '@') { if (IS(suffix.p, suffix.n, "@gotpcrel")) { a->exprs.p[x].em = EM_GOTPCREL; ++i; } else if (IS(suffix.p, suffix.n, "@dtpoff")) { a->exprs.p[x].em = EM_DTPOFF; ++i; } else if (IS(suffix.p, suffix.n, "@tpoff")) { a->exprs.p[x].em = EM_TPOFF; ++i; } } } *rest = i; return x; } // unary = ("+" | "-" | "!" | "~") unary // | postfix static int ParseUnary(struct As *a, int *rest, int i) { int x; if (IsPunct(a, i, '+')) { x = ParseUnary(a, rest, i + 1); } else if (IsPunct(a, i, '-')) { x = ParseUnary(a, rest, i + 1); if (a->exprs.p[x].kind == EX_INT) { a->exprs.p[x].x = -a->exprs.p[x].x; } else { x = NewPrimary(a, EX_NEG, x); } } else if (IsPunct(a, i, '!')) { x = ParseUnary(a, rest, i + 1); if (a->exprs.p[x].kind == EX_INT) { a->exprs.p[x].x = !a->exprs.p[x].x; } else { x = NewPrimary(a, EX_NOT, x); } } else if (IsPunct(a, i, '~')) { x = ParseUnary(a, rest, i + 1); if (a->exprs.p[x].kind == EX_INT) { a->exprs.p[x].x = ~a->exprs.p[x].x; } else { x = NewPrimary(a, EX_BITNOT, x); } } else { x = ParsePostfix(a, rest, i); } return x; } // mul = unary ("*" unary | "/" unary | "%" unary)* static int ParseMul(struct As *a, int *rest, int i) { int x, y; x = ParseUnary(a, &i, i); for (;;) { if (IsPunct(a, i, '*')) { y = ParseUnary(a, &i, i + 1); if (a->exprs.p[x].kind == EX_INT && a->exprs.p[y].kind == EX_INT) { a->exprs.p[x].x *= a->exprs.p[y].x; } else { x = NewBinary(a, EX_MUL, x, y); } } else if (IsPunct(a, i, '/')) { y = ParseUnary(a, &i, i + 1); if (a->exprs.p[x].kind == EX_INT && a->exprs.p[y].kind == EX_INT) { a->exprs.p[x].x /= a->exprs.p[y].x; } else { x = NewBinary(a, EX_DIV, x, y); } } else if (IsPunct(a, i, '%')) { y = ParseUnary(a, &i, i + 1); if (a->exprs.p[x].kind == EX_INT && a->exprs.p[y].kind == EX_INT) { a->exprs.p[x].x %= a->exprs.p[y].x; } else { x = NewBinary(a, EX_REM, x, y); } } else { *rest = i; return x; } } } // add = mul ("+" mul | "-" mul)* static int ParseAdd(struct As *a, int *rest, int i) { int x, y; x = ParseMul(a, &i, i); for (;;) { if (IsPunct(a, i, '+')) { y = ParseMul(a, &i, i + 1); if (a->exprs.p[x].kind == EX_INT && a->exprs.p[y].kind == EX_INT) { a->exprs.p[x].x += a->exprs.p[y].x; } else { x = NewBinary(a, EX_ADD, x, y); } } else if (IsPunct(a, i, '-')) { y = ParseMul(a, &i, i + 1); if (a->exprs.p[x].kind == EX_INT && a->exprs.p[y].kind == EX_INT) { a->exprs.p[x].x -= a->exprs.p[y].x; } else if (a->exprs.p[y].kind == EX_INT) { a->exprs.p[y].x = -a->exprs.p[y].x; x = NewBinary(a, EX_ADD, x, y); } else { x = NewBinary(a, EX_SUB, x, y); } } else { *rest = i; return x; } } } // shift = add ("<<" add | ">>" add)* static int ParseShift(struct As *a, int *rest, int i) { int x, y; x = ParseAdd(a, &i, i); for (;;) { if (IsPunct(a, i, '<' << 8 | '<')) { y = ParseAdd(a, &i, i + 1); if (a->exprs.p[x].kind == EX_INT && a->exprs.p[y].kind == EX_INT) { a->exprs.p[x].x <<= a->exprs.p[y].x & 63; } else { x = NewBinary(a, EX_SHL, x, y); } } else if (IsPunct(a, i, '>' << 8 | '>')) { y = ParseAdd(a, &i, i + 1); if (a->exprs.p[x].kind == EX_INT && a->exprs.p[y].kind == EX_INT) { a->exprs.p[x].x >>= a->exprs.p[y].x & 63; } else { x = NewBinary(a, EX_SHR, x, y); } } else { *rest = i; return x; } } } // relational = shift ("<" shift | "<=" shift | ">" shift | ">=" shift)* static int ParseRelational(struct As *a, int *rest, int i) { int x, y; x = ParseShift(a, &i, i); for (;;) { if (IsPunct(a, i, '<')) { y = ParseShift(a, &i, i + 1); if (a->exprs.p[x].kind == EX_INT && a->exprs.p[y].kind == EX_INT) { a->exprs.p[x].x = a->exprs.p[x].x < a->exprs.p[y].x; } else { x = NewBinary(a, EX_LT, x, y); } } else if (IsPunct(a, i, '>')) { y = ParseShift(a, &i, i + 1); if (a->exprs.p[x].kind == EX_INT && a->exprs.p[y].kind == EX_INT) { a->exprs.p[x].x = a->exprs.p[y].x < a->exprs.p[x].x; } else { x = NewBinary(a, EX_LT, y, x); } } else if (IsPunct(a, i, '<' << 8 | '=')) { y = ParseShift(a, &i, i + 1); if (a->exprs.p[x].kind == EX_INT && a->exprs.p[y].kind == EX_INT) { a->exprs.p[x].x = a->exprs.p[x].x <= a->exprs.p[y].x; } else { x = NewBinary(a, EX_LE, x, y); } } else if (IsPunct(a, i, '>' << 8 | '=')) { y = ParseShift(a, &i, i + 1); if (a->exprs.p[x].kind == EX_INT && a->exprs.p[y].kind == EX_INT) { a->exprs.p[x].x = a->exprs.p[y].x <= a->exprs.p[x].x; } else { x = NewBinary(a, EX_LE, y, x); } } else { *rest = i; return x; } } } // equality = relational ("==" relational | "!=" relational)* static int ParseEquality(struct As *a, int *rest, int i) { int x, y; x = ParseRelational(a, &i, i); for (;;) { if (IsPunct(a, i, '=' << 8 | '=')) { y = ParseRelational(a, &i, i + 1); if (a->exprs.p[x].kind == EX_INT && a->exprs.p[y].kind == EX_INT) { a->exprs.p[x].x = a->exprs.p[x].x == a->exprs.p[y].x; } else { x = NewBinary(a, EX_EQ, x, y); } } else if (IsPunct(a, i, '!' << 8 | '=')) { y = ParseRelational(a, &i, i + 1); if (a->exprs.p[x].kind == EX_INT && a->exprs.p[y].kind == EX_INT) { a->exprs.p[x].x = a->exprs.p[x].x != a->exprs.p[y].x; } else { x = NewBinary(a, EX_NE, x, y); } } else { *rest = i; return x; } } } // and = equality ("&" equality)* static int ParseAnd(struct As *a, int *rest, int i) { int x, y; x = ParseEquality(a, &i, i); for (;;) { if (IsPunct(a, i, '&')) { y = ParseEquality(a, &i, i + 1); if (a->exprs.p[x].kind == EX_INT && a->exprs.p[y].kind == EX_INT) { a->exprs.p[x].x &= a->exprs.p[y].x; } else { x = NewBinary(a, EX_AND, x, y); } } else { *rest = i; return x; } } } // xor = and ("^" and)* static int ParseXor(struct As *a, int *rest, int i) { int x, y; x = ParseAnd(a, &i, i); for (;;) { if (IsPunct(a, i, '^')) { y = ParseAnd(a, &i, i + 1); if (a->exprs.p[x].kind == EX_INT && a->exprs.p[y].kind == EX_INT) { a->exprs.p[x].x ^= a->exprs.p[y].x; } else { x = NewBinary(a, EX_XOR, x, y); } } else { *rest = i; return x; } } } // or = xor ("|" xor)* static int ParseOr(struct As *a, int *rest, int i) { int x, y; x = ParseXor(a, &i, i); for (;;) { if (IsPunct(a, i, '|')) { y = ParseXor(a, &i, i + 1); if (a->exprs.p[x].kind == EX_INT && a->exprs.p[y].kind == EX_INT) { a->exprs.p[x].x |= a->exprs.p[y].x; } else { x = NewBinary(a, EX_OR, x, y); } } else { *rest = i; return x; } } } static int Parse(struct As *a) { return ParseOr(a, &a->i, a->i); } static int128_t GetInt(struct As *a) { int x; x = Parse(a); if (a->exprs.p[x].kind == EX_INT) { return a->exprs.p[x].x; } else { Fail(a, "expected constexpr int"); } } static long double GetFloat(struct As *a) { long double res; if (IsFloat(a, a->i)) { res = a->floats.p[a->things.p[a->i].i]; ++a->i; return res; } else { Fail(a, "expected float"); } } static struct Slice GetSlice(struct As *a) { struct Slice res; if (IsSlice(a, a->i)) { res = a->slices.p[a->things.p[a->i].i]; ++a->i; return res; } else { Fail(a, "expected string"); } } static void EmitData(struct As *a, const void *p, uint128_t n) { struct Slice *s; s = &a->sections.p[a->section].binary; s->p = realloc(s->p, s->n + n); if (n) memcpy(s->p + s->n, p, n); s->n += n; } static void EmitByte(struct As *a, uint128_t i) { uint8_t x = i; unsigned char b[1]; b[0] = (x & 0xff) >> 000; EmitData(a, b, 1); } static void EmitWord(struct As *a, uint128_t i) { uint16_t x = i; unsigned char b[2]; b[0] = (x & 0x00ff) >> 000; b[1] = (x & 0xff00) >> 010; EmitData(a, b, 2); } static void EmitLong(struct As *a, uint128_t i) { uint32_t x = i; unsigned char b[4]; b[0] = (x & 0x000000ff) >> 000; b[1] = (x & 0x0000ff00) >> 010; b[2] = (x & 0x00ff0000) >> 020; b[3] = (x & 0xff000000) >> 030; EmitData(a, b, 4); } void EmitQuad(struct As *a, uint128_t i) { uint64_t x = i; unsigned char b[8]; b[0] = (x & 0x00000000000000ff) >> 000; b[1] = (x & 0x000000000000ff00) >> 010; b[2] = (x & 0x0000000000ff0000) >> 020; b[3] = (x & 0x00000000ff000000) >> 030; b[4] = (x & 0x000000ff00000000) >> 040; b[5] = (x & 0x0000ff0000000000) >> 050; b[6] = (x & 0x00ff000000000000) >> 060; b[7] = (x & 0xff00000000000000) >> 070; EmitData(a, b, 8); } void EmitOcta(struct As *a, uint128_t i) { EmitQuad(a, i); EmitQuad(a, i >> 64); } static void EmitVarword(struct As *a, unsigned long x) { if (x > 255) EmitVarword(a, x >> 8); EmitByte(a, x); } static void OnSleb128(struct As *a, struct Slice s) { int c; int128_t x; for (;;) { x = GetInt(a); for (;;) { c = x & 0x7f; x >>= 7; if ((x == 0 && !(c & 0x40)) || (x == -1 && (c & 0x40))) { break; } else { c |= 0x80; } EmitByte(a, c); if (IsSemicolon(a)) break; ConsumeComma(a); } } } static void OnUleb128(struct As *a, struct Slice s) { int c; uint128_t x; for (;;) { x = GetInt(a); do { c = x & 0x7f; x >>= 7; if (x) c |= 0x80; EmitByte(a, c); } while (x); if (IsSemicolon(a)) break; ConsumeComma(a); } } static void OnZleb128(struct As *a, struct Slice s) { int c; uint128_t x; for (;;) { x = GetInt(a); x = (x << 1) ^ ((int128_t)x >> 127); do { c = x & 0x7f; x >>= 7; if (x) c |= 0x80; EmitByte(a, c); } while (x); if (IsSemicolon(a)) break; ConsumeComma(a); } } static void OnZero(struct As *a, struct Slice s) { long n; char *p; for (;;) { n = GetInt(a); p = calloc(n, 1); EmitData(a, p, n); free(p); if (IsSemicolon(a)) break; ConsumeComma(a); } } static void OnSpace(struct As *a, struct Slice s) { long n; char *p; int fill; p = malloc((n = GetInt(a))); if (IsComma(a)) { ConsumeComma(a); fill = GetInt(a); } else { fill = 0; } memset(p, fill, n); EmitData(a, p, n); free(p); } static long GetRelaAddend(int kind) { switch (kind) { case R_X86_64_PC8: return -1; case R_X86_64_PC16: return -2; case R_X86_64_PC32: case R_X86_64_PLT32: case R_X86_64_GOTPCRELX: return -4; default: return 0; } } static void EmitExpr(struct As *a, int expr, int kind, void emitter(struct As *, uint128_t)) { if (expr == -1) { emitter(a, 0); } else if (a->exprs.p[expr].kind == EX_INT) { emitter(a, a->exprs.p[expr].x); } else { AppendRela(a); a->relas.p[a->relas.n - 1].kind = kind; a->relas.p[a->relas.n - 1].expr = expr; a->relas.p[a->relas.n - 1].section = a->section; a->relas.p[a->relas.n - 1].offset = a->sections.p[a->section].binary.n; a->relas.p[a->relas.n - 1].addend = GetRelaAddend(kind); emitter(a, 0); } } static void OpInt(struct As *a, int kind, void emitter(struct As *, uint128_t)) { for (;;) { EmitExpr(a, Parse(a), kind, emitter); if (IsSemicolon(a)) break; ConsumeComma(a); } } static void OnByte(struct As *a, struct Slice s) { OpInt(a, R_X86_64_8, EmitByte); } static void OnWord(struct As *a, struct Slice s) { OpInt(a, R_X86_64_16, EmitWord); } static void OnLong(struct As *a, struct Slice s) { OpInt(a, R_X86_64_32, EmitLong); } static void OnQuad(struct As *a, struct Slice s) { OpInt(a, R_X86_64_64, EmitQuad); } static void OnOcta(struct As *a, struct Slice s) { OpInt(a, R_X86_64_64, EmitOcta); } static void OnFloat(struct As *a, struct Slice s) { float f; char b[4]; for (;;) { if (IsFloat(a, a->i)) { f = GetFloat(a); } else { f = GetInt(a); } memcpy(b, &f, 4); EmitData(a, b, 4); if (IsSemicolon(a)) break; ConsumeComma(a); } } static void OnDouble(struct As *a, struct Slice s) { double f; char b[8]; for (;;) { if (IsFloat(a, a->i)) { f = GetFloat(a); } else { f = GetInt(a); } memcpy(b, &f, 8); EmitData(a, b, 8); if (IsSemicolon(a)) break; ConsumeComma(a); } } static void OnLongDouble(struct As *a, int n) { char b[16]; long double f; for (;;) { if (IsFloat(a, a->i)) { f = GetFloat(a); } else { f = GetInt(a); } bzero(b, 16); memcpy(b, &f, sizeof(f)); EmitData(a, b, n); if (IsSemicolon(a)) break; ConsumeComma(a); } } static void OnFloat80(struct As *a, struct Slice s) { OnLongDouble(a, 10); } static void OnLdbl(struct As *a, struct Slice s) { OnLongDouble(a, 16); } static void OnAscii(struct As *a, struct Slice s) { struct Slice arg; for (;;) { arg = GetSlice(a); EmitData(a, arg.p, arg.n); if (IsSemicolon(a)) break; ConsumeComma(a); } } static void OnAsciz(struct As *a, struct Slice s) { struct Slice arg; for (;;) { arg = GetSlice(a); EmitData(a, arg.p, arg.n); EmitByte(a, 0); if (IsSemicolon(a)) break; ConsumeComma(a); } } static void OnAbort(struct As *a, struct Slice s) { Fail(a, "aborted"); } static void OnErr(struct As *a, struct Slice s) { if (a->inhibiterr) return; Fail(a, "error"); } static void OnError(struct As *a, struct Slice s) { struct Slice msg = GetSlice(a); if (a->inhibiterr) return; Fail(a, "%.*s", msg.n, msg.p); } static void OnWarning(struct As *a, struct Slice s) { struct Slice msg = GetSlice(a); if (a->inhibitwarn) return; PrintLocation(a); fprintf(stderr, "%.*s\n", msg.n, msg.p); } static void OnText(struct As *a, struct Slice s) { SetSection(a, 1); } static void OnData(struct As *a, struct Slice s) { SetSection(a, 2); } static void OnBss(struct As *a, struct Slice s) { SetSection(a, 3); } static void OnPrevious(struct As *a, struct Slice s) { SetSection(a, a->previous); } static void OnAlign(struct As *a, struct Slice s) { long i, n, align, fill, maxskip; align = GetInt(a); if (!IS2POW(align)) Fail(a, "alignment not power of 2"); fill = (a->sections.p[a->section].flags & SHF_EXECINSTR) ? 0x90 : 0; maxskip = 268435456; if (IsComma(a)) { ++a->i; fill = GetInt(a); if (IsComma(a)) { ++a->i; maxskip = GetInt(a); } } i = a->sections.p[a->section].binary.n; n = ROUNDUP(i, align) - i; if (n > maxskip) return; a->sections.p[a->section].align = MAX(a->sections.p[a->section].align, align); for (i = 0; i < n; ++i) EmitByte(a, fill); } static int SectionFlag(struct As *a, int c) { switch (c) { case 'a': return SHF_ALLOC; case 'w': return SHF_WRITE; case 'x': return SHF_EXECINSTR; case 'g': return SHF_GROUP; case 'M': return SHF_MERGE; case 'S': return SHF_STRINGS; case 'T': return SHF_TLS; default: Fail(a, "unknown section flag: %`'c", c); } } static int SectionFlags(struct As *a, struct Slice s) { int i, flags; for (flags = i = 0; i < s.n; ++i) { flags |= SectionFlag(a, s.p[i]); } return flags; } static int SectionType(struct As *a, struct Slice s) { if (IS(s.p, s.n, "@progbits") || IS(s.p, s.n, "SHT_PROGBITS")) { return SHT_PROGBITS; } else if (IS(s.p, s.n, "@note") || IS(s.p, s.n, "SHT_NOTE")) { return SHT_NOTE; } else if (IS(s.p, s.n, "@nobits") || IS(s.p, s.n, "SHT_NOBITS")) { return SHT_NOBITS; } else if (IS(s.p, s.n, "@preinit_array") || IS(s.p, s.n, "SHT_PREINIT_ARRAY")) { return SHT_PREINIT_ARRAY; } else if (IS(s.p, s.n, "@init_array") || IS(s.p, s.n, "SHT_INIT_ARRAY")) { return SHT_INIT_ARRAY; } else if (IS(s.p, s.n, "@fini_array") || IS(s.p, s.n, "SHT_FINI_ARRAY")) { return SHT_FINI_ARRAY; } else { Fail(a, "unknown section type: %.*s", s.n, s.p); } } static int SymbolType(struct As *a, struct Slice s) { if (IS(s.p, s.n, "@object") || IS(s.p, s.n, "STT_OBJECT")) { return STT_OBJECT; } else if (IS(s.p, s.n, "@function") || IS(s.p, s.n, "STT_FUNC")) { return STT_FUNC; } else if (IS(s.p, s.n, "@common") || IS(s.p, s.n, "STT_COMMON")) { return STT_COMMON; } else if (IS(s.p, s.n, "@notype") || IS(s.p, s.n, "STT_NOTYPE")) { return STT_NOTYPE; } else if (IS(s.p, s.n, "@tls_object") || IS(s.p, s.n, "STT_TLS")) { return STT_TLS; } else { Fail(a, "unknown symbol type: %.*s", s.n, s.p); } } static int GrabSection(struct As *a, int name, int flags, int type, int group, int comdat) { int i; for (i = 0; i < a->sections.n; ++i) { if (!strcmp(a->strings.p[name], a->strings.p[a->sections.p[i].name])) { return i; } } return AppendSection(a, name, flags, type); } static void OnSection(struct As *a, struct Slice s) { int name, flags, type, group = -1, comdat = -1; name = SliceDup(a, GetSlice(a)); if (_startswith(a->strings.p[name], ".text")) { flags = SHF_ALLOC | SHF_EXECINSTR; type = SHT_PROGBITS; } else if (_startswith(a->strings.p[name], ".data")) { flags = SHF_ALLOC | SHF_WRITE; type = SHT_PROGBITS; } else if (_startswith(a->strings.p[name], ".bss")) { flags = SHF_ALLOC | SHF_WRITE; type = SHT_NOBITS; } else { flags = SHF_ALLOC | SHF_EXECINSTR | SHF_WRITE; type = SHT_PROGBITS; } if (IsComma(a)) { ++a->i; flags = SectionFlags(a, GetSlice(a)); if (IsComma(a)) { ++a->i; type = SectionType(a, GetSlice(a)); if (IsComma(a)) { ++a->i; group = SectionType(a, GetSlice(a)); if (IsComma(a)) { ++a->i; comdat = SectionType(a, GetSlice(a)); } } } } SetSection(a, GrabSection(a, name, flags, type, group, comdat)); } static void OnPushsection(struct As *a, struct Slice s) { APPEND(a->sectionstack); a->sectionstack.p[a->sectionstack.n - 1] = a->section; OnSection(a, s); } static void OnPopsection(struct As *a, struct Slice s) { if (!a->sectionstack.n) Fail(a, "stack smashed"); a->section = a->sectionstack.p[--a->sectionstack.n]; } static void OnIdent(struct As *a, struct Slice s) { struct Slice arg; int comment, oldsection; comment = GrabSection(a, StrDup(a, ".comment"), SHF_MERGE | SHF_STRINGS, SHT_PROGBITS, -1, -1); oldsection = a->section; a->section = comment; arg = GetSlice(a); EmitData(a, arg.p, arg.n); EmitByte(a, 0); a->section = oldsection; } static void OnIncbin(struct As *a, struct Slice s) { int fd; struct stat st; char *path, *path2; struct Slice *data, arg; arg = GetSlice(a); path = strndup(arg.p, arg.n); if ((path2 = FindInclude(a, path))) { if ((fd = open(path2, O_RDONLY)) == -1 || fstat(fd, &st) == -1) { Fail(a, "open failed: %s", path2); } data = &a->sections.p[a->section].binary; data->p = realloc(data->p, data->n + st.st_size); if (read(fd, data->p, st.st_size) != st.st_size) { Fail(a, "read failed: %s", path2); } data->n += st.st_size; close(fd); free(path2); } else { Fail(a, "not found: %s", path); } free(path); } static void OnType(struct As *a, struct Slice s) { int i; i = GetSymbol(a, a->things.p[a->i++].i); ConsumeComma(a); a->symbols.p[i].type = SymbolType(a, GetSlice(a)); } static void OnSize(struct As *a, struct Slice s) { int i; i = GetSymbol(a, a->things.p[a->i++].i); ConsumeComma(a); a->symbols.p[i].size = GetInt(a); } static void OnEqu(struct As *a, struct Slice s) { int i, j; i = GetSymbol(a, a->things.p[a->i++].i); ConsumeComma(a); a->symbols.p[i].offset = GetInt(a); a->symbols.p[i].section = SHN_ABS; } static void OnComm(struct As *a, struct Slice s) { int i; i = GetSymbol(a, a->things.p[a->i++].i); ConsumeComma(a); a->symbols.p[i].size = GetInt(a); a->symbols.p[i].type = STT_COMMON; a->symbols.p[i].section = SHN_COMMON; } static void OpVisibility(struct As *a, int visibility) { int i; for (;;) { i = GetSymbol(a, a->things.p[a->i++].i); a->symbols.p[i].stv = visibility; if (IsSemicolon(a)) break; ConsumeComma(a); } } static void OnInternal(struct As *a, struct Slice s) { OpVisibility(a, STV_INTERNAL); } static void OnHidden(struct As *a, struct Slice s) { OpVisibility(a, STV_HIDDEN); } static void OnProtected(struct As *a, struct Slice s) { OpVisibility(a, STV_PROTECTED); } static void OpBind(struct As *a, int bind) { int i; for (;;) { i = GetSymbol(a, a->things.p[a->i++].i); a->symbols.p[i].stb = bind; if (IsSemicolon(a)) break; ConsumeComma(a); } } static void OnLocal(struct As *a, struct Slice s) { OpBind(a, STB_LOCAL); } static void OnWeak(struct As *a, struct Slice s) { OpBind(a, STB_WEAK); } static void OnGlobal(struct As *a, struct Slice s) { OpBind(a, STB_GLOBAL); } static int GetOpSize(struct As *a, struct Slice s, int modrm, int i) { if (modrm & ISREG) { return (modrm & 070) >> 3; } else { switch (s.p[s.n - i]) { case 'b': case 'B': return 0; case 'w': case 'W': return 1; case 'l': case 'L': return 2; case 'q': case 'Q': return 3; default: Fail(a, "could not size instruction"); } } } static bool ConsumeSegment(struct As *a) { int i; struct Slice s; if (IsSlice(a, a->i)) { s = a->slices.p[a->things.p[a->i].i]; if (s.n == 3 && *s.p == '%') { for (i = 0; i < ARRAYLEN(kSegment); ++i) { if (s.p[1] == kSegment[i][0] && s.p[2] == kSegment[i][1]) { ++a->i; EmitByte(a, kSegmentByte[i]); ConsumePunct(a, ':'); return true; } } } } return false; } static void CopyLower(char *k, const char *p, int n) { int i; for (i = 0; i < n; ++i) { k[i] = tolower(p[i]); } } static unsigned long MakeKey64(const char *p, int n) { char k[8] = {0}; CopyLower(k, p, n); return READ64BE(k); } static uint128_t MakeKey128(const char *p, int n) { char k[16] = {0}; CopyLower(k, p, n); return READ128BE(k); } static bool Prefix(struct As *a, const char *p, int n) { int m, l, r; unsigned long x, y; if (n && n <= 8) { x = MakeKey64(p, n); l = 0; r = ARRAYLEN(kPrefix) - 1; while (l <= r) { m = (l + r) >> 1; y = READ64BE(kPrefix[m]); if (x < y) { r = m - 1; } else if (x > y) { l = m + 1; } else { EmitByte(a, kPrefixByte[m]); return true; } } } return false; } static bool FindReg(const char *p, int n, struct Reg *out_reg) { int m, l, r; unsigned long x, y; if (n && n <= 8 && *p == '%') { ++p; --n; x = MakeKey64(p, n); l = 0; r = ARRAYLEN(kRegs) - 1; while (l <= r) { m = (l + r) >> 1; y = READ64BE(kRegs[m].s); if (x < y) { r = m - 1; } else if (x > y) { l = m + 1; } else { *out_reg = kRegs[m]; return true; } } } return false; } static int FindRegReg(struct Slice s) { struct Reg reg; if (!FindReg(s.p, s.n, &reg)) return -1; return reg.reg; } static int FindRegRm(struct Slice s) { struct Reg reg; if (!FindReg(s.p, s.n, &reg)) return -1; return reg.rm; } static int FindRegBase(struct Slice s) { struct Reg reg; if (!FindReg(s.p, s.n, &reg)) return -1; return reg.base; } static int FindRegIndex(struct Slice s) { struct Reg reg; if (!FindReg(s.p, s.n, &reg)) return -1; return reg.index; } static int RemoveRexw(int x) { if (x == -1) return x; x &= ~0x0800; if (((x & 0xff00) >> 8) == REX) x &= ~0xff00; return x; } static int GetRegisterReg(struct As *a) { int reg; struct Slice wut; if ((reg = FindRegReg(GetSlice(a))) == -1) InvalidRegister(a); return reg; } static int GetRegisterRm(struct As *a) { int reg; struct Slice wut; if ((reg = FindRegRm(GetSlice(a))) == -1) InvalidRegister(a); return reg; } static int ParseModrm(struct As *a, int *disp) { /* ┌isreg │┌isrip ││┌hasindex │││┌hasbase ││││┌hasasz │││││┌rex ││││││ ┌scale ││││││ │ ┌index or size ││││││ │ │ ┌base or reg │││││├──────┐├┐├─┐├─┐ 0b00000000000000000000000000000000*/ struct Slice str; int reg, scale, modrm = 0; if (!ConsumeSegment(a) && IsRegister(a, a->i)) { *disp = 0; modrm = GetRegisterRm(a) | ISREG; } else { if (!IsPunct(a, a->i, '(')) { *disp = Parse(a); } else { *disp = -1; } if (IsPunct(a, a->i, '(')) { ++a->i; if (!IsComma(a)) { str = GetSlice(a); modrm |= HASBASE; if (!strncasecmp(str.p, "%rip", str.n)) { modrm |= ISRIP; } else { reg = FindRegBase(str); if (reg == -1) InvalidRegister(a); modrm |= reg & 007; // reg modrm |= reg & 0xff00; // rex if (((reg & 070) >> 3) == 2) modrm |= HASASZ; // asz } } if (IsComma(a)) { ++a->i; modrm |= HASINDEX; reg = FindRegIndex(GetSlice(a)); if (reg == -1) InvalidRegister(a); modrm |= (reg & 007) << 3; // index modrm |= reg & 0xff00; // rex if (((reg & 070) >> 3) == 2) modrm |= HASASZ; // asz if (IsComma(a)) { ++a->i; modrm |= (_bsr(GetInt(a)) & 3) << 6; } } ConsumePunct(a, ')'); } if (modrm & HASASZ) { EmitByte(a, ASZ); } } return modrm; } static void EmitImm(struct As *a, int reg, int imm) { switch ((reg & 030) >> 3) { case 0: EmitExpr(a, imm, R_X86_64_8, EmitByte); break; case 1: EmitExpr(a, imm, R_X86_64_16, EmitWord); break; case 2: case 3: EmitExpr(a, imm, R_X86_64_32S, EmitLong); break; default: abort(); } } static void EmitModrm(struct As *a, int reg, int modrm, int disp) { int relo, mod, rm; void (*emitter)(struct As *, uint128_t); reg &= 7; reg <<= 3; if (modrm & ISREG) { EmitByte(a, 0300 | reg | modrm & 7); } else { if (modrm & ISRIP) { EmitByte(a, 005 | reg); emitter = EmitLong; relo = a->pcrelative ?: R_X86_64_PC32; } else if (modrm & (HASBASE | HASINDEX)) { if (disp == -1) { emitter = NULL; relo = 0; mod = 0; } else if (a->exprs.p[disp].kind == EX_INT) { if (!a->exprs.p[disp].x) { emitter = NULL; relo = 0; mod = 0; } else if (-128 <= a->exprs.p[disp].x && a->exprs.p[disp].x <= 127) { emitter = EmitByte; relo = R_X86_64_32S; mod = 0100; } else { emitter = EmitLong; relo = R_X86_64_32S; mod = 0200; } } else { emitter = EmitLong; relo = R_X86_64_32S; mod = 0200; } if (!(modrm & HASINDEX) && (modrm & 7) != 4) { EmitByte(a, mod | reg | modrm); } else if (!(modrm & HASINDEX) && (modrm & 7) == 4) { EmitByte(a, mod | reg | 4); // (%rsp) must be (%rsp,%riz) EmitByte(a, 040 | modrm); } else if (!mod && (modrm & 7) == 5) { EmitByte(a, 0100 | reg | 4); // (%rbp,𝑥) is special EmitByte(a, modrm); EmitByte(a, 0); } else { EmitByte(a, mod | reg | 4); EmitByte(a, modrm); } } else { EmitByte(a, 004 | reg); // (%rbp,%riz) is disp32 EmitByte(a, 045); emitter = EmitLong; relo = R_X86_64_32S; } if (emitter) { EmitExpr(a, disp, relo, emitter); } } } static void EmitRex(struct As *a, int x) { if (x & 0xff00) { EmitByte(a, x >> 8); } } static void EmitOpModrm(struct As *a, long op, int reg, int modrm, int disp, int skew) { switch ((reg & 070) >> 3) { case 0: EmitVarword(a, op); EmitModrm(a, reg, modrm, disp); break; case 1: EmitVarword(a, op + skew); EmitModrm(a, reg, modrm, disp); break; case 2: case 3: case 4: EmitVarword(a, op + skew); EmitModrm(a, reg, modrm, disp); break; default: abort(); } } static void EmitRexOpModrm(struct As *a, long op, int reg, int modrm, int disp, int skew) { if (((reg & 070) >> 3) == 1) EmitByte(a, OSZ); EmitRex(a, reg | modrm); EmitOpModrm(a, op, reg, modrm, disp, skew); } static void OnLoad(struct As *a, struct Slice s, int op) { int modrm, reg, disp; modrm = ParseModrm(a, &disp); ConsumeComma(a); reg = GetRegisterReg(a); EmitRexOpModrm(a, op, reg, modrm, disp, 0); } static void OnLea(struct As *a, struct Slice s) { return OnLoad(a, s, 0x8D); } static void OnLar(struct As *a, struct Slice s) { return OnLoad(a, s, 0x0f02); } static void OnLsl(struct As *a, struct Slice s) { return OnLoad(a, s, 0x0f03); } static void OnMov(struct As *a, struct Slice s) { int reg, modrm, disp, imm; if (IsPunct(a, a->i, '$')) { ++a->i; imm = Parse(a); ConsumeComma(a); if (IsSlice(a, a->i)) { // imm -> reg reg = GetRegisterRm(a); switch ((reg & 070) >> 3) { case 0: EmitRex(a, reg); EmitByte(a, 0xB0 + (reg & 7)); EmitExpr(a, imm, R_X86_64_8, EmitByte); break; case 1: EmitByte(a, OSZ); EmitRex(a, reg); EmitByte(a, 0xB8 + (reg & 7)); EmitExpr(a, imm, R_X86_64_16, EmitWord); break; case 2: EmitRex(a, reg); EmitByte(a, 0xB8 + (reg & 7)); EmitExpr(a, imm, R_X86_64_32, EmitLong); break; case 3: EmitRex(a, reg); EmitByte(a, 0xB8 + (reg & 7)); // suboptimal EmitExpr(a, imm, R_X86_64_64, EmitQuad); break; default: Fail(a, "todo movd/movq"); } } else { // imm -> modrm modrm = ParseModrm(a, &disp); switch (GetOpSize(a, s, modrm, 1)) { case 0: EmitRex(a, modrm); EmitByte(a, 0xC6); EmitModrm(a, 0, modrm, disp); EmitExpr(a, imm, R_X86_64_8, EmitByte); break; case 1: EmitByte(a, OSZ); EmitRex(a, modrm); EmitByte(a, 0xC7); EmitModrm(a, 0, modrm, disp); EmitExpr(a, imm, R_X86_64_16, EmitWord); break; case 2: EmitRex(a, modrm); EmitByte(a, 0xC7); EmitModrm(a, 0, modrm, disp); EmitExpr(a, imm, R_X86_64_32, EmitLong); break; case 3: EmitRex(a, modrm | REXW << 8); EmitByte(a, 0xC7); // suboptimal EmitModrm(a, 0, modrm, disp); EmitExpr(a, imm, R_X86_64_32, EmitLong); break; default: abort(); } } } else if (IsSlice(a, a->i)) { // reg -> reg/modrm reg = GetRegisterReg(a); ConsumeComma(a); modrm = ParseModrm(a, &disp); EmitRexOpModrm(a, 0x88, reg, modrm, disp, 1); } else { // modrm -> reg modrm = ParseModrm(a, &disp); ConsumeComma(a); reg = GetRegisterReg(a); EmitRexOpModrm(a, 0x8A, reg, modrm, disp, 1); } } static void EmitMovx(struct As *a, struct Slice opname, int op) { int reg, modrm, disp; modrm = ParseModrm(a, &disp); ConsumeComma(a); reg = GetRegisterReg(a); EmitRex(a, reg); if (((reg & 070) >> 3) == 1) EmitByte(a, OSZ); EmitVarword(a, op + !!GetOpSize(a, opname, modrm, 2)); EmitModrm(a, reg, modrm, disp); } static void OnMovzbwx(struct As *a, struct Slice s) { EmitMovx(a, s, 0x0FB6); } static void OnMovsbwx(struct As *a, struct Slice s) { EmitMovx(a, s, 0x0FBE); } static void OnMovslq(struct As *a, struct Slice s) { int reg, modrm, disp; modrm = ParseModrm(a, &disp); ConsumeComma(a); reg = GetRegisterReg(a); EmitByte(a, REXW); EmitByte(a, 0x63); EmitModrm(a, reg, modrm, disp); } static dontinline void OpAluImpl(struct As *a, struct Slice opname, int op) { int reg, modrm, imm, disp; if (IsPunct(a, a->i, '$')) { // imm -> reg/modrm ++a->i; imm = Parse(a); ConsumeComma(a); modrm = ParseModrm(a, &disp); reg = GetOpSize(a, opname, modrm, 1) << 3 | op; EmitRexOpModrm(a, 0x80, reg, modrm, disp, 1); // suboptimal EmitImm(a, reg, imm); } else if (IsSlice(a, a->i)) { // reg -> reg/modrm reg = GetRegisterReg(a); ConsumeComma(a); modrm = ParseModrm(a, &disp); EmitRexOpModrm(a, op << 3, reg, modrm, disp, 1); } else { // modrm -> reg modrm = ParseModrm(a, &disp); ConsumeComma(a); reg = GetRegisterReg(a); EmitRexOpModrm(a, op << 3 | 2, reg, modrm, disp, 1); } } static dontinline void OpAlu(struct As *a, struct Slice opname, int op) { OpAluImpl(a, opname, op); } static dontinline void OpBsuImpl(struct As *a, struct Slice opname, int op) { int reg, modrm, imm, disp; if (IsPunct(a, a->i, '$')) { ++a->i; imm = Parse(a); ConsumeComma(a); } else if (IsSlice(a, a->i) && (a->slices.p[a->things.p[a->i].i].n == 3 && !strncasecmp(a->slices.p[a->things.p[a->i].i].p, "%cl", 3)) && !IsPunct(a, a->i + 1, ';')) { ++a->i; ConsumeComma(a); modrm = ParseModrm(a, &disp); reg = GetOpSize(a, opname, modrm, 1) << 3 | op; EmitRexOpModrm(a, 0xD2, reg, modrm, disp, 1); return; } else { AppendExpr(a); a->exprs.p[a->exprs.n - 1].kind = EX_INT; a->exprs.p[a->exprs.n - 1].tok = a->i; a->exprs.p[a->exprs.n - 1].x = 1; imm = a->exprs.n - 1; } modrm = ParseModrm(a, &disp); reg = GetOpSize(a, opname, modrm, 1) << 3 | op; EmitRexOpModrm(a, 0xC0, reg, modrm, disp, 1); // suboptimal EmitExpr(a, imm, R_X86_64_8, EmitByte); } static dontinline void OpBsu(struct As *a, struct Slice opname, int op) { OpBsuImpl(a, opname, op); } static dontinline void OpXadd(struct As *a) { int reg, modrm, disp; reg = GetRegisterReg(a); ConsumeComma(a); modrm = ParseModrm(a, &disp); EmitRexOpModrm(a, 0x0FC0, reg, modrm, disp, 1); } static dontinline int OpF6Impl(struct As *a, struct Slice s, int reg) { int modrm, imm, disp; modrm = ParseModrm(a, &disp); reg |= GetOpSize(a, s, modrm, 1) << 3; EmitRexOpModrm(a, 0xF6, reg, modrm, disp, 1); return reg; } static dontinline int OpF6(struct As *a, struct Slice s, int reg) { return OpF6Impl(a, s, reg); } static void OnTest(struct As *a, struct Slice s) { int reg, modrm, imm, disp; if (IsPunct(a, a->i, '$')) { ++a->i; imm = Parse(a); ConsumeComma(a); reg = OpF6(a, s, 0); // suboptimal EmitImm(a, reg, imm); } else { reg = GetRegisterReg(a); ConsumeComma(a); modrm = ParseModrm(a, &disp); EmitRexOpModrm(a, 0x84, reg, modrm, disp, 1); } } static void OnCmpxchg(struct As *a, struct Slice s) { int reg, modrm, disp; reg = GetRegisterReg(a); ConsumeComma(a); modrm = ParseModrm(a, &disp); EmitRexOpModrm(a, 0x0FB0, reg, modrm, disp, 1); } static void OnImul(struct As *a, struct Slice s) { int reg, modrm, imm, disp; if (IsPunct(a, a->i, '$')) { ++a->i; imm = Parse(a); ConsumeComma(a); modrm = ParseModrm(a, &disp); ConsumeComma(a); reg = GetRegisterReg(a); EmitRexOpModrm(a, 0x69, reg, modrm, disp, 0); EmitImm(a, reg, imm); } else { modrm = ParseModrm(a, &disp); if (IsComma(a)) { ++a->i; reg = GetRegisterReg(a); EmitRexOpModrm(a, 0x0FAF, reg, modrm, disp, 0); } else { reg = GetOpSize(a, s, modrm, 1) << 3 | 5; EmitRexOpModrm(a, 0xF6, reg, modrm, disp, 1); } } } static void OpBit(struct As *a, int op) { int reg, modrm, disp; modrm = ParseModrm(a, &disp); ConsumeComma(a); reg = GetRegisterReg(a); EmitRexOpModrm(a, op, reg, modrm, disp, 0); } static void OnXchg(struct As *a, struct Slice s) { int reg, modrm, disp; reg = GetRegisterReg(a); ConsumeComma(a); modrm = ParseModrm(a, &disp); EmitRexOpModrm(a, 0x86, reg, modrm, disp, 1); } static void OpBump(struct As *a, struct Slice s, int reg) { int modrm, disp; modrm = ParseModrm(a, &disp); EmitRexOpModrm(a, 0xFE, GetOpSize(a, s, modrm, 1) << 3 | reg, modrm, disp, 1); } static void OpShrld(struct As *a, struct Slice s, int op) { int imm, reg, modrm, disp, skew; if (IsSlice(a, a->i) && (a->slices.p[a->things.p[a->i].i].n == 3 && !strncasecmp(a->slices.p[a->things.p[a->i].i].p, "%cl", 3))) { ++a->i; ConsumeComma(a); skew = 1; reg = GetRegisterReg(a); ConsumeComma(a); modrm = ParseModrm(a, &disp); EmitRexOpModrm(a, 0x0F00 | op | skew, reg, modrm, disp, 0); } else { skew = 0; ConsumePunct(a, '$'); imm = GetInt(a); ConsumeComma(a); reg = GetRegisterReg(a); modrm = ParseModrm(a, &disp); EmitRexOpModrm(a, 0x0F00 | op | skew, reg, modrm, disp, 0); EmitExpr(a, imm, R_X86_64_8, EmitByte); } } static void OnShrd(struct As *a, struct Slice s) { OpShrld(a, s, 0xAC); } static void OnShld(struct As *a, struct Slice s) { OpShrld(a, s, 0xA4); } static void OpSseMov(struct As *a, int opWsdVsd, int opVsdWsd) { int reg, modrm, disp; if (IsRegister(a, a->i)) { reg = GetRegisterReg(a); ConsumeComma(a); modrm = ParseModrm(a, &disp); EmitRexOpModrm(a, opWsdVsd, reg, modrm, disp, 0); } else { modrm = ParseModrm(a, &disp); ConsumeComma(a); reg = GetRegisterReg(a); EmitRexOpModrm(a, opVsdWsd, reg, modrm, disp, 0); } } static void OpMovntdq(struct As *a) { int reg, modrm, disp; reg = GetRegisterReg(a); ConsumeComma(a); modrm = ParseModrm(a, &disp); EmitRexOpModrm(a, 0x660FE7, reg, modrm, disp, 0); } static void OpMovdqx(struct As *a, int op) { OpSseMov(a, op + 0x10, op); } static void OnMovdqu(struct As *a, struct Slice s) { EmitByte(a, 0xF3); OpMovdqx(a, 0x0F6F); } static void OnMovups(struct As *a, struct Slice s) { OpSseMov(a, 0x0F11, 0x0F10); } static void OnMovupd(struct As *a, struct Slice s) { EmitByte(a, 0x66); OnMovups(a, s); } static void OnMovdqa(struct As *a, struct Slice s) { EmitByte(a, 0x66); OpMovdqx(a, 0x0F6F); } static void OnMovaps(struct As *a, struct Slice s) { OpSseMov(a, 0x0F29, 0x0F28); } static void OnMovapd(struct As *a, struct Slice s) { EmitByte(a, 0x66); OpSseMov(a, 0x0F29, 0x0F28); } static void OpMovdq(struct As *a, bool is64) { int reg, modrm, boop, disp, ugh; EmitByte(a, 0x66); // todo mmx if (IsRegister(a, a->i)) { reg = GetRegisterReg(a); ConsumeComma(a); modrm = ParseModrm(a, &disp); if (!(modrm & ISREG)) { if (((reg & 070) >> 3) == 4) { if (is64) reg |= REXW << 8; boop = 0x10; } else { boop = 0; } } else { if (((reg & 070) >> 3) == 4) { boop = 0x10; } else { boop = 0; ugh = modrm & 7; modrm &= ~7; modrm |= reg & 7; reg &= ~7; reg |= ugh; } } } else { modrm = ParseModrm(a, &disp); ConsumeComma(a); reg = GetRegisterReg(a); if (!(modrm & ISREG)) { if (((reg & 070) >> 3) == 4) { if (is64) reg |= REXW << 8; boop = 0; } else { boop = 0x10; } } else { boop = ((modrm & 070) >> 3) == 4 ? 0 : 0x10; } } EmitRexOpModrm(a, 0x0F6E + boop, reg, modrm, disp, 0); } static void OnMovss(struct As *a, struct Slice s) { OpSseMov(a, 0xF30F11, 0xF30F10); } static void OnMovsd(struct As *a, struct Slice s) { OpSseMov(a, 0xF20F11, 0xF20F10); } static bool IsSsePrefix(int c) { return c == 0x66 || c == 0xF2 || c == 0xF3; // must come before rex } static dontinline void OpSseImpl(struct As *a, int op) { int reg, modrm, disp; if (IsSsePrefix((op & 0xff000000) >> 24)) { EmitByte(a, (op & 0xff000000) >> 24); op &= 0xffffff; } if (IsSsePrefix((op & 0x00ff0000) >> 16)) { EmitByte(a, (op & 0x00ff0000) >> 16); op &= 0xffff; } modrm = ParseModrm(a, &disp); ConsumeComma(a); reg = GetRegisterReg(a); EmitRexOpModrm(a, op, reg, modrm, disp, 0); } static dontinline void OpSse(struct As *a, int op) { OpSseImpl(a, op); } static dontinline void OpSseIbImpl(struct As *a, int op) { int imm; ConsumePunct(a, '$'); imm = Parse(a); ConsumeComma(a); OpSse(a, op); EmitExpr(a, imm, R_X86_64_8, EmitByte); } static dontinline void OpSseIb(struct As *a, int op) { OpSseIbImpl(a, op); } static bool HasXmmOnLine(struct As *a) { int i; for (i = 0; !IsPunct(a, a->i + i, ';'); ++i) { if (IsSlice(a, a->i + i) && a->slices.p[a->things.p[a->i + i].i].n >= 4 && (_startswith(a->slices.p[a->things.p[a->i + i].i].p, "xmm") || _startswith(a->slices.p[a->things.p[a->i + i].i].p, "%xmm"))) { return true; } } return false; } static void OnMovd(struct As *a, struct Slice s) { OpMovdq(a, false); } static void OnMovq(struct As *a, struct Slice s) { if (HasXmmOnLine(a)) { OpMovdq(a, true); } else { OnMov(a, s); } } static void OnPush(struct As *a, struct Slice s) { int modrm, disp; if (IsPunct(a, a->i, '$')) { ++a->i; EmitByte(a, 0x68); EmitLong(a, GetInt(a)); } else { modrm = RemoveRexw(ParseModrm(a, &disp)); EmitRexOpModrm(a, 0xFF, 6, modrm, disp, 0); // suboptimal } } static void OnRdpid(struct As *a, struct Slice s) { int modrm, disp; EmitVarword(a, 0xf30fc7); EmitByte(a, 0370 | GetRegisterReg(a)); } static void OnPop(struct As *a, struct Slice s) { int modrm, disp; modrm = RemoveRexw(ParseModrm(a, &disp)); EmitRexOpModrm(a, 0x8F, 0, modrm, disp, 0); // suboptimal } static void OnNop(struct As *a, struct Slice opname) { int modrm, disp; if (IsSemicolon(a)) { EmitByte(a, 0x90); } else { modrm = ParseModrm(a, &disp); EmitRexOpModrm(a, 0x0F1F, 6, modrm, disp, 0); } } static void OnRet(struct As *a, struct Slice s) { if (IsPunct(a, a->i, '$')) { ++a->i; EmitByte(a, 0xC2); EmitWord(a, GetInt(a)); } else { EmitByte(a, 0xC3); } } static dontinline void OpCmovccImpl(struct As *a, int cc) { int reg, modrm, disp; modrm = ParseModrm(a, &disp); ConsumeComma(a); reg = GetRegisterReg(a); EmitRexOpModrm(a, 0x0F40 | cc, reg, modrm, disp, 0); } static dontinline void OpCmovcc(struct As *a, int cc) { OpCmovccImpl(a, cc); } static dontinline void OpSetccImpl(struct As *a, int cc) { int modrm, disp; modrm = ParseModrm(a, &disp); EmitRexOpModrm(a, 0x0F90 | cc, 6, modrm, disp, 0); } static dontinline void OpSetcc(struct As *a, int cc) { OpSetccImpl(a, cc); } static void OnFile(struct As *a, struct Slice s) { int fileno; struct Slice path; fileno = GetInt(a); path = GetSlice(a); // TODO: DWARF } static void OnLoc(struct As *a, struct Slice s) { int fileno, lineno; fileno = GetInt(a); lineno = GetInt(a); // TODO: DWARF } static void OnCall(struct As *a, struct Slice s) { int modrm, disp; if (IsPunct(a, a->i, '*')) ++a->i; modrm = RemoveRexw(ParseModrm(a, &disp)); if (modrm & (ISREG | ISRIP | HASINDEX | HASBASE)) { if (modrm & ISRIP) a->pcrelative = R_X86_64_GOTPCRELX; EmitRexOpModrm(a, 0xFF, 2, modrm, disp, 0); a->pcrelative = 0; } else { EmitByte(a, 0xE8); EmitExpr(a, disp, R_X86_64_PC32, EmitLong); } } static dontinline void OpJmpImpl(struct As *a, int cc) { int modrm, disp; if (IsPunct(a, a->i, '*')) ++a->i; modrm = RemoveRexw(ParseModrm(a, &disp)); if (cc == -1) { if (modrm & (ISREG | ISRIP | HASINDEX | HASBASE)) { if (modrm & ISRIP) a->pcrelative = R_X86_64_GOTPCRELX; EmitRexOpModrm(a, 0xFF, 4, modrm, disp, 0); a->pcrelative = 0; } else { EmitByte(a, 0xE9); EmitExpr(a, disp, R_X86_64_PC32, EmitLong); } } else { EmitByte(a, 0x0F); EmitByte(a, 0x80 + cc); EmitExpr(a, disp, R_X86_64_PC32, EmitLong); } } static dontinline void OpJmp(struct As *a, int cc) { OpJmpImpl(a, cc); } static dontinline void OpFpu1Impl(struct As *a, int op, int reg) { int modrm, disp; modrm = ParseModrm(a, &disp); EmitRexOpModrm(a, op, reg, modrm, disp, 0); } static dontinline void OpFpu1(struct As *a, int op, int reg) { OpFpu1Impl(a, op, reg); } static void OnFxch(struct As *a, struct Slice s) { int rm; rm = !IsSemicolon(a) ? GetRegisterRm(a) : 1; EmitByte(a, 0xD9); EmitByte(a, 0310 | rm & 7); } static void OnBswap(struct As *a, struct Slice s) { int srm; srm = GetRegisterRm(a); EmitRex(a, srm); EmitByte(a, 0x0F); EmitByte(a, 0310 | srm & 7); } static dontinline void OpFcomImpl(struct As *a, int op) { int rm; if (IsSemicolon(a)) { rm = 1; } else { rm = GetRegisterRm(a); if (IsComma(a)) { ++a->i; if (GetRegisterReg(a) & 7) { Fail(a, "bad register"); } } } EmitVarword(a, op | rm & 7); } static dontinline void OpFcom(struct As *a, int op) { OpFcomImpl(a, op); } // clang-format off static void OnAdc(struct As *a, struct Slice s) { OpAlu(a, s, 2); } static void OnAdd(struct As *a, struct Slice s) { OpAlu(a, s, 0); } static void OnAddpd(struct As *a, struct Slice s) { OpSse(a, 0x660F58); } static void OnAddps(struct As *a, struct Slice s) { OpSse(a, 0x0F58); } static void OnAddsd(struct As *a, struct Slice s) { OpSse(a, 0xF20F58); } static void OnAddss(struct As *a, struct Slice s) { OpSse(a, 0xF30F58); } static void OnAddsubpd(struct As *a, struct Slice s) { OpSse(a, 0x660FD0); } static void OnAddsubps(struct As *a, struct Slice s) { OpSse(a, 0xF20FD0); } static void OnAnd(struct As *a, struct Slice s) { OpAlu(a, s, 4); } static void OnAndnpd(struct As *a, struct Slice s) { OpSse(a, 0x660F55); } static void OnAndnps(struct As *a, struct Slice s) { OpSse(a, 0x0F55); } static void OnAndpd(struct As *a, struct Slice s) { OpSse(a, 0x660F54); } static void OnAndps(struct As *a, struct Slice s) { OpSse(a, 0x0F54); } static void OnBlendpd(struct As *a, struct Slice s) { OpSseIb(a, 0x660F3A0D); } static void OnBlendvpd(struct As *a, struct Slice s) { OpSse(a, 0x660F3815); } static void OnBsf(struct As *a, struct Slice s) { OpBit(a, 0x0FBC); } static void OnBsr(struct As *a, struct Slice s) { OpBit(a, 0x0FBD); } static void OnCbtw(struct As *a, struct Slice s) { EmitVarword(a, 0x6698); } static void OnClc(struct As *a, struct Slice s) { EmitByte(a, 0xF8); } static void OnCld(struct As *a, struct Slice s) { EmitByte(a, 0xFC); } static void OnCli(struct As *a, struct Slice s) { EmitByte(a, 0xFA); } static void OnCltd(struct As *a, struct Slice s) { EmitByte(a, 0x99); } static void OnCltq(struct As *a, struct Slice s) { EmitVarword(a, 0x4898); } static void OnCmc(struct As *a, struct Slice s) { EmitByte(a, 0xF5); } static void OnCmovb(struct As *a, struct Slice s) { OpCmovcc(a, 2); } static void OnCmovbe(struct As *a, struct Slice s) { OpCmovcc(a, 6); } static void OnCmovl(struct As *a, struct Slice s) { OpCmovcc(a, 12); } static void OnCmovle(struct As *a, struct Slice s) { OpCmovcc(a, 14); } static void OnCmovnb(struct As *a, struct Slice s) { OpCmovcc(a, 3); } static void OnCmovnbe(struct As *a, struct Slice s) { OpCmovcc(a, 7); } static void OnCmovnl(struct As *a, struct Slice s) { OpCmovcc(a, 13); } static void OnCmovnle(struct As *a, struct Slice s) { OpCmovcc(a, 15); } static void OnCmovno(struct As *a, struct Slice s) { OpCmovcc(a, 1); } static void OnCmovnp(struct As *a, struct Slice s) { OpCmovcc(a, 11); } static void OnCmovns(struct As *a, struct Slice s) { OpCmovcc(a, 9); } static void OnCmovnz(struct As *a, struct Slice s) { OpCmovcc(a, 5); } static void OnCmovo(struct As *a, struct Slice s) { OpCmovcc(a, 0); } static void OnCmovp(struct As *a, struct Slice s) { OpCmovcc(a, 10); } static void OnCmovs(struct As *a, struct Slice s) { OpCmovcc(a, 8); } static void OnCmovz(struct As *a, struct Slice s) { OpCmovcc(a, 4); } static void OnCmp(struct As *a, struct Slice s) { OpAlu(a, s, 7); } static void OnCmppd(struct As *a, struct Slice s) { OpSseIb(a, 0x660FC2); } static void OnCmpps(struct As *a, struct Slice s) { OpSseIb(a, 0x0FC2); } static void OnCmpsd(struct As *a, struct Slice s) { OpSseIb(a, 0xF20FC2); } static void OnCmpss(struct As *a, struct Slice s) { OpSseIb(a, 0xF30FC2); } static void OnComisd(struct As *a, struct Slice s) { OpSse(a, 0x660F2F); } static void OnComiss(struct As *a, struct Slice s) { OpSse(a, 0x0F2F); } static void OnCqto(struct As *a, struct Slice s) { EmitVarword(a, 0x4899); } static void OnCvtdq2pd(struct As *a, struct Slice s) { OpSse(a, 0xF30FE6); } static void OnCvtdq2ps(struct As *a, struct Slice s) { OpSse(a, 0xF5B); } static void OnCvtpd2dq(struct As *a, struct Slice s) { OpSse(a, 0xF20FE6); } static void OnCvtpd2ps(struct As *a, struct Slice s) { OpSse(a, 0x660F5A); } static void OnCvtps2dq(struct As *a, struct Slice s) { OpSse(a, 0x660F5B); } static void OnCvtps2pd(struct As *a, struct Slice s) { OpSse(a, 0x0F5A); } static void OnCvtsd2ss(struct As *a, struct Slice s) { OpSse(a, 0xF20F5A); } static void OnCvtsi2sd(struct As *a, struct Slice s) { OpSse(a, 0xF20F2A); } static void OnCvtsi2ss(struct As *a, struct Slice s) { OpSse(a, 0xF30F2A); } static void OnCvtss2sd(struct As *a, struct Slice s) { OpSse(a, 0xF30F5A); } static void OnCvttpd2dq(struct As *a, struct Slice s) { OpSse(a, 0x660FE6); } static void OnCvttps2dq(struct As *a, struct Slice s) { OpSse(a, 0xF30F5B); } static void OnCvttsd2si(struct As *a, struct Slice s) { OpSse(a, 0xF20F2C); } static void OnCvttss2si(struct As *a, struct Slice s) { OpSse(a, 0xF30F2C); } static void OnCwtd(struct As *a, struct Slice s) { EmitVarword(a, 0x6699); } static void OnCwtl(struct As *a, struct Slice s) { EmitByte(a, 0x98); } static void OnDec(struct As *a, struct Slice s) { OpBump(a, s, 1); } static void OnDiv(struct As *a, struct Slice s) { OpF6(a, s, 6); } static void OnDivpd(struct As *a, struct Slice s) { OpSse(a, 0x660F5E); } static void OnDivps(struct As *a, struct Slice s) { OpSse(a, 0x0F5E); } static void OnDivsd(struct As *a, struct Slice s) { OpSse(a, 0xF20F5E); } static void OnDivss(struct As *a, struct Slice s) { OpSse(a, 0xF30F5E); } static void OnDppd(struct As *a, struct Slice s) { OpSse(a, 0x660F3A41); } static void OnFabs(struct As *a, struct Slice s) { EmitVarword(a, 0xD9E1); } static void OnFaddl(struct As *a, struct Slice s) { OpFpu1(a, 0xDC, 0); } static void OnFaddp(struct As *a, struct Slice s) { EmitVarword(a, 0xDEC1); } static void OnFadds(struct As *a, struct Slice s) { OpFpu1(a, 0xD8, 0); } static void OnFchs(struct As *a, struct Slice s) { EmitVarword(a, 0xD9E0); } static void OnFcmovb(struct As *a, struct Slice s) { OpFcom(a, 0xDAC0); } static void OnFcmovbe(struct As *a, struct Slice s) { OpFcom(a, 0xDAD0); } static void OnFcmove(struct As *a, struct Slice s) { OpFcom(a, 0xDAC8); } static void OnFcmovnb(struct As *a, struct Slice s) { OpFcom(a, 0xDBC0); } static void OnFcmovnbe(struct As *a, struct Slice s) { OpFcom(a, 0xDBD0); } static void OnFcmovne(struct As *a, struct Slice s) { OpFcom(a, 0xDBC8); } static void OnFcmovnu(struct As *a, struct Slice s) { OpFcom(a, 0xDBD8); } static void OnFcmovu(struct As *a, struct Slice s) { OpFcom(a, 0xDAD8); } static void OnFcomi(struct As *a, struct Slice s) { OpFcom(a, 0xDBF0); } static void OnFcomip(struct As *a, struct Slice s) { OpFcom(a, 0xDFF0); } static void OnFdivrp(struct As *a, struct Slice s) { EmitVarword(a, 0xDEF9); } static void OnFildl(struct As *a, struct Slice s) { OpFpu1(a, 0xDB, 0); } static void OnFildll(struct As *a, struct Slice s) { OpFpu1(a, 0xDF, 5); } static void OnFildq(struct As *a, struct Slice s) { OpFpu1(a, 0xDF, 5); } static void OnFilds(struct As *a, struct Slice s) { OpFpu1(a, 0xDF, 0); } static void OnFistpq(struct As *a, struct Slice s) { OpFpu1(a, 0xDF, 7); } static void OnFisttpq(struct As *a, struct Slice s) { OpFpu1(a, 0xDD, 1); } static void OnFisttps(struct As *a, struct Slice s) { OpFpu1(a, 0xDF, 1); } static void OnFld(struct As *a, struct Slice s) { OpFpu1(a, 0xD9, 0); } static void OnFld1(struct As *a, struct Slice s) { EmitVarword(a, 0xd9e8); } static void OnFldcw(struct As *a, struct Slice s) { OpFpu1(a, 0xD9, 5); } static void OnFldl(struct As *a, struct Slice s) { OpFpu1(a, 0xDD, 0); } static void OnFldl2e(struct As *a, struct Slice s) { EmitVarword(a, 0xd9ea); } static void OnFldl2t(struct As *a, struct Slice s) { EmitVarword(a, 0xd9e9); } static void OnFldlg2(struct As *a, struct Slice s) { EmitVarword(a, 0xd9ec); } static void OnFldln2(struct As *a, struct Slice s) { EmitVarword(a, 0xd9ed); } static void OnFldpi(struct As *a, struct Slice s) { EmitVarword(a, 0xd9eb); } static void OnFlds(struct As *a, struct Slice s) { OpFpu1(a, 0xD9, 0); } static void OnFldt(struct As *a, struct Slice s) { OpFpu1(a, 0xDB, 5); } static void OnFldz(struct As *a, struct Slice s) { EmitVarword(a, 0xd9ee); } static void OnFmulp(struct As *a, struct Slice s) { EmitVarword(a, 0xdec9); } static void OnFnstcw(struct As *a, struct Slice s) { OpFpu1(a, 0xD9, 7); } static void OnFnstsw(struct As *a, struct Slice s) { OpFpu1(a, 0xDF, 4); } static void OnFstp(struct As *a, struct Slice s) { OpFpu1(a, 0xDD, 3); } static void OnFstpl(struct As *a, struct Slice s) { OpFpu1(a, 0xDD, 3); } static void OnFstps(struct As *a, struct Slice s) { OpFpu1(a, 0xD9, 3); } static void OnFstpt(struct As *a, struct Slice s) { OpFpu1(a, 0xDB, 7); } static void OnFsubrp(struct As *a, struct Slice s) { EmitVarword(a, 0xDEE9); } static void OnFtst(struct As *a, struct Slice s) { EmitVarword(a, 0xD9E4); } static void OnFucomi(struct As *a, struct Slice s) { OpFcom(a, 0xDBE8); } static void OnFucomip(struct As *a, struct Slice s) { OpFcom(a, 0xDFE8); } static void OnFwait(struct As *a, struct Slice s) { EmitByte(a, 0x9B); } static void OnFxam(struct As *a, struct Slice s) { EmitVarword(a, 0xD9E5); } static void OnFxtract(struct As *a, struct Slice s) { EmitVarword(a, 0xD9F4); } static void OnHaddpd(struct As *a, struct Slice s) { OpSse(a, 0x660F7C); } static void OnHaddps(struct As *a, struct Slice s) { OpSse(a, 0xF20F7C); } static void OnHlt(struct As *a, struct Slice s) { EmitByte(a, 0xF4); } static void OnHsubpd(struct As *a, struct Slice s) { OpSse(a, 0x660F7D); } static void OnHsubps(struct As *a, struct Slice s) { OpSse(a, 0xF20F7D); } static void OnIdiv(struct As *a, struct Slice s) { OpF6(a, s, 7); } static void OnInc(struct As *a, struct Slice s) { OpBump(a, s, 0); } static void OnInt1(struct As *a, struct Slice s) { EmitByte(a, 0xF1); } static void OnInt3(struct As *a, struct Slice s) { EmitByte(a, 0xCC); } static void OnJb(struct As *a, struct Slice s) { OpJmp(a, 2); } static void OnJbe(struct As *a, struct Slice s) { OpJmp(a, 6); } static void OnJl(struct As *a, struct Slice s) { OpJmp(a, 12); } static void OnJle(struct As *a, struct Slice s) { OpJmp(a, 14); } static void OnJmp(struct As *a, struct Slice s) { OpJmp(a, -1); } static void OnJnb(struct As *a, struct Slice s) { OpJmp(a, 3); } static void OnJnbe(struct As *a, struct Slice s) { OpJmp(a, 7); } static void OnJnl(struct As *a, struct Slice s) { OpJmp(a, 13); } static void OnJnle(struct As *a, struct Slice s) { OpJmp(a, 15); } static void OnJno(struct As *a, struct Slice s) { OpJmp(a, 1); } static void OnJnp(struct As *a, struct Slice s) { OpJmp(a, 11); } static void OnJns(struct As *a, struct Slice s) { OpJmp(a, 9); } static void OnJnz(struct As *a, struct Slice s) { OpJmp(a, 5); } static void OnJo(struct As *a, struct Slice s) { OpJmp(a, 0); } static void OnJp(struct As *a, struct Slice s) { OpJmp(a, 10); } static void OnJs(struct As *a, struct Slice s) { OpJmp(a, 8); } static void OnJz(struct As *a, struct Slice s) { OpJmp(a, 4); } static void OnLeave(struct As *a, struct Slice s) { EmitByte(a, 0xC9); } static void OnLodsb(struct As *a, struct Slice s) { EmitByte(a, 0xAC); } static void OnLodsl(struct As *a, struct Slice s) { EmitByte(a, 0xAD); } static void OnLodsq(struct As *a, struct Slice s) { EmitVarword(a, 0x48AD); } static void OnLodsw(struct As *a, struct Slice s) { EmitVarword(a, 0x66AD); } static void OnMaxpd(struct As *a, struct Slice s) { OpSse(a, 0x660F5F); } static void OnMaxps(struct As *a, struct Slice s) { OpSse(a, 0x0F5F); } static void OnMaxsd(struct As *a, struct Slice s) { OpSse(a, 0xF20F5F); } static void OnMaxss(struct As *a, struct Slice s) { OpSse(a, 0xF30F5F); } static void OnMfence(struct As *a, struct Slice s) { EmitVarword(a, 0x0faef0); } static void OnMinpd(struct As *a, struct Slice s) { OpSse(a, 0x660F5D); } static void OnMinps(struct As *a, struct Slice s) { OpSse(a, 0x0F5D); } static void OnMinsd(struct As *a, struct Slice s) { OpSse(a, 0xF20F5D); } static void OnMinss(struct As *a, struct Slice s) { OpSse(a, 0xF30F5D); } static void OnMovmskpd(struct As *a, struct Slice s) { OpSse(a, 0x660F50); } static void OnMovmskps(struct As *a, struct Slice s) { OpSse(a, 0x0F50); } static void OnMovntdq(struct As *a, struct Slice s) { OpMovntdq(a); } static void OnMovsb(struct As *a, struct Slice s) { EmitByte(a, 0xA4); } static void OnMovsl(struct As *a, struct Slice s) { EmitByte(a, 0xA5); } static void OnMovsq(struct As *a, struct Slice s) { EmitVarword(a, 0x48A5); } static void OnMovsw(struct As *a, struct Slice s) { EmitVarword(a, 0x66A5); } static void OnMpsadbw(struct As *a, struct Slice s) { OpSseIb(a, 0x660F3A42); } static void OnMul(struct As *a, struct Slice s) { OpF6(a, s, 4); } static void OnMulpd(struct As *a, struct Slice s) { OpSse(a, 0x660F59); } static void OnMulps(struct As *a, struct Slice s) { OpSse(a, 0x0F59); } static void OnMulsd(struct As *a, struct Slice s) { OpSse(a, 0xF20F59); } static void OnMulss(struct As *a, struct Slice s) { OpSse(a, 0xF30F59); } static void OnNeg(struct As *a, struct Slice s) { OpF6(a, s, 3); } static void OnNot(struct As *a, struct Slice s) { OpF6(a, s, 2); } static void OnOr(struct As *a, struct Slice s) { OpAlu(a, s, 1); } static void OnOrpd(struct As *a, struct Slice s) { OpSse(a, 0x660F56); } static void OnOrps(struct As *a, struct Slice s) { OpSse(a, 0x0F56); } static void OnPabsb(struct As *a, struct Slice s) { OpSse(a, 0x660F381C); } static void OnPabsd(struct As *a, struct Slice s) { OpSse(a, 0x660F381E); } static void OnPabsw(struct As *a, struct Slice s) { OpSse(a, 0x660F381D); } static void OnPackssdw(struct As *a, struct Slice s) { OpSse(a, 0x660F6B); } static void OnPacksswb(struct As *a, struct Slice s) { OpSse(a, 0x660F63); } static void OnPackusdw(struct As *a, struct Slice s) { OpSse(a, 0x660F382B); } static void OnPackuswb(struct As *a, struct Slice s) { OpSse(a, 0x660F67); } static void OnPaddb(struct As *a, struct Slice s) { OpSse(a, 0x660FFC); } static void OnPaddd(struct As *a, struct Slice s) { OpSse(a, 0x660FFE); } static void OnPaddq(struct As *a, struct Slice s) { OpSse(a, 0x660FD4); } static void OnPaddsb(struct As *a, struct Slice s) { OpSse(a, 0x660FEC); } static void OnPaddsw(struct As *a, struct Slice s) { OpSse(a, 0x660FED); } static void OnPaddusb(struct As *a, struct Slice s) { OpSse(a, 0x660FDC); } static void OnPaddusw(struct As *a, struct Slice s) { OpSse(a, 0x660FDD); } static void OnPaddw(struct As *a, struct Slice s) { OpSse(a, 0x660FFD); } static void OnPalignr(struct As *a, struct Slice s) { OpSse(a, 0x660F3A0F); } static void OnPand(struct As *a, struct Slice s) { OpSse(a, 0x660FDB); } static void OnPandn(struct As *a, struct Slice s) { OpSse(a, 0x660FDF); } static void OnPause(struct As *a, struct Slice s) { EmitVarword(a, 0xF390); } static void OnPavgb(struct As *a, struct Slice s) { OpSse(a, 0x660FE0); } static void OnPavgw(struct As *a, struct Slice s) { OpSse(a, 0x660FE3); } static void OnPblendvb(struct As *a, struct Slice s) { OpSse(a, 0x660F3810); } static void OnPblendw(struct As *a, struct Slice s) { OpSseIb(a, 0x660F3A0E); } static void OnPcmpeqb(struct As *a, struct Slice s) { OpSse(a, 0x660F74); } static void OnPcmpeqd(struct As *a, struct Slice s) { OpSse(a, 0x660F76); } static void OnPcmpeqq(struct As *a, struct Slice s) { OpSse(a, 0x660F3829); } static void OnPcmpeqw(struct As *a, struct Slice s) { OpSse(a, 0x660F75); } static void OnPcmpgtb(struct As *a, struct Slice s) { OpSse(a, 0x660F64); } static void OnPcmpgtd(struct As *a, struct Slice s) { OpSse(a, 0x660F66); } static void OnPcmpgtq(struct As *a, struct Slice s) { OpSse(a, 0x660F3837); } static void OnPcmpgtw(struct As *a, struct Slice s) { OpSse(a, 0x660F65); } static void OnPcmpistri(struct As *a, struct Slice s) { OpSseIb(a, 0x660F3A63); } static void OnPcmpistrm(struct As *a, struct Slice s) { OpSseIb(a, 0x660F3A62); } static void OnPhaddd(struct As *a, struct Slice s) { OpSse(a, 0x660F3802); } static void OnPhaddsw(struct As *a, struct Slice s) { OpSse(a, 0x660F3803); } static void OnPhaddw(struct As *a, struct Slice s) { OpSse(a, 0x660F3801); } static void OnPhminposuw(struct As *a, struct Slice s) { OpSse(a, 0x660F3841); } static void OnPhsubd(struct As *a, struct Slice s) { OpSse(a, 0x660F3806); } static void OnPhsubsw(struct As *a, struct Slice s) { OpSse(a, 0x660F3807); } static void OnPhsubw(struct As *a, struct Slice s) { OpSse(a, 0x660F3805); } static void OnPmaddubsw(struct As *a, struct Slice s) { OpSse(a, 0x660F3804); } static void OnPmaddwd(struct As *a, struct Slice s) { OpSse(a, 0x660FF5); } static void OnPmaxsb(struct As *a, struct Slice s) { OpSse(a, 0x660F383C); } static void OnPmaxsd(struct As *a, struct Slice s) { OpSse(a, 0x660F383D); } static void OnPmaxsw(struct As *a, struct Slice s) { OpSse(a, 0x660FEE); } static void OnPmaxub(struct As *a, struct Slice s) { OpSse(a, 0x660FDE); } static void OnPmaxud(struct As *a, struct Slice s) { OpSse(a, 0x660F383F); } static void OnPmaxuw(struct As *a, struct Slice s) { OpSse(a, 0x660F383E); } static void OnPminsb(struct As *a, struct Slice s) { OpSse(a, 0x660F3838); } static void OnPminsd(struct As *a, struct Slice s) { OpSse(a, 0x660F3839); } static void OnPminsw(struct As *a, struct Slice s) { OpSse(a, 0x660FEA); } static void OnPminub(struct As *a, struct Slice s) { OpSse(a, 0x660FDA); } static void OnPminud(struct As *a, struct Slice s) { OpSse(a, 0x660F383B); } static void OnPminuw(struct As *a, struct Slice s) { OpSse(a, 0x660F383A); } static void OnPmovmskb(struct As *a, struct Slice s) { OpSse(a, 0x660FD7); } static void OnPmuldq(struct As *a, struct Slice s) { OpSse(a, 0x660F3828); } static void OnPmulhrsw(struct As *a, struct Slice s) { OpSse(a, 0x660F380B); } static void OnPmulhuw(struct As *a, struct Slice s) { OpSse(a, 0x660FE4); } static void OnPmulhw(struct As *a, struct Slice s) { OpSse(a, 0x660FE5); } static void OnPmulld(struct As *a, struct Slice s) { OpSse(a, 0x660F3840); } static void OnPmullw(struct As *a, struct Slice s) { OpSse(a, 0x660FD5); } static void OnPmuludq(struct As *a, struct Slice s) { OpSse(a, 0x660FF4); } static void OnPopcnt(struct As *a, struct Slice s) { OpBit(a, 0xF30FB8); } static void OnPor(struct As *a, struct Slice s) { OpSse(a, 0x660FEB); } static void OnPsadbw(struct As *a, struct Slice s) { OpSse(a, 0x660FF6); } static void OnPshufb(struct As *a, struct Slice s) { OpSse(a, 0x660F3800); } static void OnPshufd(struct As *a, struct Slice s) { OpSseIb(a, 0x660F70); } static void OnPshufhw(struct As *a, struct Slice s) { OpSseIb(a, 0xF30F70); } static void OnPshuflw(struct As *a, struct Slice s) { OpSseIb(a, 0xF20F70); } static void OnPsignb(struct As *a, struct Slice s) { OpSse(a, 0x660F3808); } static void OnPsignd(struct As *a, struct Slice s) { OpSse(a, 0x660F380A); } static void OnPsignw(struct As *a, struct Slice s) { OpSse(a, 0x660F3809); } static void OnPslld(struct As *a, struct Slice s) { OpSse(a, 0x660FF2); } static void OnPsllq(struct As *a, struct Slice s) { OpSse(a, 0x660FF3); } static void OnPsllw(struct As *a, struct Slice s) { OpSse(a, 0x660FF1); } static void OnPsrad(struct As *a, struct Slice s) { OpSse(a, 0x660FE2); } static void OnPsraw(struct As *a, struct Slice s) { OpSse(a, 0x660FE1); } static void OnPsrld(struct As *a, struct Slice s) { OpSse(a, 0x660FD2); } static void OnPsrlq(struct As *a, struct Slice s) { OpSse(a, 0x660FD3); } static void OnPsrlw(struct As *a, struct Slice s) { OpSse(a, 0x660FD1); } static void OnPsubb(struct As *a, struct Slice s) { OpSse(a, 0x660FF8); } static void OnPsubd(struct As *a, struct Slice s) { OpSse(a, 0x660FFA); } static void OnPsubq(struct As *a, struct Slice s) { OpSse(a, 0x660FFB); } static void OnPsubsb(struct As *a, struct Slice s) { OpSse(a, 0x660FE8); } static void OnPsubsw(struct As *a, struct Slice s) { OpSse(a, 0x660FE9); } static void OnPsubusb(struct As *a, struct Slice s) { OpSse(a, 0x660FD8); } static void OnPsubusw(struct As *a, struct Slice s) { OpSse(a, 0x660FD9); } static void OnPsubw(struct As *a, struct Slice s) { OpSse(a, 0x660FF9); } static void OnPtest(struct As *a, struct Slice s) { OpSse(a, 0x660F3817); } static void OnPunpckhbw(struct As *a, struct Slice s) { OpSse(a, 0x660F68); } static void OnPunpckhdq(struct As *a, struct Slice s) { OpSse(a, 0x660F6A); } static void OnPunpckhqdq(struct As *a, struct Slice s) { OpSse(a, 0x660F6D); } static void OnPunpckhwd(struct As *a, struct Slice s) { OpSse(a, 0x660F69); } static void OnPunpcklbw(struct As *a, struct Slice s) { OpSse(a, 0x660F60); } static void OnPunpckldq(struct As *a, struct Slice s) { OpSse(a, 0x660F62); } static void OnPunpcklqdq(struct As *a, struct Slice s) { OpSse(a, 0x660F6C); } static void OnPunpcklwd(struct As *a, struct Slice s) { OpSse(a, 0x660F61); } static void OnPxor(struct As *a, struct Slice s) { OpSse(a, 0x660FEF); } static void OnRcl(struct As *a, struct Slice s) { OpBsu(a, s, 2); } static void OnRcpps(struct As *a, struct Slice s) { OpSse(a, 0x0F53); } static void OnRcpss(struct As *a, struct Slice s) { OpSse(a, 0xF30F53); } static void OnRcr(struct As *a, struct Slice s) { OpBsu(a, s, 3); } static void OnRdtsc(struct As *a, struct Slice s) { EmitVarword(a, 0x0f31); } static void OnRdtscp(struct As *a, struct Slice s) { EmitVarword(a, 0x0f01f9); } static void OnRol(struct As *a, struct Slice s) { OpBsu(a, s, 0); } static void OnRor(struct As *a, struct Slice s) { OpBsu(a, s, 1); } static void OnRoundsd(struct As *a, struct Slice s) { OpSseIb(a, 0x660F3A0B); } static void OnRoundss(struct As *a, struct Slice s) { OpSseIb(a, 0x660F3A0A); } static void OnRsqrtps(struct As *a, struct Slice s) { OpSse(a, 0x0F52); } static void OnRsqrtss(struct As *a, struct Slice s) { OpSse(a, 0xF30F52); } static void OnSal(struct As *a, struct Slice s) { OpBsu(a, s, 6); } static void OnSar(struct As *a, struct Slice s) { OpBsu(a, s, 7); } static void OnSbb(struct As *a, struct Slice s) { OpAlu(a, s, 3); } static void OnSetb(struct As *a, struct Slice s) { OpSetcc(a, 2); } static void OnSetbe(struct As *a, struct Slice s) { OpSetcc(a, 6); } static void OnSetl(struct As *a, struct Slice s) { OpSetcc(a, 12); } static void OnSetle(struct As *a, struct Slice s) { OpSetcc(a, 14); } static void OnSetnb(struct As *a, struct Slice s) { OpSetcc(a, 3); } static void OnSetnbe(struct As *a, struct Slice s) { OpSetcc(a, 7); } static void OnSetnl(struct As *a, struct Slice s) { OpSetcc(a, 13); } static void OnSetnle(struct As *a, struct Slice s) { OpSetcc(a, 15); } static void OnSetno(struct As *a, struct Slice s) { OpSetcc(a, 1); } static void OnSetnp(struct As *a, struct Slice s) { OpSetcc(a, 11); } static void OnSetns(struct As *a, struct Slice s) { OpSetcc(a, 9); } static void OnSetnz(struct As *a, struct Slice s) { OpSetcc(a, 5); } static void OnSeto(struct As *a, struct Slice s) { OpSetcc(a, 0); } static void OnSetp(struct As *a, struct Slice s) { OpSetcc(a, 10); } static void OnSets(struct As *a, struct Slice s) { OpSetcc(a, 8); } static void OnSetz(struct As *a, struct Slice s) { OpSetcc(a, 4); } static void OnSfence(struct As *a, struct Slice s) { EmitVarword(a, 0x0faef8); } static void OnShl(struct As *a, struct Slice s) { OpBsu(a, s, 4); } static void OnShr(struct As *a, struct Slice s) { OpBsu(a, s, 5); } static void OnShufpd(struct As *a, struct Slice s) { OpSseIb(a, 0x660FC6); } static void OnShufps(struct As *a, struct Slice s) { OpSseIb(a, 0x0FC6); } static void OnSqrtpd(struct As *a, struct Slice s) { OpSse(a, 0x660F51); } static void OnSqrtps(struct As *a, struct Slice s) { OpSse(a, 0x0F51); } static void OnSqrtsd(struct As *a, struct Slice s) { OpSse(a, 0xF20F51); } static void OnSqrtss(struct As *a, struct Slice s) { OpSse(a, 0xF30F51); } static void OnStc(struct As *a, struct Slice s) { EmitByte(a, 0xF9); } static void OnStd(struct As *a, struct Slice s) { EmitByte(a, 0xFD); } static void OnSti(struct As *a, struct Slice s) { EmitByte(a, 0xFB); } static void OnStosb(struct As *a, struct Slice s) { EmitByte(a, 0xAA); } static void OnStosl(struct As *a, struct Slice s) { EmitByte(a, 0xAB); } static void OnStosq(struct As *a, struct Slice s) { EmitVarword(a, 0x48AB); } static void OnStosw(struct As *a, struct Slice s) { EmitVarword(a, 0x66AB); } static void OnSub(struct As *a, struct Slice s) { OpAlu(a, s, 5); } static void OnSubpd(struct As *a, struct Slice s) { OpSse(a, 0x660F5C); } static void OnSubps(struct As *a, struct Slice s) { OpSse(a, 0x0F5C); } static void OnSubsd(struct As *a, struct Slice s) { OpSse(a, 0xF20F5C); } static void OnSubss(struct As *a, struct Slice s) { OpSse(a, 0xF30F5C); } static void OnSyscall(struct As *a, struct Slice s) { EmitVarword(a, 0x0F05); } static void OnUcomisd(struct As *a, struct Slice s) { OpSse(a, 0x660F2E); } static void OnUcomiss(struct As *a, struct Slice s) { OpSse(a, 0x0F2E); } static void OnUd2(struct As *a, struct Slice s) { EmitVarword(a, 0x0F0B); } static void OnUnpckhpd(struct As *a, struct Slice s) { OpSse(a, 0x660F15); } static void OnUnpcklpd(struct As *a, struct Slice s) { OpSse(a, 0x660F14); } static void OnXadd(struct As *a, struct Slice s) { OpXadd(a); } static void OnXor(struct As *a, struct Slice s) { OpAlu(a, s, 6); } static void OnXorpd(struct As *a, struct Slice s) { OpSse(a, 0x660F57); } static void OnXorps(struct As *a, struct Slice s) { OpSse(a, 0x0F57); } // clang-format on static const struct Directive8 { char s[8]; void (*f)(struct As *, struct Slice); } kDirective8[] = { {".abort", OnAbort}, // {".align", OnAlign}, // {".ascii", OnAscii}, // {".asciz", OnAsciz}, // {".balign", OnAlign}, // {".bss", OnBss}, // {".byte", OnByte}, // {".comm", OnComm}, // {".data", OnData}, // {".double", OnDouble}, // {".equ", OnEqu}, // {".err", OnErr}, // {".error", OnError}, // {".file", OnFile}, // {".float", OnFloat}, // {".float80", OnFloat80}, // {".global", OnGlobal}, // {".globl", OnGlobal}, // {".hidden", OnHidden}, // {".ident", OnIdent}, // {".incbin", OnIncbin}, // {".ldbl", OnLdbl}, // {".loc", OnLoc}, // {".local", OnLocal}, // {".long", OnLong}, // {".octa", OnOcta}, // {".quad", OnQuad}, // {".section", OnSection}, // {".short", OnWord}, // {".size", OnSize}, // {".sleb128", OnSleb128}, // {".space", OnSpace}, // {".text", OnText}, // {".type", OnType}, // {".uleb128", OnUleb128}, // {".warning", OnWarning}, // {".weak", OnWeak}, // {".word", OnWord}, // {".zero", OnZero}, // {".zleb128", OnZleb128}, // {"adc", OnAdc}, // {"adcb", OnAdc}, // {"adcl", OnAdc}, // {"adcq", OnAdc}, // {"adcw", OnAdc}, // {"add", OnAdd}, // {"addb", OnAdd}, // {"addl", OnAdd}, // {"addpd", OnAddpd}, // {"addps", OnAddps}, // {"addq", OnAdd}, // {"addsd", OnAddsd}, // {"addss", OnAddss}, // {"addsubpd", OnAddsubpd}, // {"addsubps", OnAddsubps}, // {"addw", OnAdd}, // {"and", OnAnd}, // {"andb", OnAnd}, // {"andl", OnAnd}, // {"andnpd", OnAndnpd}, // {"andnps", OnAndnps}, // {"andpd", OnAndpd}, // {"andps", OnAndps}, // {"andq", OnAnd}, // {"andw", OnAnd}, // {"blendpd", OnBlendpd}, // {"blendvpd", OnBlendvpd}, // {"bsf", OnBsf}, // {"bsr", OnBsr}, // {"bswap", OnBswap}, // {"call", OnCall}, // {"callq", OnCall}, // {"cbtw", OnCbtw}, // {"cbw", OnCbtw}, // {"cdq", OnCltd}, // {"cdqe", OnCltq}, // {"clc", OnClc}, // {"cld", OnCld}, // {"cli", OnCli}, // {"cltd", OnCltd}, // {"cltq", OnCltq}, // {"cmc", OnCmc}, // {"cmova", OnCmovnbe}, // {"cmovae", OnCmovnb}, // {"cmovb", OnCmovb}, // {"cmovbe", OnCmovbe}, // {"cmovc", OnCmovb}, // {"cmove", OnCmovz}, // {"cmovg", OnCmovnle}, // {"cmovge", OnCmovnl}, // {"cmovl", OnCmovl}, // {"cmovle", OnCmovle}, // {"cmovna", OnCmovbe}, // {"cmovnae", OnCmovb}, // {"cmovnb", OnCmovnb}, // {"cmovnbe", OnCmovnbe}, // {"cmovnc", OnCmovnb}, // {"cmovne", OnCmovnz}, // {"cmovng", OnCmovle}, // {"cmovnge", OnCmovl}, // {"cmovnl", OnCmovnl}, // {"cmovnle", OnCmovnle}, // {"cmovno", OnCmovno}, // {"cmovnp", OnCmovnp}, // {"cmovns", OnCmovns}, // {"cmovnz", OnCmovnz}, // {"cmovo", OnCmovo}, // {"cmovp", OnCmovp}, // {"cmovpe", OnCmovp}, // {"cmovpo", OnCmovnp}, // {"cmovs", OnCmovs}, // {"cmovz", OnCmovz}, // {"cmp", OnCmp}, // {"cmpb", OnCmp}, // {"cmpl", OnCmp}, // {"cmppd", OnCmppd}, // {"cmpps", OnCmpps}, // {"cmpq", OnCmp}, // {"cmpsd", OnCmpsd}, // {"cmpss", OnCmpss}, // {"cmpw", OnCmp}, // {"cmpxchg", OnCmpxchg}, // {"comisd", OnComisd}, // {"comiss", OnComiss}, // {"cqo", OnCqto}, // {"cqto", OnCqto}, // {"cvtdq2pd", OnCvtdq2pd}, // {"cvtdq2ps", OnCvtdq2ps}, // {"cvtpd2dq", OnCvtpd2dq}, // {"cvtpd2ps", OnCvtpd2ps}, // {"cvtps2dq", OnCvtps2dq}, // {"cvtps2pd", OnCvtps2pd}, // {"cvtsd2ss", OnCvtsd2ss}, // {"cvtsd2ss", OnCvtsd2ss}, // {"cvtsi2sd", OnCvtsi2sd}, // {"cvtsi2sd", OnCvtsi2sd}, // {"cvtsi2ss", OnCvtsi2ss}, // {"cvtsi2ss", OnCvtsi2ss}, // {"cvtss2sd", OnCvtss2sd}, // {"cwd", OnCwtd}, // {"cwde", OnCwtl}, // {"cwtd", OnCwtd}, // {"cwtl", OnCwtl}, // {"dec", OnDec}, // {"decb", OnDec}, // {"decl", OnDec}, // {"decq", OnDec}, // {"decw", OnDec}, // {"div", OnDiv}, // {"divpd", OnDivpd}, // {"divps", OnDivps}, // {"divsd", OnDivsd}, // {"divss", OnDivss}, // {"dppd", OnDppd}, // {"fabs", OnFabs}, // {"faddl", OnFaddl}, // {"faddp", OnFaddp}, // {"fadds", OnFadds}, // {"fchs", OnFchs}, // {"fcmovb", OnFcmovb}, // {"fcmovbe", OnFcmovbe}, // {"fcmove", OnFcmove}, // {"fcmovnb", OnFcmovnb}, // {"fcmovnbe", OnFcmovnbe}, // {"fcmovne", OnFcmovne}, // {"fcmovnu", OnFcmovnu}, // {"fcmovu", OnFcmovu}, // {"fcomi", OnFcomi}, // {"fcomip", OnFcomip}, // {"fdivrp", OnFdivrp}, // {"fildl", OnFildl}, // {"fildll", OnFildll}, // {"fildq", OnFildq}, // {"filds", OnFilds}, // {"fistpll", OnFistpq}, // {"fistpq", OnFistpq}, // {"fisttpll", OnFisttpq}, // {"fisttpq", OnFisttpq}, // {"fisttps", OnFisttps}, // {"fld", OnFld}, // {"fld1", OnFld1}, // {"fldcw", OnFldcw}, // {"fldl", OnFldl}, // {"fldl2e", OnFldl2e}, // {"fldl2t", OnFldl2t}, // {"fldlg2", OnFldlg2}, // {"fldln2", OnFldln2}, // {"fldpi", OnFldpi}, // {"flds", OnFlds}, // {"fldt", OnFldt}, // {"fldz", OnFldz}, // {"fmulp", OnFmulp}, // {"fnstcw", OnFnstcw}, // {"fnstsw", OnFnstsw}, // {"fstp", OnFstp}, // {"fstpl", OnFstpl}, // {"fstps", OnFstps}, // {"fstpt", OnFstpt}, // {"fsubrp", OnFsubrp}, // {"ftst", OnFtst}, // {"fucomi", OnFucomi}, // {"fucomip", OnFucomip}, // {"fwait", OnFwait}, // {"fxam", OnFxam}, // {"fxch", OnFxch}, // {"fxtract", OnFxtract}, // {"haddpd", OnHaddpd}, // {"haddps", OnHaddps}, // {"hlt", OnHlt}, // {"hsubpd", OnHsubpd}, // {"hsubps", OnHsubps}, // {"icebp", OnInt1}, // {"idiv", OnIdiv}, // {"imul", OnImul}, // {"inc", OnInc}, // {"incb", OnInc}, // {"incl", OnInc}, // {"incq", OnInc}, // {"incw", OnInc}, // {"int1", OnInt1}, // {"int3", OnInt3}, // {"ja", OnJnbe}, // {"jae", OnJnb}, // {"jb", OnJb}, // {"jbe", OnJbe}, // {"jc", OnJb}, // {"je", OnJz}, // {"jg", OnJnle}, // {"jge", OnJnl}, // {"jl", OnJl}, // {"jle", OnJle}, // {"jmp", OnJmp}, // {"jmpq", OnJmp}, // {"jna", OnJbe}, // {"jnae", OnJb}, // {"jnb", OnJnb}, // {"jnbe", OnJnbe}, // {"jnc", OnJnb}, // {"jne", OnJnz}, // {"jng", OnJle}, // {"jnge", OnJl}, // {"jnl", OnJnl}, // {"jnle", OnJnle}, // {"jno", OnJno}, // {"jnp", OnJnp}, // {"jns", OnJns}, // {"jnz", OnJnz}, // {"jo", OnJo}, // {"jp", OnJp}, // {"jpe", OnJp}, // {"jpo", OnJnp}, // {"js", OnJs}, // {"jz", OnJz}, // {"lar", OnLar}, // {"lea", OnLea}, // {"leave", OnLeave}, // {"lodsb", OnLodsb}, // {"lodsl", OnLodsl}, // {"lodsq", OnLodsq}, // {"lodsw", OnLodsw}, // {"lsl", OnLsl}, // {"maxpd", OnMaxpd}, // {"maxps", OnMaxps}, // {"maxsd", OnMaxsd}, // {"maxss", OnMaxss}, // {"mfence", OnMfence}, // {"minpd", OnMinpd}, // {"minps", OnMinps}, // {"minsd", OnMinsd}, // {"minss", OnMinss}, // {"mov", OnMov}, // {"movabs", OnMov}, // {"movapd", OnMovapd}, // {"movaps", OnMovaps}, // {"movb", OnMov}, // {"movd", OnMovd}, // {"movdqa", OnMovdqa}, // {"movdqa", OnMovdqa}, // {"movdqu", OnMovdqu}, // {"movdqu", OnMovdqu}, // {"movl", OnMov}, // {"movmskpd", OnMovmskpd}, // {"movmskps", OnMovmskps}, // {"movq", OnMovq}, // {"movsb", OnMovsb}, // {"movsbl", OnMovsbwx}, // {"movsbq", OnMovsbwx}, // {"movsbw", OnMovsbwx}, // {"movsd", OnMovsd}, // {"movsd", OnMovsd}, // {"movsl", OnMovsl}, // {"movslq", OnMovslq}, // {"movsq", OnMovsq}, // {"movss", OnMovss}, // {"movss", OnMovss}, // {"movsw", OnMovsw}, // {"movswl", OnMovsbwx}, // {"movswq", OnMovsbwx}, // {"movupd", OnMovupd}, // {"movups", OnMovups}, // {"movw", OnMov}, // {"movzbl", OnMovzbwx}, // {"movzbq", OnMovzbwx}, // {"movzbw", OnMovzbwx}, // {"movzwl", OnMovzbwx}, // {"movzwq", OnMovzbwx}, // {"mpsadbw", OnMpsadbw}, // {"mul", OnMul}, // {"mulpd", OnMulpd}, // {"mulps", OnMulps}, // {"mulsd", OnMulsd}, // {"mulss", OnMulss}, // {"neg", OnNeg}, // {"negb", OnNeg}, // {"negl", OnNeg}, // {"negq", OnNeg}, // {"negw", OnNeg}, // {"nop", OnNop}, // {"nopb", OnNop}, // {"nopl", OnNop}, // {"nopq", OnNop}, // {"nopw", OnNop}, // {"not", OnNot}, // {"notb", OnNot}, // {"notl", OnNot}, // {"notq", OnNot}, // {"notw", OnNot}, // {"or", OnOr}, // {"orb", OnOr}, // {"orl", OnOr}, // {"orpd", OnOrpd}, // {"orps", OnOrps}, // {"orq", OnOr}, // {"orw", OnOr}, // {"pabsb", OnPabsb}, // {"pabsd", OnPabsd}, // {"pabsw", OnPabsw}, // {"packssdw", OnPackssdw}, // {"packsswb", OnPacksswb}, // {"packusdw", OnPackusdw}, // {"packuswb", OnPackuswb}, // {"paddb", OnPaddb}, // {"paddd", OnPaddd}, // {"paddq", OnPaddq}, // {"paddsb", OnPaddsb}, // {"paddsw", OnPaddsw}, // {"paddusb", OnPaddusb}, // {"paddusw", OnPaddusw}, // {"paddw", OnPaddw}, // {"palignr", OnPalignr}, // {"pand", OnPand}, // {"pandn", OnPandn}, // {"pause", OnPause}, // {"pavgb", OnPavgb}, // {"pavgw", OnPavgw}, // {"pblendvb", OnPblendvb}, // {"pblendw", OnPblendw}, // {"pcmpeqb", OnPcmpeqb}, // {"pcmpeqd", OnPcmpeqd}, // {"pcmpeqq", OnPcmpeqq}, // {"pcmpeqw", OnPcmpeqw}, // {"pcmpgtb", OnPcmpgtb}, // {"pcmpgtd", OnPcmpgtd}, // {"pcmpgtq", OnPcmpgtq}, // {"pcmpgtw", OnPcmpgtw}, // {"phaddd", OnPhaddd}, // {"phaddsw", OnPhaddsw}, // {"phaddw", OnPhaddw}, // {"phsubd", OnPhsubd}, // {"phsubsw", OnPhsubsw}, // {"phsubw", OnPhsubw}, // {"pmaddwd", OnPmaddwd}, // {"pmaxsb", OnPmaxsb}, // {"pmaxsd", OnPmaxsd}, // {"pmaxsw", OnPmaxsw}, // {"pmaxub", OnPmaxub}, // {"pmaxud", OnPmaxud}, // {"pmaxuw", OnPmaxuw}, // {"pminsb", OnPminsb}, // {"pminsd", OnPminsd}, // {"pminsw", OnPminsw}, // {"pminub", OnPminub}, // {"pminud", OnPminud}, // {"pminuw", OnPminuw}, // {"pmovmskb", OnPmovmskb}, // {"pmuldq", OnPmuldq}, // {"pmulhrsw", OnPmulhrsw}, // {"pmulhuw", OnPmulhuw}, // {"pmulhw", OnPmulhw}, // {"pmulld", OnPmulld}, // {"pmullw", OnPmullw}, // {"pmuludq", OnPmuludq}, // {"pop", OnPop}, // {"popcnt", OnPopcnt}, // {"por", OnPor}, // {"psadbw", OnPsadbw}, // {"pshufb", OnPshufb}, // {"pshufd", OnPshufd}, // {"pshufhw", OnPshufhw}, // {"pshuflw", OnPshuflw}, // {"psignb", OnPsignb}, // {"psignd", OnPsignd}, // {"psignw", OnPsignw}, // {"pslld", OnPslld}, // {"psllq", OnPsllq}, // {"psllw", OnPsllw}, // {"psrad", OnPsrad}, // {"psraw", OnPsraw}, // {"psrld", OnPsrld}, // {"psrlq", OnPsrlq}, // {"psrlw", OnPsrlw}, // {"psubb", OnPsubb}, // {"psubd", OnPsubd}, // {"psubq", OnPsubq}, // {"psubsb", OnPsubsb}, // {"psubsw", OnPsubsw}, // {"psubusb", OnPsubusb}, // {"psubusw", OnPsubusw}, // {"psubw", OnPsubw}, // {"ptest", OnPtest}, // {"push", OnPush}, // {"pxor", OnPxor}, // {"rcl", OnRcl}, // {"rclb", OnRcl}, // {"rcll", OnRcl}, // {"rclq", OnRcl}, // {"rclw", OnRcl}, // {"rcpps", OnRcpps}, // {"rcpss", OnRcpss}, // {"rcr", OnRcr}, // {"rcrb", OnRcr}, // {"rcrl", OnRcr}, // {"rcrq", OnRcr}, // {"rcrw", OnRcr}, // {"rdpid", OnRdpid}, // {"rdtsc", OnRdtsc}, // {"rdtscp", OnRdtscp}, // {"ret", OnRet}, // {"rol", OnRol}, // {"rolb", OnRol}, // {"roll", OnRol}, // {"rolq", OnRol}, // {"rolw", OnRol}, // {"ror", OnRor}, // {"rorb", OnRor}, // {"rorl", OnRor}, // {"rorq", OnRor}, // {"rorw", OnRor}, // {"roundsd", OnRoundsd}, // {"roundss", OnRoundss}, // {"rsqrtps", OnRsqrtps}, // {"rsqrtss", OnRsqrtss}, // {"sal", OnSal}, // {"salb", OnSal}, // {"sall", OnSal}, // {"salq", OnSal}, // {"salw", OnSal}, // {"sar", OnSar}, // {"sarb", OnSar}, // {"sarl", OnSar}, // {"sarq", OnSar}, // {"sarw", OnSar}, // {"sbb", OnSbb}, // {"sbbb", OnSbb}, // {"sbbl", OnSbb}, // {"sbbq", OnSbb}, // {"sbbw", OnSbb}, // {"seta", OnSetnbe}, // {"setae", OnSetnb}, // {"setb", OnSetb}, // {"setbe", OnSetbe}, // {"setc", OnSetb}, // {"sete", OnSetz}, // {"setg", OnSetnle}, // {"setge", OnSetnl}, // {"setl", OnSetl}, // {"setle", OnSetle}, // {"setna", OnSetbe}, // {"setnae", OnSetb}, // {"setnb", OnSetnb}, // {"setnbe", OnSetnbe}, // {"setnc", OnSetnb}, // {"setne", OnSetnz}, // {"setng", OnSetle}, // {"setnge", OnSetl}, // {"setnl", OnSetnl}, // {"setnle", OnSetnle}, // {"setno", OnSetno}, // {"setnp", OnSetnp}, // {"setns", OnSetns}, // {"setnz", OnSetnz}, // {"seto", OnSeto}, // {"setp", OnSetp}, // {"setpe", OnSetp}, // {"setpo", OnSetnp}, // {"sets", OnSets}, // {"setz", OnSetz}, // {"sfence", OnSfence}, // {"shl", OnShl}, // {"shlb", OnShl}, // {"shld", OnShld}, // {"shll", OnShl}, // {"shlq", OnShl}, // {"shlw", OnShl}, // {"shr", OnShr}, // {"shrb", OnShr}, // {"shrd", OnShrd}, // {"shrl", OnShr}, // {"shrq", OnShr}, // {"shrw", OnShr}, // {"shufpd", OnShufpd}, // {"shufps", OnShufps}, // {"sqrtpd", OnSqrtpd}, // {"sqrtps", OnSqrtps}, // {"sqrtsd", OnSqrtsd}, // {"sqrtss", OnSqrtss}, // {"stc", OnStc}, // {"std", OnStd}, // {"sti", OnSti}, // {"stosb", OnStosb}, // {"stosl", OnStosl}, // {"stosq", OnStosq}, // {"stosw", OnStosw}, // {"sub", OnSub}, // {"subb", OnSub}, // {"subl", OnSub}, // {"subpd", OnSubpd}, // {"subps", OnSubps}, // {"subq", OnSub}, // {"subsd", OnSubsd}, // {"subss", OnSubss}, // {"subw", OnSub}, // {"syscall", OnSyscall}, // {"test", OnTest}, // {"testb", OnTest}, // {"testl", OnTest}, // {"testq", OnTest}, // {"testw", OnTest}, // {"ucomisd", OnUcomisd}, // {"ucomiss", OnUcomiss}, // {"ud2", OnUd2}, // {"unpckhpd", OnUnpckhpd}, // {"unpcklpd", OnUnpcklpd}, // {"wait", OnFwait}, // {"xadd", OnXadd}, // {"xchg", OnXchg}, // {"xor", OnXor}, // {"xorb", OnXor}, // {"xorl", OnXor}, // {"xorpd", OnXorpd}, // {"xorps", OnXorps}, // {"xorq", OnXor}, // {"xorw", OnXor}, // }; static const struct Directive16 { char s[16]; void (*f)(struct As *, struct Slice); } kDirective16[] = { {".internal", OnInternal}, // {".popsection", OnPopsection}, // {".previous", OnPrevious}, // {".protected", OnProtected}, // {".pushsection", OnPushsection}, // {"cvtsd2ssl", OnCvtsd2ss}, // {"cvtsd2ssq", OnCvtsd2ss}, // {"cvtsi2sdl", OnCvtsi2sd}, // {"cvtsi2sdq", OnCvtsi2sd}, // {"cvtsi2sdq", OnCvtsi2sd}, // {"cvtsi2ssl", OnCvtsi2ss}, // {"cvtsi2ssq", OnCvtsi2ss}, // {"cvttpd2dq", OnCvttpd2dq}, // {"cvttps2dq", OnCvttps2dq}, // {"cvttsd2si", OnCvttsd2si}, // {"cvttsd2sil", OnCvttsd2si}, // {"cvttsd2siq", OnCvttsd2si}, // {"cvttss2si", OnCvttss2si}, // {"cvttss2sil", OnCvttss2si}, // {"cvttss2siq", OnCvttss2si}, // {"movntdq", OnMovntdq}, // {"pcmpistri", OnPcmpistri}, // {"pcmpistrm", OnPcmpistrm}, // {"phminposuw", OnPhminposuw}, // {"pmaddubsw", OnPmaddubsw}, // {"punpckhbw", OnPunpckhbw}, // {"punpckhdq", OnPunpckhdq}, // {"punpckhqdq", OnPunpckhqdq}, // {"punpckhwd", OnPunpckhwd}, // {"punpcklbw", OnPunpcklbw}, // {"punpckldq", OnPunpckldq}, // {"punpcklqdq", OnPunpcklqdq}, // {"punpcklwd", OnPunpcklwd}, // }; static bool OnDirective8(struct As *a, struct Slice s) { int m, l, r; uint64_t x, y; if (s.n && s.n <= 8) { x = MakeKey64(s.p, s.n); l = 0; r = ARRAYLEN(kDirective8) - 1; while (l <= r) { m = (l + r) >> 1; y = READ64BE(kDirective8[m].s); if (x < y) { r = m - 1; } else if (x > y) { l = m + 1; } else { kDirective8[m].f(a, s); return true; } } } return false; } static bool OnDirective16(struct As *a, struct Slice s) { int m, l, r; unsigned __int128 x, y; if (s.n && s.n <= 16) { x = MakeKey128(s.p, s.n); l = 0; r = ARRAYLEN(kDirective16) - 1; while (l <= r) { m = (l + r) >> 1; y = READ128BE(kDirective16[m].s); if (x < y) { r = m - 1; } else if (x > y) { l = m + 1; } else { kDirective16[m].f(a, s); return true; } } } return false; } static void OnDirective(struct As *a) { struct Slice s; for (;;) { s = GetSlice(a); if (Prefix(a, s.p, s.n)) { if (IsSemicolon(a)) { return; } } else { break; } } if (!OnDirective8(a, s)) { if (!OnDirective16(a, s)) { Fail(a, "unexpected op: %.*s", s.n, s.p); } } ConsumePunct(a, ';'); } static void Assemble(struct As *a) { while (a->i < a->things.n) { if (IsSemicolon(a)) { ++a->i; continue; } switch (a->things.p[a->i].t) { case TT_SLICE: if (IsPunct(a, a->i + 1, ':')) { OnSymbol(a, a->things.p[a->i].i); } else { OnDirective(a); } break; case TT_INT: if (IsPunct(a, a->i + 1, ':')) { OnLocalLabel(a, a->ints.p[a->things.p[a->i].i]); break; } Fail(a, "unexpected token"); default: Fail(a, "unexpected token"); } } } static int FindLabelForward(struct As *a, int id) { int i; for (i = 0; i < a->labels.n; ++i) { if (a->labels.p[i].id == id && a->labels.p[i].tok > a->i) { return a->labels.p[i].symbol; } } Fail(a, "label not found"); } static int FindLabelBackward(struct As *a, int id) { int i; for (i = a->labels.n; i--;) { if (a->labels.p[i].id == id && a->labels.p[i].tok < a->i) { return a->labels.p[i].symbol; } } Fail(a, "label not found"); } static int ResolveSymbol(struct As *a, int i) { switch (a->things.p[i].t) { case TT_SLICE: return GetSymbol(a, a->things.p[i].i); case TT_BACKWARD: return FindLabelBackward(a, a->ints.p[a->things.p[i].i]); case TT_FORWARD: return FindLabelForward(a, a->ints.p[a->things.p[i].i]); default: Fail(a, "this corruption %d", a->things.p[i].t); } } static void EvaluateExpr(struct As *a, int i) { if (i == -1) return; if (a->exprs.p[i].isevaluated) return; if (a->exprs.p[i].isvisited) Fail(a, "circular expr"); a->exprs.p[i].isvisited = true; EvaluateExpr(a, a->exprs.p[i].lhs); EvaluateExpr(a, a->exprs.p[i].rhs); a->i = a->exprs.p[i].tok; switch (a->exprs.p[i].kind) { case EX_INT: break; case EX_SYM: a->exprs.p[i].x = ResolveSymbol(a, a->exprs.p[i].x); break; default: break; } a->exprs.p[i].isevaluated = true; } static void Write32(char b[4], int x) { b[0] = x >> 000; b[1] = x >> 010; b[2] = x >> 020; b[3] = x >> 030; } static void MarkUsedSymbols(struct As *a, int i) { if (i == -1) return; MarkUsedSymbols(a, a->exprs.p[i].lhs); MarkUsedSymbols(a, a->exprs.p[i].rhs); if (a->exprs.p[i].kind == EX_SYM) { a->symbols.p[a->exprs.p[i].x].isused = true; } } static void Evaluate(struct As *a) { int i; struct Expr *e; for (i = 0; i < a->relas.n; ++i) { EvaluateExpr(a, a->relas.p[i].expr); if (a->relas.p[i].kind == R_X86_64_PC32) { e = a->exprs.p + a->relas.p[i].expr; if (e->kind == EX_SYM && a->symbols.p[e->x].stb == STB_LOCAL && a->symbols.p[e->x].section == a->relas.p[i].section) { a->relas.p[i].isdead = true; Write32((a->sections.p[a->relas.p[i].section].binary.p + a->relas.p[i].offset), (a->symbols.p[e->x].offset - a->relas.p[i].offset + a->relas.p[i].addend)); } } } for (i = 0; i < a->relas.n; ++i) { if (a->relas.p[i].isdead) continue; MarkUsedSymbols(a, a->relas.p[i].expr); } } static void MarkUndefinedSymbolsGlobal(struct As *a) { int i; for (i = 0; i < a->symbols.n; ++i) { if (a->symbols.p[i].isused && !a->symbols.p[i].section && a->symbols.p[i].stb == STB_LOCAL) { a->symbols.p[i].stb = STB_GLOBAL; } } } static bool IsLocal(struct As *a, int name) { if (name < 0) return true; return a->slices.p[name].n >= 2 && !memcmp(a->slices.p[name].p, ".L", 2); } static bool IsLiveSymbol(struct As *a, int i) { return !(!a->symbols.p[i].isused && a->symbols.p[i].stb == STB_LOCAL && a->symbols.p[i].section && IsLocal(a, a->symbols.p[i].name)); } static void Objectify(struct As *a, int path) { char *p; int i, j, s, e; struct ElfWriter *elf; elf = elfwriter_open(a->strings.p[path], 0644); for (i = 0; i < a->symbols.n; ++i) { if (!IsLiveSymbol(a, i)) continue; p = strndup(a->slices.p[a->symbols.p[i].name].p, a->slices.p[a->symbols.p[i].name].n); a->symbols.p[i].ref = elfwriter_appendsym( elf, p, ELF64_ST_INFO(a->symbols.p[i].stb, a->symbols.p[i].type), a->symbols.p[i].stv, a->symbols.p[i].offset, a->symbols.p[i].size); if (a->symbols.p[i].section >= SHN_LORESERVE) { elfwriter_setsection(elf, a->symbols.p[i].ref, a->symbols.p[i].section); } free(p); } for (i = 0; i < a->sections.n; ++i) { elfwriter_align(elf, a->sections.p[i].align, 0); s = elfwriter_startsection(elf, a->strings.p[a->sections.p[i].name], a->sections.p[i].type, a->sections.p[i].flags); for (j = 0; j < a->symbols.n; ++j) { if (!IsLiveSymbol(a, j)) continue; if (a->symbols.p[j].section != i) continue; elfwriter_setsection(elf, a->symbols.p[j].ref, s); } for (j = 0; j < a->relas.n; ++j) { if (a->relas.p[j].isdead) continue; if (a->relas.p[j].section != i) continue; e = a->relas.p[j].expr; a->i = a->exprs.p[e].tok; switch (a->exprs.p[e].kind) { case EX_INT: break; case EX_SYM: elfwriter_appendrela(elf, a->relas.p[j].offset, a->symbols.p[a->exprs.p[e].x].ref, a->relas.p[j].kind, a->relas.p[j].addend); break; case EX_ADD: if (a->exprs.p[a->exprs.p[e].lhs].kind == EX_SYM && a->exprs.p[a->exprs.p[e].rhs].kind == EX_INT) { elfwriter_appendrela( elf, a->relas.p[j].offset, a->symbols.p[a->exprs.p[a->exprs.p[e].lhs].x].ref, a->relas.p[j].kind, a->relas.p[j].addend + a->exprs.p[a->exprs.p[e].rhs].x); } else { Fail(a, "bad addend"); } break; default: Fail(a, "unsupported relocation type"); } } if (a->sections.p[i].binary.n) { memcpy(elfwriter_reserve(elf, a->sections.p[i].binary.n), a->sections.p[i].binary.p, a->sections.p[i].binary.n); } elfwriter_commit(elf, a->sections.p[i].binary.n); elfwriter_finishsection(elf); } elfwriter_close(elf); } static void CheckIntegrity(struct As *a) { int i; for (i = 0; i < a->things.n; ++i) { CHECK_LT((int)a->things.p[i].s, a->sauces.n); switch (a->things.p[i].t) { case TT_INT: case TT_FORWARD: case TT_BACKWARD: CHECK_LT(a->things.p[i].i, a->ints.n); break; case TT_FLOAT: CHECK_LT(a->things.p[i].i, a->floats.n); break; case TT_SLICE: CHECK_LT(a->things.p[i].i, a->slices.n); break; default: break; } } for (i = 0; i < a->sections.n; ++i) { CHECK_LT(a->sections.p[i].name, a->strings.n); } for (i = 0; i < a->symbols.n; ++i) { CHECK_LT(a->symbols.p[i].name, a->slices.n); CHECK_LT(a->symbols.p[i].section, a->sections.n); } for (i = 0; i < a->labels.n; ++i) { CHECK_LT(a->labels.p[i].tok, a->things.n); CHECK_LT(a->labels.p[i].symbol, a->symbols.n); } for (i = 0; i < a->relas.n; ++i) { CHECK_LT(a->relas.p[i].expr, a->exprs.n); CHECK_LT(a->relas.p[i].section, a->sections.n); } for (i = 0; i < a->exprs.n; ++i) { CHECK_LT(a->exprs.p[i].tok, a->things.n); if (a->exprs.p[i].lhs != -1) CHECK_LT(a->exprs.p[i].lhs, a->exprs.n); if (a->exprs.p[i].rhs != -1) CHECK_LT(a->exprs.p[i].rhs, a->exprs.n); switch (a->exprs.p[i].kind) { case EX_SYM: CHECK_LT(a->exprs.p[i].x, a->things.n); CHECK(a->things.p[a->exprs.p[i].x].t == TT_SLICE || a->things.p[a->exprs.p[i].x].t == TT_FORWARD || a->things.p[a->exprs.p[i].x].t == TT_BACKWARD); break; default: break; } } } static void PrintThings(struct As *a) { int i; char pbuf[4], fbuf[32]; for (i = 0; i < a->things.n; ++i) { printf("%s:%d:: ", a->strings.p[a->sauces.p[a->things.p[i].s].path], a->sauces.p[a->things.p[i].s].line); switch (a->things.p[i].t) { case TT_INT: printf("TT_INT %jd\n", a->ints.p[a->things.p[i].i]); break; case TT_FLOAT: g_xfmt_p(fbuf, &a->floats.p[a->things.p[i].i], 19, sizeof(fbuf), 0); printf("TT_FLOAT %s\n", fbuf); break; case TT_SLICE: printf("TT_SLICE %.*s\n", a->slices.p[a->things.p[i].i].n, a->slices.p[a->things.p[i].i].p); break; case TT_PUNCT: printf("TT_PUNCT %s\n", PunctToStr(a->things.p[i].i, pbuf)); break; case TT_BACKWARD: printf("TT_BACKWARD %jd\n", a->ints.p[a->things.p[i].i]); break; case TT_FORWARD: printf("TT_FORWARD %jd\n", a->ints.p[a->things.p[i].i]); break; default: abort(); } } } void Assembler(int argc, char *argv[]) { struct As *a; a = NewAssembler(); ReadFlags(a, argc, argv); SaveString(&a->incpaths, strdup(".")); SaveString(&a->incpaths, xdirname(a->strings.p[a->inpath])); Tokenize(a, a->inpath); /* PrintThings(a); */ Assemble(a); /* CheckIntegrity(a); */ Evaluate(a); MarkUndefinedSymbolsGlobal(a); Objectify(a, a->outpath); /* malloc_stats(); */ FreeAssembler(a); }
138,290
4,105
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/README.md
# chibicc: A Small C Compiler (The old master has moved to [historical/old](https://github.com/rui314/chibicc/tree/historical/old) branch. This is a new one uploaded in September 2020.) chibicc is yet another small C compiler that implements most C11 features. Even though it still probably falls into the "toy compilers" category just like other small compilers do, chibicc can compile several real-world programs, including [Git](https://git-scm.com/), [SQLite](https://sqlite.org) and [libpng](http://www.libpng.org/pub/png/libpng.html), without making modifications to the compiled programs. Generated executables of these programs pass their corresponding test suites. So, chibicc actually supports a wide variety of C11 features and is able to compile hundreds of thousands of lines of real-world C code correctly. chibicc is developed as the reference implementation for a book I'm currently writing about the C compiler and the low-level programming. The book covers the vast topic with an incremental approach; in the first chapter, readers will implement a "compiler" that accepts just a single number as a "language", which will then gain one feature at a time in each section of the book until the language that the compiler accepts matches what the C11 spec specifies. I took this incremental approach from [the paper](http://scheme2006.cs.uchicago.edu/11-ghuloum.pdf) by Abdulaziz Ghuloum. Each commit of this project corresponds to a section of the book. For this purpose, not only the final state of the project but each commit was carefully written with readability in mind. Readers should be able to learn how a C language feature can be implemented just by reading one or a few commits of this project. For example, this is how [while](https://github.com/rui314/chibicc/commit/773115ab2a9c4b96f804311b95b20e9771f0190a), [[]](https://github.com/rui314/chibicc/commit/75fbd3dd6efde12eac8225d8b5723093836170a5), [?:](https://github.com/rui314/chibicc/commit/1d0e942fd567a35d296d0f10b7693e98b3dd037c), and [thread-local variable](https://github.com/rui314/chibicc/commit/79644e54cc1805e54428cde68b20d6d493b76d34) are implemented. If you have plenty of spare time, it might be fun to read it from the [first commit](https://github.com/rui314/chibicc/commit/0522e2d77e3ab82d3b80a5be8dbbdc8d4180561c). If you like this project, please consider purchasing a copy of the book when it becomes available! 😀 I publish the source code here to give people early access to it, because I was planing to do that anyway with a permissive open-source license after publishing the book. If I don't charge for the source code, it doesn't make much sense to me to keep it private. I hope to publish the book in 2021. I pronounce chibicc as _chee bee cee cee_. "chibi" means "mini" or "small" in Japanese. "cc" stands for C compiler. ## Status Features that are often missing in a small compiler but supported by chibicc include (but not limited to): - Preprocessor - long double (x87 80-bit floting point numbers) - Bit-field - alloca() - Variable-length array - Thread-local variable - Atomic variable - Common symbol - Designated initializer - L, u, U and u8 string literals chibicc does not support digraphs, trigraphs, complex numbers, K&R-style function prototype, and inline assembly. chibicc outputs a simple but nice error message when it finds an error in source code. There's no optimization pass. chibicc emits terrible code which is probably twice or more slower than GCC's output. I have a plan to add an optimization pass once the frontend is done. ## Internals chibicc consists of the following stages: - Tokenize: A tokenizer takes a string as an input, breaks it into a list of tokens and returns them. - Preprocess: A preprocessor takes as an input a list of tokens and output a new list of macro-expanded tokens. It interprets preprocessor directives while expanding macros. - Parse: A recursive descendent parser constructs abstract syntax trees from the output of the preprocessor. It also adds a type to each AST node. - Codegen: A code generator emits an assembly text for given AST nodes. ## Contributing When I find a bug in this compiler, I go back to the original commit that introduced the bug and rewrite the commit history as if there were no such bug from the beginning. This is an unusual way of fixing bugs, but as a a part of a book, it is important to keep every commit bug-free. Thus, I do not take pull requests in this repo. You can send me a pull request if you find a bug, but it is very likely that I will read your patch and then apply that to my previous commits by rewriting history. I'll credit your name somewhere, but your changes will be rewritten by me before submitted to this repository. Also, please assume that I will occasionally force-push my local repository to this public one to rewrite history. If you clone this project and make local commits on top of it, your changes will have to be rebased by hand when I force-push new commits. ## About the Author I'm Rui Ueyama. I'm the creator of [8cc](https://github.com/rui314/8cc), which is a hobby C compiler, and also the original creator of the current version of [LLVM lld](https://lld.llvm.org) linker, which is a production-quality linker used by various operating systems and large-scale build systems. ## References - [tcc](https://bellard.org/tcc/): A small C compiler written by Fabrice Bellard. I learned a lot from this compiler, but the design of tcc and chibicc are different. In particular, tcc is a one-pass compiler, while chibicc is a multi-pass one. - [lcc](https://github.com/drh/lcc): Another small C compiler. The creators wrote a [book](https://sites.google.com/site/lccretargetablecompiler/) about the internals of lcc, which I found a good resource to see how a compiler is implemented. - [An Incremental Approach to Compiler Construction](http://scheme2006.cs.uchicago.edu/11-ghuloum.pdf)
5,972
135
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/hashmap.c
// This is an implementation of the open-addressing hash table. #include "libc/nexgen32e/crc32.h" #include "third_party/chibicc/chibicc.h" #define INIT_SIZE 16 // initial hash bucket size #define LOW_WATERMARK 20 // keep usage below 50% after rehashing #define HIGH_WATERMARK 40 // perform rehash when usage exceeds 70% #define TOMBSTONE ((void *)-1) // represents deleted hash table entry long chibicc_hashmap_hits; long chibicc_hashmap_miss; static inline uint64_t fnv_hash(char *s, int len) { return crc32c(0, s, len); uint64_t hash = 0xcbf29ce484222325; for (int i = 0; i < len; i++) { hash *= 0x100000001b3; hash ^= (unsigned char)s[i]; } return hash; } // Make room for new entires in a given hashmap by removing // tombstones and possibly extending the bucket size. static void rehash(HashMap *map) { // Compute the size of the new hashmap. int nkeys = 0; for (int i = 0; i < map->capacity; i++) { if (map->buckets[i].key && map->buckets[i].key != TOMBSTONE) { nkeys++; } } int cap = MAX(8, map->capacity); while ((nkeys * 100) / cap >= LOW_WATERMARK) cap = cap * 2; assert(cap > 0); // Create a new hashmap and copy all key-values. HashMap map2 = {}; map2.buckets = calloc(cap, sizeof(HashEntry)); map2.capacity = cap; for (int i = 0; i < map->capacity; i++) { HashEntry *ent = &map->buckets[i]; if (ent->key && ent->key != TOMBSTONE) { hashmap_put2(&map2, ent->key, ent->keylen, ent->val); } } assert(map2.used == nkeys); *map = map2; } static bool match(HashEntry *ent, char *key, int keylen) { if (ent->key && ent->key != TOMBSTONE) { if (ent->keylen == keylen && !memcmp(ent->key, key, keylen)) { ++chibicc_hashmap_hits; return true; } else { ++chibicc_hashmap_miss; return false; } } else { return false; } } static HashEntry *get_entry(HashMap *map, char *key, int keylen) { if (!map->buckets) return NULL; uint64_t hash = fnv_hash(key, keylen); for (int i = 0; i < map->capacity; i++) { HashEntry *ent = &map->buckets[(hash + i) & (map->capacity - 1)]; if (match(ent, key, keylen)) return ent; if (ent->key == NULL) return NULL; } UNREACHABLE(); } static HashEntry *get_or_insert_entry(HashMap *map, char *key, int keylen) { if (!map->buckets) { map->buckets = calloc(INIT_SIZE, sizeof(HashEntry)); map->capacity = INIT_SIZE; } else if ((map->used * 100) / map->capacity >= HIGH_WATERMARK) { rehash(map); } uint64_t hash = fnv_hash(key, keylen); for (int i = 0; i < map->capacity; i++) { HashEntry *ent = &map->buckets[(hash + i) & (map->capacity - 1)]; if (match(ent, key, keylen)) return ent; if (ent->key == TOMBSTONE) { ent->key = key; ent->keylen = keylen; return ent; } if (ent->key == NULL) { ent->key = key; ent->keylen = keylen; map->used++; return ent; } } UNREACHABLE(); } void *hashmap_get(HashMap *map, char *key) { return hashmap_get2(map, key, strlen(key)); } void *hashmap_get2(HashMap *map, char *key, int keylen) { HashEntry *ent = get_entry(map, key, keylen); return ent ? ent->val : NULL; } void hashmap_put(HashMap *map, char *key, void *val) { hashmap_put2(map, key, strlen(key), val); } void hashmap_put2(HashMap *map, char *key, int keylen, void *val) { HashEntry *ent = get_or_insert_entry(map, key, keylen); ent->val = val; } void hashmap_delete(HashMap *map, char *key) { hashmap_delete2(map, key, strlen(key)); } void hashmap_delete2(HashMap *map, char *key, int keylen) { HashEntry *ent = get_entry(map, key, keylen); if (ent) ent->key = TOMBSTONE; }
3,712
128
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/cast.c
#include "third_party/chibicc/chibicc.h" #define REDZONE(X) \ "sub\t$16,%rsp\n" \ "\t" X "\n" \ "\tadd\t$16,%rsp" #define PUSHPOPRAX(X) \ "push\t%rax\n" \ "\t" X "\n" \ "\tpop\t%rax" #ifdef __SSE3__ #define FIST(X) REDZONE("fistt" X) #else #define FIST(X) \ REDZONE("fnstcw\t8(%rsp)\n" \ "\tmovzwl\t8(%rsp),%eax\n" \ "\tor\t$12,%ah\n" \ "\tmov\t%ax,10(%rsp)\n" \ "\tfldcw\t10(%rsp)\n" \ "\tfist" X "\n" \ "\tfldcw\t8(%rsp)") #endif #define u64f64 \ "test\t%rax,%rax\n" \ "\tjs\t1f\n" \ "\tpxor\t%xmm0,%xmm0\n" \ "\tcvtsi2sd %rax,%xmm0\n" \ "\tjmp\t2f\n" \ "1:\n" \ "\tmov\t%rax,%rdi\n" \ "\tand\t$1,%eax\n" \ "\tpxor\t%xmm0,%xmm0\n" \ "\tshr\t%rdi\n" \ "\tor\t%rax,%rdi\n" \ "\tcvtsi2sd %rdi,%xmm0\n" \ "\taddsd\t%xmm0,%xmm0\n" \ "2:" #define u64f80 \ PUSHPOPRAX("fildq\t(%rsp)\n" \ "\ttest\t%rax,%rax\n" \ "\tjns\t1f\n" \ "\tmovq\t$0x5f800000,(%rsp)\n" \ "\tfadds\t(%rsp)\n" \ "1:") #define i32i8 "movsbl\t%al,%eax" #define i32u8 "movzbl\t%al,%eax" #define i32i16 "cwtl" #define i32u16 "movzwl\t%ax,%eax" #define i32f32 "cvtsi2ssl %eax,%xmm0" #define i32i64 "cltq" #define i32f64 "cvtsi2sdl %eax,%xmm0" #define u32i64 "mov\t%eax,%eax" #define i64f32 "cvtsi2ssq %rax,%xmm0" #define i64f64 "cvtsi2sdq %rax,%xmm0" #define f32i32 "cvttss2sil %xmm0,%eax" #define f32u32 "cvttss2siq %xmm0,%rax" #define f32i64 "cvttss2siq %xmm0,%rax" #define f32u64 "cvttss2siq %xmm0,%rax" #define f32f64 "cvtss2sd %xmm0,%xmm0" #define f64i32 "cvttsd2sil %xmm0,%eax" #define f64u32 "cvttsd2siq %xmm0,%rax" #define f64i64 "cvttsd2siq %xmm0,%rax" #define f64u64 "cvttsd2siq %xmm0,%rax" #define f64f32 "cvtsd2ss %xmm0,%xmm0" #define u64f32 "cvtsi2ssq %rax,%xmm0" #define f32i8 "cvttss2sil %xmm0,%eax\n\tmovsbl\t%al,%eax" #define f32u8 "cvttss2sil %xmm0,%eax\n\tmovzbl\t%al,%eax" #define f32i16 "cvttss2sil %xmm0,%eax\n\tmovswl\t%ax,%eax" #define f32u16 "cvttss2sil %xmm0,%eax\n\tmovzwl\t%ax,%eax" #define f64i8 "cvttsd2sil %xmm0,%eax\n\tmovsbl\t%al,%eax" #define f64u8 "cvttsd2sil %xmm0,%eax\n\tmovzbl\t%al,%eax" #define f64i16 "cvttsd2sil %xmm0,%eax\n\tmovswl\t%ax,%eax" #define f64u16 "cvttsd2sil %xmm0,%eax\n\tmovzwl\t%ax,%eax" #define f64f80 REDZONE("movsd\t%xmm0,(%rsp)\n\tfldl\t(%rsp)") #define f80i8 FIST("ps\t(%rsp)\n\tmovsbl\t(%rsp),%eax") #define f80u8 FIST("ps\t(%rsp)\n\tmovzbl\t(%rsp),%eax") #define f80i16 FIST("ps\t(%rsp)\n\tmovzbl\t(%rsp),%eax") #define f80u16 FIST("pl\t(%rsp)\n\tmovswl\t(%rsp),%eax") #define f80i32 FIST("pl\t(%rsp)\n\tmov\t(%rsp),%eax") #define f80u32 FIST("pl\t(%rsp)\n\tmov\t(%rsp),%eax") #define f80i64 FIST("pq\t(%rsp)\n\tmov\t(%rsp),%rax") #define f80u64 FIST("pq\t(%rsp)\n\tmov\t(%rsp),%rax") #define f80f32 REDZONE("fstps\t(%rsp)\n\tmovss\t(%rsp),%xmm0") #define f80f64 REDZONE("fstpl\t(%rsp)\n\tmovsd\t(%rsp),%xmm0") #define i32f80 PUSHPOPRAX("fildl\t(%rsp)") #define u32f32 "mov\t%eax,%eax\n\tcvtsi2ssq %rax,%xmm0" #define u32f64 "mov\t%eax,%eax\n\tcvtsi2sdq %rax,%xmm0" #define u32f80 "mov\t%eax,%eax\n\tpush %rax\n\tfildll\t(%rsp)\n\tpop\t%rax" #define i64f80 PUSHPOPRAX("fildll\t(%rsp)") #define f32f80 REDZONE("movss\t%xmm0,(%rsp)\n\tflds\t(%rsp)") #define i8i128 "movsbq\t%al,%rax\n\tcqto" #define i16i128 "movswq\t%ax,%rax\n\tcqto" #define i32i128 "cltq\n\tcqto" #define i64i128 "cqto" #define u8i128 "movzbq\t%al,%rax\n\txor\t%edx,%edx" #define u16i128 "movzwq\t%ax,%rax\n\txor\t%edx,%edx" #define u32i128 "cltq\n\txor\t%edx,%edx" #define u64i128 "xor\t%edx,%edx" #define i128f32 "mov\t%rax,%rdi\n\tmov\t%rdx,%rsi\n\tcall\t__floattisf" #define i128f64 "mov\t%rax,%rdi\n\tmov\t%rdx,%rsi\n\tcall\t__floattidf" #define i128f80 "mov\t%rax,%rdi\n\tmov\t%rdx,%rsi\n\tcall\t__floattixf" #define u128f32 "mov\t%rax,%rdi\n\tmov\t%rdx,%rsi\n\tcall\t__floatuntisf" #define u128f64 "mov\t%rax,%rdi\n\tmov\t%rdx,%rsi\n\tcall\t__floatuntidf" #define u128f80 "mov\t%rax,%rdi\n\tmov\t%rdx,%rsi\n\tcall\t__floatuntixf" #define f32i128 "call\t__fixsfti" #define f64i128 "call\t__fixdfti" #define f80i128 REDZONE("fstpt\t(%rsp)\n\tcall\t__fixxfti") #define f32u128 "call\t__fixunssfti" #define f64u128 "call\t__fixunsdfti" #define f80u128 REDZONE("fstpt\t(%rsp)\n\tcall\t__fixunsxfti") enum { I8, I16, I32, I64, U8, U16, U32, U64, F32, F64, F80, I128, U128 }; static const char *const cast_table[13][13] = /* clang-format off */ { // i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 f80 i128 u128 {NULL, NULL, NULL, i32i64, i32u8, i32u16, NULL, i32i64, i32f32, i32f64, i32f80, i32i128, i32i128}, // i8 {i32i8, NULL, NULL, i32i64, i32u8, i32u16, NULL, i32i64, i32f32, i32f64, i32f80, i32i128, i32i128}, // i16 {i32i8, i32i16, NULL, i32i64, i32u8, i32u16, NULL, i32i64, i32f32, i32f64, i32f80, i32i128, i32i128}, // i32 {i32i8, i32i16, NULL, NULL, i32u8, i32u16, NULL, NULL, i64f32, i64f64, i64f80, i64i128, i64i128}, // i64 {i32i8, NULL, NULL, i32i64, NULL, NULL, NULL, i32i64, i32f32, i32f64, i32f80, i32i128, i32i128}, // u8 {i32i8, i32i16, NULL, i32i64, i32u8, NULL, NULL, i32i64, i32f32, i32f64, i32f80, i32i128, i32i128}, // u16 {i32i8, i32i16, NULL, u32i64, i32u8, i32u16, NULL, u32i64, u32f32, u32f64, u32f80, u32i128, i32i128}, // u32 {i32i8, i32i16, NULL, NULL, i32u8, i32u16, NULL, NULL, u64f32, u64f64, u64f80, u64i128, u64i128}, // u64 {f32i8, f32i16, f32i32, f32i64, f32u8, f32u16, f32u32, f32u64, NULL, f32f64, f32f80, f32i128, f32u128}, // f32 {f64i8, f64i16, f64i32, f64i64, f64u8, f64u16, f64u32, f64u64, f64f32, NULL, f64f80, f64i128, f64u128}, // f64 {f80i8, f80i16, f80i32, f80i64, f80u8, f80u16, f80u32, f80u64, f80f32, f80f64, NULL, f80i128, f80u128}, // f80 {i32i8, i32i16, NULL, NULL, i32u8, i32u16, NULL, NULL, i128f32, i128f64, i128f80, NULL, NULL }, // i128 {i32i8, i32i16, NULL, NULL, i32u8, i32u16, NULL, NULL, u128f32, u128f64, u128f80, NULL, NULL }, // u128 } /* clang-format on */; static int getTypeId(Type *ty) { switch (ty->kind) { case TY_CHAR: return ty->is_unsigned ? U8 : I8; case TY_SHORT: return ty->is_unsigned ? U16 : I16; case TY_INT: return ty->is_unsigned ? U32 : I32; case TY_LONG: return ty->is_unsigned ? U64 : I64; case TY_INT128: return ty->is_unsigned ? U128 : I128; case TY_FLOAT: return F32; case TY_DOUBLE: return F64; case TY_LDOUBLE: return F80; default: return U64; } } void gen_cast(Type *from, Type *to) { bool skew; if (to->kind == TY_VOID) return; if (to->kind == TY_BOOL) { cmp_zero(from); println("\tsetne\t%%al"); println("\tmovzbl\t%%al,%%eax"); return; } int t1 = getTypeId(from); int t2 = getTypeId(to); if (cast_table[t1][t2]) { if ((skew = (depth & 1) && strstr(cast_table[t1][t2], "call"))) { println("\tsub\t$8,%%rsp"); depth++; } println("\t%s", cast_table[t1][t2]); if (skew) { println("\tadd\t$8,%%rsp"); depth--; } } }
7,553
181
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/kw.h
#ifndef COSMOPOLITAN_THIRD_PARTY_CHIBICC_KW_H_ #define COSMOPOLITAN_THIRD_PARTY_CHIBICC_KW_H_ #define KW_STRUCT 1 /* keyword typename */ #define KW_STATIC 2 /* keyword typename */ #define KW_VOID 3 /* keyword typename */ #define KW_CHAR 4 /* keyword typename */ #define KW_UNSIGNED 5 /* keyword typename */ #define KW_LONG 6 /* keyword typename */ #define KW_UNION 7 /* keyword typename */ #define KW_DOUBLE 8 /* keyword typename */ #define KW_CONST 9 /* keyword typename */ #define KW_FLOAT 10 /* keyword typename */ #define KW_SHORT 11 /* keyword typename */ #define KW_SIGNED 12 /* keyword typename */ #define KW_ENUM 13 /* keyword typename */ #define KW_AUTO 14 /* keyword typename */ #define KW_REGISTER 15 /* keyword typename */ #define KW__NORETURN 16 /* keyword typename */ #define KW_EXTERN 17 /* keyword typename */ #define KW_INLINE 18 /* keyword typename */ #define KW_INT 19 /* keyword typename */ #define KW_RESTRICT 20 /* keyword typename */ #define KW_TYPEDEF 21 /* keyword typename */ #define KW_TYPEOF 22 /* keyword typename */ #define KW_VOLATILE 23 /* keyword typename */ #define KW__ALIGNAS 24 /* keyword typename */ #define KW__ATOMIC 25 /* keyword typename */ #define KW__BOOL 26 /* keyword typename */ #define KW__THREAD_LOCAL 27 /* keyword typename */ #define KW___INT128 28 /* keyword typename */ #define KW_IF 33 /* keyword */ #define KW_RETURN 34 /* keyword */ #define KW_CASE 35 /* keyword */ #define KW_ELSE 36 /* keyword */ #define KW_FOR 37 /* keyword */ #define KW_DO 38 /* keyword */ #define KW_SIZEOF 39 /* keyword */ #define KW_WHILE 40 /* keyword */ #define KW_SWITCH 41 /* keyword */ #define KW_BREAK 42 /* keyword */ #define KW_CONTINUE 43 /* keyword */ #define KW_ASM 44 /* keyword */ #define KW_DEFAULT 45 /* keyword */ #define KW___ATTRIBUTE__ 46 /* keyword */ #define KW_GOTO 47 /* keyword */ #define KW__ALIGNOF 48 /* keyword */ #define KW_INCLUDE 64 #define KW_IFDEF 65 #define KW_IFNDEF 66 #define KW_DEFINE 67 #define KW_DEFINED 68 #define KW_ELIF 69 #define KW_ENDIF 70 #define KW_ERROR 71 #define KW_INCLUDE_NEXT 72 #define KW_LINE 73 #define KW_PRAGMA 74 #define KW_STRCHR 75 #define KW_STRLEN 76 #define KW_STRPBRK 77 #define KW_STRSTR 78 #define KW_UNDEF 79 #define KW__GENERIC 80 #define KW__STATIC_ASSERT 81 #define KW___VA_OPT__ 82 #define KW___ALIGNOF__ 83 #define KW___ASM__ 84 #define KW___BUILTIN_ADD_OVERFLOW 85 #define KW___BUILTIN_ASSUME_ALIGNED 86 #define KW___BUILTIN_CONSTANT_P 87 #define KW___BUILTIN_EXPECT 88 #define KW___BUILTIN_FFS 89 #define KW___BUILTIN_FFSL 90 #define KW___BUILTIN_FFSLL 91 #define KW___BUILTIN_FPCLASSIFY 92 #define KW___BUILTIN_MUL_OVERFLOW 93 #define KW___BUILTIN_NEG_OVERFLOW 94 #define KW___BUILTIN_OFFSETOF 95 #define KW___BUILTIN_POPCOUNT 96 #define KW___BUILTIN_POPCOUNTL 97 #define KW___BUILTIN_POPCOUNTLL 98 #define KW___BUILTIN_REG_CLASS 99 #define KW___BUILTIN_STRCHR 100 #define KW___BUILTIN_STRLEN 101 #define KW___BUILTIN_STRPBRK 102 #define KW___BUILTIN_STRSTR 103 #define KW___BUILTIN_SUB_OVERFLOW 104 #define KW___BUILTIN_TYPES_COMPATIBLE_P 105 #define KW_LP 106 #define KW_RP 107 #define KW_LB 108 #define KW_RB 109 #define KW_PLUS 110 #define KW_MINUS 111 #define KW_AMP 112 #define KW_STAR 113 #define KW_EXCLAIM 114 #define KW_TILDE 115 #define KW_INCREMENT 116 #define KW_DECREMENT 117 #define KW_LOGAND 118 #define KW_LOGOR 119 #define KW_ARROW 120 #define KW_DOT 121 #define KW___ATOMIC_LOAD 122 #define KW___SYNC_LOCK_TEST_AND_SET 123 #define KW___SYNC_LOCK_RELEASE 124 #define KW___BUILTIN_IA32_PMOVMSKB128 125 #define KW___BUILTIN_IA32_MOVNTDQ 126 #define KW___ATOMIC_FETCH_ADD 127 #define KW___ATOMIC_TEST_AND_SET 128 #define KW___ATOMIC_CLEAR 129 #define KW___ATOMIC_STORE 130 #define KW___ATOMIC_STORE_N 131 #define KW___ATOMIC_LOAD_N 132 #define KW___ATOMIC_FETCH_SUB 133 #define KW___ATOMIC_FETCH_AND 134 #define KW___ATOMIC_FETCH_OR 135 #define KW___ATOMIC_FETCH_XOR 136 #define KW___ATOMIC_COMPARE_EXCHANGE_N 137 #define KW___ATOMIC_EXCHANGE_N 138 #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ unsigned char GetKw(const char *, size_t); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_THIRD_PARTY_CHIBICC_KW_H_ */
6,344
132
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/tokenize.c
#include "libc/intrin/bsf.h" #include "libc/log/log.h" #include "libc/runtime/runtime.h" #include "libc/str/str.h" #include "libc/str/tab.internal.h" #include "third_party/chibicc/chibicc.h" #include "third_party/chibicc/file.h" #include "third_party/chibicc/kw.h" // Input file static File *current_file; // A list of all input files. static File **input_files; // True if the current position is at the beginning of a line static bool at_bol; // True if the current position follows a space character static bool has_space; // Reports an error and exit. void error(char *fmt, ...) { va_list ap; va_start(ap, fmt); vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); exit(1); } // Reports an error message in the following format. // // foo.c:10: x = y + 1; // ^ <error message here> static void verror_at(char *filename, char *input, int line_no, char *loc, char *fmt, va_list ap) { // Find a line containing `loc`. char *line = loc; while (input < line && line[-1] != '\n') line--; char *end = loc; while (*end && *end != '\n') end++; // Print out the line. int indent = fprintf(stderr, "%s:%d: ", filename, line_no); fprintf(stderr, "%.*s\n", (int)(end - line), line); // Show the error message. int pos = display_width(line, loc - line) + indent; fprintf(stderr, "%*s", pos, ""); // print pos spaces. fprintf(stderr, "^ "); vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); } void error_at(char *loc, char *fmt, ...) { int line_no = 1; for (char *p = current_file->contents; p < loc; p++) { if (*p == '\n') line_no++; } va_list ap; va_start(ap, fmt); verror_at(current_file->name, current_file->contents, line_no, loc, fmt, ap); va_end(ap); exit(1); } void error_tok(Token *tok, char *fmt, ...) { va_list ap, va; va_start(va, fmt); for (Token *t = tok; t; t = t->origin) { va_copy(ap, va); verror_at(t->file->name, t->file->contents, t->line_no, t->loc, fmt, ap); va_end(ap); } va_end(ap); exit(1); } void warn_tok(Token *tok, char *fmt, ...) { va_list ap; va_start(ap, fmt); verror_at(tok->file->name, tok->file->contents, tok->line_no, tok->loc, fmt, ap); va_end(ap); } // Consumes the current token if it matches `op`. bool equal(Token *tok, char *op, size_t n) { return n == tok->len && !memcmp(tok->loc, op, tok->len); } bool consume(Token **rest, Token *tok, char *str, size_t n) { if (n == tok->len && !memcmp(tok->loc, str, n)) { *rest = tok->next; return true; } *rest = tok; return false; } // Ensure that the current token is `op`. Token *skip(Token *tok, char op) { while (tok->kind == TK_JAVADOWN) { tok = tok->next; } if (tok->len == 1 && *tok->loc == op) { return tok->next; } else { error_tok(tok, "expected '%c'", op); } } // Create a new token. static Token *new_token(TokenKind kind, char *start, char *end) { Token *tok = alloc_token(); tok->kind = kind; tok->loc = start; tok->len = end - start; tok->file = current_file; tok->filename = current_file->display_name; tok->at_bol = at_bol; tok->has_space = has_space; at_bol = has_space = false; return tok; } // Read an identifier and returns the length of it. // If p does not point to a valid identifier, 0 is returned. dontinline int read_ident(char *start) { uint32_t c; char *p = start; if (('a' <= *p && *p <= 'z') || ('A' <= *p && *p <= 'Z') || *p == '_') { ++p; } else { c = decode_utf8(&p, p); if (!is_ident1(c)) return 0; } for (;;) { if (('a' <= *p && *p <= 'z') || ('A' <= *p && *p <= 'Z') || ('0' <= *p && *p <= '9') || *p == '_') { ++p; } else { char *q; c = decode_utf8(&q, p); if (!is_ident2(c)) { return p - start; } p = q; } } } // Read a punctuator token from p and returns its length. int read_punct(char *p) { static const char kPunct[][4] = { "<<=", ">>=", "...", "==", "!=", "<=", ">=", "->", "+=", "-=", "*=", "/=", "++", "--", "%=", "&=", "|=", "^=", "&&", "||", "<<", ">>", "##", }; if (ispunct(*p)) { for (int i = 0; i < sizeof(kPunct) / sizeof(*kPunct); i++) { for (int j = 0;;) { if (p[j] != kPunct[i][j]) break; if (!kPunct[i][++j]) return j; } } return 1; } else { return 0; } } static bool is_keyword(Token *tok) { unsigned char kw; kw = GetKw(tok->loc, tok->len); return kw && !(kw & -64); } int read_escaped_char(char **new_pos, char *p) { int x; unsigned c; if ('0' <= *p && *p <= '7') { // Read an octal number. c = *p++ - '0'; if ('0' <= *p && *p <= '7') { c = (c << 3) + (*p++ - '0'); if ('0' <= *p && *p <= '7') c = (c << 3) + (*p++ - '0'); } *new_pos = p; return c; } if (*p == 'x') { // Read a hexadecimal number. if ((++p, x = kHexToInt[*p++ & 255]) != -1) { for (c = x; (x = kHexToInt[*p & 255]) != -1; p++) { c = c << 4 | x; } *new_pos = p; return c; } else { error_at(p, "invalid hex escape sequence"); } } *new_pos = p + 1; // Escape sequences are defined using themselves here. E.g. // '\n' is implemented using '\n'. This tautological definition // works because the compiler that compiles our compiler knows // what '\n' actually is. In other words, we "inherit" the ASCII // code of '\n' from the compiler that compiles our compiler, // so we don't have to teach the actual code here. // // This fact has huge implications not only for the correctness // of the compiler but also for the security of the generated code. // For more info, read "Reflections on Trusting Trust" by Ken Thompson. // https://github.com/rui314/chibicc/wiki/thompson1984.pdf switch (*p) { case 'a': return '\a'; case 'b': return '\b'; case 't': return '\t'; case 'n': return '\n'; case 'v': return '\v'; case 'f': return '\f'; case 'r': return '\r'; // [GNU] \e for the ASCII escape character is a GNU C extension. case 'e': return 27; default: return *p; } } // Find a closing double-quote. static char *string_literal_end(char *p) { char *start = p; for (; *p != '"'; p++) { if (*p == '\n' || *p == '\0') error_at(start, "unclosed string literal"); if (*p == '\\') p++; } return p; } static Token *read_string_literal(char *start, char *quote) { char *end = string_literal_end(quote + 1); char *buf = calloc(2, end - quote); int len = 0; for (char *p = quote + 1; p < end;) { if (*p == '\\') buf[len++] = read_escaped_char(&p, p + 1); else buf[len++] = *p++; } Token *tok = new_token(TK_STR, start, end + 1); tok->ty = array_of(ty_char, len + 1); tok->str = buf; return tok; } // Read a UTF-8-encoded string literal and transcode it in UTF-16. // // UTF-16 is yet another variable-width encoding for Unicode. Code // points smaller than U+10000 are encoded in 2 bytes. Code points // equal to or larger than that are encoded in 4 bytes. Each 2 bytes // in the 4 byte sequence is called "surrogate", and a 4 byte sequence // is called a "surrogate pair". static Token *read_utf16_string_literal(char *start, char *quote) { char *end = string_literal_end(quote + 1); uint16_t *buf = calloc(2, end - start); int len = 0; for (char *p = quote + 1; p < end;) { if (*p == '\\') { buf[len++] = read_escaped_char(&p, p + 1); continue; } uint32_t c = decode_utf8(&p, p); if (c < 0x10000) { // Encode a code point in 2 bytes. buf[len++] = c; } else { // Encode a code point in 4 bytes. c -= 0x10000; buf[len++] = 0xd800 + ((c >> 10) & 0x3ff); buf[len++] = 0xdc00 + (c & 0x3ff); } } Token *tok = new_token(TK_STR, start, end + 1); tok->ty = array_of(ty_ushort, len + 1); tok->str = (char *)buf; return tok; } // Read a UTF-8-encoded string literal and transcode it in UTF-32. // // UTF-32 is a fixed-width encoding for Unicode. Each code point is // encoded in 4 bytes. static Token *read_utf32_string_literal(char *start, char *quote, Type *ty) { char *end = string_literal_end(quote + 1); uint32_t *buf = calloc(4, end - quote); int len = 0; for (char *p = quote + 1; p < end;) { if (*p == '\\') buf[len++] = read_escaped_char(&p, p + 1); else buf[len++] = decode_utf8(&p, p); } Token *tok = new_token(TK_STR, start, end + 1); tok->ty = array_of(ty, len + 1); tok->str = (char *)buf; return tok; } static Token *read_char_literal(char *start, char *quote, Type *ty) { char *p = quote + 1; if (*p == '\0') error_at(start, "unclosed char literal"); int c; if (*p == '\\') c = read_escaped_char(&p, p + 1); else c = decode_utf8(&p, p); char *end = strchr(p, '\''); if (!end) error_at(p, "unclosed char literal"); Token *tok = new_token(TK_NUM, start, end + 1); tok->val = c; tok->ty = ty; return tok; } bool convert_pp_int(Token *tok) { char *p = tok->loc; // Read a binary, octal, decimal or hexadecimal number. int base = 10; if ((p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) && kHexToInt[p[2] & 255] != -1) { p += 2; base = 16; } else if ((p[0] == '0' && (p[1] == 'b' || p[1] == 'B')) && (p[2] == '0' || p[2] == '1')) { p += 2; base = 2; } else if (*p == '0') { base = 8; } int64_t val = strtoul(p, &p, base); // Read U, L or LL suffixes. bool l = false; bool u = false; int a, b, c; if ((a = kToLower[p[0] & 255]) && (a == 'l' || a == 'u')) { b = kToLower[p[1] & 255]; c = b ? kToLower[p[2] & 255] : 0; if ((a == 'l' && b == 'l' && c == 'u') || (a == 'u' && b == 'l' && c == 'l')) { p += 3; l = u = true; } else if ((a == 'l' && b == 'u') || (a == 'u' && b == 'l')) { p += 2; l = u = true; } else if (a == 'l' && b == 'l') { p += 2; l = true; } else if (a == 'l') { p += 1; l = true; } else if (a == 'u') { p += 1; u = true; } } if (p != tok->loc + tok->len) return false; // Infer a type. Type *ty; if (base == 10) { if (l && u) ty = ty_ulong; else if (l) ty = ty_long; else if (u) ty = (val >> 32) ? ty_ulong : ty_uint; else ty = (val >> 31) ? ty_long : ty_int; } else { if (l && u) ty = ty_ulong; else if (l) ty = (val >> 63) ? ty_ulong : ty_long; else if (u) ty = (val >> 32) ? ty_ulong : ty_uint; else if (val >> 63) ty = ty_ulong; else if (val >> 32) ty = ty_long; else if (val >> 31) ty = ty_uint; else ty = ty_int; } tok->kind = TK_NUM; tok->val = val; tok->ty = ty; return true; } // The definition of the numeric literal at the preprocessing stage // is more relaxed than the definition of that at the later stages. // In order to handle that, a numeric literal is tokenized as a // "pp-number" token first and then converted to a regular number // token after preprocessing. // // This function converts a pp-number token to a regular number token. static void convert_pp_number(Token *tok) { // Try to parse as an integer constant. if (convert_pp_int(tok)) return; // If it's not an integer, it must be a floating point constant. char *end; long double val = strtold(tok->loc, &end); Type *ty; if (*end == 'f' || *end == 'F') { ty = ty_float; end++; } else if (*end == 'l' || *end == 'L') { ty = ty_ldouble; end++; } else { ty = ty_double; } if (tok->loc + tok->len != end) error_tok(tok, "invalid numeric constant"); tok->kind = TK_NUM; tok->fval = val; tok->ty = ty; } void convert_pp_tokens(Token *tok) { for (Token *t = tok; t->kind != TK_EOF; t = t->next) { if (is_keyword(t)) t->kind = TK_KEYWORD; else if (t->kind == TK_PP_NUM) convert_pp_number(t); } } // Initialize line info for all tokens. static void add_line_numbers(Token *tok) { char *p = current_file->contents; int n = 1; do { if (p == tok->loc) { tok->line_no = n; tok = tok->next; } if (*p == '\n') n++; } while (*p++); } Token *tokenize_string_literal(Token *tok, Type *basety) { Token *t; if (basety->size == 2) t = read_utf16_string_literal(tok->loc, tok->loc); else t = read_utf32_string_literal(tok->loc, tok->loc, basety); t->next = tok->next; return t; } // Tokenize a given string and returns new tokens. Token *tokenize(File *file) { Token head = {}; Token *cur = &head; char *q, *p = file->contents; struct Javadown *javadown; current_file = file; at_bol = true; has_space = false; for (;;) { switch (*p) { case 0: cur = cur->next = new_token(TK_EOF, p, p); add_line_numbers(head.next); return head.next; case '/': // Skip line comments. if (p[1] == '/') { p += 2; while (*p != '\n') p++; has_space = true; continue; } // Javadoc-style markdown comments. if (p[1] == '*' && p[2] == '*' && p[3] != '/' && p[3] != '*') { q = strstr(p + 3, "*/"); if (!q) error_at(p, "unclosed javadown"); javadown = ParseJavadown(p + 3, q - p - 3 - 2); if (javadown->isfileoverview) { FreeJavadown(file->javadown); file->javadown = javadown; } else { cur = cur->next = new_token(TK_JAVADOWN, p, q + 2); cur->javadown = javadown; } p = q + 2; has_space = true; continue; } // Skip block comments. if (p[1] == '*') { q = strstr(p + 2, "*/"); if (!q) error_at(p, "unclosed block comment"); p = q + 2; has_space = true; continue; } break; case '\n': // Skip newline. p++; at_bol = true; has_space = false; continue; case ' ': case '\t': case '\r': case '\f': case '\v': // Skip whitespace characters. p++; has_space = true; continue; case '.': if (!isdigit(p[1])) break; /* fallthrough */ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': // Numeric literal q = p++; for (;;) { if (p[0] && p[1] && (p[0] == 'e' || p[0] == 'E' || p[0] == 'p' || p[0] == 'P') && (p[1] == '+' || p[1] == '-')) { p += 2; } else if (('0' <= *p && *p <= '9') || ('A' <= *p && *p <= 'Z') || ('a' <= *p && *p <= 'z') || *p == '.') { p++; } else { break; } } cur = cur->next = new_token(TK_PP_NUM, q, p); continue; case '"': // String literal cur = cur->next = read_string_literal(p, p); p += cur->len; continue; case 'u': // UTF-8 string literal if (p[1] == '8' && p[2] == '"') { cur = cur->next = read_string_literal(p, p + 2); p += cur->len; continue; } // UTF-16 string literal if (p[1] == '"') { cur = cur->next = read_utf16_string_literal(p, p + 1); p += cur->len; continue; } // UTF-16 character literal if (p[1] == '\'') { cur = cur->next = read_char_literal(p, p + 1, ty_ushort); cur->val &= 0xffff; p += cur->len; continue; } break; case 'L': // Wide string literal if (p[1] == '"') { cur = cur->next = read_utf32_string_literal(p, p + 1, ty_int); p += cur->len; continue; } // Wide character literal if (p[1] == '\'') { cur = cur->next = read_char_literal(p, p + 1, ty_int); p += cur->len; continue; } break; case '\'': // Character literal cur = cur->next = read_char_literal(p, p, ty_int); cur->val = (char)cur->val; p += cur->len; continue; case 'U': // UTF-32 string literal if (p[1] == '"') { cur = cur->next = read_utf32_string_literal(p, p + 1, ty_uint); p += cur->len; continue; } // UTF-32 character literal if (p[1] == '\'') { cur = cur->next = read_char_literal(p, p + 1, ty_uint); p += cur->len; continue; } default: break; } // Identifier or keyword int ident_len = read_ident(p); if (ident_len) { cur = cur->next = new_token(TK_IDENT, p, p + ident_len); p += cur->len; continue; } // Punctuators int punct_len = read_punct(p); if (punct_len) { cur = cur->next = new_token(TK_PUNCT, p, p + punct_len); p += cur->len; continue; } error_at(p, "invalid token"); } } File **get_input_files(void) { return input_files; } File *new_file(char *name, int file_no, char *contents) { File *file = calloc(1, sizeof(File)); file->name = name; file->display_name = name; file->file_no = file_no; file->contents = contents; return file; } static uint32_t read_universal_char(char *p, int len) { uint32_t c = 0; for (int i = 0; i < len; i++) { if (!isxdigit(p[i])) return 0; c = (c << 4) | hextoint(p[i]); } return c; } // Replace \u or \U escape sequences with corresponding UTF-8 bytes. static void convert_universal_chars(char *p) { uint32_t c; char *q = p; for (;;) { #if defined(__GNUC__) && defined(__x86_64__) && !defined(__chibicc__) typedef char xmm_u __attribute__((__vector_size__(16), __aligned__(1))); typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16))); if (!((uintptr_t)p & 15)) { xmm_t v; unsigned m; xmm_t z = {0}; xmm_t s = {'\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\'}; for (;;) { v = *(const xmm_t *)p; m = __builtin_ia32_pmovmskb128((v == z) | (v == s)); if (!m) { *(xmm_u *)q = v; p += 16; q += 16; } else { m = _bsf(m); memmove(q, p, m); p += m; q += m; break; } } } #endif if (p[0]) { if (p[0] == '\\') { if (p[1] == 'u') { if ((c = read_universal_char(p + 2, 4))) { p += 6; q += encode_utf8(q, c); continue; } } else if (p[1] == 'U') { if ((c = read_universal_char(p + 2, 8))) { p += 10; q += encode_utf8(q, c); continue; } } else if (p[0] == '\\') { *q++ = *p++; } } *q++ = *p++; } else { break; } } *q = '\0'; } Token *tokenize_file(char *path) { char *p = read_file(path); if (!p) return NULL; p = skip_bom(p); canonicalize_newline(p); remove_backslash_newline(p); convert_universal_chars(p); // Save the filename for assembler .file directive. static int file_no; File *file = new_file(path, file_no + 1, p); // Save the filename for assembler .file directive. input_files = realloc(input_files, sizeof(char *) * (file_no + 2)); input_files[file_no] = file; input_files[file_no + 1] = NULL; file_no++; /* ftrace_install(); */ return tokenize(file); }
19,674
736
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/unicode.c
#include "third_party/chibicc/chibicc.h" // Encode a given character in UTF-8. int encode_utf8(char *buf, uint32_t c) { if (c <= 0x7F) { buf[0] = c; return 1; } if (c <= 0x7FF) { buf[0] = 0b11000000 | (c >> 6); buf[1] = 0b10000000 | (c & 0b00111111); return 2; } if (c <= 0xFFFF) { buf[0] = 0b11100000 | (c >> 12); buf[1] = 0b10000000 | ((c >> 6) & 0b00111111); buf[2] = 0b10000000 | (c & 0b00111111); return 3; } buf[0] = 0b11110000 | (c >> 18); buf[1] = 0b10000000 | ((c >> 12) & 0b00111111); buf[2] = 0b10000000 | ((c >> 6) & 0b00111111); buf[3] = 0b10000000 | (c & 0b00111111); return 4; } // Read a UTF-8-encoded Unicode code point from a source file. // We assume that source files are always in UTF-8. // // UTF-8 is a variable-width encoding in which one code point is // encoded in one to four bytes. One byte UTF-8 code points are // identical to ASCII. Non-ASCII characters are encoded using more // than one byte. uint32_t decode_utf8(char **new_pos, char *p) { if ((unsigned char)*p < 128) { *new_pos = p + 1; return *p; } char *start = p; int len; uint32_t c; if ((unsigned char)*p >= 0b11110000) { len = 4; c = *p & 0b111; } else if ((unsigned char)*p >= 0b11100000) { len = 3; c = *p & 0b1111; } else if ((unsigned char)*p >= 0b11000000) { len = 2; c = *p & 0b11111; } else { error_at(start, "invalid UTF-8 sequence"); } for (int i = 1; i < len; i++) { if ((unsigned char)p[i] >> 6 != 0b10) error_at(start, "invalid UTF-8 sequence"); c = (c << 6) | (p[i] & 0b111111); } *new_pos = p + len; return c; } static bool in_range(const uint32_t *range, uint32_t c) { for (int i = 0; range[i] != -1; i += 2) { if (range[i] <= c && c <= range[i + 1]) { return true; } } return false; } // [https://www.sigbus.info/n1570#D] C11 allows not only ASCII but // some multibyte characters in certan Unicode ranges to be used in an // identifier. // // This function returns true if a given character is acceptable as // the first character of an identifier. // // For example, ¾ (U+00BE) is a valid identifier because characters in // 0x00BE-0x00C0 are allowed, while neither ⟘ (U+27D8) nor ' ' // (U+3000, full-width space) are allowed because they are out of range. bool is_ident1(uint32_t c) { static const uint32_t range[] = { 0x00A8, 0x00A8, 0x00AA, 0x00AA, 0x00AD, 0x00AD, 0x00AF, 0x00AF, 0x00B2, 0x00B5, 0x00B7, 0x00BA, 0x00BC, 0x00BE, 0x00C0, 0x00D6, 0x00D8, 0x00F6, 0x00F8, 0x00FF, 0x0100, 0x02FF, 0x0370, 0x167F, 0x1681, 0x180D, 0x180F, 0x1DBF, 0x1E00, 0x1FFF, 0x200B, 0x200D, 0x202A, 0x202E, 0x203F, 0x2040, 0x2054, 0x2054, 0x2060, 0x206F, 0x2070, 0x20CF, 0x2100, 0x218F, 0x2460, 0x24FF, 0x2776, 0x2793, 0x2C00, 0x2DFF, 0x2E80, 0x2FFF, 0x3004, 0x3007, 0x3021, 0x302F, 0x3031, 0x303F, 0x3040, 0xD7FF, 0xF900, 0xFD3D, 0xFD40, 0xFDCF, 0xFDF0, 0xFE1F, 0xFE30, 0xFE44, 0xFE47, 0xFFFD, 0x10000, 0x1FFFD, 0x20000, 0x2FFFD, 0x30000, 0x3FFFD, 0x40000, 0x4FFFD, 0x50000, 0x5FFFD, 0x60000, 0x6FFFD, 0x70000, 0x7FFFD, 0x80000, 0x8FFFD, 0x90000, 0x9FFFD, 0xA0000, 0xAFFFD, 0xB0000, 0xBFFFD, 0xC0000, 0xCFFFD, 0xD0000, 0xDFFFD, 0xE0000, 0xEFFFD, -1, }; if (c < 128) { return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_' || c == '$'; } else { return in_range(range, c); } } // Returns true if a given character is acceptable as a non-first // character of an identifier. bool is_ident2(uint32_t c) { static const uint32_t range[] = { 0x0300, 0x036F, 0x1DC0, 0x1DFF, 0x20D0, 0x20FF, 0xFE20, 0xFE2F, -1, }; if (is_ident1(c)) return true; if (c < 128) { return '0' <= c && c <= '9'; } else { return in_range(range, c); } } // Returns the number of columns needed to display a given // string in a fixed-width font. int display_width(char *p, int len) { char *start = p; int w = 0; while (p - start < len) { uint32_t c = decode_utf8(&p, p); w += wcwidth(c); } return w; }
4,160
131
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/kw.gperf
%{ #include "libc/str/str.h" #include "third_party/chibicc/kw.h" %} %compare-strncmp %language=ANSI-C %readonly-tables %struct-type %define lookup-function-name LookupKw struct thatispacked KwSlot { char *name; unsigned char code; }; %% if, KW_IF struct, KW_STRUCT return, KW_RETURN case, KW_CASE static, KW_STATIC void, KW_VOID char, KW_CHAR else, KW_ELSE for, KW_FOR do, KW_DO sizeof, KW_SIZEOF unsigned, KW_UNSIGNED long, KW_LONG while, KW_WHILE union, KW_UNION switch, KW_SWITCH double, KW_DOUBLE const, KW_CONST float, KW_FLOAT short, KW_SHORT signed, KW_SIGNED break, KW_BREAK enum, KW_ENUM continue, KW_CONTINUE include, KW_INCLUDE ifdef, KW_IFDEF ifndef, KW_IFNDEF define, KW_DEFINE defined, KW_DEFINED asm, KW_ASM default, KW_DEFAULT auto, KW_AUTO register, KW_REGISTER __attribute__, KW___ATTRIBUTE__ _Noreturn, KW__NORETURN elif, KW_ELIF endif, KW_ENDIF error, KW_ERROR extern, KW_EXTERN goto, KW_GOTO include_next, KW_INCLUDE_NEXT inline, KW_INLINE int, KW_INT line, KW_LINE pragma, KW_PRAGMA restrict, KW_RESTRICT strchr, KW_STRCHR strlen, KW_STRLEN strpbrk, KW_STRPBRK strstr, KW_STRSTR typedef, KW_TYPEDEF typeof, KW_TYPEOF undef, KW_UNDEF volatile, KW_VOLATILE _Alignas, KW__ALIGNAS _Alignof, KW__ALIGNOF _Atomic, KW__ATOMIC _Bool, KW__BOOL _Generic, KW__GENERIC _Static_assert, KW__STATIC_ASSERT _Thread_local, KW__THREAD_LOCAL __VA_OPT__, KW___VA_OPT__ __alignof__, KW___ALIGNOF__ __asm__, KW___ASM__ __inline, KW_INLINE __int128, KW___INT128 __restrict, KW_RESTRICT __restrict__, KW_RESTRICT __thread, KW__THREAD_LOCAL __typeof, KW_TYPEOF __builtin_add_overflow, KW___BUILTIN_ADD_OVERFLOW __builtin_assume_aligned, KW___BUILTIN_ASSUME_ALIGNED __builtin_constant_p, KW___BUILTIN_CONSTANT_P __builtin_expect, KW___BUILTIN_EXPECT __builtin_ffs, KW___BUILTIN_FFS __builtin_ffsl, KW___BUILTIN_FFSL __builtin_ffsll, KW___BUILTIN_FFSLL __builtin_fpclassify, KW___BUILTIN_FPCLASSIFY __builtin_mul_overflow, KW___BUILTIN_MUL_OVERFLOW __builtin_neg_overflow, KW___BUILTIN_NEG_OVERFLOW __builtin_offsetof, KW___BUILTIN_OFFSETOF __builtin_popcount, KW___BUILTIN_POPCOUNT __builtin_popcountl, KW___BUILTIN_POPCOUNTL __builtin_popcountll, KW___BUILTIN_POPCOUNTLL __builtin_reg_class, KW___BUILTIN_REG_CLASS __builtin_strchr, KW___BUILTIN_STRCHR __builtin_strlen, KW___BUILTIN_STRLEN __builtin_strpbrk, KW___BUILTIN_STRPBRK __builtin_strstr, KW___BUILTIN_STRSTR __builtin_sub_overflow, KW___BUILTIN_SUB_OVERFLOW __builtin_types_compatible_p, KW___BUILTIN_TYPES_COMPATIBLE_P "(", KW_LP ")", KW_RP "{", KW_LB "}", KW_RB "+", KW_PLUS "-", KW_MINUS "&", KW_AMP "*", KW_STAR "!", KW_EXCLAIM "~", KW_TILDE "++", KW_INCREMENT "--", KW_DECREMENT "&&", KW_LOGAND "||", KW_LOGOR "->", KW_ARROW ".", KW_DOT __atomic_load, KW___ATOMIC_LOAD __atomic_load_n, KW___ATOMIC_LOAD_N __atomic_store, KW___ATOMIC_STORE __atomic_store_n, KW___ATOMIC_STORE_N __atomic_clear, KW___ATOMIC_CLEAR __atomic_fetch_add, KW___ATOMIC_FETCH_ADD __atomic_fetch_sub, KW___ATOMIC_FETCH_SUB __atomic_fetch_and, KW___ATOMIC_FETCH_AND __atomic_fetch_xor, KW___ATOMIC_FETCH_XOR __atomic_fetch_or, KW___ATOMIC_FETCH_OR __atomic_test_and_set, KW___ATOMIC_TEST_AND_SET __sync_lock_test_and_set, KW___SYNC_LOCK_TEST_AND_SET __sync_lock_release, KW___SYNC_LOCK_RELEASE __builtin_ia32_movntdq, KW___BUILTIN_IA32_MOVNTDQ __builtin_ia32_pmovmskb128, KW___BUILTIN_IA32_PMOVMSKB128 __atomic_compare_exchange_n, KW___ATOMIC_COMPARE_EXCHANGE_N __atomic_exchange_n, KW___ATOMIC_EXCHANGE_N
6,186
136
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/fpclassify.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/chibicc/chibicc.h" /** * @fileoverview __builtin_fpclassify() implementation */ #define FPCLASSIFY_FLOAT \ "\tmovaps\t%%xmm0,%%xmm1\n\ \tmov\t$0x7fffffff,%%eax\n\ \tmovd\t%%eax,%%xmm2\n\ \tandps\t%%xmm2,%%xmm1\n\ \tmov\t$%d,%%eax\n\ \tucomiss\t%%xmm1,%%xmm1\n\ \tjp\t9f\n\ \tmov\t$0x7f7fffff,%%edi\n\ \tmovd\t%%edi,%%xmm2\n\ \tucomiss\t%%xmm2,%%xmm1\n\ \tja\t2f\n\ \tmov\t$0x00800000,%%edi\n\ \tmovd\t%%edi,%%xmm2\n\ \tucomiss\t%%xmm2,%%xmm1\n\ \tjnb\t3f\n\ \txorps\t%%xmm1,%%xmm1\n\ \tucomiss\t%%xmm1,%%xmm0\n\ \tjp\t1f\n\ \tmovl\t$%d,%%eax\n\ \tje\t9f\n\ 1:\tmovl\t$%d,%%eax\n\ \tjmp\t9f\n\ 2:\tmovl\t$%d,%%eax\n\ \tjmp\t9f\n\ 3:\tmovl\t$%d,%%eax\n\ 9:" #define FPCLASSIFY_DOUBLE \ "\tmovapd\t%%xmm0,%%xmm1\n\ \tmov\t$0x7fffffffffffffff,%%rax\n\ \tmovq\t%%rax,%%xmm2\n\ \tandps\t%%xmm2,%%xmm1\n\ \tmov\t$%d,%%eax\n\ \tucomisd\t%%xmm1,%%xmm1\n\ \tjp\t9f\n\ \tmov\t$0x7fefffffffffffff,%%rdi\n\ \tmovq\t%%rdi,%%xmm2\n\ \tucomisd\t%%xmm2,%%xmm1\n\ \tja\t2f\n\ \tmov\t$0x0010000000000000,%%rdi\n\ \tmovq\t%%rdi,%%xmm2\n\ \tucomisd\t%%xmm2,%%xmm1\n\ \tjnb\t3f\n\ \txorps\t%%xmm1,%%xmm1\n\ \tucomisd\t%%xmm1,%%xmm0\n\ \tjp\t1f\n\ \tmovl\t$%d,%%eax\n\ \tje\t9f\n\ 1:\tmovl\t$%d,%%eax\n\ \tjmp\t9f\n\ 2:\tmovl\t$%d,%%eax\n\ \tjmp\t9f\n\ 3:\tmovl\t$%d,%%eax\n\ 9:" #define FPCLASSIFY_LDOUBLE \ "\tmov\t$%d,%%eax\n\ \tfld\t%%st\n\ \tfabs\n\ \tfucomi\t%%st,%%st\n\ \tjp\t6f\n\ \tpush\t$0x7ffe\n\ \tpush\t$-1\n\ \tfldt\t(%%rsp)\n\ \tadd\t$16,%%rsp\n\ \tfxch\n\ \tmov\t$%d,%%eax\n\ \tfucomi\n\ \tfstp\t%%st(1)\n\ \tja\t7f\n\ \tmov\t$1,%%edi\n\ \tpush\t%%rdi\n\ \tror\t%%rdi\n\ \tpush\t%%rdi\n\ \tfldt\t(%%rsp)\n\ \tadd\t$16,%%rsp\n\ \tfxch\n\ \tmov\t$%d,%%eax\n\ \tfucomip\n\ \tfstp\t%%st\n\ \tjnb\t8f\n\ \tfldz\n\ \tfxch\n\ \tfucomip\n\ \tfstp\t%%st\n\ \tjp\t5f\n\ \tmov\t$%d,%%eax\n\ \tje\t9f\n\ 5:\tmov\t$%d,%%eax\n\ \tjmp\t9f\n\ 6:\tfstp\t%%st\n\ \tfstp\t%%st\n\ \tjmp\t9f\n\ 7:\tfstp\t%%st\n\ \tfstp\t%%st\n\ \tjmp\t9f\n\ 8:\tfstp\t%%st\n\ 9:" void gen_fpclassify(FpClassify *fpc) { int fpnan = fpc->args[0]; int fpinf = fpc->args[1]; int fpnorm = fpc->args[2]; int fpsubnorm = fpc->args[3]; int fpzero = fpc->args[4]; gen_expr(fpc->node); switch (fpc->node->ty->kind) { case TY_FLOAT: println(FPCLASSIFY_FLOAT, fpnan, fpzero, fpsubnorm, fpinf, fpnorm); break; case TY_DOUBLE: println(FPCLASSIFY_DOUBLE, fpnan, fpzero, fpsubnorm, fpinf, fpnorm); break; case TY_LDOUBLE: println(FPCLASSIFY_LDOUBLE, fpnan, fpinf, fpnorm, fpzero, fpsubnorm); break; default: UNREACHABLE(); } }
4,413
146
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/asm.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/intrin/bsf.h" #include "libc/intrin/bsr.h" #include "third_party/chibicc/chibicc.h" #define PRECIOUS 0b1111000000101000 // bx,bp,r12-r15 StaticAsm *staticasms; static const char kGreg[4][16][5] = { {"al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil", "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b"}, {"ax", "cx", "dx", "bx", "sp", "bp", "si", "di", "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w"}, {"eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d"}, {"rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"}, }; static void DecodeAsmConstraints(AsmOperand *op) { int i; char c; for (i = 0;;) { switch ((c = op->str[i++])) { case '\0': case ',': // alternative group return; // todo: read combos case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': // reference case '=': // output case '+': // output and input op->flow = c; break; case 'm': // memory case 'o': // memory offsetable op->type |= kAsmMem; op->regmask |= 0b1111111111111111; break; case 'i': // int literal, c/asm constexpr, ld embedding case 'n': // integer literal or compiler constexpr? case 's': // integer constexpr but not literal (or known at link time?) case 'M': // i∊[0,3] for index scaling, e.g. "mov\t(?,?,1<<%0),?" case 'I': // i∊[0,31] 5 bits for 32-bit shifts case 'J': // i∊[0,63] 6 bits for 64-bit shifts case 'N': // i∊[0,255] in/out immediate byte case 'K': // i∊[-128,127] signed byte integer case 'e': // i∊[-2^31,2^31) for sign-extending case 'Z': // i∊[0,2³²) for zero-extending case 'L': // i∊{0xFF,0xFFFF,0xFFFFFFFF} op->type |= kAsmImm; break; case 'a': // ax op->regmask |= 0b0000000000000001; op->type |= kAsmReg; break; case 'c': // cx op->regmask |= 0b0000000000000010; op->type |= kAsmReg; break; case 'd': // dx op->regmask |= 0b0000000000000100; op->type |= kAsmReg; break; case 'b': // bx op->regmask |= 0b0000000000001000; op->type |= kAsmReg; break; case 'S': // si op->regmask |= 0b0000000001000000; op->type |= kAsmReg; break; case 'D': // di op->regmask |= 0b0000000010000000; op->type |= kAsmReg; break; case 'r': // general register op->regmask |= 0b1111111111111111; op->type |= kAsmReg; break; case 'q': // greg lo-byte accessible op->regmask |= 0b1111111111111111; op->type |= kAsmReg; break; case 'Q': // greg hi-byte accessible op->regmask |= 0b0000000000001111; op->type |= kAsmReg; break; case 'U': // greg call-clobbered op->regmask |= 0b0000111111000111; op->type |= kAsmReg; break; case 'R': // greg all models op->regmask |= 0b0000000011111111; op->type |= kAsmReg; break; case 'l': // index register op->regmask |= 0b1111111111101111; op->type |= kAsmReg; break; case 'y': // mmx op->type |= kAsmMmx; op->regmask |= 0b0000000011111111; break; case 'x': // xmm op->type |= kAsmXmm; op->regmask |= 0b1111111111111111; break; case 'g': // rmi op->type |= kAsmImm | kAsmMem | kAsmReg; op->regmask |= 0b1111111111111111; break; case 'X': // anything op->type |= kAsmImm | kAsmMem | kAsmReg | kAsmXmm | kAsmFpu | kAsmRaw; op->regmask |= 0b1111111111111111; op->x87mask |= 0b11111111; break; case 't': // %st op->type |= kAsmFpu; op->x87mask |= 0b00000001; break; case 'u': // %st(1) op->type |= kAsmFpu; op->x87mask |= 0b00000010; break; case 'f': // %st(0..7) op->type |= kAsmFpu; op->x87mask |= 0b11111111; break; case 'A': // ax+dx error_tok(op->tok, "ax dx constraint not implemented"); case '@': // flags if (op->flow != '=' || strlen(op->str + i) < 3 || (op->str[i] != 'c' || op->str[i + 1] != 'c')) { error_tok(op->tok, "invalid flag constraint"); } if (!is_integer(op->node->ty) || op->node->ty->size != 1) { error_tok(op->node->tok, "invalid output flag type"); } op->type = kAsmFlag; op->predicate = i + 2; return; default: break; } } } static bool IsLvalue(AsmOperand *op) { switch (op->node->kind) { case ND_VAR: case ND_DEREF: case ND_MEMBER: case ND_VLA_PTR: return true; default: return false; } } static bool CanUseReg(Node **n) { if ((*n)->ty->kind == TY_ARRAY) { *n = new_cast(*n, pointer_to((*n)->ty->base)); return true; } return is_integer((*n)->ty) || (*n)->ty->kind == TY_PTR; } static bool CanUseXmm(Node *n) { return n->ty->vector_size == 16 || n->ty->kind == TY_FLOAT || n->ty->kind == TY_DOUBLE || n->ty->kind == TY_PTR || (n->ty->kind == TY_ARRAY && n->ty->size == 16); } static bool CanUseMmx(Node *n) { return n->ty->kind == TY_FLOAT || n->ty->kind == TY_DOUBLE || n->ty->kind == TY_PTR || (n->ty->kind == TY_ARRAY && n->ty->size == 8); } static int PickAsmReferenceType(AsmOperand *op, AsmOperand *ref) { switch (ref->type) { case kAsmImm: case kAsmMem: error_tok(op->tok, "bad reference"); case kAsmReg: if (!CanUseReg(&op->node)) { error_tok(op->tok, "expected integral expression"); } op->regmask = 0; return ref->type; case kAsmXmm: if (!CanUseXmm(op->node)) { error_tok(op->tok, "expected xmm expression"); } return ref->type; case kAsmFpu: if (op->node->ty->kind != TY_LDOUBLE) { error_tok(op->tok, "expected long double expression"); } op->x87mask = 0; return ref->type; default: UNREACHABLE(); } } static int PickAsmOperandType(Asm *a, AsmOperand *op) { if (op->flow == '=' || op->flow == '+') { op->type &= ~kAsmImm; if (!IsLvalue(op)) error_tok(op->tok, "lvalue required"); } if ((op->type & kAsmImm) && (is_const_expr(op->node) || // TODO(jart): confirm this is what we want op->node->ty->kind == TY_FUNC)) { op->val = eval2(op->node, &op->label); return kAsmImm; } if ((op->type & kAsmMem) && op->node->ty->kind != TY_VOID) return kAsmMem; if ((op->type & kAsmFpu) && op->node->ty->kind == TY_LDOUBLE) return kAsmFpu; if ((op->type & kAsmXmm) && CanUseXmm(op->node)) return kAsmXmm; if ((op->type & kAsmMmx) && CanUseMmx(op->node)) return kAsmMmx; if ((op->type & kAsmReg) && CanUseReg(&op->node)) return kAsmReg; if (op->type & kAsmFlag) return kAsmFlag; if (op->type & kAsmRaw) return kAsmRaw; error_tok(op->tok, "constraint mismatch"); } static Token *ParseAsmOperand(Asm *a, AsmOperand *op, Token *tok) { int i; op->tok = tok; op->str = ConsumeStringLiteral(&tok, tok); tok = skip(tok, '('); op->node = expr(&tok, tok); add_type(op->node); DecodeAsmConstraints(op); if (isdigit(op->flow)) { if ((i = op->flow - '0') >= a->n) error_tok(op->tok, "bad reference"); op->type = PickAsmReferenceType(op, a->ops + i); } else { op->type = PickAsmOperandType(a, op); } return skip(tok, ')'); } static Token *ParseAsmOperands(Asm *a, Token *tok) { if (EQUAL(tok, ":")) return tok; for (;;) { if (a->n == ARRAYLEN(a->ops)) { error_tok(tok, "too many asm operands"); } tok = ParseAsmOperand(a, &a->ops[a->n], tok); ++a->n; if (!EQUAL(tok, ",")) break; tok = skip(tok, ','); } return tok; } static void CouldNotAllocateRegister(AsmOperand *op, const char *kind) { error_tok(op->tok, "could not allocate %s register", kind); } static void PickAsmRegisters(Asm *a) { int i, j, m, pick, regset, xmmset, x87sts; regset = 0b1111111111001111; // exclude bx,sp,bp xmmset = 0b1111111111111111; x87sts = 0b0000000011111111; regset ^= regset & a->regclob; // don't allocate from clobber list xmmset ^= xmmset & a->xmmclob; x87sts ^= x87sts & a->x87clob; for (j = 1; j <= 16; ++j) { // iterate from most to least restrictive for (i = 0; i < a->n; ++i) { switch (a->ops[i].type) { case kAsmMem: case kAsmReg: if (!(m = a->ops[i].regmask)) break; if (popcnt(m) != j) break; if (!(m &= regset)) CouldNotAllocateRegister(&a->ops[i], "rm"); pick = 1 << (a->ops[i].reg = _bsf(m)); if (pick & PRECIOUS) a->regclob |= pick; regset &= ~pick; a->ops[i].regmask = 0; break; case kAsmXmm: if (!(m = a->ops[i].regmask)) break; if (!(m &= xmmset)) CouldNotAllocateRegister(&a->ops[i], "xmm"); xmmset &= ~(1 << (a->ops[i].reg = _bsf(m))); a->ops[i].regmask = 0; break; case kAsmFpu: if (!(m = a->ops[i].x87mask)) break; if (popcnt(m) != j) break; if (!(m &= x87sts)) CouldNotAllocateRegister(&a->ops[i], "fpu"); x87sts &= ~(1 << (a->ops[i].reg = _bsf(m))); a->ops[i].x87mask = 0; break; default: a->ops[i].regmask = 0; a->ops[i].x87mask = 0; break; } } } } static void MarkUsedAsmOperands(Asm *a) { char c, *p; if (!a->isgnu) return; for (p = a->str; (c = *p++);) { if (c == '%') { if (!(c = *p++)) error_tok(a->tok, "unexpected nul"); if (c == '%') continue; if (!isdigit(c)) { if (!(c = *p++)) error_tok(a->tok, "unexpected nul"); if (!isdigit(c)) { error_tok(a->tok, "bad asm specifier"); } } a->ops[c - '0'].isused = true; } } } static int GetIndexOfRegisterName(const char *s) { int i, j; for (i = 0; i < 16; ++i) { for (j = 0; j < 4; ++j) { if (!strcmp(s, kGreg[j][i])) { return i; } } } return -1; } static Token *ParseAsmClobbers(Asm *a, Token *tok) { int i; char *s; Token *stok; for (;;) { stok = tok; s = ConsumeStringLiteral(&tok, tok); if (*s == '%') ++s; if (!strcmp(s, "cc")) { a->flagclob = true; } else if ((i = GetIndexOfRegisterName(s)) != -1) { a->regclob |= 1 << i; } else if (_startswith(s, "xmm") && isdigit(s[3]) && (!s[4] || isdigit(s[4]))) { i = s[3] - '0'; if (s[4]) { i *= 10; i += s[4] - '0'; } i &= 15; a->xmmclob |= 1 << i; } else if (!strcmp(s, "st")) { a->x87clob |= 1; } else if (_startswith(s, "st(") && isdigit(s[3]) && s[4] == ')') { i = s[3] - '0'; i &= 7; a->x87clob |= 1 << i; } else if (!strcmp(s, "memory")) { /* do nothing */ } else { error_tok(stok, "unknown clobber register"); } if (!EQUAL(tok, ",")) break; tok = skip(tok, ','); } return tok; } // parses ansi c11 asm statement officially defined as follows // // asm-stmt = "asm" ("volatile" | "inline")* "(" string-literal ")" // // gnu c defines a notation for inputs, outputs, and clobbers, e.g. // // asm("foo %1,%0" // : "=r"(x) // : "r"(x) // : "cc"); // Asm *asm_stmt(Token **rest, Token *tok) { Asm *a = calloc(1, sizeof(Asm)); tok = tok->next; while (EQUAL(tok, "volatile") || EQUAL(tok, "inline")) tok = tok->next; tok = skip(tok, '('); a->tok = tok; a->str = ConsumeStringLiteral(&tok, tok); if (!EQUAL(tok, ")")) { a->isgnu = true; tok = skip(tok, ':'); tok = ParseAsmOperands(a, tok); if (!EQUAL(tok, ")")) { tok = skip(tok, ':'); tok = ParseAsmOperands(a, tok); if (!EQUAL(tok, ")")) { tok = skip(tok, ':'); tok = ParseAsmClobbers(a, tok); } } } PickAsmRegisters(a); MarkUsedAsmOperands(a); *rest = skip(tok, ')'); return a; } static void PrintAsmConstant(AsmOperand *op) { if (op->label) { fprintf(output_stream, "%s%+ld", *op->label, op->val); } else { fprintf(output_stream, "%ld", op->val); } } static void EmitAsmSpecifier(AsmOperand *op, int q, int z) { if (!q) { switch (op->type) { case kAsmImm: fputc('$', output_stream); PrintAsmConstant(op); break; case kAsmMem: fprintf(output_stream, "(%%%s)", kGreg[3][op->reg]); break; case kAsmReg: fprintf(output_stream, "%%%s", kGreg[z][op->reg]); break; case kAsmXmm: fprintf(output_stream, "%%xmm%d", op->reg); break; case kAsmFpu: fprintf(output_stream, "%%st(%d)", op->reg); break; case kAsmRaw: fprintf(output_stream, "%.*s", op->node->tok->len, op->node->tok->loc); break; default: UNREACHABLE(); } } else { switch (q) { case 'h': // hi byte fprintf(output_stream, "%%%ch", "acdb"[op->reg]); break; case 'b': // lo byte fprintf(output_stream, "%%%s", kGreg[0][op->reg]); break; case 'w': // word fprintf(output_stream, "%%%s", kGreg[1][op->reg]); break; case 'k': // dword fprintf(output_stream, "%%%s", kGreg[2][op->reg]); break; case 'q': // qword fprintf(output_stream, "%%%s", kGreg[3][op->reg]); break; case 'z': // print suffix fprintf(output_stream, "%c", "bwlq"[z]); break; case 'p': // print raw fprintf(output_stream, "%.*s", op->node->tok->len, op->node->tok->loc); break; case 'a': // print address PrintAsmConstant(op); break; case 'c': // print constant w/o punctuation PrintAsmConstant(op); break; case 'P': // print w/ @plt PrintAsmConstant(op); fprintf(output_stream, "@plt"); break; case 'l': // print label w/o punctuation if (!op->label) { error_tok(op->tok, "qualifier expected label"); } fprintf(output_stream, "%s", *op->label); break; case 'V': // print register w/o punctuation if (op->type != kAsmReg) { error_tok(op->tok, "qualifier expected register"); } fprintf(output_stream, "%s", kGreg[z][op->reg]); break; default: error_tok(op->tok, "bad asm qualifier %%%`'c", q); } } } static char *HandleAsmSpecifier(Asm *a, char *p) { int c, i, q, z; if (!(c = *p++)) error_tok(a->tok, "unexpected nul"); if (c == '%') { fputc('%', output_stream); return p; } if (c == '=') { fprintf(output_stream, "%d", count()); return p; } if (isdigit(c)) { q = '\0'; } else { q = c; if (!(c = *p++)) error_tok(a->tok, "unexpected nul"); if (!isdigit(c)) { error_tok(a->tok, "bad asm specifier at offset %d", p - a->str); } } if ((i = c - '0') >= a->n) { error_tok(a->tok, "bad asm reference at offset %d", p - a->str); } z = _bsr(a->ops[i].node->ty->size); if (z > 3 && a->ops[i].type == kAsmReg) { error_tok(a->tok, "bad asm op size"); } EmitAsmSpecifier(&a->ops[i], q, z); return p; } static void EmitAsmText(Asm *a) { char c, *p; if (*a->str) { if (a->isgnu) { flushln(); fprintf(output_stream, "\t"); for (p = a->str;;) { switch ((c = *p++)) { case '\0': fputc('\n', output_stream); return; case '%': p = HandleAsmSpecifier(a, p); break; default: fputc(c, output_stream); break; } } } else { println("\t%s", a->str); } } } static void PushAsmInput(AsmOperand *op) { gen_expr(op->node); push(); } static void PushAsmInputs(Asm *a) { int i; for (i = 0; i < a->n; ++i) { if (a->ops[i].flow == '=') continue; switch (a->ops[i].type) { case kAsmReg: PushAsmInput(&a->ops[i]); break; case kAsmMem: if (a->ops[i].isused) { PushAsmInput(&a->ops[i]); } break; case kAsmXmm: gen_expr(a->ops[i].node); println("\tsub\t$16,%%rsp"); switch (a->ops[i].node->ty->kind) { case TY_FLOAT: case TY_DOUBLE: println("\tmovdqu\t%%xmm0,(%%rsp)"); break; default: println("\tmovdqu\t(%%rax),%%xmm0"); println("\tmovdqu\t%%xmm0,(%%rsp)"); break; } break; case kAsmMmx: gen_expr(a->ops[i].node); println("\tsub\t$8,%%rsp"); switch (a->ops[i].node->ty->kind) { case TY_FLOAT: case TY_DOUBLE: println("\tmovq\t%%mm0,(%%rsp)"); break; default: println("\tmovq\t(%%rax),%%mm0"); println("\tmovq\t%%mm0,(%%rsp)"); break; } break; case kAsmFpu: /* TODO: How does this work in non-simple case? */ gen_expr(a->ops[i].node); println("\tsub\t$16,%%rsp"); println("\tfstpt\t(%%rsp)"); break; default: break; } } } static void PopAsmInput(Asm *a, AsmOperand *op) { if (isdigit(op->flow)) { popreg(kGreg[3][a->ops[op->flow - '0'].reg]); } else { popreg(kGreg[3][op->reg]); } } static void PopAsmInputs(Asm *a) { int i; for (i = a->n; i--;) { if (a->ops[i].flow == '=') continue; switch (a->ops[i].type) { case kAsmReg: PopAsmInput(a, &a->ops[i]); break; case kAsmMem: if (a->ops[i].isused) { PopAsmInput(a, &a->ops[i]); } break; case kAsmXmm: println("\tmovdqu\t(%%rsp),%%xmm%d", a->ops[i].reg); println("\tadd\t$16,%%rsp"); break; case kAsmMmx: println("\tmovq\t(%%rsp),%%mm%d", a->ops[i].reg); println("\tadd\t$8,%%rsp"); break; case kAsmFpu: /* TODO: How does this work in non-simple case? */ println("\tfldt\t(%%rsp)"); println("\tadd\t$16,%%rsp"); break; default: break; } } } static void StoreAsmOutputs(Asm *a) { int i, z, x0, x1; for (i = 0; i < a->n; ++i) { if (a->ops[i].flow == '=' || a->ops[i].flow == '+') { switch (a->ops[i].type) { case kAsmFlag: gen_addr(a->ops[i].node); println("\tset%s\t(%%rax)", a->ops[i].str + a->ops[i].predicate); break; case kAsmReg: z = _bsr(a->ops[i].node->ty->size); if (a->ops[i].reg) { gen_addr(a->ops[i].node); if (z > 3) error_tok(a->tok, "bad asm out size"); println("\tmov\t%%%s,(%%rax)", kGreg[z][a->ops[i].reg]); } else { println("\tpush\t%%rbx"); println("\tmov\t%%rax,%%rbx"); gen_addr(a->ops[i].node); println("\tmov\t%%%s,(%%rax)", kGreg[z][3]); println("\tpop\t%%rbx"); } break; case kAsmXmm: gen_addr(a->ops[i].node); switch (a->ops[i].node->ty->kind) { case TY_FLOAT: println("\tmovss\t%%xmm%d,(%%rax)", a->ops[i].reg); break; case TY_DOUBLE: println("\tmovsd\t%%xmm%d,(%%rax)", a->ops[i].reg); break; default: println("\tmovdqu\t%%xmm%d,(%%rax)", a->ops[i].reg); break; } break; case kAsmMmx: gen_addr(a->ops[i].node); switch (a->ops[i].node->ty->kind) { case TY_FLOAT: println("\tmovss\t%%mm%d,(%%rax)", a->ops[i].reg); break; case TY_DOUBLE: println("\tmovsd\t%%mm%d,(%%rax)", a->ops[i].reg); break; default: println("\tmovq\t%%mm%d,(%%rax)", a->ops[i].reg); break; } break; case kAsmFpu: /* TODO: How does this work in non-simple case? */ gen_addr(a->ops[i].node); println("\tfstpt\t(%%rax)"); break; default: break; } } } } static void PushClobbers(Asm *a) { int i, regs = a->regclob & PRECIOUS; while (regs) { i = _bsf(regs); pushreg(kGreg[3][i]); regs &= ~(1 << i); } } static void PopClobbers(Asm *a) { int i, regs = a->regclob & PRECIOUS; while (regs) { i = _bsr(regs); popreg(kGreg[3][i]); regs &= ~(1 << i); } } // generates shocking horrible code for parsed asm statement void gen_asm(Asm *a) { PushAsmInputs(a); print_loc(a->tok->file->file_no, a->tok->line_no); PopAsmInputs(a); PushClobbers(a); EmitAsmText(a); StoreAsmOutputs(a); PopClobbers(a); }
23,035
762
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/control_test.c
#include "third_party/chibicc/test/test.h" /* * This is a block comment. */ int main() { ASSERT(3, ({ int x; if (0) x = 2; else x = 3; x; })); ASSERT(3, ({ int x; if (1 - 1) x = 2; else x = 3; x; })); ASSERT(2, ({ int x; if (1) x = 2; else x = 3; x; })); ASSERT(2, ({ int x; if (2 - 1) x = 2; else x = 3; x; })); ASSERT(55, ({ int i = 0; int j = 0; for (i = 0; i <= 10; i = i + 1) j = i + j; j; })); ASSERT(10, ({ int i = 0; while (i < 10) i = i + 1; i; })); ASSERT(3, ({ 1; { 2; } 3; })); ASSERT(5, ({ ; ; ; 5; })); ASSERT(10, ({ int i = 0; while (i < 10) i = i + 1; i; })); ASSERT(55, ({ int i = 0; int j = 0; while (i <= 10) { j = i + j; i = i + 1; } j; })); ASSERT(3, (1, 2, 3)); ASSERT(5, ({ int i = 2, j = 3; (i = 5, j) = 6; i; })); ASSERT(6, ({ int i = 2, j = 3; (i = 5, j) = 6; j; })); ASSERT(55, ({ int j = 0; for (int i = 0; i <= 10; i = i + 1) j = j + i; j; })); ASSERT(3, ({ int i = 3; int j = 0; for (int i = 0; i <= 10; i = i + 1) j = j + i; i; })); ASSERT(1, 0 || 1); ASSERT(1, 0 || (2 - 2) || 5); ASSERT(0, 0 || 0); ASSERT(0, 0 || (2 - 2)); ASSERT(0, 0 && 1); ASSERT(0, (2 - 2) && 5); ASSERT(1, 1 && 5); ASSERT(3, ({ int i = 0; goto a; a: i++; b: i++; c: i++; i; })); ASSERT(2, ({ int i = 0; goto e; d: i++; e: i++; f: i++; i; })); ASSERT(1, ({ int i = 0; goto i; g: i++; h: i++; i: i++; i; })); ASSERT(1, ({ typedef int foo; goto foo; foo:; 1; })); ASSERT(3, ({ int i = 0; for (; i < 10; i++) { if (i == 3) break; } i; })); ASSERT(4, ({ int i = 0; while (1) { if (i++ == 3) break; } i; })); ASSERT(3, ({ int i = 0; for (; i < 10; i++) { for (;;) break; if (i == 3) break; } i; })); ASSERT(4, ({ int i = 0; while (1) { while (1) break; if (i++ == 3) break; } i; })); ASSERT(10, ({ int i = 0; int j = 0; for (; i < 10; i++) { if (i > 5) continue; j++; } i; })); ASSERT(6, ({ int i = 0; int j = 0; for (; i < 10; i++) { if (i > 5) continue; j++; } j; })); ASSERT(10, ({ int i = 0; int j = 0; for (; !i;) { for (; j != 10; j++) continue; break; } j; })); ASSERT(11, ({ int i = 0; int j = 0; while (i++ < 10) { if (i > 5) continue; j++; } i; })); ASSERT(5, ({ int i = 0; int j = 0; while (i++ < 10) { if (i > 5) continue; j++; } j; })); ASSERT(11, ({ int i = 0; int j = 0; while (!i) { while (j++ != 10) continue; break; } j; })); ASSERT(5, ({ int i = 0; switch (0) { case 0: i = 5; break; case 1: i = 6; break; case 2: i = 7; break; } i; })); ASSERT(6, ({ int i = 0; switch (1) { case 0: i = 5; break; case 1: i = 6; break; case 2: i = 7; break; } i; })); ASSERT(7, ({ int i = 0; switch (2) { case 0: i = 5; break; case 1: i = 6; break; case 2: i = 7; break; } i; })); ASSERT(0, ({ int i = 0; switch (3) { case 0: i = 5; break; case 1: i = 6; break; case 2: i = 7; break; } i; })); ASSERT(5, ({ int i = 0; switch (0) { case 0: i = 5; break; default: i = 7; } i; })); ASSERT(7, ({ int i = 0; switch (1) { case 0: i = 5; break; default: i = 7; } i; })); ASSERT(2, ({ int i = 0; switch (1) { case 0: 0; case 1: 0; case 2: 0; i = 2; } i; })); ASSERT(0, ({ int i = 0; switch (3) { case 0: 0; case 1: 0; case 2: 0; i = 2; } i; })); ASSERT(3, ({ int i = 0; switch (-1) { case 0xffffffff: i = 3; break; } i; })); ASSERT(7, ({ int i = 0; int j = 0; do { j++; } while (i++ < 6); j; })); ASSERT(4, ({ int i = 0; int j = 0; int k = 0; do { if (++j > 3) break; continue; k++; } while (1); j; })); ASSERT(0, 0.0 && 0.0); ASSERT(0, 0.0 && 0.1); ASSERT(0, 0.3 && 0.0); ASSERT(1, 0.3 && 0.5); ASSERT(0, 0.0 || 0.0); ASSERT(1, 0.0 || 0.1); ASSERT(1, 0.3 || 0.0); ASSERT(1, 0.3 || 0.5); ASSERT(5, ({ int x; if (0.0) x = 3; else x = 5; x; })); ASSERT(3, ({ int x; if (0.1) x = 3; else x = 5; x; })); ASSERT(5, ({ int x = 5; if (0.0) x = 3; x; })); ASSERT(3, ({ int x = 5; if (0.1) x = 3; x; })); ASSERT(10, ({ double i = 10.0; int j = 0; for (; i; i--, j++) ; j; })); ASSERT(10, ({ double i = 10.0; int j = 0; do j++; while (--i); j; })); ASSERT(2, ({ int i = 0; switch (7) { case 0 ... 5: i = 1; break; case 6 ... 20: i = 2; break; } i; })); ASSERT(1, ({ int i = 0; switch (7) { case 0 ... 7: i = 1; break; case 8 ... 10: i = 2; break; } i; })); ASSERT(1, ({ int i = 0; switch (7) { case 0: i = 1; break; case 7 ... 7: i = 1; break; } i; })); ASSERT(3, ({ void *p = &&v11; int i = 0; goto *p; v11: i++; v12: i++; v13: i++; i; })); ASSERT(2, ({ void *p = &&v22; int i = 0; goto *p; v21: i++; v22: i++; v23: i++; i; })); ASSERT(1, ({ void *p = &&v33; int i = 0; goto *p; v31: i++; v32: i++; v33: i++; i; })); ASSERT(3, ({ static void *p[] = {&&v41, &&v42, &&v43}; int i = 0; goto *p[0]; v41: i++; v42: i++; v43: i++; i; })); ASSERT(2, ({ static void *p[] = {&&v52, &&v52, &&v53}; int i = 0; goto *p[1]; v51: i++; v52: i++; v53: i++; i; })); ASSERT(1, ({ static void *p[] = {&&v62, &&v62, &&v63}; int i = 0; goto *p[2]; v61: i++; v62: i++; v63: i++; i; })); return 0; }
9,978
543
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/generic_test.c
#include "third_party/chibicc/test/test.h" int main() { ASSERT(1, _Generic(100.0, double : 1, int * : 2, int : 3, float : 4)); ASSERT(2, _Generic((int *)0, double : 1, int * : 2, int : 3, float : 4)); ASSERT(2, _Generic((int[3]){}, double : 1, int * : 2, int : 3, float : 4)); ASSERT(3, _Generic(100, double : 1, int * : 2, int : 3, float : 4)); ASSERT(4, _Generic(100f, double : 1, int * : 2, int : 3, float : 4)); return 0; }
441
11
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/common.c
#include "third_party/chibicc/test/test.h" void Assert(long expected, long actual, char *code) { if (expected != actual) { fprintf(stderr, "%s => %ld expected but got %ld\n", code, expected, actual); exit(1); } } void Assert2(long expected, long actual, char *code, char *func, int line) { if (expected != actual) { fprintf(stderr, "%s:%d: %s => expected %ld but got %ld\n", func, line, code, expected, actual); exit(1); } } void Assert128(__int128 k, __int128 x, char *code, char *func, int line) { if (k != x) { fprintf(stderr, "%s:%d: %s => want %jjd but got %jjd\n", func, line, code, k, x); exit(1); } } static int static_fn() { return 5; } int ext1 = 5; int *ext2 = &ext1; int ext3 = 7; int ext_fn1(int x) { return x; } int ext_fn2(int x) { return x; } int common_ext2 = 3; static int common_local; int false_fn() { return 512; } int true_fn() { return 513; } int char_fn() { return (2 << 8) + 3; } int short_fn() { return (2 << 16) + 5; } int uchar_fn() { return (2 << 10) - 1 - 4; } int ushort_fn() { return (2 << 20) - 1 - 7; } int schar_fn() { return (2 << 10) - 1 - 4; } int sshort_fn() { return (2 << 20) - 1 - 7; } int add_all(int n, ...) { va_list ap; va_start(ap, n); int sum = 0; for (int i = 0; i < n; i++) sum += va_arg(ap, int); va_end(ap); return sum; } float add_float(float x, float y) { return x + y; } double add_double(double x, double y) { return x + y; } int add10_int(int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8, int x9, int x10) { return x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10; } float add10_float(float x1, float x2, float x3, float x4, float x5, float x6, float x7, float x8, float x9, float x10) { return x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10; } double add10_double(double x1, double x2, double x3, double x4, double x5, double x6, double x7, double x8, double x9, double x10) { return x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10; } typedef struct { int a, b; short c; char d; } Ty4; typedef struct { int a; float b; double c; } Ty5; typedef struct { unsigned char a[3]; } Ty6; typedef struct { long a, b, c; } Ty7; int struct_test4(Ty4 x, int n) { switch (n) { case 0: return x.a; case 1: return x.b; case 2: return x.c; default: return x.d; } } int struct_test5(Ty5 x, int n) { switch (n) { case 0: return x.a; case 1: return x.b; default: return x.c; } } int struct_test6(Ty6 x, int n) { return x.a[n]; } int struct_test7(Ty7 x, int n) { switch (n) { case 0: return x.a; case 1: return x.b; default: return x.c; } } Ty4 struct_test24(void) { return (Ty4){10, 20, 30, 40}; } Ty5 struct_test25(void) { return (Ty5){10, 20, 30}; } Ty6 struct_test26(void) { return (Ty6){{10, 20, 30}}; } typedef struct { unsigned char a[10]; } Ty20; typedef struct { unsigned char a[20]; } Ty21; Ty20 struct_test27(void) { return (Ty20){{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}}; } Ty21 struct_test28(void) { return (Ty21){ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}}; }
3,304
196
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/string_test.c
#include "third_party/chibicc/test/test.h" int main() { typeof("str") StringInitParen1 = ("str"); char StringInitParen2[] = ("str"); ASSERT(0, ""[0]); ASSERT(1, sizeof("")); ASSERT(97, "abc"[0]); ASSERT(98, "abc"[1]); ASSERT(99, "abc"[2]); ASSERT(0, "abc"[3]); ASSERT(4, sizeof("abc")); ASSERT(7, "\a"[0]); ASSERT(8, "\b"[0]); ASSERT(9, "\t"[0]); ASSERT(10, "\n"[0]); ASSERT(11, "\v"[0]); ASSERT(12, "\f"[0]); ASSERT(13, "\r"[0]); ASSERT(27, "\e"[0]); ASSERT(106, "\j"[0]); ASSERT(107, "\k"[0]); ASSERT(108, "\l"[0]); ASSERT(7, "\ax\ny"[0]); ASSERT(120, "\ax\ny"[1]); ASSERT(10, "\ax\ny"[2]); ASSERT(121, "\ax\ny"[3]); ASSERT(0, "\0"[0]); ASSERT(16, "\20"[0]); ASSERT(65, "\101"[0]); ASSERT(104, "\1500"[0]); ASSERT(0, "\x00"[0]); ASSERT(119, "\x77"[0]); ASSERT(7, sizeof("abc" "def")); ASSERT(9, sizeof("abc" "d" "efgh")); ASSERT(0, strcmp("abc" "d" "\nefgh", "abcd\nefgh")); ASSERT(0, !strcmp("abc" "d", "abcd\nefgh")); ASSERT(0, strcmp("\x9" "0", "\t0")); ASSERT(16, sizeof(L"abc" "")); ASSERT(28, sizeof(L"abc" "def")); ASSERT(28, sizeof(L"abc" L"def")); ASSERT(14, sizeof(u"abc" "def")); ASSERT(14, sizeof(u"abc" u"def")); ASSERT(L'a', (L"abc" "def")[0]); ASSERT(L'd', (L"abc" "def")[3]); ASSERT(L'\0', (L"abc" "def")[6]); ASSERT(u'a', (u"abc" "def")[0]); ASSERT(u'd', (u"abc" "def")[3]); ASSERT(u'\0', (u"abc" "def")[6]); ASSERT(L'あ', ("あ" L"")[0]); ASSERT(0343, ("\343\201\202" L"")[0]); ASSERT(0201, ("\343\201\202" L"")[1]); ASSERT(0202, ("\343\201\202" L"")[2]); ASSERT(0, ("\343\201\202" L"")[3]); ASSERT(L'a', ("a" "b" L"c")[0]); ASSERT(L'b', ("a" "b" L"c")[1]); ASSERT(L'c', ("a" "b" L"c")[2]); ASSERT(0, ("a" "b" L"c")[3]); return 0; }
2,371
109
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/arith_test.c
#include "third_party/chibicc/test/test.h" int main() { ASSERT(0, 0); ASSERT(42, 42); ASSERT(21, 5 + 20 - 4); ASSERT(41, 12 + 34 - 5); ASSERT(47, 5 + 6 * 7); ASSERT(15, 5 * (9 - 6)); ASSERT(4, (3 + 5) / 2); ASSERT(10, -10 + 20); ASSERT(0, 0 == 1); ASSERT(1, 42 == 42); ASSERT(1, 0 != 1); ASSERT(0, 42 != 42); ASSERT(1, 0 < 1); ASSERT(0, 1 < 1); ASSERT(0, 2 < 1); ASSERT(1, 0 <= 1); ASSERT(1, 1 <= 1); ASSERT(0, 2 <= 1); ASSERT(1, 1 > 0); ASSERT(0, 1 > 1); ASSERT(0, 1 > 2); ASSERT(1, 1 >= 0); ASSERT(1, 1 >= 1); ASSERT(0, 1 >= 2); ASSERT(0, 1073741824 * 100 / 100); ASSERT(7, ({ int i = 2; i += 5; i; })); ASSERT(7, ({ int i = 2; i += 5; })); ASSERT(3, ({ int i = 5; i -= 2; i; })); ASSERT(3, ({ int i = 5; i -= 2; })); ASSERT(6, ({ int i = 3; i *= 2; i; })); ASSERT(6, ({ int i = 3; i *= 2; })); ASSERT(3, ({ int i = 6; i /= 2; i; })); ASSERT(3, ({ int i = 6; i /= 2; })); ASSERT(3, ({ int i = 2; ++i; })); ASSERT(2, ({ int a[3]; a[0] = 0; a[1] = 1; a[2] = 2; int *p = a + 1; ++*p; })); ASSERT(0, ({ int a[3]; a[0] = 0; a[1] = 1; a[2] = 2; int *p = a + 1; --*p; })); ASSERT(2, ({ int i = 2; i++; })); ASSERT(2, ({ int i = 2; i--; })); ASSERT(3, ({ int i = 2; i++; i; })); ASSERT(1, ({ int i = 2; i--; i; })); ASSERT(1, ({ int a[3]; a[0] = 0; a[1] = 1; a[2] = 2; int *p = a + 1; *p++; })); ASSERT(1, ({ int a[3]; a[0] = 0; a[1] = 1; a[2] = 2; int *p = a + 1; *p--; })); ASSERT(0, ({ int a[3]; a[0] = 0; a[1] = 1; a[2] = 2; int *p = a + 1; (*p++)--; a[0]; })); ASSERT(0, ({ int a[3]; a[0] = 0; a[1] = 1; a[2] = 2; int *p = a + 1; (*(p--))--; a[1]; })); ASSERT(2, ({ int a[3]; a[0] = 0; a[1] = 1; a[2] = 2; int *p = a + 1; (*p)--; a[2]; })); ASSERT(2, ({ int a[3]; a[0] = 0; a[1] = 1; a[2] = 2; int *p = a + 1; (*p)--; p++; *p; })); ASSERT(0, ({ int a[3]; a[0] = 0; a[1] = 1; a[2] = 2; int *p = a + 1; (*p++)--; a[0]; })); ASSERT(0, ({ int a[3]; a[0] = 0; a[1] = 1; a[2] = 2; int *p = a + 1; (*p++)--; a[1]; })); ASSERT(2, ({ int a[3]; a[0] = 0; a[1] = 1; a[2] = 2; int *p = a + 1; (*p++)--; a[2]; })); ASSERT(2, ({ int a[3]; a[0] = 0; a[1] = 1; a[2] = 2; int *p = a + 1; (*p++)--; *p; })); ASSERT(0, !1); ASSERT(0, !2); ASSERT(1, !0); ASSERT(1, !(char)0); ASSERT(0, !(long)3); ASSERT(4, sizeof(!(char)0)); ASSERT(4, sizeof(!(long)0)); ASSERT(-1, ~0); ASSERT(0, ~-1); ASSERT(5, 17 % 6); ASSERT(5, ((long)17) % 6); ASSERT(2, ({ int i = 10; i %= 4; i; })); ASSERT(2, ({ long i = 10; i %= 4; i; })); ASSERT(0, 0 & 1); ASSERT(1, 3 & 1); ASSERT(3, 7 & 3); ASSERT(10, -1 & 10); ASSERT(1, 0 | 1); ASSERT(0b10011, 0b10000 | 0b00011); ASSERT(0, 0 ^ 0); ASSERT(0, 0b1111 ^ 0b1111); ASSERT(0b110100, 0b111000 ^ 0b001100); ASSERT(2, ({ int i = 6; i &= 3; i; })); ASSERT(7, ({ int i = 6; i |= 3; i; })); ASSERT(10, ({ int i = 15; i ^= 5; i; })); ASSERT(1, 1 << 0); ASSERT(8, 1 << 3); ASSERT(10, 5 << 1); ASSERT(2, 5 >> 1); ASSERT(-1, -1 >> 1); ASSERT(1, ({ int i = 1; i <<= 0; i; })); ASSERT(8, ({ int i = 1; i <<= 3; i; })); ASSERT(10, ({ int i = 5; i <<= 1; i; })); ASSERT(2, ({ int i = 5; i >>= 1; i; })); ASSERT(-1, -1); ASSERT(-1, ({ int i = -1; i; })); ASSERT(-1, ({ int i = -1; i >>= 1; i; })); ASSERT(2, 0 ? 1 : 2); ASSERT(1, 1 ? 1 : 2); ASSERT(-1, 0 ? -2 : -1); ASSERT(-2, 1 ? -2 : -1); ASSERT(4, sizeof(0 ? 1 : 2)); ASSERT(8, sizeof(0 ? (long)1 : (long)2)); ASSERT(-1, 0 ? (long)-2 : -1); ASSERT(-1, 0 ? -2 : (long)-1); ASSERT(-2, 1 ? (long)-2 : -1); ASSERT(-2, 1 ? -2 : (long)-1); 1 ? -2 : (void)-1; ASSERT(20, ({ int x; int *p = &x; p + 20 - p; })); ASSERT(1, ({ int x; int *p = &x; p + 20 - p > 0; })); ASSERT(-20, ({ int x; int *p = &x; p - 20 - p; })); ASSERT(1, ({ int x; int *p = &x; p - 20 - p < 0; })); ASSERT(15, (char *)0xffffffffffffffff - (char *)0xfffffffffffffff0); ASSERT(-15, (char *)0xfffffffffffffff0 - (char *)0xffffffffffffffff); ASSERT(1, (void *)0xffffffffffffffff > (void *)0); ASSERT(3, 3 ?: 5); ASSERT(5, 0 ?: 5); ASSERT(4, ({ int i = 3; ++i ?: 10; })); ASSERT(3, (long double)3); ASSERT(5, (long double)3 + 2); ASSERT(6, (long double)3 * 2); ASSERT(5, (long double)3 + 2.0); return 0; }
6,301
342
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/include2.h
int include2 = 7;
18
2
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/const_test.c
#include "third_party/chibicc/test/test.h" int main() { { const x; } { int const x; } { const int x; } { const int const const x; } ASSERT(5, ({ const x = 5; x; })); ASSERT(8, ({ const x = 8; int *const y = &x; *y; })); ASSERT(6, ({ const x = 6; *(const *const) & x; })); return 0; }
406
24
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/msabi_test.c
#include "third_party/chibicc/test/test.h" int (*__attribute__((__ms_abi__)) NtFoo)(int x, int y); int main() { }
116
7
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/union_test.c
#include "third_party/chibicc/test/test.h" int main() { ASSERT(8, ({ union { int a; char b[6]; } x; sizeof(x); })); ASSERT(3, ({ union { int a; char b[4]; } x; x.a = 515; x.b[0]; })); ASSERT(2, ({ union { int a; char b[4]; } x; x.a = 515; x.b[1]; })); ASSERT(0, ({ union { int a; char b[4]; } x; x.a = 515; x.b[2]; })); ASSERT(0, ({ union { int a; char b[4]; } x; x.a = 515; x.b[3]; })); ASSERT(3, ({ union { int a, b; } x, y; x.a = 3; y.a = 5; y = x; y.a; })); ASSERT(3, ({ union { struct { int a, b; } c; } x, y; x.c.b = 3; y.c.b = 5; y = x; y.c.b; })); ASSERT(0xef, ({ union { struct { unsigned char a, b, c, d; }; long e; } x; x.e = 0xdeadbeef; x.a; })); ASSERT(0xbe, ({ union { struct { unsigned char a, b, c, d; }; long e; } x; x.e = 0xdeadbeef; x.b; })); ASSERT(0xad, ({ union { struct { unsigned char a, b, c, d; }; long e; } x; x.e = 0xdeadbeef; x.c; })); ASSERT(0xde, ({ union { struct { unsigned char a, b, c, d; }; long e; } x; x.e = 0xdeadbeef; x.d; })); ASSERT(3, ({ struct { union { int a, b; }; union { int c, d; }; } x; x.a = 3; x.b; })); ASSERT(5, ({ struct { union { int a, b; }; union { int c, d; }; } x; x.d = 5; x.c; })); return 0; }
2,475
133
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/offsetof_test.c
#include "third_party/chibicc/test/test.h" typedef struct { int a; char b; int c; double d; } T; int main() { ASSERT(0, offsetof(T, a)); ASSERT(4, offsetof(T, b)); ASSERT(8, offsetof(T, c)); ASSERT(16, offsetof(T, d)); return 0; }
252
18
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/struct_test.c
#include "third_party/chibicc/test/test.h" int main() { ASSERT(1, ({ struct { int a; int b; } x; x.a = 1; x.b = 2; x.a; })); ASSERT(2, ({ struct { int a; int b; } x; x.a = 1; x.b = 2; x.b; })); ASSERT(1, ({ struct { char a; int b; char c; } x; x.a = 1; x.b = 2; x.c = 3; x.a; })); ASSERT(2, ({ struct { char a; int b; char c; } x; x.b = 1; x.b = 2; x.c = 3; x.b; })); ASSERT(3, ({ struct { char a; int b; char c; } x; x.a = 1; x.b = 2; x.c = 3; x.c; })); ASSERT(0, ({ struct { char a; char b; } x[3]; char *p = x; p[0] = 0; x[0].a; })); ASSERT(1, ({ struct { char a; char b; } x[3]; char *p = x; p[1] = 1; x[0].b; })); ASSERT(2, ({ struct { char a; char b; } x[3]; char *p = x; p[2] = 2; x[1].a; })); ASSERT(3, ({ struct { char a; char b; } x[3]; char *p = x; p[3] = 3; x[1].b; })); ASSERT(6, ({ struct { char a[3]; char b[5]; } x; char *p = &x; x.a[0] = 6; p[0]; })); ASSERT(7, ({ struct { char a[3]; char b[5]; } x; char *p = &x; x.b[0] = 7; p[3]; })); ASSERT(6, ({ struct { struct { char b; } a; } x; x.a.b = 6; x.a.b; })); ASSERT(4, ({ struct { int a; } x; sizeof(x); })); ASSERT(8, ({ struct { int a; int b; } x; sizeof(x); })); ASSERT(8, ({ struct { int a, b; } x; sizeof(x); })); ASSERT(12, ({ struct { int a[3]; } x; sizeof(x); })); ASSERT(16, ({ struct { int a; } x[4]; sizeof(x); })); ASSERT(24, ({ struct { int a[3]; } x[2]; sizeof(x); })); ASSERT(2, ({ struct { char a; char b; } x; sizeof(x); })); ASSERT(0, ({ struct { } x; sizeof(x); })); ASSERT(8, ({ struct { char a; int b; } x; sizeof(x); })); ASSERT(8, ({ struct { int a; char b; } x; sizeof(x); })); ASSERT(8, ({ struct t { int a; int b; } x; struct t y; sizeof(y); })); ASSERT(8, ({ struct t { int a; int b; }; struct t y; sizeof(y); })); ASSERT(2, ({ struct t { char a[2]; }; { struct t { char a[4]; }; } struct t y; sizeof(y); })); ASSERT(3, ({ struct t { int x; }; int t = 1; struct t y; y.x = 2; t + y.x; })); ASSERT(3, ({ struct t { char a; } x; struct t *y = &x; x.a = 3; y->a; })); ASSERT(3, ({ struct t { char a; } x; struct t *y = &x; y->a = 3; x.a; })); ASSERT(3, ({ struct { int a, b; } x, y; x.a = 3; y = x; y.a; })); ASSERT(7, ({ struct t { int a, b; }; struct t x; x.a = 7; struct t y; struct t *z = &y; *z = x; y.a; })); ASSERT(7, ({ struct t { int a, b; }; struct t x; x.a = 7; struct t y, *p = &x, *q = &y; *q = *p; y.a; })); ASSERT(5, ({ struct t { char a, b; } x, y; x.a = 5; y = x; y.a; })); ASSERT(3, ({ struct { int a, b; } x, y; x.a = 3; y = x; y.a; })); ASSERT(7, ({ struct t { int a, b; }; struct t x; x.a = 7; struct t y; struct t *z = &y; *z = x; y.a; })); ASSERT(7, ({ struct t { int a, b; }; struct t x; x.a = 7; struct t y, *p = &x, *q = &y; *q = *p; y.a; })); ASSERT(5, ({ struct t { char a, b; } x, y; x.a = 5; y = x; y.a; })); ASSERT(8, ({ struct t { int a; int b; } x; struct t y; sizeof(y); })); ASSERT(8, ({ struct t { int a; int b; }; struct t y; sizeof(y); })); ASSERT(16, ({ struct { char a; long b; } x; sizeof(x); })); ASSERT(4, ({ struct { char a; short b; } x; sizeof(x); })); ASSERT(8, ({ struct foo *bar; sizeof(bar); })); ASSERT(4, ({ struct T *foo; struct T { int x; }; sizeof(struct T); })); ASSERT(1, ({ struct T { struct T *next; int x; } a; struct T b; b.x = 1; a.next = &b; a.next->x; })); ASSERT(4, ({ typedef struct T T; struct T { int x; }; sizeof(T); })); ASSERT(2, ({ struct { int a; } x = {1}, y = {2}; (x = y).a; })); ASSERT(1, ({ struct { int a; } x = {1}, y = {2}; (1 ? x : y).a; })); ASSERT(2, ({ struct { int a; } x = {1}, y = {2}; (0 ? x : y).a; })); ASSERT(2, ({ struct { int a; } x = {1}, y = {2}; (x = y).a; })); ASSERT(1, ({ struct { int a; } x = {1}, y = {2}; (1 ? x : y).a; })); ASSERT(2, ({ struct { int a; } x = {1}, y = {2}; (0 ? x : y).a; })); return 0; }
7,704
416
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/include1.h
#include "third_party/chibicc/test/include2.h" char *include1_filename = __FILE__; int include1_line = __LINE__; int include1 = 5;
132
6
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/compat_test.c
#include "third_party/chibicc/test/test.h" _Noreturn noreturn_fn(int restrict x) { exit(0); } void funcy_type(int arg[restrict static 3]) { } int main() { { volatile x; } { int volatile x; } { volatile int x; } { volatile int volatile volatile x; } { int volatile *volatile volatile x; } { auto **restrict __restrict __restrict__ const volatile *x; } return 0; }
382
19
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/attribute_test.c
#include "third_party/chibicc/test/test.h" void doge() __attribute__((__nonnull__)); void cate(char *) __attribute__((__nonnull__(1))); int var __attribute__((__section__(".data.var"))); int ar[4] __attribute__((__section__(".data.var"))); typedef int int2[2] __attribute__((__aligned__(64))); typedef int int4[4] __attribute__((__warn_if_not_aligned__(16))); __attribute__((__nonnull__)) void doge2(); __attribute__((__nonnull__(1))) void cate2(char *); __attribute__((__section__(".data.var"))) int var2; __attribute__((__section__(".data.var"))) int ar2[4]; __attribute__((__force_align_arg_pointer__, __no_caller_saved_registers__)) int main() { int2 a; ASSERT(64, _Alignof(int2)); ASSERT(64, _Alignof(a)); ASSERT(5, ({ struct { char a; int b; } __attribute__((packed)) x; sizeof(x); })); ASSERT(0, offsetof( struct __attribute__((packed)) { char a; int b; }, a)); ASSERT(1, offsetof( struct __attribute__((packed)) { char a; int b; }, b)); ASSERT(5, ({ struct __attribute__((packed)) { char a; int b; } x; sizeof(x); })); ASSERT(0, offsetof( struct { char a; int b; } __attribute__((packed)), a)); ASSERT(1, offsetof( struct { char a; int b; } __attribute__((packed)), b)); ASSERT(9, ({ typedef struct { char a; int b[2]; } __attribute__((packed)) T; sizeof(T); })); ASSERT(9, ({ typedef struct __attribute__((packed)) { char a; int b[2]; } T; sizeof(T); })); ASSERT(1, offsetof( struct __attribute__((packed)) T { char a; int b[2]; }, b)); ASSERT(1, _Alignof(struct __attribute__((packed)) { char a; int b[2]; })); ASSERT(8, ({ struct __attribute__((aligned(8))) { int a; } x; _Alignof(x); })); ASSERT(8, ({ struct { int a; } __attribute__((aligned(8))) x; _Alignof(x); })); ASSERT(8, ({ struct __attribute__((aligned(8), packed)) { char a; int b; } x; _Alignof(x); })); ASSERT(8, ({ struct { char a; int b; } __attribute__((aligned(8), packed)) x; _Alignof(x); })); ASSERT(1, offsetof( struct __attribute__((aligned(8), packed)) { char a; int b; }, b)); ASSERT(1, offsetof( struct { char a; int b; } __attribute__((aligned(8), packed)), b)); ASSERT(8, ({ struct __attribute__((aligned(8))) __attribute__((packed)) { char a; int b; } x; _Alignof(x); })); ASSERT(8, ({ struct { char a; int b; } __attribute__((aligned(8))) __attribute__((packed)) x; _Alignof(x); })); ASSERT(1, offsetof( struct __attribute__((aligned(8))) __attribute__((packed)) { char a; int b; }, b)); ASSERT(1, offsetof( struct { char a; int b; } __attribute__((aligned(8))) __attribute__((packed)), b)); ASSERT(8, ({ struct __attribute__((aligned(8))) { char a; int b; } __attribute__((packed)) x; _Alignof(x); })); ASSERT(1, offsetof( struct __attribute__((aligned(8))) { char a; int b; } __attribute__((packed)), b)); ASSERT(16, ({ struct __attribute__((aligned(8 + 8))) { char a; int b; } x; _Alignof(x); })); return 0; }
4,529
177
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/extern_test.c
#include "third_party/chibicc/test/test.h" extern int ext1; extern int *ext2; inline int inline_fn(void) { return 3; } int main() { ASSERT(5, ext1); ASSERT(5, *ext2); extern int ext3; ASSERT(7, ext3); int ext_fn1(int x); ASSERT(5, ext_fn1(5)); extern int ext_fn2(int x); ASSERT(8, ext_fn2(8)); return 0; }
333
25
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/variable_test.c
#include "third_party/chibicc/test/test.h" int g1, g2[4]; static int g3 = 3; int main() { ASSERT(3, ({ int a; a = 3; a; })); ASSERT(3, ({ int a = 3; a; })); ASSERT(8, ({ int a = 3; int z = 5; a + z; })); ASSERT(3, ({ int a = 3; a; })); ASSERT(8, ({ int a = 3; int z = 5; a + z; })); ASSERT(6, ({ int a; int b; a = b = 3; a + b; })); ASSERT(3, ({ int foo = 3; foo; })); ASSERT(8, ({ int foo123 = 3; int bar = 5; foo123 + bar; })); ASSERT(4, ({ int x; sizeof(x); })); ASSERT(4, ({ int x; sizeof x; })); ASSERT(8, ({ int *x; sizeof(x); })); ASSERT(16, ({ int x[4]; sizeof(x); })); ASSERT(48, ({ int x[3][4]; sizeof(x); })); ASSERT(16, ({ int x[3][4]; sizeof(*x); })); ASSERT(4, ({ int x[3][4]; sizeof(**x); })); ASSERT(5, ({ int x[3][4]; sizeof(**x) + 1; })); ASSERT(5, ({ int x[3][4]; sizeof **x + 1; })); ASSERT(4, ({ int x[3][4]; sizeof(**x + 1); })); ASSERT(4, ({ int x = 1; sizeof(x = 2); })); ASSERT(1, ({ int x = 1; sizeof(x = 2); x; })); ASSERT(0, g1); ASSERT(3, ({ g1 = 3; g1; })); ASSERT(0, ({ g2[0] = 0; g2[1] = 1; g2[2] = 2; g2[3] = 3; g2[0]; })); ASSERT(1, ({ g2[0] = 0; g2[1] = 1; g2[2] = 2; g2[3] = 3; g2[1]; })); ASSERT(2, ({ g2[0] = 0; g2[1] = 1; g2[2] = 2; g2[3] = 3; g2[2]; })); ASSERT(3, ({ g2[0] = 0; g2[1] = 1; g2[2] = 2; g2[3] = 3; g2[3]; })); ASSERT(4, sizeof(g1)); ASSERT(16, sizeof(g2)); ASSERT(1, ({ char x = 1; x; })); ASSERT(1, ({ char x = 1; char y = 2; x; })); ASSERT(2, ({ char x = 1; char y = 2; y; })); ASSERT(1, ({ char x; sizeof(x); })); ASSERT(10, ({ char x[10]; sizeof(x); })); ASSERT(2, ({ int x = 2; { int x = 3; } x; })); ASSERT(2, ({ int x = 2; { int x = 3; } int y = 4; x; })); ASSERT(3, ({ int x = 2; { x = 3; } x; })); ASSERT(7, ({ int x; int y; char z; char *a = &y; char *b = &z; b - a; })); ASSERT(1, ({ int x; char y; int z; char *a = &y; char *b = &z; b - a; })); ASSERT(8, ({ long x; sizeof(x); })); ASSERT(2, ({ short x; sizeof(x); })); ASSERT(24, ({ char *x[3]; sizeof(x); })); ASSERT(8, ({ char(*x)[3]; sizeof(x); })); ASSERT(1, ({ char(x); sizeof(x); })); ASSERT(3, ({ char(x)[3]; sizeof(x); })); ASSERT(12, ({ char(x[3])[4]; sizeof(x); })); ASSERT(4, ({ char(x[3])[4]; sizeof(x[0]); })); ASSERT(3, ({ char *x[3]; char y; x[0] = &y; y = 3; x[0][0]; })); ASSERT(4, ({ char x[3]; char(*y)[3] = x; y[0][0] = 4; y[0][0]; })); { void *x; } ASSERT(3, g3); return 0; }
4,244
245
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/enum_test.c
#include "third_party/chibicc/test/test.h" int main() { ASSERT(0, ({ enum { zero, one, two }; zero; })); ASSERT(1, ({ enum { zero, one, two }; one; })); ASSERT(2, ({ enum { zero, one, two }; two; })); ASSERT(5, ({ enum { five = 5, six, seven }; five; })); ASSERT(6, ({ enum { five = 5, six, seven }; six; })); ASSERT(0, ({ enum { zero, five = 5, three = 3, four }; zero; })); ASSERT(5, ({ enum { zero, five = 5, three = 3, four }; five; })); ASSERT(3, ({ enum { zero, five = 5, three = 3, four }; three; })); ASSERT(4, ({ enum { zero, five = 5, three = 3, four }; four; })); ASSERT(4, ({ enum { zero, one, two } x; sizeof(x); })); ASSERT(4, ({ enum t { zero, one, two }; enum t y; sizeof(y); })); return 0; }
1,086
51
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/cast_test.c
#include "third_party/chibicc/test/test.h" int main() { ASSERT(131585, (int)8590066177); ASSERT(513, (short)8590066177); ASSERT(1, (char)8590066177); ASSERT(1, (long)1); ASSERT(0, (long)&*(int *)0); ASSERT(513, ({ int x = 512; *(char *)&x = 1; x; })); ASSERT(5, ({ int x = 5; long y = (long)&x; *(int *)y; })); (void)1; ASSERT(-1, (char)255); ASSERT(-1, (signed char)255); ASSERT(255, (unsigned char)255); ASSERT(-1, (short)65535); ASSERT(65535, (unsigned short)65535); ASSERT(-1, (int)0xffffffff); ASSERT(0xffffffff, (unsigned)0xffffffff); ASSERT(1, -1 < 1); ASSERT(0, -1 < (unsigned)1); ASSERT(254, (char)127 + (char)127); ASSERT(65534, (short)32767 + (short)32767); ASSERT(-1, -1 >> 1); ASSERT(-1, (unsigned long)-1); ASSERT(2147483647, ((unsigned)-1) >> 1); ASSERT(-50, (-100) / 2); ASSERT(2147483598, ((unsigned)-100) / 2); ASSERT(9223372036854775758, ((unsigned long)-100) / 2); ASSERT(0, ((long)-1) / (unsigned)100); ASSERT(-2, (-100) % 7); ASSERT(2, ((unsigned)-100) % 7); ASSERT(6, ((unsigned long)-100) % 9); ASSERT(65535, (int)(unsigned short)65535); ASSERT(65535, ({ unsigned short x = 65535; x; })); ASSERT(65535, ({ unsigned short x = 65535; (int)x; })); ASSERT(-1, ({ typedef short T; T x = 65535; (int)x; })); ASSERT(65535, ({ typedef unsigned short T; T x = 65535; (int)x; })); ASSERT(0, (_Bool)0.0); ASSERT(1, (_Bool)0.1); ASSERT(3, (char)3.0); ASSERT(1000, (short)1000.3); ASSERT(3, (int)3.99); ASSERT(2000000000000000, (long)2e15); ASSERT(3, (float)3.5); ASSERT(5, (double)(float)5.5); ASSERT(3, (float)3); ASSERT(3, (double)3); ASSERT(3, (float)3L); ASSERT(3, (double)3L); return 0; }
1,910
81
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/test.h
#include "libc/fmt/fmt.h" #include "libc/runtime/runtime.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" STATIC_YOINK("sys_mmap"); /* asan needs it */ STATIC_YOINK("TrackMemoryInterval"); /* asan needs it */ #define ASSERT(x, y) Assert2(x, y, #y, __FILE__, __LINE__) #define ASSERT128(x, y) Assert128(x, y, #y, __FILE__, __LINE__) void Assert(long, long, char *); void Assert2(long, long, char *, char *, int); void Assert128(__int128, __int128, char *, char *, int);
496
15
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/dce_test.c
#include "third_party/chibicc/test/test.h" #define CRASH \ ({ \ asm(".err"); \ 666; \ }) int main(void) { if (0) { return CRASH; } if (1) { } else { return CRASH; } ASSERT(777, 777 ?: CRASH); ASSERT(777, 1 ? 777 : CRASH); ASSERT(777, 0 ? CRASH : 777); ASSERT(777, __builtin_popcount(__builtin_strlen("hihi")) == 1 ? 777 : CRASH); ASSERT(777, !__builtin_strpbrk("HELLO\n", "bxdinupo") ? 777 : CRASH); ASSERT(777, strpbrk("hihi", "ei") ? 777 : CRASH); }
522
27
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/spinlock_test.c
#include "third_party/chibicc/test/test.h" #define SPINLOCK(lock) \ do { \ for (;;) { \ typeof(*(lock)) x; \ __atomic_load(lock, &x, __ATOMIC_RELAXED); \ if (!x && !__sync_lock_test_and_set(lock, 1)) { \ break; \ } else { \ __builtin_ia32_pause(); \ } \ } \ } while (0) #define SPUNLOCK(lock) __sync_lock_release(lock) //////////////////////////////////////////////////////////////////////////////// #define SPINLOCK2(lock) \ do { \ for (;;) { \ typeof(*(lock)) x; \ __atomic_load(lock, &x, __ATOMIC_RELAXED); \ if (!x && !__atomic_test_and_set(lock, __ATOMIC_SEQ_CST)) { \ break; \ } else { \ __builtin_ia32_pause(); \ } \ } \ } while (0) #define SPUNLOCK2(lock) __sync_lock_release(lock) //////////////////////////////////////////////////////////////////////////////// _Alignas(64) char lock; main() { int x, y; ASSERT(0, lock); SPINLOCK(&lock); ASSERT(1, lock); SPUNLOCK(&lock); ASSERT(0, lock); ASSERT(0, lock); SPINLOCK2(&lock); ASSERT(1, lock); SPUNLOCK2(&lock); ASSERT(0, lock); x = 0; y = 7; ASSERT(0, x); ASSERT(7, y); __atomic_store(&x, &y, __ATOMIC_RELAXED); ASSERT(7, x); ASSERT(7, y); x = 0; y = 7; ASSERT(0, x); ASSERT(7, y); __atomic_store(&x, &y, __ATOMIC_SEQ_CST); ASSERT(7, x); ASSERT(7, y); x = 5; y = __atomic_test_and_set(&x, __ATOMIC_SEQ_CST); ASSERT(1, x); ASSERT(5, y); x = 5; __atomic_clear(&x, __ATOMIC_SEQ_CST); ASSERT(0, x); // }
2,364
81
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/initializer_test.c
#include "third_party/chibicc/test/test.h" char g3 = 3; short g4 = 4; int g5 = 5; long g6 = 6; int g9[3] = {0, 1, 2}; struct { char a; int b; } g11[2] = {{1, 2}, {3, 4}}; struct { int a[2]; } g12[2] = {{{1, 2}}}; union { int a; char b[8]; } g13[2] = {0x01020304, 0x05060708}; char g17[] = "foobar"; char g18[10] = "foobar"; char g19[3] = "foobar"; char *g20 = g17 + 0; char *g21 = g17 + 3; char *g22 = &g17 - 3; char *g23[] = {g17 + 0, g17 + 3, g17 - 3}; int g24 = 3; int *g25 = &g24; int g26[3] = {1, 2, 3}; int *g27 = g26 + 1; int *g28 = &g11[1].a; long g29 = (long)(long)g26; struct { struct { int a[3]; } a; } g30 = {{{1, 2, 3}}}; int *g31 = g30.a.a; struct { int a[2]; } g40[2] = {{1, 2}, 3, 4}; struct { int a[2]; } g41[2] = {1, 2, 3, 4}; char g43[][4] = {'f', 'o', 'o', 0, 'b', 'a', 'r', 0}; char *g44 = {"foo"}; union { int a; char b[4]; } g50 = {.b[2] = 0x12}; union { int a; } g51[2] = {}; typedef char T60[]; T60 g60 = {1, 2, 3}; T60 g61 = {1, 2, 3, 4, 5, 6}; typedef struct { char a, b[]; } T65; T65 g65 = {'f', 'o', 'o', 0}; T65 g66 = {'f', 'o', 'o', 'b', 'a', 'r', 0}; int main() { ASSERT(1, ({ int x[3] = {1, 2, 3}; x[0]; })); ASSERT(2, ({ int x[3] = {1, 2, 3}; x[1]; })); ASSERT(3, ({ int x[3] = {1, 2, 3}; x[2]; })); ASSERT(3, ({ int x[3] = {1, 2, 3}; x[2]; })); ASSERT(2, ({ int x[2][3] = {{1, 2, 3}, {4, 5, 6}}; x[0][1]; })); ASSERT(4, ({ int x[2][3] = {{1, 2, 3}, {4, 5, 6}}; x[1][0]; })); ASSERT(6, ({ int x[2][3] = {{1, 2, 3}, {4, 5, 6}}; x[1][2]; })); ASSERT(0, ({ int x[3] = {}; x[0]; })); ASSERT(0, ({ int x[3] = {}; x[1]; })); ASSERT(0, ({ int x[3] = {}; x[2]; })); ASSERT(2, ({ int x[2][3] = {{1, 2}}; x[0][1]; })); ASSERT(0, ({ int x[2][3] = {{1, 2}}; x[1][0]; })); ASSERT(0, ({ int x[2][3] = {{1, 2}}; x[1][2]; })); ASSERT('a', ({ char x[4] = "abc"; x[0]; })); ASSERT('c', ({ char x[4] = "abc"; x[2]; })); ASSERT(0, ({ char x[4] = "abc"; x[3]; })); ASSERT('a', ({ char x[2][4] = {"abc", "def"}; x[0][0]; })); ASSERT(0, ({ char x[2][4] = {"abc", "def"}; x[0][3]; })); ASSERT('d', ({ char x[2][4] = {"abc", "def"}; x[1][0]; })); ASSERT('f', ({ char x[2][4] = {"abc", "def"}; x[1][2]; })); ASSERT(4, ({ int x[] = {1, 2, 3, 4}; x[3]; })); ASSERT(16, ({ int x[] = {1, 2, 3, 4}; sizeof(x); })); ASSERT(4, ({ char x[] = "foo"; sizeof(x); })); ASSERT(4, ({ typedef char T[]; T x = "foo"; T y = "x"; sizeof(x); })); ASSERT(2, ({ typedef char T[]; T x = "foo"; T y = "x"; sizeof(y); })); ASSERT(2, ({ typedef char T[]; T x = "x"; T y = "foo"; sizeof(x); })); ASSERT(4, ({ typedef char T[]; T x = "x"; T y = "foo"; sizeof(y); })); ASSERT(1, ({ struct { int a; int b; int c; } x = {1, 2, 3}; x.a; })); ASSERT(2, ({ struct { int a; int b; int c; } x = {1, 2, 3}; x.b; })); ASSERT(3, ({ struct { int a; int b; int c; } x = {1, 2, 3}; x.c; })); ASSERT(1, ({ struct { int a; int b; int c; } x = {1}; x.a; })); ASSERT(0, ({ struct { int a; int b; int c; } x = {1}; x.b; })); ASSERT(0, ({ struct { int a; int b; int c; } x = {1}; x.c; })); ASSERT(1, ({ struct { int a; int b; } x[2] = {{1, 2}, {3, 4}}; x[0].a; })); ASSERT(2, ({ struct { int a; int b; } x[2] = {{1, 2}, {3, 4}}; x[0].b; })); ASSERT(3, ({ struct { int a; int b; } x[2] = {{1, 2}, {3, 4}}; x[1].a; })); ASSERT(4, ({ struct { int a; int b; } x[2] = {{1, 2}, {3, 4}}; x[1].b; })); ASSERT(0, ({ struct { int a; int b; } x[2] = {{1, 2}}; x[1].b; })); ASSERT(0, ({ struct { int a; int b; } x = {}; x.a; })); ASSERT(0, ({ struct { int a; int b; } x = {}; x.b; })); ASSERT(5, ({ typedef struct { int a, b, c, d, e, f; } T; T x = {1, 2, 3, 4, 5, 6}; T y; y = x; y.e; })); ASSERT(2, ({ typedef struct { int a, b; } T; T x = {1, 2}; T y, z; z = y = x; z.b; })); ASSERT(1, ({ typedef struct { int a, b; } T; T x = {1, 2}; T y = x; y.a; })); ASSERT(4, ({ union { int a; char b[4]; } x = {0x01020304}; x.b[0]; })); ASSERT(3, ({ union { int a; char b[4]; } x = {0x01020304}; x.b[1]; })); ASSERT(0x01020304, ({ union { struct { char a, b, c, d; } e; int f; } x = {{4, 3, 2, 1}}; x.f; })); ASSERT(3, g3); ASSERT(4, g4); ASSERT(5, g5); ASSERT(6, g6); ASSERT(0, g9[0]); ASSERT(1, g9[1]); ASSERT(2, g9[2]); ASSERT(1, g11[0].a); ASSERT(2, g11[0].b); ASSERT(3, g11[1].a); ASSERT(4, g11[1].b); ASSERT(1, g12[0].a[0]); ASSERT(2, g12[0].a[1]); ASSERT(0, g12[1].a[0]); ASSERT(0, g12[1].a[1]); ASSERT(4, g13[0].b[0]); ASSERT(3, g13[0].b[1]); ASSERT(8, g13[1].b[0]); ASSERT(7, g13[1].b[1]); ASSERT(7, sizeof(g17)); ASSERT(10, sizeof(g18)); ASSERT(3, sizeof(g19)); ASSERT(0, memcmp(g17, "foobar", 7)); ASSERT(0, memcmp(g18, "foobar\0\0\0", 10)); ASSERT(0, memcmp(g19, "foo", 3)); ASSERT(0, strcmp(g20, "foobar")); ASSERT(0, strcmp(g21, "bar")); ASSERT(0, strcmp(g22 + 3, "foobar")); ASSERT(0, strcmp(g23[0], "foobar")); ASSERT(0, strcmp(g23[1], "bar")); ASSERT(0, strcmp(g23[2] + 3, "foobar")); ASSERT(3, g24); ASSERT(3, *g25); ASSERT(2, *g27); ASSERT(3, *g28); ASSERT(1, *(int *)g29); ASSERT(1, g31[0]); ASSERT(2, g31[1]); ASSERT(3, g31[2]); ASSERT(1, g40[0].a[0]); ASSERT(2, g40[0].a[1]); ASSERT(3, g40[1].a[0]); ASSERT(4, g40[1].a[1]); ASSERT(1, g41[0].a[0]); ASSERT(2, g41[0].a[1]); ASSERT(3, g41[1].a[0]); ASSERT(4, g41[1].a[1]); ASSERT(0, ({ int x[2][3] = {0, 1, 2, 3, 4, 5}; x[0][0]; })); ASSERT(3, ({ int x[2][3] = {0, 1, 2, 3, 4, 5}; x[1][0]; })); ASSERT(0, ({ struct { int a; int b; } x[2] = {0, 1, 2, 3}; x[0].a; })); ASSERT(2, ({ struct { int a; int b; } x[2] = {0, 1, 2, 3}; x[1].a; })); ASSERT(0, strcmp(g43[0], "foo")); ASSERT(0, strcmp(g43[1], "bar")); ASSERT(0, strcmp(g44, "foo")); ASSERT(3, ({ int a[] = { 1, 2, 3, }; a[2]; })); ASSERT(1, ({ struct { int a, b, c; } x = { 1, 2, 3, }; x.a; })); ASSERT(1, ({ union { int a; char b; } x = { 1, }; x.a; })); ASSERT(2, ({ enum { x, y, z, }; z; })); ASSERT(3, sizeof(g60)); ASSERT(6, sizeof(g61)); ASSERT(4, sizeof(g65)); ASSERT(7, sizeof(g66)); ASSERT(0, strcmp(g65.b, "oo")); ASSERT(0, strcmp(g66.b, "oobar")); ASSERT(4, ({ int x[3] = {1, 2, 3, [0] = 4, 5}; x[0]; })); ASSERT(5, ({ int x[3] = {1, 2, 3, [0] = 4, 5}; x[1]; })); ASSERT(3, ({ int x[3] = {1, 2, 3, [0] = 4, 5}; x[2]; })); ASSERT(10, ({ int x[2][3] = {1, 2, 3, 4, 5, 6, [0][1] = 7, 8, [0] = 9, [0] = 10, 11, [1][0] = 12}; x[0][0]; })); ASSERT(11, ({ int x[2][3] = {1, 2, 3, 4, 5, 6, [0][1] = 7, 8, [0] = 9, [0] = 10, 11, [1][0] = 12}; x[0][1]; })); ASSERT(8, ({ int x[2][3] = {1, 2, 3, 4, 5, 6, [0][1] = 7, 8, [0] = 9, [0] = 10, 11, [1][0] = 12}; x[0][2]; })); ASSERT(12, ({ int x[2][3] = {1, 2, 3, 4, 5, 6, [0][1] = 7, 8, [0] = 9, [0] = 10, 11, [1][0] = 12}; x[1][0]; })); ASSERT(5, ({ int x[2][3] = {1, 2, 3, 4, 5, 6, [0][1] = 7, 8, [0] = 9, [0] = 10, 11, [1][0] = 12}; x[1][1]; })); ASSERT(6, ({ int x[2][3] = {1, 2, 3, 4, 5, 6, [0][1] = 7, 8, [0] = 9, [0] = 10, 11, [1][0] = 12}; x[1][2]; })); ASSERT(7, ({ int x[2][3] = {1, 2, 3, 4, 5, 6, [0] = {7, 8}, 9, 10}; x[0][0]; })); ASSERT(8, ({ int x[2][3] = {1, 2, 3, 4, 5, 6, [0] = {7, 8}, 9, 10}; x[0][1]; })); ASSERT(3, ({ int x[2][3] = {1, 2, 3, 4, 5, 6, [0] = {7, 8}, 9, 10}; x[0][2]; })); ASSERT(9, ({ int x[2][3] = {1, 2, 3, 4, 5, 6, [0] = {7, 8}, 9, 10}; x[1][0]; })); ASSERT(10, ({ int x[2][3] = {1, 2, 3, 4, 5, 6, [0] = {7, 8}, 9, 10}; x[1][1]; })); ASSERT(6, ({ int x[2][3] = {1, 2, 3, 4, 5, 6, [0] = {7, 8}, 9, 10}; x[1][2]; })); ASSERT(7, ((int[10]){[3] = 7})[3]); ASSERT(0, ((int[10]){[3] = 7})[4]); ASSERT(10, ({ char x[] = {[10 - 3] = 1, 2, 3}; sizeof(x); })); ASSERT(20, ({ char x[][2] = {[8][1] = 1, 2}; sizeof(x); })); ASSERT(3, sizeof(g60)); ASSERT(6, sizeof(g61)); ASSERT(4, sizeof(g65)); ASSERT(7, sizeof(g66)); ASSERT(0, strcmp(g65.b, "oo")); ASSERT(0, strcmp(g66.b, "oobar")); ASSERT(7, ((int[10]){[3] 7})[3]); ASSERT(0, ((int[10]){[3] 7})[4]); ASSERT(4, ({ struct { int a, b; } x = {1, 2, .b = 3, .a = 4}; x.a; })); ASSERT(3, ({ struct { int a, b; } x = {1, 2, .b = 3, .a = 4}; x.b; })); ASSERT(1, ({ struct { struct { int a, b; } c; } x = {.c = 1, 2}; x.c.a; })); ASSERT(2, ({ struct { struct { int a, b; } c; } x = {.c = 1, 2}; x.c.b; })); ASSERT(0, ({ struct { struct { int a, b; } c; } x = {.c.b = 1}; x.c.a; })); ASSERT(1, ({ struct { struct { int a, b; } c; } x = {.c.b = 1}; x.c.b; })); ASSERT(1, ({ struct { int a[2]; } x = {.a = 1, 2}; x.a[0]; })); ASSERT(2, ({ struct { int a[2]; } x = {.a = 1, 2}; x.a[1]; })); ASSERT(0, ({ struct { int a[2]; } x = {.a[1] = 1}; x.a[0]; })); ASSERT(1, ({ struct { int a[2]; } x = {.a[1] = 1}; x.a[1]; })); ASSERT(3, ({ struct { int a, b; } x[] = { [1].b = 1, 2, [0] = 3, 4, }; x[0].a; })); ASSERT(4, ({ struct { int a, b; } x[] = { [1].b = 1, 2, [0] = 3, 4, }; x[0].b; })); ASSERT(0, ({ struct { int a, b; } x[] = { [1].b = 1, 2, [0] = 3, 4, }; x[1].a; })); ASSERT(1, ({ struct { int a, b; } x[] = { [1].b = 1, 2, [0] = 3, 4, }; x[1].b; })); ASSERT(2, ({ struct { int a, b; } x[] = { [1].b = 1, 2, [0] = 3, 4, }; x[2].a; })); ASSERT(0, ({ struct { int a, b; } x[] = { [1].b = 1, 2, [0] = 3, 4, }; x[2].b; })); ASSERT(1, ({ typedef struct { int a, b; } T; T x = {1, 2}; T y[] = {x}; y[0].a; })); ASSERT(2, ({ typedef struct { int a, b; } T; T x = {1, 2}; T y[] = {x}; y[0].b; })); ASSERT(0, ({ typedef struct { int a, b; } T; T x = {1, 2}; T y[] = {x, [0].b = 3}; y[0].a; })); ASSERT(3, ({ typedef struct { int a, b; } T; T x = {1, 2}; T y[] = {x, [0].b = 3}; y[0].b; })); ASSERT(5, ((struct { int a, b, c; }){.c = 5}).c); ASSERT(0, ((struct { int a, b, c; }){.c = 5}).a); ASSERT(0x00ff, ({ union { unsigned short a; char b[2]; } x = {.b[0] = 0xff}; x.a; })); ASSERT(0xff00, ({ union { unsigned short a; char b[2]; } x = {.b[1] = 0xff}; x.a; })); ASSERT(0x00120000, g50.a); ASSERT(0, g51[0].a); ASSERT(0, g51[1].a); ASSERT(1, ({ struct { struct { int a; struct { int b; }; }; int c; } x = {1, 2, 3, .b = 4, 5}; x.a; })); ASSERT(4, ({ struct { struct { int a; struct { int b; }; }; int c; } x = {1, 2, 3, .b = 4, 5}; x.b; })); ASSERT(5, ({ struct { struct { int a; struct { int b; }; }; int c; } x = {1, 2, 3, .b = 4, 5}; x.c; })); ASSERT(16, ({ char x[] = {[2 ... 10] = 'a', [7] = 'b', [15 ... 15] = 'c', [3 ... 5] = 'd'}; sizeof(x); })); ASSERT(0, ({ char x[] = {[2 ... 10] = 'a', [7] = 'b', [15 ... 15] = 'c', [3 ... 5] = 'd'}; memcmp(x, "\0\0adddabaaa\0\0\0\0c", 16); })); return 0; }
16,331
803
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/macro_test.c
#include "third_party/chibicc/test/include1.h" #include "third_party/chibicc/test/test.h" /* clang-format off */ char *main_filename1 = __FILE__; int main_line1 = __LINE__; #define LINE() __LINE__ int main_line2 = LINE(); #define add2 add2 #define add6(a, b, c, d, e, f) add6(a, b, c, d, e, f) # /* */ # int ret3(void) { return 3; } int dbl(int x) { return x*x; } int add2(int x, int y) { return x + y; } int add6(int a, int b, int c, int d, int e, int f) { return a + b + c + d + e + f; } int main() { ASSERT(5, include1); ASSERT(7, include2); /* [jart] breaks mkdeps */ /* #if 0 */ /* #include "/no/such/file" */ /* ASSERT(0, 1); */ /* #if nested */ /* #endif */ /* #endif */ int m = 0; #if 1 m = 5; #endif ASSERT(5, m); #if 1 # if 0 # if 1 foo bar # endif # endif m = 3; #endif ASSERT(3, m); #if 1-1 # if 1 # endif # if 1 # else # endif # if 0 # else # endif m = 2; #else # if 1 m = 3; # endif #endif ASSERT(3, m); #if 1 m = 2; #else m = 3; #endif ASSERT(2, m); #if 1 m = 2; #else m = 3; #endif ASSERT(2, m); #if 0 m = 1; #elif 0 m = 2; #elif 3+5 m = 3; #elif 1*5 m = 4; #endif ASSERT(3, m); #if 1+5 m = 1; #elif 1 m = 2; #elif 3 m = 2; #endif ASSERT(1, m); #if 0 m = 1; #elif 1 # if 1 m = 2; # else m = 3; # endif #else m = 5; #endif ASSERT(2, m); int M1 = 5; #define M1 3 ASSERT(3, M1); #define M1 4 ASSERT(4, M1); #define M1 3+4+ ASSERT(12, M1 5); #define M1 3+4 ASSERT(23, M1*5); #define ASSERT_ Assert( #define if 5 #define five "5" #define END ) ASSERT_ 5, if, five END; #undef ASSERT_ #undef if #undef five #undef END if (0); #define M 5 #if M m = 5; #else m = 6; #endif ASSERT(5, m); #define M 5 #if M-5 m = 6; #elif M m = 5; #endif ASSERT(5, m); int M2 = 6; #define M2 M2 + 3 ASSERT(9, M2); #define M3 M2 + 3 ASSERT(12, M3); int M4 = 3; #define M4 M5 * 5 #define M5 M4 + 2 ASSERT(13, M4); #ifdef M6 m = 5; #else m = 3; #endif ASSERT(3, m); #define M6 #ifdef M6 m = 5; #else m = 3; #endif ASSERT(5, m); #ifndef M7 m = 3; #else m = 5; #endif ASSERT(3, m); #define M7 #ifndef M7 m = 3; #else m = 5; #endif ASSERT(5, m); #if 0 #ifdef NO_SUCH_MACRO #endif #ifndef NO_SUCH_MACRO #endif #else #endif #define M7() 1 int M7 = 5; ASSERT(1, M7()); ASSERT(5, M7); #define M7 () ASSERT(3, ret3 M7); #define M8(x,y) x+y ASSERT(7, M8(3, 4)); #define M8(x,y) x*y ASSERT(24, M8(3+4, 4+5)); #define M8(x,y) (x)*(y) ASSERT(63, M8(3+4, 4+5)); #define M8(x,y) x y ASSERT(9, M8(, 4+5)); #define M8(x,y) x*y ASSERT(20, M8((2+3), 4)); #define M8(x,y) x*y ASSERT(12, M8((2,3), 4)); #define dbl(x) M10(x) * x #define M10(x) dbl(x) + 3 ASSERT(10, dbl(2)); #define M11(x) #x ASSERT('a', M11( a!b `""c)[0]); ASSERT('!', M11( a!b `""c)[1]); ASSERT('b', M11( a!b `""c)[2]); ASSERT(' ', M11( a!b `""c)[3]); ASSERT('`', M11( a!b `""c)[4]); ASSERT('"', M11( a!b `""c)[5]); ASSERT('"', M11( a!b `""c)[6]); ASSERT('c', M11( a!b `""c)[7]); ASSERT(0, M11( a!b `""c)[8]); #define paste(x,y) x##y ASSERT(15, paste(1,5)); ASSERT(255, paste(0,xff)); ASSERT(3, ({ int foobar=3; paste(foo,bar); })); ASSERT(5, paste(5,)); ASSERT(5, paste(,5)); #define i 5 ASSERT(101, ({ int i3=100; paste(1+i,3); })); #undef i #define paste2(x) x##5 ASSERT(26, paste2(1+2)); #define paste3(x) 2##x ASSERT(23, paste3(1+2)); #define paste4(x, y, z) x##y##z ASSERT(123, paste4(1,2,3)); #define M12 #if defined(M12) m = 3; #else m = 4; #endif ASSERT(3, m); #define M12 #if defined M12 m = 3; #else m = 4; #endif ASSERT(3, m); #if defined(M12) - 1 m = 3; #else m = 4; #endif ASSERT(4, m); #if defined(NO_SUCH_MACRO) m = 3; #else m = 4; #endif ASSERT(4, m); #if no_such_symbol == 0 m = 5; #else m = 6; #endif ASSERT(5, m); #define STR(x) #x #define M12(x) STR(x) #define M13(x) M12(foo.x) ASSERT(0, strcmp(M13(bar), "foo.bar")); #define M13(x) M12(foo. x) ASSERT(0, strcmp(M13(bar), "foo. bar")); #define M12 foo #define M13(x) STR(x) #define M14(x) M13(x.M12) ASSERT(0, strcmp(M14(bar), "bar.foo")); #define M14(x) M13(x. M12) ASSERT(0, strcmp(M14(bar), "bar. foo")); #include "third_party/chibicc/test/include3.h" ASSERT(3, foo); #include "third_party/chibicc/test/include4.h" ASSERT(4, foo); #define M13 "third_party/chibicc/test/include3.h" #include M13 ASSERT(3, foo); #define M13 < third_party/chibicc/test/include4.h #include M13 > ASSERT(4, foo); #undef foo ASSERT(1, __STDC__); ASSERT(0, strcmp(main_filename1, "third_party/chibicc/test/macro_test.c")); ASSERT(5, main_line1); ASSERT(7, main_line2); ASSERT(0, strcmp(include1_filename, "third_party/chibicc/test/include1.h")); ASSERT(4, include1_line); #define M14(...) 3 ASSERT(3, M14()); #define M14(...) __VA_ARGS__ ASSERT(2, M14() 2); ASSERT(5, M14(5)); #define M14(...) add2(__VA_ARGS__) ASSERT(8, M14(2, 6)); #define M14(...) add6(1,2,__VA_ARGS__,6) ASSERT(21, M14(3,4,5)); #define M14(x, ...) add6(1,2,x,__VA_ARGS__,6) ASSERT(21, M14(3,4,5)); #define M14(args...) 3 ASSERT(3, M14()); #define M14(x, ...) x ASSERT(5, M14(5)); #define M14(args...) args ASSERT(2, M14() 2); ASSERT(5, M14(5)); #define M14(args...) add2(args) ASSERT(8, M14(2, 6)); #define M14(args...) add6(1,2,args,6) ASSERT(21, M14(3,4,5)); #define M14(x, args...) add6(1,2,x,args,6) ASSERT(21, M14(3,4,5)); #define M14(x, args...) x ASSERT(5, M14(5)); #define CONCAT(x,y) x##y ASSERT(5, ({ int f0zz=5; CONCAT(f,0zz); })); ASSERT(5, ({ CONCAT(4,.57) + 0.5; })); ASSERT(11, strlen(__DATE__)); ASSERT(8, strlen(__TIME__)); ASSERT(0, __COUNTER__); ASSERT(1, __COUNTER__); ASSERT(2, __COUNTER__); ASSERT(24, strlen(__TIMESTAMP__)); ASSERT(0, strcmp(__BASE_FILE__, "third_party/chibicc/test/macro_test.c")); #define M30(buf, fmt, ...) sprintf(buf, fmt __VA_OPT__(,) __VA_ARGS__) ASSERT(0, ({ char buf[100]; M30(buf, "foo"); strcmp(buf, "foo"); })); ASSERT(0, ({ char buf[100]; M30(buf, "foo%d", 3); strcmp(buf, "foo3"); })); ASSERT(0, ({ char buf[100]; M30(buf, "foo%d%d", 3, 5); strcmp(buf, "foo35"); })); #define M31(buf, fmt, ...) sprintf(buf, fmt, ## __VA_ARGS__) ASSERT(0, ({ char buf[100]; M31(buf, "foo"); strcmp(buf, "foo"); })); ASSERT(0, ({ char buf[100]; M31(buf, "foo%d", 3); strcmp(buf, "foo3"); })); ASSERT(0, ({ char buf[100]; M31(buf, "foo%d%d", 3, 5); strcmp(buf, "foo35"); })); #define M31(x, y) (1, ##x y) ASSERT(3, M31(, 3)); return 0; }
6,540
417
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/sizeof_test.c
#include "third_party/chibicc/test/test.h" int main() { ASSERT(1, sizeof(char)); ASSERT(2, sizeof(short)); ASSERT(2, sizeof(short int)); ASSERT(2, sizeof(int short)); ASSERT(4, sizeof(int)); ASSERT(8, sizeof(long)); ASSERT(8, sizeof(long int)); ASSERT(8, sizeof(long int)); ASSERT(8, sizeof(char *)); ASSERT(8, sizeof(int *)); ASSERT(8, sizeof(long *)); ASSERT(8, sizeof(int **)); ASSERT(8, sizeof(int(*)[4])); ASSERT(32, sizeof(int *[4])); ASSERT(16, sizeof(int[4])); ASSERT(48, sizeof(int[3][4])); ASSERT(8, sizeof(struct { int a; int b; })); ASSERT(8, sizeof(-10 + (long)5)); ASSERT(8, sizeof(-10 - (long)5)); ASSERT(8, sizeof(-10 * (long)5)); ASSERT(8, sizeof(-10 / (long)5)); ASSERT(8, sizeof((long)-10 + 5)); ASSERT(8, sizeof((long)-10 - 5)); ASSERT(8, sizeof((long)-10 * 5)); ASSERT(8, sizeof((long)-10 / 5)); ASSERT(1, ({ char i; sizeof(++i); })); ASSERT(1, ({ char i; sizeof(i++); })); ASSERT(8, sizeof(int(*)[10])); ASSERT(8, sizeof(int(*)[][10])); ASSERT(4, sizeof(struct { int x, y[]; })); ASSERT(1, sizeof(char)); ASSERT(1, sizeof(signed char)); ASSERT(1, sizeof(signed char signed)); ASSERT(1, sizeof(unsigned char)); ASSERT(1, sizeof(unsigned char unsigned)); ASSERT(2, sizeof(short)); ASSERT(2, sizeof(int short)); ASSERT(2, sizeof(short int)); ASSERT(2, sizeof(signed short)); ASSERT(2, sizeof(int short signed)); ASSERT(2, sizeof(unsigned short)); ASSERT(2, sizeof(int short unsigned)); ASSERT(4, sizeof(int)); ASSERT(4, sizeof(signed int)); ASSERT(4, sizeof(signed)); ASSERT(4, sizeof(signed signed)); ASSERT(4, sizeof(unsigned int)); ASSERT(4, sizeof(unsigned)); ASSERT(4, sizeof(unsigned unsigned)); ASSERT(8, sizeof(long)); ASSERT(8, sizeof(signed long)); ASSERT(8, sizeof(signed long int)); ASSERT(8, sizeof(unsigned long)); ASSERT(8, sizeof(unsigned long int)); ASSERT(8, sizeof(long long)); ASSERT(8, sizeof(signed long long)); ASSERT(8, sizeof(signed long long int)); ASSERT(8, sizeof(unsigned long long)); ASSERT(8, sizeof(unsigned long long int)); ASSERT(1, sizeof((char)1)); ASSERT(2, sizeof((short)1)); ASSERT(4, sizeof((int)1)); ASSERT(8, sizeof((long)1)); ASSERT(4, sizeof((char)1 + (char)1)); ASSERT(4, sizeof((short)1 + (short)1)); ASSERT(4, sizeof(1 ? 2 : 3)); ASSERT(4, sizeof(1 ? (short)2 : (char)3)); ASSERT(8, sizeof(1 ? (long)2 : (char)3)); ASSERT(1, sizeof(char) << 31 >> 31); ASSERT(1, sizeof(char) << 63 >> 63); ASSERT(4, sizeof(float)); ASSERT(8, sizeof(double)); ASSERT(4, sizeof(1f + 2)); ASSERT(8, sizeof(1.0 + 2)); ASSERT(4, sizeof(1f - 2)); ASSERT(8, sizeof(1.0 - 2)); ASSERT(4, sizeof(1f * 2)); ASSERT(8, sizeof(1.0 * 2)); ASSERT(4, sizeof(1f / 2)); ASSERT(8, sizeof(1.0 / 2)); ASSERT(16, sizeof(long double)); ASSERT(1, sizeof(main)); ASSERT(1, sizeof("")); ASSERT(2, sizeof("h")); ASSERT(6, sizeof("hello")); return 0; }
3,057
118
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/literal_test.c
#include "third_party/chibicc/test/test.h" int main() { ASSERT(97, 'a'); ASSERT(10, '\n'); ASSERT(-128, '\x80'); ASSERT(511, 0777); ASSERT(0, 0x0); ASSERT(10, 0xa); ASSERT(10, 0XA); ASSERT(48879, 0xbeef); ASSERT(48879, 0xBEEF); ASSERT(48879, 0XBEEF); ASSERT(0, 0b0); ASSERT(1, 0b1); ASSERT(47, 0b101111); ASSERT(47, 0B101111); ASSERT(4, sizeof(0)); ASSERT(8, sizeof(0L)); ASSERT(8, sizeof(0LU)); ASSERT(8, sizeof(0UL)); ASSERT(8, sizeof(0LL)); ASSERT(8, sizeof(0LLU)); ASSERT(8, sizeof(0Ull)); ASSERT(8, sizeof(0l)); ASSERT(8, sizeof(0ll)); ASSERT(8, sizeof(0x0L)); ASSERT(8, sizeof(0b0L)); ASSERT(4, sizeof(2147483647)); ASSERT(8, sizeof(2147483648)); ASSERT(-1, 0xffffffffffffffff); ASSERT(8, sizeof(0xffffffffffffffff)); ASSERT(4, sizeof(4294967295U)); ASSERT(8, sizeof(4294967296U)); ASSERT(3, -1U >> 30); ASSERT(3, -1Ul >> 62); ASSERT(3, -1ull >> 62); ASSERT(1, 0xffffffffffffffffl >> 63); ASSERT(1, 0xffffffffffffffffll >> 63); ASSERT(-1, 18446744073709551615); ASSERT(8, sizeof(18446744073709551615)); ASSERT(-1, 18446744073709551615 >> 63); ASSERT(-1, 0xffffffffffffffff); ASSERT(8, sizeof(0xffffffffffffffff)); ASSERT(1, 0xffffffffffffffff >> 63); ASSERT(-1, 01777777777777777777777); ASSERT(8, sizeof(01777777777777777777777)); ASSERT(1, 01777777777777777777777 >> 63); ASSERT(-1, 0b1111111111111111111111111111111111111111111111111111111111111111); ASSERT( 8, sizeof( 0b1111111111111111111111111111111111111111111111111111111111111111)); ASSERT( 1, 0b1111111111111111111111111111111111111111111111111111111111111111 >> 63); ASSERT(8, sizeof(2147483648)); ASSERT(4, sizeof(2147483647)); ASSERT(8, sizeof(0x1ffffffff)); ASSERT(4, sizeof(0xffffffff)); ASSERT(1, 0xffffffff >> 31); ASSERT(8, sizeof(040000000000)); ASSERT(4, sizeof(037777777777)); ASSERT(1, 037777777777 >> 31); ASSERT(8, sizeof(0b111111111111111111111111111111111)); ASSERT(4, sizeof(0b11111111111111111111111111111111)); ASSERT(1, 0b11111111111111111111111111111111 >> 31); ASSERT(-1, 1 << 31 >> 31); ASSERT(-1, 01 << 31 >> 31); ASSERT(-1, 0x1 << 31 >> 31); ASSERT(-1, 0b1 << 31 >> 31); 0.0; 1.0; 3e+8; 0x10.1p0; .1E4f; ASSERT(4, sizeof(8f)); ASSERT(4, sizeof(0.3F)); ASSERT(8, sizeof(0.)); ASSERT(8, sizeof(.0)); ASSERT(16, sizeof(5.l)); ASSERT(16, sizeof(2.0L)); Assert(1, size\ of(char), "sizeof(char)"); ASSERT(4, sizeof(L'\0')); ASSERT(97, L'a'); return 0; }
2,567
109
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/typeof_test.c
#include "third_party/chibicc/test/test.h" int main() { ASSERT(3, ({ typeof(int) x = 3; x; })); ASSERT(3, ({ typeof(1) x = 3; x; })); ASSERT(4, ({ int x; typeof(x) y; sizeof(y); })); ASSERT(8, ({ int x; typeof(&x) y; sizeof(y); })); ASSERT(4, ({ typeof("foo") x; sizeof(x); })); ASSERT(12, sizeof(typeof(struct { int a, b, c; }))); ASSERT(3, ({ label: 3; })); ASSERT(3, ({ __typeof(int) x = 3; x; })); }
662
36
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/bitfield_test.c
#include "third_party/chibicc/test/test.h" #define TYPE_SIGNED(type) (((type)-1) < 0) struct { char a; int b : 5; int c : 10; } g45 = {1, 2, 3}, g46 = {}; int main() { /* NOTE: Consistent w/ GCC (but MSVC would fail this) */ ASSERT(1, 2 == ({ struct { enum { a, b, c } e : 2; } x = { .e = 2, }; x.e; })); /* NOTE: GCC forbids typeof(bitfield). */ ASSERT(0, ({ struct { enum { a, b, c } e : 2; } x = { .e = 2, }; TYPE_SIGNED(typeof(x.e)); })); ASSERT(4, sizeof(struct { int x : 1; })); ASSERT(8, sizeof(struct { long x : 1; })); struct bit1 { short a; char b; int c : 2; int d : 3; int e : 3; }; ASSERT(4, sizeof(struct bit1)); ASSERT(1, ({ struct bit1 x; x.a = 1; x.b = 2; x.c = 3; x.d = 4; x.e = 5; x.a; })); ASSERT(1, ({ struct bit1 x = {1, 2, 3, 4, 5}; x.a; })); ASSERT(2, ({ struct bit1 x = {1, 2, 3, 4, 5}; x.b; })); ASSERT(-1, ({ struct bit1 x = {1, 2, 3, 4, 5}; x.c; })); ASSERT(-4, ({ struct bit1 x = {1, 2, 3, 4, 5}; x.d; })); ASSERT(-3, ({ struct bit1 x = {1, 2, 3, 4, 5}; x.e; })); ASSERT(1, g45.a); ASSERT(2, g45.b); ASSERT(3, g45.c); ASSERT(0, g46.a); ASSERT(0, g46.b); ASSERT(0, g46.c); typedef struct { int a : 10; int b : 10; int c : 10; } T3; ASSERT(1, ({ T3 x = {1, 2, 3}; x.a++; })); ASSERT(2, ({ T3 x = {1, 2, 3}; x.b++; })); ASSERT(3, ({ T3 x = {1, 2, 3}; x.c++; })); ASSERT(2, ({ T3 x = {1, 2, 3}; ++x.a; })); ASSERT(3, ({ T3 x = {1, 2, 3}; ++x.b; })); ASSERT(4, ({ T3 x = {1, 2, 3}; ++x.c; })); ASSERT(4, sizeof(struct { int a : 3; int c : 1; int c : 5; })); ASSERT(8, sizeof(struct { int a : 3; int : 0; int c : 5; })); ASSERT(4, sizeof(struct { int a : 3; int : 0; })); typedef struct { long p : 1; long a : 40; long b : 20; } T4; ASSERT(0xfffffffffffaaaaa, ({ T4 x = {1, 0x31337313371337, 0xaaaaaaaaaaaaaaaa}; x.b++; })); ASSERT(0x7313371337, ({ T4 x = {1, 0x31337313371337, 0xaaaaaaaaaaaaaaaa}; x.a++; })); ASSERT(0xffffffffffffffd4, ({ T4 x; x.b = -44; x.b++; })); return 0; }
2,814
152
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/include3.h
#define foo 3
14
2
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/complit_test.c
#include "third_party/chibicc/test/test.h" typedef struct Tree { int val; struct Tree *lhs; struct Tree *rhs; } Tree; Tree *tree = &(Tree){1, &(Tree){2, &(Tree){3, 0, 0}, &(Tree){4, 0, 0}}, 0}; int main() { ASSERT(1, (int){1}); ASSERT(2, ((int[]){0, 1, 2})[2]); ASSERT('a', ((struct { char a; int b; }){'a', 3}) .a); ASSERT(3, ({ int x = 3; (int){x}; })); (int){3} = 5; ASSERT(1, tree->val); ASSERT(2, tree->lhs->val); ASSERT(3, tree->lhs->lhs->val); ASSERT(4, tree->lhs->rhs->val); return 0; }
621
32
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/include4.h
#define foo 4
14
2
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/builtin_test.c
#include "libc/math.h" #include "third_party/chibicc/test/test.h" #define FPNAN 0 #define FPINFINITE 1 #define FPZERO 2 #define FPSUBNORMAL 3 #define FPNORMAL 4 #define FPCLASSIFY(x) \ __builtin_fpclassify(FPNAN, FPINFINITE, FPNORMAL, FPSUBNORMAL, FPZERO, x) #define conceal(x) \ ({ \ typeof(x) cloak = x; \ asm("" : "+r"(cloak)); \ cloak; \ }) struct frame { struct frame *next; intptr_t addr; }; struct frame *frame(void) { struct frame *myframe = __builtin_frame_address(0); struct frame *parentframe = myframe->next; return parentframe; } void test_frame_address(void) { ASSERT(1, __builtin_frame_address(0) == frame()); } void test_constant(void) { ASSERT(1, __builtin_constant_p(1)); ASSERT(0, __builtin_constant_p(conceal(1))); ASSERT(0, __builtin_constant_p(stdin)); ASSERT(1, __builtin_constant_p(__builtin_popcount(0b10111011))); ASSERT(1, __builtin_constant_p(__builtin_popcountll((size_t)0b10111011))); } void test_ignored(void) { ASSERT(123, __builtin_assume_aligned(123, 8)); ASSERT(123, __builtin_assume_aligned(123, 32, 8)); ASSERT(123, __builtin_expect(123, 0)); } void __attribute__((__aligned__(16))) test_clz(void) { ASSERT(31, __builtin_clz(1)); ASSERT(63, __builtin_clzl(1)); ASSERT(63, __builtin_clzll(1)); ASSERT(25, __builtin_clz(77)); ASSERT(4, __builtin_clz(0x08000000)); ASSERT(4, __builtin_clz(0xfff08000000)); ASSERT(25, __builtin_clz(conceal(77))); __builtin_clz(conceal(77)); } __attribute__((__weak__)) void test_ctz(void) { ASSERT(0, __builtin_ctz(1)); ASSERT(0, __builtin_ctz(77)); ASSERT(27, __builtin_ctz(0x08000000)); ASSERT(27, __builtin_ctz(0xffff08000000)); ASSERT(63, __builtin_ctzl(0x8000000000000000)); ASSERT(63, __builtin_ctzll(0x8000000000000000)); ASSERT(0, __builtin_ctz(conceal(77))); } void test_ffs(void) { ASSERT(0, __builtin_ffs(0)); ASSERT(1, __builtin_ffs(1)); ASSERT(1, __builtin_ffs(77)); ASSERT(28, __builtin_ffs(0x08000000)); ASSERT(28, __builtin_ffs(0xffff08000000)); ASSERT(1, __builtin_ffs(conceal(77))); } void test_popcnt(void) { ASSERT(0, __builtin_popcount(0)); ASSERT(1, __builtin_popcount(1)); ASSERT(6, __builtin_popcount(0b10111011)); ASSERT(6, __builtin_popcountl(0b10111011)); ASSERT(6, __builtin_popcountll(0xbb00000000000000)); ASSERT(6, __builtin_popcountl(conceal(0b10111011))); } void test_bswap(void) { ASSERT(0x3412, __builtin_bswap16(0x1234)); ASSERT(0x78563412, __builtin_bswap32(0x12345678)); ASSERT(0xefcdab8967452301, __builtin_bswap64(0x0123456789abcdef)); ASSERT(0xefcdab89, __builtin_bswap32(0x0123456789abcdef)); ASSERT(0x78563412, __builtin_bswap32(conceal(0x12345678))); } void test_fpclassify(void) { float minf = __FLT_MIN__; double min = __DBL_MIN__; long double minl = __LDBL_MIN__; ASSERT(FPZERO, FPCLASSIFY(.0f)); ASSERT(FPZERO, FPCLASSIFY(.0)); ASSERT(FPZERO, FPCLASSIFY(.0L)); ASSERT(FPNORMAL, FPCLASSIFY(1.f)); ASSERT(FPNORMAL, FPCLASSIFY(1.)); ASSERT(FPNORMAL, FPCLASSIFY(1.L)); ASSERT(FPNORMAL, FPCLASSIFY(1.f)); ASSERT(FPNORMAL, FPCLASSIFY(1.)); ASSERT(FPNORMAL, FPCLASSIFY(1.L)); ASSERT(FPNORMAL, FPCLASSIFY(minf)); ASSERT(FPNORMAL, FPCLASSIFY(min)); ASSERT(FPNORMAL, FPCLASSIFY(minl)); ASSERT(FPSUBNORMAL, FPCLASSIFY(minf / 2)); ASSERT(FPSUBNORMAL, FPCLASSIFY(min / 2)); ASSERT(FPSUBNORMAL, FPCLASSIFY(minl / 2)); ASSERT(FPNAN, FPCLASSIFY(__builtin_nanf(""))); ASSERT(FPNAN, FPCLASSIFY(__builtin_nan(""))); ASSERT(FPNAN, FPCLASSIFY(__builtin_nanl(""))); } void test_logb(void) { ASSERT(6, __builtin_logbl(123.456)); ASSERT(logbl(123.456L), __builtin_logbl(123.456L)); ASSERT(logbl(__LDBL_MIN__), __builtin_logbl(__LDBL_MIN__)); ASSERT(logbl(__LDBL_MAX__), __builtin_logbl(__LDBL_MAX__)); } void test_fmax(void) { ASSERT(fmaxl(1, 2), __builtin_fmaxl(1, 2)); ASSERT(2, __builtin_fmaxl(__builtin_nanl(""), 2)); ASSERT(1, __builtin_fmaxl(1, __builtin_nanl(""))); ASSERT(2, fmaxl(nanl(""), 2)); ASSERT(1, fmaxl(1, nanl(""))); ASSERT(fmaxf(1, 2), __builtin_fmaxf(1, 2)); ASSERT(2, __builtin_fmaxf(__builtin_nanl(""), 2)); ASSERT(1, __builtin_fmaxf(1, __builtin_nanl(""))); ASSERT(2, fmaxf(nanl(""), 2)); ASSERT(1, fmaxf(1, nanl(""))); ASSERT(fmax(1, 2), __builtin_fmax(1, 2)); ASSERT(2, __builtin_fmax(__builtin_nanl(""), 2)); ASSERT(1, __builtin_fmax(1, __builtin_nanl(""))); ASSERT(2, fmax(nanl(""), 2)); ASSERT(1, fmax(1, nanl(""))); } void test_strlen(void) { ASSERT(5, strlen("hello")); ASSERT(5, __builtin_strlen("hello")); } void test_strchr(void) { ASSERT(1, __builtin_strchr("hello", 'z') == NULL); ASSERT(0, (strcmp)(__builtin_strchr("hello", 'e'), "ello")); } void test_strpbrk(void) { ASSERT(1, __builtin_strpbrk("hello", "z") == NULL); ASSERT(0, (strcmp)(__builtin_strpbrk("hello", "ze"), "ello")); } void test_strstr(void) { ASSERT(1, __builtin_strstr("hello", "sup") == NULL); ASSERT(0, (strcmp)(__builtin_strstr("hello", "ell"), "ello")); } void test_memcpy(void) { { char x[5] = {4, 3, 2, 1, 0}; char y[5] = {0, 1, 2, 3, 4}; char z[5] = {2, 3, 4, 1, 0}; ASSERT(1, x == (memcpy)(x, y + 2, 3)); ASSERT(0, memcmp(x, z, 5)); } { char x[5] = {4, 3, 2, 1, 0}; char y[5] = {0, 1, 2, 3, 4}; char z[5] = {2, 3, 4, 1, 0}; ASSERT(1, x == __builtin_memcpy(x, y + 2, 3)); ASSERT(0, memcmp(x, z, 5)); } { int n = 3; char x[5] = {4, 3, 2, 1, 0}; char y[5] = {0, 1, 2, 3, 4}; char z[5] = {2, 3, 4, 1, 0}; ASSERT(1, x == __builtin_memcpy(x, y + 2, n)); ASSERT(0, memcmp(x, z, 5)); } { char x[5] = {4, 3, 2, 1, 0}; ASSERT(1, x == __builtin_memcpy(x, x + 1, 4)); ASSERT(0, memcmp(x, (char[5]){3, 2, 1, 0, 0}, 5)); } } void test_add_overflow(void) { { int z; ASSERT(0, __builtin_add_overflow(2, 3, &z)); ASSERT(5, z); } { int x, y, z; x = 2; y = 3; ASSERT(0, __builtin_add_overflow(x, y, &z)); ASSERT(5, z); } { int x, y, z; x = 0x7fffffff; y = 1; ASSERT(1, __builtin_add_overflow(x, y, &z)); ASSERT(-2147483648, z); } { long x, y, z; x = 0x7fffffff; y = 1; ASSERT(0, __builtin_add_overflow(x, y, &z)); ASSERT(2147483648, z); } } void test_sub_overflow(void) { { int x, y, z; x = 2; y = 3; ASSERT(0, __builtin_sub_overflow(x, y, &z)); ASSERT(-1, z); } { int x, y, z; x = -2147483648; y = 1; ASSERT(1, __builtin_sub_overflow(x, y, &z)); ASSERT(2147483647, z); } { long x, y, z; x = -2147483648; y = 1; ASSERT(0, __builtin_sub_overflow(x, y, &z)); ASSERT(-2147483649, z); } } void test_mul_overflow(void) { { int x, y, z; x = 2; y = 3; ASSERT(0, __builtin_mul_overflow(x, y, &z)); ASSERT(6, z); } { int x, y, z; x = 2147483647; y = 2; ASSERT(1, __builtin_mul_overflow(x, y, &z)); ASSERT(-2, z); } { long x, y, z; x = 2147483647; y = 2; ASSERT(0, __builtin_mul_overflow(x, y, &z)); ASSERT(4294967294, z); } } void test_neg_overflow(void) { { int x, z; x = 2; ASSERT(0, __builtin_neg_overflow(x, &z)); ASSERT(-2, z); } { int x, z; x = -2147483648; ASSERT(1, __builtin_neg_overflow(x, &z)); ASSERT(-2147483648, z); } { long x, z; x = -2147483648; ASSERT(0, __builtin_neg_overflow(x, &z)); ASSERT(2147483648, z); } } void test_inf(void) { ASSERT(0, __builtin_isinf(0)); ASSERT(0, __builtin_isinf(1)); ASSERT(1, __builtin_isinf(__builtin_inff())); ASSERT(1, __builtin_isinf(-__builtin_inff())); ASSERT(1, __builtin_isinf(__builtin_inf())); ASSERT(1, __builtin_isinf(-__builtin_inf())); ASSERT(1, __builtin_isinf(__builtin_infl())); ASSERT(1, __builtin_isinf(-__builtin_infl())); ASSERT(1, __builtin_isfinite(0)); ASSERT(1, __builtin_isfinite(1)); ASSERT(0, __builtin_isfinite(__builtin_inff())); ASSERT(0, __builtin_isfinite(-__builtin_inff())); ASSERT(0, __builtin_isfinite(__builtin_inf())); ASSERT(0, __builtin_isfinite(-__builtin_inf())); ASSERT(0, __builtin_isfinite(__builtin_infl())); ASSERT(0, __builtin_isfinite(-__builtin_infl())); } void test_signbit(void) { ASSERT(0, !!__builtin_signbitf(0)); ASSERT(0, !!__builtin_signbitf(0.f)); ASSERT(0, !!__builtin_signbit(0.)); ASSERT(0, !!__builtin_signbitl(0.L)); ASSERT(1, !!__builtin_signbitf(-0.f)); ASSERT(1, !!__builtin_signbit(-0.)); ASSERT(1, !!__builtin_signbitl(-0.L)); ASSERT(0, !!__builtin_signbitf(__builtin_nanf(""))); ASSERT(1, !!__builtin_signbitf(-__builtin_nanf(""))); ASSERT(0, !!__builtin_signbit(__builtin_nan(""))); ASSERT(1, !!__builtin_signbit(-__builtin_nan(""))); ASSERT(0, !!__builtin_signbitl(__builtin_nanl(""))); ASSERT(1, !!__builtin_signbitl(-__builtin_nanl(""))); ASSERT(0, !!__builtin_signbitf(__builtin_inff())); ASSERT(1, !!__builtin_signbitf(-__builtin_inff())); ASSERT(0, !!__builtin_signbit(__builtin_inf())); ASSERT(1, !!__builtin_signbit(-__builtin_inf())); ASSERT(0, !!__builtin_signbitl(__builtin_infl())); ASSERT(1, !!__builtin_signbitl(-__builtin_infl())); } void test_nan(void) { ASSERT(0, __builtin_isnan(0)); ASSERT(0, __builtin_isnan(1)); ASSERT(1, __builtin_isnan(__builtin_nanf(""))); ASSERT(1, __builtin_isnan(-__builtin_nanf(""))); ASSERT(1, __builtin_isnan(__builtin_nan(""))); ASSERT(1, __builtin_isnan(-__builtin_nan(""))); ASSERT(1, __builtin_isnan(__builtin_nanl(""))); ASSERT(1, __builtin_isnan(-__builtin_nanl(""))); ASSERT(0, __builtin_isunordered(0, 0)); ASSERT(0, __builtin_isunordered(-1, 1)); ASSERT(1, __builtin_isunordered(0, __builtin_nanf(""))); ASSERT(1, __builtin_isunordered(__builtin_nanf(""), 0)); ASSERT(1, __builtin_isunordered(__builtin_nanf(""), __builtin_nanf(""))); } void test_double(void) { /* TODO */ /* ASSERT(1, __DBL_MIN__ < 0.0L); */ /* ASSERT(1, __DBL_MAX__ > 0.0L); */ } void test_types_compatible_p(void) { ASSERT(1, __builtin_types_compatible_p(int, int)); ASSERT(1, __builtin_types_compatible_p(double, double)); ASSERT(0, __builtin_types_compatible_p(int, long)); ASSERT(0, __builtin_types_compatible_p(long, float)); ASSERT(1, __builtin_types_compatible_p(int *, int *)); ASSERT(0, __builtin_types_compatible_p(short *, int *)); ASSERT(0, __builtin_types_compatible_p(int **, int *)); ASSERT(1, __builtin_types_compatible_p(const int, int)); ASSERT(0, __builtin_types_compatible_p(unsigned, int)); ASSERT(1, __builtin_types_compatible_p(signed, int)); ASSERT(0, __builtin_types_compatible_p( struct { int a; }, struct { int a; })); ASSERT(1, __builtin_types_compatible_p(int (*)(void), int (*)(void))); ASSERT(1, __builtin_types_compatible_p(void (*)(int), void (*)(int))); ASSERT(1, __builtin_types_compatible_p(void (*)(int, double), void (*)(int, double))); ASSERT(1, __builtin_types_compatible_p(int (*)(float, double), int (*)(float, double))); ASSERT(0, __builtin_types_compatible_p(int (*)(float, double), int)); ASSERT(0, __builtin_types_compatible_p(int (*)(float, double), int (*)(float))); ASSERT(0, __builtin_types_compatible_p(int (*)(float, double), int (*)(float, double, int))); ASSERT(1, __builtin_types_compatible_p(double (*)(...), double (*)(...))); ASSERT(0, __builtin_types_compatible_p(double (*)(...), double (*)(void))); ASSERT(1, ({ typedef struct { int a; } T; __builtin_types_compatible_p(T, T); })); ASSERT(1, ({ typedef struct { int a; } T; __builtin_types_compatible_p(T, const T); })); ASSERT(1, ({ struct { int a; int b; } x; __builtin_types_compatible_p(typeof(x.a), typeof(x.b)); })); } void test_offsetof(void) { ASSERT(0, ({ struct T { int a; int b; }; __builtin_offsetof(struct T, a); })); ASSERT(4, ({ struct T { int a; int b; }; __builtin_offsetof(struct T, b); })); } int main() { test_constant(); test_frame_address(); test_types_compatible_p(); test_clz(); test_ctz(); test_ffs(); test_bswap(); test_popcnt(); test_inf(); test_nan(); test_double(); test_fpclassify(); test_signbit(); test_memcpy(); test_offsetof(); test_ignored(); test_add_overflow(); test_sub_overflow(); test_mul_overflow(); test_neg_overflow(); test_strlen(); test_strchr(); test_strpbrk(); test_strstr(); test_logb(); test_fmax(); return 0; }
12,844
447
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/pragma-once_test.c
#include "third_party/chibicc/test/test.h" #pragma once #include "third_party/chibicc/test/pragma-once_test.c" int main() { return 0; }
141
10
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/atomic_test.c
#include "third_party/chibicc/test/test.h" _Atomic(int) lock; main() { int x; ASSERT(0, __atomic_exchange_n(&lock, 1, __ATOMIC_SEQ_CST)); __atomic_load(&lock, &x, __ATOMIC_SEQ_CST); ASSERT(1, x); ASSERT(1, __atomic_exchange_n(&lock, 2, __ATOMIC_SEQ_CST)); ASSERT(2, __atomic_exchange_n(&lock, 3, __ATOMIC_SEQ_CST)); ASSERT(3, __atomic_load_n(&lock, __ATOMIC_SEQ_CST)); __atomic_store_n(&lock, 0, __ATOMIC_SEQ_CST); ASSERT(0, __atomic_fetch_xor(&lock, 3, __ATOMIC_SEQ_CST)); ASSERT(3, __atomic_fetch_xor(&lock, 1, __ATOMIC_SEQ_CST)); ASSERT(2, __atomic_load_n(&lock, __ATOMIC_SEQ_CST)); // CAS success #1 x = 2; ASSERT(1, __atomic_compare_exchange_n(&lock, &x, 3, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)); ASSERT(2, x); ASSERT(3, lock); // CAS success #2 x = 3; ASSERT(1, __atomic_compare_exchange_n(&lock, &x, 4, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)); ASSERT(3, x); ASSERT(4, lock); // CAS fail x = 3; ASSERT(0, __atomic_compare_exchange_n(&lock, &x, 7, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)); ASSERT(4, x); ASSERT(4, lock); // CAS success #3 x = 4; ASSERT(1, __atomic_compare_exchange_n(&lock, &x, 31337, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)); ASSERT(4, x); ASSERT(31337, lock); // xadd ASSERT(31337, __atomic_fetch_add(&lock, 31337, __ATOMIC_SEQ_CST)); ASSERT(62674, lock); ASSERT(62674, __atomic_fetch_sub(&lock, 31337, __ATOMIC_SEQ_CST)); ASSERT(31337, lock); // }
1,616
55
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/usualconv_test.c
#include "third_party/chibicc/test/test.h" static int ret10(void) { return 10; } int main() { ASSERT((long)-5, -10 + (long)5); ASSERT((long)-15, -10 - (long)5); ASSERT((long)-50, -10 * (long)5); ASSERT((long)-2, -10 / (long)5); ASSERT(1, -2 < (long)-1); ASSERT(1, -2 <= (long)-1); ASSERT(0, -2 > (long)-1); ASSERT(0, -2 >= (long)-1); ASSERT(1, (long)-2 < -1); ASSERT(1, (long)-2 <= -1); ASSERT(0, (long)-2 > -1); ASSERT(0, (long)-2 >= -1); ASSERT(0, 2147483647 + 2147483647 + 2); ASSERT((long)-1, ({ long x; x = -1; x; })); ASSERT(1, ({ char x[3]; x[0] = 0; x[1] = 1; x[2] = 2; char *y = x + 1; y[0]; })); ASSERT(0, ({ char x[3]; x[0] = 0; x[1] = 1; x[2] = 2; char *y = x + 1; y[-1]; })); ASSERT(5, ({ struct t { char a; } x, y; x.a = 5; y = x; y.a; })); ASSERT(10, (1 ? ret10 : (void *)0)()); return 0; }
1,123
59
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/constexpr_test.c
#include "third_party/chibicc/test/test.h" float g40 = 1.5; double g41 = 0.0 ? 55 : (0, 1 + 1 * 5.0 / 2 * (double)2 * (int)2.0); int main(int argc, char *argv[]) { ASSERT(10, ({ enum { ten = 1 + 2 + 3 + 4 }; ten; })); ASSERT(1, ({ int i = 0; switch (3) { case 5 - 2 + 0 * 3: i++; } i; })); ASSERT(8, ({ int x[1 + 1]; sizeof(x); })); ASSERT(6, ({ char x[8 - 2]; sizeof(x); })); ASSERT(6, ({ char x[2 * 3]; sizeof(x); })); ASSERT(3, ({ char x[12 / 4]; sizeof(x); })); ASSERT(2, ({ char x[12 % 10]; sizeof(x); })); ASSERT(0b100, ({ char x[0b110 & 0b101]; sizeof(x); })); ASSERT(0b111, ({ char x[0b110 | 0b101]; sizeof(x); })); ASSERT(0b110, ({ char x[0b111 ^ 0b001]; sizeof(x); })); ASSERT(4, ({ char x[1 << 2]; sizeof(x); })); ASSERT(2, ({ char x[4 >> 1]; sizeof(x); })); ASSERT(2, ({ char x[(1 == 1) + 1]; sizeof(x); })); ASSERT(1, ({ char x[(1 != 1) + 1]; sizeof(x); })); ASSERT(1, ({ char x[(1 < 1) + 1]; sizeof(x); })); ASSERT(2, ({ char x[(1 <= 1) + 1]; sizeof(x); })); ASSERT(2, ({ char x[1 ? 2 : 3]; sizeof(x); })); ASSERT(3, ({ char x[0 ? 2 : 3]; sizeof(x); })); ASSERT(3, ({ char x[(1, 3)]; sizeof(x); })); ASSERT(2, ({ char x[!0 + 1]; sizeof(x); })); ASSERT(1, ({ char x[!1 + 1]; sizeof(x); })); ASSERT(2, ({ char x[~-3]; sizeof(x); })); ASSERT(2, ({ char x[(5 || 6) + 1]; sizeof(x); })); ASSERT(1, ({ char x[(0 || 0) + 1]; sizeof(x); })); ASSERT(2, ({ char x[(1 && 1) + 1]; sizeof(x); })); ASSERT(1, ({ char x[(1 && 0) + 1]; sizeof(x); })); ASSERT(3, ({ char x[(int)3]; sizeof(x); })); ASSERT(15, ({ char x[(char)0xffffff0f]; sizeof(x); })); ASSERT(0x10f, ({ char x[(short)0xffff010f]; sizeof(x); })); ASSERT(4, ({ char x[(int)0xfffffffffff + 5]; sizeof(x); })); ASSERT(8, ({ char x[(int *)0 + 2]; sizeof(x); })); ASSERT(12, ({ char x[(int *)16 - 1]; sizeof(x); })); ASSERT(3, ({ char x[(int *)16 - (int *)4]; sizeof(x); })); ASSERT(4, ({ char x[(-1 >> 31) + 5]; sizeof(x); })); ASSERT(255, ({ char x[(unsigned char)0xffffffff]; sizeof(x); })); ASSERT(0x800f, ({ char x[(unsigned short)0xffff800f]; sizeof(x); })); ASSERT(1, ({ char x[(unsigned int)0xfffffffffff >> 31]; sizeof(x); })); ASSERT(1, ({ char x[(long)-1 / ((long)1 << 62) + 1]; sizeof(x); })); ASSERT(4, ({ char x[(unsigned long)-1 / ((long)1 << 62) + 1]; sizeof(x); })); ASSERT(1, ({ char x[(unsigned)1 < -1]; sizeof(x); })); ASSERT(1, ({ char x[(unsigned)1 <= -1]; sizeof(x); })); ASSERT(1, g40 == 1.5); ASSERT(1, g41 == 11); }
3,812
180
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/assert_test.c
#include "third_party/chibicc/test/test.h" _Static_assert(1); _Static_assert(1, "hey"); main() { _Static_assert(sizeof(int) == 4, "wut"); }
144
9
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/vector_test.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/chibicc/test/test.h" typedef char byte16 __attribute__((__vector_size__(16))); typedef float float4 __attribute__((__vector_size__(16))); typedef float float4a1 __attribute__((__vector_size__(16), __aligned__(1))); typedef float float4a16 __attribute__((__vector_size__(16), __aligned__(16))); typedef double double2 __attribute__((__vector_size__(16))); typedef double double2a1 __attribute__((__vector_size__(16), __aligned__(1))); typedef double double2a16 __attribute__((__vector_size__(16), __aligned__(16))); int main(void) { ASSERT(16, sizeof(float4)); ASSERT(16, sizeof(float4a1)); ASSERT(16, sizeof(float4a16)); ASSERT(16, sizeof(double2)); ASSERT(16, sizeof(double2a1)); ASSERT(16, sizeof(double2a16)); ASSERT(16, _Alignof(float4)); ASSERT(1, _Alignof(float4a1)); ASSERT(16, _Alignof(float4a16)); ASSERT(16, _Alignof(double2)); ASSERT(1, _Alignof(double2a1)); ASSERT(16, _Alignof(double2a16)); { float4 v1; float4 v2; float x[4] = {1, 2, 3, 4}; float y[4] = {1, 1, 1, 1}; memcpy(&v1, x, 16); memcpy(&v2, y, 16); v1 = v1 + v2; memcpy(x, &v1, 16); ASSERT(2, x[0]); // TODO(jart): fix me /* ASSERT(3, x[1]); */ /* ASSERT(4, x[2]); */ /* ASSERT(5, x[3]); */ } { byte16 v; float x1[4] = {1, 2, 3, 4}; float x2[4]; memcpy(&v, x1, 16); __builtin_ia32_movntdq(x1, &v); memcpy(x2, &v, 16); ASSERT(1, x2[0]); // TODO(jart): fix me /* ASSERT(2, x[1]); */ } return 0; }
3,353
73
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/vla_test.c
#include "libc/macros.internal.h" #include "third_party/chibicc/test/test.h" int index1d(int xn, int p[xn], int x) { return p[x]; } int index2d(int yn, int xn, int (*p)[yn][xn], int y, int x) { return (*p)[y][x]; } int main() { ASSERT(30, ({ int a[5] = {00, 10, 20, 30, 40, 50}; index1d(5, a, 3); })); /* ASSERT(210, ({ */ /* int a[3][3] = { */ /* {000, 010, 020}, */ /* {100, 110, 120}, */ /* {200, 210, 220}, */ /* }; */ /* index2d(3, 3, a, 2, 1); */ /* })); */ ASSERT(20, ({ int n = 5; int x[n]; sizeof(x); })); ASSERT(5, ({ int n = 5; int x[n]; ARRAYLEN(x); })); ASSERT((5 + 1) * (8 * 2) * 4, ({ int m = 5, n = 8; int x[m + 1][n * 2]; sizeof(x); })); ASSERT(8, ({ char n = 10; int(*x)[n][n + 2]; sizeof(x); })); ASSERT(480, ({ char n = 10; int(*x)[n][n + 2]; sizeof(*x); })); ASSERT(48, ({ char n = 10; int(*x)[n][n + 2]; sizeof(**x); })); ASSERT(4, ({ char n = 10; int(*x)[n][n + 2]; sizeof(***x); })); ASSERT(60, ({ char n = 3; int x[5][n]; sizeof(x); })); ASSERT(12, ({ char n = 3; int x[5][n]; sizeof(*x); })); ASSERT(60, ({ char n = 3; int x[n][5]; sizeof(x); })); ASSERT(20, ({ char n = 3; int x[n][5]; sizeof(*x); })); ASSERT(0, ({ int n = 10; int x[n + 1][n + 6]; int *p = x; for (int i = 0; i < sizeof(x) / 4; i++) p[i] = i; x[0][0]; })); ASSERT(5, ({ int n = 10; int x[n + 1][n + 6]; int *p = x; for (int i = 0; i < sizeof(x) / 4; i++) p[i] = i; x[0][5]; })); ASSERT(5 * 16 + 2, ({ int n = 10; int x[n + 1][n + 6]; int *p = x; for (int i = 0; i < sizeof(x) / 4; i++) p[i] = i; x[5][2]; })); ASSERT(10, ({ int n = 5; sizeof(char[2][n]); })); return 0; }
2,422
116
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/unicode_test.c
#include "third_party/chibicc/test/test.h" /* TODO(jart): shl overflow in read_escaped_char */ #define STR(x) #x typedef unsigned short char16_t; typedef unsigned int char32_t; typedef int wchar_t; int π = 3; int main() { ASSERT(4, sizeof(L'\0')); ASSERT(97, L'a'); ASSERT(0, strcmp("αβγ", "\u03B1\u03B2\u03B3")); ASSERT(0, strcmp("日本語", "\u65E5\u672C\u8A9E")); ASSERT(0, strcmp("日本語", "\U000065E5\U0000672C\U00008A9E")); ASSERT(0, strcmp("🌮", "\U0001F32E")); ASSERT(-1, L'\xffffffff' >> 31); ASSERT(946, L'β'); ASSERT(12354, L'あ'); ASSERT(127843, L'🍣'); ASSERT(2, sizeof(u'\0')); ASSERT(1, u'\xffff' >> 15); ASSERT(97, u'a'); ASSERT(946, u'β'); ASSERT(12354, u'あ'); ASSERT(62307, u'🍣'); ASSERT(0, strcmp(STR(u'a'), "u'a'")); ASSERT(4, sizeof(U'\0')); ASSERT(1, U'\xffffffff' >> 31); ASSERT(97, U'a'); ASSERT(946, U'β'); ASSERT(12354, U'あ'); ASSERT(127843, U'🍣'); ASSERT(0, strcmp(STR(U'a'), "U'a'")); ASSERT(4, sizeof(u8"abc")); ASSERT(0, strcmp(u8"abc", "abc")); ASSERT(0, strcmp(STR(u8"a"), "u8\"a\"")); ASSERT(2, sizeof(u"")); ASSERT(10, sizeof(u"\xffzzz")); ASSERT(0, memcmp(u"", "\0\0", 2)); ASSERT(0, memcmp(u"abc", "a\0b\0c\0\0\0", 8)); ASSERT(0, memcmp(u"日本語", "\345e,g\236\212\0\0", 8)); ASSERT(0, memcmp(u"🍣", "<\330c\337\0\0", 6)); ASSERT(u'β', u"βb"[0]); ASSERT(u'b', u"βb"[1]); ASSERT(0, u"βb"[2]); ASSERT(0, strcmp(STR(u"a"), "u\"a\"")); ASSERT(4, sizeof(U"")); ASSERT(20, sizeof(U"\xffzzz")); ASSERT(0, memcmp(U"", "\0\0\0\0", 4)); ASSERT(0, memcmp(U"abc", "a\0\0\0b\0\0\0c\0\0\0\0\0\0\0", 16)); ASSERT(0, memcmp(U"日本語", "\345e\0\0,g\0\0\236\212\0\0\0\0\0\0", 16)); ASSERT(0, memcmp(U"🍣", "c\363\001\0\0\0\0\0", 8)); ASSERT(u'β', U"βb"[0]); ASSERT(u'b', U"βb"[1]); ASSERT(0, U"βb"[2]); ASSERT(1, U"\xffffffff"[0] >> 31); ASSERT(0, strcmp(STR(U"a"), "U\"a\"")); ASSERT(4, sizeof(L"")); ASSERT(20, sizeof(L"\xffzzz")); ASSERT(0, memcmp(L"", "\0\0\0\0", 4)); ASSERT(0, memcmp(L"abc", "a\0\0\0b\0\0\0c\0\0\0\0\0\0\0", 16)); ASSERT(0, memcmp(L"日本語", "\345e\0\0,g\0\0\236\212\0\0\0\0\0\0", 16)); ASSERT(0, memcmp(L"🍣", "c\363\001\0\0\0\0\0", 8)); ASSERT(u'β', L"βb"[0]); ASSERT(u'b', L"βb"[1]); ASSERT(0, L"βb"[2]); ASSERT(-1, L"\xffffffff"[0] >> 31); ASSERT(0, strcmp(STR(L"a"), "L\"a\"")); ASSERT(u'α', ({ char16_t x[] = u"αβ"; x[0]; })); ASSERT(u'β', ({ char16_t x[] = u"αβ"; x[1]; })); ASSERT(6, ({ char16_t x[] = u"αβ"; sizeof(x); })); ASSERT(U'🤔', ({ char32_t x[] = U"🤔x"; x[0]; })); ASSERT(U'x', ({ char32_t x[] = U"🤔x"; x[1]; })); ASSERT(12, ({ char32_t x[] = U"🤔x"; sizeof(x); })); ASSERT(L'🤔', ({ wchar_t x[] = L"🤔x"; x[0]; })); ASSERT(L'x', ({ wchar_t x[] = L"🤔x"; x[1]; })); ASSERT(12, ({ wchar_t x[] = L"🤔x"; sizeof(x); })); ASSERT(3, π); ASSERT(3, ({ int あβ0¾ = 3; あβ0¾; })); ASSERT(5, ({ int $$$ = 5; $$$; })); return 0; }
3,374
139
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/alignof_test.c
#include "third_party/chibicc/test/test.h" # int _Alignas(512) g1; int _Alignas(512) g2; char g3; int g4; long g5; char g6; int main() { ASSERT(1, _Alignof(char)); ASSERT(2, _Alignof(short)); ASSERT(4, _Alignof(int)); ASSERT(8, _Alignof(long)); ASSERT(8, _Alignof(long long)); ASSERT(1, _Alignof(char[3])); ASSERT(4, _Alignof(int[3])); ASSERT(1, _Alignof(struct { char a; char b; }[2])); ASSERT(8, _Alignof(struct { char a; long b; }[2])); ASSERT(1, ({ _Alignas(char) char x, y; &y - &x; })); ASSERT(8, ({ _Alignas(long) char x, y; &y - &x; })); ASSERT(32, ({ _Alignas(32) char x, y; &y - &x; })); ASSERT(32, ({ _Alignas(32) int *x, *y; ((char *)&y) - ((char *)&x); })); ASSERT(16, ({ struct { _Alignas(16) char x, y; } a; &a.y - &a.x; })); ASSERT(8, ({ struct T { _Alignas(8) char a; }; _Alignof(struct T); })); ASSERT(0, (long)(char *)&g1 % 512); ASSERT(0, (long)(char *)&g2 % 512); ASSERT(0, (long)(char *)&g4 % 4); ASSERT(0, (long)(char *)&g5 % 8); ASSERT(1, ({ char x; _Alignof(x); })); ASSERT(4, ({ int x; _Alignof(x); })); ASSERT(1, ({ char x; _Alignof x; })); ASSERT(4, ({ int x; _Alignof x; })); ASSERT(1, _Alignof(char) << 31 >> 31); ASSERT(1, _Alignof(char) << 63 >> 63); ASSERT(1, ({ char x; _Alignof(x) << 63 >> 63; })); ASSERT(0, ({ char x[16]; (unsigned long)&x % 16; })); ASSERT(0, ({ char x[17]; (unsigned long)&x % 16; })); ASSERT(0, ({ char x[100]; (unsigned long)&x % 16; })); ASSERT(0, ({ char x[101]; (unsigned long)&x % 16; })); return 0; }
2,109
105
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/pointer_test.c
#include "third_party/chibicc/test/test.h" int main() { ASSERT(3, ({ int x = 3; *&x; })); ASSERT(3, ({ int x = 3; int *y = &x; int **z = &y; **z; })); ASSERT(5, ({ int x = 3; int y = 5; *(&x + 1); })); ASSERT(3, ({ int x = 3; int y = 5; *(&y - 1); })); ASSERT(5, ({ int x = 3; int y = 5; *(&x - (-1)); })); ASSERT(5, ({ int x = 3; int *y = &x; *y = 5; x; })); ASSERT(7, ({ int x = 3; int y = 5; *(&x + 1) = 7; y; })); ASSERT(7, ({ int x = 3; int y = 5; *(&y - 2 + 1) = 7; x; })); ASSERT(5, ({ int x = 3; (&x + 2) - &x + 3; })); ASSERT(8, ({ int x, y; x = 3; y = 5; x + y; })); ASSERT(8, ({ int x = 3, y = 5; x + y; })); ASSERT(3, ({ int x[2]; int *y = &x; *y = 3; *x; })); ASSERT(3, ({ int x[3]; *x = 3; *(x + 1) = 4; *(x + 2) = 5; *x; })); ASSERT(4, ({ int x[3]; *x = 3; *(x + 1) = 4; *(x + 2) = 5; *(x + 1); })); ASSERT(5, ({ int x[3]; *x = 3; *(x + 1) = 4; *(x + 2) = 5; *(x + 2); })); ASSERT(0, ({ int x[2][3]; int *y = x; *y = 0; **x; })); ASSERT(1, ({ int x[2][3]; int *y = x; *(y + 1) = 1; *(*x + 1); })); ASSERT(2, ({ int x[2][3]; int *y = x; *(y + 2) = 2; *(*x + 2); })); ASSERT(3, ({ int x[2][3]; int *y = x; *(y + 3) = 3; **(x + 1); })); ASSERT(4, ({ int x[2][3]; int *y = x; *(y + 4) = 4; *(*(x + 1) + 1); })); ASSERT(5, ({ int x[2][3]; int *y = x; *(y + 5) = 5; *(*(x + 1) + 2); })); ASSERT(3, ({ int x[3]; *x = 3; x[1] = 4; x[2] = 5; *x; })); ASSERT(4, ({ int x[3]; *x = 3; x[1] = 4; x[2] = 5; *(x + 1); })); ASSERT(5, ({ int x[3]; *x = 3; x[1] = 4; x[2] = 5; *(x + 2); })); ASSERT(5, ({ int x[3]; *x = 3; x[1] = 4; x[2] = 5; *(x + 2); })); ASSERT(5, ({ int x[3]; *x = 3; x[1] = 4; 2 [x] = 5; *(x + 2); })); ASSERT(0, ({ int x[2][3]; int *y = x; y[0] = 0; x[0][0]; })); ASSERT(1, ({ int x[2][3]; int *y = x; y[1] = 1; x[0][1]; })); ASSERT(2, ({ int x[2][3]; int *y = x; y[2] = 2; x[0][2]; })); ASSERT(3, ({ int x[2][3]; int *y = x; y[3] = 3; x[1][0]; })); ASSERT(4, ({ int x[2][3]; int *y = x; y[4] = 4; x[1][1]; })); ASSERT(5, ({ int x[2][3]; int *y = x; y[5] = 5; x[1][2]; })); return 0; }
3,764
203
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/function_test.c
#include "third_party/chibicc/test/test.h" int ret3(void) { return 3; return 5; } int add2(int x, int y) { return x + y; } int sub2(int x, int y) { return x - y; } int add6(int a, int b, int c, int d, int e, int f) { return a + b + c + d + e + f; } int addx(int *x, int y) { return *x + y; } int sub_char(char a, char b, char c) { return a - b - c; } int fib(int x) { if (x <= 1) return 1; return fib(x - 1) + fib(x - 2); } int sub_long(long a, long b, long c) { return a - b - c; } int sub_short(short a, short b, short c) { return a - b - c; } int g1; int *g1_ptr(void) { return &g1; } char int_to_char(int x) { return x; } int div_long(long a, long b) { return a / b; } _Bool bool_fn_add(_Bool x) { return x + 1; } _Bool bool_fn_sub(_Bool x) { return x - 1; } static int static_fn(void) { return 3; } int param_decay(int x[]) { return x[0]; } int counter() { static int i; static int j = 1 + 1; return i++ + j++; } void ret_none() { return; } _Bool true_fn(); _Bool false_fn(); char char_fn(); short short_fn(); unsigned char uchar_fn(); unsigned short ushort_fn(); char schar_fn(); short sshort_fn(); int add_all(int, ...); int add_all(int, ...); double add_double(double x, double y); float add_float(float x, float y); float add_float3(float x, float y, float z) { return x + y + z; } double add_double3(double x, double y, double z) { return x + y + z; } int (*fnptr(int (*fn)(int n, ...)))(int, ...) { return fn; } int param_decay2(int x()) { return x(); } char *func_fn(void) { return __func__; } char *function_fn(void) { return __FUNCTION__; } int add10_int(int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8, int x9, int x10); float add10_float(float x1, float x2, float x3, float x4, float x5, float x6, float x7, float x8, float x9, float x10); double add10_double(double x1, double x2, double x3, double x4, double x5, double x6, double x7, double x8, double x9, double x10); int many_args1(int a, int b, int c, int d, int e, int f, int g, int h) { return g / h; } double many_args2(double a, double b, double c, double d, double e, double f, double g, double h, double i, double j) { return i / j; } int many_args3(int a, double b, int c, int d, double e, int f, double g, int h, double i, double j, double k, double l, double m, int n, int o, double p) { return o / p; } typedef struct { int a, b; short c; char d; } Ty4; typedef struct { int a; float b; double c; } Ty5; typedef struct { unsigned char a[3]; } Ty6; typedef struct { long a, b, c; } Ty7; int struct_test5(Ty5 x, int n); int struct_test4(Ty4 x, int n); int struct_test6(Ty6 x, int n); int struct_test7(Ty7 x, int n); int struct_test14(Ty4 x, int n) { switch (n) { case 0: return x.a; case 1: return x.b; case 2: return x.c; default: return x.d; } } int struct_test15(Ty5 x, int n) { switch (n) { case 0: return x.a; case 1: return x.b; default: return x.c; } } typedef struct { unsigned char a[10]; } Ty20; typedef struct { unsigned char a[20]; } Ty21; Ty4 struct_test24(void); Ty5 struct_test25(void); Ty6 struct_test26(void); Ty20 struct_test27(void); Ty21 struct_test28(void); Ty4 struct_test34(void) { return (Ty4){10, 20, 30, 40}; } Ty5 struct_test35(void) { return (Ty5){10, 20, 30}; } Ty6 struct_test36(void) { return (Ty6){10, 20, 30}; } Ty20 struct_test37(void) { return (Ty20){10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; } Ty21 struct_test38(void) { return (Ty21){1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; } inline int inline_fn(void) { return 3; } double to_double(long double x) { return x; } long double to_ldouble(int x) { return x; } int main() { FILE *f = fopen("/dev/null", "w"); ASSERT(3, ret3()); ASSERT(8, add2(3, 5)); ASSERT(2, sub2(5, 3)); ASSERT(21, add6(1, 2, 3, 4, 5, 6)); ASSERT(66, add6(1, 2, add6(3, 4, 5, 6, 7, 8), 9, 10, 11)); ASSERT(136, add6(1, 2, add6(3, add6(4, 5, 6, 7, 8, 9), 10, 11, 12, 13), 14, 15, 16)); ASSERT(7, add2(3, 4)); ASSERT(1, sub2(4, 3)); ASSERT(55, fib(9)); ASSERT(1, ({ sub_char(7, 3, 3); })); ASSERT(1, sub_long(7, 3, 3)); ASSERT(1, sub_short(7, 3, 3)); g1 = 3; ASSERT(3, *g1_ptr()); ASSERT(5, int_to_char(261)); ASSERT(5, int_to_char(261)); ASSERT(-5, div_long(-10, 2)); ASSERT(1, bool_fn_add(3)); ASSERT(0, bool_fn_sub(3)); ASSERT(1, bool_fn_add(-3)); ASSERT(0, bool_fn_sub(-3)); ASSERT(1, bool_fn_add(0)); ASSERT(1, bool_fn_sub(0)); ASSERT(3, static_fn()); ASSERT(3, ({ int x[2]; x[0] = 3; param_decay(x); })); ASSERT(2, counter()); ASSERT(4, counter()); ASSERT(6, counter()); ret_none(); ASSERT(1, true_fn()); ASSERT(0, false_fn()); ASSERT(3, char_fn()); ASSERT(5, short_fn()); ASSERT(6, add_all(3, 1, 2, 3)); ASSERT(5, add_all(4, 1, 2, 3, -1)); { char buf[100]; sprintf(buf, "%d %d %s", 1, 2, "foo"); fprintf(f, "%s\n", buf); } ASSERT(0, ({ char buf[100]; sprintf(buf, "%d %d %s", 1, 2, "foo"); strcmp("1 2 foo", buf); })); ASSERT(0, ({ char buf[100]; sprintf(buf, "%d %d %s", 1, 2, "foo"); strcmp("1 2 foo", buf); })); ASSERT(251, uchar_fn()); ASSERT(65528, ushort_fn()); ASSERT(-5, schar_fn()); ASSERT(-8, sshort_fn()); ASSERT(6, add_float(2.3, 3.8)); ASSERT(6, add_double(2.3, 3.8)); ASSERT(7, add_float3(2.5, 2.5, 2.5)); ASSERT(7, add_double3(2.5, 2.5, 2.5)); ASSERT(0, ({ char buf[100]; sprintf(buf, "%.1f", (float)3.5); strcmp(buf, "3.5"); })); ASSERT(0, ({ char buf[100]; sprintf(buf, "%.1f", (float)3.5); strcmp(buf, "3.5"); })); ASSERT(5, (add2)(2, 3)); ASSERT(5, (&add2)(2, 3)); ASSERT(7, ({ int (*fn)(int, int) = add2; fn(2, 5); })); ASSERT(6, fnptr(add_all)(3, 1, 2, 3)); ASSERT(3, param_decay2(ret3)); ASSERT(5, sizeof(__func__)); ASSERT(0, strcmp("main", __func__)); ASSERT(0, strcmp("func_fn", func_fn())); ASSERT(0, strcmp("main", __FUNCTION__)); ASSERT(0, strcmp("function_fn", function_fn())); ASSERT(55, add10_int(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); ASSERT(55, add10_float(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); ASSERT(55, add10_double(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); ASSERT(0, ({ char buf[200]; sprintf(buf, "%d %.1f %.1f %.1f %d %d %.1f %d %d %d %d %.1f %d %d %.1f " "%.1f %.1f %.1f %d", 1, 1.0, 1.0, 1.0, 1, 1, 1.0, 1, 1, 1, 1, 1.0, 1, 1, 1.0, 1.0, 1.0, 1.0, 1); strcmp("1 1.0 1.0 1.0 1 1 1.0 1 1 1 1 1.0 1 1 1.0 1.0 1.0 1.0 1", buf); })); ASSERT(4, many_args1(1, 2, 3, 4, 5, 6, 40, 10)); ASSERT(4, many_args2(1, 2, 3, 4, 5, 6, 7, 8, 40, 10)); ASSERT(8, many_args3(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 80, 10)); ASSERT(10, ({ Ty4 x = {10, 20, 30, 40}; struct_test4(x, 0); })); ASSERT(20, ({ Ty4 x = {10, 20, 30, 40}; struct_test4(x, 1); })); ASSERT(30, ({ Ty4 x = {10, 20, 30, 40}; struct_test4(x, 2); })); ASSERT(40, ({ Ty4 x = {10, 20, 30, 40}; struct_test4(x, 3); })); ASSERT(10, ({ Ty5 x = {10, 20, 30}; struct_test5(x, 0); })); ASSERT(20, ({ Ty5 x = {10, 20, 30}; struct_test5(x, 1); })); ASSERT(30, ({ Ty5 x = {10, 20, 30}; struct_test5(x, 2); })); ASSERT(10, ({ Ty6 x = {10, 20, 30}; struct_test6(x, 0); })); ASSERT(20, ({ Ty6 x = {10, 20, 30}; struct_test6(x, 1); })); ASSERT(30, ({ Ty6 x = {10, 20, 30}; struct_test6(x, 2); })); ASSERT(10, ({ Ty7 x = {10, 20, 30}; struct_test7(x, 0); })); ASSERT(20, ({ Ty7 x = {10, 20, 30}; struct_test7(x, 1); })); ASSERT(30, ({ Ty7 x = {10, 20, 30}; struct_test7(x, 2); })); ASSERT(10, ({ Ty4 x = {10, 20, 30, 40}; struct_test14(x, 0); })); ASSERT(20, ({ Ty4 x = {10, 20, 30, 40}; struct_test14(x, 1); })); ASSERT(30, ({ Ty4 x = {10, 20, 30, 40}; struct_test14(x, 2); })); ASSERT(40, ({ Ty4 x = {10, 20, 30, 40}; struct_test14(x, 3); })); ASSERT(10, ({ Ty5 x = {10, 20, 30}; struct_test15(x, 0); })); ASSERT(20, ({ Ty5 x = {10, 20, 30}; struct_test15(x, 1); })); ASSERT(30, ({ Ty5 x = {10, 20, 30}; struct_test15(x, 2); })); ASSERT(10, struct_test24().a); ASSERT(20, struct_test24().b); ASSERT(30, struct_test24().c); ASSERT(40, struct_test24().d); ASSERT(10, struct_test25().a); ASSERT(20, struct_test25().b); ASSERT(30, struct_test25().c); ASSERT(10, struct_test26().a[0]); ASSERT(20, struct_test26().a[1]); ASSERT(30, struct_test26().a[2]); ASSERT(10, struct_test27().a[0]); ASSERT(60, struct_test27().a[5]); ASSERT(100, struct_test27().a[9]); ASSERT(1, struct_test28().a[0]); ASSERT(5, struct_test28().a[4]); ASSERT(10, struct_test28().a[9]); ASSERT(15, struct_test28().a[14]); ASSERT(20, struct_test28().a[19]); ASSERT(10, struct_test34().a); ASSERT(20, struct_test34().b); ASSERT(30, struct_test34().c); ASSERT(40, struct_test34().d); ASSERT(10, struct_test35().a); ASSERT(20, struct_test35().b); ASSERT(30, struct_test35().c); ASSERT(10, struct_test36().a[0]); ASSERT(20, struct_test36().a[1]); ASSERT(30, struct_test36().a[2]); ASSERT(10, struct_test37().a[0]); ASSERT(60, struct_test37().a[5]); ASSERT(100, struct_test37().a[9]); ASSERT(1, struct_test38().a[0]); ASSERT(5, struct_test38().a[4]); ASSERT(10, struct_test38().a[9]); ASSERT(15, struct_test38().a[14]); ASSERT(20, struct_test38().a[19]); ASSERT(5, (***add2)(2, 3)); ASSERT(3, inline_fn()); ASSERT(0, ({ char buf[100]; sprintf(buf, "%Lf", (long double)12.3); strncmp(buf, "12.3", 4); })); ASSERT(1, to_double(3.5) == 3.5); ASSERT(0, to_double(3.5) == 3); ASSERT(1, (long double)5.0 == (long double)5.0); ASSERT(0, (long double)5.0 == (long double)5.2); ASSERT(1, to_ldouble(5.0) == 5.0); ASSERT(0, to_ldouble(5.0) == 5.2); }
10,876
517
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/alloca_test.c
#include "libc/mem/alloca.h" #include "third_party/chibicc/test/test.h" void *fn(int x, void *p, int y) { return p; } int main() { int i = 0; char *p1 = alloca(16); char *p2 = alloca(16); char *p3 = 1 + (char *)alloca(3) + 1; p3 -= 2; char *p4 = fn(1, __builtin_alloca(16), 3); ASSERT(16, p1 - p2); ASSERT(16, p2 - p3); ASSERT(16, p3 - p4); memcpy(p1, "0123456789abcdef", 16); memcpy(p2, "ghijklmnopqrstuv", 16); memcpy(p3, "wxy", 3); ASSERT(0, memcmp(p1, "0123456789abcdef", 16)); ASSERT(0, memcmp(p2, "ghijklmnopqrstuv", 16)); ASSERT(0, memcmp(p3, "wxy", 3)); return 0; }
615
31
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/decl_test.c
#include "third_party/chibicc/test/test.h" int main() { ASSERT(1, ({ char x; sizeof(x); })); ASSERT(2, ({ short int x; sizeof(x); })); ASSERT(2, ({ int short x; sizeof(x); })); ASSERT(4, ({ int x; sizeof(x); })); ASSERT(8, ({ long int x; sizeof(x); })); ASSERT(8, ({ int long x; sizeof(x); })); ASSERT(8, ({ long long x; sizeof(x); })); ASSERT(0, ({ _Bool x = 0; x; })); ASSERT(1, ({ _Bool x = 1; x; })); ASSERT(1, ({ _Bool x = 2; x; })); ASSERT(1, (_Bool)1); ASSERT(1, (_Bool)2); ASSERT(0, (_Bool)(char)256); return 0; }
864
52
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/test.mk
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐ #───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘ # # SYNOPSIS # # C Compiler Unit Tests # # OVERVIEW # # This makefile compiles and runs each test twice. The first with # GCC-built chibicc, and a second time with chibicc-built chibicc ifeq ($(ARCH), x86_64) PKGS += THIRD_PARTY_CHIBICC_TEST THIRD_PARTY_CHIBICC_TEST_A = o/$(MODE)/third_party/chibicc/test/test.a THIRD_PARTY_CHIBICC_TEST_FILES := $(wildcard third_party/chibicc/test/*) THIRD_PARTY_CHIBICC_TEST_SRCS = $(filter %.c,$(THIRD_PARTY_CHIBICC_TEST_FILES)) THIRD_PARTY_CHIBICC_TEST_SRCS_TEST = $(filter %_test.c,$(THIRD_PARTY_CHIBICC_TEST_SRCS)) THIRD_PARTY_CHIBICC_TEST_HDRS = $(filter %.h,$(THIRD_PARTY_CHIBICC_TEST_FILES)) THIRD_PARTY_CHIBICC_TEST_TESTS = $(THIRD_PARTY_CHIBICC_TEST_COMS:%=%.ok) THIRD_PARTY_CHIBICC_TEST_COMS = \ $(THIRD_PARTY_CHIBICC_TEST_SRCS_TEST:%_test.c=o/$(MODE)/%_test.com) THIRD_PARTY_CHIBICC_TEST_OBJS = \ $(THIRD_PARTY_CHIBICC_TEST_SRCS:%.c=o/$(MODE)/%.o) THIRD_PARTY_CHIBICC_TEST_BINS = \ $(THIRD_PARTY_CHIBICC_TEST_COMS) \ $(THIRD_PARTY_CHIBICC_TEST_COMS:%=%.dbg) THIRD_PARTY_CHIBICC_TEST_CHECKS = \ $(THIRD_PARTY_CHIBICC_TEST_COMS:%=%.runs) \ $(THIRD_PARTY_CHIBICC_TEST_HDRS:%=o/$(MODE)/%.ok) THIRD_PARTY_CHIBICC_TEST_DIRECTDEPS = \ LIBC_CALLS \ LIBC_FMT \ LIBC_INTRIN \ LIBC_MEM \ LIBC_NEXGEN32E \ LIBC_RUNTIME \ LIBC_STDIO \ LIBC_STR \ LIBC_STUBS \ LIBC_TINYMATH \ LIBC_X \ THIRD_PARTY_CHIBICC \ THIRD_PARTY_COMPILER_RT THIRD_PARTY_CHIBICC_TEST_DEPS := \ $(call uniq,$(foreach x,$(THIRD_PARTY_CHIBICC_TEST_DIRECTDEPS),$($(x)))) $(THIRD_PARTY_CHIBICC_TEST_A): \ $(THIRD_PARTY_CHIBICC_TEST_A).pkg \ o/$(MODE)/third_party/chibicc/test/common.o $(THIRD_PARTY_CHIBICC_TEST_A).pkg: \ o/$(MODE)/third_party/chibicc/test/common.o \ $(foreach x,$(THIRD_PARTY_CHIBICC_TEST_DIRECTDEPS),$($(x)_A).pkg) o/$(MODE)/third_party/chibicc/test/%.com.dbg: \ $(THIRD_PARTY_CHIBICC_TEST_DEPS) \ $(THIRD_PARTY_CHIBICC_TEST_A) \ o/$(MODE)/third_party/chibicc/test/%.o \ $(THIRD_PARTY_CHIBICC_TEST_A).pkg \ $(CRT) \ $(APE_NO_MODIFY_SELF) @$(APELINK) o/$(MODE)/third_party/chibicc/test/%.o: \ third_party/chibicc/test/%.c \ $(CHIBICC) @$(COMPILE) -wAOBJECTIFY.c $(CHIBICC) $(CHIBICC_FLAGS) $(OUTPUT_OPTION) -c $< o/$(MODE)/third_party/chibicc/test/int128_test.o: private QUOTA = -M1024m endif .PHONY: o/$(MODE)/third_party/chibicc/test o/$(MODE)/third_party/chibicc/test: \ $(THIRD_PARTY_CHIBICC_TEST_BINS) \ $(THIRD_PARTY_CHIBICC_TEST_CHECKS)
2,815
86
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/int128_test.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/chibicc/test/test.h" #define BANE 0x8000000000000000 #define BANE1 0x8000000000000001 #define BANE2 0x8000000000000002 #define IMAX 0x7fffffffffffffff #define IMAX2 0xfffffffffffffffd #define I128(HI, LO) ((LO) | (unsigned __int128)(HI) << 64) __int128 add128(__int128 x, __int128 y) { return x + y; } __int128 sub128x5(__int128 a, __int128 b, __int128 c, __int128 d, __int128 e) { return a - b - c - d - e; } __int128 sub128x6(int f, __int128 a, __int128 b, __int128 c, __int128 d, __int128 e) { return f - a - b - c - d - e; } void lotsOfArgs(const char *file, int line, const char *func, int128_t beg, int128_t end, int128_t got, const char *gotcode, bool isfatal) { } void testLang128(void) { lotsOfArgs(__FILE__, __LINE__, __FUNCTION__, 0, 0, 0, "", false); ASSERT(16, sizeof(__int128)); ASSERT(16, sizeof(unsigned __int128)); ASSERT(16, _Alignof(__int128)); ASSERT(16, ({ struct T { __int128 x; }; _Alignof(struct T); })); ASSERT(2, ({ struct __attribute__((__aligned__(2))) T { __int128 x; }; _Alignof(struct T); })); ASSERT128(7, ({ __int128 i = 2; i += 5; i; })); ASSERT128(I128(0xffffffffffffffff, 0xffffffffffffffff), ({ __int128 i = 0; add128(i, -1); })); ASSERT128(I128(0x3a8eaaa2e9af03f5, 0xd7ed730a55920176), sub128x5(I128(0x0db9cd085ab6ba38, 0xdaf9c05f15896b5f), I128(0xb6429ba7b5b38454, 0x4061839d268a0a78), I128(0x19a0da005190a5ac, 0x755fa06484419e38), I128(0xafc6e44400b9eadd, 0x05e5afdb2e66cdb8), I128(0x5380c8796909a165, 0x47657977e6c4f381))); ASSERT128(I128(0x1f1b109234418f84, 0x21f9f24c8535e4f0), sub128x6(0x5ab6ba38, I128(0x0db9cd085ab6ba38, 0xdaf9c05f15896b5f), I128(0xb6429ba7b5b38454, 0x4061839d268a0a78), I128(0x19a0da005190a5ac, 0x755fa06484419e38), I128(0xafc6e44400b9eadd, 0x05e5afdb2e66cdb8), I128(0x5380c8796909a165, 0x47657977e6c4f381))); } void testCompare128(void) { __int128 x = 1, y = 2; ASSERT(0, x == y); ASSERT(1, x != y); ASSERT(1, x < y); ASSERT(1, x <= y); ASSERT(0, x > y); ASSERT(0, x >= y); ASSERT(1, x >= x); x = I128(-1ul, 2); y = I128(0xfffffffffffffff0, 2); ASSERT(0, x == y); } void testCastDblInt128(void) { int k; double f; __int128 i, w; k = 110; i = 1; i <<= k; f = i; f /= 2; i = f; w = 1; w <<= k - 1; ASSERT128(w, i); } void testCastDblUint128(void) { double f; unsigned __int128 i; i = 0x0000ffffffffffff; ++i; f = i; --f; i = f; --i; ASSERT128(0x0000fffffffffffe, i); } void testCastLdblInt128(void) { int k; __int128 i, w; long double f; k = 110; i = 1; i <<= k; f = i; f /= 2; i = f; w = 1; w <<= k - 1; ASSERT128(w, i); } void testCastLdblUint128(void) { long double f; unsigned __int128 i; i = 0xffffffffffffffff; ++i; f = i; --f; i = f; --i; ASSERT128(0xfffffffffffffffe, i); } void testAdd128(void) { __int128 x, y; x = I128(0, 0); y = I128(0, 0); ASSERT128(I128(0, 0), x + y); x = I128(0, 0); y = I128(0, 2); ASSERT128(I128(0, 2), x + y); x = I128(0, 0); y = I128(0, BANE); ASSERT128(I128(0, BANE), x + y); x = I128(0, 0); y = I128(0, -1ul); ASSERT128(I128(0, -1ul), x + y); x = I128(0, 0); y = I128(2, 0); ASSERT128(I128(2, 0), x + y); x = I128(0, 0); y = I128(2, 2); ASSERT128(I128(2, 2), x + y); x = I128(0, 0); y = I128(2, BANE); ASSERT128(I128(2, BANE), x + y); x = I128(0, 0); y = I128(2, -1ul); ASSERT128(I128(2, -1ul), x + y); x = I128(0, 0); y = I128(BANE, 0); ASSERT128(I128(BANE, 0), x + y); x = I128(0, 0); y = I128(BANE, 2); ASSERT128(I128(BANE, 2), x + y); x = I128(0, 0); y = I128(BANE, BANE); ASSERT128(I128(BANE, BANE), x + y); x = I128(0, 0); y = I128(BANE, -1ul); ASSERT128(I128(BANE, -1ul), x + y); x = I128(0, 0); y = I128(-1ul, 0); ASSERT128(I128(-1ul, 0), x + y); x = I128(0, 0); y = I128(-1ul, 2); ASSERT128(I128(-1ul, 2), x + y); x = I128(0, 0); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, BANE), x + y); x = I128(0, 0); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, -1ul), x + y); x = I128(0, 2); y = I128(0, 0); ASSERT128(I128(0, 2), x + y); x = I128(0, 2); y = I128(0, 2); ASSERT128(I128(0, 4), x + y); x = I128(0, 2); y = I128(0, BANE); ASSERT128(I128(0, BANE2), x + y); x = I128(0, 2); y = I128(0, -1ul); ASSERT128(I128(1, 1), x + y); x = I128(0, 2); y = I128(2, 0); ASSERT128(I128(2, 2), x + y); x = I128(0, 2); y = I128(2, 2); ASSERT128(I128(2, 4), x + y); x = I128(0, 2); y = I128(2, BANE); ASSERT128(I128(2, BANE2), x + y); x = I128(0, 2); y = I128(2, -1ul); ASSERT128(I128(3, 1), x + y); x = I128(0, 2); y = I128(BANE, 0); ASSERT128(I128(BANE, 2), x + y); x = I128(0, 2); y = I128(BANE, 2); ASSERT128(I128(BANE, 4), x + y); x = I128(0, 2); y = I128(BANE, BANE); ASSERT128(I128(BANE, BANE2), x + y); x = I128(0, 2); y = I128(BANE, -1ul); ASSERT128(I128(BANE1, 1), x + y); x = I128(0, 2); y = I128(-1ul, 0); ASSERT128(I128(-1ul, 2), x + y); x = I128(0, 2); y = I128(-1ul, 2); ASSERT128(I128(-1ul, 4), x + y); x = I128(0, 2); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, BANE2), x + y); x = I128(0, 2); y = I128(-1ul, -1ul); ASSERT128(I128(0, 1), x + y); x = I128(0, BANE); y = I128(0, 0); ASSERT128(I128(0, BANE), x + y); x = I128(0, BANE); y = I128(0, 2); ASSERT128(I128(0, BANE2), x + y); x = I128(0, BANE); y = I128(0, BANE); ASSERT128(I128(1, 0), x + y); x = I128(0, BANE); y = I128(0, -1ul); ASSERT128(I128(1, IMAX), x + y); x = I128(0, BANE); y = I128(2, 0); ASSERT128(I128(2, BANE), x + y); x = I128(0, BANE); y = I128(2, 2); ASSERT128(I128(2, BANE2), x + y); x = I128(0, BANE); y = I128(2, BANE); ASSERT128(I128(3, 0), x + y); x = I128(0, BANE); y = I128(2, -1ul); ASSERT128(I128(3, IMAX), x + y); x = I128(0, BANE); y = I128(BANE, 0); ASSERT128(I128(BANE, BANE), x + y); x = I128(0, BANE); y = I128(BANE, 2); ASSERT128(I128(BANE, BANE2), x + y); x = I128(0, BANE); y = I128(BANE, BANE); ASSERT128(I128(BANE1, 0), x + y); x = I128(0, BANE); y = I128(BANE, -1ul); ASSERT128(I128(BANE1, IMAX), x + y); x = I128(0, BANE); y = I128(-1ul, 0); ASSERT128(I128(-1ul, BANE), x + y); x = I128(0, BANE); y = I128(-1ul, 2); ASSERT128(I128(-1ul, BANE2), x + y); x = I128(0, BANE); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x + y); x = I128(0, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(0, IMAX), x + y); x = I128(0, -1ul); y = I128(0, 0); ASSERT128(I128(0, -1ul), x + y); x = I128(0, -1ul); y = I128(0, 2); ASSERT128(I128(1, 1), x + y); x = I128(0, -1ul); y = I128(0, BANE); ASSERT128(I128(1, IMAX), x + y); x = I128(0, -1ul); y = I128(0, -1ul); ASSERT128(I128(1, 0xfffffffffffffffe), x + y); x = I128(0, -1ul); y = I128(2, 0); ASSERT128(I128(2, -1ul), x + y); x = I128(0, -1ul); y = I128(2, 2); ASSERT128(I128(3, 1), x + y); x = I128(0, -1ul); y = I128(2, BANE); ASSERT128(I128(3, IMAX), x + y); x = I128(0, -1ul); y = I128(2, -1ul); ASSERT128(I128(3, 0xfffffffffffffffe), x + y); x = I128(0, -1ul); y = I128(BANE, 0); ASSERT128(I128(BANE, -1ul), x + y); x = I128(0, -1ul); y = I128(BANE, 2); ASSERT128(I128(BANE1, 1), x + y); x = I128(0, -1ul); y = I128(BANE, BANE); ASSERT128(I128(BANE1, IMAX), x + y); x = I128(0, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(BANE1, 0xfffffffffffffffe), x + y); x = I128(0, -1ul); y = I128(-1ul, 0); ASSERT128(I128(-1ul, -1ul), x + y); x = I128(0, -1ul); y = I128(-1ul, 2); ASSERT128(I128(0, 1), x + y); x = I128(0, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(0, IMAX), x + y); x = I128(0, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0xfffffffffffffffe), x + y); x = I128(2, 0); y = I128(0, 0); ASSERT128(I128(2, 0), x + y); x = I128(2, 0); y = I128(0, 2); ASSERT128(I128(2, 2), x + y); x = I128(2, 0); y = I128(0, BANE); ASSERT128(I128(2, BANE), x + y); x = I128(2, 0); y = I128(0, -1ul); ASSERT128(I128(2, -1ul), x + y); x = I128(2, 0); y = I128(2, 0); ASSERT128(I128(4, 0), x + y); x = I128(2, 0); y = I128(2, 2); ASSERT128(I128(4, 2), x + y); x = I128(2, 0); y = I128(2, BANE); ASSERT128(I128(4, BANE), x + y); x = I128(2, 0); y = I128(2, -1ul); ASSERT128(I128(4, -1ul), x + y); x = I128(2, 0); y = I128(BANE, 0); ASSERT128(I128(BANE2, 0), x + y); x = I128(2, 0); y = I128(BANE, 2); ASSERT128(I128(BANE2, 2), x + y); x = I128(2, 0); y = I128(BANE, BANE); ASSERT128(I128(BANE2, BANE), x + y); x = I128(2, 0); y = I128(BANE, -1ul); ASSERT128(I128(BANE2, -1ul), x + y); x = I128(2, 0); y = I128(-1ul, 0); ASSERT128(I128(1, 0), x + y); x = I128(2, 0); y = I128(-1ul, 2); ASSERT128(I128(1, 2), x + y); x = I128(2, 0); y = I128(-1ul, BANE); ASSERT128(I128(1, BANE), x + y); x = I128(2, 0); y = I128(-1ul, -1ul); ASSERT128(I128(1, -1ul), x + y); x = I128(2, 2); y = I128(0, 0); ASSERT128(I128(2, 2), x + y); x = I128(2, 2); y = I128(0, 2); ASSERT128(I128(2, 4), x + y); x = I128(2, 2); y = I128(0, BANE); ASSERT128(I128(2, BANE2), x + y); x = I128(2, 2); y = I128(0, -1ul); ASSERT128(I128(3, 1), x + y); x = I128(2, 2); y = I128(2, 0); ASSERT128(I128(4, 2), x + y); x = I128(2, 2); y = I128(2, 2); ASSERT128(I128(4, 4), x + y); x = I128(2, 2); y = I128(2, BANE); ASSERT128(I128(4, BANE2), x + y); x = I128(2, 2); y = I128(2, -1ul); ASSERT128(I128(5, 1), x + y); x = I128(2, 2); y = I128(BANE, 0); ASSERT128(I128(BANE2, 2), x + y); x = I128(2, 2); y = I128(BANE, 2); ASSERT128(I128(BANE2, 4), x + y); x = I128(2, 2); y = I128(BANE, BANE); ASSERT128(I128(BANE2, BANE2), x + y); x = I128(2, 2); y = I128(BANE, -1ul); ASSERT128(I128(0x8000000000000003, 1), x + y); x = I128(2, 2); y = I128(-1ul, 0); ASSERT128(I128(1, 2), x + y); x = I128(2, 2); y = I128(-1ul, 2); ASSERT128(I128(1, 4), x + y); x = I128(2, 2); y = I128(-1ul, BANE); ASSERT128(I128(1, BANE2), x + y); x = I128(2, 2); y = I128(-1ul, -1ul); ASSERT128(I128(2, 1), x + y); x = I128(2, BANE); y = I128(0, 0); ASSERT128(I128(2, BANE), x + y); x = I128(2, BANE); y = I128(0, 2); ASSERT128(I128(2, BANE2), x + y); x = I128(2, BANE); y = I128(0, BANE); ASSERT128(I128(3, 0), x + y); x = I128(2, BANE); y = I128(0, -1ul); ASSERT128(I128(3, IMAX), x + y); x = I128(2, BANE); y = I128(2, 0); ASSERT128(I128(4, BANE), x + y); x = I128(2, BANE); y = I128(2, 2); ASSERT128(I128(4, BANE2), x + y); x = I128(2, BANE); y = I128(2, BANE); ASSERT128(I128(5, 0), x + y); x = I128(2, BANE); y = I128(2, -1ul); ASSERT128(I128(5, IMAX), x + y); x = I128(2, BANE); y = I128(BANE, 0); ASSERT128(I128(BANE2, BANE), x + y); x = I128(2, BANE); y = I128(BANE, 2); ASSERT128(I128(BANE2, BANE2), x + y); x = I128(2, BANE); y = I128(BANE, BANE); ASSERT128(I128(0x8000000000000003, 0), x + y); x = I128(2, BANE); y = I128(BANE, -1ul); ASSERT128(I128(0x8000000000000003, IMAX), x + y); x = I128(2, BANE); y = I128(-1ul, 0); ASSERT128(I128(1, BANE), x + y); x = I128(2, BANE); y = I128(-1ul, 2); ASSERT128(I128(1, BANE2), x + y); x = I128(2, BANE); y = I128(-1ul, BANE); ASSERT128(I128(2, 0), x + y); x = I128(2, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(2, IMAX), x + y); x = I128(2, -1ul); y = I128(0, 0); ASSERT128(I128(2, -1ul), x + y); x = I128(2, -1ul); y = I128(0, 2); ASSERT128(I128(3, 1), x + y); x = I128(2, -1ul); y = I128(0, BANE); ASSERT128(I128(3, IMAX), x + y); x = I128(2, -1ul); y = I128(0, -1ul); ASSERT128(I128(3, 0xfffffffffffffffe), x + y); x = I128(2, -1ul); y = I128(2, 0); ASSERT128(I128(4, -1ul), x + y); x = I128(2, -1ul); y = I128(2, 2); ASSERT128(I128(5, 1), x + y); x = I128(2, -1ul); y = I128(2, BANE); ASSERT128(I128(5, IMAX), x + y); x = I128(2, -1ul); y = I128(2, -1ul); ASSERT128(I128(5, 0xfffffffffffffffe), x + y); x = I128(2, -1ul); y = I128(BANE, 0); ASSERT128(I128(BANE2, -1ul), x + y); x = I128(2, -1ul); y = I128(BANE, 2); ASSERT128(I128(0x8000000000000003, 1), x + y); x = I128(2, -1ul); y = I128(BANE, BANE); ASSERT128(I128(0x8000000000000003, IMAX), x + y); x = I128(2, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(0x8000000000000003, 0xfffffffffffffffe), x + y); x = I128(2, -1ul); y = I128(-1ul, 0); ASSERT128(I128(1, -1ul), x + y); x = I128(2, -1ul); y = I128(-1ul, 2); ASSERT128(I128(2, 1), x + y); x = I128(2, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(2, IMAX), x + y); x = I128(2, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(2, 0xfffffffffffffffe), x + y); x = I128(BANE, 0); y = I128(0, 0); ASSERT128(I128(BANE, 0), x + y); x = I128(BANE, 0); y = I128(0, 2); ASSERT128(I128(BANE, 2), x + y); x = I128(BANE, 0); y = I128(0, BANE); ASSERT128(I128(BANE, BANE), x + y); x = I128(BANE, 0); y = I128(0, -1ul); ASSERT128(I128(BANE, -1ul), x + y); x = I128(BANE, 0); y = I128(2, 0); ASSERT128(I128(BANE2, 0), x + y); x = I128(BANE, 0); y = I128(2, 2); ASSERT128(I128(BANE2, 2), x + y); x = I128(BANE, 0); y = I128(2, BANE); ASSERT128(I128(BANE2, BANE), x + y); x = I128(BANE, 0); y = I128(2, -1ul); ASSERT128(I128(BANE2, -1ul), x + y); x = I128(BANE, 0); y = I128(BANE, 0); ASSERT128(I128(0, 0), x + y); x = I128(BANE, 0); y = I128(BANE, 2); ASSERT128(I128(0, 2), x + y); x = I128(BANE, 0); y = I128(BANE, BANE); ASSERT128(I128(0, BANE), x + y); x = I128(BANE, 0); y = I128(BANE, -1ul); ASSERT128(I128(0, -1ul), x + y); x = I128(BANE, 0); y = I128(-1ul, 0); ASSERT128(I128(IMAX, 0), x + y); x = I128(BANE, 0); y = I128(-1ul, 2); ASSERT128(I128(IMAX, 2), x + y); x = I128(BANE, 0); y = I128(-1ul, BANE); ASSERT128(I128(IMAX, BANE), x + y); x = I128(BANE, 0); y = I128(-1ul, -1ul); ASSERT128(I128(IMAX, -1ul), x + y); x = I128(BANE, 2); y = I128(0, 0); ASSERT128(I128(BANE, 2), x + y); x = I128(BANE, 2); y = I128(0, 2); ASSERT128(I128(BANE, 4), x + y); x = I128(BANE, 2); y = I128(0, BANE); ASSERT128(I128(BANE, BANE2), x + y); x = I128(BANE, 2); y = I128(0, -1ul); ASSERT128(I128(BANE1, 1), x + y); x = I128(BANE, 2); y = I128(2, 0); ASSERT128(I128(BANE2, 2), x + y); x = I128(BANE, 2); y = I128(2, 2); ASSERT128(I128(BANE2, 4), x + y); x = I128(BANE, 2); y = I128(2, BANE); ASSERT128(I128(BANE2, BANE2), x + y); x = I128(BANE, 2); y = I128(2, -1ul); ASSERT128(I128(0x8000000000000003, 1), x + y); x = I128(BANE, 2); y = I128(BANE, 0); ASSERT128(I128(0, 2), x + y); x = I128(BANE, 2); y = I128(BANE, 2); ASSERT128(I128(0, 4), x + y); x = I128(BANE, 2); y = I128(BANE, BANE); ASSERT128(I128(0, BANE2), x + y); x = I128(BANE, 2); y = I128(BANE, -1ul); ASSERT128(I128(1, 1), x + y); x = I128(BANE, 2); y = I128(-1ul, 0); ASSERT128(I128(IMAX, 2), x + y); x = I128(BANE, 2); y = I128(-1ul, 2); ASSERT128(I128(IMAX, 4), x + y); x = I128(BANE, 2); y = I128(-1ul, BANE); ASSERT128(I128(IMAX, BANE2), x + y); x = I128(BANE, 2); y = I128(-1ul, -1ul); ASSERT128(I128(BANE, 1), x + y); x = I128(BANE, BANE); y = I128(0, 0); ASSERT128(I128(BANE, BANE), x + y); x = I128(BANE, BANE); y = I128(0, 2); ASSERT128(I128(BANE, BANE2), x + y); x = I128(BANE, BANE); y = I128(0, BANE); ASSERT128(I128(BANE1, 0), x + y); x = I128(BANE, BANE); y = I128(0, -1ul); ASSERT128(I128(BANE1, IMAX), x + y); x = I128(BANE, BANE); y = I128(2, 0); ASSERT128(I128(BANE2, BANE), x + y); x = I128(BANE, BANE); y = I128(2, 2); ASSERT128(I128(BANE2, BANE2), x + y); x = I128(BANE, BANE); y = I128(2, BANE); ASSERT128(I128(0x8000000000000003, 0), x + y); x = I128(BANE, BANE); y = I128(2, -1ul); ASSERT128(I128(0x8000000000000003, IMAX), x + y); x = I128(BANE, BANE); y = I128(BANE, 0); ASSERT128(I128(0, BANE), x + y); x = I128(BANE, BANE); y = I128(BANE, 2); ASSERT128(I128(0, BANE2), x + y); x = I128(BANE, BANE); y = I128(BANE, BANE); ASSERT128(I128(1, 0), x + y); x = I128(BANE, BANE); y = I128(BANE, -1ul); ASSERT128(I128(1, IMAX), x + y); x = I128(BANE, BANE); y = I128(-1ul, 0); ASSERT128(I128(IMAX, BANE), x + y); x = I128(BANE, BANE); y = I128(-1ul, 2); ASSERT128(I128(IMAX, BANE2), x + y); x = I128(BANE, BANE); y = I128(-1ul, BANE); ASSERT128(I128(BANE, 0), x + y); x = I128(BANE, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(BANE, IMAX), x + y); x = I128(BANE, -1ul); y = I128(0, 0); ASSERT128(I128(BANE, -1ul), x + y); x = I128(BANE, -1ul); y = I128(0, 2); ASSERT128(I128(BANE1, 1), x + y); x = I128(BANE, -1ul); y = I128(0, BANE); ASSERT128(I128(BANE1, IMAX), x + y); x = I128(BANE, -1ul); y = I128(0, -1ul); ASSERT128(I128(BANE1, 0xfffffffffffffffe), x + y); x = I128(BANE, -1ul); y = I128(2, 0); ASSERT128(I128(BANE2, -1ul), x + y); x = I128(BANE, -1ul); y = I128(2, 2); ASSERT128(I128(0x8000000000000003, 1), x + y); x = I128(BANE, -1ul); y = I128(2, BANE); ASSERT128(I128(0x8000000000000003, IMAX), x + y); x = I128(BANE, -1ul); y = I128(2, -1ul); ASSERT128(I128(0x8000000000000003, 0xfffffffffffffffe), x + y); x = I128(BANE, -1ul); y = I128(BANE, 0); ASSERT128(I128(0, -1ul), x + y); x = I128(BANE, -1ul); y = I128(BANE, 2); ASSERT128(I128(1, 1), x + y); x = I128(BANE, -1ul); y = I128(BANE, BANE); ASSERT128(I128(1, IMAX), x + y); x = I128(BANE, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(1, 0xfffffffffffffffe), x + y); x = I128(BANE, -1ul); y = I128(-1ul, 0); ASSERT128(I128(IMAX, -1ul), x + y); x = I128(BANE, -1ul); y = I128(-1ul, 2); ASSERT128(I128(BANE, 1), x + y); x = I128(BANE, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(BANE, IMAX), x + y); x = I128(BANE, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(BANE, 0xfffffffffffffffe), x + y); x = I128(-1ul, 0); y = I128(0, 0); ASSERT128(I128(-1ul, 0), x + y); x = I128(-1ul, 0); y = I128(0, 2); ASSERT128(I128(-1ul, 2), x + y); x = I128(-1ul, 0); y = I128(0, BANE); ASSERT128(I128(-1ul, BANE), x + y); x = I128(-1ul, 0); y = I128(0, -1ul); ASSERT128(I128(-1ul, -1ul), x + y); x = I128(-1ul, 0); y = I128(2, 0); ASSERT128(I128(1, 0), x + y); x = I128(-1ul, 0); y = I128(2, 2); ASSERT128(I128(1, 2), x + y); x = I128(-1ul, 0); y = I128(2, BANE); ASSERT128(I128(1, BANE), x + y); x = I128(-1ul, 0); y = I128(2, -1ul); ASSERT128(I128(1, -1ul), x + y); x = I128(-1ul, 0); y = I128(BANE, 0); ASSERT128(I128(IMAX, 0), x + y); x = I128(-1ul, 0); y = I128(BANE, 2); ASSERT128(I128(IMAX, 2), x + y); x = I128(-1ul, 0); y = I128(BANE, BANE); ASSERT128(I128(IMAX, BANE), x + y); x = I128(-1ul, 0); y = I128(BANE, -1ul); ASSERT128(I128(IMAX, -1ul), x + y); x = I128(-1ul, 0); y = I128(-1ul, 0); ASSERT128(I128(0xfffffffffffffffe, 0), x + y); x = I128(-1ul, 0); y = I128(-1ul, 2); ASSERT128(I128(0xfffffffffffffffe, 2), x + y); x = I128(-1ul, 0); y = I128(-1ul, BANE); ASSERT128(I128(0xfffffffffffffffe, BANE), x + y); x = I128(-1ul, 0); y = I128(-1ul, -1ul); ASSERT128(I128(0xfffffffffffffffe, -1ul), x + y); x = I128(-1ul, 2); y = I128(0, 0); ASSERT128(I128(-1ul, 2), x + y); x = I128(-1ul, 2); y = I128(0, 2); ASSERT128(I128(-1ul, 4), x + y); x = I128(-1ul, 2); y = I128(0, BANE); ASSERT128(I128(-1ul, BANE2), x + y); x = I128(-1ul, 2); y = I128(0, -1ul); ASSERT128(I128(0, 1), x + y); x = I128(-1ul, 2); y = I128(2, 0); ASSERT128(I128(1, 2), x + y); x = I128(-1ul, 2); y = I128(2, 2); ASSERT128(I128(1, 4), x + y); x = I128(-1ul, 2); y = I128(2, BANE); ASSERT128(I128(1, BANE2), x + y); x = I128(-1ul, 2); y = I128(2, -1ul); ASSERT128(I128(2, 1), x + y); x = I128(-1ul, 2); y = I128(BANE, 0); ASSERT128(I128(IMAX, 2), x + y); x = I128(-1ul, 2); y = I128(BANE, 2); ASSERT128(I128(IMAX, 4), x + y); x = I128(-1ul, 2); y = I128(BANE, BANE); ASSERT128(I128(IMAX, BANE2), x + y); x = I128(-1ul, 2); y = I128(BANE, -1ul); ASSERT128(I128(BANE, 1), x + y); x = I128(-1ul, 2); y = I128(-1ul, 0); ASSERT128(I128(0xfffffffffffffffe, 2), x + y); x = I128(-1ul, 2); y = I128(-1ul, 2); ASSERT128(I128(0xfffffffffffffffe, 4), x + y); x = I128(-1ul, 2); y = I128(-1ul, BANE); ASSERT128(I128(0xfffffffffffffffe, BANE2), x + y); x = I128(-1ul, 2); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, 1), x + y); x = I128(-1ul, BANE); y = I128(0, 0); ASSERT128(I128(-1ul, BANE), x + y); x = I128(-1ul, BANE); y = I128(0, 2); ASSERT128(I128(-1ul, BANE2), x + y); x = I128(-1ul, BANE); y = I128(0, BANE); ASSERT128(I128(0, 0), x + y); x = I128(-1ul, BANE); y = I128(0, -1ul); ASSERT128(I128(0, IMAX), x + y); x = I128(-1ul, BANE); y = I128(2, 0); ASSERT128(I128(1, BANE), x + y); x = I128(-1ul, BANE); y = I128(2, 2); ASSERT128(I128(1, BANE2), x + y); x = I128(-1ul, BANE); y = I128(2, BANE); ASSERT128(I128(2, 0), x + y); x = I128(-1ul, BANE); y = I128(2, -1ul); ASSERT128(I128(2, IMAX), x + y); x = I128(-1ul, BANE); y = I128(BANE, 0); ASSERT128(I128(IMAX, BANE), x + y); x = I128(-1ul, BANE); y = I128(BANE, 2); ASSERT128(I128(IMAX, BANE2), x + y); x = I128(-1ul, BANE); y = I128(BANE, BANE); ASSERT128(I128(BANE, 0), x + y); x = I128(-1ul, BANE); y = I128(BANE, -1ul); ASSERT128(I128(BANE, IMAX), x + y); x = I128(-1ul, BANE); y = I128(-1ul, 0); ASSERT128(I128(0xfffffffffffffffe, BANE), x + y); x = I128(-1ul, BANE); y = I128(-1ul, 2); ASSERT128(I128(0xfffffffffffffffe, BANE2), x + y); x = I128(-1ul, BANE); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, 0), x + y); x = I128(-1ul, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, IMAX), x + y); x = I128(-1ul, -1ul); y = I128(0, 0); ASSERT128(I128(-1ul, -1ul), x + y); x = I128(-1ul, -1ul); y = I128(0, 2); ASSERT128(I128(0, 1), x + y); x = I128(-1ul, -1ul); y = I128(0, BANE); ASSERT128(I128(0, IMAX), x + y); x = I128(-1ul, -1ul); y = I128(0, -1ul); ASSERT128(I128(0, 0xfffffffffffffffe), x + y); x = I128(-1ul, -1ul); y = I128(2, 0); ASSERT128(I128(1, -1ul), x + y); x = I128(-1ul, -1ul); y = I128(2, 2); ASSERT128(I128(2, 1), x + y); x = I128(-1ul, -1ul); y = I128(2, BANE); ASSERT128(I128(2, IMAX), x + y); x = I128(-1ul, -1ul); y = I128(2, -1ul); ASSERT128(I128(2, 0xfffffffffffffffe), x + y); x = I128(-1ul, -1ul); y = I128(BANE, 0); ASSERT128(I128(IMAX, -1ul), x + y); x = I128(-1ul, -1ul); y = I128(BANE, 2); ASSERT128(I128(BANE, 1), x + y); x = I128(-1ul, -1ul); y = I128(BANE, BANE); ASSERT128(I128(BANE, IMAX), x + y); x = I128(-1ul, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(BANE, 0xfffffffffffffffe), x + y); x = I128(-1ul, -1ul); y = I128(-1ul, 0); ASSERT128(I128(0xfffffffffffffffe, -1ul), x + y); x = I128(-1ul, -1ul); y = I128(-1ul, 2); ASSERT128(I128(-1ul, 1), x + y); x = I128(-1ul, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, IMAX), x + y); x = I128(-1ul, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, 0xfffffffffffffffe), x + y); } void testSub128(void) { __int128 x, y; x = I128(0, 0); y = I128(0, 0); ASSERT128(I128(0, 0), x - y); x = I128(0, 0); y = I128(0, 2); ASSERT128(I128(-1ul, 0xfffffffffffffffe), x - y); x = I128(0, 0); y = I128(0, BANE); ASSERT128(I128(-1ul, BANE), x - y); x = I128(0, 0); y = I128(0, -1ul); ASSERT128(I128(-1ul, 1), x - y); x = I128(0, 0); y = I128(2, 0); ASSERT128(I128(0xfffffffffffffffe, 0), x - y); x = I128(0, 0); y = I128(2, 2); ASSERT128(I128(IMAX2, 0xfffffffffffffffe), x - y); x = I128(0, 0); y = I128(2, BANE); ASSERT128(I128(IMAX2, BANE), x - y); x = I128(0, 0); y = I128(2, -1ul); ASSERT128(I128(IMAX2, 1), x - y); x = I128(0, 0); y = I128(BANE, 0); ASSERT128(I128(BANE, 0), x - y); x = I128(0, 0); y = I128(BANE, 2); ASSERT128(I128(IMAX, 0xfffffffffffffffe), x - y); x = I128(0, 0); y = I128(BANE, BANE); ASSERT128(I128(IMAX, BANE), x - y); x = I128(0, 0); y = I128(BANE, -1ul); ASSERT128(I128(IMAX, 1), x - y); x = I128(0, 0); y = I128(-1ul, 0); ASSERT128(I128(1, 0), x - y); x = I128(0, 0); y = I128(-1ul, 2); ASSERT128(I128(0, 0xfffffffffffffffe), x - y); x = I128(0, 0); y = I128(-1ul, BANE); ASSERT128(I128(0, BANE), x - y); x = I128(0, 0); y = I128(-1ul, -1ul); ASSERT128(I128(0, 1), x - y); x = I128(0, 2); y = I128(0, 0); ASSERT128(I128(0, 2), x - y); x = I128(0, 2); y = I128(0, 2); ASSERT128(I128(0, 0), x - y); x = I128(0, 2); y = I128(0, BANE); ASSERT128(I128(-1ul, BANE2), x - y); x = I128(0, 2); y = I128(0, -1ul); ASSERT128(I128(-1ul, 3), x - y); x = I128(0, 2); y = I128(2, 0); ASSERT128(I128(0xfffffffffffffffe, 2), x - y); x = I128(0, 2); y = I128(2, 2); ASSERT128(I128(0xfffffffffffffffe, 0), x - y); x = I128(0, 2); y = I128(2, BANE); ASSERT128(I128(IMAX2, BANE2), x - y); x = I128(0, 2); y = I128(2, -1ul); ASSERT128(I128(IMAX2, 3), x - y); x = I128(0, 2); y = I128(BANE, 0); ASSERT128(I128(BANE, 2), x - y); x = I128(0, 2); y = I128(BANE, 2); ASSERT128(I128(BANE, 0), x - y); x = I128(0, 2); y = I128(BANE, BANE); ASSERT128(I128(IMAX, BANE2), x - y); x = I128(0, 2); y = I128(BANE, -1ul); ASSERT128(I128(IMAX, 3), x - y); x = I128(0, 2); y = I128(-1ul, 0); ASSERT128(I128(1, 2), x - y); x = I128(0, 2); y = I128(-1ul, 2); ASSERT128(I128(1, 0), x - y); x = I128(0, 2); y = I128(-1ul, BANE); ASSERT128(I128(0, BANE2), x - y); x = I128(0, 2); y = I128(-1ul, -1ul); ASSERT128(I128(0, 3), x - y); x = I128(0, BANE); y = I128(0, 0); ASSERT128(I128(0, BANE), x - y); x = I128(0, BANE); y = I128(0, 2); ASSERT128(I128(0, 0x7ffffffffffffffe), x - y); x = I128(0, BANE); y = I128(0, BANE); ASSERT128(I128(0, 0), x - y); x = I128(0, BANE); y = I128(0, -1ul); ASSERT128(I128(-1ul, BANE1), x - y); x = I128(0, BANE); y = I128(2, 0); ASSERT128(I128(0xfffffffffffffffe, BANE), x - y); x = I128(0, BANE); y = I128(2, 2); ASSERT128(I128(0xfffffffffffffffe, 0x7ffffffffffffffe), x - y); x = I128(0, BANE); y = I128(2, BANE); ASSERT128(I128(0xfffffffffffffffe, 0), x - y); x = I128(0, BANE); y = I128(2, -1ul); ASSERT128(I128(IMAX2, BANE1), x - y); x = I128(0, BANE); y = I128(BANE, 0); ASSERT128(I128(BANE, BANE), x - y); x = I128(0, BANE); y = I128(BANE, 2); ASSERT128(I128(BANE, 0x7ffffffffffffffe), x - y); x = I128(0, BANE); y = I128(BANE, BANE); ASSERT128(I128(BANE, 0), x - y); x = I128(0, BANE); y = I128(BANE, -1ul); ASSERT128(I128(IMAX, BANE1), x - y); x = I128(0, BANE); y = I128(-1ul, 0); ASSERT128(I128(1, BANE), x - y); x = I128(0, BANE); y = I128(-1ul, 2); ASSERT128(I128(1, 0x7ffffffffffffffe), x - y); x = I128(0, BANE); y = I128(-1ul, BANE); ASSERT128(I128(1, 0), x - y); x = I128(0, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(0, BANE1), x - y); x = I128(0, -1ul); y = I128(0, 0); ASSERT128(I128(0, -1ul), x - y); x = I128(0, -1ul); y = I128(0, 2); ASSERT128(I128(0, IMAX2), x - y); x = I128(0, -1ul); y = I128(0, BANE); ASSERT128(I128(0, IMAX), x - y); x = I128(0, -1ul); y = I128(0, -1ul); ASSERT128(I128(0, 0), x - y); x = I128(0, -1ul); y = I128(2, 0); ASSERT128(I128(0xfffffffffffffffe, -1ul), x - y); x = I128(0, -1ul); y = I128(2, 2); ASSERT128(I128(0xfffffffffffffffe, IMAX2), x - y); x = I128(0, -1ul); y = I128(2, BANE); ASSERT128(I128(0xfffffffffffffffe, IMAX), x - y); x = I128(0, -1ul); y = I128(2, -1ul); ASSERT128(I128(0xfffffffffffffffe, 0), x - y); x = I128(0, -1ul); y = I128(BANE, 0); ASSERT128(I128(BANE, -1ul), x - y); x = I128(0, -1ul); y = I128(BANE, 2); ASSERT128(I128(BANE, IMAX2), x - y); x = I128(0, -1ul); y = I128(BANE, BANE); ASSERT128(I128(BANE, IMAX), x - y); x = I128(0, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(BANE, 0), x - y); x = I128(0, -1ul); y = I128(-1ul, 0); ASSERT128(I128(1, -1ul), x - y); x = I128(0, -1ul); y = I128(-1ul, 2); ASSERT128(I128(1, IMAX2), x - y); x = I128(0, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(1, IMAX), x - y); x = I128(0, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(1, 0), x - y); x = I128(2, 0); y = I128(0, 0); ASSERT128(I128(2, 0), x - y); x = I128(2, 0); y = I128(0, 2); ASSERT128(I128(1, 0xfffffffffffffffe), x - y); x = I128(2, 0); y = I128(0, BANE); ASSERT128(I128(1, BANE), x - y); x = I128(2, 0); y = I128(0, -1ul); ASSERT128(I128(1, 1), x - y); x = I128(2, 0); y = I128(2, 0); ASSERT128(I128(0, 0), x - y); x = I128(2, 0); y = I128(2, 2); ASSERT128(I128(-1ul, 0xfffffffffffffffe), x - y); x = I128(2, 0); y = I128(2, BANE); ASSERT128(I128(-1ul, BANE), x - y); x = I128(2, 0); y = I128(2, -1ul); ASSERT128(I128(-1ul, 1), x - y); x = I128(2, 0); y = I128(BANE, 0); ASSERT128(I128(BANE2, 0), x - y); x = I128(2, 0); y = I128(BANE, 2); ASSERT128(I128(BANE1, 0xfffffffffffffffe), x - y); x = I128(2, 0); y = I128(BANE, BANE); ASSERT128(I128(BANE1, BANE), x - y); x = I128(2, 0); y = I128(BANE, -1ul); ASSERT128(I128(BANE1, 1), x - y); x = I128(2, 0); y = I128(-1ul, 0); ASSERT128(I128(3, 0), x - y); x = I128(2, 0); y = I128(-1ul, 2); ASSERT128(I128(2, 0xfffffffffffffffe), x - y); x = I128(2, 0); y = I128(-1ul, BANE); ASSERT128(I128(2, BANE), x - y); x = I128(2, 0); y = I128(-1ul, -1ul); ASSERT128(I128(2, 1), x - y); x = I128(2, 2); y = I128(0, 0); ASSERT128(I128(2, 2), x - y); x = I128(2, 2); y = I128(0, 2); ASSERT128(I128(2, 0), x - y); x = I128(2, 2); y = I128(0, BANE); ASSERT128(I128(1, BANE2), x - y); x = I128(2, 2); y = I128(0, -1ul); ASSERT128(I128(1, 3), x - y); x = I128(2, 2); y = I128(2, 0); ASSERT128(I128(0, 2), x - y); x = I128(2, 2); y = I128(2, 2); ASSERT128(I128(0, 0), x - y); x = I128(2, 2); y = I128(2, BANE); ASSERT128(I128(-1ul, BANE2), x - y); x = I128(2, 2); y = I128(2, -1ul); ASSERT128(I128(-1ul, 3), x - y); x = I128(2, 2); y = I128(BANE, 0); ASSERT128(I128(BANE2, 2), x - y); x = I128(2, 2); y = I128(BANE, 2); ASSERT128(I128(BANE2, 0), x - y); x = I128(2, 2); y = I128(BANE, BANE); ASSERT128(I128(BANE1, BANE2), x - y); x = I128(2, 2); y = I128(BANE, -1ul); ASSERT128(I128(BANE1, 3), x - y); x = I128(2, 2); y = I128(-1ul, 0); ASSERT128(I128(3, 2), x - y); x = I128(2, 2); y = I128(-1ul, 2); ASSERT128(I128(3, 0), x - y); x = I128(2, 2); y = I128(-1ul, BANE); ASSERT128(I128(2, BANE2), x - y); x = I128(2, 2); y = I128(-1ul, -1ul); ASSERT128(I128(2, 3), x - y); x = I128(2, BANE); y = I128(0, 0); ASSERT128(I128(2, BANE), x - y); x = I128(2, BANE); y = I128(0, 2); ASSERT128(I128(2, 0x7ffffffffffffffe), x - y); x = I128(2, BANE); y = I128(0, BANE); ASSERT128(I128(2, 0), x - y); x = I128(2, BANE); y = I128(0, -1ul); ASSERT128(I128(1, BANE1), x - y); x = I128(2, BANE); y = I128(2, 0); ASSERT128(I128(0, BANE), x - y); x = I128(2, BANE); y = I128(2, 2); ASSERT128(I128(0, 0x7ffffffffffffffe), x - y); x = I128(2, BANE); y = I128(2, BANE); ASSERT128(I128(0, 0), x - y); x = I128(2, BANE); y = I128(2, -1ul); ASSERT128(I128(-1ul, BANE1), x - y); x = I128(2, BANE); y = I128(BANE, 0); ASSERT128(I128(BANE2, BANE), x - y); x = I128(2, BANE); y = I128(BANE, 2); ASSERT128(I128(BANE2, 0x7ffffffffffffffe), x - y); x = I128(2, BANE); y = I128(BANE, BANE); ASSERT128(I128(BANE2, 0), x - y); x = I128(2, BANE); y = I128(BANE, -1ul); ASSERT128(I128(BANE1, BANE1), x - y); x = I128(2, BANE); y = I128(-1ul, 0); ASSERT128(I128(3, BANE), x - y); x = I128(2, BANE); y = I128(-1ul, 2); ASSERT128(I128(3, 0x7ffffffffffffffe), x - y); x = I128(2, BANE); y = I128(-1ul, BANE); ASSERT128(I128(3, 0), x - y); x = I128(2, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(2, BANE1), x - y); x = I128(2, -1ul); y = I128(0, 0); ASSERT128(I128(2, -1ul), x - y); x = I128(2, -1ul); y = I128(0, 2); ASSERT128(I128(2, IMAX2), x - y); x = I128(2, -1ul); y = I128(0, BANE); ASSERT128(I128(2, IMAX), x - y); x = I128(2, -1ul); y = I128(0, -1ul); ASSERT128(I128(2, 0), x - y); x = I128(2, -1ul); y = I128(2, 0); ASSERT128(I128(0, -1ul), x - y); x = I128(2, -1ul); y = I128(2, 2); ASSERT128(I128(0, IMAX2), x - y); x = I128(2, -1ul); y = I128(2, BANE); ASSERT128(I128(0, IMAX), x - y); x = I128(2, -1ul); y = I128(2, -1ul); ASSERT128(I128(0, 0), x - y); x = I128(2, -1ul); y = I128(BANE, 0); ASSERT128(I128(BANE2, -1ul), x - y); x = I128(2, -1ul); y = I128(BANE, 2); ASSERT128(I128(BANE2, IMAX2), x - y); x = I128(2, -1ul); y = I128(BANE, BANE); ASSERT128(I128(BANE2, IMAX), x - y); x = I128(2, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(BANE2, 0), x - y); x = I128(2, -1ul); y = I128(-1ul, 0); ASSERT128(I128(3, -1ul), x - y); x = I128(2, -1ul); y = I128(-1ul, 2); ASSERT128(I128(3, IMAX2), x - y); x = I128(2, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(3, IMAX), x - y); x = I128(2, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(3, 0), x - y); x = I128(BANE, 0); y = I128(0, 0); ASSERT128(I128(BANE, 0), x - y); x = I128(BANE, 0); y = I128(0, 2); ASSERT128(I128(IMAX, 0xfffffffffffffffe), x - y); x = I128(BANE, 0); y = I128(0, BANE); ASSERT128(I128(IMAX, BANE), x - y); x = I128(BANE, 0); y = I128(0, -1ul); ASSERT128(I128(IMAX, 1), x - y); x = I128(BANE, 0); y = I128(2, 0); ASSERT128(I128(0x7ffffffffffffffe, 0), x - y); x = I128(BANE, 0); y = I128(2, 2); ASSERT128(I128(0x7ffffffffffffffd, 0xfffffffffffffffe), x - y); x = I128(BANE, 0); y = I128(2, BANE); ASSERT128(I128(0x7ffffffffffffffd, BANE), x - y); x = I128(BANE, 0); y = I128(2, -1ul); ASSERT128(I128(0x7ffffffffffffffd, 1), x - y); x = I128(BANE, 0); y = I128(BANE, 0); ASSERT128(I128(0, 0), x - y); x = I128(BANE, 0); y = I128(BANE, 2); ASSERT128(I128(-1ul, 0xfffffffffffffffe), x - y); x = I128(BANE, 0); y = I128(BANE, BANE); ASSERT128(I128(-1ul, BANE), x - y); x = I128(BANE, 0); y = I128(BANE, -1ul); ASSERT128(I128(-1ul, 1), x - y); x = I128(BANE, 0); y = I128(-1ul, 0); ASSERT128(I128(BANE1, 0), x - y); x = I128(BANE, 0); y = I128(-1ul, 2); ASSERT128(I128(BANE, 0xfffffffffffffffe), x - y); x = I128(BANE, 0); y = I128(-1ul, BANE); ASSERT128(I128(BANE, BANE), x - y); x = I128(BANE, 0); y = I128(-1ul, -1ul); ASSERT128(I128(BANE, 1), x - y); x = I128(BANE, 2); y = I128(0, 0); ASSERT128(I128(BANE, 2), x - y); x = I128(BANE, 2); y = I128(0, 2); ASSERT128(I128(BANE, 0), x - y); x = I128(BANE, 2); y = I128(0, BANE); ASSERT128(I128(IMAX, BANE2), x - y); x = I128(BANE, 2); y = I128(0, -1ul); ASSERT128(I128(IMAX, 3), x - y); x = I128(BANE, 2); y = I128(2, 0); ASSERT128(I128(0x7ffffffffffffffe, 2), x - y); x = I128(BANE, 2); y = I128(2, 2); ASSERT128(I128(0x7ffffffffffffffe, 0), x - y); x = I128(BANE, 2); y = I128(2, BANE); ASSERT128(I128(0x7ffffffffffffffd, BANE2), x - y); x = I128(BANE, 2); y = I128(2, -1ul); ASSERT128(I128(0x7ffffffffffffffd, 3), x - y); x = I128(BANE, 2); y = I128(BANE, 0); ASSERT128(I128(0, 2), x - y); x = I128(BANE, 2); y = I128(BANE, 2); ASSERT128(I128(0, 0), x - y); x = I128(BANE, 2); y = I128(BANE, BANE); ASSERT128(I128(-1ul, BANE2), x - y); x = I128(BANE, 2); y = I128(BANE, -1ul); ASSERT128(I128(-1ul, 3), x - y); x = I128(BANE, 2); y = I128(-1ul, 0); ASSERT128(I128(BANE1, 2), x - y); x = I128(BANE, 2); y = I128(-1ul, 2); ASSERT128(I128(BANE1, 0), x - y); x = I128(BANE, 2); y = I128(-1ul, BANE); ASSERT128(I128(BANE, BANE2), x - y); x = I128(BANE, 2); y = I128(-1ul, -1ul); ASSERT128(I128(BANE, 3), x - y); x = I128(BANE, BANE); y = I128(0, 0); ASSERT128(I128(BANE, BANE), x - y); x = I128(BANE, BANE); y = I128(0, 2); ASSERT128(I128(BANE, 0x7ffffffffffffffe), x - y); x = I128(BANE, BANE); y = I128(0, BANE); ASSERT128(I128(BANE, 0), x - y); x = I128(BANE, BANE); y = I128(0, -1ul); ASSERT128(I128(IMAX, BANE1), x - y); x = I128(BANE, BANE); y = I128(2, 0); ASSERT128(I128(0x7ffffffffffffffe, BANE), x - y); x = I128(BANE, BANE); y = I128(2, 2); ASSERT128(I128(0x7ffffffffffffffe, 0x7ffffffffffffffe), x - y); x = I128(BANE, BANE); y = I128(2, BANE); ASSERT128(I128(0x7ffffffffffffffe, 0), x - y); x = I128(BANE, BANE); y = I128(2, -1ul); ASSERT128(I128(0x7ffffffffffffffd, BANE1), x - y); x = I128(BANE, BANE); y = I128(BANE, 0); ASSERT128(I128(0, BANE), x - y); x = I128(BANE, BANE); y = I128(BANE, 2); ASSERT128(I128(0, 0x7ffffffffffffffe), x - y); x = I128(BANE, BANE); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x - y); x = I128(BANE, BANE); y = I128(BANE, -1ul); ASSERT128(I128(-1ul, BANE1), x - y); x = I128(BANE, BANE); y = I128(-1ul, 0); ASSERT128(I128(BANE1, BANE), x - y); x = I128(BANE, BANE); y = I128(-1ul, 2); ASSERT128(I128(BANE1, 0x7ffffffffffffffe), x - y); x = I128(BANE, BANE); y = I128(-1ul, BANE); ASSERT128(I128(BANE1, 0), x - y); x = I128(BANE, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(BANE, BANE1), x - y); x = I128(BANE, -1ul); y = I128(0, 0); ASSERT128(I128(BANE, -1ul), x - y); x = I128(BANE, -1ul); y = I128(0, 2); ASSERT128(I128(BANE, IMAX2), x - y); x = I128(BANE, -1ul); y = I128(0, BANE); ASSERT128(I128(BANE, IMAX), x - y); x = I128(BANE, -1ul); y = I128(0, -1ul); ASSERT128(I128(BANE, 0), x - y); x = I128(BANE, -1ul); y = I128(2, 0); ASSERT128(I128(0x7ffffffffffffffe, -1ul), x - y); x = I128(BANE, -1ul); y = I128(2, 2); ASSERT128(I128(0x7ffffffffffffffe, IMAX2), x - y); x = I128(BANE, -1ul); y = I128(2, BANE); ASSERT128(I128(0x7ffffffffffffffe, IMAX), x - y); x = I128(BANE, -1ul); y = I128(2, -1ul); ASSERT128(I128(0x7ffffffffffffffe, 0), x - y); x = I128(BANE, -1ul); y = I128(BANE, 0); ASSERT128(I128(0, -1ul), x - y); x = I128(BANE, -1ul); y = I128(BANE, 2); ASSERT128(I128(0, IMAX2), x - y); x = I128(BANE, -1ul); y = I128(BANE, BANE); ASSERT128(I128(0, IMAX), x - y); x = I128(BANE, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x - y); x = I128(BANE, -1ul); y = I128(-1ul, 0); ASSERT128(I128(BANE1, -1ul), x - y); x = I128(BANE, -1ul); y = I128(-1ul, 2); ASSERT128(I128(BANE1, IMAX2), x - y); x = I128(BANE, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(BANE1, IMAX), x - y); x = I128(BANE, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(BANE1, 0), x - y); x = I128(-1ul, 0); y = I128(0, 0); ASSERT128(I128(-1ul, 0), x - y); x = I128(-1ul, 0); y = I128(0, 2); ASSERT128(I128(0xfffffffffffffffe, 0xfffffffffffffffe), x - y); x = I128(-1ul, 0); y = I128(0, BANE); ASSERT128(I128(0xfffffffffffffffe, BANE), x - y); x = I128(-1ul, 0); y = I128(0, -1ul); ASSERT128(I128(0xfffffffffffffffe, 1), x - y); x = I128(-1ul, 0); y = I128(2, 0); ASSERT128(I128(IMAX2, 0), x - y); x = I128(-1ul, 0); y = I128(2, 2); ASSERT128(I128(0xfffffffffffffffc, 0xfffffffffffffffe), x - y); x = I128(-1ul, 0); y = I128(2, BANE); ASSERT128(I128(0xfffffffffffffffc, BANE), x - y); x = I128(-1ul, 0); y = I128(2, -1ul); ASSERT128(I128(0xfffffffffffffffc, 1), x - y); x = I128(-1ul, 0); y = I128(BANE, 0); ASSERT128(I128(IMAX, 0), x - y); x = I128(-1ul, 0); y = I128(BANE, 2); ASSERT128(I128(0x7ffffffffffffffe, 0xfffffffffffffffe), x - y); x = I128(-1ul, 0); y = I128(BANE, BANE); ASSERT128(I128(0x7ffffffffffffffe, BANE), x - y); x = I128(-1ul, 0); y = I128(BANE, -1ul); ASSERT128(I128(0x7ffffffffffffffe, 1), x - y); x = I128(-1ul, 0); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x - y); x = I128(-1ul, 0); y = I128(-1ul, 2); ASSERT128(I128(-1ul, 0xfffffffffffffffe), x - y); x = I128(-1ul, 0); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, BANE), x - y); x = I128(-1ul, 0); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, 1), x - y); x = I128(-1ul, 2); y = I128(0, 0); ASSERT128(I128(-1ul, 2), x - y); x = I128(-1ul, 2); y = I128(0, 2); ASSERT128(I128(-1ul, 0), x - y); x = I128(-1ul, 2); y = I128(0, BANE); ASSERT128(I128(0xfffffffffffffffe, BANE2), x - y); x = I128(-1ul, 2); y = I128(0, -1ul); ASSERT128(I128(0xfffffffffffffffe, 3), x - y); x = I128(-1ul, 2); y = I128(2, 0); ASSERT128(I128(IMAX2, 2), x - y); x = I128(-1ul, 2); y = I128(2, 2); ASSERT128(I128(IMAX2, 0), x - y); x = I128(-1ul, 2); y = I128(2, BANE); ASSERT128(I128(0xfffffffffffffffc, BANE2), x - y); x = I128(-1ul, 2); y = I128(2, -1ul); ASSERT128(I128(0xfffffffffffffffc, 3), x - y); x = I128(-1ul, 2); y = I128(BANE, 0); ASSERT128(I128(IMAX, 2), x - y); x = I128(-1ul, 2); y = I128(BANE, 2); ASSERT128(I128(IMAX, 0), x - y); x = I128(-1ul, 2); y = I128(BANE, BANE); ASSERT128(I128(0x7ffffffffffffffe, BANE2), x - y); x = I128(-1ul, 2); y = I128(BANE, -1ul); ASSERT128(I128(0x7ffffffffffffffe, 3), x - y); x = I128(-1ul, 2); y = I128(-1ul, 0); ASSERT128(I128(0, 2), x - y); x = I128(-1ul, 2); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x - y); x = I128(-1ul, 2); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, BANE2), x - y); x = I128(-1ul, 2); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, 3), x - y); x = I128(-1ul, BANE); y = I128(0, 0); ASSERT128(I128(-1ul, BANE), x - y); x = I128(-1ul, BANE); y = I128(0, 2); ASSERT128(I128(-1ul, 0x7ffffffffffffffe), x - y); x = I128(-1ul, BANE); y = I128(0, BANE); ASSERT128(I128(-1ul, 0), x - y); x = I128(-1ul, BANE); y = I128(0, -1ul); ASSERT128(I128(0xfffffffffffffffe, BANE1), x - y); x = I128(-1ul, BANE); y = I128(2, 0); ASSERT128(I128(IMAX2, BANE), x - y); x = I128(-1ul, BANE); y = I128(2, 2); ASSERT128(I128(IMAX2, 0x7ffffffffffffffe), x - y); x = I128(-1ul, BANE); y = I128(2, BANE); ASSERT128(I128(IMAX2, 0), x - y); x = I128(-1ul, BANE); y = I128(2, -1ul); ASSERT128(I128(0xfffffffffffffffc, BANE1), x - y); x = I128(-1ul, BANE); y = I128(BANE, 0); ASSERT128(I128(IMAX, BANE), x - y); x = I128(-1ul, BANE); y = I128(BANE, 2); ASSERT128(I128(IMAX, 0x7ffffffffffffffe), x - y); x = I128(-1ul, BANE); y = I128(BANE, BANE); ASSERT128(I128(IMAX, 0), x - y); x = I128(-1ul, BANE); y = I128(BANE, -1ul); ASSERT128(I128(0x7ffffffffffffffe, BANE1), x - y); x = I128(-1ul, BANE); y = I128(-1ul, 0); ASSERT128(I128(0, BANE), x - y); x = I128(-1ul, BANE); y = I128(-1ul, 2); ASSERT128(I128(0, 0x7ffffffffffffffe), x - y); x = I128(-1ul, BANE); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x - y); x = I128(-1ul, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, BANE1), x - y); x = I128(-1ul, -1ul); y = I128(0, 0); ASSERT128(I128(-1ul, -1ul), x - y); x = I128(-1ul, -1ul); y = I128(0, 2); ASSERT128(I128(-1ul, IMAX2), x - y); x = I128(-1ul, -1ul); y = I128(0, BANE); ASSERT128(I128(-1ul, IMAX), x - y); x = I128(-1ul, -1ul); y = I128(0, -1ul); ASSERT128(I128(-1ul, 0), x - y); x = I128(-1ul, -1ul); y = I128(2, 0); ASSERT128(I128(IMAX2, -1ul), x - y); x = I128(-1ul, -1ul); y = I128(2, 2); ASSERT128(I128(IMAX2, IMAX2), x - y); x = I128(-1ul, -1ul); y = I128(2, BANE); ASSERT128(I128(IMAX2, IMAX), x - y); x = I128(-1ul, -1ul); y = I128(2, -1ul); ASSERT128(I128(IMAX2, 0), x - y); x = I128(-1ul, -1ul); y = I128(BANE, 0); ASSERT128(I128(IMAX, -1ul), x - y); x = I128(-1ul, -1ul); y = I128(BANE, 2); ASSERT128(I128(IMAX, IMAX2), x - y); x = I128(-1ul, -1ul); y = I128(BANE, BANE); ASSERT128(I128(IMAX, IMAX), x - y); x = I128(-1ul, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(IMAX, 0), x - y); x = I128(-1ul, -1ul); y = I128(-1ul, 0); ASSERT128(I128(0, -1ul), x - y); x = I128(-1ul, -1ul); y = I128(-1ul, 2); ASSERT128(I128(0, IMAX2), x - y); x = I128(-1ul, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(0, IMAX), x - y); x = I128(-1ul, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x - y); } void testMul128(void) { __int128 x, y; x = I128(0, 0); y = I128(0, 0); ASSERT128(I128(0, 0), x * y); x = I128(0, 0); y = I128(0, 2); ASSERT128(I128(0, 0), x * y); x = I128(0, 0); y = I128(0, BANE); ASSERT128(I128(0, 0), x * y); x = I128(0, 0); y = I128(0, -1ul); ASSERT128(I128(0, 0), x * y); x = I128(0, 0); y = I128(2, 0); ASSERT128(I128(0, 0), x * y); x = I128(0, 0); y = I128(2, 2); ASSERT128(I128(0, 0), x * y); x = I128(0, 0); y = I128(2, BANE); ASSERT128(I128(0, 0), x * y); x = I128(0, 0); y = I128(2, -1ul); ASSERT128(I128(0, 0), x * y); x = I128(0, 0); y = I128(BANE, 0); ASSERT128(I128(0, 0), x * y); x = I128(0, 0); y = I128(BANE, 2); ASSERT128(I128(0, 0), x * y); x = I128(0, 0); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x * y); x = I128(0, 0); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x * y); x = I128(0, 0); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x * y); x = I128(0, 0); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x * y); x = I128(0, 0); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x * y); x = I128(0, 0); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x * y); x = I128(0, 2); y = I128(0, 0); ASSERT128(I128(0, 0), x * y); x = I128(0, 2); y = I128(0, 2); ASSERT128(I128(0, 4), x * y); x = I128(0, 2); y = I128(0, BANE); ASSERT128(I128(1, 0), x * y); x = I128(0, 2); y = I128(0, -1ul); ASSERT128(I128(1, 0xfffffffffffffffe), x * y); x = I128(0, 2); y = I128(2, 0); ASSERT128(I128(4, 0), x * y); x = I128(0, 2); y = I128(2, 2); ASSERT128(I128(4, 4), x * y); x = I128(0, 2); y = I128(2, BANE); ASSERT128(I128(5, 0), x * y); x = I128(0, 2); y = I128(2, -1ul); ASSERT128(I128(5, 0xfffffffffffffffe), x * y); x = I128(0, 2); y = I128(BANE, 0); ASSERT128(I128(0, 0), x * y); x = I128(0, 2); y = I128(BANE, 2); ASSERT128(I128(0, 4), x * y); x = I128(0, 2); y = I128(BANE, BANE); ASSERT128(I128(1, 0), x * y); x = I128(0, 2); y = I128(BANE, -1ul); ASSERT128(I128(1, 0xfffffffffffffffe), x * y); x = I128(0, 2); y = I128(-1ul, 0); ASSERT128(I128(0xfffffffffffffffe, 0), x * y); x = I128(0, 2); y = I128(-1ul, 2); ASSERT128(I128(0xfffffffffffffffe, 4), x * y); x = I128(0, 2); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, 0), x * y); x = I128(0, 2); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, 0xfffffffffffffffe), x * y); x = I128(0, BANE); y = I128(0, 0); ASSERT128(I128(0, 0), x * y); x = I128(0, BANE); y = I128(0, 2); ASSERT128(I128(1, 0), x * y); x = I128(0, BANE); y = I128(0, BANE); ASSERT128(I128(0x4000000000000000, 0), x * y); x = I128(0, BANE); y = I128(0, -1ul); ASSERT128(I128(IMAX, BANE), x * y); x = I128(0, BANE); y = I128(2, 0); ASSERT128(I128(0, 0), x * y); x = I128(0, BANE); y = I128(2, 2); ASSERT128(I128(1, 0), x * y); x = I128(0, BANE); y = I128(2, BANE); ASSERT128(I128(0x4000000000000000, 0), x * y); x = I128(0, BANE); y = I128(2, -1ul); ASSERT128(I128(IMAX, BANE), x * y); x = I128(0, BANE); y = I128(BANE, 0); ASSERT128(I128(0, 0), x * y); x = I128(0, BANE); y = I128(BANE, 2); ASSERT128(I128(1, 0), x * y); x = I128(0, BANE); y = I128(BANE, BANE); ASSERT128(I128(0x4000000000000000, 0), x * y); x = I128(0, BANE); y = I128(BANE, -1ul); ASSERT128(I128(IMAX, BANE), x * y); x = I128(0, BANE); y = I128(-1ul, 0); ASSERT128(I128(BANE, 0), x * y); x = I128(0, BANE); y = I128(-1ul, 2); ASSERT128(I128(BANE1, 0), x * y); x = I128(0, BANE); y = I128(-1ul, BANE); ASSERT128(I128(0xc000000000000000, 0), x * y); x = I128(0, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, BANE), x * y); x = I128(0, -1ul); y = I128(0, 0); ASSERT128(I128(0, 0), x * y); x = I128(0, -1ul); y = I128(0, 2); ASSERT128(I128(1, 0xfffffffffffffffe), x * y); x = I128(0, -1ul); y = I128(0, BANE); ASSERT128(I128(IMAX, BANE), x * y); x = I128(0, -1ul); y = I128(0, -1ul); ASSERT128(I128(0xfffffffffffffffe, 1), x * y); x = I128(0, -1ul); y = I128(2, 0); ASSERT128(I128(0xfffffffffffffffe, 0), x * y); x = I128(0, -1ul); y = I128(2, 2); ASSERT128(I128(-1ul, 0xfffffffffffffffe), x * y); x = I128(0, -1ul); y = I128(2, BANE); ASSERT128(I128(0x7ffffffffffffffd, BANE), x * y); x = I128(0, -1ul); y = I128(2, -1ul); ASSERT128(I128(0xfffffffffffffffc, 1), x * y); x = I128(0, -1ul); y = I128(BANE, 0); ASSERT128(I128(BANE, 0), x * y); x = I128(0, -1ul); y = I128(BANE, 2); ASSERT128(I128(BANE1, 0xfffffffffffffffe), x * y); x = I128(0, -1ul); y = I128(BANE, BANE); ASSERT128(I128(-1ul, BANE), x * y); x = I128(0, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(0x7ffffffffffffffe, 1), x * y); x = I128(0, -1ul); y = I128(-1ul, 0); ASSERT128(I128(1, 0), x * y); x = I128(0, -1ul); y = I128(-1ul, 2); ASSERT128(I128(2, 0xfffffffffffffffe), x * y); x = I128(0, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(BANE, BANE), x * y); x = I128(0, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, 1), x * y); x = I128(2, 0); y = I128(0, 0); ASSERT128(I128(0, 0), x * y); x = I128(2, 0); y = I128(0, 2); ASSERT128(I128(4, 0), x * y); x = I128(2, 0); y = I128(0, BANE); ASSERT128(I128(0, 0), x * y); x = I128(2, 0); y = I128(0, -1ul); ASSERT128(I128(0xfffffffffffffffe, 0), x * y); x = I128(2, 0); y = I128(2, 0); ASSERT128(I128(0, 0), x * y); x = I128(2, 0); y = I128(2, 2); ASSERT128(I128(4, 0), x * y); x = I128(2, 0); y = I128(2, BANE); ASSERT128(I128(0, 0), x * y); x = I128(2, 0); y = I128(2, -1ul); ASSERT128(I128(0xfffffffffffffffe, 0), x * y); x = I128(2, 0); y = I128(BANE, 0); ASSERT128(I128(0, 0), x * y); x = I128(2, 0); y = I128(BANE, 2); ASSERT128(I128(4, 0), x * y); x = I128(2, 0); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x * y); x = I128(2, 0); y = I128(BANE, -1ul); ASSERT128(I128(0xfffffffffffffffe, 0), x * y); x = I128(2, 0); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x * y); x = I128(2, 0); y = I128(-1ul, 2); ASSERT128(I128(4, 0), x * y); x = I128(2, 0); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x * y); x = I128(2, 0); y = I128(-1ul, -1ul); ASSERT128(I128(0xfffffffffffffffe, 0), x * y); x = I128(2, 2); y = I128(0, 0); ASSERT128(I128(0, 0), x * y); x = I128(2, 2); y = I128(0, 2); ASSERT128(I128(4, 4), x * y); x = I128(2, 2); y = I128(0, BANE); ASSERT128(I128(1, 0), x * y); x = I128(2, 2); y = I128(0, -1ul); ASSERT128(I128(-1ul, 0xfffffffffffffffe), x * y); x = I128(2, 2); y = I128(2, 0); ASSERT128(I128(4, 0), x * y); x = I128(2, 2); y = I128(2, 2); ASSERT128(I128(0x0000000000000008, 4), x * y); x = I128(2, 2); y = I128(2, BANE); ASSERT128(I128(5, 0), x * y); x = I128(2, 2); y = I128(2, -1ul); ASSERT128(I128(3, 0xfffffffffffffffe), x * y); x = I128(2, 2); y = I128(BANE, 0); ASSERT128(I128(0, 0), x * y); x = I128(2, 2); y = I128(BANE, 2); ASSERT128(I128(4, 4), x * y); x = I128(2, 2); y = I128(BANE, BANE); ASSERT128(I128(1, 0), x * y); x = I128(2, 2); y = I128(BANE, -1ul); ASSERT128(I128(-1ul, 0xfffffffffffffffe), x * y); x = I128(2, 2); y = I128(-1ul, 0); ASSERT128(I128(0xfffffffffffffffe, 0), x * y); x = I128(2, 2); y = I128(-1ul, 2); ASSERT128(I128(2, 4), x * y); x = I128(2, 2); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, 0), x * y); x = I128(2, 2); y = I128(-1ul, -1ul); ASSERT128(I128(IMAX2, 0xfffffffffffffffe), x * y); x = I128(2, BANE); y = I128(0, 0); ASSERT128(I128(0, 0), x * y); x = I128(2, BANE); y = I128(0, 2); ASSERT128(I128(5, 0), x * y); x = I128(2, BANE); y = I128(0, BANE); ASSERT128(I128(0x4000000000000000, 0), x * y); x = I128(2, BANE); y = I128(0, -1ul); ASSERT128(I128(0x7ffffffffffffffd, BANE), x * y); x = I128(2, BANE); y = I128(2, 0); ASSERT128(I128(0, 0), x * y); x = I128(2, BANE); y = I128(2, 2); ASSERT128(I128(5, 0), x * y); x = I128(2, BANE); y = I128(2, BANE); ASSERT128(I128(0x4000000000000000, 0), x * y); x = I128(2, BANE); y = I128(2, -1ul); ASSERT128(I128(0x7ffffffffffffffd, BANE), x * y); x = I128(2, BANE); y = I128(BANE, 0); ASSERT128(I128(0, 0), x * y); x = I128(2, BANE); y = I128(BANE, 2); ASSERT128(I128(5, 0), x * y); x = I128(2, BANE); y = I128(BANE, BANE); ASSERT128(I128(0x4000000000000000, 0), x * y); x = I128(2, BANE); y = I128(BANE, -1ul); ASSERT128(I128(0x7ffffffffffffffd, BANE), x * y); x = I128(2, BANE); y = I128(-1ul, 0); ASSERT128(I128(BANE, 0), x * y); x = I128(2, BANE); y = I128(-1ul, 2); ASSERT128(I128(0x8000000000000005, 0), x * y); x = I128(2, BANE); y = I128(-1ul, BANE); ASSERT128(I128(0xc000000000000000, 0), x * y); x = I128(2, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(IMAX2, BANE), x * y); x = I128(2, -1ul); y = I128(0, 0); ASSERT128(I128(0, 0), x * y); x = I128(2, -1ul); y = I128(0, 2); ASSERT128(I128(5, 0xfffffffffffffffe), x * y); x = I128(2, -1ul); y = I128(0, BANE); ASSERT128(I128(IMAX, BANE), x * y); x = I128(2, -1ul); y = I128(0, -1ul); ASSERT128(I128(0xfffffffffffffffc, 1), x * y); x = I128(2, -1ul); y = I128(2, 0); ASSERT128(I128(0xfffffffffffffffe, 0), x * y); x = I128(2, -1ul); y = I128(2, 2); ASSERT128(I128(3, 0xfffffffffffffffe), x * y); x = I128(2, -1ul); y = I128(2, BANE); ASSERT128(I128(0x7ffffffffffffffd, BANE), x * y); x = I128(2, -1ul); y = I128(2, -1ul); ASSERT128(I128(0xfffffffffffffffa, 1), x * y); x = I128(2, -1ul); y = I128(BANE, 0); ASSERT128(I128(BANE, 0), x * y); x = I128(2, -1ul); y = I128(BANE, 2); ASSERT128(I128(0x8000000000000005, 0xfffffffffffffffe), x * y); x = I128(2, -1ul); y = I128(BANE, BANE); ASSERT128(I128(-1ul, BANE), x * y); x = I128(2, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(0x7ffffffffffffffc, 1), x * y); x = I128(2, -1ul); y = I128(-1ul, 0); ASSERT128(I128(1, 0), x * y); x = I128(2, -1ul); y = I128(-1ul, 2); ASSERT128(I128(0x0000000000000006, 0xfffffffffffffffe), x * y); x = I128(2, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(BANE, BANE), x * y); x = I128(2, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(IMAX2, 1), x * y); x = I128(BANE, 0); y = I128(0, 0); ASSERT128(I128(0, 0), x * y); x = I128(BANE, 0); y = I128(0, 2); ASSERT128(I128(0, 0), x * y); x = I128(BANE, 0); y = I128(0, BANE); ASSERT128(I128(0, 0), x * y); x = I128(BANE, 0); y = I128(0, -1ul); ASSERT128(I128(BANE, 0), x * y); x = I128(BANE, 0); y = I128(2, 0); ASSERT128(I128(0, 0), x * y); x = I128(BANE, 0); y = I128(2, 2); ASSERT128(I128(0, 0), x * y); x = I128(BANE, 0); y = I128(2, BANE); ASSERT128(I128(0, 0), x * y); x = I128(BANE, 0); y = I128(2, -1ul); ASSERT128(I128(BANE, 0), x * y); x = I128(BANE, 0); y = I128(BANE, 0); ASSERT128(I128(0, 0), x * y); x = I128(BANE, 0); y = I128(BANE, 2); ASSERT128(I128(0, 0), x * y); x = I128(BANE, 0); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x * y); x = I128(BANE, 0); y = I128(BANE, -1ul); ASSERT128(I128(BANE, 0), x * y); x = I128(BANE, 0); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x * y); x = I128(BANE, 0); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x * y); x = I128(BANE, 0); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x * y); x = I128(BANE, 0); y = I128(-1ul, -1ul); ASSERT128(I128(BANE, 0), x * y); x = I128(BANE, 2); y = I128(0, 0); ASSERT128(I128(0, 0), x * y); x = I128(BANE, 2); y = I128(0, 2); ASSERT128(I128(0, 4), x * y); x = I128(BANE, 2); y = I128(0, BANE); ASSERT128(I128(1, 0), x * y); x = I128(BANE, 2); y = I128(0, -1ul); ASSERT128(I128(BANE1, 0xfffffffffffffffe), x * y); x = I128(BANE, 2); y = I128(2, 0); ASSERT128(I128(4, 0), x * y); x = I128(BANE, 2); y = I128(2, 2); ASSERT128(I128(4, 4), x * y); x = I128(BANE, 2); y = I128(2, BANE); ASSERT128(I128(5, 0), x * y); x = I128(BANE, 2); y = I128(2, -1ul); ASSERT128(I128(0x8000000000000005, 0xfffffffffffffffe), x * y); x = I128(BANE, 2); y = I128(BANE, 0); ASSERT128(I128(0, 0), x * y); x = I128(BANE, 2); y = I128(BANE, 2); ASSERT128(I128(0, 4), x * y); x = I128(BANE, 2); y = I128(BANE, BANE); ASSERT128(I128(1, 0), x * y); x = I128(BANE, 2); y = I128(BANE, -1ul); ASSERT128(I128(BANE1, 0xfffffffffffffffe), x * y); x = I128(BANE, 2); y = I128(-1ul, 0); ASSERT128(I128(0xfffffffffffffffe, 0), x * y); x = I128(BANE, 2); y = I128(-1ul, 2); ASSERT128(I128(0xfffffffffffffffe, 4), x * y); x = I128(BANE, 2); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, 0), x * y); x = I128(BANE, 2); y = I128(-1ul, -1ul); ASSERT128(I128(IMAX, 0xfffffffffffffffe), x * y); x = I128(BANE, BANE); y = I128(0, 0); ASSERT128(I128(0, 0), x * y); x = I128(BANE, BANE); y = I128(0, 2); ASSERT128(I128(1, 0), x * y); x = I128(BANE, BANE); y = I128(0, BANE); ASSERT128(I128(0x4000000000000000, 0), x * y); x = I128(BANE, BANE); y = I128(0, -1ul); ASSERT128(I128(-1ul, BANE), x * y); x = I128(BANE, BANE); y = I128(2, 0); ASSERT128(I128(0, 0), x * y); x = I128(BANE, BANE); y = I128(2, 2); ASSERT128(I128(1, 0), x * y); x = I128(BANE, BANE); y = I128(2, BANE); ASSERT128(I128(0x4000000000000000, 0), x * y); x = I128(BANE, BANE); y = I128(2, -1ul); ASSERT128(I128(-1ul, BANE), x * y); x = I128(BANE, BANE); y = I128(BANE, 0); ASSERT128(I128(0, 0), x * y); x = I128(BANE, BANE); y = I128(BANE, 2); ASSERT128(I128(1, 0), x * y); x = I128(BANE, BANE); y = I128(BANE, BANE); ASSERT128(I128(0x4000000000000000, 0), x * y); x = I128(BANE, BANE); y = I128(BANE, -1ul); ASSERT128(I128(-1ul, BANE), x * y); x = I128(BANE, BANE); y = I128(-1ul, 0); ASSERT128(I128(BANE, 0), x * y); x = I128(BANE, BANE); y = I128(-1ul, 2); ASSERT128(I128(BANE1, 0), x * y); x = I128(BANE, BANE); y = I128(-1ul, BANE); ASSERT128(I128(0xc000000000000000, 0), x * y); x = I128(BANE, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(IMAX, BANE), x * y); x = I128(BANE, -1ul); y = I128(0, 0); ASSERT128(I128(0, 0), x * y); x = I128(BANE, -1ul); y = I128(0, 2); ASSERT128(I128(1, 0xfffffffffffffffe), x * y); x = I128(BANE, -1ul); y = I128(0, BANE); ASSERT128(I128(IMAX, BANE), x * y); x = I128(BANE, -1ul); y = I128(0, -1ul); ASSERT128(I128(0x7ffffffffffffffe, 1), x * y); x = I128(BANE, -1ul); y = I128(2, 0); ASSERT128(I128(0xfffffffffffffffe, 0), x * y); x = I128(BANE, -1ul); y = I128(2, 2); ASSERT128(I128(-1ul, 0xfffffffffffffffe), x * y); x = I128(BANE, -1ul); y = I128(2, BANE); ASSERT128(I128(0x7ffffffffffffffd, BANE), x * y); x = I128(BANE, -1ul); y = I128(2, -1ul); ASSERT128(I128(0x7ffffffffffffffc, 1), x * y); x = I128(BANE, -1ul); y = I128(BANE, 0); ASSERT128(I128(BANE, 0), x * y); x = I128(BANE, -1ul); y = I128(BANE, 2); ASSERT128(I128(BANE1, 0xfffffffffffffffe), x * y); x = I128(BANE, -1ul); y = I128(BANE, BANE); ASSERT128(I128(-1ul, BANE), x * y); x = I128(BANE, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(0xfffffffffffffffe, 1), x * y); x = I128(BANE, -1ul); y = I128(-1ul, 0); ASSERT128(I128(1, 0), x * y); x = I128(BANE, -1ul); y = I128(-1ul, 2); ASSERT128(I128(2, 0xfffffffffffffffe), x * y); x = I128(BANE, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(BANE, BANE), x * y); x = I128(BANE, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(IMAX, 1), x * y); x = I128(-1ul, 0); y = I128(0, 0); ASSERT128(I128(0, 0), x * y); x = I128(-1ul, 0); y = I128(0, 2); ASSERT128(I128(0xfffffffffffffffe, 0), x * y); x = I128(-1ul, 0); y = I128(0, BANE); ASSERT128(I128(BANE, 0), x * y); x = I128(-1ul, 0); y = I128(0, -1ul); ASSERT128(I128(1, 0), x * y); x = I128(-1ul, 0); y = I128(2, 0); ASSERT128(I128(0, 0), x * y); x = I128(-1ul, 0); y = I128(2, 2); ASSERT128(I128(0xfffffffffffffffe, 0), x * y); x = I128(-1ul, 0); y = I128(2, BANE); ASSERT128(I128(BANE, 0), x * y); x = I128(-1ul, 0); y = I128(2, -1ul); ASSERT128(I128(1, 0), x * y); x = I128(-1ul, 0); y = I128(BANE, 0); ASSERT128(I128(0, 0), x * y); x = I128(-1ul, 0); y = I128(BANE, 2); ASSERT128(I128(0xfffffffffffffffe, 0), x * y); x = I128(-1ul, 0); y = I128(BANE, BANE); ASSERT128(I128(BANE, 0), x * y); x = I128(-1ul, 0); y = I128(BANE, -1ul); ASSERT128(I128(1, 0), x * y); x = I128(-1ul, 0); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x * y); x = I128(-1ul, 0); y = I128(-1ul, 2); ASSERT128(I128(0xfffffffffffffffe, 0), x * y); x = I128(-1ul, 0); y = I128(-1ul, BANE); ASSERT128(I128(BANE, 0), x * y); x = I128(-1ul, 0); y = I128(-1ul, -1ul); ASSERT128(I128(1, 0), x * y); x = I128(-1ul, 2); y = I128(0, 0); ASSERT128(I128(0, 0), x * y); x = I128(-1ul, 2); y = I128(0, 2); ASSERT128(I128(0xfffffffffffffffe, 4), x * y); x = I128(-1ul, 2); y = I128(0, BANE); ASSERT128(I128(BANE1, 0), x * y); x = I128(-1ul, 2); y = I128(0, -1ul); ASSERT128(I128(2, 0xfffffffffffffffe), x * y); x = I128(-1ul, 2); y = I128(2, 0); ASSERT128(I128(4, 0), x * y); x = I128(-1ul, 2); y = I128(2, 2); ASSERT128(I128(2, 4), x * y); x = I128(-1ul, 2); y = I128(2, BANE); ASSERT128(I128(0x8000000000000005, 0), x * y); x = I128(-1ul, 2); y = I128(2, -1ul); ASSERT128(I128(0x0000000000000006, 0xfffffffffffffffe), x * y); x = I128(-1ul, 2); y = I128(BANE, 0); ASSERT128(I128(0, 0), x * y); x = I128(-1ul, 2); y = I128(BANE, 2); ASSERT128(I128(0xfffffffffffffffe, 4), x * y); x = I128(-1ul, 2); y = I128(BANE, BANE); ASSERT128(I128(BANE1, 0), x * y); x = I128(-1ul, 2); y = I128(BANE, -1ul); ASSERT128(I128(2, 0xfffffffffffffffe), x * y); x = I128(-1ul, 2); y = I128(-1ul, 0); ASSERT128(I128(0xfffffffffffffffe, 0), x * y); x = I128(-1ul, 2); y = I128(-1ul, 2); ASSERT128(I128(0xfffffffffffffffc, 4), x * y); x = I128(-1ul, 2); y = I128(-1ul, BANE); ASSERT128(I128(IMAX, 0), x * y); x = I128(-1ul, 2); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0xfffffffffffffffe), x * y); x = I128(-1ul, BANE); y = I128(0, 0); ASSERT128(I128(0, 0), x * y); x = I128(-1ul, BANE); y = I128(0, 2); ASSERT128(I128(-1ul, 0), x * y); x = I128(-1ul, BANE); y = I128(0, BANE); ASSERT128(I128(0xc000000000000000, 0), x * y); x = I128(-1ul, BANE); y = I128(0, -1ul); ASSERT128(I128(BANE, BANE), x * y); x = I128(-1ul, BANE); y = I128(2, 0); ASSERT128(I128(0, 0), x * y); x = I128(-1ul, BANE); y = I128(2, 2); ASSERT128(I128(-1ul, 0), x * y); x = I128(-1ul, BANE); y = I128(2, BANE); ASSERT128(I128(0xc000000000000000, 0), x * y); x = I128(-1ul, BANE); y = I128(2, -1ul); ASSERT128(I128(BANE, BANE), x * y); x = I128(-1ul, BANE); y = I128(BANE, 0); ASSERT128(I128(0, 0), x * y); x = I128(-1ul, BANE); y = I128(BANE, 2); ASSERT128(I128(-1ul, 0), x * y); x = I128(-1ul, BANE); y = I128(BANE, BANE); ASSERT128(I128(0xc000000000000000, 0), x * y); x = I128(-1ul, BANE); y = I128(BANE, -1ul); ASSERT128(I128(BANE, BANE), x * y); x = I128(-1ul, BANE); y = I128(-1ul, 0); ASSERT128(I128(BANE, 0), x * y); x = I128(-1ul, BANE); y = I128(-1ul, 2); ASSERT128(I128(IMAX, 0), x * y); x = I128(-1ul, BANE); y = I128(-1ul, BANE); ASSERT128(I128(0x4000000000000000, 0), x * y); x = I128(-1ul, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(0, BANE), x * y); x = I128(-1ul, -1ul); y = I128(0, 0); ASSERT128(I128(0, 0), x * y); x = I128(-1ul, -1ul); y = I128(0, 2); ASSERT128(I128(-1ul, 0xfffffffffffffffe), x * y); x = I128(-1ul, -1ul); y = I128(0, BANE); ASSERT128(I128(-1ul, BANE), x * y); x = I128(-1ul, -1ul); y = I128(0, -1ul); ASSERT128(I128(-1ul, 1), x * y); x = I128(-1ul, -1ul); y = I128(2, 0); ASSERT128(I128(0xfffffffffffffffe, 0), x * y); x = I128(-1ul, -1ul); y = I128(2, 2); ASSERT128(I128(IMAX2, 0xfffffffffffffffe), x * y); x = I128(-1ul, -1ul); y = I128(2, BANE); ASSERT128(I128(IMAX2, BANE), x * y); x = I128(-1ul, -1ul); y = I128(2, -1ul); ASSERT128(I128(IMAX2, 1), x * y); x = I128(-1ul, -1ul); y = I128(BANE, 0); ASSERT128(I128(BANE, 0), x * y); x = I128(-1ul, -1ul); y = I128(BANE, 2); ASSERT128(I128(IMAX, 0xfffffffffffffffe), x * y); x = I128(-1ul, -1ul); y = I128(BANE, BANE); ASSERT128(I128(IMAX, BANE), x * y); x = I128(-1ul, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(IMAX, 1), x * y); x = I128(-1ul, -1ul); y = I128(-1ul, 0); ASSERT128(I128(1, 0), x * y); x = I128(-1ul, -1ul); y = I128(-1ul, 2); ASSERT128(I128(0, 0xfffffffffffffffe), x * y); x = I128(-1ul, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(0, BANE), x * y); x = I128(-1ul, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(0, 1), x * y); } void testDiv128(void) { __int128 x, y; x = I128(0, 0); y = I128(0, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(0, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(0, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(2, 0); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(2, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(2, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(2, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(BANE, 0); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(BANE, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(0, 2); ASSERT128(I128(0, 1), x / y); x = I128(0, 2); y = I128(0, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(0, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(2, 0); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(2, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(2, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(2, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(BANE, 0); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(BANE, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, 0xfffffffffffffffe), x / y); x = I128(0, BANE); y = I128(0, 2); ASSERT128(I128(0, 0x4000000000000000), x / y); x = I128(0, BANE); y = I128(0, BANE); ASSERT128(I128(0, 1), x / y); x = I128(0, BANE); y = I128(0, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, BANE); y = I128(2, 0); ASSERT128(I128(0, 0), x / y); x = I128(0, BANE); y = I128(2, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, BANE); y = I128(2, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, BANE); y = I128(2, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, BANE); y = I128(BANE, 0); ASSERT128(I128(0, 0), x / y); x = I128(0, BANE); y = I128(BANE, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, BANE); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, BANE); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, BANE); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x / y); x = I128(0, BANE); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, BANE); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, -1ul), x / y); x = I128(0, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, BANE), x / y); x = I128(0, -1ul); y = I128(0, 2); ASSERT128(I128(0, IMAX), x / y); x = I128(0, -1ul); y = I128(0, BANE); ASSERT128(I128(0, 1), x / y); x = I128(0, -1ul); y = I128(0, -1ul); ASSERT128(I128(0, 1), x / y); x = I128(0, -1ul); y = I128(2, 0); ASSERT128(I128(0, 0), x / y); x = I128(0, -1ul); y = I128(2, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, -1ul); y = I128(2, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, -1ul); y = I128(2, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, -1ul); y = I128(BANE, 0); ASSERT128(I128(0, 0), x / y); x = I128(0, -1ul); y = I128(BANE, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, -1ul); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, -1ul); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x / y); x = I128(0, -1ul); y = I128(-1ul, 2); ASSERT128(I128(-1ul, -1ul), x / y); x = I128(0, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, -1ul), x / y); x = I128(0, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, 1), x / y); x = I128(2, 0); y = I128(0, 2); ASSERT128(I128(1, 0), x / y); x = I128(2, 0); y = I128(0, BANE); ASSERT128(I128(0, 4), x / y); x = I128(2, 0); y = I128(0, -1ul); ASSERT128(I128(0, 2), x / y); x = I128(2, 0); y = I128(2, 0); ASSERT128(I128(0, 1), x / y); x = I128(2, 0); y = I128(2, 2); ASSERT128(I128(0, 0), x / y); x = I128(2, 0); y = I128(2, BANE); ASSERT128(I128(0, 0), x / y); x = I128(2, 0); y = I128(2, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(2, 0); y = I128(BANE, 0); ASSERT128(I128(0, 0), x / y); x = I128(2, 0); y = I128(BANE, 2); ASSERT128(I128(0, 0), x / y); x = I128(2, 0); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x / y); x = I128(2, 0); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(2, 0); y = I128(-1ul, 0); ASSERT128(I128(-1ul, 0xfffffffffffffffe), x / y); x = I128(2, 0); y = I128(-1ul, 2); ASSERT128(I128(-1ul, 0xfffffffffffffffe), x / y); x = I128(2, 0); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, 0xfffffffffffffffc), x / y); x = I128(2, 0); y = I128(-1ul, -1ul); ASSERT128(I128(0xfffffffffffffffe, 0), x / y); x = I128(2, 2); y = I128(0, 2); ASSERT128(I128(1, 1), x / y); x = I128(2, 2); y = I128(0, BANE); ASSERT128(I128(0, 4), x / y); x = I128(2, 2); y = I128(0, -1ul); ASSERT128(I128(0, 2), x / y); x = I128(2, 2); y = I128(2, 0); ASSERT128(I128(0, 1), x / y); x = I128(2, 2); y = I128(2, 2); ASSERT128(I128(0, 1), x / y); x = I128(2, 2); y = I128(2, BANE); ASSERT128(I128(0, 0), x / y); x = I128(2, 2); y = I128(2, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(2, 2); y = I128(BANE, 0); ASSERT128(I128(0, 0), x / y); x = I128(2, 2); y = I128(BANE, 2); ASSERT128(I128(0, 0), x / y); x = I128(2, 2); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x / y); x = I128(2, 2); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(2, 2); y = I128(-1ul, 0); ASSERT128(I128(-1ul, 0xfffffffffffffffe), x / y); x = I128(2, 2); y = I128(-1ul, 2); ASSERT128(I128(-1ul, 0xfffffffffffffffe), x / y); x = I128(2, 2); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, 0xfffffffffffffffc), x / y); x = I128(2, 2); y = I128(-1ul, -1ul); ASSERT128(I128(IMAX2, 0xfffffffffffffffe), x / y); x = I128(2, BANE); y = I128(0, 2); ASSERT128(I128(1, 0x4000000000000000), x / y); x = I128(2, BANE); y = I128(0, BANE); ASSERT128(I128(0, 5), x / y); x = I128(2, BANE); y = I128(0, -1ul); ASSERT128(I128(0, 2), x / y); x = I128(2, BANE); y = I128(2, 0); ASSERT128(I128(0, 1), x / y); x = I128(2, BANE); y = I128(2, 2); ASSERT128(I128(0, 1), x / y); x = I128(2, BANE); y = I128(2, BANE); ASSERT128(I128(0, 1), x / y); x = I128(2, BANE); y = I128(2, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(2, BANE); y = I128(BANE, 0); ASSERT128(I128(0, 0), x / y); x = I128(2, BANE); y = I128(BANE, 2); ASSERT128(I128(0, 0), x / y); x = I128(2, BANE); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x / y); x = I128(2, BANE); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(2, BANE); y = I128(-1ul, 0); ASSERT128(I128(-1ul, 0xfffffffffffffffe), x / y); x = I128(2, BANE); y = I128(-1ul, 2); ASSERT128(I128(-1ul, 0xfffffffffffffffe), x / y); x = I128(2, BANE); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, 0xfffffffffffffffb), x / y); x = I128(2, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(IMAX2, BANE), x / y); x = I128(2, -1ul); y = I128(0, 2); ASSERT128(I128(1, IMAX), x / y); x = I128(2, -1ul); y = I128(0, BANE); ASSERT128(I128(0, 5), x / y); x = I128(2, -1ul); y = I128(0, -1ul); ASSERT128(I128(0, 3), x / y); x = I128(2, -1ul); y = I128(2, 0); ASSERT128(I128(0, 1), x / y); x = I128(2, -1ul); y = I128(2, 2); ASSERT128(I128(0, 1), x / y); x = I128(2, -1ul); y = I128(2, BANE); ASSERT128(I128(0, 1), x / y); x = I128(2, -1ul); y = I128(2, -1ul); ASSERT128(I128(0, 1), x / y); x = I128(2, -1ul); y = I128(BANE, 0); ASSERT128(I128(0, 0), x / y); x = I128(2, -1ul); y = I128(BANE, 2); ASSERT128(I128(0, 0), x / y); x = I128(2, -1ul); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x / y); x = I128(2, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(2, -1ul); y = I128(-1ul, 0); ASSERT128(I128(-1ul, 0xfffffffffffffffe), x / y); x = I128(2, -1ul); y = I128(-1ul, 2); ASSERT128(I128(-1ul, IMAX2), x / y); x = I128(2, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, 0xfffffffffffffffb), x / y); x = I128(2, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(IMAX2, 1), x / y); x = I128(BANE, 0); y = I128(0, 2); ASSERT128(I128(0xc000000000000000, 0), x / y); x = I128(BANE, 0); y = I128(0, BANE); ASSERT128(I128(-1ul, 0), x / y); x = I128(BANE, 0); y = I128(0, -1ul); ASSERT128(I128(-1ul, BANE), x / y); x = I128(BANE, 0); y = I128(2, 0); ASSERT128(I128(-1ul, 0xc000000000000000), x / y); x = I128(BANE, 0); y = I128(2, 2); ASSERT128(I128(-1ul, 0xc000000000000001), x / y); x = I128(BANE, 0); y = I128(2, BANE); ASSERT128(I128(-1ul, 0xcccccccccccccccd), x / y); x = I128(BANE, 0); y = I128(2, -1ul); ASSERT128(I128(-1ul, 0xd555555555555556), x / y); x = I128(BANE, 0); y = I128(BANE, 0); ASSERT128(I128(0, 1), x / y); x = I128(BANE, 0); y = I128(BANE, 2); ASSERT128(I128(0, 1), x / y); x = I128(BANE, 0); y = I128(BANE, BANE); ASSERT128(I128(0, 1), x / y); x = I128(BANE, 0); y = I128(BANE, -1ul); ASSERT128(I128(0, 1), x / y); x = I128(BANE, 0); y = I128(-1ul, 0); ASSERT128(I128(0, BANE), x / y); x = I128(BANE, 0); y = I128(-1ul, 2); ASSERT128(I128(0, BANE1), x / y); x = I128(BANE, 0); y = I128(-1ul, BANE); ASSERT128(I128(1, 0), x / y); x = I128(BANE, 2); y = I128(0, 2); ASSERT128(I128(0xc000000000000000, 1), x / y); x = I128(BANE, 2); y = I128(0, BANE); ASSERT128(I128(-1ul, 1), x / y); x = I128(BANE, 2); y = I128(0, -1ul); ASSERT128(I128(-1ul, BANE), x / y); x = I128(BANE, 2); y = I128(2, 0); ASSERT128(I128(-1ul, 0xc000000000000001), x / y); x = I128(BANE, 2); y = I128(2, 2); ASSERT128(I128(-1ul, 0xc000000000000001), x / y); x = I128(BANE, 2); y = I128(2, BANE); ASSERT128(I128(-1ul, 0xcccccccccccccccd), x / y); x = I128(BANE, 2); y = I128(2, -1ul); ASSERT128(I128(-1ul, 0xd555555555555556), x / y); x = I128(BANE, 2); y = I128(BANE, 0); ASSERT128(I128(0, 0), x / y); x = I128(BANE, 2); y = I128(BANE, 2); ASSERT128(I128(0, 1), x / y); x = I128(BANE, 2); y = I128(BANE, BANE); ASSERT128(I128(0, 1), x / y); x = I128(BANE, 2); y = I128(BANE, -1ul); ASSERT128(I128(0, 1), x / y); x = I128(BANE, 2); y = I128(-1ul, 0); ASSERT128(I128(0, IMAX), x / y); x = I128(BANE, 2); y = I128(-1ul, 2); ASSERT128(I128(0, BANE1), x / y); x = I128(BANE, 2); y = I128(-1ul, BANE); ASSERT128(I128(0, -1ul), x / y); x = I128(BANE, 2); y = I128(-1ul, -1ul); ASSERT128(I128(IMAX, 0xfffffffffffffffe), x / y); x = I128(BANE, BANE); y = I128(0, 2); ASSERT128(I128(0xc000000000000000, 0x4000000000000000), x / y); x = I128(BANE, BANE); y = I128(0, BANE); ASSERT128(I128(-1ul, 1), x / y); x = I128(BANE, BANE); y = I128(0, -1ul); ASSERT128(I128(-1ul, BANE), x / y); x = I128(BANE, BANE); y = I128(2, 0); ASSERT128(I128(-1ul, 0xc000000000000001), x / y); x = I128(BANE, BANE); y = I128(2, 2); ASSERT128(I128(-1ul, 0xc000000000000001), x / y); x = I128(BANE, BANE); y = I128(2, BANE); ASSERT128(I128(-1ul, 0xcccccccccccccccd), x / y); x = I128(BANE, BANE); y = I128(2, -1ul); ASSERT128(I128(-1ul, 0xd555555555555556), x / y); x = I128(BANE, BANE); y = I128(BANE, 0); ASSERT128(I128(0, 0), x / y); x = I128(BANE, BANE); y = I128(BANE, 2); ASSERT128(I128(0, 0), x / y); x = I128(BANE, BANE); y = I128(BANE, BANE); ASSERT128(I128(0, 1), x / y); x = I128(BANE, BANE); y = I128(BANE, -1ul); ASSERT128(I128(0, 1), x / y); x = I128(BANE, BANE); y = I128(-1ul, 0); ASSERT128(I128(0, IMAX), x / y); x = I128(BANE, BANE); y = I128(-1ul, 2); ASSERT128(I128(0, BANE), x / y); x = I128(BANE, BANE); y = I128(-1ul, BANE); ASSERT128(I128(0, -1ul), x / y); x = I128(BANE, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(IMAX, BANE), x / y); x = I128(BANE, -1ul); y = I128(0, 2); ASSERT128(I128(0xc000000000000000, BANE), x / y); x = I128(BANE, -1ul); y = I128(0, BANE); ASSERT128(I128(-1ul, 2), x / y); x = I128(BANE, -1ul); y = I128(0, -1ul); ASSERT128(I128(-1ul, BANE1), x / y); x = I128(BANE, -1ul); y = I128(2, 0); ASSERT128(I128(-1ul, 0xc000000000000001), x / y); x = I128(BANE, -1ul); y = I128(2, 2); ASSERT128(I128(-1ul, 0xc000000000000001), x / y); x = I128(BANE, -1ul); y = I128(2, BANE); ASSERT128(I128(-1ul, 0xccccccccccccccce), x / y); x = I128(BANE, -1ul); y = I128(2, -1ul); ASSERT128(I128(-1ul, 0xd555555555555556), x / y); x = I128(BANE, -1ul); y = I128(BANE, 0); ASSERT128(I128(0, 0), x / y); x = I128(BANE, -1ul); y = I128(BANE, 2); ASSERT128(I128(0, 0), x / y); x = I128(BANE, -1ul); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x / y); x = I128(BANE, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(0, 1), x / y); x = I128(BANE, -1ul); y = I128(-1ul, 0); ASSERT128(I128(0, IMAX), x / y); x = I128(BANE, -1ul); y = I128(-1ul, 2); ASSERT128(I128(0, BANE), x / y); x = I128(BANE, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(0, 0xfffffffffffffffe), x / y); x = I128(BANE, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(IMAX, 1), x / y); x = I128(-1ul, 0); y = I128(0, 2); ASSERT128(I128(-1ul, BANE), x / y); x = I128(-1ul, 0); y = I128(0, BANE); ASSERT128(I128(-1ul, 0xfffffffffffffffe), x / y); x = I128(-1ul, 0); y = I128(0, -1ul); ASSERT128(I128(-1ul, -1ul), x / y); x = I128(-1ul, 0); y = I128(2, 0); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, 0); y = I128(2, 2); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, 0); y = I128(2, BANE); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, 0); y = I128(2, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, 0); y = I128(BANE, 0); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, 0); y = I128(BANE, 2); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, 0); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, 0); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, 0); y = I128(-1ul, 0); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, 0); y = I128(-1ul, 2); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, 0); y = I128(-1ul, BANE); ASSERT128(I128(0, 2), x / y); x = I128(-1ul, 0); y = I128(-1ul, -1ul); ASSERT128(I128(1, 0), x / y); x = I128(-1ul, 2); y = I128(0, 2); ASSERT128(I128(-1ul, BANE1), x / y); x = I128(-1ul, 2); y = I128(0, BANE); ASSERT128(I128(-1ul, -1ul), x / y); x = I128(-1ul, 2); y = I128(0, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, 2); y = I128(2, 0); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, 2); y = I128(2, 2); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, 2); y = I128(2, BANE); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, 2); y = I128(2, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, 2); y = I128(BANE, 0); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, 2); y = I128(BANE, 2); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, 2); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, 2); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, 2); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, 2); y = I128(-1ul, 2); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, 2); y = I128(-1ul, BANE); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, 2); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0xfffffffffffffffe), x / y); x = I128(-1ul, BANE); y = I128(0, 2); ASSERT128(I128(-1ul, 0xc000000000000000), x / y); x = I128(-1ul, BANE); y = I128(0, BANE); ASSERT128(I128(-1ul, -1ul), x / y); x = I128(-1ul, BANE); y = I128(0, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, BANE); y = I128(2, 0); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, BANE); y = I128(2, 2); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, BANE); y = I128(2, BANE); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, BANE); y = I128(2, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, BANE); y = I128(BANE, 0); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, BANE); y = I128(BANE, 2); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, BANE); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, BANE); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, BANE); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, BANE); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, BANE); y = I128(-1ul, BANE); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(0, BANE), x / y); x = I128(-1ul, -1ul); y = I128(0, 2); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, -1ul); y = I128(0, BANE); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, -1ul); y = I128(0, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, -1ul); y = I128(2, 0); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, -1ul); y = I128(2, 2); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, -1ul); y = I128(2, BANE); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, -1ul); y = I128(2, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, -1ul); y = I128(BANE, 0); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, -1ul); y = I128(BANE, 2); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, -1ul); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, -1ul); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, -1ul); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(0, 1), x / y); } void testDivu128(void) { unsigned __int128 x, y; x = I128(0, 0); y = I128(0, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(0, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(0, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(2, 0); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(2, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(2, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(2, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(BANE, 0); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(BANE, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, 0); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(0, 2); ASSERT128(I128(0, 1), x / y); x = I128(0, 2); y = I128(0, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(0, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(2, 0); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(2, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(2, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(2, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(BANE, 0); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(BANE, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, 2); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, BANE); y = I128(0, 2); ASSERT128(I128(0, 0x4000000000000000), x / y); x = I128(0, BANE); y = I128(0, BANE); ASSERT128(I128(0, 1), x / y); x = I128(0, BANE); y = I128(0, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, BANE); y = I128(2, 0); ASSERT128(I128(0, 0), x / y); x = I128(0, BANE); y = I128(2, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, BANE); y = I128(2, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, BANE); y = I128(2, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, BANE); y = I128(BANE, 0); ASSERT128(I128(0, 0), x / y); x = I128(0, BANE); y = I128(BANE, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, BANE); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, BANE); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, BANE); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x / y); x = I128(0, BANE); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, BANE); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, -1ul); y = I128(0, 2); ASSERT128(I128(0, IMAX), x / y); x = I128(0, -1ul); y = I128(0, BANE); ASSERT128(I128(0, 1), x / y); x = I128(0, -1ul); y = I128(0, -1ul); ASSERT128(I128(0, 1), x / y); x = I128(0, -1ul); y = I128(2, 0); ASSERT128(I128(0, 0), x / y); x = I128(0, -1ul); y = I128(2, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, -1ul); y = I128(2, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, -1ul); y = I128(2, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, -1ul); y = I128(BANE, 0); ASSERT128(I128(0, 0), x / y); x = I128(0, -1ul); y = I128(BANE, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, -1ul); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(0, -1ul); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x / y); x = I128(0, -1ul); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x / y); x = I128(0, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x / y); x = I128(0, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(2, 0); y = I128(0, 2); ASSERT128(I128(1, 0), x / y); x = I128(2, 0); y = I128(0, BANE); ASSERT128(I128(0, 4), x / y); x = I128(2, 0); y = I128(0, -1ul); ASSERT128(I128(0, 2), x / y); x = I128(2, 0); y = I128(2, 0); ASSERT128(I128(0, 1), x / y); x = I128(2, 0); y = I128(2, 2); ASSERT128(I128(0, 0), x / y); x = I128(2, 0); y = I128(2, BANE); ASSERT128(I128(0, 0), x / y); x = I128(2, 0); y = I128(2, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(2, 0); y = I128(BANE, 0); ASSERT128(I128(0, 0), x / y); x = I128(2, 0); y = I128(BANE, 2); ASSERT128(I128(0, 0), x / y); x = I128(2, 0); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x / y); x = I128(2, 0); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(2, 0); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x / y); x = I128(2, 0); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x / y); x = I128(2, 0); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x / y); x = I128(2, 0); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(2, 2); y = I128(0, 2); ASSERT128(I128(1, 1), x / y); x = I128(2, 2); y = I128(0, BANE); ASSERT128(I128(0, 4), x / y); x = I128(2, 2); y = I128(0, -1ul); ASSERT128(I128(0, 2), x / y); x = I128(2, 2); y = I128(2, 0); ASSERT128(I128(0, 1), x / y); x = I128(2, 2); y = I128(2, 2); ASSERT128(I128(0, 1), x / y); x = I128(2, 2); y = I128(2, BANE); ASSERT128(I128(0, 0), x / y); x = I128(2, 2); y = I128(2, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(2, 2); y = I128(BANE, 0); ASSERT128(I128(0, 0), x / y); x = I128(2, 2); y = I128(BANE, 2); ASSERT128(I128(0, 0), x / y); x = I128(2, 2); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x / y); x = I128(2, 2); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(2, 2); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x / y); x = I128(2, 2); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x / y); x = I128(2, 2); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x / y); x = I128(2, 2); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(2, BANE); y = I128(0, 2); ASSERT128(I128(1, 0x4000000000000000), x / y); x = I128(2, BANE); y = I128(0, BANE); ASSERT128(I128(0, 5), x / y); x = I128(2, BANE); y = I128(0, -1ul); ASSERT128(I128(0, 2), x / y); x = I128(2, BANE); y = I128(2, 0); ASSERT128(I128(0, 1), x / y); x = I128(2, BANE); y = I128(2, 2); ASSERT128(I128(0, 1), x / y); x = I128(2, BANE); y = I128(2, BANE); ASSERT128(I128(0, 1), x / y); x = I128(2, BANE); y = I128(2, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(2, BANE); y = I128(BANE, 0); ASSERT128(I128(0, 0), x / y); x = I128(2, BANE); y = I128(BANE, 2); ASSERT128(I128(0, 0), x / y); x = I128(2, BANE); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x / y); x = I128(2, BANE); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(2, BANE); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x / y); x = I128(2, BANE); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x / y); x = I128(2, BANE); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x / y); x = I128(2, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(2, -1ul); y = I128(0, 2); ASSERT128(I128(1, IMAX), x / y); x = I128(2, -1ul); y = I128(0, BANE); ASSERT128(I128(0, 5), x / y); x = I128(2, -1ul); y = I128(0, -1ul); ASSERT128(I128(0, 3), x / y); x = I128(2, -1ul); y = I128(2, 0); ASSERT128(I128(0, 1), x / y); x = I128(2, -1ul); y = I128(2, 2); ASSERT128(I128(0, 1), x / y); x = I128(2, -1ul); y = I128(2, BANE); ASSERT128(I128(0, 1), x / y); x = I128(2, -1ul); y = I128(2, -1ul); ASSERT128(I128(0, 1), x / y); x = I128(2, -1ul); y = I128(BANE, 0); ASSERT128(I128(0, 0), x / y); x = I128(2, -1ul); y = I128(BANE, 2); ASSERT128(I128(0, 0), x / y); x = I128(2, -1ul); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x / y); x = I128(2, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(2, -1ul); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x / y); x = I128(2, -1ul); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x / y); x = I128(2, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x / y); x = I128(2, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(BANE, 0); y = I128(0, 2); ASSERT128(I128(0x4000000000000000, 0), x / y); x = I128(BANE, 0); y = I128(0, BANE); ASSERT128(I128(1, 0), x / y); x = I128(BANE, 0); y = I128(0, -1ul); ASSERT128(I128(0, BANE), x / y); x = I128(BANE, 0); y = I128(2, 0); ASSERT128(I128(0, 0x4000000000000000), x / y); x = I128(BANE, 0); y = I128(2, 2); ASSERT128(I128(0, 0x3fffffffffffffff), x / y); x = I128(BANE, 0); y = I128(2, BANE); ASSERT128(I128(0, 0x3333333333333333), x / y); x = I128(BANE, 0); y = I128(2, -1ul); ASSERT128(I128(0, 0x2aaaaaaaaaaaaaaa), x / y); x = I128(BANE, 0); y = I128(BANE, 0); ASSERT128(I128(0, 1), x / y); x = I128(BANE, 0); y = I128(BANE, 2); ASSERT128(I128(0, 0), x / y); x = I128(BANE, 0); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x / y); x = I128(BANE, 0); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(BANE, 0); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x / y); x = I128(BANE, 0); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x / y); x = I128(BANE, 0); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x / y); x = I128(BANE, 2); y = I128(0, 2); ASSERT128(I128(0x4000000000000000, 1), x / y); x = I128(BANE, 2); y = I128(0, BANE); ASSERT128(I128(1, 0), x / y); x = I128(BANE, 2); y = I128(0, -1ul); ASSERT128(I128(0, BANE), x / y); x = I128(BANE, 2); y = I128(2, 0); ASSERT128(I128(0, 0x4000000000000000), x / y); x = I128(BANE, 2); y = I128(2, 2); ASSERT128(I128(0, 0x3fffffffffffffff), x / y); x = I128(BANE, 2); y = I128(2, BANE); ASSERT128(I128(0, 0x3333333333333333), x / y); x = I128(BANE, 2); y = I128(2, -1ul); ASSERT128(I128(0, 0x2aaaaaaaaaaaaaaa), x / y); x = I128(BANE, 2); y = I128(BANE, 0); ASSERT128(I128(0, 1), x / y); x = I128(BANE, 2); y = I128(BANE, 2); ASSERT128(I128(0, 1), x / y); x = I128(BANE, 2); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x / y); x = I128(BANE, 2); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(BANE, 2); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x / y); x = I128(BANE, 2); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x / y); x = I128(BANE, 2); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x / y); x = I128(BANE, 2); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(BANE, BANE); y = I128(0, 2); ASSERT128(I128(0x4000000000000000, 0x4000000000000000), x / y); x = I128(BANE, BANE); y = I128(0, BANE); ASSERT128(I128(1, 1), x / y); x = I128(BANE, BANE); y = I128(0, -1ul); ASSERT128(I128(0, BANE1), x / y); x = I128(BANE, BANE); y = I128(2, 0); ASSERT128(I128(0, 0x4000000000000000), x / y); x = I128(BANE, BANE); y = I128(2, 2); ASSERT128(I128(0, 0x4000000000000000), x / y); x = I128(BANE, BANE); y = I128(2, BANE); ASSERT128(I128(0, 0x3333333333333333), x / y); x = I128(BANE, BANE); y = I128(2, -1ul); ASSERT128(I128(0, 0x2aaaaaaaaaaaaaaa), x / y); x = I128(BANE, BANE); y = I128(BANE, 0); ASSERT128(I128(0, 1), x / y); x = I128(BANE, BANE); y = I128(BANE, 2); ASSERT128(I128(0, 1), x / y); x = I128(BANE, BANE); y = I128(BANE, BANE); ASSERT128(I128(0, 1), x / y); x = I128(BANE, BANE); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(BANE, BANE); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x / y); x = I128(BANE, BANE); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x / y); x = I128(BANE, BANE); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x / y); x = I128(BANE, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(BANE, -1ul); y = I128(0, 2); ASSERT128(I128(0x4000000000000000, IMAX), x / y); x = I128(BANE, -1ul); y = I128(0, BANE); ASSERT128(I128(1, 1), x / y); x = I128(BANE, -1ul); y = I128(0, -1ul); ASSERT128(I128(0, BANE1), x / y); x = I128(BANE, -1ul); y = I128(2, 0); ASSERT128(I128(0, 0x4000000000000000), x / y); x = I128(BANE, -1ul); y = I128(2, 2); ASSERT128(I128(0, 0x4000000000000000), x / y); x = I128(BANE, -1ul); y = I128(2, BANE); ASSERT128(I128(0, 0x3333333333333333), x / y); x = I128(BANE, -1ul); y = I128(2, -1ul); ASSERT128(I128(0, 0x2aaaaaaaaaaaaaab), x / y); x = I128(BANE, -1ul); y = I128(BANE, 0); ASSERT128(I128(0, 1), x / y); x = I128(BANE, -1ul); y = I128(BANE, 2); ASSERT128(I128(0, 1), x / y); x = I128(BANE, -1ul); y = I128(BANE, BANE); ASSERT128(I128(0, 1), x / y); x = I128(BANE, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(0, 1), x / y); x = I128(BANE, -1ul); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x / y); x = I128(BANE, -1ul); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x / y); x = I128(BANE, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x / y); x = I128(BANE, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, 0); y = I128(0, 2); ASSERT128(I128(IMAX, BANE), x / y); x = I128(-1ul, 0); y = I128(0, BANE); ASSERT128(I128(1, 0xfffffffffffffffe), x / y); x = I128(-1ul, 0); y = I128(0, -1ul); ASSERT128(I128(1, 0), x / y); x = I128(-1ul, 0); y = I128(2, 0); ASSERT128(I128(0, IMAX), x / y); x = I128(-1ul, 0); y = I128(2, 2); ASSERT128(I128(0, IMAX), x / y); x = I128(-1ul, 0); y = I128(2, BANE); ASSERT128(I128(0, 0x6666666666666666), x / y); x = I128(-1ul, 0); y = I128(2, -1ul); ASSERT128(I128(0, 0x5555555555555555), x / y); x = I128(-1ul, 0); y = I128(BANE, 0); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, 0); y = I128(BANE, 2); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, 0); y = I128(BANE, BANE); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, 0); y = I128(BANE, -1ul); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, 0); y = I128(-1ul, 0); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, 0); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, 0); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, 0); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, 2); y = I128(0, 2); ASSERT128(I128(IMAX, BANE1), x / y); x = I128(-1ul, 2); y = I128(0, BANE); ASSERT128(I128(1, 0xfffffffffffffffe), x / y); x = I128(-1ul, 2); y = I128(0, -1ul); ASSERT128(I128(1, 0), x / y); x = I128(-1ul, 2); y = I128(2, 0); ASSERT128(I128(0, IMAX), x / y); x = I128(-1ul, 2); y = I128(2, 2); ASSERT128(I128(0, IMAX), x / y); x = I128(-1ul, 2); y = I128(2, BANE); ASSERT128(I128(0, 0x6666666666666666), x / y); x = I128(-1ul, 2); y = I128(2, -1ul); ASSERT128(I128(0, 0x5555555555555555), x / y); x = I128(-1ul, 2); y = I128(BANE, 0); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, 2); y = I128(BANE, 2); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, 2); y = I128(BANE, BANE); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, 2); y = I128(BANE, -1ul); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, 2); y = I128(-1ul, 0); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, 2); y = I128(-1ul, 2); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, 2); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, 2); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, BANE); y = I128(0, 2); ASSERT128(I128(IMAX, 0xc000000000000000), x / y); x = I128(-1ul, BANE); y = I128(0, BANE); ASSERT128(I128(1, -1ul), x / y); x = I128(-1ul, BANE); y = I128(0, -1ul); ASSERT128(I128(1, 0), x / y); x = I128(-1ul, BANE); y = I128(2, 0); ASSERT128(I128(0, IMAX), x / y); x = I128(-1ul, BANE); y = I128(2, 2); ASSERT128(I128(0, IMAX), x / y); x = I128(-1ul, BANE); y = I128(2, BANE); ASSERT128(I128(0, 0x6666666666666666), x / y); x = I128(-1ul, BANE); y = I128(2, -1ul); ASSERT128(I128(0, 0x5555555555555555), x / y); x = I128(-1ul, BANE); y = I128(BANE, 0); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, BANE); y = I128(BANE, 2); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, BANE); y = I128(BANE, BANE); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, BANE); y = I128(BANE, -1ul); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, BANE); y = I128(-1ul, 0); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, BANE); y = I128(-1ul, 2); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, BANE); y = I128(-1ul, BANE); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x / y); x = I128(-1ul, -1ul); y = I128(0, 2); ASSERT128(I128(IMAX, -1ul), x / y); x = I128(-1ul, -1ul); y = I128(0, BANE); ASSERT128(I128(1, -1ul), x / y); x = I128(-1ul, -1ul); y = I128(0, -1ul); ASSERT128(I128(1, 1), x / y); x = I128(-1ul, -1ul); y = I128(2, 0); ASSERT128(I128(0, IMAX), x / y); x = I128(-1ul, -1ul); y = I128(2, 2); ASSERT128(I128(0, IMAX), x / y); x = I128(-1ul, -1ul); y = I128(2, BANE); ASSERT128(I128(0, 0x6666666666666666), x / y); x = I128(-1ul, -1ul); y = I128(2, -1ul); ASSERT128(I128(0, 0x5555555555555555), x / y); x = I128(-1ul, -1ul); y = I128(BANE, 0); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, -1ul); y = I128(BANE, 2); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, -1ul); y = I128(BANE, BANE); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, -1ul); y = I128(-1ul, 0); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, -1ul); y = I128(-1ul, 2); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(0, 1), x / y); x = I128(-1ul, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(0, 1), x / y); } void testRem128(void) { __int128 x, y; x = I128(0, 0); y = I128(0, 2); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(0, BANE); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(0, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(2, 0); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(2, 2); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(2, BANE); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(2, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(BANE, 0); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(BANE, 2); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(0, 2); y = I128(0, 2); ASSERT128(I128(0, 0), x % y); x = I128(0, 2); y = I128(0, BANE); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(0, -1ul); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(2, 0); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(2, 2); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(2, BANE); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(2, -1ul); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(BANE, 0); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(BANE, 2); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(BANE, BANE); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(BANE, -1ul); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(-1ul, 0); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(-1ul, 2); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(-1ul, BANE); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(0, BANE); y = I128(0, 2); ASSERT128(I128(0, 0), x % y); x = I128(0, BANE); y = I128(0, BANE); ASSERT128(I128(0, 0), x % y); x = I128(0, BANE); y = I128(0, -1ul); ASSERT128(I128(0, BANE), x % y); x = I128(0, BANE); y = I128(2, 0); ASSERT128(I128(0, BANE), x % y); x = I128(0, BANE); y = I128(2, 2); ASSERT128(I128(0, BANE), x % y); x = I128(0, BANE); y = I128(2, BANE); ASSERT128(I128(0, BANE), x % y); x = I128(0, BANE); y = I128(2, -1ul); ASSERT128(I128(0, BANE), x % y); x = I128(0, BANE); y = I128(BANE, 0); ASSERT128(I128(0, BANE), x % y); x = I128(0, BANE); y = I128(BANE, 2); ASSERT128(I128(0, BANE), x % y); x = I128(0, BANE); y = I128(BANE, BANE); ASSERT128(I128(0, BANE), x % y); x = I128(0, BANE); y = I128(BANE, -1ul); ASSERT128(I128(0, BANE), x % y); x = I128(0, BANE); y = I128(-1ul, 0); ASSERT128(I128(0, BANE), x % y); x = I128(0, BANE); y = I128(-1ul, 2); ASSERT128(I128(0, BANE), x % y); x = I128(0, BANE); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x % y); x = I128(0, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(0, -1ul); y = I128(0, 2); ASSERT128(I128(0, 1), x % y); x = I128(0, -1ul); y = I128(0, BANE); ASSERT128(I128(0, IMAX), x % y); x = I128(0, -1ul); y = I128(0, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(0, -1ul); y = I128(2, 0); ASSERT128(I128(0, -1ul), x % y); x = I128(0, -1ul); y = I128(2, 2); ASSERT128(I128(0, -1ul), x % y); x = I128(0, -1ul); y = I128(2, BANE); ASSERT128(I128(0, -1ul), x % y); x = I128(0, -1ul); y = I128(2, -1ul); ASSERT128(I128(0, -1ul), x % y); x = I128(0, -1ul); y = I128(BANE, 0); ASSERT128(I128(0, -1ul), x % y); x = I128(0, -1ul); y = I128(BANE, 2); ASSERT128(I128(0, -1ul), x % y); x = I128(0, -1ul); y = I128(BANE, BANE); ASSERT128(I128(0, -1ul), x % y); x = I128(0, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(0, -1ul), x % y); x = I128(0, -1ul); y = I128(-1ul, 0); ASSERT128(I128(0, -1ul), x % y); x = I128(0, -1ul); y = I128(-1ul, 2); ASSERT128(I128(0, 1), x % y); x = I128(0, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(0, IMAX), x % y); x = I128(0, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(2, 0); y = I128(0, 2); ASSERT128(I128(0, 0), x % y); x = I128(2, 0); y = I128(0, BANE); ASSERT128(I128(0, 0), x % y); x = I128(2, 0); y = I128(0, -1ul); ASSERT128(I128(0, 2), x % y); x = I128(2, 0); y = I128(2, 0); ASSERT128(I128(0, 0), x % y); x = I128(2, 0); y = I128(2, 2); ASSERT128(I128(2, 0), x % y); x = I128(2, 0); y = I128(2, BANE); ASSERT128(I128(2, 0), x % y); x = I128(2, 0); y = I128(2, -1ul); ASSERT128(I128(2, 0), x % y); x = I128(2, 0); y = I128(BANE, 0); ASSERT128(I128(2, 0), x % y); x = I128(2, 0); y = I128(BANE, 2); ASSERT128(I128(2, 0), x % y); x = I128(2, 0); y = I128(BANE, BANE); ASSERT128(I128(2, 0), x % y); x = I128(2, 0); y = I128(BANE, -1ul); ASSERT128(I128(2, 0), x % y); x = I128(2, 0); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x % y); x = I128(2, 0); y = I128(-1ul, 2); ASSERT128(I128(0, 4), x % y); x = I128(2, 0); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x % y); x = I128(2, 0); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(2, 2); y = I128(0, 2); ASSERT128(I128(0, 0), x % y); x = I128(2, 2); y = I128(0, BANE); ASSERT128(I128(0, 2), x % y); x = I128(2, 2); y = I128(0, -1ul); ASSERT128(I128(0, 4), x % y); x = I128(2, 2); y = I128(2, 0); ASSERT128(I128(0, 2), x % y); x = I128(2, 2); y = I128(2, 2); ASSERT128(I128(0, 0), x % y); x = I128(2, 2); y = I128(2, BANE); ASSERT128(I128(2, 2), x % y); x = I128(2, 2); y = I128(2, -1ul); ASSERT128(I128(2, 2), x % y); x = I128(2, 2); y = I128(BANE, 0); ASSERT128(I128(2, 2), x % y); x = I128(2, 2); y = I128(BANE, 2); ASSERT128(I128(2, 2), x % y); x = I128(2, 2); y = I128(BANE, BANE); ASSERT128(I128(2, 2), x % y); x = I128(2, 2); y = I128(BANE, -1ul); ASSERT128(I128(2, 2), x % y); x = I128(2, 2); y = I128(-1ul, 0); ASSERT128(I128(0, 2), x % y); x = I128(2, 2); y = I128(-1ul, 2); ASSERT128(I128(0, 0x0000000000000006), x % y); x = I128(2, 2); y = I128(-1ul, BANE); ASSERT128(I128(0, 2), x % y); x = I128(2, 2); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(2, BANE); y = I128(0, 2); ASSERT128(I128(0, 0), x % y); x = I128(2, BANE); y = I128(0, BANE); ASSERT128(I128(0, 0), x % y); x = I128(2, BANE); y = I128(0, -1ul); ASSERT128(I128(0, BANE2), x % y); x = I128(2, BANE); y = I128(2, 0); ASSERT128(I128(0, BANE), x % y); x = I128(2, BANE); y = I128(2, 2); ASSERT128(I128(0, 0x7ffffffffffffffe), x % y); x = I128(2, BANE); y = I128(2, BANE); ASSERT128(I128(0, 0), x % y); x = I128(2, BANE); y = I128(2, -1ul); ASSERT128(I128(2, BANE), x % y); x = I128(2, BANE); y = I128(BANE, 0); ASSERT128(I128(2, BANE), x % y); x = I128(2, BANE); y = I128(BANE, 2); ASSERT128(I128(2, BANE), x % y); x = I128(2, BANE); y = I128(BANE, BANE); ASSERT128(I128(2, BANE), x % y); x = I128(2, BANE); y = I128(BANE, -1ul); ASSERT128(I128(2, BANE), x % y); x = I128(2, BANE); y = I128(-1ul, 0); ASSERT128(I128(0, BANE), x % y); x = I128(2, BANE); y = I128(-1ul, 2); ASSERT128(I128(0, 0x8000000000000004), x % y); x = I128(2, BANE); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x % y); x = I128(2, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(2, -1ul); y = I128(0, 2); ASSERT128(I128(0, 1), x % y); x = I128(2, -1ul); y = I128(0, BANE); ASSERT128(I128(0, IMAX), x % y); x = I128(2, -1ul); y = I128(0, -1ul); ASSERT128(I128(0, 2), x % y); x = I128(2, -1ul); y = I128(2, 0); ASSERT128(I128(0, -1ul), x % y); x = I128(2, -1ul); y = I128(2, 2); ASSERT128(I128(0, IMAX2), x % y); x = I128(2, -1ul); y = I128(2, BANE); ASSERT128(I128(0, IMAX), x % y); x = I128(2, -1ul); y = I128(2, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(2, -1ul); y = I128(BANE, 0); ASSERT128(I128(2, -1ul), x % y); x = I128(2, -1ul); y = I128(BANE, 2); ASSERT128(I128(2, -1ul), x % y); x = I128(2, -1ul); y = I128(BANE, BANE); ASSERT128(I128(2, -1ul), x % y); x = I128(2, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(2, -1ul), x % y); x = I128(2, -1ul); y = I128(-1ul, 0); ASSERT128(I128(0, -1ul), x % y); x = I128(2, -1ul); y = I128(-1ul, 2); ASSERT128(I128(0, 5), x % y); x = I128(2, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(0, IMAX), x % y); x = I128(2, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(BANE, 0); y = I128(0, 2); ASSERT128(I128(0, 0), x % y); x = I128(BANE, 0); y = I128(0, BANE); ASSERT128(I128(0, 0), x % y); x = I128(BANE, 0); y = I128(0, -1ul); ASSERT128(I128(-1ul, BANE), x % y); x = I128(BANE, 0); y = I128(2, 0); ASSERT128(I128(0, 0), x % y); x = I128(BANE, 0); y = I128(2, 2); ASSERT128(I128(0xfffffffffffffffe, 0x7ffffffffffffffe), x % y); x = I128(BANE, 0); y = I128(2, BANE); ASSERT128(I128(-1ul, BANE), x % y); x = I128(BANE, 0); y = I128(2, -1ul); ASSERT128(I128(IMAX2, 0xd555555555555556), x % y); x = I128(BANE, 0); y = I128(BANE, 0); ASSERT128(I128(0, 0), x % y); x = I128(BANE, 0); y = I128(BANE, 2); ASSERT128(I128(-1ul, 0xfffffffffffffffe), x % y); x = I128(BANE, 0); y = I128(BANE, BANE); ASSERT128(I128(-1ul, BANE), x % y); x = I128(BANE, 0); y = I128(BANE, -1ul); ASSERT128(I128(-1ul, 1), x % y); x = I128(BANE, 0); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x % y); x = I128(BANE, 0); y = I128(-1ul, 2); ASSERT128(I128(-1ul, 0xfffffffffffffffe), x % y); x = I128(BANE, 0); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x % y); x = I128(BANE, 2); y = I128(0, 2); ASSERT128(I128(0, 0), x % y); x = I128(BANE, 2); y = I128(0, BANE); ASSERT128(I128(-1ul, BANE2), x % y); x = I128(BANE, 2); y = I128(0, -1ul); ASSERT128(I128(-1ul, BANE2), x % y); x = I128(BANE, 2); y = I128(2, 0); ASSERT128(I128(0xfffffffffffffffe, 2), x % y); x = I128(BANE, 2); y = I128(2, 2); ASSERT128(I128(0xfffffffffffffffe, BANE), x % y); x = I128(BANE, 2); y = I128(2, BANE); ASSERT128(I128(-1ul, BANE2), x % y); x = I128(BANE, 2); y = I128(2, -1ul); ASSERT128(I128(IMAX2, 0xd555555555555558), x % y); x = I128(BANE, 2); y = I128(BANE, 0); ASSERT128(I128(BANE, 2), x % y); x = I128(BANE, 2); y = I128(BANE, 2); ASSERT128(I128(0, 0), x % y); x = I128(BANE, 2); y = I128(BANE, BANE); ASSERT128(I128(-1ul, BANE2), x % y); x = I128(BANE, 2); y = I128(BANE, -1ul); ASSERT128(I128(-1ul, 3), x % y); x = I128(BANE, 2); y = I128(-1ul, 0); ASSERT128(I128(-1ul, 2), x % y); x = I128(BANE, 2); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x % y); x = I128(BANE, 2); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, BANE2), x % y); x = I128(BANE, 2); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(BANE, BANE); y = I128(0, 2); ASSERT128(I128(0, 0), x % y); x = I128(BANE, BANE); y = I128(0, BANE); ASSERT128(I128(0, 0), x % y); x = I128(BANE, BANE); y = I128(0, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(BANE, BANE); y = I128(2, 0); ASSERT128(I128(0xfffffffffffffffe, BANE), x % y); x = I128(BANE, BANE); y = I128(2, 2); ASSERT128(I128(0xfffffffffffffffe, 0xfffffffffffffffe), x % y); x = I128(BANE, BANE); y = I128(2, BANE); ASSERT128(I128(0, 0), x % y); x = I128(BANE, BANE); y = I128(2, -1ul); ASSERT128(I128(0xfffffffffffffffe, 0x5555555555555556), x % y); x = I128(BANE, BANE); y = I128(BANE, 0); ASSERT128(I128(BANE, BANE), x % y); x = I128(BANE, BANE); y = I128(BANE, 2); ASSERT128(I128(BANE, BANE), x % y); x = I128(BANE, BANE); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x % y); x = I128(BANE, BANE); y = I128(BANE, -1ul); ASSERT128(I128(-1ul, BANE1), x % y); x = I128(BANE, BANE); y = I128(-1ul, 0); ASSERT128(I128(-1ul, BANE), x % y); x = I128(BANE, BANE); y = I128(-1ul, 2); ASSERT128(I128(-1ul, BANE), x % y); x = I128(BANE, BANE); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x % y); x = I128(BANE, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(BANE, -1ul); y = I128(0, 2); ASSERT128(I128(-1ul, -1ul), x % y); x = I128(BANE, -1ul); y = I128(0, BANE); ASSERT128(I128(-1ul, -1ul), x % y); x = I128(BANE, -1ul); y = I128(0, -1ul); ASSERT128(I128(-1ul, BANE), x % y); x = I128(BANE, -1ul); y = I128(2, 0); ASSERT128(I128(0xfffffffffffffffe, -1ul), x % y); x = I128(BANE, -1ul); y = I128(2, 2); ASSERT128(I128(-1ul, 0x7ffffffffffffffd), x % y); x = I128(BANE, -1ul); y = I128(2, BANE); ASSERT128(I128(IMAX2, -1ul), x % y); x = I128(BANE, -1ul); y = I128(2, -1ul); ASSERT128(I128(0xfffffffffffffffe, 0xd555555555555555), x % y); x = I128(BANE, -1ul); y = I128(BANE, 0); ASSERT128(I128(BANE, -1ul), x % y); x = I128(BANE, -1ul); y = I128(BANE, 2); ASSERT128(I128(BANE, -1ul), x % y); x = I128(BANE, -1ul); y = I128(BANE, BANE); ASSERT128(I128(BANE, -1ul), x % y); x = I128(BANE, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(BANE, -1ul); y = I128(-1ul, 0); ASSERT128(I128(-1ul, -1ul), x % y); x = I128(BANE, -1ul); y = I128(-1ul, 2); ASSERT128(I128(-1ul, -1ul), x % y); x = I128(BANE, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, -1ul), x % y); x = I128(BANE, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(-1ul, 0); y = I128(0, 2); ASSERT128(I128(0, 0), x % y); x = I128(-1ul, 0); y = I128(0, BANE); ASSERT128(I128(0, 0), x % y); x = I128(-1ul, 0); y = I128(0, -1ul); ASSERT128(I128(-1ul, -1ul), x % y); x = I128(-1ul, 0); y = I128(2, 0); ASSERT128(I128(-1ul, 0), x % y); x = I128(-1ul, 0); y = I128(2, 2); ASSERT128(I128(-1ul, 0), x % y); x = I128(-1ul, 0); y = I128(2, BANE); ASSERT128(I128(-1ul, 0), x % y); x = I128(-1ul, 0); y = I128(2, -1ul); ASSERT128(I128(-1ul, 0), x % y); x = I128(-1ul, 0); y = I128(BANE, 0); ASSERT128(I128(-1ul, 0), x % y); x = I128(-1ul, 0); y = I128(BANE, 2); ASSERT128(I128(-1ul, 0), x % y); x = I128(-1ul, 0); y = I128(BANE, BANE); ASSERT128(I128(-1ul, 0), x % y); x = I128(-1ul, 0); y = I128(BANE, -1ul); ASSERT128(I128(-1ul, 0), x % y); x = I128(-1ul, 0); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x % y); x = I128(-1ul, 0); y = I128(-1ul, 2); ASSERT128(I128(-1ul, 0xfffffffffffffffe), x % y); x = I128(-1ul, 0); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x % y); x = I128(-1ul, 0); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(-1ul, 2); y = I128(0, 2); ASSERT128(I128(0, 0), x % y); x = I128(-1ul, 2); y = I128(0, BANE); ASSERT128(I128(-1ul, BANE2), x % y); x = I128(-1ul, 2); y = I128(0, -1ul); ASSERT128(I128(-1ul, 2), x % y); x = I128(-1ul, 2); y = I128(2, 0); ASSERT128(I128(-1ul, 2), x % y); x = I128(-1ul, 2); y = I128(2, 2); ASSERT128(I128(-1ul, 2), x % y); x = I128(-1ul, 2); y = I128(2, BANE); ASSERT128(I128(-1ul, 2), x % y); x = I128(-1ul, 2); y = I128(2, -1ul); ASSERT128(I128(-1ul, 2), x % y); x = I128(-1ul, 2); y = I128(BANE, 0); ASSERT128(I128(-1ul, 2), x % y); x = I128(-1ul, 2); y = I128(BANE, 2); ASSERT128(I128(-1ul, 2), x % y); x = I128(-1ul, 2); y = I128(BANE, BANE); ASSERT128(I128(-1ul, 2), x % y); x = I128(-1ul, 2); y = I128(BANE, -1ul); ASSERT128(I128(-1ul, 2), x % y); x = I128(-1ul, 2); y = I128(-1ul, 0); ASSERT128(I128(-1ul, 2), x % y); x = I128(-1ul, 2); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x % y); x = I128(-1ul, 2); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, BANE2), x % y); x = I128(-1ul, 2); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(-1ul, BANE); y = I128(0, 2); ASSERT128(I128(0, 0), x % y); x = I128(-1ul, BANE); y = I128(0, BANE); ASSERT128(I128(0, 0), x % y); x = I128(-1ul, BANE); y = I128(0, -1ul); ASSERT128(I128(-1ul, BANE), x % y); x = I128(-1ul, BANE); y = I128(2, 0); ASSERT128(I128(-1ul, BANE), x % y); x = I128(-1ul, BANE); y = I128(2, 2); ASSERT128(I128(-1ul, BANE), x % y); x = I128(-1ul, BANE); y = I128(2, BANE); ASSERT128(I128(-1ul, BANE), x % y); x = I128(-1ul, BANE); y = I128(2, -1ul); ASSERT128(I128(-1ul, BANE), x % y); x = I128(-1ul, BANE); y = I128(BANE, 0); ASSERT128(I128(-1ul, BANE), x % y); x = I128(-1ul, BANE); y = I128(BANE, 2); ASSERT128(I128(-1ul, BANE), x % y); x = I128(-1ul, BANE); y = I128(BANE, BANE); ASSERT128(I128(-1ul, BANE), x % y); x = I128(-1ul, BANE); y = I128(BANE, -1ul); ASSERT128(I128(-1ul, BANE), x % y); x = I128(-1ul, BANE); y = I128(-1ul, 0); ASSERT128(I128(-1ul, BANE), x % y); x = I128(-1ul, BANE); y = I128(-1ul, 2); ASSERT128(I128(-1ul, BANE), x % y); x = I128(-1ul, BANE); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x % y); x = I128(-1ul, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(-1ul, -1ul); y = I128(0, 2); ASSERT128(I128(-1ul, -1ul), x % y); x = I128(-1ul, -1ul); y = I128(0, BANE); ASSERT128(I128(-1ul, -1ul), x % y); x = I128(-1ul, -1ul); y = I128(0, -1ul); ASSERT128(I128(-1ul, -1ul), x % y); x = I128(-1ul, -1ul); y = I128(2, 0); ASSERT128(I128(-1ul, -1ul), x % y); x = I128(-1ul, -1ul); y = I128(2, 2); ASSERT128(I128(-1ul, -1ul), x % y); x = I128(-1ul, -1ul); y = I128(2, BANE); ASSERT128(I128(-1ul, -1ul), x % y); x = I128(-1ul, -1ul); y = I128(2, -1ul); ASSERT128(I128(-1ul, -1ul), x % y); x = I128(-1ul, -1ul); y = I128(BANE, 0); ASSERT128(I128(-1ul, -1ul), x % y); x = I128(-1ul, -1ul); y = I128(BANE, 2); ASSERT128(I128(-1ul, -1ul), x % y); x = I128(-1ul, -1ul); y = I128(BANE, BANE); ASSERT128(I128(-1ul, -1ul), x % y); x = I128(-1ul, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(-1ul, -1ul), x % y); x = I128(-1ul, -1ul); y = I128(-1ul, 0); ASSERT128(I128(-1ul, -1ul), x % y); x = I128(-1ul, -1ul); y = I128(-1ul, 2); ASSERT128(I128(-1ul, -1ul), x % y); x = I128(-1ul, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, -1ul), x % y); x = I128(-1ul, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x % y); } void testRemu128(void) { unsigned __int128 x, y; x = I128(0, 0); y = I128(0, 2); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(0, BANE); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(0, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(2, 0); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(2, 2); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(2, BANE); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(2, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(BANE, 0); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(BANE, 2); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x % y); x = I128(0, 0); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(0, 2); y = I128(0, 2); ASSERT128(I128(0, 0), x % y); x = I128(0, 2); y = I128(0, BANE); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(0, -1ul); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(2, 0); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(2, 2); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(2, BANE); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(2, -1ul); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(BANE, 0); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(BANE, 2); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(BANE, BANE); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(BANE, -1ul); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(-1ul, 0); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(-1ul, 2); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(-1ul, BANE); ASSERT128(I128(0, 2), x % y); x = I128(0, 2); y = I128(-1ul, -1ul); ASSERT128(I128(0, 2), x % y); x = I128(0, BANE); y = I128(0, 2); ASSERT128(I128(0, 0), x % y); x = I128(0, BANE); y = I128(0, BANE); ASSERT128(I128(0, 0), x % y); x = I128(0, BANE); y = I128(0, -1ul); ASSERT128(I128(0, BANE), x % y); x = I128(0, BANE); y = I128(2, 0); ASSERT128(I128(0, BANE), x % y); x = I128(0, BANE); y = I128(2, 2); ASSERT128(I128(0, BANE), x % y); x = I128(0, BANE); y = I128(2, BANE); ASSERT128(I128(0, BANE), x % y); x = I128(0, BANE); y = I128(2, -1ul); ASSERT128(I128(0, BANE), x % y); x = I128(0, BANE); y = I128(BANE, 0); ASSERT128(I128(0, BANE), x % y); x = I128(0, BANE); y = I128(BANE, 2); ASSERT128(I128(0, BANE), x % y); x = I128(0, BANE); y = I128(BANE, BANE); ASSERT128(I128(0, BANE), x % y); x = I128(0, BANE); y = I128(BANE, -1ul); ASSERT128(I128(0, BANE), x % y); x = I128(0, BANE); y = I128(-1ul, 0); ASSERT128(I128(0, BANE), x % y); x = I128(0, BANE); y = I128(-1ul, 2); ASSERT128(I128(0, BANE), x % y); x = I128(0, BANE); y = I128(-1ul, BANE); ASSERT128(I128(0, BANE), x % y); x = I128(0, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(0, BANE), x % y); x = I128(0, -1ul); y = I128(0, 2); ASSERT128(I128(0, 1), x % y); x = I128(0, -1ul); y = I128(0, BANE); ASSERT128(I128(0, IMAX), x % y); x = I128(0, -1ul); y = I128(0, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(0, -1ul); y = I128(2, 0); ASSERT128(I128(0, -1ul), x % y); x = I128(0, -1ul); y = I128(2, 2); ASSERT128(I128(0, -1ul), x % y); x = I128(0, -1ul); y = I128(2, BANE); ASSERT128(I128(0, -1ul), x % y); x = I128(0, -1ul); y = I128(2, -1ul); ASSERT128(I128(0, -1ul), x % y); x = I128(0, -1ul); y = I128(BANE, 0); ASSERT128(I128(0, -1ul), x % y); x = I128(0, -1ul); y = I128(BANE, 2); ASSERT128(I128(0, -1ul), x % y); x = I128(0, -1ul); y = I128(BANE, BANE); ASSERT128(I128(0, -1ul), x % y); x = I128(0, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(0, -1ul), x % y); x = I128(0, -1ul); y = I128(-1ul, 0); ASSERT128(I128(0, -1ul), x % y); x = I128(0, -1ul); y = I128(-1ul, 2); ASSERT128(I128(0, -1ul), x % y); x = I128(0, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(0, -1ul), x % y); x = I128(0, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(0, -1ul), x % y); x = I128(2, 0); y = I128(0, 2); ASSERT128(I128(0, 0), x % y); x = I128(2, 0); y = I128(0, BANE); ASSERT128(I128(0, 0), x % y); x = I128(2, 0); y = I128(0, -1ul); ASSERT128(I128(0, 2), x % y); x = I128(2, 0); y = I128(2, 0); ASSERT128(I128(0, 0), x % y); x = I128(2, 0); y = I128(2, 2); ASSERT128(I128(2, 0), x % y); x = I128(2, 0); y = I128(2, BANE); ASSERT128(I128(2, 0), x % y); x = I128(2, 0); y = I128(2, -1ul); ASSERT128(I128(2, 0), x % y); x = I128(2, 0); y = I128(BANE, 0); ASSERT128(I128(2, 0), x % y); x = I128(2, 0); y = I128(BANE, 2); ASSERT128(I128(2, 0), x % y); x = I128(2, 0); y = I128(BANE, BANE); ASSERT128(I128(2, 0), x % y); x = I128(2, 0); y = I128(BANE, -1ul); ASSERT128(I128(2, 0), x % y); x = I128(2, 0); y = I128(-1ul, 0); ASSERT128(I128(2, 0), x % y); x = I128(2, 0); y = I128(-1ul, 2); ASSERT128(I128(2, 0), x % y); x = I128(2, 0); y = I128(-1ul, BANE); ASSERT128(I128(2, 0), x % y); x = I128(2, 0); y = I128(-1ul, -1ul); ASSERT128(I128(2, 0), x % y); x = I128(2, 2); y = I128(0, 2); ASSERT128(I128(0, 0), x % y); x = I128(2, 2); y = I128(0, BANE); ASSERT128(I128(0, 2), x % y); x = I128(2, 2); y = I128(0, -1ul); ASSERT128(I128(0, 4), x % y); x = I128(2, 2); y = I128(2, 0); ASSERT128(I128(0, 2), x % y); x = I128(2, 2); y = I128(2, 2); ASSERT128(I128(0, 0), x % y); x = I128(2, 2); y = I128(2, BANE); ASSERT128(I128(2, 2), x % y); x = I128(2, 2); y = I128(2, -1ul); ASSERT128(I128(2, 2), x % y); x = I128(2, 2); y = I128(BANE, 0); ASSERT128(I128(2, 2), x % y); x = I128(2, 2); y = I128(BANE, 2); ASSERT128(I128(2, 2), x % y); x = I128(2, 2); y = I128(BANE, BANE); ASSERT128(I128(2, 2), x % y); x = I128(2, 2); y = I128(BANE, -1ul); ASSERT128(I128(2, 2), x % y); x = I128(2, 2); y = I128(-1ul, 0); ASSERT128(I128(2, 2), x % y); x = I128(2, 2); y = I128(-1ul, 2); ASSERT128(I128(2, 2), x % y); x = I128(2, 2); y = I128(-1ul, BANE); ASSERT128(I128(2, 2), x % y); x = I128(2, 2); y = I128(-1ul, -1ul); ASSERT128(I128(2, 2), x % y); x = I128(2, BANE); y = I128(0, 2); ASSERT128(I128(0, 0), x % y); x = I128(2, BANE); y = I128(0, BANE); ASSERT128(I128(0, 0), x % y); x = I128(2, BANE); y = I128(0, -1ul); ASSERT128(I128(0, BANE2), x % y); x = I128(2, BANE); y = I128(2, 0); ASSERT128(I128(0, BANE), x % y); x = I128(2, BANE); y = I128(2, 2); ASSERT128(I128(0, 0x7ffffffffffffffe), x % y); x = I128(2, BANE); y = I128(2, BANE); ASSERT128(I128(0, 0), x % y); x = I128(2, BANE); y = I128(2, -1ul); ASSERT128(I128(2, BANE), x % y); x = I128(2, BANE); y = I128(BANE, 0); ASSERT128(I128(2, BANE), x % y); x = I128(2, BANE); y = I128(BANE, 2); ASSERT128(I128(2, BANE), x % y); x = I128(2, BANE); y = I128(BANE, BANE); ASSERT128(I128(2, BANE), x % y); x = I128(2, BANE); y = I128(BANE, -1ul); ASSERT128(I128(2, BANE), x % y); x = I128(2, BANE); y = I128(-1ul, 0); ASSERT128(I128(2, BANE), x % y); x = I128(2, BANE); y = I128(-1ul, 2); ASSERT128(I128(2, BANE), x % y); x = I128(2, BANE); y = I128(-1ul, BANE); ASSERT128(I128(2, BANE), x % y); x = I128(2, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(2, BANE), x % y); x = I128(2, -1ul); y = I128(0, 2); ASSERT128(I128(0, 1), x % y); x = I128(2, -1ul); y = I128(0, BANE); ASSERT128(I128(0, IMAX), x % y); x = I128(2, -1ul); y = I128(0, -1ul); ASSERT128(I128(0, 2), x % y); x = I128(2, -1ul); y = I128(2, 0); ASSERT128(I128(0, -1ul), x % y); x = I128(2, -1ul); y = I128(2, 2); ASSERT128(I128(0, IMAX2), x % y); x = I128(2, -1ul); y = I128(2, BANE); ASSERT128(I128(0, IMAX), x % y); x = I128(2, -1ul); y = I128(2, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(2, -1ul); y = I128(BANE, 0); ASSERT128(I128(2, -1ul), x % y); x = I128(2, -1ul); y = I128(BANE, 2); ASSERT128(I128(2, -1ul), x % y); x = I128(2, -1ul); y = I128(BANE, BANE); ASSERT128(I128(2, -1ul), x % y); x = I128(2, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(2, -1ul), x % y); x = I128(2, -1ul); y = I128(-1ul, 0); ASSERT128(I128(2, -1ul), x % y); x = I128(2, -1ul); y = I128(-1ul, 2); ASSERT128(I128(2, -1ul), x % y); x = I128(2, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(2, -1ul), x % y); x = I128(2, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(2, -1ul), x % y); x = I128(BANE, 0); y = I128(0, 2); ASSERT128(I128(0, 0), x % y); x = I128(BANE, 0); y = I128(0, BANE); ASSERT128(I128(0, 0), x % y); x = I128(BANE, 0); y = I128(0, -1ul); ASSERT128(I128(0, BANE), x % y); x = I128(BANE, 0); y = I128(2, 0); ASSERT128(I128(0, 0), x % y); x = I128(BANE, 0); y = I128(2, 2); ASSERT128(I128(1, BANE2), x % y); x = I128(BANE, 0); y = I128(2, BANE); ASSERT128(I128(0, BANE), x % y); x = I128(BANE, 0); y = I128(2, -1ul); ASSERT128(I128(2, 0x2aaaaaaaaaaaaaaa), x % y); x = I128(BANE, 0); y = I128(BANE, 0); ASSERT128(I128(0, 0), x % y); x = I128(BANE, 0); y = I128(BANE, 2); ASSERT128(I128(BANE, 0), x % y); x = I128(BANE, 0); y = I128(BANE, BANE); ASSERT128(I128(BANE, 0), x % y); x = I128(BANE, 0); y = I128(BANE, -1ul); ASSERT128(I128(BANE, 0), x % y); x = I128(BANE, 0); y = I128(-1ul, 0); ASSERT128(I128(BANE, 0), x % y); x = I128(BANE, 0); y = I128(-1ul, 2); ASSERT128(I128(BANE, 0), x % y); x = I128(BANE, 0); y = I128(-1ul, BANE); ASSERT128(I128(BANE, 0), x % y); x = I128(BANE, 2); y = I128(0, 2); ASSERT128(I128(0, 0), x % y); x = I128(BANE, 2); y = I128(0, BANE); ASSERT128(I128(0, 2), x % y); x = I128(BANE, 2); y = I128(0, -1ul); ASSERT128(I128(0, BANE2), x % y); x = I128(BANE, 2); y = I128(2, 0); ASSERT128(I128(0, 2), x % y); x = I128(BANE, 2); y = I128(2, 2); ASSERT128(I128(1, 0x8000000000000004), x % y); x = I128(BANE, 2); y = I128(2, BANE); ASSERT128(I128(0, BANE2), x % y); x = I128(BANE, 2); y = I128(2, -1ul); ASSERT128(I128(2, 0x2aaaaaaaaaaaaaac), x % y); x = I128(BANE, 2); y = I128(BANE, 0); ASSERT128(I128(0, 2), x % y); x = I128(BANE, 2); y = I128(BANE, 2); ASSERT128(I128(0, 0), x % y); x = I128(BANE, 2); y = I128(BANE, BANE); ASSERT128(I128(BANE, 2), x % y); x = I128(BANE, 2); y = I128(BANE, -1ul); ASSERT128(I128(BANE, 2), x % y); x = I128(BANE, 2); y = I128(-1ul, 0); ASSERT128(I128(BANE, 2), x % y); x = I128(BANE, 2); y = I128(-1ul, 2); ASSERT128(I128(BANE, 2), x % y); x = I128(BANE, 2); y = I128(-1ul, BANE); ASSERT128(I128(BANE, 2), x % y); x = I128(BANE, 2); y = I128(-1ul, -1ul); ASSERT128(I128(BANE, 2), x % y); x = I128(BANE, BANE); y = I128(0, 2); ASSERT128(I128(0, 0), x % y); x = I128(BANE, BANE); y = I128(0, BANE); ASSERT128(I128(0, 0), x % y); x = I128(BANE, BANE); y = I128(0, -1ul); ASSERT128(I128(0, 1), x % y); x = I128(BANE, BANE); y = I128(2, 0); ASSERT128(I128(0, BANE), x % y); x = I128(BANE, BANE); y = I128(2, 2); ASSERT128(I128(0, 0), x % y); x = I128(BANE, BANE); y = I128(2, BANE); ASSERT128(I128(1, 0), x % y); x = I128(BANE, BANE); y = I128(2, -1ul); ASSERT128(I128(2, 0xaaaaaaaaaaaaaaaa), x % y); x = I128(BANE, BANE); y = I128(BANE, 0); ASSERT128(I128(0, BANE), x % y); x = I128(BANE, BANE); y = I128(BANE, 2); ASSERT128(I128(0, 0x7ffffffffffffffe), x % y); x = I128(BANE, BANE); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x % y); x = I128(BANE, BANE); y = I128(BANE, -1ul); ASSERT128(I128(BANE, BANE), x % y); x = I128(BANE, BANE); y = I128(-1ul, 0); ASSERT128(I128(BANE, BANE), x % y); x = I128(BANE, BANE); y = I128(-1ul, 2); ASSERT128(I128(BANE, BANE), x % y); x = I128(BANE, BANE); y = I128(-1ul, BANE); ASSERT128(I128(BANE, BANE), x % y); x = I128(BANE, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(BANE, BANE), x % y); x = I128(BANE, -1ul); y = I128(0, 2); ASSERT128(I128(0, 1), x % y); x = I128(BANE, -1ul); y = I128(0, BANE); ASSERT128(I128(0, IMAX), x % y); x = I128(BANE, -1ul); y = I128(0, -1ul); ASSERT128(I128(0, BANE), x % y); x = I128(BANE, -1ul); y = I128(2, 0); ASSERT128(I128(0, -1ul), x % y); x = I128(BANE, -1ul); y = I128(2, 2); ASSERT128(I128(0, IMAX), x % y); x = I128(BANE, -1ul); y = I128(2, BANE); ASSERT128(I128(1, IMAX), x % y); x = I128(BANE, -1ul); y = I128(2, -1ul); ASSERT128(I128(0, 0x2aaaaaaaaaaaaaaa), x % y); x = I128(BANE, -1ul); y = I128(BANE, 0); ASSERT128(I128(0, -1ul), x % y); x = I128(BANE, -1ul); y = I128(BANE, 2); ASSERT128(I128(0, IMAX2), x % y); x = I128(BANE, -1ul); y = I128(BANE, BANE); ASSERT128(I128(0, IMAX), x % y); x = I128(BANE, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(BANE, -1ul); y = I128(-1ul, 0); ASSERT128(I128(BANE, -1ul), x % y); x = I128(BANE, -1ul); y = I128(-1ul, 2); ASSERT128(I128(BANE, -1ul), x % y); x = I128(BANE, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(BANE, -1ul), x % y); x = I128(BANE, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(BANE, -1ul), x % y); x = I128(-1ul, 0); y = I128(0, 2); ASSERT128(I128(0, 0), x % y); x = I128(-1ul, 0); y = I128(0, BANE); ASSERT128(I128(0, 0), x % y); x = I128(-1ul, 0); y = I128(0, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(-1ul, 0); y = I128(2, 0); ASSERT128(I128(1, 0), x % y); x = I128(-1ul, 0); y = I128(2, 2); ASSERT128(I128(0, 2), x % y); x = I128(-1ul, 0); y = I128(2, BANE); ASSERT128(I128(0, 0), x % y); x = I128(-1ul, 0); y = I128(2, -1ul); ASSERT128(I128(0, 0x5555555555555555), x % y); x = I128(-1ul, 0); y = I128(BANE, 0); ASSERT128(I128(IMAX, 0), x % y); x = I128(-1ul, 0); y = I128(BANE, 2); ASSERT128(I128(0x7ffffffffffffffe, 0xfffffffffffffffe), x % y); x = I128(-1ul, 0); y = I128(BANE, BANE); ASSERT128(I128(0x7ffffffffffffffe, BANE), x % y); x = I128(-1ul, 0); y = I128(BANE, -1ul); ASSERT128(I128(0x7ffffffffffffffe, 1), x % y); x = I128(-1ul, 0); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x % y); x = I128(-1ul, 0); y = I128(-1ul, 2); ASSERT128(I128(-1ul, 0), x % y); x = I128(-1ul, 0); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, 0), x % y); x = I128(-1ul, 0); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, 0), x % y); x = I128(-1ul, 2); y = I128(0, 2); ASSERT128(I128(0, 0), x % y); x = I128(-1ul, 2); y = I128(0, BANE); ASSERT128(I128(0, 2), x % y); x = I128(-1ul, 2); y = I128(0, -1ul); ASSERT128(I128(0, 2), x % y); x = I128(-1ul, 2); y = I128(2, 0); ASSERT128(I128(1, 2), x % y); x = I128(-1ul, 2); y = I128(2, 2); ASSERT128(I128(0, 4), x % y); x = I128(-1ul, 2); y = I128(2, BANE); ASSERT128(I128(0, 2), x % y); x = I128(-1ul, 2); y = I128(2, -1ul); ASSERT128(I128(0, 0x5555555555555557), x % y); x = I128(-1ul, 2); y = I128(BANE, 0); ASSERT128(I128(IMAX, 2), x % y); x = I128(-1ul, 2); y = I128(BANE, 2); ASSERT128(I128(IMAX, 0), x % y); x = I128(-1ul, 2); y = I128(BANE, BANE); ASSERT128(I128(0x7ffffffffffffffe, BANE2), x % y); x = I128(-1ul, 2); y = I128(BANE, -1ul); ASSERT128(I128(0x7ffffffffffffffe, 3), x % y); x = I128(-1ul, 2); y = I128(-1ul, 0); ASSERT128(I128(0, 2), x % y); x = I128(-1ul, 2); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x % y); x = I128(-1ul, 2); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, 2), x % y); x = I128(-1ul, 2); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, 2), x % y); x = I128(-1ul, BANE); y = I128(0, 2); ASSERT128(I128(0, 0), x % y); x = I128(-1ul, BANE); y = I128(0, BANE); ASSERT128(I128(0, 0), x % y); x = I128(-1ul, BANE); y = I128(0, -1ul); ASSERT128(I128(0, BANE), x % y); x = I128(-1ul, BANE); y = I128(2, 0); ASSERT128(I128(1, BANE), x % y); x = I128(-1ul, BANE); y = I128(2, 2); ASSERT128(I128(0, BANE2), x % y); x = I128(-1ul, BANE); y = I128(2, BANE); ASSERT128(I128(0, BANE), x % y); x = I128(-1ul, BANE); y = I128(2, -1ul); ASSERT128(I128(0, 0xd555555555555555), x % y); x = I128(-1ul, BANE); y = I128(BANE, 0); ASSERT128(I128(IMAX, BANE), x % y); x = I128(-1ul, BANE); y = I128(BANE, 2); ASSERT128(I128(IMAX, 0x7ffffffffffffffe), x % y); x = I128(-1ul, BANE); y = I128(BANE, BANE); ASSERT128(I128(IMAX, 0), x % y); x = I128(-1ul, BANE); y = I128(BANE, -1ul); ASSERT128(I128(0x7ffffffffffffffe, BANE1), x % y); x = I128(-1ul, BANE); y = I128(-1ul, 0); ASSERT128(I128(0, BANE), x % y); x = I128(-1ul, BANE); y = I128(-1ul, 2); ASSERT128(I128(0, 0x7ffffffffffffffe), x % y); x = I128(-1ul, BANE); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x % y); x = I128(-1ul, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, BANE), x % y); x = I128(-1ul, -1ul); y = I128(0, 2); ASSERT128(I128(0, 1), x % y); x = I128(-1ul, -1ul); y = I128(0, BANE); ASSERT128(I128(0, IMAX), x % y); x = I128(-1ul, -1ul); y = I128(0, -1ul); ASSERT128(I128(0, 0), x % y); x = I128(-1ul, -1ul); y = I128(2, 0); ASSERT128(I128(1, -1ul), x % y); x = I128(-1ul, -1ul); y = I128(2, 2); ASSERT128(I128(1, 1), x % y); x = I128(-1ul, -1ul); y = I128(2, BANE); ASSERT128(I128(0, -1ul), x % y); x = I128(-1ul, -1ul); y = I128(2, -1ul); ASSERT128(I128(1, 0x5555555555555554), x % y); x = I128(-1ul, -1ul); y = I128(BANE, 0); ASSERT128(I128(IMAX, -1ul), x % y); x = I128(-1ul, -1ul); y = I128(BANE, 2); ASSERT128(I128(IMAX, IMAX2), x % y); x = I128(-1ul, -1ul); y = I128(BANE, BANE); ASSERT128(I128(IMAX, IMAX), x % y); x = I128(-1ul, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(IMAX, 0), x % y); x = I128(-1ul, -1ul); y = I128(-1ul, 0); ASSERT128(I128(0, -1ul), x % y); x = I128(-1ul, -1ul); y = I128(-1ul, 2); ASSERT128(I128(0, IMAX2), x % y); x = I128(-1ul, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(0, IMAX), x % y); x = I128(-1ul, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x % y); } void testShr128(void) { unsigned __int128 x; x = I128(0, 0); x >>= 0; ASSERT128(I128(0, 0), x); x = I128(0, 0); x >>= 1; ASSERT128(I128(0, 0), x); x = I128(0, 0); x >>= 126; ASSERT128(I128(0, 0), x); x = I128(0, 0); x >>= 127; ASSERT128(I128(0, 0), x); x = I128(0, 2); x >>= 0; ASSERT128(I128(0, 2), x); x = I128(0, 2); x >>= 1; ASSERT128(I128(0, 1), x); x = I128(0, 2); x >>= 126; ASSERT128(I128(0, 0), x); x = I128(0, 2); x >>= 127; ASSERT128(I128(0, 0), x); x = I128(0, BANE); x >>= 0; ASSERT128(I128(0, BANE), x); x = I128(0, BANE); x >>= 1; ASSERT128(I128(0, 0x4000000000000000), x); x = I128(0, BANE); x >>= 126; ASSERT128(I128(0, 0), x); x = I128(0, BANE); x >>= 127; ASSERT128(I128(0, 0), x); x = I128(0, -1ul); x >>= 0; ASSERT128(I128(0, -1ul), x); x = I128(0, -1ul); x >>= 1; ASSERT128(I128(0, IMAX), x); x = I128(0, -1ul); x >>= 126; ASSERT128(I128(0, 0), x); x = I128(0, -1ul); x >>= 127; ASSERT128(I128(0, 0), x); x = I128(2, 0); x >>= 0; ASSERT128(I128(2, 0), x); x = I128(2, 0); x >>= 1; ASSERT128(I128(1, 0), x); x = I128(2, 0); x >>= 126; ASSERT128(I128(0, 0), x); x = I128(2, 0); x >>= 127; ASSERT128(I128(0, 0), x); x = I128(2, 2); x >>= 0; ASSERT128(I128(2, 2), x); x = I128(2, 2); x >>= 1; ASSERT128(I128(1, 1), x); x = I128(2, 2); x >>= 126; ASSERT128(I128(0, 0), x); x = I128(2, 2); x >>= 127; ASSERT128(I128(0, 0), x); x = I128(2, BANE); x >>= 0; ASSERT128(I128(2, BANE), x); x = I128(2, BANE); x >>= 1; ASSERT128(I128(1, 0x4000000000000000), x); x = I128(2, BANE); x >>= 126; ASSERT128(I128(0, 0), x); x = I128(2, BANE); x >>= 127; ASSERT128(I128(0, 0), x); x = I128(2, -1ul); x >>= 0; ASSERT128(I128(2, -1ul), x); x = I128(2, -1ul); x >>= 1; ASSERT128(I128(1, IMAX), x); x = I128(2, -1ul); x >>= 126; ASSERT128(I128(0, 0), x); x = I128(2, -1ul); x >>= 127; ASSERT128(I128(0, 0), x); x = I128(BANE, 0); x >>= 0; ASSERT128(I128(BANE, 0), x); x = I128(BANE, 0); x >>= 1; ASSERT128(I128(0x4000000000000000, 0), x); x = I128(BANE, 0); x >>= 126; ASSERT128(I128(0, 2), x); x = I128(BANE, 0); x >>= 127; ASSERT128(I128(0, 1), x); x = I128(BANE, 2); x >>= 0; ASSERT128(I128(BANE, 2), x); x = I128(BANE, 2); x >>= 1; ASSERT128(I128(0x4000000000000000, 1), x); x = I128(BANE, 2); x >>= 126; ASSERT128(I128(0, 2), x); x = I128(BANE, 2); x >>= 127; ASSERT128(I128(0, 1), x); x = I128(BANE, BANE); x >>= 0; ASSERT128(I128(BANE, BANE), x); x = I128(BANE, BANE); x >>= 1; ASSERT128(I128(0x4000000000000000, 0x4000000000000000), x); x = I128(BANE, BANE); x >>= 126; ASSERT128(I128(0, 2), x); x = I128(BANE, BANE); x >>= 127; ASSERT128(I128(0, 1), x); x = I128(BANE, -1ul); x >>= 0; ASSERT128(I128(BANE, -1ul), x); x = I128(BANE, -1ul); x >>= 1; ASSERT128(I128(0x4000000000000000, IMAX), x); x = I128(BANE, -1ul); x >>= 126; ASSERT128(I128(0, 2), x); x = I128(BANE, -1ul); x >>= 127; ASSERT128(I128(0, 1), x); x = I128(-1ul, 0); x >>= 0; ASSERT128(I128(-1ul, 0), x); x = I128(-1ul, 0); x >>= 1; ASSERT128(I128(IMAX, BANE), x); x = I128(-1ul, 0); x >>= 126; ASSERT128(I128(0, 3), x); x = I128(-1ul, 0); x >>= 127; ASSERT128(I128(0, 1), x); x = I128(-1ul, 2); x >>= 0; ASSERT128(I128(-1ul, 2), x); x = I128(-1ul, 2); x >>= 1; ASSERT128(I128(IMAX, BANE1), x); x = I128(-1ul, 2); x >>= 126; ASSERT128(I128(0, 3), x); x = I128(-1ul, 2); x >>= 127; ASSERT128(I128(0, 1), x); x = I128(-1ul, BANE); x >>= 0; ASSERT128(I128(-1ul, BANE), x); x = I128(-1ul, BANE); x >>= 1; ASSERT128(I128(IMAX, 0xc000000000000000), x); x = I128(-1ul, BANE); x >>= 126; ASSERT128(I128(0, 3), x); x = I128(-1ul, BANE); x >>= 127; ASSERT128(I128(0, 1), x); x = I128(-1ul, -1ul); x >>= 0; ASSERT128(I128(-1ul, -1ul), x); x = I128(-1ul, -1ul); x >>= 1; ASSERT128(I128(IMAX, -1ul), x); x = I128(-1ul, -1ul); x >>= 126; ASSERT128(I128(0, 3), x); x = I128(-1ul, -1ul); x >>= 127; ASSERT128(I128(0, 1), x); } void testSar128(void) { __int128 x; x = I128(0, 0); x >>= 0; ASSERT128(I128(0, 0), x); x = I128(0, 0); x >>= 1; ASSERT128(I128(0, 0), x); x = I128(0, 0); x >>= 126; ASSERT128(I128(0, 0), x); x = I128(0, 0); x >>= 127; ASSERT128(I128(0, 0), x); x = I128(0, 2); x >>= 0; ASSERT128(I128(0, 2), x); x = I128(0, 2); x >>= 1; ASSERT128(I128(0, 1), x); x = I128(0, 2); x >>= 126; ASSERT128(I128(0, 0), x); x = I128(0, 2); x >>= 127; ASSERT128(I128(0, 0), x); x = I128(0, BANE); x >>= 0; ASSERT128(I128(0, BANE), x); x = I128(0, BANE); x >>= 1; ASSERT128(I128(0, 0x4000000000000000), x); x = I128(0, BANE); x >>= 126; ASSERT128(I128(0, 0), x); x = I128(0, BANE); x >>= 127; ASSERT128(I128(0, 0), x); x = I128(0, -1ul); x >>= 0; ASSERT128(I128(0, -1ul), x); x = I128(0, -1ul); x >>= 1; ASSERT128(I128(0, IMAX), x); x = I128(0, -1ul); x >>= 126; ASSERT128(I128(0, 0), x); x = I128(0, -1ul); x >>= 127; ASSERT128(I128(0, 0), x); x = I128(2, 0); x >>= 0; ASSERT128(I128(2, 0), x); x = I128(2, 0); x >>= 1; ASSERT128(I128(1, 0), x); x = I128(2, 0); x >>= 126; ASSERT128(I128(0, 0), x); x = I128(2, 0); x >>= 127; ASSERT128(I128(0, 0), x); x = I128(2, 2); x >>= 0; ASSERT128(I128(2, 2), x); x = I128(2, 2); x >>= 1; ASSERT128(I128(1, 1), x); x = I128(2, 2); x >>= 126; ASSERT128(I128(0, 0), x); x = I128(2, 2); x >>= 127; ASSERT128(I128(0, 0), x); x = I128(2, BANE); x >>= 0; ASSERT128(I128(2, BANE), x); x = I128(2, BANE); x >>= 1; ASSERT128(I128(1, 0x4000000000000000), x); x = I128(2, BANE); x >>= 126; ASSERT128(I128(0, 0), x); x = I128(2, BANE); x >>= 127; ASSERT128(I128(0, 0), x); x = I128(2, -1ul); x >>= 0; ASSERT128(I128(2, -1ul), x); x = I128(2, -1ul); x >>= 1; ASSERT128(I128(1, IMAX), x); x = I128(2, -1ul); x >>= 126; ASSERT128(I128(0, 0), x); x = I128(2, -1ul); x >>= 127; ASSERT128(I128(0, 0), x); x = I128(BANE, 0); x >>= 0; ASSERT128(I128(BANE, 0), x); x = I128(BANE, 0); x >>= 1; ASSERT128(I128(0xc000000000000000, 0), x); x = I128(BANE, 0); x >>= 126; ASSERT128(I128(-1ul, 0xfffffffffffffffe), x); x = I128(BANE, 0); x >>= 127; ASSERT128(I128(-1ul, -1ul), x); x = I128(BANE, 2); x >>= 0; ASSERT128(I128(BANE, 2), x); x = I128(BANE, 2); x >>= 1; ASSERT128(I128(0xc000000000000000, 1), x); x = I128(BANE, 2); x >>= 126; ASSERT128(I128(-1ul, 0xfffffffffffffffe), x); x = I128(BANE, 2); x >>= 127; ASSERT128(I128(-1ul, -1ul), x); x = I128(BANE, BANE); x >>= 0; ASSERT128(I128(BANE, BANE), x); x = I128(BANE, BANE); x >>= 1; ASSERT128(I128(0xc000000000000000, 0x4000000000000000), x); x = I128(BANE, BANE); x >>= 126; ASSERT128(I128(-1ul, 0xfffffffffffffffe), x); x = I128(BANE, BANE); x >>= 127; ASSERT128(I128(-1ul, -1ul), x); x = I128(BANE, -1ul); x >>= 0; ASSERT128(I128(BANE, -1ul), x); x = I128(BANE, -1ul); x >>= 1; ASSERT128(I128(0xc000000000000000, IMAX), x); x = I128(BANE, -1ul); x >>= 126; ASSERT128(I128(-1ul, 0xfffffffffffffffe), x); x = I128(BANE, -1ul); x >>= 127; ASSERT128(I128(-1ul, -1ul), x); x = I128(-1ul, 0); x >>= 0; ASSERT128(I128(-1ul, 0), x); x = I128(-1ul, 0); x >>= 1; ASSERT128(I128(-1ul, BANE), x); x = I128(-1ul, 0); x >>= 126; ASSERT128(I128(-1ul, -1ul), x); x = I128(-1ul, 0); x >>= 127; ASSERT128(I128(-1ul, -1ul), x); x = I128(-1ul, 2); x >>= 0; ASSERT128(I128(-1ul, 2), x); x = I128(-1ul, 2); x >>= 1; ASSERT128(I128(-1ul, BANE1), x); x = I128(-1ul, 2); x >>= 126; ASSERT128(I128(-1ul, -1ul), x); x = I128(-1ul, 2); x >>= 127; ASSERT128(I128(-1ul, -1ul), x); x = I128(-1ul, BANE); x >>= 0; ASSERT128(I128(-1ul, BANE), x); x = I128(-1ul, BANE); x >>= 1; ASSERT128(I128(-1ul, 0xc000000000000000), x); x = I128(-1ul, BANE); x >>= 126; ASSERT128(I128(-1ul, -1ul), x); x = I128(-1ul, BANE); x >>= 127; ASSERT128(I128(-1ul, -1ul), x); x = I128(-1ul, -1ul); x >>= 0; ASSERT128(I128(-1ul, -1ul), x); x = I128(-1ul, -1ul); x >>= 1; ASSERT128(I128(-1ul, -1ul), x); x = I128(-1ul, -1ul); x >>= 126; ASSERT128(I128(-1ul, -1ul), x); x = I128(-1ul, -1ul); x >>= 127; ASSERT128(I128(-1ul, -1ul), x); } void testShl128(void) { __int128 x; x = I128(0, 0); x <<= 0; ASSERT128(I128(0, 0), x); x = I128(0, 0); x <<= 1; ASSERT128(I128(0, 0), x); x = I128(0, 0); x <<= 126; ASSERT128(I128(0, 0), x); x = I128(0, 0); x <<= 127; ASSERT128(I128(0, 0), x); x = I128(0, 2); x <<= 0; ASSERT128(I128(0, 2), x); x = I128(0, 2); x <<= 1; ASSERT128(I128(0, 4), x); x = I128(0, 2); x <<= 126; ASSERT128(I128(BANE, 0), x); x = I128(0, 2); x <<= 127; ASSERT128(I128(0, 0), x); x = I128(0, BANE); x <<= 0; ASSERT128(I128(0, BANE), x); x = I128(0, BANE); x <<= 1; ASSERT128(I128(1, 0), x); x = I128(0, BANE); x <<= 126; ASSERT128(I128(0, 0), x); x = I128(0, BANE); x <<= 127; ASSERT128(I128(0, 0), x); x = I128(0, -1ul); x <<= 0; ASSERT128(I128(0, -1ul), x); x = I128(0, -1ul); x <<= 1; ASSERT128(I128(1, 0xfffffffffffffffe), x); x = I128(0, -1ul); x <<= 126; ASSERT128(I128(0xc000000000000000, 0), x); x = I128(0, -1ul); x <<= 127; ASSERT128(I128(BANE, 0), x); x = I128(2, 0); x <<= 0; ASSERT128(I128(2, 0), x); x = I128(2, 0); x <<= 1; ASSERT128(I128(4, 0), x); x = I128(2, 0); x <<= 126; ASSERT128(I128(0, 0), x); x = I128(2, 0); x <<= 127; ASSERT128(I128(0, 0), x); x = I128(2, 2); x <<= 0; ASSERT128(I128(2, 2), x); x = I128(2, 2); x <<= 1; ASSERT128(I128(4, 4), x); x = I128(2, 2); x <<= 126; ASSERT128(I128(BANE, 0), x); x = I128(2, 2); x <<= 127; ASSERT128(I128(0, 0), x); x = I128(2, BANE); x <<= 0; ASSERT128(I128(2, BANE), x); x = I128(2, BANE); x <<= 1; ASSERT128(I128(5, 0), x); x = I128(2, BANE); x <<= 126; ASSERT128(I128(0, 0), x); x = I128(2, BANE); x <<= 127; ASSERT128(I128(0, 0), x); x = I128(2, -1ul); x <<= 0; ASSERT128(I128(2, -1ul), x); x = I128(2, -1ul); x <<= 1; ASSERT128(I128(5, 0xfffffffffffffffe), x); x = I128(2, -1ul); x <<= 126; ASSERT128(I128(0xc000000000000000, 0), x); x = I128(2, -1ul); x <<= 127; ASSERT128(I128(BANE, 0), x); x = I128(BANE, 0); x <<= 0; ASSERT128(I128(BANE, 0), x); x = I128(BANE, 0); x <<= 1; ASSERT128(I128(0, 0), x); x = I128(BANE, 0); x <<= 126; ASSERT128(I128(0, 0), x); x = I128(BANE, 0); x <<= 127; ASSERT128(I128(0, 0), x); x = I128(BANE, 2); x <<= 0; ASSERT128(I128(BANE, 2), x); x = I128(BANE, 2); x <<= 1; ASSERT128(I128(0, 4), x); x = I128(BANE, 2); x <<= 126; ASSERT128(I128(BANE, 0), x); x = I128(BANE, 2); x <<= 127; ASSERT128(I128(0, 0), x); x = I128(BANE, BANE); x <<= 0; ASSERT128(I128(BANE, BANE), x); x = I128(BANE, BANE); x <<= 1; ASSERT128(I128(1, 0), x); x = I128(BANE, BANE); x <<= 126; ASSERT128(I128(0, 0), x); x = I128(BANE, BANE); x <<= 127; ASSERT128(I128(0, 0), x); x = I128(BANE, -1ul); x <<= 0; ASSERT128(I128(BANE, -1ul), x); x = I128(BANE, -1ul); x <<= 1; ASSERT128(I128(1, 0xfffffffffffffffe), x); x = I128(BANE, -1ul); x <<= 126; ASSERT128(I128(0xc000000000000000, 0), x); x = I128(BANE, -1ul); x <<= 127; ASSERT128(I128(BANE, 0), x); x = I128(-1ul, 0); x <<= 0; ASSERT128(I128(-1ul, 0), x); x = I128(-1ul, 0); x <<= 1; ASSERT128(I128(0xfffffffffffffffe, 0), x); x = I128(-1ul, 0); x <<= 126; ASSERT128(I128(0, 0), x); x = I128(-1ul, 0); x <<= 127; ASSERT128(I128(0, 0), x); x = I128(-1ul, 2); x <<= 0; ASSERT128(I128(-1ul, 2), x); x = I128(-1ul, 2); x <<= 1; ASSERT128(I128(0xfffffffffffffffe, 4), x); x = I128(-1ul, 2); x <<= 126; ASSERT128(I128(BANE, 0), x); x = I128(-1ul, 2); x <<= 127; ASSERT128(I128(0, 0), x); x = I128(-1ul, BANE); x <<= 0; ASSERT128(I128(-1ul, BANE), x); x = I128(-1ul, BANE); x <<= 1; ASSERT128(I128(-1ul, 0), x); x = I128(-1ul, BANE); x <<= 126; ASSERT128(I128(0, 0), x); x = I128(-1ul, BANE); x <<= 127; ASSERT128(I128(0, 0), x); x = I128(-1ul, -1ul); x <<= 0; ASSERT128(I128(-1ul, -1ul), x); x = I128(-1ul, -1ul); x <<= 1; ASSERT128(I128(-1ul, 0xfffffffffffffffe), x); x = I128(-1ul, -1ul); x <<= 126; ASSERT128(I128(0xc000000000000000, 0), x); x = I128(-1ul, -1ul); x <<= 127; ASSERT128(I128(BANE, 0), x); } void testNeg128(void) { __int128 x; x = I128(0, 0); ASSERT128(I128(0, 0), -x); x = I128(0, 2); ASSERT128(I128(-1ul, 0xfffffffffffffffe), -x); x = I128(0, BANE); ASSERT128(I128(-1ul, BANE), -x); x = I128(0, -1ul); ASSERT128(I128(-1ul, 1), -x); x = I128(2, 0); ASSERT128(I128(0xfffffffffffffffe, 0), -x); x = I128(2, 2); ASSERT128(I128(IMAX2, 0xfffffffffffffffe), -x); x = I128(2, BANE); ASSERT128(I128(IMAX2, BANE), -x); x = I128(2, -1ul); ASSERT128(I128(IMAX2, 1), -x); x = I128(BANE, 0); ASSERT128(I128(BANE, 0), -x); x = I128(BANE, 2); ASSERT128(I128(IMAX, 0xfffffffffffffffe), -x); x = I128(BANE, BANE); ASSERT128(I128(IMAX, BANE), -x); x = I128(BANE, -1ul); ASSERT128(I128(IMAX, 1), -x); x = I128(-1ul, 0); ASSERT128(I128(1, 0), -x); x = I128(-1ul, 2); ASSERT128(I128(0, 0xfffffffffffffffe), -x); x = I128(-1ul, BANE); ASSERT128(I128(0, BANE), -x); x = I128(-1ul, -1ul); ASSERT128(I128(0, 1), -x); } void testXor128(void) { __int128 x, y; x = I128(0, 0); y = I128(0, 2); ASSERT128(I128(0, 2), x ^ y); x = I128(0, 0); y = I128(0, BANE); ASSERT128(I128(0, BANE), x ^ y); x = I128(0, 0); y = I128(0, -1ul); ASSERT128(I128(0, -1ul), x ^ y); x = I128(0, 0); y = I128(2, 0); ASSERT128(I128(2, 0), x ^ y); x = I128(0, 0); y = I128(2, 2); ASSERT128(I128(2, 2), x ^ y); x = I128(0, 0); y = I128(2, BANE); ASSERT128(I128(2, BANE), x ^ y); x = I128(0, 0); y = I128(2, -1ul); ASSERT128(I128(2, -1ul), x ^ y); x = I128(0, 0); y = I128(BANE, 0); ASSERT128(I128(BANE, 0), x ^ y); x = I128(0, 0); y = I128(BANE, 2); ASSERT128(I128(BANE, 2), x ^ y); x = I128(0, 0); y = I128(BANE, BANE); ASSERT128(I128(BANE, BANE), x ^ y); x = I128(0, 0); y = I128(BANE, -1ul); ASSERT128(I128(BANE, -1ul), x ^ y); x = I128(0, 0); y = I128(-1ul, 0); ASSERT128(I128(-1ul, 0), x ^ y); x = I128(0, 0); y = I128(-1ul, 2); ASSERT128(I128(-1ul, 2), x ^ y); x = I128(0, 0); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, BANE), x ^ y); x = I128(0, 0); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, -1ul), x ^ y); x = I128(0, 2); y = I128(0, 2); ASSERT128(I128(0, 0), x ^ y); x = I128(0, 2); y = I128(0, BANE); ASSERT128(I128(0, BANE2), x ^ y); x = I128(0, 2); y = I128(0, -1ul); ASSERT128(I128(0, IMAX2), x ^ y); x = I128(0, 2); y = I128(2, 0); ASSERT128(I128(2, 2), x ^ y); x = I128(0, 2); y = I128(2, 2); ASSERT128(I128(2, 0), x ^ y); x = I128(0, 2); y = I128(2, BANE); ASSERT128(I128(2, BANE2), x ^ y); x = I128(0, 2); y = I128(2, -1ul); ASSERT128(I128(2, IMAX2), x ^ y); x = I128(0, 2); y = I128(BANE, 0); ASSERT128(I128(BANE, 2), x ^ y); x = I128(0, 2); y = I128(BANE, 2); ASSERT128(I128(BANE, 0), x ^ y); x = I128(0, 2); y = I128(BANE, BANE); ASSERT128(I128(BANE, BANE2), x ^ y); x = I128(0, 2); y = I128(BANE, -1ul); ASSERT128(I128(BANE, IMAX2), x ^ y); x = I128(0, 2); y = I128(-1ul, 0); ASSERT128(I128(-1ul, 2), x ^ y); x = I128(0, 2); y = I128(-1ul, 2); ASSERT128(I128(-1ul, 0), x ^ y); x = I128(0, 2); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, BANE2), x ^ y); x = I128(0, 2); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, IMAX2), x ^ y); x = I128(0, BANE); y = I128(0, 2); ASSERT128(I128(0, BANE2), x ^ y); x = I128(0, BANE); y = I128(0, BANE); ASSERT128(I128(0, 0), x ^ y); x = I128(0, BANE); y = I128(0, -1ul); ASSERT128(I128(0, IMAX), x ^ y); x = I128(0, BANE); y = I128(2, 0); ASSERT128(I128(2, BANE), x ^ y); x = I128(0, BANE); y = I128(2, 2); ASSERT128(I128(2, BANE2), x ^ y); x = I128(0, BANE); y = I128(2, BANE); ASSERT128(I128(2, 0), x ^ y); x = I128(0, BANE); y = I128(2, -1ul); ASSERT128(I128(2, IMAX), x ^ y); x = I128(0, BANE); y = I128(BANE, 0); ASSERT128(I128(BANE, BANE), x ^ y); x = I128(0, BANE); y = I128(BANE, 2); ASSERT128(I128(BANE, BANE2), x ^ y); x = I128(0, BANE); y = I128(BANE, BANE); ASSERT128(I128(BANE, 0), x ^ y); x = I128(0, BANE); y = I128(BANE, -1ul); ASSERT128(I128(BANE, IMAX), x ^ y); x = I128(0, BANE); y = I128(-1ul, 0); ASSERT128(I128(-1ul, BANE), x ^ y); x = I128(0, BANE); y = I128(-1ul, 2); ASSERT128(I128(-1ul, BANE2), x ^ y); x = I128(0, BANE); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, 0), x ^ y); x = I128(0, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, IMAX), x ^ y); x = I128(0, -1ul); y = I128(0, 2); ASSERT128(I128(0, IMAX2), x ^ y); x = I128(0, -1ul); y = I128(0, BANE); ASSERT128(I128(0, IMAX), x ^ y); x = I128(0, -1ul); y = I128(0, -1ul); ASSERT128(I128(0, 0), x ^ y); x = I128(0, -1ul); y = I128(2, 0); ASSERT128(I128(2, -1ul), x ^ y); x = I128(0, -1ul); y = I128(2, 2); ASSERT128(I128(2, IMAX2), x ^ y); x = I128(0, -1ul); y = I128(2, BANE); ASSERT128(I128(2, IMAX), x ^ y); x = I128(0, -1ul); y = I128(2, -1ul); ASSERT128(I128(2, 0), x ^ y); x = I128(0, -1ul); y = I128(BANE, 0); ASSERT128(I128(BANE, -1ul), x ^ y); x = I128(0, -1ul); y = I128(BANE, 2); ASSERT128(I128(BANE, IMAX2), x ^ y); x = I128(0, -1ul); y = I128(BANE, BANE); ASSERT128(I128(BANE, IMAX), x ^ y); x = I128(0, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(BANE, 0), x ^ y); x = I128(0, -1ul); y = I128(-1ul, 0); ASSERT128(I128(-1ul, -1ul), x ^ y); x = I128(0, -1ul); y = I128(-1ul, 2); ASSERT128(I128(-1ul, IMAX2), x ^ y); x = I128(0, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, IMAX), x ^ y); x = I128(0, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, 0), x ^ y); x = I128(2, 0); y = I128(0, 2); ASSERT128(I128(2, 2), x ^ y); x = I128(2, 0); y = I128(0, BANE); ASSERT128(I128(2, BANE), x ^ y); x = I128(2, 0); y = I128(0, -1ul); ASSERT128(I128(2, -1ul), x ^ y); x = I128(2, 0); y = I128(2, 0); ASSERT128(I128(0, 0), x ^ y); x = I128(2, 0); y = I128(2, 2); ASSERT128(I128(0, 2), x ^ y); x = I128(2, 0); y = I128(2, BANE); ASSERT128(I128(0, BANE), x ^ y); x = I128(2, 0); y = I128(2, -1ul); ASSERT128(I128(0, -1ul), x ^ y); x = I128(2, 0); y = I128(BANE, 0); ASSERT128(I128(BANE2, 0), x ^ y); x = I128(2, 0); y = I128(BANE, 2); ASSERT128(I128(BANE2, 2), x ^ y); x = I128(2, 0); y = I128(BANE, BANE); ASSERT128(I128(BANE2, BANE), x ^ y); x = I128(2, 0); y = I128(BANE, -1ul); ASSERT128(I128(BANE2, -1ul), x ^ y); x = I128(2, 0); y = I128(-1ul, 0); ASSERT128(I128(IMAX2, 0), x ^ y); x = I128(2, 0); y = I128(-1ul, 2); ASSERT128(I128(IMAX2, 2), x ^ y); x = I128(2, 0); y = I128(-1ul, BANE); ASSERT128(I128(IMAX2, BANE), x ^ y); x = I128(2, 0); y = I128(-1ul, -1ul); ASSERT128(I128(IMAX2, -1ul), x ^ y); x = I128(2, 2); y = I128(0, 2); ASSERT128(I128(2, 0), x ^ y); x = I128(2, 2); y = I128(0, BANE); ASSERT128(I128(2, BANE2), x ^ y); x = I128(2, 2); y = I128(0, -1ul); ASSERT128(I128(2, IMAX2), x ^ y); x = I128(2, 2); y = I128(2, 0); ASSERT128(I128(0, 2), x ^ y); x = I128(2, 2); y = I128(2, 2); ASSERT128(I128(0, 0), x ^ y); x = I128(2, 2); y = I128(2, BANE); ASSERT128(I128(0, BANE2), x ^ y); x = I128(2, 2); y = I128(2, -1ul); ASSERT128(I128(0, IMAX2), x ^ y); x = I128(2, 2); y = I128(BANE, 0); ASSERT128(I128(BANE2, 2), x ^ y); x = I128(2, 2); y = I128(BANE, 2); ASSERT128(I128(BANE2, 0), x ^ y); x = I128(2, 2); y = I128(BANE, BANE); ASSERT128(I128(BANE2, BANE2), x ^ y); x = I128(2, 2); y = I128(BANE, -1ul); ASSERT128(I128(BANE2, IMAX2), x ^ y); x = I128(2, 2); y = I128(-1ul, 0); ASSERT128(I128(IMAX2, 2), x ^ y); x = I128(2, 2); y = I128(-1ul, 2); ASSERT128(I128(IMAX2, 0), x ^ y); x = I128(2, 2); y = I128(-1ul, BANE); ASSERT128(I128(IMAX2, BANE2), x ^ y); x = I128(2, 2); y = I128(-1ul, -1ul); ASSERT128(I128(IMAX2, IMAX2), x ^ y); x = I128(2, BANE); y = I128(0, 2); ASSERT128(I128(2, BANE2), x ^ y); x = I128(2, BANE); y = I128(0, BANE); ASSERT128(I128(2, 0), x ^ y); x = I128(2, BANE); y = I128(0, -1ul); ASSERT128(I128(2, IMAX), x ^ y); x = I128(2, BANE); y = I128(2, 0); ASSERT128(I128(0, BANE), x ^ y); x = I128(2, BANE); y = I128(2, 2); ASSERT128(I128(0, BANE2), x ^ y); x = I128(2, BANE); y = I128(2, BANE); ASSERT128(I128(0, 0), x ^ y); x = I128(2, BANE); y = I128(2, -1ul); ASSERT128(I128(0, IMAX), x ^ y); x = I128(2, BANE); y = I128(BANE, 0); ASSERT128(I128(BANE2, BANE), x ^ y); x = I128(2, BANE); y = I128(BANE, 2); ASSERT128(I128(BANE2, BANE2), x ^ y); x = I128(2, BANE); y = I128(BANE, BANE); ASSERT128(I128(BANE2, 0), x ^ y); x = I128(2, BANE); y = I128(BANE, -1ul); ASSERT128(I128(BANE2, IMAX), x ^ y); x = I128(2, BANE); y = I128(-1ul, 0); ASSERT128(I128(IMAX2, BANE), x ^ y); x = I128(2, BANE); y = I128(-1ul, 2); ASSERT128(I128(IMAX2, BANE2), x ^ y); x = I128(2, BANE); y = I128(-1ul, BANE); ASSERT128(I128(IMAX2, 0), x ^ y); x = I128(2, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(IMAX2, IMAX), x ^ y); x = I128(2, -1ul); y = I128(0, 2); ASSERT128(I128(2, IMAX2), x ^ y); x = I128(2, -1ul); y = I128(0, BANE); ASSERT128(I128(2, IMAX), x ^ y); x = I128(2, -1ul); y = I128(0, -1ul); ASSERT128(I128(2, 0), x ^ y); x = I128(2, -1ul); y = I128(2, 0); ASSERT128(I128(0, -1ul), x ^ y); x = I128(2, -1ul); y = I128(2, 2); ASSERT128(I128(0, IMAX2), x ^ y); x = I128(2, -1ul); y = I128(2, BANE); ASSERT128(I128(0, IMAX), x ^ y); x = I128(2, -1ul); y = I128(2, -1ul); ASSERT128(I128(0, 0), x ^ y); x = I128(2, -1ul); y = I128(BANE, 0); ASSERT128(I128(BANE2, -1ul), x ^ y); x = I128(2, -1ul); y = I128(BANE, 2); ASSERT128(I128(BANE2, IMAX2), x ^ y); x = I128(2, -1ul); y = I128(BANE, BANE); ASSERT128(I128(BANE2, IMAX), x ^ y); x = I128(2, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(BANE2, 0), x ^ y); x = I128(2, -1ul); y = I128(-1ul, 0); ASSERT128(I128(IMAX2, -1ul), x ^ y); x = I128(2, -1ul); y = I128(-1ul, 2); ASSERT128(I128(IMAX2, IMAX2), x ^ y); x = I128(2, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(IMAX2, IMAX), x ^ y); x = I128(2, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(IMAX2, 0), x ^ y); x = I128(BANE, 0); y = I128(0, 2); ASSERT128(I128(BANE, 2), x ^ y); x = I128(BANE, 0); y = I128(0, BANE); ASSERT128(I128(BANE, BANE), x ^ y); x = I128(BANE, 0); y = I128(0, -1ul); ASSERT128(I128(BANE, -1ul), x ^ y); x = I128(BANE, 0); y = I128(2, 0); ASSERT128(I128(BANE2, 0), x ^ y); x = I128(BANE, 0); y = I128(2, 2); ASSERT128(I128(BANE2, 2), x ^ y); x = I128(BANE, 0); y = I128(2, BANE); ASSERT128(I128(BANE2, BANE), x ^ y); x = I128(BANE, 0); y = I128(2, -1ul); ASSERT128(I128(BANE2, -1ul), x ^ y); x = I128(BANE, 0); y = I128(BANE, 0); ASSERT128(I128(0, 0), x ^ y); x = I128(BANE, 0); y = I128(BANE, 2); ASSERT128(I128(0, 2), x ^ y); x = I128(BANE, 0); y = I128(BANE, BANE); ASSERT128(I128(0, BANE), x ^ y); x = I128(BANE, 0); y = I128(BANE, -1ul); ASSERT128(I128(0, -1ul), x ^ y); x = I128(BANE, 0); y = I128(-1ul, 0); ASSERT128(I128(IMAX, 0), x ^ y); x = I128(BANE, 0); y = I128(-1ul, 2); ASSERT128(I128(IMAX, 2), x ^ y); x = I128(BANE, 0); y = I128(-1ul, BANE); ASSERT128(I128(IMAX, BANE), x ^ y); x = I128(BANE, 2); y = I128(0, 2); ASSERT128(I128(BANE, 0), x ^ y); x = I128(BANE, 2); y = I128(0, BANE); ASSERT128(I128(BANE, BANE2), x ^ y); x = I128(BANE, 2); y = I128(0, -1ul); ASSERT128(I128(BANE, IMAX2), x ^ y); x = I128(BANE, 2); y = I128(2, 0); ASSERT128(I128(BANE2, 2), x ^ y); x = I128(BANE, 2); y = I128(2, 2); ASSERT128(I128(BANE2, 0), x ^ y); x = I128(BANE, 2); y = I128(2, BANE); ASSERT128(I128(BANE2, BANE2), x ^ y); x = I128(BANE, 2); y = I128(2, -1ul); ASSERT128(I128(BANE2, IMAX2), x ^ y); x = I128(BANE, 2); y = I128(BANE, 0); ASSERT128(I128(0, 2), x ^ y); x = I128(BANE, 2); y = I128(BANE, 2); ASSERT128(I128(0, 0), x ^ y); x = I128(BANE, 2); y = I128(BANE, BANE); ASSERT128(I128(0, BANE2), x ^ y); x = I128(BANE, 2); y = I128(BANE, -1ul); ASSERT128(I128(0, IMAX2), x ^ y); x = I128(BANE, 2); y = I128(-1ul, 0); ASSERT128(I128(IMAX, 2), x ^ y); x = I128(BANE, 2); y = I128(-1ul, 2); ASSERT128(I128(IMAX, 0), x ^ y); x = I128(BANE, 2); y = I128(-1ul, BANE); ASSERT128(I128(IMAX, BANE2), x ^ y); x = I128(BANE, 2); y = I128(-1ul, -1ul); ASSERT128(I128(IMAX, IMAX2), x ^ y); x = I128(BANE, BANE); y = I128(0, 2); ASSERT128(I128(BANE, BANE2), x ^ y); x = I128(BANE, BANE); y = I128(0, BANE); ASSERT128(I128(BANE, 0), x ^ y); x = I128(BANE, BANE); y = I128(0, -1ul); ASSERT128(I128(BANE, IMAX), x ^ y); x = I128(BANE, BANE); y = I128(2, 0); ASSERT128(I128(BANE2, BANE), x ^ y); x = I128(BANE, BANE); y = I128(2, 2); ASSERT128(I128(BANE2, BANE2), x ^ y); x = I128(BANE, BANE); y = I128(2, BANE); ASSERT128(I128(BANE2, 0), x ^ y); x = I128(BANE, BANE); y = I128(2, -1ul); ASSERT128(I128(BANE2, IMAX), x ^ y); x = I128(BANE, BANE); y = I128(BANE, 0); ASSERT128(I128(0, BANE), x ^ y); x = I128(BANE, BANE); y = I128(BANE, 2); ASSERT128(I128(0, BANE2), x ^ y); x = I128(BANE, BANE); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x ^ y); x = I128(BANE, BANE); y = I128(BANE, -1ul); ASSERT128(I128(0, IMAX), x ^ y); x = I128(BANE, BANE); y = I128(-1ul, 0); ASSERT128(I128(IMAX, BANE), x ^ y); x = I128(BANE, BANE); y = I128(-1ul, 2); ASSERT128(I128(IMAX, BANE2), x ^ y); x = I128(BANE, BANE); y = I128(-1ul, BANE); ASSERT128(I128(IMAX, 0), x ^ y); x = I128(BANE, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(IMAX, IMAX), x ^ y); x = I128(BANE, -1ul); y = I128(0, 2); ASSERT128(I128(BANE, IMAX2), x ^ y); x = I128(BANE, -1ul); y = I128(0, BANE); ASSERT128(I128(BANE, IMAX), x ^ y); x = I128(BANE, -1ul); y = I128(0, -1ul); ASSERT128(I128(BANE, 0), x ^ y); x = I128(BANE, -1ul); y = I128(2, 0); ASSERT128(I128(BANE2, -1ul), x ^ y); x = I128(BANE, -1ul); y = I128(2, 2); ASSERT128(I128(BANE2, IMAX2), x ^ y); x = I128(BANE, -1ul); y = I128(2, BANE); ASSERT128(I128(BANE2, IMAX), x ^ y); x = I128(BANE, -1ul); y = I128(2, -1ul); ASSERT128(I128(BANE2, 0), x ^ y); x = I128(BANE, -1ul); y = I128(BANE, 0); ASSERT128(I128(0, -1ul), x ^ y); x = I128(BANE, -1ul); y = I128(BANE, 2); ASSERT128(I128(0, IMAX2), x ^ y); x = I128(BANE, -1ul); y = I128(BANE, BANE); ASSERT128(I128(0, IMAX), x ^ y); x = I128(BANE, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x ^ y); x = I128(BANE, -1ul); y = I128(-1ul, 0); ASSERT128(I128(IMAX, -1ul), x ^ y); x = I128(BANE, -1ul); y = I128(-1ul, 2); ASSERT128(I128(IMAX, IMAX2), x ^ y); x = I128(BANE, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(IMAX, IMAX), x ^ y); x = I128(BANE, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(IMAX, 0), x ^ y); x = I128(-1ul, 0); y = I128(0, 2); ASSERT128(I128(-1ul, 2), x ^ y); x = I128(-1ul, 0); y = I128(0, BANE); ASSERT128(I128(-1ul, BANE), x ^ y); x = I128(-1ul, 0); y = I128(0, -1ul); ASSERT128(I128(-1ul, -1ul), x ^ y); x = I128(-1ul, 0); y = I128(2, 0); ASSERT128(I128(IMAX2, 0), x ^ y); x = I128(-1ul, 0); y = I128(2, 2); ASSERT128(I128(IMAX2, 2), x ^ y); x = I128(-1ul, 0); y = I128(2, BANE); ASSERT128(I128(IMAX2, BANE), x ^ y); x = I128(-1ul, 0); y = I128(2, -1ul); ASSERT128(I128(IMAX2, -1ul), x ^ y); x = I128(-1ul, 0); y = I128(BANE, 0); ASSERT128(I128(IMAX, 0), x ^ y); x = I128(-1ul, 0); y = I128(BANE, 2); ASSERT128(I128(IMAX, 2), x ^ y); x = I128(-1ul, 0); y = I128(BANE, BANE); ASSERT128(I128(IMAX, BANE), x ^ y); x = I128(-1ul, 0); y = I128(BANE, -1ul); ASSERT128(I128(IMAX, -1ul), x ^ y); x = I128(-1ul, 0); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x ^ y); x = I128(-1ul, 0); y = I128(-1ul, 2); ASSERT128(I128(0, 2), x ^ y); x = I128(-1ul, 0); y = I128(-1ul, BANE); ASSERT128(I128(0, BANE), x ^ y); x = I128(-1ul, 0); y = I128(-1ul, -1ul); ASSERT128(I128(0, -1ul), x ^ y); x = I128(-1ul, 2); y = I128(0, 2); ASSERT128(I128(-1ul, 0), x ^ y); x = I128(-1ul, 2); y = I128(0, BANE); ASSERT128(I128(-1ul, BANE2), x ^ y); x = I128(-1ul, 2); y = I128(0, -1ul); ASSERT128(I128(-1ul, IMAX2), x ^ y); x = I128(-1ul, 2); y = I128(2, 0); ASSERT128(I128(IMAX2, 2), x ^ y); x = I128(-1ul, 2); y = I128(2, 2); ASSERT128(I128(IMAX2, 0), x ^ y); x = I128(-1ul, 2); y = I128(2, BANE); ASSERT128(I128(IMAX2, BANE2), x ^ y); x = I128(-1ul, 2); y = I128(2, -1ul); ASSERT128(I128(IMAX2, IMAX2), x ^ y); x = I128(-1ul, 2); y = I128(BANE, 0); ASSERT128(I128(IMAX, 2), x ^ y); x = I128(-1ul, 2); y = I128(BANE, 2); ASSERT128(I128(IMAX, 0), x ^ y); x = I128(-1ul, 2); y = I128(BANE, BANE); ASSERT128(I128(IMAX, BANE2), x ^ y); x = I128(-1ul, 2); y = I128(BANE, -1ul); ASSERT128(I128(IMAX, IMAX2), x ^ y); x = I128(-1ul, 2); y = I128(-1ul, 0); ASSERT128(I128(0, 2), x ^ y); x = I128(-1ul, 2); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x ^ y); x = I128(-1ul, 2); y = I128(-1ul, BANE); ASSERT128(I128(0, BANE2), x ^ y); x = I128(-1ul, 2); y = I128(-1ul, -1ul); ASSERT128(I128(0, IMAX2), x ^ y); x = I128(-1ul, BANE); y = I128(0, 2); ASSERT128(I128(-1ul, BANE2), x ^ y); x = I128(-1ul, BANE); y = I128(0, BANE); ASSERT128(I128(-1ul, 0), x ^ y); x = I128(-1ul, BANE); y = I128(0, -1ul); ASSERT128(I128(-1ul, IMAX), x ^ y); x = I128(-1ul, BANE); y = I128(2, 0); ASSERT128(I128(IMAX2, BANE), x ^ y); x = I128(-1ul, BANE); y = I128(2, 2); ASSERT128(I128(IMAX2, BANE2), x ^ y); x = I128(-1ul, BANE); y = I128(2, BANE); ASSERT128(I128(IMAX2, 0), x ^ y); x = I128(-1ul, BANE); y = I128(2, -1ul); ASSERT128(I128(IMAX2, IMAX), x ^ y); x = I128(-1ul, BANE); y = I128(BANE, 0); ASSERT128(I128(IMAX, BANE), x ^ y); x = I128(-1ul, BANE); y = I128(BANE, 2); ASSERT128(I128(IMAX, BANE2), x ^ y); x = I128(-1ul, BANE); y = I128(BANE, BANE); ASSERT128(I128(IMAX, 0), x ^ y); x = I128(-1ul, BANE); y = I128(BANE, -1ul); ASSERT128(I128(IMAX, IMAX), x ^ y); x = I128(-1ul, BANE); y = I128(-1ul, 0); ASSERT128(I128(0, BANE), x ^ y); x = I128(-1ul, BANE); y = I128(-1ul, 2); ASSERT128(I128(0, BANE2), x ^ y); x = I128(-1ul, BANE); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x ^ y); x = I128(-1ul, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(0, IMAX), x ^ y); x = I128(-1ul, -1ul); y = I128(0, 2); ASSERT128(I128(-1ul, IMAX2), x ^ y); x = I128(-1ul, -1ul); y = I128(0, BANE); ASSERT128(I128(-1ul, IMAX), x ^ y); x = I128(-1ul, -1ul); y = I128(0, -1ul); ASSERT128(I128(-1ul, 0), x ^ y); x = I128(-1ul, -1ul); y = I128(2, 0); ASSERT128(I128(IMAX2, -1ul), x ^ y); x = I128(-1ul, -1ul); y = I128(2, 2); ASSERT128(I128(IMAX2, IMAX2), x ^ y); x = I128(-1ul, -1ul); y = I128(2, BANE); ASSERT128(I128(IMAX2, IMAX), x ^ y); x = I128(-1ul, -1ul); y = I128(2, -1ul); ASSERT128(I128(IMAX2, 0), x ^ y); x = I128(-1ul, -1ul); y = I128(BANE, 0); ASSERT128(I128(IMAX, -1ul), x ^ y); x = I128(-1ul, -1ul); y = I128(BANE, 2); ASSERT128(I128(IMAX, IMAX2), x ^ y); x = I128(-1ul, -1ul); y = I128(BANE, BANE); ASSERT128(I128(IMAX, IMAX), x ^ y); x = I128(-1ul, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(IMAX, 0), x ^ y); x = I128(-1ul, -1ul); y = I128(-1ul, 0); ASSERT128(I128(0, -1ul), x ^ y); x = I128(-1ul, -1ul); y = I128(-1ul, 2); ASSERT128(I128(0, IMAX2), x ^ y); x = I128(-1ul, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(0, IMAX), x ^ y); x = I128(-1ul, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x ^ y); } void testAnd128(void) { __int128 x, y; x = I128(0, 0); y = I128(0, 2); ASSERT128(I128(0, 0), x & y); x = I128(0, 0); y = I128(0, BANE); ASSERT128(I128(0, 0), x & y); x = I128(0, 0); y = I128(0, -1ul); ASSERT128(I128(0, 0), x & y); x = I128(0, 0); y = I128(2, 0); ASSERT128(I128(0, 0), x & y); x = I128(0, 0); y = I128(2, 2); ASSERT128(I128(0, 0), x & y); x = I128(0, 0); y = I128(2, BANE); ASSERT128(I128(0, 0), x & y); x = I128(0, 0); y = I128(2, -1ul); ASSERT128(I128(0, 0), x & y); x = I128(0, 0); y = I128(BANE, 0); ASSERT128(I128(0, 0), x & y); x = I128(0, 0); y = I128(BANE, 2); ASSERT128(I128(0, 0), x & y); x = I128(0, 0); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x & y); x = I128(0, 0); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x & y); x = I128(0, 0); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x & y); x = I128(0, 0); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x & y); x = I128(0, 0); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x & y); x = I128(0, 0); y = I128(-1ul, -1ul); ASSERT128(I128(0, 0), x & y); x = I128(0, 2); y = I128(0, 2); ASSERT128(I128(0, 2), x & y); x = I128(0, 2); y = I128(0, BANE); ASSERT128(I128(0, 0), x & y); x = I128(0, 2); y = I128(0, -1ul); ASSERT128(I128(0, 2), x & y); x = I128(0, 2); y = I128(2, 0); ASSERT128(I128(0, 0), x & y); x = I128(0, 2); y = I128(2, 2); ASSERT128(I128(0, 2), x & y); x = I128(0, 2); y = I128(2, BANE); ASSERT128(I128(0, 0), x & y); x = I128(0, 2); y = I128(2, -1ul); ASSERT128(I128(0, 2), x & y); x = I128(0, 2); y = I128(BANE, 0); ASSERT128(I128(0, 0), x & y); x = I128(0, 2); y = I128(BANE, 2); ASSERT128(I128(0, 2), x & y); x = I128(0, 2); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x & y); x = I128(0, 2); y = I128(BANE, -1ul); ASSERT128(I128(0, 2), x & y); x = I128(0, 2); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x & y); x = I128(0, 2); y = I128(-1ul, 2); ASSERT128(I128(0, 2), x & y); x = I128(0, 2); y = I128(-1ul, BANE); ASSERT128(I128(0, 0), x & y); x = I128(0, 2); y = I128(-1ul, -1ul); ASSERT128(I128(0, 2), x & y); x = I128(0, BANE); y = I128(0, 2); ASSERT128(I128(0, 0), x & y); x = I128(0, BANE); y = I128(0, BANE); ASSERT128(I128(0, BANE), x & y); x = I128(0, BANE); y = I128(0, -1ul); ASSERT128(I128(0, BANE), x & y); x = I128(0, BANE); y = I128(2, 0); ASSERT128(I128(0, 0), x & y); x = I128(0, BANE); y = I128(2, 2); ASSERT128(I128(0, 0), x & y); x = I128(0, BANE); y = I128(2, BANE); ASSERT128(I128(0, BANE), x & y); x = I128(0, BANE); y = I128(2, -1ul); ASSERT128(I128(0, BANE), x & y); x = I128(0, BANE); y = I128(BANE, 0); ASSERT128(I128(0, 0), x & y); x = I128(0, BANE); y = I128(BANE, 2); ASSERT128(I128(0, 0), x & y); x = I128(0, BANE); y = I128(BANE, BANE); ASSERT128(I128(0, BANE), x & y); x = I128(0, BANE); y = I128(BANE, -1ul); ASSERT128(I128(0, BANE), x & y); x = I128(0, BANE); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x & y); x = I128(0, BANE); y = I128(-1ul, 2); ASSERT128(I128(0, 0), x & y); x = I128(0, BANE); y = I128(-1ul, BANE); ASSERT128(I128(0, BANE), x & y); x = I128(0, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(0, BANE), x & y); x = I128(0, -1ul); y = I128(0, 2); ASSERT128(I128(0, 2), x & y); x = I128(0, -1ul); y = I128(0, BANE); ASSERT128(I128(0, BANE), x & y); x = I128(0, -1ul); y = I128(0, -1ul); ASSERT128(I128(0, -1ul), x & y); x = I128(0, -1ul); y = I128(2, 0); ASSERT128(I128(0, 0), x & y); x = I128(0, -1ul); y = I128(2, 2); ASSERT128(I128(0, 2), x & y); x = I128(0, -1ul); y = I128(2, BANE); ASSERT128(I128(0, BANE), x & y); x = I128(0, -1ul); y = I128(2, -1ul); ASSERT128(I128(0, -1ul), x & y); x = I128(0, -1ul); y = I128(BANE, 0); ASSERT128(I128(0, 0), x & y); x = I128(0, -1ul); y = I128(BANE, 2); ASSERT128(I128(0, 2), x & y); x = I128(0, -1ul); y = I128(BANE, BANE); ASSERT128(I128(0, BANE), x & y); x = I128(0, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(0, -1ul), x & y); x = I128(0, -1ul); y = I128(-1ul, 0); ASSERT128(I128(0, 0), x & y); x = I128(0, -1ul); y = I128(-1ul, 2); ASSERT128(I128(0, 2), x & y); x = I128(0, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(0, BANE), x & y); x = I128(0, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(0, -1ul), x & y); x = I128(2, 0); y = I128(0, 2); ASSERT128(I128(0, 0), x & y); x = I128(2, 0); y = I128(0, BANE); ASSERT128(I128(0, 0), x & y); x = I128(2, 0); y = I128(0, -1ul); ASSERT128(I128(0, 0), x & y); x = I128(2, 0); y = I128(2, 0); ASSERT128(I128(2, 0), x & y); x = I128(2, 0); y = I128(2, 2); ASSERT128(I128(2, 0), x & y); x = I128(2, 0); y = I128(2, BANE); ASSERT128(I128(2, 0), x & y); x = I128(2, 0); y = I128(2, -1ul); ASSERT128(I128(2, 0), x & y); x = I128(2, 0); y = I128(BANE, 0); ASSERT128(I128(0, 0), x & y); x = I128(2, 0); y = I128(BANE, 2); ASSERT128(I128(0, 0), x & y); x = I128(2, 0); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x & y); x = I128(2, 0); y = I128(BANE, -1ul); ASSERT128(I128(0, 0), x & y); x = I128(2, 0); y = I128(-1ul, 0); ASSERT128(I128(2, 0), x & y); x = I128(2, 0); y = I128(-1ul, 2); ASSERT128(I128(2, 0), x & y); x = I128(2, 0); y = I128(-1ul, BANE); ASSERT128(I128(2, 0), x & y); x = I128(2, 0); y = I128(-1ul, -1ul); ASSERT128(I128(2, 0), x & y); x = I128(2, 2); y = I128(0, 2); ASSERT128(I128(0, 2), x & y); x = I128(2, 2); y = I128(0, BANE); ASSERT128(I128(0, 0), x & y); x = I128(2, 2); y = I128(0, -1ul); ASSERT128(I128(0, 2), x & y); x = I128(2, 2); y = I128(2, 0); ASSERT128(I128(2, 0), x & y); x = I128(2, 2); y = I128(2, 2); ASSERT128(I128(2, 2), x & y); x = I128(2, 2); y = I128(2, BANE); ASSERT128(I128(2, 0), x & y); x = I128(2, 2); y = I128(2, -1ul); ASSERT128(I128(2, 2), x & y); x = I128(2, 2); y = I128(BANE, 0); ASSERT128(I128(0, 0), x & y); x = I128(2, 2); y = I128(BANE, 2); ASSERT128(I128(0, 2), x & y); x = I128(2, 2); y = I128(BANE, BANE); ASSERT128(I128(0, 0), x & y); x = I128(2, 2); y = I128(BANE, -1ul); ASSERT128(I128(0, 2), x & y); x = I128(2, 2); y = I128(-1ul, 0); ASSERT128(I128(2, 0), x & y); x = I128(2, 2); y = I128(-1ul, 2); ASSERT128(I128(2, 2), x & y); x = I128(2, 2); y = I128(-1ul, BANE); ASSERT128(I128(2, 0), x & y); x = I128(2, 2); y = I128(-1ul, -1ul); ASSERT128(I128(2, 2), x & y); x = I128(2, BANE); y = I128(0, 2); ASSERT128(I128(0, 0), x & y); x = I128(2, BANE); y = I128(0, BANE); ASSERT128(I128(0, BANE), x & y); x = I128(2, BANE); y = I128(0, -1ul); ASSERT128(I128(0, BANE), x & y); x = I128(2, BANE); y = I128(2, 0); ASSERT128(I128(2, 0), x & y); x = I128(2, BANE); y = I128(2, 2); ASSERT128(I128(2, 0), x & y); x = I128(2, BANE); y = I128(2, BANE); ASSERT128(I128(2, BANE), x & y); x = I128(2, BANE); y = I128(2, -1ul); ASSERT128(I128(2, BANE), x & y); x = I128(2, BANE); y = I128(BANE, 0); ASSERT128(I128(0, 0), x & y); x = I128(2, BANE); y = I128(BANE, 2); ASSERT128(I128(0, 0), x & y); x = I128(2, BANE); y = I128(BANE, BANE); ASSERT128(I128(0, BANE), x & y); x = I128(2, BANE); y = I128(BANE, -1ul); ASSERT128(I128(0, BANE), x & y); x = I128(2, BANE); y = I128(-1ul, 0); ASSERT128(I128(2, 0), x & y); x = I128(2, BANE); y = I128(-1ul, 2); ASSERT128(I128(2, 0), x & y); x = I128(2, BANE); y = I128(-1ul, BANE); ASSERT128(I128(2, BANE), x & y); x = I128(2, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(2, BANE), x & y); x = I128(2, -1ul); y = I128(0, 2); ASSERT128(I128(0, 2), x & y); x = I128(2, -1ul); y = I128(0, BANE); ASSERT128(I128(0, BANE), x & y); x = I128(2, -1ul); y = I128(0, -1ul); ASSERT128(I128(0, -1ul), x & y); x = I128(2, -1ul); y = I128(2, 0); ASSERT128(I128(2, 0), x & y); x = I128(2, -1ul); y = I128(2, 2); ASSERT128(I128(2, 2), x & y); x = I128(2, -1ul); y = I128(2, BANE); ASSERT128(I128(2, BANE), x & y); x = I128(2, -1ul); y = I128(2, -1ul); ASSERT128(I128(2, -1ul), x & y); x = I128(2, -1ul); y = I128(BANE, 0); ASSERT128(I128(0, 0), x & y); x = I128(2, -1ul); y = I128(BANE, 2); ASSERT128(I128(0, 2), x & y); x = I128(2, -1ul); y = I128(BANE, BANE); ASSERT128(I128(0, BANE), x & y); x = I128(2, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(0, -1ul), x & y); x = I128(2, -1ul); y = I128(-1ul, 0); ASSERT128(I128(2, 0), x & y); x = I128(2, -1ul); y = I128(-1ul, 2); ASSERT128(I128(2, 2), x & y); x = I128(2, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(2, BANE), x & y); x = I128(2, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(2, -1ul), x & y); x = I128(BANE, 0); y = I128(0, 2); ASSERT128(I128(0, 0), x & y); x = I128(BANE, 0); y = I128(0, BANE); ASSERT128(I128(0, 0), x & y); x = I128(BANE, 0); y = I128(0, -1ul); ASSERT128(I128(0, 0), x & y); x = I128(BANE, 0); y = I128(2, 0); ASSERT128(I128(0, 0), x & y); x = I128(BANE, 0); y = I128(2, 2); ASSERT128(I128(0, 0), x & y); x = I128(BANE, 0); y = I128(2, BANE); ASSERT128(I128(0, 0), x & y); x = I128(BANE, 0); y = I128(2, -1ul); ASSERT128(I128(0, 0), x & y); x = I128(BANE, 0); y = I128(BANE, 0); ASSERT128(I128(BANE, 0), x & y); x = I128(BANE, 0); y = I128(BANE, 2); ASSERT128(I128(BANE, 0), x & y); x = I128(BANE, 0); y = I128(BANE, BANE); ASSERT128(I128(BANE, 0), x & y); x = I128(BANE, 0); y = I128(BANE, -1ul); ASSERT128(I128(BANE, 0), x & y); x = I128(BANE, 0); y = I128(-1ul, 0); ASSERT128(I128(BANE, 0), x & y); x = I128(BANE, 0); y = I128(-1ul, 2); ASSERT128(I128(BANE, 0), x & y); x = I128(BANE, 0); y = I128(-1ul, BANE); ASSERT128(I128(BANE, 0), x & y); x = I128(BANE, 2); y = I128(0, 2); ASSERT128(I128(0, 2), x & y); x = I128(BANE, 2); y = I128(0, BANE); ASSERT128(I128(0, 0), x & y); x = I128(BANE, 2); y = I128(0, -1ul); ASSERT128(I128(0, 2), x & y); x = I128(BANE, 2); y = I128(2, 0); ASSERT128(I128(0, 0), x & y); x = I128(BANE, 2); y = I128(2, 2); ASSERT128(I128(0, 2), x & y); x = I128(BANE, 2); y = I128(2, BANE); ASSERT128(I128(0, 0), x & y); x = I128(BANE, 2); y = I128(2, -1ul); ASSERT128(I128(0, 2), x & y); x = I128(BANE, 2); y = I128(BANE, 0); ASSERT128(I128(BANE, 0), x & y); x = I128(BANE, 2); y = I128(BANE, 2); ASSERT128(I128(BANE, 2), x & y); x = I128(BANE, 2); y = I128(BANE, BANE); ASSERT128(I128(BANE, 0), x & y); x = I128(BANE, 2); y = I128(BANE, -1ul); ASSERT128(I128(BANE, 2), x & y); x = I128(BANE, 2); y = I128(-1ul, 0); ASSERT128(I128(BANE, 0), x & y); x = I128(BANE, 2); y = I128(-1ul, 2); ASSERT128(I128(BANE, 2), x & y); x = I128(BANE, 2); y = I128(-1ul, BANE); ASSERT128(I128(BANE, 0), x & y); x = I128(BANE, 2); y = I128(-1ul, -1ul); ASSERT128(I128(BANE, 2), x & y); x = I128(BANE, BANE); y = I128(0, 2); ASSERT128(I128(0, 0), x & y); x = I128(BANE, BANE); y = I128(0, BANE); ASSERT128(I128(0, BANE), x & y); x = I128(BANE, BANE); y = I128(0, -1ul); ASSERT128(I128(0, BANE), x & y); x = I128(BANE, BANE); y = I128(2, 0); ASSERT128(I128(0, 0), x & y); x = I128(BANE, BANE); y = I128(2, 2); ASSERT128(I128(0, 0), x & y); x = I128(BANE, BANE); y = I128(2, BANE); ASSERT128(I128(0, BANE), x & y); x = I128(BANE, BANE); y = I128(2, -1ul); ASSERT128(I128(0, BANE), x & y); x = I128(BANE, BANE); y = I128(BANE, 0); ASSERT128(I128(BANE, 0), x & y); x = I128(BANE, BANE); y = I128(BANE, 2); ASSERT128(I128(BANE, 0), x & y); x = I128(BANE, BANE); y = I128(BANE, BANE); ASSERT128(I128(BANE, BANE), x & y); x = I128(BANE, BANE); y = I128(BANE, -1ul); ASSERT128(I128(BANE, BANE), x & y); x = I128(BANE, BANE); y = I128(-1ul, 0); ASSERT128(I128(BANE, 0), x & y); x = I128(BANE, BANE); y = I128(-1ul, 2); ASSERT128(I128(BANE, 0), x & y); x = I128(BANE, BANE); y = I128(-1ul, BANE); ASSERT128(I128(BANE, BANE), x & y); x = I128(BANE, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(BANE, BANE), x & y); x = I128(BANE, -1ul); y = I128(0, 2); ASSERT128(I128(0, 2), x & y); x = I128(BANE, -1ul); y = I128(0, BANE); ASSERT128(I128(0, BANE), x & y); x = I128(BANE, -1ul); y = I128(0, -1ul); ASSERT128(I128(0, -1ul), x & y); x = I128(BANE, -1ul); y = I128(2, 0); ASSERT128(I128(0, 0), x & y); x = I128(BANE, -1ul); y = I128(2, 2); ASSERT128(I128(0, 2), x & y); x = I128(BANE, -1ul); y = I128(2, BANE); ASSERT128(I128(0, BANE), x & y); x = I128(BANE, -1ul); y = I128(2, -1ul); ASSERT128(I128(0, -1ul), x & y); x = I128(BANE, -1ul); y = I128(BANE, 0); ASSERT128(I128(BANE, 0), x & y); x = I128(BANE, -1ul); y = I128(BANE, 2); ASSERT128(I128(BANE, 2), x & y); x = I128(BANE, -1ul); y = I128(BANE, BANE); ASSERT128(I128(BANE, BANE), x & y); x = I128(BANE, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(BANE, -1ul), x & y); x = I128(BANE, -1ul); y = I128(-1ul, 0); ASSERT128(I128(BANE, 0), x & y); x = I128(BANE, -1ul); y = I128(-1ul, 2); ASSERT128(I128(BANE, 2), x & y); x = I128(BANE, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(BANE, BANE), x & y); x = I128(BANE, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(BANE, -1ul), x & y); x = I128(-1ul, 0); y = I128(0, 2); ASSERT128(I128(0, 0), x & y); x = I128(-1ul, 0); y = I128(0, BANE); ASSERT128(I128(0, 0), x & y); x = I128(-1ul, 0); y = I128(0, -1ul); ASSERT128(I128(0, 0), x & y); x = I128(-1ul, 0); y = I128(2, 0); ASSERT128(I128(2, 0), x & y); x = I128(-1ul, 0); y = I128(2, 2); ASSERT128(I128(2, 0), x & y); x = I128(-1ul, 0); y = I128(2, BANE); ASSERT128(I128(2, 0), x & y); x = I128(-1ul, 0); y = I128(2, -1ul); ASSERT128(I128(2, 0), x & y); x = I128(-1ul, 0); y = I128(BANE, 0); ASSERT128(I128(BANE, 0), x & y); x = I128(-1ul, 0); y = I128(BANE, 2); ASSERT128(I128(BANE, 0), x & y); x = I128(-1ul, 0); y = I128(BANE, BANE); ASSERT128(I128(BANE, 0), x & y); x = I128(-1ul, 0); y = I128(BANE, -1ul); ASSERT128(I128(BANE, 0), x & y); x = I128(-1ul, 0); y = I128(-1ul, 0); ASSERT128(I128(-1ul, 0), x & y); x = I128(-1ul, 0); y = I128(-1ul, 2); ASSERT128(I128(-1ul, 0), x & y); x = I128(-1ul, 0); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, 0), x & y); x = I128(-1ul, 0); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, 0), x & y); x = I128(-1ul, 2); y = I128(0, 2); ASSERT128(I128(0, 2), x & y); x = I128(-1ul, 2); y = I128(0, BANE); ASSERT128(I128(0, 0), x & y); x = I128(-1ul, 2); y = I128(0, -1ul); ASSERT128(I128(0, 2), x & y); x = I128(-1ul, 2); y = I128(2, 0); ASSERT128(I128(2, 0), x & y); x = I128(-1ul, 2); y = I128(2, 2); ASSERT128(I128(2, 2), x & y); x = I128(-1ul, 2); y = I128(2, BANE); ASSERT128(I128(2, 0), x & y); x = I128(-1ul, 2); y = I128(2, -1ul); ASSERT128(I128(2, 2), x & y); x = I128(-1ul, 2); y = I128(BANE, 0); ASSERT128(I128(BANE, 0), x & y); x = I128(-1ul, 2); y = I128(BANE, 2); ASSERT128(I128(BANE, 2), x & y); x = I128(-1ul, 2); y = I128(BANE, BANE); ASSERT128(I128(BANE, 0), x & y); x = I128(-1ul, 2); y = I128(BANE, -1ul); ASSERT128(I128(BANE, 2), x & y); x = I128(-1ul, 2); y = I128(-1ul, 0); ASSERT128(I128(-1ul, 0), x & y); x = I128(-1ul, 2); y = I128(-1ul, 2); ASSERT128(I128(-1ul, 2), x & y); x = I128(-1ul, 2); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, 0), x & y); x = I128(-1ul, 2); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, 2), x & y); x = I128(-1ul, BANE); y = I128(0, 2); ASSERT128(I128(0, 0), x & y); x = I128(-1ul, BANE); y = I128(0, BANE); ASSERT128(I128(0, BANE), x & y); x = I128(-1ul, BANE); y = I128(0, -1ul); ASSERT128(I128(0, BANE), x & y); x = I128(-1ul, BANE); y = I128(2, 0); ASSERT128(I128(2, 0), x & y); x = I128(-1ul, BANE); y = I128(2, 2); ASSERT128(I128(2, 0), x & y); x = I128(-1ul, BANE); y = I128(2, BANE); ASSERT128(I128(2, BANE), x & y); x = I128(-1ul, BANE); y = I128(2, -1ul); ASSERT128(I128(2, BANE), x & y); x = I128(-1ul, BANE); y = I128(BANE, 0); ASSERT128(I128(BANE, 0), x & y); x = I128(-1ul, BANE); y = I128(BANE, 2); ASSERT128(I128(BANE, 0), x & y); x = I128(-1ul, BANE); y = I128(BANE, BANE); ASSERT128(I128(BANE, BANE), x & y); x = I128(-1ul, BANE); y = I128(BANE, -1ul); ASSERT128(I128(BANE, BANE), x & y); x = I128(-1ul, BANE); y = I128(-1ul, 0); ASSERT128(I128(-1ul, 0), x & y); x = I128(-1ul, BANE); y = I128(-1ul, 2); ASSERT128(I128(-1ul, 0), x & y); x = I128(-1ul, BANE); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, BANE), x & y); x = I128(-1ul, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, BANE), x & y); x = I128(-1ul, -1ul); y = I128(0, 2); ASSERT128(I128(0, 2), x & y); x = I128(-1ul, -1ul); y = I128(0, BANE); ASSERT128(I128(0, BANE), x & y); x = I128(-1ul, -1ul); y = I128(0, -1ul); ASSERT128(I128(0, -1ul), x & y); x = I128(-1ul, -1ul); y = I128(2, 0); ASSERT128(I128(2, 0), x & y); x = I128(-1ul, -1ul); y = I128(2, 2); ASSERT128(I128(2, 2), x & y); x = I128(-1ul, -1ul); y = I128(2, BANE); ASSERT128(I128(2, BANE), x & y); x = I128(-1ul, -1ul); y = I128(2, -1ul); ASSERT128(I128(2, -1ul), x & y); x = I128(-1ul, -1ul); y = I128(BANE, 0); ASSERT128(I128(BANE, 0), x & y); x = I128(-1ul, -1ul); y = I128(BANE, 2); ASSERT128(I128(BANE, 2), x & y); x = I128(-1ul, -1ul); y = I128(BANE, BANE); ASSERT128(I128(BANE, BANE), x & y); x = I128(-1ul, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(BANE, -1ul), x & y); x = I128(-1ul, -1ul); y = I128(-1ul, 0); ASSERT128(I128(-1ul, 0), x & y); x = I128(-1ul, -1ul); y = I128(-1ul, 2); ASSERT128(I128(-1ul, 2), x & y); x = I128(-1ul, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, BANE), x & y); x = I128(-1ul, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, -1ul), x & y); } void testOr128(void) { __int128 x, y; x = I128(0, 0); y = I128(0, 2); ASSERT128(I128(0, 2), x | y); x = I128(0, 0); y = I128(0, BANE); ASSERT128(I128(0, BANE), x | y); x = I128(0, 0); y = I128(0, -1ul); ASSERT128(I128(0, -1ul), x | y); x = I128(0, 0); y = I128(2, 0); ASSERT128(I128(2, 0), x | y); x = I128(0, 0); y = I128(2, 2); ASSERT128(I128(2, 2), x | y); x = I128(0, 0); y = I128(2, BANE); ASSERT128(I128(2, BANE), x | y); x = I128(0, 0); y = I128(2, -1ul); ASSERT128(I128(2, -1ul), x | y); x = I128(0, 0); y = I128(BANE, 0); ASSERT128(I128(BANE, 0), x | y); x = I128(0, 0); y = I128(BANE, 2); ASSERT128(I128(BANE, 2), x | y); x = I128(0, 0); y = I128(BANE, BANE); ASSERT128(I128(BANE, BANE), x | y); x = I128(0, 0); y = I128(BANE, -1ul); ASSERT128(I128(BANE, -1ul), x | y); x = I128(0, 0); y = I128(-1ul, 0); ASSERT128(I128(-1ul, 0), x | y); x = I128(0, 0); y = I128(-1ul, 2); ASSERT128(I128(-1ul, 2), x | y); x = I128(0, 0); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, BANE), x | y); x = I128(0, 0); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(0, 2); y = I128(0, 2); ASSERT128(I128(0, 2), x | y); x = I128(0, 2); y = I128(0, BANE); ASSERT128(I128(0, BANE2), x | y); x = I128(0, 2); y = I128(0, -1ul); ASSERT128(I128(0, -1ul), x | y); x = I128(0, 2); y = I128(2, 0); ASSERT128(I128(2, 2), x | y); x = I128(0, 2); y = I128(2, 2); ASSERT128(I128(2, 2), x | y); x = I128(0, 2); y = I128(2, BANE); ASSERT128(I128(2, BANE2), x | y); x = I128(0, 2); y = I128(2, -1ul); ASSERT128(I128(2, -1ul), x | y); x = I128(0, 2); y = I128(BANE, 0); ASSERT128(I128(BANE, 2), x | y); x = I128(0, 2); y = I128(BANE, 2); ASSERT128(I128(BANE, 2), x | y); x = I128(0, 2); y = I128(BANE, BANE); ASSERT128(I128(BANE, BANE2), x | y); x = I128(0, 2); y = I128(BANE, -1ul); ASSERT128(I128(BANE, -1ul), x | y); x = I128(0, 2); y = I128(-1ul, 0); ASSERT128(I128(-1ul, 2), x | y); x = I128(0, 2); y = I128(-1ul, 2); ASSERT128(I128(-1ul, 2), x | y); x = I128(0, 2); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, BANE2), x | y); x = I128(0, 2); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(0, BANE); y = I128(0, 2); ASSERT128(I128(0, BANE2), x | y); x = I128(0, BANE); y = I128(0, BANE); ASSERT128(I128(0, BANE), x | y); x = I128(0, BANE); y = I128(0, -1ul); ASSERT128(I128(0, -1ul), x | y); x = I128(0, BANE); y = I128(2, 0); ASSERT128(I128(2, BANE), x | y); x = I128(0, BANE); y = I128(2, 2); ASSERT128(I128(2, BANE2), x | y); x = I128(0, BANE); y = I128(2, BANE); ASSERT128(I128(2, BANE), x | y); x = I128(0, BANE); y = I128(2, -1ul); ASSERT128(I128(2, -1ul), x | y); x = I128(0, BANE); y = I128(BANE, 0); ASSERT128(I128(BANE, BANE), x | y); x = I128(0, BANE); y = I128(BANE, 2); ASSERT128(I128(BANE, BANE2), x | y); x = I128(0, BANE); y = I128(BANE, BANE); ASSERT128(I128(BANE, BANE), x | y); x = I128(0, BANE); y = I128(BANE, -1ul); ASSERT128(I128(BANE, -1ul), x | y); x = I128(0, BANE); y = I128(-1ul, 0); ASSERT128(I128(-1ul, BANE), x | y); x = I128(0, BANE); y = I128(-1ul, 2); ASSERT128(I128(-1ul, BANE2), x | y); x = I128(0, BANE); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, BANE), x | y); x = I128(0, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(0, -1ul); y = I128(0, 2); ASSERT128(I128(0, -1ul), x | y); x = I128(0, -1ul); y = I128(0, BANE); ASSERT128(I128(0, -1ul), x | y); x = I128(0, -1ul); y = I128(0, -1ul); ASSERT128(I128(0, -1ul), x | y); x = I128(0, -1ul); y = I128(2, 0); ASSERT128(I128(2, -1ul), x | y); x = I128(0, -1ul); y = I128(2, 2); ASSERT128(I128(2, -1ul), x | y); x = I128(0, -1ul); y = I128(2, BANE); ASSERT128(I128(2, -1ul), x | y); x = I128(0, -1ul); y = I128(2, -1ul); ASSERT128(I128(2, -1ul), x | y); x = I128(0, -1ul); y = I128(BANE, 0); ASSERT128(I128(BANE, -1ul), x | y); x = I128(0, -1ul); y = I128(BANE, 2); ASSERT128(I128(BANE, -1ul), x | y); x = I128(0, -1ul); y = I128(BANE, BANE); ASSERT128(I128(BANE, -1ul), x | y); x = I128(0, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(BANE, -1ul), x | y); x = I128(0, -1ul); y = I128(-1ul, 0); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(0, -1ul); y = I128(-1ul, 2); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(0, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(0, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(2, 0); y = I128(0, 2); ASSERT128(I128(2, 2), x | y); x = I128(2, 0); y = I128(0, BANE); ASSERT128(I128(2, BANE), x | y); x = I128(2, 0); y = I128(0, -1ul); ASSERT128(I128(2, -1ul), x | y); x = I128(2, 0); y = I128(2, 0); ASSERT128(I128(2, 0), x | y); x = I128(2, 0); y = I128(2, 2); ASSERT128(I128(2, 2), x | y); x = I128(2, 0); y = I128(2, BANE); ASSERT128(I128(2, BANE), x | y); x = I128(2, 0); y = I128(2, -1ul); ASSERT128(I128(2, -1ul), x | y); x = I128(2, 0); y = I128(BANE, 0); ASSERT128(I128(BANE2, 0), x | y); x = I128(2, 0); y = I128(BANE, 2); ASSERT128(I128(BANE2, 2), x | y); x = I128(2, 0); y = I128(BANE, BANE); ASSERT128(I128(BANE2, BANE), x | y); x = I128(2, 0); y = I128(BANE, -1ul); ASSERT128(I128(BANE2, -1ul), x | y); x = I128(2, 0); y = I128(-1ul, 0); ASSERT128(I128(-1ul, 0), x | y); x = I128(2, 0); y = I128(-1ul, 2); ASSERT128(I128(-1ul, 2), x | y); x = I128(2, 0); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, BANE), x | y); x = I128(2, 0); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(2, 2); y = I128(0, 2); ASSERT128(I128(2, 2), x | y); x = I128(2, 2); y = I128(0, BANE); ASSERT128(I128(2, BANE2), x | y); x = I128(2, 2); y = I128(0, -1ul); ASSERT128(I128(2, -1ul), x | y); x = I128(2, 2); y = I128(2, 0); ASSERT128(I128(2, 2), x | y); x = I128(2, 2); y = I128(2, 2); ASSERT128(I128(2, 2), x | y); x = I128(2, 2); y = I128(2, BANE); ASSERT128(I128(2, BANE2), x | y); x = I128(2, 2); y = I128(2, -1ul); ASSERT128(I128(2, -1ul), x | y); x = I128(2, 2); y = I128(BANE, 0); ASSERT128(I128(BANE2, 2), x | y); x = I128(2, 2); y = I128(BANE, 2); ASSERT128(I128(BANE2, 2), x | y); x = I128(2, 2); y = I128(BANE, BANE); ASSERT128(I128(BANE2, BANE2), x | y); x = I128(2, 2); y = I128(BANE, -1ul); ASSERT128(I128(BANE2, -1ul), x | y); x = I128(2, 2); y = I128(-1ul, 0); ASSERT128(I128(-1ul, 2), x | y); x = I128(2, 2); y = I128(-1ul, 2); ASSERT128(I128(-1ul, 2), x | y); x = I128(2, 2); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, BANE2), x | y); x = I128(2, 2); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(2, BANE); y = I128(0, 2); ASSERT128(I128(2, BANE2), x | y); x = I128(2, BANE); y = I128(0, BANE); ASSERT128(I128(2, BANE), x | y); x = I128(2, BANE); y = I128(0, -1ul); ASSERT128(I128(2, -1ul), x | y); x = I128(2, BANE); y = I128(2, 0); ASSERT128(I128(2, BANE), x | y); x = I128(2, BANE); y = I128(2, 2); ASSERT128(I128(2, BANE2), x | y); x = I128(2, BANE); y = I128(2, BANE); ASSERT128(I128(2, BANE), x | y); x = I128(2, BANE); y = I128(2, -1ul); ASSERT128(I128(2, -1ul), x | y); x = I128(2, BANE); y = I128(BANE, 0); ASSERT128(I128(BANE2, BANE), x | y); x = I128(2, BANE); y = I128(BANE, 2); ASSERT128(I128(BANE2, BANE2), x | y); x = I128(2, BANE); y = I128(BANE, BANE); ASSERT128(I128(BANE2, BANE), x | y); x = I128(2, BANE); y = I128(BANE, -1ul); ASSERT128(I128(BANE2, -1ul), x | y); x = I128(2, BANE); y = I128(-1ul, 0); ASSERT128(I128(-1ul, BANE), x | y); x = I128(2, BANE); y = I128(-1ul, 2); ASSERT128(I128(-1ul, BANE2), x | y); x = I128(2, BANE); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, BANE), x | y); x = I128(2, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(2, -1ul); y = I128(0, 2); ASSERT128(I128(2, -1ul), x | y); x = I128(2, -1ul); y = I128(0, BANE); ASSERT128(I128(2, -1ul), x | y); x = I128(2, -1ul); y = I128(0, -1ul); ASSERT128(I128(2, -1ul), x | y); x = I128(2, -1ul); y = I128(2, 0); ASSERT128(I128(2, -1ul), x | y); x = I128(2, -1ul); y = I128(2, 2); ASSERT128(I128(2, -1ul), x | y); x = I128(2, -1ul); y = I128(2, BANE); ASSERT128(I128(2, -1ul), x | y); x = I128(2, -1ul); y = I128(2, -1ul); ASSERT128(I128(2, -1ul), x | y); x = I128(2, -1ul); y = I128(BANE, 0); ASSERT128(I128(BANE2, -1ul), x | y); x = I128(2, -1ul); y = I128(BANE, 2); ASSERT128(I128(BANE2, -1ul), x | y); x = I128(2, -1ul); y = I128(BANE, BANE); ASSERT128(I128(BANE2, -1ul), x | y); x = I128(2, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(BANE2, -1ul), x | y); x = I128(2, -1ul); y = I128(-1ul, 0); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(2, -1ul); y = I128(-1ul, 2); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(2, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(2, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(BANE, 0); y = I128(0, 2); ASSERT128(I128(BANE, 2), x | y); x = I128(BANE, 0); y = I128(0, BANE); ASSERT128(I128(BANE, BANE), x | y); x = I128(BANE, 0); y = I128(0, -1ul); ASSERT128(I128(BANE, -1ul), x | y); x = I128(BANE, 0); y = I128(2, 0); ASSERT128(I128(BANE2, 0), x | y); x = I128(BANE, 0); y = I128(2, 2); ASSERT128(I128(BANE2, 2), x | y); x = I128(BANE, 0); y = I128(2, BANE); ASSERT128(I128(BANE2, BANE), x | y); x = I128(BANE, 0); y = I128(2, -1ul); ASSERT128(I128(BANE2, -1ul), x | y); x = I128(BANE, 0); y = I128(BANE, 0); ASSERT128(I128(BANE, 0), x | y); x = I128(BANE, 0); y = I128(BANE, 2); ASSERT128(I128(BANE, 2), x | y); x = I128(BANE, 0); y = I128(BANE, BANE); ASSERT128(I128(BANE, BANE), x | y); x = I128(BANE, 0); y = I128(BANE, -1ul); ASSERT128(I128(BANE, -1ul), x | y); x = I128(BANE, 0); y = I128(-1ul, 0); ASSERT128(I128(-1ul, 0), x | y); x = I128(BANE, 0); y = I128(-1ul, 2); ASSERT128(I128(-1ul, 2), x | y); x = I128(BANE, 0); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, BANE), x | y); x = I128(BANE, 2); y = I128(0, 2); ASSERT128(I128(BANE, 2), x | y); x = I128(BANE, 2); y = I128(0, BANE); ASSERT128(I128(BANE, BANE2), x | y); x = I128(BANE, 2); y = I128(0, -1ul); ASSERT128(I128(BANE, -1ul), x | y); x = I128(BANE, 2); y = I128(2, 0); ASSERT128(I128(BANE2, 2), x | y); x = I128(BANE, 2); y = I128(2, 2); ASSERT128(I128(BANE2, 2), x | y); x = I128(BANE, 2); y = I128(2, BANE); ASSERT128(I128(BANE2, BANE2), x | y); x = I128(BANE, 2); y = I128(2, -1ul); ASSERT128(I128(BANE2, -1ul), x | y); x = I128(BANE, 2); y = I128(BANE, 0); ASSERT128(I128(BANE, 2), x | y); x = I128(BANE, 2); y = I128(BANE, 2); ASSERT128(I128(BANE, 2), x | y); x = I128(BANE, 2); y = I128(BANE, BANE); ASSERT128(I128(BANE, BANE2), x | y); x = I128(BANE, 2); y = I128(BANE, -1ul); ASSERT128(I128(BANE, -1ul), x | y); x = I128(BANE, 2); y = I128(-1ul, 0); ASSERT128(I128(-1ul, 2), x | y); x = I128(BANE, 2); y = I128(-1ul, 2); ASSERT128(I128(-1ul, 2), x | y); x = I128(BANE, 2); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, BANE2), x | y); x = I128(BANE, 2); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(BANE, BANE); y = I128(0, 2); ASSERT128(I128(BANE, BANE2), x | y); x = I128(BANE, BANE); y = I128(0, BANE); ASSERT128(I128(BANE, BANE), x | y); x = I128(BANE, BANE); y = I128(0, -1ul); ASSERT128(I128(BANE, -1ul), x | y); x = I128(BANE, BANE); y = I128(2, 0); ASSERT128(I128(BANE2, BANE), x | y); x = I128(BANE, BANE); y = I128(2, 2); ASSERT128(I128(BANE2, BANE2), x | y); x = I128(BANE, BANE); y = I128(2, BANE); ASSERT128(I128(BANE2, BANE), x | y); x = I128(BANE, BANE); y = I128(2, -1ul); ASSERT128(I128(BANE2, -1ul), x | y); x = I128(BANE, BANE); y = I128(BANE, 0); ASSERT128(I128(BANE, BANE), x | y); x = I128(BANE, BANE); y = I128(BANE, 2); ASSERT128(I128(BANE, BANE2), x | y); x = I128(BANE, BANE); y = I128(BANE, BANE); ASSERT128(I128(BANE, BANE), x | y); x = I128(BANE, BANE); y = I128(BANE, -1ul); ASSERT128(I128(BANE, -1ul), x | y); x = I128(BANE, BANE); y = I128(-1ul, 0); ASSERT128(I128(-1ul, BANE), x | y); x = I128(BANE, BANE); y = I128(-1ul, 2); ASSERT128(I128(-1ul, BANE2), x | y); x = I128(BANE, BANE); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, BANE), x | y); x = I128(BANE, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(BANE, -1ul); y = I128(0, 2); ASSERT128(I128(BANE, -1ul), x | y); x = I128(BANE, -1ul); y = I128(0, BANE); ASSERT128(I128(BANE, -1ul), x | y); x = I128(BANE, -1ul); y = I128(0, -1ul); ASSERT128(I128(BANE, -1ul), x | y); x = I128(BANE, -1ul); y = I128(2, 0); ASSERT128(I128(BANE2, -1ul), x | y); x = I128(BANE, -1ul); y = I128(2, 2); ASSERT128(I128(BANE2, -1ul), x | y); x = I128(BANE, -1ul); y = I128(2, BANE); ASSERT128(I128(BANE2, -1ul), x | y); x = I128(BANE, -1ul); y = I128(2, -1ul); ASSERT128(I128(BANE2, -1ul), x | y); x = I128(BANE, -1ul); y = I128(BANE, 0); ASSERT128(I128(BANE, -1ul), x | y); x = I128(BANE, -1ul); y = I128(BANE, 2); ASSERT128(I128(BANE, -1ul), x | y); x = I128(BANE, -1ul); y = I128(BANE, BANE); ASSERT128(I128(BANE, -1ul), x | y); x = I128(BANE, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(BANE, -1ul), x | y); x = I128(BANE, -1ul); y = I128(-1ul, 0); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(BANE, -1ul); y = I128(-1ul, 2); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(BANE, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(BANE, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, 0); y = I128(0, 2); ASSERT128(I128(-1ul, 2), x | y); x = I128(-1ul, 0); y = I128(0, BANE); ASSERT128(I128(-1ul, BANE), x | y); x = I128(-1ul, 0); y = I128(0, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, 0); y = I128(2, 0); ASSERT128(I128(-1ul, 0), x | y); x = I128(-1ul, 0); y = I128(2, 2); ASSERT128(I128(-1ul, 2), x | y); x = I128(-1ul, 0); y = I128(2, BANE); ASSERT128(I128(-1ul, BANE), x | y); x = I128(-1ul, 0); y = I128(2, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, 0); y = I128(BANE, 0); ASSERT128(I128(-1ul, 0), x | y); x = I128(-1ul, 0); y = I128(BANE, 2); ASSERT128(I128(-1ul, 2), x | y); x = I128(-1ul, 0); y = I128(BANE, BANE); ASSERT128(I128(-1ul, BANE), x | y); x = I128(-1ul, 0); y = I128(BANE, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, 0); y = I128(-1ul, 0); ASSERT128(I128(-1ul, 0), x | y); x = I128(-1ul, 0); y = I128(-1ul, 2); ASSERT128(I128(-1ul, 2), x | y); x = I128(-1ul, 0); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, BANE), x | y); x = I128(-1ul, 0); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, 2); y = I128(0, 2); ASSERT128(I128(-1ul, 2), x | y); x = I128(-1ul, 2); y = I128(0, BANE); ASSERT128(I128(-1ul, BANE2), x | y); x = I128(-1ul, 2); y = I128(0, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, 2); y = I128(2, 0); ASSERT128(I128(-1ul, 2), x | y); x = I128(-1ul, 2); y = I128(2, 2); ASSERT128(I128(-1ul, 2), x | y); x = I128(-1ul, 2); y = I128(2, BANE); ASSERT128(I128(-1ul, BANE2), x | y); x = I128(-1ul, 2); y = I128(2, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, 2); y = I128(BANE, 0); ASSERT128(I128(-1ul, 2), x | y); x = I128(-1ul, 2); y = I128(BANE, 2); ASSERT128(I128(-1ul, 2), x | y); x = I128(-1ul, 2); y = I128(BANE, BANE); ASSERT128(I128(-1ul, BANE2), x | y); x = I128(-1ul, 2); y = I128(BANE, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, 2); y = I128(-1ul, 0); ASSERT128(I128(-1ul, 2), x | y); x = I128(-1ul, 2); y = I128(-1ul, 2); ASSERT128(I128(-1ul, 2), x | y); x = I128(-1ul, 2); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, BANE2), x | y); x = I128(-1ul, 2); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, BANE); y = I128(0, 2); ASSERT128(I128(-1ul, BANE2), x | y); x = I128(-1ul, BANE); y = I128(0, BANE); ASSERT128(I128(-1ul, BANE), x | y); x = I128(-1ul, BANE); y = I128(0, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, BANE); y = I128(2, 0); ASSERT128(I128(-1ul, BANE), x | y); x = I128(-1ul, BANE); y = I128(2, 2); ASSERT128(I128(-1ul, BANE2), x | y); x = I128(-1ul, BANE); y = I128(2, BANE); ASSERT128(I128(-1ul, BANE), x | y); x = I128(-1ul, BANE); y = I128(2, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, BANE); y = I128(BANE, 0); ASSERT128(I128(-1ul, BANE), x | y); x = I128(-1ul, BANE); y = I128(BANE, 2); ASSERT128(I128(-1ul, BANE2), x | y); x = I128(-1ul, BANE); y = I128(BANE, BANE); ASSERT128(I128(-1ul, BANE), x | y); x = I128(-1ul, BANE); y = I128(BANE, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, BANE); y = I128(-1ul, 0); ASSERT128(I128(-1ul, BANE), x | y); x = I128(-1ul, BANE); y = I128(-1ul, 2); ASSERT128(I128(-1ul, BANE2), x | y); x = I128(-1ul, BANE); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, BANE), x | y); x = I128(-1ul, BANE); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, -1ul); y = I128(0, 2); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, -1ul); y = I128(0, BANE); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, -1ul); y = I128(0, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, -1ul); y = I128(2, 0); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, -1ul); y = I128(2, 2); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, -1ul); y = I128(2, BANE); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, -1ul); y = I128(2, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, -1ul); y = I128(BANE, 0); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, -1ul); y = I128(BANE, 2); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, -1ul); y = I128(BANE, BANE); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, -1ul); y = I128(BANE, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, -1ul); y = I128(-1ul, 0); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, -1ul); y = I128(-1ul, 2); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, -1ul); y = I128(-1ul, BANE); ASSERT128(I128(-1ul, -1ul), x | y); x = I128(-1ul, -1ul); y = I128(-1ul, -1ul); ASSERT128(I128(-1ul, -1ul), x | y); } void testNot128(void) { __int128 x; x = I128(0, 0); ASSERT128(I128(-1ul, -1ul), ~x); x = I128(0, 2); ASSERT128(I128(-1ul, IMAX2), ~x); x = I128(0, BANE); ASSERT128(I128(-1ul, IMAX), ~x); x = I128(0, -1ul); ASSERT128(I128(-1ul, 0), ~x); x = I128(2, 0); ASSERT128(I128(IMAX2, -1ul), ~x); x = I128(2, 2); ASSERT128(I128(IMAX2, IMAX2), ~x); x = I128(2, BANE); ASSERT128(I128(IMAX2, IMAX), ~x); x = I128(2, -1ul); ASSERT128(I128(IMAX2, 0), ~x); x = I128(BANE, 0); ASSERT128(I128(IMAX, -1ul), ~x); x = I128(BANE, 2); ASSERT128(I128(IMAX, IMAX2), ~x); x = I128(BANE, BANE); ASSERT128(I128(IMAX, IMAX), ~x); x = I128(BANE, -1ul); ASSERT128(I128(IMAX, 0), ~x); x = I128(-1ul, 0); ASSERT128(I128(0, -1ul), ~x); x = I128(-1ul, 2); ASSERT128(I128(0, IMAX2), ~x); x = I128(-1ul, BANE); ASSERT128(I128(0, IMAX), ~x); x = I128(-1ul, -1ul); ASSERT128(I128(0, 0), ~x); } void testAbi(void) { ASSERT(0, ({ char buf[200]; sprintf(buf, "%d %d %d %d %032jjx %032jjx", 1, 2, 3, 4, I128(0x1ffffffff, 0x2ffffffff), I128(0x3eeeeeeee, 0x4eeeeeeee)); strcmp("1 2 3 4 00000001ffffffff00000002ffffffff " "00000003eeeeeeee00000004eeeeeeee", buf); })); ASSERT(0, ({ char buf[200]; sprintf(buf, "%d %d %d %d %d %032jjx %032jjx", 1, 2, 3, 4, 5, I128(0x1ffffffff, 0x2ffffffff), I128(0x3eeeeeeee, 0x4eeeeeeee)); strcmp("1 2 3 4 5 00000001ffffffff00000002ffffffff " "00000003eeeeeeee00000004eeeeeeee", buf); })); } int main(void) { testLang128(); testCompare128(); testAdd128(); testSub128(); testMul128(); testDiv128(); testDivu128(); testRem128(); testRemu128(); testShr128(); testSar128(); testShl128(); testNeg128(); testXor128(); testAnd128(); testOr128(); testNot128(); testCastDblInt128(); testCastDblUint128(); testCastLdblInt128(); testCastLdblUint128(); testAbi(); return 0; }
213,489
8,222
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/varargs_test.c
#include "third_party/chibicc/test/test.h" int sum1(int x, ...) { va_list ap; va_start(ap, x); for (;;) { int y = va_arg(ap, int); if (y == 0) return x; x += y; } } int sum2(int x, ...) { va_list ap; va_start(ap, x); for (;;) { double y = va_arg(ap, double); x += y; int z = va_arg(ap, int); if (z == 0) return x; x += z; } } void fmt(char *buf, char *fmt, ...) { va_list ap; va_start(ap, fmt); va_list ap2; va_copy(ap2, ap); vsprintf(buf, fmt, ap2); va_end(buf); } int main() { ASSERT(6, sum1(1, 2, 3, 0)); ASSERT(55, sum1(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0)); ASSERT(21, sum2(1, 2.0, 3, 4.0, 5, 6.0, 0)); ASSERT(21, sum2(1, 2.0, 3, 4.0, 5, 6.0, 0)); ASSERT(210, sum2(1, 2.0, 3, 4.0, 5, 6.0, 7, 8.0, 9, 10.0, 11, 12.0, 13, 14.0, 15, 16.0, 17, 18.0, 19, 20.0, 0)); ASSERT(0, ({ char buf[100]; fmt(buf, "%d %d", 2, 3); strcmp(buf, "2 3"); })); return 0; }
997
53
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/asm_test.c
asm(".ident\t\"hello\""); /*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/chibicc/test/test.h" char *asm_fn1(void) { asm("mov\t$50,%rax\n\t" "mov\t%rbp,%rsp\n\t" "leave\n\t" "ret"); } char *asm_fn2(void) { asm inline volatile("mov\t$55,%rax\n\t" "mov\t%rbp,%rsp\n\t" "leave\n\t" "ret"); } void *repmovsb(void *dst, void *src, unsigned long n) { asm("rep movsb" : "=D"(dst), "=S"(src), "=c"(n), "=m"(*(char(*)[n])dst) : "0"(dst), "1"(src), "2"(n), "m"(*(const char(*)[n])src)); return dst; } void *repmovsbeax(void *dst, void *src, unsigned long n) { void *res; asm("rep movsb\n\t" "xchg\t%0,%1" : "=a"(res), "=D"(dst), "=S"(src), "=c"(n), "=m"(*(char(*)[n])dst) : "1"(dst), "2"(src), "3"(n), "m"(*(const char(*)[n])src) : "rbx", "rbp", "r12", "r13", "r14", "r15", "cc"); return res; } void testSmashStackFrame_clobberIsRestored(void) { asm volatile("xor\t%%ebp,%%ebp" : /* no outputs */ : /* no inputs */ : "rbp", "cc"); } void testFlagOutputs(void) { bool zf, cf, sf; asm("xor\t%%eax,%%eax\n\t" "inc\t%%eax" : "=@ccz"(zf), "=@ccs"(cf) : /* no inputs */ : "rax"); ASSERT(false, zf); ASSERT(false, sf); asm("xor\t%%eax,%%eax\n\t" "dec\t%%eax" : "=@ccz"(zf), "=@ccs"(cf) : /* no inputs */ : "rax"); ASSERT(false, zf); ASSERT(true, sf); asm("xor\t%%eax,%%eax\n\t" "cmc" : "=@ccz"(zf), "=@ccc"(cf), "=@ccs"(sf) : /* no inputs */ : "rax"); ASSERT(true, zf); ASSERT(true, cf); ASSERT(false, sf); } void testAugmentLoByte_onlyModifiesLowerBits(void) { int x, y; x = 0x01020304; y = 0x00000005; asm("sub\t%b1,%b0" : "+q"(x) : "q"(y)); ASSERT(0x010203ff, x); ASSERT(0x00000005, y); } void testAugmentHiByte_onlyModifiesHigherBits(void) { int x, y; x = 0x01020304; y = 0x00000400; asm("sub\t%h1,%h0" : "+Q"(x) : "Q"(y)); ASSERT(0x0102ff04, x); ASSERT(0x00000400, y); } int main() { ASSERT(50, asm_fn1()); ASSERT(55, asm_fn2()); { char buf[] = "HELLO"; char *s = "hello"; char *p = repmovsb(buf, s, 4); ASSERT(4, p - buf); ASSERT('h', buf[0]); ASSERT('e', buf[1]); ASSERT('l', buf[2]); ASSERT('l', buf[3]); ASSERT('O', buf[4]); } { char buf[] = "HELLO"; char *s = "hello"; char *p = repmovsbeax(buf, s, 4); ASSERT(4, p - buf); ASSERT('h', buf[0]); ASSERT('e', buf[1]); ASSERT('l', buf[2]); ASSERT('l', buf[3]); ASSERT('O', buf[4]); } testSmashStackFrame_clobberIsRestored(); short v1[8] = {0, 1, 2, 3, 4, 5, 6, 7}; short v2[8] = {1, 1, 1, 1, 1, 1, 1, 1}; short v3[8] = {2, 2, 2, 2, 2, 2, 2, 2}; asm("paddsw\t%1,%0\n\t" "paddsw\t%2,%0" : "+x"(v1) : "xm"(v2), "xm"(v3)); ASSERT(3, v1[0]); ASSERT(4, v1[1]); ASSERT(5, v1[2]); ASSERT(6, v1[3]); ASSERT(7, v1[4]); ASSERT(8, v1[5]); ASSERT(9, v1[6]); ASSERT(10, v1[7]); { char *p; asm("mov\t%1,%0" : "=r"(p) : "r"("hello")); ASSERT(1, !strcmp(p, "hello")); } testAugmentLoByte_onlyModifiesLowerBits(); testAugmentHiByte_onlyModifiesHigherBits(); return 0; }
5,066
161
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/line_test.c
#include "third_party/chibicc/test/test.h" int main() { #line 500 "foo" ASSERT(501, __LINE__); ASSERT(0, strcmp(__FILE__, "foo")); #line 800 "bar" ASSERT(801, __LINE__); ASSERT(0, strcmp(__FILE__, "bar")); #line 1 ASSERT(2, __LINE__); # 200 "xyz" 2 3 ASSERT(201, __LINE__); ASSERT(0, strcmp(__FILE__, "xyz")); return 0; }
343
21
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/tls_test.c.todo
#include "third_party/chibicc/test/test.h" _Thread_local int x; void add(void) { x += 3; } _Noreturn int test(void) { x = 7; add(); ASSERT(10, x); exit(0); } int main(void) { test(); }
201
19
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/typedef_test.c
#include "third_party/chibicc/test/test.h" typedef int MyInt, MyInt2[4]; typedef int; int main() { ASSERT(1, ({ typedef int t; t x = 1; x; })); ASSERT(1, ({ typedef struct { int a; } t; t x; x.a = 1; x.a; })); ASSERT(1, ({ typedef int t; t t = 1; t; })); ASSERT(2, ({ typedef struct { int a; } t; { typedef int t; } t x; x.a = 2; x.a; })); ASSERT(4, ({ typedef t; t x; sizeof(x); })); ASSERT(3, ({ MyInt x = 3; x; })); ASSERT(16, ({ MyInt2 x; sizeof(x); })); return 0; }
843
50
jart/cosmopolitan
false
cosmopolitan/third_party/chibicc/test/float_test.c
#include "third_party/chibicc/test/test.h" int main() { ASSERT(35, (float)(char)35); ASSERT(35, (float)(short)35); ASSERT(35, (float)(int)35); ASSERT(35, (float)(long)35); ASSERT(35, (float)(unsigned char)35); ASSERT(35, (float)(unsigned short)35); ASSERT(35, (float)(unsigned int)35); ASSERT(35, (float)(unsigned long)35); ASSERT(35, (double)(char)35); ASSERT(35, (double)(short)35); ASSERT(35, (double)(int)35); ASSERT(35, (double)(long)35); ASSERT(35, (double)(unsigned char)35); ASSERT(35, (double)(unsigned short)35); ASSERT(35, (double)(unsigned int)35); ASSERT(35, (double)(unsigned long)35); ASSERT(35, (char)(float)35); ASSERT(35, (short)(float)35); ASSERT(35, (int)(float)35); ASSERT(35, (long)(float)35); ASSERT(35, (unsigned char)(float)35); ASSERT(35, (unsigned short)(float)35); ASSERT(35, (unsigned int)(float)35); ASSERT(35, (unsigned long)(float)35); ASSERT(35, (char)(double)35); ASSERT(35, (short)(double)35); ASSERT(35, (int)(double)35); ASSERT(35, (long)(double)35); ASSERT(35, (unsigned char)(double)35); ASSERT(35, (unsigned short)(double)35); ASSERT(35, (unsigned int)(double)35); ASSERT(35, (unsigned long)(double)35); ASSERT(0x8000000000000000, (double)(unsigned long)(long)-1); ASSERT(1, 2e3 == 2e3); ASSERT(0, 2e3 == 2e5); ASSERT(1, 2.0 == 2); ASSERT(0, 5.1 < 5); ASSERT(0, 5.0 < 5); ASSERT(1, 4.9 < 5); ASSERT(0, 5.1 <= 5); ASSERT(1, 5.0 <= 5); ASSERT(1, 4.9 <= 5); ASSERT(1, 2e3f == 2e3); ASSERT(0, 2e3f == 2e5); ASSERT(1, 2.0f == 2); ASSERT(0, 5.1f < 5); ASSERT(0, 5.0f < 5); ASSERT(1, 4.9f < 5); ASSERT(0, 5.1f <= 5); ASSERT(1, 5.0f <= 5); ASSERT(1, 4.9f <= 5); ASSERT(6, 2.3 + 3.8); ASSERT(-1, 2.3 - 3.8); ASSERT(-3, -3.8); ASSERT(13, 3.3 * 4); ASSERT(2, 5.0 / 2); ASSERT(6, 2.3f + 3.8f); ASSERT(6, 2.3f + 3.8); ASSERT(-1, 2.3f - 3.8); ASSERT(-3, -3.8f); ASSERT(13, 3.3f * 4); ASSERT(2, 5.0f / 2); ASSERT(0, 0.0 / 0.0 == 0.0 / 0.0); ASSERT(1, 0.0 / 0.0 != 0.0 / 0.0); ASSERT(0, 0.0 / 0.0 < 0); ASSERT(0, 0.0 / 0.0 <= 0); ASSERT(0, 0.0 / 0.0 > 0); ASSERT(0, 0.0 / 0.0 >= 0); ASSERT(0, !3.); ASSERT(1, !0.); ASSERT(0, !3.f); ASSERT(1, !0.f); ASSERT(5, 0.0 ? 3 : 5); ASSERT(3, 1.2 ? 3 : 5); return 0; }
2,297
93
jart/cosmopolitan
false
cosmopolitan/third_party/awk/LICENSE
/**************************************************************** Copyright (C) Lucent Technologies 1997 All Rights Reserved Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that the copyright notice and this permission notice and warranty disclaimer appear in supporting documentation, and that the name Lucent Technologies or any of its entities not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ****************************************************************/
1,161
24
jart/cosmopolitan
false
cosmopolitan/third_party/awk/lex.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Copyright (C) Lucent Technologies 1997 │ │ All Rights Reserved │ │ │ │ Permission to use, copy, modify, and distribute this software and │ │ its documentation for any purpose and without fee is hereby │ │ granted, provided that the above copyright notice appear in all │ │ copies and that both that the copyright notice and this │ │ permission notice and warranty disclaimer appear in supporting │ │ documentation, and that the name Lucent Technologies or any of │ │ its entities not be used in advertising or publicity pertaining │ │ to distribution of the software without specific, written prior │ │ permission. │ │ │ │ LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, │ │ INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. │ │ IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY │ │ SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES │ │ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER │ │ IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, │ │ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF │ │ THIS SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" #include "libc/fmt/conv.h" #include "libc/fmt/fmt.h" #include "libc/mem/mem.h" #include "libc/runtime/runtime.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" #include "third_party/awk/awk.h" #include "third_party/awk/awkgram.tab.h" #include "third_party/gdtoa/gdtoa.h" // clang-format off extern bool infunc; int lineno = 1; int bracecnt = 0; int brackcnt = 0; int parencnt = 0; typedef struct Keyword { const char *word; int sub; int type; } Keyword; const Keyword keywords[] = { /* keep sorted: binary searched */ { "BEGIN", XBEGIN, XBEGIN }, { "END", XEND, XEND }, { "NF", VARNF, VARNF }, { "atan2", FATAN, BLTIN }, { "break", BREAK, BREAK }, { "close", CLOSE, CLOSE }, { "continue", CONTINUE, CONTINUE }, { "cos", FCOS, BLTIN }, { "delete", DELETE, DELETE }, { "do", DO, DO }, { "else", ELSE, ELSE }, { "exit", EXIT, EXIT }, { "exp", FEXP, BLTIN }, { "fflush", FFLUSH, BLTIN }, { "for", FOR, FOR }, { "func", FUNC, FUNC }, { "function", FUNC, FUNC }, { "getline", GETLINE, GETLINE }, { "gsub", GSUB, GSUB }, { "if", IF, IF }, { "in", IN, IN }, { "index", INDEX, INDEX }, { "int", FINT, BLTIN }, { "length", FLENGTH, BLTIN }, { "log", FLOG, BLTIN }, { "match", MATCHFCN, MATCHFCN }, { "next", NEXT, NEXT }, { "nextfile", NEXTFILE, NEXTFILE }, { "print", PRINT, PRINT }, { "printf", PRINTF, PRINTF }, { "rand", FRAND, BLTIN }, { "return", RETURN, RETURN }, { "sin", FSIN, BLTIN }, { "split", SPLIT, SPLIT }, { "sprintf", SPRINTF, SPRINTF }, { "sqrt", FSQRT, BLTIN }, { "srand", FSRAND, BLTIN }, { "sub", SUB, SUB }, { "substr", SUBSTR, SUBSTR }, { "system", FSYSTEM, BLTIN }, { "tolower", FTOLOWER, BLTIN }, { "toupper", FTOUPPER, BLTIN }, { "while", WHILE, WHILE }, }; #define RET(x) { if(dbg)printf("lex %s\n", tokname(x)); return(x); } static int peek(void) { int c = input(); unput(c); return c; } static int gettok(char **pbuf, int *psz) /* get next input token */ { int c, retc; char *buf = *pbuf; int sz = *psz; char *bp = buf; c = input(); if (c == 0) return 0; buf[0] = c; buf[1] = 0; if (!isalnum(c) && c != '.' && c != '_') return c; *bp++ = c; if (isalpha(c) || c == '_') { /* it's a varname */ for ( ; (c = input()) != 0; ) { if (bp-buf >= sz) if (!adjbuf(&buf, &sz, bp-buf+2, 100, &bp, "gettok")) FATAL( "out of space for name %.10s...", buf ); if (isalnum(c) || c == '_') *bp++ = c; else { *bp = 0; unput(c); break; } } *bp = 0; retc = 'a'; /* alphanumeric */ } else { /* maybe it's a number, but could be . */ char *rem; /* read input until can't be a number */ for ( ; (c = input()) != 0; ) { if (bp-buf >= sz) if (!adjbuf(&buf, &sz, bp-buf+2, 100, &bp, "gettok")) FATAL( "out of space for number %.10s...", buf ); if (isdigit(c) || c == 'e' || c == 'E' || c == '.' || c == '+' || c == '-') *bp++ = c; else { unput(c); break; } } *bp = 0; strtod(buf, &rem); /* parse the number */ if (rem == buf) { /* it wasn't a valid number at all */ buf[1] = 0; /* return one character as token */ retc = (uschar)buf[0]; /* character is its own type */ unputstr(rem+1); /* put rest back for later */ } else { /* some prefix was a number */ unputstr(rem); /* put rest back for later */ rem[0] = 0; /* truncate buf after number part */ retc = '0'; /* type is number */ } } *pbuf = buf; *psz = sz; return retc; } int word(char *); int string(void); int regexpr(void); bool sc = false; /* true => return a } right now */ bool reg = false; /* true => return a REGEXPR now */ int yylex(void) { int c; static char *buf = NULL; static int bufsize = 5; /* BUG: setting this small causes core dump! */ if (buf == NULL && (buf = (char *) malloc(bufsize)) == NULL) FATAL( "out of space in yylex" ); if (sc) { sc = false; RET('}'); } if (reg) { reg = false; return regexpr(); } for (;;) { c = gettok(&buf, &bufsize); if (c == 0) return 0; if (isalpha(c) || c == '_') return word(buf); if (isdigit(c)) { char *cp = tostring(buf); double result; if (is_number(cp, & result)) yylval.cp = setsymtab(buf, cp, result, CON|NUM, symtab); else yylval.cp = setsymtab(buf, cp, 0.0, STR, symtab); free(cp); /* should this also have STR set? */ RET(NUMBER); } yylval.i = c; switch (c) { case '\n': /* {EOL} */ lineno++; RET(NL); case '\r': /* assume \n is coming */ case ' ': /* {WS}+ */ case '\t': break; case '#': /* #.* strip comments */ while ((c = input()) != '\n' && c != 0) ; unput(c); /* * Next line is a hack, itcompensates for * unput's treatment of \n. */ lineno++; break; case ';': RET(';'); case '\\': if (peek() == '\n') { input(); lineno++; } else if (peek() == '\r') { input(); input(); /* \n */ lineno++; } else { RET(c); } break; case '&': if (peek() == '&') { input(); RET(AND); } else RET('&'); case '|': if (peek() == '|') { input(); RET(BOR); } else RET('|'); case '!': if (peek() == '=') { input(); yylval.i = NE; RET(NE); } else if (peek() == '~') { input(); yylval.i = NOTMATCH; RET(MATCHOP); } else RET(NOT); case '~': yylval.i = MATCH; RET(MATCHOP); case '<': if (peek() == '=') { input(); yylval.i = LE; RET(LE); } else { yylval.i = LT; RET(LT); } case '=': if (peek() == '=') { input(); yylval.i = EQ; RET(EQ); } else { yylval.i = ASSIGN; RET(ASGNOP); } case '>': if (peek() == '=') { input(); yylval.i = GE; RET(GE); } else if (peek() == '>') { input(); yylval.i = APPEND; RET(APPEND); } else { yylval.i = GT; RET(GT); } case '+': if (peek() == '+') { input(); yylval.i = INCR; RET(INCR); } else if (peek() == '=') { input(); yylval.i = ADDEQ; RET(ASGNOP); } else RET('+'); case '-': if (peek() == '-') { input(); yylval.i = DECR; RET(DECR); } else if (peek() == '=') { input(); yylval.i = SUBEQ; RET(ASGNOP); } else RET('-'); case '*': if (peek() == '=') { /* *= */ input(); yylval.i = MULTEQ; RET(ASGNOP); } else if (peek() == '*') { /* ** or **= */ input(); /* eat 2nd * */ if (peek() == '=') { input(); yylval.i = POWEQ; RET(ASGNOP); } else { RET(POWER); } } else RET('*'); case '/': RET('/'); case '%': if (peek() == '=') { input(); yylval.i = MODEQ; RET(ASGNOP); } else RET('%'); case '^': if (peek() == '=') { input(); yylval.i = POWEQ; RET(ASGNOP); } else RET(POWER); case '$': /* BUG: awkward, if not wrong */ c = gettok(&buf, &bufsize); if (isalpha(c)) { if (strcmp(buf, "NF") == 0) { /* very special */ unputstr("(NF)"); RET(INDIRECT); } c = peek(); if (c == '(' || c == '[' || (infunc && isarg(buf) >= 0)) { unputstr(buf); RET(INDIRECT); } yylval.cp = setsymtab(buf, "", 0.0, STR|NUM, symtab); RET(IVAR); } else if (c == 0) { /* */ SYNTAX( "unexpected end of input after $" ); RET(';'); } else { unputstr(buf); RET(INDIRECT); } case '}': if (--bracecnt < 0) SYNTAX( "extra }" ); sc = true; RET(';'); case ']': if (--brackcnt < 0) SYNTAX( "extra ]" ); RET(']'); case ')': if (--parencnt < 0) SYNTAX( "extra )" ); RET(')'); case '{': bracecnt++; RET('{'); case '[': brackcnt++; RET('['); case '(': parencnt++; RET('('); case '"': return string(); /* BUG: should be like tran.c ? */ default: RET(c); } } } int string(void) { int c, n; char *s, *bp; static char *buf = NULL; static int bufsz = 500; if (buf == NULL && (buf = (char *) malloc(bufsz)) == NULL) FATAL("out of space for strings"); for (bp = buf; (c = input()) != '"'; ) { if (!adjbuf(&buf, &bufsz, bp-buf+2, 500, &bp, "string")) FATAL("out of space for string %.10s...", buf); switch (c) { case '\n': case '\r': case 0: *bp = '\0'; SYNTAX( "non-terminated string %.10s...", buf ); if (c == 0) /* hopeless */ FATAL( "giving up" ); lineno++; break; case '\\': c = input(); switch (c) { case '\n': break; case '"': *bp++ = '"'; break; case 'n': *bp++ = '\n'; break; case 't': *bp++ = '\t'; break; case 'f': *bp++ = '\f'; break; case 'r': *bp++ = '\r'; break; case 'b': *bp++ = '\b'; break; case 'v': *bp++ = '\v'; break; case 'a': *bp++ = '\a'; break; case '\\': *bp++ = '\\'; break; case '0': case '1': case '2': /* octal: \d \dd \ddd */ case '3': case '4': case '5': case '6': case '7': n = c - '0'; if ((c = peek()) >= '0' && c < '8') { n = 8 * n + input() - '0'; if ((c = peek()) >= '0' && c < '8') n = 8 * n + input() - '0'; } *bp++ = n; break; case 'x': /* hex \x0-9a-fA-F + */ { char xbuf[100], *px; for (px = xbuf; (c = input()) != 0 && px-xbuf < 100-2; ) { if (isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) *px++ = c; else break; } *px = 0; unput(c); sscanf(xbuf, "%x", (unsigned int *) &n); *bp++ = n; break; } default: *bp++ = c; break; } break; default: *bp++ = c; break; } } *bp = 0; s = tostring(buf); *bp++ = ' '; *bp++ = '\0'; yylval.cp = setsymtab(buf, s, 0.0, CON|STR|DONTFREE, symtab); free(s); RET(STRING); } static int binsearch(char *w, const Keyword *kp, int n) { int cond, low, mid, high; low = 0; high = n - 1; while (low <= high) { mid = (low + high) / 2; if ((cond = strcmp(w, kp[mid].word)) < 0) high = mid - 1; else if (cond > 0) low = mid + 1; else return mid; } return -1; } int word(char *w) { const Keyword *kp; int c, n; n = binsearch(w, keywords, sizeof(keywords)/sizeof(keywords[0])); if (n != -1) { /* found in table */ kp = keywords + n; yylval.i = kp->sub; switch (kp->type) { /* special handling */ case BLTIN: if (kp->sub == FSYSTEM && safe) SYNTAX( "system is unsafe" ); RET(kp->type); case FUNC: if (infunc) SYNTAX( "illegal nested function" ); RET(kp->type); case RETURN: if (!infunc) SYNTAX( "return not in function" ); RET(kp->type); case VARNF: yylval.cp = setsymtab("NF", "", 0.0, NUM, symtab); RET(VARNF); default: RET(kp->type); } } c = peek(); /* look for '(' */ if (c != '(' && infunc && (n=isarg(w)) >= 0) { yylval.i = n; RET(ARG); } else { yylval.cp = setsymtab(w, "", 0.0, STR|NUM|DONTFREE, symtab); if (c == '(') { RET(CALL); } else { RET(VAR); } } } void startreg(void) /* next call to yylex will return a regular expression */ { reg = true; } int regexpr(void) { int c; static char *buf = NULL; static int bufsz = 500; char *bp; if (buf == NULL && (buf = (char *) malloc(bufsz)) == NULL) FATAL("out of space for rex expr"); bp = buf; for ( ; (c = input()) != '/' && c != 0; ) { if (!adjbuf(&buf, &bufsz, bp-buf+3, 500, &bp, "regexpr")) FATAL("out of space for reg expr %.10s...", buf); if (c == '\n') { *bp = '\0'; SYNTAX( "newline in regular expression %.10s...", buf ); unput('\n'); break; } else if (c == '\\') { *bp++ = '\\'; *bp++ = input(); } else { *bp++ = c; } } *bp = 0; if (c == 0) SYNTAX("non-terminated regular expression %.10s...", buf); yylval.s = tostring(buf); unput('/'); RET(REGEXPR); } /* low-level lexical stuff, sort of inherited from lex */ char ebuf[300]; char *ep = ebuf; char yysbuf[100]; /* pushback buffer */ char *yysptr = yysbuf; FILE *yyin = NULL; int input(void) /* get next lexical input character */ { int c; extern char *lexprog; if (yysptr > yysbuf) c = (uschar)*--yysptr; else if (lexprog != NULL) { /* awk '...' */ if ((c = (uschar)*lexprog) != 0) lexprog++; } else /* awk -f ... */ c = pgetc(); if (c == EOF) c = 0; if (ep >= ebuf + sizeof ebuf) ep = ebuf; *ep = c; if (c != 0) { ep++; } return (c); } void unput(int c) /* put lexical character back on input */ { if (c == '\n') lineno--; if (yysptr >= yysbuf + sizeof(yysbuf)) FATAL("pushed back too much: %.20s...", yysbuf); *yysptr++ = c; if (--ep < ebuf) ep = ebuf + sizeof(ebuf) - 1; } void unputstr(const char *s) /* put a string back on input */ { int i; for (i = strlen(s)-1; i >= 0; i--) unput(s[i]); }
15,076
609
jart/cosmopolitan
false
cosmopolitan/third_party/awk/lib.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Copyright (C) Lucent Technologies 1997 │ │ All Rights Reserved │ │ │ │ Permission to use, copy, modify, and distribute this software and │ │ its documentation for any purpose and without fee is hereby │ │ granted, provided that the above copyright notice appear in all │ │ copies and that both that the copyright notice and this │ │ permission notice and warranty disclaimer appear in supporting │ │ documentation, and that the name Lucent Technologies or any of │ │ its entities not be used in advertising or publicity pertaining │ │ to distribution of the software without specific, written prior │ │ permission. │ │ │ │ LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, │ │ INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. │ │ IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY │ │ SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES │ │ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER │ │ IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, │ │ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF │ │ THIS SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #define DEBUG #include "libc/calls/calls.h" #include "libc/errno.h" #include "libc/fmt/fmt.h" #include "libc/limits.h" #include "libc/math.h" #include "libc/mem/mem.h" #include "libc/runtime/runtime.h" #include "libc/str/str.h" #include "third_party/awk/awk.h" #include "third_party/gdtoa/gdtoa.h" // clang-format off char EMPTY[] = { '\0' }; static FILE *infile = NULL; bool innew; /* true = infile has not been read by readrec */ char *file = EMPTY; char *record; int recsize = RECSIZE; char *fields; int fieldssize = RECSIZE; Cell **fldtab; /* pointers to Cells */ static size_t len_inputFS = 0; static char *inputFS = NULL; /* FS at time of input, for field splitting */ #define MAXFLD 2 int nfields = MAXFLD; /* last allocated slot for $i */ bool donefld; /* true = implies rec broken into fields */ bool donerec; /* true = record is valid (no flds have changed) */ int lastfld = 0; /* last used field */ int argno = 1; /* current input argument number */ extern Awkfloat *ARGC; static Cell dollar0 = { OCELL, CFLD, NULL, EMPTY, 0.0, REC|STR|DONTFREE, NULL, NULL }; static Cell dollar1 = { OCELL, CFLD, NULL, EMPTY, 0.0, FLD|STR|DONTFREE, NULL, NULL }; void recinit(unsigned int n) { if ( (record = (char *) malloc(n)) == NULL || (fields = (char *) malloc(n+1)) == NULL || (fldtab = (Cell **) calloc(nfields+2, sizeof(*fldtab))) == NULL || (fldtab[0] = (Cell *) malloc(sizeof(**fldtab))) == NULL) FATAL("out of space for $0 and fields"); *record = '\0'; *fldtab[0] = dollar0; fldtab[0]->sval = record; fldtab[0]->nval = tostring("0"); makefields(1, nfields); } void makefields(int n1, int n2) /* create $n1..$n2 inclusive */ { char temp[50]; int i; for (i = n1; i <= n2; i++) { fldtab[i] = (Cell *) malloc(sizeof(**fldtab)); if (fldtab[i] == NULL) FATAL("out of space in makefields %d", i); *fldtab[i] = dollar1; snprintf(temp, sizeof(temp), "%d", i); fldtab[i]->nval = tostring(temp); } } void initgetrec(void) { int i; char *p; for (i = 1; i < *ARGC; i++) { p = getargv(i); /* find 1st real filename */ if (p == NULL || *p == '\0') { /* deleted or zapped */ argno++; continue; } if (!isclvar(p)) { setsval(lookup("FILENAME", symtab), p); return; } setclvar(p); /* a commandline assignment before filename */ argno++; } infile = stdin; /* no filenames, so use stdin */ innew = true; } /* * POSIX specifies that fields are supposed to be evaluated as if they were * split using the value of FS at the time that the record's value ($0) was * read. * * Since field-splitting is done lazily, we save the current value of FS * whenever a new record is read in (implicitly or via getline), or when * a new value is assigned to $0. */ void savefs(void) { size_t len; if ((len = strlen(getsval(fsloc))) < len_inputFS) { strcpy(inputFS, *FS); /* for subsequent field splitting */ return; } len_inputFS = len + 1; inputFS = (char *) realloc(inputFS, len_inputFS); if (inputFS == NULL) FATAL("field separator %.10s... is too long", *FS); memcpy(inputFS, *FS, len_inputFS); } static bool firsttime = true; int getrec(char **pbuf, int *pbufsize, bool isrecord) /* get next input record */ { /* note: cares whether buf == record */ int c; char *buf = *pbuf; uschar saveb0; int bufsize = *pbufsize, savebufsize = bufsize; if (firsttime) { firsttime = false; initgetrec(); } DPRINTF("RS=<%s>, FS=<%s>, ARGC=%g, FILENAME=%s\n", *RS, *FS, *ARGC, *FILENAME); if (isrecord) { donefld = false; donerec = true; savefs(); } saveb0 = buf[0]; buf[0] = 0; while (argno < *ARGC || infile == stdin) { DPRINTF("argno=%d, file=|%s|\n", argno, file); if (infile == NULL) { /* have to open a new file */ file = getargv(argno); if (file == NULL || *file == '\0') { /* deleted or zapped */ argno++; continue; } if (isclvar(file)) { /* a var=value arg */ setclvar(file); argno++; continue; } *FILENAME = file; DPRINTF("opening file %s\n", file); if (*file == '-' && *(file+1) == '\0') infile = stdin; else if ((infile = fopen(file, "r")) == NULL) FATAL("can't open file %s", file); innew = true; setfval(fnrloc, 0.0); } c = readrec(&buf, &bufsize, infile, innew); if (innew) innew = false; if (c != 0 || buf[0] != '\0') { /* normal record */ if (isrecord) { double result; if (freeable(fldtab[0])) xfree(fldtab[0]->sval); fldtab[0]->sval = buf; /* buf == record */ fldtab[0]->tval = REC | STR | DONTFREE; if (is_number(fldtab[0]->sval, & result)) { fldtab[0]->fval = result; fldtab[0]->tval |= NUM; } } setfval(nrloc, nrloc->fval+1); setfval(fnrloc, fnrloc->fval+1); *pbuf = buf; *pbufsize = bufsize; return 1; } /* EOF arrived on this file; set up next */ if (infile != stdin) fclose(infile); infile = NULL; argno++; } buf[0] = saveb0; *pbuf = buf; *pbufsize = savebufsize; return 0; /* true end of file */ } void nextfile(void) { if (infile != NULL && infile != stdin) fclose(infile); infile = NULL; argno++; } int readrec(char **pbuf, int *pbufsize, FILE *inf, bool newflag) /* read one record into buf */ { int sep, c, isrec; char *rr, *buf = *pbuf; int bufsize = *pbufsize; char *rs = getsval(rsloc); if (*rs && rs[1]) { bool found; fa *pfa = makedfa(rs, 1); if (newflag) found = fnematch(pfa, inf, &buf, &bufsize, recsize); else { int tempstat = pfa->initstat; pfa->initstat = 2; found = fnematch(pfa, inf, &buf, &bufsize, recsize); pfa->initstat = tempstat; } if (found) setptr(patbeg, '\0'); isrec = (found == 0 && *buf == '\0') ? false : true; } else { if ((sep = *rs) == 0) { sep = '\n'; while ((c=getc(inf)) == '\n' && c != EOF) /* skip leading \n's */ ; if (c != EOF) ungetc(c, inf); } for (rr = buf; ; ) { for (; (c=getc(inf)) != sep && c != EOF; ) { if (rr-buf+1 > bufsize) if (!adjbuf(&buf, &bufsize, 1+rr-buf, recsize, &rr, "readrec 1")) FATAL("input record `%.30s...' too long", buf); *rr++ = c; } if (*rs == sep || c == EOF) break; if ((c = getc(inf)) == '\n' || c == EOF) /* 2 in a row */ break; if (!adjbuf(&buf, &bufsize, 2+rr-buf, recsize, &rr, "readrec 2")) FATAL("input record `%.30s...' too long", buf); *rr++ = '\n'; *rr++ = c; } if (!adjbuf(&buf, &bufsize, 1+rr-buf, recsize, &rr, "readrec 3")) FATAL("input record `%.30s...' too long", buf); *rr = 0; isrec = (c == EOF && rr == buf) ? false : true; } *pbuf = buf; *pbufsize = bufsize; DPRINTF("readrec saw <%s>, returns %d\n", buf, isrec); return isrec; } char *getargv(int n) /* get ARGV[n] */ { Cell *x; char *s, temp[50]; extern Array *ARGVtab; snprintf(temp, sizeof(temp), "%d", n); if (lookup(temp, ARGVtab) == NULL) return NULL; x = setsymtab(temp, "", 0.0, STR, ARGVtab); s = getsval(x); DPRINTF("getargv(%d) returns |%s|\n", n, s); return s; } void setclvar(char *s) /* set var=value from s */ { char *e, *p; Cell *q; double result; for (p=s; *p != '='; p++) ; e = p; *p++ = 0; p = qstring(p, '\0'); q = setsymtab(s, p, 0.0, STR, symtab); setsval(q, p); if (is_number(q->sval, & result)) { q->fval = result; q->tval |= NUM; } DPRINTF("command line set %s to |%s|\n", s, p); *e = '='; } void fldbld(void) /* create fields from current record */ { /* this relies on having fields[] the same length as $0 */ /* the fields are all stored in this one array with \0's */ /* possibly with a final trailing \0 not associated with any field */ char *r, *fr, sep; Cell *p; int i, j, n; if (donefld) return; if (!isstr(fldtab[0])) getsval(fldtab[0]); r = fldtab[0]->sval; n = strlen(r); if (n > fieldssize) { xfree(fields); if ((fields = (char *) malloc(n+2)) == NULL) /* possibly 2 final \0s */ FATAL("out of space for fields in fldbld %d", n); fieldssize = n; } fr = fields; i = 0; /* number of fields accumulated here */ if (inputFS == NULL) /* make sure we have a copy of FS */ savefs(); if (strlen(inputFS) > 1) { /* it's a regular expression */ i = refldbld(r, inputFS); } else if ((sep = *inputFS) == ' ') { /* default whitespace */ for (i = 0; ; ) { while (*r == ' ' || *r == '\t' || *r == '\n') r++; if (*r == 0) break; i++; if (i > nfields) growfldtab(i); if (freeable(fldtab[i])) xfree(fldtab[i]->sval); fldtab[i]->sval = fr; fldtab[i]->tval = FLD | STR | DONTFREE; do *fr++ = *r++; while (*r != ' ' && *r != '\t' && *r != '\n' && *r != '\0'); *fr++ = 0; } *fr = 0; } else if ((sep = *inputFS) == 0) { /* new: FS="" => 1 char/field */ for (i = 0; *r != '\0'; r += n) { char buf[MB_LEN_MAX + 1]; i++; if (i > nfields) growfldtab(i); if (freeable(fldtab[i])) xfree(fldtab[i]->sval); n = mblen(r, MB_LEN_MAX); if (n < 0) n = 1; memcpy(buf, r, n); buf[n] = '\0'; fldtab[i]->sval = tostring(buf); fldtab[i]->tval = FLD | STR; } *fr = 0; } else if (*r != 0) { /* if 0, it's a null field */ /* subtlecase : if length(FS) == 1 && length(RS > 0) * \n is NOT a field separator (cf awk book 61,84). * this variable is tested in the inner while loop. */ int rtest = '\n'; /* normal case */ if (strlen(*RS) > 0) rtest = '\0'; for (;;) { i++; if (i > nfields) growfldtab(i); if (freeable(fldtab[i])) xfree(fldtab[i]->sval); fldtab[i]->sval = fr; fldtab[i]->tval = FLD | STR | DONTFREE; while (*r != sep && *r != rtest && *r != '\0') /* \n is always a separator */ *fr++ = *r++; *fr++ = 0; if (*r++ == 0) break; } *fr = 0; } if (i > nfields) FATAL("record `%.30s...' has too many fields; can't happen", r); cleanfld(i+1, lastfld); /* clean out junk from previous record */ lastfld = i; donefld = true; for (j = 1; j <= lastfld; j++) { double result; p = fldtab[j]; if(is_number(p->sval, & result)) { p->fval = result; p->tval |= NUM; } } setfval(nfloc, (Awkfloat) lastfld); donerec = true; /* restore */ if (dbg) { for (j = 0; j <= lastfld; j++) { p = fldtab[j]; printf("field %d (%s): |%s|\n", j, p->nval, p->sval); } } } void cleanfld(int n1, int n2) /* clean out fields n1 .. n2 inclusive */ { /* nvals remain intact */ Cell *p; int i; for (i = n1; i <= n2; i++) { p = fldtab[i]; if (freeable(p)) xfree(p->sval); p->sval = EMPTY, p->tval = FLD | STR | DONTFREE; } } void newfld(int n) /* add field n after end of existing lastfld */ { if (n > nfields) growfldtab(n); cleanfld(lastfld+1, n); lastfld = n; setfval(nfloc, (Awkfloat) n); } void setlastfld(int n) /* set lastfld cleaning fldtab cells if necessary */ { if (n < 0) FATAL("cannot set NF to a negative value"); if (n > nfields) growfldtab(n); if (lastfld < n) cleanfld(lastfld+1, n); else cleanfld(n+1, lastfld); lastfld = n; } Cell *fieldadr(int n) /* get nth field */ { if (n < 0) FATAL("trying to access out of range field %d", n); if (n > nfields) /* fields after NF are empty */ growfldtab(n); /* but does not increase NF */ return(fldtab[n]); } void growfldtab(int n) /* make new fields up to at least $n */ { int nf = 2 * nfields; size_t s; if (n > nf) nf = n; s = (nf+1) * (sizeof (struct Cell *)); /* freebsd: how much do we need? */ if (s / sizeof(struct Cell *) - 1 == (size_t)nf) /* didn't overflow */ fldtab = (Cell **) realloc(fldtab, s); else /* overflow sizeof int */ xfree(fldtab); /* make it null */ if (fldtab == NULL) FATAL("out of space creating %d fields", nf); makefields(nfields+1, nf); nfields = nf; } int refldbld(const char *rec, const char *fs) /* build fields from reg expr in FS */ { /* this relies on having fields[] the same length as $0 */ /* the fields are all stored in this one array with \0's */ char *fr; int i, tempstat, n; fa *pfa; n = strlen(rec); if (n > fieldssize) { xfree(fields); if ((fields = (char *) malloc(n+1)) == NULL) FATAL("out of space for fields in refldbld %d", n); fieldssize = n; } fr = fields; *fr = '\0'; if (*rec == '\0') return 0; pfa = makedfa(fs, 1); DPRINTF("into refldbld, rec = <%s>, pat = <%s>\n", rec, fs); tempstat = pfa->initstat; for (i = 1; ; i++) { if (i > nfields) growfldtab(i); if (freeable(fldtab[i])) xfree(fldtab[i]->sval); fldtab[i]->tval = FLD | STR | DONTFREE; fldtab[i]->sval = fr; DPRINTF("refldbld: i=%d\n", i); if (nematch(pfa, rec)) { pfa->initstat = 2; /* horrible coupling to b.c */ DPRINTF("match %s (%d chars)\n", patbeg, patlen); strncpy(fr, rec, patbeg-rec); fr += patbeg - rec + 1; *(fr-1) = '\0'; rec = patbeg + patlen; } else { DPRINTF("no match %s\n", rec); strcpy(fr, rec); pfa->initstat = tempstat; break; } } return i; } void recbld(void) /* create $0 from $1..$NF if necessary */ { int i; char *r, *p; char *sep = getsval(ofsloc); if (donerec) return; r = record; for (i = 1; i <= *NF; i++) { p = getsval(fldtab[i]); if (!adjbuf(&record, &recsize, 1+strlen(p)+r-record, recsize, &r, "recbld 1")) FATAL("created $0 `%.30s...' too long", record); while ((*r = *p++) != 0) r++; if (i < *NF) { if (!adjbuf(&record, &recsize, 2+strlen(sep)+r-record, recsize, &r, "recbld 2")) FATAL("created $0 `%.30s...' too long", record); for (p = sep; (*r = *p++) != 0; ) r++; } } if (!adjbuf(&record, &recsize, 2+r-record, recsize, &r, "recbld 3")) FATAL("built giant record `%.30s...'", record); *r = '\0'; DPRINTF("in recbld inputFS=%s, fldtab[0]=%p\n", inputFS, (void*)fldtab[0]); if (freeable(fldtab[0])) xfree(fldtab[0]->sval); fldtab[0]->tval = REC | STR | DONTFREE; fldtab[0]->sval = record; DPRINTF("in recbld inputFS=%s, fldtab[0]=%p\n", inputFS, (void*)fldtab[0]); DPRINTF("recbld = |%s|\n", record); donerec = true; } int errorflag = 0; void yyerror(const char *s) { SYNTAX("%s", s); } extern char *cmdname; void SYNTAX(const char *fmt, ...) { extern char *curfname; static int been_here = 0; va_list varg; if (been_here++ > 2) return; fprintf(stderr, "%s: ", cmdname); va_start(varg, fmt); vfprintf(stderr, fmt, varg); va_end(varg); fprintf(stderr, " at source line %d", lineno); if (curfname != NULL) fprintf(stderr, " in function %s", curfname); if (compile_time == COMPILING && cursource() != NULL) fprintf(stderr, " source file %s", cursource()); fprintf(stderr, "\n"); errorflag = 2; eprint(); } extern int bracecnt, brackcnt, parencnt; void bracecheck(void) { int c; static int beenhere = 0; if (beenhere++) return; while ((c = input()) != EOF && c != '\0') bclass(c); bcheck2(bracecnt, '{', '}'); bcheck2(brackcnt, '[', ']'); bcheck2(parencnt, '(', ')'); } void bcheck2(int n, int c1, int c2) { if (n == 1) fprintf(stderr, "\tmissing %c\n", c2); else if (n > 1) fprintf(stderr, "\t%d missing %c's\n", n, c2); else if (n == -1) fprintf(stderr, "\textra %c\n", c2); else if (n < -1) fprintf(stderr, "\t%d extra %c's\n", -n, c2); } void FATAL(const char *fmt, ...) { va_list varg; fflush(stdout); fprintf(stderr, "%s: ", cmdname); va_start(varg, fmt); vfprintf(stderr, fmt, varg); va_end(varg); error(); if (dbg > 1) /* core dump if serious debugging on */ abort(); exit(2); } void WARNING(const char *fmt, ...) { va_list varg; fflush(stdout); fprintf(stderr, "%s: ", cmdname); va_start(varg, fmt); vfprintf(stderr, fmt, varg); va_end(varg); error(); } void error() { extern Node *curnode; fprintf(stderr, "\n"); if (compile_time != ERROR_PRINTING) { if (NR && *NR > 0) { fprintf(stderr, " input record number %d", (int) (*FNR)); if (strcmp(*FILENAME, "-") != 0) fprintf(stderr, ", file %s", *FILENAME); fprintf(stderr, "\n"); } if (curnode) fprintf(stderr, " source line number %d", curnode->lineno); else if (lineno) fprintf(stderr, " source line number %d", lineno); if (compile_time == COMPILING && cursource() != NULL) fprintf(stderr, " source file %s", cursource()); fprintf(stderr, "\n"); eprint(); } } void eprint(void) /* try to print context around error */ { char *p, *q; int c; static int been_here = 0; extern char ebuf[], *ep; if (compile_time != COMPILING || been_here++ > 0 || ebuf == ep) return; if (ebuf == ep) return; p = ep - 1; if (p > ebuf && *p == '\n') p--; for ( ; p > ebuf && *p != '\n' && *p != '\0'; p--) ; while (*p == '\n') p++; fprintf(stderr, " context is\n\t"); for (q=ep-1; q>=p && *q!=' ' && *q!='\t' && *q!='\n'; q--) ; for ( ; p < q; p++) if (*p) putc(*p, stderr); fprintf(stderr, " >>> "); for ( ; p < ep; p++) if (*p) putc(*p, stderr); fprintf(stderr, " <<< "); if (*ep) while ((c = input()) != '\n' && c != '\0' && c != EOF) { putc(c, stderr); bclass(c); } putc('\n', stderr); ep = ebuf; } void bclass(int c) { switch (c) { case '{': bracecnt++; break; case '}': bracecnt--; break; case '[': brackcnt++; break; case ']': brackcnt--; break; case '(': parencnt++; break; case ')': parencnt--; break; } } double errcheck(double x, const char *s) { if (errno == EDOM) { errno = 0; WARNING("%s argument out of domain", s); x = 1; } else if (errno == ERANGE) { errno = 0; WARNING("%s result out of range", s); x = 1; } return x; } int isclvar(const char *s) /* is s of form var=something ? */ { const char *os = s; if (!isalpha((uschar) *s) && *s != '_') return 0; for ( ; *s; s++) if (!(isalnum((uschar) *s) || *s == '_')) break; return *s == '=' && s > os; } /* strtod is supposed to be a proper test of what's a valid number */ /* appears to be broken in gcc on linux: thinks 0x123 is a valid FP number */ /* wrong: violates 4.10.1.4 of ansi C standard */ /* well, not quite. As of C99, hex floating point is allowed. so this is * a bit of a mess. We work around the mess by checking for a hexadecimal * value and disallowing it. Similarly, we now follow gawk and allow only * +nan, -nan, +inf, and -inf for NaN and infinity values. */ /* * This routine now has a more complicated interface, the main point * being to avoid the double conversion of a string to double, and * also to convey out, if requested, the information that the numeric * value was a leading string or is all of the string. The latter bit * is used in getfval(). */ bool is_valid_number(const char *s, bool trailing_stuff_ok, bool *no_trailing, double *result) { double r; char *ep; bool retval = false; bool is_nan = false; bool is_inf = false; if (no_trailing) *no_trailing = false; while (isspace(*s)) s++; // no hex floating point, sorry if (s[0] == '0' && tolower(s[1]) == 'x') return false; // allow +nan, -nan, +inf, -inf, any other letter, no if (s[0] == '+' || s[0] == '-') { is_nan = (strncasecmp(s+1, "nan", 3) == 0); is_inf = (strncasecmp(s+1, "inf", 3) == 0); if ((is_nan || is_inf) && (isspace(s[4]) || s[4] == '\0')) goto convert; else if (! isdigit(s[1]) && s[1] != '.') return false; } else if (! isdigit(s[0]) && s[0] != '.') return false; convert: errno = 0; r = strtod(s, &ep); if (ep == s || errno == ERANGE) return false; if (isnan(r) && s[0] == '-' && signbit(r) == 0) r = -r; if (result != NULL) *result = r; /* * check for trailing stuff */ while (isspace(*ep)) ep++; if (no_trailing != NULL) *no_trailing = (*ep == '\0'); // return true if found the end, or trailing stuff is allowed retval = *ep == '\0' || trailing_stuff_ok; return retval; }
22,140
847
jart/cosmopolitan
false
cosmopolitan/third_party/awk/proctab.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Copyright (C) Lucent Technologies 1997 │ │ All Rights Reserved │ │ │ │ Permission to use, copy, modify, and distribute this software and │ │ its documentation for any purpose and without fee is hereby │ │ granted, provided that the above copyright notice appear in all │ │ copies and that both that the copyright notice and this │ │ permission notice and warranty disclaimer appear in supporting │ │ documentation, and that the name Lucent Technologies or any of │ │ its entities not be used in advertising or publicity pertaining │ │ to distribution of the software without specific, written prior │ │ permission. │ │ │ │ LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, │ │ INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. │ │ IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY │ │ SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES │ │ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER │ │ IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, │ │ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF │ │ THIS SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/fmt/fmt.h" #include "third_party/awk/awk.h" #include "third_party/awk/awkgram.tab.h" // clang-format off static const char * const printname[95] = { "FIRSTTOKEN", /* 258 */ "PROGRAM", /* 259 */ "PASTAT", /* 260 */ "PASTAT2", /* 261 */ "XBEGIN", /* 262 */ "XEND", /* 263 */ "NL", /* 264 */ "ARRAY", /* 265 */ "MATCH", /* 266 */ "NOTMATCH", /* 267 */ "MATCHOP", /* 268 */ "FINAL", /* 269 */ "DOT", /* 270 */ "ALL", /* 271 */ "CCL", /* 272 */ "NCCL", /* 273 */ "CHAR", /* 274 */ "OR", /* 275 */ "STAR", /* 276 */ "QUEST", /* 277 */ "PLUS", /* 278 */ "EMPTYRE", /* 279 */ "ZERO", /* 280 */ "AND", /* 281 */ "BOR", /* 282 */ "APPEND", /* 283 */ "EQ", /* 284 */ "GE", /* 285 */ "GT", /* 286 */ "LE", /* 287 */ "LT", /* 288 */ "NE", /* 289 */ "IN", /* 290 */ "ARG", /* 291 */ "BLTIN", /* 292 */ "BREAK", /* 293 */ "CLOSE", /* 294 */ "CONTINUE", /* 295 */ "DELETE", /* 296 */ "DO", /* 297 */ "EXIT", /* 298 */ "FOR", /* 299 */ "FUNC", /* 300 */ "SUB", /* 301 */ "GSUB", /* 302 */ "IF", /* 303 */ "INDEX", /* 304 */ "LSUBSTR", /* 305 */ "MATCHFCN", /* 306 */ "NEXT", /* 307 */ "NEXTFILE", /* 308 */ "ADD", /* 309 */ "MINUS", /* 310 */ "MULT", /* 311 */ "DIVIDE", /* 312 */ "MOD", /* 313 */ "ASSIGN", /* 314 */ "ASGNOP", /* 315 */ "ADDEQ", /* 316 */ "SUBEQ", /* 317 */ "MULTEQ", /* 318 */ "DIVEQ", /* 319 */ "MODEQ", /* 320 */ "POWEQ", /* 321 */ "PRINT", /* 322 */ "PRINTF", /* 323 */ "SPRINTF", /* 324 */ "ELSE", /* 325 */ "INTEST", /* 326 */ "CONDEXPR", /* 327 */ "POSTINCR", /* 328 */ "PREINCR", /* 329 */ "POSTDECR", /* 330 */ "PREDECR", /* 331 */ "VAR", /* 332 */ "IVAR", /* 333 */ "VARNF", /* 334 */ "CALL", /* 335 */ "NUMBER", /* 336 */ "STRING", /* 337 */ "REGEXPR", /* 338 */ "GETLINE", /* 339 */ "RETURN", /* 340 */ "SPLIT", /* 341 */ "SUBSTR", /* 342 */ "WHILE", /* 343 */ "CAT", /* 344 */ "NOT", /* 345 */ "UMINUS", /* 346 */ "UPLUS", /* 347 */ "POWER", /* 348 */ "DECR", /* 349 */ "INCR", /* 350 */ "INDIRECT", /* 351 */ "LASTTOKEN", /* 352 */ }; Cell *(*proctab[95])(Node **, int) = { nullproc, /* FIRSTTOKEN */ program, /* PROGRAM */ pastat, /* PASTAT */ dopa2, /* PASTAT2 */ nullproc, /* XBEGIN */ nullproc, /* XEND */ nullproc, /* NL */ array, /* ARRAY */ matchop, /* MATCH */ matchop, /* NOTMATCH */ nullproc, /* MATCHOP */ nullproc, /* FINAL */ nullproc, /* DOT */ nullproc, /* ALL */ nullproc, /* CCL */ nullproc, /* NCCL */ nullproc, /* CHAR */ nullproc, /* OR */ nullproc, /* STAR */ nullproc, /* QUEST */ nullproc, /* PLUS */ nullproc, /* EMPTYRE */ nullproc, /* ZERO */ boolop, /* AND */ boolop, /* BOR */ nullproc, /* APPEND */ relop, /* EQ */ relop, /* GE */ relop, /* GT */ relop, /* LE */ relop, /* LT */ relop, /* NE */ instat, /* IN */ arg, /* ARG */ bltin, /* BLTIN */ jump, /* BREAK */ closefile, /* CLOSE */ jump, /* CONTINUE */ awkdelete, /* DELETE */ dostat, /* DO */ jump, /* EXIT */ forstat, /* FOR */ nullproc, /* FUNC */ sub, /* SUB */ gsub, /* GSUB */ ifstat, /* IF */ sindex, /* INDEX */ nullproc, /* LSUBSTR */ matchop, /* MATCHFCN */ jump, /* NEXT */ jump, /* NEXTFILE */ arith, /* ADD */ arith, /* MINUS */ arith, /* MULT */ arith, /* DIVIDE */ arith, /* MOD */ assign, /* ASSIGN */ nullproc, /* ASGNOP */ assign, /* ADDEQ */ assign, /* SUBEQ */ assign, /* MULTEQ */ assign, /* DIVEQ */ assign, /* MODEQ */ assign, /* POWEQ */ printstat, /* PRINT */ awkprintf, /* PRINTF */ awksprintf, /* SPRINTF */ nullproc, /* ELSE */ intest, /* INTEST */ condexpr, /* CONDEXPR */ incrdecr, /* POSTINCR */ incrdecr, /* PREINCR */ incrdecr, /* POSTDECR */ incrdecr, /* PREDECR */ nullproc, /* VAR */ nullproc, /* IVAR */ getnf, /* VARNF */ call, /* CALL */ nullproc, /* NUMBER */ nullproc, /* STRING */ nullproc, /* REGEXPR */ awkgetline, /* GETLINE */ jump, /* RETURN */ split, /* SPLIT */ substr, /* SUBSTR */ whilestat, /* WHILE */ cat, /* CAT */ boolop, /* NOT */ arith, /* UMINUS */ arith, /* UPLUS */ arith, /* POWER */ nullproc, /* DECR */ nullproc, /* INCR */ indirect, /* INDIRECT */ nullproc, /* LASTTOKEN */ }; const char *tokname(int n) { static char buf[100]; if (n < FIRSTTOKEN || n > LASTTOKEN) { snprintf(buf, sizeof(buf), "token %d", n); return buf; } return printname[n-FIRSTTOKEN]; }
6,941
240
jart/cosmopolitan
false
cosmopolitan/third_party/awk/README.cosmo
DESCRIPTION The One True Awk This is the version of awk described in The AWK Programming Language, by Al Aho, Brian Kernighan, and Peter Weinberger (Addison-Wesley, 1988, ISBN 0-201-07981-X). SOURCE https://github.com/onetrueawk/awk commit b92d8cecd132ce8e02a373e28dd42e6be34d3d59 Author: ozan yigit <[email protected]> Date: Mon May 30 11:34:52 2022 -0400 added YACC definition for byacc.
421
18
jart/cosmopolitan
false
cosmopolitan/third_party/awk/README
AWK(1) General Commands Manual AWK(1) 𝐍𝐀𝐌𝐄 awk - pattern-directed scanning and processing language 𝐒𝐘𝐍𝐎𝐏𝐒𝐈𝐒 𝗮𝘄𝗸 [ -𝐅 f̲s̲ ] [ -𝘃 v̲a̲r̲=̲v̲a̲l̲u̲e̲ ] [ '̲p̲r̲o̲g̲'̲ | -𝗳 p̲r̲o̲g̲f̲i̲l̲e̲ ] [ f̲i̲l̲e̲ .̲.̲.̲ ] 𝐃𝐄𝐒𝐂𝐑𝐈𝐏𝐓𝐈𝐎𝐍 A̲w̲k̲ scans each input f̲i̲l̲e̲ for lines that match any of a set of patterns specified literally in p̲r̲o̲g̲ or in one or more files specified as -𝗳 p̲r̲o̲g̲‐̲ f̲i̲l̲e̲. With each pattern there can be an associated action that will be performed when a line of a f̲i̲l̲e̲ matches the pattern. Each line is matched against the pattern portion of every pattern-action statement; the associated action is performed for each matched pattern. The file name - means the standard input. Any f̲i̲l̲e̲ of the form v̲a̲r̲=̲v̲a̲l̲u̲e̲ is treated as an assignment, not a filename, and is executed at the time it would have been opened if it were a filename. The option -𝘃 followed by v̲a̲r̲=̲v̲a̲l̲u̲e̲ is an assignment to be done before p̲r̲o̲g̲ is executed; any number of -𝘃 options may be present. The -𝐅 f̲s̲ option defines the input field separator to be the regular expression f̲s̲. An input line is normally made up of fields separated by white space, or by the regular expression 𝐅𝐒. The fields are denoted $𝟭, $𝟮, ..., while $𝟬 refers to the entire line. If 𝐅𝐒 is null, the input line is split into one field per character. A pattern-action statement has the form: p̲a̲t̲t̲e̲r̲n̲ { a̲c̲t̲i̲o̲n̲ } A missing { a̲c̲t̲i̲o̲n̲ } means print the line; a missing pattern always matches. Pattern-action statements are separated by newlines or semi‐ colons. An action is a sequence of statements. A statement can be one of the following: if( e̲x̲p̲r̲e̲s̲s̲i̲o̲n̲ ) s̲t̲a̲t̲e̲m̲e̲n̲t̲ [ else s̲t̲a̲t̲e̲m̲e̲n̲t̲ ] while( e̲x̲p̲r̲e̲s̲s̲i̲o̲n̲ ) s̲t̲a̲t̲e̲m̲e̲n̲t̲ for( e̲x̲p̲r̲e̲s̲s̲i̲o̲n̲ ; e̲x̲p̲r̲e̲s̲s̲i̲o̲n̲ ; e̲x̲p̲r̲e̲s̲s̲i̲o̲n̲ ) s̲t̲a̲t̲e̲m̲e̲n̲t̲ for( v̲a̲r̲ in a̲r̲r̲a̲y̲ ) s̲t̲a̲t̲e̲m̲e̲n̲t̲ do s̲t̲a̲t̲e̲m̲e̲n̲t̲ while( e̲x̲p̲r̲e̲s̲s̲i̲o̲n̲ ) break continue { [ s̲t̲a̲t̲e̲m̲e̲n̲t̲ .̲.̲.̲ ] } e̲x̲p̲r̲e̲s̲s̲i̲o̲n̲ # commonly v̲a̲r̲ =̲ e̲x̲p̲r̲e̲s̲s̲i̲o̲n̲ print [ e̲x̲p̲r̲e̲s̲s̲i̲o̲n̲-̲l̲i̲s̲t̲ ] [ > e̲x̲p̲r̲e̲s̲s̲i̲o̲n̲ ] printf f̲o̲r̲m̲a̲t̲ [ , e̲x̲p̲r̲e̲s̲s̲i̲o̲n̲-̲l̲i̲s̲t̲ ] [ > e̲x̲p̲r̲e̲s̲s̲i̲o̲n̲ ] return [ e̲x̲p̲r̲e̲s̲s̲i̲o̲n̲ ] next # skip remaining patterns on this input line nextfile # skip rest of this file, open next, start at top delete a̲r̲r̲a̲y̲[ e̲x̲p̲r̲e̲s̲s̲i̲o̲n̲ ]# delete an array element delete a̲r̲r̲a̲y̲ # delete all elements of array exit [ e̲x̲p̲r̲e̲s̲s̲i̲o̲n̲ ] # exit immediately; status is e̲x̲p̲r̲e̲s̲s̲i̲o̲n̲ Statements are terminated by semicolons, newlines or right braces. An empty e̲x̲p̲r̲e̲s̲s̲i̲o̲n̲-̲l̲i̲s̲t̲ stands for $𝟬. String constants are quoted " ", with the usual C escapes recognized within. Expressions take on string or numeric values as appropriate, and are built using the operators + - * / % ^ (exponentiation), and concatenation (indicated by white space). The operators ! ++ -- += -= *= /= %= ^= > >= < <= == != ?: are also available in expressions. Variables may be scalars, array elements (de‐ noted x̲[i̲]) or fields. Variables are initialized to the null string. Array subscripts may be any string, not necessarily numeric; this allows for a form of associative memory. Multiple subscripts such as [𝗶,𝗷,𝗸] are permitted; the constituents are concatenated, separated by the value of 𝐒𝐔𝐁𝐒𝐄𝐏. The 𝗽𝗿𝗶𝗻𝘁 statement prints its arguments on the standard output (or on a file if > f̲i̲l̲e̲ or >> f̲i̲l̲e̲ is present or on a pipe if | c̲m̲d̲ is present), separated by the current output field separator, and terminated by the output record separator. f̲i̲l̲e̲ and c̲m̲d̲ may be literal names or parenthe‐ sized expressions; identical string values in different statements denote the same open file. The 𝗽𝗿𝗶𝗻𝘁𝗳 statement formats its expression list ac‐ cording to the f̲o̲r̲m̲a̲t̲ (see p̲r̲i̲n̲t̲f̲(3)). The built-in function 𝗰𝗹𝗼𝘀𝗲(e̲x̲p̲r̲) closes the file or pipe e̲x̲p̲r̲. The built-in function 𝗳𝗳𝗹𝘂𝘀𝗵(e̲x̲p̲r̲) flushes any buffered output for the file or pipe e̲x̲p̲r̲. The mathematical functions 𝗮𝘁𝗮𝗻𝟮, 𝗰𝗼𝘀, 𝗲𝘅𝗽, 𝗹𝗼𝗴, 𝘀𝗶𝗻, and 𝘀𝗾𝗿𝘁 are built in. Other built-in functions: 𝗹𝗲𝗻𝗴𝘁𝗵 the length of its argument taken as a string, number of elements in an array for an array argument, or length of $𝟬 if no argu‐ ment. 𝗿𝗮𝗻𝗱 random number on [0,1). 𝘀𝗿𝗮𝗻𝗱 sets seed for 𝗿𝗮𝗻𝗱 and returns the previous seed. 𝗶𝗻𝘁 truncates to an integer value. 𝘀𝘂𝗯𝘀𝘁𝗿(s̲, m̲ [, n̲]) the n̲-character substring of s̲ that begins at position m̲ counted from 1. If no n̲, use the rest of the string. 𝗶𝗻𝗱𝗲𝘅(s̲, t̲) the position in s̲ where the string t̲ occurs, or 0 if it does not. 𝗺𝗮𝘁𝗰𝗵(s̲, r̲) the position in s̲ where the regular expression r̲ occurs, or 0 if it does not. The variables 𝐑𝐒𝐓𝐀𝐑𝐓 and 𝐑𝐋𝐄𝐍𝐆𝐓𝐇 are set to the po‐ sition and length of the matched string. 𝘀𝗽𝗹𝗶𝘁(s̲, a̲ [, f̲s̲]) splits the string s̲ into array elements a̲[𝟭], a̲[𝟮], ..., a̲[n̲], and returns n̲. The separation is done with the regular expres‐ sion f̲s̲ or with the field separator 𝐅𝐒 if f̲s̲ is not given. An empty string as field separator splits the string into one array element per character. 𝘀𝘂𝗯(r̲, t̲ [, s̲]) substitutes t̲ for the first occurrence of the regular expression r̲ in the string s̲. If s̲ is not given, $𝟬 is used. 𝗴𝘀𝘂𝗯(r̲, t̲ [, s̲]) same as 𝘀𝘂𝗯 except that all occurrences of the regular expression are replaced; 𝘀𝘂𝗯 and 𝗴𝘀𝘂𝗯 return the number of replacements. 𝘀𝗽𝗿𝗶𝗻𝘁𝗳(f̲m̲t̲, e̲x̲p̲r̲, .̲.̲.̲) the string resulting from formatting e̲x̲p̲r̲ .̲.̲.̲ according to the p̲r̲i̲n̲t̲f̲(3) format f̲m̲t̲. 𝘀𝘆𝘀𝘁𝗲𝗺(c̲m̲d̲) executes c̲m̲d̲ and returns its exit status. This will be -1 upon error, c̲m̲d̲'s exit status upon a normal exit, 256 + s̲i̲g̲ upon death-by-signal, where s̲i̲g̲ is the number of the murdering signal, or 512 + s̲i̲g̲ if there was a core dump. 𝘁𝗼𝗹𝗼𝘄𝗲𝗿(s̲t̲r̲) returns a copy of s̲t̲r̲ with all upper-case characters translated to their corresponding lower-case equivalents. 𝘁𝗼𝘂𝗽𝗽𝗲𝗿(s̲t̲r̲) returns a copy of s̲t̲r̲ with all lower-case characters translated to their corresponding upper-case equivalents. The ``function'' 𝗴𝗲𝘁𝗹𝗶𝗻𝗲 sets $𝟬 to the next input record from the cur‐ rent input file; 𝗴𝗲𝘁𝗹𝗶𝗻𝗲 < f̲i̲l̲e̲ sets $𝟬 to the next record from f̲i̲l̲e̲. 𝗴𝗲𝘁𝗹𝗶𝗻𝗲 x̲ sets variable x̲ instead. Finally, c̲m̲d̲ | 𝗴𝗲𝘁𝗹𝗶𝗻𝗲 pipes the out‐ put of c̲m̲d̲ into 𝗴𝗲𝘁𝗹𝗶𝗻𝗲; each call of 𝗴𝗲𝘁𝗹𝗶𝗻𝗲 returns the next line of output from c̲m̲d̲. In all cases, 𝗴𝗲𝘁𝗹𝗶𝗻𝗲 returns 1 for a successful input, 0 for end of file, and -1 for an error. Patterns are arbitrary Boolean combinations (with ! || &&) of regular ex‐ pressions and relational expressions. Regular expressions are as in e̲g̲r̲e̲p̲; see g̲r̲e̲p̲(1). Isolated regular expressions in a pattern apply to the entire line. Regular expressions may also occur in relational ex‐ pressions, using the operators ~ and !~. /r̲e̲/ is a constant regular ex‐ pression; any string (constant or variable) may be used as a regular ex‐ pression, except in the position of an isolated regular expression in a pattern. A pattern may consist of two patterns separated by a comma; in this case, the action is performed for all lines from an occurrence of the first pattern though an occurrence of the second. A relational expression is one of the following: e̲x̲p̲r̲e̲s̲s̲i̲o̲n̲ m̲a̲t̲c̲h̲o̲p̲ r̲e̲g̲u̲l̲a̲r̲-̲e̲x̲p̲r̲e̲s̲s̲i̲o̲n̲ e̲x̲p̲r̲e̲s̲s̲i̲o̲n̲ r̲e̲l̲o̲p̲ e̲x̲p̲r̲e̲s̲s̲i̲o̲n̲ e̲x̲p̲r̲e̲s̲s̲i̲o̲n̲ 𝗶𝗻 a̲r̲r̲a̲y̲-̲n̲a̲m̲e̲ (e̲x̲p̲r̲,e̲x̲p̲r̲,̲.̲.̲.̲) 𝗶𝗻 a̲r̲r̲a̲y̲-̲n̲a̲m̲e̲ where a r̲e̲l̲o̲p̲ is any of the six relational operators in C, and a m̲a̲t̲c̲h̲o̲p̲ is either ~ (matches) or !~ (does not match). A conditional is an arith‐ metic expression, a relational expression, or a Boolean combination of these. The special patterns 𝐁𝐄𝐆𝐈𝐍 and 𝐄𝐍𝐃 may be used to capture control before the first input line is read and after the last. 𝐁𝐄𝐆𝐈𝐍 and 𝐄𝐍𝐃 do not combine with other patterns. They may appear multiple times in a program and execute in the order they are read by a̲w̲k̲. Variable names with special meanings: 𝐀𝐑𝐆𝐂 argument count, assignable. 𝐀𝐑𝐆𝐕 argument array, assignable; non-null members are taken as file‐ names. 𝐂𝐎𝐍𝐕𝐅𝐌𝐓 conversion format used when converting numbers (default %.𝟲𝗴). 𝐄𝐍𝐕𝐈𝐑𝐎𝐍 array of environment variables; subscripts are names. 𝐅𝐈𝐋𝐄𝐍𝐀𝐌𝐄 the name of the current input file. 𝐅𝐍𝐑 ordinal number of the current record in the current file. 𝐅𝐒 regular expression used to separate fields; also settable by option -𝐅f̲s̲. 𝐍𝐅 number of fields in the current record. 𝐍𝐑 ordinal number of the current record. 𝐎𝐅𝐌𝐓 output format for numbers (default %.𝟲𝗴). 𝐎𝐅𝐒 output field separator (default space). 𝐎𝐑𝐒 output record separator (default newline). 𝐑𝐋𝐄𝐍𝐆𝐓𝐇 the length of a string matched by 𝗺𝗮𝘁𝗰𝗵. 𝐑𝐒 input record separator (default newline). If empty, blank lines separate records. If more than one character long, 𝐑𝐒 is treated as a regular expression, and records are separated by text matching the expression. 𝐑𝐒𝐓𝐀𝐑𝐓 the start position of a string matched by 𝗺𝗮𝘁𝗰𝗵. 𝐒𝐔𝐁𝐒𝐄𝐏 separates multiple subscripts (default 034). Functions may be defined (at the position of a pattern-action statement) thus: 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻 𝗳𝗼𝗼(𝗮, 𝗯, 𝗰) { ...; 𝗿𝗲𝘁𝘂𝗿𝗻 𝘅 } Parameters are passed by value if scalar and by reference if array name; functions may be called recursively. Parameters are local to the func‐ tion; all other variables are global. Thus local variables may be cre‐ ated by providing excess parameters in the function definition. 𝐄𝐍𝐕𝐈𝐑𝐎𝐍𝐌𝐄𝐍𝐓 𝐕𝐀𝐑𝐈𝐀𝐁𝐋𝐄𝐒 If 𝐏𝐎𝐒𝐈𝐗𝐋𝐘_𝐂𝐎𝐑𝐑𝐄𝐂𝐓 is set in the environment, then a̲w̲k̲ follows the POSIX rules for 𝘀𝘂𝗯 and 𝗴𝘀𝘂𝗯 with respect to consecutive backslashes and amper‐ sands. 𝐄𝐗𝐀𝐌𝐏𝐋𝐄𝐒 length($0) > 72 Print lines longer than 72 characters. { print $2, $1 } Print first two fields in opposite order. BEGIN { FS = ",[ \t]*|[ \t]+" } { print $2, $1 } Same, with input fields separated by comma and/or spaces and tabs. { s += $1 } END { print "sum is", s, " average is", s/NR } Add up first column, print sum and average. /start/, /stop/ Print all lines between start/stop pairs. BEGIN { # Simulate echo(1) for (i = 1; i < ARGC; i++) printf "%s ", ARGV[i] printf "\n" exit } 𝐒𝐄𝐄 𝐀𝐋𝐒𝐎 g̲r̲e̲p̲(1), l̲e̲x̲(1), s̲e̲d̲(1) A. V. Aho, B. W. Kernighan, P. J. Weinberger, T̲h̲e̲ A̲W̲K̲ P̲r̲o̲g̲r̲a̲m̲m̲i̲n̲g̲ L̲a̲n̲‐̲ g̲u̲a̲g̲e̲, Addison-Wesley, 1988. ISBN 0-201-07981-X. 𝐁𝐔𝐆𝐒 There are no explicit conversions between numbers and strings. To force an expression to be treated as a number add 0 to it; to force it to be treated as a string concatenate "" to it. The scope rules for variables in functions are a botch; the syntax is worse. Only eight-bit characters sets are handled correctly. 𝐔𝐍𝐔𝐒𝐔𝐀𝐋 𝐅𝐋𝐎𝐀𝐓𝐈𝐍𝐆-𝐏𝐎𝐈𝐍𝐓 𝐕𝐀𝐋𝐔𝐄𝐒 A̲w̲k̲ was designed before IEEE 754 arithmetic defined Not-A-Number (NaN) and Infinity values, which are supported by all modern floating-point hardware. Because a̲w̲k̲ uses s̲t̲r̲t̲o̲d̲(3) and a̲t̲o̲f̲(3) to convert string values to dou‐ ble-precision floating-point values, modern C libraries also convert strings starting with 𝗶𝗻𝗳 and 𝗻𝗮𝗻 into infinity and NaN values respec‐ tively. This led to strange results, with something like this: echo nancy | awk '{ print $1 + 0 }' printing 𝗻𝗮𝗻 instead of zero. A̲w̲k̲ now follows GNU AWK, and prefilters string values before attempting to convert them to numbers, as follows: H̲e̲x̲a̲d̲e̲c̲i̲m̲a̲l̲ v̲a̲l̲u̲e̲s̲ Hexadecimal values (allowed since C99) convert to zero, as they did prior to C99. N̲a̲N̲ v̲a̲l̲u̲e̲s̲ The two strings +𝗻𝗮𝗻 and -𝗻𝗮𝗻 (case independent) convert to NaN. No others do. (NaNs can have signs.) I̲n̲f̲i̲n̲i̲t̲y̲ v̲a̲l̲u̲e̲s̲ The two strings +𝗶𝗻𝗳 and -𝗶𝗻𝗳 (case independent) convert to posi‐ tive and negative infinity, respectively. No others do. AWK(1)
16,524
275
jart/cosmopolitan
false
cosmopolitan/third_party/awk/awk.mk
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐ #───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘ PKGS += THIRD_PARTY_AWK THIRD_PARTY_AWK_ARTIFACTS += THIRD_PARTY_AWK_A THIRD_PARTY_AWK = $(THIRD_PARTY_AWK_A_DEPS) $(THIRD_PARTY_AWK_A) THIRD_PARTY_AWK_A = o/$(MODE)/third_party/awk/awk.a THIRD_PARTY_AWK_FILES := $(wildcard third_party/awk/*) THIRD_PARTY_AWK_HDRS = $(filter %.h,$(THIRD_PARTY_AWK_FILES)) THIRD_PARTY_AWK_INCS = $(filter %.inc,$(THIRD_PARTY_AWK_FILES)) THIRD_PARTY_AWK_SRCS = $(filter %.c,$(THIRD_PARTY_AWK_FILES)) THIRD_PARTY_AWK_OBJS = $(THIRD_PARTY_AWK_SRCS:%.c=o/$(MODE)/%.o) THIRD_PARTY_AWK_A_DIRECTDEPS = \ LIBC_FMT \ LIBC_INTRIN \ LIBC_MEM \ LIBC_NEXGEN32E \ LIBC_RUNTIME \ LIBC_CALLS \ LIBC_STDIO \ LIBC_SYSV \ LIBC_STR \ LIBC_STUBS \ LIBC_TINYMATH \ TOOL_ARGS \ THIRD_PARTY_GDTOA THIRD_PARTY_AWK_A_DEPS := \ $(call uniq,$(foreach x,$(THIRD_PARTY_AWK_A_DIRECTDEPS),$($(x)))) THIRD_PARTY_AWK_CHECKS = \ $(THIRD_PARTY_AWK_A).pkg \ $(THIRD_PARTY_AWK_HDRS:%=o/$(MODE)/%.ok) $(THIRD_PARTY_AWK_A): \ third_party/awk/ \ $(THIRD_PARTY_AWK_A).pkg \ $(THIRD_PARTY_AWK_OBJS) $(THIRD_PARTY_AWK_A).pkg: \ $(THIRD_PARTY_AWK_OBJS) \ $(foreach x,$(THIRD_PARTY_AWK_A_DIRECTDEPS),$($(x)_A).pkg) o/$(MODE)/third_party/awk/awk.com.dbg: \ $(THIRD_PARTY_AWK) \ o/$(MODE)/third_party/awk/cmd.o \ o/$(MODE)/third_party/awk/README.zip.o \ $(CRT) \ $(APE_NO_MODIFY_SELF) @$(APELINK) o/$(MODE)/third_party/awk/README.zip.o: \ ZIPOBJ_FLAGS += \ -B THIRD_PARTY_AWK_BINS = $(THIRD_PARTY_AWK_COMS) $(THIRD_PARTY_AWK_COMS:%=%.dbg) THIRD_PARTY_AWK_COMS = o/$(MODE)/third_party/awk/awk.com THIRD_PARTY_AWK_LIBS = $(THIRD_PARTY_AWK_A) $(THIRD_PARTY_AWK_OBJS): $(BUILD_FILES) third_party/awk/awk.mk .PHONY: o/$(MODE)/third_party/awk o/$(MODE)/third_party/awk: \ $(THIRD_PARTY_AWK_BINS) \ $(THIRD_PARTY_AWK_CHECKS)
2,048
67
jart/cosmopolitan
false
cosmopolitan/third_party/awk/main.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Copyright (C) Lucent Technologies 1997 │ │ All Rights Reserved │ │ │ │ Permission to use, copy, modify, and distribute this software and │ │ its documentation for any purpose and without fee is hereby │ │ granted, provided that the above copyright notice appear in all │ │ copies and that both that the copyright notice and this │ │ permission notice and warranty disclaimer appear in supporting │ │ documentation, and that the name Lucent Technologies or any of │ │ its entities not be used in advertising or publicity pertaining │ │ to distribution of the software without specific, written prior │ │ permission. │ │ │ │ LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, │ │ INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. │ │ IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY │ │ SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES │ │ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER │ │ IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, │ │ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF │ │ THIS SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #define DEBUG #include "libc/calls/calls.h" #include "libc/calls/struct/sigaction.h" #include "libc/calls/struct/siginfo.h" #include "libc/mem/mem.h" #include "libc/runtime/runtime.h" #include "libc/stdio/rand.h" #include "libc/str/locale.h" #include "libc/str/str.h" #include "libc/sysv/consts/sa.h" #include "libc/sysv/consts/sicode.h" #include "third_party/awk/awk.h" // clang-format off asm(".ident\t\"\\n\\n\ Copyright (C) Lucent Technologies 1997\\n\ All Rights Reserved\\n\ \\n\ Permission to use, copy, modify, and distribute this software and\\n\ its documentation for any purpose and without fee is hereby\\n\ granted, provided that the above copyright notice appear in all\\n\ copies and that both that the copyright notice and this\\n\ permission notice and warranty disclaimer appear in supporting\\n\ documentation, and that the name Lucent Technologies or any of\\n\ its entities not be used in advertising or publicity pertaining\\n\ to distribution of the software without specific, written prior\\n\ permission.\\n\ \\n\ LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,\\n\ INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.\\n\ IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY\\n\ SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES\\n\ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER\\n\ IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,\\n\ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF\\n\ THIS SOFTWARE.\""); const char *version = "version 20220530"; extern int nfields; int dbg = 0; Awkfloat srand_seed = 1; char *cmdname; /* gets argv[0] for error messages */ extern FILE *yyin; /* lex input file */ char *lexprog; /* points to program argument if it exists */ enum compile_states compile_time = ERROR_PRINTING; static char **pfile; /* program filenames from -f's */ static size_t maxpfile; /* max program filename */ static size_t npfile; /* number of filenames */ static size_t curpfile; /* current filename */ bool safe = false; /* true => "safe" mode */ static wontreturn void fpecatch(int n, siginfo_t *si, void *uc) { const char *emsg[10]; emsg[0] = "Unknown error"; emsg[FPE_INTDIV] = "Integer divide by zero"; emsg[FPE_INTOVF] = "Integer overflow"; emsg[FPE_FLTDIV] = "Floating point divide by zero"; emsg[FPE_FLTOVF] = "Floating point overflow"; emsg[FPE_FLTUND] = "Floating point underflow"; emsg[FPE_FLTRES] = "Floating point inexact result"; emsg[FPE_FLTINV] = "Invalid Floating point operation"; emsg[FPE_FLTSUB] = "Subscript out of range"; FATAL("floating point exception: %s", (size_t)si->si_code < sizeof(emsg) / sizeof(emsg[0]) && emsg[si->si_code] ? emsg[si->si_code] : emsg[0]); } /* Can this work with recursive calls? I don't think so. void segvcatch(int n) { FATAL("segfault. Do you have an unbounded recursive call?", n); } */ static const char * setfs(char *p) { /* wart: t=>\t */ if (p[0] == 't' && p[1] == '\0') return "\t"; return p; } static char * getarg(int *argc, char ***argv, const char *msg) { if ((*argv)[1][2] != '\0') { /* arg is -fsomething */ return &(*argv)[1][2]; } else { /* arg is -f something */ (*argc)--; (*argv)++; if (*argc <= 1) FATAL("%s", msg); return (*argv)[1]; } } int _awk(int argc, char *argv[]) { const char *fs = NULL; struct sigaction sa; char *fn, *vn; setlocale(LC_CTYPE, ""); setlocale(LC_NUMERIC, "C"); /* for parsing cmdline & prog */ cmdname = argv[0]; if (pledge("stdio rpath wpath cpath proc exec", NULL) == -1) { fprintf(stderr, "%s: pledge: incorrect arguments\n", cmdname); exit(1); } if (argc == 1) { fprintf(stderr, "usage: %s [-F fs] [-v var=value] [-f progfile | 'prog'] [file ...]\n", cmdname); exit(1); } sa.sa_sigaction = fpecatch; sa.sa_flags = SA_SIGINFO; sigemptyset(&sa.sa_mask); (void)sigaction(SIGFPE, &sa, NULL); /* Set and keep track of the random seed */ srand_seed = 1; srandom((unsigned long) srand_seed); yyin = NULL; symtab = makesymtab(NSYMTAB/NSYMTAB); while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') { if (strcmp(argv[1], "-version") == 0 || strcmp(argv[1], "--version") == 0) { printf("awk %s\n", version); return 0; } if (strcmp(argv[1], "--") == 0) { /* explicit end of args */ argc--; argv++; break; } switch (argv[1][1]) { case 's': if (strcmp(argv[1], "-safe") == 0) safe = true; break; case 'f': /* next argument is program filename */ fn = getarg(&argc, &argv, "no program filename"); if (npfile >= maxpfile) { maxpfile += 20; pfile = (char **) realloc(pfile, maxpfile * sizeof(*pfile)); if (pfile == NULL) FATAL("error allocating space for -f options"); } pfile[npfile++] = fn; break; case 'F': /* set field separator */ fs = setfs(getarg(&argc, &argv, "no field separator")); break; case 'v': /* -v a=1 to be done NOW. one -v for each */ vn = getarg(&argc, &argv, "no variable name"); if (isclvar(vn)) setclvar(vn); else FATAL("invalid -v option argument: %s", vn); break; case 'd': dbg = atoi(&argv[1][2]); if (dbg == 0) dbg = 1; printf("awk %s\n", version); break; default: WARNING("unknown option %s ignored", argv[1]); break; } argc--; argv++; } /* argv[1] is now the first argument */ if (npfile == 0) { /* no -f; first argument is program */ if (argc <= 1) { if (dbg) exit(0); FATAL("no program given"); } DPRINTF("program = |%s|\n", argv[1]); lexprog = argv[1]; argc--; argv++; } recinit(recsize); syminit(); compile_time = COMPILING; argv[0] = cmdname; /* put prog name at front of arglist */ DPRINTF("argc=%d, argv[0]=%s\n", argc, argv[0]); arginit(argc, argv); if (!safe) envinit(environ); yyparse(); if (fs) *FS = qstring(fs, '\0'); DPRINTF("errorflag=%d\n", errorflag); if (errorflag == 0) { compile_time = RUNNING; run(winner); } else bracecheck(); return(errorflag); } int pgetc(void) /* get 1 character from awk program */ { int c; for (;;) { if (yyin == NULL) { if (curpfile >= npfile) return EOF; if (strcmp(pfile[curpfile], "-") == 0) yyin = stdin; else if ((yyin = fopen(pfile[curpfile], "r")) == NULL) FATAL("can't open file %s", pfile[curpfile]); lineno = 1; } if ((c = getc(yyin)) != EOF) return c; if (yyin != stdin) fclose(yyin); yyin = NULL; curpfile++; } } char *cursource(void) /* current source file name */ { if (npfile > 0) return pfile[curpfile < npfile ? curpfile : curpfile - 1]; else return NULL; }
9,295
272
jart/cosmopolitan
false
cosmopolitan/third_party/awk/run.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Copyright (C) Lucent Technologies 1997 │ │ All Rights Reserved │ │ │ │ Permission to use, copy, modify, and distribute this software and │ │ its documentation for any purpose and without fee is hereby │ │ granted, provided that the above copyright notice appear in all │ │ copies and that both that the copyright notice and this │ │ permission notice and warranty disclaimer appear in supporting │ │ documentation, and that the name Lucent Technologies or any of │ │ its entities not be used in advertising or publicity pertaining │ │ to distribution of the software without specific, written prior │ │ permission. │ │ │ │ LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, │ │ INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. │ │ IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY │ │ SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES │ │ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER │ │ IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, │ │ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF │ │ THIS SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #define DEBUG #include "libc/calls/calls.h" #include "libc/calls/weirdtypes.h" #include "libc/errno.h" #include "libc/fmt/conv.h" #include "libc/fmt/fmt.h" #include "libc/mem/mem.h" #include "libc/runtime/runtime.h" #include "libc/str/str.h" #include "libc/sysv/consts/f.h" #include "libc/sysv/consts/fd.h" #include "libc/time/time.h" #include "third_party/awk/awk.h" #include "third_party/awk/awkgram.tab.h" #include "third_party/libcxx/math.h" // clang-format off static void stdinit(void); static void flush_all(void); #if 1 #define tempfree(x) do { if (istemp(x)) tfree(x); } while (/*CONSTCOND*/0) #else void tempfree(Cell *p) { if (p->ctype == OCELL && (p->csub < CUNK || p->csub > CFREE)) { WARNING("bad csub %d in Cell %d %s", p->csub, p->ctype, p->sval); } if (istemp(p)) tfree(p); } #endif jmp_buf env; extern Awkfloat srand_seed; Node *winner = NULL; /* root of parse tree */ Cell *tmps; /* free temporary cells for execution */ static Cell truecell ={ OBOOL, BTRUE, 0, 0, 1.0, NUM, NULL, NULL }; Cell *True = &truecell; static Cell falsecell ={ OBOOL, BFALSE, 0, 0, 0.0, NUM, NULL, NULL }; Cell *False = &falsecell; static Cell breakcell ={ OJUMP, JBREAK, 0, 0, 0.0, NUM, NULL, NULL }; Cell *jbreak = &breakcell; static Cell contcell ={ OJUMP, JCONT, 0, 0, 0.0, NUM, NULL, NULL }; Cell *jcont = &contcell; static Cell nextcell ={ OJUMP, JNEXT, 0, 0, 0.0, NUM, NULL, NULL }; Cell *jnext = &nextcell; static Cell nextfilecell ={ OJUMP, JNEXTFILE, 0, 0, 0.0, NUM, NULL, NULL }; Cell *jnextfile = &nextfilecell; static Cell exitcell ={ OJUMP, JEXIT, 0, 0, 0.0, NUM, NULL, NULL }; Cell *jexit = &exitcell; static Cell retcell ={ OJUMP, JRET, 0, 0, 0.0, NUM, NULL, NULL }; Cell *jret = &retcell; static Cell tempcell ={ OCELL, CTEMP, 0, EMPTY, 0.0, NUM|STR|DONTFREE, NULL, NULL }; Node *curnode = NULL; /* the node being executed, for debugging */ /* buffer memory management */ int adjbuf(char **pbuf, int *psiz, int minlen, int quantum, char **pbptr, const char *whatrtn) /* pbuf: address of pointer to buffer being managed * psiz: address of buffer size variable * minlen: minimum length of buffer needed * quantum: buffer size quantum * pbptr: address of movable pointer into buffer, or 0 if none * whatrtn: name of the calling routine if failure should cause fatal error * * return 0 for realloc failure, !=0 for success */ { if (minlen > *psiz) { char *tbuf; int rminlen = quantum ? minlen % quantum : 0; int boff = pbptr ? *pbptr - *pbuf : 0; /* round up to next multiple of quantum */ if (rminlen) minlen += quantum - rminlen; tbuf = (char *) realloc(*pbuf, minlen); DPRINTF("adjbuf %s: %d %d (pbuf=%p, tbuf=%p)\n", whatrtn, *psiz, minlen, (void*)*pbuf, (void*)tbuf); if (tbuf == NULL) { if (whatrtn) FATAL("out of memory in %s", whatrtn); return 0; } *pbuf = tbuf; *psiz = minlen; if (pbptr) *pbptr = tbuf + boff; } return 1; } void run(Node *a) /* execution of parse tree starts here */ { stdinit(); execute(a); closeall(); } Cell *execute(Node *u) /* execute a node of the parse tree */ { Cell *(*proc)(Node **, int); Cell *x; Node *a; if (u == NULL) return(True); for (a = u; ; a = a->nnext) { curnode = a; if (isvalue(a)) { x = (Cell *) (a->narg[0]); if (isfld(x) && !donefld) fldbld(); else if (isrec(x) && !donerec) recbld(); return(x); } if (notlegal(a->nobj)) /* probably a Cell* but too risky to print */ FATAL("illegal statement"); proc = proctab[a->nobj-FIRSTTOKEN]; x = (*proc)(a->narg, a->nobj); if (isfld(x) && !donefld) fldbld(); else if (isrec(x) && !donerec) recbld(); if (isexpr(a)) return(x); if (isjump(x)) return(x); if (a->nnext == NULL) return(x); tempfree(x); } } Cell *program(Node **a, int n) /* execute an awk program */ { /* a[0] = BEGIN, a[1] = body, a[2] = END */ Cell *x; if (setjmp(env) != 0) goto ex; if (a[0]) { /* BEGIN */ x = execute(a[0]); if (isexit(x)) return(True); if (isjump(x)) FATAL("illegal break, continue, next or nextfile from BEGIN"); tempfree(x); } if (a[1] || a[2]) while (getrec(&record, &recsize, true) > 0) { x = execute(a[1]); if (isexit(x)) break; tempfree(x); } ex: if (setjmp(env) != 0) /* handles exit within END */ goto ex1; if (a[2]) { /* END */ x = execute(a[2]); if (isbreak(x) || isnext(x) || iscont(x)) FATAL("illegal break, continue, next or nextfile from END"); tempfree(x); } ex1: return(True); } struct Frame { /* stack frame for awk function calls */ int nargs; /* number of arguments in this call */ Cell *fcncell; /* pointer to Cell for function */ Cell **args; /* pointer to array of arguments after execute */ Cell *retval; /* return value */ }; #define NARGS 50 /* max args in a call */ struct Frame *frame = NULL; /* base of stack frames; dynamically allocated */ int nframe = 0; /* number of frames allocated */ struct Frame *frp = NULL; /* frame pointer. bottom level unused */ Cell *call(Node **a, int n) /* function call. very kludgy and fragile */ { static const Cell newcopycell = { OCELL, CCOPY, 0, EMPTY, 0.0, NUM|STR|DONTFREE, NULL, NULL }; int i, ncall, ndef; int freed = 0; /* handles potential double freeing when fcn & param share a tempcell */ Node *x; Cell *args[NARGS], *oargs[NARGS]; /* BUG: fixed size arrays */ Cell *y, *z, *fcn; char *s; fcn = execute(a[0]); /* the function itself */ s = fcn->nval; if (!isfcn(fcn)) FATAL("calling undefined function %s", s); if (frame == NULL) { frp = frame = (struct Frame *) calloc(nframe += 100, sizeof(*frame)); if (frame == NULL) FATAL("out of space for stack frames calling %s", s); } for (ncall = 0, x = a[1]; x != NULL; x = x->nnext) /* args in call */ ncall++; ndef = (int) fcn->fval; /* args in defn */ DPRINTF("calling %s, %d args (%d in defn), frp=%d\n", s, ncall, ndef, (int) (frp-frame)); if (ncall > ndef) WARNING("function %s called with %d args, uses only %d", s, ncall, ndef); if (ncall + ndef > NARGS) FATAL("function %s has %d arguments, limit %d", s, ncall+ndef, NARGS); for (i = 0, x = a[1]; x != NULL; i++, x = x->nnext) { /* get call args */ DPRINTF("evaluate args[%d], frp=%d:\n", i, (int) (frp-frame)); y = execute(x); oargs[i] = y; DPRINTF("args[%d]: %s %f <%s>, t=%o\n", i, NN(y->nval), y->fval, isarr(y) ? "(array)" : NN(y->sval), y->tval); if (isfcn(y)) FATAL("can't use function %s as argument in %s", y->nval, s); if (isarr(y)) args[i] = y; /* arrays by ref */ else args[i] = copycell(y); tempfree(y); } for ( ; i < ndef; i++) { /* add null args for ones not provided */ args[i] = gettemp(); *args[i] = newcopycell; } frp++; /* now ok to up frame */ if (frp >= frame + nframe) { int dfp = frp - frame; /* old index */ frame = (struct Frame *) realloc(frame, (nframe += 100) * sizeof(*frame)); if (frame == NULL) FATAL("out of space for stack frames in %s", s); frp = frame + dfp; } frp->fcncell = fcn; frp->args = args; frp->nargs = ndef; /* number defined with (excess are locals) */ frp->retval = gettemp(); DPRINTF("start exec of %s, frp=%d\n", s, (int) (frp-frame)); y = execute((Node *)(fcn->sval)); /* execute body */ DPRINTF("finished exec of %s, frp=%d\n", s, (int) (frp-frame)); for (i = 0; i < ndef; i++) { Cell *t = frp->args[i]; if (isarr(t)) { if (t->csub == CCOPY) { if (i >= ncall) { freesymtab(t); t->csub = CTEMP; tempfree(t); } else { oargs[i]->tval = t->tval; oargs[i]->tval &= ~(STR|NUM|DONTFREE); oargs[i]->sval = t->sval; tempfree(t); } } } else if (t != y) { /* kludge to prevent freeing twice */ t->csub = CTEMP; tempfree(t); } else if (t == y && t->csub == CCOPY) { t->csub = CTEMP; tempfree(t); freed = 1; } } tempfree(fcn); if (isexit(y) || isnext(y)) return y; if (freed == 0) { tempfree(y); /* don't free twice! */ } z = frp->retval; /* return value */ DPRINTF("%s returns %g |%s| %o\n", s, getfval(z), getsval(z), z->tval); frp--; return(z); } Cell *copycell(Cell *x) /* make a copy of a cell in a temp */ { Cell *y; /* copy is not constant or field */ y = gettemp(); y->tval = x->tval & ~(CON|FLD|REC); y->csub = CCOPY; /* prevents freeing until call is over */ y->nval = x->nval; /* BUG? */ if (isstr(x) /* || x->ctype == OCELL */) { y->sval = tostring(x->sval); y->tval &= ~DONTFREE; } else y->tval |= DONTFREE; y->fval = x->fval; return y; } Cell *arg(Node **a, int n) /* nth argument of a function */ { n = ptoi(a[0]); /* argument number, counting from 0 */ DPRINTF("arg(%d), frp->nargs=%d\n", n, frp->nargs); if (n+1 > frp->nargs) FATAL("argument #%d of function %s was not supplied", n+1, frp->fcncell->nval); return frp->args[n]; } Cell *jump(Node **a, int n) /* break, continue, next, nextfile, return */ { Cell *y; switch (n) { case EXIT: if (a[0] != NULL) { y = execute(a[0]); errorflag = (int) getfval(y); tempfree(y); } longjmp(env, 1); case RETURN: if (a[0] != NULL) { y = execute(a[0]); if ((y->tval & (STR|NUM)) == (STR|NUM)) { setsval(frp->retval, getsval(y)); frp->retval->fval = getfval(y); frp->retval->tval |= NUM; } else if (y->tval & STR) setsval(frp->retval, getsval(y)); else if (y->tval & NUM) setfval(frp->retval, getfval(y)); else /* can't happen */ FATAL("bad type variable %d", y->tval); tempfree(y); } return(jret); case NEXT: return(jnext); case NEXTFILE: nextfile(); return(jnextfile); case BREAK: return(jbreak); case CONTINUE: return(jcont); default: /* can't happen */ FATAL("illegal jump type %d", n); } return 0; /* not reached */ } Cell *awkgetline(Node **a, int n) /* get next line from specific input */ { /* a[0] is variable, a[1] is operator, a[2] is filename */ Cell *r, *x; extern Cell **fldtab; FILE *fp; char *buf; int bufsize = recsize; int mode; bool newflag; double result; if ((buf = (char *) malloc(bufsize)) == NULL) FATAL("out of memory in getline"); fflush(stdout); /* in case someone is waiting for a prompt */ r = gettemp(); if (a[1] != NULL) { /* getline < file */ x = execute(a[2]); /* filename */ mode = ptoi(a[1]); if (mode == '|') /* input pipe */ mode = LE; /* arbitrary flag */ fp = openfile(mode, getsval(x), &newflag); tempfree(x); if (fp == NULL) n = -1; else n = readrec(&buf, &bufsize, fp, newflag); if (n <= 0) { ; } else if (a[0] != NULL) { /* getline var <file */ x = execute(a[0]); setsval(x, buf); if (is_number(x->sval, & result)) { x->fval = result; x->tval |= NUM; } tempfree(x); } else { /* getline <file */ setsval(fldtab[0], buf); if (is_number(fldtab[0]->sval, & result)) { fldtab[0]->fval = result; fldtab[0]->tval |= NUM; } } } else { /* bare getline; use current input */ if (a[0] == NULL) /* getline */ n = getrec(&record, &recsize, true); else { /* getline var */ n = getrec(&buf, &bufsize, false); if (n > 0) { x = execute(a[0]); setsval(x, buf); if (is_number(x->sval, & result)) { x->fval = result; x->tval |= NUM; } tempfree(x); } } } setfval(r, (Awkfloat) n); free(buf); return r; } Cell *getnf(Node **a, int n) /* get NF */ { if (!donefld) fldbld(); return (Cell *) a[0]; } static char * makearraystring(Node *p, const char *func) { char *buf; int bufsz = recsize; size_t blen; if ((buf = (char *) malloc(bufsz)) == NULL) { FATAL("%s: out of memory", func); } blen = 0; buf[blen] = '\0'; for (; p; p = p->nnext) { Cell *x = execute(p); /* expr */ char *s = getsval(x); size_t seplen = strlen(getsval(subseploc)); size_t nsub = p->nnext ? seplen : 0; size_t slen = strlen(s); size_t tlen = blen + slen + nsub; if (!adjbuf(&buf, &bufsz, tlen + 1, recsize, 0, func)) { FATAL("%s: out of memory %s[%s...]", func, x->nval, buf); } memcpy(buf + blen, s, slen); if (nsub) { memcpy(buf + blen + slen, *SUBSEP, nsub); } buf[tlen] = '\0'; blen = tlen; tempfree(x); } return buf; } Cell *array(Node **a, int n) /* a[0] is symtab, a[1] is list of subscripts */ { Cell *x, *z; char *buf; x = execute(a[0]); /* Cell* for symbol table */ buf = makearraystring(a[1], __func__); if (!isarr(x)) { DPRINTF("making %s into an array\n", NN(x->nval)); if (freeable(x)) xfree(x->sval); x->tval &= ~(STR|NUM|DONTFREE); x->tval |= ARR; x->sval = (char *) makesymtab(NSYMTAB); } z = setsymtab(buf, "", 0.0, STR|NUM, (Array *) x->sval); z->ctype = OCELL; z->csub = CVAR; tempfree(x); free(buf); return(z); } Cell *awkdelete(Node **a, int n) /* a[0] is symtab, a[1] is list of subscripts */ { Cell *x; x = execute(a[0]); /* Cell* for symbol table */ if (x == symtabloc) { FATAL("cannot delete SYMTAB or its elements"); } if (!isarr(x)) return True; if (a[1] == NULL) { /* delete the elements, not the table */ freesymtab(x); x->tval &= ~STR; x->tval |= ARR; x->sval = (char *) makesymtab(NSYMTAB); } else { char *buf = makearraystring(a[1], __func__); freeelem(x, buf); free(buf); } tempfree(x); return True; } Cell *intest(Node **a, int n) /* a[0] is index (list), a[1] is symtab */ { Cell *ap, *k; char *buf; ap = execute(a[1]); /* array name */ if (!isarr(ap)) { DPRINTF("making %s into an array\n", ap->nval); if (freeable(ap)) xfree(ap->sval); ap->tval &= ~(STR|NUM|DONTFREE); ap->tval |= ARR; ap->sval = (char *) makesymtab(NSYMTAB); } buf = makearraystring(a[0], __func__); k = lookup(buf, (Array *) ap->sval); tempfree(ap); free(buf); if (k == NULL) return(False); else return(True); } Cell *matchop(Node **a, int n) /* ~ and match() */ { Cell *x, *y; char *s, *t; int i; fa *pfa; int (*mf)(fa *, const char *) = match, mode = 0; if (n == MATCHFCN) { mf = pmatch; mode = 1; } x = execute(a[1]); /* a[1] = target text */ s = getsval(x); if (a[0] == NULL) /* a[1] == 0: already-compiled reg expr */ i = (*mf)((fa *) a[2], s); else { y = execute(a[2]); /* a[2] = regular expr */ t = getsval(y); pfa = makedfa(t, mode); i = (*mf)(pfa, s); tempfree(y); } tempfree(x); if (n == MATCHFCN) { int start = patbeg - s + 1; if (patlen < 0) start = 0; setfval(rstartloc, (Awkfloat) start); setfval(rlengthloc, (Awkfloat) patlen); x = gettemp(); x->tval = NUM; x->fval = start; return x; } else if ((n == MATCH && i == 1) || (n == NOTMATCH && i == 0)) return(True); else return(False); } Cell *boolop(Node **a, int n) /* a[0] || a[1], a[0] && a[1], !a[0] */ { Cell *x, *y; int i; x = execute(a[0]); i = istrue(x); tempfree(x); switch (n) { case BOR: if (i) return(True); y = execute(a[1]); i = istrue(y); tempfree(y); if (i) return(True); else return(False); case AND: if ( !i ) return(False); y = execute(a[1]); i = istrue(y); tempfree(y); if (i) return(True); else return(False); case NOT: if (i) return(False); else return(True); default: /* can't happen */ FATAL("unknown boolean operator %d", n); } return 0; /*NOTREACHED*/ } Cell *relop(Node **a, int n) /* a[0 < a[1], etc. */ { int i; Cell *x, *y; Awkfloat j; x = execute(a[0]); y = execute(a[1]); if (x->tval&NUM && y->tval&NUM) { j = x->fval - y->fval; i = j<0? -1: (j>0? 1: 0); } else { i = strcmp(getsval(x), getsval(y)); } tempfree(x); tempfree(y); switch (n) { case LT: if (i<0) return(True); else return(False); case LE: if (i<=0) return(True); else return(False); case NE: if (i!=0) return(True); else return(False); case EQ: if (i == 0) return(True); else return(False); case GE: if (i>=0) return(True); else return(False); case GT: if (i>0) return(True); else return(False); default: /* can't happen */ FATAL("unknown relational operator %d", n); } return 0; /*NOTREACHED*/ } void tfree(Cell *a) /* free a tempcell */ { if (freeable(a)) { DPRINTF("freeing %s %s %o\n", NN(a->nval), NN(a->sval), a->tval); xfree(a->sval); } if (a == tmps) FATAL("tempcell list is curdled"); a->cnext = tmps; tmps = a; } Cell *gettemp(void) /* get a tempcell */ { int i; Cell *x; if (!tmps) { tmps = (Cell *) calloc(100, sizeof(*tmps)); if (!tmps) FATAL("out of space for temporaries"); for (i = 1; i < 100; i++) tmps[i-1].cnext = &tmps[i]; tmps[i-1].cnext = NULL; } x = tmps; tmps = x->cnext; *x = tempcell; return(x); } Cell *indirect(Node **a, int n) /* $( a[0] ) */ { Awkfloat val; Cell *x; int m; char *s; x = execute(a[0]); val = getfval(x); /* freebsd: defend against super large field numbers */ if ((Awkfloat)INT_MAX < val) FATAL("trying to access out of range field %s", x->nval); m = (int) val; if (m == 0 && !is_number(s = getsval(x), NULL)) /* suspicion! */ FATAL("illegal field $(%s), name \"%s\"", s, x->nval); /* BUG: can x->nval ever be null??? */ tempfree(x); x = fieldadr(m); x->ctype = OCELL; /* BUG? why are these needed? */ x->csub = CFLD; return(x); } Cell *substr(Node **a, int nnn) /* substr(a[0], a[1], a[2]) */ { int k, m, n; char *s; int temp; Cell *x, *y, *z = NULL; x = execute(a[0]); y = execute(a[1]); if (a[2] != NULL) z = execute(a[2]); s = getsval(x); k = strlen(s) + 1; if (k <= 1) { tempfree(x); tempfree(y); if (a[2] != NULL) { tempfree(z); } x = gettemp(); setsval(x, ""); return(x); } m = (int) getfval(y); if (m <= 0) m = 1; else if (m > k) m = k; tempfree(y); if (a[2] != NULL) { n = (int) getfval(z); tempfree(z); } else n = k - 1; if (n < 0) n = 0; else if (n > k - m) n = k - m; DPRINTF("substr: m=%d, n=%d, s=%s\n", m, n, s); y = gettemp(); temp = s[n+m-1]; /* with thanks to John Linderman */ s[n+m-1] = '\0'; setsval(y, s + m - 1); s[n+m-1] = temp; tempfree(x); return(y); } Cell *sindex(Node **a, int nnn) /* index(a[0], a[1]) */ { Cell *x, *y, *z; char *s1, *s2, *p1, *p2, *q; Awkfloat v = 0.0; x = execute(a[0]); s1 = getsval(x); y = execute(a[1]); s2 = getsval(y); z = gettemp(); for (p1 = s1; *p1 != '\0'; p1++) { for (q = p1, p2 = s2; *p2 != '\0' && *q == *p2; q++, p2++) continue; if (*p2 == '\0') { v = (Awkfloat) (p1 - s1 + 1); /* origin 1 */ break; } } tempfree(x); tempfree(y); setfval(z, v); return(z); } #define MAXNUMSIZE 50 int format(char **pbuf, int *pbufsize, const char *s, Node *a) /* printf-like conversions */ { char *fmt; char *p, *t; const char *os; Cell *x; int flag = 0, n; int fmtwd; /* format width */ int fmtsz = recsize; char *buf = *pbuf; int bufsize = *pbufsize; #define FMTSZ(a) (fmtsz - ((a) - fmt)) #define BUFSZ(a) (bufsize - ((a) - buf)) static bool first = true; static bool have_a_format = false; if (first) { char xbuf[100]; snprintf(xbuf, sizeof(xbuf), "%a", 42.0); have_a_format = (strcmp(xbuf, "0x1.5p+5") == 0); first = false; } os = s; p = buf; if ((fmt = (char *) malloc(fmtsz)) == NULL) FATAL("out of memory in format()"); while (*s) { adjbuf(&buf, &bufsize, MAXNUMSIZE+1+p-buf, recsize, &p, "format1"); if (*s != '%') { *p++ = *s++; continue; } if (*(s+1) == '%') { *p++ = '%'; s += 2; continue; } /* have to be real careful in case this is a huge number, eg, %100000d */ fmtwd = atoi(s+1); if (fmtwd < 0) fmtwd = -fmtwd; adjbuf(&buf, &bufsize, fmtwd+1+p-buf, recsize, &p, "format2"); for (t = fmt; (*t++ = *s) != '\0'; s++) { if (!adjbuf(&fmt, &fmtsz, MAXNUMSIZE+1+t-fmt, recsize, &t, "format3")) FATAL("format item %.30s... ran format() out of memory", os); /* Ignore size specifiers */ if (strchr("hjLlqtz", *s) != NULL) { /* the ansi panoply */ t--; continue; } if (isalpha((uschar)*s)) break; if (*s == '$') { FATAL("'$' not permitted in awk formats"); } if (*s == '*') { if (a == NULL) { FATAL("not enough args in printf(%s)", os); } x = execute(a); a = a->nnext; snprintf(t - 1, FMTSZ(t - 1), "%d", fmtwd=(int) getfval(x)); if (fmtwd < 0) fmtwd = -fmtwd; adjbuf(&buf, &bufsize, fmtwd+1+p-buf, recsize, &p, "format"); t = fmt + strlen(fmt); tempfree(x); } } *t = '\0'; if (fmtwd < 0) fmtwd = -fmtwd; adjbuf(&buf, &bufsize, fmtwd+1+p-buf, recsize, &p, "format4"); switch (*s) { case 'a': case 'A': if (have_a_format) flag = *s; else flag = 'f'; break; case 'f': case 'e': case 'g': case 'E': case 'G': flag = 'f'; break; case 'd': case 'i': case 'o': case 'x': case 'X': case 'u': flag = (*s == 'd' || *s == 'i') ? 'd' : 'u'; *(t-1) = 'j'; *t = *s; *++t = '\0'; break; case 's': flag = 's'; break; case 'c': flag = 'c'; break; default: WARNING("weird printf conversion %s", fmt); flag = '?'; break; } if (a == NULL) FATAL("not enough args in printf(%s)", os); x = execute(a); a = a->nnext; n = MAXNUMSIZE; if (fmtwd > n) n = fmtwd; adjbuf(&buf, &bufsize, 1+n+p-buf, recsize, &p, "format5"); switch (flag) { case '?': snprintf(p, BUFSZ(p), "%s", fmt); /* unknown, so dump it too */ t = getsval(x); n = strlen(t); if (fmtwd > n) n = fmtwd; adjbuf(&buf, &bufsize, 1+strlen(p)+n+p-buf, recsize, &p, "format6"); p += strlen(p); snprintf(p, BUFSZ(p), "%s", t); break; case 'a': case 'A': case 'f': snprintf(p, BUFSZ(p), fmt, getfval(x)); break; case 'd': snprintf(p, BUFSZ(p), fmt, (intmax_t) getfval(x)); break; case 'u': snprintf(p, BUFSZ(p), fmt, (uintmax_t) getfval(x)); break; case 's': t = getsval(x); n = strlen(t); if (fmtwd > n) n = fmtwd; if (!adjbuf(&buf, &bufsize, 1+n+p-buf, recsize, &p, "format7")) FATAL("huge string/format (%d chars) in printf %.30s... ran format() out of memory", n, t); snprintf(p, BUFSZ(p), fmt, t); break; case 'c': if (isnum(x)) { if ((int)getfval(x)) snprintf(p, BUFSZ(p), fmt, (int) getfval(x)); else { *p++ = '\0'; /* explicit null byte */ *p = '\0'; /* next output will start here */ } } else snprintf(p, BUFSZ(p), fmt, getsval(x)[0]); break; default: FATAL("can't happen: bad conversion %c in format()", flag); } tempfree(x); p += strlen(p); s++; } *p = '\0'; free(fmt); for ( ; a; a = a->nnext) /* evaluate any remaining args */ execute(a); *pbuf = buf; *pbufsize = bufsize; return p - buf; } Cell *awksprintf(Node **a, int n) /* sprintf(a[0]) */ { Cell *x; Node *y; char *buf; int bufsz=3*recsize; if ((buf = (char *) malloc(bufsz)) == NULL) FATAL("out of memory in awksprintf"); y = a[0]->nnext; x = execute(a[0]); if (format(&buf, &bufsz, getsval(x), y) == -1) FATAL("sprintf string %.30s... too long. can't happen.", buf); tempfree(x); x = gettemp(); x->sval = buf; x->tval = STR; return(x); } Cell *awkprintf(Node **a, int n) /* printf */ { /* a[0] is list of args, starting with format string */ /* a[1] is redirection operator, a[2] is redirection file */ FILE *fp; Cell *x; Node *y; char *buf; int len; int bufsz=3*recsize; if ((buf = (char *) malloc(bufsz)) == NULL) FATAL("out of memory in awkprintf"); y = a[0]->nnext; x = execute(a[0]); if ((len = format(&buf, &bufsz, getsval(x), y)) == -1) FATAL("printf string %.30s... too long. can't happen.", buf); tempfree(x); if (a[1] == NULL) { /* fputs(buf, stdout); */ fwrite(buf, len, 1, stdout); if (ferror(stdout)) FATAL("write error on stdout"); } else { fp = redirect(ptoi(a[1]), a[2]); /* fputs(buf, fp); */ fwrite(buf, len, 1, fp); fflush(fp); if (ferror(fp)) FATAL("write error on %s", filename(fp)); } free(buf); return(True); } Cell *arith(Node **a, int n) /* a[0] + a[1], etc. also -a[0] */ { Awkfloat i, j = 0; double v; Cell *x, *y, *z; x = execute(a[0]); i = getfval(x); tempfree(x); if (n != UMINUS && n != UPLUS) { y = execute(a[1]); j = getfval(y); tempfree(y); } z = gettemp(); switch (n) { case ADD: i += j; break; case MINUS: i -= j; break; case MULT: i *= j; break; case DIVIDE: if (j == 0) FATAL("division by zero"); i /= j; break; case MOD: if (j == 0) FATAL("division by zero in mod"); modf(i/j, &v); i = i - j * v; break; case UMINUS: i = -i; break; case UPLUS: /* handled by getfval(), above */ break; case POWER: if (j >= 0 && modf(j, &v) == 0.0) /* pos integer exponent */ i = ipow(i, (int) j); else { errno = 0; i = errcheck(pow(i, j), "pow"); } break; default: /* can't happen */ FATAL("illegal arithmetic operator %d", n); } setfval(z, i); return(z); } double ipow(double x, int n) /* x**n. ought to be done by pow, but isn't always */ { double v; if (n <= 0) return 1; v = ipow(x, n/2); if (n % 2 == 0) return v * v; else return x * v * v; } Cell *incrdecr(Node **a, int n) /* a[0]++, etc. */ { Cell *x, *z; int k; Awkfloat xf; x = execute(a[0]); xf = getfval(x); k = (n == PREINCR || n == POSTINCR) ? 1 : -1; if (n == PREINCR || n == PREDECR) { setfval(x, xf + k); return(x); } z = gettemp(); setfval(z, xf); setfval(x, xf + k); tempfree(x); return(z); } Cell *assign(Node **a, int n) /* a[0] = a[1], a[0] += a[1], etc. */ { /* this is subtle; don't muck with it. */ Cell *x, *y; Awkfloat xf, yf; double v; y = execute(a[1]); x = execute(a[0]); if (n == ASSIGN) { /* ordinary assignment */ if (x == y && !(x->tval & (FLD|REC)) && x != nfloc) ; /* self-assignment: leave alone unless it's a field or NF */ else if ((y->tval & (STR|NUM)) == (STR|NUM)) { setsval(x, getsval(y)); x->fval = getfval(y); x->tval |= NUM; } else if (isstr(y)) setsval(x, getsval(y)); else if (isnum(y)) setfval(x, getfval(y)); else funnyvar(y, "read value of"); tempfree(y); return(x); } xf = getfval(x); yf = getfval(y); switch (n) { case ADDEQ: xf += yf; break; case SUBEQ: xf -= yf; break; case MULTEQ: xf *= yf; break; case DIVEQ: if (yf == 0) FATAL("division by zero in /="); xf /= yf; break; case MODEQ: if (yf == 0) FATAL("division by zero in %%="); modf(xf/yf, &v); xf = xf - yf * v; break; case POWEQ: if (yf >= 0 && modf(yf, &v) == 0.0) /* pos integer exponent */ xf = ipow(xf, (int) yf); else { errno = 0; xf = errcheck(pow(xf, yf), "pow"); } break; default: FATAL("illegal assignment operator %d", n); break; } tempfree(y); setfval(x, xf); return(x); } Cell *cat(Node **a, int q) /* a[0] cat a[1] */ { Cell *x, *y, *z; int n1, n2; char *s = NULL; int ssz = 0; x = execute(a[0]); n1 = strlen(getsval(x)); adjbuf(&s, &ssz, n1, recsize, 0, "cat1"); memcpy(s, x->sval, n1); y = execute(a[1]); n2 = strlen(getsval(y)); adjbuf(&s, &ssz, n1 + n2 + 1, recsize, 0, "cat2"); memcpy(s + n1, y->sval, n2); s[n1 + n2] = '\0'; tempfree(x); tempfree(y); z = gettemp(); z->sval = s; z->tval = STR; return(z); } Cell *pastat(Node **a, int n) /* a[0] { a[1] } */ { Cell *x; if (a[0] == NULL) x = execute(a[1]); else { x = execute(a[0]); if (istrue(x)) { tempfree(x); x = execute(a[1]); } } return x; } Cell *dopa2(Node **a, int n) /* a[0], a[1] { a[2] } */ { Cell *x; int pair; pair = ptoi(a[3]); if (pairstack[pair] == 0) { x = execute(a[0]); if (istrue(x)) pairstack[pair] = 1; tempfree(x); } if (pairstack[pair] == 1) { x = execute(a[1]); if (istrue(x)) pairstack[pair] = 0; tempfree(x); x = execute(a[2]); return(x); } return(False); } Cell *split(Node **a, int nnn) /* split(a[0], a[1], a[2]); a[3] is type */ { Cell *x = NULL, *y, *ap; const char *s, *origs, *t; const char *fs = NULL; char *origfs = NULL; int sep; char temp, num[50]; int n, tempstat, arg3type; double result; y = execute(a[0]); /* source string */ origs = s = strdup(getsval(y)); arg3type = ptoi(a[3]); if (a[2] == NULL) /* fs string */ fs = getsval(fsloc); else if (arg3type == STRING) { /* split(str,arr,"string") */ x = execute(a[2]); fs = origfs = strdup(getsval(x)); tempfree(x); } else if (arg3type == REGEXPR) fs = "(regexpr)"; /* split(str,arr,/regexpr/) */ else FATAL("illegal type of split"); sep = *fs; ap = execute(a[1]); /* array name */ freesymtab(ap); DPRINTF("split: s=|%s|, a=%s, sep=|%s|\n", s, NN(ap->nval), fs); ap->tval &= ~STR; ap->tval |= ARR; ap->sval = (char *) makesymtab(NSYMTAB); n = 0; if (arg3type == REGEXPR && strlen((char*)((fa*)a[2])->restr) == 0) { /* split(s, a, //); have to arrange that it looks like empty sep */ arg3type = 0; fs = ""; sep = 0; } if (*s != '\0' && (strlen(fs) > 1 || arg3type == REGEXPR)) { /* reg expr */ fa *pfa; if (arg3type == REGEXPR) { /* it's ready already */ pfa = (fa *) a[2]; } else { pfa = makedfa(fs, 1); } if (nematch(pfa,s)) { tempstat = pfa->initstat; pfa->initstat = 2; do { n++; snprintf(num, sizeof(num), "%d", n); temp = *patbeg; setptr(patbeg, '\0'); if (is_number(s, & result)) setsymtab(num, s, result, STR|NUM, (Array *) ap->sval); else setsymtab(num, s, 0.0, STR, (Array *) ap->sval); setptr(patbeg, temp); s = patbeg + patlen; if (*(patbeg+patlen-1) == '\0' || *s == '\0') { n++; snprintf(num, sizeof(num), "%d", n); setsymtab(num, "", 0.0, STR, (Array *) ap->sval); pfa->initstat = tempstat; goto spdone; } } while (nematch(pfa,s)); pfa->initstat = tempstat; /* bwk: has to be here to reset */ /* cf gsub and refldbld */ } n++; snprintf(num, sizeof(num), "%d", n); if (is_number(s, & result)) setsymtab(num, s, result, STR|NUM, (Array *) ap->sval); else setsymtab(num, s, 0.0, STR, (Array *) ap->sval); spdone: pfa = NULL; } else if (sep == ' ') { for (n = 0; ; ) { #define ISWS(c) ((c) == ' ' || (c) == '\t' || (c) == '\n') while (ISWS(*s)) s++; if (*s == '\0') break; n++; t = s; do s++; while (*s != '\0' && !ISWS(*s)); temp = *s; setptr(s, '\0'); snprintf(num, sizeof(num), "%d", n); if (is_number(t, & result)) setsymtab(num, t, result, STR|NUM, (Array *) ap->sval); else setsymtab(num, t, 0.0, STR, (Array *) ap->sval); setptr(s, temp); if (*s != '\0') s++; } } else if (sep == 0) { /* new: split(s, a, "") => 1 char/elem */ for (n = 0; *s != '\0'; s++) { char buf[2]; n++; snprintf(num, sizeof(num), "%d", n); buf[0] = *s; buf[1] = '\0'; if (isdigit((uschar)buf[0])) setsymtab(num, buf, atof(buf), STR|NUM, (Array *) ap->sval); else setsymtab(num, buf, 0.0, STR, (Array *) ap->sval); } } else if (*s != '\0') { for (;;) { n++; t = s; while (*s != sep && *s != '\n' && *s != '\0') s++; temp = *s; setptr(s, '\0'); snprintf(num, sizeof(num), "%d", n); if (is_number(t, & result)) setsymtab(num, t, result, STR|NUM, (Array *) ap->sval); else setsymtab(num, t, 0.0, STR, (Array *) ap->sval); setptr(s, temp); if (*s++ == '\0') break; } } tempfree(ap); tempfree(y); xfree(origs); xfree(origfs); x = gettemp(); x->tval = NUM; x->fval = n; return(x); } Cell *condexpr(Node **a, int n) /* a[0] ? a[1] : a[2] */ { Cell *x; x = execute(a[0]); if (istrue(x)) { tempfree(x); x = execute(a[1]); } else { tempfree(x); x = execute(a[2]); } return(x); } Cell *ifstat(Node **a, int n) /* if (a[0]) a[1]; else a[2] */ { Cell *x; x = execute(a[0]); if (istrue(x)) { tempfree(x); x = execute(a[1]); } else if (a[2] != NULL) { tempfree(x); x = execute(a[2]); } return(x); } Cell *whilestat(Node **a, int n) /* while (a[0]) a[1] */ { Cell *x; for (;;) { x = execute(a[0]); if (!istrue(x)) return(x); tempfree(x); x = execute(a[1]); if (isbreak(x)) { x = True; return(x); } if (isnext(x) || isexit(x) || isret(x)) return(x); tempfree(x); } } Cell *dostat(Node **a, int n) /* do a[0]; while(a[1]) */ { Cell *x; for (;;) { x = execute(a[0]); if (isbreak(x)) return True; if (isnext(x) || isexit(x) || isret(x)) return(x); tempfree(x); x = execute(a[1]); if (!istrue(x)) return(x); tempfree(x); } } Cell *forstat(Node **a, int n) /* for (a[0]; a[1]; a[2]) a[3] */ { Cell *x; x = execute(a[0]); tempfree(x); for (;;) { if (a[1]!=NULL) { x = execute(a[1]); if (!istrue(x)) return(x); else tempfree(x); } x = execute(a[3]); if (isbreak(x)) /* turn off break */ return True; if (isnext(x) || isexit(x) || isret(x)) return(x); tempfree(x); x = execute(a[2]); tempfree(x); } } Cell *instat(Node **a, int n) /* for (a[0] in a[1]) a[2] */ { Cell *x, *vp, *arrayp, *cp, *ncp; Array *tp; int i; vp = execute(a[0]); arrayp = execute(a[1]); if (!isarr(arrayp)) { return True; } tp = (Array *) arrayp->sval; tempfree(arrayp); for (i = 0; i < tp->size; i++) { /* this routine knows too much */ for (cp = tp->tab[i]; cp != NULL; cp = ncp) { setsval(vp, cp->nval); ncp = cp->cnext; x = execute(a[2]); if (isbreak(x)) { tempfree(vp); return True; } if (isnext(x) || isexit(x) || isret(x)) { tempfree(vp); return(x); } tempfree(x); } } return True; } static char *nawk_convert(const char *s, int (*fun_c)(int), wint_t (*fun_wc)(wint_t)) { char *buf = NULL; char *pbuf = NULL; const char *ps = NULL; size_t n = 0; wchar_t wc; size_t sz = MB_CUR_MAX; if (sz == 1) { buf = tostring(s); for (pbuf = buf; *pbuf; pbuf++) *pbuf = fun_c((uschar)*pbuf); return buf; } else { /* upper/lower character may be shorter/longer */ buf = tostringN(s, strlen(s) * sz + 1); (void) mbtowc(NULL, NULL, 0); /* reset internal state */ /* * Reset internal state here too. * Assign result to avoid a compiler warning. (Casting to void * doesn't work.) * Increment said variable to avoid a different warning. */ int unused = wctomb(NULL, L'\0'); unused++; ps = s; pbuf = buf; while (n = mbtowc(&wc, ps, sz), n > 0 && n != (size_t)-1 && n != (size_t)-2) { ps += n; n = wctomb(pbuf, fun_wc(wc)); if (n == (size_t)-1) FATAL("illegal wide character %s", s); pbuf += n; } *pbuf = '\0'; if (n) FATAL("illegal byte sequence %s", s); return buf; } } #ifdef __DJGPP__ static wint_t towupper(wint_t wc) { if (wc >= 0 && wc < 256) return toupper(wc & 0xFF); return wc; } static wint_t towlower(wint_t wc) { if (wc >= 0 && wc < 256) return tolower(wc & 0xFF); return wc; } #endif static char *nawk_toupper(const char *s) { return nawk_convert(s, toupper, towupper); } static char *nawk_tolower(const char *s) { return nawk_convert(s, tolower, towlower); } Cell *bltin(Node **a, int n) /* builtin functions. a[0] is type, a[1] is arg list */ { Cell *x, *y; Awkfloat u; int t; Awkfloat tmp; char *buf; Node *nextarg; FILE *fp; int status = 0; t = ptoi(a[0]); x = execute(a[1]); nextarg = a[1]->nnext; switch (t) { case FLENGTH: if (isarr(x)) u = ((Array *) x->sval)->nelem; /* GROT. should be function*/ else u = strlen(getsval(x)); break; case FLOG: errno = 0; u = errcheck(log(getfval(x)), "log"); break; case FINT: modf(getfval(x), &u); break; case FEXP: errno = 0; u = errcheck(exp(getfval(x)), "exp"); break; case FSQRT: errno = 0; u = errcheck(sqrt(getfval(x)), "sqrt"); break; case FSIN: u = sin(getfval(x)); break; case FCOS: u = cos(getfval(x)); break; case FATAN: if (nextarg == NULL) { WARNING("atan2 requires two arguments; returning 1.0"); u = 1.0; } else { y = execute(a[1]->nnext); u = atan2(getfval(x), getfval(y)); tempfree(y); nextarg = nextarg->nnext; } break; case FSYSTEM: fflush(stdout); /* in case something is buffered already */ status = system(getsval(x)); u = status; if (status != -1) { if (WIFEXITED(status)) { u = WEXITSTATUS(status); } else if (WIFSIGNALED(status)) { u = WTERMSIG(status) + 256; if (WCOREDUMP(status)) u += 256; } else /* something else?!? */ u = 0; } break; case FRAND: /* random() returns numbers in [0..2^31-1] * in order to get a number in [0, 1), divide it by 2^31 */ u = (Awkfloat) random() / (0x7fffffffL + 0x1UL); break; case FSRAND: if (isrec(x)) /* no argument provided */ u = time((time_t *)0); else u = getfval(x); tmp = u; srandom((unsigned long) u); u = srand_seed; srand_seed = tmp; break; case FTOUPPER: case FTOLOWER: if (t == FTOUPPER) buf = nawk_toupper(getsval(x)); else buf = nawk_tolower(getsval(x)); tempfree(x); x = gettemp(); setsval(x, buf); free(buf); return x; case FFLUSH: if (isrec(x) || strlen(getsval(x)) == 0) { flush_all(); /* fflush() or fflush("") -> all */ u = 0; } else if ((fp = openfile(FFLUSH, getsval(x), NULL)) == NULL) u = EOF; else u = fflush(fp); break; default: /* can't happen */ FATAL("illegal function type %d", t); break; } tempfree(x); x = gettemp(); setfval(x, u); if (nextarg != NULL) { WARNING("warning: function has too many arguments"); for ( ; nextarg; nextarg = nextarg->nnext) execute(nextarg); } return(x); } Cell *printstat(Node **a, int n) /* print a[0] */ { Node *x; Cell *y; FILE *fp; if (a[1] == NULL) /* a[1] is redirection operator, a[2] is file */ fp = stdout; else fp = redirect(ptoi(a[1]), a[2]); for (x = a[0]; x != NULL; x = x->nnext) { y = execute(x); fputs(getpssval(y), fp); tempfree(y); if (x->nnext == NULL) fputs(getsval(orsloc), fp); else fputs(getsval(ofsloc), fp); } if (a[1] != NULL) fflush(fp); if (ferror(fp)) FATAL("write error on %s", filename(fp)); return(True); } Cell *nullproc(Node **a, int n) { return 0; } FILE *redirect(int a, Node *b) /* set up all i/o redirections */ { FILE *fp; Cell *x; char *fname; x = execute(b); fname = getsval(x); fp = openfile(a, fname, NULL); if (fp == NULL) FATAL("can't open file %s", fname); tempfree(x); return fp; } struct files { FILE *fp; const char *fname; int mode; /* '|', 'a', 'w' => LE/LT, GT */ } *files; size_t nfiles; static void stdinit(void) /* in case stdin, etc., are not constants */ { nfiles = FOPEN_MAX; files = (struct files *) calloc(nfiles, sizeof(*files)); if (files == NULL) FATAL("can't allocate file memory for %zu files", nfiles); files[0].fp = stdin; files[0].fname = tostring("/dev/stdin"); files[0].mode = LT; files[1].fp = stdout; files[1].fname = tostring("/dev/stdout"); files[1].mode = GT; files[2].fp = stderr; files[2].fname = tostring("/dev/stderr"); files[2].mode = GT; } FILE *openfile(int a, const char *us, bool *pnewflag) { const char *s = us; size_t i; int m; FILE *fp = NULL; if (*s == '\0') FATAL("null file name in print or getline"); for (i = 0; i < nfiles; i++) if (files[i].fname && strcmp(s, files[i].fname) == 0 && (a == files[i].mode || (a==APPEND && files[i].mode==GT) || a == FFLUSH)) { if (pnewflag) *pnewflag = false; return files[i].fp; } if (a == FFLUSH) /* didn't find it, so don't create it! */ return NULL; for (i = 0; i < nfiles; i++) if (files[i].fp == NULL) break; if (i >= nfiles) { struct files *nf; size_t nnf = nfiles + FOPEN_MAX; nf = (struct files *) realloc(files, nnf * sizeof(*nf)); if (nf == NULL) FATAL("cannot grow files for %s and %zu files", s, nnf); memset(&nf[nfiles], 0, FOPEN_MAX * sizeof(*nf)); nfiles = nnf; files = nf; } fflush(stdout); /* force a semblance of order */ m = a; if (a == GT) { fp = fopen(s, "w"); } else if (a == APPEND) { fp = fopen(s, "a"); m = GT; /* so can mix > and >> */ } else if (a == '|') { /* output pipe */ fp = popen(s, "w"); } else if (a == LE) { /* input pipe */ fp = popen(s, "r"); } else if (a == LT) { /* getline <file */ fp = strcmp(s, "-") == 0 ? stdin : fopen(s, "r"); /* "-" is stdin */ } else /* can't happen */ FATAL("illegal redirection %d", a); if (fp != NULL) { files[i].fname = tostring(s); files[i].fp = fp; files[i].mode = m; if (pnewflag) *pnewflag = true; if (fp != stdin && fp != stdout && fp != stderr) (void) fcntl(fileno(fp), F_SETFD, FD_CLOEXEC); } return fp; } const char *filename(FILE *fp) { size_t i; for (i = 0; i < nfiles; i++) if (fp == files[i].fp) return files[i].fname; return "???"; } Cell *closefile(Node **a, int n) { Cell *x; size_t i; bool stat; x = execute(a[0]); getsval(x); stat = true; for (i = 0; i < nfiles; i++) { if (!files[i].fname || strcmp(x->sval, files[i].fname) != 0) continue; if (files[i].mode == GT || files[i].mode == '|') fflush(files[i].fp); if (ferror(files[i].fp)) { if ((files[i].mode == GT && files[i].fp != stderr) || files[i].mode == '|') FATAL("write error on %s", files[i].fname); else WARNING("i/o error occurred on %s", files[i].fname); } if (files[i].fp == stdin || files[i].fp == stdout || files[i].fp == stderr) stat = freopen("/dev/null", "r+", files[i].fp) == NULL; else if (files[i].mode == '|' || files[i].mode == LE) stat = pclose(files[i].fp) == -1; else stat = fclose(files[i].fp) == EOF; if (stat) WARNING("i/o error occurred closing %s", files[i].fname); xfree(files[i].fname); files[i].fname = NULL; /* watch out for ref thru this */ files[i].fp = NULL; break; } tempfree(x); x = gettemp(); setfval(x, (Awkfloat) (stat ? -1 : 0)); return(x); } void closeall(void) { size_t i; bool stat = false; for (i = 0; i < nfiles; i++) { if (! files[i].fp) continue; if (files[i].mode == GT || files[i].mode == '|') fflush(files[i].fp); if (ferror(files[i].fp)) { if ((files[i].mode == GT && files[i].fp != stderr) || files[i].mode == '|') FATAL("write error on %s", files[i].fname); else WARNING("i/o error occurred on %s", files[i].fname); } if (files[i].fp == stdin || files[i].fp == stdout || files[i].fp == stderr) continue; if (files[i].mode == '|' || files[i].mode == LE) stat = pclose(files[i].fp) == -1; else stat = fclose(files[i].fp) == EOF; if (stat) WARNING("i/o error occurred while closing %s", files[i].fname); } } static void flush_all(void) { size_t i; for (i = 0; i < nfiles; i++) if (files[i].fp) fflush(files[i].fp); } void backsub(char **pb_ptr, const char **sptr_ptr); Cell *sub(Node **a, int nnn) /* substitute command */ { const char *sptr, *q; Cell *x, *y, *result; char *t, *buf, *pb; fa *pfa; int bufsz = recsize; if ((buf = (char *) malloc(bufsz)) == NULL) FATAL("out of memory in sub"); x = execute(a[3]); /* target string */ t = getsval(x); if (a[0] == NULL) /* 0 => a[1] is already-compiled regexpr */ pfa = (fa *) a[1]; /* regular expression */ else { y = execute(a[1]); pfa = makedfa(getsval(y), 1); tempfree(y); } y = execute(a[2]); /* replacement string */ result = False; if (pmatch(pfa, t)) { sptr = t; adjbuf(&buf, &bufsz, 1+patbeg-sptr, recsize, 0, "sub"); pb = buf; while (sptr < patbeg) *pb++ = *sptr++; sptr = getsval(y); while (*sptr != '\0') { adjbuf(&buf, &bufsz, 5+pb-buf, recsize, &pb, "sub"); if (*sptr == '\\') { backsub(&pb, &sptr); } else if (*sptr == '&') { sptr++; adjbuf(&buf, &bufsz, 1+patlen+pb-buf, recsize, &pb, "sub"); for (q = patbeg; q < patbeg+patlen; ) *pb++ = *q++; } else *pb++ = *sptr++; } *pb = '\0'; if (pb > buf + bufsz) FATAL("sub result1 %.30s too big; can't happen", buf); sptr = patbeg + patlen; if ((patlen == 0 && *patbeg) || (patlen && *(sptr-1))) { adjbuf(&buf, &bufsz, 1+strlen(sptr)+pb-buf, 0, &pb, "sub"); while ((*pb++ = *sptr++) != '\0') continue; } if (pb > buf + bufsz) FATAL("sub result2 %.30s too big; can't happen", buf); setsval(x, buf); /* BUG: should be able to avoid copy */ result = True; } tempfree(x); tempfree(y); free(buf); return result; } Cell *gsub(Node **a, int nnn) /* global substitute */ { Cell *x, *y; char *rptr, *pb; const char *q, *t, *sptr; char *buf; fa *pfa; int mflag, tempstat, num; int bufsz = recsize; if ((buf = (char *) malloc(bufsz)) == NULL) FATAL("out of memory in gsub"); mflag = 0; /* if mflag == 0, can replace empty string */ num = 0; x = execute(a[3]); /* target string */ t = getsval(x); if (a[0] == NULL) /* 0 => a[1] is already-compiled regexpr */ pfa = (fa *) a[1]; /* regular expression */ else { y = execute(a[1]); pfa = makedfa(getsval(y), 1); tempfree(y); } y = execute(a[2]); /* replacement string */ if (pmatch(pfa, t)) { tempstat = pfa->initstat; pfa->initstat = 2; pb = buf; rptr = getsval(y); do { if (patlen == 0 && *patbeg != '\0') { /* matched empty string */ if (mflag == 0) { /* can replace empty */ num++; sptr = rptr; while (*sptr != '\0') { adjbuf(&buf, &bufsz, 5+pb-buf, recsize, &pb, "gsub"); if (*sptr == '\\') { backsub(&pb, &sptr); } else if (*sptr == '&') { sptr++; adjbuf(&buf, &bufsz, 1+patlen+pb-buf, recsize, &pb, "gsub"); for (q = patbeg; q < patbeg+patlen; ) *pb++ = *q++; } else *pb++ = *sptr++; } } if (*t == '\0') /* at end */ goto done; adjbuf(&buf, &bufsz, 2+pb-buf, recsize, &pb, "gsub"); *pb++ = *t++; if (pb > buf + bufsz) /* BUG: not sure of this test */ FATAL("gsub result0 %.30s too big; can't happen", buf); mflag = 0; } else { /* matched nonempty string */ num++; sptr = t; adjbuf(&buf, &bufsz, 1+(patbeg-sptr)+pb-buf, recsize, &pb, "gsub"); while (sptr < patbeg) *pb++ = *sptr++; sptr = rptr; while (*sptr != '\0') { adjbuf(&buf, &bufsz, 5+pb-buf, recsize, &pb, "gsub"); if (*sptr == '\\') { backsub(&pb, &sptr); } else if (*sptr == '&') { sptr++; adjbuf(&buf, &bufsz, 1+patlen+pb-buf, recsize, &pb, "gsub"); for (q = patbeg; q < patbeg+patlen; ) *pb++ = *q++; } else *pb++ = *sptr++; } t = patbeg + patlen; if (patlen == 0 || *t == '\0' || *(t-1) == '\0') goto done; if (pb > buf + bufsz) FATAL("gsub result1 %.30s too big; can't happen", buf); mflag = 1; } } while (pmatch(pfa,t)); sptr = t; adjbuf(&buf, &bufsz, 1+strlen(sptr)+pb-buf, 0, &pb, "gsub"); while ((*pb++ = *sptr++) != '\0') continue; done: if (pb < buf + bufsz) *pb = '\0'; else if (*(pb-1) != '\0') FATAL("gsub result2 %.30s truncated; can't happen", buf); setsval(x, buf); /* BUG: should be able to avoid copy + free */ pfa->initstat = tempstat; } tempfree(x); tempfree(y); x = gettemp(); x->tval = NUM; x->fval = num; free(buf); return(x); } void backsub(char **pb_ptr, const char **sptr_ptr) /* handle \\& variations */ { /* sptr[0] == '\\' */ char *pb = *pb_ptr; const char *sptr = *sptr_ptr; static bool first = true; static bool do_posix = false; if (first) { first = false; do_posix = (getenv("POSIXLY_CORRECT") != NULL); } if (sptr[1] == '\\') { if (sptr[2] == '\\' && sptr[3] == '&') { /* \\\& -> \& */ *pb++ = '\\'; *pb++ = '&'; sptr += 4; } else if (sptr[2] == '&') { /* \\& -> \ + matched */ *pb++ = '\\'; sptr += 2; } else if (do_posix) { /* \\x -> \x */ sptr++; *pb++ = *sptr++; } else { /* \\x -> \\x */ *pb++ = *sptr++; *pb++ = *sptr++; } } else if (sptr[1] == '&') { /* literal & */ sptr++; *pb++ = *sptr++; } else /* literal \ */ *pb++ = *sptr++; *pb_ptr = pb; *sptr_ptr = sptr; }
50,184
2,126
jart/cosmopolitan
false
cosmopolitan/third_party/awk/awk.1
.de EX .nf .ft CW .. .de EE .br .fi .ft 1 .. .de TF .IP "" "\w'\fB\\$1\ \ \fP'u" .PD 0 .. .TH AWK 1 .CT 1 files prog_other .SH NAME awk \- pattern-directed scanning and processing language .SH SYNOPSIS .B awk [ .BI \-F .I fs ] [ .BI \-v .I var=value ] [ .I 'prog' | .BI \-f .I progfile ] [ .I file ... ] .SH DESCRIPTION .I Awk scans each input .I file for lines that match any of a set of patterns specified literally in .I prog or in one or more files specified as .B \-f .IR progfile . With each pattern there can be an associated action that will be performed when a line of a .I file matches the pattern. Each line is matched against the pattern portion of every pattern-action statement; the associated action is performed for each matched pattern. The file name .B \- means the standard input. Any .I file of the form .I var=value is treated as an assignment, not a filename, and is executed at the time it would have been opened if it were a filename. The option .B \-v followed by .I var=value is an assignment to be done before .I prog is executed; any number of .B \-v options may be present. The .B \-F .I fs option defines the input field separator to be the regular expression .IR fs . .PP An input line is normally made up of fields separated by white space, or by the regular expression .BR FS . The fields are denoted .BR $1 , .BR $2 , \&..., while .B $0 refers to the entire line. If .BR FS is null, the input line is split into one field per character. .PP A pattern-action statement has the form: .IP .IB pattern " { " action " } .PP A missing .BI { " action " } means print the line; a missing pattern always matches. Pattern-action statements are separated by newlines or semicolons. .PP An action is a sequence of statements. A statement can be one of the following: .PP .EX .ta \w'\f(CWdelete array[expression]\fR'u .RS .nf .ft CW if(\fI expression \fP)\fI statement \fP\fR[ \fPelse\fI statement \fP\fR]\fP while(\fI expression \fP)\fI statement\fP for(\fI expression \fP;\fI expression \fP;\fI expression \fP)\fI statement\fP for(\fI var \fPin\fI array \fP)\fI statement\fP do\fI statement \fPwhile(\fI expression \fP) break continue {\fR [\fP\fI statement ... \fP\fR] \fP} \fIexpression\fP #\fR commonly\fP\fI var = expression\fP print\fR [ \fP\fIexpression-list \fP\fR] \fP\fR[ \fP>\fI expression \fP\fR]\fP printf\fI format \fP\fR[ \fP,\fI expression-list \fP\fR] \fP\fR[ \fP>\fI expression \fP\fR]\fP return\fR [ \fP\fIexpression \fP\fR]\fP next #\fR skip remaining patterns on this input line\fP nextfile #\fR skip rest of this file, open next, start at top\fP delete\fI array\fP[\fI expression \fP] #\fR delete an array element\fP delete\fI array\fP #\fR delete all elements of array\fP exit\fR [ \fP\fIexpression \fP\fR]\fP #\fR exit immediately; status is \fP\fIexpression\fP .fi .RE .EE .DT .PP Statements are terminated by semicolons, newlines or right braces. An empty .I expression-list stands for .BR $0 . String constants are quoted \&\f(CW"\ "\fR, with the usual C escapes recognized within. Expressions take on string or numeric values as appropriate, and are built using the operators .B + \- * / % ^ (exponentiation), and concatenation (indicated by white space). The operators .B ! ++ \-\- += \-= *= /= %= ^= > >= < <= == != ?: are also available in expressions. Variables may be scalars, array elements (denoted .IB x [ i ] \fR) or fields. Variables are initialized to the null string. Array subscripts may be any string, not necessarily numeric; this allows for a form of associative memory. Multiple subscripts such as .B [i,j,k] are permitted; the constituents are concatenated, separated by the value of .BR SUBSEP . .PP The .B print statement prints its arguments on the standard output (or on a file if .BI > " file or .BI >> " file is present or on a pipe if .BI | " cmd is present), separated by the current output field separator, and terminated by the output record separator. .I file and .I cmd may be literal names or parenthesized expressions; identical string values in different statements denote the same open file. The .B printf statement formats its expression list according to the .I format (see .IR printf (3)). The built-in function .BI close( expr ) closes the file or pipe .IR expr . The built-in function .BI fflush( expr ) flushes any buffered output for the file or pipe .IR expr . .PP The mathematical functions .BR atan2 , .BR cos , .BR exp , .BR log , .BR sin , and .B sqrt are built in. Other built-in functions: .TF length .TP .B length the length of its argument taken as a string, number of elements in an array for an array argument, or length of .B $0 if no argument. .TP .B rand random number on [0,1). .TP .B srand sets seed for .B rand and returns the previous seed. .TP .B int truncates to an integer value. .TP \fBsubstr(\fIs\fB, \fIm\fR [\fB, \fIn\^\fR]\fB)\fR the .IR n -character substring of .I s that begins at position .I m counted from 1. If no .IR n , use the rest of the string. .TP .BI index( s , " t" ) the position in .I s where the string .I t occurs, or 0 if it does not. .TP .BI match( s , " r" ) the position in .I s where the regular expression .I r occurs, or 0 if it does not. The variables .B RSTART and .B RLENGTH are set to the position and length of the matched string. .TP \fBsplit(\fIs\fB, \fIa \fR[\fB, \fIfs\^\fR]\fB)\fR splits the string .I s into array elements .IB a [1] \fR, .IB a [2] \fR, \&..., .IB a [ n ] \fR, and returns .IR n . The separation is done with the regular expression .I fs or with the field separator .B FS if .I fs is not given. An empty string as field separator splits the string into one array element per character. .TP \fBsub(\fIr\fB, \fIt \fR[, \fIs\^\fR]\fB) substitutes .I t for the first occurrence of the regular expression .I r in the string .IR s . If .I s is not given, .B $0 is used. .TP \fBgsub(\fIr\fB, \fIt \fR[, \fIs\^\fR]\fB) same as .B sub except that all occurrences of the regular expression are replaced; .B sub and .B gsub return the number of replacements. .TP .BI sprintf( fmt , " expr" , " ...\fB) the string resulting from formatting .I expr ... according to the .IR printf (3) format .IR fmt . .TP .BI system( cmd ) executes .I cmd and returns its exit status. This will be \-1 upon error, .IR cmd 's exit status upon a normal exit, 256 + .I sig upon death-by-signal, where .I sig is the number of the murdering signal, or 512 + .I sig if there was a core dump. .TP .BI tolower( str ) returns a copy of .I str with all upper-case characters translated to their corresponding lower-case equivalents. .TP .BI toupper( str ) returns a copy of .I str with all lower-case characters translated to their corresponding upper-case equivalents. .PD .PP The ``function'' .B getline sets .B $0 to the next input record from the current input file; .B getline .BI < " file sets .B $0 to the next record from .IR file . .B getline .I x sets variable .I x instead. Finally, .IB cmd " | getline pipes the output of .I cmd into .BR getline ; each call of .B getline returns the next line of output from .IR cmd . In all cases, .B getline returns 1 for a successful input, 0 for end of file, and \-1 for an error. .PP Patterns are arbitrary Boolean combinations (with .BR "! || &&" ) of regular expressions and relational expressions. Regular expressions are as in .IR egrep ; see .IR grep (1). Isolated regular expressions in a pattern apply to the entire line. Regular expressions may also occur in relational expressions, using the operators .B ~ and .BR !~ . .BI / re / is a constant regular expression; any string (constant or variable) may be used as a regular expression, except in the position of an isolated regular expression in a pattern. .PP A pattern may consist of two patterns separated by a comma; in this case, the action is performed for all lines from an occurrence of the first pattern though an occurrence of the second. .PP A relational expression is one of the following: .IP .I expression matchop regular-expression .br .I expression relop expression .br .IB expression " in " array-name .br .BI ( expr , expr,... ") in " array-name .PP where a .I relop is any of the six relational operators in C, and a .I matchop is either .B ~ (matches) or .B !~ (does not match). A conditional is an arithmetic expression, a relational expression, or a Boolean combination of these. .PP The special patterns .B BEGIN and .B END may be used to capture control before the first input line is read and after the last. .B BEGIN and .B END do not combine with other patterns. They may appear multiple times in a program and execute in the order they are read by .IR awk . .PP Variable names with special meanings: .TF FILENAME .TP .B ARGC argument count, assignable. .TP .B ARGV argument array, assignable; non-null members are taken as filenames. .TP .B CONVFMT conversion format used when converting numbers (default .BR "%.6g" ). .TP .B ENVIRON array of environment variables; subscripts are names. .TP .B FILENAME the name of the current input file. .TP .B FNR ordinal number of the current record in the current file. .TP .B FS regular expression used to separate fields; also settable by option .BI \-F fs\fR. .TP .BR NF number of fields in the current record. .TP .B NR ordinal number of the current record. .TP .B OFMT output format for numbers (default .BR "%.6g" ). .TP .B OFS output field separator (default space). .TP .B ORS output record separator (default newline). .TP .B RLENGTH the length of a string matched by .BR match . .TP .B RS input record separator (default newline). If empty, blank lines separate records. If more than one character long, .B RS is treated as a regular expression, and records are separated by text matching the expression. .TP .B RSTART the start position of a string matched by .BR match . .TP .B SUBSEP separates multiple subscripts (default 034). .PD .PP Functions may be defined (at the position of a pattern-action statement) thus: .IP .B function foo(a, b, c) { ...; return x } .PP Parameters are passed by value if scalar and by reference if array name; functions may be called recursively. Parameters are local to the function; all other variables are global. Thus local variables may be created by providing excess parameters in the function definition. .SH ENVIRONMENT VARIABLES If .B POSIXLY_CORRECT is set in the environment, then .I awk follows the POSIX rules for .B sub and .B gsub with respect to consecutive backslashes and ampersands. .SH EXAMPLES .TP .EX length($0) > 72 .EE Print lines longer than 72 characters. .TP .EX { print $2, $1 } .EE Print first two fields in opposite order. .PP .EX BEGIN { FS = ",[ \et]*|[ \et]+" } { print $2, $1 } .EE .ns .IP Same, with input fields separated by comma and/or spaces and tabs. .PP .EX .nf { s += $1 } END { print "sum is", s, " average is", s/NR } .fi .EE .ns .IP Add up first column, print sum and average. .TP .EX /start/, /stop/ .EE Print all lines between start/stop pairs. .PP .EX .nf BEGIN { # Simulate echo(1) for (i = 1; i < ARGC; i++) printf "%s ", ARGV[i] printf "\en" exit } .fi .EE .SH SEE ALSO .IR grep (1), .IR lex (1), .IR sed (1) .br A. V. Aho, B. W. Kernighan, P. J. Weinberger, .IR "The AWK Programming Language" , Addison-Wesley, 1988. ISBN 0-201-07981-X. .SH BUGS There are no explicit conversions between numbers and strings. To force an expression to be treated as a number add 0 to it; to force it to be treated as a string concatenate \&\f(CW""\fP to it. .PP The scope rules for variables in functions are a botch; the syntax is worse. .PP Only eight-bit characters sets are handled correctly. .SH UNUSUAL FLOATING-POINT VALUES .I Awk was designed before IEEE 754 arithmetic defined Not-A-Number (NaN) and Infinity values, which are supported by all modern floating-point hardware. .PP Because .I awk uses .IR strtod (3) and .IR atof (3) to convert string values to double-precision floating-point values, modern C libraries also convert strings starting with .B inf and .B nan into infinity and NaN values respectively. This led to strange results, with something like this: .PP .EX .nf echo nancy | awk '{ print $1 + 0 }' .fi .EE .PP printing .B nan instead of zero. .PP .I Awk now follows GNU AWK, and prefilters string values before attempting to convert them to numbers, as follows: .TP .I "Hexadecimal values" Hexadecimal values (allowed since C99) convert to zero, as they did prior to C99. .TP .I "NaN values" The two strings .B +nan and .B \-nan (case independent) convert to NaN. No others do. (NaNs can have signs.) .TP .I "Infinity values" The two strings .B +inf and .B \-inf (case independent) convert to positive and negative infinity, respectively. No others do.
12,680
633
jart/cosmopolitan
false
cosmopolitan/third_party/awk/maketab.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Copyright (C) Lucent Technologies 1997 │ │ All Rights Reserved │ │ │ │ Permission to use, copy, modify, and distribute this software and │ │ its documentation for any purpose and without fee is hereby │ │ granted, provided that the above copyright notice appear in all │ │ copies and that both that the copyright notice and this │ │ permission notice and warranty disclaimer appear in supporting │ │ documentation, and that the name Lucent Technologies or any of │ │ its entities not be used in advertising or publicity pertaining │ │ to distribution of the software without specific, written prior │ │ permission. │ │ │ │ LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, │ │ INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. │ │ IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY │ │ SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES │ │ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER │ │ IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, │ │ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF │ │ THIS SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/fmt/fmt.h" #include "libc/mem/mem.h" #include "libc/runtime/runtime.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" #include "third_party/awk/awk.h" #include "third_party/awk/awkgram.tab.h" // clang-format off /* * this program makes the table to link function names * and type indices that is used by execute() in run.c. * it finds the indices in awkgram.tab.h, produced by bison. */ struct xx { int token; const char *name; const char *pname; } proc[] = { { PROGRAM, "program", NULL }, { BOR, "boolop", " || " }, { AND, "boolop", " && " }, { NOT, "boolop", " !" }, { NE, "relop", " != " }, { EQ, "relop", " == " }, { LE, "relop", " <= " }, { LT, "relop", " < " }, { GE, "relop", " >= " }, { GT, "relop", " > " }, { ARRAY, "array", NULL }, { INDIRECT, "indirect", "$(" }, { SUBSTR, "substr", "substr" }, { SUB, "sub", "sub" }, { GSUB, "gsub", "gsub" }, { INDEX, "sindex", "sindex" }, { SPRINTF, "awksprintf", "sprintf " }, { ADD, "arith", " + " }, { MINUS, "arith", " - " }, { MULT, "arith", " * " }, { DIVIDE, "arith", " / " }, { MOD, "arith", " % " }, { UMINUS, "arith", " -" }, { UPLUS, "arith", " +" }, { POWER, "arith", " **" }, { PREINCR, "incrdecr", "++" }, { POSTINCR, "incrdecr", "++" }, { PREDECR, "incrdecr", "--" }, { POSTDECR, "incrdecr", "--" }, { CAT, "cat", " " }, { PASTAT, "pastat", NULL }, { PASTAT2, "dopa2", NULL }, { MATCH, "matchop", " ~ " }, { NOTMATCH, "matchop", " !~ " }, { MATCHFCN, "matchop", "matchop" }, { INTEST, "intest", "intest" }, { PRINTF, "awkprintf", "printf" }, { PRINT, "printstat", "print" }, { CLOSE, "closefile", "closefile" }, { DELETE, "awkdelete", "awkdelete" }, { SPLIT, "split", "split" }, { ASSIGN, "assign", " = " }, { ADDEQ, "assign", " += " }, { SUBEQ, "assign", " -= " }, { MULTEQ, "assign", " *= " }, { DIVEQ, "assign", " /= " }, { MODEQ, "assign", " %= " }, { POWEQ, "assign", " ^= " }, { CONDEXPR, "condexpr", " ?: " }, { IF, "ifstat", "if(" }, { WHILE, "whilestat", "while(" }, { FOR, "forstat", "for(" }, { DO, "dostat", "do" }, { IN, "instat", "instat" }, { NEXT, "jump", "next" }, { NEXTFILE, "jump", "nextfile" }, { EXIT, "jump", "exit" }, { BREAK, "jump", "break" }, { CONTINUE, "jump", "continue" }, { RETURN, "jump", "ret" }, { BLTIN, "bltin", "bltin" }, { CALL, "call", "call" }, { ARG, "arg", "arg" }, { VARNF, "getnf", "NF" }, { GETLINE, "awkgetline", "getline" }, { 0, "", "" }, }; #define SIZE (LASTTOKEN - FIRSTTOKEN + 1) const char *table[SIZE]; char *names[SIZE]; int main(int argc, char *argv[]) { const struct xx *p; int i, n, tok; char c; FILE *fp; char buf[200], name[200], def[200]; enum { TOK_UNKNOWN, TOK_ENUM, TOK_DEFINE } tokentype = TOK_UNKNOWN; printf("#include \"libc/calls/calls.h\"\n"); printf("#include \"libc/fmt/fmt.h\"\n"); printf("#include \"libc/stdio/lock.h\"\n"); printf("#include \"libc/stdio/stdio.h\"\n"); printf("#include \"libc/stdio/temp.h\"\n"); printf("#include \"third_party/awk/awk.h\"\n"); printf("#include \"third_party/awk/awkgram.tab.h\"\n\n"); if (argc != 2) { fprintf(stderr, "usage: maketab YTAB_H\n"); exit(1); } if ((fp = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "maketab can't open %s!\n", argv[1]); exit(1); } printf("static const char * const printname[%d] = {\n", SIZE); i = 0; while (fgets(buf, sizeof buf, fp) != NULL) { // 199 is sizeof(def) - 1 if (tokentype != TOK_ENUM) { n = sscanf(buf, "%1c %199s %199s %d", &c, def, name, &tok); if (n == 4 && c == '#' && strcmp(def, "define") == 0) { tokentype = TOK_DEFINE; } else if (tokentype != TOK_UNKNOWN) { continue; } } if (tokentype != TOK_DEFINE) { /* not a valid #define, bison uses enums now */ n = sscanf(buf, "%199s = %d,\n", name, &tok); if (n != 2) continue; tokentype = TOK_ENUM; } if (strcmp(name, "YYSTYPE_IS_DECLARED") == 0) { tokentype = TOK_UNKNOWN; continue; } if (tok < FIRSTTOKEN || tok > LASTTOKEN) { tokentype = TOK_UNKNOWN; /* fprintf(stderr, "maketab funny token %d %s ignored\n", tok, buf); */ continue; } names[tok-FIRSTTOKEN] = strdup(name); if (names[tok-FIRSTTOKEN] == NULL) { fprintf(stderr, "maketab out of space copying %s", name); continue; } printf("\t\"%s\",\t/* %d */\n", name, tok); i++; } printf("};\n\n"); for (p=proc; p->token!=0; p++) table[p->token-FIRSTTOKEN] = p->name; printf("\nCell *(*proctab[%d])(Node **, int) = {\n", SIZE); for (i=0; i<SIZE; i++) printf("\t%s,\t/* %s */\n", table[i] ? table[i] : "nullproc", names[i] ? names[i] : ""); printf("};\n\n"); printf("const char *tokname(int n)\n"); /* print a tokname() function */ printf("{\n"); printf("\tstatic char buf[100];\n\n"); printf("\tif (n < FIRSTTOKEN || n > LASTTOKEN) {\n"); printf("\t\tsnprintf(buf, sizeof(buf), \"token %%d\", n);\n"); printf("\t\treturn buf;\n"); printf("\t}\n"); printf("\treturn printname[n-FIRSTTOKEN];\n"); printf("}\n"); return 0; }
7,623
203
jart/cosmopolitan
false
cosmopolitan/third_party/awk/awkgram.tab.c
// clang-format off #include "libc/mem/mem.h" #include "libc/str/str.h" #define YYBYACC 1 #define YYMAJOR 1 #define YYMINOR 9 #define YYLEX yylex() #define YYEMPTY -1 #define yyclearin (yychar=(YYEMPTY)) #define yyerrok (yyerrflag=0) #define YYRECOVERING() (yyerrflag!=0) #define YYPREFIX "yy" #line 26 "awkgram.y" #include "third_party/awk/awk.h" void checkdup(Node *list, Cell *item); int yywrap(void) { return(1); } Node *beginloc = 0; Node *endloc = 0; bool infunc = false; /* = true if in arglist or body of func */ int inloop = 0; /* >= 1 if in while, for, do; can't be bool, since loops can next */ char *curfname = 0; /* current function name */ Node *arglist = 0; /* list of args for current function */ #line 41 "awkgram.y" #ifndef YYSTYPE_DEFINED #define YYSTYPE_DEFINED typedef union { Node *p; Cell *cp; int i; char *s; } YYSTYPE; #endif /* YYSTYPE_DEFINED */ #line 37 "awkgram.tab.c" #define FIRSTTOKEN 257 #define PROGRAM 258 #define PASTAT 259 #define PASTAT2 260 #define XBEGIN 261 #define XEND 262 #define NL 263 #define ARRAY 264 #define MATCH 265 #define NOTMATCH 266 #define MATCHOP 267 #define FINAL 268 #define DOT 269 #define ALL 270 #define CCL 271 #define NCCL 272 #define CHAR 273 #define OR 274 #define STAR 275 #define QUEST 276 #define PLUS 277 #define EMPTYRE 278 #define ZERO 279 #define AND 280 #define BOR 281 #define APPEND 282 #define EQ 283 #define GE 284 #define GT 285 #define LE 286 #define LT 287 #define NE 288 #define IN 289 #define ARG 290 #define BLTIN 291 #define BREAK 292 #define CLOSE 293 #define CONTINUE 294 #define DELETE 295 #define DO 296 #define EXIT 297 #define FOR 298 #define FUNC 299 #define SUB 300 #define GSUB 301 #define IF 302 #define INDEX 303 #define LSUBSTR 304 #define MATCHFCN 305 #define NEXT 306 #define NEXTFILE 307 #define ADD 308 #define MINUS 309 #define MULT 310 #define DIVIDE 311 #define MOD 312 #define ASSIGN 313 #define ASGNOP 314 #define ADDEQ 315 #define SUBEQ 316 #define MULTEQ 317 #define DIVEQ 318 #define MODEQ 319 #define POWEQ 320 #define PRINT 321 #define PRINTF 322 #define SPRINTF 323 #define ELSE 324 #define INTEST 325 #define CONDEXPR 326 #define POSTINCR 327 #define PREINCR 328 #define POSTDECR 329 #define PREDECR 330 #define VAR 331 #define IVAR 332 #define VARNF 333 #define CALL 334 #define NUMBER 335 #define STRING 336 #define REGEXPR 337 #define GETLINE 338 #define RETURN 339 #define SPLIT 340 #define SUBSTR 341 #define WHILE 342 #define CAT 343 #define NOT 344 #define UMINUS 345 #define UPLUS 346 #define POWER 347 #define DECR 348 #define INCR 349 #define INDIRECT 350 #define LASTTOKEN 351 #define YYERRCODE 256 const short yylhs[] = { -1, 0, 0, 36, 36, 37, 37, 33, 33, 26, 26, 24, 24, 41, 22, 42, 22, 43, 22, 20, 20, 23, 30, 30, 34, 34, 35, 35, 29, 29, 15, 15, 1, 1, 10, 11, 11, 11, 11, 11, 11, 11, 44, 11, 12, 12, 6, 6, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 5, 5, 7, 7, 7, 39, 39, 28, 28, 28, 28, 31, 31, 9, 9, 45, 13, 32, 32, 14, 14, 14, 14, 14, 14, 14, 14, 27, 27, 16, 16, 46, 47, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 48, 16, 16, 17, 17, 38, 38, 40, 40, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 18, 18, 18, 18, 21, 21, 21, 19, 19, 19, 25, }; const short yylen[] = { 2, 1, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 0, 12, 0, 10, 0, 8, 1, 1, 4, 1, 2, 1, 2, 0, 1, 0, 1, 0, 1, 1, 3, 1, 1, 4, 4, 7, 3, 4, 4, 0, 9, 1, 3, 1, 3, 3, 5, 3, 3, 3, 3, 3, 5, 2, 1, 1, 3, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 4, 3, 2, 1, 1, 3, 3, 1, 3, 0, 1, 3, 1, 1, 1, 1, 2, 2, 1, 2, 1, 2, 0, 4, 1, 2, 4, 4, 4, 2, 5, 2, 1, 1, 1, 2, 2, 2, 0, 0, 9, 3, 2, 1, 4, 2, 3, 2, 2, 3, 2, 2, 0, 3, 2, 1, 2, 1, 1, 1, 2, 4, 3, 3, 3, 3, 3, 3, 2, 2, 2, 3, 4, 1, 3, 4, 2, 2, 2, 2, 2, 4, 3, 2, 1, 6, 6, 3, 6, 6, 1, 8, 8, 6, 4, 1, 6, 6, 8, 8, 8, 6, 1, 1, 4, 1, 2, 0, 1, 3, 1, 1, 1, 4, }; const short yydefred[] = { 0, 2, 87, 88, 0, 1, 0, 0, 89, 90, 0, 0, 22, 0, 95, 184, 0, 0, 0, 130, 131, 0, 0, 0, 183, 178, 185, 0, 163, 132, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76, 0, 44, 0, 93, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 94, 150, 151, 179, 0, 0, 3, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 152, 153, 0, 106, 23, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 85, 86, 0, 0, 0, 0, 128, 0, 116, 0, 125, 0, 0, 0, 0, 133, 0, 0, 7, 160, 0, 0, 0, 0, 144, 0, 0, 0, 0, 0, 0, 0, 147, 0, 0, 0, 0, 0, 0, 0, 69, 0, 0, 0, 0, 0, 0, 71, 0, 4, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 24, 0, 0, 0, 45, 0, 0, 127, 0, 109, 0, 110, 0, 0, 115, 0, 0, 120, 121, 0, 123, 0, 124, 39, 129, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 57, 0, 0, 0, 40, 41, 8, 0, 0, 0, 96, 145, 0, 181, 0, 0, 0, 167, 148, 0, 0, 0, 73, 0, 0, 25, 0, 36, 177, 108, 0, 114, 31, 0, 0, 0, 122, 0, 11, 0, 126, 112, 91, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72, 0, 97, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 117, 0, 92, 0, 0, 0, 52, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 0, 182, 158, 159, 162, 161, 166, 0, 174, 0, 0, 103, 0, 0, 0, 0, 0, 0, 0, 170, 0, 169, 0, 0, 0, 0, 0, 38, 0, 0, 0, 0, 55, 0, 0, 0, 0, 0, 164, 165, 173, 0, 0, 0, 0, 172, 171, 43, 0, 0, 18, 0, 0, 0, 113, 16, 0, 0, 14, }; const short yydgoto[] = { 4, 5, 123, 210, 54, 211, 145, 212, 40, 41, 42, 43, 44, 45, 124, 247, 125, 126, 46, 47, 63, 228, 127, 128, 253, 129, 130, 188, 6, 7, 131, 257, 275, 226, 189, 181, 92, 93, 49, 132, 50, 367, 362, 353, 310, 55, 207, 292, 205, }; const short yysindex[] = { -55, 0, 0, 0, 0, 0, -39, 8470, 0, 0, -66, -66, 0, 5698, 0, 0, 32, 1365, -255, 0, 0, 43, 53, 64, 0, 0, 0, 89, 0, 0, 56, 90, 107, 1365, 1365, 9050, -114, -114, 1365, 7895, -37, 0, -13, 0, -29, 0, -264, 40, 5023, 108, -184, 5023, 5023, 1864, 46, -179, 368, 5698, 1365, -37, -230, 0, 0, 120, 5698, 5698, 5698, 3071, 1365, -117, 5698, 5698, -176, -176, -176, 0, 0, 0, 0, -151, 5698, 0, 0, 5698, 5698, 5698, 5698, 5698, 5698, -178, 5698, -37, 8646, 8734, 866, 1365, 1365, 1365, 1365, 1365, -74, 5023, 8470, 5698, 0, 0, 5698, 0, 0, -74, -24, -24, -178, 0, 8566, 165, 172, -24, -24, 0, 0, 8566, 205, 7895, -24, 0, 5157, 0, 6011, 0, -2, 5023, 9074, 5698, 0, 5352, 5425, 0, 0, 8801, -19, 8801, 203, 0, 7895, 96, 7035, -64, 7107, 7107, 100, 0, 102, -37, 1365, 7107, 7107, -114, 9202, 0, 9202, 9202, 9202, 9202, 9202, 9202, 0, 7182, 0, 8331, 0, 8240, 1365, -176, -35, -35, -176, -176, -176, 0, 3, 5698, 5486, 0, 7895, -23, 0, -74, 0, 3, 0, 180, 6709, 0, 1771, 5698, 0, 0, 6709, 0, 5698, 0, 0, 0, -52, 6011, 0, 6011, 5547, 5698, 8542, 233, -108, -37, 0, -252, 7107, 233, 0, 0, 0, 7895, -178, 7895, 0, 0, 8801, 0, 118, 8801, 8801, 0, 0, -37, -210, 8801, 0, 5698, -37, 0, -66, 0, 0, 0, 5698, 0, 0, 236, -80, 7289, 0, 7289, 0, 5621, 0, 0, 0, 44, 134, 9126, -178, 9126, -37, 8877, 8959, 8983, 1365, 1365, 1365, 9126, 8801, 8801, 0, 7895, 0, 50, -223, 7374, 256, 7461, 281, 138, 6781, 7895, 5023, 8, -5, -178, 50, 50, 0, 0, -17, 0, 38, 5698, 9202, 0, 0, 8407, 4219, 271, 8542, -37, -37, -37, 8542, 6853, 6963, 0, -66, 0, 0, 0, 0, 0, 0, 8801, 0, 8801, 5815, 0, -74, 5698, 282, 291, -178, 142, 9126, 0, 36, 0, 36, 5023, 7556, 297, 7641, 0, 1771, 7727, 50, 5698, 0, 38, 8542, 303, 314, 5889, 0, 0, 0, 282, -74, 6011, 7823, 0, 0, 0, 50, 1771, 0, -24, 6011, 282, 0, 0, 50, 6011, 0,}; const short yyrindex[] = { 3301, 0, 0, 0, 0, 0, 3353, 356, 0, 0, 0, 0, 0, 0, 0, 0, 91, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3620, 0, 0, 0, 0, 0, 0, 0, 0, 67, 2889, 0, 3155, 0, 3301, 0, 1993, 1, 0, 0, 185, 0, 0, 0, 0, 0, 0, 0, 0, 2349, 1682, 0, 0, 0, 0, 0, 0, 0, 0, 500, 0, 0, 590, 684, 999, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2983, 0, 0, 0, 0, 0, 0, 0, 0, 9150, 0, 362, 0, 0, 0, 0, 0, 0, 4816, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -32, 0, 0, 0, 0, 0, 0, 6084, 0, 239, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22, 0, 0, 162, 0, 0, 0, 0, 0, 2443, 0, 0, 0, 3690, 1047, 0, 1546, 2490, 3723, 3774, 3784, 3817, 0, 0, 0, 4547, 0, 856, 0, 1089, 2083, 2177, 1183, 1498, 1588, 0, 4099, 0, 0, 0, 58, 0, 0, 4816, 0, 4928, 0, -22, 0, 0, 305, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5950, 0, 0, 0, 0, 0, 1011, 306, 6, 8077, 0, 4755, 0, 7981, 0, 0, 0, 163, 0, 169, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2533, 0, 0, 0, 0, 2799, 0, 3249, 0, 0, 0, 0, 0, 0, 0, 4662, 0, 0, 0, 0, 0, 0, 0, 0, 5084, 0, 0, 0, 0, 8153, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 34, 0, 247, 0, 0, 7981, 0, 7981, 0, 0, 98, 0, 0, 9150, 0, 6157, 6218, 0, 0, 0, 0, 403, 0, 216, 0, 0, 0, 179, 635, 1034, 10, 30, 33, 539, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6620, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7981, 0, 0, 342, 0, 6279, 0, 0, 0, 768, 0, 0, 0, 0, 0, 0, 0, 6620, 0, 0, 0, 0, 0, 6352, 342, 0, 0, 0, 0, 0, 0, 6425, 0, 0,}; const short yygindex[] = { 0, 0, 4594, 391, -175, 0, -11, 0, 4313, -10, 204, 293, 0, -65, -166, -277, 1227, -28, 4033, 891, 0, 0, 0, 0, 0, 0, 0, -85, 0, 349, 7, 0, -163, 429, -87, -106, -133, -104, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; #define YYTABLESIZE 9552 const short yytable[] = { 98, 176, 98, 186, 3, 97, 95, 97, 96, 105, 94, 106, 94, 180, 48, 159, 266, 51, 52, 104, 9, 137, 180, 135, 136, 75, 190, 105, 246, 193, 3, 100, 196, 197, 258, 187, 199, 104, 176, 201, 220, 176, 176, 176, 176, 176, 176, 102, 176, 101, 103, 99, 137, 220, 322, 150, 152, 12, 59, 176, 176, 351, 269, 46, 176, 102, 46, 34, 217, 99, 242, 100, 56, 182, 101, 47, 61, 263, 47, 62, 15, 243, 363, 64, 104, 105, 288, 140, 289, 100, 137, 146, 101, 65, 176, 185, 104, 105, 60, 59, 180, 321, 59, 208, 66, 59, 264, 245, 311, 48, 12, 34, 15, 250, 34, 46, 59, 59, 104, 105, 327, 24, 214, 26, 176, 176, 34, 47, 146, 67, 70, 106, 146, 146, 146, 146, 146, 225, 146, 60, 137, 231, 60, 232, 137, 60, 137, 71, 133, 146, 146, 59, 134, 24, 146, 26, 60, 60, 142, 274, 147, 340, 137, 263, 278, 280, 263, 263, 263, 263, 154, 99, 246, 263, 267, 294, 15, 268, 137, 316, 323, 59, 137, 343, 146, 168, 137, 157, 358, 179, 34, 60, 264, 246, 297, 264, 264, 264, 264, 180, 366, 1, 264, 180, 78, 194, 180, 78, 2, 287, 79, 263, 195, 79, 146, 146, 338, 24, 25, 26, 51, 60, 168, 51, 8, 168, 168, 168, 168, 168, 168, 105, 168, 285, 2, 180, 38, 51, 51, 179, 264, 104, 51, 168, 168, 200, 359, 284, 168, 214, 224, 214, 335, 214, 214, 214, 320, 53, 179, 214, 53, 206, 176, 176, 176, 180, 239, 227, 176, 102, 222, 244, 252, 99, 53, 53, 364, 137, 168, 53, 82, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 100, 176, 286, 101, 313, 82, 220, 176, 176, 176, 51, 176, 347, 176, 293, 168, 168, 99, 57, 99, 309, 33, 176, 34, 333, 214, 59, 59, 59, 315, 274, 176, 325, 15, 326, 34, 34, 34, 341, 176, 176, 176, 176, 176, 176, 349, 176, 53, 176, 176, 68, 355, 176, 15, 83, 176, 176, 176, 176, 146, 146, 146, 356, 32, 59, 146, 60, 60, 60, 33, 82, 30, 83, 34, 24, 25, 26, 42, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 30, 146, 240, 38, 24, 25, 26, 146, 146, 146, 102, 146, 183, 146, 60, 0, 0, 0, 0, 0, 0, 0, 146, 38, 0, 13, 143, 0, 33, 0, 34, 146, 14, 0, 0, 0, 0, 0, 0, 146, 146, 146, 146, 146, 146, 0, 146, 83, 146, 146, 0, 0, 146, 0, 0, 146, 146, 146, 146, 51, 0, 84, 0, 168, 168, 168, 0, 0, 0, 168, 0, 0, 0, 0, 0, 0, 51, 51, 51, 84, 0, 51, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 0, 168, 53, 0, 0, 139, 141, 168, 168, 168, 0, 168, 0, 168, 0, 0, 0, 0, 0, 53, 53, 53, 168, 156, 53, 82, 0, 0, 0, 0, 0, 168, 0, 0, 0, 0, 0, 0, 0, 168, 168, 168, 168, 168, 82, 0, 168, 82, 168, 168, 84, 0, 168, 0, 0, 168, 168, 168, 168, 0, 156, 259, 0, 156, 156, 156, 156, 156, 156, 0, 156, 0, 0, 0, 81, 0, 0, 53, 0, 0, 0, 156, 156, 260, 15, 16, 156, 17, 0, 0, 0, 0, 83, 0, 19, 20, 0, 21, 0, 22, 229, 230, 0, 48, 0, 0, 48, 234, 235, 0, 0, 83, 0, 142, 83, 0, 156, 23, 0, 0, 48, 48, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 0, 58, 0, 0, 0, 36, 37, 38, 0, 156, 156, 0, 0, 142, 0, 0, 142, 142, 142, 142, 142, 142, 0, 142, 0, 0, 265, 0, 0, 0, 0, 270, 271, 0, 142, 142, 296, 0, 299, 142, 300, 301, 302, 276, 15, 16, 306, 17, 0, 48, 0, 0, 84, 0, 19, 20, 0, 21, 0, 22, 0, 0, 50, 0, 0, 50, 0, 0, 0, 142, 141, 84, 0, 141, 84, 0, 0, 23, 0, 50, 50, 0, 0, 0, 50, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 317, 319, 35, 142, 142, 0, 36, 37, 38, 344, 0, 141, 0, 0, 141, 141, 141, 141, 141, 141, 0, 141, 0, 0, 0, 0, 330, 332, 0, 0, 0, 0, 141, 141, 0, 0, 0, 141, 0, 0, 0, 0, 0, 0, 0, 0, 141, 0, 0, 50, 0, 156, 156, 156, 0, 0, 0, 156, 0, 0, 0, 0, 0, 0, 0, 0, 0, 141, 0, 0, 156, 156, 156, 156, 156, 156, 156, 0, 156, 156, 156, 156, 0, 156, 0, 0, 0, 0, 0, 156, 156, 156, 48, 156, 0, 156, 0, 141, 141, 49, 0, 0, 49, 0, 156, 0, 0, 0, 0, 0, 0, 48, 0, 156, 48, 0, 49, 49, 0, 0, 0, 156, 156, 156, 156, 156, 156, 0, 156, 0, 156, 156, 0, 0, 156, 0, 0, 156, 156, 156, 156, 142, 142, 142, 0, 0, 61, 142, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 0, 142, 0, 0, 0, 0, 0, 142, 142, 142, 49, 142, 0, 142, 0, 61, 50, 0, 61, 0, 0, 61, 142, 0, 57, 0, 0, 33, 0, 34, 0, 142, 61, 61, 50, 50, 0, 61, 50, 142, 142, 142, 142, 142, 142, 0, 142, 0, 142, 142, 0, 0, 142, 0, 0, 0, 142, 142, 142, 0, 0, 0, 0, 141, 141, 141, 0, 61, 0, 141, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 0, 141, 0, 61, 166, 0, 0, 141, 141, 141, 0, 141, 0, 141, 0, 0, 0, 0, 0, 0, 0, 0, 141, 143, 0, 0, 0, 191, 0, 0, 0, 141, 0, 0, 0, 0, 0, 0, 0, 141, 141, 141, 141, 141, 141, 0, 141, 0, 141, 141, 0, 0, 141, 0, 0, 49, 141, 141, 141, 0, 143, 0, 0, 143, 143, 143, 143, 143, 143, 0, 143, 70, 0, 0, 49, 0, 80, 49, 0, 80, 0, 143, 143, 0, 0, 0, 143, 0, 0, 0, 0, 0, 0, 0, 80, 0, 0, 0, 0, 81, 0, 0, 81, 0, 0, 0, 0, 0, 0, 248, 0, 0, 70, 138, 0, 70, 143, 81, 70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 70, 70, 0, 0, 0, 70, 0, 0, 272, 0, 0, 0, 61, 61, 61, 0, 0, 143, 143, 0, 281, 138, 0, 0, 138, 138, 138, 138, 138, 138, 80, 138, 61, 0, 0, 70, 0, 0, 0, 0, 0, 0, 138, 138, 0, 0, 298, 138, 0, 0, 61, 15, 16, 81, 17, 0, 0, 0, 0, 0, 0, 19, 20, 0, 21, 70, 22, 0, 0, 0, 0, 0, 0, 324, 0, 172, 0, 138, 137, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 0, 58, 0, 138, 138, 36, 37, 38, 342, 0, 0, 137, 0, 0, 137, 137, 137, 137, 137, 137, 0, 137, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 137, 137, 0, 0, 0, 137, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 143, 143, 143, 0, 0, 0, 143, 0, 0, 0, 0, 0, 0, 0, 80, 0, 137, 0, 0, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 0, 143, 80, 0, 0, 80, 81, 143, 143, 143, 0, 143, 0, 143, 0, 137, 137, 70, 70, 70, 0, 0, 143, 0, 0, 81, 0, 0, 81, 0, 0, 143, 0, 0, 0, 0, 70, 70, 0, 143, 143, 143, 143, 143, 143, 0, 143, 0, 143, 143, 0, 0, 143, 0, 0, 70, 143, 143, 143, 138, 138, 138, 203, 0, 204, 138, 0, 0, 0, 0, 0, 203, 203, 0, 0, 0, 0, 0, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 0, 138, 0, 0, 70, 0, 0, 138, 138, 138, 0, 138, 0, 138, 0, 0, 0, 0, 0, 0, 0, 0, 138, 0, 57, 0, 0, 33, 203, 34, 0, 138, 0, 0, 0, 0, 0, 0, 0, 138, 138, 138, 138, 138, 138, 0, 138, 0, 138, 138, 0, 254, 138, 255, 203, 0, 138, 138, 138, 0, 0, 0, 0, 137, 137, 137, 0, 0, 0, 137, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 0, 137, 0, 0, 0, 291, 0, 137, 137, 137, 0, 137, 0, 137, 0, 0, 0, 0, 0, 0, 0, 0, 137, 139, 0, 0, 0, 0, 0, 0, 0, 137, 0, 0, 0, 0, 0, 0, 0, 137, 137, 137, 137, 137, 137, 0, 137, 0, 137, 137, 0, 0, 137, 0, 0, 0, 137, 137, 137, 0, 139, 0, 0, 139, 139, 139, 139, 139, 139, 0, 139, 63, 203, 0, 0, 0, 0, 0, 0, 0, 0, 139, 139, 0, 0, 0, 139, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 203, 0, 0, 0, 0, 0, 360, 0, 0, 0, 0, 0, 0, 63, 140, 365, 63, 139, 0, 63, 368, 0, 0, 0, 0, 0, 0, 0, 0, 0, 63, 63, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 139, 139, 0, 0, 140, 0, 0, 140, 140, 140, 140, 140, 140, 0, 140, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, 140, 140, 0, 0, 0, 140, 0, 0, 0, 15, 16, 0, 17, 0, 0, 0, 0, 0, 0, 19, 20, 0, 21, 63, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 140, 175, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 0, 58, 0, 140, 140, 36, 37, 38, 0, 0, 0, 175, 0, 0, 175, 175, 175, 175, 175, 175, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 175, 175, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 139, 139, 139, 0, 0, 0, 139, 0, 0, 0, 0, 0, 0, 0, 0, 0, 175, 0, 0, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 0, 139, 0, 0, 0, 0, 0, 139, 139, 139, 0, 139, 0, 139, 0, 175, 175, 63, 63, 63, 0, 13, 139, 0, 33, 0, 34, 0, 14, 0, 0, 139, 0, 0, 0, 0, 63, 63, 0, 139, 139, 139, 139, 139, 139, 0, 139, 0, 139, 139, 0, 0, 139, 0, 0, 63, 139, 139, 139, 140, 140, 140, 0, 0, 0, 140, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 0, 140, 0, 0, 63, 0, 0, 140, 140, 140, 0, 140, 0, 140, 0, 0, 0, 0, 0, 0, 0, 0, 140, 0, 57, 138, 0, 33, 137, 34, 0, 140, 0, 0, 0, 0, 0, 0, 0, 140, 140, 140, 140, 140, 140, 0, 140, 90, 140, 140, 0, 0, 140, 0, 0, 0, 140, 140, 140, 0, 0, 0, 0, 175, 175, 175, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 0, 175, 0, 0, 0, 0, 0, 175, 175, 175, 0, 175, 0, 175, 79, 0, 0, 0, 0, 175, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 175, 175, 175, 175, 175, 175, 0, 175, 0, 175, 175, 0, 0, 175, 107, 0, 175, 175, 0, 175, 175, 175, 175, 175, 175, 175, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 175, 175, 0, 0, 0, 175, 0, 0, 0, 0, 15, 16, 0, 17, 0, 112, 0, 0, 0, 0, 19, 20, 0, 21, 0, 22, 0, 0, 0, 0, 0, 0, 135, 0, 0, 175, 0, 0, 0, 0, 0, 119, 120, 23, 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 0, 35, 175, 175, 0, 36, 37, 38, 0, 135, 135, 0, 135, 135, 135, 0, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 135, 135, 0, 81, 82, 135, 83, 84, 85, 86, 87, 88, 89, 15, 16, 0, 17, 0, 0, 0, 0, 0, 0, 19, 20, 0, 21, 0, 22, 0, 0, 0, 0, 0, 0, 135, 136, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 135, 135, 58, 0, 0, 0, 36, 37, 38, 0, 0, 136, 136, 0, 136, 136, 136, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 136, 136, 0, 0, 0, 136, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 175, 175, 175, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 136, 0, 0, 175, 175, 0, 175, 175, 175, 175, 175, 175, 175, 175, 175, 0, 175, 0, 0, 0, 0, 0, 175, 175, 175, 0, 175, 0, 175, 0, 136, 136, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 175, 175, 175, 175, 175, 175, 0, 175, 0, 175, 175, 0, 0, 175, 0, 0, 175, 0, 0, 175, 135, 135, 135, 0, 0, 149, 135, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 0, 135, 0, 0, 0, 0, 0, 135, 135, 135, 0, 135, 0, 135, 149, 149, 0, 0, 149, 0, 0, 0, 135, 0, 0, 0, 0, 0, 0, 0, 0, 135, 149, 149, 0, 0, 0, 149, 0, 135, 135, 135, 135, 135, 135, 0, 135, 0, 135, 135, 0, 0, 135, 0, 0, 0, 135, 135, 135, 0, 0, 0, 0, 136, 136, 136, 0, 149, 155, 136, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 0, 136, 0, 149, 149, 0, 0, 136, 136, 136, 0, 136, 0, 136, 155, 155, 0, 0, 155, 0, 0, 64, 136, 0, 0, 0, 0, 0, 0, 0, 0, 136, 155, 155, 0, 0, 0, 155, 0, 136, 136, 136, 136, 136, 136, 0, 136, 0, 136, 136, 0, 0, 136, 0, 0, 0, 136, 136, 136, 0, 0, 0, 64, 0, 154, 64, 0, 155, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 64, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155, 155, 0, 0, 0, 0, 0, 154, 154, 0, 0, 154, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 154, 154, 0, 0, 0, 154, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 149, 149, 149, 64, 0, 0, 149, 0, 0, 0, 0, 0, 0, 0, 0, 0, 154, 0, 0, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 0, 149, 0, 0, 0, 0, 0, 149, 149, 149, 0, 149, 0, 149, 0, 154, 154, 0, 0, 0, 0, 0, 149, 0, 0, 0, 0, 0, 0, 0, 0, 149, 0, 0, 0, 0, 0, 0, 0, 149, 149, 149, 149, 149, 149, 0, 149, 0, 149, 149, 0, 0, 149, 0, 0, 0, 149, 149, 149, 0, 0, 0, 0, 155, 155, 155, 0, 0, 0, 155, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 0, 155, 0, 0, 0, 0, 0, 155, 155, 155, 0, 155, 0, 155, 0, 0, 64, 64, 64, 0, 0, 0, 155, 0, 0, 0, 0, 0, 0, 0, 0, 155, 0, 0, 0, 64, 64, 0, 0, 155, 155, 155, 155, 155, 155, 0, 155, 0, 155, 155, 0, 0, 155, 0, 64, 0, 155, 155, 155, 154, 154, 154, 0, 0, 134, 154, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154, 0, 154, 0, 64, 0, 0, 0, 154, 154, 154, 0, 154, 0, 154, 134, 134, 0, 0, 134, 0, 0, 0, 154, 0, 0, 0, 0, 0, 0, 0, 0, 154, 134, 134, 0, 0, 0, 134, 0, 154, 154, 154, 154, 154, 154, 0, 154, 0, 154, 154, 0, 0, 154, 0, 0, 0, 154, 154, 154, 0, 0, 0, 0, 0, 77, 0, 0, 134, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 134, 134, 0, 0, 0, 0, 0, 77, 77, 0, 0, 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 77, 77, 0, 0, 0, 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 77, 75, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 77, 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 75, 75, 0, 0, 75, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 75, 75, 0, 0, 0, 75, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 134, 134, 134, 0, 0, 0, 134, 0, 0, 0, 0, 0, 0, 0, 0, 0, 75, 0, 0, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 0, 134, 0, 0, 0, 0, 0, 134, 134, 134, 0, 134, 0, 134, 0, 75, 75, 0, 0, 0, 13, 151, 134, 33, 0, 34, 0, 14, 0, 0, 0, 134, 0, 0, 0, 0, 0, 0, 0, 134, 134, 134, 134, 134, 134, 0, 134, 0, 134, 134, 0, 0, 134, 0, 0, 0, 134, 134, 134, 77, 77, 77, 0, 0, 35, 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 77, 77, 0, 77, 77, 77, 77, 77, 77, 77, 77, 77, 0, 77, 0, 0, 0, 0, 0, 77, 77, 77, 0, 77, 0, 77, 35, 0, 0, 35, 0, 35, 0, 35, 0, 0, 0, 0, 0, 0, 0, 0, 0, 77, 0, 35, 0, 0, 0, 0, 0, 77, 77, 77, 77, 77, 77, 0, 77, 0, 77, 77, 0, 0, 77, 0, 0, 0, 77, 77, 77, 0, 0, 0, 0, 75, 75, 75, 0, 0, 37, 75, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 75, 75, 0, 75, 75, 75, 75, 75, 75, 75, 75, 75, 0, 75, 0, 0, 0, 0, 0, 75, 75, 75, 0, 75, 0, 75, 37, 0, 0, 37, 0, 37, 0, 37, 0, 0, 0, 0, 28, 0, 0, 0, 0, 75, 0, 37, 0, 0, 0, 0, 0, 75, 75, 75, 75, 75, 75, 0, 75, 0, 75, 75, 0, 0, 75, 0, 0, 0, 75, 75, 75, 0, 0, 0, 0, 0, 0, 0, 28, 0, 0, 28, 0, 28, 0, 28, 0, 0, 0, 0, 29, 0, 0, 0, 0, 0, 0, 0, 15, 16, 0, 17, 0, 0, 0, 0, 0, 0, 19, 20, 0, 21, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 23, 0, 29, 0, 29, 0, 29, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 0, 35, 35, 35, 35, 36, 37, 38, 0, 0, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 35, 35, 0, 35, 0, 0, 0, 0, 0, 35, 35, 35, 0, 35, 0, 35, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 35, 0, 0, 0, 0, 0, 0, 0, 35, 35, 35, 35, 35, 35, 0, 35, 0, 35, 35, 0, 0, 35, 0, 0, 0, 35, 35, 35, 0, 0, 0, 0, 37, 37, 37, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 37, 0, 37, 0, 0, 0, 0, 0, 37, 37, 37, 0, 37, 0, 37, 0, 0, 0, 0, 0, 0, 0, 28, 28, 0, 0, 0, 0, 0, 0, 0, 0, 37, 0, 0, 0, 0, 0, 0, 0, 37, 37, 37, 37, 37, 37, 0, 37, 0, 37, 37, 28, 28, 37, 28, 0, 0, 37, 37, 37, 28, 28, 28, 0, 28, 0, 28, 0, 0, 0, 0, 0, 0, 0, 29, 29, 0, 0, 0, 0, 157, 0, 0, 0, 28, 0, 0, 0, 0, 0, 0, 0, 28, 28, 28, 28, 28, 28, 0, 28, 0, 28, 28, 29, 29, 28, 29, 0, 0, 28, 28, 28, 29, 29, 29, 0, 29, 157, 29, 0, 157, 157, 157, 157, 157, 157, 0, 157, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 157, 157, 0, 0, 0, 157, 29, 29, 29, 29, 29, 29, 74, 29, 0, 29, 29, 0, 0, 29, 0, 0, 0, 29, 29, 29, 0, 0, 0, 0, 0, 0, 0, 0, 0, 157, 0, 0, 0, 0, 0, 0, 0, 0, 0, 65, 0, 0, 0, 0, 0, 0, 74, 74, 0, 74, 74, 74, 0, 74, 0, 0, 0, 0, 0, 157, 157, 0, 0, 0, 74, 74, 0, 0, 0, 74, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 65, 0, 0, 65, 0, 0, 65, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 65, 65, 74, 67, 0, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 74, 74, 66, 65, 68, 66, 0, 0, 66, 0, 0, 0, 67, 0, 0, 67, 0, 0, 67, 66, 66, 0, 0, 0, 66, 0, 0, 0, 0, 67, 67, 0, 0, 65, 67, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 68, 0, 0, 68, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 68, 68, 67, 0, 0, 68, 157, 157, 157, 0, 0, 0, 157, 0, 0, 0, 0, 0, 0, 0, 0, 0, 66, 0, 0, 157, 157, 157, 157, 157, 157, 157, 67, 157, 157, 68, 157, 0, 157, 0, 0, 0, 0, 0, 157, 157, 157, 0, 157, 0, 157, 0, 0, 0, 0, 0, 0, 0, 0, 157, 0, 0, 0, 0, 0, 68, 0, 0, 157, 0, 0, 0, 0, 0, 0, 0, 74, 74, 74, 157, 157, 157, 74, 157, 0, 157, 157, 0, 0, 157, 0, 0, 157, 157, 157, 74, 74, 0, 74, 74, 74, 74, 74, 74, 74, 0, 74, 0, 74, 65, 65, 65, 0, 0, 74, 74, 74, 0, 74, 0, 74, 0, 0, 0, 0, 0, 0, 0, 65, 65, 0, 0, 0, 0, 0, 0, 0, 0, 74, 0, 0, 0, 0, 0, 0, 0, 0, 65, 0, 74, 74, 74, 0, 74, 0, 74, 74, 0, 0, 74, 66, 66, 66, 74, 74, 0, 0, 0, 0, 0, 67, 67, 67, 0, 0, 60, 0, 0, 0, 66, 66, 0, 0, 0, 0, 0, 65, 0, 69, 67, 67, 60, 60, 60, 76, 77, 60, 60, 66, 0, 0, 0, 0, 68, 68, 68, 0, 0, 67, 0, 0, 60, 0, 0, 0, 0, 60, 0, 0, 0, 0, 0, 68, 68, 0, 0, 60, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 66, 0, 0, 0, 68, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 60, 60, 60, 60, 60, 60, 0, 0, 0, 0, 0, 0, 27, 27, 0, 27, 0, 27, 0, 27, 0, 0, 0, 0, 0, 0, 0, 0, 68, 60, 0, 27, 0, 0, 0, 0, 0, 0, 215, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60, 0, 60, 0, 60, 60, 0, 0, 0, 0, 60, 60, 60, 236, 60, 0, 60, 60, 60, 60, 60, 60, 0, 60, 0, 60, 0, 60, 60, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60, 0, 0, 0, 0, 27, 0, 27, 60, 0, 0, 0, 0, 0, 60, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60, 0, 0, 0, 0, 0, 60, 0, 0, 0, 0, 60, 0, 60, 0, 0, 57, 0, 0, 33, 0, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60, 0, 60, 0, 0, 0, 0, 0, 0, 0, 215, 0, 215, 0, 215, 215, 215, 60, 60, 60, 215, 0, 0, 0, 60, 0, 0, 0, 60, 0, 60, 0, 0, 60, 60, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60, 59, 0, 60, 60, 60, 60, 0, 0, 0, 60, 60, 60, 0, 0, 0, 0, 72, 73, 74, 0, 0, 78, 91, 0, 0, 27, 0, 0, 0, 0, 0, 215, 0, 345, 0, 346, 91, 60, 0, 60, 0, 74, 60, 0, 0, 0, 0, 60, 0, 0, 0, 153, 0, 0, 0, 0, 0, 60, 0, 27, 27, 27, 27, 27, 27, 27, 27, 27, 0, 27, 27, 27, 27, 0, 27, 27, 27, 173, 174, 175, 176, 177, 178, 0, 0, 0, 0, 0, 0, 0, 27, 27, 27, 27, 0, 0, 0, 0, 0, 0, 27, 27, 27, 27, 27, 27, 91, 27, 27, 27, 27, 27, 0, 27, 0, 213, 0, 27, 27, 27, 0, 0, 0, 0, 0, 0, 0, 91, 0, 91, 0, 91, 91, 0, 0, 0, 0, 233, 91, 91, 0, 91, 0, 91, 91, 91, 91, 91, 91, 0, 91, 0, 91, 0, 91, 238, 259, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 91, 0, 0, 0, 0, 0, 0, 0, 91, 0, 0, 260, 15, 16, 91, 17, 0, 0, 0, 0, 0, 0, 19, 20, 0, 21, 262, 22, 0, 0, 0, 0, 91, 0, 0, 0, 0, 91, 0, 91, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 62, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 91, 58, 91, 0, 0, 36, 37, 38, 0, 0, 213, 0, 213, 0, 213, 213, 213, 303, 304, 305, 213, 0, 0, 0, 91, 0, 62, 0, 91, 62, 91, 0, 62, 91, 91, 0, 0, 0, 0, 39, 0, 0, 0, 62, 62, 53, 0, 262, 62, 0, 262, 262, 262, 262, 0, 0, 0, 262, 91, 91, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 213, 0, 0, 0, 0, 0, 91, 0, 91, 144, 146, 91, 0, 0, 0, 0, 262, 148, 149, 144, 144, 0, 0, 155, 156, 0, 91, 0, 0, 62, 0, 0, 0, 158, 0, 0, 160, 161, 162, 163, 164, 165, 0, 167, 0, 169, 171, 0, 0, 0, 0, 0, 0, 0, 0, 39, 184, 0, 176, 144, 0, 176, 0, 176, 176, 0, 176, 192, 176, 0, 0, 0, 0, 0, 198, 0, 0, 0, 0, 0, 176, 0, 0, 0, 176, 0, 216, 0, 0, 0, 0, 0, 221, 0, 223, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 176, 0, 0, 249, 0, 0, 175, 0, 251, 175, 175, 175, 175, 175, 175, 0, 175, 53, 0, 0, 0, 0, 62, 62, 62, 0, 0, 175, 175, 0, 0, 0, 175, 0, 273, 0, 0, 277, 279, 0, 0, 62, 62, 282, 0, 283, 0, 0, 0, 0, 0, 0, 144, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 0, 0, 26, 0, 26, 0, 26, 307, 308, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 53, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 334, 0, 336, 0, 0, 0, 339, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 176, 0, 0, 0, 0, 0, 354, 0, 0, 0, 26, 0, 26, 176, 176, 0, 176, 176, 176, 176, 176, 176, 0, 176, 176, 0, 176, 0, 0, 0, 0, 0, 0, 176, 176, 0, 176, 0, 176, 107, 0, 0, 107, 0, 107, 0, 107, 176, 0, 0, 0, 0, 0, 0, 0, 0, 176, 0, 107, 0, 0, 0, 0, 0, 176, 176, 176, 176, 176, 176, 0, 176, 0, 176, 176, 0, 0, 176, 0, 0, 176, 176, 176, 176, 0, 0, 0, 0, 0, 175, 0, 0, 0, 175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 175, 175, 175, 0, 0, 175, 0, 0, 0, 175, 175, 175, 0, 175, 0, 0, 107, 0, 107, 0, 175, 175, 0, 175, 0, 175, 0, 0, 13, 0, 0, 33, 0, 34, 0, 14, 0, 26, 0, 0, 0, 0, 0, 175, 0, 0, 0, 109, 0, 0, 0, 175, 175, 175, 175, 175, 175, 0, 175, 0, 175, 175, 0, 0, 175, 0, 0, 175, 0, 0, 175, 26, 26, 26, 26, 26, 26, 26, 26, 26, 0, 26, 26, 26, 26, 0, 26, 26, 26, 119, 0, 0, 119, 0, 119, 0, 119, 0, 0, 0, 0, 0, 26, 26, 26, 26, 0, 0, 119, 0, 0, 12, 26, 26, 26, 26, 26, 26, 0, 26, 26, 26, 26, 26, 0, 26, 0, 0, 0, 26, 26, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 107, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 33, 0, 34, 0, 14, 0, 0, 119, 0, 119, 0, 0, 0, 0, 0, 0, 109, 0, 107, 107, 107, 107, 107, 107, 107, 107, 107, 0, 107, 107, 107, 107, 0, 107, 107, 107, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 107, 107, 107, 107, 0, 0, 0, 0, 0, 0, 107, 107, 107, 107, 107, 107, 0, 107, 107, 107, 107, 107, 0, 107, 0, 0, 0, 107, 107, 107, 107, 12, 0, 202, 0, 0, 0, 108, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 16, 110, 17, 111, 112, 113, 114, 115, 0, 19, 20, 116, 21, 0, 22, 117, 118, 0, 0, 0, 0, 0, 0, 0, 0, 0, 119, 0, 0, 0, 119, 120, 23, 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 121, 31, 32, 122, 0, 35, 0, 0, 0, 36, 37, 38, 119, 119, 119, 119, 119, 119, 119, 119, 119, 0, 119, 119, 119, 119, 0, 119, 119, 119, 13, 0, 0, 33, 0, 34, 0, 14, 0, 0, 0, 0, 0, 119, 119, 119, 119, 0, 0, 109, 0, 107, 0, 119, 119, 119, 119, 119, 119, 0, 119, 119, 119, 119, 119, 0, 119, 0, 0, 0, 119, 119, 119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 16, 110, 17, 111, 112, 113, 114, 115, 0, 19, 20, 116, 21, 0, 22, 117, 118, 13, 0, 0, 33, 0, 34, 0, 14, 0, 0, 12, 0, 218, 119, 120, 23, 0, 0, 0, 109, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 121, 31, 32, 122, 0, 35, 0, 0, 0, 36, 37, 38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 33, 0, 34, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 109, 0, 0, 12, 0, 219, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 33, 0, 34, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 109, 0, 107, 12, 0, 241, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 16, 110, 17, 111, 112, 113, 114, 115, 0, 19, 20, 116, 21, 0, 22, 117, 118, 0, 13, 0, 0, 33, 0, 34, 0, 14, 0, 12, 0, 256, 119, 120, 23, 0, 0, 0, 0, 109, 107, 0, 24, 25, 26, 27, 28, 29, 0, 30, 121, 31, 32, 122, 0, 35, 0, 0, 0, 36, 37, 38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 16, 110, 17, 111, 112, 113, 114, 115, 0, 19, 20, 116, 21, 0, 22, 117, 118, 0, 0, 0, 0, 0, 13, 0, 0, 33, 107, 34, 12, 14, 119, 120, 23, 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 121, 31, 32, 122, 0, 35, 0, 0, 0, 36, 37, 38, 15, 16, 110, 17, 111, 112, 113, 114, 115, 0, 19, 20, 116, 21, 0, 22, 117, 118, 0, 0, 0, 0, 0, 0, 0, 0, 0, 107, 0, 0, 0, 119, 120, 23, 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 121, 31, 32, 122, 0, 35, 0, 0, 0, 36, 37, 38, 15, 16, 110, 17, 111, 112, 113, 114, 115, 0, 19, 20, 116, 21, 0, 22, 117, 118, 13, 0, 0, 33, 0, 34, 0, 14, 0, 0, 0, 0, 0, 119, 120, 23, 0, 0, 0, 109, 0, 0, 107, 24, 25, 26, 27, 28, 29, 290, 30, 121, 31, 32, 122, 0, 35, 0, 0, 0, 36, 37, 38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 16, 110, 17, 111, 112, 113, 114, 115, 0, 19, 20, 116, 21, 0, 22, 117, 118, 13, 0, 0, 33, 0, 34, 0, 14, 0, 12, 0, 337, 0, 119, 120, 23, 0, 0, 0, 109, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 121, 31, 32, 122, 0, 35, 0, 0, 0, 36, 37, 38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 16, 118, 17, 0, 118, 0, 118, 0, 118, 19, 20, 0, 21, 0, 22, 0, 0, 0, 0, 0, 118, 0, 0, 12, 0, 357, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 0, 35, 0, 0, 0, 36, 37, 38, 0, 0, 13, 0, 0, 33, 0, 34, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 109, 107, 0, 118, 0, 118, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 16, 110, 17, 111, 112, 113, 114, 115, 0, 19, 20, 116, 21, 0, 22, 117, 118, 0, 111, 0, 0, 111, 0, 111, 0, 111, 0, 0, 12, 0, 119, 120, 23, 0, 0, 0, 0, 111, 0, 107, 24, 25, 26, 27, 28, 29, 0, 30, 121, 31, 32, 122, 0, 35, 0, 0, 0, 36, 37, 38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 16, 110, 17, 111, 112, 113, 114, 115, 0, 19, 20, 116, 21, 0, 22, 117, 118, 21, 0, 0, 21, 0, 21, 0, 21, 0, 118, 111, 0, 0, 119, 120, 23, 0, 0, 0, 21, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 121, 31, 32, 122, 0, 35, 0, 0, 0, 36, 37, 38, 118, 118, 118, 118, 118, 118, 118, 118, 118, 0, 118, 118, 118, 118, 0, 118, 118, 118, 186, 0, 0, 186, 0, 186, 0, 186, 0, 107, 0, 0, 0, 118, 118, 118, 0, 0, 0, 186, 0, 0, 21, 118, 118, 118, 118, 118, 118, 0, 118, 118, 118, 118, 118, 0, 118, 0, 0, 0, 118, 118, 118, 15, 16, 110, 17, 111, 112, 113, 114, 115, 0, 19, 20, 116, 21, 0, 22, 117, 118, 17, 0, 0, 17, 0, 17, 0, 17, 0, 0, 0, 0, 0, 119, 120, 23, 0, 0, 0, 17, 0, 111, 186, 24, 25, 26, 27, 28, 29, 0, 30, 121, 31, 32, 122, 0, 35, 0, 0, 0, 36, 37, 38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 111, 111, 111, 111, 111, 111, 111, 111, 111, 0, 111, 111, 111, 111, 0, 111, 111, 111, 15, 0, 0, 15, 0, 15, 0, 15, 0, 0, 17, 0, 0, 111, 111, 111, 0, 0, 0, 15, 0, 21, 0, 111, 111, 111, 111, 111, 111, 0, 111, 111, 111, 111, 111, 0, 111, 0, 0, 0, 111, 111, 111, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 21, 21, 21, 21, 21, 21, 21, 21, 0, 21, 21, 21, 21, 0, 21, 21, 21, 13, 0, 0, 13, 0, 13, 0, 13, 0, 186, 15, 0, 0, 21, 21, 21, 0, 0, 0, 13, 0, 0, 0, 21, 21, 21, 21, 21, 21, 0, 21, 21, 21, 21, 21, 0, 21, 0, 0, 0, 21, 21, 21, 186, 186, 186, 186, 186, 186, 186, 186, 186, 0, 186, 186, 186, 186, 0, 186, 186, 186, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 0, 0, 0, 186, 186, 186, 0, 0, 0, 0, 0, 0, 13, 186, 186, 186, 186, 186, 186, 0, 186, 186, 186, 186, 186, 0, 186, 0, 0, 0, 186, 186, 186, 17, 17, 17, 17, 17, 17, 17, 17, 17, 0, 17, 17, 17, 17, 0, 17, 17, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 17, 17, 0, 0, 0, 0, 0, 15, 0, 17, 17, 17, 17, 17, 17, 0, 17, 17, 17, 17, 17, 0, 17, 0, 0, 0, 17, 17, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 15, 15, 15, 15, 0, 15, 15, 15, 26, 26, 0, 26, 0, 26, 0, 26, 0, 0, 0, 0, 0, 15, 15, 15, 0, 0, 0, 0, 0, 13, 0, 15, 15, 15, 15, 15, 15, 0, 15, 15, 15, 15, 15, 0, 15, 0, 0, 0, 15, 15, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 13, 13, 13, 13, 13, 13, 13, 13, 0, 13, 13, 13, 13, 0, 13, 13, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 13, 13, 57, 0, 0, 33, 0, 34, 0, 13, 13, 13, 13, 13, 13, 0, 13, 13, 13, 13, 13, 187, 13, 0, 0, 90, 13, 13, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 57, 318, 0, 33, 137, 34, 0, 0, 0, 0, 0, 0, 79, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 57, 329, 0, 33, 137, 34, 0, 0, 0, 0, 0, 0, 79, 0, 0, 0, 0, 26, 26, 0, 26, 0, 26, 90, 0, 0, 0, 26, 26, 0, 26, 0, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 26, 26, 0, 0, 0, 0, 0, 0, 0, 26, 26, 26, 26, 26, 26, 0, 26, 0, 26, 26, 0, 0, 26, 0, 0, 0, 26, 26, 26, 0, 179, 0, 0, 0, 80, 79, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 82, 0, 83, 84, 85, 86, 87, 88, 89, 15, 16, 0, 17, 57, 331, 0, 33, 137, 34, 19, 20, 0, 21, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 90, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 80, 31, 32, 0, 0, 58, 0, 0, 0, 36, 37, 38, 0, 81, 82, 0, 83, 84, 85, 86, 87, 88, 89, 15, 16, 0, 17, 57, 138, 0, 33, 0, 34, 19, 20, 0, 21, 0, 22, 79, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 90, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 80, 31, 32, 0, 0, 58, 0, 0, 0, 36, 37, 38, 0, 81, 82, 0, 83, 84, 85, 86, 87, 88, 89, 15, 16, 0, 17, 57, 0, 0, 33, 137, 34, 19, 20, 0, 21, 0, 22, 79, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 90, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 0, 58, 0, 0, 0, 36, 37, 38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 57, 0, 0, 33, 0, 34, 0, 0, 80, 79, 0, 0, 0, 0, 0, 0, 0, 0, 237, 0, 0, 81, 82, 90, 83, 84, 85, 86, 87, 88, 89, 15, 16, 0, 17, 0, 0, 0, 0, 0, 0, 19, 20, 0, 21, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 80, 31, 32, 0, 79, 58, 0, 0, 0, 36, 37, 38, 0, 81, 82, 0, 83, 84, 85, 86, 87, 88, 89, 15, 16, 0, 17, 57, 274, 0, 33, 0, 34, 19, 20, 0, 21, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 90, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 80, 31, 32, 0, 0, 58, 0, 0, 0, 36, 37, 38, 0, 81, 82, 0, 83, 84, 85, 86, 87, 88, 89, 15, 16, 0, 17, 0, 0, 0, 0, 0, 0, 19, 20, 0, 21, 0, 22, 79, 57, 312, 0, 33, 0, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 90, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 80, 0, 58, 0, 0, 0, 36, 37, 38, 0, 0, 0, 0, 81, 82, 0, 83, 84, 85, 86, 87, 88, 89, 15, 16, 0, 17, 0, 0, 0, 0, 0, 0, 19, 20, 0, 21, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 79, 0, 0, 57, 314, 0, 33, 23, 34, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 90, 0, 58, 0, 0, 0, 36, 37, 38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 82, 0, 83, 84, 85, 86, 87, 88, 89, 15, 16, 0, 17, 0, 0, 79, 0, 0, 0, 19, 20, 0, 21, 0, 22, 0, 57, 348, 0, 33, 0, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 90, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 0, 58, 0, 0, 0, 36, 37, 38, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 82, 0, 83, 84, 85, 86, 87, 88, 89, 15, 16, 0, 17, 0, 0, 0, 0, 0, 0, 19, 20, 0, 21, 0, 22, 79, 57, 350, 0, 33, 0, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 90, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 0, 58, 0, 0, 0, 36, 37, 38, 0, 0, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 82, 0, 83, 84, 85, 86, 87, 88, 89, 15, 16, 0, 17, 0, 0, 0, 0, 0, 0, 19, 20, 0, 21, 79, 22, 57, 0, 0, 33, 0, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 0, 352, 0, 0, 0, 90, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 0, 58, 0, 0, 0, 36, 37, 38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 82, 0, 83, 84, 85, 86, 87, 88, 89, 15, 16, 0, 17, 0, 79, 0, 0, 0, 0, 19, 20, 0, 21, 0, 22, 0, 57, 361, 0, 33, 0, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 90, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 0, 58, 0, 0, 0, 36, 37, 38, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 82, 0, 83, 84, 85, 86, 87, 88, 89, 15, 16, 0, 17, 57, 0, 0, 33, 0, 34, 19, 20, 0, 21, 0, 22, 79, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 90, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 0, 58, 0, 0, 0, 36, 37, 38, 0, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 82, 0, 83, 84, 85, 86, 87, 88, 89, 15, 16, 79, 17, 93, 0, 0, 93, 0, 93, 19, 20, 0, 21, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 93, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 0, 58, 0, 0, 0, 36, 37, 38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 82, 93, 83, 84, 85, 86, 87, 88, 89, 15, 16, 0, 17, 58, 58, 0, 0, 58, 0, 19, 20, 0, 21, 0, 22, 0, 0, 0, 0, 0, 0, 58, 58, 0, 0, 0, 58, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 80, 31, 32, 0, 0, 58, 0, 0, 0, 36, 37, 38, 0, 81, 82, 0, 83, 84, 85, 86, 87, 88, 89, 15, 16, 0, 17, 0, 0, 0, 0, 56, 56, 19, 20, 56, 21, 0, 22, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 56, 56, 0, 0, 0, 56, 0, 23, 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 0, 58, 0, 0, 0, 36, 37, 38, 0, 0, 93, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 93, 93, 0, 93, 93, 93, 93, 93, 93, 93, 93, 93, 0, 93, 0, 0, 56, 0, 0, 57, 93, 93, 33, 93, 34, 93, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 93, 0, 0, 0, 0, 0, 0, 0, 93, 93, 93, 93, 93, 93, 0, 93, 0, 93, 93, 0, 0, 93, 0, 0, 0, 93, 93, 93, 0, 0, 0, 0, 0, 0, 0, 0, 58, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 58, 58, 58, 0, 0, 58, 0, 79, 0, 58, 58, 58, 0, 58, 57, 0, 0, 33, 0, 34, 58, 58, 0, 58, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 58, 0, 0, 0, 0, 0, 0, 0, 58, 58, 58, 58, 58, 58, 0, 58, 56, 58, 58, 0, 56, 58, 0, 0, 0, 58, 58, 58, 0, 0, 0, 0, 0, 56, 56, 56, 0, 0, 56, 0, 0, 0, 56, 56, 56, 0, 56, 57, 0, 0, 33, 0, 34, 56, 56, 79, 56, 0, 56, 0, 0, 0, 0, 0, 0, 328, 0, 0, 0, 0, 261, 0, 0, 0, 0, 0, 56, 0, 0, 0, 0, 0, 0, 0, 56, 56, 56, 56, 56, 56, 0, 56, 0, 56, 56, 0, 0, 56, 0, 0, 0, 56, 56, 56, 0, 0, 0, 80, 0, 0, 13, 0, 0, 33, 0, 34, 0, 14, 0, 0, 81, 0, 0, 83, 84, 85, 86, 87, 88, 89, 15, 16, 0, 17, 0, 0, 0, 0, 0, 0, 19, 20, 0, 21, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 57, 0, 58, 33, 0, 34, 36, 37, 38, 0, 0, 12, 0, 0, 0, 0, 80, 0, 0, 0, 0, 0, 0, 261, 13, 0, 0, 33, 0, 34, 0, 14, 83, 84, 85, 86, 87, 88, 89, 15, 16, 0, 17, 187, 0, 0, 0, 0, 0, 19, 20, 0, 21, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 259, 58, 0, 0, 0, 36, 37, 38, 0, 0, 0, 0, 13, 81, 82, 33, 0, 34, 0, 14, 0, 0, 260, 15, 16, 0, 17, 0, 0, 0, 0, 0, 0, 19, 20, 0, 21, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 10, 11, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 0, 58, 0, 0, 0, 36, 37, 38, 0, 0, 15, 16, 0, 17, 0, 0, 0, 0, 0, 18, 19, 20, 0, 21, 13, 22, 0, 33, 0, 34, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 259, 31, 32, 0, 0, 35, 0, 0, 0, 36, 37, 38, 0, 81, 82, 0, 0, 0, 0, 0, 179, 0, 260, 15, 16, 0, 17, 0, 0, 0, 0, 0, 13, 19, 20, 33, 21, 34, 22, 14, 0, 0, 0, 0, 0, 0, 0, 15, 16, 0, 17, 0, 0, 0, 0, 0, 23, 19, 20, 0, 21, 0, 22, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 0, 58, 0, 0, 23, 36, 37, 38, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 168, 35, 0, 0, 0, 36, 37, 38, 295, 0, 0, 33, 0, 34, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 16, 0, 17, 0, 0, 0, 0, 0, 0, 19, 20, 0, 21, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 0, 35, 0, 0, 0, 36, 37, 38, 170, 0, 295, 0, 0, 33, 0, 34, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 295, 15, 16, 33, 17, 34, 0, 14, 0, 0, 0, 19, 20, 0, 21, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 220, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 0, 35, 0, 0, 0, 36, 37, 38, 0, 0, 0, 0, 0, 57, 15, 16, 33, 17, 34, 0, 14, 0, 0, 0, 19, 20, 0, 21, 0, 22, 0, 0, 0, 0, 0, 0, 0, 209, 0, 0, 33, 0, 34, 0, 14, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 168, 31, 32, 0, 0, 35, 0, 0, 0, 36, 37, 38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 295, 15, 16, 33, 17, 34, 0, 14, 0, 0, 0, 19, 20, 0, 21, 0, 22, 0, 0, 0, 0, 0, 0, 0, 26, 0, 0, 26, 0, 26, 0, 26, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 0, 35, 170, 0, 0, 36, 37, 38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 57, 0, 0, 33, 220, 34, 0, 15, 16, 0, 17, 0, 0, 0, 0, 0, 0, 19, 20, 0, 21, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 15, 16, 0, 17, 0, 0, 0, 0, 0, 23, 19, 20, 0, 21, 0, 22, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 0, 35, 0, 0, 23, 36, 37, 38, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 0, 35, 0, 0, 0, 36, 37, 38, 0, 0, 0, 0, 0, 0, 15, 16, 0, 17, 0, 0, 0, 0, 0, 0, 19, 20, 0, 21, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 15, 16, 0, 17, 0, 0, 0, 0, 0, 23, 19, 20, 0, 21, 0, 22, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 0, 35, 0, 0, 23, 36, 37, 38, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 15, 16, 35, 17, 0, 0, 36, 37, 38, 0, 19, 20, 0, 21, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 26, 26, 0, 26, 0, 0, 0, 0, 0, 23, 26, 26, 0, 26, 0, 26, 0, 24, 25, 26, 27, 28, 29, 0, 30, 0, 31, 32, 0, 0, 35, 0, 0, 26, 36, 37, 38, 0, 0, 0, 0, 26, 26, 26, 26, 26, 26, 0, 26, 0, 26, 26, 15, 16, 26, 17, 0, 0, 26, 26, 26, 0, 19, 20, 0, 21, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 27, 28, 29, 0, 0, 0, 31, 32, 0, 0, 58, 0, 0, 0, 36, 37, 38, }; const short yycheck[] = { 37, 0, 37, 109, 59, 42, 43, 42, 45, 41, 47, 91, 47, 100, 7, 80, 124, 10, 11, 41, 59, 44, 109, 51, 52, 35, 111, 59, 194, 114, 59, 44, 117, 118, 209, 59, 121, 59, 37, 124, 263, 40, 41, 42, 43, 44, 45, 41, 47, 42, 314, 41, 44, 263, 59, 66, 67, 123, 0, 58, 59, 338, 314, 41, 63, 59, 44, 0, 133, 59, 93, 41, 40, 101, 41, 41, 331, 210, 44, 334, 290, 187, 359, 40, 348, 349, 249, 41, 251, 59, 44, 0, 59, 40, 93, 106, 348, 349, 0, 41, 187, 93, 44, 131, 40, 47, 210, 192, 331, 102, 123, 44, 290, 198, 47, 93, 58, 59, 348, 349, 295, 331, 132, 333, 123, 124, 59, 93, 37, 40, 40, 91, 41, 42, 43, 44, 45, 41, 47, 41, 44, 41, 44, 41, 44, 47, 44, 40, 40, 58, 59, 93, 336, 331, 63, 333, 58, 59, 337, 41, 40, 324, 44, 296, 229, 230, 299, 300, 301, 302, 287, 347, 338, 306, 282, 41, 290, 285, 44, 41, 286, 123, 44, 41, 93, 0, 44, 338, 351, 263, 123, 93, 296, 359, 259, 299, 300, 301, 302, 286, 363, 256, 306, 41, 41, 40, 44, 44, 263, 289, 41, 344, 40, 44, 123, 124, 322, 331, 332, 333, 41, 123, 37, 44, 263, 40, 41, 42, 43, 44, 45, 263, 47, 244, 263, 322, 350, 58, 59, 263, 344, 263, 63, 58, 59, 40, 352, 240, 63, 259, 47, 261, 317, 263, 264, 265, 284, 41, 263, 269, 44, 263, 261, 262, 263, 352, 263, 331, 267, 263, 289, 91, 324, 263, 58, 59, 361, 44, 93, 63, 41, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 263, 293, 59, 263, 41, 59, 263, 299, 300, 301, 124, 303, 333, 305, 263, 123, 124, 347, 40, 347, 263, 43, 314, 45, 310, 328, 261, 262, 263, 41, 41, 323, 342, 290, 289, 261, 262, 263, 40, 331, 332, 333, 334, 335, 336, 41, 338, 124, 340, 341, 287, 41, 344, 290, 41, 347, 348, 349, 350, 261, 262, 263, 41, 0, 299, 267, 261, 262, 263, 0, 124, 59, 59, 299, 331, 332, 333, 123, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 41, 293, 181, 350, 331, 332, 333, 299, 300, 301, 44, 303, 102, 305, 299, -1, -1, -1, -1, -1, -1, -1, 314, 350, -1, 40, 41, -1, 43, -1, 45, 323, 47, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, 124, 340, 341, -1, -1, 344, -1, -1, 347, 348, 349, 350, 263, -1, 41, -1, 261, 262, 263, -1, -1, -1, 267, -1, -1, -1, -1, -1, -1, 280, 281, 282, 59, -1, 285, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, 263, -1, -1, 53, 54, 299, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, 280, 281, 282, 314, 0, 285, 263, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 282, -1, 338, 285, 340, 341, 124, -1, 344, -1, -1, 347, 348, 349, 350, -1, 37, 267, -1, 40, 41, 42, 43, 44, 45, -1, 47, -1, -1, -1, 280, -1, -1, 338, -1, -1, -1, 58, 59, 289, 290, 291, 63, 293, -1, -1, -1, -1, 263, -1, 300, 301, -1, 303, -1, 305, 148, 149, -1, 41, -1, -1, 44, 155, 156, -1, -1, 282, -1, 0, 285, -1, 93, 323, -1, -1, 58, 59, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, 123, 124, -1, -1, 37, -1, -1, 40, 41, 42, 43, 44, 45, -1, 47, -1, -1, 211, -1, -1, -1, -1, 216, 217, -1, 58, 59, 259, -1, 261, 63, 263, 264, 265, 228, 290, 291, 269, 293, -1, 124, -1, -1, 263, -1, 300, 301, -1, 303, -1, 305, -1, -1, 41, -1, -1, 44, -1, -1, -1, 93, 0, 282, -1, 258, 285, -1, -1, 323, -1, 58, 59, -1, -1, -1, 63, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, 281, 282, 344, 123, 124, -1, 348, 349, 350, 328, -1, 37, -1, -1, 40, 41, 42, 43, 44, 45, -1, 47, -1, -1, -1, -1, 307, 308, -1, -1, -1, -1, 58, 59, -1, -1, -1, 63, -1, -1, -1, -1, -1, -1, -1, -1, 327, -1, -1, 124, -1, 261, 262, 263, -1, -1, -1, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, 93, -1, -1, 280, 281, 282, 283, 284, 285, 286, -1, 288, 289, 290, 291, -1, 293, -1, -1, -1, -1, -1, 299, 300, 301, 263, 303, -1, 305, -1, 123, 124, 41, -1, -1, 44, -1, 314, -1, -1, -1, -1, -1, -1, 282, -1, 323, 285, -1, 58, 59, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, 347, 348, 349, 350, 261, 262, 263, -1, -1, 0, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, -1, -1, -1, -1, 299, 300, 301, 124, 303, -1, 305, -1, 41, 263, -1, 44, -1, -1, 47, 314, -1, 40, -1, -1, 43, -1, 45, -1, 323, 58, 59, 281, 282, -1, 63, 285, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, 261, 262, 263, -1, 93, -1, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, 123, 89, -1, -1, 299, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, -1, 314, 0, -1, -1, -1, 112, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, 263, 348, 349, 350, -1, 37, -1, -1, 40, 41, 42, 43, 44, 45, -1, 47, 0, -1, -1, 282, -1, 41, 285, -1, 44, -1, 58, 59, -1, -1, -1, 63, -1, -1, -1, -1, -1, -1, -1, 59, -1, -1, -1, -1, 41, -1, -1, 44, -1, -1, -1, -1, -1, -1, 194, -1, -1, 41, 0, -1, 44, 93, 59, 47, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 58, 59, -1, -1, -1, 63, -1, -1, 222, -1, -1, -1, 261, 262, 263, -1, -1, 123, 124, -1, 234, 37, -1, -1, 40, 41, 42, 43, 44, 45, 124, 47, 281, -1, -1, 93, -1, -1, -1, -1, -1, -1, 58, 59, -1, -1, 260, 63, -1, -1, 299, 290, 291, 124, 293, -1, -1, -1, -1, -1, -1, 300, 301, -1, 303, 123, 305, -1, -1, -1, -1, -1, -1, 287, -1, 314, -1, 93, 0, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, 123, 124, 348, 349, 350, 326, -1, -1, 37, -1, -1, 40, 41, 42, 43, 44, 45, -1, 47, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 58, 59, -1, -1, -1, 63, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 261, 262, 263, -1, -1, -1, 267, -1, -1, -1, -1, -1, -1, -1, 263, -1, 93, -1, -1, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, 282, -1, -1, 285, 263, 299, 300, 301, -1, 303, -1, 305, -1, 123, 124, 261, 262, 263, -1, -1, 314, -1, -1, 282, -1, -1, 285, -1, -1, 323, -1, -1, -1, -1, 280, 281, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, 299, 348, 349, 350, 261, 262, 263, 126, -1, 128, 267, -1, -1, -1, -1, -1, 135, 136, -1, -1, -1, -1, -1, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, -1, 338, -1, -1, 299, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, -1, 314, -1, 40, -1, -1, 43, 182, 45, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, 205, 344, 207, 208, -1, 348, 349, 350, -1, -1, -1, -1, 261, 262, 263, -1, -1, -1, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, -1, -1, 253, -1, 299, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, -1, 314, 0, -1, -1, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, 37, -1, -1, 40, 41, 42, 43, 44, 45, -1, 47, 0, 320, -1, -1, -1, -1, -1, -1, -1, -1, 58, 59, -1, -1, -1, 63, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 347, -1, -1, -1, -1, -1, 353, -1, -1, -1, -1, -1, -1, 41, 0, 362, 44, 93, -1, 47, 367, -1, -1, -1, -1, -1, -1, -1, -1, -1, 58, 59, -1, -1, -1, 63, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 123, 124, -1, -1, 37, -1, -1, 40, 41, 42, 43, 44, 45, -1, 47, -1, -1, -1, 93, -1, -1, -1, -1, -1, -1, 58, 59, -1, -1, -1, 63, -1, -1, -1, 290, 291, -1, 293, -1, -1, -1, -1, -1, -1, 300, 301, -1, 303, 123, 305, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 93, 0, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, 123, 124, 348, 349, 350, -1, -1, -1, 37, -1, -1, 40, 41, 42, 43, 44, 45, -1, 47, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 58, 59, -1, -1, -1, 63, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 261, 262, 263, -1, -1, -1, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, 93, -1, -1, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, -1, -1, -1, -1, 299, 300, 301, -1, 303, -1, 305, -1, 123, 124, 261, 262, 263, -1, 40, 314, -1, 43, -1, 45, -1, 47, -1, -1, 323, -1, -1, -1, -1, 280, 281, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, 299, 348, 349, 350, 261, 262, 263, -1, -1, -1, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, -1, 338, -1, -1, 299, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, -1, 314, -1, 40, 41, -1, 43, 44, 45, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, 63, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, 261, 262, 263, -1, -1, -1, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, -1, -1, -1, -1, 299, 300, 301, -1, 303, -1, 305, 124, -1, -1, -1, -1, 0, -1, -1, 314, -1, -1, -1, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, 256, -1, 347, 37, -1, 350, 40, 41, 42, 43, 44, 45, -1, 47, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 58, 59, -1, -1, -1, 63, -1, -1, -1, -1, 290, 291, -1, 293, -1, 295, -1, -1, -1, -1, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, 0, -1, -1, 93, -1, -1, -1, -1, -1, 321, 322, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, 123, 124, -1, 348, 349, 350, -1, 40, 41, -1, 43, 44, 45, -1, -1, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, 58, 59, -1, 280, 281, 63, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, -1, -1, -1, -1, -1, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, 93, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, 123, 124, 344, -1, -1, -1, 348, 349, 350, -1, -1, 40, 41, -1, 43, 44, 45, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 58, 59, -1, -1, -1, 63, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 261, 262, 263, -1, -1, -1, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, 93, -1, -1, 280, 281, -1, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, -1, -1, -1, -1, 299, 300, 301, -1, 303, -1, 305, -1, 123, 124, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, 347, -1, -1, 350, 261, 262, 263, -1, -1, 0, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, -1, -1, -1, -1, 299, 300, 301, -1, 303, -1, 305, 40, 41, -1, -1, 44, -1, -1, -1, 314, -1, -1, -1, -1, -1, -1, -1, -1, 323, 58, 59, -1, -1, -1, 63, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, 261, 262, 263, -1, 93, 0, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, 123, 124, -1, -1, 299, 300, 301, -1, 303, -1, 305, 40, 41, -1, -1, 44, -1, -1, 0, 314, -1, -1, -1, -1, -1, -1, -1, -1, 323, 58, 59, -1, -1, -1, 63, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, 41, -1, 0, 44, -1, 93, 47, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 58, 59, -1, -1, -1, 63, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 123, 124, -1, -1, -1, -1, -1, 40, 41, -1, -1, 44, -1, -1, -1, -1, -1, 93, -1, -1, -1, -1, -1, -1, -1, 58, 59, -1, -1, -1, 63, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 261, 262, 263, 123, -1, -1, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, 93, -1, -1, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, -1, -1, -1, -1, 299, 300, 301, -1, 303, -1, 305, -1, 123, 124, -1, -1, -1, -1, -1, 314, -1, -1, -1, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, 261, 262, 263, -1, -1, -1, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, -1, -1, -1, -1, 299, 300, 301, -1, 303, -1, 305, -1, -1, 261, 262, 263, -1, -1, -1, 314, -1, -1, -1, -1, -1, -1, -1, -1, 323, -1, -1, -1, 280, 281, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, 299, -1, 348, 349, 350, 261, 262, 263, -1, -1, 0, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, 338, -1, -1, -1, 299, 300, 301, -1, 303, -1, 305, 40, 41, -1, -1, 44, -1, -1, -1, 314, -1, -1, -1, -1, -1, -1, -1, -1, 323, 58, 59, -1, -1, -1, 63, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, -1, 0, -1, -1, 93, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 123, 124, -1, -1, -1, -1, -1, 40, 41, -1, -1, 44, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 58, 59, -1, -1, -1, 63, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 93, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 123, 124, -1, -1, -1, -1, -1, -1, -1, -1, -1, 40, 41, -1, -1, 44, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 58, 59, -1, -1, -1, 63, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 261, 262, 263, -1, -1, -1, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, 93, -1, -1, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, -1, -1, -1, -1, 299, 300, 301, -1, 303, -1, 305, -1, 123, 124, -1, -1, -1, 40, 41, 314, 43, -1, 45, -1, 47, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, 261, 262, 263, -1, -1, 0, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 280, 281, -1, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, -1, -1, -1, -1, 299, 300, 301, -1, 303, -1, 305, 40, -1, -1, 43, -1, 45, -1, 47, -1, -1, -1, -1, -1, -1, -1, -1, -1, 323, -1, 59, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, 261, 262, 263, -1, -1, 0, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 280, 281, -1, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, -1, -1, -1, -1, 299, 300, 301, -1, 303, -1, 305, 40, -1, -1, 43, -1, 45, -1, 47, -1, -1, -1, -1, 0, -1, -1, -1, -1, 323, -1, 59, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, -1, -1, -1, 40, -1, -1, 43, -1, 45, -1, 47, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1, 290, 291, -1, 293, -1, -1, -1, -1, -1, -1, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 40, 323, -1, 43, -1, 45, -1, 47, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, 261, 262, 263, 348, 349, 350, -1, -1, 123, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 290, 291, -1, 293, -1, -1, -1, -1, -1, 299, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 123, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, 261, 262, 263, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 290, 291, -1, 293, -1, -1, -1, -1, -1, 299, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, 261, 262, -1, -1, -1, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, 290, 291, 344, 293, -1, -1, 348, 349, 350, 299, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, 261, 262, -1, -1, -1, -1, 0, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, 290, 291, 344, 293, -1, -1, 348, 349, 350, 299, 300, 301, -1, 303, 37, 305, -1, 40, 41, 42, 43, 44, 45, -1, 47, -1, -1, -1, -1, -1, -1, -1, -1, 323, -1, 58, 59, -1, -1, -1, 63, 331, 332, 333, 334, 335, 336, 0, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, -1, -1, -1, -1, -1, 93, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, 40, 41, -1, 43, 44, 45, -1, 47, -1, -1, -1, -1, -1, 123, 124, -1, -1, -1, 58, 59, -1, -1, -1, 63, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 41, -1, -1, 44, -1, -1, 47, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, 58, 59, 93, 0, -1, 63, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 123, 124, 41, 93, 0, 44, -1, -1, 47, -1, -1, -1, 41, -1, -1, 44, -1, -1, 47, 58, 59, -1, -1, -1, 63, -1, -1, -1, -1, 58, 59, -1, -1, 123, 63, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 41, -1, -1, 44, -1, -1, 47, -1, -1, 93, -1, -1, -1, -1, -1, -1, -1, 58, 59, 93, -1, -1, 63, 261, 262, 263, -1, -1, -1, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, 123, -1, -1, 280, 281, 282, 283, 284, 285, 286, 123, 288, 289, 93, 291, -1, 293, -1, -1, -1, -1, -1, 299, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, -1, 314, -1, -1, -1, -1, -1, 123, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 261, 262, 263, 334, 335, 336, 267, 338, -1, 340, 341, -1, -1, 344, -1, -1, 347, 348, 349, 280, 281, -1, 283, 284, 285, 286, 287, 288, 289, -1, 291, -1, 293, 261, 262, 263, -1, -1, 299, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, 280, 281, -1, -1, -1, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, -1, 299, -1, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, 261, 262, 263, 348, 349, -1, -1, -1, -1, -1, 261, 262, 263, -1, -1, 17, -1, -1, -1, 280, 281, -1, -1, -1, -1, -1, 338, -1, 30, 280, 281, 33, 34, 35, 36, 37, 38, 39, 299, -1, -1, -1, -1, 261, 262, 263, -1, -1, 299, -1, -1, 53, -1, -1, -1, -1, 58, -1, -1, -1, -1, -1, 280, 281, -1, -1, 68, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 338, -1, -1, -1, 299, -1, -1, -1, -1, -1, 338, -1, -1, -1, -1, 94, 95, 96, 97, 98, 99, -1, -1, -1, -1, -1, -1, 40, 41, -1, 43, -1, 45, -1, 47, -1, -1, -1, -1, -1, -1, -1, -1, 338, 123, -1, 59, -1, -1, -1, -1, -1, -1, 132, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 144, -1, 146, -1, 148, 149, -1, -1, -1, -1, 154, 155, 156, 157, 158, -1, 160, 161, 162, 163, 164, 165, -1, 167, -1, 169, -1, 171, 172, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 184, -1, -1, -1, -1, 123, -1, 125, 192, -1, -1, -1, -1, -1, 198, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 210, -1, -1, -1, -1, -1, 216, -1, -1, -1, -1, 221, -1, 223, -1, -1, 40, -1, -1, 43, -1, 45, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 249, -1, 251, -1, -1, -1, -1, -1, -1, -1, 259, -1, 261, -1, 263, 264, 265, 266, 267, 268, 269, -1, -1, -1, 273, -1, -1, -1, 277, -1, 279, -1, -1, 282, 283, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 296, 17, -1, 299, 300, 301, 302, -1, -1, -1, 306, 307, 308, -1, -1, -1, -1, 33, 34, 35, -1, -1, 38, 39, -1, -1, 256, -1, -1, -1, -1, -1, 328, -1, 330, -1, 332, 53, 334, -1, 336, -1, 58, 339, -1, -1, -1, -1, 344, -1, -1, -1, 68, -1, -1, -1, -1, -1, 354, -1, 290, 291, 292, 293, 294, 295, 296, 297, 298, -1, 300, 301, 302, 303, -1, 305, 306, 307, 94, 95, 96, 97, 98, 99, -1, -1, -1, -1, -1, -1, -1, 321, 322, 323, 324, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, 123, 338, 339, 340, 341, 342, -1, 344, -1, 132, -1, 348, 349, 350, -1, -1, -1, -1, -1, -1, -1, 144, -1, 146, -1, 148, 149, -1, -1, -1, -1, 154, 155, 156, -1, 158, -1, 160, 161, 162, 163, 164, 165, -1, 167, -1, 169, -1, 171, 172, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 184, -1, -1, -1, -1, -1, -1, -1, 192, -1, -1, 289, 290, 291, 198, 293, -1, -1, -1, -1, -1, -1, 300, 301, -1, 303, 210, 305, -1, -1, -1, -1, 216, -1, -1, -1, -1, 221, -1, 223, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, 0, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, 249, 344, 251, -1, -1, 348, 349, 350, -1, -1, 259, -1, 261, -1, 263, 264, 265, 266, 267, 268, 269, -1, -1, -1, 273, -1, 41, -1, 277, 44, 279, -1, 47, 282, 283, -1, -1, -1, -1, 7, -1, -1, -1, 58, 59, 13, -1, 296, 63, -1, 299, 300, 301, 302, -1, -1, -1, 306, 307, 308, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 93, 328, -1, -1, -1, -1, -1, 334, -1, 336, 56, 57, 339, -1, -1, -1, -1, 344, 64, 65, 66, 67, -1, -1, 70, 71, -1, 354, -1, -1, 123, -1, -1, -1, 80, -1, -1, 83, 84, 85, 86, 87, 88, -1, 90, -1, 92, 93, -1, -1, -1, -1, -1, -1, -1, -1, 102, 103, -1, 37, 106, -1, 40, -1, 42, 43, -1, 45, 114, 47, -1, -1, -1, -1, -1, 121, -1, -1, -1, -1, -1, 59, -1, -1, -1, 63, -1, 133, -1, -1, -1, -1, -1, 139, -1, 141, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 181, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 124, -1, -1, 195, -1, -1, 37, -1, 200, 40, 41, 42, 43, 44, 45, -1, 47, 209, -1, -1, -1, -1, 261, 262, 263, -1, -1, 58, 59, -1, -1, -1, 63, -1, 226, -1, -1, 229, 230, -1, -1, 280, 281, 235, -1, 237, -1, -1, -1, -1, -1, -1, 244, -1, -1, -1, -1, -1, -1, -1, 299, -1, -1, -1, -1, -1, -1, -1, -1, -1, 40, -1, -1, 43, -1, 45, -1, 47, 270, 271, -1, -1, -1, -1, -1, -1, -1, -1, -1, 59, -1, -1, -1, 124, -1, -1, -1, -1, -1, -1, -1, -1, -1, 295, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 317, -1, 319, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 267, -1, -1, -1, -1, -1, 341, -1, -1, -1, 123, -1, 125, 280, 281, -1, 283, 284, 285, 286, 287, 288, -1, 290, 291, -1, 293, -1, -1, -1, -1, -1, -1, 300, 301, -1, 303, -1, 305, 40, -1, -1, 43, -1, 45, -1, 47, 314, -1, -1, -1, -1, -1, -1, -1, -1, 323, -1, 59, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, 347, 348, 349, 350, -1, -1, -1, -1, -1, 263, -1, -1, -1, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 280, 281, 282, -1, -1, 285, -1, -1, -1, 289, 290, 291, -1, 293, -1, -1, 123, -1, 125, -1, 300, 301, -1, 303, -1, 305, -1, -1, 40, -1, -1, 43, -1, 45, -1, 47, -1, 256, -1, -1, -1, -1, -1, 323, -1, -1, -1, 59, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, 347, -1, -1, 350, 290, 291, 292, 293, 294, 295, 296, 297, 298, -1, 300, 301, 302, 303, -1, 305, 306, 307, 40, -1, -1, 43, -1, 45, -1, 47, -1, -1, -1, -1, -1, 321, 322, 323, 324, -1, -1, 59, -1, -1, 123, 331, 332, 333, 334, 335, 336, -1, 338, 339, 340, 341, 342, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 256, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 40, -1, -1, 43, -1, 45, -1, 47, -1, -1, 123, -1, 125, -1, -1, -1, -1, -1, -1, 59, -1, 290, 291, 292, 293, 294, 295, 296, 297, 298, -1, 300, 301, 302, 303, -1, 305, 306, 307, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 321, 322, 323, 324, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, 339, 340, 341, 342, -1, 344, -1, -1, -1, 348, 349, 350, 256, 123, -1, 125, -1, -1, -1, 263, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 290, 291, 292, 293, 294, 295, 296, 297, 298, -1, 300, 301, 302, 303, -1, 305, 306, 307, -1, -1, -1, -1, -1, -1, -1, -1, -1, 256, -1, -1, -1, 321, 322, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, 339, 340, 341, 342, -1, 344, -1, -1, -1, 348, 349, 350, 290, 291, 292, 293, 294, 295, 296, 297, 298, -1, 300, 301, 302, 303, -1, 305, 306, 307, 40, -1, -1, 43, -1, 45, -1, 47, -1, -1, -1, -1, -1, 321, 322, 323, 324, -1, -1, 59, -1, 256, -1, 331, 332, 333, 334, 335, 336, -1, 338, 339, 340, 341, 342, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 290, 291, 292, 293, 294, 295, 296, 297, 298, -1, 300, 301, 302, 303, -1, 305, 306, 307, 40, -1, -1, 43, -1, 45, -1, 47, -1, -1, 123, -1, 125, 321, 322, 323, -1, -1, -1, 59, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, 339, 340, 341, 342, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 40, -1, -1, 43, -1, 45, -1, 47, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 59, -1, -1, 123, -1, 125, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 40, -1, -1, 43, -1, 45, -1, 47, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 59, -1, 256, 123, -1, 125, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 290, 291, 292, 293, 294, 295, 296, 297, 298, -1, 300, 301, 302, 303, -1, 305, 306, 307, -1, 40, -1, -1, 43, -1, 45, -1, 47, -1, 123, -1, 125, 321, 322, 323, -1, -1, -1, -1, 59, 256, -1, 331, 332, 333, 334, 335, 336, -1, 338, 339, 340, 341, 342, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 290, 291, 292, 293, 294, 295, 296, 297, 298, -1, 300, 301, 302, 303, -1, 305, 306, 307, -1, -1, -1, -1, -1, 40, -1, -1, 43, 256, 45, 123, 47, 321, 322, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, 339, 340, 341, 342, -1, 344, -1, -1, -1, 348, 349, 350, 290, 291, 292, 293, 294, 295, 296, 297, 298, -1, 300, 301, 302, 303, -1, 305, 306, 307, -1, -1, -1, -1, -1, -1, -1, -1, -1, 256, -1, -1, -1, 321, 322, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, 339, 340, 341, 342, -1, 344, -1, -1, -1, 348, 349, 350, 290, 291, 292, 293, 294, 295, 296, 297, 298, -1, 300, 301, 302, 303, -1, 305, 306, 307, 40, -1, -1, 43, -1, 45, -1, 47, -1, -1, -1, -1, -1, 321, 322, 323, -1, -1, -1, 59, -1, -1, 256, 331, 332, 333, 334, 335, 336, 263, 338, 339, 340, 341, 342, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 290, 291, 292, 293, 294, 295, 296, 297, 298, -1, 300, 301, 302, 303, -1, 305, 306, 307, 40, -1, -1, 43, -1, 45, -1, 47, -1, 123, -1, 125, -1, 321, 322, 323, -1, -1, -1, 59, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, 339, 340, 341, 342, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 290, 291, 40, 293, -1, 43, -1, 45, -1, 47, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, 59, -1, -1, 123, -1, 125, -1, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, 40, -1, -1, 43, -1, 45, -1, 47, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 59, 256, -1, 123, -1, 125, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 290, 291, 292, 293, 294, 295, 296, 297, 298, -1, 300, 301, 302, 303, -1, 305, 306, 307, -1, 40, -1, -1, 43, -1, 45, -1, 47, -1, -1, 123, -1, 321, 322, 323, -1, -1, -1, -1, 59, -1, 256, 331, 332, 333, 334, 335, 336, -1, 338, 339, 340, 341, 342, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 290, 291, 292, 293, 294, 295, 296, 297, 298, -1, 300, 301, 302, 303, -1, 305, 306, 307, 40, -1, -1, 43, -1, 45, -1, 47, -1, 256, 123, -1, -1, 321, 322, 323, -1, -1, -1, 59, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, 339, 340, 341, 342, -1, 344, -1, -1, -1, 348, 349, 350, 290, 291, 292, 293, 294, 295, 296, 297, 298, -1, 300, 301, 302, 303, -1, 305, 306, 307, 40, -1, -1, 43, -1, 45, -1, 47, -1, 256, -1, -1, -1, 321, 322, 323, -1, -1, -1, 59, -1, -1, 123, 331, 332, 333, 334, 335, 336, -1, 338, 339, 340, 341, 342, -1, 344, -1, -1, -1, 348, 349, 350, 290, 291, 292, 293, 294, 295, 296, 297, 298, -1, 300, 301, 302, 303, -1, 305, 306, 307, 40, -1, -1, 43, -1, 45, -1, 47, -1, -1, -1, -1, -1, 321, 322, 323, -1, -1, -1, 59, -1, 256, 123, 331, 332, 333, 334, 335, 336, -1, 338, 339, 340, 341, 342, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 290, 291, 292, 293, 294, 295, 296, 297, 298, -1, 300, 301, 302, 303, -1, 305, 306, 307, 40, -1, -1, 43, -1, 45, -1, 47, -1, -1, 123, -1, -1, 321, 322, 323, -1, -1, -1, 59, -1, 256, -1, 331, 332, 333, 334, 335, 336, -1, 338, 339, 340, 341, 342, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 290, 291, 292, 293, 294, 295, 296, 297, 298, -1, 300, 301, 302, 303, -1, 305, 306, 307, 40, -1, -1, 43, -1, 45, -1, 47, -1, 256, 123, -1, -1, 321, 322, 323, -1, -1, -1, 59, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, 339, 340, 341, 342, -1, 344, -1, -1, -1, 348, 349, 350, 290, 291, 292, 293, 294, 295, 296, 297, 298, -1, 300, 301, 302, 303, -1, 305, 306, 307, -1, -1, -1, -1, -1, -1, -1, -1, -1, 256, -1, -1, -1, 321, 322, 323, -1, -1, -1, -1, -1, -1, 123, 331, 332, 333, 334, 335, 336, -1, 338, 339, 340, 341, 342, -1, 344, -1, -1, -1, 348, 349, 350, 290, 291, 292, 293, 294, 295, 296, 297, 298, -1, 300, 301, 302, 303, -1, 305, 306, 307, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 321, 322, 323, -1, -1, -1, -1, -1, 256, -1, 331, 332, 333, 334, 335, 336, -1, 338, 339, 340, 341, 342, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 290, 291, 292, 293, 294, 295, 296, 297, 298, -1, 300, 301, 302, 303, -1, 305, 306, 307, 40, 41, -1, 43, -1, 45, -1, 47, -1, -1, -1, -1, -1, 321, 322, 323, -1, -1, -1, -1, -1, 256, -1, 331, 332, 333, 334, 335, 336, -1, 338, 339, 340, 341, 342, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 290, 291, 292, 293, 294, 295, 296, 297, 298, -1, 300, 301, 302, 303, -1, 305, 306, 307, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 321, 322, 323, 40, -1, -1, 43, -1, 45, -1, 331, 332, 333, 334, 335, 336, -1, 338, 339, 340, 341, 342, 59, 344, -1, -1, 63, 348, 349, 350, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 40, 41, -1, 43, 44, 45, -1, -1, -1, -1, -1, -1, 124, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 63, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 256, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 40, 41, -1, 43, 44, 45, -1, -1, -1, -1, -1, -1, 124, -1, -1, -1, -1, 290, 291, -1, 293, -1, 295, 63, -1, -1, -1, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 321, 322, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, 263, -1, -1, -1, 267, 124, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 280, 281, -1, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, 40, 41, -1, 43, 44, 45, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 63, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, 267, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, 280, 281, -1, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, 40, 41, -1, 43, -1, 45, 300, 301, -1, 303, -1, 305, 124, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 63, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, 267, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, 280, 281, -1, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, 40, -1, -1, 43, 44, 45, 300, 301, -1, 303, -1, 305, 124, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 63, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 40, -1, -1, 43, -1, 45, -1, -1, 267, 124, -1, -1, -1, -1, -1, -1, -1, -1, 58, -1, -1, 280, 281, 63, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, -1, -1, -1, -1, -1, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, 267, 340, 341, -1, 124, 344, -1, -1, -1, 348, 349, 350, -1, 280, 281, -1, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, 40, 41, -1, 43, -1, 45, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 63, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, 267, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, 280, 281, -1, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, -1, -1, -1, -1, -1, 300, 301, -1, 303, -1, 305, 124, 40, 41, -1, 43, -1, 45, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, 63, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, 267, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, 280, 281, -1, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, -1, -1, -1, -1, -1, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 124, -1, -1, 40, 41, -1, 43, 323, 45, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, 63, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 280, 281, -1, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, -1, 124, -1, -1, -1, 300, 301, -1, 303, -1, 305, -1, 40, 41, -1, 43, -1, 45, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, 63, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 280, 281, -1, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, -1, -1, -1, -1, -1, 300, 301, -1, 303, -1, 305, 124, 40, 41, -1, 43, -1, 45, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, 63, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 280, 281, -1, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, -1, -1, -1, -1, -1, 300, 301, -1, 303, 124, 305, 40, -1, -1, 43, -1, 45, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 323, -1, 59, -1, -1, -1, 63, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 280, 281, -1, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, 124, -1, -1, -1, -1, 300, 301, -1, 303, -1, 305, -1, 40, 41, -1, 43, -1, 45, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, 63, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 280, 281, -1, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, 40, -1, -1, 43, -1, 45, 300, 301, -1, 303, -1, 305, 124, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 63, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 280, 281, -1, 283, 284, 285, 286, 287, 288, 289, 290, 291, 124, 293, 40, -1, -1, 43, -1, 45, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 63, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 280, 281, 124, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, 40, 41, -1, -1, 44, -1, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, 58, 59, -1, -1, -1, 63, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, 267, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, 280, 281, -1, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, -1, -1, -1, 40, 41, 300, 301, 44, 303, -1, 305, 124, -1, -1, -1, -1, -1, -1, -1, -1, -1, 58, 59, -1, -1, -1, 63, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 280, 281, -1, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, -1, 124, -1, -1, 40, 300, 301, 43, 303, 45, 305, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, -1, -1, -1, -1, 263, -1, -1, -1, 267, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 280, 281, 282, -1, -1, 285, -1, 124, -1, 289, 290, 291, -1, 293, 40, -1, -1, 43, -1, 45, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, 263, 340, 341, -1, 267, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, -1, 280, 281, 282, -1, -1, 285, -1, -1, -1, 289, 290, 291, -1, 293, 40, -1, -1, 43, -1, 45, 300, 301, 124, 303, -1, 305, -1, -1, -1, -1, -1, -1, 58, -1, -1, -1, -1, 63, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, 267, -1, -1, 40, -1, -1, 43, -1, 45, -1, 47, -1, -1, 280, -1, -1, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, -1, -1, -1, -1, -1, -1, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, 40, -1, 344, 43, -1, 45, 348, 349, 350, -1, -1, 123, -1, -1, -1, -1, 267, -1, -1, -1, -1, -1, -1, 63, 40, -1, -1, 43, -1, 45, -1, 47, 283, 284, 285, 286, 287, 288, 289, 290, 291, -1, 293, 59, -1, -1, -1, -1, -1, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, 267, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, 40, 280, 281, 43, -1, 45, -1, 47, -1, -1, 289, 290, 291, -1, 293, -1, -1, -1, -1, -1, -1, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 323, 261, 262, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, 290, 291, -1, 293, -1, -1, -1, -1, -1, 299, 300, 301, -1, 303, 40, 305, -1, 43, -1, 45, -1, 47, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, 267, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, 280, 281, -1, -1, -1, -1, -1, 263, -1, 289, 290, 291, -1, 293, -1, -1, -1, -1, -1, 40, 300, 301, 43, 303, 45, 305, 47, -1, -1, -1, -1, -1, -1, -1, 290, 291, -1, 293, -1, -1, -1, -1, -1, 323, 300, 301, -1, 303, -1, 305, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, 323, 348, 349, 350, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, 263, 344, -1, -1, -1, 348, 349, 350, 40, -1, -1, 43, -1, 45, -1, 47, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 290, 291, -1, 293, -1, -1, -1, -1, -1, -1, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, 263, -1, 40, -1, -1, 43, -1, 45, -1, 47, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 40, 290, 291, 43, 293, 45, -1, 47, -1, -1, -1, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, 263, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, -1, 40, 290, 291, 43, 293, 45, -1, 47, -1, -1, -1, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, 40, -1, -1, 43, -1, 45, -1, 47, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, 263, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 40, 290, 291, 43, 293, 45, -1, 47, -1, -1, -1, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, 40, -1, -1, 43, -1, 45, -1, 47, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, 263, -1, -1, 348, 349, 350, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 40, -1, -1, 43, 263, 45, -1, 290, 291, -1, 293, -1, -1, -1, -1, -1, -1, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, -1, 290, 291, -1, 293, -1, -1, -1, -1, -1, 323, 300, 301, -1, 303, -1, 305, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, 323, 348, 349, 350, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, -1, -1, -1, -1, -1, -1, 290, 291, -1, 293, -1, -1, -1, -1, -1, -1, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, -1, 290, 291, -1, 293, -1, -1, -1, -1, -1, 323, 300, 301, -1, 303, -1, 305, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, 323, 348, 349, 350, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, 290, 291, 344, 293, -1, -1, 348, 349, 350, -1, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, -1, 290, 291, -1, 293, -1, -1, -1, -1, -1, 323, 300, 301, -1, 303, -1, 305, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, -1, -1, 344, -1, -1, 323, 348, 349, 350, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, 338, -1, 340, 341, 290, 291, 344, 293, -1, -1, 348, 349, 350, -1, 300, 301, -1, 303, -1, 305, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 323, -1, -1, -1, -1, -1, -1, -1, 331, 332, 333, 334, 335, 336, -1, -1, -1, 340, 341, -1, -1, 344, -1, -1, -1, 348, 349, 350, }; #define YYFINAL 4 #ifndef YYDEBUG #define YYDEBUG 0 #endif #define YYMAXTOKEN 351 #if YYDEBUG const char * const yyname[] = { "end-of-file",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,"'%'",0,0,"'('","')'","'*'","'+'","','","'-'",0,"'/'",0,0,0,0,0,0,0,0,0,0, "':'","';'",0,0,0,"'?'",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, "'['",0,"']'",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"'{'", "'|'","'}'",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"FIRSTTOKEN","PROGRAM","PASTAT","PASTAT2", "XBEGIN","XEND","NL","ARRAY","MATCH","NOTMATCH","MATCHOP","FINAL","DOT","ALL", "CCL","NCCL","CHAR","OR","STAR","QUEST","PLUS","EMPTYRE","ZERO","AND","BOR", "APPEND","EQ","GE","GT","LE","LT","NE","IN","ARG","BLTIN","BREAK","CLOSE", "CONTINUE","DELETE","DO","EXIT","FOR","FUNC","SUB","GSUB","IF","INDEX", "LSUBSTR","MATCHFCN","NEXT","NEXTFILE","ADD","MINUS","MULT","DIVIDE","MOD", "ASSIGN","ASGNOP","ADDEQ","SUBEQ","MULTEQ","DIVEQ","MODEQ","POWEQ","PRINT", "PRINTF","SPRINTF","ELSE","INTEST","CONDEXPR","POSTINCR","PREINCR","POSTDECR", "PREDECR","VAR","IVAR","VARNF","CALL","NUMBER","STRING","REGEXPR","GETLINE", "RETURN","SPLIT","SUBSTR","WHILE","CAT","NOT","UMINUS","UPLUS","POWER","DECR", "INCR","INDIRECT","LASTTOKEN", }; const char * const yyrule[] = {"$accept : program", "program : pas", "program : error", "and : AND", "and : and NL", "bor : BOR", "bor : bor NL", "comma : ','", "comma : comma NL", "do : DO", "do : do NL", "else : ELSE", "else : else NL", "$$1 :", "for : FOR '(' opt_simple_stmt ';' opt_nl pattern ';' opt_nl opt_simple_stmt rparen $$1 stmt", "$$2 :", "for : FOR '(' opt_simple_stmt ';' ';' opt_nl opt_simple_stmt rparen $$2 stmt", "$$3 :", "for : FOR '(' varname IN varname rparen $$3 stmt", "funcname : VAR", "funcname : CALL", "if : IF '(' pattern rparen", "lbrace : '{'", "lbrace : lbrace NL", "nl : NL", "nl : nl NL", "opt_nl :", "opt_nl : nl", "opt_pst :", "opt_pst : pst", "opt_simple_stmt :", "opt_simple_stmt : simple_stmt", "pas : opt_pst", "pas : opt_pst pa_stats opt_pst", "pa_pat : pattern", "pa_stat : pa_pat", "pa_stat : pa_pat lbrace stmtlist '}'", "pa_stat : pa_pat ',' opt_nl pa_pat", "pa_stat : pa_pat ',' opt_nl pa_pat lbrace stmtlist '}'", "pa_stat : lbrace stmtlist '}'", "pa_stat : XBEGIN lbrace stmtlist '}'", "pa_stat : XEND lbrace stmtlist '}'", "$$4 :", "pa_stat : FUNC funcname '(' varlist rparen $$4 lbrace stmtlist '}'", "pa_stats : pa_stat", "pa_stats : pa_stats opt_pst pa_stat", "patlist : pattern", "patlist : patlist comma pattern", "ppattern : var ASGNOP ppattern", "ppattern : ppattern '?' ppattern ':' ppattern", "ppattern : ppattern bor ppattern", "ppattern : ppattern and ppattern", "ppattern : ppattern MATCHOP reg_expr", "ppattern : ppattern MATCHOP ppattern", "ppattern : ppattern IN varname", "ppattern : '(' plist ')' IN varname", "ppattern : ppattern term", "ppattern : re", "ppattern : term", "pattern : var ASGNOP pattern", "pattern : pattern '?' pattern ':' pattern", "pattern : pattern bor pattern", "pattern : pattern and pattern", "pattern : pattern EQ pattern", "pattern : pattern GE pattern", "pattern : pattern GT pattern", "pattern : pattern LE pattern", "pattern : pattern LT pattern", "pattern : pattern NE pattern", "pattern : pattern MATCHOP reg_expr", "pattern : pattern MATCHOP pattern", "pattern : pattern IN varname", "pattern : '(' plist ')' IN varname", "pattern : pattern '|' GETLINE var", "pattern : pattern '|' GETLINE", "pattern : pattern term", "pattern : re", "pattern : term", "plist : pattern comma pattern", "plist : plist comma pattern", "pplist : ppattern", "pplist : pplist comma ppattern", "prarg :", "prarg : pplist", "prarg : '(' plist ')'", "print : PRINT", "print : PRINTF", "pst : NL", "pst : ';'", "pst : pst NL", "pst : pst ';'", "rbrace : '}'", "rbrace : rbrace NL", "re : reg_expr", "re : NOT re", "$$5 :", "reg_expr : '/' $$5 REGEXPR '/'", "rparen : ')'", "rparen : rparen NL", "simple_stmt : print prarg '|' term", "simple_stmt : print prarg APPEND term", "simple_stmt : print prarg GT term", "simple_stmt : print prarg", "simple_stmt : DELETE varname '[' patlist ']'", "simple_stmt : DELETE varname", "simple_stmt : pattern", "simple_stmt : error", "st : nl", "st : ';' opt_nl", "stmt : BREAK st", "stmt : CONTINUE st", "$$6 :", "$$7 :", "stmt : do $$6 stmt $$7 WHILE '(' pattern ')' st", "stmt : EXIT pattern st", "stmt : EXIT st", "stmt : for", "stmt : if stmt else stmt", "stmt : if stmt", "stmt : lbrace stmtlist rbrace", "stmt : NEXT st", "stmt : NEXTFILE st", "stmt : RETURN pattern st", "stmt : RETURN st", "stmt : simple_stmt st", "$$8 :", "stmt : while $$8 stmt", "stmt : ';' opt_nl", "stmtlist : stmt", "stmtlist : stmtlist stmt", "subop : SUB", "subop : GSUB", "string : STRING", "string : string STRING", "term : term '/' ASGNOP term", "term : term '+' term", "term : term '-' term", "term : term '*' term", "term : term '/' term", "term : term '%' term", "term : term POWER term", "term : '-' term", "term : '+' term", "term : NOT term", "term : BLTIN '(' ')'", "term : BLTIN '(' patlist ')'", "term : BLTIN", "term : CALL '(' ')'", "term : CALL '(' patlist ')'", "term : CLOSE term", "term : DECR var", "term : INCR var", "term : var DECR", "term : var INCR", "term : GETLINE var LT term", "term : GETLINE LT term", "term : GETLINE var", "term : GETLINE", "term : INDEX '(' pattern comma pattern ')'", "term : INDEX '(' pattern comma reg_expr ')'", "term : '(' pattern ')'", "term : MATCHFCN '(' pattern comma reg_expr ')'", "term : MATCHFCN '(' pattern comma pattern ')'", "term : NUMBER", "term : SPLIT '(' pattern comma varname comma pattern ')'", "term : SPLIT '(' pattern comma varname comma reg_expr ')'", "term : SPLIT '(' pattern comma varname ')'", "term : SPRINTF '(' patlist ')'", "term : string", "term : subop '(' reg_expr comma pattern ')'", "term : subop '(' pattern comma pattern ')'", "term : subop '(' reg_expr comma pattern comma var ')'", "term : subop '(' pattern comma pattern comma var ')'", "term : SUBSTR '(' pattern comma pattern comma pattern ')'", "term : SUBSTR '(' pattern comma pattern ')'", "term : var", "var : varname", "var : varname '[' patlist ']'", "var : IVAR", "var : INDIRECT term", "varlist :", "varlist : VAR", "varlist : varlist comma VAR", "varname : VAR", "varname : ARG", "varname : VARNF", "while : WHILE '(' pattern rparen", }; #endif #ifdef YYSTACKSIZE #undef YYMAXDEPTH #define YYMAXDEPTH YYSTACKSIZE #else #ifdef YYMAXDEPTH #define YYSTACKSIZE YYMAXDEPTH #else #define YYSTACKSIZE 10000 #define YYMAXDEPTH 10000 #endif #endif #define YYINITSTACKSIZE 200 /* LINTUSED */ int yydebug; int yynerrs; int yyerrflag; int yychar; short *yyssp; YYSTYPE *yyvsp; YYSTYPE yyval; YYSTYPE yylval; short *yyss; short *yysslim; YYSTYPE *yyvs; unsigned int yystacksize; int yyparse(void); #line 452 "awkgram.y" void setfname(Cell *p) { if (isarr(p)) SYNTAX("%s is an array, not a function", p->nval); else if (isfcn(p)) SYNTAX("you can't define function %s more than once", p->nval); curfname = p->nval; } int constnode(Node *p) { return isvalue(p) && ((Cell *) (p->narg[0]))->csub == CCON; } char *strnode(Node *p) { return ((Cell *)(p->narg[0]))->sval; } Node *notnull(Node *n) { switch (n->nobj) { case LE: case LT: case EQ: case NE: case GT: case GE: case BOR: case AND: case NOT: return n; default: return op2(NE, n, nullnode); } } void checkdup(Node *vl, Cell *cp) /* check if name already in list */ { char *s = cp->nval; for ( ; vl; vl = vl->nnext) { if (strcmp(s, ((Cell *)(vl->narg[0]))->nval) == 0) { SYNTAX("duplicate argument %s", s); break; } } } #line 2508 "awkgram.tab.c" /* allocate initial stack or double stack size, up to YYMAXDEPTH */ static int yygrowstack(void) { unsigned int newsize; long sslen; short *newss; YYSTYPE *newvs; if ((newsize = yystacksize) == 0) newsize = YYINITSTACKSIZE; else if (newsize >= YYMAXDEPTH) return -1; else if ((newsize *= 2) > YYMAXDEPTH) newsize = YYMAXDEPTH; sslen = yyssp - yyss; #ifdef SIZE_MAX #define YY_SIZE_MAX SIZE_MAX #else #define YY_SIZE_MAX 0xffffffffU #endif if (newsize && YY_SIZE_MAX / newsize < sizeof *newss) goto bail; newss = yyss ? (short *)realloc(yyss, newsize * sizeof *newss) : (short *)malloc(newsize * sizeof *newss); /* overflow check above */ if (newss == NULL) goto bail; yyss = newss; yyssp = newss + sslen; if (newsize && YY_SIZE_MAX / newsize < sizeof *newvs) goto bail; newvs = yyvs ? (YYSTYPE *)realloc(yyvs, newsize * sizeof *newvs) : (YYSTYPE *)malloc(newsize * sizeof *newvs); /* overflow check above */ if (newvs == NULL) goto bail; yyvs = newvs; yyvsp = newvs + sslen; yystacksize = newsize; yysslim = yyss + newsize - 1; return 0; bail: if (yyss) free(yyss); if (yyvs) free(yyvs); yyss = yyssp = NULL; yyvs = yyvsp = NULL; yystacksize = 0; return -1; } #define YYABORT goto yyabort #define YYREJECT goto yyabort #define YYACCEPT goto yyaccept #define YYERROR goto yyerrlab int yyparse(void) { int yym, yyn, yystate; #if YYDEBUG const char *yys; if ((yys = getenv("YYDEBUG"))) { yyn = *yys; if (yyn >= '0' && yyn <= '9') yydebug = yyn - '0'; } #endif /* YYDEBUG */ yynerrs = 0; yyerrflag = 0; yychar = (-1); if (yyss == NULL && yygrowstack()) goto yyoverflow; yyssp = yyss; yyvsp = yyvs; *yyssp = yystate = 0; yyloop: if ((yyn = yydefred[yystate]) != 0) goto yyreduce; if (yychar < 0) { if ((yychar = yylex()) < 0) yychar = 0; #if YYDEBUG if (yydebug) { yys = 0; if (yychar <= YYMAXTOKEN) yys = yyname[yychar]; if (!yys) yys = "illegal-symbol"; printf("%sdebug: state %d, reading %d (%s)\n", YYPREFIX, yystate, yychar, yys); } #endif } if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 && yyn <= YYTABLESIZE && yycheck[yyn] == yychar) { #if YYDEBUG if (yydebug) printf("%sdebug: state %d, shifting to state %d\n", YYPREFIX, yystate, yytable[yyn]); #endif if (yyssp >= yysslim && yygrowstack()) { goto yyoverflow; } *++yyssp = yystate = yytable[yyn]; *++yyvsp = yylval; yychar = (-1); if (yyerrflag > 0) --yyerrflag; goto yyloop; } if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 && yyn <= YYTABLESIZE && yycheck[yyn] == yychar) { yyn = yytable[yyn]; goto yyreduce; } if (yyerrflag) goto yyinrecovery; #if defined(__GNUC__) goto yynewerror; #endif yynewerror: yyerror("syntax error"); #if defined(__GNUC__) goto yyerrlab; #endif yyerrlab: ++yynerrs; yyinrecovery: if (yyerrflag < 3) { yyerrflag = 3; for (;;) { if ((yyn = yysindex[*yyssp]) && (yyn += YYERRCODE) >= 0 && yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE) { #if YYDEBUG if (yydebug) printf("%sdebug: state %d, error recovery shifting\ to state %d\n", YYPREFIX, *yyssp, yytable[yyn]); #endif if (yyssp >= yysslim && yygrowstack()) { goto yyoverflow; } *++yyssp = yystate = yytable[yyn]; *++yyvsp = yylval; goto yyloop; } else { #if YYDEBUG if (yydebug) printf("%sdebug: error recovery discarding state %d\n", YYPREFIX, *yyssp); #endif if (yyssp <= yyss) goto yyabort; --yyssp; --yyvsp; } } } else { if (yychar == 0) goto yyabort; #if YYDEBUG if (yydebug) { yys = 0; if (yychar <= YYMAXTOKEN) yys = yyname[yychar]; if (!yys) yys = "illegal-symbol"; printf("%sdebug: state %d, error recovery discards token %d (%s)\n", YYPREFIX, yystate, yychar, yys); } #endif yychar = (-1); goto yyloop; } yyreduce: #if YYDEBUG if (yydebug) printf("%sdebug: state %d, reducing by rule %d (%s)\n", YYPREFIX, yystate, yyn, yyrule[yyn]); #endif yym = yylen[yyn]; if (yym) yyval = yyvsp[1-yym]; else memset(&yyval, 0, sizeof yyval); switch (yyn) { case 1: #line 99 "awkgram.y" { if (errorflag==0) winner = (Node *)stat3(PROGRAM, beginloc, yyvsp[0].p, endloc); } break; case 2: #line 101 "awkgram.y" { yyclearin; bracecheck(); SYNTAX("bailing out"); } break; case 13: #line 125 "awkgram.y" {inloop++;} break; case 14: #line 126 "awkgram.y" { --inloop; yyval.p = stat4(FOR, yyvsp[-9].p, notnull(yyvsp[-6].p), yyvsp[-3].p, yyvsp[0].p); } break; case 15: #line 127 "awkgram.y" {inloop++;} break; case 16: #line 128 "awkgram.y" { --inloop; yyval.p = stat4(FOR, yyvsp[-7].p, NIL, yyvsp[-3].p, yyvsp[0].p); } break; case 17: #line 129 "awkgram.y" {inloop++;} break; case 18: #line 130 "awkgram.y" { --inloop; yyval.p = stat3(IN, yyvsp[-5].p, makearr(yyvsp[-3].p), yyvsp[0].p); } break; case 19: #line 134 "awkgram.y" { setfname(yyvsp[0].cp); } break; case 20: #line 135 "awkgram.y" { setfname(yyvsp[0].cp); } break; case 21: #line 139 "awkgram.y" { yyval.p = notnull(yyvsp[-1].p); } break; case 26: #line 151 "awkgram.y" { yyval.i = 0; } break; case 28: #line 156 "awkgram.y" { yyval.i = 0; } break; case 30: #line 162 "awkgram.y" { yyval.p = 0; } break; case 32: #line 167 "awkgram.y" { yyval.p = 0; } break; case 33: #line 168 "awkgram.y" { yyval.p = yyvsp[-1].p; } break; case 34: #line 172 "awkgram.y" { yyval.p = notnull(yyvsp[0].p); } break; case 35: #line 176 "awkgram.y" { yyval.p = stat2(PASTAT, yyvsp[0].p, stat2(PRINT, rectonode(), NIL)); } break; case 36: #line 177 "awkgram.y" { yyval.p = stat2(PASTAT, yyvsp[-3].p, yyvsp[-1].p); } break; case 37: #line 178 "awkgram.y" { yyval.p = pa2stat(yyvsp[-3].p, yyvsp[0].p, stat2(PRINT, rectonode(), NIL)); } break; case 38: #line 179 "awkgram.y" { yyval.p = pa2stat(yyvsp[-6].p, yyvsp[-3].p, yyvsp[-1].p); } break; case 39: #line 180 "awkgram.y" { yyval.p = stat2(PASTAT, NIL, yyvsp[-1].p); } break; case 40: #line 182 "awkgram.y" { beginloc = linkum(beginloc, yyvsp[-1].p); yyval.p = 0; } break; case 41: #line 184 "awkgram.y" { endloc = linkum(endloc, yyvsp[-1].p); yyval.p = 0; } break; case 42: #line 185 "awkgram.y" {infunc = true;} break; case 43: #line 186 "awkgram.y" { infunc = false; curfname=0; defn((Cell *)yyvsp[-7].p, yyvsp[-5].p, yyvsp[-1].p); yyval.p = 0; } break; case 45: #line 191 "awkgram.y" { yyval.p = linkum(yyvsp[-2].p, yyvsp[0].p); } break; case 47: #line 196 "awkgram.y" { yyval.p = linkum(yyvsp[-2].p, yyvsp[0].p); } break; case 48: #line 200 "awkgram.y" { yyval.p = op2(yyvsp[-1].i, yyvsp[-2].p, yyvsp[0].p); } break; case 49: #line 202 "awkgram.y" { yyval.p = op3(CONDEXPR, notnull(yyvsp[-4].p), yyvsp[-2].p, yyvsp[0].p); } break; case 50: #line 204 "awkgram.y" { yyval.p = op2(BOR, notnull(yyvsp[-2].p), notnull(yyvsp[0].p)); } break; case 51: #line 206 "awkgram.y" { yyval.p = op2(AND, notnull(yyvsp[-2].p), notnull(yyvsp[0].p)); } break; case 52: #line 207 "awkgram.y" { yyval.p = op3(yyvsp[-1].i, NIL, yyvsp[-2].p, (Node*)makedfa(yyvsp[0].s, 0)); } break; case 53: #line 209 "awkgram.y" { if (constnode(yyvsp[0].p)) yyval.p = op3(yyvsp[-1].i, NIL, yyvsp[-2].p, (Node*)makedfa(strnode(yyvsp[0].p), 0)); else yyval.p = op3(yyvsp[-1].i, (Node *)1, yyvsp[-2].p, yyvsp[0].p); } break; case 54: #line 213 "awkgram.y" { yyval.p = op2(INTEST, yyvsp[-2].p, makearr(yyvsp[0].p)); } break; case 55: #line 214 "awkgram.y" { yyval.p = op2(INTEST, yyvsp[-3].p, makearr(yyvsp[0].p)); } break; case 56: #line 215 "awkgram.y" { yyval.p = op2(CAT, yyvsp[-1].p, yyvsp[0].p); } break; case 59: #line 221 "awkgram.y" { yyval.p = op2(yyvsp[-1].i, yyvsp[-2].p, yyvsp[0].p); } break; case 60: #line 223 "awkgram.y" { yyval.p = op3(CONDEXPR, notnull(yyvsp[-4].p), yyvsp[-2].p, yyvsp[0].p); } break; case 61: #line 225 "awkgram.y" { yyval.p = op2(BOR, notnull(yyvsp[-2].p), notnull(yyvsp[0].p)); } break; case 62: #line 227 "awkgram.y" { yyval.p = op2(AND, notnull(yyvsp[-2].p), notnull(yyvsp[0].p)); } break; case 63: #line 228 "awkgram.y" { yyval.p = op2(yyvsp[-1].i, yyvsp[-2].p, yyvsp[0].p); } break; case 64: #line 229 "awkgram.y" { yyval.p = op2(yyvsp[-1].i, yyvsp[-2].p, yyvsp[0].p); } break; case 65: #line 230 "awkgram.y" { yyval.p = op2(yyvsp[-1].i, yyvsp[-2].p, yyvsp[0].p); } break; case 66: #line 231 "awkgram.y" { yyval.p = op2(yyvsp[-1].i, yyvsp[-2].p, yyvsp[0].p); } break; case 67: #line 232 "awkgram.y" { yyval.p = op2(yyvsp[-1].i, yyvsp[-2].p, yyvsp[0].p); } break; case 68: #line 233 "awkgram.y" { yyval.p = op2(yyvsp[-1].i, yyvsp[-2].p, yyvsp[0].p); } break; case 69: #line 234 "awkgram.y" { yyval.p = op3(yyvsp[-1].i, NIL, yyvsp[-2].p, (Node*)makedfa(yyvsp[0].s, 0)); } break; case 70: #line 236 "awkgram.y" { if (constnode(yyvsp[0].p)) yyval.p = op3(yyvsp[-1].i, NIL, yyvsp[-2].p, (Node*)makedfa(strnode(yyvsp[0].p), 0)); else yyval.p = op3(yyvsp[-1].i, (Node *)1, yyvsp[-2].p, yyvsp[0].p); } break; case 71: #line 240 "awkgram.y" { yyval.p = op2(INTEST, yyvsp[-2].p, makearr(yyvsp[0].p)); } break; case 72: #line 241 "awkgram.y" { yyval.p = op2(INTEST, yyvsp[-3].p, makearr(yyvsp[0].p)); } break; case 73: #line 242 "awkgram.y" { if (safe) SYNTAX("cmd | getline is unsafe"); else yyval.p = op3(GETLINE, yyvsp[0].p, itonp(yyvsp[-2].i), yyvsp[-3].p); } break; case 74: #line 245 "awkgram.y" { if (safe) SYNTAX("cmd | getline is unsafe"); else yyval.p = op3(GETLINE, (Node*)0, itonp(yyvsp[-1].i), yyvsp[-2].p); } break; case 75: #line 248 "awkgram.y" { yyval.p = op2(CAT, yyvsp[-1].p, yyvsp[0].p); } break; case 78: #line 254 "awkgram.y" { yyval.p = linkum(yyvsp[-2].p, yyvsp[0].p); } break; case 79: #line 255 "awkgram.y" { yyval.p = linkum(yyvsp[-2].p, yyvsp[0].p); } break; case 81: #line 260 "awkgram.y" { yyval.p = linkum(yyvsp[-2].p, yyvsp[0].p); } break; case 82: #line 264 "awkgram.y" { yyval.p = rectonode(); } break; case 84: #line 266 "awkgram.y" { yyval.p = yyvsp[-1].p; } break; case 93: #line 283 "awkgram.y" { yyval.p = op3(MATCH, NIL, rectonode(), (Node*)makedfa(yyvsp[0].s, 0)); } break; case 94: #line 284 "awkgram.y" { yyval.p = op1(NOT, notnull(yyvsp[0].p)); } break; case 95: #line 288 "awkgram.y" {startreg();} break; case 96: #line 288 "awkgram.y" { yyval.s = yyvsp[-1].s; } break; case 99: #line 296 "awkgram.y" { if (safe) SYNTAX("print | is unsafe"); else yyval.p = stat3(yyvsp[-3].i, yyvsp[-2].p, itonp(yyvsp[-1].i), yyvsp[0].p); } break; case 100: #line 299 "awkgram.y" { if (safe) SYNTAX("print >> is unsafe"); else yyval.p = stat3(yyvsp[-3].i, yyvsp[-2].p, itonp(yyvsp[-1].i), yyvsp[0].p); } break; case 101: #line 302 "awkgram.y" { if (safe) SYNTAX("print > is unsafe"); else yyval.p = stat3(yyvsp[-3].i, yyvsp[-2].p, itonp(yyvsp[-1].i), yyvsp[0].p); } break; case 102: #line 305 "awkgram.y" { yyval.p = stat3(yyvsp[-1].i, yyvsp[0].p, NIL, NIL); } break; case 103: #line 306 "awkgram.y" { yyval.p = stat2(DELETE, makearr(yyvsp[-3].p), yyvsp[-1].p); } break; case 104: #line 307 "awkgram.y" { yyval.p = stat2(DELETE, makearr(yyvsp[0].p), 0); } break; case 105: #line 308 "awkgram.y" { yyval.p = exptostat(yyvsp[0].p); } break; case 106: #line 309 "awkgram.y" { yyclearin; SYNTAX("illegal statement"); } break; case 109: #line 318 "awkgram.y" { if (!inloop) SYNTAX("break illegal outside of loops"); yyval.p = stat1(BREAK, NIL); } break; case 110: #line 320 "awkgram.y" { if (!inloop) SYNTAX("continue illegal outside of loops"); yyval.p = stat1(CONTINUE, NIL); } break; case 111: #line 322 "awkgram.y" {inloop++;} break; case 112: #line 322 "awkgram.y" {--inloop;} break; case 113: #line 323 "awkgram.y" { yyval.p = stat2(DO, yyvsp[-6].p, notnull(yyvsp[-2].p)); } break; case 114: #line 324 "awkgram.y" { yyval.p = stat1(EXIT, yyvsp[-1].p); } break; case 115: #line 325 "awkgram.y" { yyval.p = stat1(EXIT, NIL); } break; case 117: #line 327 "awkgram.y" { yyval.p = stat3(IF, yyvsp[-3].p, yyvsp[-2].p, yyvsp[0].p); } break; case 118: #line 328 "awkgram.y" { yyval.p = stat3(IF, yyvsp[-1].p, yyvsp[0].p, NIL); } break; case 119: #line 329 "awkgram.y" { yyval.p = yyvsp[-1].p; } break; case 120: #line 330 "awkgram.y" { if (infunc) SYNTAX("next is illegal inside a function"); yyval.p = stat1(NEXT, NIL); } break; case 121: #line 333 "awkgram.y" { if (infunc) SYNTAX("nextfile is illegal inside a function"); yyval.p = stat1(NEXTFILE, NIL); } break; case 122: #line 336 "awkgram.y" { yyval.p = stat1(RETURN, yyvsp[-1].p); } break; case 123: #line 337 "awkgram.y" { yyval.p = stat1(RETURN, NIL); } break; case 125: #line 339 "awkgram.y" {inloop++;} break; case 126: #line 339 "awkgram.y" { --inloop; yyval.p = stat2(WHILE, yyvsp[-2].p, yyvsp[0].p); } break; case 127: #line 340 "awkgram.y" { yyval.p = 0; } break; case 129: #line 345 "awkgram.y" { yyval.p = linkum(yyvsp[-1].p, yyvsp[0].p); } break; case 133: #line 354 "awkgram.y" { yyval.cp = catstr(yyvsp[-1].cp, yyvsp[0].cp); } break; case 134: #line 358 "awkgram.y" { yyval.p = op2(DIVEQ, yyvsp[-3].p, yyvsp[0].p); } break; case 135: #line 359 "awkgram.y" { yyval.p = op2(ADD, yyvsp[-2].p, yyvsp[0].p); } break; case 136: #line 360 "awkgram.y" { yyval.p = op2(MINUS, yyvsp[-2].p, yyvsp[0].p); } break; case 137: #line 361 "awkgram.y" { yyval.p = op2(MULT, yyvsp[-2].p, yyvsp[0].p); } break; case 138: #line 362 "awkgram.y" { yyval.p = op2(DIVIDE, yyvsp[-2].p, yyvsp[0].p); } break; case 139: #line 363 "awkgram.y" { yyval.p = op2(MOD, yyvsp[-2].p, yyvsp[0].p); } break; case 140: #line 364 "awkgram.y" { yyval.p = op2(POWER, yyvsp[-2].p, yyvsp[0].p); } break; case 141: #line 365 "awkgram.y" { yyval.p = op1(UMINUS, yyvsp[0].p); } break; case 142: #line 366 "awkgram.y" { yyval.p = op1(UPLUS, yyvsp[0].p); } break; case 143: #line 367 "awkgram.y" { yyval.p = op1(NOT, notnull(yyvsp[0].p)); } break; case 144: #line 368 "awkgram.y" { yyval.p = op2(BLTIN, itonp(yyvsp[-2].i), rectonode()); } break; case 145: #line 369 "awkgram.y" { yyval.p = op2(BLTIN, itonp(yyvsp[-3].i), yyvsp[-1].p); } break; case 146: #line 370 "awkgram.y" { yyval.p = op2(BLTIN, itonp(yyvsp[0].i), rectonode()); } break; case 147: #line 371 "awkgram.y" { yyval.p = op2(CALL, celltonode(yyvsp[-2].cp,CVAR), NIL); } break; case 148: #line 372 "awkgram.y" { yyval.p = op2(CALL, celltonode(yyvsp[-3].cp,CVAR), yyvsp[-1].p); } break; case 149: #line 373 "awkgram.y" { yyval.p = op1(CLOSE, yyvsp[0].p); } break; case 150: #line 374 "awkgram.y" { yyval.p = op1(PREDECR, yyvsp[0].p); } break; case 151: #line 375 "awkgram.y" { yyval.p = op1(PREINCR, yyvsp[0].p); } break; case 152: #line 376 "awkgram.y" { yyval.p = op1(POSTDECR, yyvsp[-1].p); } break; case 153: #line 377 "awkgram.y" { yyval.p = op1(POSTINCR, yyvsp[-1].p); } break; case 154: #line 378 "awkgram.y" { yyval.p = op3(GETLINE, yyvsp[-2].p, itonp(yyvsp[-1].i), yyvsp[0].p); } break; case 155: #line 379 "awkgram.y" { yyval.p = op3(GETLINE, NIL, itonp(yyvsp[-1].i), yyvsp[0].p); } break; case 156: #line 380 "awkgram.y" { yyval.p = op3(GETLINE, yyvsp[0].p, NIL, NIL); } break; case 157: #line 381 "awkgram.y" { yyval.p = op3(GETLINE, NIL, NIL, NIL); } break; case 158: #line 383 "awkgram.y" { yyval.p = op2(INDEX, yyvsp[-3].p, yyvsp[-1].p); } break; case 159: #line 385 "awkgram.y" { SYNTAX("index() doesn't permit regular expressions"); yyval.p = op2(INDEX, yyvsp[-3].p, (Node*)yyvsp[-1].s); } break; case 160: #line 387 "awkgram.y" { yyval.p = yyvsp[-1].p; } break; case 161: #line 389 "awkgram.y" { yyval.p = op3(MATCHFCN, NIL, yyvsp[-3].p, (Node*)makedfa(yyvsp[-1].s, 1)); } break; case 162: #line 391 "awkgram.y" { if (constnode(yyvsp[-1].p)) yyval.p = op3(MATCHFCN, NIL, yyvsp[-3].p, (Node*)makedfa(strnode(yyvsp[-1].p), 1)); else yyval.p = op3(MATCHFCN, (Node *)1, yyvsp[-3].p, yyvsp[-1].p); } break; case 163: #line 395 "awkgram.y" { yyval.p = celltonode(yyvsp[0].cp, CCON); } break; case 164: #line 397 "awkgram.y" { yyval.p = op4(SPLIT, yyvsp[-5].p, makearr(yyvsp[-3].p), yyvsp[-1].p, (Node*)STRING); } break; case 165: #line 399 "awkgram.y" { yyval.p = op4(SPLIT, yyvsp[-5].p, makearr(yyvsp[-3].p), (Node*)makedfa(yyvsp[-1].s, 1), (Node *)REGEXPR); } break; case 166: #line 401 "awkgram.y" { yyval.p = op4(SPLIT, yyvsp[-3].p, makearr(yyvsp[-1].p), NIL, (Node*)STRING); } break; case 167: #line 402 "awkgram.y" { yyval.p = op1(yyvsp[-3].i, yyvsp[-1].p); } break; case 168: #line 403 "awkgram.y" { yyval.p = celltonode(yyvsp[0].cp, CCON); } break; case 169: #line 405 "awkgram.y" { yyval.p = op4(yyvsp[-5].i, NIL, (Node*)makedfa(yyvsp[-3].s, 1), yyvsp[-1].p, rectonode()); } break; case 170: #line 407 "awkgram.y" { if (constnode(yyvsp[-3].p)) yyval.p = op4(yyvsp[-5].i, NIL, (Node*)makedfa(strnode(yyvsp[-3].p), 1), yyvsp[-1].p, rectonode()); else yyval.p = op4(yyvsp[-5].i, (Node *)1, yyvsp[-3].p, yyvsp[-1].p, rectonode()); } break; case 171: #line 412 "awkgram.y" { yyval.p = op4(yyvsp[-7].i, NIL, (Node*)makedfa(yyvsp[-5].s, 1), yyvsp[-3].p, yyvsp[-1].p); } break; case 172: #line 414 "awkgram.y" { if (constnode(yyvsp[-5].p)) yyval.p = op4(yyvsp[-7].i, NIL, (Node*)makedfa(strnode(yyvsp[-5].p), 1), yyvsp[-3].p, yyvsp[-1].p); else yyval.p = op4(yyvsp[-7].i, (Node *)1, yyvsp[-5].p, yyvsp[-3].p, yyvsp[-1].p); } break; case 173: #line 419 "awkgram.y" { yyval.p = op3(SUBSTR, yyvsp[-5].p, yyvsp[-3].p, yyvsp[-1].p); } break; case 174: #line 421 "awkgram.y" { yyval.p = op3(SUBSTR, yyvsp[-3].p, yyvsp[-1].p, NIL); } break; case 177: #line 427 "awkgram.y" { yyval.p = op2(ARRAY, makearr(yyvsp[-3].p), yyvsp[-1].p); } break; case 178: #line 428 "awkgram.y" { yyval.p = op1(INDIRECT, celltonode(yyvsp[0].cp, CVAR)); } break; case 179: #line 429 "awkgram.y" { yyval.p = op1(INDIRECT, yyvsp[0].p); } break; case 180: #line 433 "awkgram.y" { arglist = yyval.p = 0; } break; case 181: #line 434 "awkgram.y" { arglist = yyval.p = celltonode(yyvsp[0].cp,CVAR); } break; case 182: #line 435 "awkgram.y" { checkdup(yyvsp[-2].p, yyvsp[0].cp); arglist = yyval.p = linkum(yyvsp[-2].p,celltonode(yyvsp[0].cp,CVAR)); } break; case 183: #line 441 "awkgram.y" { yyval.p = celltonode(yyvsp[0].cp, CVAR); } break; case 184: #line 442 "awkgram.y" { yyval.p = op1(ARG, itonp(yyvsp[0].i)); } break; case 185: #line 443 "awkgram.y" { yyval.p = op1(VARNF, (Node *) yyvsp[0].cp); } break; case 186: #line 448 "awkgram.y" { yyval.p = notnull(yyvsp[-1].p); } break; #line 3301 "awkgram.tab.c" } yyssp -= yym; yystate = *yyssp; yyvsp -= yym; yym = yylhs[yyn]; if (yystate == 0 && yym == 0) { #if YYDEBUG if (yydebug) printf("%sdebug: after reduction, shifting from state 0 to\ state %d\n", YYPREFIX, YYFINAL); #endif yystate = YYFINAL; *++yyssp = YYFINAL; *++yyvsp = yyval; if (yychar < 0) { if ((yychar = yylex()) < 0) yychar = 0; #if YYDEBUG if (yydebug) { yys = 0; if (yychar <= YYMAXTOKEN) yys = yyname[yychar]; if (!yys) yys = "illegal-symbol"; printf("%sdebug: state %d, reading %d (%s)\n", YYPREFIX, YYFINAL, yychar, yys); } #endif } if (yychar == 0) goto yyaccept; goto yyloop; } if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 && yyn <= YYTABLESIZE && yycheck[yyn] == yystate) yystate = yytable[yyn]; else yystate = yydgoto[yym]; #if YYDEBUG if (yydebug) printf("%sdebug: after reduction, shifting from state %d \ to state %d\n", YYPREFIX, *yyssp, yystate); #endif if (yyssp >= yysslim && yygrowstack()) { goto yyoverflow; } *++yyssp = yystate; *++yyvsp = yyval; goto yyloop; yyoverflow: yyerror("yacc stack overflow"); yyabort: if (yyss) free(yyss); if (yyvs) free(yyvs); yyss = yyssp = NULL; yyvs = yyvsp = NULL; yystacksize = 0; return (1); yyaccept: if (yyss) free(yyss); if (yyvs) free(yyvs); yyss = yyssp = NULL; yyvs = yyvsp = NULL; yystacksize = 0; return (0); }
158,127
3,378
jart/cosmopolitan
false
cosmopolitan/third_party/awk/tran.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Copyright (C) Lucent Technologies 1997 │ │ All Rights Reserved │ │ │ │ Permission to use, copy, modify, and distribute this software and │ │ its documentation for any purpose and without fee is hereby │ │ granted, provided that the above copyright notice appear in all │ │ copies and that both that the copyright notice and this │ │ permission notice and warranty disclaimer appear in supporting │ │ documentation, and that the name Lucent Technologies or any of │ │ its entities not be used in advertising or publicity pertaining │ │ to distribution of the software without specific, written prior │ │ permission. │ │ │ │ LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, │ │ INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. │ │ IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY │ │ SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES │ │ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER │ │ IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, │ │ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF │ │ THIS SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #define DEBUG #include "libc/fmt/conv.h" #include "libc/fmt/fmt.h" #include "libc/math.h" #include "libc/mem/mem.h" #include "libc/str/str.h" #include "third_party/awk/awk.h" // clang-format off #define FULLTAB 2 /* rehash when table gets this x full */ #define GROWTAB 4 /* grow table by this factor */ Array *symtab; /* main symbol table */ char **FS; /* initial field sep */ char **RS; /* initial record sep */ char **OFS; /* output field sep */ char **ORS; /* output record sep */ char **OFMT; /* output format for numbers */ char **CONVFMT; /* format for conversions in getsval */ Awkfloat *NF; /* number of fields in current record */ Awkfloat *NR; /* number of current record */ Awkfloat *FNR; /* number of current record in current file */ char **FILENAME; /* current filename argument */ Awkfloat *ARGC; /* number of arguments from command line */ char **SUBSEP; /* subscript separator for a[i,j,k]; default \034 */ Awkfloat *RSTART; /* start of re matched with ~; origin 1 (!) */ Awkfloat *RLENGTH; /* length of same */ Cell *fsloc; /* FS */ Cell *nrloc; /* NR */ Cell *nfloc; /* NF */ Cell *fnrloc; /* FNR */ Cell *ofsloc; /* OFS */ Cell *orsloc; /* ORS */ Cell *rsloc; /* RS */ Array *ARGVtab; /* symbol table containing ARGV[...] */ Array *ENVtab; /* symbol table containing ENVIRON[...] */ Cell *rstartloc; /* RSTART */ Cell *rlengthloc; /* RLENGTH */ Cell *subseploc; /* SUBSEP */ Cell *symtabloc; /* SYMTAB */ Cell *nullloc; /* a guaranteed empty cell */ Node *nullnode; /* zero&null, converted into a node for comparisons */ Cell *literal0; extern Cell **fldtab; void syminit(void) /* initialize symbol table with builtin vars */ { literal0 = setsymtab("0", "0", 0.0, NUM|STR|CON|DONTFREE, symtab); /* this is used for if(x)... tests: */ nullloc = setsymtab("$zero&null", "", 0.0, NUM|STR|CON|DONTFREE, symtab); nullnode = celltonode(nullloc, CCON); fsloc = setsymtab("FS", " ", 0.0, STR|DONTFREE, symtab); FS = &fsloc->sval; rsloc = setsymtab("RS", "\n", 0.0, STR|DONTFREE, symtab); RS = &rsloc->sval; ofsloc = setsymtab("OFS", " ", 0.0, STR|DONTFREE, symtab); OFS = &ofsloc->sval; orsloc = setsymtab("ORS", "\n", 0.0, STR|DONTFREE, symtab); ORS = &orsloc->sval; OFMT = &setsymtab("OFMT", "%.6g", 0.0, STR|DONTFREE, symtab)->sval; CONVFMT = &setsymtab("CONVFMT", "%.6g", 0.0, STR|DONTFREE, symtab)->sval; FILENAME = &setsymtab("FILENAME", "", 0.0, STR|DONTFREE, symtab)->sval; nfloc = setsymtab("NF", "", 0.0, NUM, symtab); NF = &nfloc->fval; nrloc = setsymtab("NR", "", 0.0, NUM, symtab); NR = &nrloc->fval; fnrloc = setsymtab("FNR", "", 0.0, NUM, symtab); FNR = &fnrloc->fval; subseploc = setsymtab("SUBSEP", "\034", 0.0, STR|DONTFREE, symtab); SUBSEP = &subseploc->sval; rstartloc = setsymtab("RSTART", "", 0.0, NUM, symtab); RSTART = &rstartloc->fval; rlengthloc = setsymtab("RLENGTH", "", 0.0, NUM, symtab); RLENGTH = &rlengthloc->fval; symtabloc = setsymtab("SYMTAB", "", 0.0, ARR, symtab); free(symtabloc->sval); symtabloc->sval = (char *) symtab; } void arginit(int ac, char **av) /* set up ARGV and ARGC */ { Cell *cp; int i; char temp[50]; ARGC = &setsymtab("ARGC", "", (Awkfloat) ac, NUM, symtab)->fval; cp = setsymtab("ARGV", "", 0.0, ARR, symtab); ARGVtab = makesymtab(NSYMTAB); /* could be (int) ARGC as well */ free(cp->sval); cp->sval = (char *) ARGVtab; for (i = 0; i < ac; i++) { double result; sprintf(temp, "%d", i); if (is_number(*av, & result)) setsymtab(temp, *av, result, STR|NUM, ARGVtab); else setsymtab(temp, *av, 0.0, STR, ARGVtab); av++; } } void envinit(char **envp) /* set up ENVIRON variable */ { Cell *cp; char *p; cp = setsymtab("ENVIRON", "", 0.0, ARR, symtab); ENVtab = makesymtab(NSYMTAB); free(cp->sval); cp->sval = (char *) ENVtab; for ( ; *envp; envp++) { double result; if ((p = strchr(*envp, '=')) == NULL) continue; if( p == *envp ) /* no left hand side name in env string */ continue; *p++ = 0; /* split into two strings at = */ if (is_number(p, & result)) setsymtab(*envp, p, result, STR|NUM, ENVtab); else setsymtab(*envp, p, 0.0, STR, ENVtab); p[-1] = '='; /* restore in case env is passed down to a shell */ } } Array *makesymtab(int n) /* make a new symbol table */ { Array *ap; Cell **tp; ap = (Array *) malloc(sizeof(*ap)); tp = (Cell **) calloc(n, sizeof(*tp)); if (ap == NULL || tp == NULL) FATAL("out of space in makesymtab"); ap->nelem = 0; ap->size = n; ap->tab = tp; return(ap); } void freesymtab(Cell *ap) /* free a symbol table */ { Cell *cp, *temp; Array *tp; int i; if (!isarr(ap)) return; tp = (Array *) ap->sval; if (tp == NULL) return; for (i = 0; i < tp->size; i++) { for (cp = tp->tab[i]; cp != NULL; cp = temp) { xfree(cp->nval); if (freeable(cp)) xfree(cp->sval); temp = cp->cnext; /* avoids freeing then using */ free(cp); tp->nelem--; } tp->tab[i] = NULL; } if (tp->nelem != 0) WARNING("can't happen: inconsistent element count freeing %s", ap->nval); free(tp->tab); free(tp); } void freeelem(Cell *ap, const char *s) /* free elem s from ap (i.e., ap["s"] */ { Array *tp; Cell *p, *prev = NULL; int h; tp = (Array *) ap->sval; h = hash(s, tp->size); for (p = tp->tab[h]; p != NULL; prev = p, p = p->cnext) if (strcmp(s, p->nval) == 0) { if (prev == NULL) /* 1st one */ tp->tab[h] = p->cnext; else /* middle somewhere */ prev->cnext = p->cnext; if (freeable(p)) xfree(p->sval); free(p->nval); free(p); tp->nelem--; return; } } Cell *setsymtab(const char *n, const char *s, Awkfloat f, unsigned t, Array *tp) { int h; Cell *p; if (n != NULL && (p = lookup(n, tp)) != NULL) { DPRINTF("setsymtab found %p: n=%s s=\"%s\" f=%g t=%o\n", (void*)p, NN(p->nval), NN(p->sval), p->fval, p->tval); return(p); } p = (Cell *) malloc(sizeof(*p)); if (p == NULL) FATAL("out of space for symbol table at %s", n); p->nval = tostring(n); p->sval = s ? tostring(s) : tostring(""); p->fval = f; p->tval = t; p->csub = CUNK; p->ctype = OCELL; tp->nelem++; if (tp->nelem > FULLTAB * tp->size) rehash(tp); h = hash(n, tp->size); p->cnext = tp->tab[h]; tp->tab[h] = p; DPRINTF("setsymtab set %p: n=%s s=\"%s\" f=%g t=%o\n", (void*)p, p->nval, p->sval, p->fval, p->tval); return(p); } int hash(const char *s, int n) /* form hash value for string s */ { unsigned hashval; for (hashval = 0; *s != '\0'; s++) hashval = (*s + 31 * hashval); return hashval % n; } void rehash(Array *tp) /* rehash items in small table into big one */ { int i, nh, nsz; Cell *cp, *op, **np; nsz = GROWTAB * tp->size; np = (Cell **) calloc(nsz, sizeof(*np)); if (np == NULL) /* can't do it, but can keep running. */ return; /* someone else will run out later. */ for (i = 0; i < tp->size; i++) { for (cp = tp->tab[i]; cp; cp = op) { op = cp->cnext; nh = hash(cp->nval, nsz); cp->cnext = np[nh]; np[nh] = cp; } } free(tp->tab); tp->tab = np; tp->size = nsz; } Cell *lookup(const char *s, Array *tp) /* look for s in tp */ { Cell *p; int h; h = hash(s, tp->size); for (p = tp->tab[h]; p != NULL; p = p->cnext) if (strcmp(s, p->nval) == 0) return(p); /* found it */ return(NULL); /* not found */ } Awkfloat setfval(Cell *vp, Awkfloat f) /* set float val of a Cell */ { int fldno; f += 0.0; /* normalise negative zero to positive zero */ if ((vp->tval & (NUM | STR)) == 0) funnyvar(vp, "assign to"); if (isfld(vp)) { donerec = false; /* mark $0 invalid */ fldno = atoi(vp->nval); if (fldno > *NF) newfld(fldno); DPRINTF("setting field %d to %g\n", fldno, f); } else if (&vp->fval == NF) { donerec = false; /* mark $0 invalid */ setlastfld(f); DPRINTF("setting NF to %g\n", f); } else if (isrec(vp)) { donefld = false; /* mark $1... invalid */ donerec = true; savefs(); } else if (vp == ofsloc) { if (!donerec) recbld(); } if (freeable(vp)) xfree(vp->sval); /* free any previous string */ vp->tval &= ~(STR|CONVC|CONVO); /* mark string invalid */ vp->fmt = NULL; vp->tval |= NUM; /* mark number ok */ if (f == -0) /* who would have thought this possible? */ f = 0; DPRINTF("setfval %p: %s = %g, t=%o\n", (void*)vp, NN(vp->nval), f, vp->tval); return vp->fval = f; } void funnyvar(Cell *vp, const char *rw) { if (isarr(vp)) FATAL("can't %s %s; it's an array name.", rw, vp->nval); if (vp->tval & FCN) FATAL("can't %s %s; it's a function.", rw, vp->nval); WARNING("funny variable %p: n=%s s=\"%s\" f=%g t=%o", (void *)vp, vp->nval, vp->sval, vp->fval, vp->tval); } char *setsval(Cell *vp, const char *s) /* set string val of a Cell */ { char *t; int fldno; Awkfloat f; DPRINTF("starting setsval %p: %s = \"%s\", t=%o, r,f=%d,%d\n", (void*)vp, NN(vp->nval), s, vp->tval, donerec, donefld); if ((vp->tval & (NUM | STR)) == 0) funnyvar(vp, "assign to"); if (isfld(vp)) { donerec = false; /* mark $0 invalid */ fldno = atoi(vp->nval); if (fldno > *NF) newfld(fldno); DPRINTF("setting field %d to %s (%p)\n", fldno, s, (const void*)s); } else if (isrec(vp)) { donefld = false; /* mark $1... invalid */ donerec = true; savefs(); } else if (vp == ofsloc) { if (!donerec) recbld(); } t = s ? tostring(s) : tostring(""); /* in case it's self-assign */ if (freeable(vp)) xfree(vp->sval); vp->tval &= ~(NUM|DONTFREE|CONVC|CONVO); vp->tval |= STR; vp->fmt = NULL; DPRINTF("setsval %p: %s = \"%s (%p) \", t=%o r,f=%d,%d\n", (void*)vp, NN(vp->nval), t, (void*)t, vp->tval, donerec, donefld); vp->sval = t; if (&vp->fval == NF) { donerec = false; /* mark $0 invalid */ f = getfval(vp); setlastfld(f); DPRINTF("setting NF to %g\n", f); } return(vp->sval); } Awkfloat getfval(Cell *vp) /* get float val of a Cell */ { if ((vp->tval & (NUM | STR)) == 0) funnyvar(vp, "read value of"); if (isfld(vp) && !donefld) fldbld(); else if (isrec(vp) && !donerec) recbld(); if (!isnum(vp)) { /* not a number */ double fval; bool no_trailing; if (is_valid_number(vp->sval, true, & no_trailing, & fval)) { vp->fval = fval; if (no_trailing && !(vp->tval&CON)) vp->tval |= NUM; /* make NUM only sparingly */ } else vp->fval = 0.0; } DPRINTF("getfval %p: %s = %g, t=%o\n", (void*)vp, NN(vp->nval), vp->fval, vp->tval); return(vp->fval); } static const char *get_inf_nan(double d) { if (isinf(d)) { return (d < 0 ? "-inf" : "+inf"); } else if (isnan(d)) { return (signbit(d) != 0 ? "-nan" : "+nan"); } else return NULL; } static char *get_str_val(Cell *vp, char **fmt) /* get string val of a Cell */ { char s[256]; double dtemp; const char *p; if ((vp->tval & (NUM | STR)) == 0) funnyvar(vp, "read value of"); if (isfld(vp) && ! donefld) fldbld(); else if (isrec(vp) && ! donerec) recbld(); /* * ADR: This is complicated and more fragile than is desirable. * Retrieving a string value for a number associates the string * value with the scalar. Previously, the string value was * sticky, meaning if converted via OFMT that became the value * (even though POSIX wants it to be via CONVFMT). Or if CONVFMT * changed after a string value was retrieved, the original value * was maintained and used. Also not per POSIX. * * We work around this design by adding two additional flags, * CONVC and CONVO, indicating how the string value was * obtained (via CONVFMT or OFMT) and _also_ maintaining a copy * of the pointer to the xFMT format string used for the * conversion. This pointer is only read, **never** dereferenced. * The next time we do a conversion, if it's coming from the same * xFMT as last time, and the pointer value is different, we * know that the xFMT format string changed, and we need to * redo the conversion. If it's the same, we don't have to. * * There are also several cases where we don't do a conversion, * such as for a field (see the checks below). */ /* Don't duplicate the code for actually updating the value */ #define update_str_val(vp) \ { \ if (freeable(vp)) \ xfree(vp->sval); \ if ((p = get_inf_nan(vp->fval)) != NULL) \ strcpy(s, p); \ else if (modf(vp->fval, &dtemp) == 0) /* it's integral */ \ snprintf(s, sizeof (s), "%.30g", vp->fval); \ else \ snprintf(s, sizeof (s), *fmt, vp->fval); \ vp->sval = tostring(s); \ vp->tval &= ~DONTFREE; \ vp->tval |= STR; \ } if (isstr(vp) == 0) { update_str_val(vp); if (fmt == OFMT) { vp->tval &= ~CONVC; vp->tval |= CONVO; } else { /* CONVFMT */ vp->tval &= ~CONVO; vp->tval |= CONVC; } vp->fmt = *fmt; } else if ((vp->tval & DONTFREE) != 0 || ! isnum(vp) || isfld(vp)) { goto done; } else if (isstr(vp)) { if (fmt == OFMT) { if ((vp->tval & CONVC) != 0 || ((vp->tval & CONVO) != 0 && vp->fmt != *fmt)) { update_str_val(vp); vp->tval &= ~CONVC; vp->tval |= CONVO; vp->fmt = *fmt; } } else { /* CONVFMT */ if ((vp->tval & CONVO) != 0 || ((vp->tval & CONVC) != 0 && vp->fmt != *fmt)) { update_str_val(vp); vp->tval &= ~CONVO; vp->tval |= CONVC; vp->fmt = *fmt; } } } done: DPRINTF("getsval %p: %s = \"%s (%p)\", t=%o\n", (void*)vp, NN(vp->nval), vp->sval, (void*)vp->sval, vp->tval); return(vp->sval); } char *getsval(Cell *vp) /* get string val of a Cell */ { return get_str_val(vp, CONVFMT); } char *getpssval(Cell *vp) /* get string val of a Cell for print */ { return get_str_val(vp, OFMT); } char *tostring(const char *s) /* make a copy of string s */ { char *p = strdup(s); if (p == NULL) FATAL("out of space in tostring on %s", s); return(p); } char *tostringN(const char *s, size_t n) /* make a copy of string s */ { char *p; p = (char *) malloc(n); if (p == NULL) FATAL("out of space in tostring on %s", s); strcpy(p, s); return(p); } Cell *catstr(Cell *a, Cell *b) /* concatenate a and b */ { Cell *c; char *p; char *sa = getsval(a); char *sb = getsval(b); size_t l = strlen(sa) + strlen(sb) + 1; p = (char *) malloc(l); if (p == NULL) FATAL("out of space concatenating %s and %s", sa, sb); snprintf(p, l, "%s%s", sa, sb); l++; // add room for ' ' char *newbuf = (char *) malloc(l); if (newbuf == NULL) FATAL("out of space concatenating %s and %s", sa, sb); // See string() in lex.c; a string "xx" is stored in the symbol // table as "xx ". snprintf(newbuf, l, "%s ", p); c = setsymtab(newbuf, p, 0.0, CON|STR|DONTFREE, symtab); free(p); free(newbuf); return c; } char *qstring(const char *is, int delim) /* collect string up to next delim */ { const char *os = is; int c, n; const uschar *s = (const uschar *) is; uschar *buf, *bp; if ((buf = (uschar *) malloc(strlen(is)+3)) == NULL) FATAL( "out of space in qstring(%s)", s); for (bp = buf; (c = *s) != delim; s++) { if (c == '\n') SYNTAX( "newline in string %.20s...", os ); else if (c != '\\') *bp++ = c; else { /* \something */ c = *++s; if (c == 0) { /* \ at end */ *bp++ = '\\'; break; /* for loop */ } switch (c) { case '\\': *bp++ = '\\'; break; case 'n': *bp++ = '\n'; break; case 't': *bp++ = '\t'; break; case 'b': *bp++ = '\b'; break; case 'f': *bp++ = '\f'; break; case 'r': *bp++ = '\r'; break; case 'v': *bp++ = '\v'; break; case 'a': *bp++ = '\a'; break; default: if (!isdigit(c)) { *bp++ = c; break; } n = c - '0'; if (isdigit(s[1])) { n = 8 * n + *++s - '0'; if (isdigit(s[1])) n = 8 * n + *++s - '0'; } *bp++ = n; break; } } } *bp++ = 0; return (char *) buf; } const char *flags2str(int flags) { static const struct ftab { const char *name; int value; } flagtab[] = { { "NUM", NUM }, { "STR", STR }, { "DONTFREE", DONTFREE }, { "CON", CON }, { "ARR", ARR }, { "FCN", FCN }, { "FLD", FLD }, { "REC", REC }, { "CONVC", CONVC }, { "CONVO", CONVO }, { NULL, 0 } }; static char buf[100]; int i; char *cp = buf; for (i = 0; flagtab[i].name != NULL; i++) { if ((flags & flagtab[i].value) != 0) { if (cp > buf) *cp++ = '|'; strcpy(cp, flagtab[i].name); cp += strlen(cp); } } return buf; }
18,760
650
jart/cosmopolitan
false
cosmopolitan/third_party/awk/awkgram.y
/**************************************************************** Copyright (C) Lucent Technologies 1997 All Rights Reserved Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that the copyright notice and this permission notice and warranty disclaimer appear in supporting documentation, and that the name Lucent Technologies or any of its entities not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ****************************************************************/ %{ #include "libc/calls/calls.h" #include "libc/fmt/fmt.h" #include "libc/stdio/lock.internal.h" #include "libc/stdio/stdio.h" #include "libc/stdio/temp.h" #include "libc/mem/alg.h" #include "libc/str/str.h" #include "third_party/awk/awk.h" void checkdup(Node *list, Cell *item); int yywrap(void) { return(1); } Node *beginloc = 0; Node *endloc = 0; bool infunc = false; /* = true if in arglist or body of func */ int inloop = 0; /* >= 1 if in while, for, do; can't be bool, since loops can next */ char *curfname = 0; /* current function name */ Node *arglist = 0; /* list of args for current function */ %} %union { Node *p; Cell *cp; int i; char *s; } %token <i> FIRSTTOKEN /* must be first */ %token <p> PROGRAM PASTAT PASTAT2 XBEGIN XEND %token <i> NL ',' '{' '(' '|' ';' '/' ')' '}' '[' ']' %token <i> ARRAY %token <i> MATCH NOTMATCH MATCHOP %token <i> FINAL DOT ALL CCL NCCL CHAR OR STAR QUEST PLUS EMPTYRE ZERO %token <i> AND BOR APPEND EQ GE GT LE LT NE IN %token <i> ARG BLTIN BREAK CLOSE CONTINUE DELETE DO EXIT FOR FUNC %token <i> SUB GSUB IF INDEX LSUBSTR MATCHFCN NEXT NEXTFILE %token <i> ADD MINUS MULT DIVIDE MOD %token <i> ASSIGN ASGNOP ADDEQ SUBEQ MULTEQ DIVEQ MODEQ POWEQ %token <i> PRINT PRINTF SPRINTF %token <p> ELSE INTEST CONDEXPR %token <i> POSTINCR PREINCR POSTDECR PREDECR %token <cp> VAR IVAR VARNF CALL NUMBER STRING %token <s> REGEXPR %type <p> pas pattern ppattern plist pplist patlist prarg term re %type <p> pa_pat pa_stat pa_stats %type <s> reg_expr %type <p> simple_stmt opt_simple_stmt stmt stmtlist %type <p> var varname funcname varlist %type <p> for if else while %type <i> do st %type <i> pst opt_pst lbrace rbrace rparen comma nl opt_nl and bor %type <i> subop print %type <cp> string %right ASGNOP %right '?' %right ':' %left BOR %left AND %left GETLINE %nonassoc APPEND EQ GE GT LE LT NE MATCHOP IN '|' %left ARG BLTIN BREAK CALL CLOSE CONTINUE DELETE DO EXIT FOR FUNC %left GSUB IF INDEX LSUBSTR MATCHFCN NEXT NUMBER %left PRINT PRINTF RETURN SPLIT SPRINTF STRING SUB SUBSTR %left REGEXPR VAR VARNF IVAR WHILE '(' %left CAT %left '+' '-' %left '*' '/' '%' %left NOT UMINUS UPLUS %right POWER %right DECR INCR %left INDIRECT %token LASTTOKEN /* must be last */ %% program: pas { if (errorflag==0) winner = (Node *)stat3(PROGRAM, beginloc, $1, endloc); } | error { yyclearin; bracecheck(); SYNTAX("bailing out"); } ; and: AND | and NL ; bor: BOR | bor NL ; comma: ',' | comma NL ; do: DO | do NL ; else: ELSE | else NL ; for: FOR '(' opt_simple_stmt ';' opt_nl pattern ';' opt_nl opt_simple_stmt rparen {inloop++;} stmt { --inloop; $$ = stat4(FOR, $3, notnull($6), $9, $12); } | FOR '(' opt_simple_stmt ';' ';' opt_nl opt_simple_stmt rparen {inloop++;} stmt { --inloop; $$ = stat4(FOR, $3, NIL, $7, $10); } | FOR '(' varname IN varname rparen {inloop++;} stmt { --inloop; $$ = stat3(IN, $3, makearr($5), $8); } ; funcname: VAR { setfname($1); } | CALL { setfname($1); } ; if: IF '(' pattern rparen { $$ = notnull($3); } ; lbrace: '{' | lbrace NL ; nl: NL | nl NL ; opt_nl: /* empty */ { $$ = 0; } | nl ; opt_pst: /* empty */ { $$ = 0; } | pst ; opt_simple_stmt: /* empty */ { $$ = 0; } | simple_stmt ; pas: opt_pst { $$ = 0; } | opt_pst pa_stats opt_pst { $$ = $2; } ; pa_pat: pattern { $$ = notnull($1); } ; pa_stat: pa_pat { $$ = stat2(PASTAT, $1, stat2(PRINT, rectonode(), NIL)); } | pa_pat lbrace stmtlist '}' { $$ = stat2(PASTAT, $1, $3); } | pa_pat ',' opt_nl pa_pat { $$ = pa2stat($1, $4, stat2(PRINT, rectonode(), NIL)); } | pa_pat ',' opt_nl pa_pat lbrace stmtlist '}' { $$ = pa2stat($1, $4, $6); } | lbrace stmtlist '}' { $$ = stat2(PASTAT, NIL, $2); } | XBEGIN lbrace stmtlist '}' { beginloc = linkum(beginloc, $3); $$ = 0; } | XEND lbrace stmtlist '}' { endloc = linkum(endloc, $3); $$ = 0; } | FUNC funcname '(' varlist rparen {infunc = true;} lbrace stmtlist '}' { infunc = false; curfname=0; defn((Cell *)$2, $4, $8); $$ = 0; } ; pa_stats: pa_stat | pa_stats opt_pst pa_stat { $$ = linkum($1, $3); } ; patlist: pattern | patlist comma pattern { $$ = linkum($1, $3); } ; ppattern: var ASGNOP ppattern { $$ = op2($2, $1, $3); } | ppattern '?' ppattern ':' ppattern %prec '?' { $$ = op3(CONDEXPR, notnull($1), $3, $5); } | ppattern bor ppattern %prec BOR { $$ = op2(BOR, notnull($1), notnull($3)); } | ppattern and ppattern %prec AND { $$ = op2(AND, notnull($1), notnull($3)); } | ppattern MATCHOP reg_expr { $$ = op3($2, NIL, $1, (Node*)makedfa($3, 0)); } | ppattern MATCHOP ppattern { if (constnode($3)) $$ = op3($2, NIL, $1, (Node*)makedfa(strnode($3), 0)); else $$ = op3($2, (Node *)1, $1, $3); } | ppattern IN varname { $$ = op2(INTEST, $1, makearr($3)); } | '(' plist ')' IN varname { $$ = op2(INTEST, $2, makearr($5)); } | ppattern term %prec CAT { $$ = op2(CAT, $1, $2); } | re | term ; pattern: var ASGNOP pattern { $$ = op2($2, $1, $3); } | pattern '?' pattern ':' pattern %prec '?' { $$ = op3(CONDEXPR, notnull($1), $3, $5); } | pattern bor pattern %prec BOR { $$ = op2(BOR, notnull($1), notnull($3)); } | pattern and pattern %prec AND { $$ = op2(AND, notnull($1), notnull($3)); } | pattern EQ pattern { $$ = op2($2, $1, $3); } | pattern GE pattern { $$ = op2($2, $1, $3); } | pattern GT pattern { $$ = op2($2, $1, $3); } | pattern LE pattern { $$ = op2($2, $1, $3); } | pattern LT pattern { $$ = op2($2, $1, $3); } | pattern NE pattern { $$ = op2($2, $1, $3); } | pattern MATCHOP reg_expr { $$ = op3($2, NIL, $1, (Node*)makedfa($3, 0)); } | pattern MATCHOP pattern { if (constnode($3)) $$ = op3($2, NIL, $1, (Node*)makedfa(strnode($3), 0)); else $$ = op3($2, (Node *)1, $1, $3); } | pattern IN varname { $$ = op2(INTEST, $1, makearr($3)); } | '(' plist ')' IN varname { $$ = op2(INTEST, $2, makearr($5)); } | pattern '|' GETLINE var { if (safe) SYNTAX("cmd | getline is unsafe"); else $$ = op3(GETLINE, $4, itonp($2), $1); } | pattern '|' GETLINE { if (safe) SYNTAX("cmd | getline is unsafe"); else $$ = op3(GETLINE, (Node*)0, itonp($2), $1); } | pattern term %prec CAT { $$ = op2(CAT, $1, $2); } | re | term ; plist: pattern comma pattern { $$ = linkum($1, $3); } | plist comma pattern { $$ = linkum($1, $3); } ; pplist: ppattern | pplist comma ppattern { $$ = linkum($1, $3); } ; prarg: /* empty */ { $$ = rectonode(); } | pplist | '(' plist ')' { $$ = $2; } ; print: PRINT | PRINTF ; pst: NL | ';' | pst NL | pst ';' ; rbrace: '}' | rbrace NL ; re: reg_expr { $$ = op3(MATCH, NIL, rectonode(), (Node*)makedfa($1, 0)); } | NOT re { $$ = op1(NOT, notnull($2)); } ; reg_expr: '/' {startreg();} REGEXPR '/' { $$ = $3; } ; rparen: ')' | rparen NL ; simple_stmt: print prarg '|' term { if (safe) SYNTAX("print | is unsafe"); else $$ = stat3($1, $2, itonp($3), $4); } | print prarg APPEND term { if (safe) SYNTAX("print >> is unsafe"); else $$ = stat3($1, $2, itonp($3), $4); } | print prarg GT term { if (safe) SYNTAX("print > is unsafe"); else $$ = stat3($1, $2, itonp($3), $4); } | print prarg { $$ = stat3($1, $2, NIL, NIL); } | DELETE varname '[' patlist ']' { $$ = stat2(DELETE, makearr($2), $4); } | DELETE varname { $$ = stat2(DELETE, makearr($2), 0); } | pattern { $$ = exptostat($1); } | error { yyclearin; SYNTAX("illegal statement"); } ; st: nl | ';' opt_nl ; stmt: BREAK st { if (!inloop) SYNTAX("break illegal outside of loops"); $$ = stat1(BREAK, NIL); } | CONTINUE st { if (!inloop) SYNTAX("continue illegal outside of loops"); $$ = stat1(CONTINUE, NIL); } | do {inloop++;} stmt {--inloop;} WHILE '(' pattern ')' st { $$ = stat2(DO, $3, notnull($7)); } | EXIT pattern st { $$ = stat1(EXIT, $2); } | EXIT st { $$ = stat1(EXIT, NIL); } | for | if stmt else stmt { $$ = stat3(IF, $1, $2, $4); } | if stmt { $$ = stat3(IF, $1, $2, NIL); } | lbrace stmtlist rbrace { $$ = $2; } | NEXT st { if (infunc) SYNTAX("next is illegal inside a function"); $$ = stat1(NEXT, NIL); } | NEXTFILE st { if (infunc) SYNTAX("nextfile is illegal inside a function"); $$ = stat1(NEXTFILE, NIL); } | RETURN pattern st { $$ = stat1(RETURN, $2); } | RETURN st { $$ = stat1(RETURN, NIL); } | simple_stmt st | while {inloop++;} stmt { --inloop; $$ = stat2(WHILE, $1, $3); } | ';' opt_nl { $$ = 0; } ; stmtlist: stmt | stmtlist stmt { $$ = linkum($1, $2); } ; subop: SUB | GSUB ; string: STRING | string STRING { $$ = catstr($1, $2); } ; term: term '/' ASGNOP term { $$ = op2(DIVEQ, $1, $4); } | term '+' term { $$ = op2(ADD, $1, $3); } | term '-' term { $$ = op2(MINUS, $1, $3); } | term '*' term { $$ = op2(MULT, $1, $3); } | term '/' term { $$ = op2(DIVIDE, $1, $3); } | term '%' term { $$ = op2(MOD, $1, $3); } | term POWER term { $$ = op2(POWER, $1, $3); } | '-' term %prec UMINUS { $$ = op1(UMINUS, $2); } | '+' term %prec UMINUS { $$ = op1(UPLUS, $2); } | NOT term %prec UMINUS { $$ = op1(NOT, notnull($2)); } | BLTIN '(' ')' { $$ = op2(BLTIN, itonp($1), rectonode()); } | BLTIN '(' patlist ')' { $$ = op2(BLTIN, itonp($1), $3); } | BLTIN { $$ = op2(BLTIN, itonp($1), rectonode()); } | CALL '(' ')' { $$ = op2(CALL, celltonode($1,CVAR), NIL); } | CALL '(' patlist ')' { $$ = op2(CALL, celltonode($1,CVAR), $3); } | CLOSE term { $$ = op1(CLOSE, $2); } | DECR var { $$ = op1(PREDECR, $2); } | INCR var { $$ = op1(PREINCR, $2); } | var DECR { $$ = op1(POSTDECR, $1); } | var INCR { $$ = op1(POSTINCR, $1); } | GETLINE var LT term { $$ = op3(GETLINE, $2, itonp($3), $4); } | GETLINE LT term { $$ = op3(GETLINE, NIL, itonp($2), $3); } | GETLINE var { $$ = op3(GETLINE, $2, NIL, NIL); } | GETLINE { $$ = op3(GETLINE, NIL, NIL, NIL); } | INDEX '(' pattern comma pattern ')' { $$ = op2(INDEX, $3, $5); } | INDEX '(' pattern comma reg_expr ')' { SYNTAX("index() doesn't permit regular expressions"); $$ = op2(INDEX, $3, (Node*)$5); } | '(' pattern ')' { $$ = $2; } | MATCHFCN '(' pattern comma reg_expr ')' { $$ = op3(MATCHFCN, NIL, $3, (Node*)makedfa($5, 1)); } | MATCHFCN '(' pattern comma pattern ')' { if (constnode($5)) $$ = op3(MATCHFCN, NIL, $3, (Node*)makedfa(strnode($5), 1)); else $$ = op3(MATCHFCN, (Node *)1, $3, $5); } | NUMBER { $$ = celltonode($1, CCON); } | SPLIT '(' pattern comma varname comma pattern ')' /* string */ { $$ = op4(SPLIT, $3, makearr($5), $7, (Node*)STRING); } | SPLIT '(' pattern comma varname comma reg_expr ')' /* const /regexp/ */ { $$ = op4(SPLIT, $3, makearr($5), (Node*)makedfa($7, 1), (Node *)REGEXPR); } | SPLIT '(' pattern comma varname ')' { $$ = op4(SPLIT, $3, makearr($5), NIL, (Node*)STRING); } /* default */ | SPRINTF '(' patlist ')' { $$ = op1($1, $3); } | string { $$ = celltonode($1, CCON); } | subop '(' reg_expr comma pattern ')' { $$ = op4($1, NIL, (Node*)makedfa($3, 1), $5, rectonode()); } | subop '(' pattern comma pattern ')' { if (constnode($3)) $$ = op4($1, NIL, (Node*)makedfa(strnode($3), 1), $5, rectonode()); else $$ = op4($1, (Node *)1, $3, $5, rectonode()); } | subop '(' reg_expr comma pattern comma var ')' { $$ = op4($1, NIL, (Node*)makedfa($3, 1), $5, $7); } | subop '(' pattern comma pattern comma var ')' { if (constnode($3)) $$ = op4($1, NIL, (Node*)makedfa(strnode($3), 1), $5, $7); else $$ = op4($1, (Node *)1, $3, $5, $7); } | SUBSTR '(' pattern comma pattern comma pattern ')' { $$ = op3(SUBSTR, $3, $5, $7); } | SUBSTR '(' pattern comma pattern ')' { $$ = op3(SUBSTR, $3, $5, NIL); } | var ; var: varname | varname '[' patlist ']' { $$ = op2(ARRAY, makearr($1), $3); } | IVAR { $$ = op1(INDIRECT, celltonode($1, CVAR)); } | INDIRECT term { $$ = op1(INDIRECT, $2); } ; varlist: /* nothing */ { arglist = $$ = 0; } | VAR { arglist = $$ = celltonode($1,CVAR); } | varlist comma VAR { checkdup($1, $3); arglist = $$ = linkum($1,celltonode($3,CVAR)); } ; varname: VAR { $$ = celltonode($1, CVAR); } | ARG { $$ = op1(ARG, itonp($1)); } | VARNF { $$ = op1(VARNF, (Node *) $1); } ; while: WHILE '(' pattern rparen { $$ = notnull($3); } ; %% void setfname(Cell *p) { if (isarr(p)) SYNTAX("%s is an array, not a function", p->nval); else if (isfcn(p)) SYNTAX("you can't define function %s more than once", p->nval); curfname = p->nval; } int constnode(Node *p) { return isvalue(p) && ((Cell *) (p->narg[0]))->csub == CCON; } char *strnode(Node *p) { return ((Cell *)(p->narg[0]))->sval; } Node *notnull(Node *n) { switch (n->nobj) { case LE: case LT: case EQ: case NE: case GT: case GE: case BOR: case AND: case NOT: return n; default: return op2(NE, n, nullnode); } } void checkdup(Node *vl, Cell *cp) /* check if name already in list */ { char *s = cp->nval; for ( ; vl; vl = vl->nnext) { if (strcmp(s, ((Cell *)(vl->narg[0]))->nval) == 0) { SYNTAX("duplicate argument %s", s); break; } } }
14,253
498
jart/cosmopolitan
false
cosmopolitan/third_party/awk/reflow.awk
# fmt - format # input: text # output: text formatted into lines of <= 72 characters BEGIN { maxlen = 72 } /^[ \t]/ { printline(); print; next } # verbatim ###/^ +/ { printline(); } # whitespace == break /./ { for (i = 1; i <= NF; i++) addword($i); next } /^$/ { printline(); print "" } END { printline() } function addword(w) { ## print "adding [", w, "] ", length(w), length(line), maxlen if (length(line) + length(w) > maxlen) printline() if (length(w) > 2 && ( w ~ /[\.!]["?)]?$/ || w ~ /[?!]"?$/) && w !~ /^(Mr|Dr|Ms|Mrs|vs|Ph.D)\.$/) w = w " " line = line " " w } function printline() { if (length(line) > 0) { sub(/ +$/, "", line) print substr(line, 2) # removes leading blank line = "" } }
797
34
jart/cosmopolitan
false
cosmopolitan/third_party/awk/parse.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Copyright (C) Lucent Technologies 1997 │ │ All Rights Reserved │ │ │ │ Permission to use, copy, modify, and distribute this software and │ │ its documentation for any purpose and without fee is hereby │ │ granted, provided that the above copyright notice appear in all │ │ copies and that both that the copyright notice and this │ │ permission notice and warranty disclaimer appear in supporting │ │ documentation, and that the name Lucent Technologies or any of │ │ its entities not be used in advertising or publicity pertaining │ │ to distribution of the software without specific, written prior │ │ permission. │ │ │ │ LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, │ │ INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. │ │ IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY │ │ SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES │ │ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER │ │ IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, │ │ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF │ │ THIS SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #define DEBUG #include "libc/mem/mem.h" #include "libc/str/str.h" #include "third_party/awk/awk.h" #include "third_party/awk/awkgram.tab.h" // clang-format off Node *nodealloc(int n) { Node *x; x = (Node *) malloc(sizeof(*x) + (n-1) * sizeof(x)); if (x == NULL) FATAL("out of space in nodealloc"); x->nnext = NULL; x->lineno = lineno; return(x); } Node *exptostat(Node *a) { a->ntype = NSTAT; return(a); } Node *node1(int a, Node *b) { Node *x; x = nodealloc(1); x->nobj = a; x->narg[0]=b; return(x); } Node *node2(int a, Node *b, Node *c) { Node *x; x = nodealloc(2); x->nobj = a; x->narg[0] = b; x->narg[1] = c; return(x); } Node *node3(int a, Node *b, Node *c, Node *d) { Node *x; x = nodealloc(3); x->nobj = a; x->narg[0] = b; x->narg[1] = c; x->narg[2] = d; return(x); } Node *node4(int a, Node *b, Node *c, Node *d, Node *e) { Node *x; x = nodealloc(4); x->nobj = a; x->narg[0] = b; x->narg[1] = c; x->narg[2] = d; x->narg[3] = e; return(x); } Node *stat1(int a, Node *b) { Node *x; x = node1(a,b); x->ntype = NSTAT; return(x); } Node *stat2(int a, Node *b, Node *c) { Node *x; x = node2(a,b,c); x->ntype = NSTAT; return(x); } Node *stat3(int a, Node *b, Node *c, Node *d) { Node *x; x = node3(a,b,c,d); x->ntype = NSTAT; return(x); } Node *stat4(int a, Node *b, Node *c, Node *d, Node *e) { Node *x; x = node4(a,b,c,d,e); x->ntype = NSTAT; return(x); } Node *op1(int a, Node *b) { Node *x; x = node1(a,b); x->ntype = NEXPR; return(x); } Node *op2(int a, Node *b, Node *c) { Node *x; x = node2(a,b,c); x->ntype = NEXPR; return(x); } Node *op3(int a, Node *b, Node *c, Node *d) { Node *x; x = node3(a,b,c,d); x->ntype = NEXPR; return(x); } Node *op4(int a, Node *b, Node *c, Node *d, Node *e) { Node *x; x = node4(a,b,c,d,e); x->ntype = NEXPR; return(x); } Node *celltonode(Cell *a, int b) { Node *x; a->ctype = OCELL; a->csub = b; x = node1(0, (Node *) a); x->ntype = NVALUE; return(x); } Node *rectonode(void) /* make $0 into a Node */ { extern Cell *literal0; return op1(INDIRECT, celltonode(literal0, CUNK)); } Node *makearr(Node *p) { Cell *cp; if (isvalue(p)) { cp = (Cell *) (p->narg[0]); if (isfcn(cp)) SYNTAX( "%s is a function, not an array", cp->nval ); else if (!isarr(cp)) { xfree(cp->sval); cp->sval = (char *) makesymtab(NSYMTAB); cp->tval = ARR; } } return p; } #define PA2NUM 50 /* max number of pat,pat patterns allowed */ int paircnt; /* number of them in use */ int pairstack[PA2NUM]; /* state of each pat,pat */ Node *pa2stat(Node *a, Node *b, Node *c) /* pat, pat {...} */ { Node *x; x = node4(PASTAT2, a, b, c, itonp(paircnt)); if (paircnt++ >= PA2NUM) SYNTAX( "limited to %d pat,pat statements", PA2NUM ); x->ntype = NSTAT; return(x); } Node *linkum(Node *a, Node *b) { Node *c; if (errorflag) /* don't link things that are wrong */ return a; if (a == NULL) return(b); else if (b == NULL) return(a); for (c = a; c->nnext != NULL; c = c->nnext) ; c->nnext = b; return(a); } void defn(Cell *v, Node *vl, Node *st) /* turn on FCN bit in definition, */ { /* body of function, arglist */ Node *p; int n; if (isarr(v)) { SYNTAX( "`%s' is an array name and a function name", v->nval ); return; } if (isarg(v->nval) != -1) { SYNTAX( "`%s' is both function name and argument name", v->nval ); return; } v->tval = FCN; v->sval = (char *) st; n = 0; /* count arguments */ for (p = vl; p; p = p->nnext) n++; v->fval = n; DPRINTF("defining func %s (%d args)\n", v->nval, n); } int isarg(const char *s) /* is s in argument list for current function? */ { /* return -1 if not, otherwise arg # */ extern Node *arglist; Node *p = arglist; int n; for (n = 0; p != NULL; p = p->nnext, n++) if (strcmp(((Cell *)(p->narg[0]))->nval, s) == 0) return n; return -1; } int ptoi(void *p) /* convert pointer to integer */ { return (int) (long) p; /* swearing that p fits, of course */ } Node *itonp(int i) /* and vice versa */ { return (Node *) (long) i; }
6,670
280
jart/cosmopolitan
false
cosmopolitan/third_party/awk/cmd.h
#ifndef COSMOPOLITAN_THIRD_PARTY_AWK_CMD_H_ #define COSMOPOLITAN_THIRD_PARTY_AWK_CMD_H_ #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ int _awk(int, char *[]); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_THIRD_PARTY_AWK_CMD_H_ */
291
11
jart/cosmopolitan
false
cosmopolitan/third_party/awk/b.c
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ │vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Copyright (C) Lucent Technologies 1997 │ │ All Rights Reserved │ │ │ │ Permission to use, copy, modify, and distribute this software and │ │ its documentation for any purpose and without fee is hereby │ │ granted, provided that the above copyright notice appear in all │ │ copies and that both that the copyright notice and this │ │ permission notice and warranty disclaimer appear in supporting │ │ documentation, and that the name Lucent Technologies or any of │ │ its entities not be used in advertising or publicity pertaining │ │ to distribution of the software without specific, written prior │ │ permission. │ │ │ │ LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, │ │ INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. │ │ IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY │ │ SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES │ │ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER │ │ IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, │ │ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF │ │ THIS SOFTWARE. │ │ │ ╚─────────────────────────────────────────────────────────────────────────────*/ #define DEBUG #include "libc/calls/calls.h" #include "libc/mem/mem.h" #include "libc/str/str.h" #include "third_party/awk/awk.h" #include "third_party/awk/awkgram.tab.h" // clang-format off /* lasciate ogne speranza, voi ch'intrate. */ #define MAXLIN 22 #define type(v) (v)->nobj /* badly overloaded here */ #define info(v) (v)->ntype /* badly overloaded here */ #define left(v) (v)->narg[0] #define right(v) (v)->narg[1] #define parent(v) (v)->nnext #define LEAF case CCL: case NCCL: case CHAR: case DOT: case FINAL: case ALL: #define ELEAF case EMPTYRE: /* empty string in regexp */ #define UNARY case STAR: case PLUS: case QUEST: /* encoding in tree Nodes: leaf (CCL, NCCL, CHAR, DOT, FINAL, ALL, EMPTYRE): left is index, right contains value or pointer to value unary (STAR, PLUS, QUEST): left is child, right is null binary (CAT, OR): left and right are children parent contains pointer to parent */ int *setvec; int *tmpset; int maxsetvec = 0; int rtok; /* next token in current re */ int rlxval; static const uschar *rlxstr; static const uschar *prestr; /* current position in current re */ static const uschar *lastre; /* origin of last re */ static const uschar *lastatom; /* origin of last Atom */ static const uschar *starttok; static const uschar *basestr; /* starts with original, replaced during repetition processing */ static const uschar *firstbasestr; static int setcnt; static int poscnt; const char *patbeg; int patlen; #define NFA 128 /* cache this many dynamic fa's */ fa *fatab[NFA]; int nfatab = 0; /* entries in fatab */ static int * intalloc(size_t n, const char *f) { int *p = (int *) calloc(n, sizeof(int)); if (p == NULL) overflo(f); return p; } static void resizesetvec(const char *f) { if (maxsetvec == 0) maxsetvec = MAXLIN; else maxsetvec *= 4; setvec = (int *) realloc(setvec, maxsetvec * sizeof(*setvec)); tmpset = (int *) realloc(tmpset, maxsetvec * sizeof(*tmpset)); if (setvec == NULL || tmpset == NULL) overflo(f); } static void resize_state(fa *f, int state) { unsigned int **p; uschar *p2; int **p3; int i, new_count; if (++state < f->state_count) return; new_count = state + 10; /* needs to be tuned */ p = (unsigned int **) realloc(f->gototab, new_count * sizeof(f->gototab[0])); if (p == NULL) goto out; f->gototab = p; p2 = (uschar *) realloc(f->out, new_count * sizeof(f->out[0])); if (p2 == NULL) goto out; f->out = p2; p3 = (int **) realloc(f->posns, new_count * sizeof(f->posns[0])); if (p3 == NULL) goto out; f->posns = p3; for (i = f->state_count; i < new_count; ++i) { f->gototab[i] = (unsigned int *) calloc(NCHARS, sizeof(**f->gototab)); if (f->gototab[i] == NULL) goto out; f->out[i] = 0; f->posns[i] = NULL; } f->state_count = new_count; return; out: overflo(__func__); } fa *makedfa(const char *s, bool anchor) /* returns dfa for reg expr s */ { int i, use, nuse; fa *pfa; static int now = 1; if (setvec == NULL) { /* first time through any RE */ resizesetvec(__func__); } if (compile_time != RUNNING) /* a constant for sure */ return mkdfa(s, anchor); for (i = 0; i < nfatab; i++) /* is it there already? */ if (fatab[i]->anchor == anchor && strcmp((const char *) fatab[i]->restr, s) == 0) { fatab[i]->use = now++; return fatab[i]; } pfa = mkdfa(s, anchor); if (nfatab < NFA) { /* room for another */ fatab[nfatab] = pfa; fatab[nfatab]->use = now++; nfatab++; return pfa; } use = fatab[0]->use; /* replace least-recently used */ nuse = 0; for (i = 1; i < nfatab; i++) if (fatab[i]->use < use) { use = fatab[i]->use; nuse = i; } freefa(fatab[nuse]); fatab[nuse] = pfa; pfa->use = now++; return pfa; } fa *mkdfa(const char *s, bool anchor) /* does the real work of making a dfa */ /* anchor = true for anchored matches, else false */ { Node *p, *p1; fa *f; firstbasestr = (const uschar *) s; basestr = firstbasestr; p = reparse(s); p1 = op2(CAT, op2(STAR, op2(ALL, NIL, NIL), NIL), p); /* put ALL STAR in front of reg. exp. */ p1 = op2(CAT, p1, op2(FINAL, NIL, NIL)); /* put FINAL after reg. exp. */ poscnt = 0; penter(p1); /* enter parent pointers and leaf indices */ if ((f = (fa *) calloc(1, sizeof(fa) + poscnt * sizeof(rrow))) == NULL) overflo(__func__); f->accept = poscnt-1; /* penter has computed number of positions in re */ cfoll(f, p1); /* set up follow sets */ freetr(p1); resize_state(f, 1); f->posns[0] = intalloc(*(f->re[0].lfollow), __func__); f->posns[1] = intalloc(1, __func__); *f->posns[1] = 0; f->initstat = makeinit(f, anchor); f->anchor = anchor; f->restr = (uschar *) tostring(s); if (firstbasestr != basestr) { if (basestr) xfree(basestr); } return f; } int makeinit(fa *f, bool anchor) { int i, k; f->curstat = 2; f->out[2] = 0; k = *(f->re[0].lfollow); xfree(f->posns[2]); f->posns[2] = intalloc(k + 1, __func__); for (i = 0; i <= k; i++) { (f->posns[2])[i] = (f->re[0].lfollow)[i]; } if ((f->posns[2])[1] == f->accept) f->out[2] = 1; for (i = 0; i < NCHARS; i++) f->gototab[2][i] = 0; f->curstat = cgoto(f, 2, HAT); if (anchor) { *f->posns[2] = k-1; /* leave out position 0 */ for (i = 0; i < k; i++) { (f->posns[0])[i] = (f->posns[2])[i]; } f->out[0] = f->out[2]; if (f->curstat != 2) --(*f->posns[f->curstat]); } return f->curstat; } void penter(Node *p) /* set up parent pointers and leaf indices */ { switch (type(p)) { ELEAF LEAF info(p) = poscnt; poscnt++; break; UNARY penter(left(p)); parent(left(p)) = p; break; case CAT: case OR: penter(left(p)); penter(right(p)); parent(left(p)) = p; parent(right(p)) = p; break; case ZERO: break; default: /* can't happen */ FATAL("can't happen: unknown type %d in penter", type(p)); break; } } void freetr(Node *p) /* free parse tree */ { switch (type(p)) { ELEAF LEAF xfree(p); break; UNARY case ZERO: freetr(left(p)); xfree(p); break; case CAT: case OR: freetr(left(p)); freetr(right(p)); xfree(p); break; default: /* can't happen */ FATAL("can't happen: unknown type %d in freetr", type(p)); break; } } /* in the parsing of regular expressions, metacharacters like . have */ /* to be seen literally; \056 is not a metacharacter. */ int hexstr(const uschar **pp) /* find and eval hex string at pp, return new p */ { /* only pick up one 8-bit byte (2 chars) */ const uschar *p; int n = 0; int i; for (i = 0, p = *pp; i < 2 && isxdigit(*p); i++, p++) { if (isdigit(*p)) n = 16 * n + *p - '0'; else if (*p >= 'a' && *p <= 'f') n = 16 * n + *p - 'a' + 10; else if (*p >= 'A' && *p <= 'F') n = 16 * n + *p - 'A' + 10; } *pp = p; return n; } #define isoctdigit(c) ((c) >= '0' && (c) <= '7') /* multiple use of arg */ int quoted(const uschar **pp) /* pick up next thing after a \\ */ /* and increment *pp */ { const uschar *p = *pp; int c; if ((c = *p++) == 't') c = '\t'; else if (c == 'n') c = '\n'; else if (c == 'f') c = '\f'; else if (c == 'r') c = '\r'; else if (c == 'b') c = '\b'; else if (c == 'v') c = '\v'; else if (c == 'a') c = '\a'; else if (c == '\\') c = '\\'; else if (c == 'x') { /* hexadecimal goo follows */ c = hexstr(&p); /* this adds a null if number is invalid */ } else if (isoctdigit(c)) { /* \d \dd \ddd */ int n = c - '0'; if (isoctdigit(*p)) { n = 8 * n + *p++ - '0'; if (isoctdigit(*p)) n = 8 * n + *p++ - '0'; } c = n; } /* else */ /* c = c; */ *pp = p; return c; } char *cclenter(const char *argp) /* add a character class */ { int i, c, c2; const uschar *op, *p = (const uschar *) argp; uschar *bp; static uschar *buf = NULL; static int bufsz = 100; op = p; if (buf == NULL && (buf = (uschar *) malloc(bufsz)) == NULL) FATAL("out of space for character class [%.10s...] 1", p); bp = buf; for (i = 0; (c = *p++) != 0; ) { if (c == '\\') { c = quoted(&p); } else if (c == '-' && i > 0 && bp[-1] != 0) { if (*p != 0) { c = bp[-1]; c2 = *p++; if (c2 == '\\') c2 = quoted(&p); if (c > c2) { /* empty; ignore */ bp--; i--; continue; } while (c < c2) { if (!adjbuf((char **) &buf, &bufsz, bp-buf+2, 100, (char **) &bp, "cclenter1")) FATAL("out of space for character class [%.10s...] 2", p); *bp++ = ++c; i++; } continue; } } if (!adjbuf((char **) &buf, &bufsz, bp-buf+2, 100, (char **) &bp, "cclenter2")) FATAL("out of space for character class [%.10s...] 3", p); *bp++ = c; i++; } *bp = 0; DPRINTF("cclenter: in = |%s|, out = |%s|\n", op, buf); xfree(op); return (char *) tostring((char *) buf); } void overflo(const char *s) { FATAL("regular expression too big: out of space in %.30s...", s); } void cfoll(fa *f, Node *v) /* enter follow set of each leaf of vertex v into lfollow[leaf] */ { int i; int *p; switch (type(v)) { ELEAF LEAF f->re[info(v)].ltype = type(v); f->re[info(v)].lval.np = right(v); while (f->accept >= maxsetvec) { /* guessing here! */ resizesetvec(__func__); } for (i = 0; i <= f->accept; i++) setvec[i] = 0; setcnt = 0; follow(v); /* computes setvec and setcnt */ p = intalloc(setcnt + 1, __func__); f->re[info(v)].lfollow = p; *p = setcnt; for (i = f->accept; i >= 0; i--) if (setvec[i] == 1) *++p = i; break; UNARY cfoll(f,left(v)); break; case CAT: case OR: cfoll(f,left(v)); cfoll(f,right(v)); break; case ZERO: break; default: /* can't happen */ FATAL("can't happen: unknown type %d in cfoll", type(v)); } } int first(Node *p) /* collects initially active leaves of p into setvec */ /* returns 0 if p matches empty string */ { int b, lp; switch (type(p)) { ELEAF LEAF lp = info(p); /* look for high-water mark of subscripts */ while (setcnt >= maxsetvec || lp >= maxsetvec) { /* guessing here! */ resizesetvec(__func__); } if (type(p) == EMPTYRE) { setvec[lp] = 0; return(0); } if (setvec[lp] != 1) { setvec[lp] = 1; setcnt++; } if (type(p) == CCL && (*(char *) right(p)) == '\0') return(0); /* empty CCL */ return(1); case PLUS: if (first(left(p)) == 0) return(0); return(1); case STAR: case QUEST: first(left(p)); return(0); case CAT: if (first(left(p)) == 0 && first(right(p)) == 0) return(0); return(1); case OR: b = first(right(p)); if (first(left(p)) == 0 || b == 0) return(0); return(1); case ZERO: return 0; } FATAL("can't happen: unknown type %d in first", type(p)); /* can't happen */ return(-1); } void follow(Node *v) /* collects leaves that can follow v into setvec */ { Node *p; if (type(v) == FINAL) return; p = parent(v); switch (type(p)) { case STAR: case PLUS: first(v); follow(p); return; case OR: case QUEST: follow(p); return; case CAT: if (v == left(p)) { /* v is left child of p */ if (first(right(p)) == 0) { follow(p); return; } } else /* v is right child */ follow(p); return; } } int member(int c, const char *sarg) /* is c in s? */ { const uschar *s = (const uschar *) sarg; while (*s) if (c == *s++) return(1); return(0); } int match(fa *f, const char *p0) /* shortest match ? */ { int s, ns; const uschar *p = (const uschar *) p0; s = f->initstat; assert (s < f->state_count); if (f->out[s]) return(1); do { /* assert(*p < NCHARS); */ if ((ns = f->gototab[s][*p]) != 0) s = ns; else s = cgoto(f, s, *p); if (f->out[s]) return(1); } while (*p++ != 0); return(0); } int pmatch(fa *f, const char *p0) /* longest match, for sub */ { int s, ns; const uschar *p = (const uschar *) p0; const uschar *q; s = f->initstat; assert(s < f->state_count); patbeg = (const char *)p; patlen = -1; do { q = p; do { if (f->out[s]) /* final state */ patlen = q-p; /* assert(*q < NCHARS); */ if ((ns = f->gototab[s][*q]) != 0) s = ns; else s = cgoto(f, s, *q); assert(s < f->state_count); if (s == 1) { /* no transition */ if (patlen >= 0) { patbeg = (const char *) p; return(1); } else goto nextin; /* no match */ } } while (*q++ != 0); if (f->out[s]) patlen = q-p-1; /* don't count $ */ if (patlen >= 0) { patbeg = (const char *) p; return(1); } nextin: s = 2; } while (*p++); return (0); } int nematch(fa *f, const char *p0) /* non-empty match, for sub */ { int s, ns; const uschar *p = (const uschar *) p0; const uschar *q; s = f->initstat; assert(s < f->state_count); patbeg = (const char *)p; patlen = -1; while (*p) { q = p; do { if (f->out[s]) /* final state */ patlen = q-p; /* assert(*q < NCHARS); */ if ((ns = f->gototab[s][*q]) != 0) s = ns; else s = cgoto(f, s, *q); if (s == 1) { /* no transition */ if (patlen > 0) { patbeg = (const char *) p; return(1); } else goto nnextin; /* no nonempty match */ } } while (*q++ != 0); if (f->out[s]) patlen = q-p-1; /* don't count $ */ if (patlen > 0 ) { patbeg = (const char *) p; return(1); } nnextin: s = 2; p++; } return (0); } /* * NAME * fnematch * * DESCRIPTION * A stream-fed version of nematch which transfers characters to a * null-terminated buffer. All characters up to and including the last * character of the matching text or EOF are placed in the buffer. If * a match is found, patbeg and patlen are set appropriately. * * RETURN VALUES * false No match found. * true Match found. */ bool fnematch(fa *pfa, FILE *f, char **pbuf, int *pbufsize, int quantum) { char *buf = *pbuf; int bufsize = *pbufsize; int c, i, j, k, ns, s; s = pfa->initstat; patlen = 0; /* * All indices relative to buf. * i <= j <= k <= bufsize * * i: origin of active substring * j: current character * k: destination of next getc() */ i = -1, k = 0; do { j = i++; do { if (++j == k) { if (k == bufsize) if (!adjbuf((char **) &buf, &bufsize, bufsize+1, quantum, 0, "fnematch")) FATAL("stream '%.30s...' too long", buf); buf[k++] = (c = getc(f)) != EOF ? c : 0; } c = (uschar)buf[j]; /* assert(c < NCHARS); */ if ((ns = pfa->gototab[s][c]) != 0) s = ns; else s = cgoto(pfa, s, c); if (pfa->out[s]) { /* final state */ patlen = j - i + 1; if (c == 0) /* don't count $ */ patlen--; } } while (buf[j] && s != 1); s = 2; } while (buf[i] && !patlen); /* adjbuf() may have relocated a resized buffer. Inform the world. */ *pbuf = buf; *pbufsize = bufsize; if (patlen) { patbeg = (char *) buf + i; /* * Under no circumstances is the last character fed to * the automaton part of the match. It is EOF's nullbyte, * or it sent the automaton into a state with no further * transitions available (s==1), or both. Room for a * terminating nullbyte is guaranteed. * * ungetc any chars after the end of matching text * (except for EOF's nullbyte, if present) and null * terminate the buffer. */ do if (buf[--k] && ungetc(buf[k], f) == EOF) FATAL("unable to ungetc '%c'", buf[k]); while (k > i + patlen); buf[k] = '\0'; return true; } else return false; } Node *reparse(const char *p) /* parses regular expression pointed to by p */ { /* uses relex() to scan regular expression */ Node *np; DPRINTF("reparse <%s>\n", p); lastre = prestr = (const uschar *) p; /* prestr points to string to be parsed */ rtok = relex(); /* GNU compatibility: an empty regexp matches anything */ if (rtok == '\0') { /* FATAL("empty regular expression"); previous */ return(op2(EMPTYRE, NIL, NIL)); } np = regexp(); if (rtok != '\0') FATAL("syntax error in regular expression %s at %s", lastre, prestr); return(np); } Node *regexp(void) /* top-level parse of reg expr */ { return (alt(concat(primary()))); } Node *primary(void) { Node *np; int savelastatom; switch (rtok) { case CHAR: lastatom = starttok; np = op2(CHAR, NIL, itonp(rlxval)); rtok = relex(); return (unary(np)); case ALL: rtok = relex(); return (unary(op2(ALL, NIL, NIL))); case EMPTYRE: rtok = relex(); return (unary(op2(EMPTYRE, NIL, NIL))); case DOT: lastatom = starttok; rtok = relex(); return (unary(op2(DOT, NIL, NIL))); case CCL: np = op2(CCL, NIL, (Node*) cclenter((const char *) rlxstr)); lastatom = starttok; rtok = relex(); return (unary(np)); case NCCL: np = op2(NCCL, NIL, (Node *) cclenter((const char *) rlxstr)); lastatom = starttok; rtok = relex(); return (unary(np)); case '^': rtok = relex(); return (unary(op2(CHAR, NIL, itonp(HAT)))); case '$': rtok = relex(); return (unary(op2(CHAR, NIL, NIL))); case '(': lastatom = starttok; savelastatom = starttok - basestr; /* Retain over recursion */ rtok = relex(); if (rtok == ')') { /* special pleading for () */ rtok = relex(); return unary(op2(CCL, NIL, (Node *) tostring(""))); } np = regexp(); if (rtok == ')') { lastatom = basestr + savelastatom; /* Restore */ rtok = relex(); return (unary(np)); } else FATAL("syntax error in regular expression %s at %s", lastre, prestr); default: FATAL("illegal primary in regular expression %s at %s", lastre, prestr); } return 0; /*NOTREACHED*/ } Node *concat(Node *np) { switch (rtok) { case CHAR: case DOT: case ALL: case CCL: case NCCL: case '$': case '(': return (concat(op2(CAT, np, primary()))); case EMPTYRE: rtok = relex(); return (concat(op2(CAT, op2(CCL, NIL, (Node *) tostring("")), primary()))); } return (np); } Node *alt(Node *np) { if (rtok == OR) { rtok = relex(); return (alt(op2(OR, np, concat(primary())))); } return (np); } Node *unary(Node *np) { switch (rtok) { case STAR: rtok = relex(); return (unary(op2(STAR, np, NIL))); case PLUS: rtok = relex(); return (unary(op2(PLUS, np, NIL))); case QUEST: rtok = relex(); return (unary(op2(QUEST, np, NIL))); case ZERO: rtok = relex(); return (unary(op2(ZERO, np, NIL))); default: return (np); } } /* * Character class definitions conformant to the POSIX locale as * defined in IEEE P1003.1 draft 7 of June 2001, assuming the source * and operating character sets are both ASCII (ISO646) or supersets * thereof. * * Note that to avoid overflowing the temporary buffer used in * relex(), the expanded character class (prior to range expansion) * must be less than twice the size of their full name. */ static const struct charclass { const char *cc_name; int cc_namelen; int (*cc_func)(int); } charclasses[] = { { "alnum", 5, isalnum }, { "alpha", 5, isalpha }, { "blank", 5, isblank }, { "cntrl", 5, iscntrl }, { "digit", 5, isdigit }, { "graph", 5, isgraph }, { "lower", 5, islower }, { "print", 5, isprint }, { "punct", 5, ispunct }, { "space", 5, isspace }, { "upper", 5, isupper }, { "xdigit", 6, isxdigit }, { NULL, 0, NULL }, }; #define REPEAT_SIMPLE 0 #define REPEAT_PLUS_APPENDED 1 #define REPEAT_WITH_Q 2 #define REPEAT_ZERO 3 static int replace_repeat(const uschar *reptok, int reptoklen, const uschar *atom, int atomlen, int firstnum, int secondnum, int special_case) { int i, j; uschar *buf = 0; int ret = 1; int init_q = (firstnum == 0); /* first added char will be ? */ int n_q_reps = secondnum-firstnum; /* m>n, so reduce until {1,m-n} left */ int prefix_length = reptok - basestr; /* prefix includes first rep */ int suffix_length = strlen((const char *) reptok) - reptoklen; /* string after rep specifier */ int size = prefix_length + suffix_length; if (firstnum > 1) { /* add room for reps 2 through firstnum */ size += atomlen*(firstnum-1); } /* Adjust size of buffer for special cases */ if (special_case == REPEAT_PLUS_APPENDED) { size++; /* for the final + */ } else if (special_case == REPEAT_WITH_Q) { size += init_q + (atomlen+1)* (n_q_reps-init_q); } else if (special_case == REPEAT_ZERO) { size += 2; /* just a null ERE: () */ } if ((buf = (uschar *) malloc(size + 1)) == NULL) FATAL("out of space in reg expr %.10s..", lastre); memcpy(buf, basestr, prefix_length); /* copy prefix */ j = prefix_length; if (special_case == REPEAT_ZERO) { j -= atomlen; buf[j++] = '('; buf[j++] = ')'; } for (i = 1; i < firstnum; i++) { /* copy x reps */ memcpy(&buf[j], atom, atomlen); j += atomlen; } if (special_case == REPEAT_PLUS_APPENDED) { buf[j++] = '+'; } else if (special_case == REPEAT_WITH_Q) { if (init_q) buf[j++] = '?'; for (i = init_q; i < n_q_reps; i++) { /* copy x? reps */ memcpy(&buf[j], atom, atomlen); j += atomlen; buf[j++] = '?'; } } memcpy(&buf[j], reptok+reptoklen, suffix_length); j += suffix_length; buf[j] = '\0'; /* free old basestr */ if (firstbasestr != basestr) { if (basestr) xfree(basestr); } basestr = buf; prestr = buf + prefix_length; if (special_case == REPEAT_ZERO) { prestr -= atomlen; ret++; } return ret; } static int repeat(const uschar *reptok, int reptoklen, const uschar *atom, int atomlen, int firstnum, int secondnum) { /* In general, the repetition specifier or "bound" is replaced here by an equivalent ERE string, repeating the immediately previous atom and appending ? and + as needed. Note that the first copy of the atom is left in place, except in the special_case of a zero-repeat (i.e., {0}). */ if (secondnum < 0) { /* means {n,} -> repeat n-1 times followed by PLUS */ if (firstnum < 2) { /* 0 or 1: should be handled before you get here */ FATAL("internal error"); } else { return replace_repeat(reptok, reptoklen, atom, atomlen, firstnum, secondnum, REPEAT_PLUS_APPENDED); } } else if (firstnum == secondnum) { /* {n} or {n,n} -> simply repeat n-1 times */ if (firstnum == 0) { /* {0} or {0,0} */ /* This case is unusual because the resulting replacement string might actually be SMALLER than the original ERE */ return replace_repeat(reptok, reptoklen, atom, atomlen, firstnum, secondnum, REPEAT_ZERO); } else { /* (firstnum >= 1) */ return replace_repeat(reptok, reptoklen, atom, atomlen, firstnum, secondnum, REPEAT_SIMPLE); } } else if (firstnum < secondnum) { /* {n,m} -> repeat n-1 times then alternate */ /* x{n,m} => xx...x{1, m-n+1} => xx...x?x?x?..x? */ return replace_repeat(reptok, reptoklen, atom, atomlen, firstnum, secondnum, REPEAT_WITH_Q); } else { /* Error - shouldn't be here (n>m) */ FATAL("internal error"); } return 0; } int relex(void) /* lexical analyzer for reparse */ { int c, n; int cflag; static uschar *buf = NULL; static int bufsz = 100; uschar *bp; const struct charclass *cc; int i; int num, m; bool commafound, digitfound; const uschar *startreptok; static int parens = 0; rescan: starttok = prestr; switch (c = *prestr++) { case '|': return OR; case '*': return STAR; case '+': return PLUS; case '?': return QUEST; case '.': return DOT; case '\0': prestr--; return '\0'; case '^': case '$': return c; case '(': parens++; return c; case ')': if (parens) { parens--; return c; } /* unmatched close parenthesis; per POSIX, treat as literal */ rlxval = c; return CHAR; case '\\': rlxval = quoted(&prestr); return CHAR; default: rlxval = c; return CHAR; case '[': if (buf == NULL && (buf = (uschar *) malloc(bufsz)) == NULL) FATAL("out of space in reg expr %.10s..", lastre); bp = buf; if (*prestr == '^') { cflag = 1; prestr++; } else cflag = 0; n = 2 * strlen((const char *) prestr)+1; if (!adjbuf((char **) &buf, &bufsz, n, n, (char **) &bp, "relex1")) FATAL("out of space for reg expr %.10s...", lastre); for (; ; ) { if ((c = *prestr++) == '\\') { *bp++ = '\\'; if ((c = *prestr++) == '\0') FATAL("nonterminated character class %.20s...", lastre); *bp++ = c; /* } else if (c == '\n') { */ /* FATAL("newline in character class %.20s...", lastre); */ } else if (c == '[' && *prestr == ':') { /* POSIX char class names, Dag-Erling Smorgrav, [email protected] */ for (cc = charclasses; cc->cc_name; cc++) if (strncmp((const char *) prestr + 1, (const char *) cc->cc_name, cc->cc_namelen) == 0) break; if (cc->cc_name != NULL && prestr[1 + cc->cc_namelen] == ':' && prestr[2 + cc->cc_namelen] == ']') { prestr += cc->cc_namelen + 3; /* * BUG: We begin at 1, instead of 0, since we * would otherwise prematurely terminate the * string for classes like [[:cntrl:]]. This * means that we can't match the NUL character, * not without first adapting the entire * program to track each string's length. */ for (i = 1; i <= UCHAR_MAX; i++) { if (!adjbuf((char **) &buf, &bufsz, bp-buf+2, 100, (char **) &bp, "relex2")) FATAL("out of space for reg expr %.10s...", lastre); if (cc->cc_func(i)) { /* escape backslash */ if (i == '\\') { *bp++ = '\\'; n++; } *bp++ = i; n++; } } } else *bp++ = c; } else if (c == '[' && *prestr == '.') { char collate_char; prestr++; collate_char = *prestr++; if (*prestr == '.' && prestr[1] == ']') { prestr += 2; /* Found it: map via locale TBD: for now, simply return this char. This is sufficient to pass conformance test awk.ex 156 */ if (*prestr == ']') { prestr++; rlxval = collate_char; return CHAR; } } } else if (c == '[' && *prestr == '=') { char equiv_char; prestr++; equiv_char = *prestr++; if (*prestr == '=' && prestr[1] == ']') { prestr += 2; /* Found it: map via locale TBD: for now simply return this char. This is sufficient to pass conformance test awk.ex 156 */ if (*prestr == ']') { prestr++; rlxval = equiv_char; return CHAR; } } } else if (c == '\0') { FATAL("nonterminated character class %.20s", lastre); } else if (bp == buf) { /* 1st char is special */ *bp++ = c; } else if (c == ']') { *bp++ = 0; rlxstr = (uschar *) tostring((char *) buf); if (cflag == 0) return CCL; else return NCCL; } else *bp++ = c; } break; case '{': if (isdigit(*(prestr))) { num = 0; /* Process as a repetition */ n = -1; m = -1; commafound = false; digitfound = false; startreptok = prestr-1; /* Remember start of previous atom here ? */ } else { /* just a { char, not a repetition */ rlxval = c; return CHAR; } for (; ; ) { if ((c = *prestr++) == '}') { if (commafound) { if (digitfound) { /* {n,m} */ m = num; if (m < n) FATAL("illegal repetition expression: class %.20s", lastre); if (n == 0 && m == 1) { return QUEST; } } else { /* {n,} */ if (n == 0) return STAR; else if (n == 1) return PLUS; } } else { if (digitfound) { /* {n} same as {n,n} */ n = num; m = num; } else { /* {} */ FATAL("illegal repetition expression: class %.20s", lastre); } } if (repeat(starttok, prestr-starttok, lastatom, startreptok - lastatom, n, m) > 0) { if (n == 0 && m == 0) { return ZERO; } /* must rescan input for next token */ goto rescan; } /* Failed to replace: eat up {...} characters and treat like just PLUS */ return PLUS; } else if (c == '\0') { FATAL("nonterminated character class %.20s", lastre); } else if (isdigit(c)) { num = 10 * num + c - '0'; digitfound = true; } else if (c == ',') { if (commafound) FATAL("illegal repetition expression: class %.20s", lastre); /* looking for {n,} or {n,m} */ commafound = true; n = num; digitfound = false; /* reset */ num = 0; } else { FATAL("illegal repetition expression: class %.20s", lastre); } } break; } } int cgoto(fa *f, int s, int c) { int *p, *q; int i, j, k; assert(c == HAT || c < NCHARS); while (f->accept >= maxsetvec) { /* guessing here! */ resizesetvec(__func__); } for (i = 0; i <= f->accept; i++) setvec[i] = 0; setcnt = 0; resize_state(f, s); /* compute positions of gototab[s,c] into setvec */ p = f->posns[s]; for (i = 1; i <= *p; i++) { if ((k = f->re[p[i]].ltype) != FINAL) { if ((k == CHAR && c == ptoi(f->re[p[i]].lval.np)) || (k == DOT && c != 0 && c != HAT) || (k == ALL && c != 0) || (k == EMPTYRE && c != 0) || (k == CCL && member(c, (char *) f->re[p[i]].lval.up)) || (k == NCCL && !member(c, (char *) f->re[p[i]].lval.up) && c != 0 && c != HAT)) { q = f->re[p[i]].lfollow; for (j = 1; j <= *q; j++) { if (q[j] >= maxsetvec) { resizesetvec(__func__); } if (setvec[q[j]] == 0) { setcnt++; setvec[q[j]] = 1; } } } } } /* determine if setvec is a previous state */ tmpset[0] = setcnt; j = 1; for (i = f->accept; i >= 0; i--) if (setvec[i]) { tmpset[j++] = i; } resize_state(f, f->curstat > s ? f->curstat : s); /* tmpset == previous state? */ for (i = 1; i <= f->curstat; i++) { p = f->posns[i]; if ((k = tmpset[0]) != p[0]) goto different; for (j = 1; j <= k; j++) if (tmpset[j] != p[j]) goto different; /* setvec is state i */ if (c != HAT) f->gototab[s][c] = i; return i; different:; } /* add tmpset to current set of states */ ++(f->curstat); resize_state(f, f->curstat); for (i = 0; i < NCHARS; i++) f->gototab[f->curstat][i] = 0; xfree(f->posns[f->curstat]); p = intalloc(setcnt + 1, __func__); f->posns[f->curstat] = p; if (c != HAT) f->gototab[s][c] = f->curstat; for (i = 0; i <= setcnt; i++) p[i] = tmpset[i]; if (setvec[f->accept]) f->out[f->curstat] = 1; else f->out[f->curstat] = 0; return f->curstat; } void freefa(fa *f) /* free a finite automaton */ { int i; if (f == NULL) return; for (i = 0; i < f->state_count; i++) xfree(f->gototab[i]) for (i = 0; i <= f->curstat; i++) xfree(f->posns[i]); for (i = 0; i <= f->accept; i++) { xfree(f->re[i].lfollow); if (f->re[i].ltype == CCL || f->re[i].ltype == NCCL) xfree(f->re[i].lval.np); } xfree(f->restr); xfree(f->out); xfree(f->posns); xfree(f->gototab); xfree(f); }
32,988
1,320
jart/cosmopolitan
false
cosmopolitan/third_party/awk/cmd.c
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2022 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ │ above copyright notice and this permission notice appear in all copies. │ │ │ │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "third_party/awk/cmd.h" #include "tool/args/args.h" int main(int argc, char *argv[]) { LoadZipArgs(&argc, &argv); return _awk(argc, argv); }
1,991
26
jart/cosmopolitan
false
cosmopolitan/third_party/awk/awk.h
#ifndef COSMOPOLITAN_THIRD_PARTY_AWK_AWK_H_ #define COSMOPOLITAN_THIRD_PARTY_AWK_AWK_H_ #include "libc/assert.h" #include "libc/limits.h" #include "libc/literal.h" #include "libc/stdio/stdio.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ /* clang-format off */ typedef double Awkfloat; /* unsigned char is more trouble than it's worth */ typedef unsigned char uschar; #define xfree(a) { if ((a) != NULL) { free((void *)(intptr_t)(a)); (a) = NULL; } } /* * We sometimes cheat writing read-only pointers to NUL-terminate them * and then put back the original value */ #define setptr(ptr, a) (*(char *)(intptr_t)(ptr)) = (a) #define NN(p) ((p) ? (p) : "(null)") /* guaranteed non-null for DPRINTF */ #define DEBUG #ifdef DEBUG # define DPRINTF(...) if (dbg) printf(__VA_ARGS__) #else # define DPRINTF(...) #endif extern enum compile_states { RUNNING, COMPILING, ERROR_PRINTING } compile_time; extern bool safe; /* false => unsafe, true => safe */ #define RECSIZE (8 * 1024) /* sets limit on records, fields, etc., etc. */ extern int recsize; /* size of current record, orig RECSIZE */ extern char EMPTY[]; /* this avoid -Wwritable-strings issues */ extern char **FS; extern char **RS; extern char **ORS; extern char **OFS; extern char **OFMT; extern Awkfloat *NR; extern Awkfloat *FNR; extern Awkfloat *NF; extern char **FILENAME; extern char **SUBSEP; extern Awkfloat *RSTART; extern Awkfloat *RLENGTH; extern char *record; /* points to $0 */ extern int lineno; /* line number in awk program */ extern int errorflag; /* 1 if error has occurred */ extern bool donefld; /* true if record broken into fields */ extern bool donerec; /* true if record is valid (no fld has changed */ extern int dbg; extern const char *patbeg; /* beginning of pattern matched */ extern int patlen; /* length of pattern matched. set in b.c */ /* Cell: all information about a variable or constant */ typedef struct Cell { uschar ctype; /* OCELL, OBOOL, OJUMP, etc. */ uschar csub; /* CCON, CTEMP, CFLD, etc. */ char *nval; /* name, for variables only */ char *sval; /* string value */ Awkfloat fval; /* value as number */ int tval; /* type info: STR|NUM|ARR|FCN|FLD|CON|DONTFREE|CONVC|CONVO */ char *fmt; /* CONVFMT/OFMT value used to convert from number */ struct Cell *cnext; /* ptr to next if chained */ } Cell; typedef struct Array { /* symbol table array */ int nelem; /* elements in table right now */ int size; /* size of tab */ Cell **tab; /* hash table pointers */ } Array; #define NSYMTAB 50 /* initial size of a symbol table */ extern Array *symtab; extern Cell *nrloc; /* NR */ extern Cell *fnrloc; /* FNR */ extern Cell *fsloc; /* FS */ extern Cell *nfloc; /* NF */ extern Cell *ofsloc; /* OFS */ extern Cell *orsloc; /* ORS */ extern Cell *rsloc; /* RS */ extern Cell *rstartloc; /* RSTART */ extern Cell *rlengthloc; /* RLENGTH */ extern Cell *subseploc; /* SUBSEP */ extern Cell *symtabloc; /* SYMTAB */ /* Cell.tval values: */ #define NUM 01 /* number value is valid */ #define STR 02 /* string value is valid */ #define DONTFREE 04 /* string space is not freeable */ #define CON 010 /* this is a constant */ #define ARR 020 /* this is an array */ #define FCN 040 /* this is a function name */ #define FLD 0100 /* this is a field $1, $2, ... */ #define REC 0200 /* this is $0 */ #define CONVC 0400 /* string was converted from number via CONVFMT */ #define CONVO 01000 /* string was converted from number via OFMT */ /* function types */ #define FLENGTH 1 #define FSQRT 2 #define FEXP 3 #define FLOG 4 #define FINT 5 #define FSYSTEM 6 #define FRAND 7 #define FSRAND 8 #define FSIN 9 #define FCOS 10 #define FATAN 11 #define FTOUPPER 12 #define FTOLOWER 13 #define FFLUSH 14 /* Node: parse tree is made of nodes, with Cell's at bottom */ typedef struct Node { int ntype; struct Node *nnext; int lineno; int nobj; struct Node *narg[1]; /* variable: actual size set by calling malloc */ } Node; #define NIL ((Node *) 0) extern Node *winner; extern Node *nullstat; extern Node *nullnode; /* ctypes */ #define OCELL 1 #define OBOOL 2 #define OJUMP 3 /* Cell subtypes: csub */ #define CFREE 7 #define CCOPY 6 #define CCON 5 #define CTEMP 4 #define CNAME 3 #define CVAR 2 #define CFLD 1 #define CUNK 0 /* bool subtypes */ #define BTRUE 11 #define BFALSE 12 /* jump subtypes */ #define JEXIT 21 #define JNEXT 22 #define JBREAK 23 #define JCONT 24 #define JRET 25 #define JNEXTFILE 26 /* node types */ #define NVALUE 1 #define NSTAT 2 #define NEXPR 3 extern int pairstack[], paircnt; #define notlegal(n) (n <= FIRSTTOKEN || n >= LASTTOKEN || proctab[n-FIRSTTOKEN] == nullproc) #define isvalue(n) ((n)->ntype == NVALUE) #define isexpr(n) ((n)->ntype == NEXPR) #define isjump(n) ((n)->ctype == OJUMP) #define isexit(n) ((n)->csub == JEXIT) #define isbreak(n) ((n)->csub == JBREAK) #define iscont(n) ((n)->csub == JCONT) #define isnext(n) ((n)->csub == JNEXT || (n)->csub == JNEXTFILE) #define isret(n) ((n)->csub == JRET) #define isrec(n) ((n)->tval & REC) #define isfld(n) ((n)->tval & FLD) #define isstr(n) ((n)->tval & STR) #define isnum(n) ((n)->tval & NUM) #define isarr(n) ((n)->tval & ARR) #define isfcn(n) ((n)->tval & FCN) #define istrue(n) ((n)->csub == BTRUE) #define istemp(n) ((n)->csub == CTEMP) #define isargument(n) ((n)->nobj == ARG) /* #define freeable(p) (!((p)->tval & DONTFREE)) */ #define freeable(p) ( ((p)->tval & (STR|DONTFREE)) == STR ) /* structures used by regular expression matching machinery, mostly b.c: */ #define NCHARS (256+3) /* 256 handles 8-bit chars; 128 does 7-bit */ /* watch out in match(), etc. */ #define HAT (NCHARS+2) /* matches ^ in regular expr */ #define NSTATES 32 typedef struct rrow { long ltype; /* long avoids pointer warnings on 64-bit */ union { int i; Node *np; uschar *up; } lval; /* because Al stores a pointer in it! */ int *lfollow; } rrow; typedef struct fa { unsigned int **gototab; uschar *out; uschar *restr; int **posns; int state_count; bool anchor; int use; int initstat; int curstat; int accept; struct rrow re[1]; /* variable: actual size set by calling malloc */ } fa; extern int yywrap(void); extern void setfname(Cell *); extern int constnode(Node *); extern char *strnode(Node *); extern Node *notnull(Node *); extern int yyparse(void); extern int yylex(void); extern void startreg(void); extern int input(void); extern void unput(int); extern void unputstr(const char *); extern int yylook(void); extern int yyback(int *, int); extern int yyinput(void); extern fa *makedfa(const char *, bool); extern fa *mkdfa(const char *, bool); extern int makeinit(fa *, bool); extern void penter(Node *); extern void freetr(Node *); extern int hexstr(const uschar **); extern int quoted(const uschar **); extern char *cclenter(const char *); extern wontreturn void overflo(const char *); extern void cfoll(fa *, Node *); extern int first(Node *); extern void follow(Node *); extern int member(int, const char *); extern int match(fa *, const char *); extern int pmatch(fa *, const char *); extern int nematch(fa *, const char *); extern bool fnematch(fa *, FILE *, char **, int *, int); extern Node *reparse(const char *); extern Node *regexp(void); extern Node *primary(void); extern Node *concat(Node *); extern Node *alt(Node *); extern Node *unary(Node *); extern int relex(void); extern int cgoto(fa *, int, int); extern void freefa(fa *); extern int pgetc(void); extern char *cursource(void); extern Node *nodealloc(int); extern Node *exptostat(Node *); extern Node *node1(int, Node *); extern Node *node2(int, Node *, Node *); extern Node *node3(int, Node *, Node *, Node *); extern Node *node4(int, Node *, Node *, Node *, Node *); extern Node *stat3(int, Node *, Node *, Node *); extern Node *op2(int, Node *, Node *); extern Node *op1(int, Node *); extern Node *stat1(int, Node *); extern Node *op3(int, Node *, Node *, Node *); extern Node *op4(int, Node *, Node *, Node *, Node *); extern Node *stat2(int, Node *, Node *); extern Node *stat4(int, Node *, Node *, Node *, Node *); extern Node *celltonode(Cell *, int); extern Node *rectonode(void); extern Node *makearr(Node *); extern Node *pa2stat(Node *, Node *, Node *); extern Node *linkum(Node *, Node *); extern void defn(Cell *, Node *, Node *); extern int isarg(const char *); extern const char *tokname(int); extern Cell *(*proctab[])(Node **, int); extern int ptoi(void *); extern Node *itonp(int); extern void syminit(void); extern void arginit(int, char **); extern void envinit(char **); extern Array *makesymtab(int); extern void freesymtab(Cell *); extern void freeelem(Cell *, const char *); extern Cell *setsymtab(const char *, const char *, double, unsigned int, Array *); extern int hash(const char *, int); extern void rehash(Array *); extern Cell *lookup(const char *, Array *); extern double setfval(Cell *, double); extern void funnyvar(Cell *, const char *); extern char *setsval(Cell *, const char *); extern double getfval(Cell *); extern char *getsval(Cell *); extern char *getpssval(Cell *); /* for print */ extern char *tostring(const char *); extern char *tostringN(const char *, size_t); extern char *qstring(const char *, int); extern Cell *catstr(Cell *, Cell *); extern void recinit(unsigned int); extern void initgetrec(void); extern void makefields(int, int); extern void growfldtab(int n); extern void savefs(void); extern int getrec(char **, int *, bool); extern void nextfile(void); extern int readrec(char **buf, int *bufsize, FILE *inf, bool isnew); extern char *getargv(int); extern void setclvar(char *); extern void fldbld(void); extern void cleanfld(int, int); extern void newfld(int); extern void setlastfld(int); extern int refldbld(const char *, const char *); extern void recbld(void); extern Cell *fieldadr(int); extern void yyerror(const char *); extern void bracecheck(void); extern void bcheck2(int, int, int); extern void SYNTAX(const char *, ...) __attribute__((__format__(__printf__, 1, 2))); extern wontreturn void FATAL(const char *, ...) __attribute__((__format__(__printf__, 1, 2))); extern void WARNING(const char *, ...) __attribute__((__format__(__printf__, 1, 2))); extern void error(void); extern void eprint(void); extern void bclass(int); extern double errcheck(double, const char *); extern int isclvar(const char *); extern bool is_valid_number(const char *s, bool trailing_stuff_ok, bool *no_trailing, double *result); #define is_number(s, val) is_valid_number(s, false, NULL, val) extern int adjbuf(char **pb, int *sz, int min, int q, char **pbp, const char *what); extern void run(Node *); extern Cell *execute(Node *); extern Cell *program(Node **, int); extern Cell *call(Node **, int); extern Cell *copycell(Cell *); extern Cell *arg(Node **, int); extern Cell *jump(Node **, int); extern Cell *awkgetline(Node **, int); extern Cell *getnf(Node **, int); extern Cell *array(Node **, int); extern Cell *awkdelete(Node **, int); extern Cell *intest(Node **, int); extern Cell *matchop(Node **, int); extern Cell *boolop(Node **, int); extern Cell *relop(Node **, int); extern void tfree(Cell *); extern Cell *gettemp(void); extern Cell *field(Node **, int); extern Cell *indirect(Node **, int); extern Cell *substr(Node **, int); extern Cell *sindex(Node **, int); extern int format(char **, int *, const char *, Node *); extern Cell *awksprintf(Node **, int); extern Cell *awkprintf(Node **, int); extern Cell *arith(Node **, int); extern double ipow(double, int); extern Cell *incrdecr(Node **, int); extern Cell *assign(Node **, int); extern Cell *cat(Node **, int); extern Cell *pastat(Node **, int); extern Cell *dopa2(Node **, int); extern Cell *split(Node **, int); extern Cell *condexpr(Node **, int); extern Cell *ifstat(Node **, int); extern Cell *whilestat(Node **, int); extern Cell *dostat(Node **, int); extern Cell *forstat(Node **, int); extern Cell *instat(Node **, int); extern Cell *bltin(Node **, int); extern Cell *printstat(Node **, int); extern Cell *nullproc(Node **, int); extern FILE *redirect(int, Node *); extern FILE *openfile(int, const char *, bool *); extern const char *filename(FILE *); extern Cell *closefile(Node **, int); extern void closeall(void); extern Cell *sub(Node **, int); extern Cell *gsub(Node **, int); extern const char *flags2str(int flags); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_THIRD_PARTY_AWK_AWK_H_ */
12,441
417
jart/cosmopolitan
false