]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - exec.c
sqrt and normalize builtins
[xonotic/gmqcc.git] / exec.c
diff --git a/exec.c b/exec.c
index dc5980dc4e27cde690c81c58e92a1bb2032124dc..5bff85e06a8605e692c58778820096b19cffb8ad 100644 (file)
--- a/exec.c
+++ b/exec.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012
+ * Copyright (C) 2012, 2013
  *     Wolfgang Bumiller
  *     Dale Weiler
  *
@@ -359,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] = ' ';
     }
 }
@@ -475,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
@@ -732,6 +733,16 @@ static int qc_kill(qc_program *prog)
     return 0;
 }
 
+static int qc_sqrt(qc_program *prog)
+{
+    qcany *num, out;
+    CheckArgs(1);
+    num = GetArg(0);
+    out._float = sqrt(num->_float);
+    Return(out);
+    return 0;
+}
+
 static int qc_vlen(qc_program *prog)
 {
     qcany *vec, len;
@@ -744,6 +755,27 @@ static int qc_vlen(qc_program *prog)
     return 0;
 }
 
+static int qc_normalize(qc_program *prog)
+{
+    double len;
+    qcany *vec;
+    qcany out;
+    CheckArgs(1);
+    vec = GetArg(0);
+    len = sqrt(vec->vector[0] * vec->vector[0] +
+               vec->vector[1] * vec->vector[1] +
+               vec->vector[2] * vec->vector[2]);
+    if (len)
+        len = 1.0 / len;
+    else
+        len = 0;
+    out.vector[0] = len * vec->vector[0];
+    out.vector[1] = len * vec->vector[1];
+    out.vector[2] = len * vec->vector[2];
+    Return(out);
+    return 0;
+}
+
 static int qc_strcat(qc_program *prog)
 {
     char  *buffer;
@@ -794,17 +826,19 @@ static int qc_strcmp(qc_program *prog)
 
 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_strcat, /*   10  */
-    &qc_strcmp  /*   11  */
+    &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  */
+    &qc_normalize,   /*   12  */
+    &qc_sqrt         /*   13  */
 };
 static size_t qc_builtins_count = sizeof(qc_builtins) / sizeof(qc_builtins[0]);
 
@@ -824,14 +858,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"
@@ -889,6 +926,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];
 
@@ -905,8 +944,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();
@@ -934,6 +990,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;
@@ -966,7 +1034,7 @@ int main(int argc, char **argv)
 
             --argc;
             ++argv;
-            if (argc < 3) {
+            if (argc < 2) {
                 usage();
                 exit(1);
             }
@@ -993,27 +1061,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);
     }
@@ -1048,6 +1109,17 @@ int main(int argc, char **argv)
         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);
@@ -1080,9 +1152,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) {