src/main.cpp
author David Anderson <dvander@alliedmods.net>
Sun Jul 29 12:24:49 2012 -0700 (2012-07-29)
changeset 129 86b368a4d80d
parent 92 e3974e7360e9
child 161 b3f0aaee735a
permissions -rw-r--r--
Fix various error reporting bugs related to Invoke.
     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 #include "Spaces-inl.h"
    31 #include "Heap-inl.h"
    32 
    33 using namespace ke;
    34 
    35 class MessageReporter : public Ke::IMessageReporter
    36 {
    37   public:
    38     void onOutOfMemory() {
    39         fprintf(stderr, "Out of memory!\n");
    40     }
    41     void onUncaughtException(const char *message) {
    42         fprintf(stderr, "Uncaught exception: %s\n", message);
    43 
    44         Ke::IStackFrameIterator *iter = Ke::Keima::NewStackFrameIterator();
    45         unsigned index = 0;
    46         while (!iter->done()) {
    47             fprintf(stderr, "[%d] %s (%s line %d)\n",
    48                     index,
    49                     iter->function(),
    50                     iter->source(),
    51                     iter->lineNumber());
    52             iter->prev();
    53             index++;
    54         }
    55     }
    56     virtual void beginCompilationErrors(const char *file) {
    57         fprintf(stderr, "\nDisplaying compilation errors for \"%s\"\n", file);
    58     }
    59     virtual void reportCompilationError(const char *message, unsigned line, unsigned col) {
    60         fprintf(stderr, "  line %d, col %d: %s\n", line, col, message);
    61     }
    62     virtual void endCompilationErrors() {
    63     }
    64 } s_Reporter;
    65 
    66 #if 0
    67 static bool
    68 Compile(const char *stream, size_t length)
    69 {
    70 
    71     printf("\n ------------ \n\n");
    72 
    73     Disassemble(module->getFunction(1)->code(), stdout);
    74     //Disassemble(module->getFunction(1), stdout);
    75 
    76     printf("\n ------------ \n\n");
    77 
    78     Function *main = module->getFunction(1);
    79 
    80     Top top = ZONE()->stack().begin();
    81     LocalTop localTop(top);
    82 
    83     Frame *frame = ZONE()->stack().pushFrame(main);
    84     if (!frame)
    85         return false;
    86 
    87     LocalSlot result;
    88     if (!Interpret(ZONE(), frame, &result))
    89         return false;
    90 
    91     printf("Result: %d\n", result.i32);
    92 
    93     return true;
    94 }
    95 #endif
    96 
    97 intptr_t PrintToServer(void *blah, intptr_t *params)
    98 {
    99     assert(params[0] == 3);
   100     const char *fmt = reinterpret_cast<const char *>(params[1]);
   101     const char *str = reinterpret_cast<const char *>(params[2]);
   102     int *d = reinterpret_cast<int *>(params[3]);
   103     printf(fmt, str, *d);
   104     return 16;
   105 }
   106 
   107 int main(int argc, char **argv)
   108 {
   109     if (argc < 2) {
   110         fprintf(stderr, "Usage: <file>\n");
   111         return 1;
   112     }
   113 
   114     if (!Ke::Keima::Initialize())
   115         return 1;
   116     Ke::Keima::InstallMessageReporter(&s_Reporter);
   117     {
   118         Ke::HandleScope scope;
   119         Ke::Local<Ke::Module> module = Ke::Compile(argv[1]);
   120         if (!module)
   121             return 1;
   122 
   123         //Ke::Local<Ke::Native> native = module->getNative(0);
   124         //native->bind(reinterpret_cast<void *>(PrintToServer));
   125         //module->setNativeIdentity(reinterpret_cast<void *>(5));
   126 
   127         int result;
   128         Ke::Local<Ke::Function> fun = module->getPublic(0);
   129         if (Ke::Keima::Invoke(fun, NULL, 0, true, &result))
   130             printf("%d\n", result);
   131     }
   132 
   133     Ke::Keima::Shutdown();
   134 }
   135 
   136 /* Overload a few things to prevent libstdc++ linking */
   137 #if defined __linux__ || defined __APPLE__
   138 extern "C" void __cxa_pure_virtual(void)
   139 {
   140 }
   141 
   142 void *operator new(size_t size)
   143 {
   144 	return malloc(size);
   145 }
   146 
   147 void *operator new[](size_t size) 
   148 {
   149 	return malloc(size);
   150 }
   151 
   152 void operator delete(void *ptr) 
   153 {
   154 	free(ptr);
   155 }
   156 
   157 void operator delete[](void * ptr)
   158 {
   159 	free(ptr);
   160 }
   161 #endif
   162