jitcraft/main.cpp
author David Anderson <dvander@alliedmods.net>
Sun Jul 08 01:54:08 2012 -0700 (2012-07-08)
changeset 60 581a9c793062
parent 59 b7d61ddb3c8d
child 61 864e4b02a4c1
permissions -rw-r--r--
Initial import of some kind of API.
     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 
    30 using namespace ke;
    31 
    32 class File
    33 {
    34     FILE *fp_;
    35 
    36   public:
    37     File(FILE *fp)
    38       : fp_(fp)
    39     {
    40     }
    41 
    42     ~File() {
    43         if (fp_)
    44             fclose(fp_);
    45     }
    46 
    47     operator FILE *() {
    48         return fp_;
    49     }
    50 
    51     operator bool () {
    52         return !!fp_;
    53     }
    54 };
    55 
    56 static bool
    57 Compile(const char *stream, size_t length)
    58 {
    59     if (!ZONE()->initialize())
    60         return false;
    61 
    62     RootScope roots(ZONE());
    63     CompileContext cc(ZONE());
    64 
    65     Parser parser(ZONE(), cc, stream, length);
    66     ParseTree *tree = parser.parse();
    67     if (!tree)
    68         return false;
    69     tree->dump(stdout);
    70 
    71     if (!Scope::Bind(tree))
    72         return false;
    73 
    74     SemanticAnalysis semantics(ZONE(), cc, tree);
    75     if (!semantics.analyze())
    76         return false;
    77 
    78     Scope::Allocate(tree);
    79 
    80     BytecodeCompiler compiler(ZONE(), tree, NULL);
    81     Module *module = compiler.compile();
    82     if (!module)
    83         return false;
    84 
    85     printf("\n ------------ \n\n");
    86 
    87     Disassemble(module->getFunction(1)->code(), stdout);
    88     //Disassemble(module->getFunction(1), stdout);
    89 
    90     printf("\n ------------ \n\n");
    91 
    92     Function *main = module->getFunction(1);
    93 
    94     Top top = ZONE()->stack().begin();
    95     LocalTop localTop(top);
    96 
    97     Frame *frame = ZONE()->stack().pushFrame(main);
    98     if (!frame)
    99         return false;
   100 
   101     LocalSlot result;
   102     if (!Interpret(ZONE(), frame, &result))
   103         return false;
   104 
   105     printf("Result: %d\n", result.i32);
   106 
   107     return true;
   108 }
   109 
   110 int main(int argc, char **argv)
   111 {
   112     if (argc < 2) {
   113         fprintf(stderr, "Usage: <file>\n");
   114         return 1;
   115     }
   116 
   117     AutoVec<char> bytes(NULL);
   118     size_t length;
   119     {
   120         File fp = fopen(argv[1], "rb");
   121         if (!fp) {
   122             fprintf(stderr, "Can't open file: \"%s\"\n", argv[1]);
   123             return 1;
   124         }
   125 
   126         if (fseek(fp, 0, SEEK_END)) {
   127             fprintf(stderr, "Error seeking file!\n");
   128             return 1;
   129         }
   130         long size = ftell(fp);
   131         if (size < 0) {
   132             fprintf(stderr, "Error getting file length!\n");
   133             return 1;
   134         }
   135         rewind(fp);
   136         length = size_t(size);
   137         bytes = new char[length + 1];
   138         if (fread(bytes, 1, length, fp) != length) {
   139             fprintf(stderr, "Error reading file!\n");
   140             return 1;
   141         }
   142     }
   143 
   144     Compile(bytes, length);
   145 
   146 }