v8世界探險(3) - v8的抽象語法樹結構

v8世界探險(3) - v8的抽象語法樹結構

AST的結構

首先,我們還是先來看一下地圖:


v8 ast.png

基于Zone的內存分配

AST對象都是基于Zone進行內存管理的,Zone是多次分配臨時塊對象,然后可以一次性釋放掉。
我們看一下Zone的定義,在src/zone.h中:

// The Zone supports very fast allocation of small chunks of
// memory. The chunks cannot be deallocated individually, but instead
// the Zone supports deallocating all chunks in one fast
// operation. The Zone is used to hold temporary data structures like
// the abstract syntax tree, which is deallocated after compilation.
//
// Note: There is no need to initialize the Zone; the first time an
// allocation is attempted, a segment of memory will be requested
// through a call to malloc().
//
// Note: The implementation is inherently not thread safe. Do not use
// from multi-threaded code.
class Zone final {
 public:
  Zone();
  ~Zone();

  // Allocate 'size' bytes of memory in the Zone; expands the Zone by
  // allocating new segments of memory on demand using malloc().
  void* New(size_t size);

  template <typename T>
  T* NewArray(size_t length) {
    DCHECK_LT(length, std::numeric_limits<size_t>::max() / sizeof(T));
    return static_cast<T*>(New(length * sizeof(T)));
  }

  // Deletes all objects and free all memory allocated in the Zone. Keeps one
  // small (size <= kMaximumKeptSegmentSize) segment around if it finds one.
  void DeleteAll();

  // Deletes the last small segment kept around by DeleteAll(). You
  // may no longer allocate in the Zone after a call to this method.
  void DeleteKeptSegment();

  // Returns true if more memory has been allocated in zones than
  // the limit allows.
  bool excess_allocation() const {
    return segment_bytes_allocated_ > kExcessLimit;
  }

  size_t allocation_size() const { return allocation_size_; }

 private:
  // All pointers returned from New() have this alignment.  In addition, if the
  // object being allocated has a size that is divisible by 8 then its alignment
  // will be 8. ASan requires 8-byte alignment.
#ifdef V8_USE_ADDRESS_SANITIZER
  static const size_t kAlignment = 8;
  STATIC_ASSERT(kPointerSize <= 8);
#else
  static const size_t kAlignment = kPointerSize;
#endif

  // Never allocate segments smaller than this size in bytes.
  static const size_t kMinimumSegmentSize = 8 * KB;

  // Never allocate segments larger than this size in bytes.
  static const size_t kMaximumSegmentSize = 1 * MB;

  // Never keep segments larger than this size in bytes around.
  static const size_t kMaximumKeptSegmentSize = 64 * KB;

  // Report zone excess when allocation exceeds this limit.
  static const size_t kExcessLimit = 256 * MB;

  // The number of bytes allocated in this zone so far.
  size_t allocation_size_;

  // The number of bytes allocated in segments.  Note that this number
  // includes memory allocated from the OS but not yet allocated from
  // the zone.
  size_t segment_bytes_allocated_;

  // Expand the Zone to hold at least 'size' more bytes and allocate
  // the bytes. Returns the address of the newly allocated chunk of
  // memory in the Zone. Should only be called if there isn't enough
  // room in the Zone already.
  Address NewExpand(size_t size);

  // Creates a new segment, sets it size, and pushes it to the front
  // of the segment chain. Returns the new segment.
  inline Segment* NewSegment(size_t size);

  // Deletes the given segment. Does not touch the segment chain.
  inline void DeleteSegment(Segment* segment, size_t size);

  // The free region in the current (front) segment is represented as
  // the half-open interval [position, limit). The 'position' variable
  // is guaranteed to be aligned as dictated by kAlignment.
  Address position_;
  Address limit_;

  Segment* segment_head_;
};

ZoneObject

基于Zone分配,v8封裝了ZoneObject來作為AST對象的基類。

// ZoneObject is an abstraction that helps define classes of objects
// allocated in the Zone. Use it as a base class; see ast.h.
class ZoneObject {
 public:
  // Allocate a new ZoneObject of 'size' bytes in the Zone.
  void* operator new(size_t size, Zone* zone) { return zone->New(size); }

  // Ideally, the delete operator should be private instead of
  // public, but unfortunately the compiler sometimes synthesizes
  // (unused) destructors for classes derived from ZoneObject, which
  // require the operator to be visible. MSVC requires the delete
  // operator to be public.

  // ZoneObjects should never be deleted individually; use
  // Zone::DeleteAll() to delete all zone objects in one go.
  void operator delete(void*, size_t) { UNREACHABLE(); }
  void operator delete(void* pointer, Zone* zone) { UNREACHABLE(); }
};

