├── tests ├── arginc.c ├── cond.c ├── inc.c ├── while.c ├── read.c ├── fib.c ├── swap.s ├── eq.c └── x86.s ├── hello.c ├── README.md ├── dlfcn32.h ├── mman32.h ├── JIT.md ├── LICENSE ├── c4.c └── c4x86.c /tests/arginc.c: -------------------------------------------------------------------------------- 1 | int main(int argc, char **argv) { 2 | return argc + 1; 3 | } 4 | -------------------------------------------------------------------------------- /hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | printf("hello, world\n"); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /tests/cond.c: -------------------------------------------------------------------------------- 1 | int main(int argc, char **argv) { 2 | if (argc == 1) { printf("Usage: \n"); } 3 | else { printf("argc = %d\n", argc); } 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /tests/inc.c: -------------------------------------------------------------------------------- 1 | int inc(int x) { 2 | return x + 1; 3 | } 4 | 5 | int add2(int x) { 6 | return inc(inc(x)); 7 | } 8 | 9 | int main(int argc, char **argv) { 10 | return add2(argc); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /tests/while.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int fact(int n) { 4 | int r; 5 | r = 1; 6 | while (n > 0) { 7 | r = r * n; 8 | printf("n = %d, r = %d\n", n, r); 9 | --n; 10 | } 11 | return r; 12 | } 13 | 14 | int main(int argc, char **argv) { 15 | printf("%d\n", fact(8)); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | c4 - C in four functions 2 | ======================== 3 | 4 | An exercise in minimalism. 5 | 6 | Try the following: 7 | 8 | gcc -o c4 c4.c (you may need the -m32 option on 64bit machines) 9 | ./c4 hello.c 10 | ./c4 -s hello.c 11 | 12 | ./c4 c4.c hello.c 13 | ./c4 c4.c c4.c hello.c 14 | 15 | 16 | c4x86 - JIT compiler for x86 in 86 lines 17 | ======================================== 18 | 19 | An exercise in bit-twiddling masochism. 20 | 21 | x86 only, not self-hosted! 22 | 23 | gcc -m32 c4x86.c -o c4x86 24 | ./c4x86 hello.c 25 | ./c4x86 c4.c hello.c 26 | -------------------------------------------------------------------------------- /tests/read.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char **argv) { 5 | char *p; 6 | int fd, readsz; 7 | ++argv; 8 | 9 | if (!(p = malloc(1024 * 16))) { printf("failed to malloc memory\n"); exit(1); } 10 | 11 | printf("opening %s...\n", *argv); 12 | fd = open(*argv, 0); 13 | //if (fd < 0) { printf("failed to open %s\n", *argv); exit(2); } 14 | 15 | readsz = read(fd, p, 1024 * 16); 16 | printf("read %d bytes\nContents:\n______________________________\n%s", readsz, p); 17 | printf("\n_______________________________\n"); 18 | 19 | exit(0); 20 | } 21 | -------------------------------------------------------------------------------- /tests/fib.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int my_atoi(char *s) { 5 | int res; 6 | res = 0; 7 | while (*s) { 8 | if (*s < '0' || '9' < *s) 9 | return 0; 10 | res = res * 10 + (*s - '0'); 11 | ++s; 12 | } 13 | return res; 14 | } 15 | 16 | int fib(int n) { 17 | if (n < 2) return 1; 18 | return fib(n - 1) + fib(n - 2); 19 | } 20 | 21 | int main(int argc, char **argv) { 22 | int n; 23 | if (argc < 2) { printf("Usage: %s \n", argv[0]); exit(1); } 24 | 25 | n = my_atoi(argv[1]); 26 | printf("%d\n", fib(n)); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /tests/swap.s: -------------------------------------------------------------------------------- 1 | argc1: 2 | pop %ecx 3 | mov %esp, %esi 4 | andl $0xfffffff0, %esi 5 | mov %ecx, (%esi) 6 | xchg %esi, %esp 7 | 8 | call 0xdeadbeef 9 | xchg %esi, %esp 10 | 11 | argc2: 12 | pop %ecx 13 | pop %edx 14 | movl %esp, %esi 15 | andl $0xfffffff0, %esi 16 | mov %edx, (%esi) 17 | mov %ecx, 4(%esi) 18 | xchg %esi, %esp 19 | 20 | call 0xdeadbeef 21 | 22 | xchg %esi, %esp 23 | 24 | argN: # n = 4 25 | mov $0x10, %ecx 26 | mov %esp, %esi 27 | subl %ecx, %esi 28 | shr $2, %ecx 29 | andl $0xfffffff0, %esi 30 | 31 | 1: 32 | pop %edx 33 | mov %edx, -4(%esi, %ecx, 4) # -4 to compensate %ecx off-by-one 34 | loop 1b 35 | 36 | xchg %esi, %esp 37 | 38 | call 0xdeadbeef 39 | 40 | xchg %esi, %esp 41 | -------------------------------------------------------------------------------- /dlfcn32.h: -------------------------------------------------------------------------------- 1 | #ifdef _WIN32 2 | #include 3 | #include 4 | #include 5 | #include "mman32.h" 6 | enum { 7 | RTLD_LAZY = 1, 8 | RTLD_GLOBAL = 2 9 | }; 10 | int dlopen(int x, int y) 11 | { 12 | return 0; 13 | } 14 | void *dlsym(void *handle, char *name) 15 | { 16 | if (!strcmp(name, "open" )) return &open; 17 | if (!strcmp(name, "read" )) return &read; 18 | if (!strcmp(name, "close" )) return &close; 19 | if (!strcmp(name, "printf")) return &printf; 20 | if (!strcmp(name, "malloc")) return &malloc; 21 | if (!strcmp(name, "memset")) return &memset; 22 | if (!strcmp(name, "memcmp")) return &memcmp; 23 | if (!strcmp(name, "memcpy")) return &memcpy; 24 | if (!strcmp(name, "mmap" )) return &mmap; 25 | if (!strcmp(name, "dlsym" )) return &dlsym; 26 | if (!strcmp(name, "qsort" )) return &qsort; 27 | if (!strcmp(name, "exit" )) return &exit; 28 | return 0; 29 | } 30 | #else 31 | #include 32 | #endif 33 | -------------------------------------------------------------------------------- /tests/eq.c: -------------------------------------------------------------------------------- 1 | int main() { 2 | printf("16000 == 16000 : %d\n", 16000 == 16000); 3 | printf("16000 == 17000 : %d\n", 16000 == 17000); 4 | printf("2 == 2 : %d\n", 2 == 2); 5 | printf("0 == 0 : %d\n", 0 == 0); 6 | printf("-1 == -1 : %d\n", -1 == -1); 7 | printf("\n"); 8 | 9 | printf("16000 != 16000 : %d\n", 16000 != 16000); 10 | printf("16000 != 17000 : %d\n", 16000 != 17000); 11 | printf("2 != 2 : %d\n", 2 != 2); 12 | printf("0 != 0 : %d\n", 0 != 0); 13 | printf("-1 != -1 : %d\n", -1 != -1); 14 | printf("\n"); 15 | 16 | printf("17000 > 16000 : %d\n", 17000 > 16000); 17 | printf("17000 > 17000 : %d\n", 17000 > 17000); 18 | printf("16000 > 17000 : %d\n", 16000 > 17000); 19 | printf("16000 > -17000 : %d\n", 16000 > -17000); 20 | printf("-16000 > -17000 : %d\n", -16000 > -17000); 21 | printf("\n"); 22 | 23 | printf("17000 >= 16000 : %d\n", 17000 >= 16000); 24 | printf("17000 >= 17000 : %d\n", 17000 >= 17000); 25 | printf("16000 >= 17000 : %d\n", 16000 >= 17000); 26 | printf("16000 >= -17000 : %d\n", 16000 >= -17000); 27 | printf("-16000 >= -17000 : %d\n", -16000 >= -17000); 28 | printf("\n"); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /tests/x86.s: -------------------------------------------------------------------------------- 1 | ENT: # 2 2 | push %ebp 3 | mov %esp, %ebp 4 | subl $8, %esp 5 | 6 | BZ: #0xdeadbeef 7 | test %eax, %eax 8 | jz .exit 9 | # BNZ 0xdeadbeef 10 | test %eax, %eax 11 | jnz .exit 12 | 13 | jnz 0x0 14 | jz .exit 15 | jz 0x0 16 | 17 | leal (-4 * 1)(%ebp), %eax # LEA -1 18 | movl $42, %eax # IMM 42 19 | jmp 0x210 # JMP 0xdeadbeef 20 | call *0xdeadbeef # JSR 0xdeadbeef 21 | addl $(4 * 5), %esp # ADJ 5 22 | movl (%eax), %eax # LI 23 | movzb (%eax), %eax # LC 24 | 25 | SI: 26 | pop %ecx 27 | movl %eax, (%ecx) 28 | 29 | SC: 30 | pop %ecx 31 | movb %al, (%ecx) 32 | 33 | push %eax 34 | 35 | signextend: 36 | cbw 37 | cwde 38 | 39 | GT1: 40 | mov %eax, %edx 41 | #push %eax pop %edx 42 | pop %ecx 43 | xor %eax, %eax 44 | cmp %edx, %ecx 45 | setg %al 46 | 47 | GT2: 48 | pop %ecx 49 | cmp %eax, %ecx 50 | setg %al 51 | cbw 52 | cwde 53 | 54 | EQ1: 55 | pop %ecx 56 | cmp %eax, %ecx 57 | sete %al 58 | cbw 59 | cwde 60 | 61 | cmps: 62 | setl %al 63 | setge %al 64 | setle %al 65 | setne %al 66 | 67 | MOD: 68 | pop %ecx 69 | xchg %ecx, %eax 70 | xor %edx, %edx 71 | idiv %ecx 72 | xchg %edx, %eax 73 | 74 | .exit: 75 | mov %ebp, %esp 76 | pop %ebp 77 | ret 78 | 79 | pop %ecx 80 | orl %ecx, %eax 81 | xchg %ecx, %eax 82 | xorl %ecx, %eax 83 | andl %ecx, %eax 84 | test %ecx, %eax # EQ 85 | shll %cl, %eax 86 | shrl %cl, %eax 87 | add %ecx, %eax 88 | subl %ecx, %eax 89 | imul %ecx, %eax 90 | idiv %ecx, %eax 91 | 92 | 93 | -------------------------------------------------------------------------------- /mman32.h: -------------------------------------------------------------------------------- 1 | #ifndef PORTABLE_MMAP_H 2 | #define PORTABLE_MMAP_H 3 | 4 | #ifdef _WIN32 5 | /* mmap() replacement for Windows 6 | * 7 | * Author: Mike Frysinger 8 | * Placed into the public domain 9 | */ 10 | 11 | /* References: 12 | * CreateFileMapping: http://msdn.microsoft.com/en-us/library/aa366537(VS.85).aspx 13 | * CloseHandle: http://msdn.microsoft.com/en-us/library/ms724211(VS.85).aspx 14 | * MapViewOfFile: http://msdn.microsoft.com/en-us/library/aa366761(VS.85).aspx 15 | * UnmapViewOfFile: http://msdn.microsoft.com/en-us/library/aa366882(VS.85).aspx 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | #define PROT_READ 0x1 23 | #define PROT_WRITE 0x2 24 | /* This flag is only available in WinXP+ */ 25 | #ifdef FILE_MAP_EXECUTE 26 | #define PROT_EXEC 0x4 27 | #else 28 | #define PROT_EXEC 0x0 29 | #define FILE_MAP_EXECUTE 0 30 | #endif 31 | 32 | #define MAP_SHARED 0x01 33 | #define MAP_PRIVATE 0x02 34 | #define MAP_ANONYMOUS 0x20 35 | #define MAP_ANON MAP_ANONYMOUS 36 | #define MAP_FAILED ((void *) -1) 37 | 38 | #ifdef __USE_FILE_OFFSET64 39 | # define DWORD_HI(x) (x >> 32) 40 | # define DWORD_LO(x) ((x) & 0xffffffff) 41 | #else 42 | # define DWORD_HI(x) (0) 43 | # define DWORD_LO(x) (x) 44 | #endif 45 | 46 | static void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset) 47 | { 48 | if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) 49 | return MAP_FAILED; 50 | if (fd == -1) { 51 | if (!(flags & MAP_ANON) || offset) 52 | return MAP_FAILED; 53 | } else if (flags & MAP_ANON) 54 | return MAP_FAILED; 55 | 56 | DWORD flProtect; 57 | if (prot & PROT_WRITE) { 58 | if (prot & PROT_EXEC) 59 | flProtect = PAGE_EXECUTE_READWRITE; 60 | else 61 | flProtect = PAGE_READWRITE; 62 | } else if (prot & PROT_EXEC) { 63 | if (prot & PROT_READ) 64 | flProtect = PAGE_EXECUTE_READ; 65 | else if (prot & PROT_EXEC) 66 | flProtect = PAGE_EXECUTE; 67 | } else 68 | flProtect = PAGE_READONLY; 69 | 70 | off_t end = length + offset; 71 | HANDLE mmap_fd, h; 72 | if (fd == -1) 73 | mmap_fd = INVALID_HANDLE_VALUE; 74 | else 75 | mmap_fd = (HANDLE)_get_osfhandle(fd); 76 | h = CreateFileMapping(mmap_fd, NULL, flProtect, DWORD_HI(end), DWORD_LO(end), NULL); 77 | if (h == NULL) 78 | return MAP_FAILED; 79 | 80 | DWORD dwDesiredAccess; 81 | if (prot & PROT_WRITE) 82 | dwDesiredAccess = FILE_MAP_WRITE; 83 | else 84 | dwDesiredAccess = FILE_MAP_READ; 85 | if (prot & PROT_EXEC) 86 | dwDesiredAccess |= FILE_MAP_EXECUTE; 87 | if (flags & MAP_PRIVATE) 88 | dwDesiredAccess |= FILE_MAP_COPY; 89 | void *ret = MapViewOfFile(h, dwDesiredAccess, DWORD_HI(offset), DWORD_LO(offset), length); 90 | if (ret == NULL) { 91 | CloseHandle(h); 92 | ret = MAP_FAILED; 93 | } 94 | return ret; 95 | } 96 | 97 | static void munmap(void *addr, size_t length) 98 | { 99 | UnmapViewOfFile(addr); 100 | /* ruh-ro, we leaked handle from CreateFileMapping() ... */ 101 | } 102 | 103 | #undef DWORD_HI 104 | #undef DWORD_LO 105 | 106 | #else 107 | # include 108 | #endif 109 | 110 | #endif 111 | -------------------------------------------------------------------------------- /JIT.md: -------------------------------------------------------------------------------- 1 | What is it? 2 | ============= 3 | 4 | `c4x86.c` is a primitive x86 Just-In-Time compiler for awesome c4 virtual machine. It took about 86 lines of C code. 5 | 6 | It is known to work on Linux (and OS X?). 7 | 8 | How JIT works 9 | ============= 10 | 11 | JIT compilation is based on the fact that mapping c4 opcodes into x86 instructions is quite straightforward: 12 | 13 | | c4 opcode | x86 instructions | comments 14 | |--------------|--------------------------------------------|----------------------- 15 | | `IMM` *val* |`movl $val, %eax` | 16 | | `PSH` |`push %eax` | 17 | | `LEV` |`movl %ebp, %esp; pop %esp; ret` | 18 | | `ADJ` *val* |`subl $(4 * val), %esp)` | 19 | | `LI` |`movl (%eax), %eax` | 20 | | `LC` |`movzbl (%eax), %eax` | 21 | | `SI` |`pop %ecx; movl %eax, (%ecx)` | `%ecx` is used as a temporary register 22 | | `SC` |`pop %ecx; movb %al, (%ecx)` | 23 | | `OR` |`pop %ecx; orl %ecx, %eax` | 24 | | `XOR` |`pop %ecx; xorl %ecx, %eax` | 25 | | `AND` |`pop %ecx; andl %ecx, %eax` | 26 | | `NE` |see `Comparisons` | using `setne %al` opcode 27 | | `EQ` |see `Comparisons` | using `sete %al` opcode 28 | | `GE` |see `Comparisons` | using `setge %al` opcode 29 | | `LE` |see `Comparisons` | using `setle %al` opcode 30 | | `GT` |see `Comparisons` | using `setg %al` opcode 31 | | `LT` |see `Comparisons` | using `setl %al` opcode 32 | | `SHL` |`pop %ecx; xchg %eax, %ecx; shl %cl, %eax` | `xchg` adjusts the operands order 33 | | `SHR` |`pop %ecx; xchg %eax, %ecx; shr %cl, %eax` | 34 | | `ADD` |`pop %ecx; addl %ecx, %eax` | 35 | | `SUB` |`pop %ecx; xchg %eax, %ecx; subl %ecx, %eax`| 36 | | `MUL` |`pop %ecx; imul %ecx, %eax` | 37 | | `DIV` |`pop %ecx; xchg %eax, %ecx; idiv %ecx, %eax`| 38 | | `MOD` |`pop %ecx; xchg %ecx, %eax; xor %edx, %edx; idiv %ecx; xchg %edx, %eax` | `%edx` holds remainder after `idiv` 39 | | `JMP` |`jmp ` | 40 | | `JSR` |`call ` | 41 | | `BZ` |`jz ` | 42 | | `BNZ` |`jnz ` | 43 | | `OPEN`; `ADJ ` | see `Native calls` section | 44 | | `READ` ; `ADJ ` | see `Native calls` section | 45 | | `CLOS` ; `ADJ ` | see `Native calls` section | 46 | | `PRTF` ; `ADJ ` | see `Native calls` section | 47 | | `MALC` ; `ADJ ` | see `Native calls` section | 48 | | `MSET` ; `ADJ ` | see `Native calls` section | 49 | | `MCMP` ; `ADJ ` | see `Native calls` section | 50 | | `EXIT` ; `ADJ ` | see `Native calls` section | 51 | 52 | Some executable and writable memory is allocated with `mmap()`, its address in `jitmem` pointer. 53 | 54 | First pass of the JIT compiler translates c4 opcodes into instructions directly, leaving stubs for relative offsets in `JSR`, `JMP` (4 byte offset), `BZ`, `BNZ` (1 byte offset) to be filled during the second pass. 55 | 56 | Comparisons 57 | =========== 58 | 59 | Comparison uses `set` x86 operations after `cmp %ecx, %eax` where `%ecx` is popped from the stack with ensuing sign-extension of `%al` to `%eax`. 60 | 61 | So, the full comparison code for, e.g. `EQ` is: 62 | 63 | pop %ecx 64 | cmp %ecx, %eax 65 | sete %al # set %al to 0/1 depending on equality 66 | cbw # %al to %ax sign extension 67 | cwde # %ax to %eax sign extension 68 | 69 | 70 | Filling up relative offsets 71 | =========================== 72 | 73 | For addresses of compiled "labels" to be known, the first pass stores addresses of compiled x86 code for each c4 opcode in the opcode cell (of `text[]` array) itself: 74 | 75 | before: | | 0x00 | 0x00 | 0x00 | 76 | after: | | | 77 | 78 | The most significant byte is restored from `jitmem` value (it is assumed that the native code does not take more than 24 megabytes, so the most significant byte is the same for all pointers). 79 | 80 | `JMP`/`JSR`/`BNZ`/`BZ` arguments are not modified. 81 | 82 | The second pass reads c4 pointers of `JMP`/`JSR`/`BNZ`/`BZ` and extracts native codes addresses. Then relative offsets are calculated to fill offset gaps in native code. 83 | 84 | Native calls 85 | ============ 86 | Native calls are tricky for x86: 87 | 88 | 1. order of arguments is reversed; 89 | 2. OS X ABI requires stack to be aligned at 16 bytes before calls. 90 | 91 | Fortunately, arguments count is known for each call, it can be retrieved from `ADJ` right after c4 opcode of the call. 92 | 93 | Both these complications require a quite hacky solution: the arguments evaluation code is left as is, but some additional stack memory is allocated before native calls (aligned to 16 bytes), arguments are copied there in reverse order and "old" stack pointer (without arguments) is saved in `%esi` register. On return from the native routine `%esp` is restored from `%esi`. 94 | 95 | movl $(4 * n), %ecx # store 4 * #args in %ecx 96 | mov %esp, %esi # %esi temporarily holds the native call stack pointer (to become %esp later) 97 | sub %ecx, %esi # %esp - %esi must be large enough to contain all arguments 98 | andl $0xfffffff0, %esi # the future %esp must be aligned at 16 bytes 99 | shr $2, %ecx # let %ecx be just #args now 100 | 101 | 1: # this is a loop copying %ecx arguments: 102 | pop %edx # movl (%esp), %edx; addl $4, %esp; - %esp grows until all arguments are below 103 | mov %edx, -4(%esi,%ecx,4) # %edx value is now stored at %esi+4*%ecx; -4 to compensate %ecx off-by-one 104 | loop 1b # dec %ecx; while %ecx is not 0, return to 1: 105 | 106 | xchg %esi, %esp # now %esi contains the 'old' stack pointer, %esp is adjusted properly 107 | call printf # %esi must be preserved according to cdecl calling convention 108 | 109 | xchg %esi, %esp # ADJust: restore the stack state before the call 110 | 111 | This is a lengthy and costly solution, but it keeps code size small. 112 | 113 | 114 | Issues 115 | ====== 116 | 117 | 1. this is x86 only; requires Unix-like calls; not self-hosted; 118 | 2. uses registers `%eax`, `%ecx`, `%ebp`, `%esp` only with quite redundant memory loads/stores; no register allocation; 119 | 3. it is limited to `open`/`read`/`close`/`printf`/`malloc`/`memset`/`memcmp`/`exit` calls. 120 | 121 | 122 | (c) Dmytro Sirenko, 2014 123 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | 341 | -------------------------------------------------------------------------------- /c4.c: -------------------------------------------------------------------------------- 1 | // c4.c - C in four functions 2 | 3 | // char, int, and pointer types 4 | // if, while, return, and expression statements 5 | // just enough features to allow self-compilation and a bit more 6 | 7 | // Written by Robert Swierczek 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #ifdef _WIN32 14 | #include "w32.h" 15 | #endif 16 | 17 | char *p, *lp, // current position in source code 18 | *data; // data/bss pointer 19 | 20 | int *e, *le, // current position in emitted code 21 | *id, // currently parsed identifier 22 | *sym, // symbol table (simple list of identifiers) 23 | tk, // current token 24 | ival, // current token value 25 | ty, // current expression type 26 | loc, // local variable offset 27 | line, // current line number 28 | src, // print source and assembly flag 29 | debug; // print executed instructions 30 | 31 | // tokens and classes (operators last and in precedence order) 32 | enum { 33 | Num = 128, Fun, Sys, Glo, Loc, Id, 34 | Char, Else, Enum, If, Int, Return, Sizeof, While, 35 | Assign, Cond, Lor, Lan, Or, Xor, And, Eq, Ne, Lt, Gt, Le, Ge, Shl, Shr, Add, Sub, Mul, Div, Mod, Inc, Dec, Brak 36 | }; 37 | 38 | // opcodes 39 | enum { LEA ,IMM ,JMP ,JSR ,BZ ,BNZ ,ENT ,ADJ ,LEV ,LI ,LC ,SI ,SC ,PSH , 40 | OR ,XOR ,AND ,EQ ,NE ,LT ,GT ,LE ,GE ,SHL ,SHR ,ADD ,SUB ,MUL ,DIV ,MOD , 41 | OPEN,READ,CLOS,PRTF,MALC,MSET,MCMP,MCPY,MMAP,DSYM,QSRT,EXIT }; 42 | 43 | // types 44 | enum { CHAR, INT, PTR }; 45 | 46 | // identifier offsets (since we can't create an ident struct) 47 | enum { Tk, Hash, Name, Class, Type, Val, HClass, HType, HVal, Idsz }; 48 | 49 | void next() 50 | { 51 | char *pp; 52 | 53 | while (tk = *p) { 54 | ++p; 55 | if (tk == '\n') { 56 | if (src) { 57 | printf("%d: %.*s", line, p - lp, lp); 58 | lp = p; 59 | while (le < e) { 60 | printf("%8.4s", &"LEA ,IMM ,JMP ,JSR ,BZ ,BNZ ,ENT ,ADJ ,LEV ,LI ,LC ,SI ,SC ,PSH ," 61 | "OR ,XOR ,AND ,EQ ,NE ,LT ,GT ,LE ,GE ,SHL ,SHR ,ADD ,SUB ,MUL ,DIV ,MOD ," 62 | "OPEN,READ,CLOS,PRTF,MALC,MSET,MCMP,MCPY,MMAP,DSYM,QSRT,EXIT,"[*++le * 5]); 63 | if (*le <= ADJ) printf(" %d\n", *++le); else printf("\n"); 64 | } 65 | } 66 | ++line; 67 | } 68 | else if (tk == '#') { 69 | while (*p != 0 && *p != '\n') ++p; 70 | } 71 | else if ((tk >= 'a' && tk <= 'z') || (tk >= 'A' && tk <= 'Z') || tk == '_') { 72 | pp = p - 1; 73 | while ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || (*p >= '0' && *p <= '9') || *p == '_') 74 | tk = tk * 147 + *p++; 75 | tk = (tk << 6) + (p - pp); 76 | id = sym; 77 | while (id[Tk]) { 78 | if (tk == id[Hash] && !memcmp((char *)id[Name], pp, p - pp)) { tk = id[Tk]; return; } 79 | id = id + Idsz; 80 | } 81 | id[Name] = (int)pp; 82 | id[Hash] = tk; 83 | tk = id[Tk] = Id; 84 | return; 85 | } 86 | else if (tk >= '0' && tk <= '9') { 87 | if (ival = tk - '0') { while (*p >= '0' && *p <= '9') ival = ival * 10 + *p++ - '0'; } 88 | else if (*p == 'x' || *p == 'X') { 89 | while ((tk = *++p) && ((tk >= '0' && tk <= '9') || (tk >= 'a' && tk <= 'f') || (tk >= 'A' && tk <= 'F'))) 90 | ival = ival * 16 + (tk & 15) + (tk >= 'A' ? 9 : 0); 91 | } 92 | else { while (*p >= '0' && *p <= '7') ival = ival * 8 + *p++ - '0'; } 93 | tk = Num; 94 | return; 95 | } 96 | else if (tk == '/') { 97 | if (*p == '/') { 98 | ++p; 99 | while (*p != 0 && *p != '\n') ++p; 100 | } 101 | else { 102 | tk = Div; 103 | return; 104 | } 105 | } 106 | else if (tk == '\'' || tk == '"') { 107 | pp = data; 108 | while (*p != 0 && *p != tk) { 109 | if ((ival = *p++) == '\\') { 110 | if ((ival = *p++) == 'n') ival = '\n'; 111 | } 112 | if (tk == '"') *data++ = ival; 113 | } 114 | ++p; 115 | if (tk == '"') ival = (int)pp; else tk = Num; 116 | return; 117 | } 118 | else if (tk == '=') { if (*p == '=') { ++p; tk = Eq; } else tk = Assign; return; } 119 | else if (tk == '+') { if (*p == '+') { ++p; tk = Inc; } else tk = Add; return; } 120 | else if (tk == '-') { if (*p == '-') { ++p; tk = Dec; } else tk = Sub; return; } 121 | else if (tk == '!') { if (*p == '=') { ++p; tk = Ne; } return; } 122 | else if (tk == '<') { if (*p == '=') { ++p; tk = Le; } else if (*p == '<') { ++p; tk = Shl; } else tk = Lt; return; } 123 | else if (tk == '>') { if (*p == '=') { ++p; tk = Ge; } else if (*p == '>') { ++p; tk = Shr; } else tk = Gt; return; } 124 | else if (tk == '|') { if (*p == '|') { ++p; tk = Lor; } else tk = Or; return; } 125 | else if (tk == '&') { if (*p == '&') { ++p; tk = Lan; } else tk = And; return; } 126 | else if (tk == '^') { tk = Xor; return; } 127 | else if (tk == '%') { tk = Mod; return; } 128 | else if (tk == '*') { tk = Mul; return; } 129 | else if (tk == '[') { tk = Brak; return; } 130 | else if (tk == '?') { tk = Cond; return; } 131 | else if (tk == '~' || tk == ';' || tk == '{' || tk == '}' || tk == '(' || tk == ')' || tk == ']' || tk == ',' || tk == ':') return; 132 | } 133 | } 134 | 135 | void expr(int lev) 136 | { 137 | int t, *d; 138 | 139 | if (!tk) { printf("%d: unexpected eof in expression\n", line); exit(-1); } 140 | else if (tk == Num) { *++e = IMM; *++e = ival; next(); ty = INT; } 141 | else if (tk == '"') { 142 | *++e = IMM; *++e = ival; next(); 143 | while (tk == '"') next(); 144 | data = (char *)((int)data + sizeof(int) & -sizeof(int)); ty = PTR; 145 | } 146 | else if (tk == Sizeof) { 147 | next(); if (tk == '(') next(); else { printf("%d: open paren expected in sizeof\n", line); exit(-1); } 148 | ty = INT; if (tk == Int) next(); else if (tk == Char) { next(); ty = CHAR; } 149 | while (tk == Mul) { next(); ty = ty + PTR; } 150 | if (tk == ')') next(); else { printf("%d: close paren expected in sizeof\n", line); exit(-1); } 151 | *++e = IMM; *++e = (ty == CHAR) ? sizeof(char) : sizeof(int); 152 | ty = INT; 153 | } 154 | else if (tk == Id) { 155 | d = id; next(); 156 | if (tk == '(') { 157 | next(); 158 | t = 0; 159 | while (tk != ')') { expr(Assign); *++e = PSH; ++t; if (tk == ',') next(); } 160 | next(); 161 | if (d[Class] == Sys) *++e = d[Val]; 162 | else if (d[Class] == Fun) { *++e = JSR; *++e = d[Val]; } 163 | else { printf("%d: bad function call\n", line); exit(-1); } 164 | if (t) { *++e = ADJ; *++e = t; } 165 | ty = d[Type]; 166 | } 167 | else if (d[Class] == Num) { *++e = IMM; *++e = d[Val]; ty = INT; } 168 | else { 169 | if (d[Class] == Loc) { *++e = LEA; *++e = loc - d[Val]; } 170 | else if (d[Class] == Glo) { *++e = IMM; *++e = d[Val]; } 171 | else { printf("%d: undefined variable\n", line); exit(-1); } 172 | *++e = ((ty = d[Type]) == CHAR) ? LC : LI; 173 | } 174 | } 175 | else if (tk == '(') { 176 | next(); 177 | if (tk == Int || tk == Char) { 178 | t = (tk == Int) ? INT : CHAR; next(); 179 | while (tk == Mul) { next(); t = t + PTR; } 180 | if (tk == ')') next(); else { printf("%d: bad cast\n", line); exit(-1); } 181 | expr(Inc); 182 | ty = t; 183 | } 184 | else { 185 | expr(Assign); 186 | if (tk == ')') next(); else { printf("%d: close paren expected\n", line); exit(-1); } 187 | } 188 | } 189 | else if (tk == Mul) { 190 | next(); expr(Inc); 191 | if (ty > INT) ty = ty - PTR; else { printf("%d: bad dereference\n", line); exit(-1); } 192 | *++e = (ty == CHAR) ? LC : LI; 193 | } 194 | else if (tk == And) { 195 | next(); expr(Inc); 196 | if (*e == LC || *e == LI) --e; else { printf("%d: bad address-of\n", line); exit(-1); } 197 | ty = ty + PTR; 198 | } 199 | else if (tk == '!') { next(); expr(Inc); *++e = PSH; *++e = IMM; *++e = 0; *++e = EQ; ty = INT; } 200 | else if (tk == '~') { next(); expr(Inc); *++e = PSH; *++e = IMM; *++e = -1; *++e = XOR; ty = INT; } 201 | else if (tk == Add) { next(); expr(Inc); ty = INT; } 202 | else if (tk == Sub) { 203 | next(); *++e = IMM; 204 | if (tk == Num) { *++e = -ival; next(); } else { *++e = -1; *++e = PSH; expr(Inc); *++e = MUL; } 205 | ty = INT; 206 | } 207 | else if (tk == Inc || tk == Dec) { 208 | t = tk; next(); expr(Inc); 209 | if (*e == LC) { *e = PSH; *++e = LC; } 210 | else if (*e == LI) { *e = PSH; *++e = LI; } 211 | else { printf("%d: bad lvalue in pre-increment\n", line); exit(-1); } 212 | *++e = PSH; 213 | *++e = IMM; *++e = (ty > PTR) ? sizeof(int) : sizeof(char); 214 | *++e = (t == Inc) ? ADD : SUB; 215 | *++e = (ty == CHAR) ? SC : SI; 216 | } 217 | else { printf("%d: bad expression\n", line); exit(-1); } 218 | 219 | while (tk >= lev) { // "precedence climbing" or "Top Down Operator Precedence" method 220 | t = ty; 221 | if (tk == Assign) { 222 | next(); 223 | if (*e == LC || *e == LI) *e = PSH; else { printf("%d: bad lvalue in assignment\n", line); exit(-1); } 224 | expr(Assign); *++e = ((ty = t) == CHAR) ? SC : SI; 225 | } 226 | else if (tk == Cond) { 227 | next(); 228 | *++e = BZ; d = ++e; 229 | expr(Assign); 230 | if (tk == ':') next(); else { printf("%d: conditional missing colon\n", line); exit(-1); } 231 | *d = (int)(e + 3); *++e = JMP; d = ++e; 232 | expr(Cond); 233 | *d = (int)(e + 1); 234 | } 235 | else if (tk == Lor) { next(); *++e = BNZ; d = ++e; expr(Lan); *d = (int)(e + 1); ty = INT; } 236 | else if (tk == Lan) { next(); *++e = BZ; d = ++e; expr(Or); *d = (int)(e + 1); ty = INT; } 237 | else if (tk == Or) { next(); *++e = PSH; expr(Xor); *++e = OR; ty = INT; } 238 | else if (tk == Xor) { next(); *++e = PSH; expr(And); *++e = XOR; ty = INT; } 239 | else if (tk == And) { next(); *++e = PSH; expr(Eq); *++e = AND; ty = INT; } 240 | else if (tk == Eq) { next(); *++e = PSH; expr(Lt); *++e = EQ; ty = INT; } 241 | else if (tk == Ne) { next(); *++e = PSH; expr(Lt); *++e = NE; ty = INT; } 242 | else if (tk == Lt) { next(); *++e = PSH; expr(Shl); *++e = LT; ty = INT; } 243 | else if (tk == Gt) { next(); *++e = PSH; expr(Shl); *++e = GT; ty = INT; } 244 | else if (tk == Le) { next(); *++e = PSH; expr(Shl); *++e = LE; ty = INT; } 245 | else if (tk == Ge) { next(); *++e = PSH; expr(Shl); *++e = GE; ty = INT; } 246 | else if (tk == Shl) { next(); *++e = PSH; expr(Add); *++e = SHL; ty = INT; } 247 | else if (tk == Shr) { next(); *++e = PSH; expr(Add); *++e = SHR; ty = INT; } 248 | else if (tk == Add) { 249 | next(); *++e = PSH; expr(Mul); 250 | if ((ty = t) > PTR) { *++e = PSH; *++e = IMM; *++e = sizeof(int); *++e = MUL; } 251 | *++e = ADD; 252 | } 253 | else if (tk == Sub) { 254 | next(); *++e = PSH; expr(Mul); 255 | if ((ty = t) > PTR) { *++e = PSH; *++e = IMM; *++e = sizeof(int); *++e = MUL; } 256 | *++e = SUB; 257 | } 258 | else if (tk == Mul) { next(); *++e = PSH; expr(Inc); *++e = MUL; ty = INT; } 259 | else if (tk == Div) { next(); *++e = PSH; expr(Inc); *++e = DIV; ty = INT; } 260 | else if (tk == Mod) { next(); *++e = PSH; expr(Inc); *++e = MOD; ty = INT; } 261 | else if (tk == Inc || tk == Dec) { 262 | if (*e == LC) { *e = PSH; *++e = LC; } 263 | else if (*e == LI) { *e = PSH; *++e = LI; } 264 | else { printf("%d: bad lvalue in post-increment\n", line); exit(-1); } 265 | *++e = PSH; *++e = IMM; *++e = (ty > PTR) ? sizeof(int) : sizeof(char); 266 | *++e = (tk == Inc) ? ADD : SUB; 267 | *++e = (ty == CHAR) ? SC : SI; 268 | *++e = PSH; *++e = IMM; *++e = (ty > PTR) ? sizeof(int) : sizeof(char); 269 | *++e = (tk == Inc) ? SUB : ADD; 270 | next(); 271 | } 272 | else if (tk == Brak) { 273 | next(); *++e = PSH; expr(Assign); 274 | if (tk == ']') next(); else { printf("%d: close bracket expected\n", line); exit(-1); } 275 | if (t > PTR) { *++e = PSH; *++e = IMM; *++e = sizeof(int); *++e = MUL; } 276 | else if (t < PTR) { printf("%d: pointer type expected\n", line); exit(-1); } 277 | *++e = ADD; 278 | *++e = ((ty = t - PTR) == CHAR) ? LC : LI; 279 | } 280 | else { printf("%d: compiler error tk=%d\n", line, tk); exit(-1); } 281 | } 282 | } 283 | 284 | void stmt() 285 | { 286 | int *a, *b; 287 | 288 | if (tk == If) { 289 | next(); 290 | if (tk == '(') next(); else { printf("%d: open paren expected\n", line); exit(-1); } 291 | expr(Assign); 292 | if (tk == ')') next(); else { printf("%d: close paren expected\n", line); exit(-1); } 293 | *++e = BZ; b = ++e; 294 | stmt(); 295 | if (tk == Else) { 296 | *b = (int)(e + 3); *++e = JMP; b = ++e; 297 | next(); 298 | stmt(); 299 | } 300 | *b = (int)(e + 1); 301 | } 302 | else if (tk == While) { 303 | next(); 304 | a = e + 1; 305 | if (tk == '(') next(); else { printf("%d: open paren expected\n", line); exit(-1); } 306 | expr(Assign); 307 | if (tk == ')') next(); else { printf("%d: close paren expected\n", line); exit(-1); } 308 | *++e = BZ; b = ++e; 309 | stmt(); 310 | *++e = JMP; *++e = (int)a; 311 | *b = (int)(e + 1); 312 | } 313 | else if (tk == Return) { 314 | next(); 315 | if (tk != ';') expr(Assign); 316 | *++e = LEV; 317 | if (tk == ';') next(); else { printf("%d: semicolon expected\n", line); exit(-1); } 318 | } 319 | else if (tk == '{') { 320 | next(); 321 | while (tk != '}') stmt(); 322 | next(); 323 | } 324 | else if (tk == ';') { 325 | next(); 326 | } 327 | else { 328 | expr(Assign); 329 | if (tk == ';') next(); else { printf("%d: semicolon expected\n", line); exit(-1); } 330 | } 331 | } 332 | 333 | int main(int argc, char **argv) 334 | { 335 | int fd, bt, ty, poolsz, *idmain; 336 | int *pc, *sp, *bp, a, cycle; // vm registers 337 | int i, *t; // temps 338 | 339 | --argc; ++argv; 340 | if (argc > 0 && **argv == '-' && (*argv)[1] == 's') { src = 1; --argc; ++argv; } 341 | if (argc > 0 && **argv == '-' && (*argv)[1] == 'd') { debug = 1; --argc; ++argv; } 342 | if (argc < 1) { printf("usage: c4 [-s] [-d] file ...\n"); return -1; } 343 | 344 | if ((fd = open(*argv, 0)) < 0) { printf("could not open(%s)\n", *argv); return -1; } 345 | 346 | poolsz = 256*1024; // arbitrary size 347 | if (!(sym = malloc(poolsz))) { printf("could not malloc(%d) symbol area\n", poolsz); return -1; } 348 | if (!(le = e = malloc(poolsz))) { printf("could not malloc(%d) text area\n", poolsz); return -1; } 349 | if (!(data = malloc(poolsz))) { printf("could not malloc(%d) data area\n", poolsz); return -1; } 350 | if (!(sp = malloc(poolsz))) { printf("could not malloc(%d) stack area\n", poolsz); return -1; } 351 | 352 | memset(sym, 0, poolsz); 353 | memset(e, 0, poolsz); 354 | memset(data, 0, poolsz); 355 | 356 | p = "char else enum if int return sizeof while " 357 | "open read close printf malloc memset memcmp memcpy mmap dlsym qsort exit void main"; 358 | i = Char; while (i <= While) { next(); id[Tk] = i++; } // add keywords to symbol table 359 | i = OPEN; while (i <= EXIT) { next(); id[Class] = Sys; id[Type] = INT; id[Val] = i++; } // add library to symbol table 360 | next(); id[Tk] = Char; // handle void type 361 | next(); idmain = id; // keep track of main 362 | 363 | if (!(lp = p = malloc(poolsz))) { printf("could not malloc(%d) source area\n", poolsz); return -1; } 364 | if ((i = read(fd, p, poolsz-1)) <= 0) { printf("read() returned %d\n", i); return -1; } 365 | p[i] = 0; 366 | close(fd); 367 | 368 | // parse declarations 369 | line = 1; 370 | next(); 371 | while (tk) { 372 | bt = INT; // basetype 373 | if (tk == Int) next(); 374 | else if (tk == Char) { next(); bt = CHAR; } 375 | else if (tk == Enum) { 376 | next(); 377 | if (tk != '{') next(); 378 | if (tk == '{') { 379 | next(); 380 | i = 0; 381 | while (tk != '}') { 382 | if (tk != Id) { printf("%d: bad enum identifier %d\n", line, tk); return -1; } 383 | next(); 384 | if (tk == Assign) { 385 | next(); 386 | if (tk != Num) { printf("%d: bad enum initializer\n", line); return -1; } 387 | i = ival; 388 | next(); 389 | } 390 | id[Class] = Num; id[Type] = INT; id[Val] = i++; 391 | if (tk == ',') next(); 392 | } 393 | next(); 394 | } 395 | } 396 | while (tk != ';' && tk != '}') { 397 | ty = bt; 398 | while (tk == Mul) { next(); ty = ty + PTR; } 399 | if (tk != Id) { printf("%d: bad global declaration\n", line); return -1; } 400 | if (id[Class]) { printf("%d: duplicate global definition\n", line); return -1; } 401 | next(); 402 | id[Type] = ty; 403 | if (tk == '(') { // function 404 | id[Class] = Fun; 405 | id[Val] = (int)(e + 1); 406 | next(); i = 0; 407 | while (tk != ')') { 408 | ty = INT; 409 | if (tk == Int) next(); 410 | else if (tk == Char) { next(); ty = CHAR; } 411 | while (tk == Mul) { next(); ty = ty + PTR; } 412 | if (tk != Id) { printf("%d: bad parameter declaration\n", line); return -1; } 413 | if (id[Class] == Loc) { printf("%d: duplicate parameter definition\n", line); return -1; } 414 | id[HClass] = id[Class]; id[Class] = Loc; 415 | id[HType] = id[Type]; id[Type] = ty; 416 | id[HVal] = id[Val]; id[Val] = i++; 417 | next(); 418 | if (tk == ',') next(); 419 | } 420 | next(); 421 | if (tk != '{') { printf("%d: bad function definition\n", line); return -1; } 422 | loc = ++i; 423 | next(); 424 | while (tk == Int || tk == Char) { 425 | bt = (tk == Int) ? INT : CHAR; 426 | next(); 427 | while (tk != ';') { 428 | ty = bt; 429 | while (tk == Mul) { next(); ty = ty + PTR; } 430 | if (tk != Id) { printf("%d: bad local declaration\n", line); return -1; } 431 | if (id[Class] == Loc) { printf("%d: duplicate local definition\n", line); return -1; } 432 | id[HClass] = id[Class]; id[Class] = Loc; 433 | id[HType] = id[Type]; id[Type] = ty; 434 | id[HVal] = id[Val]; id[Val] = ++i; 435 | next(); 436 | if (tk == ',') next(); 437 | } 438 | next(); 439 | } 440 | *++e = ENT; *++e = i - loc; 441 | while (tk != '}') stmt(); 442 | *++e = LEV; 443 | id = sym; // unwind symbol table locals 444 | while (id[Tk]) { 445 | if (id[Class] == Loc) { 446 | id[Class] = id[HClass]; 447 | id[Type] = id[HType]; 448 | id[Val] = id[HVal]; 449 | } 450 | id = id + Idsz; 451 | } 452 | } 453 | else { 454 | id[Class] = Glo; 455 | id[Val] = (int)data; 456 | data = data + sizeof(int); 457 | } 458 | if (tk == ',') next(); 459 | } 460 | next(); 461 | } 462 | 463 | if (!(pc = (int *)idmain[Val])) { printf("main() not defined\n"); return -1; } 464 | if (src) return 0; 465 | 466 | // setup stack 467 | sp = (int *)((int)sp + poolsz); 468 | *--sp = EXIT; // call exit if main returns 469 | *--sp = PSH; t = sp; 470 | *--sp = argc; 471 | *--sp = (int)argv; 472 | *--sp = (int)t; 473 | 474 | // run... 475 | cycle = 0; 476 | while (1) { 477 | i = *pc++; ++cycle; 478 | if (debug) { 479 | printf("%d> %.4s", cycle, 480 | &"LEA ,IMM ,JMP ,JSR ,BZ ,BNZ ,ENT ,ADJ ,LEV ,LI ,LC ,SI ,SC ,PSH ," 481 | "OR ,XOR ,AND ,EQ ,NE ,LT ,GT ,LE ,GE ,SHL ,SHR ,ADD ,SUB ,MUL ,DIV ,MOD ," 482 | "OPEN,READ,CLOS,PRTF,MALC,MSET,MCMP,MCPY,MMAP,DSYM,QSRT,EXIT,"[i * 5]); 483 | if (i <= ADJ) printf(" %d\n", *pc); else printf("\n"); 484 | } 485 | if (i == LEA) a = (int)(bp + *pc++); // load local address 486 | else if (i == IMM) a = *pc++; // load global address or immediate 487 | else if (i == JMP) pc = (int *)*pc; // jump 488 | else if (i == JSR) { *--sp = (int)(pc + 1); pc = (int *)*pc; } // jump to subroutine 489 | else if (i == BZ) pc = a ? pc + 1 : (int *)*pc; // branch if zero 490 | else if (i == BNZ) pc = a ? (int *)*pc : pc + 1; // branch if not zero 491 | else if (i == ENT) { *--sp = (int)bp; bp = sp; sp = sp - *pc++; } // enter subroutine 492 | else if (i == ADJ) sp = sp + *pc++; // stack adjust 493 | else if (i == LEV) { sp = bp; bp = (int *)*sp++; pc = (int *)*sp++; } // leave subroutine 494 | else if (i == LI) a = *(int *)a; // load int 495 | else if (i == LC) a = *(char *)a; // load char 496 | else if (i == SI) *(int *)*sp++ = a; // store int 497 | else if (i == SC) a = *(char *)*sp++ = a; // store char 498 | else if (i == PSH) *--sp = a; // push 499 | 500 | else if (i == OR) a = *sp++ | a; 501 | else if (i == XOR) a = *sp++ ^ a; 502 | else if (i == AND) a = *sp++ & a; 503 | else if (i == EQ) a = *sp++ == a; 504 | else if (i == NE) a = *sp++ != a; 505 | else if (i == LT) a = *sp++ < a; 506 | else if (i == GT) a = *sp++ > a; 507 | else if (i == LE) a = *sp++ <= a; 508 | else if (i == GE) a = *sp++ >= a; 509 | else if (i == SHL) a = *sp++ << a; 510 | else if (i == SHR) a = *sp++ >> a; 511 | else if (i == ADD) a = *sp++ + a; 512 | else if (i == SUB) a = *sp++ - a; 513 | else if (i == MUL) a = *sp++ * a; 514 | else if (i == DIV) a = *sp++ / a; 515 | else if (i == MOD) a = *sp++ % a; 516 | 517 | else if (i == OPEN) a = open((char *)sp[1], *sp); 518 | else if (i == READ) a = read(sp[2], (char *)sp[1], *sp); 519 | else if (i == CLOS) a = close(*sp); 520 | else if (i == PRTF) { t = sp + pc[1]; a = printf((char *)t[-1], t[-2], t[-3], t[-4], t[-5], t[-6]); } 521 | else if (i == MALC) a = (int)malloc(*sp); 522 | else if (i == MSET) a = (int)memset((char *)sp[2], sp[1], *sp); 523 | else if (i == MCMP) a = memcmp((char *)sp[2], (char *)sp[1], *sp); 524 | else if (i == MCPY) a = (int)memcpy((char *)sp[2], (char *)sp[1], *sp); 525 | else if (i == MMAP) a = (int)mmap((char *)sp[5], sp[4], sp[3], sp[2], sp[1], *sp); 526 | else if (i == DSYM) a = (int)dlsym((char *)sp[1], (char *)*sp); 527 | else if (i == QSRT) qsort((char *)sp[3], sp[2], sp[1], (void *)*sp); 528 | else if (i == EXIT) { printf("exit(%d) cycle = %d\n", *sp, cycle); return *sp; } 529 | else { printf("unknown instruction = %d! cycle = %d\n", i, cycle); return -1; } 530 | } 531 | } 532 | -------------------------------------------------------------------------------- /c4x86.c: -------------------------------------------------------------------------------- 1 | // c4.c - C in four functions 2 | 3 | // char, int, and pointer types 4 | // if, while, return, and expression statements 5 | // just enough features to allow self-compilation and a bit more 6 | 7 | // Written by Robert Swierczek 8 | // + x86 JIT compiler by Dmytro Sirenko 9 | 10 | #include 11 | #include 12 | #include 13 | #ifndef _WIN32 14 | #include 15 | #include 16 | #else 17 | #include "mman32.h" 18 | #include "dlfcn32.h" 19 | #define CHAR TYCHAR 20 | #define INT TYINT 21 | #endif 22 | 23 | char *p, *lp, // current position in source code 24 | *jitmem, // executable memory for JIT-compiled native code 25 | *data, // data/bss pointer 26 | **linemap; // maps a line number into its source position 27 | 28 | int *e, *le, *text, // current position in emitted code 29 | *id, // currently parsed indentifier 30 | *sym, // symbol table (simple list of identifiers) 31 | tk, // current token 32 | ival, // current token value 33 | ty, // current expression type 34 | loc, // local variable offset 35 | line, // current line number 36 | *srcmap, // maps a bytecode into its corresponding source line number 37 | src; // print source, c4 assembly and JIT addresses 38 | 39 | enum Token { 40 | Num = 128, Fun, Sys, Glo, Loc, Id, 41 | Char, Else, Enum, If, Int, Return, Sizeof, While, 42 | Assign, Cond, Lor, Lan, Or, Xor, And, Eq, Ne, Lt, Gt, Le, Ge, Shl, Shr, Add, Sub, Mul, Div, Mod, Inc, Dec, Brak 43 | }; 44 | 45 | enum Opcode { 46 | LEA ,IMM ,JMP ,JSR ,BZ ,BNZ ,ENT ,ADJ ,LEV ,LI ,LC ,SI ,SC ,PSH , 47 | OR ,XOR ,AND ,EQ ,NE ,LT ,GT ,LE ,GE ,SHL ,SHR ,ADD ,SUB ,MUL ,DIV ,MOD , 48 | OPEN,READ,CLOS,PRTF,MALC,MSET,MCMP,MCPY,MMAP,DOPN,DSYM,QSRT,EXIT 49 | }; 50 | 51 | enum Ty { CHAR, INT, PTR }; 52 | 53 | // identifier offsets (since we can't create an ident struct) 54 | enum Identifier { Tk, Hash, Name, Class, Type, Val, HClass, HType, HVal, Idsz }; 55 | 56 | void next() 57 | { 58 | char *pp; 59 | 60 | while (tk = *p) { 61 | ++p; 62 | if (tk == '\n') { 63 | if (src) { 64 | linemap[line] = lp; 65 | while (le < e) { srcmap[le - text] = line; le++; }; 66 | } 67 | lp = p; 68 | ++line; 69 | } 70 | else if (tk == '#') { 71 | while (*p != 0 && *p != '\n') ++p; 72 | } 73 | else if ((tk >= 'a' && tk <= 'z') || (tk >= 'A' && tk <= 'Z') || tk == '_') { 74 | pp = p - 1; 75 | while ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || (*p >= '0' && *p <= '9') || *p == '_') 76 | tk = tk * 147 + *p++; 77 | tk = (tk << 6) + (p - pp); 78 | id = sym; 79 | while (id[Tk]) { 80 | if (tk == id[Hash] && !memcmp((char *)id[Name], pp, p - pp)) { tk = id[Tk]; return; } 81 | id = id + Idsz; 82 | } 83 | id[Name] = (int)pp; 84 | id[Hash] = tk; 85 | tk = id[Tk] = Id; 86 | return; 87 | } 88 | else if (tk >= '0' && tk <= '9') { 89 | if (ival = tk - '0') { while (*p >= '0' && *p <= '9') ival = ival * 10 + *p++ - '0'; } 90 | else if (*p == 'x' || *p == 'X') { 91 | while ((tk = *++p) && ((tk >= '0' && tk <= '9') || (tk >= 'a' && tk <= 'f') || (tk >= 'A' && tk <= 'F'))) 92 | ival = ival * 16 + (tk & 15) + (tk >= 'A' ? 9 : 0); 93 | } 94 | else { while (*p >= '0' && *p <= '7') ival = ival * 8 + *p++ - '0'; } 95 | tk = Num; 96 | return; 97 | } 98 | else if (tk == '/') { 99 | if (*p == '/') { 100 | ++p; 101 | while (*p != 0 && *p != '\n') ++p; 102 | } 103 | else { 104 | tk = Div; 105 | return; 106 | } 107 | } 108 | else if (tk == '\'' || tk == '"') { 109 | pp = data; 110 | while (*p != 0 && *p != tk) { 111 | if ((ival = *p++) == '\\') { 112 | if ((ival = *p++) == 'n') ival = '\n'; 113 | } 114 | if (tk == '"') *data++ = ival; 115 | } 116 | ++p; 117 | if (tk == '"') ival = (int)pp; else tk = Num; 118 | return; 119 | } 120 | else if (tk == '=') { if (*p == '=') { ++p; tk = Eq; } else tk = Assign; return; } 121 | else if (tk == '+') { if (*p == '+') { ++p; tk = Inc; } else tk = Add; return; } 122 | else if (tk == '-') { if (*p == '-') { ++p; tk = Dec; } else tk = Sub; return; } 123 | else if (tk == '!') { if (*p == '=') { ++p; tk = Ne; } return; } 124 | else if (tk == '<') { if (*p == '=') { ++p; tk = Le; } else if (*p == '<') { ++p; tk = Shl; } else tk = Lt; return; } 125 | else if (tk == '>') { if (*p == '=') { ++p; tk = Ge; } else if (*p == '>') { ++p; tk = Shr; } else tk = Gt; return; } 126 | else if (tk == '|') { if (*p == '|') { ++p; tk = Lor; } else tk = Or; return; } 127 | else if (tk == '&') { if (*p == '&') { ++p; tk = Lan; } else tk = And; return; } 128 | else if (tk == '^') { tk = Xor; return; } 129 | else if (tk == '%') { tk = Mod; return; } 130 | else if (tk == '*') { tk = Mul; return; } 131 | else if (tk == '[') { tk = Brak; return; } 132 | else if (tk == '?') { tk = Cond; return; } 133 | else if (tk == '~' || tk == ';' || tk == '{' || tk == '}' || tk == '(' || tk == ')' || tk == ']' || tk == ',' || tk == ':') return; 134 | } 135 | } 136 | 137 | void expr(int lev) 138 | { 139 | int t, *d; 140 | 141 | if (!tk) { printf("%d: unexpected eof in expression\n", line); exit(-1); } 142 | else if (tk == Num) { *++e = IMM; *++e = ival; next(); ty = INT; } 143 | else if (tk == '"') { 144 | *++e = IMM; *++e = ival; next(); 145 | while (tk == '"') next(); 146 | data = (char *)((int)data + sizeof(int) & -sizeof(int)); ty = PTR; 147 | } 148 | else if (tk == Sizeof) { 149 | next(); if (tk == '(') next(); else { printf("%d: open paren expected in sizeof\n", line); exit(-1); } 150 | ty = INT; if (tk == Int) next(); else if (tk == Char) { next(); ty = CHAR; } 151 | while (tk == Mul) { next(); ty = ty + PTR; } 152 | if (tk == ')') next(); else { printf("%d: close paren expected in sizeof\n", line); exit(-1); } 153 | *++e = IMM; *++e = (ty == CHAR) ? sizeof(char) : sizeof(int); 154 | ty = INT; 155 | } 156 | else if (tk == Id) { 157 | d = id; next(); 158 | if (tk == '(') { 159 | next(); 160 | t = 0; 161 | while (tk != ')') { expr(Assign); *++e = PSH; ++t; if (tk == ',') next(); } 162 | next(); 163 | if (d[Class] == Sys) *++e = d[Val]; 164 | else if (d[Class] == Fun) { *++e = JSR; *++e = d[Val]; } 165 | else { printf("%d: bad function call\n", line); exit(-1); } 166 | if (t) { *++e = ADJ; *++e = t; } 167 | ty = d[Type]; 168 | } 169 | else if (d[Class] == Num) { *++e = IMM; *++e = d[Val]; ty = INT; } 170 | else { 171 | if (d[Class] == Loc) { *++e = LEA; *++e = loc - d[Val]; } 172 | else if (d[Class] == Glo) { *++e = IMM; *++e = d[Val]; } 173 | else { printf("%d: undefined variable\n", line); exit(-1); } 174 | *++e = ((ty = d[Type]) == CHAR) ? LC : LI; 175 | } 176 | } 177 | else if (tk == '(') { 178 | next(); 179 | if (tk == Int || tk == Char) { 180 | t = (tk == Int) ? INT : CHAR; next(); 181 | while (tk == Mul) { next(); t = t + PTR; } 182 | if (tk == ')') next(); else { printf("%d: bad cast\n", line); exit(-1); } 183 | expr(Inc); 184 | ty = t; 185 | } 186 | else { 187 | expr(Assign); 188 | if (tk == ')') next(); else { printf("%d: close paren expected\n", line); exit(-1); } 189 | } 190 | } 191 | else if (tk == Mul) { 192 | next(); expr(Inc); 193 | if (ty > INT) ty = ty - PTR; else { printf("%d: bad dereference\n", line); exit(-1); } 194 | *++e = (ty == CHAR) ? LC : LI; 195 | } 196 | else if (tk == And) { 197 | next(); expr(Inc); 198 | if (*e == LC || *e == LI) --e; else { printf("%d: bad address-of\n", line); exit(-1); } 199 | ty = ty + PTR; 200 | } 201 | else if (tk == '!') { next(); expr(Inc); *++e = PSH; *++e = IMM; *++e = 0; *++e = EQ; ty = INT; } 202 | else if (tk == '~') { next(); expr(Inc); *++e = PSH; *++e = IMM; *++e = -1; *++e = XOR; ty = INT; } 203 | else if (tk == Add) { next(); expr(Inc); ty = INT; } 204 | else if (tk == Sub) { 205 | next(); *++e = IMM; 206 | if (tk == Num) { *++e = -ival; next(); } else { *++e = -1; *++e = PSH; expr(Inc); *++e = MUL; } 207 | ty = INT; 208 | } 209 | else if (tk == Inc || tk == Dec) { 210 | t = tk; next(); expr(Inc); 211 | if (*e == LC) { *e = PSH; *++e = LC; } 212 | else if (*e == LI) { *e = PSH; *++e = LI; } 213 | else { printf("%d: bad lvalue in pre-increment\n", line); exit(-1); } 214 | *++e = PSH; 215 | *++e = IMM; *++e = (ty > PTR) ? sizeof(int) : sizeof(char); 216 | *++e = (t == Inc) ? ADD : SUB; 217 | *++e = (ty == CHAR) ? SC : SI; 218 | } 219 | else { printf("%d: bad expression\n", line); exit(-1); } 220 | 221 | while (tk >= lev) { // "precedence climbing" or "Top Down Operator Precedence" method 222 | t = ty; 223 | if (tk == Assign) { 224 | next(); 225 | if (*e == LC || *e == LI) *e = PSH; else { printf("%d: bad lvalue in assignment\n", line); exit(-1); } 226 | expr(Assign); *++e = ((ty = t) == CHAR) ? SC : SI; 227 | } 228 | else if (tk == Cond) { 229 | next(); 230 | *++e = BZ; d = ++e; 231 | expr(Assign); 232 | if (tk == ':') next(); else { printf("%d: conditional missing colon\n", line); exit(-1); } 233 | *d = (int)(e + 3); *++e = JMP; d = ++e; 234 | expr(Cond); 235 | *d = (int)(e + 1); 236 | } 237 | else if (tk == Lor) { next(); *++e = BNZ; d = ++e; expr(Lan); *d = (int)(e + 1); ty = INT; } 238 | else if (tk == Lan) { next(); *++e = BZ; d = ++e; expr(Or); *d = (int)(e + 1); ty = INT; } 239 | else if (tk == Or) { next(); *++e = PSH; expr(Xor); *++e = OR; ty = INT; } 240 | else if (tk == Xor) { next(); *++e = PSH; expr(And); *++e = XOR; ty = INT; } 241 | else if (tk == And) { next(); *++e = PSH; expr(Eq); *++e = AND; ty = INT; } 242 | else if (tk == Eq) { next(); *++e = PSH; expr(Lt); *++e = EQ; ty = INT; } 243 | else if (tk == Ne) { next(); *++e = PSH; expr(Lt); *++e = NE; ty = INT; } 244 | else if (tk == Lt) { next(); *++e = PSH; expr(Shl); *++e = LT; ty = INT; } 245 | else if (tk == Gt) { next(); *++e = PSH; expr(Shl); *++e = GT; ty = INT; } 246 | else if (tk == Le) { next(); *++e = PSH; expr(Shl); *++e = LE; ty = INT; } 247 | else if (tk == Ge) { next(); *++e = PSH; expr(Shl); *++e = GE; ty = INT; } 248 | else if (tk == Shl) { next(); *++e = PSH; expr(Add); *++e = SHL; ty = INT; } 249 | else if (tk == Shr) { next(); *++e = PSH; expr(Add); *++e = SHR; ty = INT; } 250 | else if (tk == Add) { 251 | next(); *++e = PSH; expr(Mul); 252 | if ((ty = t) > PTR) { *++e = PSH; *++e = IMM; *++e = sizeof(int); *++e = MUL; } 253 | *++e = ADD; 254 | } 255 | else if (tk == Sub) { 256 | next(); *++e = PSH; expr(Mul); 257 | if ((ty = t) > PTR) { *++e = PSH; *++e = IMM; *++e = sizeof(int); *++e = MUL; } 258 | *++e = SUB; 259 | } 260 | else if (tk == Mul) { next(); *++e = PSH; expr(Inc); *++e = MUL; ty = INT; } 261 | else if (tk == Div) { next(); *++e = PSH; expr(Inc); *++e = DIV; ty = INT; } 262 | else if (tk == Mod) { next(); *++e = PSH; expr(Inc); *++e = MOD; ty = INT; } 263 | else if (tk == Inc || tk == Dec) { 264 | if (*e == LC) { *e = PSH; *++e = LC; } 265 | else if (*e == LI) { *e = PSH; *++e = LI; } 266 | else { printf("%d: bad lvalue in post-increment\n", line); exit(-1); } 267 | *++e = PSH; *++e = IMM; *++e = (ty > PTR) ? sizeof(int) : sizeof(char); 268 | *++e = (tk == Inc) ? ADD : SUB; 269 | *++e = (ty == CHAR) ? SC : SI; 270 | *++e = PSH; *++e = IMM; *++e = (ty > PTR) ? sizeof(int) : sizeof(char); 271 | *++e = (tk == Inc) ? SUB : ADD; 272 | next(); 273 | } 274 | else if (tk == Brak) { 275 | next(); *++e = PSH; expr(Assign); 276 | if (tk == ']') next(); else { printf("%d: close bracket expected\n", line); exit(-1); } 277 | if (t > PTR) { *++e = PSH; *++e = IMM; *++e = sizeof(int); *++e = MUL; } 278 | else if (t < PTR) { printf("%d: pointer type expected\n", line); exit(-1); } 279 | *++e = ADD; 280 | *++e = ((ty = t - PTR) == CHAR) ? LC : LI; 281 | } 282 | else { printf("%d: compiler error tk=%d\n", line, tk); exit(-1); } 283 | } 284 | } 285 | 286 | void stmt() 287 | { 288 | int *a, *b; 289 | 290 | if (tk == If) { 291 | next(); 292 | if (tk == '(') next(); else { printf("%d: open paren expected\n", line); exit(-1); } 293 | expr(Assign); 294 | if (tk == ')') next(); else { printf("%d: close paren expected\n", line); exit(-1); } 295 | *++e = BZ; b = ++e; 296 | stmt(); 297 | if (tk == Else) { 298 | *b = (int)(e + 3); *++e = JMP; b = ++e; 299 | next(); 300 | stmt(); 301 | } 302 | *b = (int)(e + 1); 303 | } 304 | else if (tk == While) { 305 | next(); 306 | a = e + 1; 307 | if (tk == '(') next(); else { printf("%d: open paren expected\n", line); exit(-1); } 308 | expr(Assign); 309 | if (tk == ')') next(); else { printf("%d: close paren expected\n", line); exit(-1); } 310 | *++e = BZ; b = ++e; 311 | stmt(); 312 | *++e = JMP; *++e = (int)a; 313 | *b = (int)(e + 1); 314 | } 315 | else if (tk == Return) { 316 | next(); 317 | if (tk != ';') expr(Assign); 318 | *++e = LEV; 319 | if (tk == ';') next(); else { printf("%d: semicolon expected\n", line); exit(-1); } 320 | } 321 | else if (tk == '{') { 322 | next(); 323 | while (tk != '}') stmt(); 324 | next(); 325 | } 326 | else if (tk == ';') { 327 | next(); 328 | } 329 | else { 330 | expr(Assign); 331 | if (tk == ';') next(); else { printf("%d: semicolon expected\n", line); exit(-1); } 332 | } 333 | } 334 | 335 | int main(int argc, char **argv) 336 | { 337 | int fd, bt, ty, poolsz, *idmain; 338 | int *pc; 339 | int i, tmp; // temps 340 | void *dl; 341 | int (*jitmain)(); 342 | char *je, // current position in emitted native code 343 | **jitmap; // maps c4 bytecode index into native code position 344 | 345 | --argc; ++argv; 346 | if (argc > 0 && **argv == '-' && (*argv)[1] == 's') { src = 1; --argc; ++argv; } 347 | if (argc < 1) { printf("usage: c4x86 [-s] file ...\n"); return -1; } 348 | 349 | if ((fd = open(*argv, 0)) < 0) { printf("could not open(%s)\n", *argv); return -1; } 350 | 351 | poolsz = 256*1024; // arbitrary size 352 | if (!(sym = malloc(poolsz))) { printf("could not malloc(%d) symbol area\n", poolsz); return -1; } 353 | if (!(text = le = e = malloc(poolsz))) { printf("could not malloc(%d) text area\n", poolsz); return -1; } 354 | if (!(data = malloc(poolsz))) { printf("could not malloc(%d) data area\n", poolsz); return -1; } 355 | 356 | memset(sym, 0, poolsz); 357 | memset(e, 0, poolsz); 358 | memset(data, 0, poolsz); 359 | 360 | p = "char else enum if int return sizeof while " 361 | "open read close printf malloc memset memcmp memcpy mmap dlopen dlsym qsort exit void main"; 362 | i = Char; while (i <= While) { next(); id[Tk] = i++; } // add keywords to symbol table 363 | i = OPEN; while (i <= EXIT) { next(); id[Class] = Sys; id[Type] = INT; id[Val] = i++; } // add library to symbol table 364 | next(); id[Tk] = Char; // handle void type 365 | next(); idmain = id; // keep track of main 366 | 367 | if (!(lp = p = malloc(poolsz))) { printf("could not malloc(%d) source area\n", poolsz); return -1; } 368 | if ((i = read(fd, p, poolsz-1)) <= 0) { printf("read() returned %d\n", i); return -1; } 369 | close(fd); 370 | p[i] = 0; 371 | if (src) { 372 | linemap = (char **)(((int)(p + i + 1) & 0xffffff00) + 0x100); 373 | srcmap = text + (poolsz / 8); 374 | } 375 | 376 | // parse declarations 377 | line = 1; 378 | next(); 379 | while (tk) { 380 | bt = INT; // basetype 381 | if (tk == Int) next(); 382 | else if (tk == Char) { next(); bt = CHAR; } 383 | else if (tk == Enum) { 384 | next(); 385 | if (tk != '{') next(); 386 | if (tk == '{') { 387 | next(); 388 | i = 0; 389 | while (tk != '}') { 390 | if (tk != Id) { printf("%d: bad enum identifier %d\n", line, tk); return -1; } 391 | next(); 392 | if (tk == Assign) { 393 | next(); 394 | if (tk != Num) { printf("%d: bad enum initializer\n", line); return -1; } 395 | i = ival; 396 | next(); 397 | } 398 | id[Class] = Num; id[Type] = INT; id[Val] = i++; 399 | if (tk == ',') next(); 400 | } 401 | next(); 402 | } 403 | } 404 | while (tk != ';' && tk != '}') { 405 | ty = bt; 406 | while (tk == Mul) { next(); ty = ty + PTR; } 407 | if (tk != Id) { printf("%d: bad global declaration\n", line); return -1; } 408 | if (id[Class]) { printf("%d: duplicate global definition\n", line); return -1; } 409 | next(); 410 | id[Type] = ty; 411 | if (tk == '(') { // function 412 | id[Class] = Fun; 413 | id[Val] = (int)(e + 1); 414 | next(); i = 0; 415 | while (tk != ')') { 416 | ty = INT; 417 | if (tk == Int) next(); 418 | else if (tk == Char) { next(); ty = CHAR; } 419 | while (tk == Mul) { next(); ty = ty + PTR; } 420 | if (tk != Id) { printf("%d: bad parameter declaration\n", line); return -1; } 421 | if (id[Class] == Loc) { printf("%d: duplicate parameter definition\n", line); return -1; } 422 | id[HClass] = id[Class]; id[Class] = Loc; 423 | id[HType] = id[Type]; id[Type] = ty; 424 | id[HVal] = id[Val]; id[Val] = i++; 425 | next(); 426 | if (tk == ',') next(); 427 | } 428 | next(); 429 | if (tk != '{') { printf("%d: bad function definition\n", line); return -1; } 430 | loc = ++i; 431 | next(); 432 | while (tk == Int || tk == Char) { 433 | bt = (tk == Int) ? INT : CHAR; 434 | next(); 435 | while (tk != ';') { 436 | ty = bt; 437 | while (tk == Mul) { next(); ty = ty + PTR; } 438 | if (tk != Id) { printf("%d: bad local declaration\n", line); return -1; } 439 | if (id[Class] == Loc) { printf("%d: duplicate local definition\n", line); return -1; } 440 | id[HClass] = id[Class]; id[Class] = Loc; 441 | id[HType] = id[Type]; id[Type] = ty; 442 | id[HVal] = id[Val]; id[Val] = ++i; 443 | next(); 444 | if (tk == ',') next(); 445 | } 446 | next(); 447 | } 448 | *++e = ENT; *++e = i - loc; 449 | while (tk != '}') stmt(); 450 | *++e = LEV; 451 | id = sym; // unwind symbol table locals 452 | while (id[Tk]) { 453 | if (id[Class] == Loc) { 454 | id[Class] = id[HClass]; 455 | id[Type] = id[HType]; 456 | id[Val] = id[HVal]; 457 | } 458 | id = id + Idsz; 459 | } 460 | } 461 | else { 462 | id[Class] = Glo; 463 | id[Val] = (int)data; 464 | data = data + sizeof(int); 465 | } 466 | if (tk == ',') next(); 467 | } 468 | next(); 469 | } 470 | 471 | dl = dlopen(0, RTLD_LAZY | RTLD_GLOBAL); // RTLD_LAZY = 1 472 | 473 | // setup jit memory 474 | //jitmem = mmap(0, poolsz, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); 475 | jitmem = mmap(0, poolsz, 7, 0x1002 | MAP_ANON, -1, 0); 476 | if (!jitmem) { printf("could not mmap(%d) jit executable memory\n", poolsz); return -1; } 477 | 478 | jitmap = (char **)(jitmem + poolsz / 2); 479 | 480 | // first pass: emit native code 481 | pc = text + 1; je = jitmem; line = 0; 482 | while (pc <= e) { 483 | i = *pc; 484 | if (src) { 485 | while (line < srcmap[pc - text]) { 486 | line++; printf("% 4d | %.*s", line, linemap[line + 1] - linemap[line], linemap[line]); 487 | } 488 | printf("0x%05x (%p):\t%8.4s", pc - text, je, 489 | &"LEA ,IMM ,JMP ,JSR ,BZ ,BNZ ,ENT ,ADJ ,LEV ,LI ,LC ,SI ,SC ,PSH ," 490 | "OR ,XOR ,AND ,EQ ,NE ,LT ,GT ,LE ,GE ,SHL ,SHR ,ADD ,SUB ,MUL ,DIV ,MOD ," 491 | "OPEN,READ,CLOS,PRTF,MALC,MSET,MCMP,MCPY,MMAP,DOPN,DSYM,QSRT,EXIT,"[i * 5]); 492 | if (i <= ADJ) printf(" 0x%x\n", *(pc + 1)); else printf("\n"); 493 | } 494 | jitmap[pc - text] = je; // for later relocation of JMP/JSR/BZ/BNZ 495 | pc++; 496 | if (i == LEA) { 497 | i = 4 * *pc++; if (i < -128 || i > 127) { printf("jit: LEA out of bounds\n"); return -1; } 498 | *(int*)je = 0x458d; je = je + 2; *je++ = i; // leal $(4 * n)(%ebp), %eax 499 | } 500 | else if (i == ENT) { 501 | i = 4 * *pc++; if (i < -128 || i > 127) { printf("jit: ENT out of bounds\n"); return -1; } 502 | *(int *)je = 0xe58955; je = je + 3; // push %ebp; movl %esp, %ebp 503 | if (i > 0) { *(int *)je = 0xec83; je = je + 2; *(int*)je++ = i; } // subl $(i*4), %esp 504 | } 505 | else if (i == IMM) { *je++ = 0xb8; *(int *)je = *pc++; je = je + 4; } // movl $imm, %eax 506 | else if (i == ADJ) { i = 4 * *pc++; *(int *)je = 0xc483; je = je + 2; *(int *)je = i; je++; } // addl $(n * 4), %esp 507 | else if (i == PSH) *(int *)je++ = 0x50; // push %eax 508 | else if (i == LEV) { *(int *)je = 0xc35dec89; je = je + 4; } // mov %ebp, %esp; pop %ebp; ret 509 | else if (i == LI) { *(int *)je = 0x008b; je = je + 2; } // movl (%eax), %eax 510 | else if (i == LC) { *(int *)je = 0x00b60f; je = je + 3; } // movzbl (%eax), %eax 511 | else if (i == SI) { *(int *)je = 0x018959; je = je + 3; } // pop %ecx; movl %eax, (%ecx) 512 | else if (i == SC) { *(int *)je = 0x018859; je = je + 3; } // pop %ecx; movb %al, (%ecx) 513 | else if (i == OR) { *(int *)je = 0xc80959; je = je + 3; } // pop %ecx; orl %ecx, %eax 514 | else if (i == XOR) { *(int *)je = 0xc83159; je = je + 3; } // pop %ecx; xorl %ecx, %eax 515 | else if (i == AND) { *(int *)je = 0xc82159; je = je + 3; } // pop %ecx; andl %ecx, %eax 516 | else if (EQ <= i && i <= GE) { 517 | *(int*)je=0x0fc13959; je = je + 4; *(int*)je=0x9866c094; // pop %ecx; cmp %ecx, %eax; sete %al; cbw; - EQ 518 | if (i == NE) { *je = 0x95; } // setne %al 519 | else if (i == LT) { *je = 0x9c; } // setl %al 520 | else if (i == GT) { *je = 0x9f; } // setg %al 521 | else if (i == LE) { *je = 0x9e; } // setle %al 522 | else if (i == GE) { *je = 0x9d; } // setge %al 523 | je=je+4; *je++=0x98; // cwde 524 | } 525 | else if (i == SHL) { *(int*)je = 0xe0d39159; je = je + 4; } // pop %ecx; xchg %eax, %ecx; shl %cl, %eax 526 | else if (i == SHR) { *(int*)je = 0xe8d39159; je = je + 4; } // pop %ecx; xchg %eax, %ecx; shr %cl, %eax 527 | else if (i == ADD) { *(int*)je = 0xc80159; je = je + 3; } // pop %ecx; addl %ecx, %eax 528 | else if (i == SUB) { *(int*)je = 0xc8299159; je = je + 4; } // pop %ecx; xchg %eax, %ecx; subl %ecx, %eax 529 | else if (i == MUL) { *(int*)je = 0xc1af0f59; je = je + 4; } // pop %ecx; imul %ecx, %eax 530 | else if (i == DIV) { *(int*)je = 0xf9f79159; je = je + 4; } // pop %ecx; xchg %eax, %ecx; idiv %ecx, %eax 531 | else if (i == MOD) { *(int*)je = 0xd2319159; je = je + 4; *(int *)je = 0x92f9f7; je = je + 3; } 532 | else if (i == JMP) { ++pc; *je = 0xe9; je = je + 5; } // jmp 533 | else if (i == JSR) { ++pc; *je = 0xe8; je = je + 5; } // call 534 | else if (i == BZ) { ++pc; *(int*)je = 0x840fc085; je = je + 8; } // test %eax, %eax; jz 535 | else if (i == BNZ) { ++pc; *(int*)je = 0x850fc085; je = je + 8; } // test %eax, %eax; jnz 536 | else if (i >= OPEN) { 537 | if (i == OPEN) tmp = (int)dlsym(dl, "open"); 538 | else if (i == READ) tmp = (int)dlsym(dl, "read"); 539 | else if (i == CLOS) tmp = (int)dlsym(dl, "close"); 540 | else if (i == PRTF) tmp = (int)dlsym(dl, "printf"); 541 | else if (i == MALC) tmp = (int)dlsym(dl, "malloc"); 542 | else if (i == MSET) tmp = (int)dlsym(dl, "memset"); 543 | else if (i == MCMP) tmp = (int)dlsym(dl, "memcmp"); 544 | else if (i == MCPY) tmp = (int)dlsym(dl, "memcpy"); 545 | else if (i == MMAP) tmp = (int)dlsym(dl, "mmap"); 546 | else if (i == DOPN) tmp = (int)dlsym(dl, "dlopen"); 547 | else if (i == DSYM) tmp = (int)dlsym(dl, "dlsym"); 548 | else if (i == QSRT) tmp = (int)dlsym(dl, "qsort"); 549 | else if (i == EXIT) tmp = (int)dlsym(dl, "exit"); 550 | 551 | if (*pc++ == ADJ) { i = *pc++; } else { printf("no ADJ after native proc!\n"); exit(2); } 552 | 553 | *je++ = 0xb9; *(int*)je = i << 2; je = je + 4; // movl $(4 * n), %ecx; 554 | *(int*)je = 0xce29e689; je = je + 4; // mov %esp, %esi; sub %ecx, %esi; -- %esi will adjust the stack 555 | *(int*)je = 0x8302e9c1; je = je + 4; // shr $2, %ecx; and -- alignment of %esp for OS X 556 | *(int*)je = 0x895af0e6; je = je + 4; // $0xfffffff0, %esi; pop %edx; mov.. 557 | *(int*)je = 0xe2fc8e54; je = je + 4; // ..%edx, -4(%esi,%ecx,4); loop.. -- reversing args order 558 | *(int*)je = 0xe8f487f9; je = je + 4; // ..<'pop' offset>; xchg %esi, %esp; call -- saving old stack in %esi 559 | *(int*)je = tmp - (int)(je + 4); je = je + 4; // <*tmp offset>; 560 | *(int*)je = 0xf487; je = je + 2; // xchg %esi, %esp -- ADJ, back to old stack without arguments 561 | } 562 | else { printf("code generation failed for %d!\n", i); return -1; } 563 | } 564 | 565 | // second pass, relocation 566 | pc = text + 1; 567 | while (pc <= e) { 568 | je = jitmap[pc - text]; 569 | i = *pc++; 570 | if (i == JSR || i == JMP || i == BZ || i == BNZ) { 571 | tmp = (int)jitmap[(int *)*pc++ - text]; 572 | if (i == JSR || i == JMP) { je = je + 1; *(int*)je = tmp - (int)(je + 4); } 573 | else if (i == BZ || i == BNZ) { je = je + 4; *(int*)je = tmp - (int)(je + 4); } 574 | } 575 | else if (i < LEV) { ++pc; } 576 | } 577 | 578 | // run jitted code 579 | pc = (int *) idmain[Val]; 580 | jitmain = (void *) jitmap[ pc - text ]; 581 | return jitmain(argv, argc); // c4 vm pushes first argument first, unlike cdecl 582 | } 583 | --------------------------------------------------------------------------------