]> de.git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - exec.c
moving -Olocal-temps to -O4 until the issues are solved
[xonotic/gmqcc.git] / exec.c
diff --git a/exec.c b/exec.c
index 6e51c027930a8a44c7dd360fd1443b029473b8c9..2383ad70e5bf23827dae59e657b7877252745d3e 100644 (file)
--- a/exec.c
+++ b/exec.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012
+ * Copyright (C) 2012, 2013
  *     Wolfgang Bumiller
  *     Dale Weiler
  *
@@ -53,29 +53,28 @@ static void qcvmerror(qc_program *prog, const char *fmt, ...)
 
 qc_program* prog_load(const char *filename)
 {
-    qc_program *prog;
-    prog_header header;
-    FILE *file;
+    qc_program   *prog;
+    prog_header   header;
+    FILE         *file   = file_open(filename, "rb");
 
-    file = util_fopen(filename, "rb");
     if (!file)
         return NULL;
 
-    if (fread(&header, sizeof(header), 1, file) != 1) {
+    if (file_read(&header, sizeof(header), 1, file) != 1) {
         loaderror("failed to read header from '%s'", filename);
-        fclose(file);
+        file_close(file);
         return NULL;
     }
 
     if (header.version != 6) {
         loaderror("header says this is a version %i progs, we need version 6\n", header.version);
-        fclose(file);
+        file_close(file);
         return NULL;
     }
 
     prog = (qc_program*)mem_a(sizeof(qc_program));
     if (!prog) {
-        fclose(file);
+        file_close(file);
         printf("failed to allocate program data\n");
         return NULL;
     }
@@ -91,15 +90,17 @@ qc_program* prog_load(const char *filename)
     }
 
 #define read_data(hdrvar, progvar, reserved)                           \
-    if (fseek(file, header.hdrvar.offset, SEEK_SET) != 0) {            \
+    if (file_seek(file, header.hdrvar.offset, SEEK_SET) != 0) {        \
         loaderror("seek failed");                                      \
         goto error;                                                    \
     }                                                                  \
-    if (fread(vec_add(prog->progvar, header.hdrvar.length + reserved), \
-              sizeof(*prog->progvar),                                  \
-              header.hdrvar.length, file)                              \
-        != header.hdrvar.length)                                       \
-    {                                                                  \
+    if (file_read (                                                    \
+            vec_add(prog->progvar, header.hdrvar.length + reserved),   \
+            sizeof(*prog->progvar),                                    \
+            header.hdrvar.length,                                      \
+            file                                                       \
+        )!= header.hdrvar.length                                       \
+    ) {                                                                \
         loaderror("read failed");                                      \
         goto error;                                                    \
     }
@@ -113,7 +114,7 @@ qc_program* prog_load(const char *filename)
     read_data1(strings);
     read_data2(globals, 2); /* reserve more in case a RETURN using with the global at "the end" exists */
 
-    fclose(file);
+    file_close(file);
 
     /* profile counters */
     memset(vec_add(prog->profile, vec_size(prog->code)), 0, sizeof(prog->profile[0]) * vec_size(prog->code));
@@ -168,8 +169,9 @@ void prog_delete(qc_program *prog)
 
 char* prog_getstring(qc_program *prog, qcint str)
 {
+    /* cast for return required for C++ */
     if (str < 0 || str >= (qcint)vec_size(prog->strings))
-        return "<<<invalid string>>>";
+        return (char*)"<<<invalid string>>>";
     return prog->strings + str;
 }
 
@@ -342,7 +344,10 @@ static void trace_print_global(qc_program *prog, unsigned int glob, int vtype)
                                          value->vector[2]);
             break;
         case TYPE_STRING:
-            len += print_escaped_string(prog_getstring(prog, value->string), sizeof(spaces)-len-5);
+            if (value->string)
+                len += print_escaped_string(prog_getstring(prog, value->string), sizeof(spaces)-len-5);
+            else
+                len += printf("(null)");
             len += printf(",");
             /* len += printf("\"%s\",", prog_getstring(prog, value->string)); */
             break;
@@ -354,7 +359,7 @@ static void trace_print_global(qc_program *prog, unsigned int glob, int vtype)
 done:
     if (len < (int)sizeof(spaces)-1) {
         spaces[sizeof(spaces)-1-len] = 0;
-        printf(spaces);
+        file_puts(stdout, spaces);
         spaces[sizeof(spaces)-1-len] = ' ';
     }
 }
@@ -470,7 +475,8 @@ static qcint prog_enterfunction(qc_program *prog, prog_section_function *func)
     st.function = func;
 
     if (prog->xflags & VMXF_TRACE) {
-        vec_push(prog->function_stack, prog_getstring(prog, func->name));
+        const char *str = prog_getstring(prog, func->name);
+        vec_push(prog->function_stack, str);
     }
 
 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
@@ -530,7 +536,7 @@ static qcint prog_leavefunction(qc_program *prog)
 #endif
     if (prev) {
         qcint *globals = prog->globals + prev->firstlocal;
-        memcpy(globals, prog->localstack + oldsp, prev->locals);
+        memcpy(globals, prog->localstack + oldsp, prev->locals * sizeof(prog->localstack[0]));
         /* vec_remove(prog->localstack, oldsp, vec_size(prog->localstack)-oldsp); */
         vec_shrinkto(prog->localstack, oldsp);
     }
@@ -560,28 +566,24 @@ bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long
 #define QCVM_PROFILE 0
 #define QCVM_TRACE   0
 #           include __FILE__
-            break;
         }
         case (VMXF_TRACE):
         {
 #define QCVM_PROFILE 0
 #define QCVM_TRACE   1
 #           include __FILE__
-            break;
         }
         case (VMXF_PROFILE):
         {
 #define QCVM_PROFILE 1
 #define QCVM_TRACE   0
 #           include __FILE__
-            break;
         }
         case (VMXF_TRACE|VMXF_PROFILE):
         {
 #define QCVM_PROFILE 1
 #define QCVM_TRACE   1
 #           include __FILE__
-            break;
         }
     };
 
