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