jitcraft/main.cpp
author David Anderson <dvander@alliedmods.net>
Tue Jul 24 23:53:34 2012 -0700 (2012-07-24)
changeset 86 32ad0523b309
parent 85 06a0015b6a5c
permissions -rw-r--r--
Renamed jitcraft/ to src/, fixed some /Wp64 warnings.
     1 /*
     2  * Copyright (C) 2012 David Anderson
     3  *
     4  * This file is part of JITCraft.
     5  *
     6  * JITCraft is free software: you can redistribute it and/or modify it under
     7  * the terms of the GNU General Public License as published by the Free
     8  * Software Foundation, either version 3 of the License, or (at your option)
     9  * any later version.
    10  * 
    11  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along with
    16  * JITCraft. If not, see http://www.gnu.org/licenses/.
    17  */
    18 #include <stdio.h>
    19 #include "Utility.h"
    20 #include "Scanner.h"
    21 #include "Parser.h"
    22 #include "SemanticAnalysis.h"
    23 #include "Zone.h"
    24 #include "BytecodeCompiler.h"
    25 #include "Code.h"
    26 #include "Interpreter.h"
    27 #include "Modules.h"
    28 #include "RootScopes.h"
    29 #include "../include/keima.h"
    30 
    31 using namespace ke;
    32 
    33 class MessageReporter : public Ke::IMessageReporter
    34 {
    35   public:
    36     void onOutOfMemory() {
    37         fprintf(stderr, "Out of memory!\n");
    38     }
    39     void onUncaughtException(const char *message) {
    40         fprintf(stderr, "Uncaught exception: %s\n", message);
    41 
    42         Ke::IStackFrameIterator *iter = Ke::Keima::NewStackFrameIterator();
    43         unsigned index = 0;
    44         while (!iter->done()) {
    45             fprintf(stderr, "[%d] %s (%s line %d)\n",
    46                     index,
    47                     iter->function(),
    48                     iter->source(),
    49                     iter->lineNumber());
    50             iter->prev();
    51             index++;
    52         }
    53     }
    54     virtual void beginCompilationErrors(const char *file) {
    55         fprintf(stderr, "\nDisplaying compilation errors for \"%s\"\n", file);
    56     }
    57     virtual void reportCompilationError(const char *message, unsigned line, unsigned col) {
    58         fprintf(stderr, "  line %d, col %d: %s\n", line, col, message);
    59     }
    60     virtual void endCompilationErrors() {
    61     }
    62 } s_Reporter;
    63 
    64 #if 0
    65 static bool
    66 Compile(const char *stream, size_t length)
    67 {
    68 
    69     printf("\n ------------ \n\n");
    70 
    71     Disassemble(module->getFunction(1)->code(), stdout);
    72     //Disassemble(module->getFunction(1), stdout);
    73 
    74     printf("\n ------------ \n\n");
    75 
    76     Function *main = module->getFunction(1);
    77 
    78     Top top = ZONE()->stack().begin();
    79     LocalTop localTop(top);
    80 
    81     Frame *frame = ZONE()->stack().pushFrame(main);
    82     if (!frame)
    83         return false;
    84 
    85     LocalSlot result;
    86     if (!Interpret(ZONE(), frame, &result))
    87         return false;
    88 
    89     printf("Result: %d\n", result.i32);
    90 
    91     return true;
    92 }
    93 #endif
    94 
    95 intptr_t PrintToServer(void *blah, intptr_t *params)
    96 {
    97     assert(params[0] == 3);
    98     const char *fmt = reinterpret_cast<const char *>(params[1]);
    99     const char *str = reinterpret_cast<const char *>(params[2]);
   100     int *d = reinterpret_cast<int *>(params[3]);
   101     printf(fmt, str, *d);
   102     return 16;
   103 }
   104 
   105 int main(int argc, char **argv)
   106 {
   107     if (argc < 2) {
   108         fprintf(stderr, "Usage: <file>\n");
   109         return 1;
   110     }
   111 
   112     if (!Ke::Keima::Initialize())
   113         return 1;
   114     Ke::Keima::InstallMessageReporter(&s_Reporter);
   115     {
   116         Ke::HandleScope scope;
   117         Ke::Local<Ke::Module> module = Ke::Compile(argv[1]);
   118         if (!module)
   119             return 1;
   120 
   121         //Ke::Local<Ke::Native> native = module->getNative(0);
   122         //native->bind(reinterpret_cast<void *>(PrintToServer));
   123         //module->setNativeIdentity(reinterpret_cast<void *>(5));
   124 
   125         int result;
   126         Ke::Local<Ke::Function> fun = module->getPublic(0);
   127         if (Ke::Keima::Invoke(fun, NULL, 0, &result))
   128             printf("%d\n", result);
   129     }
   130 
   131     Ke::Keima::Shutdown();
   132 }