Add support for passing arrays.
authorDavid Anderson <dvander@alliedmods.net>
Mon Jul 09 21:40:20 2012 -0700 (2012-07-09)
changeset 66cbba1997dc71
parent 65 7c942b5adebc
child 67 b765a6383849
Add support for passing arrays.
jitcraft/AST.h
jitcraft/Array.cpp
jitcraft/BytecodeCompiler.cpp
jitcraft/Messages.tbl
jitcraft/Objects.h
jitcraft/Parser.cpp
jitcraft/SemanticAnalysis.cpp
jitcraft/main.cpp
tests/prototype/passarray.jc
     1.1 --- a/jitcraft/AST.h	Mon Jul 09 01:22:03 2012 -0700
     1.2 +++ b/jitcraft/AST.h	Mon Jul 09 21:40:20 2012 -0700
     1.3 @@ -198,12 +198,14 @@
     1.4      Variable *variable_;
     1.5      ScopedRoot<String> tag_;
     1.6      bool reference_;
     1.7 +    ExpressionList *dimensions_;
     1.8  
     1.9    public:
    1.10 -    Parameter(Variable *var, Handle<String> tag, bool reference)
    1.11 +    Parameter(Variable *var, Handle<String> tag, bool reference, ExpressionList *dimensions)
    1.12        : variable_(var),
    1.13          tag_(tag),
    1.14 -        reference_(reference)
    1.15 +        reference_(reference),
    1.16 +        dimensions_(dimensions)
    1.17      {
    1.18      }
    1.19  
    1.20 @@ -216,6 +218,9 @@
    1.21      bool reference() const {
    1.22          return reference_;
    1.23      }
    1.24 +    ExpressionList *dimensions() const {
    1.25 +        return dimensions_;
    1.26 +    }
    1.27  };
    1.28  
    1.29  class NameProxy : public Expression
     2.1 --- a/jitcraft/Array.cpp	Mon Jul 09 01:22:03 2012 -0700
     2.2 +++ b/jitcraft/Array.cpp	Mon Jul 09 21:40:20 2012 -0700
     2.3 @@ -58,7 +58,7 @@
     2.4          return NULL;
     2.5  
     2.6      array->nelements_ = elements;
     2.7 -    array->elements_ = VoidPtr((Array *)array + 1);
     2.8 +    array->elements_ = (Array *)array + 1;
     2.9      memset(array->elements_, 0, elementBytes);
    2.10      return array;
    2.11  }
     3.1 --- a/jitcraft/BytecodeCompiler.cpp	Mon Jul 09 01:22:03 2012 -0700
     3.2 +++ b/jitcraft/BytecodeCompiler.cpp	Mon Jul 09 21:40:20 2012 -0700
     3.3 @@ -763,6 +763,8 @@
     3.4          if (!moduleData_->publics.append(root))
     3.5              return NULL;
     3.6      }
     3.7 +
     3.8 +    Disassemble(code, stdout);
     3.9      
    3.10      return fun;
    3.11  }
     4.1 --- a/jitcraft/Messages.tbl	Mon Jul 09 01:22:03 2012 -0700
     4.2 +++ b/jitcraft/Messages.tbl	Mon Jul 09 21:40:20 2012 -0700
     4.3 @@ -43,4 +43,6 @@
     4.4  MSG(ErrorReadingFile,               SyntaxError,            "error reading file")
     4.5  MSG(ExpectedGlobal,                 SyntaxError,            "expected global variable or function")
     4.6  MSG(ForwardNotImplemented,          SyntaxError,            "forward '%s' not implemented")
     4.7 -MSG(ForwardDeclareMismatches,       SyntaxError,            "declaration does not match original forward signature")
     4.8 \ No newline at end of file
     4.9 +MSG(ForwardDeclareMismatches,       SyntaxError,            "declaration does not match original forward signature")
    4.10 +MSG(ArrayCantBeReference,           SyntaxError,            "a parameter cannot be both an array and a reference")
    4.11 +MSG(ArraySizeMustBeConstant,        SyntaxError,            "array size must be a constant integer")
    4.12 \ No newline at end of file
     5.1 --- a/jitcraft/Objects.h	Mon Jul 09 01:22:03 2012 -0700
     5.2 +++ b/jitcraft/Objects.h	Mon Jul 09 21:40:20 2012 -0700
     5.3 @@ -60,8 +60,6 @@
     5.4      MapKinds_Total
     5.5  };
     5.6  
     5.7 -typedef void * VoidPtr;
     5.8 -
     5.9  // All garbage collected objects must inherit from GCObject. Note that using
    5.10  // virtual functions is illegal, as the |map| field would be displaced.
    5.11  class Object
     6.1 --- a/jitcraft/Parser.cpp	Mon Jul 09 01:22:03 2012 -0700
     6.2 +++ b/jitcraft/Parser.cpp	Mon Jul 09 21:40:20 2012 -0700
     6.3 @@ -896,6 +896,11 @@
     6.4          while (true) {
     6.5              bool ref = match(TOK_AMPERSAND);
     6.6  
     6.7 +            SourcePosition begin = scanner_.begin();
     6.8 +            Local<String> tag(zone_);
     6.9 +            if (matchName(tag.ref(), TOK_LABEL) && !tag)
    6.10 +                return NULL;
    6.11 +
    6.12              Local<String> name(zone_, expectName());
    6.13              if (!name)
    6.14                  return NULL;
    6.15 @@ -908,8 +913,25 @@
    6.16              if (!arg)
    6.17                  return NULL;
    6.18  
    6.19 -            Local<String> tag(zone_);
    6.20 -            Parameter *parameter = new (pool_) Parameter(arg, tag, ref);
    6.21 +            // Look for dimensions.
    6.22 +            ExpressionList *postDimensions = NULL;
    6.23 +            while (match(TOK_LBRACKET)) {
    6.24 +                if (!postDimensions)
    6.25 +                    postDimensions = new (pool_) ExpressionList();
    6.26 +
    6.27 +                Expression *dim = NULL;
    6.28 +                if (!match(TOK_RBRACKET)) {
    6.29 +                    if ((dim = expression()) == NULL)
    6.30 +                        return NULL;
    6.31 +                    if (!expect(TOK_RBRACKET))
    6.32 +                        return NULL;
    6.33 +                }
    6.34 +
    6.35 +                if (!postDimensions->append(dim))
    6.36 +                    return NULL;
    6.37 +            }
    6.38 +
    6.39 +            Parameter *parameter = new (pool_) Parameter(arg, tag, ref, postDimensions);
    6.40              if (!parameters->append(parameter))
    6.41                  return NULL;
    6.42  
     7.1 --- a/jitcraft/SemanticAnalysis.cpp	Mon Jul 09 01:22:03 2012 -0700
     7.2 +++ b/jitcraft/SemanticAnalysis.cpp	Mon Jul 09 21:40:20 2012 -0700
     7.3 @@ -542,9 +542,38 @@
     7.4          }
     7.5  
     7.6          if (parameter->reference()) {
     7.7 +            if (parameter->dimensions()) {
     7.8 +                cc_.reportError(parameter->variable()->pos(), Message_ArrayCantBeReference);
     7.9 +                return NULL;
    7.10 +            }
    7.11 +
    7.12              type = zone_->types()->getReference(type);
    7.13              if (!type)
    7.14                  return false;
    7.15 +        } else if (parameter->dimensions()) {
    7.16 +            if (parameter->dimensions()->length() > Type::MAX_UNSAFE_ARRAY_DEPTH) {
    7.17 +                cc_.reportError(node->pos(), Message_MaximumArrayDepth);
    7.18 +                return false;
    7.19 +            }
    7.20 +
    7.21 +            for (size_t i = parameter->dimensions()->length() - 1;
    7.22 +                 i < parameter->dimensions()->length();
    7.23 +                 i++)
    7.24 +            {
    7.25 +                int size = Type::UNKNOWN_SIZE;
    7.26 +                if (Expression *expr = parameter->dimensions()->at(i)) {
    7.27 +                    BoxedPrimitive box;
    7.28 +                    if (!EvaluateForConstant(expr, &box)) {
    7.29 +                        cc_.reportError(expr->pos(), Message_ArraySizeMustBeConstant);
    7.30 +                        return false;
    7.31 +                    }
    7.32 +                    size = box.toInt();
    7.33 +                }
    7.34 +
    7.35 +                type = Type::NewArray(zone_, type, size, false);
    7.36 +                if (!type)
    7.37 +                    return false;
    7.38 +            }
    7.39          }
    7.40  
    7.41          args->set(zone_, i, type);
    7.42 @@ -731,6 +760,8 @@
    7.43          if (actual->isReference() && !given->isReference())
    7.44              scope_->setUsesLifoHeap();
    7.45      }
    7.46 +
    7.47 +    node->setType(fun->type()->returnType());
    7.48  }
    7.49  
    7.50  void
     8.1 --- a/jitcraft/main.cpp	Mon Jul 09 01:22:03 2012 -0700
     8.2 +++ b/jitcraft/main.cpp	Mon Jul 09 21:40:20 2012 -0700
     8.3 @@ -76,7 +76,7 @@
     8.4          if (!module)
     8.5              return 1;
     8.6  
     8.7 -        Ke::Root<Ke::Module> root = Ke::Root<Ke::Module>::New(module);
     8.8 +       
     8.9      }
    8.10  
    8.11      Ke::Keima::Shutdown();
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/tests/prototype/passarray.jc	Mon Jul 09 21:40:20 2012 -0700
     9.3 @@ -0,0 +1,12 @@
     9.4 +f(a[])
     9.5 +{
     9.6 +    return a[3];
     9.7 +}
     9.8 +
     9.9 +public main()
    9.10 +{
    9.11 +    new b[5];
    9.12 +    b[2] = 3;
    9.13 +    return f(b);
    9.14 +}
    9.15 +