@@ -644,7 +646,8 @@ static int qc_print(qc_program *prog)
     const char *laststr = NULL;
     for (i = 0; i < (size_t)prog->argc; ++i) {
         qcany *str = (qcany*)(prog->globals + OFS_PARM0 + 3*i);
-        printf("%s", (laststr = prog_getstring(prog, str->string)));
+        laststr = prog_getstring(prog, str->string);
+        printf("%s", laststr);
     }
     if (laststr && (prog->xflags & VMXF_TRACE)) {
         size_t len = strlen(laststr);
@@ -742,17 +745,67 @@ static int qc_vlen(qc_program *prog)
     return 0;
 }
 
+static int qc_strcat(qc_program *prog)
+{
+    char  *buffer;
+    size_t len1,   len2;
+    char  *cstr1, *cstr2;
+    qcany *str1,  *str2;
+    qcany  out;
+
+    CheckArgs(2);
+    str1 = GetArg(0);
+    str2 = GetArg(1);
+    cstr1 = prog_getstring(prog, str1->string);
+    cstr2 = prog_getstring(prog, str2->string);
+    len1 = strlen(cstr1);
+    len2 = strlen(cstr2);
+    buffer = (char*)mem_a(len1 + len2 + 1);
+    memcpy(buffer, cstr1, len1);
+    memcpy(buffer+len1, cstr2, len2+1);
+    out.string = prog_tempstring(prog, buffer);
+    mem_d(buffer);
+    Return(out);
+    return 0;
+}
+
+static int qc_strcmp(qc_program *prog)
+{
+    char  *cstr1, *cstr2;
+    qcany *str1,  *str2;
+    qcany out;
+
+    if (prog->argc != 2 && prog->argc != 3) {
+        printf("ERROR: invalid number of arguments for strcmp/strncmp: %i, expected 2 or 3\n",
+               prog->argc);
+        return -1;
+    }
+
+    str1 = GetArg(0);
+    str2 = GetArg(1);
+    cstr1 = prog_getstring(prog, str1->string);
+    cstr2 = prog_getstring(prog, str2->string);
+    if (prog->argc == 3)
+        out._float = strncmp(cstr1, cstr2, GetArg(2)->_float);
+    else
+        out._float = strcmp(cstr1, cstr2);
+    Return(out);
+    return 0;
+}
+
 static prog_builtin qc_builtins[] = {
     NULL,
-    &qc_print, /*   1   */
-    &qc_ftos,  /*   2   */
-    &qc_spawn, /*   3   */
-    &qc_kill,  /*   4   */
-    &qc_vtos,  /*   5   */
-    &qc_error, /*   6   */
-    &qc_vlen,  /*   7   */
-    &qc_etos,  /*   8   */
-    &qc_stof   /*   9   */
+    &qc_print,  /*   1   */
+    &qc_ftos,   /*   2   */
+    &qc_spawn,  /*   3   */
+    &qc_kill,   /*   4   */
+    &qc_vtos,   /*   5   */
+    &qc_error,  /*   6   */
+    &qc_vlen,   /*   7   */
+    &qc_etos,   /*   8   */
+    &qc_stof,   /*   9   */
+    &qc_strcat, /*   10  */
+    &qc_strcmp  /*   11  */
 };
 static size_t qc_builtins_count = sizeof(qc_builtins) / sizeof(qc_builtins[0]);
 