AstNode

AstNode繼承自ZoneObject,是所有的語句、表達式和聲明的基類。

class AstNode: public ZoneObject {
 public:
#define DECLARE_TYPE_ENUM(type) k##type,
  enum NodeType {
    AST_NODE_LIST(DECLARE_TYPE_ENUM)
    kInvalid = -1
  };
#undef DECLARE_TYPE_ENUM

  void* operator new(size_t size, Zone* zone) { return zone->New(size); }

  explicit AstNode(int position): position_(position) {}
  virtual ~AstNode() {}

  virtual void Accept(AstVisitor* v) = 0;
  virtual NodeType node_type() const = 0;
  int position() const { return position_; }

  // Type testing & conversion functions overridden by concrete subclasses.
#define DECLARE_NODE_FUNCTIONS(type) \
  bool Is##type() const { return node_type() == AstNode::k##type; } \
  type* As##type() { \
    return Is##type() ? reinterpret_cast<type*>(this) : NULL; \
  } \
  const type* As##type() const { \
    return Is##type() ? reinterpret_cast<const type*>(this) : NULL; \
  }
  AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
#undef DECLARE_NODE_FUNCTIONS

  virtual BreakableStatement* AsBreakableStatement() { return NULL; }
  virtual IterationStatement* AsIterationStatement() { return NULL; }
  virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; }

  // The interface for feedback slots, with default no-op implementations for
  // node types which don't actually have this. Note that this is conceptually
  // not really nice, but multiple inheritance would introduce yet another
  // vtable entry per node, something we don't want for space reasons.
  virtual void AssignFeedbackVectorSlots(Isolate* isolate,
                                         FeedbackVectorSpec* spec,
                                         FeedbackVectorSlotCache* cache) {}

 private:
  // Hidden to prevent accidental usage. It would have to load the
  // current zone from the TLS.
  void* operator new(size_t size);

  friend class CaseClause;  // Generates AST IDs.

  int position_;
};

AstNode的子類有4個大類:

  • Statement: 語句
  • Expression: 表達式
  • Declaration: 聲明
  • Module: ES6新增的模塊

我們來一張AstNode的圖,大家加深一下印象:


v8 AstNode

聲明

Declaration是AstNode4大類中最簡單的,它只有四個直接子類:

  • VariableDeclaration: 變量聲明
  • FunctionDeclaration: 函數聲明
  • ImportDeclaration: 引用其它模塊聲明
  • ExportDeclaration: 導出聲明
class Declaration : public AstNode {
 public:
  VariableProxy* proxy() const { return proxy_; }
  VariableMode mode() const { return mode_; }
  Scope* scope() const { return scope_; }
  virtual InitializationFlag initialization() const = 0;
  virtual bool IsInlineable() const;

 protected:
  Declaration(Zone* zone, VariableProxy* proxy, VariableMode mode, Scope* scope,
              int pos)
      : AstNode(pos), mode_(mode), proxy_(proxy), scope_(scope) {
    DCHECK(IsDeclaredVariableMode(mode));
  }

 private:
  VariableMode mode_;
  VariableProxy* proxy_;

  // Nested scope from which the declaration originated.
  Scope* scope_;
};
變量聲明
class VariableDeclaration final : public Declaration {
 public:
  DECLARE_NODE_TYPE(VariableDeclaration)

  InitializationFlag initialization() const override {
    return mode() == VAR ? kCreatedInitialized : kNeedsInitialization;
  }

  bool is_class_declaration() const { return is_class_declaration_; }

...

  int declaration_group_start() const { return declaration_group_start_; }

 protected:
  VariableDeclaration(Zone* zone, VariableProxy* proxy, VariableMode mode,
                      Scope* scope, int pos, bool is_class_declaration = false,
                      int declaration_group_start = -1)
      : Declaration(zone, proxy, mode, scope, pos),
        is_class_declaration_(is_class_declaration),
        declaration_group_start_(declaration_group_start) {}

  bool is_class_declaration_;
  int declaration_group_start_;
};
函數聲明

函數聲明的最主要結構就是有一個FunctionLiteral函數文本的指針。

class FunctionDeclaration final : public Declaration {
 public:
  DECLARE_NODE_TYPE(FunctionDeclaration)

  FunctionLiteral* fun() const { return fun_; }
  void set_fun(FunctionLiteral* f) { fun_ = f; }
  InitializationFlag initialization() const override {
    return kCreatedInitialized;
  }
  bool IsInlineable() const override;

 protected:
  FunctionDeclaration(Zone* zone,
                      VariableProxy* proxy,
                      VariableMode mode,
                      FunctionLiteral* fun,
                      Scope* scope,
                      int pos)
      : Declaration(zone, proxy, mode, scope, pos),
        fun_(fun) {
    DCHECK(mode == VAR || mode == LET || mode == CONST);
    DCHECK(fun != NULL);
  }

