Move ExpressionList to a jc::Vector.
authorDavid Anderson <dvander@alliedmods.net>
Sat Jul 07 20:53:10 2012 -0700 (2012-07-07)
changeset 55e073a9fe8e57
parent 54 8e22792a3921
child 56 abb4ae5bc915
Move ExpressionList to a jc::Vector.
jitcraft/AST.h
jitcraft/BytecodeCompiler.cpp
jitcraft/Parser.cpp
jitcraft/PoolAllocator.cpp
jitcraft/PoolAllocator.h
jitcraft/SemanticAnalysis.cpp
jitcraft/Utility.h
jitcraft/Vector.h
jitcraft/jitcraft.vcxproj
     1.1 --- a/jitcraft/AST.h	Sat Jul 07 20:28:29 2012 -0700
     1.2 +++ b/jitcraft/AST.h	Sat Jul 07 20:53:10 2012 -0700
     1.3 @@ -22,6 +22,7 @@
     1.4  #include "Token.h"
     1.5  #include "NameBinding.h"
     1.6  #include <vector>
     1.7 +#include "Vector.h"
     1.8  
     1.9  namespace jc {
    1.10  
    1.11 @@ -153,7 +154,7 @@
    1.12  };
    1.13  
    1.14  typedef PoolList<Statement *> StatementList;
    1.15 -typedef std::vector<Expression *> ExpressionList;
    1.16 +typedef Vector<Expression *, PoolAllocationPolicy> ExpressionList;
    1.17  
    1.18  class VariableDeclaration : public Statement
    1.19  {
     2.1 --- a/jitcraft/BytecodeCompiler.cpp	Sat Jul 07 20:28:29 2012 -0700
     2.2 +++ b/jitcraft/BytecodeCompiler.cpp	Sat Jul 07 20:53:10 2012 -0700
     2.3 @@ -595,7 +595,7 @@
     2.4  {
     2.5      Local<Type> funType(zone_, node->callee()->type());
     2.6  
     2.7 -    for (size_t i = 0; i < node->arguments()->size(); i++) {
     2.8 +    for (size_t i = 0; i < node->arguments()->length(); i++) {
     2.9          Expression *arg = node->arguments()->at(i);
    2.10  
    2.11          Local<Type> given(zone_, arg->type());
    2.12 @@ -629,7 +629,7 @@
    2.13      }
    2.14  
    2.15      __ callglobal(node->callee()->toNameProxy()->binding()->toNamedFunction()->slot());
    2.16 -    __ drop(node->arguments()->size());
    2.17 +    __ drop(node->arguments()->length());
    2.18  }
    2.19  
    2.20  void
     3.1 --- a/jitcraft/Parser.cpp	Sat Jul 07 20:28:29 2012 -0700
     3.2 +++ b/jitcraft/Parser.cpp	Sat Jul 07 20:53:10 2012 -0700
     3.3 @@ -179,7 +179,8 @@
     3.4              if (!expr)
     3.5                  return NULL;
     3.6  
     3.7 -            arguments->push_back(expr);
     3.8 +            if (!arguments->append(expr))
     3.9 +                return NULL;
    3.10  
    3.11              if (!match(TOK_COMMA))
    3.12                  break;
    3.13 @@ -592,7 +593,8 @@
    3.14              if (!expect(TOK_RBRACKET))
    3.15                  return NULL;
    3.16  
    3.17 -            postDimensions->push_back(dim);
    3.18 +            if (!postDimensions->append(dim))
    3.19 +                return NULL;
    3.20          }
    3.21      }
    3.22      
    3.23 @@ -977,7 +979,7 @@
    3.24          fprintf(fp_, "[ CallExpression\n");
    3.25          indent();
    3.26          node->callee()->accept(this);
    3.27 -        for (size_t i = 0; i < node->arguments()->size(); i++)
    3.28 +        for (size_t i = 0; i < node->arguments()->length(); i++)
    3.29              node->arguments()->at(i)->accept(this);
    3.30          unindent();
    3.31      }
     4.1 --- a/jitcraft/PoolAllocator.cpp	Sat Jul 07 20:28:29 2012 -0700
     4.2 +++ b/jitcraft/PoolAllocator.cpp	Sat Jul 07 20:53:10 2012 -0700
     4.3 @@ -19,6 +19,7 @@
     4.4  #include <stdlib.h>
     4.5  #include <new>
     4.6  #include "PoolAllocator.h"
     4.7 +#include "Zone.h"
     4.8  
     4.9  using namespace jc;
    4.10  
    4.11 @@ -101,3 +102,31 @@
    4.12      last_ = pool;
    4.13      return pool->base;
    4.14  }
    4.15 +
    4.16 +void
    4.17 +PoolAllocationPolicy::reportOutOfMemory()
    4.18 +{
    4.19 +    // :TODO: report
    4.20 +    assert(false);
    4.21 +}
    4.22 +
    4.23 +void
    4.24 +PoolAllocationPolicy::reportAllocationOverflow()
    4.25 +{
    4.26 +    // :TODO: report
    4.27 +    assert(false);
    4.28 +}
    4.29 +
    4.30 +void *
    4.31 +PoolAllocationPolicy::malloc(size_t bytes)
    4.32 +{
    4.33 +    void *ptr = ZONE()->pool().allocate(bytes);
    4.34 +    if (!ptr)
    4.35 +        reportOutOfMemory();
    4.36 +    return ptr;
    4.37 +}
    4.38 +
    4.39 +void
    4.40 +PoolAllocationPolicy::free(void *ptr)
    4.41 +{
    4.42 +}
     5.1 --- a/jitcraft/PoolAllocator.h	Sat Jul 07 20:28:29 2012 -0700
     5.2 +++ b/jitcraft/PoolAllocator.h	Sat Jul 07 20:53:10 2012 -0700
     5.3 @@ -167,6 +167,17 @@
     5.4      }
     5.5  };
     5.6  
     5.7 +class PoolAllocationPolicy
     5.8 +{
     5.9 +  protected:
    5.10 +    void reportAllocationOverflow();
    5.11 +    void reportOutOfMemory();
    5.12 +
    5.13 +  public:
    5.14 +    void *malloc(size_t bytes);
    5.15 +    void free(void *ptr);
    5.16 +};
    5.17 +
    5.18  }
    5.19  
    5.20  #endif // _include_jitcraft_pool_allocator_h_
     6.1 --- a/jitcraft/SemanticAnalysis.cpp	Sat Jul 07 20:28:29 2012 -0700
     6.2 +++ b/jitcraft/SemanticAnalysis.cpp	Sat Jul 07 20:53:10 2012 -0700
     6.3 @@ -402,19 +402,19 @@
     6.4                  return;
     6.5          }
     6.6  
     6.7 -        if (node->postDimensions()->size() > Type::MAX_UNSAFE_ARRAY_DEPTH) {
     6.8 +        if (node->postDimensions()->length() > Type::MAX_UNSAFE_ARRAY_DEPTH) {
     6.9              cc_.reportError(node->pos(), Message_MaximumArrayDepth);
    6.10              return;
    6.11          }
    6.12  
    6.13          // If we have an initializer, make sure it's the same type.
    6.14 -        if (init && node->postDimensions()->size() != init->levels()) {
    6.15 +        if (init && node->postDimensions()->length() != init->levels()) {
    6.16              cc_.reportError(node->pos(), Message_ArrayInitializerDepth);
    6.17              return;
    6.18          }
    6.19  
    6.20          Local<Type> check(zone_, init);
    6.21 -        for (size_t i = node->postDimensions()->size() - 1; i < node->postDimensions()->size(); i--) {
    6.22 +        for (size_t i = node->postDimensions()->length() - 1; i < node->postDimensions()->length(); i--) {
    6.23              Expression *expr = node->postDimensions()->at(i);
    6.24              int size = -1;
    6.25  
    6.26 @@ -684,12 +684,12 @@
    6.27          cc_.reportError(node->pos(), Message_CalleeNotFunction);
    6.28          return;
    6.29      }
    6.30 -    if (node->arguments()->size() != type->parameters()->length()) {
    6.31 +    if (node->arguments()->length() != type->parameters()->length()) {
    6.32          cc_.reportError(node->pos(), Message_ArgumentCountMismatch);
    6.33          return;
    6.34      }
    6.35  
    6.36 -    for (unsigned i = 0; i < node->arguments()->size(); i++) {
    6.37 +    for (unsigned i = 0; i < node->arguments()->length(); i++) {
    6.38          // Note: we don't compute references unless the callee needs a
    6.39          // reference type.
    6.40          Expression *expression = node->arguments()->at(i);
     7.1 --- a/jitcraft/Utility.h	Sat Jul 07 20:28:29 2012 -0700
     7.2 +++ b/jitcraft/Utility.h	Sat Jul 07 20:53:10 2012 -0700
     7.3 @@ -163,6 +163,8 @@
     7.4  static inline bool
     7.5  IsUint32AddSafe(unsigned a, unsigned b)
     7.6  {
     7.7 +    if (!a || !b)
     7.8 +        return true;
     7.9      unsigned log2_a = Log2(a);
    7.10      unsigned log2_b = Log2(b);
    7.11      return (log2_a < sizeof(unsigned) * 8) &&
    7.12 @@ -170,6 +172,17 @@
    7.13  }
    7.14  
    7.15  static inline bool
    7.16 +IsUintPtrAddSafe(size_t a, size_t b)
    7.17 +{
    7.18 +    if (!a || !b)
    7.19 +        return true;
    7.20 +    size_t log2_a = Log2(a);
    7.21 +    size_t log2_b = Log2(b);
    7.22 +    return (log2_a < sizeof(size_t) * 8) &&
    7.23 +           (log2_b < sizeof(size_t) * 8);
    7.24 +}
    7.25 +
    7.26 +static inline bool
    7.27  IsUint32MultiplySafe(unsigned a, unsigned b)
    7.28  {
    7.29      if (a <= 1 || b <= 1)
    7.30 @@ -180,6 +193,17 @@
    7.31      return log2_a + log2_b <= sizeof(unsigned) * 8;
    7.32  }
    7.33  
    7.34 +static inline bool
    7.35 +IsUintPtrMultiplySafe(size_t a, size_t b)
    7.36 +{
    7.37 +    if (a <= 1 || b <= 1)
    7.38 +        return true;
    7.39 +
    7.40 +    size_t log2_a = Log2(a);
    7.41 +    size_t log2_b = Log2(b);
    7.42 +    return log2_a + log2_b <= sizeof(size_t) * 8;
    7.43 +}
    7.44 +
    7.45  #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
    7.46  #define STATIC_ASSERT(cond) extern int static_assert_f(int a[(cond) ? 1 : -1])
    7.47  
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/jitcraft/Vector.h	Sat Jul 07 20:53:10 2012 -0700
     8.3 @@ -0,0 +1,110 @@
     8.4 +/* vim: set ts=4 sw=4 tw=99 et:
     8.5 + *
     8.6 + * Copyright (C) 2012 David Anderson
     8.7 + *
     8.8 + * This file is part of JITCraft.
     8.9 + *
    8.10 + * JITCraft is free software: you can redistribute it and/or modify it under
    8.11 + * the terms of the GNU General Public License as published by the Free
    8.12 + * Software Foundation, either version 3 of the License, or (at your option)
    8.13 + * any later version.
    8.14 + * 
    8.15 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    8.16 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    8.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    8.18 + *
    8.19 + * You should have received a copy of the GNU General Public License along with
    8.20 + * JITCraft. If not, see http://www.gnu.org/licenses/.
    8.21 + */
    8.22 +#ifndef _INCLUDE_KEIMA_TPL_CPP_VECTOR_H_
    8.23 +#define _INCLUDE_KEIMA_TPL_CPP_VECTOR_H_
    8.24 +
    8.25 +#include <new>
    8.26 +#include <stdlib.h>
    8.27 +#include "AllocatorPolicies.h"
    8.28 +#include "Utility.h"
    8.29 +
    8.30 +namespace jc {
    8.31 +
    8.32 +template <typename T, typename AllocPolicy = NormalAllocPolicy>
    8.33 +class Vector : public AllocPolicy
    8.34 +{
    8.35 +  public:
    8.36 +    Vector(AllocPolicy = AllocPolicy())
    8.37 +      : data(NULL),
    8.38 +        nitems(0),
    8.39 +        maxsize(0)
    8.40 +    { }
    8.41 +    ~Vector() {
    8.42 +        for (size_t i = 0; i < nitems; i++)
    8.43 +            data[i].~T();
    8.44 +        this->free(data);
    8.45 +    }
    8.46 +    bool append(const T& item) {
    8.47 +        if (!GrowIfNeeded(1))
    8.48 +            return false;
    8.49 +        new (&data[nitems]) T(item);
    8.50 +        nitems++;
    8.51 +        return true;
    8.52 +    }
    8.53 +    size_t length() const {
    8.54 +        return nitems;
    8.55 +    }
    8.56 +    T& at(size_t i) {
    8.57 +        assert(i < length());
    8.58 +        return data[i];
    8.59 +    }
    8.60 +    const T& at(size_t i) const {
    8.61 +        assert(i < length());
    8.62 +        return data[i];
    8.63 +    }
    8.64 +    T& operator [](size_t i) {
    8.65 +        return get(i);
    8.66 +    }
    8.67 +    const T& operator [](size_t i) const {
    8.68 +        return get(i);
    8.69 +    }
    8.70 +    void clear() {
    8.71 +        nitems = 0;
    8.72 +    }
    8.73 +
    8.74 +  private:
    8.75 +    bool GrowIfNeeded(size_t needed)
    8.76 +    {
    8.77 +        if (!IsUintPtrAddSafe(nitems, needed)) {
    8.78 +            this->reportAllocationOverflow();
    8.79 +            return false;
    8.80 +        }
    8.81 +        if (maxsize == 0)
    8.82 +            maxsize = 8;
    8.83 +        while (nitems + needed > maxsize)
    8.84 +        {
    8.85 +            if (!IsUintPtrMultiplySafe(maxsize, 2)) {
    8.86 +                this->reportAllocationOverflow();
    8.87 +                return false;
    8.88 +            }
    8.89 +            maxsize *= 2;
    8.90 +        }
    8.91 +        T* newdata = (T*)this->malloc(sizeof(T) * maxsize);
    8.92 +        if (newdata == NULL)
    8.93 +            return false;
    8.94 +        for (size_t i = 0; i < nitems; i++)
    8.95 +        {
    8.96 +            new (&newdata[i]) T(data[i]);
    8.97 +            data[i].~T();
    8.98 +        }
    8.99 +        this->free(data);
   8.100 +        data = newdata;
   8.101 +        return true;
   8.102 +    }
   8.103 +
   8.104 +  private:
   8.105 +    T* data;
   8.106 +    size_t nitems;
   8.107 +    size_t maxsize;
   8.108 +};
   8.109 +
   8.110 +}
   8.111 +
   8.112 +#endif /* _INCLUDE_KEIMA_TPL_CPP_VECTOR_H_ */
   8.113 +
     9.1 --- a/jitcraft/jitcraft.vcxproj	Sat Jul 07 20:28:29 2012 -0700
     9.2 +++ b/jitcraft/jitcraft.vcxproj	Sat Jul 07 20:53:10 2012 -0700
     9.3 @@ -141,6 +141,7 @@
     9.4      <ClInclude Include="StringPool.h" />
     9.5      <ClInclude Include="TypeManager.h" />
     9.6      <ClInclude Include="Types.h" />
     9.7 +    <ClInclude Include="Vector.h" />
     9.8      <ClInclude Include="Zone.h" />
     9.9      <ClInclude Include="Token.h" />
    9.10      <ClInclude Include="Utility.h" />