@@ -772,14 +825,17 @@ static void usage()
 {
     printf("usage: %s [options] [parameters] file\n", arg0);
     printf("options:\n");
-    printf("  -h, --help    print this message\n"
-           "  -trace        trace the execution\n"
-           "  -profile      perform profiling during execution\n"
-           "  -info         print information from the prog's header\n"
-           "  -disasm       disassemble and exit\n"
-           "  -printdefs    list the defs section\n"
-           "  -printfields  list the field section\n"
-           "  -printfuns    list functions information\n");
+    printf("  -h, --help         print this message\n"
+           "  -trace             trace the execution\n"
+           "  -profile           perform profiling during execution\n"
+           "  -info              print information from the prog's header\n"
+           "  -disasm            disassemble and exit\n"
+           "  -disasm-func func  disassemble and exit\n"
+           "  -printdefs         list the defs section\n"
+           "  -printfields       list the field section\n"
+           "  -printfuns         list functions information\n"
+           "  -v                 be verbose\n"
+           "  -vv                be even more verbose\n");
     printf("parameters:\n");
     printf("  -vector <V>   pass a vector parameter to main()\n"
            "  -float  <f>   pass a float parameter to main()\n"
@@ -837,6 +893,8 @@ int main(int argc, char **argv)
     bool        opts_info        = false;
     bool        noexec           = false;
     const char *progsfile        = NULL;
+    const char **dis_list        = NULL;
+    int         opts_v           = 0;
 
     arg0 = argv[0];
 
@@ -853,8 +911,25 @@ int main(int argc, char **argv)
             usage();
             exit(0);
         }
-        else if (!strcmp(argv[1], "-v") ||
-                 !strcmp(argv[1], "-version") ||
+        else if (!strcmp(argv[1], "-v")) {
+            ++opts_v;
+            --argc;
+            ++argv;
+        }
+        else if (!strncmp(argv[1], "-vv", 3)) {
+            const char *av = argv[1]+1;
+            for (; *av; ++av) {
+                if (*av == 'v')
+                    ++opts_v;
+                else {
+                    usage();
+                    exit(1);
+                }
+            }
+            --argc;
+            ++argv;
+        }
+        else if (!strcmp(argv[1], "-version") ||
                  !strcmp(argv[1], "--version"))
         {
             version();
@@ -882,6 +957,18 @@ int main(int argc, char **argv)
             opts_disasm = true;
             noexec = true;
         }
+        else if (!strcmp(argv[1], "-disasm-func")) {
+            --argc;
+            ++argv;
+            if (argc <= 1) {
+                usage();
+                exit(1);
+            }
+            vec_push(dis_list, argv[1]);
+            --argc;
+            ++argv;
+            noexec = true;
+        }
         else if (!strcmp(argv[1], "-printdefs")) {
             --argc;
             ++argv;
@@ -914,7 +1001,7 @@ int main(int argc, char **argv)
 
             --argc;
             ++argv;
-            if (argc < 3) {
+            if (argc < 2) {
                 usage();
                 exit(1);
             }
@@ -941,27 +1028,20 @@ int main(int argc, char **argv)
         }
         else
         {
+            printf("unknown parameter: %s\n", argv[1]);
             usage();
             exit(1);
         }
     }
 
-    if (argc > 2) {
-        usage();
-        exit(1);
-    }
-    if (argc > 1) {
-        if (progsfile) {
-            printf("only 1 program file may be specified\n");
-            usage();
-            exit(1);
-        }
+    if (argc == 2 && !progsfile) {
         progsfile = argv[1];
         --argc;
         ++argv;
     }
 
     if (!progsfile) {
+        printf("must specify a program to execute\n");
         usage();
         exit(1);
     }
@@ -979,12 +1059,34 @@ int main(int argc, char **argv)
         printf("Program's system-checksum = 0x%04x\n", (unsigned int)prog->crc16);
         printf("Entity field space: %u\n", (unsigned int)prog->entityfields);
         printf("Globals: %u\n", (unsigned int)vec_size(prog->globals));
+        printf("Counts:\n"
+               "      code: %lu\n"
+               "      defs: %lu\n"
+               "    fields: %lu\n"
+               " functions: %lu\n"
+               "   strings: %lu\n",
+               (unsigned long)vec_size(prog->code),
+               (unsigned long)vec_size(prog->defs),
+               (unsigned long)vec_size(prog->fields),
+               (unsigned long)vec_size(prog->functions),
+               (unsigned long)vec_size(prog->strings));
     }
 
     if (opts_info) {
         prog_delete(prog);
         return 0;
     }
+    for (i = 0; i < vec_size(dis_list); ++i) {
+        size_t k;
+        printf("Looking for `%s`\n", dis_list[i]);
+        for (k = 1; k < vec_size(prog->functions); ++k) {
+            const char *name = prog_getstring(prog, prog->functions[k].name);
+            if (!strcmp(name, dis_list[i])) {
+                prog_disasm_function(prog, k);
+                break;
+            }
+        }
+    }
     if (opts_disasm) {
         for (i = 1; i < vec_size(prog->functions); ++i)
             prog_disasm_function(prog, i);
@@ -1017,9 +1119,32 @@ int main(int argc, char **argv)
             for (a = 0; a < prog->functions[i].nargs; ++a) {
                 printf(" %i", prog->functions[i].argsize[a]);
             }
-            printf(") locals: %i + %i\n",
-                   prog->functions[i].firstlocal,
-                   prog->functions[i].locals);
+            if (opts_v > 1) {
+                int32_t start = prog->functions[i].entry;
+                if (start < 0)
+                    printf(") builtin %i\n", (int)-start);
+                else {
+                    size_t funsize = 0;
+                    prog_section_statement *st = prog->code + start;
+                    for (;st->opcode != INSTR_DONE; ++st)
+                        ++funsize;
+                    printf(") - %lu instructions", (unsigned long)funsize);
+                    if (opts_v > 2) {
+                        printf(" - locals: %i + %i\n",
+                               prog->functions[i].firstlocal,
+                               prog->functions[i].locals);
+                    }
+                    else
+                        printf("\n");
+                }
+            }
+            else if (opts_v) {
+                printf(") locals: %i + %i\n",
+                       prog->functions[i].firstlocal,
+                       prog->functions[i].locals);
+            }
+            else
+                printf(")\n");
         }
     }
     if (!noexec) {