 private:
  FunctionLiteral* fun_;
};
引用模塊聲明

引用的模塊名,保存在兩個AstRawString指針中。

class ImportDeclaration final : public Declaration {
 public:
  DECLARE_NODE_TYPE(ImportDeclaration)

  const AstRawString* import_name() const { return import_name_; }
  const AstRawString* module_specifier() const { return module_specifier_; }
  void set_module_specifier(const AstRawString* module_specifier) {
    DCHECK(module_specifier_ == NULL);
    module_specifier_ = module_specifier;
  }
  InitializationFlag initialization() const override {
    return kNeedsInitialization;
  }

 protected:
  ImportDeclaration(Zone* zone, VariableProxy* proxy,
                    const AstRawString* import_name,
                    const AstRawString* module_specifier, Scope* scope, int pos)
      : Declaration(zone, proxy, IMPORT, scope, pos),
        import_name_(import_name),
        module_specifier_(module_specifier) {}

 private:
  const AstRawString* import_name_;
  const AstRawString* module_specifier_;
};
導出聲明

導出聲明是ES6引入的Module的功能,可以導出變量,也可以導出函數,例:

//導出常量
export const sqrt = Math.sqrt;
//導出函數
export function square(x) {
    return x * x;
}

導出聲明的類定義如下:

class ExportDeclaration final : public Declaration {
 public:
  DECLARE_NODE_TYPE(ExportDeclaration)

  InitializationFlag initialization() const override {
    return kCreatedInitialized;
  }

 protected:
  ExportDeclaration(Zone* zone, VariableProxy* proxy, Scope* scope, int pos)
      : Declaration(zone, proxy, LET, scope, pos) {}
};

這其中通過一個宏DECLARE_NODE_TYPE來輸出導出的類型. 不禁讓人聯想起了MFC中著名的DECLARE_MESSAGE_MAP宏,不知道v8這部分的作者是不是有MFC的經歷,哈哈

