Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Android "Almost" C Compiler. |
| 3 | * This is a compiler for a small subset of the C language, intended for use |
| 4 | * in scripting environments where speed and memory footprint are important. |
| 5 | * |
| 6 | * This code is based upon the "unobfuscated" version of the |
| 7 | * Obfuscated Tiny C compiler, see the file LICENSE for details. |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | #include <ctype.h> |
| 12 | #include <dlfcn.h> |
| 13 | #include <stdarg.h> |
| 14 | #include <stdint.h> |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | |
| 19 | #if defined(__arm__) |
| 20 | #include <unistd.h> |
| 21 | #endif |
| 22 | |
| 23 | #include <acc/acc.h> |
| 24 | |
| 25 | |
| 26 | typedef int (*MainPtr)(int, char**); |
| 27 | // This is a separate function so it can easily be set by breakpoint in gdb. |
| 28 | int run(MainPtr mainFunc, int argc, char** argv) { |
| 29 | return mainFunc(argc, argv); |
| 30 | } |
| 31 | |
| 32 | int main(int argc, char** argv) { |
| 33 | const char* inFile = NULL; |
| 34 | bool printListing; |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame^] | 35 | bool runResults = false; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 36 | FILE* in = stdin; |
| 37 | int i; |
| 38 | for (i = 1; i < argc; i++) { |
| 39 | char* arg = argv[i]; |
| 40 | if (arg[0] == '-') { |
| 41 | switch (arg[1]) { |
| 42 | case 'S': |
| 43 | printListing = true; |
| 44 | break; |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame^] | 45 | case 'R': |
| 46 | runResults = true; |
| 47 | break; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 48 | default: |
| 49 | fprintf(stderr, "Unrecognized flag %s\n", arg); |
| 50 | return 3; |
| 51 | } |
| 52 | } else if (inFile == NULL) { |
| 53 | inFile = arg; |
| 54 | } else { |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if (! inFile) { |
| 60 | fprintf(stderr, "input file required\n"); |
| 61 | return 2; |
| 62 | } |
| 63 | |
| 64 | if (inFile) { |
| 65 | in = fopen(inFile, "r"); |
| 66 | if (!in) { |
| 67 | fprintf(stderr, "Could not open input file %s\n", inFile); |
| 68 | return 1; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | fseek(in, 0, SEEK_END); |
| 73 | size_t fileSize = (size_t) ftell(in); |
| 74 | rewind(in); |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 75 | ACCchar* text = new ACCchar[fileSize + 1]; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 76 | size_t bytesRead = fread(text, 1, fileSize, in); |
| 77 | if (bytesRead != fileSize) { |
| 78 | fprintf(stderr, "Could not read all of file %s\n", inFile); |
| 79 | } |
| 80 | |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 81 | text[fileSize] = '\0'; |
| 82 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 83 | ACCscript* script = accCreateScript(); |
| 84 | |
| 85 | const ACCchar* scriptSource[] = {text}; |
| 86 | accScriptSource(script, 1, scriptSource, NULL); |
| 87 | delete[] text; |
| 88 | |
| 89 | accCompileScript(script); |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 90 | int result = accGetError(script); |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 91 | MainPtr mainPointer = 0; |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 92 | if (result != 0) { |
| 93 | char buf[1024]; |
| 94 | accGetScriptInfoLog(script, sizeof(buf), NULL, buf); |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 95 | fprintf(stderr, "%s", buf); |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 96 | goto exit; |
| 97 | } |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 98 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 99 | { |
| 100 | ACCsizei numPragmaStrings; |
| 101 | accGetPragmas(script, &numPragmaStrings, 0, NULL); |
| 102 | if (numPragmaStrings) { |
| 103 | char** strings = new char*[numPragmaStrings]; |
| 104 | accGetPragmas(script, NULL, numPragmaStrings, strings); |
| 105 | for(ACCsizei i = 0; i < numPragmaStrings; i += 2) { |
| 106 | fprintf(stderr, "#pragma %s(%s)\n", strings[i], strings[i+1]); |
| 107 | } |
| 108 | delete[] strings; |
| 109 | } |
| 110 | } |
| 111 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 112 | accGetScriptLabel(script, "main", (ACCvoid**) & mainPointer); |
| 113 | |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 114 | result = accGetError(script); |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame^] | 115 | if (result == ACC_NO_ERROR && runResults) { |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 116 | fprintf(stderr, "Executing compiled code:\n"); |
| 117 | int codeArgc = argc - i + 1; |
| 118 | char** codeArgv = argv + i - 1; |
| 119 | codeArgv[0] = (char*) (inFile ? inFile : "stdin"); |
| 120 | result = run(mainPointer, codeArgc, codeArgv); |
| 121 | fprintf(stderr, "result: %d\n", result); |
| 122 | } |
| 123 | |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 124 | exit: |
| 125 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 126 | accDeleteScript(script); |
| 127 | |
| 128 | return result; |
| 129 | } |