#define DECLARE_NODE_TYPE(type)                                          \
  void Accept(AstVisitor* v) override;                                   \
  AstNode::NodeType node_type() const final { return AstNode::k##type; } \
  friend class AstNodeFactory;

Statement

Statement表示語句。畢竟JavaScript還是語句式為主的,表達式是為語句服務的,所以Statement對應了js程序的每一個基本執行元素。

class Statement : public AstNode {
 public:
  explicit Statement(Zone* zone, int position) : AstNode(position) {}

  bool IsEmpty() { return AsEmptyStatement() != NULL; }
  virtual bool IsJump() const { return false; }
  virtual void MarkTail() {}
};

語句有對于流程的控制,所以定義了IsJump和MarkTail兩個函數。

IsJump用于控制是否是跳轉型的指令。JumpStatement就是專為跳轉而生的,所以JumpStatement的定義就一條有用的,IsJump返回true:

class JumpStatement : public Statement {
 public:
  bool IsJump() const final { return true; }

 protected:
  explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
};

MarkTail是用來標識語句結束的,比如我們看看Block的MarkTail的實現:

  void MarkTail() override {
    if (!statements_.is_empty()) statements_.last()->MarkTail();
  }

如果語句列表不為空,那么語句列表中最后一條就標識為尾巴。

語句的定義很簡單,下面的子類卻不少:

  • BreakableStatement: 所有流程中可以退出和跳轉的語句
    • Block:塊語句當然是BreakableStatement,一個塊也是流程的控制結構
    • IterationStatement: 循環語句是可中斷的啊,有兩種中斷方法:break和continue
      • DoWhileStatement: do while循環語句
      • WhileStatement: while循環語句
      • ForStatement: for循環語句
      • ForEachStatement: for each型循環,適用于集合遍歷的循環形式
        • ForInStatement: for in循環語句
        • ForOfStatement: ES6新增,使用迭代器的for of循環
      • SwitchStatement: switch語句
  • ExpressionStatement: 表達式語句,整條語句由一條表達式構成
  • JumpStatement: 流程跳轉型指令
    • ContinueStatement: continue語句
    • BreakStatement: break語句
    • ReturnStatement: return語句
  • WithStatement: with語句
  • IfStatement: if語句
  • TryStatement: try語句
    • TryCatchStatement: try catch語句
    • TryFinallyStatement: try finally語句
  • DebuggerStatement: 調試用途,我暫時也不知道它是做什么的
  • EmptyStatement: 空語句
  • SloppyBlockFunctionStatement: ES2016 Annex B3.3定義的可被覆蓋的其它語句的代理

我們也畫一張圖來加深一下對于Statement的印象:


Statement.png

IterationStatement

循環類語句的特點是要支持continue語句的落腳點,就是要記錄,執行continue的時候該回到哪里。
break就不用考慮了,跳到結尾就好了么。

class IterationStatement : public BreakableStatement {
 public:
  // Type testing & conversion.
  IterationStatement* AsIterationStatement() final { return this; }

  Statement* body() const { return body_; }
  void set_body(Statement* s) { body_ = s; }

  static int num_ids() { return parent_num_ids() + 1; }
  BailoutId OsrEntryId() const { return BailoutId(local_id(0)); }
  virtual BailoutId ContinueId() const = 0;
  virtual BailoutId StackCheckId() const = 0;

  // Code generation
  Label* continue_target()  { return &continue_target_; }

 protected:
  IterationStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
      : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
        body_(NULL) {}
  static int parent_num_ids() { return BreakableStatement::num_ids(); }
  void Initialize(Statement* body) { body_ = body; }

 private:
  int local_id(int n) const { return base_id() + parent_num_ids() + n; }

  Statement* body_;
  Label continue_target_;
};
DoWhileStatement

DoWhileStatement比普通的IterationStatement多了一個while時的表達式。

class DoWhileStatement final : public IterationStatement {
 public:
  DECLARE_NODE_TYPE(DoWhileStatement)

  void Initialize(Expression* cond, Statement* body) {
    IterationStatement::Initialize(body);
    cond_ = cond;
  }

  Expression* cond() const { return cond_; }
  void set_cond(Expression* e) { cond_ = e; }

  static int num_ids() { return parent_num_ids() + 2; }
  BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
  BailoutId StackCheckId() const override { return BackEdgeId(); }
  BailoutId BackEdgeId() const { return BailoutId(local_id(1)); }

 protected:
  DoWhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
      : IterationStatement(zone, labels, pos), cond_(NULL) {}
  static int parent_num_ids() { return IterationStatement::num_ids(); }

 private:
  int local_id(int n) const { return base_id() + parent_num_ids() + n; }

  Expression* cond_;
};
WhileStatement

WhileStatement對應了while循環,其實除了表達式的判斷位置不同,它與DoWhileStatement的結構是基本一樣的:

class WhileStatement final : public IterationStatement {
 public:
  DECLARE_NODE_TYPE(WhileStatement)

  void Initialize(Expression* cond, Statement* body) {
    IterationStatement::Initialize(body);
    cond_ = cond;
  }

  Expression* cond() const { return cond_; }
  void set_cond(Expression* e) { cond_ = e; }

  static int num_ids() { return parent_num_ids() + 1; }
  BailoutId ContinueId() const override { return EntryId(); }
  BailoutId StackCheckId() const override { return BodyId(); }
  BailoutId BodyId() const { return BailoutId(local_id(0)); }

 protected:
  WhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
      : IterationStatement(zone, labels, pos), cond_(NULL) {}
  static int parent_num_ids() { return IterationStatement::num_ids(); }

 private:
  int local_id(int n) const { return base_id() + parent_num_ids() + n; }

  Expression* cond_;
};
ForStatement

前面大家已經被代碼轟炸得差不多了,下面就不重復貼完整代碼,只貼干貨。
for循環的特點是有三個表達式,分別對應:初始條件,結束條件和下一個的處理三種操作,對應到代碼中是這樣的:

  Statement* init_;
  Expression* cond_;
  Statement* next_;

IfStatement

if語句有兩個分支,所以有兩個尾巴:

  void MarkTail() override {
    then_statement_->MarkTail();
    else_statement_->MarkTail();
  }

if有一個條件判斷,外加then和else兩個執行塊:

  Expression* condition_;
  Statement* then_statement_;
  Statement* else_statement_;

Expression

表達式解釋一向都是語法分析的重點,后面我們再詳細展開介紹,這里我們先看下表達式分類圖:


v8 expression.png

表達式包括對于對象、類、函數、數組、正則表達式等字面量的表示,一元,二元,比較等運算等操作。

針對于字面和表達式,v8還提供了AstVisitor工具類集來幫助訪問和修改。

其它

像變量、AstString等組件并不屬于AstNode,而是直接從ZoneObject派生出來的。后面用到的時候我們再詳細介紹。

小結

v8的語法分析,最終會生成一棵抽象語法樹AST。這些聲明、語句、表達式和模塊都以AstNode的形式來保存。
AstNode和變量,AstString等對象都是基于Zone方式多次分配,一次性回收來進行內存管理的。
Statement是語句,主要對應分支、循環、跳轉、異常處理等流程控制上的操作。
Expression是表達式,構成了語句的組成部分,相對比較復雜。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容